From 932c79966aa82017cd1cdc6aa4d828553767bdcb Mon Sep 17 00:00:00 2001
From: balexey88
Date: Wed, 3 Apr 2024 05:17:40 +0300
Subject: [PATCH 1/2] * Add 'wp_stateless_get_setting_...' filter for
overriding WP-Stateless settings * Fix migration for 'filemd5' meta * Fix:
loading assets with the proper plugin version * Add Setting to switch back to
postmeta
---
changelog.txt | 4 +++-
changes.md | 4 +++-
lib/classes/class-bootstrap.php | 4 +++-
lib/classes/class-db.php | 6 +++---
lib/classes/class-settings.php | 23 +++++++++++++++++++++
lib/classes/class-utility.php | 7 +++++--
readme.txt | 8 ++++---
static/migrations/20240219175240.php | 2 +-
static/views/settings-sections/file-url.php | 17 +++++++++++++++
wp-stateless-media.php | 2 +-
10 files changed, 64 insertions(+), 13 deletions(-)
diff --git a/changelog.txt b/changelog.txt
index 502198405..466871caf 100644
--- a/changelog.txt
+++ b/changelog.txt
@@ -5,7 +5,9 @@
* NEW - added filter `wp_stateless_get_file_sizes`, retrieves the GCS file data for image sizes, should be used instead of getting `sm_cloud` postmeta directly.
* NEW - added filter `wp_stateless_get_file_meta`, retrieves all GCS file meta data, should be used instead of getting `sm_cloud` postmeta directly.
* NEW - added filter `wp_stateless_get_file_meta_value`, retrieves the GCS file meta data by meta_key, should be used instead of getting `sm_cloud` postmeta directly.
-* NEW - added setting allowing to change email for WP-Stateless notifications.
+* NEW - added filter `wp_stateless_get_setting_...` which allows to override any WP-Stateless setting.
+* NEW - added setting "Send Status Emails" allowing to change email for WP-Stateless notifications.
+* NEW - added setting "Use Post Meta" allowing to switch back to using `postmeta` instead of custom DB tables. Can be used in case of issues after upgrading to 4.0.0.
* NEW - added new Settings tab `Addons`, which contains the list of WP-Stateless Addons, which replace Compatibilities.
* NEW - added new Settings tab `Status`, which contains status and health information related to Google Cloud Storage and WP-Stateless.
* NEW - CLI command `wp stateless migrate` to list and operate data optimizations.
diff --git a/changes.md b/changes.md
index 9160496e5..34919436c 100644
--- a/changes.md
+++ b/changes.md
@@ -4,7 +4,9 @@
* NEW - added filter `wp_stateless_get_file_sizes`, retrieves the GCS file data for image sizes, should be used instead of getting `sm_cloud` postmeta directly.
* NEW - added filter `wp_stateless_get_file_meta`, retrieves all GCS file meta data, should be used instead of getting `sm_cloud` postmeta directly.
* NEW - added filter `wp_stateless_get_file_meta_value`, retrieves the GCS file meta data by meta_key, should be used instead of getting `sm_cloud` postmeta directly.
-* NEW - added setting allowing to change email for WP-Stateless notifications.
+* NEW - added filter `wp_stateless_get_setting_...` which allows to override any WP-Stateless setting.
+* NEW - added setting "Send Status Emails" allowing to change email for WP-Stateless notifications.
+* NEW - added setting "Use Post Meta" allowing to switch back to using `postmeta` instead of custom DB tables. Can be used in case of issues after upgrading to 4.0.0.
* NEW - added new Settings tab `Addons`, which contains the list of WP-Stateless Addons, which replace Compatibilities.
* NEW - added new Settings tab `Status`, which contains status and health information related to Google Cloud Storage and WP-Stateless.
* NEW - CLI command `wp stateless migrate` to list and operate data optimizations.
diff --git a/lib/classes/class-bootstrap.php b/lib/classes/class-bootstrap.php
index 262e1b715..41d4594cb 100644
--- a/lib/classes/class-bootstrap.php
+++ b/lib/classes/class-bootstrap.php
@@ -64,6 +64,8 @@ protected function __construct($args) {
parent::__construct($args);
+ self::$version = $args['version'] ?? self::$version;
+
/**
* Add custom args to api ping request
*/
@@ -1874,7 +1876,7 @@ public function attachment_url_to_postid($post_id, $url) {
// User can use this constant if they change the Bucket Folder (root_dir) after uploading image.
// This can be little slow at first run.
if (empty($post_id)) {
- if ( !defined('WP_STATELESS_POSTMETA') || !WP_STATELESS_POSTMETA ) {
+ if ( !$this->get('sm.use_postmeta') ) {
$query = 'SELECT post_id FROM ' . ud_stateless_db()->files . ' WHERE file_link = %s';
$post_id = $wpdb->get_var($wpdb->prepare($query, $url));
}
diff --git a/lib/classes/class-db.php b/lib/classes/class-db.php
index d4dfa7746..7f7b84208 100644
--- a/lib/classes/class-db.php
+++ b/lib/classes/class-db.php
@@ -705,7 +705,7 @@ public function get_total_files() {
* @return array
*/
public function get_file($meta, $attachment_id, $with_sizes = false) {
- if ( defined('WP_STATELESS_POSTMETA') && WP_STATELESS_POSTMETA ) {
+ if ( ud_get_stateless_media()->get('sm.use_postmeta') ) {
$meta = get_post_meta($attachment_id, 'sm_cloud', true);
if ( !empty($meta) ) {
@@ -751,7 +751,7 @@ public function get_file($meta, $attachment_id, $with_sizes = false) {
* @return array
*/
public function get_file_sizes($sizes, $attachment_id) {
- if ( defined('WP_STATELESS_POSTMETA') && WP_STATELESS_POSTMETA ) {
+ if ( ud_get_stateless_media()->get('sm.use_postmeta') ) {
$meta = get_post_meta($attachment_id, 'sm_cloud', true);
return isset($meta['sizes']) ? $meta['sizes'] : [];
}
@@ -796,7 +796,7 @@ public function get_file_sizes($sizes, $attachment_id) {
* @return mixed
*/
public function get_file_meta_value($value, $post_id, $key, $default = null) {
- if ( defined('WP_STATELESS_POSTMETA') && WP_STATELESS_POSTMETA ) {
+ if ( ud_get_stateless_media()->get('sm.use_postmeta') ) {
$meta = get_post_meta($post_id, 'sm_cloud', []);
return isset($meta[$key]) ? $meta[$key] : $default;
diff --git a/lib/classes/class-settings.php b/lib/classes/class-settings.php
index cd11a4ab5..0b71d1579 100644
--- a/lib/classes/class-settings.php
+++ b/lib/classes/class-settings.php
@@ -48,6 +48,7 @@ final class Settings extends \UDX\Settings {
'dynamic_image_support' => array(['WP_STATELESS_MEDIA_ON_FLY' => 'WP_STATELESS_DYNAMIC_IMAGE_SUPPORT'], 'false'),
'status_email_type' => array('', 'true'),
'status_email_address' => array('', ''),
+ 'use_postmeta' => array('WP_STATELESS_POSTMETA', ['false', '']),
);
private $network_only_settings = array(
@@ -626,6 +627,28 @@ public function processing_tab_content() {
include ud_get_stateless_media()->path('static/views/processing_interface.php', 'dir');
}
+
+ /**
+ * Getter for settings
+ *
+ * @param string|bool $key
+ * @param mixed $default
+ * @return mixed
+ */
+ public function get( $key = false, $default = false ) {
+ $value = parent::get( $key, $default );
+
+ if ( $key === 'sm' && is_array($value) ) {
+ foreach ( $value as $key => $val ) {
+ $value[$key] = apply_filters("wp_stateless_get_setting_$key", $val, $default);
+ }
+ } else if ( is_string($key) ) {
+ $key = str_replace('sm.', '', $key);
+ $value = apply_filters("wp_stateless_get_setting_$key", $value, $default);
+ }
+
+ return $value;
+ }
}
}
diff --git a/lib/classes/class-utility.php b/lib/classes/class-utility.php
index c95089bc6..22388b785 100644
--- a/lib/classes/class-utility.php
+++ b/lib/classes/class-utility.php
@@ -920,8 +920,11 @@ public static function convert_to_byte($size) {
public static function get_stateless_media_data_count() {
global $wpdb;
- if ( !defined('WP_STATELESS_POSTMETA') || !WP_STATELESS_POSTMETA ) {
- return ud_stateless_db()->get_total_files();
+ if ( !ud_get_stateless_media()->get('sm.use_postmeta') ) {
+ try {
+ return ud_stateless_db()->get_total_files();
+ } catch (\Throwable $th) {
+ }
}
$stateless_media = $wpdb->get_var($wpdb->prepare("
diff --git a/readme.txt b/readme.txt
index 1ff3c24f5..4da2b6038 100644
--- a/readme.txt
+++ b/readme.txt
@@ -5,8 +5,8 @@ Tags: google cloud, google cloud storage, cdn, uploads, backup
License: GPLv2 or later
Requires PHP: 8.0
Requires at least: 5.0
-Tested up to: 6.4.3
-Stable tag: 4.0.0-RC.1
+Tested up to: 6.5.0
+Stable tag: 4.0.0-RC.2
Upload and serve your WordPress media files from Google Cloud Storage.
@@ -127,7 +127,9 @@ Before upgrading to WP-Stateless 3.0, please, make sure you tested it on your de
* NEW - added filter `wp_stateless_get_file_sizes`, retrieves the GCS file data for image sizes, should be used instead of getting `sm_cloud` postmeta directly.
* NEW - added filter `wp_stateless_get_file_meta`, retrieves all GCS file meta data, should be used instead of getting `sm_cloud` postmeta directly.
* NEW - added filter `wp_stateless_get_file_meta_value`, retrieves the GCS file meta data by meta_key, should be used instead of getting `sm_cloud` postmeta directly.
-* NEW - added setting allowing to change email for WP-Stateless notifications.
+* NEW - added filter `wp_stateless_get_setting_...` which allows to override any WP-Stateless setting.
+* NEW - added setting "Send Status Emails" allowing to change email for WP-Stateless notifications.
+* NEW - added setting "Use Post Meta" allowing to switch back to using `postmeta` instead of custom DB tables. Can be used in case of issues after upgrading to 4.0.0.
* NEW - added new Settings tab `Addons`, which contains the list of WP-Stateless Addons, which replace Compatibilities.
* NEW - added new Settings tab `Status`, which contains status and health information related to Google Cloud Storage and WP-Stateless.
* NEW - CLI command `wp stateless migrate` to list and operate data optimizations.
diff --git a/static/migrations/20240219175240.php b/static/migrations/20240219175240.php
index fb5270a2d..66844231e 100644
--- a/static/migrations/20240219175240.php
+++ b/static/migrations/20240219175240.php
@@ -328,7 +328,7 @@ public function process_item($item) {
// Update file meta data ('fileMd5' for LiteSpeed Cache)
$key = 'fileMd5';
- $md5_data = $meta[$key] ?? [];
+ $md5_data = $meta[$key] ?? null;
if ( !empty($meta) ) {
$data = [
diff --git a/static/views/settings-sections/file-url.php b/static/views/settings-sections/file-url.php
index afcb6743c..6c1b88dd9 100644
--- a/static/views/settings-sections/file-url.php
+++ b/static/views/settings-sections/file-url.php
@@ -114,6 +114,23 @@
domain); ?>
+
+
domain); ?>
+
+
+
+
+
+
+
+ only if you experience technical issues after upgrading to WP-Stateless 4.0.0', ud_get_stateless_media()->domain); ?>
+
diff --git a/wp-stateless-media.php b/wp-stateless-media.php
index d389290db..a59a1ab8e 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-RC.1
+ * Version: 4.0.0-RC.2
* Text Domain: stateless-media
* Author URI: https://udx.io
* License: GPLv2 or later
From 1c2782ac1b822213bb49f996d5b347852e0d7963 Mon Sep 17 00:00:00 2001
From: balexey88
Date: Wed, 3 Apr 2024 17:38:18 +0300
Subject: [PATCH 2/2] #87 Fix 'filemd5' meta migration
---
static/migrations/20240219175240.php | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/static/migrations/20240219175240.php b/static/migrations/20240219175240.php
index 66844231e..f08bc5107 100644
--- a/static/migrations/20240219175240.php
+++ b/static/migrations/20240219175240.php
@@ -330,7 +330,7 @@ public function process_item($item) {
$key = 'fileMd5';
$md5_data = $meta[$key] ?? null;
- if ( !empty($meta) ) {
+ if ( !empty($md5_data) ) {
$data = [
'post_id' => $item,
'meta_key' => sanitize_key($key),