Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Data Optimization #737

Merged
merged 1 commit into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
== Changelog ==
= 4.0.1 =
* FIX - improvements to Data Optimization process.
* FIX - Data Optimization fixed for multisite environment.

= 4.0.0 =
* NEW - use custom database tables to store GCS file data. This increases plugin performance and will be used for future improvements.
* NEW - added filter `wp_stateless_get_file`, retrieves the GCS file data, should be used instead of getting `sm_cloud` postmeta directly.
Expand Down
4 changes: 4 additions & 0 deletions changes.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#### 4.0.1
* FIX - improvements to Data Optimization process.
* FIX - Data Optimization fixed for multisite environment.

#### 4.0.0
* NEW - use custom database tables to store GCS file data. This increases plugin performance and will be used for future improvements.
* NEW - added filter `wp_stateless_get_file`, retrieves the GCS file data, should be used instead of getting `sm_cloud` postmeta directly.
Expand Down
14 changes: 6 additions & 8 deletions lib/classes/batch/class-batch-task-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ private function _check_force_continue() {
* @return array
*/
private function _update_state($state) {
update_site_option( $this->identifier . self::STATE_KEY, $state );
update_site_option( $this->identifier . self::UPDATED_KEY, time() );
update_option( $this->identifier . self::STATE_KEY, $state );
update_option( $this->identifier . self::UPDATED_KEY, time() );
}

/**
Expand All @@ -87,7 +87,7 @@ private function _update_state($state) {
* @return array
*/
private function _get_state() {
return get_site_option( $this->identifier . self::STATE_KEY, [] );
return get_option( $this->identifier . self::STATE_KEY, [] );
}

/**
Expand All @@ -96,7 +96,7 @@ private function _get_state() {
* @return int|null
*/
private function _get_last_updated() {
return get_site_option( $this->identifier . self::UPDATED_KEY, null );
return get_option( $this->identifier . self::UPDATED_KEY, null );
}

/**
Expand All @@ -105,8 +105,8 @@ private function _get_last_updated() {
* @return array
*/
private function _delete_state() {
delete_site_option( $this->identifier . self::STATE_KEY );
delete_site_option( $this->identifier . self::UPDATED_KEY );
delete_option( $this->identifier . self::STATE_KEY );
delete_option( $this->identifier . self::UPDATED_KEY );
}

/**
Expand Down Expand Up @@ -226,14 +226,12 @@ public function task($item) {
*/
protected function complete() {
$class = '';
$description = 'Batch process';

// Check if we have more batched to run
try {
$object = $this->_get_batch_task_object();
$class = $object::class;
$batch = $object->get_batch();
$description = $object->get_description();

if ( !empty($batch) ) {
$this->_add_batch( $batch );
Expand Down
2 changes: 1 addition & 1 deletion lib/classes/class-bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ public function init() {
add_action('wp_stateless_send_admin_email', array($this, 'send_admin_email'), 10, 3);

// Should be created unconditionally and as early as possible to handle batch migration requests
BatchTaskManager::instance();
Migrator::instance();
BatchTaskManager::instance();

new SyncNonMedia();

Expand Down
4 changes: 2 additions & 2 deletions lib/classes/class-db.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public function __get($property) {
* Creates or updates DB structure
*/
public function create_db() {
$version = get_site_option('sm_db_version');
$version = get_option('sm_db_version');

if ($version === self::DB_VERSION) {
return;
Expand Down Expand Up @@ -197,7 +197,7 @@ public function create_db() {
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);

update_site_option('sm_db_version', self::DB_VERSION);
update_option('sm_db_version', self::DB_VERSION);
}

/**
Expand Down
33 changes: 17 additions & 16 deletions lib/classes/class-migrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ private function _get_object($id) {
*/
private function _check_required_migrations($migrations = null) {
if ( empty($migrations) ) {
$migrations = get_site_option(self::MIGRATIONS_KEY, []);
$migrations = get_option(self::MIGRATIONS_KEY, []);
}

$require_migrations = false;
Expand All @@ -163,12 +163,12 @@ private function _check_required_migrations($migrations = null) {
}

if ( $require_migrations ) {
update_site_option(self::MIGRATIONS_NOTIFY_KEY, self::NOTIFY_REQUIRE);
delete_site_option(self::MIGRATIONS_NOTIFY_DISMISSED_KEY);
update_option(self::MIGRATIONS_NOTIFY_KEY, self::NOTIFY_REQUIRE);
delete_option(self::MIGRATIONS_NOTIFY_DISMISSED_KEY);
} else {
$notify = get_site_option(self::MIGRATIONS_NOTIFY_KEY, false);
$notify = get_option(self::MIGRATIONS_NOTIFY_KEY, false);

empty($notify) ? delete_site_option(self::MIGRATIONS_NOTIFY_KEY) : update_site_option(self::MIGRATIONS_NOTIFY_KEY, self::NOTIFY_FINISHED);
empty($notify) ? delete_option(self::MIGRATIONS_NOTIFY_KEY) : update_option(self::MIGRATIONS_NOTIFY_KEY, self::NOTIFY_FINISHED);
}
}

Expand All @@ -178,7 +178,7 @@ private function _check_required_migrations($migrations = null) {
* @param string $option_name
*/
public function notice_dismissed($option_name) {
delete_site_option(self::MIGRATIONS_NOTIFY_KEY);
delete_option(self::MIGRATIONS_NOTIFY_KEY);
}

/**
Expand All @@ -192,7 +192,7 @@ public function migrate() {
// Rebuild the migrations list and state according to the new version
$ids = $this->_get_migration_ids();

$migrations = get_site_option(self::MIGRATIONS_KEY, []);
$migrations = get_option(self::MIGRATIONS_KEY, []);
$existing = array_keys($migrations);

foreach ($ids as $id) {
Expand All @@ -219,7 +219,7 @@ public function migrate() {

krsort($migrations);

update_site_option(self::MIGRATIONS_KEY, $migrations);
update_option(self::MIGRATIONS_KEY, $migrations);

// Check if we need to run any migrations
$this->_check_required_migrations($migrations);
Expand All @@ -230,7 +230,7 @@ public function migrate() {
*/
public function show_messages() {
$is_running = BatchTaskManager::instance()->is_processing() || BatchTaskManager::instance()->is_paused();
$notify = get_site_option(self::MIGRATIONS_NOTIFY_KEY, false);
$notify = get_option(self::MIGRATIONS_NOTIFY_KEY, false);

if ( $notify ) {
ud_get_stateless_media()->errors->add([
Expand Down Expand Up @@ -271,14 +271,15 @@ public function show_messages() {
* @param string $file
*/
public function migration_started($class, $file) {
$migrations = get_site_option(self::MIGRATIONS_KEY, []);
$migrations = get_option(self::MIGRATIONS_KEY, []);
$id = $this->_file_to_id($file);

if ( array_key_exists($id, $migrations) ) {
$migrations[$id]['status'] = self::STATUS_RUNNING;
$migrations[$id]['started'] = time();
$migrations[$id]['finished'] = '';

update_site_option(self::MIGRATIONS_KEY, $migrations);
update_option(self::MIGRATIONS_KEY, $migrations);
}
}

Expand All @@ -290,14 +291,14 @@ public function migration_started($class, $file) {
* @param string $message
*/
public function migration_failed($class, $file, $message) {
$migrations = get_site_option(self::MIGRATIONS_KEY, []);
$migrations = get_option(self::MIGRATIONS_KEY, []);
$id = $this->_file_to_id($file);

if ( array_key_exists($id, $migrations) ) {
$migrations[$id]['status'] = self::STATUS_FAILED;
$migrations[$id]['message'] = $message;

update_site_option(self::MIGRATIONS_KEY, $migrations);
update_option(self::MIGRATIONS_KEY, $migrations);
$this->_check_required_migrations($migrations);
}
}
Expand All @@ -308,14 +309,14 @@ public function migration_failed($class, $file, $message) {
* @param string $class
*/
public function migration_finished($class) {
$migrations = get_site_option(self::MIGRATIONS_KEY, []);
$migrations = get_option(self::MIGRATIONS_KEY, []);
$id = $this->_class_to_id($class);

if ( array_key_exists($id, $migrations) ) {
$migrations[$id]['status'] = self::STATUS_FINISHED;
$migrations[$id]['finished'] = time();

update_site_option(self::MIGRATIONS_KEY, $migrations);
update_option(self::MIGRATIONS_KEY, $migrations);
$this->_check_required_migrations($migrations);
}
}
Expand All @@ -335,7 +336,7 @@ public function start_migration($state, $params) {
}

$id = $params['id'];
$migrations = get_site_option(self::MIGRATIONS_KEY, []);
$migrations = get_option(self::MIGRATIONS_KEY, []);

// Unknown migration?
if ( !array_key_exists($id, $migrations) ) {
Expand Down
4 changes: 2 additions & 2 deletions lib/classes/status/class-migrations.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public static function get_status_text($status) {
*/
private function _get_migrations_state($state = null) {
$migrations = [];
$this->migrations = get_site_option(Migrator::MIGRATIONS_KEY, []);
$this->migrations = get_option(Migrator::MIGRATIONS_KEY, []);

if ( !is_array($this->migrations) ) {
$this->migrations = [];
Expand Down Expand Up @@ -237,7 +237,7 @@ public function migrations_state($state, $params) {
}

$state['migrations'] = $this->_get_migrations_state($state);
$state['migrations_notify'] = get_site_option(Migrator::MIGRATIONS_NOTIFY_KEY, false);
$state['migrations_notify'] = get_option(Migrator::MIGRATIONS_NOTIFY_KEY, false);

return $state;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/cli/class-sm-cli-command.php
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ private function _check_progress($progress) {
$state = maybe_unserialize($state);

if ( empty($state) || !isset($state['is_migration']) || !$state['is_migration'] ) {
WP_CLI::success('No migration in progress');
WP_CLI::success('All migrations finished.');
return;
}

Expand Down
6 changes: 5 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ License: GPLv2 or later
Requires PHP: 8.0
Requires at least: 5.0
Tested up to: 6.5.0
Stable tag: 4.0.0
Stable tag: 4.0.1

Upload and serve your WordPress media files from Google Cloud Storage.

Expand Down Expand Up @@ -121,6 +121,10 @@ Before upgrading to WP-Stateless 3.2.0, please, make sure you use PHP 7.2 or abo
Before upgrading to WP-Stateless 3.0, please, make sure you tested it on your development environment.

== Changelog ==
= 4.0.1 =
* FIX - improvements to Data Optimization process.
* FIX - Data Optimization fixed for multisite environment.

= 4.0.0 =
* NEW - use custom database tables to store GCS file data. This increases plugin performance and will be used for future improvements.
* NEW - added filter `wp_stateless_get_file`, retrieves the GCS file data, should be used instead of getting `sm_cloud` postmeta directly.
Expand Down
4 changes: 4 additions & 0 deletions static/migrations/20240219175240.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,8 @@ public function process_item($item) {

$wp_meta = get_post_meta( $item, '_wp_attachment_metadata', true );

$old_suppress = $wpdb->suppress_errors();

// Disable autocommit and use transactions to ensure data integrity
$wpdb->query( 'SET autocommit = 0;' );
$wpdb->query( 'START TRANSACTION;' );
Expand Down Expand Up @@ -352,6 +354,8 @@ public function process_item($item) {

$wpdb->query( 'SET autocommit = 1;' );

$wpdb->suppress_errors($old_suppress);

return false;
}

Expand Down
2 changes: 1 addition & 1 deletion wp-stateless-media.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Plugin URI: https://stateless.udx.io/
* Description: Upload and serve your WordPress media files from Google Cloud Storage.
* Author: UDX
* Version: 4.0.0
* Version: 4.0.1
* Text Domain: stateless-media
* Author URI: https://udx.io
* License: GPLv2 or later
Expand Down
Loading