Skip to content

Commit

Permalink
Options now update correctly on multisites
Browse files Browse the repository at this point in the history
  • Loading branch information
Diego Orellano authored and Diego Orellano committed Nov 21, 2024
1 parent 935078c commit 6f603a2
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 25 deletions.
2 changes: 1 addition & 1 deletion admin/Ilove_Img_Compress_Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ public function ilove_img_restore() {
if ( false !== $key_founded ) {
unset( $images_restore[ $key_founded ] );
wp_delete_file( ILOVE_IMG_COMPRESS_BACKUP_FOLDER . basename( get_attached_file( $attachment_id ) ) );
update_option( 'iloveimg_images_to_restore', wp_json_encode( $images_restore ) );
Ilove_Img_Compress_Resources::update_option( 'iloveimg_images_to_restore', wp_json_encode( $images_restore ) );
}

wp_send_json_success( __( 'It was restored correctly', 'iloveimg' ), 200 );
Expand Down
2 changes: 1 addition & 1 deletion admin/Ilove_Img_Compress_Process.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function compress( $images_id ) {

$images_restore = array_unique( $images_restore );

update_option( 'iloveimg_images_to_restore', wp_json_encode( $images_restore, JSON_FORCE_OBJECT ) );
Ilove_Img_Compress_Resources::update_option( 'iloveimg_images_to_restore', wp_json_encode( $images_restore, JSON_FORCE_OBJECT ) );
}

foreach ( $_sizes as $_size ) {
Expand Down
23 changes: 23 additions & 0 deletions admin/Ilove_Img_Compress_Resources.php
Original file line number Diff line number Diff line change
Expand Up @@ -567,4 +567,27 @@ public static function regenerate_attachment_data( $attachment_id ) {

wp_update_attachment_metadata( $attachment_id, $metadata ); // Update new attachment metadata
}

/**
* Update option, works with multisite if enabled
*
* @since 2.2.5
* @param string $option Name of the option to update. Expected to not be SQL-escaped.
* @param mixed $value Option value. Must be serializable if non-scalar. Expected to not be SQL-escaped.
* @param bool|null $autoload Optional. Whether to load the option when WordPress starts up. Accepts a boolean, or null.
*/
public static function update_option( $option, $value, $autoload = null ) {

if ( ! is_multisite() ) {
update_option( $option, $value, $autoload );
return;
}

$sites = get_sites();
foreach ( $sites as $site ) {
switch_to_blog( (int) $site->blog_id );
update_option( $option, $value, $autoload );
restore_current_blog();
}
}
}
32 changes: 16 additions & 16 deletions admin/Ilove_Img_Compress_Serializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function save() {
}
}

update_option( 'iloveimg_options_compress', wp_json_encode( $posts_value ) );
Ilove_Img_Compress_Resources::update_option( 'iloveimg_options_compress', wp_json_encode( $posts_value ) );
}

