Changeset 14264
- Timestamp:
- 12/11/2024 07:38:03 AM (18 months ago)
- Location:
- sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory
- Files:
-
- 1 added
- 2 edited
-
admin/class-customizations.php (modified) (1 diff)
-
admin/metabox/class-cron-logs.php (added)
-
jobs/class-manager.php (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/admin/class-customizations.php
r13793 r14264 777 777 } 778 778 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 779 789 // Remove unnecessary metaboxes. 780 790 remove_meta_box( 'commentsdiv', 'plugin', 'normal' ); -
sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/jobs/class-manager.php
r14126 r14264 10 10 */ 11 11 class 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 ); 12 26 13 27 /** … … 291 305 $cron_array = get_option( 'cron' ); 292 306 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 299 307 // 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 ) { 301 309 if ( ! str_contains( $hook, ':' ) ) { 302 310 return; 303 311 } 304 312 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; 307 315 308 316 if ( ! $callback ) { … … 365 373 } 366 374 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 } 367 443 } 368 444
Note: See TracChangeset
for help on using the changeset viewer.