@@ -17,71 +17,71 @@ |
||
| 17 | 17 | * @package Wordlift\Mappings\Validators |
| 18 | 18 | */ |
| 19 | 19 | class Post_Type_Rule_Validator implements Rule_Validator { |
| 20 | - /** |
|
| 21 | - * @since 3.25.0 |
|
| 22 | - * Enum for the post type rule validator. |
|
| 23 | - */ |
|
| 24 | - const POST_TYPE = 'post_type'; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * Post_Type_Rule_Validator constructor. |
|
| 28 | - * |
|
| 29 | - * When initializing the class hooks to `wl_mappings_rule_validators`. |
|
| 30 | - */ |
|
| 31 | - public function __construct() { |
|
| 32 | - |
|
| 33 | - add_filter( 'wl_mappings_rule_validators', array( $this, 'wl_mappings_rule_validators' ) ); |
|
| 34 | - |
|
| 35 | - } |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * Hook to `wl_mappings_rule_validators` to register ourselves. |
|
| 39 | - * |
|
| 40 | - * @param array $value An array with validators. |
|
| 41 | - * |
|
| 42 | - * @return array An array with validators plus ours. |
|
| 43 | - */ |
|
| 44 | - public function wl_mappings_rule_validators( $value ) { |
|
| 45 | - |
|
| 46 | - $value[ self::POST_TYPE ] = $this; |
|
| 47 | - |
|
| 48 | - return $value; |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * {@inheritdoc} |
|
| 53 | - */ |
|
| 54 | - public function get_label() { |
|
| 55 | - return __( 'Post Type', 'wordlift' ); |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * {@inheritdoc} |
|
| 60 | - */ |
|
| 61 | - public function is_valid( $identifier, $operator, $operand_1, $operand_2, $type ) { |
|
| 62 | - /** |
|
| 63 | - * If this is not done then this will validate for term |
|
| 64 | - * which causes a bug, so this rule validator would return false |
|
| 65 | - * when the current thing is not a post. |
|
| 66 | - */ |
|
| 67 | - if ( $type !== Jsonld_Converter::POST ) { |
|
| 68 | - return false; |
|
| 69 | - } |
|
| 70 | - // Get the post type and then check whether it matches or not according to the operator. |
|
| 71 | - $post_type = get_post_type( $identifier ); |
|
| 72 | - |
|
| 73 | - switch ( $operator ) { |
|
| 74 | - case Rule_Validator::IS_NOT_EQUAL_TO: |
|
| 75 | - return $post_type !== $operand_2; |
|
| 76 | - |
|
| 77 | - case Rule_Validator::IS_EQUAL_TO: |
|
| 78 | - return $post_type === $operand_2; |
|
| 79 | - |
|
| 80 | - default: |
|
| 81 | - |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - return false; |
|
| 85 | - } |
|
| 20 | + /** |
|
| 21 | + * @since 3.25.0 |
|
| 22 | + * Enum for the post type rule validator. |
|
| 23 | + */ |
|
| 24 | + const POST_TYPE = 'post_type'; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * Post_Type_Rule_Validator constructor. |
|
| 28 | + * |
|
| 29 | + * When initializing the class hooks to `wl_mappings_rule_validators`. |
|
| 30 | + */ |
|
| 31 | + public function __construct() { |
|
| 32 | + |
|
| 33 | + add_filter( 'wl_mappings_rule_validators', array( $this, 'wl_mappings_rule_validators' ) ); |
|
| 34 | + |
|
| 35 | + } |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * Hook to `wl_mappings_rule_validators` to register ourselves. |
|
| 39 | + * |
|
| 40 | + * @param array $value An array with validators. |
|
| 41 | + * |
|
| 42 | + * @return array An array with validators plus ours. |
|
| 43 | + */ |
|
| 44 | + public function wl_mappings_rule_validators( $value ) { |
|
| 45 | + |
|
| 46 | + $value[ self::POST_TYPE ] = $this; |
|
| 47 | + |
|
| 48 | + return $value; |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * {@inheritdoc} |
|
| 53 | + */ |
|
| 54 | + public function get_label() { |
|
| 55 | + return __( 'Post Type', 'wordlift' ); |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * {@inheritdoc} |
|
| 60 | + */ |
|
| 61 | + public function is_valid( $identifier, $operator, $operand_1, $operand_2, $type ) { |
|
| 62 | + /** |
|
| 63 | + * If this is not done then this will validate for term |
|
| 64 | + * which causes a bug, so this rule validator would return false |
|
| 65 | + * when the current thing is not a post. |
|
| 66 | + */ |
|
| 67 | + if ( $type !== Jsonld_Converter::POST ) { |
|
| 68 | + return false; |
|
| 69 | + } |
|
| 70 | + // Get the post type and then check whether it matches or not according to the operator. |
|
| 71 | + $post_type = get_post_type( $identifier ); |
|
| 72 | + |
|
| 73 | + switch ( $operator ) { |
|
| 74 | + case Rule_Validator::IS_NOT_EQUAL_TO: |
|
| 75 | + return $post_type !== $operand_2; |
|
| 76 | + |
|
| 77 | + case Rule_Validator::IS_EQUAL_TO: |
|
| 78 | + return $post_type === $operand_2; |
|
| 79 | + |
|
| 80 | + default: |
|
| 81 | + |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + return false; |
|
| 85 | + } |
|
| 86 | 86 | |
| 87 | 87 | } |
@@ -30,7 +30,7 @@ discard block |
||
| 30 | 30 | */ |
| 31 | 31 | public function __construct() { |
| 32 | 32 | |
| 33 | - add_filter( 'wl_mappings_rule_validators', array( $this, 'wl_mappings_rule_validators' ) ); |
|
| 33 | + add_filter('wl_mappings_rule_validators', array($this, 'wl_mappings_rule_validators')); |
|
| 34 | 34 | |
| 35 | 35 | } |
| 36 | 36 | |
@@ -41,9 +41,9 @@ discard block |
||
| 41 | 41 | * |
| 42 | 42 | * @return array An array with validators plus ours. |
| 43 | 43 | */ |
| 44 | - public function wl_mappings_rule_validators( $value ) { |
|
| 44 | + public function wl_mappings_rule_validators($value) { |
|
| 45 | 45 | |
| 46 | - $value[ self::POST_TYPE ] = $this; |
|
| 46 | + $value[self::POST_TYPE] = $this; |
|
| 47 | 47 | |
| 48 | 48 | return $value; |
| 49 | 49 | } |
@@ -52,25 +52,25 @@ discard block |
||
| 52 | 52 | * {@inheritdoc} |
| 53 | 53 | */ |
| 54 | 54 | public function get_label() { |
| 55 | - return __( 'Post Type', 'wordlift' ); |
|
| 55 | + return __('Post Type', 'wordlift'); |
|
| 56 | 56 | } |
| 57 | 57 | |
| 58 | 58 | /** |
| 59 | 59 | * {@inheritdoc} |
| 60 | 60 | */ |
| 61 | - public function is_valid( $identifier, $operator, $operand_1, $operand_2, $type ) { |
|
| 61 | + public function is_valid($identifier, $operator, $operand_1, $operand_2, $type) { |
|
| 62 | 62 | /** |
| 63 | 63 | * If this is not done then this will validate for term |
| 64 | 64 | * which causes a bug, so this rule validator would return false |
| 65 | 65 | * when the current thing is not a post. |
| 66 | 66 | */ |
| 67 | - if ( $type !== Jsonld_Converter::POST ) { |
|
| 67 | + if ($type !== Jsonld_Converter::POST) { |
|
| 68 | 68 | return false; |
| 69 | 69 | } |
| 70 | 70 | // Get the post type and then check whether it matches or not according to the operator. |
| 71 | - $post_type = get_post_type( $identifier ); |
|
| 71 | + $post_type = get_post_type($identifier); |
|
| 72 | 72 | |
| 73 | - switch ( $operator ) { |
|
| 73 | + switch ($operator) { |
|
| 74 | 74 | case Rule_Validator::IS_NOT_EQUAL_TO: |
| 75 | 75 | return $post_type !== $operand_2; |
| 76 | 76 | |
@@ -13,264 +13,264 @@ discard block |
||
| 13 | 13 | */ |
| 14 | 14 | abstract class Wordlift_Plugin_WP_Background_Process extends Wordlift_Plugin_WP_Async_Request { |
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * Action |
|
| 18 | - * |
|
| 19 | - * (default value: 'background_process') |
|
| 20 | - * |
|
| 21 | - * @var string |
|
| 22 | - * @access protected |
|
| 23 | - */ |
|
| 24 | - protected $action = 'background_process'; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * Start time of current process. |
|
| 28 | - * |
|
| 29 | - * (default value: 0) |
|
| 30 | - * |
|
| 31 | - * @var int |
|
| 32 | - * @access protected |
|
| 33 | - */ |
|
| 34 | - protected $start_time = 0; |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * Cron_hook_identifier |
|
| 38 | - * |
|
| 39 | - * @var mixed |
|
| 40 | - * @access protected |
|
| 41 | - */ |
|
| 42 | - protected $cron_hook_identifier; |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * Cron_interval_identifier |
|
| 46 | - * |
|
| 47 | - * @var mixed |
|
| 48 | - * @access protected |
|
| 49 | - */ |
|
| 50 | - protected $cron_interval_identifier; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * Initiate new background process |
|
| 54 | - */ |
|
| 55 | - public function __construct() { |
|
| 56 | - parent::__construct(); |
|
| 57 | - |
|
| 58 | - $this->cron_hook_identifier = $this->identifier . '_cron'; |
|
| 59 | - $this->cron_interval_identifier = $this->identifier . '_cron_interval'; |
|
| 60 | - |
|
| 61 | - add_action( $this->cron_hook_identifier, array( $this, 'handle_cron_healthcheck' ) ); |
|
| 62 | - add_filter( 'cron_schedules', array( $this, 'schedule_cron_healthcheck' ) ); |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * Dispatch |
|
| 67 | - * |
|
| 68 | - * @access public |
|
| 69 | - * @return void |
|
| 70 | - */ |
|
| 71 | - public function dispatch() { |
|
| 72 | - // Schedule the cron healthcheck. |
|
| 73 | - $this->schedule_event(); |
|
| 74 | - |
|
| 75 | - // Perform remote post. |
|
| 76 | - return parent::dispatch(); |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * Push to queue |
|
| 81 | - * |
|
| 82 | - * @param mixed $data Data. |
|
| 83 | - * |
|
| 84 | - * @return $this |
|
| 85 | - */ |
|
| 86 | - public function push_to_queue( $data ) { |
|
| 87 | - $this->data[] = $data; |
|
| 88 | - |
|
| 89 | - return $this; |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * Save queue |
|
| 94 | - * |
|
| 95 | - * @return $this |
|
| 96 | - */ |
|
| 97 | - public function save() { |
|
| 98 | - $key = $this->generate_key(); |
|
| 99 | - |
|
| 100 | - if ( ! empty( $this->data ) ) { |
|
| 101 | - update_site_option( $key, $this->data ); |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - return $this; |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * Update queue |
|
| 109 | - * |
|
| 110 | - * @param string $key Key. |
|
| 111 | - * @param array $data Data. |
|
| 112 | - * |
|
| 113 | - * @return $this |
|
| 114 | - */ |
|
| 115 | - public function update( $key, $data ) { |
|
| 116 | - if ( ! empty( $data ) ) { |
|
| 117 | - update_site_option( $key, $data ); |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - return $this; |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * Delete queue |
|
| 125 | - * |
|
| 126 | - * @param string $key Key. |
|
| 127 | - * |
|
| 128 | - * @return $this |
|
| 129 | - */ |
|
| 130 | - public function delete( $key ) { |
|
| 131 | - delete_site_option( $key ); |
|
| 132 | - |
|
| 133 | - return $this; |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - /** |
|
| 137 | - * Generate key |
|
| 138 | - * |
|
| 139 | - * Generates a unique key based on microtime. Queue items are |
|
| 140 | - * given a unique key so that they can be merged upon save. |
|
| 141 | - * |
|
| 142 | - * @param int $length Length. |
|
| 143 | - * |
|
| 144 | - * @return string |
|
| 145 | - */ |
|
| 146 | - protected function generate_key( $length = 64 ) { |
|
| 147 | - $unique = md5( microtime() . rand() ); |
|
| 148 | - $prepend = $this->identifier . '_batch_'; |
|
| 149 | - |
|
| 150 | - return substr( $prepend . $unique, 0, $length ); |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - /** |
|
| 154 | - * Maybe process queue |
|
| 155 | - * |
|
| 156 | - * Checks whether data exists within the queue and that |
|
| 157 | - * the process is not already running. |
|
| 158 | - */ |
|
| 159 | - public function maybe_handle() { |
|
| 160 | - // Don't lock up other requests while processing |
|
| 161 | - session_write_close(); |
|
| 162 | - |
|
| 163 | - if ( $this->is_process_running() ) { |
|
| 164 | - // Background process already running. |
|
| 165 | - wp_die(); |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - if ( $this->is_queue_empty() ) { |
|
| 169 | - // No data to process. |
|
| 170 | - wp_die(); |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - check_ajax_referer( $this->identifier, 'nonce' ); |
|
| 174 | - |
|
| 175 | - $this->handle(); |
|
| 176 | - |
|
| 177 | - wp_die(); |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - /** |
|
| 181 | - * Is queue empty |
|
| 182 | - * |
|
| 183 | - * @return bool |
|
| 184 | - */ |
|
| 185 | - protected function is_queue_empty() { |
|
| 186 | - global $wpdb; |
|
| 187 | - |
|
| 188 | - $table = $wpdb->options; |
|
| 189 | - $column = 'option_name'; |
|
| 190 | - |
|
| 191 | - if ( is_multisite() ) { |
|
| 192 | - $table = $wpdb->sitemeta; |
|
| 193 | - $column = 'meta_key'; |
|
| 194 | - } |
|
| 195 | - |
|
| 196 | - $key = $wpdb->esc_like( $this->identifier . '_batch_' ) . '%'; |
|
| 197 | - |
|
| 198 | - $count = $wpdb->get_var( $wpdb->prepare( " |
|
| 16 | + /** |
|
| 17 | + * Action |
|
| 18 | + * |
|
| 19 | + * (default value: 'background_process') |
|
| 20 | + * |
|
| 21 | + * @var string |
|
| 22 | + * @access protected |
|
| 23 | + */ |
|
| 24 | + protected $action = 'background_process'; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * Start time of current process. |
|
| 28 | + * |
|
| 29 | + * (default value: 0) |
|
| 30 | + * |
|
| 31 | + * @var int |
|
| 32 | + * @access protected |
|
| 33 | + */ |
|
| 34 | + protected $start_time = 0; |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * Cron_hook_identifier |
|
| 38 | + * |
|
| 39 | + * @var mixed |
|
| 40 | + * @access protected |
|
| 41 | + */ |
|
| 42 | + protected $cron_hook_identifier; |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * Cron_interval_identifier |
|
| 46 | + * |
|
| 47 | + * @var mixed |
|
| 48 | + * @access protected |
|
| 49 | + */ |
|
| 50 | + protected $cron_interval_identifier; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * Initiate new background process |
|
| 54 | + */ |
|
| 55 | + public function __construct() { |
|
| 56 | + parent::__construct(); |
|
| 57 | + |
|
| 58 | + $this->cron_hook_identifier = $this->identifier . '_cron'; |
|
| 59 | + $this->cron_interval_identifier = $this->identifier . '_cron_interval'; |
|
| 60 | + |
|
| 61 | + add_action( $this->cron_hook_identifier, array( $this, 'handle_cron_healthcheck' ) ); |
|
| 62 | + add_filter( 'cron_schedules', array( $this, 'schedule_cron_healthcheck' ) ); |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * Dispatch |
|
| 67 | + * |
|
| 68 | + * @access public |
|
| 69 | + * @return void |
|
| 70 | + */ |
|
| 71 | + public function dispatch() { |
|
| 72 | + // Schedule the cron healthcheck. |
|
| 73 | + $this->schedule_event(); |
|
| 74 | + |
|
| 75 | + // Perform remote post. |
|
| 76 | + return parent::dispatch(); |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * Push to queue |
|
| 81 | + * |
|
| 82 | + * @param mixed $data Data. |
|
| 83 | + * |
|
| 84 | + * @return $this |
|
| 85 | + */ |
|
| 86 | + public function push_to_queue( $data ) { |
|
| 87 | + $this->data[] = $data; |
|
| 88 | + |
|
| 89 | + return $this; |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * Save queue |
|
| 94 | + * |
|
| 95 | + * @return $this |
|
| 96 | + */ |
|
| 97 | + public function save() { |
|
| 98 | + $key = $this->generate_key(); |
|
| 99 | + |
|
| 100 | + if ( ! empty( $this->data ) ) { |
|
| 101 | + update_site_option( $key, $this->data ); |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + return $this; |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * Update queue |
|
| 109 | + * |
|
| 110 | + * @param string $key Key. |
|
| 111 | + * @param array $data Data. |
|
| 112 | + * |
|
| 113 | + * @return $this |
|
| 114 | + */ |
|
| 115 | + public function update( $key, $data ) { |
|
| 116 | + if ( ! empty( $data ) ) { |
|
| 117 | + update_site_option( $key, $data ); |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + return $this; |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * Delete queue |
|
| 125 | + * |
|
| 126 | + * @param string $key Key. |
|
| 127 | + * |
|
| 128 | + * @return $this |
|
| 129 | + */ |
|
| 130 | + public function delete( $key ) { |
|
| 131 | + delete_site_option( $key ); |
|
| 132 | + |
|
| 133 | + return $this; |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + /** |
|
| 137 | + * Generate key |
|
| 138 | + * |
|
| 139 | + * Generates a unique key based on microtime. Queue items are |
|
| 140 | + * given a unique key so that they can be merged upon save. |
|
| 141 | + * |
|
| 142 | + * @param int $length Length. |
|
| 143 | + * |
|
| 144 | + * @return string |
|
| 145 | + */ |
|
| 146 | + protected function generate_key( $length = 64 ) { |
|
| 147 | + $unique = md5( microtime() . rand() ); |
|
| 148 | + $prepend = $this->identifier . '_batch_'; |
|
| 149 | + |
|
| 150 | + return substr( $prepend . $unique, 0, $length ); |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + /** |
|
| 154 | + * Maybe process queue |
|
| 155 | + * |
|
| 156 | + * Checks whether data exists within the queue and that |
|
| 157 | + * the process is not already running. |
|
| 158 | + */ |
|
| 159 | + public function maybe_handle() { |
|
| 160 | + // Don't lock up other requests while processing |
|
| 161 | + session_write_close(); |
|
| 162 | + |
|
| 163 | + if ( $this->is_process_running() ) { |
|
| 164 | + // Background process already running. |
|
| 165 | + wp_die(); |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + if ( $this->is_queue_empty() ) { |
|
| 169 | + // No data to process. |
|
| 170 | + wp_die(); |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + check_ajax_referer( $this->identifier, 'nonce' ); |
|
| 174 | + |
|
| 175 | + $this->handle(); |
|
| 176 | + |
|
| 177 | + wp_die(); |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + /** |
|
| 181 | + * Is queue empty |
|
| 182 | + * |
|
| 183 | + * @return bool |
|
| 184 | + */ |
|
| 185 | + protected function is_queue_empty() { |
|
| 186 | + global $wpdb; |
|
| 187 | + |
|
| 188 | + $table = $wpdb->options; |
|
| 189 | + $column = 'option_name'; |
|
| 190 | + |
|
| 191 | + if ( is_multisite() ) { |
|
| 192 | + $table = $wpdb->sitemeta; |
|
| 193 | + $column = 'meta_key'; |
|
| 194 | + } |
|
| 195 | + |
|
| 196 | + $key = $wpdb->esc_like( $this->identifier . '_batch_' ) . '%'; |
|
| 197 | + |
|
| 198 | + $count = $wpdb->get_var( $wpdb->prepare( " |
|
| 199 | 199 | SELECT COUNT(*) |
| 200 | 200 | FROM {$table} |
| 201 | 201 | WHERE {$column} LIKE %s |
| 202 | 202 | ", $key ) ); |
| 203 | 203 | |
| 204 | - return ( $count > 0 ) ? false : true; |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - /** |
|
| 208 | - * Is process running |
|
| 209 | - * |
|
| 210 | - * Check whether the current process is already running |
|
| 211 | - * in a background process. |
|
| 212 | - */ |
|
| 213 | - protected function is_process_running() { |
|
| 214 | - if ( get_site_transient( $this->identifier . '_process_lock' ) ) { |
|
| 215 | - // Process already running. |
|
| 216 | - return true; |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - return false; |
|
| 220 | - } |
|
| 221 | - |
|
| 222 | - /** |
|
| 223 | - * Lock process |
|
| 224 | - * |
|
| 225 | - * Lock the process so that multiple instances can't run simultaneously. |
|
| 226 | - * Override if applicable, but the duration should be greater than that |
|
| 227 | - * defined in the time_exceeded() method. |
|
| 228 | - */ |
|
| 229 | - protected function lock_process() { |
|
| 230 | - $this->start_time = time(); // Set start time of current process. |
|
| 231 | - |
|
| 232 | - $lock_duration = ( property_exists( $this, 'queue_lock_time' ) ) ? $this->queue_lock_time : 60; // 1 minute |
|
| 233 | - $lock_duration = apply_filters( $this->identifier . '_queue_lock_time', $lock_duration ); |
|
| 234 | - |
|
| 235 | - set_site_transient( $this->identifier . '_process_lock', microtime(), $lock_duration ); |
|
| 236 | - } |
|
| 237 | - |
|
| 238 | - /** |
|
| 239 | - * Unlock process |
|
| 240 | - * |
|
| 241 | - * Unlock the process so that other instances can spawn. |
|
| 242 | - * |
|
| 243 | - * @return $this |
|
| 244 | - */ |
|
| 245 | - protected function unlock_process() { |
|
| 246 | - delete_site_transient( $this->identifier . '_process_lock' ); |
|
| 247 | - |
|
| 248 | - return $this; |
|
| 249 | - } |
|
| 250 | - |
|
| 251 | - /** |
|
| 252 | - * Get batch |
|
| 253 | - * |
|
| 254 | - * @return stdClass Return the first batch from the queue |
|
| 255 | - */ |
|
| 256 | - protected function get_batch() { |
|
| 257 | - global $wpdb; |
|
| 258 | - |
|
| 259 | - $table = $wpdb->options; |
|
| 260 | - $column = 'option_name'; |
|
| 261 | - $key_column = 'option_id'; |
|
| 262 | - $value_column = 'option_value'; |
|
| 263 | - |
|
| 264 | - if ( is_multisite() ) { |
|
| 265 | - $table = $wpdb->sitemeta; |
|
| 266 | - $column = 'meta_key'; |
|
| 267 | - $key_column = 'meta_id'; |
|
| 268 | - $value_column = 'meta_value'; |
|
| 269 | - } |
|
| 270 | - |
|
| 271 | - $key = $wpdb->esc_like( $this->identifier . '_batch_' ) . '%'; |
|
| 272 | - |
|
| 273 | - $query = $wpdb->get_row( $wpdb->prepare( " |
|
| 204 | + return ( $count > 0 ) ? false : true; |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + /** |
|
| 208 | + * Is process running |
|
| 209 | + * |
|
| 210 | + * Check whether the current process is already running |
|
| 211 | + * in a background process. |
|
| 212 | + */ |
|
| 213 | + protected function is_process_running() { |
|
| 214 | + if ( get_site_transient( $this->identifier . '_process_lock' ) ) { |
|
| 215 | + // Process already running. |
|
| 216 | + return true; |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + return false; |
|
| 220 | + } |
|
| 221 | + |
|
| 222 | + /** |
|
| 223 | + * Lock process |
|
| 224 | + * |
|
| 225 | + * Lock the process so that multiple instances can't run simultaneously. |
|
| 226 | + * Override if applicable, but the duration should be greater than that |
|
| 227 | + * defined in the time_exceeded() method. |
|
| 228 | + */ |
|
| 229 | + protected function lock_process() { |
|
| 230 | + $this->start_time = time(); // Set start time of current process. |
|
| 231 | + |
|
| 232 | + $lock_duration = ( property_exists( $this, 'queue_lock_time' ) ) ? $this->queue_lock_time : 60; // 1 minute |
|
| 233 | + $lock_duration = apply_filters( $this->identifier . '_queue_lock_time', $lock_duration ); |
|
| 234 | + |
|
| 235 | + set_site_transient( $this->identifier . '_process_lock', microtime(), $lock_duration ); |
|
| 236 | + } |
|
| 237 | + |
|
| 238 | + /** |
|
| 239 | + * Unlock process |
|
| 240 | + * |
|
| 241 | + * Unlock the process so that other instances can spawn. |
|
| 242 | + * |
|
| 243 | + * @return $this |
|
| 244 | + */ |
|
| 245 | + protected function unlock_process() { |
|
| 246 | + delete_site_transient( $this->identifier . '_process_lock' ); |
|
| 247 | + |
|
| 248 | + return $this; |
|
| 249 | + } |
|
| 250 | + |
|
| 251 | + /** |
|
| 252 | + * Get batch |
|
| 253 | + * |
|
| 254 | + * @return stdClass Return the first batch from the queue |
|
| 255 | + */ |
|
| 256 | + protected function get_batch() { |
|
| 257 | + global $wpdb; |
|
| 258 | + |
|
| 259 | + $table = $wpdb->options; |
|
| 260 | + $column = 'option_name'; |
|
| 261 | + $key_column = 'option_id'; |
|
| 262 | + $value_column = 'option_value'; |
|
| 263 | + |
|
| 264 | + if ( is_multisite() ) { |
|
| 265 | + $table = $wpdb->sitemeta; |
|
| 266 | + $column = 'meta_key'; |
|
| 267 | + $key_column = 'meta_id'; |
|
| 268 | + $value_column = 'meta_value'; |
|
| 269 | + } |
|
| 270 | + |
|
| 271 | + $key = $wpdb->esc_like( $this->identifier . '_batch_' ) . '%'; |
|
| 272 | + |
|
| 273 | + $query = $wpdb->get_row( $wpdb->prepare( " |
|
| 274 | 274 | SELECT * |
| 275 | 275 | FROM {$table} |
| 276 | 276 | WHERE {$column} LIKE %s |
@@ -278,228 +278,228 @@ discard block |
||
| 278 | 278 | LIMIT 1 |
| 279 | 279 | ", $key ) ); |
| 280 | 280 | |
| 281 | - $batch = new stdClass(); |
|
| 282 | - $batch->key = $query->$column; |
|
| 283 | - $batch->data = maybe_unserialize( $query->$value_column ); |
|
| 284 | - |
|
| 285 | - return $batch; |
|
| 286 | - } |
|
| 287 | - |
|
| 288 | - /** |
|
| 289 | - * Handle |
|
| 290 | - * |
|
| 291 | - * Pass each queue item to the task handler, while remaining |
|
| 292 | - * within server memory and time limit constraints. |
|
| 293 | - */ |
|
| 294 | - protected function handle() { |
|
| 295 | - $this->lock_process(); |
|
| 296 | - |
|
| 297 | - do { |
|
| 298 | - $batch = $this->get_batch(); |
|
| 299 | - |
|
| 300 | - foreach ( $batch->data as $key => $value ) { |
|
| 301 | - $task = $this->task( $value ); |
|
| 302 | - |
|
| 303 | - if ( false !== $task ) { |
|
| 304 | - $batch->data[ $key ] = $task; |
|
| 305 | - } else { |
|
| 306 | - unset( $batch->data[ $key ] ); |
|
| 307 | - } |
|
| 308 | - |
|
| 309 | - if ( $this->time_exceeded() || $this->memory_exceeded() ) { |
|
| 310 | - // Batch limits reached. |
|
| 311 | - break; |
|
| 312 | - } |
|
| 313 | - } |
|
| 314 | - |
|
| 315 | - // Update or delete current batch. |
|
| 316 | - if ( ! empty( $batch->data ) ) { |
|
| 317 | - $this->update( $batch->key, $batch->data ); |
|
| 318 | - } else { |
|
| 319 | - $this->delete( $batch->key ); |
|
| 320 | - } |
|
| 321 | - } while ( ! $this->time_exceeded() && ! $this->memory_exceeded() && ! $this->is_queue_empty() ); |
|
| 322 | - |
|
| 323 | - $this->unlock_process(); |
|
| 324 | - |
|
| 325 | - // Start next batch or complete process. |
|
| 326 | - if ( ! $this->is_queue_empty() ) { |
|
| 327 | - $this->dispatch(); |
|
| 328 | - } else { |
|
| 329 | - $this->complete(); |
|
| 330 | - } |
|
| 331 | - |
|
| 332 | - wp_die(); |
|
| 333 | - } |
|
| 334 | - |
|
| 335 | - /** |
|
| 336 | - * Memory exceeded |
|
| 337 | - * |
|
| 338 | - * Ensures the batch process never exceeds 90% |
|
| 339 | - * of the maximum WordPress memory. |
|
| 340 | - * |
|
| 341 | - * @return bool |
|
| 342 | - */ |
|
| 343 | - protected function memory_exceeded() { |
|
| 344 | - $memory_limit = $this->get_memory_limit() * 0.9; // 90% of max memory |
|
| 345 | - $current_memory = memory_get_usage( true ); |
|
| 346 | - $return = false; |
|
| 347 | - |
|
| 348 | - if ( $current_memory >= $memory_limit ) { |
|
| 349 | - $return = true; |
|
| 350 | - } |
|
| 351 | - |
|
| 352 | - return apply_filters( $this->identifier . '_memory_exceeded', $return ); |
|
| 353 | - } |
|
| 354 | - |
|
| 355 | - /** |
|
| 356 | - * Get memory limit |
|
| 357 | - * |
|
| 358 | - * @return int |
|
| 359 | - */ |
|
| 360 | - protected function get_memory_limit() { |
|
| 361 | - if ( function_exists( 'ini_get' ) ) { |
|
| 362 | - $memory_limit = ini_get( 'memory_limit' ); |
|
| 363 | - } else { |
|
| 364 | - // Sensible default. |
|
| 365 | - $memory_limit = '128M'; |
|
| 366 | - } |
|
| 367 | - |
|
| 368 | - if ( ! $memory_limit || - 1 === intval( $memory_limit ) ) { |
|
| 369 | - // Unlimited, set to 32GB. |
|
| 370 | - $memory_limit = '32000M'; |
|
| 371 | - } |
|
| 372 | - |
|
| 373 | - return wp_convert_hr_to_bytes( $memory_limit ); |
|
| 374 | - } |
|
| 375 | - |
|
| 376 | - /** |
|
| 377 | - * Time exceeded. |
|
| 378 | - * |
|
| 379 | - * Ensures the batch never exceeds a sensible time limit. |
|
| 380 | - * A timeout limit of 30s is common on shared hosting. |
|
| 381 | - * |
|
| 382 | - * @return bool |
|
| 383 | - */ |
|
| 384 | - protected function time_exceeded() { |
|
| 385 | - $finish = $this->start_time + apply_filters( $this->identifier . '_default_time_limit', 20 ); // 20 seconds |
|
| 386 | - $return = false; |
|
| 387 | - |
|
| 388 | - if ( time() >= $finish ) { |
|
| 389 | - $return = true; |
|
| 390 | - } |
|
| 391 | - |
|
| 392 | - return apply_filters( $this->identifier . '_time_exceeded', $return ); |
|
| 393 | - } |
|
| 394 | - |
|
| 395 | - /** |
|
| 396 | - * Complete. |
|
| 397 | - * |
|
| 398 | - * Override if applicable, but ensure that the below actions are |
|
| 399 | - * performed, or, call parent::complete(). |
|
| 400 | - */ |
|
| 401 | - protected function complete() { |
|
| 402 | - // Unschedule the cron healthcheck. |
|
| 403 | - $this->clear_scheduled_event(); |
|
| 404 | - } |
|
| 405 | - |
|
| 406 | - /** |
|
| 407 | - * Schedule cron healthcheck |
|
| 408 | - * |
|
| 409 | - * @access public |
|
| 410 | - * |
|
| 411 | - * @param mixed $schedules Schedules. |
|
| 412 | - * |
|
| 413 | - * @return mixed |
|
| 414 | - */ |
|
| 415 | - public function schedule_cron_healthcheck( $schedules ) { |
|
| 416 | - $interval = apply_filters( $this->identifier . '_cron_interval', 5 ); |
|
| 417 | - |
|
| 418 | - if ( property_exists( $this, 'cron_interval' ) ) { |
|
| 419 | - $interval = apply_filters( $this->identifier . '_cron_interval', $this->cron_interval ); |
|
| 420 | - } |
|
| 421 | - |
|
| 422 | - // Adds every 5 minutes to the existing schedules. |
|
| 423 | - $schedules[ $this->identifier . '_cron_interval' ] = array( |
|
| 424 | - 'interval' => MINUTE_IN_SECONDS * $interval, |
|
| 425 | - 'display' => sprintf( __( 'Every %d Minutes' ), $interval ), |
|
| 426 | - ); |
|
| 427 | - |
|
| 428 | - return $schedules; |
|
| 429 | - } |
|
| 430 | - |
|
| 431 | - /** |
|
| 432 | - * Handle cron healthcheck |
|
| 433 | - * |
|
| 434 | - * Restart the background process if not already running |
|
| 435 | - * and data exists in the queue. |
|
| 436 | - */ |
|
| 437 | - public function handle_cron_healthcheck() { |
|
| 438 | - if ( $this->is_process_running() ) { |
|
| 439 | - // Background process already running. |
|
| 440 | - exit; |
|
| 441 | - } |
|
| 442 | - |
|
| 443 | - if ( $this->is_queue_empty() ) { |
|
| 444 | - // No data to process. |
|
| 445 | - $this->clear_scheduled_event(); |
|
| 446 | - exit; |
|
| 447 | - } |
|
| 448 | - |
|
| 449 | - $this->handle(); |
|
| 450 | - |
|
| 451 | - exit; |
|
| 452 | - } |
|
| 453 | - |
|
| 454 | - /** |
|
| 455 | - * Schedule event |
|
| 456 | - */ |
|
| 457 | - protected function schedule_event() { |
|
| 458 | - if ( ! wp_next_scheduled( $this->cron_hook_identifier ) ) { |
|
| 459 | - wp_schedule_event( time(), $this->cron_interval_identifier, $this->cron_hook_identifier ); |
|
| 460 | - } |
|
| 461 | - } |
|
| 462 | - |
|
| 463 | - /** |
|
| 464 | - * Clear scheduled event |
|
| 465 | - */ |
|
| 466 | - protected function clear_scheduled_event() { |
|
| 467 | - $timestamp = wp_next_scheduled( $this->cron_hook_identifier ); |
|
| 468 | - |
|
| 469 | - if ( $timestamp ) { |
|
| 470 | - wp_unschedule_event( $timestamp, $this->cron_hook_identifier ); |
|
| 471 | - } |
|
| 472 | - } |
|
| 473 | - |
|
| 474 | - /** |
|
| 475 | - * Cancel Process |
|
| 476 | - * |
|
| 477 | - * Stop processing queue items, clear cronjob and delete batch. |
|
| 478 | - * |
|
| 479 | - */ |
|
| 480 | - public function cancel_process() { |
|
| 481 | - if ( ! $this->is_queue_empty() ) { |
|
| 482 | - $batch = $this->get_batch(); |
|
| 483 | - |
|
| 484 | - $this->delete( $batch->key ); |
|
| 485 | - |
|
| 486 | - wp_clear_scheduled_hook( $this->cron_hook_identifier ); |
|
| 487 | - } |
|
| 488 | - |
|
| 489 | - } |
|
| 490 | - |
|
| 491 | - /** |
|
| 492 | - * Task |
|
| 493 | - * |
|
| 494 | - * Override this method to perform any actions required on each |
|
| 495 | - * queue item. Return the modified item for further processing |
|
| 496 | - * in the next pass through. Or, return false to remove the |
|
| 497 | - * item from the queue. |
|
| 498 | - * |
|
| 499 | - * @param mixed $item Queue item to iterate over. |
|
| 500 | - * |
|
| 501 | - * @return mixed |
|
| 502 | - */ |
|
| 503 | - abstract protected function task( $item ); |
|
| 281 | + $batch = new stdClass(); |
|
| 282 | + $batch->key = $query->$column; |
|
| 283 | + $batch->data = maybe_unserialize( $query->$value_column ); |
|
| 284 | + |
|
| 285 | + return $batch; |
|
| 286 | + } |
|
| 287 | + |
|
| 288 | + /** |
|
| 289 | + * Handle |
|
| 290 | + * |
|
| 291 | + * Pass each queue item to the task handler, while remaining |
|
| 292 | + * within server memory and time limit constraints. |
|
| 293 | + */ |
|
| 294 | + protected function handle() { |
|
| 295 | + $this->lock_process(); |
|
| 296 | + |
|
| 297 | + do { |
|
| 298 | + $batch = $this->get_batch(); |
|
| 299 | + |
|
| 300 | + foreach ( $batch->data as $key => $value ) { |
|
| 301 | + $task = $this->task( $value ); |
|
| 302 | + |
|
| 303 | + if ( false !== $task ) { |
|
| 304 | + $batch->data[ $key ] = $task; |
|
| 305 | + } else { |
|
| 306 | + unset( $batch->data[ $key ] ); |
|
| 307 | + } |
|
| 308 | + |
|
| 309 | + if ( $this->time_exceeded() || $this->memory_exceeded() ) { |
|
| 310 | + // Batch limits reached. |
|
| 311 | + break; |
|
| 312 | + } |
|
| 313 | + } |
|
| 314 | + |
|
| 315 | + // Update or delete current batch. |
|
| 316 | + if ( ! empty( $batch->data ) ) { |
|
| 317 | + $this->update( $batch->key, $batch->data ); |
|
| 318 | + } else { |
|
| 319 | + $this->delete( $batch->key ); |
|
| 320 | + } |
|
| 321 | + } while ( ! $this->time_exceeded() && ! $this->memory_exceeded() && ! $this->is_queue_empty() ); |
|
| 322 | + |
|
| 323 | + $this->unlock_process(); |
|
| 324 | + |
|
| 325 | + // Start next batch or complete process. |
|
| 326 | + if ( ! $this->is_queue_empty() ) { |
|
| 327 | + $this->dispatch(); |
|
| 328 | + } else { |
|
| 329 | + $this->complete(); |
|
| 330 | + } |
|
| 331 | + |
|
| 332 | + wp_die(); |
|
| 333 | + } |
|
| 334 | + |
|
| 335 | + /** |
|
| 336 | + * Memory exceeded |
|
| 337 | + * |
|
| 338 | + * Ensures the batch process never exceeds 90% |
|
| 339 | + * of the maximum WordPress memory. |
|
| 340 | + * |
|
| 341 | + * @return bool |
|
| 342 | + */ |
|
| 343 | + protected function memory_exceeded() { |
|
| 344 | + $memory_limit = $this->get_memory_limit() * 0.9; // 90% of max memory |
|
| 345 | + $current_memory = memory_get_usage( true ); |
|
| 346 | + $return = false; |
|
| 347 | + |
|
| 348 | + if ( $current_memory >= $memory_limit ) { |
|
| 349 | + $return = true; |
|
| 350 | + } |
|
| 351 | + |
|
| 352 | + return apply_filters( $this->identifier . '_memory_exceeded', $return ); |
|
| 353 | + } |
|
| 354 | + |
|
| 355 | + /** |
|
| 356 | + * Get memory limit |
|
| 357 | + * |
|
| 358 | + * @return int |
|
| 359 | + */ |
|
| 360 | + protected function get_memory_limit() { |
|
| 361 | + if ( function_exists( 'ini_get' ) ) { |
|
| 362 | + $memory_limit = ini_get( 'memory_limit' ); |
|
| 363 | + } else { |
|
| 364 | + // Sensible default. |
|
| 365 | + $memory_limit = '128M'; |
|
| 366 | + } |
|
| 367 | + |
|
| 368 | + if ( ! $memory_limit || - 1 === intval( $memory_limit ) ) { |
|
| 369 | + // Unlimited, set to 32GB. |
|
| 370 | + $memory_limit = '32000M'; |
|
| 371 | + } |
|
| 372 | + |
|
| 373 | + return wp_convert_hr_to_bytes( $memory_limit ); |
|
| 374 | + } |
|
| 375 | + |
|
| 376 | + /** |
|
| 377 | + * Time exceeded. |
|
| 378 | + * |
|
| 379 | + * Ensures the batch never exceeds a sensible time limit. |
|
| 380 | + * A timeout limit of 30s is common on shared hosting. |
|
| 381 | + * |
|
| 382 | + * @return bool |
|
| 383 | + */ |
|
| 384 | + protected function time_exceeded() { |
|
| 385 | + $finish = $this->start_time + apply_filters( $this->identifier . '_default_time_limit', 20 ); // 20 seconds |
|
| 386 | + $return = false; |
|
| 387 | + |
|
| 388 | + if ( time() >= $finish ) { |
|
| 389 | + $return = true; |
|
| 390 | + } |
|
| 391 | + |
|
| 392 | + return apply_filters( $this->identifier . '_time_exceeded', $return ); |
|
| 393 | + } |
|
| 394 | + |
|
| 395 | + /** |
|
| 396 | + * Complete. |
|
| 397 | + * |
|
| 398 | + * Override if applicable, but ensure that the below actions are |
|
| 399 | + * performed, or, call parent::complete(). |
|
| 400 | + */ |
|
| 401 | + protected function complete() { |
|
| 402 | + // Unschedule the cron healthcheck. |
|
| 403 | + $this->clear_scheduled_event(); |
|
| 404 | + } |
|
| 405 | + |
|
| 406 | + /** |
|
| 407 | + * Schedule cron healthcheck |
|
| 408 | + * |
|
| 409 | + * @access public |
|
| 410 | + * |
|
| 411 | + * @param mixed $schedules Schedules. |
|
| 412 | + * |
|
| 413 | + * @return mixed |
|
| 414 | + */ |
|
| 415 | + public function schedule_cron_healthcheck( $schedules ) { |
|
| 416 | + $interval = apply_filters( $this->identifier . '_cron_interval', 5 ); |
|
| 417 | + |
|
| 418 | + if ( property_exists( $this, 'cron_interval' ) ) { |
|
| 419 | + $interval = apply_filters( $this->identifier . '_cron_interval', $this->cron_interval ); |
|
| 420 | + } |
|
| 421 | + |
|
| 422 | + // Adds every 5 minutes to the existing schedules. |
|
| 423 | + $schedules[ $this->identifier . '_cron_interval' ] = array( |
|
| 424 | + 'interval' => MINUTE_IN_SECONDS * $interval, |
|
| 425 | + 'display' => sprintf( __( 'Every %d Minutes' ), $interval ), |
|
| 426 | + ); |
|
| 427 | + |
|
| 428 | + return $schedules; |
|
| 429 | + } |
|
| 430 | + |
|
| 431 | + /** |
|
| 432 | + * Handle cron healthcheck |
|
| 433 | + * |
|
| 434 | + * Restart the background process if not already running |
|
| 435 | + * and data exists in the queue. |
|
| 436 | + */ |
|
| 437 | + public function handle_cron_healthcheck() { |
|
| 438 | + if ( $this->is_process_running() ) { |
|
| 439 | + // Background process already running. |
|
| 440 | + exit; |
|
| 441 | + } |
|
| 442 | + |
|
| 443 | + if ( $this->is_queue_empty() ) { |
|
| 444 | + // No data to process. |
|
| 445 | + $this->clear_scheduled_event(); |
|
| 446 | + exit; |
|
| 447 | + } |
|
| 448 | + |
|
| 449 | + $this->handle(); |
|
| 450 | + |
|
| 451 | + exit; |
|
| 452 | + } |
|
| 453 | + |
|
| 454 | + /** |
|
| 455 | + * Schedule event |
|
| 456 | + */ |
|
| 457 | + protected function schedule_event() { |
|
| 458 | + if ( ! wp_next_scheduled( $this->cron_hook_identifier ) ) { |
|
| 459 | + wp_schedule_event( time(), $this->cron_interval_identifier, $this->cron_hook_identifier ); |
|
| 460 | + } |
|
| 461 | + } |
|
| 462 | + |
|
| 463 | + /** |
|
| 464 | + * Clear scheduled event |
|
| 465 | + */ |
|
| 466 | + protected function clear_scheduled_event() { |
|
| 467 | + $timestamp = wp_next_scheduled( $this->cron_hook_identifier ); |
|
| 468 | + |
|
| 469 | + if ( $timestamp ) { |
|
| 470 | + wp_unschedule_event( $timestamp, $this->cron_hook_identifier ); |
|
| 471 | + } |
|
| 472 | + } |
|
| 473 | + |
|
| 474 | + /** |
|
| 475 | + * Cancel Process |
|
| 476 | + * |
|
| 477 | + * Stop processing queue items, clear cronjob and delete batch. |
|
| 478 | + * |
|
| 479 | + */ |
|
| 480 | + public function cancel_process() { |
|
| 481 | + if ( ! $this->is_queue_empty() ) { |
|
| 482 | + $batch = $this->get_batch(); |
|
| 483 | + |
|
| 484 | + $this->delete( $batch->key ); |
|
| 485 | + |
|
| 486 | + wp_clear_scheduled_hook( $this->cron_hook_identifier ); |
|
| 487 | + } |
|
| 488 | + |
|
| 489 | + } |
|
| 490 | + |
|
| 491 | + /** |
|
| 492 | + * Task |
|
| 493 | + * |
|
| 494 | + * Override this method to perform any actions required on each |
|
| 495 | + * queue item. Return the modified item for further processing |
|
| 496 | + * in the next pass through. Or, return false to remove the |
|
| 497 | + * item from the queue. |
|
| 498 | + * |
|
| 499 | + * @param mixed $item Queue item to iterate over. |
|
| 500 | + * |
|
| 501 | + * @return mixed |
|
| 502 | + */ |
|
| 503 | + abstract protected function task( $item ); |
|
| 504 | 504 | |
| 505 | 505 | } |
| 506 | 506 | \ No newline at end of file |
@@ -55,11 +55,11 @@ discard block |
||
| 55 | 55 | public function __construct() { |
| 56 | 56 | parent::__construct(); |
| 57 | 57 | |
| 58 | - $this->cron_hook_identifier = $this->identifier . '_cron'; |
|
| 59 | - $this->cron_interval_identifier = $this->identifier . '_cron_interval'; |
|
| 58 | + $this->cron_hook_identifier = $this->identifier.'_cron'; |
|
| 59 | + $this->cron_interval_identifier = $this->identifier.'_cron_interval'; |
|
| 60 | 60 | |
| 61 | - add_action( $this->cron_hook_identifier, array( $this, 'handle_cron_healthcheck' ) ); |
|
| 62 | - add_filter( 'cron_schedules', array( $this, 'schedule_cron_healthcheck' ) ); |
|
| 61 | + add_action($this->cron_hook_identifier, array($this, 'handle_cron_healthcheck')); |
|
| 62 | + add_filter('cron_schedules', array($this, 'schedule_cron_healthcheck')); |
|
| 63 | 63 | } |
| 64 | 64 | |
| 65 | 65 | /** |
@@ -83,7 +83,7 @@ discard block |
||
| 83 | 83 | * |
| 84 | 84 | * @return $this |
| 85 | 85 | */ |
| 86 | - public function push_to_queue( $data ) { |
|
| 86 | + public function push_to_queue($data) { |
|
| 87 | 87 | $this->data[] = $data; |
| 88 | 88 | |
| 89 | 89 | return $this; |
@@ -97,8 +97,8 @@ discard block |
||
| 97 | 97 | public function save() { |
| 98 | 98 | $key = $this->generate_key(); |
| 99 | 99 | |
| 100 | - if ( ! empty( $this->data ) ) { |
|
| 101 | - update_site_option( $key, $this->data ); |
|
| 100 | + if ( ! empty($this->data)) { |
|
| 101 | + update_site_option($key, $this->data); |
|
| 102 | 102 | } |
| 103 | 103 | |
| 104 | 104 | return $this; |
@@ -112,9 +112,9 @@ discard block |
||
| 112 | 112 | * |
| 113 | 113 | * @return $this |
| 114 | 114 | */ |
| 115 | - public function update( $key, $data ) { |
|
| 116 | - if ( ! empty( $data ) ) { |
|
| 117 | - update_site_option( $key, $data ); |
|
| 115 | + public function update($key, $data) { |
|
| 116 | + if ( ! empty($data)) { |
|
| 117 | + update_site_option($key, $data); |
|
| 118 | 118 | } |
| 119 | 119 | |
| 120 | 120 | return $this; |
@@ -127,8 +127,8 @@ discard block |
||
| 127 | 127 | * |
| 128 | 128 | * @return $this |
| 129 | 129 | */ |
| 130 | - public function delete( $key ) { |
|
| 131 | - delete_site_option( $key ); |
|
| 130 | + public function delete($key) { |
|
| 131 | + delete_site_option($key); |
|
| 132 | 132 | |
| 133 | 133 | return $this; |
| 134 | 134 | } |
@@ -143,11 +143,11 @@ discard block |
||
| 143 | 143 | * |
| 144 | 144 | * @return string |
| 145 | 145 | */ |
| 146 | - protected function generate_key( $length = 64 ) { |
|
| 147 | - $unique = md5( microtime() . rand() ); |
|
| 148 | - $prepend = $this->identifier . '_batch_'; |
|
| 146 | + protected function generate_key($length = 64) { |
|
| 147 | + $unique = md5(microtime().rand()); |
|
| 148 | + $prepend = $this->identifier.'_batch_'; |
|
| 149 | 149 | |
| 150 | - return substr( $prepend . $unique, 0, $length ); |
|
| 150 | + return substr($prepend.$unique, 0, $length); |
|
| 151 | 151 | } |
| 152 | 152 | |
| 153 | 153 | /** |
@@ -160,17 +160,17 @@ discard block |
||
| 160 | 160 | // Don't lock up other requests while processing |
| 161 | 161 | session_write_close(); |
| 162 | 162 | |
| 163 | - if ( $this->is_process_running() ) { |
|
| 163 | + if ($this->is_process_running()) { |
|
| 164 | 164 | // Background process already running. |
| 165 | 165 | wp_die(); |
| 166 | 166 | } |
| 167 | 167 | |
| 168 | - if ( $this->is_queue_empty() ) { |
|
| 168 | + if ($this->is_queue_empty()) { |
|
| 169 | 169 | // No data to process. |
| 170 | 170 | wp_die(); |
| 171 | 171 | } |
| 172 | 172 | |
| 173 | - check_ajax_referer( $this->identifier, 'nonce' ); |
|
| 173 | + check_ajax_referer($this->identifier, 'nonce'); |
|
| 174 | 174 | |
| 175 | 175 | $this->handle(); |
| 176 | 176 | |
@@ -188,20 +188,20 @@ discard block |
||
| 188 | 188 | $table = $wpdb->options; |
| 189 | 189 | $column = 'option_name'; |
| 190 | 190 | |
| 191 | - if ( is_multisite() ) { |
|
| 191 | + if (is_multisite()) { |
|
| 192 | 192 | $table = $wpdb->sitemeta; |
| 193 | 193 | $column = 'meta_key'; |
| 194 | 194 | } |
| 195 | 195 | |
| 196 | - $key = $wpdb->esc_like( $this->identifier . '_batch_' ) . '%'; |
|
| 196 | + $key = $wpdb->esc_like($this->identifier.'_batch_').'%'; |
|
| 197 | 197 | |
| 198 | - $count = $wpdb->get_var( $wpdb->prepare( " |
|
| 198 | + $count = $wpdb->get_var($wpdb->prepare(" |
|
| 199 | 199 | SELECT COUNT(*) |
| 200 | 200 | FROM {$table} |
| 201 | 201 | WHERE {$column} LIKE %s |
| 202 | - ", $key ) ); |
|
| 202 | + ", $key)); |
|
| 203 | 203 | |
| 204 | - return ( $count > 0 ) ? false : true; |
|
| 204 | + return ($count > 0) ? false : true; |
|
| 205 | 205 | } |
| 206 | 206 | |
| 207 | 207 | /** |
@@ -211,7 +211,7 @@ discard block |
||
| 211 | 211 | * in a background process. |
| 212 | 212 | */ |
| 213 | 213 | protected function is_process_running() { |
| 214 | - if ( get_site_transient( $this->identifier . '_process_lock' ) ) { |
|
| 214 | + if (get_site_transient($this->identifier.'_process_lock')) { |
|
| 215 | 215 | // Process already running. |
| 216 | 216 | return true; |
| 217 | 217 | } |
@@ -229,10 +229,10 @@ discard block |
||
| 229 | 229 | protected function lock_process() { |
| 230 | 230 | $this->start_time = time(); // Set start time of current process. |
| 231 | 231 | |
| 232 | - $lock_duration = ( property_exists( $this, 'queue_lock_time' ) ) ? $this->queue_lock_time : 60; // 1 minute |
|
| 233 | - $lock_duration = apply_filters( $this->identifier . '_queue_lock_time', $lock_duration ); |
|
| 232 | + $lock_duration = (property_exists($this, 'queue_lock_time')) ? $this->queue_lock_time : 60; // 1 minute |
|
| 233 | + $lock_duration = apply_filters($this->identifier.'_queue_lock_time', $lock_duration); |
|
| 234 | 234 | |
| 235 | - set_site_transient( $this->identifier . '_process_lock', microtime(), $lock_duration ); |
|
| 235 | + set_site_transient($this->identifier.'_process_lock', microtime(), $lock_duration); |
|
| 236 | 236 | } |
| 237 | 237 | |
| 238 | 238 | /** |
@@ -243,7 +243,7 @@ discard block |
||
| 243 | 243 | * @return $this |
| 244 | 244 | */ |
| 245 | 245 | protected function unlock_process() { |
| 246 | - delete_site_transient( $this->identifier . '_process_lock' ); |
|
| 246 | + delete_site_transient($this->identifier.'_process_lock'); |
|
| 247 | 247 | |
| 248 | 248 | return $this; |
| 249 | 249 | } |
@@ -261,26 +261,26 @@ discard block |
||
| 261 | 261 | $key_column = 'option_id'; |
| 262 | 262 | $value_column = 'option_value'; |
| 263 | 263 | |
| 264 | - if ( is_multisite() ) { |
|
| 264 | + if (is_multisite()) { |
|
| 265 | 265 | $table = $wpdb->sitemeta; |
| 266 | 266 | $column = 'meta_key'; |
| 267 | 267 | $key_column = 'meta_id'; |
| 268 | 268 | $value_column = 'meta_value'; |
| 269 | 269 | } |
| 270 | 270 | |
| 271 | - $key = $wpdb->esc_like( $this->identifier . '_batch_' ) . '%'; |
|
| 271 | + $key = $wpdb->esc_like($this->identifier.'_batch_').'%'; |
|
| 272 | 272 | |
| 273 | - $query = $wpdb->get_row( $wpdb->prepare( " |
|
| 273 | + $query = $wpdb->get_row($wpdb->prepare(" |
|
| 274 | 274 | SELECT * |
| 275 | 275 | FROM {$table} |
| 276 | 276 | WHERE {$column} LIKE %s |
| 277 | 277 | ORDER BY {$key_column} ASC |
| 278 | 278 | LIMIT 1 |
| 279 | - ", $key ) ); |
|
| 279 | + ", $key)); |
|
| 280 | 280 | |
| 281 | 281 | $batch = new stdClass(); |
| 282 | 282 | $batch->key = $query->$column; |
| 283 | - $batch->data = maybe_unserialize( $query->$value_column ); |
|
| 283 | + $batch->data = maybe_unserialize($query->$value_column); |
|
| 284 | 284 | |
| 285 | 285 | return $batch; |
| 286 | 286 | } |
@@ -297,33 +297,33 @@ discard block |
||
| 297 | 297 | do { |
| 298 | 298 | $batch = $this->get_batch(); |
| 299 | 299 | |
| 300 | - foreach ( $batch->data as $key => $value ) { |
|
| 301 | - $task = $this->task( $value ); |
|
| 300 | + foreach ($batch->data as $key => $value) { |
|
| 301 | + $task = $this->task($value); |
|
| 302 | 302 | |
| 303 | - if ( false !== $task ) { |
|
| 304 | - $batch->data[ $key ] = $task; |
|
| 303 | + if (false !== $task) { |
|
| 304 | + $batch->data[$key] = $task; |
|
| 305 | 305 | } else { |
| 306 | - unset( $batch->data[ $key ] ); |
|
| 306 | + unset($batch->data[$key]); |
|
| 307 | 307 | } |
| 308 | 308 | |
| 309 | - if ( $this->time_exceeded() || $this->memory_exceeded() ) { |
|
| 309 | + if ($this->time_exceeded() || $this->memory_exceeded()) { |
|
| 310 | 310 | // Batch limits reached. |
| 311 | 311 | break; |
| 312 | 312 | } |
| 313 | 313 | } |
| 314 | 314 | |
| 315 | 315 | // Update or delete current batch. |
| 316 | - if ( ! empty( $batch->data ) ) { |
|
| 317 | - $this->update( $batch->key, $batch->data ); |
|
| 316 | + if ( ! empty($batch->data)) { |
|
| 317 | + $this->update($batch->key, $batch->data); |
|
| 318 | 318 | } else { |
| 319 | - $this->delete( $batch->key ); |
|
| 319 | + $this->delete($batch->key); |
|
| 320 | 320 | } |
| 321 | - } while ( ! $this->time_exceeded() && ! $this->memory_exceeded() && ! $this->is_queue_empty() ); |
|
| 321 | + } while ( ! $this->time_exceeded() && ! $this->memory_exceeded() && ! $this->is_queue_empty()); |
|
| 322 | 322 | |
| 323 | 323 | $this->unlock_process(); |
| 324 | 324 | |
| 325 | 325 | // Start next batch or complete process. |
| 326 | - if ( ! $this->is_queue_empty() ) { |
|
| 326 | + if ( ! $this->is_queue_empty()) { |
|
| 327 | 327 | $this->dispatch(); |
| 328 | 328 | } else { |
| 329 | 329 | $this->complete(); |
@@ -342,14 +342,14 @@ discard block |
||
| 342 | 342 | */ |
| 343 | 343 | protected function memory_exceeded() { |
| 344 | 344 | $memory_limit = $this->get_memory_limit() * 0.9; // 90% of max memory |
| 345 | - $current_memory = memory_get_usage( true ); |
|
| 345 | + $current_memory = memory_get_usage(true); |
|
| 346 | 346 | $return = false; |
| 347 | 347 | |
| 348 | - if ( $current_memory >= $memory_limit ) { |
|
| 348 | + if ($current_memory >= $memory_limit) { |
|
| 349 | 349 | $return = true; |
| 350 | 350 | } |
| 351 | 351 | |
| 352 | - return apply_filters( $this->identifier . '_memory_exceeded', $return ); |
|
| 352 | + return apply_filters($this->identifier.'_memory_exceeded', $return); |
|
| 353 | 353 | } |
| 354 | 354 | |
| 355 | 355 | /** |
@@ -358,19 +358,19 @@ discard block |
||
| 358 | 358 | * @return int |
| 359 | 359 | */ |
| 360 | 360 | protected function get_memory_limit() { |
| 361 | - if ( function_exists( 'ini_get' ) ) { |
|
| 362 | - $memory_limit = ini_get( 'memory_limit' ); |
|
| 361 | + if (function_exists('ini_get')) { |
|
| 362 | + $memory_limit = ini_get('memory_limit'); |
|
| 363 | 363 | } else { |
| 364 | 364 | // Sensible default. |
| 365 | 365 | $memory_limit = '128M'; |
| 366 | 366 | } |
| 367 | 367 | |
| 368 | - if ( ! $memory_limit || - 1 === intval( $memory_limit ) ) { |
|
| 368 | + if ( ! $memory_limit || - 1 === intval($memory_limit)) { |
|
| 369 | 369 | // Unlimited, set to 32GB. |
| 370 | 370 | $memory_limit = '32000M'; |
| 371 | 371 | } |
| 372 | 372 | |
| 373 | - return wp_convert_hr_to_bytes( $memory_limit ); |
|
| 373 | + return wp_convert_hr_to_bytes($memory_limit); |
|
| 374 | 374 | } |
| 375 | 375 | |
| 376 | 376 | /** |
@@ -382,14 +382,14 @@ discard block |
||
| 382 | 382 | * @return bool |
| 383 | 383 | */ |
| 384 | 384 | protected function time_exceeded() { |
| 385 | - $finish = $this->start_time + apply_filters( $this->identifier . '_default_time_limit', 20 ); // 20 seconds |
|
| 385 | + $finish = $this->start_time + apply_filters($this->identifier.'_default_time_limit', 20); // 20 seconds |
|
| 386 | 386 | $return = false; |
| 387 | 387 | |
| 388 | - if ( time() >= $finish ) { |
|
| 388 | + if (time() >= $finish) { |
|
| 389 | 389 | $return = true; |
| 390 | 390 | } |
| 391 | 391 | |
| 392 | - return apply_filters( $this->identifier . '_time_exceeded', $return ); |
|
| 392 | + return apply_filters($this->identifier.'_time_exceeded', $return); |
|
| 393 | 393 | } |
| 394 | 394 | |
| 395 | 395 | /** |
@@ -412,17 +412,17 @@ discard block |
||
| 412 | 412 | * |
| 413 | 413 | * @return mixed |
| 414 | 414 | */ |
| 415 | - public function schedule_cron_healthcheck( $schedules ) { |
|
| 416 | - $interval = apply_filters( $this->identifier . '_cron_interval', 5 ); |
|
| 415 | + public function schedule_cron_healthcheck($schedules) { |
|
| 416 | + $interval = apply_filters($this->identifier.'_cron_interval', 5); |
|
| 417 | 417 | |
| 418 | - if ( property_exists( $this, 'cron_interval' ) ) { |
|
| 419 | - $interval = apply_filters( $this->identifier . '_cron_interval', $this->cron_interval ); |
|
| 418 | + if (property_exists($this, 'cron_interval')) { |
|
| 419 | + $interval = apply_filters($this->identifier.'_cron_interval', $this->cron_interval); |
|
| 420 | 420 | } |
| 421 | 421 | |
| 422 | 422 | // Adds every 5 minutes to the existing schedules. |
| 423 | - $schedules[ $this->identifier . '_cron_interval' ] = array( |
|
| 423 | + $schedules[$this->identifier.'_cron_interval'] = array( |
|
| 424 | 424 | 'interval' => MINUTE_IN_SECONDS * $interval, |
| 425 | - 'display' => sprintf( __( 'Every %d Minutes' ), $interval ), |
|
| 425 | + 'display' => sprintf(__('Every %d Minutes'), $interval), |
|
| 426 | 426 | ); |
| 427 | 427 | |
| 428 | 428 | return $schedules; |
@@ -435,12 +435,12 @@ discard block |
||
| 435 | 435 | * and data exists in the queue. |
| 436 | 436 | */ |
| 437 | 437 | public function handle_cron_healthcheck() { |
| 438 | - if ( $this->is_process_running() ) { |
|
| 438 | + if ($this->is_process_running()) { |
|
| 439 | 439 | // Background process already running. |
| 440 | 440 | exit; |
| 441 | 441 | } |
| 442 | 442 | |
| 443 | - if ( $this->is_queue_empty() ) { |
|
| 443 | + if ($this->is_queue_empty()) { |
|
| 444 | 444 | // No data to process. |
| 445 | 445 | $this->clear_scheduled_event(); |
| 446 | 446 | exit; |
@@ -455,8 +455,8 @@ discard block |
||
| 455 | 455 | * Schedule event |
| 456 | 456 | */ |
| 457 | 457 | protected function schedule_event() { |
| 458 | - if ( ! wp_next_scheduled( $this->cron_hook_identifier ) ) { |
|
| 459 | - wp_schedule_event( time(), $this->cron_interval_identifier, $this->cron_hook_identifier ); |
|
| 458 | + if ( ! wp_next_scheduled($this->cron_hook_identifier)) { |
|
| 459 | + wp_schedule_event(time(), $this->cron_interval_identifier, $this->cron_hook_identifier); |
|
| 460 | 460 | } |
| 461 | 461 | } |
| 462 | 462 | |
@@ -464,10 +464,10 @@ discard block |
||
| 464 | 464 | * Clear scheduled event |
| 465 | 465 | */ |
| 466 | 466 | protected function clear_scheduled_event() { |
| 467 | - $timestamp = wp_next_scheduled( $this->cron_hook_identifier ); |
|
| 467 | + $timestamp = wp_next_scheduled($this->cron_hook_identifier); |
|
| 468 | 468 | |
| 469 | - if ( $timestamp ) { |
|
| 470 | - wp_unschedule_event( $timestamp, $this->cron_hook_identifier ); |
|
| 469 | + if ($timestamp) { |
|
| 470 | + wp_unschedule_event($timestamp, $this->cron_hook_identifier); |
|
| 471 | 471 | } |
| 472 | 472 | } |
| 473 | 473 | |
@@ -478,12 +478,12 @@ discard block |
||
| 478 | 478 | * |
| 479 | 479 | */ |
| 480 | 480 | public function cancel_process() { |
| 481 | - if ( ! $this->is_queue_empty() ) { |
|
| 481 | + if ( ! $this->is_queue_empty()) { |
|
| 482 | 482 | $batch = $this->get_batch(); |
| 483 | 483 | |
| 484 | - $this->delete( $batch->key ); |
|
| 484 | + $this->delete($batch->key); |
|
| 485 | 485 | |
| 486 | - wp_clear_scheduled_hook( $this->cron_hook_identifier ); |
|
| 486 | + wp_clear_scheduled_hook($this->cron_hook_identifier); |
|
| 487 | 487 | } |
| 488 | 488 | |
| 489 | 489 | } |
@@ -500,6 +500,6 @@ discard block |
||
| 500 | 500 | * |
| 501 | 501 | * @return mixed |
| 502 | 502 | */ |
| 503 | - abstract protected function task( $item ); |
|
| 503 | + abstract protected function task($item); |
|
| 504 | 504 | |
| 505 | 505 | } |
| 506 | 506 | \ No newline at end of file |
@@ -12,170 +12,170 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | abstract class Wordlift_Plugin_WP_Async_Request { |
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * Prefix |
|
| 17 | - * |
|
| 18 | - * (default value: 'wp') |
|
| 19 | - * |
|
| 20 | - * @var string |
|
| 21 | - * @access protected |
|
| 22 | - */ |
|
| 23 | - protected $prefix = 'wp'; |
|
| 24 | - |
|
| 25 | - /** |
|
| 26 | - * Action |
|
| 27 | - * |
|
| 28 | - * (default value: 'async_request') |
|
| 29 | - * |
|
| 30 | - * @var string |
|
| 31 | - * @access protected |
|
| 32 | - */ |
|
| 33 | - protected $action = 'async_request'; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * Identifier |
|
| 37 | - * |
|
| 38 | - * @var mixed |
|
| 39 | - * @access protected |
|
| 40 | - */ |
|
| 41 | - protected $identifier; |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * Data |
|
| 45 | - * |
|
| 46 | - * (default value: array()) |
|
| 47 | - * |
|
| 48 | - * @var array |
|
| 49 | - * @access protected |
|
| 50 | - */ |
|
| 51 | - protected $data = array(); |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * Initiate new async request |
|
| 55 | - */ |
|
| 56 | - public function __construct() { |
|
| 57 | - $this->identifier = $this->prefix . '_' . $this->action; |
|
| 58 | - |
|
| 59 | - add_action( 'wp_ajax_' . $this->identifier, array( $this, 'maybe_handle' ) ); |
|
| 60 | - add_action( 'wp_ajax_nopriv_' . $this->identifier, array( $this, 'maybe_handle' ) ); |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * Set data used during the request |
|
| 65 | - * |
|
| 66 | - * @param array $data Data. |
|
| 67 | - * |
|
| 68 | - * @return $this |
|
| 69 | - */ |
|
| 70 | - public function data( $data ) { |
|
| 71 | - $this->data = $data; |
|
| 72 | - |
|
| 73 | - return $this; |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * Dispatch the async request |
|
| 78 | - * |
|
| 79 | - * @return array|WP_Error |
|
| 80 | - */ |
|
| 81 | - public function dispatch() { |
|
| 82 | - $url = add_query_arg( $this->get_query_args(), $this->get_query_url() ); |
|
| 83 | - $args = $this->get_post_args(); |
|
| 84 | - |
|
| 85 | - return wp_remote_post( esc_url_raw( $url ), $args ); |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * Get query args |
|
| 90 | - * |
|
| 91 | - * @return array |
|
| 92 | - */ |
|
| 93 | - protected function get_query_args() { |
|
| 94 | - if ( property_exists( $this, 'query_args' ) ) { |
|
| 95 | - return $this->query_args; |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - $args = array( |
|
| 99 | - 'action' => $this->identifier, |
|
| 100 | - 'nonce' => wp_create_nonce( $this->identifier ), |
|
| 101 | - ); |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * Filters the post arguments used during an async request. |
|
| 105 | - * |
|
| 106 | - * @param array $url |
|
| 107 | - */ |
|
| 108 | - return apply_filters( $this->identifier . '_query_args', $args ); |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * Get query URL |
|
| 113 | - * |
|
| 114 | - * @return string |
|
| 115 | - */ |
|
| 116 | - protected function get_query_url() { |
|
| 117 | - if ( property_exists( $this, 'query_url' ) ) { |
|
| 118 | - return $this->query_url; |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - $url = admin_url( 'admin-ajax.php' ); |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * Filters the post arguments used during an async request. |
|
| 125 | - * |
|
| 126 | - * @param string $url |
|
| 127 | - */ |
|
| 128 | - return apply_filters( $this->identifier . '_query_url', $url ); |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - /** |
|
| 132 | - * Get post args |
|
| 133 | - * |
|
| 134 | - * @return array |
|
| 135 | - */ |
|
| 136 | - protected function get_post_args() { |
|
| 137 | - if ( property_exists( $this, 'post_args' ) ) { |
|
| 138 | - return $this->post_args; |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - $args = array( |
|
| 142 | - 'timeout' => 0.01, |
|
| 143 | - 'blocking' => false, |
|
| 144 | - 'body' => $this->data, |
|
| 145 | - 'cookies' => $_COOKIE, |
|
| 146 | - 'sslverify' => apply_filters( 'https_local_ssl_verify', false ), |
|
| 147 | - ); |
|
| 148 | - |
|
| 149 | - /** |
|
| 150 | - * Filters the post arguments used during an async request. |
|
| 151 | - * |
|
| 152 | - * @param array $args |
|
| 153 | - */ |
|
| 154 | - return apply_filters( $this->identifier . '_post_args', $args ); |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - /** |
|
| 158 | - * Maybe handle |
|
| 159 | - * |
|
| 160 | - * Check for correct nonce and pass to handler. |
|
| 161 | - */ |
|
| 162 | - public function maybe_handle() { |
|
| 163 | - // Don't lock up other requests while processing |
|
| 164 | - session_write_close(); |
|
| 165 | - |
|
| 166 | - check_ajax_referer( $this->identifier, 'nonce' ); |
|
| 167 | - |
|
| 168 | - $this->handle(); |
|
| 169 | - |
|
| 170 | - wp_die(); |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - /** |
|
| 174 | - * Handle |
|
| 175 | - * |
|
| 176 | - * Override this method to perform any actions required |
|
| 177 | - * during the async request. |
|
| 178 | - */ |
|
| 179 | - abstract protected function handle(); |
|
| 15 | + /** |
|
| 16 | + * Prefix |
|
| 17 | + * |
|
| 18 | + * (default value: 'wp') |
|
| 19 | + * |
|
| 20 | + * @var string |
|
| 21 | + * @access protected |
|
| 22 | + */ |
|
| 23 | + protected $prefix = 'wp'; |
|
| 24 | + |
|
| 25 | + /** |
|
| 26 | + * Action |
|
| 27 | + * |
|
| 28 | + * (default value: 'async_request') |
|
| 29 | + * |
|
| 30 | + * @var string |
|
| 31 | + * @access protected |
|
| 32 | + */ |
|
| 33 | + protected $action = 'async_request'; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * Identifier |
|
| 37 | + * |
|
| 38 | + * @var mixed |
|
| 39 | + * @access protected |
|
| 40 | + */ |
|
| 41 | + protected $identifier; |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * Data |
|
| 45 | + * |
|
| 46 | + * (default value: array()) |
|
| 47 | + * |
|
| 48 | + * @var array |
|
| 49 | + * @access protected |
|
| 50 | + */ |
|
| 51 | + protected $data = array(); |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * Initiate new async request |
|
| 55 | + */ |
|
| 56 | + public function __construct() { |
|
| 57 | + $this->identifier = $this->prefix . '_' . $this->action; |
|
| 58 | + |
|
| 59 | + add_action( 'wp_ajax_' . $this->identifier, array( $this, 'maybe_handle' ) ); |
|
| 60 | + add_action( 'wp_ajax_nopriv_' . $this->identifier, array( $this, 'maybe_handle' ) ); |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * Set data used during the request |
|
| 65 | + * |
|
| 66 | + * @param array $data Data. |
|
| 67 | + * |
|
| 68 | + * @return $this |
|
| 69 | + */ |
|
| 70 | + public function data( $data ) { |
|
| 71 | + $this->data = $data; |
|
| 72 | + |
|
| 73 | + return $this; |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * Dispatch the async request |
|
| 78 | + * |
|
| 79 | + * @return array|WP_Error |
|
| 80 | + */ |
|
| 81 | + public function dispatch() { |
|
| 82 | + $url = add_query_arg( $this->get_query_args(), $this->get_query_url() ); |
|
| 83 | + $args = $this->get_post_args(); |
|
| 84 | + |
|
| 85 | + return wp_remote_post( esc_url_raw( $url ), $args ); |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * Get query args |
|
| 90 | + * |
|
| 91 | + * @return array |
|
| 92 | + */ |
|
| 93 | + protected function get_query_args() { |
|
| 94 | + if ( property_exists( $this, 'query_args' ) ) { |
|
| 95 | + return $this->query_args; |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + $args = array( |
|
| 99 | + 'action' => $this->identifier, |
|
| 100 | + 'nonce' => wp_create_nonce( $this->identifier ), |
|
| 101 | + ); |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * Filters the post arguments used during an async request. |
|
| 105 | + * |
|
| 106 | + * @param array $url |
|
| 107 | + */ |
|
| 108 | + return apply_filters( $this->identifier . '_query_args', $args ); |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * Get query URL |
|
| 113 | + * |
|
| 114 | + * @return string |
|
| 115 | + */ |
|
| 116 | + protected function get_query_url() { |
|
| 117 | + if ( property_exists( $this, 'query_url' ) ) { |
|
| 118 | + return $this->query_url; |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + $url = admin_url( 'admin-ajax.php' ); |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * Filters the post arguments used during an async request. |
|
| 125 | + * |
|
| 126 | + * @param string $url |
|
| 127 | + */ |
|
| 128 | + return apply_filters( $this->identifier . '_query_url', $url ); |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + /** |
|
| 132 | + * Get post args |
|
| 133 | + * |
|
| 134 | + * @return array |
|
| 135 | + */ |
|
| 136 | + protected function get_post_args() { |
|
| 137 | + if ( property_exists( $this, 'post_args' ) ) { |
|
| 138 | + return $this->post_args; |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + $args = array( |
|
| 142 | + 'timeout' => 0.01, |
|
| 143 | + 'blocking' => false, |
|
| 144 | + 'body' => $this->data, |
|
| 145 | + 'cookies' => $_COOKIE, |
|
| 146 | + 'sslverify' => apply_filters( 'https_local_ssl_verify', false ), |
|
| 147 | + ); |
|
| 148 | + |
|
| 149 | + /** |
|
| 150 | + * Filters the post arguments used during an async request. |
|
| 151 | + * |
|
| 152 | + * @param array $args |
|
| 153 | + */ |
|
| 154 | + return apply_filters( $this->identifier . '_post_args', $args ); |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + /** |
|
| 158 | + * Maybe handle |
|
| 159 | + * |
|
| 160 | + * Check for correct nonce and pass to handler. |
|
| 161 | + */ |
|
| 162 | + public function maybe_handle() { |
|
| 163 | + // Don't lock up other requests while processing |
|
| 164 | + session_write_close(); |
|
| 165 | + |
|
| 166 | + check_ajax_referer( $this->identifier, 'nonce' ); |
|
| 167 | + |
|
| 168 | + $this->handle(); |
|
| 169 | + |
|
| 170 | + wp_die(); |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + /** |
|
| 174 | + * Handle |
|
| 175 | + * |
|
| 176 | + * Override this method to perform any actions required |
|
| 177 | + * during the async request. |
|
| 178 | + */ |
|
| 179 | + abstract protected function handle(); |
|
| 180 | 180 | |
| 181 | 181 | } |
@@ -54,10 +54,10 @@ discard block |
||
| 54 | 54 | * Initiate new async request |
| 55 | 55 | */ |
| 56 | 56 | public function __construct() { |
| 57 | - $this->identifier = $this->prefix . '_' . $this->action; |
|
| 57 | + $this->identifier = $this->prefix.'_'.$this->action; |
|
| 58 | 58 | |
| 59 | - add_action( 'wp_ajax_' . $this->identifier, array( $this, 'maybe_handle' ) ); |
|
| 60 | - add_action( 'wp_ajax_nopriv_' . $this->identifier, array( $this, 'maybe_handle' ) ); |
|
| 59 | + add_action('wp_ajax_'.$this->identifier, array($this, 'maybe_handle')); |
|
| 60 | + add_action('wp_ajax_nopriv_'.$this->identifier, array($this, 'maybe_handle')); |
|
| 61 | 61 | } |
| 62 | 62 | |
| 63 | 63 | /** |
@@ -67,7 +67,7 @@ discard block |
||
| 67 | 67 | * |
| 68 | 68 | * @return $this |
| 69 | 69 | */ |
| 70 | - public function data( $data ) { |
|
| 70 | + public function data($data) { |
|
| 71 | 71 | $this->data = $data; |
| 72 | 72 | |
| 73 | 73 | return $this; |
@@ -79,10 +79,10 @@ discard block |
||
| 79 | 79 | * @return array|WP_Error |
| 80 | 80 | */ |
| 81 | 81 | public function dispatch() { |
| 82 | - $url = add_query_arg( $this->get_query_args(), $this->get_query_url() ); |
|
| 82 | + $url = add_query_arg($this->get_query_args(), $this->get_query_url()); |
|
| 83 | 83 | $args = $this->get_post_args(); |
| 84 | 84 | |
| 85 | - return wp_remote_post( esc_url_raw( $url ), $args ); |
|
| 85 | + return wp_remote_post(esc_url_raw($url), $args); |
|
| 86 | 86 | } |
| 87 | 87 | |
| 88 | 88 | /** |
@@ -91,13 +91,13 @@ discard block |
||
| 91 | 91 | * @return array |
| 92 | 92 | */ |
| 93 | 93 | protected function get_query_args() { |
| 94 | - if ( property_exists( $this, 'query_args' ) ) { |
|
| 94 | + if (property_exists($this, 'query_args')) { |
|
| 95 | 95 | return $this->query_args; |
| 96 | 96 | } |
| 97 | 97 | |
| 98 | 98 | $args = array( |
| 99 | 99 | 'action' => $this->identifier, |
| 100 | - 'nonce' => wp_create_nonce( $this->identifier ), |
|
| 100 | + 'nonce' => wp_create_nonce($this->identifier), |
|
| 101 | 101 | ); |
| 102 | 102 | |
| 103 | 103 | /** |
@@ -105,7 +105,7 @@ discard block |
||
| 105 | 105 | * |
| 106 | 106 | * @param array $url |
| 107 | 107 | */ |
| 108 | - return apply_filters( $this->identifier . '_query_args', $args ); |
|
| 108 | + return apply_filters($this->identifier.'_query_args', $args); |
|
| 109 | 109 | } |
| 110 | 110 | |
| 111 | 111 | /** |
@@ -114,18 +114,18 @@ discard block |
||
| 114 | 114 | * @return string |
| 115 | 115 | */ |
| 116 | 116 | protected function get_query_url() { |
| 117 | - if ( property_exists( $this, 'query_url' ) ) { |
|
| 117 | + if (property_exists($this, 'query_url')) { |
|
| 118 | 118 | return $this->query_url; |
| 119 | 119 | } |
| 120 | 120 | |
| 121 | - $url = admin_url( 'admin-ajax.php' ); |
|
| 121 | + $url = admin_url('admin-ajax.php'); |
|
| 122 | 122 | |
| 123 | 123 | /** |
| 124 | 124 | * Filters the post arguments used during an async request. |
| 125 | 125 | * |
| 126 | 126 | * @param string $url |
| 127 | 127 | */ |
| 128 | - return apply_filters( $this->identifier . '_query_url', $url ); |
|
| 128 | + return apply_filters($this->identifier.'_query_url', $url); |
|
| 129 | 129 | } |
| 130 | 130 | |
| 131 | 131 | /** |
@@ -134,7 +134,7 @@ discard block |
||
| 134 | 134 | * @return array |
| 135 | 135 | */ |
| 136 | 136 | protected function get_post_args() { |
| 137 | - if ( property_exists( $this, 'post_args' ) ) { |
|
| 137 | + if (property_exists($this, 'post_args')) { |
|
| 138 | 138 | return $this->post_args; |
| 139 | 139 | } |
| 140 | 140 | |
@@ -143,7 +143,7 @@ discard block |
||
| 143 | 143 | 'blocking' => false, |
| 144 | 144 | 'body' => $this->data, |
| 145 | 145 | 'cookies' => $_COOKIE, |
| 146 | - 'sslverify' => apply_filters( 'https_local_ssl_verify', false ), |
|
| 146 | + 'sslverify' => apply_filters('https_local_ssl_verify', false), |
|
| 147 | 147 | ); |
| 148 | 148 | |
| 149 | 149 | /** |
@@ -151,7 +151,7 @@ discard block |
||
| 151 | 151 | * |
| 152 | 152 | * @param array $args |
| 153 | 153 | */ |
| 154 | - return apply_filters( $this->identifier . '_post_args', $args ); |
|
| 154 | + return apply_filters($this->identifier.'_post_args', $args); |
|
| 155 | 155 | } |
| 156 | 156 | |
| 157 | 157 | /** |
@@ -163,7 +163,7 @@ discard block |
||
| 163 | 163 | // Don't lock up other requests while processing |
| 164 | 164 | session_write_close(); |
| 165 | 165 | |
| 166 | - check_ajax_referer( $this->identifier, 'nonce' ); |
|
| 166 | + check_ajax_referer($this->identifier, 'nonce'); |
|
| 167 | 167 | |
| 168 | 168 | $this->handle(); |
| 169 | 169 | |
@@ -59,7 +59,7 @@ discard block |
||
| 59 | 59 | |
| 60 | 60 | public function getPrefixes() |
| 61 | 61 | { |
| 62 | - if (!empty($this->prefixesPsr0)) { |
|
| 62 | + if ( ! empty($this->prefixesPsr0)) { |
|
| 63 | 63 | return call_user_func_array('array_merge', array_values($this->prefixesPsr0)); |
| 64 | 64 | } |
| 65 | 65 | |
@@ -108,7 +108,7 @@ discard block |
||
| 108 | 108 | */ |
| 109 | 109 | public function add($prefix, $paths, $prepend = false) |
| 110 | 110 | { |
| 111 | - if (!$prefix) { |
|
| 111 | + if ( ! $prefix) { |
|
| 112 | 112 | if ($prepend) { |
| 113 | 113 | $this->fallbackDirsPsr0 = array_merge( |
| 114 | 114 | (array) $paths, |
@@ -125,7 +125,7 @@ discard block |
||
| 125 | 125 | } |
| 126 | 126 | |
| 127 | 127 | $first = $prefix[0]; |
| 128 | - if (!isset($this->prefixesPsr0[$first][$prefix])) { |
|
| 128 | + if ( ! isset($this->prefixesPsr0[$first][$prefix])) { |
|
| 129 | 129 | $this->prefixesPsr0[$first][$prefix] = (array) $paths; |
| 130 | 130 | |
| 131 | 131 | return; |
@@ -155,7 +155,7 @@ discard block |
||
| 155 | 155 | */ |
| 156 | 156 | public function addPsr4($prefix, $paths, $prepend = false) |
| 157 | 157 | { |
| 158 | - if (!$prefix) { |
|
| 158 | + if ( ! $prefix) { |
|
| 159 | 159 | // Register directories for the root namespace. |
| 160 | 160 | if ($prepend) { |
| 161 | 161 | $this->fallbackDirsPsr4 = array_merge( |
@@ -168,7 +168,7 @@ discard block |
||
| 168 | 168 | (array) $paths |
| 169 | 169 | ); |
| 170 | 170 | } |
| 171 | - } elseif (!isset($this->prefixDirsPsr4[$prefix])) { |
|
| 171 | + } elseif ( ! isset($this->prefixDirsPsr4[$prefix])) { |
|
| 172 | 172 | // Register directories for a new namespace. |
| 173 | 173 | $length = strlen($prefix); |
| 174 | 174 | if ('\\' !== $prefix[$length - 1]) { |
@@ -200,7 +200,7 @@ discard block |
||
| 200 | 200 | */ |
| 201 | 201 | public function set($prefix, $paths) |
| 202 | 202 | { |
| 203 | - if (!$prefix) { |
|
| 203 | + if ( ! $prefix) { |
|
| 204 | 204 | $this->fallbackDirsPsr0 = (array) $paths; |
| 205 | 205 | } else { |
| 206 | 206 | $this->prefixesPsr0[$prefix[0]][$prefix] = (array) $paths; |
@@ -218,7 +218,7 @@ discard block |
||
| 218 | 218 | */ |
| 219 | 219 | public function setPsr4($prefix, $paths) |
| 220 | 220 | { |
| 221 | - if (!$prefix) { |
|
| 221 | + if ( ! $prefix) { |
|
| 222 | 222 | $this->fallbackDirsPsr4 = (array) $paths; |
| 223 | 223 | } else { |
| 224 | 224 | $length = strlen($prefix); |
@@ -370,18 +370,18 @@ discard block |
||
| 370 | 370 | private function findFileWithExtension($class, $ext) |
| 371 | 371 | { |
| 372 | 372 | // PSR-4 lookup |
| 373 | - $logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR) . $ext; |
|
| 373 | + $logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR).$ext; |
|
| 374 | 374 | |
| 375 | 375 | $first = $class[0]; |
| 376 | 376 | if (isset($this->prefixLengthsPsr4[$first])) { |
| 377 | 377 | $subPath = $class; |
| 378 | 378 | while (false !== $lastPos = strrpos($subPath, '\\')) { |
| 379 | 379 | $subPath = substr($subPath, 0, $lastPos); |
| 380 | - $search = $subPath . '\\'; |
|
| 380 | + $search = $subPath.'\\'; |
|
| 381 | 381 | if (isset($this->prefixDirsPsr4[$search])) { |
| 382 | - $pathEnd = DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $lastPos + 1); |
|
| 382 | + $pathEnd = DIRECTORY_SEPARATOR.substr($logicalPathPsr4, $lastPos + 1); |
|
| 383 | 383 | foreach ($this->prefixDirsPsr4[$search] as $dir) { |
| 384 | - if (file_exists($file = $dir . $pathEnd)) { |
|
| 384 | + if (file_exists($file = $dir.$pathEnd)) { |
|
| 385 | 385 | return $file; |
| 386 | 386 | } |
| 387 | 387 | } |
@@ -391,7 +391,7 @@ discard block |
||
| 391 | 391 | |
| 392 | 392 | // PSR-4 fallback dirs |
| 393 | 393 | foreach ($this->fallbackDirsPsr4 as $dir) { |
| 394 | - if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr4)) { |
|
| 394 | + if (file_exists($file = $dir.DIRECTORY_SEPARATOR.$logicalPathPsr4)) { |
|
| 395 | 395 | return $file; |
| 396 | 396 | } |
| 397 | 397 | } |
@@ -403,14 +403,14 @@ discard block |
||
| 403 | 403 | . strtr(substr($logicalPathPsr4, $pos + 1), '_', DIRECTORY_SEPARATOR); |
| 404 | 404 | } else { |
| 405 | 405 | // PEAR-like class name |
| 406 | - $logicalPathPsr0 = strtr($class, '_', DIRECTORY_SEPARATOR) . $ext; |
|
| 406 | + $logicalPathPsr0 = strtr($class, '_', DIRECTORY_SEPARATOR).$ext; |
|
| 407 | 407 | } |
| 408 | 408 | |
| 409 | 409 | if (isset($this->prefixesPsr0[$first])) { |
| 410 | 410 | foreach ($this->prefixesPsr0[$first] as $prefix => $dirs) { |
| 411 | 411 | if (0 === strpos($class, $prefix)) { |
| 412 | 412 | foreach ($dirs as $dir) { |
| 413 | - if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) { |
|
| 413 | + if (file_exists($file = $dir.DIRECTORY_SEPARATOR.$logicalPathPsr0)) { |
|
| 414 | 414 | return $file; |
| 415 | 415 | } |
| 416 | 416 | } |
@@ -420,7 +420,7 @@ discard block |
||
| 420 | 420 | |
| 421 | 421 | // PSR-0 fallback dirs |
| 422 | 422 | foreach ($this->fallbackDirsPsr0 as $dir) { |
| 423 | - if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) { |
|
| 423 | + if (file_exists($file = $dir.DIRECTORY_SEPARATOR.$logicalPathPsr0)) { |
|
| 424 | 424 | return $file; |
| 425 | 425 | } |
| 426 | 426 | } |
@@ -6,9 +6,9 @@ |
||
| 6 | 6 | $baseDir = dirname($vendorDir); |
| 7 | 7 | |
| 8 | 8 | return array( |
| 9 | - 'Composer\\InstalledVersions' => $vendorDir . '/composer/InstalledVersions.php', |
|
| 10 | - 'WP_Async_Request' => $vendorDir . '/deliciousbrains/wp-background-processing/classes/wp-async-request.php', |
|
| 11 | - 'WP_Background_Process' => $vendorDir . '/deliciousbrains/wp-background-processing/classes/wp-background-process.php', |
|
| 12 | - 'Wordlift_Plugin_WP_Async_Request' => $baseDir . '/ext/dependencies/deliciousbrains/wp-background-processing/classes/wp-async-request.php', |
|
| 13 | - 'Wordlift_Plugin_WP_Background_Process' => $baseDir . '/ext/dependencies/deliciousbrains/wp-background-processing/classes/wp-background-process.php', |
|
| 9 | + 'Composer\\InstalledVersions' => $vendorDir.'/composer/InstalledVersions.php', |
|
| 10 | + 'WP_Async_Request' => $vendorDir.'/deliciousbrains/wp-background-processing/classes/wp-async-request.php', |
|
| 11 | + 'WP_Background_Process' => $vendorDir.'/deliciousbrains/wp-background-processing/classes/wp-background-process.php', |
|
| 12 | + 'Wordlift_Plugin_WP_Async_Request' => $baseDir.'/ext/dependencies/deliciousbrains/wp-background-processing/classes/wp-async-request.php', |
|
| 13 | + 'Wordlift_Plugin_WP_Background_Process' => $baseDir.'/ext/dependencies/deliciousbrains/wp-background-processing/classes/wp-background-process.php', |
|
| 14 | 14 | ); |
@@ -13,264 +13,264 @@ discard block |
||
| 13 | 13 | */ |
| 14 | 14 | abstract class WP_Background_Process extends WP_Async_Request { |
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * Action |
|
| 18 | - * |
|
| 19 | - * (default value: 'background_process') |
|
| 20 | - * |
|
| 21 | - * @var string |
|
| 22 | - * @access protected |
|
| 23 | - */ |
|
| 24 | - protected $action = 'background_process'; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * Start time of current process. |
|
| 28 | - * |
|
| 29 | - * (default value: 0) |
|
| 30 | - * |
|
| 31 | - * @var int |
|
| 32 | - * @access protected |
|
| 33 | - */ |
|
| 34 | - protected $start_time = 0; |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * Cron_hook_identifier |
|
| 38 | - * |
|
| 39 | - * @var mixed |
|
| 40 | - * @access protected |
|
| 41 | - */ |
|
| 42 | - protected $cron_hook_identifier; |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * Cron_interval_identifier |
|
| 46 | - * |
|
| 47 | - * @var mixed |
|
| 48 | - * @access protected |
|
| 49 | - */ |
|
| 50 | - protected $cron_interval_identifier; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * Initiate new background process |
|
| 54 | - */ |
|
| 55 | - public function __construct() { |
|
| 56 | - parent::__construct(); |
|
| 57 | - |
|
| 58 | - $this->cron_hook_identifier = $this->identifier . '_cron'; |
|
| 59 | - $this->cron_interval_identifier = $this->identifier . '_cron_interval'; |
|
| 60 | - |
|
| 61 | - add_action( $this->cron_hook_identifier, array( $this, 'handle_cron_healthcheck' ) ); |
|
| 62 | - add_filter( 'cron_schedules', array( $this, 'schedule_cron_healthcheck' ) ); |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * Dispatch |
|
| 67 | - * |
|
| 68 | - * @access public |
|
| 69 | - * @return void |
|
| 70 | - */ |
|
| 71 | - public function dispatch() { |
|
| 72 | - // Schedule the cron healthcheck. |
|
| 73 | - $this->schedule_event(); |
|
| 74 | - |
|
| 75 | - // Perform remote post. |
|
| 76 | - return parent::dispatch(); |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * Push to queue |
|
| 81 | - * |
|
| 82 | - * @param mixed $data Data. |
|
| 83 | - * |
|
| 84 | - * @return $this |
|
| 85 | - */ |
|
| 86 | - public function push_to_queue( $data ) { |
|
| 87 | - $this->data[] = $data; |
|
| 88 | - |
|
| 89 | - return $this; |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * Save queue |
|
| 94 | - * |
|
| 95 | - * @return $this |
|
| 96 | - */ |
|
| 97 | - public function save() { |
|
| 98 | - $key = $this->generate_key(); |
|
| 99 | - |
|
| 100 | - if ( ! empty( $this->data ) ) { |
|
| 101 | - update_site_option( $key, $this->data ); |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - return $this; |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * Update queue |
|
| 109 | - * |
|
| 110 | - * @param string $key Key. |
|
| 111 | - * @param array $data Data. |
|
| 112 | - * |
|
| 113 | - * @return $this |
|
| 114 | - */ |
|
| 115 | - public function update( $key, $data ) { |
|
| 116 | - if ( ! empty( $data ) ) { |
|
| 117 | - update_site_option( $key, $data ); |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - return $this; |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * Delete queue |
|
| 125 | - * |
|
| 126 | - * @param string $key Key. |
|
| 127 | - * |
|
| 128 | - * @return $this |
|
| 129 | - */ |
|
| 130 | - public function delete( $key ) { |
|
| 131 | - delete_site_option( $key ); |
|
| 132 | - |
|
| 133 | - return $this; |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - /** |
|
| 137 | - * Generate key |
|
| 138 | - * |
|
| 139 | - * Generates a unique key based on microtime. Queue items are |
|
| 140 | - * given a unique key so that they can be merged upon save. |
|
| 141 | - * |
|
| 142 | - * @param int $length Length. |
|
| 143 | - * |
|
| 144 | - * @return string |
|
| 145 | - */ |
|
| 146 | - protected function generate_key( $length = 64 ) { |
|
| 147 | - $unique = md5( microtime() . rand() ); |
|
| 148 | - $prepend = $this->identifier . '_batch_'; |
|
| 149 | - |
|
| 150 | - return substr( $prepend . $unique, 0, $length ); |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - /** |
|
| 154 | - * Maybe process queue |
|
| 155 | - * |
|
| 156 | - * Checks whether data exists within the queue and that |
|
| 157 | - * the process is not already running. |
|
| 158 | - */ |
|
| 159 | - public function maybe_handle() { |
|
| 160 | - // Don't lock up other requests while processing |
|
| 161 | - session_write_close(); |
|
| 162 | - |
|
| 163 | - if ( $this->is_process_running() ) { |
|
| 164 | - // Background process already running. |
|
| 165 | - wp_die(); |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - if ( $this->is_queue_empty() ) { |
|
| 169 | - // No data to process. |
|
| 170 | - wp_die(); |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - check_ajax_referer( $this->identifier, 'nonce' ); |
|
| 174 | - |
|
| 175 | - $this->handle(); |
|
| 176 | - |
|
| 177 | - wp_die(); |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - /** |
|
| 181 | - * Is queue empty |
|
| 182 | - * |
|
| 183 | - * @return bool |
|
| 184 | - */ |
|
| 185 | - protected function is_queue_empty() { |
|
| 186 | - global $wpdb; |
|
| 187 | - |
|
| 188 | - $table = $wpdb->options; |
|
| 189 | - $column = 'option_name'; |
|
| 190 | - |
|
| 191 | - if ( is_multisite() ) { |
|
| 192 | - $table = $wpdb->sitemeta; |
|
| 193 | - $column = 'meta_key'; |
|
| 194 | - } |
|
| 195 | - |
|
| 196 | - $key = $wpdb->esc_like( $this->identifier . '_batch_' ) . '%'; |
|
| 197 | - |
|
| 198 | - $count = $wpdb->get_var( $wpdb->prepare( " |
|
| 16 | + /** |
|
| 17 | + * Action |
|
| 18 | + * |
|
| 19 | + * (default value: 'background_process') |
|
| 20 | + * |
|
| 21 | + * @var string |
|
| 22 | + * @access protected |
|
| 23 | + */ |
|
| 24 | + protected $action = 'background_process'; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * Start time of current process. |
|
| 28 | + * |
|
| 29 | + * (default value: 0) |
|
| 30 | + * |
|
| 31 | + * @var int |
|
| 32 | + * @access protected |
|
| 33 | + */ |
|
| 34 | + protected $start_time = 0; |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * Cron_hook_identifier |
|
| 38 | + * |
|
| 39 | + * @var mixed |
|
| 40 | + * @access protected |
|
| 41 | + */ |
|
| 42 | + protected $cron_hook_identifier; |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * Cron_interval_identifier |
|
| 46 | + * |
|
| 47 | + * @var mixed |
|
| 48 | + * @access protected |
|
| 49 | + */ |
|
| 50 | + protected $cron_interval_identifier; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * Initiate new background process |
|
| 54 | + */ |
|
| 55 | + public function __construct() { |
|
| 56 | + parent::__construct(); |
|
| 57 | + |
|
| 58 | + $this->cron_hook_identifier = $this->identifier . '_cron'; |
|
| 59 | + $this->cron_interval_identifier = $this->identifier . '_cron_interval'; |
|
| 60 | + |
|
| 61 | + add_action( $this->cron_hook_identifier, array( $this, 'handle_cron_healthcheck' ) ); |
|
| 62 | + add_filter( 'cron_schedules', array( $this, 'schedule_cron_healthcheck' ) ); |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * Dispatch |
|
| 67 | + * |
|
| 68 | + * @access public |
|
| 69 | + * @return void |
|
| 70 | + */ |
|
| 71 | + public function dispatch() { |
|
| 72 | + // Schedule the cron healthcheck. |
|
| 73 | + $this->schedule_event(); |
|
| 74 | + |
|
| 75 | + // Perform remote post. |
|
| 76 | + return parent::dispatch(); |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * Push to queue |
|
| 81 | + * |
|
| 82 | + * @param mixed $data Data. |
|
| 83 | + * |
|
| 84 | + * @return $this |
|
| 85 | + */ |
|
| 86 | + public function push_to_queue( $data ) { |
|
| 87 | + $this->data[] = $data; |
|
| 88 | + |
|
| 89 | + return $this; |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * Save queue |
|
| 94 | + * |
|
| 95 | + * @return $this |
|
| 96 | + */ |
|
| 97 | + public function save() { |
|
| 98 | + $key = $this->generate_key(); |
|
| 99 | + |
|
| 100 | + if ( ! empty( $this->data ) ) { |
|
| 101 | + update_site_option( $key, $this->data ); |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + return $this; |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * Update queue |
|
| 109 | + * |
|
| 110 | + * @param string $key Key. |
|
| 111 | + * @param array $data Data. |
|
| 112 | + * |
|
| 113 | + * @return $this |
|
| 114 | + */ |
|
| 115 | + public function update( $key, $data ) { |
|
| 116 | + if ( ! empty( $data ) ) { |
|
| 117 | + update_site_option( $key, $data ); |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + return $this; |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * Delete queue |
|
| 125 | + * |
|
| 126 | + * @param string $key Key. |
|
| 127 | + * |
|
| 128 | + * @return $this |
|
| 129 | + */ |
|
| 130 | + public function delete( $key ) { |
|
| 131 | + delete_site_option( $key ); |
|
| 132 | + |
|
| 133 | + return $this; |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + /** |
|
| 137 | + * Generate key |
|
| 138 | + * |
|
| 139 | + * Generates a unique key based on microtime. Queue items are |
|
| 140 | + * given a unique key so that they can be merged upon save. |
|
| 141 | + * |
|
| 142 | + * @param int $length Length. |
|
| 143 | + * |
|
| 144 | + * @return string |
|
| 145 | + */ |
|
| 146 | + protected function generate_key( $length = 64 ) { |
|
| 147 | + $unique = md5( microtime() . rand() ); |
|
| 148 | + $prepend = $this->identifier . '_batch_'; |
|
| 149 | + |
|
| 150 | + return substr( $prepend . $unique, 0, $length ); |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + /** |
|
| 154 | + * Maybe process queue |
|
| 155 | + * |
|
| 156 | + * Checks whether data exists within the queue and that |
|
| 157 | + * the process is not already running. |
|
| 158 | + */ |
|
| 159 | + public function maybe_handle() { |
|
| 160 | + // Don't lock up other requests while processing |
|
| 161 | + session_write_close(); |
|
| 162 | + |
|
| 163 | + if ( $this->is_process_running() ) { |
|
| 164 | + // Background process already running. |
|
| 165 | + wp_die(); |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + if ( $this->is_queue_empty() ) { |
|
| 169 | + // No data to process. |
|
| 170 | + wp_die(); |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + check_ajax_referer( $this->identifier, 'nonce' ); |
|
| 174 | + |
|
| 175 | + $this->handle(); |
|
| 176 | + |
|
| 177 | + wp_die(); |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + /** |
|
| 181 | + * Is queue empty |
|
| 182 | + * |
|
| 183 | + * @return bool |
|
| 184 | + */ |
|
| 185 | + protected function is_queue_empty() { |
|
| 186 | + global $wpdb; |
|
| 187 | + |
|
| 188 | + $table = $wpdb->options; |
|
| 189 | + $column = 'option_name'; |
|
| 190 | + |
|
| 191 | + if ( is_multisite() ) { |
|
| 192 | + $table = $wpdb->sitemeta; |
|
| 193 | + $column = 'meta_key'; |
|
| 194 | + } |
|
| 195 | + |
|
| 196 | + $key = $wpdb->esc_like( $this->identifier . '_batch_' ) . '%'; |
|
| 197 | + |
|
| 198 | + $count = $wpdb->get_var( $wpdb->prepare( " |
|
| 199 | 199 | SELECT COUNT(*) |
| 200 | 200 | FROM {$table} |
| 201 | 201 | WHERE {$column} LIKE %s |
| 202 | 202 | ", $key ) ); |
| 203 | 203 | |
| 204 | - return ( $count > 0 ) ? false : true; |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - /** |
|
| 208 | - * Is process running |
|
| 209 | - * |
|
| 210 | - * Check whether the current process is already running |
|
| 211 | - * in a background process. |
|
| 212 | - */ |
|
| 213 | - protected function is_process_running() { |
|
| 214 | - if ( get_site_transient( $this->identifier . '_process_lock' ) ) { |
|
| 215 | - // Process already running. |
|
| 216 | - return true; |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - return false; |
|
| 220 | - } |
|
| 221 | - |
|
| 222 | - /** |
|
| 223 | - * Lock process |
|
| 224 | - * |
|
| 225 | - * Lock the process so that multiple instances can't run simultaneously. |
|
| 226 | - * Override if applicable, but the duration should be greater than that |
|
| 227 | - * defined in the time_exceeded() method. |
|
| 228 | - */ |
|
| 229 | - protected function lock_process() { |
|
| 230 | - $this->start_time = time(); // Set start time of current process. |
|
| 231 | - |
|
| 232 | - $lock_duration = ( property_exists( $this, 'queue_lock_time' ) ) ? $this->queue_lock_time : 60; // 1 minute |
|
| 233 | - $lock_duration = apply_filters( $this->identifier . '_queue_lock_time', $lock_duration ); |
|
| 234 | - |
|
| 235 | - set_site_transient( $this->identifier . '_process_lock', microtime(), $lock_duration ); |
|
| 236 | - } |
|
| 237 | - |
|
| 238 | - /** |
|
| 239 | - * Unlock process |
|
| 240 | - * |
|
| 241 | - * Unlock the process so that other instances can spawn. |
|
| 242 | - * |
|
| 243 | - * @return $this |
|
| 244 | - */ |
|
| 245 | - protected function unlock_process() { |
|
| 246 | - delete_site_transient( $this->identifier . '_process_lock' ); |
|
| 247 | - |
|
| 248 | - return $this; |
|
| 249 | - } |
|
| 250 | - |
|
| 251 | - /** |
|
| 252 | - * Get batch |
|
| 253 | - * |
|
| 254 | - * @return stdClass Return the first batch from the queue |
|
| 255 | - */ |
|
| 256 | - protected function get_batch() { |
|
| 257 | - global $wpdb; |
|
| 258 | - |
|
| 259 | - $table = $wpdb->options; |
|
| 260 | - $column = 'option_name'; |
|
| 261 | - $key_column = 'option_id'; |
|
| 262 | - $value_column = 'option_value'; |
|
| 263 | - |
|
| 264 | - if ( is_multisite() ) { |
|
| 265 | - $table = $wpdb->sitemeta; |
|
| 266 | - $column = 'meta_key'; |
|
| 267 | - $key_column = 'meta_id'; |
|
| 268 | - $value_column = 'meta_value'; |
|
| 269 | - } |
|
| 270 | - |
|
| 271 | - $key = $wpdb->esc_like( $this->identifier . '_batch_' ) . '%'; |
|
| 272 | - |
|
| 273 | - $query = $wpdb->get_row( $wpdb->prepare( " |
|
| 204 | + return ( $count > 0 ) ? false : true; |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + /** |
|
| 208 | + * Is process running |
|
| 209 | + * |
|
| 210 | + * Check whether the current process is already running |
|
| 211 | + * in a background process. |
|
| 212 | + */ |
|
| 213 | + protected function is_process_running() { |
|
| 214 | + if ( get_site_transient( $this->identifier . '_process_lock' ) ) { |
|
| 215 | + // Process already running. |
|
| 216 | + return true; |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + return false; |
|
| 220 | + } |
|
| 221 | + |
|
| 222 | + /** |
|
| 223 | + * Lock process |
|
| 224 | + * |
|
| 225 | + * Lock the process so that multiple instances can't run simultaneously. |
|
| 226 | + * Override if applicable, but the duration should be greater than that |
|
| 227 | + * defined in the time_exceeded() method. |
|
| 228 | + */ |
|
| 229 | + protected function lock_process() { |
|
| 230 | + $this->start_time = time(); // Set start time of current process. |
|
| 231 | + |
|
| 232 | + $lock_duration = ( property_exists( $this, 'queue_lock_time' ) ) ? $this->queue_lock_time : 60; // 1 minute |
|
| 233 | + $lock_duration = apply_filters( $this->identifier . '_queue_lock_time', $lock_duration ); |
|
| 234 | + |
|
| 235 | + set_site_transient( $this->identifier . '_process_lock', microtime(), $lock_duration ); |
|
| 236 | + } |
|
| 237 | + |
|
| 238 | + /** |
|
| 239 | + * Unlock process |
|
| 240 | + * |
|
| 241 | + * Unlock the process so that other instances can spawn. |
|
| 242 | + * |
|
| 243 | + * @return $this |
|
| 244 | + */ |
|
| 245 | + protected function unlock_process() { |
|
| 246 | + delete_site_transient( $this->identifier . '_process_lock' ); |
|
| 247 | + |
|
| 248 | + return $this; |
|
| 249 | + } |
|
| 250 | + |
|
| 251 | + /** |
|
| 252 | + * Get batch |
|
| 253 | + * |
|
| 254 | + * @return stdClass Return the first batch from the queue |
|
| 255 | + */ |
|
| 256 | + protected function get_batch() { |
|
| 257 | + global $wpdb; |
|
| 258 | + |
|
| 259 | + $table = $wpdb->options; |
|
| 260 | + $column = 'option_name'; |
|
| 261 | + $key_column = 'option_id'; |
|
| 262 | + $value_column = 'option_value'; |
|
| 263 | + |
|
| 264 | + if ( is_multisite() ) { |
|
| 265 | + $table = $wpdb->sitemeta; |
|
| 266 | + $column = 'meta_key'; |
|
| 267 | + $key_column = 'meta_id'; |
|
| 268 | + $value_column = 'meta_value'; |
|
| 269 | + } |
|
| 270 | + |
|
| 271 | + $key = $wpdb->esc_like( $this->identifier . '_batch_' ) . '%'; |
|
| 272 | + |
|
| 273 | + $query = $wpdb->get_row( $wpdb->prepare( " |
|
| 274 | 274 | SELECT * |
| 275 | 275 | FROM {$table} |
| 276 | 276 | WHERE {$column} LIKE %s |
@@ -278,228 +278,228 @@ discard block |
||
| 278 | 278 | LIMIT 1 |
| 279 | 279 | ", $key ) ); |
| 280 | 280 | |
| 281 | - $batch = new stdClass(); |
|
| 282 | - $batch->key = $query->$column; |
|
| 283 | - $batch->data = maybe_unserialize( $query->$value_column ); |
|
| 284 | - |
|
| 285 | - return $batch; |
|
| 286 | - } |
|
| 287 | - |
|
| 288 | - /** |
|
| 289 | - * Handle |
|
| 290 | - * |
|
| 291 | - * Pass each queue item to the task handler, while remaining |
|
| 292 | - * within server memory and time limit constraints. |
|
| 293 | - */ |
|
| 294 | - protected function handle() { |
|
| 295 | - $this->lock_process(); |
|
| 296 | - |
|
| 297 | - do { |
|
| 298 | - $batch = $this->get_batch(); |
|
| 299 | - |
|
| 300 | - foreach ( $batch->data as $key => $value ) { |
|
| 301 | - $task = $this->task( $value ); |
|
| 302 | - |
|
| 303 | - if ( false !== $task ) { |
|
| 304 | - $batch->data[ $key ] = $task; |
|
| 305 | - } else { |
|
| 306 | - unset( $batch->data[ $key ] ); |
|
| 307 | - } |
|
| 308 | - |
|
| 309 | - if ( $this->time_exceeded() || $this->memory_exceeded() ) { |
|
| 310 | - // Batch limits reached. |
|
| 311 | - break; |
|
| 312 | - } |
|
| 313 | - } |
|
| 314 | - |
|
| 315 | - // Update or delete current batch. |
|
| 316 | - if ( ! empty( $batch->data ) ) { |
|
| 317 | - $this->update( $batch->key, $batch->data ); |
|
| 318 | - } else { |
|
| 319 | - $this->delete( $batch->key ); |
|
| 320 | - } |
|
| 321 | - } while ( ! $this->time_exceeded() && ! $this->memory_exceeded() && ! $this->is_queue_empty() ); |
|
| 322 | - |
|
| 323 | - $this->unlock_process(); |
|
| 324 | - |
|
| 325 | - // Start next batch or complete process. |
|
| 326 | - if ( ! $this->is_queue_empty() ) { |
|
| 327 | - $this->dispatch(); |
|
| 328 | - } else { |
|
| 329 | - $this->complete(); |
|
| 330 | - } |
|
| 331 | - |
|
| 332 | - wp_die(); |
|
| 333 | - } |
|
| 334 | - |
|
| 335 | - /** |
|
| 336 | - * Memory exceeded |
|
| 337 | - * |
|
| 338 | - * Ensures the batch process never exceeds 90% |
|
| 339 | - * of the maximum WordPress memory. |
|
| 340 | - * |
|
| 341 | - * @return bool |
|
| 342 | - */ |
|
| 343 | - protected function memory_exceeded() { |
|
| 344 | - $memory_limit = $this->get_memory_limit() * 0.9; // 90% of max memory |
|
| 345 | - $current_memory = memory_get_usage( true ); |
|
| 346 | - $return = false; |
|
| 347 | - |
|
| 348 | - if ( $current_memory >= $memory_limit ) { |
|
| 349 | - $return = true; |
|
| 350 | - } |
|
| 351 | - |
|
| 352 | - return apply_filters( $this->identifier . '_memory_exceeded', $return ); |
|
| 353 | - } |
|
| 354 | - |
|
| 355 | - /** |
|
| 356 | - * Get memory limit |
|
| 357 | - * |
|
| 358 | - * @return int |
|
| 359 | - */ |
|
| 360 | - protected function get_memory_limit() { |
|
| 361 | - if ( function_exists( 'ini_get' ) ) { |
|
| 362 | - $memory_limit = ini_get( 'memory_limit' ); |
|
| 363 | - } else { |
|
| 364 | - // Sensible default. |
|
| 365 | - $memory_limit = '128M'; |
|
| 366 | - } |
|
| 367 | - |
|
| 368 | - if ( ! $memory_limit || - 1 === intval( $memory_limit ) ) { |
|
| 369 | - // Unlimited, set to 32GB. |
|
| 370 | - $memory_limit = '32000M'; |
|
| 371 | - } |
|
| 372 | - |
|
| 373 | - return wp_convert_hr_to_bytes( $memory_limit ); |
|
| 374 | - } |
|
| 375 | - |
|
| 376 | - /** |
|
| 377 | - * Time exceeded. |
|
| 378 | - * |
|
| 379 | - * Ensures the batch never exceeds a sensible time limit. |
|
| 380 | - * A timeout limit of 30s is common on shared hosting. |
|
| 381 | - * |
|
| 382 | - * @return bool |
|
| 383 | - */ |
|
| 384 | - protected function time_exceeded() { |
|
| 385 | - $finish = $this->start_time + apply_filters( $this->identifier . '_default_time_limit', 20 ); // 20 seconds |
|
| 386 | - $return = false; |
|
| 387 | - |
|
| 388 | - if ( time() >= $finish ) { |
|
| 389 | - $return = true; |
|
| 390 | - } |
|
| 391 | - |
|
| 392 | - return apply_filters( $this->identifier . '_time_exceeded', $return ); |
|
| 393 | - } |
|
| 394 | - |
|
| 395 | - /** |
|
| 396 | - * Complete. |
|
| 397 | - * |
|
| 398 | - * Override if applicable, but ensure that the below actions are |
|
| 399 | - * performed, or, call parent::complete(). |
|
| 400 | - */ |
|
| 401 | - protected function complete() { |
|
| 402 | - // Unschedule the cron healthcheck. |
|
| 403 | - $this->clear_scheduled_event(); |
|
| 404 | - } |
|
| 405 | - |
|
| 406 | - /** |
|
| 407 | - * Schedule cron healthcheck |
|
| 408 | - * |
|
| 409 | - * @access public |
|
| 410 | - * |
|
| 411 | - * @param mixed $schedules Schedules. |
|
| 412 | - * |
|
| 413 | - * @return mixed |
|
| 414 | - */ |
|
| 415 | - public function schedule_cron_healthcheck( $schedules ) { |
|
| 416 | - $interval = apply_filters( $this->identifier . '_cron_interval', 5 ); |
|
| 417 | - |
|
| 418 | - if ( property_exists( $this, 'cron_interval' ) ) { |
|
| 419 | - $interval = apply_filters( $this->identifier . '_cron_interval', $this->cron_interval ); |
|
| 420 | - } |
|
| 421 | - |
|
| 422 | - // Adds every 5 minutes to the existing schedules. |
|
| 423 | - $schedules[ $this->identifier . '_cron_interval' ] = array( |
|
| 424 | - 'interval' => MINUTE_IN_SECONDS * $interval, |
|
| 425 | - 'display' => sprintf( __( 'Every %d Minutes' ), $interval ), |
|
| 426 | - ); |
|
| 427 | - |
|
| 428 | - return $schedules; |
|
| 429 | - } |
|
| 430 | - |
|
| 431 | - /** |
|
| 432 | - * Handle cron healthcheck |
|
| 433 | - * |
|
| 434 | - * Restart the background process if not already running |
|
| 435 | - * and data exists in the queue. |
|
| 436 | - */ |
|
| 437 | - public function handle_cron_healthcheck() { |
|
| 438 | - if ( $this->is_process_running() ) { |
|
| 439 | - // Background process already running. |
|
| 440 | - exit; |
|
| 441 | - } |
|
| 442 | - |
|
| 443 | - if ( $this->is_queue_empty() ) { |
|
| 444 | - // No data to process. |
|
| 445 | - $this->clear_scheduled_event(); |
|
| 446 | - exit; |
|
| 447 | - } |
|
| 448 | - |
|
| 449 | - $this->handle(); |
|
| 450 | - |
|
| 451 | - exit; |
|
| 452 | - } |
|
| 453 | - |
|
| 454 | - /** |
|
| 455 | - * Schedule event |
|
| 456 | - */ |
|
| 457 | - protected function schedule_event() { |
|
| 458 | - if ( ! wp_next_scheduled( $this->cron_hook_identifier ) ) { |
|
| 459 | - wp_schedule_event( time(), $this->cron_interval_identifier, $this->cron_hook_identifier ); |
|
| 460 | - } |
|
| 461 | - } |
|
| 462 | - |
|
| 463 | - /** |
|
| 464 | - * Clear scheduled event |
|
| 465 | - */ |
|
| 466 | - protected function clear_scheduled_event() { |
|
| 467 | - $timestamp = wp_next_scheduled( $this->cron_hook_identifier ); |
|
| 468 | - |
|
| 469 | - if ( $timestamp ) { |
|
| 470 | - wp_unschedule_event( $timestamp, $this->cron_hook_identifier ); |
|
| 471 | - } |
|
| 472 | - } |
|
| 473 | - |
|
| 474 | - /** |
|
| 475 | - * Cancel Process |
|
| 476 | - * |
|
| 477 | - * Stop processing queue items, clear cronjob and delete batch. |
|
| 478 | - * |
|
| 479 | - */ |
|
| 480 | - public function cancel_process() { |
|
| 481 | - if ( ! $this->is_queue_empty() ) { |
|
| 482 | - $batch = $this->get_batch(); |
|
| 483 | - |
|
| 484 | - $this->delete( $batch->key ); |
|
| 485 | - |
|
| 486 | - wp_clear_scheduled_hook( $this->cron_hook_identifier ); |
|
| 487 | - } |
|
| 488 | - |
|
| 489 | - } |
|
| 490 | - |
|
| 491 | - /** |
|
| 492 | - * Task |
|
| 493 | - * |
|
| 494 | - * Override this method to perform any actions required on each |
|
| 495 | - * queue item. Return the modified item for further processing |
|
| 496 | - * in the next pass through. Or, return false to remove the |
|
| 497 | - * item from the queue. |
|
| 498 | - * |
|
| 499 | - * @param mixed $item Queue item to iterate over. |
|
| 500 | - * |
|
| 501 | - * @return mixed |
|
| 502 | - */ |
|
| 503 | - abstract protected function task( $item ); |
|
| 281 | + $batch = new stdClass(); |
|
| 282 | + $batch->key = $query->$column; |
|
| 283 | + $batch->data = maybe_unserialize( $query->$value_column ); |
|
| 284 | + |
|
| 285 | + return $batch; |
|
| 286 | + } |
|
| 287 | + |
|
| 288 | + /** |
|
| 289 | + * Handle |
|
| 290 | + * |
|
| 291 | + * Pass each queue item to the task handler, while remaining |
|
| 292 | + * within server memory and time limit constraints. |
|
| 293 | + */ |
|
| 294 | + protected function handle() { |
|
| 295 | + $this->lock_process(); |
|
| 296 | + |
|
| 297 | + do { |
|
| 298 | + $batch = $this->get_batch(); |
|
| 299 | + |
|
| 300 | + foreach ( $batch->data as $key => $value ) { |
|
| 301 | + $task = $this->task( $value ); |
|
| 302 | + |
|
| 303 | + if ( false !== $task ) { |
|
| 304 | + $batch->data[ $key ] = $task; |
|
| 305 | + } else { |
|
| 306 | + unset( $batch->data[ $key ] ); |
|
| 307 | + } |
|
| 308 | + |
|
| 309 | + if ( $this->time_exceeded() || $this->memory_exceeded() ) { |
|
| 310 | + // Batch limits reached. |
|
| 311 | + break; |
|
| 312 | + } |
|
| 313 | + } |
|
| 314 | + |
|
| 315 | + // Update or delete current batch. |
|
| 316 | + if ( ! empty( $batch->data ) ) { |
|
| 317 | + $this->update( $batch->key, $batch->data ); |
|
| 318 | + } else { |
|
| 319 | + $this->delete( $batch->key ); |
|
| 320 | + } |
|
| 321 | + } while ( ! $this->time_exceeded() && ! $this->memory_exceeded() && ! $this->is_queue_empty() ); |
|
| 322 | + |
|
| 323 | + $this->unlock_process(); |
|
| 324 | + |
|
| 325 | + // Start next batch or complete process. |
|
| 326 | + if ( ! $this->is_queue_empty() ) { |
|
| 327 | + $this->dispatch(); |
|
| 328 | + } else { |
|
| 329 | + $this->complete(); |
|
| 330 | + } |
|
| 331 | + |
|
| 332 | + wp_die(); |
|
| 333 | + } |
|
| 334 | + |
|
| 335 | + /** |
|
| 336 | + * Memory exceeded |
|
| 337 | + * |
|
| 338 | + * Ensures the batch process never exceeds 90% |
|
| 339 | + * of the maximum WordPress memory. |
|
| 340 | + * |
|
| 341 | + * @return bool |
|
| 342 | + */ |
|
| 343 | + protected function memory_exceeded() { |
|
| 344 | + $memory_limit = $this->get_memory_limit() * 0.9; // 90% of max memory |
|
| 345 | + $current_memory = memory_get_usage( true ); |
|
| 346 | + $return = false; |
|
| 347 | + |
|
| 348 | + if ( $current_memory >= $memory_limit ) { |
|
| 349 | + $return = true; |
|
| 350 | + } |
|
| 351 | + |
|
| 352 | + return apply_filters( $this->identifier . '_memory_exceeded', $return ); |
|
| 353 | + } |
|
| 354 | + |
|
| 355 | + /** |
|
| 356 | + * Get memory limit |
|
| 357 | + * |
|
| 358 | + * @return int |
|
| 359 | + */ |
|
| 360 | + protected function get_memory_limit() { |
|
| 361 | + if ( function_exists( 'ini_get' ) ) { |
|
| 362 | + $memory_limit = ini_get( 'memory_limit' ); |
|
| 363 | + } else { |
|
| 364 | + // Sensible default. |
|
| 365 | + $memory_limit = '128M'; |
|
| 366 | + } |
|
| 367 | + |
|
| 368 | + if ( ! $memory_limit || - 1 === intval( $memory_limit ) ) { |
|
| 369 | + // Unlimited, set to 32GB. |
|
| 370 | + $memory_limit = '32000M'; |
|
| 371 | + } |
|
| 372 | + |
|
| 373 | + return wp_convert_hr_to_bytes( $memory_limit ); |
|
| 374 | + } |
|
| 375 | + |
|
| 376 | + /** |
|
| 377 | + * Time exceeded. |
|
| 378 | + * |
|
| 379 | + * Ensures the batch never exceeds a sensible time limit. |
|
| 380 | + * A timeout limit of 30s is common on shared hosting. |
|
| 381 | + * |
|
| 382 | + * @return bool |
|
| 383 | + */ |
|
| 384 | + protected function time_exceeded() { |
|
| 385 | + $finish = $this->start_time + apply_filters( $this->identifier . '_default_time_limit', 20 ); // 20 seconds |
|
| 386 | + $return = false; |
|
| 387 | + |
|
| 388 | + if ( time() >= $finish ) { |
|
| 389 | + $return = true; |
|
| 390 | + } |
|
| 391 | + |
|
| 392 | + return apply_filters( $this->identifier . '_time_exceeded', $return ); |
|
| 393 | + } |
|
| 394 | + |
|
| 395 | + /** |
|
| 396 | + * Complete. |
|
| 397 | + * |
|
| 398 | + * Override if applicable, but ensure that the below actions are |
|
| 399 | + * performed, or, call parent::complete(). |
|
| 400 | + */ |
|
| 401 | + protected function complete() { |
|
| 402 | + // Unschedule the cron healthcheck. |
|
| 403 | + $this->clear_scheduled_event(); |
|
| 404 | + } |
|
| 405 | + |
|
| 406 | + /** |
|
| 407 | + * Schedule cron healthcheck |
|
| 408 | + * |
|
| 409 | + * @access public |
|
| 410 | + * |
|
| 411 | + * @param mixed $schedules Schedules. |
|
| 412 | + * |
|
| 413 | + * @return mixed |
|
| 414 | + */ |
|
| 415 | + public function schedule_cron_healthcheck( $schedules ) { |
|
| 416 | + $interval = apply_filters( $this->identifier . '_cron_interval', 5 ); |
|
| 417 | + |
|
| 418 | + if ( property_exists( $this, 'cron_interval' ) ) { |
|
| 419 | + $interval = apply_filters( $this->identifier . '_cron_interval', $this->cron_interval ); |
|
| 420 | + } |
|
| 421 | + |
|
| 422 | + // Adds every 5 minutes to the existing schedules. |
|
| 423 | + $schedules[ $this->identifier . '_cron_interval' ] = array( |
|
| 424 | + 'interval' => MINUTE_IN_SECONDS * $interval, |
|
| 425 | + 'display' => sprintf( __( 'Every %d Minutes' ), $interval ), |
|
| 426 | + ); |
|
| 427 | + |
|
| 428 | + return $schedules; |
|
| 429 | + } |
|
| 430 | + |
|
| 431 | + /** |
|
| 432 | + * Handle cron healthcheck |
|
| 433 | + * |
|
| 434 | + * Restart the background process if not already running |
|
| 435 | + * and data exists in the queue. |
|
| 436 | + */ |
|
| 437 | + public function handle_cron_healthcheck() { |
|
| 438 | + if ( $this->is_process_running() ) { |
|
| 439 | + // Background process already running. |
|
| 440 | + exit; |
|
| 441 | + } |
|
| 442 | + |
|
| 443 | + if ( $this->is_queue_empty() ) { |
|
| 444 | + // No data to process. |
|
| 445 | + $this->clear_scheduled_event(); |
|
| 446 | + exit; |
|
| 447 | + } |
|
| 448 | + |
|
| 449 | + $this->handle(); |
|
| 450 | + |
|
| 451 | + exit; |
|
| 452 | + } |
|
| 453 | + |
|
| 454 | + /** |
|
| 455 | + * Schedule event |
|
| 456 | + */ |
|
| 457 | + protected function schedule_event() { |
|
| 458 | + if ( ! wp_next_scheduled( $this->cron_hook_identifier ) ) { |
|
| 459 | + wp_schedule_event( time(), $this->cron_interval_identifier, $this->cron_hook_identifier ); |
|
| 460 | + } |
|
| 461 | + } |
|
| 462 | + |
|
| 463 | + /** |
|
| 464 | + * Clear scheduled event |
|
| 465 | + */ |
|
| 466 | + protected function clear_scheduled_event() { |
|
| 467 | + $timestamp = wp_next_scheduled( $this->cron_hook_identifier ); |
|
| 468 | + |
|
| 469 | + if ( $timestamp ) { |
|
| 470 | + wp_unschedule_event( $timestamp, $this->cron_hook_identifier ); |
|
| 471 | + } |
|
| 472 | + } |
|
| 473 | + |
|
| 474 | + /** |
|
| 475 | + * Cancel Process |
|
| 476 | + * |
|
| 477 | + * Stop processing queue items, clear cronjob and delete batch. |
|
| 478 | + * |
|
| 479 | + */ |
|
| 480 | + public function cancel_process() { |
|
| 481 | + if ( ! $this->is_queue_empty() ) { |
|
| 482 | + $batch = $this->get_batch(); |
|
| 483 | + |
|
| 484 | + $this->delete( $batch->key ); |
|
| 485 | + |
|
| 486 | + wp_clear_scheduled_hook( $this->cron_hook_identifier ); |
|
| 487 | + } |
|
| 488 | + |
|
| 489 | + } |
|
| 490 | + |
|
| 491 | + /** |
|
| 492 | + * Task |
|
| 493 | + * |
|
| 494 | + * Override this method to perform any actions required on each |
|
| 495 | + * queue item. Return the modified item for further processing |
|
| 496 | + * in the next pass through. Or, return false to remove the |
|
| 497 | + * item from the queue. |
|
| 498 | + * |
|
| 499 | + * @param mixed $item Queue item to iterate over. |
|
| 500 | + * |
|
| 501 | + * @return mixed |
|
| 502 | + */ |
|
| 503 | + abstract protected function task( $item ); |
|
| 504 | 504 | |
| 505 | 505 | } |
| 506 | 506 | \ No newline at end of file |
@@ -55,11 +55,11 @@ discard block |
||
| 55 | 55 | public function __construct() { |
| 56 | 56 | parent::__construct(); |
| 57 | 57 | |
| 58 | - $this->cron_hook_identifier = $this->identifier . '_cron'; |
|
| 59 | - $this->cron_interval_identifier = $this->identifier . '_cron_interval'; |
|
| 58 | + $this->cron_hook_identifier = $this->identifier.'_cron'; |
|
| 59 | + $this->cron_interval_identifier = $this->identifier.'_cron_interval'; |
|
| 60 | 60 | |
| 61 | - add_action( $this->cron_hook_identifier, array( $this, 'handle_cron_healthcheck' ) ); |
|
| 62 | - add_filter( 'cron_schedules', array( $this, 'schedule_cron_healthcheck' ) ); |
|
| 61 | + add_action($this->cron_hook_identifier, array($this, 'handle_cron_healthcheck')); |
|
| 62 | + add_filter('cron_schedules', array($this, 'schedule_cron_healthcheck')); |
|
| 63 | 63 | } |
| 64 | 64 | |
| 65 | 65 | /** |
@@ -83,7 +83,7 @@ discard block |
||
| 83 | 83 | * |
| 84 | 84 | * @return $this |
| 85 | 85 | */ |
| 86 | - public function push_to_queue( $data ) { |
|
| 86 | + public function push_to_queue($data) { |
|
| 87 | 87 | $this->data[] = $data; |
| 88 | 88 | |
| 89 | 89 | return $this; |
@@ -97,8 +97,8 @@ discard block |
||
| 97 | 97 | public function save() { |
| 98 | 98 | $key = $this->generate_key(); |
| 99 | 99 | |
| 100 | - if ( ! empty( $this->data ) ) { |
|
| 101 | - update_site_option( $key, $this->data ); |
|
| 100 | + if ( ! empty($this->data)) { |
|
| 101 | + update_site_option($key, $this->data); |
|
| 102 | 102 | } |
| 103 | 103 | |
| 104 | 104 | return $this; |
@@ -112,9 +112,9 @@ discard block |
||
| 112 | 112 | * |
| 113 | 113 | * @return $this |
| 114 | 114 | */ |
| 115 | - public function update( $key, $data ) { |
|
| 116 | - if ( ! empty( $data ) ) { |
|
| 117 | - update_site_option( $key, $data ); |
|
| 115 | + public function update($key, $data) { |
|
| 116 | + if ( ! empty($data)) { |
|
| 117 | + update_site_option($key, $data); |
|
| 118 | 118 | } |
| 119 | 119 | |
| 120 | 120 | return $this; |
@@ -127,8 +127,8 @@ discard block |
||
| 127 | 127 | * |
| 128 | 128 | * @return $this |
| 129 | 129 | */ |
| 130 | - public function delete( $key ) { |
|
| 131 | - delete_site_option( $key ); |
|
| 130 | + public function delete($key) { |
|
| 131 | + delete_site_option($key); |
|
| 132 | 132 | |
| 133 | 133 | return $this; |
| 134 | 134 | } |
@@ -143,11 +143,11 @@ discard block |
||
| 143 | 143 | * |
| 144 | 144 | * @return string |
| 145 | 145 | */ |
| 146 | - protected function generate_key( $length = 64 ) { |
|
| 147 | - $unique = md5( microtime() . rand() ); |
|
| 148 | - $prepend = $this->identifier . '_batch_'; |
|
| 146 | + protected function generate_key($length = 64) { |
|
| 147 | + $unique = md5(microtime().rand()); |
|
| 148 | + $prepend = $this->identifier.'_batch_'; |
|
| 149 | 149 | |
| 150 | - return substr( $prepend . $unique, 0, $length ); |
|
| 150 | + return substr($prepend.$unique, 0, $length); |
|
| 151 | 151 | } |
| 152 | 152 | |
| 153 | 153 | /** |
@@ -160,17 +160,17 @@ discard block |
||
| 160 | 160 | // Don't lock up other requests while processing |
| 161 | 161 | session_write_close(); |
| 162 | 162 | |
| 163 | - if ( $this->is_process_running() ) { |
|
| 163 | + if ($this->is_process_running()) { |
|
| 164 | 164 | // Background process already running. |
| 165 | 165 | wp_die(); |
| 166 | 166 | } |
| 167 | 167 | |
| 168 | - if ( $this->is_queue_empty() ) { |
|
| 168 | + if ($this->is_queue_empty()) { |
|
| 169 | 169 | // No data to process. |
| 170 | 170 | wp_die(); |
| 171 | 171 | } |
| 172 | 172 | |
| 173 | - check_ajax_referer( $this->identifier, 'nonce' ); |
|
| 173 | + check_ajax_referer($this->identifier, 'nonce'); |
|
| 174 | 174 | |
| 175 | 175 | $this->handle(); |
| 176 | 176 | |
@@ -188,20 +188,20 @@ discard block |
||
| 188 | 188 | $table = $wpdb->options; |
| 189 | 189 | $column = 'option_name'; |
| 190 | 190 | |
| 191 | - if ( is_multisite() ) { |
|
| 191 | + if (is_multisite()) { |
|
| 192 | 192 | $table = $wpdb->sitemeta; |
| 193 | 193 | $column = 'meta_key'; |
| 194 | 194 | } |
| 195 | 195 | |
| 196 | - $key = $wpdb->esc_like( $this->identifier . '_batch_' ) . '%'; |
|
| 196 | + $key = $wpdb->esc_like($this->identifier.'_batch_').'%'; |
|
| 197 | 197 | |
| 198 | - $count = $wpdb->get_var( $wpdb->prepare( " |
|
| 198 | + $count = $wpdb->get_var($wpdb->prepare(" |
|
| 199 | 199 | SELECT COUNT(*) |
| 200 | 200 | FROM {$table} |
| 201 | 201 | WHERE {$column} LIKE %s |
| 202 | - ", $key ) ); |
|
| 202 | + ", $key)); |
|
| 203 | 203 | |
| 204 | - return ( $count > 0 ) ? false : true; |
|
| 204 | + return ($count > 0) ? false : true; |
|
| 205 | 205 | } |
| 206 | 206 | |
| 207 | 207 | /** |
@@ -211,7 +211,7 @@ discard block |
||
| 211 | 211 | * in a background process. |
| 212 | 212 | */ |
| 213 | 213 | protected function is_process_running() { |
| 214 | - if ( get_site_transient( $this->identifier . '_process_lock' ) ) { |
|
| 214 | + if (get_site_transient($this->identifier.'_process_lock')) { |
|
| 215 | 215 | // Process already running. |
| 216 | 216 | return true; |
| 217 | 217 | } |
@@ -229,10 +229,10 @@ discard block |
||
| 229 | 229 | protected function lock_process() { |
| 230 | 230 | $this->start_time = time(); // Set start time of current process. |
| 231 | 231 | |
| 232 | - $lock_duration = ( property_exists( $this, 'queue_lock_time' ) ) ? $this->queue_lock_time : 60; // 1 minute |
|
| 233 | - $lock_duration = apply_filters( $this->identifier . '_queue_lock_time', $lock_duration ); |
|
| 232 | + $lock_duration = (property_exists($this, 'queue_lock_time')) ? $this->queue_lock_time : 60; // 1 minute |
|
| 233 | + $lock_duration = apply_filters($this->identifier.'_queue_lock_time', $lock_duration); |
|
| 234 | 234 | |
| 235 | - set_site_transient( $this->identifier . '_process_lock', microtime(), $lock_duration ); |
|
| 235 | + set_site_transient($this->identifier.'_process_lock', microtime(), $lock_duration); |
|
| 236 | 236 | } |
| 237 | 237 | |
| 238 | 238 | /** |
@@ -243,7 +243,7 @@ discard block |
||
| 243 | 243 | * @return $this |
| 244 | 244 | */ |
| 245 | 245 | protected function unlock_process() { |
| 246 | - delete_site_transient( $this->identifier . '_process_lock' ); |
|
| 246 | + delete_site_transient($this->identifier.'_process_lock'); |
|
| 247 | 247 | |
| 248 | 248 | return $this; |
| 249 | 249 | } |
@@ -261,26 +261,26 @@ discard block |
||
| 261 | 261 | $key_column = 'option_id'; |
| 262 | 262 | $value_column = 'option_value'; |
| 263 | 263 | |
| 264 | - if ( is_multisite() ) { |
|
| 264 | + if (is_multisite()) { |
|
| 265 | 265 | $table = $wpdb->sitemeta; |
| 266 | 266 | $column = 'meta_key'; |
| 267 | 267 | $key_column = 'meta_id'; |
| 268 | 268 | $value_column = 'meta_value'; |
| 269 | 269 | } |
| 270 | 270 | |
| 271 | - $key = $wpdb->esc_like( $this->identifier . '_batch_' ) . '%'; |
|
| 271 | + $key = $wpdb->esc_like($this->identifier.'_batch_').'%'; |
|
| 272 | 272 | |
| 273 | - $query = $wpdb->get_row( $wpdb->prepare( " |
|
| 273 | + $query = $wpdb->get_row($wpdb->prepare(" |
|
| 274 | 274 | SELECT * |
| 275 | 275 | FROM {$table} |
| 276 | 276 | WHERE {$column} LIKE %s |
| 277 | 277 | ORDER BY {$key_column} ASC |
| 278 | 278 | LIMIT 1 |
| 279 | - ", $key ) ); |
|
| 279 | + ", $key)); |
|
| 280 | 280 | |
| 281 | 281 | $batch = new stdClass(); |
| 282 | 282 | $batch->key = $query->$column; |
| 283 | - $batch->data = maybe_unserialize( $query->$value_column ); |
|
| 283 | + $batch->data = maybe_unserialize($query->$value_column); |
|
| 284 | 284 | |
| 285 | 285 | return $batch; |
| 286 | 286 | } |
@@ -297,33 +297,33 @@ discard block |
||
| 297 | 297 | do { |
| 298 | 298 | $batch = $this->get_batch(); |
| 299 | 299 | |
| 300 | - foreach ( $batch->data as $key => $value ) { |
|
| 301 | - $task = $this->task( $value ); |
|
| 300 | + foreach ($batch->data as $key => $value) { |
|
| 301 | + $task = $this->task($value); |
|
| 302 | 302 | |
| 303 | - if ( false !== $task ) { |
|
| 304 | - $batch->data[ $key ] = $task; |
|
| 303 | + if (false !== $task) { |
|
| 304 | + $batch->data[$key] = $task; |
|
| 305 | 305 | } else { |
| 306 | - unset( $batch->data[ $key ] ); |
|
| 306 | + unset($batch->data[$key]); |
|
| 307 | 307 | } |
| 308 | 308 | |
| 309 | - if ( $this->time_exceeded() || $this->memory_exceeded() ) { |
|
| 309 | + if ($this->time_exceeded() || $this->memory_exceeded()) { |
|
| 310 | 310 | // Batch limits reached. |
| 311 | 311 | break; |
| 312 | 312 | } |
| 313 | 313 | } |
| 314 | 314 | |
| 315 | 315 | // Update or delete current batch. |
| 316 | - if ( ! empty( $batch->data ) ) { |
|
| 317 | - $this->update( $batch->key, $batch->data ); |
|
| 316 | + if ( ! empty($batch->data)) { |
|
| 317 | + $this->update($batch->key, $batch->data); |
|
| 318 | 318 | } else { |
| 319 | - $this->delete( $batch->key ); |
|
| 319 | + $this->delete($batch->key); |
|
| 320 | 320 | } |
| 321 | - } while ( ! $this->time_exceeded() && ! $this->memory_exceeded() && ! $this->is_queue_empty() ); |
|
| 321 | + } while ( ! $this->time_exceeded() && ! $this->memory_exceeded() && ! $this->is_queue_empty()); |
|
| 322 | 322 | |
| 323 | 323 | $this->unlock_process(); |
| 324 | 324 | |
| 325 | 325 | // Start next batch or complete process. |
| 326 | - if ( ! $this->is_queue_empty() ) { |
|
| 326 | + if ( ! $this->is_queue_empty()) { |
|
| 327 | 327 | $this->dispatch(); |
| 328 | 328 | } else { |
| 329 | 329 | $this->complete(); |
@@ -342,14 +342,14 @@ discard block |
||
| 342 | 342 | */ |
| 343 | 343 | protected function memory_exceeded() { |
| 344 | 344 | $memory_limit = $this->get_memory_limit() * 0.9; // 90% of max memory |
| 345 | - $current_memory = memory_get_usage( true ); |
|
| 345 | + $current_memory = memory_get_usage(true); |
|
| 346 | 346 | $return = false; |
| 347 | 347 | |
| 348 | - if ( $current_memory >= $memory_limit ) { |
|
| 348 | + if ($current_memory >= $memory_limit) { |
|
| 349 | 349 | $return = true; |
| 350 | 350 | } |
| 351 | 351 | |
| 352 | - return apply_filters( $this->identifier . '_memory_exceeded', $return ); |
|
| 352 | + return apply_filters($this->identifier.'_memory_exceeded', $return); |
|
| 353 | 353 | } |
| 354 | 354 | |
| 355 | 355 | /** |
@@ -358,19 +358,19 @@ discard block |
||
| 358 | 358 | * @return int |
| 359 | 359 | */ |
| 360 | 360 | protected function get_memory_limit() { |
| 361 | - if ( function_exists( 'ini_get' ) ) { |
|
| 362 | - $memory_limit = ini_get( 'memory_limit' ); |
|
| 361 | + if (function_exists('ini_get')) { |
|
| 362 | + $memory_limit = ini_get('memory_limit'); |
|
| 363 | 363 | } else { |
| 364 | 364 | // Sensible default. |
| 365 | 365 | $memory_limit = '128M'; |
| 366 | 366 | } |
| 367 | 367 | |
| 368 | - if ( ! $memory_limit || - 1 === intval( $memory_limit ) ) { |
|
| 368 | + if ( ! $memory_limit || - 1 === intval($memory_limit)) { |
|
| 369 | 369 | // Unlimited, set to 32GB. |
| 370 | 370 | $memory_limit = '32000M'; |
| 371 | 371 | } |
| 372 | 372 | |
| 373 | - return wp_convert_hr_to_bytes( $memory_limit ); |
|
| 373 | + return wp_convert_hr_to_bytes($memory_limit); |
|
| 374 | 374 | } |
| 375 | 375 | |
| 376 | 376 | /** |
@@ -382,14 +382,14 @@ discard block |
||
| 382 | 382 | * @return bool |
| 383 | 383 | */ |
| 384 | 384 | protected function time_exceeded() { |
| 385 | - $finish = $this->start_time + apply_filters( $this->identifier . '_default_time_limit', 20 ); // 20 seconds |
|
| 385 | + $finish = $this->start_time + apply_filters($this->identifier.'_default_time_limit', 20); // 20 seconds |
|
| 386 | 386 | $return = false; |
| 387 | 387 | |
| 388 | - if ( time() >= $finish ) { |
|
| 388 | + if (time() >= $finish) { |
|
| 389 | 389 | $return = true; |
| 390 | 390 | } |
| 391 | 391 | |
| 392 | - return apply_filters( $this->identifier . '_time_exceeded', $return ); |
|
| 392 | + return apply_filters($this->identifier.'_time_exceeded', $return); |
|
| 393 | 393 | } |
| 394 | 394 | |
| 395 | 395 | /** |
@@ -412,17 +412,17 @@ discard block |
||
| 412 | 412 | * |
| 413 | 413 | * @return mixed |
| 414 | 414 | */ |
| 415 | - public function schedule_cron_healthcheck( $schedules ) { |
|
| 416 | - $interval = apply_filters( $this->identifier . '_cron_interval', 5 ); |
|
| 415 | + public function schedule_cron_healthcheck($schedules) { |
|
| 416 | + $interval = apply_filters($this->identifier.'_cron_interval', 5); |
|
| 417 | 417 | |
| 418 | - if ( property_exists( $this, 'cron_interval' ) ) { |
|
| 419 | - $interval = apply_filters( $this->identifier . '_cron_interval', $this->cron_interval ); |
|
| 418 | + if (property_exists($this, 'cron_interval')) { |
|
| 419 | + $interval = apply_filters($this->identifier.'_cron_interval', $this->cron_interval); |
|
| 420 | 420 | } |
| 421 | 421 | |
| 422 | 422 | // Adds every 5 minutes to the existing schedules. |
| 423 | - $schedules[ $this->identifier . '_cron_interval' ] = array( |
|
| 423 | + $schedules[$this->identifier.'_cron_interval'] = array( |
|
| 424 | 424 | 'interval' => MINUTE_IN_SECONDS * $interval, |
| 425 | - 'display' => sprintf( __( 'Every %d Minutes' ), $interval ), |
|
| 425 | + 'display' => sprintf(__('Every %d Minutes'), $interval), |
|
| 426 | 426 | ); |
| 427 | 427 | |
| 428 | 428 | return $schedules; |
@@ -435,12 +435,12 @@ discard block |
||
| 435 | 435 | * and data exists in the queue. |
| 436 | 436 | */ |
| 437 | 437 | public function handle_cron_healthcheck() { |
| 438 | - if ( $this->is_process_running() ) { |
|
| 438 | + if ($this->is_process_running()) { |
|
| 439 | 439 | // Background process already running. |
| 440 | 440 | exit; |
| 441 | 441 | } |
| 442 | 442 | |
| 443 | - if ( $this->is_queue_empty() ) { |
|
| 443 | + if ($this->is_queue_empty()) { |
|
| 444 | 444 | // No data to process. |
| 445 | 445 | $this->clear_scheduled_event(); |
| 446 | 446 | exit; |
@@ -455,8 +455,8 @@ discard block |
||
| 455 | 455 | * Schedule event |
| 456 | 456 | */ |
| 457 | 457 | protected function schedule_event() { |
| 458 | - if ( ! wp_next_scheduled( $this->cron_hook_identifier ) ) { |
|
| 459 | - wp_schedule_event( time(), $this->cron_interval_identifier, $this->cron_hook_identifier ); |
|
| 458 | + if ( ! wp_next_scheduled($this->cron_hook_identifier)) { |
|
| 459 | + wp_schedule_event(time(), $this->cron_interval_identifier, $this->cron_hook_identifier); |
|
| 460 | 460 | } |
| 461 | 461 | } |
| 462 | 462 | |
@@ -464,10 +464,10 @@ discard block |
||
| 464 | 464 | * Clear scheduled event |
| 465 | 465 | */ |
| 466 | 466 | protected function clear_scheduled_event() { |
| 467 | - $timestamp = wp_next_scheduled( $this->cron_hook_identifier ); |
|
| 467 | + $timestamp = wp_next_scheduled($this->cron_hook_identifier); |
|
| 468 | 468 | |
| 469 | - if ( $timestamp ) { |
|
| 470 | - wp_unschedule_event( $timestamp, $this->cron_hook_identifier ); |
|
| 469 | + if ($timestamp) { |
|
| 470 | + wp_unschedule_event($timestamp, $this->cron_hook_identifier); |
|
| 471 | 471 | } |
| 472 | 472 | } |
| 473 | 473 | |
@@ -478,12 +478,12 @@ discard block |
||
| 478 | 478 | * |
| 479 | 479 | */ |
| 480 | 480 | public function cancel_process() { |
| 481 | - if ( ! $this->is_queue_empty() ) { |
|
| 481 | + if ( ! $this->is_queue_empty()) { |
|
| 482 | 482 | $batch = $this->get_batch(); |
| 483 | 483 | |
| 484 | - $this->delete( $batch->key ); |
|
| 484 | + $this->delete($batch->key); |
|
| 485 | 485 | |
| 486 | - wp_clear_scheduled_hook( $this->cron_hook_identifier ); |
|
| 486 | + wp_clear_scheduled_hook($this->cron_hook_identifier); |
|
| 487 | 487 | } |
| 488 | 488 | |
| 489 | 489 | } |
@@ -500,6 +500,6 @@ discard block |
||
| 500 | 500 | * |
| 501 | 501 | * @return mixed |
| 502 | 502 | */ |
| 503 | - abstract protected function task( $item ); |
|
| 503 | + abstract protected function task($item); |
|
| 504 | 504 | |
| 505 | 505 | } |
| 506 | 506 | \ No newline at end of file |
@@ -12,170 +12,170 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | abstract class WP_Async_Request { |
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * Prefix |
|
| 17 | - * |
|
| 18 | - * (default value: 'wp') |
|
| 19 | - * |
|
| 20 | - * @var string |
|
| 21 | - * @access protected |
|
| 22 | - */ |
|
| 23 | - protected $prefix = 'wp'; |
|
| 24 | - |
|
| 25 | - /** |
|
| 26 | - * Action |
|
| 27 | - * |
|
| 28 | - * (default value: 'async_request') |
|
| 29 | - * |
|
| 30 | - * @var string |
|
| 31 | - * @access protected |
|
| 32 | - */ |
|
| 33 | - protected $action = 'async_request'; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * Identifier |
|
| 37 | - * |
|
| 38 | - * @var mixed |
|
| 39 | - * @access protected |
|
| 40 | - */ |
|
| 41 | - protected $identifier; |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * Data |
|
| 45 | - * |
|
| 46 | - * (default value: array()) |
|
| 47 | - * |
|
| 48 | - * @var array |
|
| 49 | - * @access protected |
|
| 50 | - */ |
|
| 51 | - protected $data = array(); |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * Initiate new async request |
|
| 55 | - */ |
|
| 56 | - public function __construct() { |
|
| 57 | - $this->identifier = $this->prefix . '_' . $this->action; |
|
| 58 | - |
|
| 59 | - add_action( 'wp_ajax_' . $this->identifier, array( $this, 'maybe_handle' ) ); |
|
| 60 | - add_action( 'wp_ajax_nopriv_' . $this->identifier, array( $this, 'maybe_handle' ) ); |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * Set data used during the request |
|
| 65 | - * |
|
| 66 | - * @param array $data Data. |
|
| 67 | - * |
|
| 68 | - * @return $this |
|
| 69 | - */ |
|
| 70 | - public function data( $data ) { |
|
| 71 | - $this->data = $data; |
|
| 72 | - |
|
| 73 | - return $this; |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * Dispatch the async request |
|
| 78 | - * |
|
| 79 | - * @return array|WP_Error |
|
| 80 | - */ |
|
| 81 | - public function dispatch() { |
|
| 82 | - $url = add_query_arg( $this->get_query_args(), $this->get_query_url() ); |
|
| 83 | - $args = $this->get_post_args(); |
|
| 84 | - |
|
| 85 | - return wp_remote_post( esc_url_raw( $url ), $args ); |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * Get query args |
|
| 90 | - * |
|
| 91 | - * @return array |
|
| 92 | - */ |
|
| 93 | - protected function get_query_args() { |
|
| 94 | - if ( property_exists( $this, 'query_args' ) ) { |
|
| 95 | - return $this->query_args; |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - $args = array( |
|
| 99 | - 'action' => $this->identifier, |
|
| 100 | - 'nonce' => wp_create_nonce( $this->identifier ), |
|
| 101 | - ); |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * Filters the post arguments used during an async request. |
|
| 105 | - * |
|
| 106 | - * @param array $url |
|
| 107 | - */ |
|
| 108 | - return apply_filters( $this->identifier . '_query_args', $args ); |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * Get query URL |
|
| 113 | - * |
|
| 114 | - * @return string |
|
| 115 | - */ |
|
| 116 | - protected function get_query_url() { |
|
| 117 | - if ( property_exists( $this, 'query_url' ) ) { |
|
| 118 | - return $this->query_url; |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - $url = admin_url( 'admin-ajax.php' ); |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * Filters the post arguments used during an async request. |
|
| 125 | - * |
|
| 126 | - * @param string $url |
|
| 127 | - */ |
|
| 128 | - return apply_filters( $this->identifier . '_query_url', $url ); |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - /** |
|
| 132 | - * Get post args |
|
| 133 | - * |
|
| 134 | - * @return array |
|
| 135 | - */ |
|
| 136 | - protected function get_post_args() { |
|
| 137 | - if ( property_exists( $this, 'post_args' ) ) { |
|
| 138 | - return $this->post_args; |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - $args = array( |
|
| 142 | - 'timeout' => 0.01, |
|
| 143 | - 'blocking' => false, |
|
| 144 | - 'body' => $this->data, |
|
| 145 | - 'cookies' => $_COOKIE, |
|
| 146 | - 'sslverify' => apply_filters( 'https_local_ssl_verify', false ), |
|
| 147 | - ); |
|
| 148 | - |
|
| 149 | - /** |
|
| 150 | - * Filters the post arguments used during an async request. |
|
| 151 | - * |
|
| 152 | - * @param array $args |
|
| 153 | - */ |
|
| 154 | - return apply_filters( $this->identifier . '_post_args', $args ); |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - /** |
|
| 158 | - * Maybe handle |
|
| 159 | - * |
|
| 160 | - * Check for correct nonce and pass to handler. |
|
| 161 | - */ |
|
| 162 | - public function maybe_handle() { |
|
| 163 | - // Don't lock up other requests while processing |
|
| 164 | - session_write_close(); |
|
| 165 | - |
|
| 166 | - check_ajax_referer( $this->identifier, 'nonce' ); |
|
| 167 | - |
|
| 168 | - $this->handle(); |
|
| 169 | - |
|
| 170 | - wp_die(); |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - /** |
|
| 174 | - * Handle |
|
| 175 | - * |
|
| 176 | - * Override this method to perform any actions required |
|
| 177 | - * during the async request. |
|
| 178 | - */ |
|
| 179 | - abstract protected function handle(); |
|
| 15 | + /** |
|
| 16 | + * Prefix |
|
| 17 | + * |
|
| 18 | + * (default value: 'wp') |
|
| 19 | + * |
|
| 20 | + * @var string |
|
| 21 | + * @access protected |
|
| 22 | + */ |
|
| 23 | + protected $prefix = 'wp'; |
|
| 24 | + |
|
| 25 | + /** |
|
| 26 | + * Action |
|
| 27 | + * |
|
| 28 | + * (default value: 'async_request') |
|
| 29 | + * |
|
| 30 | + * @var string |
|
| 31 | + * @access protected |
|
| 32 | + */ |
|
| 33 | + protected $action = 'async_request'; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * Identifier |
|
| 37 | + * |
|
| 38 | + * @var mixed |
|
| 39 | + * @access protected |
|
| 40 | + */ |
|
| 41 | + protected $identifier; |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * Data |
|
| 45 | + * |
|
| 46 | + * (default value: array()) |
|
| 47 | + * |
|
| 48 | + * @var array |
|
| 49 | + * @access protected |
|
| 50 | + */ |
|
| 51 | + protected $data = array(); |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * Initiate new async request |
|
| 55 | + */ |
|
| 56 | + public function __construct() { |
|
| 57 | + $this->identifier = $this->prefix . '_' . $this->action; |
|
| 58 | + |
|
| 59 | + add_action( 'wp_ajax_' . $this->identifier, array( $this, 'maybe_handle' ) ); |
|
| 60 | + add_action( 'wp_ajax_nopriv_' . $this->identifier, array( $this, 'maybe_handle' ) ); |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * Set data used during the request |
|
| 65 | + * |
|
| 66 | + * @param array $data Data. |
|
| 67 | + * |
|
| 68 | + * @return $this |
|
| 69 | + */ |
|
| 70 | + public function data( $data ) { |
|
| 71 | + $this->data = $data; |
|
| 72 | + |
|
| 73 | + return $this; |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * Dispatch the async request |
|
| 78 | + * |
|
| 79 | + * @return array|WP_Error |
|
| 80 | + */ |
|
| 81 | + public function dispatch() { |
|
| 82 | + $url = add_query_arg( $this->get_query_args(), $this->get_query_url() ); |
|
| 83 | + $args = $this->get_post_args(); |
|
| 84 | + |
|
| 85 | + return wp_remote_post( esc_url_raw( $url ), $args ); |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * Get query args |
|
| 90 | + * |
|
| 91 | + * @return array |
|
| 92 | + */ |
|
| 93 | + protected function get_query_args() { |
|
| 94 | + if ( property_exists( $this, 'query_args' ) ) { |
|
| 95 | + return $this->query_args; |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + $args = array( |
|
| 99 | + 'action' => $this->identifier, |
|
| 100 | + 'nonce' => wp_create_nonce( $this->identifier ), |
|
| 101 | + ); |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * Filters the post arguments used during an async request. |
|
| 105 | + * |
|
| 106 | + * @param array $url |
|
| 107 | + */ |
|
| 108 | + return apply_filters( $this->identifier . '_query_args', $args ); |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * Get query URL |
|
| 113 | + * |
|
| 114 | + * @return string |
|
| 115 | + */ |
|
| 116 | + protected function get_query_url() { |
|
| 117 | + if ( property_exists( $this, 'query_url' ) ) { |
|
| 118 | + return $this->query_url; |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + $url = admin_url( 'admin-ajax.php' ); |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * Filters the post arguments used during an async request. |
|
| 125 | + * |
|
| 126 | + * @param string $url |
|
| 127 | + */ |
|
| 128 | + return apply_filters( $this->identifier . '_query_url', $url ); |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + /** |
|
| 132 | + * Get post args |
|
| 133 | + * |
|
| 134 | + * @return array |
|
| 135 | + */ |
|
| 136 | + protected function get_post_args() { |
|
| 137 | + if ( property_exists( $this, 'post_args' ) ) { |
|
| 138 | + return $this->post_args; |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + $args = array( |
|
| 142 | + 'timeout' => 0.01, |
|
| 143 | + 'blocking' => false, |
|
| 144 | + 'body' => $this->data, |
|
| 145 | + 'cookies' => $_COOKIE, |
|
| 146 | + 'sslverify' => apply_filters( 'https_local_ssl_verify', false ), |
|
| 147 | + ); |
|
| 148 | + |
|
| 149 | + /** |
|
| 150 | + * Filters the post arguments used during an async request. |
|
| 151 | + * |
|
| 152 | + * @param array $args |
|
| 153 | + */ |
|
| 154 | + return apply_filters( $this->identifier . '_post_args', $args ); |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + /** |
|
| 158 | + * Maybe handle |
|
| 159 | + * |
|
| 160 | + * Check for correct nonce and pass to handler. |
|
| 161 | + */ |
|
| 162 | + public function maybe_handle() { |
|
| 163 | + // Don't lock up other requests while processing |
|
| 164 | + session_write_close(); |
|
| 165 | + |
|
| 166 | + check_ajax_referer( $this->identifier, 'nonce' ); |
|
| 167 | + |
|
| 168 | + $this->handle(); |
|
| 169 | + |
|
| 170 | + wp_die(); |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + /** |
|
| 174 | + * Handle |
|
| 175 | + * |
|
| 176 | + * Override this method to perform any actions required |
|
| 177 | + * during the async request. |
|
| 178 | + */ |
|
| 179 | + abstract protected function handle(); |
|
| 180 | 180 | |
| 181 | 181 | } |
@@ -54,10 +54,10 @@ discard block |
||
| 54 | 54 | * Initiate new async request |
| 55 | 55 | */ |
| 56 | 56 | public function __construct() { |
| 57 | - $this->identifier = $this->prefix . '_' . $this->action; |
|
| 57 | + $this->identifier = $this->prefix.'_'.$this->action; |
|
| 58 | 58 | |
| 59 | - add_action( 'wp_ajax_' . $this->identifier, array( $this, 'maybe_handle' ) ); |
|
| 60 | - add_action( 'wp_ajax_nopriv_' . $this->identifier, array( $this, 'maybe_handle' ) ); |
|
| 59 | + add_action('wp_ajax_'.$this->identifier, array($this, 'maybe_handle')); |
|
| 60 | + add_action('wp_ajax_nopriv_'.$this->identifier, array($this, 'maybe_handle')); |
|
| 61 | 61 | } |
| 62 | 62 | |
| 63 | 63 | /** |
@@ -67,7 +67,7 @@ discard block |
||
| 67 | 67 | * |
| 68 | 68 | * @return $this |
| 69 | 69 | */ |
| 70 | - public function data( $data ) { |
|
| 70 | + public function data($data) { |
|
| 71 | 71 | $this->data = $data; |
| 72 | 72 | |
| 73 | 73 | return $this; |
@@ -79,10 +79,10 @@ discard block |
||
| 79 | 79 | * @return array|WP_Error |
| 80 | 80 | */ |
| 81 | 81 | public function dispatch() { |
| 82 | - $url = add_query_arg( $this->get_query_args(), $this->get_query_url() ); |
|
| 82 | + $url = add_query_arg($this->get_query_args(), $this->get_query_url()); |
|
| 83 | 83 | $args = $this->get_post_args(); |
| 84 | 84 | |
| 85 | - return wp_remote_post( esc_url_raw( $url ), $args ); |
|
| 85 | + return wp_remote_post(esc_url_raw($url), $args); |
|
| 86 | 86 | } |
| 87 | 87 | |
| 88 | 88 | /** |
@@ -91,13 +91,13 @@ discard block |
||
| 91 | 91 | * @return array |
| 92 | 92 | */ |
| 93 | 93 | protected function get_query_args() { |
| 94 | - if ( property_exists( $this, 'query_args' ) ) { |
|
| 94 | + if (property_exists($this, 'query_args')) { |
|
| 95 | 95 | return $this->query_args; |
| 96 | 96 | } |
| 97 | 97 | |
| 98 | 98 | $args = array( |
| 99 | 99 | 'action' => $this->identifier, |
| 100 | - 'nonce' => wp_create_nonce( $this->identifier ), |
|
| 100 | + 'nonce' => wp_create_nonce($this->identifier), |
|
| 101 | 101 | ); |
| 102 | 102 | |
| 103 | 103 | /** |
@@ -105,7 +105,7 @@ discard block |
||
| 105 | 105 | * |
| 106 | 106 | * @param array $url |
| 107 | 107 | */ |
| 108 | - return apply_filters( $this->identifier . '_query_args', $args ); |
|
| 108 | + return apply_filters($this->identifier.'_query_args', $args); |
|
| 109 | 109 | } |
| 110 | 110 | |
| 111 | 111 | /** |
@@ -114,18 +114,18 @@ discard block |
||
| 114 | 114 | * @return string |
| 115 | 115 | */ |
| 116 | 116 | protected function get_query_url() { |
| 117 | - if ( property_exists( $this, 'query_url' ) ) { |
|
| 117 | + if (property_exists($this, 'query_url')) { |
|
| 118 | 118 | return $this->query_url; |
| 119 | 119 | } |
| 120 | 120 | |
| 121 | - $url = admin_url( 'admin-ajax.php' ); |
|
| 121 | + $url = admin_url('admin-ajax.php'); |
|
| 122 | 122 | |
| 123 | 123 | /** |
| 124 | 124 | * Filters the post arguments used during an async request. |
| 125 | 125 | * |
| 126 | 126 | * @param string $url |
| 127 | 127 | */ |
| 128 | - return apply_filters( $this->identifier . '_query_url', $url ); |
|
| 128 | + return apply_filters($this->identifier.'_query_url', $url); |
|
| 129 | 129 | } |
| 130 | 130 | |
| 131 | 131 | /** |
@@ -134,7 +134,7 @@ discard block |
||
| 134 | 134 | * @return array |
| 135 | 135 | */ |
| 136 | 136 | protected function get_post_args() { |
| 137 | - if ( property_exists( $this, 'post_args' ) ) { |
|
| 137 | + if (property_exists($this, 'post_args')) { |
|
| 138 | 138 | return $this->post_args; |
| 139 | 139 | } |
| 140 | 140 | |
@@ -143,7 +143,7 @@ discard block |
||
| 143 | 143 | 'blocking' => false, |
| 144 | 144 | 'body' => $this->data, |
| 145 | 145 | 'cookies' => $_COOKIE, |
| 146 | - 'sslverify' => apply_filters( 'https_local_ssl_verify', false ), |
|
| 146 | + 'sslverify' => apply_filters('https_local_ssl_verify', false), |
|
| 147 | 147 | ); |
| 148 | 148 | |
| 149 | 149 | /** |
@@ -151,7 +151,7 @@ discard block |
||
| 151 | 151 | * |
| 152 | 152 | * @param array $args |
| 153 | 153 | */ |
| 154 | - return apply_filters( $this->identifier . '_post_args', $args ); |
|
| 154 | + return apply_filters($this->identifier.'_post_args', $args); |
|
| 155 | 155 | } |
| 156 | 156 | |
| 157 | 157 | /** |
@@ -163,7 +163,7 @@ discard block |
||
| 163 | 163 | // Don't lock up other requests while processing |
| 164 | 164 | session_write_close(); |
| 165 | 165 | |
| 166 | - check_ajax_referer( $this->identifier, 'nonce' ); |
|
| 166 | + check_ajax_referer($this->identifier, 'nonce'); |
|
| 167 | 167 | |
| 168 | 168 | $this->handle(); |
| 169 | 169 | |
@@ -17,8 +17,8 @@ |
||
| 17 | 17 | */ |
| 18 | 18 | |
| 19 | 19 | if ( ! class_exists( 'WP_Async_Request' ) ) { |
| 20 | - require_once plugin_dir_path( __FILE__ ) . 'classes/wp-async-request.php'; |
|
| 20 | + require_once plugin_dir_path( __FILE__ ) . 'classes/wp-async-request.php'; |
|
| 21 | 21 | } |
| 22 | 22 | if ( ! class_exists( 'WP_Background_Process' ) ) { |
| 23 | - require_once plugin_dir_path( __FILE__ ) . 'classes/wp-background-process.php'; |
|
| 23 | + require_once plugin_dir_path( __FILE__ ) . 'classes/wp-background-process.php'; |
|
| 24 | 24 | } |
@@ -16,9 +16,9 @@ |
||
| 16 | 16 | GitHub Branch: master |
| 17 | 17 | */ |
| 18 | 18 | |
| 19 | -if ( ! class_exists( 'WP_Async_Request' ) ) { |
|
| 20 | - require_once plugin_dir_path( __FILE__ ) . 'classes/wp-async-request.php'; |
|
| 19 | +if ( ! class_exists('WP_Async_Request')) { |
|
| 20 | + require_once plugin_dir_path(__FILE__).'classes/wp-async-request.php'; |
|
| 21 | 21 | } |
| 22 | -if ( ! class_exists( 'WP_Background_Process' ) ) { |
|
| 23 | - require_once plugin_dir_path( __FILE__ ) . 'classes/wp-background-process.php'; |
|
| 22 | +if ( ! class_exists('WP_Background_Process')) { |
|
| 23 | + require_once plugin_dir_path(__FILE__).'classes/wp-background-process.php'; |
|
| 24 | 24 | } |
@@ -21,12 +21,12 @@ |
||
| 21 | 21 | */ |
| 22 | 22 | private $body; |
| 23 | 23 | |
| 24 | - public function __construct( $response ) { |
|
| 24 | + public function __construct($response) { |
|
| 25 | 25 | |
| 26 | 26 | $this->response = $response; |
| 27 | - $this->code = wp_remote_retrieve_response_code( $this->response ); |
|
| 28 | - $this->is_success = ! empty( $this->code ) && 2 === intval( $this->code / 100 ); |
|
| 29 | - $this->body = wp_remote_retrieve_body( $this->response ); |
|
| 27 | + $this->code = wp_remote_retrieve_response_code($this->response); |
|
| 28 | + $this->is_success = ! empty($this->code) && 2 === intval($this->code / 100); |
|
| 29 | + $this->body = wp_remote_retrieve_body($this->response); |
|
| 30 | 30 | |
| 31 | 31 | } |
| 32 | 32 | |
@@ -4,46 +4,46 @@ |
||
| 4 | 4 | |
| 5 | 5 | class Response { |
| 6 | 6 | |
| 7 | - private $response; |
|
| 7 | + private $response; |
|
| 8 | 8 | |
| 9 | - /** |
|
| 10 | - * @var int|string |
|
| 11 | - */ |
|
| 12 | - private $code; |
|
| 9 | + /** |
|
| 10 | + * @var int|string |
|
| 11 | + */ |
|
| 12 | + private $code; |
|
| 13 | 13 | |
| 14 | - /** |
|
| 15 | - * @var bool |
|
| 16 | - */ |
|
| 17 | - private $is_success; |
|
| 14 | + /** |
|
| 15 | + * @var bool |
|
| 16 | + */ |
|
| 17 | + private $is_success; |
|
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * @var string |
|
| 21 | - */ |
|
| 22 | - private $body; |
|
| 19 | + /** |
|
| 20 | + * @var string |
|
| 21 | + */ |
|
| 22 | + private $body; |
|
| 23 | 23 | |
| 24 | - public function __construct( $response ) { |
|
| 24 | + public function __construct( $response ) { |
|
| 25 | 25 | |
| 26 | - $this->response = $response; |
|
| 27 | - $this->code = wp_remote_retrieve_response_code( $this->response ); |
|
| 28 | - $this->is_success = ! empty( $this->code ) && 2 === intval( $this->code / 100 ); |
|
| 29 | - $this->body = wp_remote_retrieve_body( $this->response ); |
|
| 26 | + $this->response = $response; |
|
| 27 | + $this->code = wp_remote_retrieve_response_code( $this->response ); |
|
| 28 | + $this->is_success = ! empty( $this->code ) && 2 === intval( $this->code / 100 ); |
|
| 29 | + $this->body = wp_remote_retrieve_body( $this->response ); |
|
| 30 | 30 | |
| 31 | - } |
|
| 31 | + } |
|
| 32 | 32 | |
| 33 | - public function is_success() { |
|
| 33 | + public function is_success() { |
|
| 34 | 34 | |
| 35 | - return $this->is_success; |
|
| 35 | + return $this->is_success; |
|
| 36 | 36 | |
| 37 | - } |
|
| 37 | + } |
|
| 38 | 38 | |
| 39 | - public function get_body() { |
|
| 39 | + public function get_body() { |
|
| 40 | 40 | |
| 41 | - return $this->body; |
|
| 42 | - } |
|
| 41 | + return $this->body; |
|
| 42 | + } |
|
| 43 | 43 | |
| 44 | - public function get_response() { |
|
| 44 | + public function get_response() { |
|
| 45 | 45 | |
| 46 | - return $this->response; |
|
| 47 | - } |
|
| 46 | + return $this->response; |
|
| 47 | + } |
|
| 48 | 48 | |
| 49 | 49 | } |