if ( 'iloveimg_action_logout' === $_POST['iloveimg_action'] ) {// phpcs:ignore WordPress.Security.NonceVerification.Missing
Expand All @@ -45,7 +45,7 @@ public function save() {
unset( $options['iloveimg_field_compress_activated'] );
unset( $options['iloveimg_field_autocompress'] );
unset( $options['iloveimg_field_resize_full'] );
update_option( 'iloveimg_options_compress', wp_json_encode( $options ) );
Ilove_Img_Compress_Resources::update_option( 'iloveimg_options_compress', wp_json_encode( $options ) );
}

if ( 'iloveimg_action_login' === $_POST['iloveimg_action'] ) {// phpcs:ignore WordPress.Security.NonceVerification.Missing
Expand All @@ -63,14 +63,14 @@ public function save() {
)
);
if ( wp_remote_retrieve_response_code( $response ) === 200 ) {
update_option( 'iloveimg_account', $response['body'] );
update_option( 'iloveimg_user_is_migrated', 1 );
Ilove_Img_Compress_Resources::update_option( 'iloveimg_account', $response['body'] );
Ilove_Img_Compress_Resources::update_option( 'iloveimg_user_is_migrated', 1 );
$options = json_decode( get_option( 'iloveimg_options_compress' ), true );
$options['iloveimg_field_compress_activated'] = 1;
$options['iloveimg_field_autocompress'] = 1;
update_option( 'iloveimg_options_compress', wp_json_encode( $options ) );
Ilove_Img_Compress_Resources::update_option( 'iloveimg_options_compress', wp_json_encode( $options ) );
} else {
update_option(
Ilove_Img_Compress_Resources::update_option(
'iloveimg_account_error',
wp_json_encode(
array(
Expand Down Expand Up @@ -103,27 +103,27 @@ public function save() {
if ( get_option( $key ) ) {
$num = (int) get_option( $key );
++$num;
update_option( $key, $num );
Ilove_Img_Compress_Resources::update_option( $key, $num );
} else {
update_option( $key, 1 );
Ilove_Img_Compress_Resources::update_option( $key, 1 );
}
if ( (int) get_option( $key ) <= 3 ) {
update_option( 'iloveimg_account', $response['body'] );
Ilove_Img_Compress_Resources::update_option( 'iloveimg_account', $response['body'] );
$options = json_decode( get_option( 'iloveimg_options_compress' ) );
$options['iloveimg_field_compress_activated'] = 1;
$options['iloveimg_field_autocompress'] = 1;
update_option( 'iloveimg_options_compress', wp_json_encode( $options ) );
Ilove_Img_Compress_Resources::update_option( 'iloveimg_options_compress', wp_json_encode( $options ) );
} else {
update_option( 'iloveimg_account_error', wp_json_encode( array( 'action' => 'register_limit' ) ) );
Ilove_Img_Compress_Resources::update_option( 'iloveimg_account_error', wp_json_encode( array( 'action' => 'register_limit' ) ) );
}
} else {
update_option(
Ilove_Img_Compress_Resources::update_option(
'iloveimg_account_error',
wp_json_encode(
array(
'action' => 'register',
'email' => sanitize_email( wp_unslash( $_POST['iloveimg_field_email'] ) ), // phpcs:ignore WordPress.Security.NonceVerification.Missing
'name' => sanitize_text_field( wp_unslash( $_POST['iloveimg_field_name'] ) ), // phpcs:ignore WordPress.Security.NonceVerification.Missing
'action' => 'register',
'email' => sanitize_email( wp_unslash( $_POST['iloveimg_field_email'] ) ), // phpcs:ignore WordPress.Security.NonceVerification.Missing
'name' => sanitize_text_field( wp_unslash( $_POST['iloveimg_field_name'] ) ), // phpcs:ignore WordPress.Security.NonceVerification.Missing
)
)
);
Expand All @@ -134,7 +134,7 @@ public function save() {
if ( ! isset( $_POST['iloveimg_field_proyect'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
$this->redirect();
}
update_option( 'iloveimg_proyect', sanitize_text_field( wp_unslash( $_POST['iloveimg_field_proyect'] ) ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing
Ilove_Img_Compress_Resources::update_option( 'iloveimg_proyect', sanitize_text_field( wp_unslash( $_POST['iloveimg_field_proyect'] ) ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing
}
}

Expand Down
8 changes: 5 additions & 3 deletions admin/views/account.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
use Ilove_Img_Compress\Ilove_Img_Compress_Resources;

$ilove_img_is_logged = false;
$ilove_img_account = array();

Expand All @@ -12,7 +14,7 @@
unset( $ilove_img_options['iloveimg_field_compress_activated'] );
unset( $ilove_img_options['iloveimg_field_autocompress'] );
unset( $ilove_img_options['iloveimg_field_resize_full'] );
update_option( 'iloveimg_options_compress', wp_json_encode( $ilove_img_options ) );
Ilove_Img_Compress_Resources::update_option( 'iloveimg_options_compress', wp_json_encode( $ilove_img_options ) );

wp_safe_redirect( admin_url( 'admin.php?page=iloveimg-compress-admin-page' ) );
exit();
Expand All @@ -21,7 +23,7 @@
$ilove_img_account = json_decode( get_option( 'iloveimg_account' ), true );

$ilove_img_is_logged = true;
update_option( 'iloveimg_first_loggued', 1 );
Ilove_Img_Compress_Resources::update_option( 'iloveimg_first_loggued', 1 );
$ilove_img_token = $ilove_img_account['token'];
$ilove_img_response = wp_remote_get(
ILOVE_IMG_COMPRESS_USER_URL . '/' . $ilove_img_account['id'],
Expand All @@ -33,7 +35,7 @@
if ( isset( $ilove_img_response['response']['code'] ) && 200 === (int) $ilove_img_response['response']['code'] ) {
$ilove_img_account = json_decode( $ilove_img_response['body'], true );
$ilove_img_account['token'] = $ilove_img_token;
update_option( 'iloveimg_account', wp_json_encode( $ilove_img_account ) );
Ilove_Img_Compress_Resources::update_option( 'iloveimg_account', wp_json_encode( $ilove_img_account ) );
}
} elseif ( get_option( 'iloveimg_account_error' ) ) {
$ilove_img_account_error = json_decode( get_option( 'iloveimg_account_error' ), true );
Expand Down
10 changes: 6 additions & 4 deletions ilove-img-compress.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
define( 'ILOVE_IMG_COMPRESS_PLUGIN_URL', plugin_dir_url( __FILE__ ) );

use Ilove_Img_Compress\Ilove_Img_Compress_Plugin;
use Ilove_Img_Compress\Ilove_Img_Compress_Resources;
use Ilove_Img_Compress\Ilove_Img_Compress_Serializer;
use Ilove_Img_Compress\Ilove_Img_Compress_Submenu;
use Ilove_Img_Compress\Ilove_Img_Compress_Submenu_Page;
Expand Down Expand Up @@ -98,7 +99,7 @@ function ilove_img_compress_add_plugin_page_settings_link( $links ) {
* @since 1.0.0
*/
function ilove_img_compress_activate() {
update_option( 'ilove_img_compress_db_version', ILOVE_IMG_COMPRESS_DB_VERSION );
Ilove_Img_Compress_Resources::update_option( 'ilove_img_compress_db_version', ILOVE_IMG_COMPRESS_DB_VERSION );

if ( ! file_exists( ILOVE_IMG_COMPRESS_BACKUP_FOLDER ) ) {
wp_mkdir_p( ILOVE_IMG_COMPRESS_BACKUP_FOLDER );
Expand All @@ -109,7 +110,8 @@ function ilove_img_compress_activate() {
if ( ! extension_loaded( 'gd' ) ) {
$iloveimg_thumbnails = array( 'full' );
}
update_option(

Ilove_Img_Compress_Resources::update_option(
'iloveimg_options_compress',
wp_json_encode(
array(
Expand All @@ -118,15 +120,15 @@ function ilove_img_compress_activate() {
'iloveimg_field_size_full_width' => 2048,
'iloveimg_field_size_full_height' => 2048,
'iloveimg_field_backup' => 'on',
)
)
)
);
} else {
$old_data = get_option( 'iloveimg_options_compress' );

if ( is_serialized( $old_data ) ) {
$old_data_serialize = unserialize( get_option( 'iloveimg_options_compress' ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_unserialize
update_option( 'iloveimg_options_compress', wp_json_encode( $old_data_serialize ) );
Ilove_Img_Compress_Resources::update_option( 'iloveimg_options_compress', wp_json_encode( $old_data_serialize ) );
}
}
}
Expand Down

0 comments on commit 6f603a2

Please sign in to comment.