| @@ -3,8 +3,11 @@ | ||
| 3 | 3 | |
| 4 | 4 | /** |
| 5 | 5 | * WP Background Process |
| 6 | 6 | * |
| 7 | + * Multisite: queue batches and process locks use per-blog options/transients (not network | |
| 8 | + * sitemeta). SellKit tables live in each site's $wpdb->prefix; DB version is per-site in the | |
| 9 | + * sellkit option — a network-wide queue caused cross-site contention and duplicate migrations. | |
| 7 | 10 | * |
| 8 | 11 | * @package WP-Background-Processing |
| 9 | 12 | */ |
| 10 | 13 | |
| @@ -105,9 +108,9 @@ | ||
| 105 | 108 | public function save() { |
| 106 | 109 | $key = $this->generate_key(); |
| 107 | 110 | |
| 108 | 111 | if ( ! empty( $this->data ) ) { |
| 109 | - update_site_option( $key, $this->data ); | |
| 112 | + update_option( $key, $this->data, false ); | |
| 110 | 113 | } |
| 111 | 114 | |
| 112 | 115 | return $this; |
| 113 | 116 | } |
| @@ -121,9 +124,9 @@ | ||
| 121 | 124 | * @return $this |
| 122 | 125 | */ |
| 123 | 126 | public function update( $key, $data ) { |
| 124 | 127 | if ( ! empty( $data ) ) { |
| 125 | - update_site_option( $key, $data ); | |
| 128 | + update_option( $key, $data, false ); | |
| 126 | 129 | } |
| 127 | 130 | |
| 128 | 131 | return $this; |
| 129 | 132 | } |
| @@ -135,9 +138,9 @@ | ||
| 135 | 138 | * |
| 136 | 139 | * @return $this |
| 137 | 140 | */ |
| 138 | 141 | public function delete( $key ) { |
| 139 | - delete_site_option( $key ); | |
| 142 | + delete_option( $key ); | |
| 140 | 143 | |
| 141 | 144 | return $this; |
| 142 | 145 | } |
| 143 | 146 | |
| @@ -195,13 +198,8 @@ | ||
| 195 | 198 | |
| 196 | 199 | $table = $wpdb->options; |
| 197 | 200 | $column = 'option_name'; |
| 198 | 201 | |
| 199 | - if ( is_multisite() ) { | |
| 200 | - $table = $wpdb->sitemeta; | |
| 201 | - $column = 'meta_key'; | |
| 202 | - } | |
| 203 | - | |
| 204 | 202 | $key = $wpdb->esc_like( $this->identifier . '_batch_' ) . '%'; |
| 205 | 203 | |
| 206 | 204 | $count = $wpdb->get_var( $wpdb->prepare( " |
| 207 | 205 | SELECT COUNT(*) |
| @@ -218,9 +216,9 @@ | ||
| 218 | 216 | * Check whether the current process is already running |
| 219 | 217 | * in a background process. |
| 220 | 218 | */ |
| 221 | 219 | protected function is_process_running() { |
| 222 | - if ( get_site_transient( $this->identifier . '_process_lock' ) ) { | |
| 220 | + if ( get_transient( $this->identifier . '_process_lock' ) ) { | |
| 223 | 221 | // Process already running. |
| 224 | 222 | return true; |
| 225 | 223 | } |
| 226 | 224 | |
| @@ -239,9 +237,9 @@ | ||
| 239 | 237 | |
| 240 | 238 | $lock_duration = ( property_exists( $this, 'queue_lock_time' ) ) ? $this->queue_lock_time : 60; // 1 minute |
| 241 | 239 | $lock_duration = apply_filters( $this->identifier . '_queue_lock_time', $lock_duration ); |
| 242 | 240 | |
| 243 | - set_site_transient( $this->identifier . '_process_lock', microtime(), $lock_duration ); | |
| 241 | + set_transient( $this->identifier . '_process_lock', microtime(), $lock_duration ); | |
| 244 | 242 | } |
| 245 | 243 | |
| 246 | 244 | /** |
| 247 | 245 | * Unlock process |
| @@ -250,9 +248,9 @@ | ||
| 250 | 248 | * |
| 251 | 249 | * @return $this |
| 252 | 250 | */ |
| 253 | 251 | protected function unlock_process() { |
| 254 | - delete_site_transient( $this->identifier . '_process_lock' ); | |
| 252 | + delete_transient( $this->identifier . '_process_lock' ); | |
| 255 | 253 | |
| 256 | 254 | return $this; |
| 257 | 255 | } |
| 258 | 256 | |
| @@ -268,15 +266,8 @@ | ||
| 268 | 266 | $column = 'option_name'; |
| 269 | 267 | $key_column = 'option_id'; |
| 270 | 268 | $value_column = 'option_value'; |
| 271 | 269 | |
| 272 | - if ( is_multisite() ) { | |
| 273 | - $table = $wpdb->sitemeta; | |
| 274 | - $column = 'meta_key'; | |
| 275 | - $key_column = 'meta_id'; | |
| 276 | - $value_column = 'meta_value'; | |
| 277 | - } | |
| 278 | - | |
| 279 | 270 | $key = $wpdb->esc_like( $this->identifier . '_batch_' ) . '%'; |
| 280 | 271 | |
| 281 | 272 | $query = $wpdb->get_row( $wpdb->prepare( " |
| 282 | 273 | SELECT * |
| @@ -285,9 +276,16 @@ | ||
| 285 | 276 | ORDER BY {$key_column} ASC |
| 286 | 277 | LIMIT 1 |
| 287 | 278 | ", $key ) ); |
| 288 | 279 | |
| 289 | - $batch = new stdClass(); | |
| 280 | + $batch = new stdClass(); | |
| 281 | + | |
| 282 | + if ( ! $query ) { | |
| 283 | + $batch->key = ''; | |
| 284 | + $batch->data = array(); | |
| 285 | + return $batch; | |
| 286 | + } | |
| 287 | + | |
| 290 | 288 | $batch->key = $query->$column; |
| 291 | 289 | $batch->data = maybe_unserialize( $query->$value_column ); |
| 292 | 290 | |
| 293 | 291 | return $batch; |
| @@ -303,8 +301,12 @@ | ||
| 303 | 301 | $this->lock_process(); |
| 304 | 302 | |
| 305 | 303 | do { |
| 306 | 304 | $batch = $this->get_batch(); |
| 305 | + | |
| 306 | + if ( empty( $batch->key ) ) { | |
| 307 | + break; | |
| 308 | + } | |
| 307 | 309 | |
| 308 | 310 | foreach ( $batch->data as $key => $value ) { |
| 309 | 311 | $task = $this->task( $value ); |
| 310 | 312 | |