From b1a9eb825790e0babd193bc0359d369ca22fccb3 Mon Sep 17 00:00:00 2001 From: balexey88 Date: Sat, 6 Apr 2024 00:22:12 +0300 Subject: [PATCH] * Fix Data Optimization reporting issues * Fix Data Optimization for Multisite --- changelog.txt | 4 +++ changes.md | 4 +++ .../batch/class-batch-task-manager.php | 14 ++++---- lib/classes/class-bootstrap.php | 2 +- lib/classes/class-db.php | 4 +-- lib/classes/class-migrator.php | 33 ++++++++++--------- lib/classes/status/class-migrations.php | 4 +-- lib/cli/class-sm-cli-command.php | 2 +- readme.txt | 6 +++- static/migrations/20240219175240.php | 4 +++ wp-stateless-media.php | 2 +- 11 files changed, 47 insertions(+), 32 deletions(-) diff --git a/changelog.txt b/changelog.txt index 466871caf..7d0944286 100644 --- a/changelog.txt +++ b/changelog.txt @@ -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. diff --git a/changes.md b/changes.md index 34919436c..8cd4f42f2 100644 --- a/changes.md +++ b/changes.md @@ -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. diff --git a/lib/classes/batch/class-batch-task-manager.php b/lib/classes/batch/class-batch-task-manager.php index 790c8732a..ab800e26e 100644 --- a/lib/classes/batch/class-batch-task-manager.php +++ b/lib/classes/batch/class-batch-task-manager.php @@ -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() ); } /** @@ -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, [] ); } /** @@ -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 ); } /** @@ -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 ); } /** @@ -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 ); diff --git a/lib/classes/class-bootstrap.php b/lib/classes/class-bootstrap.php index 41d4594cb..39c30006c 100644 --- a/lib/classes/class-bootstrap.php +++ b/lib/classes/class-bootstrap.php @@ -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(); diff --git a/lib/classes/class-db.php b/lib/classes/class-db.php index 7f7b84208..d48a30ad2 100644 --- a/lib/classes/class-db.php +++ b/lib/classes/class-db.php @@ -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; @@ -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); } /** diff --git a/lib/classes/class-migrator.php b/lib/classes/class-migrator.php index 4205124f7..8d6d9de94 100644 --- a/lib/classes/class-migrator.php +++ b/lib/classes/class-migrator.php @@ -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; @@ -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); } } @@ -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); } /** @@ -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) { @@ -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); @@ -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([ @@ -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); } } @@ -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); } } @@ -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); } } @@ -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) ) { diff --git a/lib/classes/status/class-migrations.php b/lib/classes/status/class-migrations.php index 10dce1426..f7520379c 100644 --- a/lib/classes/status/class-migrations.php +++ b/lib/classes/status/class-migrations.php @@ -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 = []; @@ -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; } diff --git a/lib/cli/class-sm-cli-command.php b/lib/cli/class-sm-cli-command.php index 55e4a5db9..f732d2bb3 100644 --- a/lib/cli/class-sm-cli-command.php +++ b/lib/cli/class-sm-cli-command.php @@ -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; } diff --git a/readme.txt b/readme.txt index 14140d8ba..c67833a65 100644 --- a/readme.txt +++ b/readme.txt @@ -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. @@ -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. diff --git a/static/migrations/20240219175240.php b/static/migrations/20240219175240.php index f08bc5107..c00f7ca64 100644 --- a/static/migrations/20240219175240.php +++ b/static/migrations/20240219175240.php @@ -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;' ); @@ -352,6 +354,8 @@ public function process_item($item) { $wpdb->query( 'SET autocommit = 1;' ); + $wpdb->suppress_errors($old_suppress); + return false; } diff --git a/wp-stateless-media.php b/wp-stateless-media.php index 69630abd8..93eeb043a 100644 --- a/wp-stateless-media.php +++ b/wp-stateless-media.php @@ -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