Making WordPress.org


Ignore:
Timestamp:
05/16/2024 02:34:14 AM (2 years ago)
Author:
dd32
Message:

Plugin Directory: Cron: Refactor how we hook the colon-based jobs, such as import_plugin:example-slug such that the cron task will be executed when run manually through wp-cli.

File:
1 edited

Legend:

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

    r12681 r13710  
    22namespace WordPressdotorg\Plugin_Directory\Jobs;
    33use WordPressdotorg\Plugin_Directory\Tools;
     4use const \WP_CLI;
    45
    56/**
     
    3132
    3233        // Register the wildcard cron hook tasks.
    33         if ( defined( 'DOING_CRON' ) && DOING_CRON ) {
     34        if ( wp_doing_cron() || ( defined( 'WP_CLI' ) && WP_CLI ) ) {
    3435            // This must be run after plugins_loaded, as that's when Cavalcade hooks in.
    3536            add_action( 'init', array( $this, 'register_colon_based_hook_handlers' ) );
     
    295296        );
    296297
     298        // Add the wildcard cron task above to the specified colon-based hook.
     299        $add_callback = static function( $hook ) use( $wildcard_cron_tasks ) {
     300            if ( ! str_contains( $hook, ':' ) ) {
     301                return;
     302            }
     303
     304            list( $partial_hook, $slug ) = explode( ':', $hook, 2 );
     305            $callback                    = $wildcard_cron_tasks[ $partial_hook ] ?? false;
     306
     307            if ( ! $callback ) {
     308                return;
     309            }
     310
     311            if ( ! has_action( $hook, $callback ) ) {
     312                add_action( $hook, $callback, 10, PHP_INT_MAX );
     313            }
     314        };
     315
    297316        if ( is_array( $cron_array ) ) {
    298317            foreach ( $cron_array as $timestamp => $handlers ) {
     
    302321
    303322                foreach ( $handlers as $hook => $jobs ) {
    304                     $pos = strpos( $hook, ':' );
    305                     if ( ! $pos ) {
    306                         continue;
    307                     }
    308 
    309                     $partial_hook = substr( $hook, 0, $pos );
    310 
    311                     if ( isset( $wildcard_cron_tasks[ $partial_hook ] ) ) {
    312                         add_action( $hook, $wildcard_cron_tasks[ $partial_hook ], 10, PHP_INT_MAX );
    313                     }
     323                    $add_callback( $hook );
     324                }
     325            }
     326        }
     327
     328        /*
     329         * When jobs are run manually or after-the-fact, we need to find the current job first.
     330         *
     331         * The `CAVALCADE_JOB_ID` constant exists inside Cavalcade, which WordPress.org uses for cron,
     332         * but the constant is only set just before the cron task fires, and is not available at the
     333         * time that this code executes.
     334         *
     335         * We can get the job hook via the job id, either through `$job_id` global that our loader sets,
     336         * or through the WP CLI arguments.
     337         */
     338        if ( wp_doing_cron() || ( defined( 'WP_CLI' ) && WP_CLI ) ) {
     339            // The WordPress.org cavalcade loader sets the $job_id variable.
     340            $job_id = $GLOBALS['job_id'] ?? false;
     341
     342            // Try to get it from the CLI args. `wp cavalcade run 12345`
     343            if ( ! $job_id && in_array( 'run', $GLOBALS['argv'] ) ) {
     344                $job_id = $GLOBALS['argv'][ array_search( 'run', $GLOBALS['argv'] ) + 1 ] ?? false;
     345            }
     346
     347            if ( $job_id && class_exists( '\HM\Cavalcade\Plugin\Job' ) ) {
     348                $job = \HM\Cavalcade\Plugin\Job::get( $job_id );
     349                if ( $job ) {
     350                    $add_callback( $job->hook );
    314351                }
    315352            }
Note: See TracChangeset for help on using the changeset viewer.