Skip to content

Commit

Permalink
Merge pull request #4066 from impress-org/issue/4065
Browse files Browse the repository at this point in the history
fix: make db upgrade background process compatible with multisite #4065
  • Loading branch information
Devin Walker committed Apr 2, 2019
2 parents 458424b + 423d1ae commit 1a68cb7
Showing 1 changed file with 141 additions and 3 deletions.
144 changes: 141 additions & 3 deletions includes/class-give-background-updater.php
Expand Up @@ -47,7 +47,7 @@ public function dispatch() {
* @return stdClass
*/
public function get_all_batch() {
return parent::get_batch();
return $this->get_batch();
}

/**
Expand All @@ -58,7 +58,7 @@ public function get_all_batch() {
* @return bool
*/
public function has_queue() {
return ( ! parent::is_queue_empty() );
return ( ! $this->is_queue_empty() );
}


Expand Down Expand Up @@ -99,7 +99,7 @@ protected function lock_process() {
$lock_duration = ( property_exists( $this, 'queue_lock_time' ) ) ? $this->queue_lock_time : 60; // 1 minute
$lock_duration = apply_filters( $this->identifier . '_queue_lock_time', $lock_duration );

set_site_transient( $this->identifier . '_process_lock', microtime(), $lock_duration );
set_transient( $this->identifier . '_process_lock', microtime(), $lock_duration );
}

/**
Expand Down Expand Up @@ -133,6 +133,144 @@ protected function schedule_event() {
}
}

/**
* Is queue empty
*
* @since 2.4.5
*
* @return bool
*/
protected function is_queue_empty() {
global $wpdb;

$table = $wpdb->options;
$column = 'option_name';

$key = $wpdb->esc_like( $this->identifier . '_batch_' ) . '%';

$count = $wpdb->get_var( $wpdb->prepare( "
SELECT COUNT(*)
FROM {$table}
WHERE {$column} LIKE %s
", $key ) );

return ! ( $count > 0 );
}

/**
* Get batch
*
* @since 2.4.5
*
* @return stdClass Return the first batch from the queue
*/
protected function get_batch() {
global $wpdb;

$table = $wpdb->options;
$column = 'option_name';
$key_column = 'option_id';
$value_column = 'option_value';

$key = $wpdb->esc_like( $this->identifier . '_batch_' ) . '%';

$query = $wpdb->get_row( $wpdb->prepare( "
SELECT *
FROM {$table}
WHERE {$column} LIKE %s
ORDER BY {$key_column} ASC
LIMIT 1
", $key ) );

$batch = new stdClass();
$batch->key = $query->$column;
$batch->data = maybe_unserialize( $query->$value_column );

return $batch;
}

/**
* Save queue
*
* @since 2.4.5
*
* @return $this
*/
public function save() {
$key = $this->generate_key();

if ( ! empty( $this->data ) ) {
update_option( $key, $this->data );
}

return $this;
}

/**
* Update queue
*
* @since 2.4.5
*
* @param string $key Key.
* @param array $data Data.
*
* @return $this
*/
public function update( $key, $data ) {
if ( ! empty( $data ) ) {
update_option( $key, $data );
}

return $this;
}

/**
* Delete queue
*
* @since 2.4.5
*
* @param string $key Key.
*
* @return $this
*/
public function delete( $key ) {
delete_option( $key );

return $this;
}

/**
* Is process running
*
* @since 2.4.5
*
* Check whether the current process is already running
* in a background process.
*/
public function is_process_running() {
if ( get_transient( $this->identifier . '_process_lock' ) ) {
// Process already running.
return true;
}

return false;
}

/**
* Unlock process
*
* Unlock the process so that other instances can spawn.
*
* @since 2.4.5
*
* @return $this
*/
protected function unlock_process() {
delete_transient( $this->identifier . '_process_lock' );

return $this;
}

/**
* Task
*
Expand Down

0 comments on commit 1a68cb7

Please sign in to comment.