diff --git a/includes/WpMinions/Cron/Client.php b/includes/WpMinions/Cron/Client.php index 76df989..2f628b8 100644 --- a/includes/WpMinions/Cron/Client.php +++ b/includes/WpMinions/Cron/Client.php @@ -30,9 +30,31 @@ public function register() { */ public function add( $hook, $args = array(), $priority = 'normal' ) { // Priority isn't really something we can manage with wp-cron - wp_schedule_single_event( time(), $hook, $args ); + $job_data = array( + 'hook' => $hook, + 'args' => $args, + 'blog_id' => get_current_blog_id(), + ); + + $job_data = apply_filters( 'wp_async_task_add_job_data', $job_data ); + + if ( function_exists( 'is_multisite' ) && is_multisite() && $job_data['blog_id'] ) { + $blog_id = $job_data['blog_id']; + + if ( get_current_blog_id() !== $blog_id ) { + switch_to_blog( $blog_id ); + $switched = true; + } else { + $switched = false; + } + } + + wp_schedule_single_event( time(), $job_data['hook'], $job_data['args'] ); + + if ( $switched ) { + restore_current_blog(); + } return true; } - } diff --git a/includes/WpMinions/Gearman/Client.php b/includes/WpMinions/Gearman/Client.php index b769261..c4b8b89 100644 --- a/includes/WpMinions/Gearman/Client.php +++ b/includes/WpMinions/Gearman/Client.php @@ -45,7 +45,7 @@ public function register() { if ( ! defined( 'PHPUNIT_RUNNER' ) ) { error_log( "Fatal Gearman Error: Failed to register servers ($servers)" ); - error_log( " Cause: " . $e->getMessage() ); + error_log( ' Cause: ' . $e->getMessage() ); } return false; @@ -70,6 +70,8 @@ public function add( $hook, $args = array(), $priority = 'normal' ) { 'blog_id' => $this->get_blog_id(), ); + $job_data = apply_filters( 'wp_async_task_add_job_data', $job_data ); + $client = $this->get_gearman_client(); if ( $client !== false ) { @@ -80,6 +82,7 @@ public function add( $hook, $args = array(), $priority = 'normal' ) { return call_user_func( $callable, $group, $payload ); } else { + error_log( 'Client fail' ); return false; } } @@ -93,7 +96,7 @@ public function add( $hook, $args = array(), $priority = 'normal' ) { * @return string The corresponding method name */ function get_background_method( $priority ) { - switch( strtolower( $priority ) ) { + switch ( strtolower( $priority ) ) { case 'high': $method = 'doHighBackground'; break; @@ -178,5 +181,4 @@ function get_gearman_client() { function get_blog_id() { return function_exists( 'is_multisite' ) && is_multisite() ? get_current_blog_id() : false; } - } diff --git a/includes/WpMinions/Gearman/Worker.php b/includes/WpMinions/Gearman/Worker.php index 75ece33..1e6ff28 100644 --- a/includes/WpMinions/Gearman/Worker.php +++ b/includes/WpMinions/Gearman/Worker.php @@ -49,7 +49,7 @@ public function register() { if ( ! defined( 'PHPUNIT_RUNNER' ) ) { error_log( "Fatal Gearman Error: Failed to register servers ($servers)" ); - error_log( " Cause: " . $e->getMessage() ); + error_log( ' Cause: ' . $e->getMessage() ); } return false; diff --git a/includes/WpMinions/Plugin.php b/includes/WpMinions/Plugin.php index f0c25bd..6857149 100644 --- a/includes/WpMinions/Plugin.php +++ b/includes/WpMinions/Plugin.php @@ -316,7 +316,7 @@ function quit( $status_code ) { * @return string Path to the wp-load.php file */ function get_wp_load() { - $wp_dir = dirname( $_SERVER['SCRIPT_FILENAME'] ) ; + $wp_dir = dirname( $_SERVER['SCRIPT_FILENAME'] ); return $wp_dir . '/wp-load.php'; } @@ -338,5 +338,4 @@ function load_wordpress() { return $this->quit( 1 ); } } - } diff --git a/includes/WpMinions/RabbitMQ/Client.php b/includes/WpMinions/RabbitMQ/Client.php index 7c9fd3a..aa137ee 100644 --- a/includes/WpMinions/RabbitMQ/Client.php +++ b/includes/WpMinions/RabbitMQ/Client.php @@ -62,8 +62,10 @@ public function add( $hook, $args = array(), $priority = 'normal' ) { 'blog_id' => get_current_blog_id(), ); + $job_data = apply_filters( 'wp_async_task_add_job_data', $job_data ); + $message = new \PhpAmqpLib\Message\AMQPMessage( - json_encode( $job_data ) ); + json_encode( $job_data ) ); $this->connection->get_channel()->basic_publish( $message, '', 'wordpress' ); }