Making WordPress.org

Changeset 14264


Ignore:
Timestamp:
12/11/2024 07:38:03 AM (18 months ago)
Author:
dd32
Message:

Plugin Directory: Admin: Add a cronjob metabox for highly-trusted users, to ease debugging plugin imports.

This allows those users to see the output from the cron jobs that are related to the plugin.

This is only shown to super admins and in non-production environments for now.

Location:
sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/admin/class-customizations.php

    r13793 r14264  
    777777        }
    778778
     779        // For highly trusted users, add a cron-jobs metabox for debugging plugin imports.
     780        if ( is_super_admin() || 'production' !== wp_get_environment_type() ) {
     781            add_meta_box(
     782                'cron-logs',
     783                'Cron Job Logs',
     784                array( __NAMESPACE__ . '\Metabox\Cron_Logs', 'display' ),
     785                'plugin', 'normal', 'low'
     786            );
     787        }
     788
    779789        // Remove unnecessary metaboxes.
    780790        remove_meta_box( 'commentsdiv', 'plugin', 'normal' );
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/jobs/class-manager.php

    r14126 r14264  
    1010 */
    1111class Manager {
     12
     13    /**
     14     * The cron tasks that are triggered by a colon-based hook.
     15     *
     16     * @see Manager::register_colon_based_hook_handlers()
     17     * @static
     18     * @var array
     19     */
     20    public static $wildcard_cron_tasks = array(
     21        'import_plugin'      => array( __NAMESPACE__ . '\Plugin_Import', 'cron_trigger' ),
     22        'import_plugin_i18n' => array( __NAMESPACE__ . '\Plugin_i18n_Import', 'cron_trigger' ),
     23        'import_zip'         => array( __NAMESPACE__ . '\Plugin_ZIP_Import', 'cron_trigger' ),
     24        'tide_sync'          => array( __NAMESPACE__ . '\Tide_Sync', 'cron_trigger' ),
     25    );
    1226
    1327    /**
     
    291305        $cron_array = get_option( 'cron' );
    292306
    293         $wildcard_cron_tasks = array(
    294             'import_plugin'      => array( __NAMESPACE__ . '\Plugin_Import', 'cron_trigger' ),
    295             'import_plugin_i18n' => array( __NAMESPACE__ . '\Plugin_i18n_Import', 'cron_trigger' ),
    296             'tide_sync'          => array( __NAMESPACE__ . '\Tide_Sync', 'cron_trigger' ),
    297         );
    298 
    299307        // Add the wildcard cron task above to the specified colon-based hook.
    300         $add_callback = static function( $hook ) use( $wildcard_cron_tasks ) {
     308        $add_callback = static function( $hook ) {
    301309            if ( ! str_contains( $hook, ':' ) ) {
    302310                return;
    303311            }
    304312
    305             list( $partial_hook, $slug ) = explode( ':', $hook, 2 );
    306             $callback                    = $wildcard_cron_tasks[ $partial_hook ] ?? false;
     313            $partial_hook = explode( ':', $hook )[0];
     314            $callback     = self::$wildcard_cron_tasks[ $partial_hook ] ?? false;
    307315
    308316            if ( ! $callback ) {
     
    365373    }
    366374
     375    /**
     376     * Fetch all the cron jobs for a plugin.
     377     *
     378     * @static
     379     *
     380     * @param \WP_Post $plugin    The plugin post object.
     381     * @param array    $args      Additional arguments to filter the jobs. See \HM\Cavalcade\Plugin\Job::get_jobs_by_query() for more details.
     382     * @param bool     $with_logs Whether to fetch logs for the jobs.
     383     * @return array
     384     */
     385    public static function get_plugin_cron_jobs( \WP_Post $plugin, $args = [], $with_logs = false ) {
     386        global $wpdb;
     387
     388        if ( ! class_exists( '\HM\Cavalcade\Plugin\Job' ) ) {
     389            return [];
     390        }
     391
     392        $args['statuses'] ??= [ 'waiting', 'running', 'completed', 'failed', 'cancelled' ];
     393        $args['limit']    ??= 20;
     394        $args['args']     ??= null; // All jobs, regardless of args.
     395
     396        $jobs = [];
     397        foreach ( self::$wildcard_cron_tasks as $job_prefix => $callback ) {
     398            $args['hook'] = $job_prefix . ':' . $plugin->post_name;
     399            $jobs = array_merge(
     400                $jobs,
     401                \HM\Cavalcade\Plugin\Job::get_jobs_by_query( $args )
     402            );
     403        }
     404
     405        // Fetch logs for the tasks.
     406        if ( $with_logs ) {
     407            $log_table = str_replace( 'jobs', 'logs', \HM\Cavalcade\Plugin\Job::get_table() );
     408            foreach ( $jobs as &$job ) {
     409                // Fetch logs for the task.
     410                $job->logs = $wpdb->get_results(
     411                    $wpdb->prepare(
     412                        "SELECT status, timestamp, content FROM %i WHERE job = %d ORDER BY id DESC LIMIT 20",
     413                        $log_table,
     414                        $job->id
     415                    )
     416                );
     417                // Decode the JSON content.
     418                array_walk( $job->logs, static function( &$log ) {
     419                    $log->content = json_decode( $log->content, true ) ?: $log->content;
     420                } );
     421            }
     422        }
     423
     424        // Sort jobs based on last run.
     425        usort( $jobs, static function( $a, $b ) {
     426            $a_last_log = 0;
     427            $b_last_log = 0;
     428            if ( $a->logs ) {
     429                $a_last_log = max( array_map( 'strtotime', wp_list_pluck( $a->logs, 'timestamp' ) ) );
     430            }
     431            if ( $b->logs ) {
     432                $b_last_log = max( array_map( 'strtotime', wp_list_pluck( $b->logs, 'timestamp' ) ) );
     433            }
     434
     435            $a_last_log = max( $a->start, $a_last_log );
     436            $b_last_log = max( $b->start, $b_last_log );
     437
     438            return $a_last_log <=> $b_last_log;
     439        } );
     440
     441        return $jobs;
     442    }
    367443}
    368444
Note: See TracChangeset for help on using the changeset viewer.