Cleaning repo & changes on readME

This commit is contained in:
2025-11-21 14:14:09 +00:00
parent df1f90c6cd
commit 97b818da54
1395 changed files with 85615 additions and 130792 deletions
@@ -0,0 +1,35 @@
<?php
namespace ImageOptimization\Modules\Backups\Classes;
use ImageOptimization\Classes\Async_Operation\{
Async_Operation,
Async_Operation_Hook,
Async_Operation_Queue,
};
use ImageOptimization\Classes\Image\Image_Query_Builder;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Remove_All_Backups {
private const CHUNK_SIZE = 100;
public static function find_and_schedule_removing(): void {
$query = ( new Image_Query_Builder() )
->return_images_only_with_backups()
->execute();
$attachment_ids = $query->posts;
$chunks = array_chunk( $attachment_ids, self::CHUNK_SIZE );
foreach ( $chunks as $chunk ) {
Async_Operation::create(
Async_Operation_Hook::REMOVE_MANY_BACKUPS,
[ 'attachment_ids' => $chunk ],
Async_Operation_Queue::BACKUP
);
}
}
}
@@ -0,0 +1,115 @@
<?php
namespace ImageOptimization\Modules\Backups\Classes;
use ImageOptimization\Classes\Async_Operation\{
Async_Operation,
Async_Operation_Hook,
Async_Operation_Queue,
};
use ImageOptimization\Classes\Image\{
Image_Meta,
Image_Query_Builder,
Image_Status
};
use Throwable;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Restore_Images {
private const CHUNK_SIZE = 100;
/**
* Schedules a single restoring operation.
*
* @param int $image_id
* @return void
* @throws Throwable
* @throws \ImageOptimization\Classes\Async_Operation\Exceptions\Async_Operation_Exception
*/
public static function schedule_single_restoring( int $image_id ): void {
$meta = new Image_Meta( $image_id );
try {
$meta
->set_status( Image_Status::RESTORING_IN_PROGRESS )
->save();
Async_Operation::create(
Async_Operation_Hook::RESTORE_SINGLE_IMAGE,
[ 'attachment_id' => $image_id ],
Async_Operation_Queue::RESTORE
);
} catch ( Throwable $t ) {
$meta
->set_status( Image_Status::RESTORING_FAILED )
->save();
throw $t;
}
}
/**
* Schedules restoring operations for all valid images with backups created.
*
* @param array $image_ids
*
* @return void
*/
public static function find_and_schedule_restoring( array $image_ids = [] ): void {
$query = ( new Image_Query_Builder() )
->return_images_only_with_backups();
if ( ! empty( $image_ids ) ) {
$query->set_image_ids( $image_ids );
}
$query = $query->execute();
$attachment_ids = $query->posts;
$chunks = array_chunk( $attachment_ids, self::CHUNK_SIZE );
foreach ( $chunks as $chunk ) {
try {
Async_Operation::create(
Async_Operation_Hook::RESTORE_MANY_IMAGES,
[ 'attachment_ids' => $chunk ],
Async_Operation_Queue::RESTORE
);
self::mark_chunk_as_in_progress( $chunk );
} catch ( Throwable $t ) {
self::fail_restoring_for_chunk( $chunk );
}
}
}
/**
* Updates chunk images optimization status to in-progress.
*
* @param int[] $chunk
* @return void
*/
private static function mark_chunk_as_in_progress( array $chunk ) {
foreach ( $chunk as $image_id ) {
( new Image_Meta( $image_id ) )
->set_status( Image_Status::RESTORING_IN_PROGRESS )
->save();
}
}
/**
* Updates chunk images optimization status to failed.
*
* @param int[] $chunk
* @return void
*/
private static function fail_restoring_for_chunk( array $chunk ) {
foreach ( $chunk as $image_id ) {
( new Image_Meta( $image_id ) )
->set_status( Image_Status::RESTORING_FAILED )
->save();
}
}
}
@@ -0,0 +1,37 @@
<?php
namespace ImageOptimization\Modules\Backups\Classes;
use ImageOptimization\Classes\Route;
use WP_REST_Request;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Route_Base extends Route {
protected $auth = true;
protected string $path = '';
public function get_methods(): array {
return [];
}
public function get_endpoint(): string {
return 'backups/' . $this->get_path();
}
public function get_path(): string {
return $this->path;
}
public function get_name(): string {
return '';
}
public function get_permission_callback( WP_REST_Request $request ): bool {
$valid = $this->permission_callback( $request );
return $valid && user_can( $this->current_user_id, 'manage_options' );
}
}
@@ -0,0 +1,31 @@
<?php
namespace ImageOptimization\Modules\Backups\Components;
use ImageOptimization\Classes\Async_Operation\Async_Operation_Hook;
use ImageOptimization\Classes\Image\Image_Backup;
use WP_Post;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Handle_Backups_Removing {
public function remove_backups_on_attachment_removing( int $attachment_id, WP_Post $attachment_post ) {
if ( ! wp_attachment_is_image( $attachment_post ) ) {
return;
}
Image_Backup::remove( $attachment_id );
}
/** @async */
public function remove_many_backups( array $attachment_ids ) {
Image_Backup::remove_many( $attachment_ids );
}
public function __construct() {
add_action( 'delete_attachment', [ $this, 'remove_backups_on_attachment_removing' ], 10, 2 );
add_action( Async_Operation_Hook::REMOVE_MANY_BACKUPS, [ $this, 'remove_many_backups' ] );
}
}
@@ -0,0 +1,44 @@
<?php
namespace ImageOptimization\Modules\Backups\Components;
use ImageOptimization\Classes\Async_Operation\Async_Operation_Hook;
use ImageOptimization\Classes\Image\{
Image_Meta,
Image_Restore,
Image_Status,
};
use ImageOptimization\Classes\Logger;
use Throwable;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Restore_Images {
/** @async */
public function restore_image( int $image_id ) {
try {
Image_Restore::restore( $image_id );
} catch ( Throwable $t ) {
Logger::log( Logger::LEVEL_ERROR, 'Image restoring error: ' . $t->getMessage() );
( new Image_Meta( $image_id ) )
->set_status( Image_Status::RESTORING_FAILED )
->save();
throw $t;
}
}
/** @async */
public function restore_many_images( array $attachment_ids ) {
Image_Restore::restore_many( $attachment_ids );
}
public function __construct() {
add_action( Async_Operation_Hook::RESTORE_SINGLE_IMAGE, [ $this, 'restore_image' ] );
add_action( Async_Operation_Hook::RESTORE_MANY_IMAGES, [ $this, 'restore_many_images' ] );
}
}
@@ -0,0 +1,38 @@
<?php
namespace ImageOptimization\Modules\Backups;
use ImageOptimization\Classes\Module_Base;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Module extends Module_Base {
public function get_name(): string {
return 'backups';
}
public static function routes_list() : array {
return [
'Remove_Backups',
'Restore_Single',
'Restore_All',
];
}
public static function component_list() : array {
return [
'Handle_Backups_Removing',
'Restore_Images',
];
}
/**
* Module constructor.
*/
public function __construct() {
$this->register_components();
$this->register_routes();
}
}
@@ -0,0 +1,46 @@
<?php
namespace ImageOptimization\Modules\Backups\Rest;
use ImageOptimization\Modules\Backups\Classes\{
Route_Base,
Remove_All_Backups,
};
use Throwable;
use WP_REST_Request;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Remove_Backups extends Route_Base {
const NONCE_NAME = 'image-optimization-remove-backups';
protected string $path = '';
public function get_name(): string {
return 'remove-backups';
}
public function get_methods(): array {
return [ 'DELETE' ];
}
public function DELETE( WP_REST_Request $request ) {
$this->verify_nonce_and_capability(
$request->get_param( self::NONCE_NAME ),
self::NONCE_NAME
);
try {
Remove_All_Backups::find_and_schedule_removing();
return $this->respond_success_json();
} catch ( Throwable $t ) {
return $this->respond_error_json([
'message' => $t->getMessage(),
'code' => 'internal_server_error',
]);
}
}
}
@@ -0,0 +1,46 @@
<?php
namespace ImageOptimization\Modules\Backups\Rest;
use ImageOptimization\Modules\Backups\Classes\{
Restore_Images,
Route_Base,
};
use Throwable;
use WP_REST_Request;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Restore_All extends Route_Base {
const NONCE_NAME = 'image-optimization-restore-all';
protected string $path = 'restore';
public function get_name(): string {
return 'restore-all';
}
public function get_methods(): array {
return [ 'POST' ];
}
public function POST( WP_REST_Request $request ) {
$this->verify_nonce_and_capability(
$request->get_param( self::NONCE_NAME ),
self::NONCE_NAME
);
try {
Restore_Images::find_and_schedule_restoring();
return $this->respond_success_json();
} catch ( Throwable $t ) {
return $this->respond_error_json([
'message' => $t->getMessage(),
'code' => 'internal_server_error',
]);
}
}
}
@@ -0,0 +1,58 @@
<?php
namespace ImageOptimization\Modules\Backups\Rest;
use ImageOptimization\Modules\Optimization\Classes\Validate_Image;
use ImageOptimization\Modules\Backups\Classes\{
Restore_Images,
Route_Base,
};
use Throwable;
use WP_REST_Request;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Restore_Single extends Route_Base {
const NONCE_NAME = 'image-optimization-restore-single';
protected string $path = 'restore/(?P<image_id>\d+)';
public function get_name(): string {
return 'restore-single';
}
public function get_methods(): array {
return [ 'POST' ];
}
public function POST( WP_REST_Request $request ) {
$this->verify_nonce_and_capability(
$request->get_param( self::NONCE_NAME ),
self::NONCE_NAME
);
$image_id = (int) $request->get_param( 'image_id' );
if ( empty( $image_id ) ) {
return $this->respond_error_json( [
'message' => esc_html__( 'Invalid image id', 'image-optimization' ),
'code' => 'internal_server_error',
] );
}
try {
Validate_Image::is_valid( $image_id );
Restore_Images::schedule_single_restoring( $image_id );
return $this->respond_success_json();
} catch ( Throwable $t ) {
return $this->respond_error_json([
'message' => $t->getMessage(),
'code' => 'internal_server_error',
]);
}
}
}
@@ -0,0 +1,98 @@
<?php
namespace ImageOptimization\Modules\ConnectManager\Classes;
use ImageOptimization\Modules\ConnectManager\Components\Connect_Interface;
if ( ! defined( 'ABSPATH' ) ) {
exit; // exit if accessed directly
}
class Connect_Runner {
public $connect;
public function __construct( Connect_Interface $connect ) {
$this->connect = $connect;
}
public function is_connected() {
return $this->connect->is_connected();
}
public function is_activated() : bool {
return $this->connect->is_activated();
}
public function is_valid_home_url() : bool {
return $this->connect->is_valid_home_url();
}
public function get_connect_status() {
return $this->connect->get_connect_status();
}
public function get_connect_data( bool $force = false ): array {
return $this->connect->get_connect_data( $force );
}
public function update_usage_data( $new_usage_data ): void {
$this->connect->update_usage_data( $new_usage_data );
}
public function get_activation_state() : string {
return $this->connect->get_activation_state();
}
public function get_access_token() {
return $this->connect->get_access_token();
}
public function get_client_id(): string {
return $this->connect->get_client_id();
}
public function get_client_secret(): string {
return $this->connect->get_client_secret();
}
public function images_left(): int {
return $this->connect->images_left();
}
public function user_is_subscription_owner(): bool {
return $this->connect->user_is_subscription_owner();
}
// Legacy Nonces.
public function connect_init_nonce(): string {
return $this->connect->connect_init_nonce();
}
public function disconnect_nonce(): string {
return $this->connect->disconnect_nonce();
}
public function deactivate_nonce(): string {
return $this->connect->deactivate_nonce();
}
public function get_subscriptions_nonce(): string {
return $this->connect->get_subscriptions_nonce();
}
public function activate_nonce(): string {
return $this->connect->activate_nonce();
}
public function version_nonce(): string {
return $this->connect->version_nonce();
}
public function get_is_connect_on_fly(): bool {
return $this->connect->get_is_connect_on_fly();
}
public function refresh_token(): void {
$this->connect->refresh_token();
}
}
@@ -0,0 +1,51 @@
<?php
namespace ImageOptimization\Modules\ConnectManager\Components;
if ( ! defined( 'ABSPATH' ) ) {
exit; // exit if accessed directly
}
interface Connect_Interface {
// Connect.
public function is_connected(): bool;
public function is_activated() : bool;
public function is_valid_home_url() : bool;
public function get_connect_status();
public function get_connect_data( bool $force = false ): array;
public function update_usage_data( $new_usage_data ): void;
public function get_activation_state() : string;
public function get_access_token();
public function get_client_id(): string;
public function get_client_secret(): string;
public function images_left(): int;
public function user_is_subscription_owner(): bool;
// Nonces.
public function connect_init_nonce(): string;
public function disconnect_nonce(): string;
public function deactivate_nonce(): string;
public function get_subscriptions_nonce(): string;
public function activate_nonce(): string;
public function version_nonce(): string;
public function get_is_connect_on_fly(): bool;
public function refresh_token(): void;
}
@@ -0,0 +1,191 @@
<?php
namespace ImageOptimization\Modules\ConnectManager\Components;
use ImageOptimization\Modules\Connect\{
Module as ConnectModule,
Classes\Data,
Classes\Service,
Classes\Utils as ConnectUtils,
Rest\Authorize,
Rest\Version
};
use ImageOptimization\Classes\Logger;
use ImageOptimization\Classes\Utils;
use Throwable;
if ( ! defined( 'ABSPATH' ) ) {
exit; // exit if accessed directly
}
class Connect implements Connect_Interface {
const STATUS_CHECK_TRANSIENT = 'image_optimizer_status_check';
public function is_connected() : bool {
return ConnectModule::is_connected();
}
public function is_activated() : bool {
return ConnectModule::is_connected();
}
public function is_valid_home_url() : bool {
return ConnectUtils::is_valid_home_url();
}
public function get_connect_status() {
if ( ! $this->is_connected() ) {
Logger::log( Logger::LEVEL_INFO, 'Status check error. Reason: User is not connected' );
return null;
}
$cached_status = get_transient( self::STATUS_CHECK_TRANSIENT );
if ( $cached_status ) {
return $cached_status;
}
try {
$response = Utils::get_api_client()->make_request(
'POST',
'status/check'
);
} catch ( Throwable $t ) {
Logger::log(
Logger::LEVEL_ERROR,
'Status check error. Reason: ' . $t->getMessage()
);
return null;
}
if ( ! isset( $response->status ) ) {
Logger::log( Logger::LEVEL_ERROR, 'Invalid response from server' );
return null;
}
if ( ! empty( $response->site_url ) && Data::get_home_url() !== $response->site_url ) {
Data::set_home_url( $response->site_url );
}
set_transient( self::STATUS_CHECK_TRANSIENT, $response, MINUTE_IN_SECONDS * 5 );
return $response;
}
public function get_connect_data( bool $force = false ): array {
$data = get_transient( self::STATUS_CHECK_TRANSIENT );
$user = [];
// Return empty array if transient does not exist or is expired.
if ( ! $data ) {
return $user;
}
// Return if user property does not exist in the data object.
if ( ! property_exists( $data, 'user' ) ) {
return $user;
}
if ( $data->user->email ) {
$user = [
'user' => [
'email' => $data->user->email,
],
];
}
return $user;
}
public function update_usage_data( $new_usage_data ): void {
$connect_status = $this->get_connect_status();
if ( ! isset( $new_usage_data->allowed ) || ! isset( $new_usage_data->used ) ) {
return;
}
if ( 0 === $new_usage_data->allowed - $new_usage_data->used ) {
$connect_status->status = 'expired';
}
$connect_status->quota = $new_usage_data->allowed;
$connect_status->used_quota = $new_usage_data->used;
set_transient( self::STATUS_CHECK_TRANSIENT, $connect_status, MINUTE_IN_SECONDS * 5 );
}
public function get_activation_state() : string {
/**
* Returning true because the license key is
* not used for deactivation in connect.
*/
return true;
}
public function get_access_token() {
return Data::get_access_token();
}
public function get_client_id(): string {
return Data::get_client_id();
}
public function get_client_secret(): string {
return Data::get_client_secret();
}
public function images_left(): int {
$plan_data = $this->get_connect_status();
if ( empty( $plan_data ) ) {
return 0;
}
$quota = $plan_data->quota;
$used_quota = $plan_data->used_quota;
return max( $quota - $used_quota, 0 );
}
public function user_is_subscription_owner(): bool {
return Data::user_is_subscription_owner();
}
// Nonces.
public function connect_init_nonce(): string {
return Authorize::NONCE_NAME;
}
public function disconnect_nonce(): string {
return Authorize::NONCE_NAME;
}
public function deactivate_nonce(): string {
return Authorize::NONCE_NAME;
}
public function get_subscriptions_nonce(): string {
return Authorize::NONCE_NAME;
}
public function activate_nonce(): string {
return Authorize::NONCE_NAME;
}
public function version_nonce(): string {
return Version::NONCE_NAME;
}
public function get_is_connect_on_fly(): bool {
return true;
}
public function refresh_token(): void {
Service::refresh_token();
}
}
@@ -0,0 +1,102 @@
<?php
namespace ImageOptimization\Modules\ConnectManager\Components;
use ImageOptimization\Modules\Oauth\{
Components\Connect,
Classes\Data,
Rest\Activate,
Rest\Connect_Init,
Rest\Deactivate,
Rest\Disconnect,
Rest\Get_Subscriptions,
Rest\Version,
};
if ( ! defined( 'ABSPATH' ) ) {
exit; // exit if accessed directly
}
class Legacy_Connect implements Connect_Interface {
public function is_connected() : bool {
return Connect::is_connected();
}
public function is_activated() : bool {
return Connect::is_activated();
}
public function is_valid_home_url() : bool {
return true;
}
public function get_connect_status() {
return Connect::get_connect_status();
}
public function get_connect_data( bool $force = false ): array {
return Data::get_connect_data( $force );
}
public function update_usage_data( $new_usage_data ): void {
Connect::update_usage_data( $new_usage_data );
}
public function get_activation_state() : string {
return Data::get_activation_state();
}
public function get_access_token() {
return Data::get_access_token();
}
public function get_client_id(): string {
return Data::get_client_id();
}
public function get_client_secret(): string {
return Data::get_client_secret();
}
public function images_left(): int {
return Data::images_left();
}
public function user_is_subscription_owner(): bool {
return Data::user_is_subscription_owner();
}
// Legacy Nonces.
public function connect_init_nonce(): string {
return Connect_Init::NONCE_NAME;
}
public function disconnect_nonce(): string {
return Disconnect::NONCE_NAME;
}
public function deactivate_nonce(): string {
return Deactivate::NONCE_NAME;
}
public function get_subscriptions_nonce(): string {
return Get_Subscriptions::NONCE_NAME;
}
public function activate_nonce(): string {
return Activate::NONCE_NAME;
}
public function version_nonce(): string {
return Version::NONCE_NAME;
}
public function get_is_connect_on_fly(): bool {
return false;
}
// Placeholder function for legacy connect.
public function refresh_token(): void {
}
}
@@ -0,0 +1,77 @@
<?php
namespace ImageOptimization\Modules\ConnectManager;
use ImageOptimization;
use ImageOptimization\Classes\{
Module_Base,
};
use ImageOptimization\Modules\ConnectManager\Classes\Connect_Runner;
use ImageOptimization\Modules\ConnectManager\Components\Legacy_Connect;
use ImageOptimization\Modules\ConnectManager\Components\Connect;
use ImageOptimization\Plugin;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
/**
* Class Module
*/
class Module extends Module_Base {
/**
* Connect instance
*
* @var connect_instance
*/
public $connect_instance;
/**
* Get module name.
* Retrieve the module name.
* @access public
* @return string Module name.
*/
public function get_name() {
return 'connect-manager';
}
/**
* component_list
* @return string[]
*/
public static function component_list() : array {
return [
'Legacy_Connect',
'Connect',
];
}
public function __construct() {
// Register components.
$this->register_components();
// Load Connect Manager.
add_action( 'plugins_loaded', [ $this, 'load_connect_manager' ] );
}
/**
* Load Connect Manager
*
* Load the correct version of Connect Manager based on whether
* the user is already connected using legacy version or not.
*
* @return void
*/
public function load_connect_manager() {
if ( ImageOptimization\Modules\Connect\Module::is_active() ) {
$this->connect_instance = new Connect_Runner( new Connect() );
} else {
$this->connect_instance = new Connect_Runner( new Legacy_Connect() );
}
}
}
@@ -0,0 +1,35 @@
<?php
namespace ImageOptimization\Modules\Connect\Classes;
use ReflectionClass;
use ReflectionException;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
abstract class Basic_Enum {
private static array $entries = [];
/**
* @throws ReflectionException
*/
public static function get_values(): array {
return array_values( self::get_entries() );
}
/**
* @throws ReflectionException
*/
protected static function get_entries(): array {
$caller = get_called_class();
if ( ! array_key_exists( $caller, self::$entries ) ) {
$reflect = new ReflectionClass( $caller );
self::$entries[ $caller ] = $reflect->getConstants();
}
return self::$entries[ $caller ];
}
}
@@ -0,0 +1,26 @@
<?php
namespace ImageOptimization\Modules\Connect\Classes;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
/**
* Class Config
*/
class Config {
const APP_NAME = 'image-optimizer';
const APP_PREFIX = 'image_optimizer';
const APP_REST_NAMESPACE = 'image_optimizer';
const BASE_URL = 'https://my.elementor.com/connect';
const ADMIN_PAGE = 'upload.php?page=image-optimization-settings';
const APP_TYPE = 'app_io';
const SCOPES = 'openid offline_access';
const STATE_NONCE = 'image_optimizer_auth_nonce';
/**
* Connect mode
* accepts 'site' or 'user'
*/
const CONNECT_MODE = 'site';
}
@@ -0,0 +1,351 @@
<?php
namespace ImageOptimization\Modules\Connect\Classes;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
/**
* Class Data
*/
class Data {
const CLIENT_ID = '_client_id';
const CLIENT_SECRET = '_client_secret';
const ACCESS_TOKEN = '_access_token';
const REFRESH_TOKEN = '_refresh_token';
const TOKEN_ID = '_token_id';
const SUBSCRIPTION_ID = '_subscription_id';
const OPTION_OWNER_USER_ID = '_owner_user_id';
const HOME_URL = '_home_url';
/**
* get_option
* @param $option_name
* @param $default
*
* @return false|mixed|null
*/
public static function get_option( $option_name, $default ) {
return get_option( Config::APP_PREFIX . $option_name, $default );
}
/**
* set_option
* @param $option_name
* @param $option_value
* @param $auto_load
*
* @return bool
*/
public static function set_option( $option_name, $option_value, $auto_load = false ): bool {
return update_option( Config::APP_PREFIX . $option_name, $option_value, $auto_load );
}
/**
* delete_option
* @param $option_name
*
* @return bool
*/
public static function delete_option( $option_name ) : bool {
return delete_option( Config::APP_PREFIX . $option_name );
}
/**
* get_user_data
* @param $user_id
* @param $data_name
* @param mixed|bool $default
*
* @return false|mixed
*/
public static function get_user_data( $user_id, $data_name, $default = false ) {
$data = get_user_meta( $user_id, Config::APP_PREFIX . $data_name, true );
return empty( $data ) ? $default : $data;
}
/**
* set_user_data
* @param $user_id
* @param $data_name
* @param $value
*
* @return bool|int
*/
public static function set_user_data( $user_id, $data_name, $value ) {
return update_user_meta( $user_id, Config::APP_PREFIX . $data_name, $value );
}
/**
* delete_user_data
* @param $user_id
* @param $data_name
*
* @return bool
*/
public static function delete_user_data( $user_id, $data_name ): bool {
return delete_user_meta( $user_id, Config::APP_PREFIX . $data_name );
}
/**
* get_connect_mode_data
* @param ...$data
*
* @return false|mixed|null|string
*/
public static function get_connect_mode_data( ...$data ) {
if ( Config::CONNECT_MODE === 'site' ) {
return self::get_option( ...$data );
}
$user_id = get_current_user_id();
return self::get_user_data( ...( [ $user_id, ...$data ] ) );
}
/**
* set_connect_mode_data
* @param ...$data
*
* @return bool|int
*/
public static function set_connect_mode_data( ...$data ) {
if ( Config::CONNECT_MODE === 'site' ) {
return self::set_option( ...$data );
}
$user_id = get_current_user_id();
return self::set_user_data( ...( [ $user_id, ...$data ] ) );
}
/**
* get_client_id
*
* @return string|bool|mixed
*/
public static function get_client_id() {
return self::get_connect_mode_data( self::CLIENT_ID, false );
}
/**
* get_client_secret
* @return false|mixed|string|null
*/
public static function get_client_secret() {
return self::get_connect_mode_data( self::CLIENT_SECRET, false );
}
/**
* set_client_id
* @param $value
*
* @return bool
*/
public static function set_client_id( $value ): bool {
return self::set_connect_mode_data( self::CLIENT_ID, $value );
}
public static function get_subscription_id() {
return self::get_connect_mode_data( self::SUBSCRIPTION_ID, false );
}
public static function set_subscription_id( $value ): bool {
return self::set_connect_mode_data( self::SUBSCRIPTION_ID, $value );
}
/**
* set_client_secret
* @param $value
*
* @return bool
*/
public static function set_client_secret( $value ): bool {
return self::set_connect_mode_data( self::CLIENT_SECRET, $value );
}
/**
* get_access_token
* @return false|mixed|string|null
*/
public static function get_access_token() {
return self::get_connect_mode_data( self::ACCESS_TOKEN, false );
}
public static function get_token_id() {
return self::get_connect_mode_data( self::TOKEN_ID, false );
}
/**
* get_refresh_token
* @return false|mixed|string|null
*/
public static function get_refresh_token() {
return self::get_connect_mode_data( self::REFRESH_TOKEN, false );
}
public static function get_home_url() {
$raw = self::get_connect_mode_data( self::HOME_URL, false );
$is_base64 = base64_encode( base64_decode( $raw, true ) ) === $raw;
return $is_base64 ? base64_decode( $raw ) : $raw;
}
/**
* set_home_url
*
* Stores home URL as a base64 string to avoid migration/stg tools from overriding value
*/
public static function set_home_url( ?string $home_url = null ): bool {
$home_url = $home_url ?? home_url();
return self::set_connect_mode_data( self::HOME_URL, base64_encode( $home_url ) );
}
/**
* set_user_is_owner_option
*/
public static function set_user_is_owner_option( $value ) {
return self::set_connect_mode_data( self::OPTION_OWNER_USER_ID, $value );
}
/**
* get_user_is_owner_option
*/
public static function get_user_is_owner_option() {
return self::get_connect_mode_data( self::OPTION_OWNER_USER_ID, false );
}
/**
* fetch_option
* direct query to avoid cache and race condition issues
*
* @param $option_name
* @param $default
*
* @return mixed|null
*/
public static function fetch_option( $option_name, $default = null ) {
global $wpdb;
$cache_buster = wp_generate_uuid4();
$option = $wpdb->get_col(
$wpdb->prepare(
"SELECT option_value
FROM $wpdb->options
WHERE option_name = %s
AND %s = %s
LIMIT 1",
$option_name,
$cache_buster,
$cache_buster
)
);
if ( ! empty( $option ) ) {
return $option[0];
}
return $default;
}
/**
* insert_option_uniquely
* used to insert option if not there already
* direct query to avoid cache and race condition issues
* @param $option_name
* @param $option_value
*
* @return bool
*/
public static function insert_option_uniquely( $option_name, $option_value ): bool {
global $wpdb;
$cache_buster = wp_generate_uuid4();
$result = $wpdb->query(
$wpdb->prepare(
"INSERT INTO $wpdb->options (option_name, option_value, autoload)
SELECT * FROM (SELECT %s, %s, 'no') AS tmp
WHERE NOT EXISTS (
SELECT option_name
FROM $wpdb->options
WHERE option_name = %s
AND option_value = %s
AND %s = %s
) LIMIT 1",
$option_name,
$option_value,
$option_name,
$option_value,
$cache_buster,
$cache_buster
)
);
if ( false === $result || 0 === $result ) {
// false means query failed, 0 means no row inserted because it exists
return false;
}
return true;
}
/**
* User is subscription owner.
*
* Check if current user is subscription owner.
*
* @return boolean
*/
public static function user_is_subscription_owner(): bool {
$owner_id = (int) self::get_connect_mode_data( self::OPTION_OWNER_USER_ID, false );
return get_current_user_id() === $owner_id;
}
public static function ensure_reset_connect() {
if ( Config::CONNECT_MODE === 'site' ) {
self::delete_option( self::ACCESS_TOKEN );
self::delete_option( self::REFRESH_TOKEN );
self::delete_option( self::TOKEN_ID );
self::delete_option( self::SUBSCRIPTION_ID );
self::delete_option( self::OPTION_OWNER_USER_ID );
self::delete_option( self::HOME_URL );
delete_transient( 'image_optimizer_status_check' );
} else {
$user_id = get_current_user_id();
self::delete_user_data( $user_id, self::ACCESS_TOKEN );
self::delete_user_data( $user_id, self::REFRESH_TOKEN );
self::delete_user_data( $user_id, self::TOKEN_ID );
self::delete_user_data( $user_id, self::SUBSCRIPTION_ID );
self::delete_user_data( $user_id, self::OPTION_OWNER_USER_ID );
self::delete_user_data( $user_id, self::HOME_URL );
delete_transient( 'image_optimizer_status_check' );
}
}
/**
* clear_session
*/
public static function clear_session() {
if ( Config::CONNECT_MODE === 'site' ) {
self::delete_option( self::CLIENT_ID );
self::delete_option( self::CLIENT_SECRET );
self::delete_option( self::ACCESS_TOKEN );
self::delete_option( self::REFRESH_TOKEN );
self::delete_option( self::TOKEN_ID );
self::delete_option( self::SUBSCRIPTION_ID );
self::delete_option( self::OPTION_OWNER_USER_ID );
self::delete_option( self::HOME_URL );
delete_transient( 'image_optimizer_status_check' );
} else {
$user_id = get_current_user_id();
self::delete_user_data( $user_id, self::CLIENT_ID );
self::delete_user_data( $user_id, self::CLIENT_SECRET );
self::delete_user_data( $user_id, self::ACCESS_TOKEN );
self::delete_user_data( $user_id, self::REFRESH_TOKEN );
self::delete_user_data( $user_id, self::TOKEN_ID );
self::delete_user_data( $user_id, self::SUBSCRIPTION_ID );
self::delete_user_data( $user_id, self::OPTION_OWNER_USER_ID );
self::delete_user_data( $user_id, self::HOME_URL );
delete_transient( 'image_optimizer_status_check' );
}
}
}
@@ -0,0 +1,13 @@
<?php
namespace ImageOptimization\Modules\Connect\Classes\Exceptions;
use Exception;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Service_Exception extends Exception {
protected $message = 'Service Exception';
}
@@ -0,0 +1,16 @@
<?php
namespace ImageOptimization\Modules\Connect\Classes;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
/**
* Class GrantTypes
*/
class GrantTypes extends Basic_Enum {
const CLIENT_CREDENTIALS = 'client_credentials';
const AUTHORIZATION_CODE = 'authorization_code';
const REFRESH_TOKEN = 'refresh_token';
}
@@ -0,0 +1,43 @@
<?php
namespace ImageOptimization\Modules\Connect\Classes;
use ImageOptimization\Classes\Rest\Route;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
/**
* Class Route_Base
*/
class Route_Base extends Route {
protected bool $override = false;
protected $auth = true;
protected string $path = '';
public function get_methods(): array {
return [];
}
public function get_endpoint(): string {
return 'connect/' . $this->get_path();
}
public function get_path(): string {
return $this->path;
}
public function get_name(): string {
return '';
}
public function post_permission_callback( \WP_REST_Request $request ): bool {
return $this->get_permission_callback( $request );
}
public function get_permission_callback( \WP_REST_Request $request ): bool {
$valid = $this->permission_callback( $request );
return $valid && user_can( $this->current_user_id, 'manage_options' );
}
}
@@ -0,0 +1,301 @@
<?php
namespace ImageOptimization\Modules\Connect\Classes;
use ImageOptimization\Modules\Connect\Classes\Exceptions\Service_Exception;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
/**
* Class Service
*/
class Service {
const REFRESH_TOKEN_LOCK = '_connect_refresh_token';
const APP_TYPE = 'APP_IO';
/**
* Registers new client and returns client ID
*
* @return string
* @throws Service_Exception
*/
public static function register_client(): string {
$clients_url = Utils::get_clients_url();
if ( ! $clients_url ) {
throw new Service_Exception( 'Missing client registration URL' );
}
$client_data = self::request( $clients_url, [
'method' => 'POST',
'headers' => [
'Content-Type' => 'application/json',
],
'body' => wp_json_encode( [
'redirect_uri' => Utils::get_redirect_uri(),
'app_type' => Config::APP_TYPE,
] ),
], 201 );
$client_id = $client_data['client_id'] ?? null;
$client_secret = $client_data['client_secret'] ?? null;
Data::set_client_id( $client_id );
Data::set_client_secret( $client_secret );
Data::set_home_url();
return $client_id;
}
/**
* Deactivate license
*
* @return void
* @throws Service_Exception
*/
public static function deactivate_license(): void {
$client_id = Data::get_client_id();
if ( ! $client_id ) {
throw new Service_Exception( 'Missing client ID' );
}
$deactivation_url = Utils::get_deactivation_url( $client_id );
if ( ! $deactivation_url ) {
throw new Service_Exception( 'Missing deactivation URL' );
}
$access_token = Data::get_access_token();
if ( ! $access_token ) {
throw new Service_Exception( 'Missing access token' );
}
$refresh_token = Data::get_refresh_token();
if ( ! $refresh_token ) {
throw new Service_Exception( 'Missing refresh token' );
}
self::request( $deactivation_url, [
'method' => 'DELETE',
'headers' => [
'Authorization' => "Bearer {$access_token}",
],
], 204 );
self::get_token( 'refresh_token', $refresh_token );
}
/**
* disconnect
*
* @return void
* @throws Service_Exception
*/
public static function disconnect(): void {
$sessions_url = Utils::get_sessions_url();
if ( ! $sessions_url ) {
throw new Service_Exception( 'Missing sessions URL' );
}
$access_token = Data::get_access_token();
if ( ! $access_token ) {
throw new Service_Exception( 'Missing access token' );
}
self::request( $sessions_url, [
'method' => 'DELETE',
'headers' => [
'Content-Type' => 'application/json',
'Authorization' => "Bearer {$access_token}",
],
], 204 );
Data::clear_session();
}
/**
* Get token & optionally save to user
*
* @param string $grant_type
*
* @param string|null $credential
*
* @return array
* @throws Service_Exception
*/
public static function get_token( string $grant_type, ?string $credential = null, ?bool $update = true ): array {
$token_url = Utils::get_token_url();
if ( ! $token_url ) {
throw new Service_Exception( 'Missing token URL' );
}
$client_id = Data::get_client_id();
$client_secret = Data::get_client_secret();
if ( empty( $client_id ) || empty( $client_secret ) ) {
throw new Service_Exception( 'Missing client ID or secret' );
}
$body = [
'grant_type' => $grant_type,
'redirect_uri' => Utils::get_redirect_uri(),
];
switch ( $grant_type ) {
case GrantTypes::AUTHORIZATION_CODE:
$body['code'] = $credential;
break;
case GrantTypes::REFRESH_TOKEN:
$body[ GrantTypes::REFRESH_TOKEN ] = $credential;
break;
case GrantTypes::CLIENT_CREDENTIALS:
$body['redirect_uri'] = Utils::get_redirect_uri( Data::get_home_url() );
break;
default:
throw new Service_Exception( 'Invalid grant type' );
}
$data = self::request( $token_url, [
'method' => 'POST',
'headers' => [
'x-elementor-apps' => Config::APP_NAME,
'Authorization' => 'Basic ' . base64_encode( "{$client_id}:{$client_secret}" ),
],
'body' => $body,
] );
if ( $update ) {
Data::set_connect_mode_data( Data::TOKEN_ID, $data['id_token'] ?? null );
Data::set_connect_mode_data( Data::ACCESS_TOKEN, $data['access_token'] ?? null );
Data::set_connect_mode_data( Data::REFRESH_TOKEN, $data['refresh_token'] ?? null );
Data::set_connect_mode_data( Data::OPTION_OWNER_USER_ID, get_current_user_id() );
}
return $data;
}
public static function jwt_decode( $payload ): string {
static $jwks = null;
$jwks_url = Utils::get_jwks_url();
if ( ! $jwks_url ) {
return __( 'Missing JWKS URL' );
}
if ( ! $jwks ) {
$jwks = self::request( $jwks_url, [
'method' => 'GET',
] );
}
if ( ! class_exists( 'JWT' ) ) {
require_once IMAGE_OPTIMIZATION_PATH . 'vendor/autoload.php';
}
try {
$decoded = \Firebase\JWT\JWT::decode( $payload, \Firebase\JWT\JWK::parseKeySet( $jwks ) );
return json_encode( $decoded, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES );
} catch ( \Throwable $th ) {
if ( $th instanceof \Firebase\JWT\ExpiredException ) {
self::get_token( GrantTypes::REFRESH_TOKEN, Data::get_refresh_token() );
return self::jwt_decode( $payload );
}
return $th->getMessage();
}
}
/**
* @param string $url
* @param array $args
* @param int $valid_response_code
*
* @return array|null
* @throws Service_Exception
*/
public static function request( string $url, array $args, int $valid_response_code = 200 ): ?array {
$args['timeout'] = 30;
$args['headers'] = array_replace_recursive( [
'x-elementor-app-type' => self::APP_TYPE,
], $args['headers'] ?? [] );
$response = wp_remote_request( $url, $args );
if ( is_wp_error( $response ) ) {
error_log( '[Connect Service]: ' . $response->get_error_message() );
throw new Service_Exception( $response->get_error_message() );
}
if ( $valid_response_code !== wp_remote_retrieve_response_code( $response ) ) {
error_log( '[Connect Service]: invalid status code' . wp_remote_retrieve_response_code( $response ) );
throw new Service_Exception( wp_remote_retrieve_body( $response ) );
}
return json_decode( wp_remote_retrieve_body( $response ), true );
}
public static function refresh_token() {
$lock_key = Config::APP_NAME . self::REFRESH_TOKEN_LOCK;
$last_token = Data::fetch_option( $lock_key, '' );
$current_refresh_token = Data::get_refresh_token();
if ( ! empty( $last_token ) && $last_token === $current_refresh_token ) {
sleep( 1 );
delete_option( $lock_key );
return;
}
delete_option( $lock_key );
$locked = Data::insert_option_uniquely( $lock_key, $current_refresh_token );
if ( ! $locked ) {
sleep( 1 );
delete_option( $lock_key );
return;
}
self::get_token( GrantTypes::REFRESH_TOKEN, $current_refresh_token );
delete_option( $lock_key );
}
/**
* @throws Service_Exception
*/
public static function update_redirect_uri(): void {
$client_id = Data::get_client_id();
if ( ! $client_id ) {
throw new Service_Exception( 'Missing client ID' );
}
$client_patch_url = Utils::get_clients_patch_url( $client_id );
[ 'access_token' => $access_token ] = self::get_token(
GrantTypes::CLIENT_CREDENTIALS,
null,
false
);
self::request( $client_patch_url, [
'method' => 'PATCH',
'headers' => [
'Content-Type' => 'application/json',
'Authorization' => "Bearer {$access_token}",
],
'body' => json_encode( [
'redirect_uri' => Utils::get_redirect_uri(),
] ),
] );
self::refresh_token();
Data::set_home_url();
}
}
@@ -0,0 +1,128 @@
<?php
namespace ImageOptimization\Modules\Connect\Classes;
use ImageOptimization\Modules\Connect\Classes\Exceptions\Service_Exception;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
/**
* Class Utils
*/
class Utils {
/**
* get_clients_url
* @return string
*/
public static function get_clients_url(): string {
return self::get_base_url() . '/api/v1/clients';
}
/**
* get_redirect_uri
* @return string
*/
public static function get_redirect_uri( string $domain = '' ): string {
if ( false !== strpos( Config::ADMIN_PAGE, '?page=' ) ) {
$admin_url = admin_url( Config::ADMIN_PAGE );
} else {
$admin_url = admin_url( 'options-general.php?page=' . Config::ADMIN_PAGE );
}
if ( $domain ) {
$parsed_url = parse_url( $admin_url );
$path = $parsed_url['path'] . ( isset( $parsed_url['query'] ) ? '?' . $parsed_url['query'] : '' );
return rtrim( $domain, '/' ) . $path;
}
return $admin_url;
}
public static function get_auth_url(): string {
return self::get_base_url() . '/v1/oauth2/auth';
}
/**
* Get full authorization URL with all required parameters
*
* @param string $client_id
*
* @return string
* @throws Service_Exception
*/
public static function get_authorize_url( string $client_id ): string {
return add_query_arg( [
'client_id' => $client_id,
'redirect_uri' => rawurlencode( self::get_redirect_uri() ),
'response_type' => 'code',
'scope' => Config::SCOPES,
'state' => wp_create_nonce( Config::STATE_NONCE ),
], self::get_auth_url() );
}
/**
* get_deactivation_url
* @param string $client_id
*
* @return string
*/
public static function get_deactivation_url( string $client_id ): string {
return self::get_base_url() . "/api/v1/clients/{$client_id}/activation";
}
public static function get_jwks_url(): string {
return self::get_base_url() . '/v1/.well-known/jwks.json';
}
/**
* get_sessions_url
* @return string
*/
public static function get_sessions_url(): string {
return self::get_base_url() . '/api/v1/session';
}
public static function get_token_url(): string {
return self::get_base_url() . '/api/v1/oauth2/token';
}
/**
* Get clients URL
*
* @param string $client_id
*
* @return string
*/
public static function get_clients_patch_url( string $client_id ): string {
return self::get_base_url() . "/api/v1/clients/{$client_id}";
}
/**
* get_base_url
* @return string
*/
public static function get_base_url(): string {
return apply_filters( 'image_optimizer_connect_get_base_url', Config::BASE_URL );
}
/**
* is_valid_home_url
* @return bool
*/
public static function is_valid_home_url(): bool {
static $valid = null;
if ( null === $valid ) {
if ( empty( Data::get_home_url() ) ) {
$valid = true;
} else {
$valid = Data::get_home_url() === home_url();
}
}
return $valid;
}
}
@@ -0,0 +1,85 @@
<?php
namespace ImageOptimization\Modules\Connect\Components;
use ImageOptimization\Modules\Connect\Classes\{
Config,
Data,
GrantTypes,
Service,
Utils,
};
use ImageOptimization\Classes\Services\Client;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
/**
* Class Handler
*/
class Handler {
private function should_handle_auth_code(): bool {
global $plugin_page;
$page_slug = explode( 'page=', Config::ADMIN_PAGE );
$is_connect_admin_page = false;
if ( ! empty( $page_slug[1] ) && $page_slug[1] === $plugin_page ) {
$is_connect_admin_page = true;
}
if ( ! $is_connect_admin_page && Config::ADMIN_PAGE === $plugin_page ) {
$is_connect_admin_page = true;
}
if ( ! $is_connect_admin_page ) {
return false;
}
$code = $_GET['code'] ?? null;
$state = $_GET['state'] ?? null;
if ( empty( $code ) || empty( $state ) ) {
return false;
}
return true;
}
private function validate_nonce( $state ) {
if ( ! wp_verify_nonce( $state, Config::STATE_NONCE ) ) {
wp_die( 'Invalid state' );
}
}
public function handle_auth_code() {
if ( ! $this->should_handle_auth_code() ) {
return;
}
$code = sanitize_text_field( $_GET['code'] );
$state = sanitize_text_field( $_GET['state'] );
// Check if the state is valid
$this->validate_nonce( $state );
// Exchange the code for an access token and store it
Service::get_token( GrantTypes::AUTHORIZATION_CODE, $code );
// Makes sure we won't stick in the mismatch limbo
Data::set_home_url();
// Redirect to the redirect URI
wp_redirect( Utils::get_redirect_uri() );
exit;
}
/**
* Handler constructor.
*/
public function __construct() {
add_action( 'admin_init', [ $this, 'handle_auth_code' ] );
}
}
@@ -0,0 +1,68 @@
<?php
namespace ImageOptimization\Modules\Connect;
use ImageOptimization\Classes\Module_Base;
use ImageOptimization\Modules\Connect\Classes\{
Data,
Utils,
};
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
/**
* Class Module
*/
class Module extends Module_Base {
/**
* Get module name.
* Retrieve the module name.
* @access public
* @return string Module name.
*/
public function get_name() {
return 'connect';
}
/**
* component_list
* @return string[]
*/
public static function component_list() : array {
return [
'Handler',
];
}
/**
* routes_list
* @return string[]
*/
public static function routes_list() : array {
return [
'Authorize',
'Disconnect',
'Deactivate',
'Deactivate_And_Disconnect',
'Version',
'Switch_Domain',
];
}
public static function is_connected() : bool {
return ! ! Data::get_access_token() && Utils::is_valid_home_url();
}
public static function is_active() : bool {
// TODO: Add login to check if the function should be active or not.
return empty( get_option( 'image_optimizer_client_data' ) );
}
public function __construct() {
$this->register_components();
$this->register_routes();
}
}
@@ -0,0 +1,103 @@
<?php
namespace ImageOptimization\Modules\Connect\Rest;
use ImageOptimization\Modules\Connect\Classes\{
Data,
Route_Base,
Service,
Utils
};
use ImageOptimization\Modules\Connect\Module as Connect;
use Throwable;
use WP_REST_Request;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
/**
* Class Authorize
*/
class Authorize extends Route_Base {
public string $path = 'authorize';
public const NONCE_NAME = 'wp_rest';
public function get_methods(): array {
return [ 'POST' ];
}
public function get_name(): string {
return 'authorize';
}
public function POST( WP_REST_Request $request ) {
$valid = $this->verify_nonce_and_capability(
$request->get_param( self::NONCE_NAME ),
self::NONCE_NAME
);
if ( is_wp_error( $valid ) ) {
return $this->respond_error_json( [
'message' => $valid->get_error_message(),
'code' => 'forbidden',
] );
}
if ( Connect::is_connected() && Utils::is_valid_home_url() ) {
return $this->respond_error_json( [
'message' => esc_html__( 'You are already connected', 'image-optimization' ),
'code' => 'forbidden',
] );
}
try {
$client_id = Data::get_client_id();
if ( ! $client_id ) {
$client_id = Service::register_client();
}
if ( ! Utils::is_valid_home_url() ) {
if ( $request->get_param( 'update_redirect_uri' ) ) {
Service::update_redirect_uri();
} else {
return $this->respond_error_json( [
'message' => esc_html__( 'Connected domain mismatch', 'image-optimization' ),
'code' => 'forbidden',
] );
}
}
$authorize_url = Utils::get_authorize_url( $client_id );
$additional_source_campaign = [];
$image_optimization_campaign = get_transient( 'elementor_image_optimization_campaign' );
if ( ! empty( $image_optimization_campaign['source'] ) ) {
$additional_source_campaign['utm_source'] = $image_optimization_campaign['source'];
}
if ( ! empty( $image_optimization_campaign['medium'] ) ) {
$additional_source_campaign['utm_medium'] = $image_optimization_campaign['medium'];
}
if ( ! empty( $image_optimization_campaign['campaign'] ) ) {
$additional_source_campaign['utm_campaign'] = $image_optimization_campaign['campaign'];
}
if ( ! empty( $additional_source_campaign ) ) {
$authorize_url = add_query_arg( $additional_source_campaign, $authorize_url );
}
return $this->respond_success_json( $authorize_url );
} catch ( Throwable $t ) {
return $this->respond_error_json( [
'message' => $t->getMessage(),
'code' => 'internal_server_error',
] );
}
}
}
@@ -0,0 +1,63 @@
<?php
namespace ImageOptimization\Modules\Connect\Rest;
use ImageOptimization\Modules\Connect\Classes\{
Data,
Route_Base,
Service
};
use Throwable;
use WP_REST_Request;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
/**
* Class Disconnect
*/
class Deactivate_And_Disconnect extends Route_Base {
public string $path = 'deactivate_and_disconnect';
public const NONCE_NAME = 'wp_rest';
public function get_methods(): array {
return [ 'POST' ];
}
public function get_name(): string {
return 'deactivate_and_disconnect';
}
public function POST( WP_REST_Request $request ) {
$valid = $this->verify_nonce_and_capability(
$request->get_param( self::NONCE_NAME ),
self::NONCE_NAME
);
if ( is_wp_error( $valid ) ) {
return $this->respond_error_json( [
'message' => $valid->get_error_message(),
'code' => 'forbidden',
] );
}
try {
if ( $request->get_param( 'clear_session' ) ) {
Data::clear_session();
return $this->respond_success_json();
}
Service::deactivate_license();
Service::disconnect();
return $this->respond_success_json();
} catch ( Throwable $t ) {
Data::ensure_reset_connect();
return $this->respond_error_json( [
'message' => $t->getMessage(),
'code' => 'internal_server_error',
] );
}
}
}
@@ -0,0 +1,57 @@
<?php
namespace ImageOptimization\Modules\Connect\Rest;
use ImageOptimization\Modules\Connect\Classes\{
Data,
Route_Base,
Service
};
use Throwable;
use WP_REST_Request;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
/**
* Class Deactivate
*/
class Deactivate extends Route_Base {
public string $path = 'deactivate';
public const NONCE_NAME = 'wp_rest';
public function get_methods(): array {
return [ 'POST' ];
}
public function get_name(): string {
return 'deactivate';
}
public function POST( WP_REST_Request $request ) {
$valid = $this->verify_nonce_and_capability(
$request->get_param( self::NONCE_NAME ),
self::NONCE_NAME
);
if ( is_wp_error( $valid ) ) {
return $this->respond_error_json( [
'message' => $valid->get_error_message(),
'code' => 'forbidden',
] );
}
try {
Service::deactivate_license();
return $this->respond_success_json();
} catch ( Throwable $t ) {
Data::ensure_reset_connect();
return $this->respond_error_json( [
'message' => $t->getMessage(),
'code' => 'internal_server_error',
] );
}
}
}
@@ -0,0 +1,57 @@
<?php
namespace ImageOptimization\Modules\Connect\Rest;
use ImageOptimization\Modules\Connect\Classes\{
Data,
Route_Base,
Service
};
use Throwable;
use WP_REST_Request;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
/**
* Class Disconnect
*/
class Disconnect extends Route_Base {
public string $path = 'disconnect';
public const NONCE_NAME = 'wp_rest';
public function get_methods(): array {
return [ 'POST' ];
}
public function get_name(): string {
return 'disconnect';
}
public function POST( WP_REST_Request $request ) {
$valid = $this->verify_nonce_and_capability(
$request->get_param( self::NONCE_NAME ),
self::NONCE_NAME
);
if ( is_wp_error( $valid ) ) {
return $this->respond_error_json( [
'message' => $valid->get_error_message(),
'code' => 'forbidden',
] );
}
try {
Service::disconnect();
return $this->respond_success_json();
} catch ( Throwable $t ) {
Data::ensure_reset_connect();
return $this->respond_error_json( [
'message' => $t->getMessage(),
'code' => 'internal_server_error',
] );
}
}
}
@@ -0,0 +1,68 @@
<?php
namespace ImageOptimization\Modules\Connect\Rest;
use ImageOptimization\Modules\Connect\Classes\{
Data,
Route_Base,
Service,
};
use Throwable;
use WP_REST_Request;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
/**
* Class Switch_Domain
*/
class Switch_Domain extends Route_Base {
public string $path = 'switch_domain';
public const NONCE_NAME = 'wp_rest';
public function get_methods(): array {
return [ 'POST' ];
}
public function get_name(): string {
return 'switch_domain';
}
public function POST( WP_REST_Request $request ) {
$valid = $this->verify_nonce_and_capability(
$request->get_param( self::NONCE_NAME ),
self::NONCE_NAME
);
if ( is_wp_error( $valid ) ) {
return $this->respond_error_json( [
'message' => $valid->get_error_message(),
'code' => 'forbidden',
] );
}
try {
$client_id = Data::get_client_id();
if ( ! $client_id ) {
return $this->respond_error_json( [
'message' => esc_html__( 'Client ID not found', 'image-optimization' ),
'code' => 'bad_request',
] );
}
Service::update_redirect_uri();
return $this->respond_success_json( [
'message' => esc_html__( 'Domain updated!', 'image-optimization' ),
] );
} catch ( Throwable $t ) {
return $this->respond_error_json( [
'message' => $t->getMessage(),
'code' => 'internal_server_error',
] );
}
}
}
@@ -0,0 +1,59 @@
<?php
namespace ImageOptimization\Modules\Connect\Rest;
use ImageOptimization\Modules\Connect\Classes\{
Data,
Route_Base,
Service,
Utils
};
use ImageOptimization\Modules\Connect\Module as Connect;
use Throwable;
use WP_REST_Request;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
/**
* Class Version
*/
class Version extends Route_Base {
public string $path = 'version';
public const NONCE_NAME = 'wp_rest';
public function get_methods(): array {
return [ 'GET' ];
}
public function get_name(): string {
return 'version';
}
public function GET( WP_REST_Request $request ) {
$valid = $this->verify_nonce_and_capability(
$request->get_param( self::NONCE_NAME ),
self::NONCE_NAME
);
if ( is_wp_error( $valid ) ) {
return $this->respond_error_json( [
'message' => $valid->get_error_message(),
'code' => 'forbidden',
] );
}
try {
return $this->respond_success_json([
'version' => 2,
]);
} catch ( Throwable $t ) {
return $this->respond_error_json( [
'message' => $t->getMessage(),
'code' => 'internal_server_error',
] );
}
}
}
@@ -0,0 +1,85 @@
<?php
namespace ImageOptimization\Modules\Core\Components;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Conflicts {
private array $conflicting_plugins;
private const CONFLICTING_PLUGINS = [
'imagify/imagify.php' => 'Imagify',
'optimole-wp/optimole-wp.php' => 'Image optimization service by Optimole',
'ewww-image-optimizer/ewww-image-optimizer.php' => 'EWWW Image Optimizer',
'ewww-image-optimizer-cloud/ewww-image-optimizer-cloud.php' => 'EWWW Image Optimizer Cloud',
'kraken-image-optimizer/kraken.php' => 'Kraken Image Optimizer',
'shortpixel-image-optimiser/wp-shortpixel.php' => 'ShortPixel Image Optimizer',
'wp-smushit/wp-smush.php' => 'Smush',
'wp-smush-pro/wp-smush.php' => 'Smush PRO',
'tiny-compress-images/tiny-compress-images.php' => 'TinyPNG - JPEG, PNG & WebP image compression',
];
public function render_notice(): void {
$conflicting_plugins_names = $this->conflicting_plugins;
?>
<div class="notice notice-warning image-optimizer__notice image-optimizer__notice--warning">
<p>
<b>
<?php esc_html_e(
'Image optimizer has detected multiple active image optimization plugins.',
'image-optimization'
); ?>
</b>
<span>
<?php esc_html_e(
'Having more than one may result in unexpected results.',
'image-optimization'
); ?>
</span>
</p>
<form action="<?php echo esc_url( admin_url( 'plugins.php' ) ); ?>" method="post" style="margin:0.5em 0;padding:2px">
<span style="margin-inline-end: 8px;"><?php echo esc_html( join( ', ', $conflicting_plugins_names ) ); ?></span>
<input type="hidden" name="action" value="deactivate-selected">
<?php foreach ( array_keys( $this->conflicting_plugins ) as $plugin ) { ?>
<input type="hidden" name="checked[]" value="<?php echo esc_attr( $plugin ); ?>">
<?php } ?>
<input type="hidden" name="_wpnonce" value="<?php echo esc_attr( wp_create_nonce( 'bulk-plugins' ) ); ?>">
<input type="submit"
style="border:none;background-color:transparent;text-decoration:underline;cursor:pointer;font-size:13px;color:#135e96;"
value="<?php esc_html_e( 'Deactivate All', 'image-optimization' ); ?>">
</form>
</div>
<?php
}
public function get_conflicting_plugins(): array {
$plugins = get_option( 'active_plugins' );
$conflicting_plugins_file_names = array_keys( self::CONFLICTING_PLUGINS );
$output = [];
foreach ( $plugins as $plugin_file_name ) {
if ( in_array( $plugin_file_name, $conflicting_plugins_file_names, true ) ) {
$output[ $plugin_file_name ] = self::CONFLICTING_PLUGINS[ $plugin_file_name ];
}
}
return $output;
}
public function __construct() {
$this->conflicting_plugins = $this->get_conflicting_plugins();
if ( ! empty( $this->conflicting_plugins ) ) {
add_action( 'admin_notices', [ $this, 'render_notice' ] );
}
}
}
@@ -0,0 +1,72 @@
<?php
namespace ImageOptimization\Modules\Core\Components;
use ImageOptimization\Classes\Async_Operation\Async_Operation_Hook;
use ImageOptimization\Classes\Logger;
use ImageOptimization\Classes\Migration\{
Migration_Manager,
Migration_Meta,
};
use Throwable;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Migrations {
/**
* @async
* @param string $migration_name
*
* @return bool
*/
public function run_migration( string $migration_name ): bool {
$migration_class = Migration_Manager::get_migration( $migration_name );
if ( ! $migration_class ) {
Logger::log(
Logger::LEVEL_ERROR,
"Migration class for `$migration_name` does not exist."
);
return false;
}
if ( ! method_exists( $migration_class, 'run' ) || ! is_callable( [ $migration_class, 'run' ] ) ) {
Logger::log(
Logger::LEVEL_ERROR,
"The run method does not exist or is not static in the class `$migration_class`."
);
return false;
}
try {
$migration_class::run();
( new Migration_Meta() )
->add_migration_passed( $migration_name )
->save();
Logger::log(
Logger::LEVEL_INFO,
"The migration `$migration_name` successfully executed."
);
} catch ( Throwable $t ) {
Logger::log(
Logger::LEVEL_ERROR,
"Error while running the migration `$migration_name`: " . $t->getMessage()
);
return false;
}
return true;
}
public function __construct() {
add_action( Async_Operation_Hook::DB_MIGRATION, [ $this, 'run_migration' ] );
}
}
@@ -0,0 +1,105 @@
<?php
namespace ImageOptimization\Modules\Core\Components;
use ImageOptimization\Classes\Utils;
use ImageOptimization\Modules\Settings\Module;
use ImageOptimization\Plugin;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Not_Connected_Modal {
const NOT_CONNECTED_MODAL_SLUG = 'image-optimizer-not-connected-modal';
public function render_not_connected_modal() {
add_thickbox(); ?>
<div
id="<?php echo esc_html( self::NOT_CONNECTED_MODAL_SLUG ); ?>"
style="display:none;"
>
<div class="image-optimizer__modal image-optimizer__modal--not-connected" data-modal-slug="<?php echo esc_attr( self::NOT_CONNECTED_MODAL_SLUG ); ?>">
<button class="image-optimizer__modal--not-connected-close">
<svg viewBox="0 0 24 24" width="24" height="24">
<path fill-rule="evenodd" clip-rule="evenodd" d="M18.5303 5.46967C18.8232 5.76256 18.8232 6.23744 18.5303 6.53033L6.53033 18.5303C6.23744 18.8232 5.76256 18.8232 5.46967 18.5303C5.17678 18.2374 5.17678 17.7626 5.46967 17.4697L17.4697 5.46967C17.7626 5.17678 18.2374 5.17678 18.5303 5.46967Z"></path><path fill-rule="evenodd" clip-rule="evenodd" d="M5.46967 5.46967C5.76256 5.17678 6.23744 5.17678 6.53033 5.46967L18.5303 17.4697C18.8232 17.7626 18.8232 18.2374 18.5303 18.5303C18.2374 18.8232 17.7626 18.8232 17.4697 18.5303L5.46967 6.53033C5.17678 6.23744 5.17678 5.76256 5.46967 5.46967Z"></path>
</svg>
</button>
<img src="<?php echo esc_url( plugins_url( 'assets/static/connect.jpg', dirname( __FILE__, 3 ) ) ); ?>" alt="<?php esc_html_e( 'Connect now', 'image-optimization' ); ?>">
<h2><?php esc_html_e( 'Let\'s start optimizing your images!', 'image-optimization' ); ?></h2>
<p><?php esc_html_e( 'Connect the Image Optimizer plugin to speed up your site. We\'ll handle the rest.', 'image-optimization' ); ?></p>
<ul>
<li><?php esc_html_e( 'Boost site speed and compress images', 'image-optimization' ); ?></li>
<li><?php esc_html_e( 'Bulk optimization for faster processing', 'image-optimization' ); ?></li>
<li><?php esc_html_e( 'Convert to AVIF and WebP formats', 'image-optimization' ); ?></li>
<li><?php esc_html_e( 'Works across unlimited websites', 'image-optimization' ); ?></li>
</ul>
<a href="<?php echo admin_url( 'admin.php?page=' . Module::SETTING_BASE_SLUG . '&action=connect' ); ?>" class="image-optimizer__modal--not-connected-connect">
<?php esc_html_e( 'Connect to start', 'image-optimization' ); ?>
</a>
</div>
</div>
<script>
jQuery( document ).ready( function( $ ) {
setTimeout(() => {
const TB_WIDTH = 630;
const TB_HEIGHT = 620;
const msInOneDay = 24 * 60 * 60 * 1000;
const time_dismissed = localStorage.getItem('<?php echo self::NOT_CONNECTED_MODAL_SLUG; ?>');
const show_notice = !time_dismissed || Date.now() - time_dismissed >= msInOneDay;
const $modal = $( '[data-modal-slug="<?php echo esc_js( self::NOT_CONNECTED_MODAL_SLUG ); ?>"]' );
if (show_notice) {
tb_show( '', `/?TB_inline&inlineId=<?php echo esc_js( self::NOT_CONNECTED_MODAL_SLUG ); ?>&height=${TB_HEIGHT - 40}&width=${TB_WIDTH - 30}&modal=true` );
$modal.parents('#TB_window')
.width(TB_WIDTH)
.height(TB_HEIGHT)
.css('margin-left', - TB_WIDTH / 2)
.css('margin-top', - TB_HEIGHT / 2)
.css('top', '50%');
const hideModal = (e) => {
e.preventDefault();
localStorage.setItem('<?php echo esc_js( self::NOT_CONNECTED_MODAL_SLUG ); ?>', Date.now().toString());
tb_remove();
}
const $connectButton = $modal.find( '.image-optimizer__modal--not-connected-connect' );
const $closeButton = $modal.find( '.image-optimizer__modal--not-connected-close' );
$connectButton.on( 'click', function (e) {
hideModal(e);
window.open( $( this ).attr( 'href' ), '_blank' ).focus();
} );
$closeButton.on( 'click', function (e) {
hideModal(e);
} );
} else {
$modal.remove();
}
}, 0);
} );
</script>
<?php
}
public function __construct() {
add_action('current_screen', function () {
// @var ImageOptimizer/Modules/ConnectManager/Module
$module = Plugin::instance()->modules_manager->get_modules( 'connect-manager' );
if ( $module->connect_instance->is_connected() || ! Utils::user_is_admin() ) {
return;
}
if (
Utils::is_plugin_settings_page() ||
Utils::is_bulk_optimization_page()
) {
add_action( 'admin_notices', [ $this, 'render_not_connected_modal' ] );
}
});
}
}
@@ -0,0 +1,128 @@
<?php
namespace ImageOptimization\Modules\Core\Components;
use ImageOptimization\Classes\Image\Image_Query_Builder;
use ImageOptimization\Classes\Utils;
use ImageOptimization\Plugin;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Not_Connected {
const NOT_CONNECTED_NOTICE_SLUG = 'image-optimizer-not-connected';
public function render_not_connected_notice() {
if ( Pointers::is_dismissed( self::NOT_CONNECTED_NOTICE_SLUG ) ) {
return;
}
?>
<div class="notice notice-info notice is-dismissible image-optimizer__notice image-optimizer__notice--pink"
data-notice-slug="<?php echo esc_attr( self::NOT_CONNECTED_NOTICE_SLUG ); ?>">
<div class="image-optimizer__icon-block">
<svg width="32" height="32" fill="none" role="presentation">
<rect width="32" height="32" fill="#FF7BE5" rx="16"/>
<path fill="#fff" d="M10.508 4.135a.125.125 0 0 0-.236 0l-1.183 3.42a.125.125 0 0 1-.078.078L5.553 8.8a.125.125 0 0 0 0 .237l3.458 1.166a.125.125 0 0 1 .078.078l1.183 3.42a.125.125 0 0 0 .236 0l1.182-3.42a.125.125 0 0 1 .078-.078l3.458-1.166a.125.125 0 0 0 0-.237l-3.458-1.167a.125.125 0 0 1-.078-.077l-1.182-3.421ZM17.425 12.738v3.683l-4.073 4.582L26.495 9.598a.125.125 0 0 1 .207.094v14.851a.125.125 0 0 1-.125.125H5.874a.125.125 0 0 1-.09-.212l11.427-11.805a.125.125 0 0 1 .214.087Z"/>
</svg>
</div>
<p>
<b>
<?php esc_html_e(
'Image Optimizer is not connected right now. To start optimizing your images, please connect your account.',
'image-optimization'
); ?>
</b>
<span>
<a href="<?php echo admin_url( 'admin.php?page=' . \ImageOptimization\Modules\Settings\Module::SETTING_BASE_SLUG . '&action=connect' ); ?>">
<?php esc_html_e(
'Connect now',
'image-optimization'
); ?>
</a>
</span>
</p>
</div>
<script>
const onNotConnectedNoticeClose = () => {
const pointer = '<?php echo esc_js( self::NOT_CONNECTED_NOTICE_SLUG ); ?>';
return wp.ajax.post( 'image_optimizer_pointer_dismissed', {
data: {
pointer,
},
nonce: '<?php echo esc_js( wp_create_nonce( 'image-optimization-pointer-dismissed' ) ); ?>',
} );
}
jQuery( document ).ready( function( $ ) {
setTimeout(() => {
const $closeButton = $( '[data-notice-slug="<?php echo esc_js( self::NOT_CONNECTED_NOTICE_SLUG ); ?>"] .notice-dismiss' )
$closeButton
.first()
.on( 'click', onNotConnectedNoticeClose )
$( '[data-notice-slug="<?php echo esc_js( self::NOT_CONNECTED_NOTICE_SLUG ); ?>"] a' )
.first()
.on( 'click', function ( e ) {
e.preventDefault();
onNotConnectedNoticeClose().promise().done(() => {
window.open( $( this ).attr( 'href' ), '_blank' ).focus();
$closeButton.click();
});
})
}, 0);
} );
</script>
<?php
}
public function add_media_menu_badge( $parent_file ) {
global $menu;
foreach ( $menu as &$item ) {
if ( 'upload.php' === $item[2] ) {
$item[0] .= ' <span class="update-plugins count-1"><span class="plugin-count">1</span></span>';
break;
}
}
return $parent_file;
}
public function __construct() {
add_action('current_screen', function () {
if ( ! Utils::user_is_admin() ) {
return;
}
// @var ImageOptimizer/Modules/ConnectManager/Module
$module = Plugin::instance()->modules_manager->get_modules( 'connect-manager' );
if ( $module->connect_instance->is_connected() || ! $module->connect_instance->is_valid_home_url() ) {
return;
}
add_filter( 'parent_file', [ $this, 'add_media_menu_badge' ] );
if (
Utils::is_media_page() ||
Utils::is_plugin_page() ||
Utils::is_single_attachment_page() ||
Utils::is_media_upload_page() ||
Utils::is_wp_dashboard_page() ||
Utils::is_wp_updates_page()
) {
add_action( 'admin_notices', [ $this, 'render_not_connected_notice' ] );
}
});
}
}
@@ -0,0 +1,49 @@
<?php
namespace ImageOptimization\Modules\Core\Components;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Pointers {
const DISMISSED_POINTERS_META_KEY = 'image_optimizer_dismissed_pointers';
public function dismiss_pointers() {
if ( empty( $_POST['nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'image-optimization-pointer-dismissed' ) ) {
wp_send_json_error( [ 'message' => 'Invalid nonce' ] );
}
$pointer = sanitize_text_field( $_POST['data']['pointer'] ) ?? null;
if ( empty( $pointer ) ) {
wp_send_json_error( [ 'message' => 'The pointer id must be provided' ] );
}
$pointer = explode( ',', $pointer );
$user_dismissed_meta = get_user_meta( get_current_user_id(), self::DISMISSED_POINTERS_META_KEY, true );
if ( ! $user_dismissed_meta ) {
$user_dismissed_meta = [];
}
foreach ( $pointer as $item ) {
$user_dismissed_meta[ $item ] = true;
}
update_user_meta( get_current_user_id(), self::DISMISSED_POINTERS_META_KEY, $user_dismissed_meta );
wp_send_json_success( [] );
}
public static function is_dismissed( string $slug ): bool {
$meta = (array) get_user_meta( get_current_user_id(), self::DISMISSED_POINTERS_META_KEY, true );
return key_exists( $slug, $meta );
}
public function __construct() {
add_action( 'wp_ajax_image_optimizer_pointer_dismissed', [ $this, 'dismiss_pointers' ] );
}
}
@@ -0,0 +1,146 @@
<?php
namespace ImageOptimization\Modules\Core\Components;
use DateTime;
use ImageOptimization\Classes\Client\Client;
use ImageOptimization\Classes\Utils;
use ImageOptimization\Plugin;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Renewal_Notice {
const RENEWAL_NOTICE_SLUG = 'image-optimizer-renewal-notice';
public ?int $days_diff = null;
public function date_diff_from_current( $date ) {
$current_date = new DateTime(); // Current date
$given_date = new DateTime( $date );
$interval = $current_date->diff( $given_date );
$this->days_diff = $interval->invert ? -$interval->days : $interval->days;
return $this->days_diff;
}
public function get_renewal_text(): array {
if ( $this->days_diff <= 30 && $this->days_diff > 0 ) {
return [
'title' => esc_html__( 'Image Optimizer Expires Soon!', 'image-optimization' ),
'description' => esc_html__( 'Renew your subscription to maintain optimized images, faster website load times, and high performance.', 'image-optimization' ),
'btn' => esc_html__( 'Turn Auto Renew On', 'image-optimization' ),
'link' => esc_url( 'https://go.elementor.com/io-renew-30/' ),
];
}
if ( $this->days_diff <= 0 && $this->days_diff > -7 ) {
return [
'title' => esc_html__( 'Image Optimizer Has Expired!', 'image-optimization' ),
'description' => esc_html__( 'Renew your license today to continue enjoying premium image optimization and fast load times.', 'image-optimization' ),
'btn' => esc_html__( 'Renew Now', 'image-optimization' ),
'link' => esc_url( 'https://go.elementor.com/io-renew-expire/' ),
];
}
return [
'title' => esc_html__( 'It’s Not Too Late! Renew Image Optimizer', 'image-optimization' ),
'description' => esc_html__( 'Reactivate your license to get back to top image performance.', 'image-optimization' ),
'btn' => esc_html__( 'Renew and Optimize', 'image-optimization' ),
'link' => esc_url( 'https://go.elementor.com/io-renew-post-expire/' ),
];
}
public function render_renewal_notice() {
$text = $this->get_renewal_text();
?>
<div class="notice notice-info is-dismissible image-optimizer__notice image-optimizer__notice--error image-optimizer__renewal-notice"
data-notice-slug="<?php echo esc_attr( self::RENEWAL_NOTICE_SLUG ); ?>" style="display:none;">
<div class="image-optimizer__content-block">
<svg width="32" height="32" fill="none" role="presentation">
<rect width="32" height="32" fill="#232629" rx="16"/>
<path fill="#fff" d="M10.508 4.135a.125.125 0 0 0-.236 0l-1.183 3.42a.125.125 0 0 1-.078.078L5.553 8.8a.125.125 0 0 0 0 .237l3.458 1.166a.125.125 0 0 1 .078.078l1.183 3.42a.125.125 0 0 0 .236 0l1.182-3.42a.125.125 0 0 1 .078-.078l3.458-1.166a.125.125 0 0 0 0-.237l-3.458-1.167a.125.125 0 0 1-.078-.077l-1.182-3.421ZM17.425 12.738v3.683l-4.073 4.582L26.495 9.598a.125.125 0 0 1 .207.094v14.851a.125.125 0 0 1-.125.125H5.874a.125.125 0 0 1-.09-.212l11.427-11.805a.125.125 0 0 1 .214.087Z"/>
</svg>
<p>
<b>
<?php echo $text['title']; ?>
</b>
<span>
<?php echo $text['description']; ?>
</span>
</p>
</div>
<a href="<?php echo $text['link']; ?>" target="_blank" rel="noopener noreferrer">
<?php echo $text['btn']; ?>
</a>
</div>
<script>
jQuery( document ).ready( function( $ ) {
setTimeout(() => {
const $msInOneDay = 24 * 60 * 60 * 1000;
const $time_dismissed = localStorage.getItem('<?php echo self::RENEWAL_NOTICE_SLUG; ?>');
const $show_notice = !$time_dismissed || Date.now() - $time_dismissed >= $msInOneDay;
const $notice = $( '[data-notice-slug="<?php echo esc_js( self::RENEWAL_NOTICE_SLUG ); ?>"]' );
const $closeButton = $( '[data-notice-slug="<?php echo esc_js( self::RENEWAL_NOTICE_SLUG ); ?>"] .notice-dismiss' );
if ($show_notice) {
$notice.css('display', 'flex');
$closeButton.on( 'click', function () {
localStorage.setItem('<?php echo esc_js( self::RENEWAL_NOTICE_SLUG ); ?>', Date.now().toString());
} );
} else {
$notice.remove();
}
}, 0);
} );
</script>
<?php
}
public function add_media_menu_badge( $parent_file ) {
global $menu;
foreach ( $menu as &$item ) {
if ( 'upload.php' === $item[2] ) {
$item[0] .= ' <span class="update-plugins count-1"><span class="plugin-count">1</span></span>';
break;
}
}
return $parent_file;
}
public function __construct() {
add_action('current_screen', function () {
// @var ImageOptimizer/Modules/ConnectManager/Module
$module = Plugin::instance()->modules_manager->get_modules( 'connect-manager' );
if ( ! $module->connect_instance->is_connected() || ! Utils::user_is_admin() ) {
return;
}
$info = Client::get_subscription_info();
if ( empty( $info ) || $info->auto_renew || $this->date_diff_from_current( ( $info->date_next_payment ?? $info->date_end ) ) > 30 ) {
return;
}
add_filter( 'parent_file', [ $this, 'add_media_menu_badge' ] );
if (
Utils::is_media_page() ||
Utils::is_plugin_page() ||
Utils::is_single_attachment_page() ||
Utils::is_media_upload_page() ||
Utils::is_wp_dashboard_page() ||
Utils::is_wp_updates_page()
) {
add_action( 'admin_notices', [ $this, 'render_renewal_notice' ] );
}
});
}
}
@@ -0,0 +1,264 @@
<?php
namespace ImageOptimization\Modules\Core\Components;
use ImageOptimization\Classes\Image\Image_Query_Builder;
use ImageOptimization\Classes\Utils;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class User_Feedback {
public const NOTICE_FEEDBACK_LINK = 'https://go.elementor.com/io-wp-dash-notice-review';
public const FOOTER_FEEDBACK_LINK = 'https://go.elementor.com/io-wp-dash-footer-review';
private const FIRST_IMAGE_OPTIMIZED_FEEDBACK_SLUG = 'image-optimizer-first-image-optimized-feedback';
private const THIRD_OF_IMAGES_OPTIMIZED_FEEDBACK_SLUG = 'image-optimizer-third-images-optimized-feedback';
/**
* Checks if the notice after the first optimization should be rendered.
*
* @param bool $check_location
*
* @return bool
*/
public function should_render_first_optimized_notice( bool $check_location = true ): bool {
if ( Pointers::is_dismissed( self::FIRST_IMAGE_OPTIMIZED_FEEDBACK_SLUG ) ) {
return false;
}
$is_wrong_page = ( ! Utils::is_media_page() && ! Utils::is_plugin_page() && ! Utils::is_single_attachment_page() ) ||
Utils::is_plugin_settings_page();
if ( $check_location && $is_wrong_page ) {
return false;
}
$optimized_image_query = ( new Image_Query_Builder() )
->return_optimized_images()
->set_paging_size( 1 )
->execute();
if ( ! $optimized_image_query->post_count ) {
return false;
}
return true;
}
/**
* Checks if the notice should be rendered after the one third of images were optimized.
*
* @param bool $check_location
*
* @return bool
*/
public function should_render_third_optimized_notice( bool $check_location = true ): bool {
if ( Pointers::is_dismissed( self::THIRD_OF_IMAGES_OPTIMIZED_FEEDBACK_SLUG ) ) {
return false;
}
if ( $check_location && ! Utils::is_plugin_settings_page() ) {
return false;
}
$images_query = ( new Image_Query_Builder() )
->execute();
$optimized_images_query = ( new Image_Query_Builder() )
->return_optimized_images()
->execute();
if ( ! $optimized_images_query->post_count ) {
return false;
}
$optimized_percentage = round( $optimized_images_query->post_count / $images_query->post_count * 100 );
if ( $optimized_percentage < 33.33 ) {
return false;
}
return true;
}
/**
* Renders the notice after the first optimization.
*
* @return void
*/
public function render_first_optimized_notice() {
?>
<div class="notice is-dismissible notice-success notice image-optimizer__notice image-optimizer__notice--success image-optimizer__notice--feedback"
data-notice-slug="<?php echo esc_attr( self::FIRST_IMAGE_OPTIMIZED_FEEDBACK_SLUG ); ?>">
<p>
<b>
<?php esc_html_e(
'Your image has been optimized!',
'image-optimization'
); ?>
</b>
<span>
<?php printf(
__(
'If you enjoyed using Image Optimizer, consider leaving a <a href="%1$s" aria-label="%2$s" target="_blank"
rel="noopener noreferrer">★★★★★</a> review to spread the word.',
'image-optimization'
),
esc_url( self::NOTICE_FEEDBACK_LINK ),
esc_attr__( 'Five stars', 'image-optimization' )
); ?>
</span>
</p>
</div>
<script>
const onClose = () => {
const pointer = '<?php
echo $this->should_render_third_optimized_notice( false ) ?
esc_js( join( ',', [ static::FIRST_IMAGE_OPTIMIZED_FEEDBACK_SLUG, static::THIRD_OF_IMAGES_OPTIMIZED_FEEDBACK_SLUG ] ) ) :
esc_js( static::FIRST_IMAGE_OPTIMIZED_FEEDBACK_SLUG )
?>';
return wp.ajax.post( 'image_optimizer_pointer_dismissed', {
data: {
pointer,
},
nonce: '<?php echo esc_js( wp_create_nonce( 'image-optimization-pointer-dismissed' ) ); ?>',
} );
}
jQuery( document ).ready( function( $ ) {
setTimeout(() => {
const $closeButton = $( '[data-notice-slug="<?php echo esc_js( self::FIRST_IMAGE_OPTIMIZED_FEEDBACK_SLUG ); ?>"] .notice-dismiss' )
$closeButton
.first()
.on( 'click', onClose )
$( '[data-notice-slug="<?php echo esc_js( self::FIRST_IMAGE_OPTIMIZED_FEEDBACK_SLUG ); ?>"] a' )
.first()
.on( 'click', function ( e ) {
e.preventDefault();
onClose().promise().done(() => {
window.open( $( this ).attr( 'href' ), '_blank' ).focus();
$closeButton.click();
});
})
}, 0);
} );
</script>
<?php
}
/**
* Renders the notice after the one third of images were optimized.
*
* @return void
*/
public function render_third_optimized_notice() {
?>
<div class="notice is-dismissible notice-success notice image-optimizer__notice image-optimizer__notice--success image-optimizer__notice--feedback"
data-notice-slug="<?php echo esc_attr( self::THIRD_OF_IMAGES_OPTIMIZED_FEEDBACK_SLUG ); ?>">
<p>
<b>
<?php esc_html_e(
'Thanks for using Image Optimizer!',
'image-optimization'
); ?>
</b>
<span>
<?php printf(
__(
'If you\'ve enjoyed it, consider leaving a <a href="%1$s" aria-label="%2$s" target="_blank"
rel="noopener noreferrer">★★★★★</a> review to spread the word.',
'image-optimization'
),
esc_url( self::NOTICE_FEEDBACK_LINK ),
esc_attr__( 'Five stars', 'image-optimization' )
); ?>
</span>
</p>
</div>
<script>
const onClose = () => {
const pointer = '<?php
echo $this->should_render_first_optimized_notice( false ) ?
esc_js( join( ',', [ static::FIRST_IMAGE_OPTIMIZED_FEEDBACK_SLUG, static::THIRD_OF_IMAGES_OPTIMIZED_FEEDBACK_SLUG ] ) ) :
esc_js( static::THIRD_OF_IMAGES_OPTIMIZED_FEEDBACK_SLUG )
?>';
return wp.ajax.post( 'image_optimizer_pointer_dismissed', {
data: {
pointer,
},
nonce: '<?php echo esc_js( wp_create_nonce( 'image-optimization-pointer-dismissed' ) ); ?>',
} );
}
jQuery( document ).ready( function( $ ) {
setTimeout(() => {
const $closeButton = $( '[data-notice-slug="<?php echo esc_js( self::THIRD_OF_IMAGES_OPTIMIZED_FEEDBACK_SLUG ); ?>"] .notice-dismiss' );
$closeButton
.first()
.on( 'click', onClose)
$( '[data-notice-slug="<?php echo esc_js( self::THIRD_OF_IMAGES_OPTIMIZED_FEEDBACK_SLUG ); ?>"] a' )
.first()
.on( 'click', function ( e ) {
e.preventDefault();
onClose().promise().done(() => {
window.open( $( this ).attr( 'href' ), '_blank' ).focus();
$closeButton.click();
});
})
}, 0);
} );
</script>
<?php
}
/**
* Renders the admin footer feedback notice.
*
* @return void
*/
public function add_leave_feedback_footer_text(): void {
printf(
__( '<b>Found Image Optimizer helpful?</b> Leave us a <a href="%1$s" aria-label="%2$s" target="_blank"
rel="noopener noreferrer">★★★★★</a> rating!',
'image-optimization'
),
esc_url( self::FOOTER_FEEDBACK_LINK ),
esc_attr__( 'Five stars', 'image-optimization' )
);
}
public function __construct() {
add_action('current_screen', function () {
if ( ! Utils::user_is_admin() ) {
return;
}
if ( $this->should_render_first_optimized_notice() ) {
add_action( 'admin_notices', [ $this, 'render_first_optimized_notice' ] );
}
if ( $this->should_render_third_optimized_notice() ) {
add_action( 'admin_notices', [ $this, 'render_third_optimized_notice' ] );
}
if ( Utils::is_media_page() || Utils::is_plugin_page() || Utils::is_single_attachment_page() ) {
add_filter( 'admin_footer_text', [ $this, 'add_leave_feedback_footer_text' ] );
}
});
}
}
@@ -0,0 +1,436 @@
<?php
namespace ImageOptimization\Modules\Core;
use ImageOptimization\Modules\Optimization\{
Classes\Validate_Image,
Rest\Cancel_Bulk_Optimization,
Rest\Optimize_Bulk,
};
use ImageOptimization\Modules\Settings\Classes\Settings;
use ImageOptimization\Modules\Backups\Rest\{
Restore_All,
Remove_Backups,
};
use ImageOptimization\Classes\{
Async_Operation\Async_Operation,
Async_Operation\Async_Operation_Queue,
Async_Operation\Queries\Operation_Query,
Image\Image_Meta,
Image\Image_Optimization_Error_Type,
Image\Image_Status,
Migration\Migration_Manager,
Module_Base,
Utils,
};
use ImageOptimization\Plugin;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Module extends Module_Base {
public function get_name(): string {
return 'core';
}
public static function component_list() : array {
return [
'Pointers',
'Migrations',
'Conflicts',
'User_Feedback',
'Not_Connected',
'Not_Connected_Modal',
'Renewal_Notice',
];
}
private function render_top_bar() {
?>
<div id="image-optimization-top-bar"></div>
<?php
}
private function render_app() {
?>
<div class="clear"></div>
<div id="image-optimization-app"></div>
<?php
}
public function maybe_add_quota_reached_notice() {
// @var ImageOptimizer/Modules/ConnectManager/Module
$module = Plugin::instance()->modules_manager->get_modules( 'connect-manager' );
if ( ! $module->connect_instance->get_connect_status() || $module->connect_instance->images_left() > 0 ) {
return;
}
?>
<div class="notice notice-warning notice image-optimizer__notice image-optimizer__notice--warning">
<p>
<b>
<?php esc_html_e(
'You’ve reached your plan quota.',
'image-optimization'
); ?>
</b>
<span>
<?php esc_html_e(
'You have no images left to optimize in your current plan.',
'image-optimization'
); ?>
<a href="https://go.elementor.com/io-quota-upgrade/">
<?php esc_html_e(
'Upgrade plan now',
'image-optimization'
); ?>
</a>
</span>
</p>
</div>
<?php
}
public function maybe_add_80_quota_reached_notice() {
// @var ImageOptimizer/Modules/ConnectManager/Module
$module = Plugin::instance()->modules_manager->get_modules( 'connect-manager' );
$connect_status = $module->connect_instance->get_connect_status();
if ( ! isset( $connect_status->quota ) && ! isset( $connect_status->used_quota ) ) {
return;
}
$usage = $connect_status->used_quota / $connect_status->quota * 100;
if ( ! $module->connect_instance->get_connect_status() || ( $usage < 80 || $usage === 100 ) ) {
return;
}
?>
<div class="notice notice-warning notice image-optimizer__notice image-optimizer__notice--warning">
<p>
<b>
<?php esc_html_e(
'You’ve used 80% of your plan quota.',
'image-optimization'
); ?>
</b>
<span>
<?php esc_html_e(
'Upgrade now to avoid interruptions.',
'image-optimization'
); ?>
<a href="https://go.elementor.com/io-quota-upgrade/">
<?php esc_html_e(
'Upgrade plan now',
'image-optimization'
); ?>
</a>
</span>
</p>
</div>
<?php
}
public function maybe_add_url_mismatch_notice() {
// @var ImageOptimizer/Modules/ConnectManager/Module
$module = Plugin::instance()->modules_manager->get_modules( 'connect-manager' );
if ( $module->connect_instance->is_valid_home_url() ) {
return;
}
?>
<div class="notice notice-error notice image-optimizer__notice image-optimizer__notice--error">
<p>
<b>
<?php esc_html_e(
'Your license key does not match your current domain, causing a mismatch.',
'image-optimization'
); ?>
</b>
<span>
<?php esc_html_e(
'This is most likely due to a change in the domain URL of your site (including HTTP/SSL migration).',
'image-optimization'
); ?>
<button type="button" onclick="document.dispatchEvent( new Event( 'image-optimizer/auth/url-mismatch-modal/open' ) );">
<?php esc_html_e(
'Fix mismatched URL',
'image-optimization'
); ?>
</button>
</span>
</p>
</div>
<?php
}
public function add_plugin_links( $links, $plugin_file_name ): array {
if ( ! str_ends_with( $plugin_file_name, '/image-optimization.php' ) ) {
return (array) $links;
}
$custom_links = [
'settings' => sprintf(
'<a href="%s">%s</a>',
admin_url( 'admin.php?page=' . \ImageOptimization\Modules\Settings\Module::SETTING_BASE_SLUG ),
esc_html__( 'Settings', 'image-optimization' )
),
];
// @var ImageOptimizer/Modules/ConnectManager/Module
$module = Plugin::instance()->modules_manager->get_modules( 'connect-manager' );
if ( $module->connect_instance->is_connected() ) {
$custom_links['upgrade'] = sprintf(
'<a href="%s" style="color: #524CFF; font-weight: 700;" target="_blank" rel="noopener noreferrer">%s</a>',
'https://go.elementor.com/io-plugins-upgrade/',
esc_html__( 'Upgrade', 'image-optimization' )
);
} else {
$custom_links['connect'] = sprintf(
'<a href="%s" style="color: #524CFF; font-weight: 700;">%s</a>',
admin_url( 'admin.php?page=' . \ImageOptimization\Modules\Settings\Module::SETTING_BASE_SLUG ),
esc_html__( 'Connect', 'image-optimization' )
);
}
return array_merge( $custom_links, $links );
}
public function enqueue_global_assets() {
wp_enqueue_style(
'image-optimization-admin-fonts',
'https://fonts.googleapis.com/css2?family=Roboto:wght@400;500;700&display=swap',
[],
IMAGE_OPTIMIZATION_VERSION
);
wp_enqueue_style(
'image-optimization-core-style-admin',
$this->get_css_assets_url( 'style-admin', 'assets/build/' ),
[],
IMAGE_OPTIMIZATION_VERSION,
);
}
/**
* Enqueue styles and scripts
*/
private function enqueue_scripts() {
$asset_file = require IMAGE_OPTIMIZATION_ASSETS_PATH . 'build/admin.asset.php';
foreach ( $asset_file['dependencies'] as $style ) {
wp_enqueue_style( $style );
}
wp_enqueue_script(
'image-optimization-admin',
$this->get_js_assets_url( 'admin' ),
array_merge( $asset_file['dependencies'], [ 'wp-util' ] ),
$asset_file['version'],
true
);
wp_localize_script(
'image-optimization-admin',
'imageOptimizerAppSettings',
[
'siteUrl' => wp_parse_url( get_site_url(), PHP_URL_HOST ),
'thumbnailSizes' => wp_get_registered_image_subsizes(),
'isDevelopment' => defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG,
]
);
/**
* @var ImageOptimizer\Modules\ConnectManager\Module $module
*/
$module = Plugin::instance()->modules_manager->get_modules( 'connect-manager' );
$is_connect_on_fly = $module->connect_instance->get_is_connect_on_fly();
$connect_email = $module->connect_instance->get_connect_data()['user']['email'] ?? null;
$show_reset = ! $module->connect_instance->is_connected()
&& ( $module->connect_instance->get_client_id() || $module->connect_instance->get_client_secret() );
wp_localize_script(
'image-optimization-admin',
'imageOptimizerUserData',
[
'isConnectOnFly' => $is_connect_on_fly,
'isConnected' => $module->connect_instance->is_connected(),
'isActivated' => $module->connect_instance->is_activated(),
'isUrlMismatch' => ! $module->connect_instance->is_valid_home_url(),
'planData' => $module->connect_instance->is_activated() ? $module->connect_instance->get_connect_status() : null,
'licenseKey' => $module->connect_instance->is_activated() ? $module->connect_instance->get_activation_state() : null,
'imagesLeft' => $module->connect_instance->is_activated() ? $module->connect_instance->images_left() : null,
'isOwner' => $module->connect_instance->is_connected() ? $module->connect_instance->user_is_subscription_owner() : null,
'subscriptionEmail' => $connect_email ? $connect_email : null,
'showResetButton' => $show_reset,
'maxFileSize' => Validate_Image::get_max_file_size(),
'helpVideos' => Settings::get( Settings::HELP_VIDEOS ),
'wpRestNonce' => wp_create_nonce( 'wp_rest' ),
'disconnect' => wp_create_nonce( 'wp_rest' ),
'authInitNonce' => wp_create_nonce( $module->connect_instance->connect_init_nonce() ),
'authDisconnectNonce' => wp_create_nonce( $module->connect_instance->disconnect_nonce() ),
'authDeactivateNonce' => wp_create_nonce( $module->connect_instance->deactivate_nonce() ),
'authGetSubscriptionsNonce' => wp_create_nonce( $module->connect_instance->get_subscriptions_nonce() ),
'authActivateNonce' => wp_create_nonce( $module->connect_instance->activate_nonce() ),
'versionNonce' => wp_create_nonce( $module->connect_instance->version_nonce() ),
'removeBackupsNonce' => wp_create_nonce( Remove_Backups::NONCE_NAME ),
'restoreAllImagesNonce' => wp_create_nonce( Restore_All::NONCE_NAME ),
'optimizeBulkNonce' => wp_create_nonce( Optimize_Bulk::NONCE_NAME ),
'cancelBulkOptimizationNonce' => wp_create_nonce( Cancel_Bulk_Optimization::NONCE_NAME ),
]
);
wp_set_script_translations( 'image-optimization-admin', 'image-optimization' );
}
private function should_render(): bool {
return ( Utils::is_media_page() || Utils::is_plugin_page() ) && Utils::user_is_admin();
}
public static function on_deactivation(): void {
$optimization_query = ( new Operation_Query() )
->set_queue( Async_Operation_Queue::OPTIMIZE )
->set_status( [ Async_Operation::OPERATION_STATUS_PENDING, Async_Operation::OPERATION_STATUS_RUNNING ] )
->set_limit( -1 );
$restoring_query = ( new Operation_Query() )
->set_queue( Async_Operation_Queue::RESTORE )
->set_status( [ Async_Operation::OPERATION_STATUS_PENDING, Async_Operation::OPERATION_STATUS_RUNNING ] )
->set_limit( -1 );
$optimization_operations = Async_Operation::get( $optimization_query );
$restoring_operations = Async_Operation::get( $restoring_query );
foreach ( $optimization_operations as $operation ) {
$image_id = $operation->get_args()['attachment_id'];
if ( ! $image_id ) {
continue;
}
Async_Operation::remove( [ $operation->get_id() ] );
$image_meta = new Image_Meta( $image_id );
if ( empty( $image_meta->get_optimized_sizes() ) ) {
$image_meta->delete();
} else {
$image_meta
->set_status( Image_Status::OPTIMIZATION_FAILED )
->set_error_type( Image_Optimization_Error_Type::PLUGIN_DEACTIVATION )
->save();
}
}
foreach ( $restoring_operations as $operation ) {
$image_id = $operation->get_args()['attachment_id'];
if ( ! $image_id ) {
continue;
}
Async_Operation::remove( [ $operation->get_id() ] );
$image_meta = new Image_Meta( $image_id );
$image_meta
->set_status( Image_Status::RESTORING_FAILED )
->set_error_type( Image_Optimization_Error_Type::PLUGIN_DEACTIVATION )
->save();
}
}
/**
* Renders the Bulk Optimization link on the media pages.
*
* @return void
*/
public function add_bulk_optimization_links(): void {
$page_url = add_query_arg(
[ 'page' => 'image-optimization-bulk-optimization' ],
admin_url( 'upload.php' )
);
?>
<script>
document.addEventListener( 'DOMContentLoaded', function () {
// Grid media is rendered by JS, so the timeout is required
setTimeout( () => {
const targetButton = document.querySelector( '.filter-items .actions input[type=submit]' ) ||
document.querySelector( '.media-toolbar-secondary .select-mode-toggle-button' );
if ( targetButton ) {
const link = document.createElement('a');
link.href = '<?php echo esc_js( $page_url ); ?>';
link.innerText = '<?php echo esc_js( __( 'Bulk Optimization', 'image-optimization' ) ); ?>';
link.className = 'button is-primary image-optimizer__button image-optimizer__button--pink';
targetButton.insertAdjacentElement( 'afterend', link );
}
}, 100 )
} );
</script>
<?php
}
/**
* Module constructor.
*/
public function __construct() {
$this->register_components();
add_action( 'action_scheduler_init', [ Migration_Manager::class, 'init' ] );
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_global_assets' ] );
add_filter( 'plugin_action_links', [ $this, 'add_plugin_links' ], 10, 2 );
add_action('current_screen', function () {
if ( ! $this->should_render() ) {
return;
}
add_action( 'admin_notices', [ $this, 'maybe_add_quota_reached_notice' ] );
add_action( 'admin_notices', [ $this, 'maybe_add_80_quota_reached_notice' ] );
add_action( 'admin_notices', [ $this, 'maybe_add_url_mismatch_notice' ] );
if ( Utils::is_media_page() ) {
add_action('in_admin_header', function () {
$this->render_top_bar();
});
add_action('all_admin_notices', function () {
$this->render_app();
});
}
if ( Utils::is_plugin_page() ) {
add_action('in_admin_header', function () {
$this->render_top_bar();
});
}
add_action('admin_enqueue_scripts', function () {
$this->enqueue_scripts();
});
if ( Utils::is_media_page() ) {
add_action( 'admin_enqueue_scripts', [ $this, 'add_bulk_optimization_links' ] );
}
});
}
}
@@ -0,0 +1,152 @@
.image-optimization-deactivation-modal {
display: none;
}
.image-optimization-deactivation-content {
padding: 20px;
}
/* Thickbox auto-height adjustments */
#TB_window.image-optimization-feedback-thickbox {
height: auto !important;
max-height: 90vh;
overflow: auto;
top: 50% !important;
left: 50% !important;
transform: translate(-50%, -50%) !important;
margin: 0 !important;
width: 600px !important;
border-radius: 8px;
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
}
.image-optimization-feedback-thickbox #TB_ajaxContent {
overflow: visible;
padding: 0;
}
.image-optimization-feedback-thickbox #TB_title {
padding: 5px;
display: flex;
flex-direction: row;
}
.image-optimization-feedback-thickbox #TB_closeWindowButton .tb-close-icon {
box-shadow: none !important;
}
.image-optimization-feedback-thickbox #TB_ajaxWindowTitle {
font-size: 14px;
letter-spacing: 1px;
}
.image-optimization-deactivation-content h3 {
margin-top: 0;
color: #23282d;
font-size: 18px;
font-weight: 600;
}
.image-optimization-deactivation-content p {
color: #666;
margin-bottom: 20px;
}
.image-optimization-feedback-options {
margin-bottom: 20px;
}
.image-optimization-feedback-option {
margin-bottom: 15px;
}
.image-optimization-feedback-option > label {
display: flex;
align-items: center;
cursor: pointer;
color: #23282d;
margin-bottom: 8px;
}
.image-optimization-feedback-option input[type="radio"] {
margin-right: 8px;
}
.image-optimization-feedback-text-field {
margin-left: 24px;
margin-top: 8px;
}
.text-field-hidden {
display: none;
}
.image-optimization-feedback-text-field label {
display: block;
font-size: 12px;
color: #666;
margin-bottom: 4px;
}
.image-optimization-feedback-text-field input,
.image-optimization-feedback-text-field textarea {
width: 100%;
padding: 6px 8px;
border: 1px solid #ddd;
border-radius: 3px;
font-size: 13px;
resize: vertical;
}
.image-optimization-feedback-text-field input:focus,
.image-optimization-feedback-text-field textarea:focus {
border-color: #0073aa;
box-shadow: 0 0 0 1px #0073aa;
outline: none;
}
.image-optimization-deactivation-buttons {
display: flex;
flex-direction: row-reverse;
justify-content: space-between;
gap: 10px;
border-top: 1px solid #e1e1e1;
padding-top: 20px;
}
.image-optimization-btn {
padding: 8px 16px;
border: none;
background: none;
text-decoration: none;
border-radius: 3px;
cursor: pointer;
font-size: 13px;
font-weight: 600;
color: #c0c0c0;
transition: all 0.3s ease;
}
.image-optimization-btn:hover {
background: none;
color: #000;
}
.image-optimization-btn-primary {
background: rgb(240, 171, 252);
border-color: rgb(240, 171, 252);
color: #000;
font-weight: 600;
transition: all 0.3s ease;
}
.image-optimization-btn-primary:hover {
background: #e881fa;
border-color: #e881fa;
color: #000;
}
.image-optimization-btn:focus {
box-shadow: 0 0 0 1px #5b9dd9, 0 0 2px 1px rgba(30, 140, 190, 0.8);
outline: none;
}
@@ -0,0 +1,117 @@
import '../css/style.css';
const REASON_FIELDS = {
unclear_how_to_use: [ 'io_text_field_unclear', 'io_unclear_details' ],
switched_solution: [ 'io_text_field_switched', 'io_switched_details' ],
other: [ 'io_text_field_other', 'io_other_details' ],
};
class ImageOptimizationDeactivationHandler {
constructor() {
this.deactivationLink = document.querySelector(
'[id*="deactivate-image-optimizer"], [id*="deactivate-image-optimization"]',
);
if ( ! this.deactivationLink ) {
return;
}
this.originalDeactivationLink = this.deactivationLink.getAttribute( 'href' );
this.init();
}
modal( title, url, cssClass ) {
window.tb_show?.( title, url );
setTimeout(
() => document.getElementById( 'TB_window' )?.classList.add( cssClass ),
5,
);
}
hideFields() {
document
.querySelectorAll( '.image-optimization-feedback-text-field' )
.forEach( ( f ) => ( f.classList.add( 'text-field-hidden' ) ) );
}
toggleField( reason ) {
this.hideFields();
const fieldId = REASON_FIELDS[ reason ]?.[ 0 ];
if ( fieldId ) {
document.getElementById( fieldId ).classList.remove( 'text-field-hidden' );
}
}
sendRequest( data, done ) {
fetch( window?.imageOptimizationDeactivationFeedback?.ajaxurl || '', {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: new URLSearchParams( data ),
} ).finally( done );
}
handleSubmit() {
const reason = document.querySelector(
'input[name="image_optimization_deactivation_reason"]:checked',
)?.value;
const detailsId = REASON_FIELDS[ reason ]?.[ 1 ];
const extra = detailsId
? document.getElementById( detailsId )?.value || ''
: '';
if ( ! reason ) {
this.deactivate();
return;
}
this.sendRequest(
{
action: 'image_optimization_deactivation_feedback',
reason,
additional_data: extra,
nonce: window?.imageOptimizationDeactivationFeedback?.nonce || '',
},
() => this.deactivate(),
);
}
deactivate() {
window.tb_remove?.();
window.location.href = this.originalDeactivationLink;
}
init() {
this.deactivationLink.addEventListener( 'click', ( e ) => {
e.preventDefault();
this.hideFields();
this.modal(
'QUICK FEEDBACK',
'#TB_inline?width=550&height=auto&inlineId=image-optimization-deactivation-modal',
'image-optimization-feedback-thickbox',
);
} );
document.addEventListener( 'change', ( e ) => {
if ( e.target?.name === 'image_optimization_deactivation_reason' ) {
this.toggleField( e.target.value );
}
} );
document.addEventListener( 'click', ( e ) => {
if ( e.target?.id === 'image-optimization-submit-deactivate' ) {
e.preventDefault();
this.handleSubmit();
}
if ( e.target?.id === 'image-optimization-skip-deactivate' ) {
e.preventDefault();
this.deactivate();
}
} );
}
}
document.addEventListener(
'DOMContentLoaded',
() => new ImageOptimizationDeactivationHandler(),
);
@@ -0,0 +1,274 @@
<?php
namespace ImageOptimization\Modules\Deactivation;
use ImageOptimization\Classes\Logger;
use ImageOptimization\Classes\Module_Base;
use ImageOptimization\Classes\Utils;
use ImageOptimization\Classes\Client\Client;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
/**
* Class Module
*/
class Module extends Module_Base {
const SERVICE_ENDPOINT = 'feedback/deactivation';
/**
* Get module name.
* Retrieve the module name.
*
* @access public
* @return string Module name.
*/
public function get_name(): string {
return 'deactivation';
}
/**
* Check if we should show the deactivation feedback modal
*
* @return bool
*/
private function should_show_feedback(): bool {
global $pagenow;
return 'plugins.php' === $pagenow && Utils::user_is_admin();
}
/**
* Enqueue deactivation feedback assets
*/
public function enqueue_deactivation_assets(): void {
if ( ! $this->should_show_feedback() ) {
return;
}
// Enqueue thickbox for modal
add_thickbox();
wp_enqueue_script(
'deactivation-io',
$this->get_js_assets_url( 'deactivation' ),
[],
IMAGE_OPTIMIZATION_VERSION,
true
);
wp_enqueue_style(
'deactivation-io',
$this->get_css_assets_url( 'style-deactivation' ),
[],
IMAGE_OPTIMIZATION_VERSION,
);
wp_localize_script(
'deactivation-io',
'imageOptimizationDeactivationFeedback',
[
'nonce' => wp_create_nonce( 'image_optimization_deactivation_feedback' ),
'ajaxurl' => admin_url( 'admin-ajax.php' ),
]
);
}
/**
* Add deactivation feedback modal HTML to footer
*/
public function add_deactivation_modal(): void {
if ( ! $this->should_show_feedback() ) {
return;
}
?>
<div id="image-optimization-deactivation-modal" class="image-optimization-deactivation-modal">
<div class="image-optimization-deactivation-content">
<h4>
<?php esc_html_e( 'If you have a moment, please share why you are deactivating Image Optimizer:', 'image-optimization' ); ?>
</h4>
<div class="image-optimization-feedback-options">
<div class="image-optimization-feedback-option">
<label for="io_reason_no_longer_needed">
<input type="radio" name="image_optimization_deactivation_reason" value="no_longer_needed" id="io_reason_no_longer_needed">
<?php esc_html_e( 'I no longer need this plugin', 'image-optimization' ); ?>
</label>
</div>
<div class="image-optimization-feedback-option">
<label for="io_reason_too_expensive">
<input type="radio" name="image_optimization_deactivation_reason" value="too_expensive" id="io_reason_too_expensive">
<?php esc_html_e( 'It\'s too expensive', 'image-optimization' ); ?>
</label>
</div>
<div class="image-optimization-feedback-option">
<label for="io_reason_no_results">
<input type="radio" name="image_optimization_deactivation_reason" value="no_results" id="io_reason_no_results">
<?php esc_html_e( 'The plugin didn\'t provide the results I was hoping for', 'image-optimization' ); ?>
</label>
</div>
<div class="image-optimization-feedback-option">
<label for="io_reason_unclear">
<input type="radio" name="image_optimization_deactivation_reason" value="unclear_how_to_use" id="io_reason_unclear">
<?php esc_html_e( 'I wasn\'t sure how to use the plugin', 'image-optimization' ); ?>
</label>
<div class="image-optimization-feedback-text-field" id="io_text_field_unclear">
<label for="io_unclear_details"><?php esc_html_e( 'Optional: Was anything unclear or confusing?', 'image-optimization' ); ?></label>
<textarea id="io_unclear_details" name="io_unclear_details" rows="3" placeholder="<?php esc_attr_e( 'Please share details...', 'image-optimization' ); ?>"></textarea>
</div>
</div>
<div class="image-optimization-feedback-option">
<label for="io_reason_technical">
<input type="radio" name="image_optimization_deactivation_reason" value="technical_issues" id="io_reason_technical">
<?php esc_html_e( 'I had technical issues or conflicts with my site', 'image-optimization' ); ?>
</label>
</div>
<div class="image-optimization-feedback-option">
<label for="io_reason_switched">
<input type="radio" name="image_optimization_deactivation_reason" value="switched_solution" id="io_reason_switched">
<?php esc_html_e( 'I switched to a different solution', 'image-optimization' ); ?>
</label>
<div class="image-optimization-feedback-text-field" id="io_text_field_switched">
<label for="io_switched_details"><?php esc_html_e( 'Optional: Please share which solution:', 'image-optimization' ); ?></label>
<input type="text" id="io_switched_details" name="io_switched_details" placeholder="<?php esc_attr_e( 'Solution name...', 'image-optimization' ); ?>">
</div>
</div>
<div class="image-optimization-feedback-option">
<label for="io_reason_other">
<input type="radio" name="image_optimization_deactivation_reason" value="other" id="io_reason_other">
<?php esc_html_e( 'Other', 'image-optimization' ); ?>
</label>
<div class="image-optimization-feedback-text-field" id="io_text_field_other">
<label for="io_other_details"><?php esc_html_e( 'Optional: Please share the reason:', 'image-optimization' ); ?></label>
<textarea id="io_other_details" name="io_other_details" rows="3" placeholder="<?php esc_attr_e( 'Please explain...', 'image-optimization' ); ?>"></textarea>
</div>
</div>
</div>
<div class="image-optimization-deactivation-buttons">
<button type="button" id="image-optimization-skip-deactivate" class="image-optimization-btn">
<?php esc_html_e( 'Skip & Deactivate', 'image-optimization' ); ?>
</button>
<button type="button" id="image-optimization-submit-deactivate" class="image-optimization-btn image-optimization-btn-primary">
<?php esc_html_e( 'Submit & Deactivate', 'image-optimization' ); ?>
</button>
</div>
</div>
</div>
<?php
}
/**
* Handle AJAX feedback submission
*/
public function handle_deactivation_feedback(): void {
// Verify nonce
$nonce = sanitize_text_field( wp_unslash( $_POST['nonce'] ?? '' ) );
if ( ! wp_verify_nonce( $nonce, 'image_optimization_deactivation_feedback' ) ) {
wp_send_json_error( esc_html__( 'Security check failed', 'image-optimization' ) );
return;
}
// Check user capabilities
if ( ! Utils::user_is_admin() ) {
wp_send_json_error( esc_html__( 'Insufficient permissions', 'image-optimization' ) );
return;
}
$reason = sanitize_text_field( wp_unslash( $_POST['reason'] ?? '' ) );
$additional_data = '';
// Safely handle additional_data if it exists
if ( isset( $_POST['additional_data'] ) ) {
$additional_data = sanitize_textarea_field( wp_unslash( $_POST['additional_data'] ) );
}
if ( empty( $reason ) ) {
wp_send_json_success( [ 'message' => 'No reason provided' ] );
return;
}
// Send feedback to external service
$feedback_sent = $this->send_feedback_to_service( $reason, $additional_data );
if ( $feedback_sent ) {
wp_send_json_success( [ 'message' => 'Feedback sent successfully' ] );
} else {
// Still return success to not block deactivation, but log the error
Logger::log( Logger::LEVEL_ERROR, 'Failed to send deactivation feedback to service' );
wp_send_json_success( [ 'message' => 'Feedback logged locally' ] );
}
}
/**
* Send feedback to external service
*
* @param string $reason The deactivation reason
* @param string $additional_data Additional feedback data from text fields
* @return bool Whether the feedback was sent successfully
*/
private function send_feedback_to_service( string $reason, string $additional_data = '' ): bool {
$feedback_data = $this->prepare_feedback_data( $reason, $additional_data );
$response = Client::get_instance()->make_request(
'POST',
self::SERVICE_ENDPOINT,
$feedback_data
);
if ( empty( $response ) || is_wp_error( $response ) ) {
$error_message = is_wp_error( $response ) ? $response->get_error_message() : 'Unknown error';
Logger::log( Logger::LEVEL_ERROR, 'Failed to post feedback: ' . $error_message );
return false;
}
return true;
}
/**
* Prepare feedback data for the service
*
* @param string $reason The deactivation reason
* @param string $additional_data Additional feedback data from text fields
* @return array Formatted feedback data
*/
private function prepare_feedback_data( string $reason, string $additional_data = '' ): array {
$data = [
'app' => 'image-optimizer',
'app_version' => IMAGE_OPTIMIZATION_VERSION,
'selected_answer' => $reason,
'site_url' => home_url(),
'wp_version' => get_bloginfo( 'version' ),
'php_version' => PHP_VERSION,
'timestamp' => current_time( 'mysql' ),
'user_agent' => sanitize_text_field( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ?? '' ) ),
'locale' => get_locale(),
];
// Add additional data if provided
if ( ! empty( $additional_data ) ) {
$data['feedback_text'] = $additional_data;
}
return $data;
}
/**
* Constructor
*/
public function __construct() {
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_deactivation_assets' ] );
add_action( 'admin_footer', [ $this, 'add_deactivation_modal' ] );
add_action( 'wp_ajax_image_optimization_deactivation_feedback', [ $this, 'handle_deactivation_feedback' ] );
}
}
@@ -0,0 +1,190 @@
<?php
namespace ImageOptimization\Modules\Oauth\Classes;
use ImageOptimization\Modules\Oauth\Components\Connect;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
/**
* Class Data
*/
class Data {
const CONNECT_CLIENT_DATA_OPTION_NAME = 'image_optimizer_client_data';
const CONNECT_DATA_OPTION_NAME = 'image_optimizer_connect_data';
const OPTION_CONNECT_SITE_KEY = 'image_optimizer_site_key';
const OPTION_CONNECT_STATE = 'image_optimizer_connect_state';
const OPTION_ACTIVATION_STATE = 'image_optimizer_activation_state';
const OPTION_OWNER_USER_ID = 'image_optimizer_owner_user_id';
/**
* set_client_data
* @param $client_id
* @param $auth_secret
*/
public static function set_client_data( $client_id, $auth_secret ) {
update_option( self::CONNECT_CLIENT_DATA_OPTION_NAME, [
'client_id' => $client_id,
'auth_secret' => $auth_secret,
], false );
}
/**
* get_client_data
* @return array
*/
public static function get_client_data(): array {
return get_option( self::CONNECT_CLIENT_DATA_OPTION_NAME, [
'client_id' => '',
'auth_secret' => '',
] );
}
/**
* get_client_id
* @return string
*/
public static function get_client_id(): string {
return self::get_client_data()['client_id'] ?? '';
}
/**
* get_client_secret
* @return string
*/
public static function get_client_secret(): string {
return self::get_client_data()['auth_secret'] ?? '';
}
/**
* get_connect_data
*
* @param bool $force
*
* @return array|null
*/
public static function get_connect_data( bool $force = false ): array {
static $connect_data = null;
if ( $connect_data === null || $force ) {
$connect_data = array_merge(
[
'access_token' => '',
'access_token_secret' => '',
'last_update' => 0,
'token_type' => 'bearer',
'user' => [],
'version' => 1,
],
get_option( self::CONNECT_DATA_OPTION_NAME, [] )
);
}
return $connect_data;
}
/**
* set_connect_data
*
* @param array $data
*/
public static function set_connect_data( array $data = [] ): bool {
$data['last_update'] = time();
$data['version'] = 1;
update_option( self::OPTION_OWNER_USER_ID, get_current_user_id() );
return update_option( self::CONNECT_DATA_OPTION_NAME, $data );
}
/**
* get_access_token
* @return false|mixed
*/
public static function get_access_token() {
return self::get_connect_data()['access_token'] ?? false;
}
/**
* get_connect_state
*
* @param bool $force
*
* @return false|mixed|null|string
*/
public static function get_connect_state( bool $force = false ) {
$state = get_option( static::OPTION_CONNECT_STATE );
if ( ! $state || $force ) {
$state = wp_generate_password( 12, false );
update_option( static::OPTION_CONNECT_STATE, $state, false );
}
return $state;
}
/**
* get_site_key
* @return false|mixed|string|null
*/
public static function get_site_key() {
$site_key = get_option( static::OPTION_CONNECT_SITE_KEY );
if ( ! $site_key ) {
$site_key = md5( uniqid( wp_generate_password() ) );
update_option( static::OPTION_CONNECT_SITE_KEY, $site_key, false );
}
return $site_key;
}
/**
* delete_connect_state
*/
public static function delete_connect_state(): bool {
return delete_option( static::OPTION_CONNECT_STATE );
}
public static function get_activation_state(): string {
return get_option( self::OPTION_ACTIVATION_STATE, '' );
}
public static function set_activation_state( $state ): bool {
return update_option( self::OPTION_ACTIVATION_STATE, $state );
}
public static function delete_activation_state(): bool {
return delete_option( self::OPTION_ACTIVATION_STATE );
}
/**
* reset connect data
*/
public static function reset(): void {
self::delete_connect_state();
self::delete_activation_state();
delete_option( self::OPTION_OWNER_USER_ID );
delete_option( static::CONNECT_DATA_OPTION_NAME );
delete_option( self::CONNECT_CLIENT_DATA_OPTION_NAME );
delete_transient( Connect::STATUS_CHECK_TRANSIENT );
}
public static function images_left(): int {
$plan_data = Connect::get_connect_status();
if ( empty( $plan_data ) ) {
return 0;
}
$quota = $plan_data->quota;
$used_quota = $plan_data->used_quota;
return max( $quota - $used_quota, 0 );
}
public static function user_is_subscription_owner(): bool {
$owner_id = (int) get_option( self::OPTION_OWNER_USER_ID );
return get_current_user_id() === $owner_id;
}
}
@@ -0,0 +1,51 @@
<?php
namespace ImageOptimization\Modules\Oauth\Classes;
use ImageOptimization\Classes\Route;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Route_Base extends Route {
const SITE_URL = 'https://my.elementor.com/connect/v1/';
protected $auth = true;
protected string $path = '';
public function get_methods(): array {
return [];
}
public function get_endpoint(): string {
return 'connect/' . $this->get_path();
}
public function get_path(): string {
return $this->path;
}
public function get_name(): string {
return '';
}
public function get_permission_callback( \WP_REST_Request $request ): bool {
$valid = $this->permission_callback( $request );
return $valid && user_can( $this->current_user_id, 'manage_options' );
}
public function verify_nonce( $nonce = '', $name = '' ) {
if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $nonce ) ), $name ) ) {
wp_die( 'Invalid nonce', 'image-optimization' );
}
}
public function verify_nonce_and_capability( $nonce = '', $name = '', $capability = 'manage_options' ) {
$this->verify_nonce( $nonce, $name );
if ( ! current_user_can( $capability ) ) {
wp_die( 'You do not have sufficient permissions to access this page.' );
}
}
}
@@ -0,0 +1,61 @@
<?php
namespace ImageOptimization\Modules\Oauth\Components;
use ImageOptimization\Classes\Utils;
use ImageOptimization\Modules\Oauth\Classes\Data;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
/**
* Class Checkpoint
*/
class Checkpoint {
const ON_CONNECT = 'image-optimizer-connect';
const ON_DISCONNECT = 'image-optimizer-disconnect';
const ON_ACTIVATE = 'image-optimizer-activate';
const ON_DEACTIVATE = 'image-optimizer-deactivate';
/**
* event
*
* @param array $event_data
*/
public static function event( array $event_data = [] ): void {
$event_name = current_action();
// only allow specific events
if ( ! in_array( $event_name, self::get_checkpoints() ) ) {
return;
}
$response = Utils::get_api_client()->make_request(
'POST',
'status/checkpoint',
[
'event_name' => $event_name,
'event_data' => $event_data,
]
);
}
/**
* get_checkpoints
* @return string[]
*/
public static function get_checkpoints(): array {
return [
self::ON_DISCONNECT,
self::ON_CONNECT,
self::ON_ACTIVATE,
self::ON_DEACTIVATE,
];
}
public function __construct() {
foreach ( self::get_checkpoints() as $checkpoint ) {
add_action( $checkpoint, [ __CLASS__, 'event' ], 10, 0 );
}
}
}
@@ -0,0 +1,64 @@
<?php
namespace ImageOptimization\Modules\Oauth\Components;
use ImageOptimization\Modules\Core\Components\Pointers;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Connect_Pointer {
const CURRENT_POINTER_SLUG = 'image-optimization-auth-connect';
public function admin_print_script() {
if ( Connect::is_connected() ) {
return;
}
wp_enqueue_script( 'wp-pointer' );
wp_enqueue_style( 'wp-pointer' );
$pointer_content = '<h3>' . esc_html__( 'Start by connecting your license', 'image-optimization' ) . '</h3>';
$pointer_content .= '<p>' . esc_html__( 'You’re one click away from improving your site’s performance dramatically!', 'image-optimization' ) . '</p>';
?>
<script>
jQuery( document ).ready( function( $ ) {
console.log( $( '.image-optimization-stats-connect-button' ) );
const intervalId = setInterval( () => {
if ( ! $( '.image-optimization-stats-connect-button' ).length ) {
return;
}
clearInterval(intervalId);
$( '.image-optimization-stats-connect-button' ).first().pointer( {
content: '<?php echo $pointer_content; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>',
pointerClass: 'image-optimization-auth-connect-pointer',
position: {
edge: 'top',
align: <?php echo is_rtl() ? "'left'" : "'right'"; ?>,
},
} ).pointer( 'open' );
}, 100 );
} );
</script>
<style>
.image-optimization-auth-connect-pointer .wp-pointer-arrow {
inset-block-start: 4px;
inset-inline-start: 78%;
}
.image-optimization-auth-connect-pointer .wp-pointer-arrow-inner {
inset-block-start: 10px;
}
</style>
<?php
}
public function __construct() {
add_action( 'in_admin_header', [ $this, 'admin_print_script' ] );
}
}
@@ -0,0 +1,380 @@
<?php
namespace ImageOptimization\Modules\Oauth\Components;
use ImageOptimization\Modules\Oauth\{
Classes\Route_Base,
Components\Exceptions\Auth_Error,
Classes\Data,
};
use ImageOptimization\Classes\Logger;
use ImageOptimization\Classes\Utils;
use ImageOptimization\Modules\Settings\Module as Settings_Module;
use stdClass;
use Throwable;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
/**
* Class Connect
*/
class Connect {
const API_URL = 'https://my.elementor.com/api/connect/v1';
const STATUS_CHECK_TRANSIENT = 'image_optimizer_status_check';
/**
* is_connected
* @return bool
*/
public static function is_connected(): bool {
return ! empty( Data::get_connect_data()['access_token'] );
}
/**
* is_activated
* @return bool
*/
public static function is_activated(): bool {
return ! empty( Data::get_activation_state() );
}
/**
* maybe_handle_admin_connect_page
* @return bool
*/
public static function maybe_handle_admin_connect_page(): bool {
if ( ! isset( $_GET['nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['nonce'] ) ), 'nonce_actionget_token' ) ) {
return false;
}
$args = [
'page' => 'elementor-connect',
'app' => 'library',
'action' => 'get_token',
'state' => Data::get_connect_state(),
];
foreach ( $args as $key => $value ) {
if ( ! isset( $_GET[ $key ] ) || $_GET[ $key ] !== $value ) {
return false;
}
}
if ( ! isset( $_GET['nonce'] ) || ! isset( $_GET['code'] ) ) {
return false;
}
return true;
}
/**
* handle_elementor_connect_admin
*/
public function handle_elementor_connect_admin(): void {
// validate args
if ( ! self::maybe_handle_admin_connect_page() ) {
return;
}
// validate nonce
if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['nonce'] ) ), 'nonce_actionget_token' ) ) {
wp_die( 'Nonce verification failed', 'image-optimization' );
}
$token_response = wp_remote_request( self::API_URL . '/get_token', [
'method' => 'POST',
'body' => [
'app' => 'library',
'grant_type' => 'authorization_code',
'client_id' => Data::get_client_id(),
'code' => sanitize_text_field( $_GET['code'] ),
],
] );
if ( is_wp_error( $token_response ) ) {
wp_die( $token_response->get_error_message(), 'image-optimization' );
}
$data = json_decode( wp_remote_retrieve_body( $token_response ), true );
Data::set_connect_data( $data );
do_action( Checkpoint::ON_CONNECT );
// cleanup
Data::delete_connect_state();
wp_redirect( add_query_arg( [
'page' => Settings_Module::SETTING_BASE_SLUG,
'connected' => 'true',
], admin_url( 'admin.php' ) ) );
die();
}
/**
* Gets required connection data from the service and generates a connect link.
*
* @return string User connect link
*
* @throws Auth_Error
*/
public static function initialize_connect(): string {
try {
$response = wp_remote_request(
self::API_URL . '/library/get_client_id',
[
'method' => 'POST',
'body' => [
'local_id' => get_current_user_id(),
'site_key' => Data::get_site_key(),
'app' => 'library',
'home_url' => trailingslashit( home_url() ),
'source' => 'image-optimizer',
],
]
);
} catch ( Throwable $t ) {
Logger::log(
Logger::LEVEL_ERROR,
'Error while sending connection initialization request: ' . $t->getMessage()
);
throw new Auth_Error( $t->getMessage() );
}
$data = json_decode( wp_remote_retrieve_body( $response ) );
if ( ! isset( $data->client_id ) || ! isset( $data->auth_secret ) ) {
Logger::log(
Logger::LEVEL_ERROR,
'Invalid response from server: client id or auth secret are undefined'
);
throw new Auth_Error( esc_html__( 'Invalid response from server', 'image-optimization' ) );
}
Data::set_client_data( $data->client_id, $data->auth_secret );
return add_query_arg( [
'utm_source' => 'image-optimizer-panel',
'utm_campaign' => 'image-optimizer',
'utm_medium' => 'wp-dash',
'source' => 'generic',
'action' => 'authorize',
'response_type' => 'code',
'client_id' => $data->client_id,
'auth_secret' => $data->auth_secret,
'state' => Data::get_connect_state( true ),
'redirect_uri' => rawurlencode( add_query_arg( [
'page' => 'elementor-connect',
'app' => 'library',
'action' => 'get_token',
'nonce' => wp_create_nonce( 'nonce_action' . 'get_token' ),
], admin_url( 'admin.php' ) ) ),
'may_share_data' => 0,
'reconnect_nonce' => wp_create_nonce( 'nonce_action' . 'reconnect' ),
], Route_Base::SITE_URL . 'library' );
}
/**
* Disconnects a user and removes connection data from the DB.
*/
public static function disconnect() {
do_action( Checkpoint::ON_DISCONNECT );
try {
return wp_remote_request( self::API_URL . '/disconnect', [
'method' => 'POST',
'body' => [
'app' => 'library',
'home_url' => trailingslashit( home_url() ),
'client_id' => Data::get_client_id(),
'access_token' => Data::get_access_token(),
],
] );
} catch ( Throwable $t ) {
Logger::log( Logger::LEVEL_ERROR, 'Error while sending disconnection request: ' . $t->getMessage() );
throw new Auth_Error( $t->getMessage() );
} finally {
Data::reset();
}
}
/**
* Sends an activation request and stores activation data in the DB.
*
* @param $license_key string License key to activate with.
* @return mixed
*
* @throws Auth_Error
*/
public static function activate( string $license_key ) {
try {
$response = Utils::get_api_client()->make_request(
'POST',
'activation/activate',
[],
[ 'key' => $license_key ]
);
} catch ( Throwable $t ) {
Logger::log( Logger::LEVEL_ERROR, 'Error while sending activation request: ' . $t->getMessage() );
throw new Auth_Error( $t->getMessage() );
}
if ( ! isset( $response->id ) ) {
Logger::log( Logger::LEVEL_ERROR, 'Invalid response from server' );
throw new Auth_Error( esc_html__( 'Invalid response from server', 'image-optimization' ) );
}
Data::set_activation_state( $license_key );
do_action( Checkpoint::ON_ACTIVATE, $license_key );
return $response;
}
/**
* Deactivate specific license and remove activation data from the DB.
*
* @param $license_key string License key to deactivate.
*
* @return mixed
*
* @throws Auth_Error
*/
public static function deactivate( string $license_key ) {
do_action( Checkpoint::ON_DEACTIVATE, $license_key );
try {
$response = Utils::get_api_client()->make_request(
'POST',
'activation/deactivate',
[
'key' => $license_key,
]
);
} catch ( Throwable $t ) {
Logger::log( Logger::LEVEL_ERROR, 'Error while sending deactivation request: ' . $t->getMessage() );
throw new Auth_Error( $t->getMessage() );
} finally {
Data::delete_activation_state();
}
if ( ! isset( $response->id ) ) {
Logger::log( Logger::LEVEL_ERROR, 'Invalid response from server' );
throw new Auth_Error( esc_html__( 'Invalid response from server', 'image-optimization' ) );
}
return $response;
}
/**
* Fetches and returns a list of available licenses for a specific user.
*
* @return array Available subscriptions or an empty array
*
* @throws Auth_Error
*/
public static function get_subscriptions(): array {
try {
$response = Utils::get_api_client()->make_request(
'POST',
'activation/get-subscriptions'
);
} catch ( Throwable $t ) {
Logger::log( Logger::LEVEL_ERROR, 'Error while fetching subscriptions: ' . $t->getMessage() );
throw new Auth_Error( $t->getMessage() );
}
if ( ! isset( $response->subscriptions ) ) {
Logger::log( Logger::LEVEL_ERROR, 'Invalid response from server' );
throw new Auth_Error( esc_html__( 'Invalid response from server', 'image-optimization' ) );
}
return $response->subscriptions;
}
public static function get_connect_status() {
if ( ! self::is_connected() ) {
Logger::log( Logger::LEVEL_INFO, 'Status getting error. Reason: User is not connected' );
return null;
}
$cached_status = get_transient( self::STATUS_CHECK_TRANSIENT );
if ( $cached_status ) {
return $cached_status;
}
$status = self::check_connect_status();
set_transient( self::STATUS_CHECK_TRANSIENT, $status, MINUTE_IN_SECONDS * 5 );
return $status;
}
private static function check_connect_status() {
if ( ! self::is_connected() ) {
Logger::log( Logger::LEVEL_INFO, 'Status check error. Reason: User is not connected' );
return null;
}
try {
$response = Utils::get_api_client()->make_request(
'POST',
'status/check'
);
} catch ( Throwable $t ) {
Logger::log(
Logger::LEVEL_ERROR,
'Status check error. Reason: ' . $t->getMessage()
);
return null;
}
if ( ! isset( $response->status ) ) {
Logger::log( Logger::LEVEL_ERROR, 'Invalid response from server' );
return null;
}
return $response;
}
public static function update_usage_data( stdClass $new_usage_data ) {
$connect_status = self::get_connect_status();
if ( ! isset( $new_usage_data->allowed ) || ! isset( $new_usage_data->used ) ) {
return;
}
if ( 0 === $new_usage_data->allowed - $new_usage_data->used ) {
$connect_status->status = 'expired';
}
$connect_status->quota = $new_usage_data->allowed;
$connect_status->used_quota = $new_usage_data->used;
set_transient( self::STATUS_CHECK_TRANSIENT, $connect_status, MINUTE_IN_SECONDS * 5 );
}
public function __construct() {
// handle connect if elementor is active
add_action( 'load-elementor_page_elementor-connect', [ $this, 'handle_elementor_connect_admin' ], 9 );
// handle connect if elementor is not active
add_action( '_admin_menu', [ $this, 'handle_elementor_connect_admin' ] );
}
}
@@ -0,0 +1,13 @@
<?php
namespace ImageOptimization\Modules\Oauth\Components\Exceptions;
use Exception;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Auth_Error extends Exception {
protected $message = 'Auth error';
}
@@ -0,0 +1,53 @@
<?php
namespace ImageOptimization\Modules\Oauth;
use ImageOptimization\Classes\Module_Base;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
/**
* Class Module
*/
class Module extends Module_Base {
/**
* Get module name.
* Retrieve the module name.
* @access public
* @return string Module name.
*/
public function get_name(): string {
return 'oauth';
}
public static function routes_list() : array {
return [
'Connect_Init',
'Disconnect',
'Get_Subscriptions',
'Activate',
'Deactivate',
'Version',
];
}
public static function component_list() : array {
return [
'Connect',
'Checkpoint',
'Connect_Pointer',
];
}
public static function is_active() : bool {
return ! empty( get_option( 'image_optimizer_client_data' ) );
}
public function __construct() {
$this->register_components();
$this->register_routes();
}
}
@@ -0,0 +1,70 @@
<?php
namespace ImageOptimization\Modules\Oauth\Rest;
use ImageOptimization\Modules\Oauth\{
Classes\Route_Base,
Components\Connect,
};
use Throwable;
use WP_REST_Request;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Activate extends Route_Base {
const NONCE_NAME = 'image-optimization-activate-subscription';
protected string $path = 'activate';
public function get_name(): string {
return 'activate';
}
public function get_methods(): array {
return [ 'POST' ];
}
public function POST( WP_REST_Request $request ) {
$this->verify_nonce_and_capability(
$request->get_param( self::NONCE_NAME ),
self::NONCE_NAME
);
if ( ! Connect::is_connected() ) {
return $this->respond_error_json( [
'message' => esc_html__( 'Please connect first', 'image-optimization' ),
'code' => 'forbidden',
] );
}
if ( Connect::is_activated() ) {
return $this->respond_error_json( [
'message' => esc_html__( 'Already activated', 'image-optimization' ),
'code' => 'bad_request',
] );
}
if ( ! $request->get_param( 'license_key' ) ) {
return $this->respond_error_json( [
'message' => esc_html__( 'Missing license key', 'image-optimization' ),
'code' => 'bad_request',
] );
}
$license_key = $request->get_param( 'license_key' );
try {
Connect::activate( $license_key );
return $this->respond_success_json();
} catch ( Throwable $t ) {
return $this->respond_error_json( [
'message' => $t->getMessage(),
'code' => 'internal_server_error',
] );
}
}
}
@@ -0,0 +1,54 @@
<?php
namespace ImageOptimization\Modules\Oauth\Rest;
use ImageOptimization\Modules\Oauth\{
Classes\Route_Base,
Components\Connect,
};
use Throwable;
use WP_REST_Request;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Connect_Init extends Route_Base {
const NONCE_NAME = 'image-optimization-connect';
protected string $path = 'init';
public function get_name(): string {
return 'connect-init';
}
public function get_methods(): array {
return [ 'GET' ];
}
public function get( WP_REST_Request $request ) {
$this->verify_nonce_and_capability(
$request->get_param( self::NONCE_NAME ),
self::NONCE_NAME
);
if ( Connect::is_connected() ) {
return $this->respond_error_json( [
'message' => esc_html__( 'You are already connected', 'image-optimization' ),
'code' => 'forbidden',
] );
}
try {
$connect_url = Connect::initialize_connect();
return $this->respond_success_json( $connect_url );
} catch ( Throwable $t ) {
return $this->respond_error_json( [
'message' => $t->getMessage(),
'code' => 'internal_server_error',
] );
}
}
}
@@ -0,0 +1,64 @@
<?php
namespace ImageOptimization\Modules\Oauth\Rest;
use ImageOptimization\Modules\Oauth\{
Classes\Data,
Classes\Route_Base,
Components\Connect
};
use Throwable;
use WP_REST_Request;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Deactivate extends Route_Base {
const NONCE_NAME = 'image-optimization-deactivate-subscription';
protected string $path = 'deactivate';
public function get_name(): string {
return 'deactivate';
}
public function get_methods(): array {
return [ 'POST' ];
}
public function POST( WP_REST_Request $request ) {
$this->verify_nonce_and_capability(
$request->get_param( self::NONCE_NAME ),
self::NONCE_NAME
);
if ( ! Connect::is_connected() ) {
return $this->respond_error_json( [
'message' => esc_html__( 'Please connect first', 'image-optimization' ),
'code' => 'forbidden',
] );
}
if ( ! $request->get_param( 'license_key' ) ) {
return $this->respond_error_json( [
'message' => esc_html__( 'Missing license key', 'image-optimization' ),
'code' => 'bad_request',
] );
}
$license_key = $request->get_param( 'license_key' );
try {
Connect::deactivate( $license_key );
return $this->respond_success_json();
} catch ( Throwable $t ) {
Data::delete_activation_state();
return $this->respond_error_json( [
'message' => $t->getMessage(),
'code' => 'internal_server_error',
] );
}
}
}
@@ -0,0 +1,48 @@
<?php
namespace ImageOptimization\Modules\Oauth\Rest;
use ImageOptimization\Modules\Oauth\{
Classes\Data,
Classes\Route_Base,
Components\Connect
};
use Throwable;
use WP_REST_Request;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Disconnect extends Route_Base {
const NONCE_NAME = 'image-optimization-disconnect';
protected string $path = 'disconnect';
public function get_name(): string {
return 'disconnect';
}
public function get_methods(): array {
return [ 'POST' ];
}
public function POST( WP_REST_Request $request ) {
$this->verify_nonce_and_capability(
$request->get_param( self::NONCE_NAME ),
self::NONCE_NAME
);
try {
Connect::disconnect();
} catch ( Throwable $t ) {
Data::reset();
return $this->respond_error_json( [
'message' => $t->getMessage(),
'code' => 'internal_server_error',
] );
}
return $this->respond_success_json();
}
}
@@ -0,0 +1,54 @@
<?php
namespace ImageOptimization\Modules\Oauth\Rest;
use ImageOptimization\Modules\Oauth\{
Classes\Route_Base,
Components\Connect,
};
use Throwable;
use WP_REST_Request;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Get_Subscriptions extends Route_Base {
const NONCE_NAME = 'image-optimization-get-subscription';
protected string $path = 'get-subscriptions';
public function get_name(): string {
return 'get_subscriptions';
}
public function get_methods(): array {
return [ 'POST' ];
}
public function POST( WP_REST_Request $request ) {
$this->verify_nonce_and_capability(
$request->get_param( self::NONCE_NAME ),
self::NONCE_NAME
);
if ( ! Connect::is_connected() ) {
return $this->respond_error_json( [
'message' => esc_html__( 'Please connect first', 'image-optimization' ),
'code' => 'forbidden',
] );
}
try {
$subscriptions = Connect::get_subscriptions();
return $this->respond_success_json( $subscriptions );
} catch ( Throwable $t ) {
return $this->respond_error_json( [
'message' => $t->getMessage(),
'code' => 'internal_server_error',
] );
}
}
}
@@ -0,0 +1,35 @@
<?php
namespace ImageOptimization\Modules\Oauth\Rest;
use ImageOptimization\Modules\Oauth\{
Classes\Route_Base,
Components\Connect,
};
use Throwable;
use WP_REST_Request;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Version extends Route_Base {
const NONCE_NAME = 'image-optimization-version';
protected string $path = 'version';
public function get_name(): string {
return 'version';
}
public function get_methods(): array {
return [ 'GET' ];
}
public function GET( WP_REST_Request $request ) {
return $this->respond_success_json( [
'version' => 1,
] );
}
}
@@ -0,0 +1,173 @@
.image-optimization-control--list-view {
display: flex;
justify-content: flex-start;
align-items: center;
min-height: 105px;
}
.image-optimization-control--list-view.image-optimization-control--error {
justify-content: center;
align-items: flex-start;
}
.image-optimization-control__buttons-wrapper {
margin-block-start: 8px;
}
.image-optimization-control__buttons-wrapper :not(:last-of-type) {
margin-inline-end: 8px;
}
.image-optimization-control--error {
flex-direction: column;
}
.image-optimization-control--optimized {
flex-direction: column;
align-items: flex-start;
}
.image-optimization-control__error-message {
display: flex;
flex-direction: column;
font-family: Roboto, sans-serif;
font-size: 14px;
font-weight: 600;
line-height: 24px;
margin-block-end: 15px;
max-width: 200px;
}
.image-optimization-control__error-title {
margin-block-end: 10px;
}
.image-optimization-control__error-subtitle {
color: #DC2626;
}
.image-optimization-control__button {
font-family: Roboto, sans-serif;
font-weight: 500;
border-radius: 4px;
}
.image-optimization-control__button--optimize > .spinner {
float: left;
padding: 0;
margin-block-start: 4px;
margin-left: 0;
margin-block-end: 0;
}
.image-optimization-control--error .image-optimization-control__button--try-again,
.image-optimization-control--error .image-optimization-control__button--try-again:hover {
color: #DC2626;
border-color: #DC2626;
background: transparent;
}
.image-optimization-control__property {
margin: 9px 0;
}
.image-optimization-control__details-button {
background-color: transparent;
border: none;
cursor: pointer;
color: #135e96;
padding: 0;
}
.image-optimization-control__property:last-of-type {
margin-block-end: 0;
}
.image-optimization-control .image-optimization-setting {
margin-block-end: 0;
}
.image-optimization-control__property-value,
.image-optimization-control--details-view .setting .image-optimization-control__property-value {
font-weight: 600;
font-size: 13px;
line-height: 24px;
}
.image-optimization-control--details-view .setting .image-optimization-control__property-value {
margin: 6px 3px 3px;
text-align: start;
}
.image-optimization-control--details-view .setting .image-optimization-control__property-value--button {
margin: 0 0 0 3px;
font-weight: initial;
}
.image-optimization-control--details-view .setting .image-optimization-control__property-value--spinner {
display: flex;
justify-content: center;
margin: 25px 0 0 3px;
}
.image-optimization-control__action-button-wrapper {
margin-block-start: 37px;
display: flex;
width: 100%;
justify-content: flex-end;
}
.image-optimization-control__action-button-wrapper .button-link:not(:last-of-type) {
margin-inline-end: 24px;
}
.image-optimization-control__action-button-wrapper .button-link,
.image-optimization-control--details-view .image-optimization-control__property-value--button .button-link {
text-decoration: none;
}
.image-optimization-control__action-button-wrapper .button-link:hover,
.image-optimization-control--details-view .image-optimization-control__property-value--button .button-link:hover {
background-color: transparent;
}
.image-optimization-control__action-spinner-wrapper {
height: 75px;
display: flex;
justify-content: center;
align-items: center;
}
div .components-tooltip {
max-width: 165px;
text-align: start;
}
/* Optimization details */
#image-optimization-optimization-details-modal {
padding: 20px 0;
}
.image-optimization-details-table__property {
display: block;
}
.image-optimization-details-table__error {
color: #DC2626;
font-size: 18px;
font-weight: 700;
}
.image-optimization-details-table__property--error {
color: #DC2626;
}
body.dialog-container #TB_overlay {
z-index: 999999;
}
body.dialog-container #TB_window {
z-index: 9999999;
}
@@ -0,0 +1,72 @@
import apiFetch from '@wordpress/api-fetch';
import { addQueryArgs } from '@wordpress/url';
import APIError from './exceptions/APIError';
const v1Prefix = '/image-optimizer/v1';
class API {
static async request( { path, data, method = 'POST' } ) {
try {
const response = await apiFetch( {
path,
method,
data,
} );
if ( ! response.success ) {
throw new APIError( response.data.message );
}
return response.data;
} catch ( e ) {
if ( e instanceof APIError ) {
throw e;
} else {
throw new APIError( e.message );
}
}
}
static async optimizeSingleImage( { imageId, reoptimize = false } ) {
return API.request( {
path: `${ v1Prefix }/optimize/image`,
data: {
imageId,
reoptimize,
'image-optimization-optimize-image': window?.imageOptimizerControlSettings?.optimizeSingleImageNonce,
},
} );
}
static async restoreSingleImage( imageId ) {
return API.request( {
path: `${ v1Prefix }/backups/restore/${ imageId }`,
data: {
'image-optimization-restore-single': window?.imageOptimizerControlSettings?.restoreSingleImageNonce,
},
} );
}
static async getOptimizationStatus( imageIds ) {
return API.request( {
path: `${ v1Prefix }/optimize/status`,
data: {
image_ids: imageIds,
},
} );
}
static async getOptimizationDetails( imageId ) {
const queryParams = {
image_id: imageId,
};
return API.request( {
method: 'GET',
path: addQueryArgs( `${ v1Prefix }/stats/optimization-details`, queryParams ),
} );
}
}
export default API;
@@ -0,0 +1,39 @@
class ControlMeta {
constructor( controlNode ) {
this.controlNode = controlNode;
}
getImageId() {
return this.controlNode.dataset?.imageOptimizationImageId
? parseInt( this.controlNode.dataset?.imageOptimizationImageId, 10 )
: null;
}
getAction() {
return this.controlNode.dataset?.imageOptimizationAction || null;
}
getContext() {
return this.controlNode.dataset?.imageOptimizationContext || null;
}
getStatus() {
return this.controlNode.dataset?.imageOptimizationStatus || null;
}
canBeRestored() {
const value = this.controlNode.dataset?.imageOptimizationCanBeRestored;
if ( ! value ) {
return null;
}
return '1' === value;
}
allowRetry() {
return this.controlNode.dataset?.allowRetry || null;
}
}
export default ControlMeta;
@@ -0,0 +1,72 @@
import listViewTemplates from '../../templates/list-view';
import metaBoxTemplates from '../../templates/meta-box';
import detailsViewTemplates from '../../templates/details-view';
import { SELECTORS } from '../../constants';
import ControlMeta from './control-meta';
class ControlStates {
constructor( controlWrapper ) {
this.controlWrapper = controlWrapper;
this.context = new ControlMeta( controlWrapper ).getContext();
this.action = new ControlMeta( controlWrapper ).getAction();
this.canBeRestored = new ControlMeta( controlWrapper ).canBeRestored();
this.templates = {
'list-view': listViewTemplates,
'meta-box': metaBoxTemplates,
'details-view': detailsViewTemplates,
};
}
renderNotOptimized( data ) {
this.controlWrapper.className = this.mixControlContextClass( SELECTORS.controlNotOptimizedClassName );
this.controlWrapper.innerHTML = this.getTemplates().notOptimizedTemplate( data );
this.controlWrapper.dataset.imageOptimizationStatus = 'not-optimized';
}
renderOptimized( data ) {
const canBeRestored = this.canBeRestored && data?.saved?.absolute !== 0;
this.controlWrapper.className = this.mixControlContextClass( SELECTORS.controlOptimizedClassName );
this.controlWrapper.innerHTML = this.getTemplates().optimizedTemplate( { ...data, canBeRestored } );
this.controlWrapper.dataset.imageOptimizationStatus = 'optimized';
}
renderError( { message, imagesLeft, action } ) {
this.controlWrapper.className = this.mixControlContextClass( SELECTORS.controlErrorClassName );
this.controlWrapper.innerHTML = this.getTemplates().errorTemplate( message, imagesLeft );
this.controlWrapper.dataset.imageOptimizationAction = action;
this.controlWrapper.dataset.imageOptimizationStatus = 'error';
}
renderLoading( action ) {
this.controlWrapper.className = this.mixControlContextClass( SELECTORS.controlLoadingClassName );
this.controlWrapper.innerHTML = this.getTemplates().loadingTemplate( action );
this.controlWrapper.dataset.imageOptimizationStatus = 'loading';
}
getTemplates() {
const template = this.templates[ this.context ];
if ( ! template ) {
throw new Error( `No templates found for the context ${ this.context }` );
}
return template;
}
mixControlContextClass( className ) {
const contextClassName = SELECTORS.controlWrapper[ this.context ];
if ( ! contextClassName ) {
throw new Error( `No context className found for the context ${ this.context }` );
}
return `${ className } ${ contextClassName }`;
}
}
export default ControlStates;
@@ -0,0 +1,107 @@
import API from '../api';
import { SELECTORS } from '../../constants';
import ControlMeta from './control-meta';
import ControlStates from './control-states';
class ControlSync {
async run() {
const controls = document.querySelectorAll( SELECTORS.controlWrapperSelector );
if ( ! controls.length ) {
return;
}
const imageIds = this.mapImageIds( controls );
const { status } = await API.getOptimizationStatus( imageIds );
controls.forEach( ( controlWrapper ) => {
const imageId = new ControlMeta( controlWrapper ).getImageId();
const currentStatus = new ControlMeta( controlWrapper ).getStatus();
const imageData = status[ imageId ];
const controlStates = new ControlStates( controlWrapper );
if ( currentStatus === imageData.status ) {
return;
}
if ( controlWrapper.dataset.isFrozen === 'true' ) {
controlWrapper.dataset.isFrozen = false;
return;
}
if ( currentStatus === 'error' && ! new ControlMeta( controlWrapper ).allowRetry() ) {
return;
}
switch ( imageData.status ) {
case 'optimization-in-progress':
controlStates.renderLoading( 'optimize' );
break;
case 'reoptimizing-in-progress':
controlStates.renderLoading( 'reoptimize' );
break;
case 'restoring-in-progress':
controlStates.renderLoading( 'restore' );
break;
case 'not-optimized':
controlStates.renderNotOptimized();
break;
case 'optimized':
const statsData = {
sizesTotal: imageData.stats.total_image_count,
sizesOptimized: imageData.stats.optimized_image_count,
saved: {
absolute: imageData.stats.initial_image_size - imageData.stats.current_image_size,
relative: Math.max( 100 - Math.round( imageData.stats.current_image_size / imageData.stats.initial_image_size * 100 ), 0 ),
},
};
controlStates.renderOptimized( statsData );
break;
case 'optimization-failed':
controlStates.renderError( {
message: imageData.message,
imagesLeft: imageData.images_left,
action: 'optimize',
} );
break;
case 'reoptimizing-failed':
controlStates.renderError( {
message: imageData.message,
imagesLeft: imageData.images_left,
action: 'reoptimize',
} );
break;
case 'restoring-failed':
controlStates.renderError( {
message: imageData.message,
action: 'restore',
} );
}
} );
}
mapImageIds( nodes ) {
return Array.prototype.map.call(
nodes,
( node ) => new ControlMeta( node ).getImageId(),
);
}
}
export default ControlSync;
@@ -0,0 +1,9 @@
class APIError extends Error {
constructor( message ) {
super( message );
this.name = 'APIError';
}
}
export default APIError;
@@ -0,0 +1,85 @@
class ExtendViews {
constructor() {
this.init();
}
init() {
this.extendAttachmentDetails();
this.extendAttachmentDetailsTwoColumn();
}
// #tmpl-attachment-details
extendAttachmentDetails() {
if ( ! wp?.media?.view?.Attachment?.Details ) {
return;
}
wp.media.view.Attachment.Details = wp.media.view.Attachment.Details.extend( {
template( view ) {
const html = wp.media.template( 'attachment-details' )( view );
if ( this.model.attributes.type !== 'image' ) {
return html;
}
const content = document.createElement( 'div' );
content.innerHTML = html;
const optimizationControl = this.getOptimizationControlHTML( view.compat.item );
if ( ! optimizationControl ) {
return content.innerHTML;
}
content.innerHTML += optimizationControl;
return content.innerHTML;
},
getOptimizationControlHTML( compatData ) {
const tempElement = document.createElement( 'div' );
tempElement.innerHTML = compatData;
return tempElement.querySelector( 'input[name*="[image_optimization_modal]"]' )?.value;
},
} );
}
// #tmpl-attachment-details-two-column
extendAttachmentDetailsTwoColumn() {
if ( ! wp?.media?.view?.Attachment?.Details?.TwoColumn ) {
return;
}
wp.media.view.Attachment.Details.TwoColumn = wp.media.view.Attachment.Details.TwoColumn.extend( {
template( view ) {
const html = wp.media.template( 'attachment-details-two-column' )( view );
if ( this.model.attributes.type !== 'image' ) {
return html;
}
const content = document.createElement( 'div' );
content.innerHTML = html;
const optimizationControl = this.getOptimizationControlHTML( view.compat.item );
if ( ! optimizationControl ) {
return content.innerHTML;
}
const settings = content.querySelector( '.settings' );
settings.innerHTML += optimizationControl;
return content.innerHTML;
},
getOptimizationControlHTML( compatData ) {
const tempElement = document.createElement( 'div' );
tempElement.innerHTML = compatData;
return tempElement.querySelector( 'input[name*="[image_optimization_modal]"]' )?.value;
},
} );
}
}
export default ExtendViews;
@@ -0,0 +1,149 @@
/* global tb_show, tb_click, tb_remove */
import { __ } from '@wordpress/i18n';
import { SELECTORS } from '../constants';
import optimizationDetailsTemplate from '../templates/optimization-details';
const MODAL_WIDTH = 700;
const MODAL_HEIGHT = 500;
/**
* @class
*
* Contains methods to work with the optimization details modal.
* It fully relies on the ThickBox API.
*/
class OptimizationDetails {
/**
* Checks if the modal template element exists on the page.
* @return {boolean} Returns true if the modal template element exists, returns false otherwise.
*/
static modalTemplateExists() {
return !! document.getElementById( SELECTORS.optimizationDetailsModalId );
}
/**
* Creates a basic template element to render inside a modal.
*/
static initModal() {
if ( OptimizationDetails.modalTemplateExists() ) {
return;
}
const container = document.createElement( 'div' );
container.id = SELECTORS.optimizationDetailsWrapperId;
container.style = 'display:none;';
container.innerHTML = `<div id="${ SELECTORS.optimizationDetailsModalId }"></div>`;
document.body.appendChild( container );
}
/**
* Opens a modal and removes the template element.
*/
static openModal() {
tb_show(
__( 'Optimization Details', 'image-optimization' ),
`#TB_inline?width=${ MODAL_WIDTH }&height=${ MODAL_HEIGHT }&inlineId=${ SELECTORS.optimizationDetailsWrapperId }`,
);
document
.getElementById( SELECTORS.optimizationDetailsWrapperId )
?.remove();
}
/**
* Closes the modal.
*/
static closeModal() {
tb_remove();
}
/**
* Renders an error message.
*
* @param {string} message
*
* @return {boolean} Returns false if modal container not found.
*/
static renderError( message ) {
const container = document.getElementById( SELECTORS.optimizationDetailsModalId );
if ( ! container ) {
return false;
}
const { error } = optimizationDetailsTemplate;
container.innerHTML = error( { message } );
return true;
}
/**
* Shows a default ThickBox loader.
*/
static renderLoading() {
tb_click();
}
/**
* Renders the modal content.
*
* @param {number} imageId
* @param {{total: number, sizes: Array}} optimizationDetails
*
* @return {boolean} Returns false if modal container not found.
*/
static renderData( imageId, optimizationDetails ) {
const container = document.getElementById( SELECTORS.optimizationDetailsModalId );
if ( ! container ) {
return false;
}
const {
header,
footer,
rowStart,
rowEnd,
optimizedChunk,
notOptimizedChunk,
notFoundChunk,
tooLargeChunk,
} = optimizationDetailsTemplate;
let html = header();
optimizationDetails?.sizes.forEach( ( size ) => {
html += rowStart( size );
if ( 'optimized' === size.status ) {
html += optimizedChunk( size );
}
if ( 'not-optimized' === size.status ) {
html += notOptimizedChunk( { imageId } );
}
if ( 'file-not-found' === size.status ) {
html += notFoundChunk();
}
if ( 'file-too-large' === size.status ) {
html += tooLargeChunk();
}
html += rowEnd();
} );
html += footer( optimizationDetails );
container.innerHTML = html;
return true;
}
}
export default OptimizationDetails;
@@ -0,0 +1,25 @@
export const UPGRADE_LINK = 'https://go.elementor.com/io-panel-upgrade/';
export const SELECTORS = Object.freeze( {
optimizeButtonSelector: '.image-optimization-control__button--optimize',
reoptimizeButtonSelector: '.image-optimization-control__button--reoptimize',
tryAgainOptimizeButtonSelector: '[data-image-optimization-action="optimize"] .image-optimization-control__button--try-again',
tryAgainReoptimizeButtonSelector: '[data-image-optimization-action="reoptimize"] .image-optimization-control__button--try-again',
tryAgainRestoreButtonSelector: '[data-image-optimization-action="restore"] .image-optimization-control__button--try-again',
controlWrapperSelector: '.image-optimization-control',
controlNotOptimizedClassName: 'image-optimization-control image-optimization-control--not-optimized',
controlLoadingClassName: 'image-optimization-control image-optimization-control--loading',
controlOptimizedClassName: 'image-optimization-control image-optimization-control--optimized',
controlErrorClassName: 'image-optimization-control image-optimization-control--error',
controlWrapper: {
'list-view': 'image-optimization-control--list-view',
'meta-box': 'image-optimization-control--meta-box',
'details-view': 'image-optimization-control--details-view',
},
restoreButtonSelector: '.image-optimization-control__button--restore-original',
loadingControlsSelector: '[data-image-optimization-status="loading"]',
optimizationDetailsButtonSelector: '.image-optimization-control__details-button',
optimizationDetailsOptimizeButtonSelector: '.image-optimization-details-table__optimization-button',
optimizationDetailsWrapperId: 'image-optimization-optimization-details-wrapper',
optimizationDetailsModalId: 'image-optimization-optimization-details-modal',
} );
@@ -0,0 +1,155 @@
import { __ } from '@wordpress/i18n';
import { speak } from '@wordpress/a11y';
import { SELECTORS } from './constants';
import API from './classes/api';
import ControlSync from './classes/control/control-sync';
import ControlStates from './classes/control/control-states';
import ControlMeta from './classes/control/control-meta';
import OptimizationDetails from './classes/optimization-details';
class OptimizationControl {
constructor() {
this.controlSyncRequestInProgress = false;
this.init();
this.controlSync = new ControlSync();
}
init() {
this.initEventListeners();
setInterval( () => this.runStatusCheckLoop(), 5000 );
}
async runStatusCheckLoop() {
if ( this.controlSyncRequestInProgress ) {
return;
}
this.controlSyncRequestInProgress = true;
await this.controlSync.run();
this.controlSyncRequestInProgress = false;
}
initEventListeners() {
document.addEventListener( 'click', ( e ) => this.handleOptimizeButtonClick( e ) );
document.addEventListener( 'click', ( e ) => this.handleReoptimizeButtonClick( e ) );
document.addEventListener( 'click', ( e ) => this.handleRestoreButtonClick( e ) );
document.addEventListener( 'click', ( e ) => this.handleOptimizationDetailsOpen( e ) );
document.addEventListener( 'click', ( e ) => this.handleOptimizationDetailsClick( e ) );
}
async handleOptimizeButtonClick( e ) {
if ( ! e.target.closest( `${ SELECTORS.optimizeButtonSelector }, ${ SELECTORS.tryAgainOptimizeButtonSelector }` ) ) {
return;
}
speak( __( 'Optimization is in progress', 'image-optimization' ), 'assertive' );
const controlWrapper = e.target.closest( SELECTORS.controlWrapperSelector );
const states = new ControlStates( controlWrapper );
states.renderLoading( 'optimize' );
try {
controlWrapper.dataset.isFrozen = true;
await API.optimizeSingleImage( {
imageId: new ControlMeta( controlWrapper ).getImageId(),
reoptimize: false,
} );
} catch ( error ) {
states.renderError( error );
}
}
async handleReoptimizeButtonClick( e ) {
if ( ! e.target.closest( `${ SELECTORS.reoptimizeButtonSelector }, ${ SELECTORS.tryAgainReoptimizeButtonSelector }` ) ) {
return;
}
speak( __( 'Reoptimizing is in progress', 'image-optimization' ), 'assertive' );
const controlWrapper = e.target.closest( SELECTORS.controlWrapperSelector );
const states = new ControlStates( controlWrapper );
states.renderLoading( 'reoptimize' );
try {
controlWrapper.dataset.isFrozen = true;
await API.optimizeSingleImage( {
imageId: new ControlMeta( controlWrapper ).getImageId(),
reoptimize: true,
} );
} catch ( error ) {
states.renderError( error );
}
}
async handleRestoreButtonClick( e ) {
if ( ! e.target.closest( `${ SELECTORS.restoreButtonSelector }, ${ SELECTORS.tryAgainRestoreButtonSelector }` ) ) {
return;
}
speak( __( 'Image restoring is in progress', 'image-optimization' ), 'assertive' );
const controlWrapper = e.target.closest( SELECTORS.controlWrapperSelector );
const states = new ControlStates( controlWrapper );
states.renderLoading( 'restore' );
try {
controlWrapper.dataset.isFrozen = true;
await API.restoreSingleImage( new ControlMeta( controlWrapper ).getImageId() );
} catch ( error ) {
states.renderError( error );
}
}
async handleOptimizationDetailsOpen( e ) {
if ( ! e.target.closest( `${ SELECTORS.optimizationDetailsButtonSelector }` ) ) {
return;
}
const controlWrapper = e.target.closest( SELECTORS.controlWrapperSelector );
const imageId = new ControlMeta( controlWrapper ).getImageId();
try {
OptimizationDetails.initModal();
OptimizationDetails.renderLoading();
const details = await API.getOptimizationDetails( imageId );
OptimizationDetails.openModal();
OptimizationDetails.renderData( imageId, details );
} catch ( error ) {
OptimizationDetails.openModal();
OptimizationDetails.renderError( error.message );
}
}
async handleOptimizationDetailsClick( e ) {
if ( ! e.target.closest( `${ SELECTORS.optimizationDetailsOptimizeButtonSelector }` ) ) {
return;
}
const imageId = parseInt( e.target.dataset?.imageId, 10 );
await API.optimizeSingleImage( { imageId } );
const controlWrapper = document.querySelector( `.image-optimization-control[data-image-optimization-image-id="${ imageId }"]` );
if ( controlWrapper ) {
new ControlStates( controlWrapper ).renderLoading( 'optimize' );
}
OptimizationDetails.closeModal();
}
}
export default OptimizationControl;
@@ -0,0 +1,16 @@
import '../css/control.css';
import ExtendViews from './classes/extend-views';
import OptimizationControl from './control';
class Module {
constructor() {
this.init();
}
init() {
new ExtendViews();
new OptimizationControl();
}
}
document.addEventListener( 'DOMContentLoaded', () => new Module() );
@@ -0,0 +1,174 @@
import { __ } from '@wordpress/i18n';
import { escapeHTML } from '@wordpress/escape-html';
import { formatFileSize } from '../../utils';
import { UPGRADE_LINK } from '../../constants';
const notOptimizedTemplate = () => {
return `
<span class="setting image-optimization-setting">
<span class="name image-optimization-control__property">
${ __( 'Status', 'image-optimization' ) }:
</span>
<span class="image-optimization-control__property-value">
${ __( 'Not optimized', 'image-optimization' ) }
</span>
</span>
<span class="setting image-optimization-setting">
<span class="name image-optimization-control__property"></span>
<span class="image-optimization-control__property-value image-optimization-control__property-value--button">
<button type="button"
class="button button-primary image-optimization-control__button image-optimization-control__button--optimize">
${ __( 'Optimize now', 'image-optimization' ) }
</button>
</span>
</span>
`;
};
const loadingTemplate = () => {
return `
<span class="setting image-optimization-setting">
<span class="name image-optimization-control__property">
${ __( 'Status', 'image-optimization' ) }:
</span>
<span class="image-optimization-control__property-value">
${ __( 'In Progress', 'image-optimization' ) }
</span>
</span>
<span class="setting image-optimization-setting">
<span class="name image-optimization-control__property"></span>
<span class="image-optimization-control__property-value image-optimization-control__property-value--spinner">
<span class="spinner is-active"></span>
</span>
</span>
`;
};
const errorTemplate = ( message, imagesLeft ) => {
return `
<span class="setting image-optimization-setting">
<span class="name image-optimization-control__property">
${ __( 'Status', 'image-optimization' ) }:
</span>
<span class="image-optimization-control__property-value">
${ __( 'Error', 'image-optimization' ) }
</span>
</span>
<span class="setting image-optimization-setting">
<span class="name image-optimization-control__property">
${ __( 'Reason', 'image-optimization' ) }:
</span>
<span class="image-optimization-control__property-value">
${ escapeHTML( message ) }
</span>
</span>
<span class="setting image-optimization-setting">
<span class="name image-optimization-control__property"></span>
<span class="image-optimization-control__property-value image-optimization-control__property-value--button">
${ imagesLeft === 0
? `<a class="button button-secondary button-large image-optimization-control__button"
href="${ UPGRADE_LINK }"
target="_blank" rel="noopener noreferrer">
${ __( 'Upgrade', 'image-optimization' ) }
</a>
` : `
<button class="button button-secondary button-large button-link-delete image-optimization-control__button image-optimization-control__button--try-again"
type="button">
${ __( 'Try again', 'image-optimization' ) }
</button>` }
</span>
</span>
`;
};
const optimizedTemplate = ( data ) => {
const absoluteValue = formatFileSize( data?.saved?.absolute, 1 );
return `
<span class="setting image-optimization-setting">
<span class="name image-optimization-control__property">
${ __( 'Status', 'image-optimization' ) }:
</span>
<span class="image-optimization-control__property-value">
${ __( 'Optimized', 'image-optimization' ) }
</span>
</span>
<span class="setting image-optimization-setting">
<span class="name image-optimization-control__property">
${ __( 'Image sizes optimized', 'image-optimization' ) }:
</span>
<span class="image-optimization-control__property-value">
${ data?.sizesOptimized }/${ data?.sizesTotal }
<button type="button"
class="image-optimization-control__details-button"
aria-label="${ __( 'Open optimization details', 'image-optimization' ) }">
(+)
</button>
</span>
</span>
<span class="setting image-optimization-setting">
${ data?.saved?.absolute !== 0
? `<span class="name image-optimization-control__property">
${ __( 'Overall saving', 'image-optimization' ) }:
</span>
<span class="image-optimization-control__property-value">
${ data?.saved?.relative }% (${ absoluteValue })
</span>`
: `<span class="name image-optimization-control__property"></span>
<span class="image-optimization-control__property-value">
${ __( 'Image is fully optimized', 'image-optimization' ) }
</span>` }
</span>
<span class="setting image-optimization-setting">
<span class="name image-optimization-control__property"></span>
<span class="image-optimization-control__property-value image-optimization-control__property-value--button">
<button class="button button-link image-optimization-control__button image-optimization-control__button--reoptimize"
type="button">
${ __( 'Reoptimize', 'image-optimization' ) }
</button>
</span>
</span>
${ data?.canBeRestored ? `
<span class="setting image-optimization-setting">
<span class="name image-optimization-control__property"></span>
<span class="image-optimization-control__property-value image-optimization-control__property-value--button">
<button class="button button-link image-optimization-control__button image-optimization-control__button--restore-original"
type="button">
${ __( 'Restore original', 'image-optimization' ) }
</button>
</span>
</span>
` : '' }
`;
};
const detailsViewTemplates = Object.freeze( {
notOptimizedTemplate,
loadingTemplate,
errorTemplate,
optimizedTemplate,
} );
export default detailsViewTemplates;
@@ -0,0 +1,108 @@
import { __ } from '@wordpress/i18n';
import { escapeHTML } from '@wordpress/escape-html';
import { formatFileSize } from '../../utils';
import { UPGRADE_LINK } from '../../constants';
const notOptimizedTemplate = () => {
return `
<button type="button"
class="button button-primary image-optimization-control__button image-optimization-control__button--optimize">
${ __( 'Optimize now', 'image-optimization' ) }
</button>
`;
};
const loadingTemplate = ( action ) => {
let buttonText;
switch ( action ) {
case 'restore':
buttonText = __( 'Restoring…', 'image-optimization' );
break;
case 'optimize':
buttonText = __( 'Optimizing…', 'image-optimization' );
break;
case 'reoptimize':
buttonText = __( 'Reoptimizing…', 'image-optimization' );
break;
default:
buttonText = __( 'Loading…', 'image-optimization' );
}
return `
<button class="button button-secondary image-optimization-control__button image-optimization-control__button--optimize"
disabled="">
<span class="spinner is-active"></span> ${ buttonText }
</button>
`;
};
const errorTemplate = ( message, imagesLeft ) => {
return `
<span class="image-optimization-control__error-message">${ escapeHTML( message ) }</span>
${ imagesLeft === 0
? `<a class="button button-secondary button-large image-optimization-control__button"
href="${ UPGRADE_LINK }"
target="_blank" rel="noopener noreferrer">
${ __( 'Upgrade', 'image-optimization' ) }
</a>
` : `
<button class="button button-secondary button-large button-link-delete image-optimization-control__button image-optimization-control__button--try-again"
type="button">
${ __( 'Try again', 'image-optimization' ) }
</button>` }
`;
};
const optimizedTemplate = ( data ) => {
const absoluteValue = formatFileSize( data?.saved?.absolute, 1 );
return `
<p class="image-optimization-control__property">
${ __( 'Image sizes optimized', 'image-optimization' ) }:
<span>
${ data?.sizesOptimized }/${ data?.sizesTotal }
<button type="button"
class="image-optimization-control__details-button"
aria-label="${ __( 'Open optimization details', 'image-optimization' ) }">
(+)
</button>
</span>
</p>
<p class="image-optimization-control__property">
${ data?.saved?.absolute !== 0
? `${ __( 'Overall saving', 'image-optimization' ) }: <span>${ data?.saved?.relative }% (${ absoluteValue })</span>`
: `<span>${ __( 'Image is fully optimized', 'image-optimization' ) }</span>` }
</p>
<div class="image-optimization-control__buttons-wrapper">
${ data?.canBeRestored ? `
<button type="button"
class="button button-secondary image-optimization-control__button image-optimization-control__button--restore-original">
${ __( 'Restore original', 'image-optimization' ) }
</button>
` : '' }
<button type="button"
class="button button-secondary image-optimization-control__button image-optimization-control__button--reoptimize">
${ __( 'Reoptimize', 'image-optimization' ) }
</button>
</div>
`;
};
const listViewTemplates = Object.freeze( {
notOptimizedTemplate,
loadingTemplate,
errorTemplate,
optimizedTemplate,
} );
export default listViewTemplates;
@@ -0,0 +1,137 @@
import { __ } from '@wordpress/i18n';
import { escapeHTML } from '@wordpress/escape-html';
import { formatFileSize } from '../../utils';
import { UPGRADE_LINK } from '../../constants';
const notOptimizedTemplate = () => {
return `
<p class="image-optimization-control__property">
${ __( 'Status', 'image-optimization' ) }:
<span class="image-optimization-control__property-value">
${ __( 'Not optimized', 'image-optimization' ) }
</span>
</p>
<div class="image-optimization-control__action-button-wrapper">
<button type="button"
class="button button-primary image-optimization-control__button image-optimization-control__button--optimize">
${ __( 'Optimize now', 'image-optimization' ) }
</button>
</div>
`;
};
const loadingTemplate = () => {
return `
<p class="image-optimization-control__property">
${ __( 'Status', 'image-optimization' ) }:
<span class="image-optimization-control__property-value">
${ __( 'In Progress', 'image-optimization' ) }
</span>
</p>
<div class="image-optimization-control__action-spinner-wrapper">
<span class="spinner is-active"></span>
</div>
`;
};
const errorTemplate = ( message, imagesLeft ) => {
return `
<p class="image-optimization-control__property">
${ __( 'Status', 'image-optimization' ) }:
<span class="image-optimization-control__property-value">
${ __( 'Error', 'image-optimization' ) }
</span>
</p>
<p class="image-optimization-control__property">
${ __( 'Reason', 'image-optimization' ) }:
<span class="image-optimization-control__property-value">
${ escapeHTML( message ) }
</span>
</p>
<div class="image-optimization-control__action-button-wrapper">
${ imagesLeft === 0
? `<a class="button button-secondary button-large image-optimization-control__button"
href="${ UPGRADE_LINK }"
target="_blank" rel="noopener noreferrer">
${ __( 'Upgrade', 'image-optimization' ) }
</a>
` : `
<button class="button button-secondary button-large button-link-delete image-optimization-control__button image-optimization-control__button--try-again"
type="button">
${ __( 'Try again', 'image-optimization' ) }
</button>` }
</div>
`;
};
const optimizedTemplate = ( data ) => {
const absoluteValue = formatFileSize( data?.saved?.absolute, 1 );
return `
<p class="image-optimization-control__property">
${ __( 'Status', 'image-optimization' ) }:
<span class="image-optimization-control__property-value">
${ __( 'Optimized', 'image-optimization' ) }
</span>
</p>
<p class="image-optimization-control__property">
${ __( 'Image sizes optimized', 'image-optimization' ) }:
<span class="image-optimization-control__property-value">
${ data?.sizesOptimized }/${ data?.sizesTotal }
<button type="button"
class="image-optimization-control__details-button"
aria-label="${ __( 'Open optimization details', 'image-optimization' ) }">
(+)
</button>
</span>
</p>
<p class="image-optimization-control__property">
${ data?.saved?.absolute !== 0
? `
<span class="image-optimization-control__property-value">
${ __( 'Overall saving', 'image-optimization' ) }: ${ data?.saved?.relative }% (${ absoluteValue })
</span>
` : `
<span class="image-optimization-control__property-value">
${ __( 'Image is fully optimized', 'image-optimization' ) }
</span>
` }
</p>
<div class="image-optimization-control__action-button-wrapper">
${ data?.canBeRestored ? `
<button class="button button-link image-optimization-control__button image-optimization-control__button--restore-original"
type="button">
${ __( 'Restore original', 'image-optimization' ) }
</button>
` : '' }
<button class="button button-link image-optimization-control__button image-optimization-control__button--reoptimize"
type="button">
${ __( 'Reoptimize', 'image-optimization' ) }
</button>
</div>
`;
};
const metaBoxTemplates = Object.freeze( {
notOptimizedTemplate,
loadingTemplate,
errorTemplate,
optimizedTemplate,
} );
export default metaBoxTemplates;
@@ -0,0 +1,155 @@
import { __, sprintf } from '@wordpress/i18n';
import { formatFileSize } from '../../utils';
const header = () => {
return `
<div>
<table class="wp-list-table widefat striped image-optimization-details-table">
<thead>
<tr>
<th>${ __( 'Size Name', 'image-optimization' ) }</th>
<th>${ __( 'Image Size', 'image-optimization' ) }</th>
<th>${ __( 'Savings', 'image-optimization' ) }</th>
<th></th>
</tr>
</thead>
<tbody>
`;
};
const footer = ( data ) => {
const totalSize = sprintf(
// Translators: %s - total file size
__( 'Total: %s', 'image-optimization' ),
formatFileSize( data?.total ),
);
return `
</tbody>
</table>
<p>
<b>
${ totalSize }
</b>
</p>
</div>
`;
};
const rowStart = ( data ) => {
return `
<tr>
<td class="image-optimization-details-table__size-name"><b>${ data.size_name }</b></td>
<td class="image-optimization-details-table__size">${ formatFileSize( data.file_size ) }</td>
`;
};
const rowEnd = () => {
return '</tr>';
};
const optimizedChunk = ( data ) => {
const optimizationStats = sprintf(
// Translators: %1$s: Optimization percentage, %2$ file size decrease
__( 'Reduced by %1$s%% (%2$s)', 'image-optimization' ),
data.saved.relative,
formatFileSize( data.saved.absolute ),
);
const croppingStats = data.new_dimensions ? sprintf(
// Translators: %1$s: Width, %2$ height
__( 'Resized to %1$s(w) x %2$s(h)', 'image-optimization' ),
data.new_dimensions.width,
data.new_dimensions.height,
) : '';
return `
<td class="image-optimization-details-table__status">
<span class="image-optimization-details-table__property">
${ optimizationStats }
</span>
<span class="image-optimization-details-table__property">
${ croppingStats }
</span>
</td>
<td class="image-optimization-details-table__action"></td>
`;
};
const notOptimizedChunk = ( data ) => {
return `
<td class="image-optimization-details-table__status">
<span class="image-optimization-details-table__property">
${ __( 'Not optimized', 'image-optimization' ) }
</span>
</td>
<td class="image-optimization-details-table__action">
<button type="button"
class="button button-primary image-optimization-details-table__optimization-button"
data-image-id="${ data.imageId }">
${ __( 'Optimize', 'image-optimization' ) }
</button>
</td>
`;
};
const notFoundChunk = () => {
return `
<td class="image-optimization-details-table__status">
<span class="image-optimization-details-table__property image-optimization-details-table__property--error">
${ __( 'File is missing', 'image-optimization' ) }
</span>
</td>
<td class="image-optimization-details-table__action"></td>
`;
};
const tooLargeChunk = () => {
const message = sprintf(
// Translators: %s - max file size
__( 'File is too large. Max size is %s', 'image-optimization' ),
formatFileSize( window?.imageOptimizerUserData?.maxFileSize ),
);
return `
<td class="image-optimization-details-table__status">
<span class="image-optimization-details-table__property">
${ message }
</span>
</td>
<td class="image-optimization-details-table__action">
<a class="button button-primary button-large"
href="https://go.elementor.com/io-panel-upgrade/"
target="_blank" rel="noopener noreferrer">
${ __( 'Upgrade', 'image-optimization' ) }
</a>
</td>
`;
};
const error = ( data ) => {
return `
<span class="image-optimization-details-table__error">${ data.message }</span>
`;
};
const optimizationDetailsTemplate = Object.freeze( {
header,
footer,
rowStart,
rowEnd,
optimizedChunk,
notOptimizedChunk,
notFoundChunk,
tooLargeChunk,
error,
} );
export default optimizationDetailsTemplate;
@@ -0,0 +1,25 @@
import { __, sprintf } from '@wordpress/i18n';
export const formatFileSize = ( fileSizeInBytes, decimals = 2 ) => {
const sizes = [
// translators: %s: file size in bytes
__( '%s Bytes', 'image-optimization' ),
// translators: %s: file size in kilobytes
__( '%s Kb', 'image-optimization' ),
// translators: %s: file size in megabytes
__( '%s Mb', 'image-optimization' ),
// translators: %s: file size in gigabytes
__( '%s Gb', 'image-optimization' ),
];
if ( ! fileSizeInBytes ) {
// translators: %s: file size in bytes
return sprintf( __( '%s Bytes', 'image-optimization' ), 0 );
}
const currentScale = Math.floor( Math.log( fileSizeInBytes ) / Math.log( 1024 ) );
const formattedValue = parseFloat( ( fileSizeInBytes / Math.pow( 1024, currentScale ) ).toFixed( decimals ) );
// eslint-disable-next-line @wordpress/valid-sprintf
return sprintf( sizes[ currentScale ], formattedValue );
};
@@ -0,0 +1,499 @@
<?php
namespace ImageOptimization\Modules\Optimization\Classes;
use ImageOptimization\Classes\Async_Operation\{
Async_Operation,
Async_Operation_Hook,
Async_Operation_Queue,
Exceptions\Async_Operation_Exception,
Queries\Image_Optimization_Operation_Query
};
use ImageOptimization\Classes\Image\{
Exceptions\Invalid_Image_Exception,
Image,
Image_Meta,
Image_Optimization_Error_Type,
Image_Query_Builder,
Image_Status,
WP_Image_Meta
};
use ImageOptimization\Classes\Logger;
use ImageOptimization\Classes\Utils;
use ImageOptimization\Classes\Exceptions\Quota_Exceeded_Error;
use ImageOptimization\Modules\Optimization\Classes\Exceptions\Bulk_Token_Obtaining_Error;
use ImageOptimization\Modules\Optimization\Components\Exceptions\Bulk_Optimization_Token_Not_Found_Error;
use ImageOptimization\Modules\Settings\Classes\Settings;
use ImageOptimization\Modules\Stats\Classes\Optimization_Stats;
use ImageOptimization\Plugin;
use Throwable;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Bulk_Optimization_Controller {
private const OBTAIN_TOKEN_ENDPOINT = 'image/bulk-token';
public static function reschedule_bulk_optimization() {
self::delete_bulk_optimization();
self::find_images_and_schedule_optimization();
}
public static function reschedule_bulk_reoptimization() {
self::delete_bulk_reoptimization();
self::find_optimized_images_and_schedule_reoptimization();
}
/**
* Cancels pending bulk optimization operations.
*
* @return void
* @throws Async_Operation_Exception
*/
public static function delete_bulk_optimization(): void {
$query = ( new Image_Optimization_Operation_Query() )
->set_hook( Async_Operation_Hook::OPTIMIZE_BULK )
// It's risky to cancel in-progress operations at that point, so we cancel only the pending ones.
->set_status( Async_Operation::OPERATION_STATUS_PENDING )
->set_limit( -1 );
$operations = Async_Operation::get( $query );
foreach ( $operations as $operation ) {
$image_id = $operation->get_args()['attachment_id'];
Async_Operation::remove( [ $operation->get_id() ] );
( new Image_Meta( $image_id ) )->delete();
}
}
/**
* Cancels pending bulk re-optimization operations.
*
* @return void
* @throws Async_Operation_Exception
*/
public static function delete_bulk_reoptimization(): void {
$query = ( new Image_Optimization_Operation_Query() )
->set_hook( Async_Operation_Hook::REOPTIMIZE_BULK )
// It's risky to cancel in-progress operations at that point, so we cancel only the pending ones.
->set_status( Async_Operation::OPERATION_STATUS_PENDING )
->set_limit( -1 );
$operations = Async_Operation::get( $query );
foreach ( $operations as $operation ) {
$image_id = $operation->get_args()['attachment_id'];
Async_Operation::remove( [ $operation->get_id() ] );
( new Image_Meta( $image_id ) )->delete();
}
}
/**
* Looks for all non-optimized images and creates a bulk operation for each of them.
* Also, obtains bulk token and passes it to a newly created operation.
*
* @return void
*
* @throws Quota_Exceeded_Error|Invalid_Image_Exception
*/
public static function find_images_and_schedule_optimization(): void {
$images = self::find_images(
( new Image_Query_Builder() )
->return_not_optimized_images(),
true
);
if ( ! $images['total_images_count'] ) {
$not_fully_optimized_images = self::find_images(
self::query_not_fully_optimized_images(),
true
);
if ( ! $not_fully_optimized_images['total_images_count'] ) {
return;
}
$images = $not_fully_optimized_images;
}
$operation_id = wp_generate_password( 10, false );
try {
$bulk_token = self::obtain_bulk_token( $images['total_images_count'] );
self::set_bulk_operation_token( $operation_id, $bulk_token );
} catch ( Bulk_Token_Obtaining_Error $e ) {
$bulk_token = null;
}
foreach ( $images['attachments_in_quota'] as $attachment_id ) {
$meta = new Image_Meta( $attachment_id );
if ( null === $bulk_token ) {
$meta
->set_status( Image_Status::OPTIMIZATION_FAILED )
->save();
continue;
}
try {
Async_Operation::create(
Async_Operation_Hook::OPTIMIZE_BULK,
[
'attachment_id' => $attachment_id,
'operation_id' => $operation_id,
],
Async_Operation_Queue::OPTIMIZE
);
$meta
->set_status( Image_Status::OPTIMIZATION_IN_PROGRESS )
->save();
} catch ( Async_Operation_Exception $aoe ) {
$meta
->set_status( Image_Status::OPTIMIZATION_FAILED )
->save();
continue;
}
}
}
/**
* Looks for already optimized images with backups and creates a bulk operation for each of them.
* Also, obtains bulk token and passes it to a newly created operation.
*
* @return void
*
* @throws Quota_Exceeded_Error|Invalid_Image_Exception
*/
public static function find_optimized_images_and_schedule_reoptimization(): void {
$images = self::find_images(
( new Image_Query_Builder() )
->return_optimized_images()
);
if ( ! $images['total_images_count'] ) {
return;
}
$operation_id = wp_generate_password( 10, false );
try {
$bulk_token = self::obtain_bulk_token( $images['total_images_count'] );
self::set_bulk_operation_token( $operation_id, $bulk_token );
} catch ( Bulk_Token_Obtaining_Error $e ) {
$bulk_token = null;
}
foreach ( $images['attachments_in_quota'] as $attachment_id ) {
$meta = new Image_Meta( $attachment_id );
if ( null === $bulk_token ) {
$meta
->set_status( Image_Status::REOPTIMIZING_FAILED )
->save();
continue;
}
try {
Async_Operation::create(
Async_Operation_Hook::REOPTIMIZE_BULK,
[
'attachment_id' => $attachment_id,
'operation_id' => $operation_id,
],
Async_Operation_Queue::OPTIMIZE
);
$meta
->set_status( Image_Status::REOPTIMIZING_IN_PROGRESS )
->save();
} catch ( Async_Operation_Exception $aoe ) {
$meta
->set_status( Image_Status::REOPTIMIZING_FAILED )
->save();
continue;
}
}
foreach ( $images['attachments_out_of_quota'] as $attachment_id ) {
( new Image_Meta( $attachment_id ) )
->set_status( Image_Status::REOPTIMIZING_FAILED )
->set_error_type( Image_Optimization_Error_Type::QUOTA_EXCEEDED )
->save();
}
}
/**
* Looks for images for bulk optimization operations based on a query passed and the quota left.
*
* @param Image_Query_Builder $query Image query to execute.
* @param bool $limit_to_quota If true, it limits image query to the quota left.
* @return array{total_images_count: int, attachments_in_quota: array, attachments_out_of_quota: array}
*
* @throws Invalid_Image_Exception
* @throws Quota_Exceeded_Error
*/
private static function find_images( Image_Query_Builder $query, bool $limit_to_quota = false ): array {
$output = [
'total_images_count' => 0,
'attachments_in_quota' => [],
'attachments_out_of_quota' => [],
];
$images_left = Plugin::instance()->modules_manager->get_modules( 'connect-manager' )->connect_instance->images_left();
if ( ! $images_left ) {
throw new Quota_Exceeded_Error( __( 'Images quota exceeded', 'image-optimization' ) );
}
if ( $limit_to_quota ) {
$query->set_paging_size( $images_left );
}
$wp_query = $query->execute();
if ( ! $wp_query->post_count ) {
return $output;
}
foreach ( $wp_query->posts as $attachment_id ) {
try {
Validate_Image::is_valid( $attachment_id );
$wp_meta = new WP_Image_Meta( $attachment_id );
} catch ( Invalid_Image_Exception | Exceptions\Image_Validation_Error $ie ) {
continue;
}
$sizes_count = count( $wp_meta->get_size_keys() );
if ( $output['total_images_count'] + $sizes_count <= $images_left ) {
$output['total_images_count'] += $sizes_count;
$output['attachments_in_quota'][] = $attachment_id;
} else {
break;
}
}
$output['attachments_out_of_quota'] = array_diff( $wp_query->posts, $output['attachments_in_quota'] );
return $output;
}
/**
* Looks for images that were optimized, but not all their sizes were processed.
*
* @return Image_Query_Builder
*/
private static function query_not_fully_optimized_images(): Image_Query_Builder {
$result = [];
$sizes_enabled = Settings::get( Settings::CUSTOM_SIZES_OPTION_NAME );
$optimized_images = ( new Image_Query_Builder() )
->return_optimized_images()
->execute();
foreach ( $optimized_images->posts as $attachment_id ) {
try {
$image_meta = new Image_Meta( $attachment_id );
$wp_meta = new WP_Image_Meta( $attachment_id );
} catch ( Invalid_Image_Exception $iie ) {
continue;
}
$registered_sizes = $wp_meta->get_size_keys();
$optimized_sizes = $image_meta->get_optimized_sizes();
if ( 'all' !== $sizes_enabled ) {
$registered_sizes = array_filter($registered_sizes, function( $size ) use ( $sizes_enabled ) {
return in_array( $size, $sizes_enabled, true );
});
$optimized_sizes = array_filter($optimized_sizes, function( $size ) use ( $sizes_enabled ) {
return in_array( $size, $sizes_enabled, true );
});
}
if ( count( $optimized_sizes ) < count( $registered_sizes ) ) {
$result[] = $attachment_id;
}
}
return ( new Image_Query_Builder() )
->set_image_ids( $result );
}
/**
* Looks for the bulk token in transients.
*
* @param string $operation_id Bulk optimization operation id
*
* @return string|null Bulk token.
*
* @throws Bulk_Optimization_Token_Not_Found_Error
*/
public static function get_bulk_operation_token( string $operation_id ): ?string {
$bulk_token = get_transient( "image_optimizer_bulk_token_$operation_id" );
if ( ! $bulk_token ) {
throw new Bulk_Optimization_Token_Not_Found_Error( "There is no token found for the operation $operation_id" );
}
return $bulk_token;
}
/**
* Saves bulk optimization token to transients for a day.
*
* @param string $operation_id Bulk optimization operation id
* @param string $bulk_token Bulk optimization token
* @return void
*/
public static function set_bulk_operation_token( string $operation_id, string $bulk_token ): void {
set_transient( "image_optimizer_bulk_token_$operation_id", $bulk_token, HOUR_IN_SECONDS );
}
/**
* Sends a request to the BE to obtain bulk optimization token.
* It prevents obtaining a token for each and every optimization operation.
*
* @return string
*
* @throws Bulk_Token_Obtaining_Error
*/
private static function obtain_bulk_token( int $images_count ): ?string {
try {
$response = Utils::get_api_client()->make_request(
'POST',
self::OBTAIN_TOKEN_ENDPOINT,
[
'images_count' => $images_count,
]
);
} catch ( Throwable $t ) {
Logger::log( Logger::LEVEL_ERROR, 'Error while sending bulk token request: ' . $t->getMessage() );
throw new Bulk_Token_Obtaining_Error( $t->getMessage() );
}
return $response->token ?? null;
}
/**
* Checks if there is a bulk optimization operation in progress.
* If there is at least a single active bulk optimization operation it returns true, otherwise false.
*
* @return bool
* @throws Async_Operation_Exception
*/
public static function is_optimization_in_progress(): bool {
$query = ( new Image_Optimization_Operation_Query() )
->set_hook( Async_Operation_Hook::OPTIMIZE_BULK )
->set_status( [ Async_Operation::OPERATION_STATUS_PENDING, Async_Operation::OPERATION_STATUS_RUNNING ] )
->set_limit( 1 )
->return_ids();
return ! empty( Async_Operation::get( $query ) );
}
/**
* Checks if there is a bulk re-optimization operation in progress.
* If there is at least a single active bulk re-optimization operation it returns true, otherwise false.
*
* @return bool
* @throws Async_Operation_Exception
*/
public static function is_reoptimization_in_progress(): bool {
$query = ( new Image_Optimization_Operation_Query() )
->set_hook( Async_Operation_Hook::REOPTIMIZE_BULK )
->set_status( [ Async_Operation::OPERATION_STATUS_PENDING, Async_Operation::OPERATION_STATUS_RUNNING ] )
->set_limit( 1 )
->return_ids();
return ! empty( Async_Operation::get( $query ) );
}
/**
* Retrieves the bulk optimization process status.
*
* @return array{status: string, stats: array}
* @throws Async_Operation_Exception
*/
public static function get_status(): array {
$stats = Optimization_Stats::get_image_stats();
$output = [
'status' => 'not-started',
'percentage' => round( $stats['optimized_image_count'] / $stats['total_image_count'] * 100 ),
];
$active_query = ( new Image_Optimization_Operation_Query() )
->set_hook( Async_Operation_Hook::OPTIMIZE_BULK )
->set_status( [ Async_Operation::OPERATION_STATUS_PENDING, Async_Operation::OPERATION_STATUS_RUNNING ] )
->set_limit( -1 );
if ( empty( Async_Operation::get( $active_query ) ) ) {
return $output;
}
$output['status'] = 'in-progress';
return $output;
}
/**
* Returns latest operations for the bulk optimization screen.
*
* @param string|null $operation_id
*
* @return array
* @throws Async_Operation_Exception
*/
public static function get_processed_images( string $operation_id ): array {
$output = [];
$query = ( new Image_Optimization_Operation_Query() )
->set_hook( Async_Operation_Hook::OPTIMIZE_BULK )
->set_bulk_operation_id( $operation_id )
->set_limit( 50 );
$operations = Async_Operation::get( $query );
foreach ( $operations as $operation ) {
$image_id = $operation->get_args()['attachment_id'];
$image = new Image( $image_id );
try {
$stats = Optimization_Stats::get_image_stats( $image_id );
} catch ( Invalid_Image_Exception $iie ) {
continue;
} catch ( Throwable $t ) {
$original_file_size = 0;
$current_file_size = 0;
}
$output[] = [
'id' => $operation->get_id(),
'status' => $operation->get_status() === Async_Operation::OPERATION_STATUS_COMPLETE
? ( new Image_Meta( $image_id ) )->get_status()
: $operation->get_status(),
'image_name' => $image->get_attachment_object()->post_title,
'image_id' => $image_id,
'thumbnail_url' => $image->get_url( 'thumbnail' ),
'original_file_size' => $stats['initial_image_size'],
'current_file_size' => $stats['current_image_size'],
];
}
return $output;
}
}
@@ -0,0 +1,13 @@
<?php
namespace ImageOptimization\Modules\Optimization\Classes\Exceptions;
use Exception;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Bulk_Token_Expired_Error extends Exception {
protected $message = 'Bulk token expired error';
}
@@ -0,0 +1,13 @@
<?php
namespace ImageOptimization\Modules\Optimization\Classes\Exceptions;
use Exception;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Bulk_Token_Obtaining_Error extends Exception {
protected $message = 'Bulk token obtaining error';
}
@@ -0,0 +1,16 @@
<?php
namespace ImageOptimization\Modules\Optimization\Classes\Exceptions;
use Exception;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
/**
* The service responses with the error when the optimized image size >= than the original size.
*/
class Image_Already_Optimized_Error extends Exception {
protected $message = 'Image already optimized';
}
@@ -0,0 +1,13 @@
<?php
namespace ImageOptimization\Modules\Optimization\Classes\Exceptions;
use Exception;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Image_File_Already_Exists_Error extends Exception {
protected $message = 'Image file with this name already exists';
}
@@ -0,0 +1,13 @@
<?php
namespace ImageOptimization\Modules\Optimization\Classes\Exceptions;
use Exception;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Image_Optimization_Already_In_Progress_Error extends Exception {
protected $message = 'Image optimization already in progress error';
}
@@ -0,0 +1,13 @@
<?php
namespace ImageOptimization\Modules\Optimization\Classes\Exceptions;
use Exception;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Image_Optimization_Error extends Exception {
protected $message = 'Image optimization error';
}
@@ -0,0 +1,14 @@
<?php
namespace ImageOptimization\Modules\Optimization\Classes\Exceptions;
use Exception;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Image_Optimization_Retryable_Error extends Exception {
protected $message = 'Image optimization error';
}
@@ -0,0 +1,13 @@
<?php
namespace ImageOptimization\Modules\Optimization\Classes\Exceptions;
use Exception;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Image_Validation_Error extends Exception {
protected $message = 'Image validation error';
}
@@ -0,0 +1,39 @@
<?php
namespace ImageOptimization\Modules\Optimization\Classes;
use ImageOptimization\Classes\Image\Image_Optimization_Error_Type;
use TypeError;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Optimization_Error_Message {
public static function get_reoptimization_error_message( string $error_type ) {
if ( Image_Optimization_Error_Type::GENERIC === $error_type ) {
return esc_html__( 'Image reoptimizing failed', 'image-optimization' );
}
return self::get_optimization_error_message( $error_type );
}
public static function get_optimization_error_message( string $error_type ) {
if ( ! in_array( $error_type, Image_Optimization_Error_Type::get_values(), true ) ) {
throw new TypeError( "Error type $error_type is not a part of Image_Optimization_Error_Type values" );
}
$messages = [
Image_Optimization_Error_Type::FILE_ALREADY_EXISTS => esc_html__( 'File with this name already exists', 'image-optimization' ),
Image_Optimization_Error_Type::QUOTA_EXCEEDED => esc_html__( 'Plan quota reached', 'image-optimization' ),
Image_Optimization_Error_Type::GENERIC => esc_html__( 'Optimization error', 'image-optimization' ),
];
if ( isset( $messages[ $error_type ] ) ) {
return $messages[ $error_type ];
}
return $messages[ Image_Optimization_Error_Type::GENERIC ];
}
}
@@ -0,0 +1,67 @@
<?php
namespace ImageOptimization\Modules\Optimization\Classes;
use ImageOptimization\Classes\Image\{
Image_Meta,
Image_Optimization_Error_Type,
Image_Status
};
use ImageOptimization\Modules\Oauth\Classes\Data;
use ImageOptimization\Modules\Stats\Classes\Optimization_Stats;
use ImageOptimization\Plugin;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Optimization_Status {
/**
*
*
* @param int[] $image_ids
* @return array
*/
public static function get_images_optimization_statuses( array $image_ids ): array {
$output = [];
$images_left = Plugin::instance()->modules_manager->get_modules( 'connect-manager' )->connect_instance->images_left();
foreach ( $image_ids as $image_id ) {
$meta = new Image_Meta( $image_id );
$data = [];
$status = $meta->get_status();
$data['status'] = $status;
if ( Image_Status::OPTIMIZED === $status ) {
$data['stats'] = Optimization_Stats::get_image_stats( $image_id );
}
if ( Image_Status::RESTORING_FAILED === $status ) {
$data['message'] = esc_html__( 'Image restoring failed', 'image-optimization' );
}
if ( Image_Status::OPTIMIZATION_FAILED === $status ) {
$error_type = $meta->get_error_type() ?? Image_Optimization_Error_Type::GENERIC;
$error_message = Optimization_Error_Message::get_optimization_error_message( $error_type );
$data['message'] = $error_message;
$data['images_left'] = $images_left;
}
if ( Image_Status::REOPTIMIZING_FAILED === $status ) {
$error_type = $meta->get_error_type() ?? Image_Optimization_Error_Type::GENERIC;
$error_message = Optimization_Error_Message::get_reoptimization_error_message( $error_type );
$data['message'] = $error_message;
$data['images_left'] = $images_left;
}
$output[ $image_id ] = $data;
}
return $output;
}
}
@@ -0,0 +1,381 @@
<?php
namespace ImageOptimization\Modules\Optimization\Classes;
use ImageOptimization\Classes\File_System\{
Exceptions\File_System_Operation_Error,
File_System,
};
use ImageOptimization\Classes\File_Utils;
use ImageOptimization\Classes\Image\{
Exceptions\Image_Backup_Creation_Error,
Exceptions\Invalid_Image_Exception,
Image,
Image_Backup,
Image_Conversion,
Image_DB_Update,
Image_Dimensions,
Image_Meta,
Image_Status,
WP_Image_Meta
};
use ImageOptimization\Classes\Logger;
use ImageOptimization\Classes\Utils;
use ImageOptimization\Classes\Exceptions\Quota_Exceeded_Error;
use ImageOptimization\Modules\Optimization\Classes\{
Exceptions\Image_File_Already_Exists_Error,
Exceptions\Image_Optimization_Error,
Exceptions\Bulk_Token_Expired_Error,
Exceptions\Image_Already_Optimized_Error,
};
use ImageOptimization\Modules\Settings\Classes\Settings;
use Throwable;
use ImageOptimization\Plugin;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
/**
* The class is responsible for the optimization logic itself. It gets an image file, runs
* backup process if needed, sends a file to the service, stores the result and updates image meta.
*
* This class is used by manual, bulk and on-upload optimization flows.
*/
class Optimize_Image {
private const IMAGE_OPTIMIZE_ENDPOINT = 'image/optimize';
protected ?Image $image;
protected WP_Image_Meta $wp_meta;
protected string $initiator;
protected ?string $bulk_token;
private string $current_image_path;
private string $current_image_size;
private bool $keep_backups;
private array $current_size_duplicates;
protected Image_Conversion $image_conversion;
private bool $is_reoptimize;
/**
* @throws Quota_Exceeded_Error|Bulk_Token_Expired_Error|Image_File_Already_Exists_Error|Image_Optimization_Error
*/
public function optimize(): void {
$sizes_enabled = Settings::get( Settings::CUSTOM_SIZES_OPTION_NAME );
$sizes_exist = $this->wp_meta->get_size_keys();
Logger::log(
Logger::LEVEL_INFO,
"Start optimization of {$this->image->get_id()}"
);
foreach ( $sizes_exist as $size_exist ) {
// If some image sizes optimization is disabled in settings, we check if the current one is still enabled
if (
'all' !== $sizes_enabled &&
Image::SIZE_FULL !== $size_exist &&
! in_array( $size_exist, $sizes_enabled, true )
) {
continue;
}
// Elementor editor generates thumbnails we don't need to optimize.
if ( str_starts_with( $size_exist, 'elementor_' ) ) {
continue;
}
$image_meta = new Image_Meta( $this->image->get_id() );
// If the current size was already optimized -- ignore it.
if ( ! $this->is_reoptimize && in_array( $size_exist, $image_meta->get_optimized_sizes(), true ) ) {
Logger::log(
Logger::LEVEL_INFO,
"Size `$size_exist` is already optimized"
);
continue;
}
if ( ! file_exists( $this->image->get_file_path( $size_exist ) ) ) {
Logger::log(
Logger::LEVEL_ERROR,
"Can't access file for size `$size_exist`"
);
continue;
}
$this->current_image_size = $size_exist;
$this->current_size_duplicates = $this->wp_meta->get_size_duplicates( $size_exist );
$this->current_image_path = $this->image->get_file_path( $size_exist );
$this->optimize_current_size();
$this->current_image_size = '';
$this->current_size_duplicates = [];
$this->current_image_path = '';
}
$this->mark_as_optimized();
if ( ! $this->keep_backups ) {
Image_Backup::remove( $this->image->get_id() );
}
Logger::log(
Logger::LEVEL_INFO,
"End optimization of {$this->image->get_id()}"
);
}
private function optimize_current_size(): void {
try {
$original_path = $this->current_image_path;
$response = $this->send_file();
$optimized_size = (int) $response->optimizedSize; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
$optimized_image_binary = base64_decode( $response->image, true ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode
$this->replace_image_file( $optimized_image_binary );
$this->update_attachment_meta( $optimized_size );
if ( $original_path !== $this->current_image_path ) {
$this->update_posts( $original_path, $this->current_image_path );
}
// This should only be updated after meta
$this->update_attachment_post();
} catch ( Image_Already_Optimized_Error $iao ) {
// If we can't optimize it further, just update the meta
$original_size = $this->wp_meta->get_file_size( $this->current_image_size )
?? File_System::size( $this->image->get_file_path( $this->current_image_size ) );
$this->update_attachment_meta( $original_size, true );
$this->update_attachment_post( true );
} catch ( Bulk_Token_Expired_Error | Quota_Exceeded_Error | Image_File_Already_Exists_Error $e ) {
throw $e;
} catch ( Throwable $t ) {
// In case of anything else
throw new Image_Optimization_Error( $t->getMessage() );
}
}
private function send_file() {
$connect_status = Plugin::instance()->modules_manager->get_modules( 'connect-manager' )->connect_instance->get_connect_status();
$headers = [
'access_token' => $connect_status->access_token ?? '',
];
if ( $this->bulk_token ) {
$headers['x-elementor-bulk-token'] = $this->bulk_token;
}
$optimization_options = [
'compression_level' => Settings::get( Settings::COMPRESSION_LEVEL_OPTION_NAME ),
'convert_to_format' => $this->image_conversion->get_current_conversion_option(),
'strip_exif' => Settings::get( Settings::STRIP_EXIF_METADATA_OPTION_NAME ),
];
if ( Settings::get( Settings::RESIZE_LARGER_IMAGES_OPTION_NAME ) ) {
$optimization_options['resize'] = Settings::get( Settings::RESIZE_LARGER_IMAGES_SIZE_OPTION_NAME );
}
$image_key = wp_generate_password( 15, false );
$response = Utils::get_api_client()->make_request(
'POST',
self::IMAGE_OPTIMIZE_ENDPOINT,
[
'initiator' => $this->initiator,
'image_url' => $this->image->get_url( $this->current_image_size ),
'image_key' => $image_key,
'checksum' => md5_file( $this->current_image_path ),
'attachment_id' => $this->image->get_id(),
'attachment_parent_id' => Image::SIZE_FULL === $this->current_image_size ? 0 : $this->image->get_id(),
'image_optimization_settings' => base64_encode( wp_json_encode( $optimization_options ) ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
],
$headers,
$this->current_image_path,
'image'
);
if ( isset( $response->stats ) ) {
Plugin::instance()->modules_manager->get_modules( 'connect-manager' )->connect_instance->update_usage_data( $response->stats );
}
if ( ! isset( $response->imageKey ) || $image_key !== $response->imageKey ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
Logger::log(
Logger::LEVEL_ERROR,
"Image key must be $image_key, instead got $response->imageKey"
);
throw new Image_Optimization_Error( esc_html__( 'Service response is incorrect', 'image-optimization' ) );
}
$received_file_hash = md5( base64_decode( $response->image, true ) );
if ( ! isset( $response->checksum ) || $received_file_hash !== $response->checksum ) {
Logger::log(
Logger::LEVEL_ERROR,
"Image key must be $response->checksum, instead calculated $received_file_hash"
);
throw new Image_Optimization_Error( esc_html__( 'Service response is incorrect', 'image-optimization' ) );
}
return $response;
}
/**
* @throws File_System_Operation_Error|Image_Backup_Creation_Error|Image_File_Already_Exists_Error
*/
private function replace_image_file( string $file_data ): void {
$path = $this->current_image_path;
// If we have backups disabled, we want to make sure we can download and save new file before we
// remove an existing one.
$tmp_path = File_Utils::replace_extension( $path, 'tmp' );
File_System::put_contents( $tmp_path, $file_data );
if ( $this->image_conversion->is_enabled() ) {
$converted_path = File_Utils::replace_extension( $tmp_path, $this->image_conversion->get_current_file_extension() );
$original_is_already_converted = strtolower( $path ) === strtolower( $converted_path );
if ( ! $original_is_already_converted && File_System::exists( $converted_path ) ) {
throw new Image_File_Already_Exists_Error();
}
// We want to keep both original as a fallback and optimized webp to prevent 404s
if ( ! $original_is_already_converted ) {
$this->keep_backups = true;
( new Image_Meta( $this->image->get_id() ) )
->set_image_backup_path( $this->current_image_size, $this->current_image_path )
->save();
}
if ( $original_is_already_converted ) {
Image_Backup::create( $this->image->get_id(), $this->current_image_size, $this->current_image_path );
}
File_System::move( $tmp_path, $converted_path, true );
$path = $converted_path;
} else {
Image_Backup::create( $this->image->get_id(), $this->current_image_size, $this->current_image_path );
File_System::move( $tmp_path, $path, true );
}
if ( Image::SIZE_FULL === $this->current_image_size ) {
// Drop WP caches
update_attached_file( $this->image->get_id(), $path );
}
// Updating to the correct value
$this->current_image_path = $path;
}
/**
* Updates attachment records in the `wp_posts` table.
*
* @return void
*/
private function update_attachment_post( ?bool $is_fully_optimized = false ) {
$update_query = [];
if ( $this->image_conversion->is_enabled() && ! $is_fully_optimized ) {
$attachment_object = $this->image->get_attachment_object();
$update_query['guid'] = File_Utils::replace_extension(
$attachment_object->guid,
$this->image_conversion->get_current_file_extension()
);
$update_query['post_mime_type'] = $this->image_conversion->get_current_mime_type();
}
$update_query['post_modified'] = current_time( 'mysql' );
$update_query['post_modified_gmt'] = current_time( 'mysql', true );
$this->image->update_attachment( $update_query );
}
/**
* Updates attachment records in the `wp_postmeta` table.
*
* @param int $optimized_size
* @param bool|null $is_fully_optimized
*
* @return void
*/
private function update_attachment_meta( int $optimized_size, ?bool $is_fully_optimized = false ) {
$meta = new Image_Meta( $this->image->get_id() );
$dimensions = Image_Dimensions::get_by_path( $this->current_image_path );
$sizes_to_update = [ $this->current_image_size, ...$this->current_size_duplicates ];
foreach ( $sizes_to_update as $size ) {
$meta
->set_compression_level( Settings::get( Settings::COMPRESSION_LEVEL_OPTION_NAME ) )
->add_optimized_size( $size )
->add_original_data( $size, $this->wp_meta->get_size_data( $size ) );
$this->wp_meta
->set_width( $size, $dimensions->width )
->set_height( $size, $dimensions->height )
->set_file_size( $size, $optimized_size );
if ( $this->image_conversion->is_enabled() && ! $is_fully_optimized ) {
$this->wp_meta
->set_file_path( $size, $this->current_image_path )
->set_mime_type( $size, $this->image_conversion->get_current_mime_type() );
}
}
$meta->save();
$this->wp_meta->save();
}
/**
* If we change an image extension, we should walk through the wp_posts table and update all the
* hardcoded image links to prevent 404s.
*
* @param string $old_path Previous image path
* @param string $new_path Current image path
*
* @return void
*/
private function update_posts( string $old_path, string $new_path ) {
Image_DB_Update::update_posts_table_urls(
File_Utils::get_url_from_path( $old_path ),
File_Utils::get_url_from_path( $new_path )
);
}
/**
* Changes image status after all image sizes were optimized.
*
* @return void
*/
private function mark_as_optimized() {
( new Image_Meta( $this->image->get_id() ) )
->set_status( Image_Status::OPTIMIZED )
->set_error_type( null )
->save();
}
/**
* @throws Invalid_Image_Exception
*/
public function __construct( int $image_id, string $initiator, ?string $bulk_token = null, ?bool $is_reoptimize = false ) {
$this->image = new Image( $image_id );
$this->wp_meta = new WP_Image_Meta( $image_id, $this->image );
$this->initiator = $initiator;
$this->bulk_token = $bulk_token;
$this->is_reoptimize = $is_reoptimize;
$this->image_conversion = new Image_Conversion();
$this->keep_backups = Settings::get( Settings::BACKUP_ORIGINAL_IMAGES_OPTION_NAME );
}
}
@@ -0,0 +1,41 @@
<?php
namespace ImageOptimization\Modules\Optimization\Classes;
use ImageOptimization\Classes\Route;
use WP_REST_Request;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Route_Base extends Route {
protected $auth = true;
protected string $path = '';
public function get_methods(): array {
return [];
}
public function get_endpoint(): string {
return 'optimize/' . $this->get_path();
}
public function get_path(): string {
return $this->path;
}
public function get_name(): string {
return '';
}
public function post_permission_callback( WP_REST_Request $request ): bool {
return $this->get_permission_callback( $request );
}
public function get_permission_callback( WP_REST_Request $request ): bool {
$valid = $this->permission_callback( $request );
return $valid && user_can( $this->current_user_id, 'manage_options' );
}
}
@@ -0,0 +1,87 @@
<?php
namespace ImageOptimization\Modules\Optimization\Classes;
use ImageOptimization\Classes\Async_Operation\{
Async_Operation,
Async_Operation_Hook,
Async_Operation_Queue,
Exceptions\Async_Operation_Exception,
Queries\Image_Optimization_Operation_Query
};
use ImageOptimization\Classes\Image\{
Image_Meta,
Image_Status,
};
use ImageOptimization\Modules\Optimization\Classes\Exceptions\Image_Optimization_Already_In_Progress_Error;
use Throwable;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Single_Optimization {
/**
* @throws Throwable|Async_Operation_Exception
*/
public static function optimize_many( array $image_ids, bool $is_reoptimize = false ): void {
foreach ( $image_ids as $image_id ) {
try {
self::schedule_single_optimization( $image_id, $is_reoptimize );
} catch ( Image_Optimization_Already_In_Progress_Error $ioe ) {
continue;
} catch ( Throwable $t ) {
throw $t;
}
}
}
/**
* @throws Throwable|Async_Operation_Exception|Image_Optimization_Already_In_Progress_Error
*/
public static function schedule_single_optimization( int $image_id, bool $is_reoptimize = false ): void {
if ( self::is_optimization_in_progress( $image_id ) ) {
throw new Image_Optimization_Already_In_Progress_Error(
esc_html__( 'Optimization is already in progress', 'image-optimization' )
);
}
$meta = new Image_Meta( $image_id );
try {
$meta
->set_status(
$is_reoptimize
? Image_Status::REOPTIMIZING_IN_PROGRESS
: Image_Status::OPTIMIZATION_IN_PROGRESS
)
->save();
Async_Operation::create(
$is_reoptimize ? Async_Operation_Hook::REOPTIMIZE_SINGLE : Async_Operation_Hook::OPTIMIZE_SINGLE,
[ 'attachment_id' => $image_id ],
Async_Operation_Queue::OPTIMIZE
);
} catch ( Throwable $t ) {
$meta
->set_status( $is_reoptimize ? Image_Status::REOPTIMIZING_FAILED : Image_Status::OPTIMIZATION_FAILED )
->save();
throw $t;
}
}
/**
* @throws Async_Operation_Exception
*/
private static function is_optimization_in_progress( int $image_id ): bool {
$query = ( new Image_Optimization_Operation_Query() )
->set_status( [ Async_Operation::OPERATION_STATUS_PENDING, Async_Operation::OPERATION_STATUS_RUNNING ] )
->set_image_id( $image_id )
->return_ids();
$operations = Async_Operation::get( $query );
return count( $operations ) > 0;
}
}
@@ -0,0 +1,152 @@
<?php
namespace ImageOptimization\Modules\Optimization\Classes;
use ImageOptimization\Classes\Image\{
Exceptions\Invalid_Image_Exception,
Image,
Image_Meta,
WP_Image_Meta,
};
use ImageOptimization\Classes\File_System\Exceptions\File_System_Operation_Error;
use ImageOptimization\Classes\File_System\File_System;
use ImageOptimization\Classes\File_Utils;
use ImageOptimization\Modules\Optimization\Classes\Exceptions\Image_Validation_Error;
use ImageOptimization\Plugin;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Validate_Image {
public const MAX_FILE_SIZE = 10 * 1024 * 1024;
public const MAX_BIG_FILE_SIZE = 25 * 1024 * 1024;
/**
* Returns true if $image_id provided associated with an image that can be optimized.
*
* @param int $image_id Attachment id.
*
* @return true
* @throws Image_Validation_Error
* @throws Invalid_Image_Exception
*/
public static function is_valid( int $image_id ): bool {
$attachment_object = get_post( $image_id );
if ( ! $attachment_object ) {
throw new Image_Validation_Error(
__( 'Can\'t optimize this file. If the issue persists, Contact Support', 'image-optimization' )
);
}
if ( ! wp_attachment_is_image( $attachment_object ) ||
! in_array( $attachment_object->post_mime_type, Image::get_supported_mime_types(), true ) ||
(
in_array( $attachment_object->post_mime_type, Image::get_mime_types_cannot_be_optimized(), true ) &&
! get_post_meta( $image_id, Image_Meta::IMAGE_OPTIMIZER_METADATA_KEY, true )
)
) {
throw new Image_Validation_Error( self::prepare_supported_formats_list_error() );
}
if ( ! file_exists( get_attached_file( $image_id ) ) ) {
throw new Image_Validation_Error(
esc_html__( 'File is missing. Verify the upload', 'image-optimization' )
);
}
self::validate_file_size( $image_id );
return true;
}
/**
* Prepares the error message for the unsupported file formats.
*
* @return string The error message.
*/
private static function prepare_supported_formats_list_error(): string {
$formats = Image::get_supported_formats();
$formats = array_filter(
$formats,
fn ( $format ) => ! in_array( $format, Image::get_formats_cannot_be_optimized(), true )
);
$last_item = strtoupper( array_pop( $formats ) );
$formats_list = join( ', ', array_map( 'strtoupper', $formats ) );
return sprintf(
__( 'Unsupported file format. Only %1$s, or %2$s are supported', 'image-optimization' ),
$formats_list,
$last_item
);
}
/**
* @throws Invalid_Image_Exception
* @throws Image_Validation_Error
*/
public static function validate_file_size( int $image_id, string $image_size = Image::SIZE_FULL ) {
try {
$wp_meta = new WP_Image_Meta( $image_id );
$image_size = $wp_meta->get_file_size( $image_size )
?? File_System::size( ( new Image( $image_id ) )->get_file_path( $image_size ) );
} catch ( File_System_Operation_Error $e ) {
throw new Image_Validation_Error(
esc_html__( 'File is missing. Verify the upload', 'image-optimization' )
);
}
if ( $image_size > self::MAX_FILE_SIZE ) {
if ( ! self::are_big_files_supported() ) {
throw new Image_Validation_Error(
sprintf(
__( 'File is too large. Max size is %s', 'image-optimization' ),
File_Utils::format_file_size( self::MAX_FILE_SIZE, 0 ),
)
);
}
if ( $image_size > self::MAX_BIG_FILE_SIZE ) {
throw new Image_Validation_Error(
sprintf(
__( 'File is too large. Max size is %s', 'image-optimization' ),
File_Utils::format_file_size( self::MAX_BIG_FILE_SIZE, 0 ),
)
);
}
}
}
/**
* Returns the max file size allowed by the current plan.
*
* @return int File size in bytes.
*/
public static function get_max_file_size(): int {
return self::are_big_files_supported() ? self::MAX_BIG_FILE_SIZE : self::MAX_FILE_SIZE;
}
public static function are_big_files_supported(): bool {
static $is_allowed = null;
$connect_manager = Plugin::instance()->modules_manager->get_modules( 'connect-manager' );
if ( null === $is_allowed ) {
if ( ! $connect_manager->connect_instance->is_connected() ) {
$is_allowed = false;
return $is_allowed;
}
$plan_data = $connect_manager->connect_instance->get_connect_status();
if ( ! isset( $plan_data->subscription_plan->features->large_upload_allowed ) ) {
$is_allowed = false;
} else {
$is_allowed = (bool) $plan_data->subscription_plan->features->large_upload_allowed;
}
}
return $is_allowed;
}
}
@@ -0,0 +1,153 @@
<?php
namespace ImageOptimization\Modules\Optimization\Components;
use ImageOptimization\Classes\Async_Operation\{
Async_Operation,
Async_Operation_Hook,
Async_Operation_Queue,
Exceptions\Async_Operation_Exception,
Queries\Image_Optimization_Operation_Query,
Queries\Operation_Query
};
use ImageOptimization\Classes\Image\{
Image_Meta,
Image_Optimization_Error_Type,
Image_Query_Builder,
Image_Status,
};
use ImageOptimization\Classes\Logger;
use Throwable;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Actions_Cleanup {
const FIVE_MINUTES_IN_SECONDS = 300;
/**
* @async
* @return void
*/
public function cleanup_stuck_operations() {
global $wpdb;
$table_name = $wpdb->prefix . 'actionscheduler_actions';
$now = time();
$threshold = $now - self::FIVE_MINUTES_IN_SECONDS;
// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
$results = $wpdb->get_col(
$wpdb->prepare(
"
SELECT action_id
FROM {$table_name}
WHERE last_attempt_gmt IS NOT NULL
AND UNIX_TIMESTAMP(last_attempt_gmt) < %d
",
$threshold
)
);
// phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
if ( empty( $results ) ) {
Logger::debug( 'No stuck optimization operations found for cleanup.' );
return;
}
foreach ( $results as $action_id ) {
$action = Async_Operation::get_by_id( (int) $action_id );
if (
! $action ||
Async_Operation_Queue::OPTIMIZE !== $action->get_queue() ||
Async_Operation::OPERATION_STATUS_RUNNING !== $action->get_status()
) {
continue;
}
try {
do_action(
'action_scheduler_failed_action',
$action_id,
self::FIVE_MINUTES_IN_SECONDS
);
Logger::debug( "Triggered retry for stuck action ID {$action_id}." );
} catch ( Throwable $t ) {
Logger::warn( "Failed to handle stuck operation for action ID {$action_id}: " . $t->getMessage() );
}
}
try {
$this->cleanup_stuck_statuses();
} catch ( Throwable $t ) {
Logger::warn( 'Failed to run stuck statuses clearing job: ' . $t->getMessage() );
}
}
/**
* The handler checks if there are any attachments that have the in-progress status, but no jobs are currently
* run or pending. Those attachment statuses will be updated to a generic error.
*
* @throws Async_Operation_Exception
*/
public function cleanup_stuck_statuses() {
$operations_query = ( new Image_Optimization_Operation_Query() )
->set_status( [ Async_Operation::OPERATION_STATUS_PENDING, Async_Operation::OPERATION_STATUS_RUNNING ] )
->set_limit( 1 );
$operations = Async_Operation::get( $operations_query );
if ( ! empty( $operations ) ) {
return;
}
$image_query = ( new Image_Query_Builder() )
->return_optimization_in_progress_images()
->set_paging_size( -1 )
->execute();
foreach ( $image_query->posts as $attachment_id ) {
( new Image_Meta( $attachment_id ) )
->set_status( Image_Status::OPTIMIZATION_FAILED )
->set_error_type( Image_Optimization_Error_Type::GENERIC )
->save();
}
}
public function schedule_cleanup() {
try {
$cleanup_job_query = ( new Operation_Query() )
->set_queue( Async_Operation_Queue::CLEANUP )
->set_hook( Async_Operation_Hook::STUCK_OPERATION_CLEANUP )
->set_status( [ Async_Operation::OPERATION_STATUS_PENDING, Async_Operation::OPERATION_STATUS_RUNNING ] )
->set_limit( 1 );
// Prevents job duplication. For some reason unique=true is not enough
if ( ! empty( Async_Operation::get( $cleanup_job_query ) ) ) {
return;
}
Async_Operation::create_recurring(
time(),
self::FIVE_MINUTES_IN_SECONDS,
Async_Operation_Hook::STUCK_OPERATION_CLEANUP,
[],
Async_Operation_Queue::CLEANUP,
10,
true
);
} catch ( Async_Operation_Exception $aoe ) {
Logger::warn( 'Failed to schedule recurring stuck operation cleanup: ' . $aoe->getMessage() );
}
}
public function __construct() {
add_action( 'action_scheduler_init', [ $this, 'schedule_cleanup' ] );
add_action( Async_Operation_Hook::STUCK_OPERATION_CLEANUP, [ $this, 'cleanup_stuck_operations' ] );
}
}
@@ -0,0 +1,40 @@
<?php
namespace ImageOptimization\Modules\Optimization\Components;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
use ImageOptimization\Modules\Backups\Classes\Restore_Images;
use ImageOptimization\Modules\Optimization\Classes\Single_Optimization as Single_Optimization_Controller;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Admin_Bulk_Actions {
public function add_bulk_actions( array $bulk_actions ): array {
$bulk_actions['image-optimization-optimize'] = esc_html__( 'Optimize', 'image-optimization' );
$bulk_actions['image-optimization-restore'] = esc_html__( 'Restore original', 'image-optimization' );
return $bulk_actions;
}
public function handle_bulk_actions( $redirect_url, $action, $post_ids ): string {
if ( 'image-optimization-optimize' === $action ) {
Single_Optimization_Controller::optimize_many( $post_ids );
}
if ( 'image-optimization-restore' === $action ) {
Restore_Images::find_and_schedule_restoring( $post_ids );
}
return $redirect_url;
}
public function __construct() {
add_filter( 'bulk_actions-upload', [ $this, 'add_bulk_actions' ] );
add_filter( 'handle_bulk_actions-upload', [ $this, 'handle_bulk_actions' ], 10, 3 );
}
}
@@ -0,0 +1,116 @@
<?php
namespace ImageOptimization\Modules\Optimization\Components;
use ImageOptimization\Classes\Image\Image_Meta;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Admin_Filter {
public function add_filter( string $post_type ) {
if ( 'attachment' !== $post_type ) {
return;
}
$options = [
'' => __( 'All Media Files', 'image-optimization' ),
'optimized' => __( 'Optimized', 'image-optimization' ),
'not-optimized' => __( 'Unoptimized', 'image-optimization' ),
'in-progress' => __( 'In progress', 'image-optimization' ),
'failed' => __( 'Errors', 'image-optimization' ),
];
$current_value = $this->get_current_filter();
?>
<label class="screen-reader-text" for="image-optimization-filter">
<?php esc_html_e( 'Filter by optimization status', 'image-optimization' ); ?>
</label>
<select class="image-optimization-filter" id="image-optimization-filter" name="image-optimization-filter">
<?php
foreach ( $options as $value => $title ) {
printf(
'<option value="%s" %s>%s</option>',
esc_attr( $value ),
selected( $value, $current_value, false ),
esc_html( $title )
);
}
?>
</select>
<?php
}
public function handle_filter( $query ) {
global $pagenow;
if ( 'upload.php' !== $pagenow ) {
return;
}
$current_value = $this->get_current_filter();
if ( empty( $current_value ) ) {
return;
}
$meta_query = empty( $query->get( 'meta_query' ) ) ? [] : $query->get( 'meta_query' );
$meta_query[] = [
[
'key' => '_wp_attachment_metadata', // Images without this field considered invalid
'compare' => 'EXISTS',
],
];
switch ( $current_value ) {
case 'not-optimized':
$meta_query[] = [
'key' => Image_Meta::IMAGE_OPTIMIZER_METADATA_KEY,
'compare' => 'NOT EXISTS',
];
break;
case 'optimized':
$meta_query[] = [
'compare' => 'LIKE',
'value' => '"status";s:9:"optimized"',
'key' => Image_Meta::IMAGE_OPTIMIZER_METADATA_KEY,
];
break;
case 'in-progress':
$meta_query[] = [
'compare' => 'LIKE',
'value' => '-in-progress"', // Covers both optimization and restoring
'key' => Image_Meta::IMAGE_OPTIMIZER_METADATA_KEY,
];
break;
case 'failed':
$meta_query[] = [
'compare' => 'LIKE',
'value' => '-failed"', // Covers both optimization and restoring
'key' => Image_Meta::IMAGE_OPTIMIZER_METADATA_KEY,
];
break;
}
$query->set( 'meta_query', $meta_query );
}
private function get_current_filter(): string {
return sanitize_text_field( wp_unslash( $_GET['image-optimization-filter'] ?? '' ) );
}
public function __construct() {
add_filter( 'restrict_manage_posts', [ $this, 'add_filter' ] );
add_filter( 'parse_query', [ $this, 'handle_filter' ] );
}
}
@@ -0,0 +1,82 @@
<?php
namespace ImageOptimization\Modules\Optimization\Components;
use ImageOptimization\Classes\Image\Image_Dimensions;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Avif_Compatibility {
public function is_avif_supported(): bool {
return in_array( 'image/avif', get_allowed_mime_types(), true );
}
public function add_to_supported_types( $mime_types ) {
$mime_types['avif'] = 'image/avif';
return $mime_types;
}
public function add_to_supported_mime_types( $mime_types ) {
$mime_types['image/avif'] = 'avif';
return $mime_types;
}
public function mark_as_displayable( $result, $path ) {
if ( str_ends_with( $path, '.avif' ) ) {
return true;
}
return $result;
}
public function fix_avif_images( $metadata, $attachment_id ) {
if ( empty( $metadata ) ) {
return $metadata;
}
$attachment = get_post( $attachment_id );
if ( ! $attachment || is_wp_error( $attachment ) || 'image/avif' !== $attachment->post_mime_type ) {
return $metadata;
}
if ( ( ! empty( $metadata['width'] ) && ( 0 !== $metadata['width'] ) ) && ( ! empty( $metadata['height'] ) && 0 !== $metadata['height'] ) ) {
return $metadata;
}
$file = get_attached_file( $attachment_id );
if ( ! $file ) {
return $metadata;
}
$dimensions = Image_Dimensions::get_by_path( $file );
$metadata['width'] = $dimensions->width;
$metadata['height'] = $dimensions->height;
if ( empty( $metadata['file'] ) ) {
$metadata['file'] = _wp_relative_upload_path( $file );
}
if ( empty( $metadata['sizes'] ) ) {
$metadata['sizes'] = [];
}
return $metadata;
}
public function __construct() {
if ( $this->is_avif_supported() ) {
return;
}
add_filter( 'upload_mimes', [ $this, 'add_to_supported_types' ] );
add_filter( 'mime_types', [ $this, 'add_to_supported_types' ] );
add_filter( 'getimagesize_mimes_to_exts', [ $this, 'add_to_supported_mime_types' ] );
add_filter( 'file_is_displayable_image', [ $this, 'mark_as_displayable' ], 10, 2 );
add_filter( 'wp_generate_attachment_metadata', [ $this, 'fix_avif_images' ], 1, 3 );
}
}
@@ -0,0 +1,184 @@
<?php
namespace ImageOptimization\Modules\Optimization\Components;
use ImageOptimization\Classes\Async_Operation\Async_Operation_Hook;
use ImageOptimization\Classes\Image\{
Image,
Image_Meta,
Image_Optimization_Error_Type,
Image_Restore,
Image_Status
};
use ImageOptimization\Classes\Async_Operation\Exceptions\Async_Operation_Exception;
use ImageOptimization\Classes\Logger;
use ImageOptimization\Classes\Utils;
use ImageOptimization\Classes\Exceptions\Quota_Exceeded_Error;
use ImageOptimization\Modules\Optimization\{
Classes\Exceptions\Bulk_Token_Expired_Error,
Classes\Exceptions\Image_File_Already_Exists_Error,
Classes\Optimize_Image,
Classes\Bulk_Optimization_Controller,
Components\Exceptions\Bulk_Optimization_Token_Not_Found_Error,
};
use ImageOptimization\Modules\Stats\Classes\Optimization_Stats;
use Throwable;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Bulk_Optimization {
const BULK_OPTIMIZATION_BASE_SLUG = 'image-optimization-bulk-optimization';
const BULK_OPTIMIZATION_CAPABILITY = 'manage_options';
public function render_app() {
?>
<!-- The hack required to wrap WP notifications -->
<div class="wrap">
<h1 style="display: none;" role="presentation"></h1>
</div>
<div id="image-optimization-app"></div>
<?php
}
public function register_page() {
add_media_page(
__( 'Bulk Optimization', 'image-optimization' ),
__( 'Bulk Optimization', 'image-optimization' ),
self::BULK_OPTIMIZATION_CAPABILITY,
self::BULK_OPTIMIZATION_BASE_SLUG,
[ $this, 'render_app' ],
7
);
}
/** @async */
public function optimize_bulk( int $image_id, string $operation_id ) {
try {
$bulk_token = Bulk_Optimization_Controller::get_bulk_operation_token( $operation_id );
$oi = new Optimize_Image(
$image_id,
'bulk',
$bulk_token
);
$oi->optimize();
} catch ( Quota_Exceeded_Error $qe ) {
( new Image_Meta( $image_id ) )
->set_status( Image_Status::OPTIMIZATION_FAILED )
->set_error_type( Image_Optimization_Error_Type::QUOTA_EXCEEDED )
->save();
} catch ( Image_File_Already_Exists_Error $fe ) {
( new Image_Meta( $image_id ) )
->set_status( Image_Status::OPTIMIZATION_FAILED )
->set_error_type( Image_Optimization_Error_Type::FILE_ALREADY_EXISTS )
->save();
} catch ( Bulk_Token_Expired_Error | Bulk_Optimization_Token_Not_Found_Error $bte ) {
( new Image_Meta( $image_id ) )
->set_status( Image_Status::NOT_OPTIMIZED )
->save();
Bulk_Optimization_Controller::reschedule_bulk_optimization();
} catch ( Throwable $t ) {
Logger::log( Logger::LEVEL_ERROR, 'Optimization error. Reason: ' . $t->getMessage() );
Retry::maybe_retry_optimization( $image_id );
} finally {
Optimization_Stats::get_image_stats( null, true );
}
}
/** @async */
public function reoptimize_bulk( int $image_id, string $operation_id ) {
try {
$image = new Image( $image_id );
if ( $image->can_be_restored() ) {
Image_Restore::restore( $image_id, true );
}
$bulk_token = Bulk_Optimization_Controller::get_bulk_operation_token( $operation_id );
$oi = new Optimize_Image(
$image_id,
'bulk',
$bulk_token,
true
);
$oi->optimize();
} catch ( Quota_Exceeded_Error $qe ) {
( new Image_Meta( $image_id ) )
->set_status( Image_Status::REOPTIMIZING_FAILED )
->set_error_type( Image_Optimization_Error_Type::QUOTA_EXCEEDED )
->save();
} catch ( Image_File_Already_Exists_Error $fe ) {
( new Image_Meta( $image_id ) )
->set_status( Image_Status::REOPTIMIZING_FAILED )
->set_error_type( Image_Optimization_Error_Type::FILE_ALREADY_EXISTS )
->save();
} catch ( Bulk_Token_Expired_Error | Bulk_Optimization_Token_Not_Found_Error $bte ) {
( new Image_Meta( $image_id ) )
->set_status( Image_Status::NOT_OPTIMIZED )
->save();
Bulk_Optimization_Controller::reschedule_bulk_reoptimization();
} catch ( Throwable $t ) {
Logger::log( Logger::LEVEL_ERROR, 'Reoptimization error. Reason: ' . $t->getMessage() );
Retry::maybe_retry_optimization( $image_id );
} finally {
Optimization_Stats::get_image_stats( null, true );
}
}
/**
* Renders the bulk optimization notice
*
* @return void
*/
public function render_bulk_optimization_notice() {
try {
$is_in_progress = Bulk_Optimization_Controller::is_optimization_in_progress();
} catch ( Async_Operation_Exception $aoe ) {
$is_in_progress = false;
}
?>
<div class="notice notice-info notice image-optimizer__notice image-optimizer__notice--info image-optimizer__notice--bulk-tip"
style="display: <?php echo $is_in_progress ? 'block' : 'none'; ?>">
<p>
<b>
<?php esc_html_e(
'Heads up!',
'image-optimization'
); ?>
</b>
<span>
<?php esc_html_e(
'Bulk optimizing may take a lot of processing and server time, depending on the number of images. Your site will still work smoothly until the processing is all done, without any downtime.',
'image-optimization'
); ?>
</span>
</p>
</div>
<?php
}
public function __construct() {
add_action( 'admin_menu', [ $this, 'register_page' ] );
add_action( Async_Operation_Hook::OPTIMIZE_BULK, [ $this, 'optimize_bulk' ], 10, 2 );
add_action( Async_Operation_Hook::REOPTIMIZE_BULK, [ $this, 'reoptimize_bulk' ], 10, 2 );
add_action('current_screen', function () {
if ( Utils::is_bulk_optimization_page() ) {
add_filter( 'admin_footer_text', [ $this, 'render_bulk_optimization_notice' ] );
}
});
}
}
@@ -0,0 +1,13 @@
<?php
namespace ImageOptimization\Modules\Optimization\Components\Exceptions;
use Exception;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Bulk_Optimization_Token_Not_Found_Error extends Exception {
protected $message = 'Bulk optimization token not found';
}
@@ -0,0 +1,75 @@
<?php
namespace ImageOptimization\Modules\Optimization\Components;
use ImageOptimization\Modules\Core\Components\Pointers;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class List_View_Pointer {
const CURRENT_POINTER_SLUG = 'image-optimizer-list-view';
public function admin_print_script() {
if ( $this->is_dismissed() ) {
return;
}
wp_enqueue_script( 'wp-pointer' );
wp_enqueue_style( 'wp-pointer' );
$pointer_content = '<h3>' . esc_html__( 'Switch to list view', 'image-optimization' ) . '</h3>';
$pointer_content .= '<p>' . esc_html__( 'Get the most out of your optimizing options. Use the List view to quickly optimize your uploaded images with Image Optimizer.', 'image-optimization' ) . '</p>';
$allowed_tags = [
'h3' => [],
'p' => [],
];
?>
<script>
jQuery( document ).ready( function( $ ) {
setTimeout(() => {
$( '#wp-media-grid div.media-toolbar.wp-filter' ).first().pointer( {
content: '<?php echo wp_kses( $pointer_content, $allowed_tags ); ?>',
pointerClass: 'image-optimization-list-view-pointer',
position: {
edge: 'top',
align: '<?php echo is_rtl() ? 'right' : 'left'; ?>',
},
close() {
return jQuery.ajax( {
url: ajaxurl,
method: 'POST',
data: {
action: 'image_optimizer_pointer_dismissed',
data: {
pointer: '<?php echo esc_attr( static::CURRENT_POINTER_SLUG ); ?>'
},
nonce: '<?php echo esc_attr( wp_create_nonce( 'image-optimization-pointer-dismissed' ) ); ?>'
}
} );
}
} ).pointer( 'open' );
}, 0)
} );
</script>
<style>
.image-optimization-list-view-pointer .wp-pointer-arrow {
inset-inline-start: 15px;
}
</style>
<?php
}
private function is_dismissed(): bool {
$meta = (array) get_user_meta( get_current_user_id(), Pointers::DISMISSED_POINTERS_META_KEY, true );
return key_exists( static::CURRENT_POINTER_SLUG, $meta );
}
public function __construct() {
add_action( 'in_admin_header', [ $this, 'admin_print_script' ] );
}
}
@@ -0,0 +1,302 @@
<?php
namespace ImageOptimization\Modules\Optimization\Components;
use ImageOptimization\Classes\Image\{
Exceptions\Invalid_Image_Exception,
Image,
Image_Conversion,
Image_Conversion_Option,
Image_Meta,
Image_Optimization_Error_Type,
Image_Status
};
use ImageOptimization\Modules\Oauth\Components\{
Exceptions\Auth_Error,
};
use ImageOptimization\Modules\Optimization\{
Classes\Exceptions\Image_Validation_Error,
Classes\Optimization_Error_Message,
Classes\Validate_Image,
Module,
};
use ImageOptimization\Classes\File_Utils;
use ImageOptimization\Modules\Settings\Classes\Settings;
use ImageOptimization\Modules\Stats\Classes\Optimization_Stats;
use Throwable;
use ImageOptimization\Plugin;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
/**
* The class is responsible for rendering optimization control in all views.
*/
class Media_Control {
const COLUMN_ID = 'image_optimization';
const META_BOX_ID = 'image_optimization_meta_box';
const DETAILS_MODAL_FIELD_ID = 'image_optimization_modal';
/**
* Adds a new optimization column to the library list view.
*
* @param array $columns
* @return array
*/
public function add_optimization_column( array $columns ): array {
if ( empty( $columns ) ) {
return $columns;
}
$columns[ self::COLUMN_ID ] = esc_html__( 'Image optimization', 'image-optimization' );
return $columns;
}
/**
* Renders optimization controls in the library list view.
*
* @param string $column_name
* @param int $attachment_id
* @return void
*/
public function render_optimization_column( string $column_name, int $attachment_id ) {
if ( self::COLUMN_ID !== $column_name ) {
return;
}
$this->render_optimization_control( 'list-view', $attachment_id );
}
/**
* Adds optimization control as media meta box.
*
* @return void
*/
public function add_optimization_meta_box() {
add_meta_box(
self::META_BOX_ID,
__( 'Image optimization', 'image-optimization' ),
function( $post ) {
$this->render_optimization_control( 'meta-box', $post->ID );
},
'attachment',
'side',
);
}
/**
* Adds optimization control to media modals.
*
* @param array $form_fields
* @param \WP_Post $post
*
* @return array
*/
public function add_media_modal_details( array $form_fields, \WP_Post $post ): array {
$form_fields[ self::DETAILS_MODAL_FIELD_ID ] = [
'label' => '',
'input' => 'hidden',
'value' => $this->get_optimization_control_html( 'details-view', $post->ID ),
'required' => false,
];
return $form_fields;
}
/**
* Returns optimization control as an HTML string.
*
* @param string $context Control placement context. One of ['list-view', 'meta-box', 'details-view'].
* @param int $image_id Attachment id.
* @return false|string
*/
public function get_optimization_control_html( string $context, int $image_id ) {
ob_start();
$this->render_optimization_control( $context, $image_id );
$html = ob_get_contents();
ob_end_clean();
return $html;
}
/**
* Renders the optimization control for the current image in the current context.
*
* @param string $context Control placement context. One of ['list-view', 'meta-box', 'details-view'].
* @param int $image_id Attachment id.
* @return void Renders the control.
*/
public function render_optimization_control( string $context, int $image_id ) {
$global_context = [
'image_id' => $image_id,
'can_be_restored' => false,
];
// @var ImageOptimizer/Modules/ConnectManager/Module
$module = Plugin::instance()->modules_manager->get_modules( 'connect-manager' );
try {
if ( ! $module->connect_instance->is_connected() || ! $module->connect_instance->is_activated() ) {
throw new Auth_Error( 'You have to activate your license to use Image Optimizer' );
}
Validate_Image::is_valid( $image_id );
$image = new Image( $image_id );
$meta = new Image_Meta( $image->get_id() );
if ( Image_Status::OPTIMIZED === $meta->get_status() ) {
$global_context['can_be_restored'] = $image->can_be_restored();
} else {
$ic = new Image_Conversion();
$path = $image->get_file_path( Image::SIZE_FULL );
$backups_enabled = Settings::get( Settings::BACKUP_ORIGINAL_IMAGES_OPTION_NAME );
$conversion_enabled = $ic->is_enabled();
$needs_conversion = $conversion_enabled &&
strtolower( File_Utils::get_extension( $path ) ) !== $ic->get_current_file_extension();
$global_context['can_be_restored'] = ( $needs_conversion && $conversion_enabled ) || $backups_enabled;
}
switch ( $meta->get_status() ) {
case Image_Status::OPTIMIZATION_IN_PROGRESS:
Module::load_template( $context, 'loading', array_merge(
$global_context, [
'action' => 'optimize',
]
) );
break;
case Image_Status::REOPTIMIZING_IN_PROGRESS:
Module::load_template( $context, 'loading', array_merge(
$global_context, [
'action' => 'reoptimize',
]
) );
break;
case Image_Status::RESTORING_IN_PROGRESS:
Module::load_template( $context, 'loading', array_merge(
$global_context, [
'action' => 'restore',
]
) );
break;
case Image_Status::OPTIMIZED:
$stats = Optimization_Stats::get_image_stats( $image_id );
$saved = [
'relative' => max( 100 - round( $stats['current_image_size'] / $stats['initial_image_size'] * 100 ), 0 ),
'absolute' => $stats['initial_image_size'] - $stats['current_image_size'],
];
$is_losseless_and_webp = Image_Conversion_Option::WEBP === Settings::get( Settings::CONVERT_TO_FORMAT_OPTION_NAME )
&& 'lossless' === Settings::get( Settings::COMPRESSION_LEVEL_OPTION_NAME );
Module::load_template( $context, 'optimized', array_merge(
$global_context, [
'sizes_optimized_count' => $stats['optimized_image_count'],
'sizes_total' => $stats['total_image_count'],
'saved' => $saved,
'is_losseless_and_webp' => $is_losseless_and_webp,
]
) );
break;
case Image_Status::OPTIMIZATION_FAILED:
$error_type = $meta->get_error_type() ?? Image_Optimization_Error_Type::GENERIC;
$error_message = Optimization_Error_Message::get_optimization_error_message( $error_type );
$images_left = $module->connect_instance->images_left();
Module::load_template( $context, 'error', array_merge(
$global_context, [
'message' => $error_message,
'optimization_error_type' => $error_type,
'images_left' => $images_left,
'allow_retry' => true,
'action' => 'optimize',
]
) );
break;
case Image_Status::REOPTIMIZING_FAILED:
$error_type = $meta->get_error_type() ?? Image_Optimization_Error_Type::GENERIC;
$error_message = Optimization_Error_Message::get_reoptimization_error_message( $error_type );
$images_left = $module->connect_instance->images_left();
Module::load_template( $context, 'error', array_merge(
$global_context, [
'message' => $error_message,
'optimization_error_type' => $error_type,
'images_left' => $images_left,
'allow_retry' => true,
'action' => 'reoptimize',
]
) );
break;
case Image_Status::RESTORING_FAILED:
Module::load_template( $context, 'error', array_merge(
$global_context, [
'message' => esc_html__( 'Image restoring error', 'image-optimization' ),
'allow_retry' => true,
'action' => 'restore',
]
) );
break;
default:
Module::load_template( $context, 'not-optimized', $global_context );
}
} catch ( Invalid_Image_Exception | Image_Validation_Error $iie ) {
Module::load_template( $context, 'error', array_merge(
$global_context, [
'action' => 'error',
'message' => $iie->getMessage(),
'allow_retry' => false,
]
) );
} catch ( Auth_Error $ae ) {
Module::load_template( $context, 'error', array_merge(
$global_context, [
'action' => 'error',
'message' => esc_html__( 'N/A', 'image-optimization' ),
'allow_retry' => false,
]
) );
} catch ( Throwable $t ) {
Module::load_template( $context, 'error', array_merge(
$global_context, [
'action' => 'error',
'message' => esc_html__( 'Internal server error', 'image-optimization' ),
'allow_retry' => false,
]
) );
}
}
public function __construct() {
add_filter( 'manage_upload_columns', [ $this, 'add_optimization_column' ] );
add_action( 'manage_media_custom_column', [ $this, 'render_optimization_column' ], 10, 2 );
add_action( 'add_meta_boxes_attachment', [ $this, 'add_optimization_meta_box' ] );
add_filter( 'attachment_fields_to_edit', [ $this, 'add_media_modal_details' ], 10, 2 );
}
}
@@ -0,0 +1,109 @@
<?php
namespace ImageOptimization\Modules\Optimization\Components;
use ImageOptimization\Classes\Async_Operation\{
Async_Operation,
Async_Operation_Queue,
Exceptions\Async_Operation_Exception,
Queries\Image_Optimization_Operation_Query,
};
use ImageOptimization\Classes\Image\{
Image_Meta,
Image_Optimization_Error_Type,
Image_Status,
};
use ImageOptimization\Classes\Logger;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Retry {
const RETRY_LIMIT = 3;
const ONE_MINUTE_IN_SECONDS = 60;
public static function maybe_retry_optimization( int $image_id ) {
$query = ( new Image_Optimization_Operation_Query() )
->set_image_id( $image_id )
->set_status( Async_Operation::OPERATION_STATUS_RUNNING )
->set_limit( 1 );
try {
[ $action ] = Async_Operation::get( $query );
if ( ! $action ) {
Logger::info( 'Could not find an action to retry for ' . $image_id );
( new Image_Meta( $image_id ) )
->set_status( Image_Status::OPTIMIZATION_FAILED )
->set_error_type( Image_Optimization_Error_Type::GENERIC )
->save();
return;
}
// Unlikely timeout to separate force retries from the real timeout issues
do_action( 'action_scheduler_failed_action', $action->get_id(), 1337 );
} catch ( Async_Operation_Exception $aoe ) {
Logger::error( "Failed to retry an optimization operation for `$image_id`: " . $aoe->getMessage() );
}
}
public function retry_failed_optimizations( $action_id ) {
$action = Async_Operation::get_by_id( (int) $action_id );
// Ensure it's the optimization action we want to reschedule
if ( Async_Operation_Queue::OPTIMIZE !== $action->get_queue() ) {
return;
}
Logger::info( "Optimization {$action_id} failed" );
$attachment_id = isset( $action->get_args()['attachment_id'] ) ? (int) $action->get_args()['attachment_id'] : null;
if ( ! $attachment_id ) {
Logger::info( "Missing image ID in failed action {$action_id}" );
return;
}
$image_meta = new Image_Meta( $attachment_id );
$retry_count = $image_meta->get_retry_count() ?? 0;
if ( $retry_count < self::RETRY_LIMIT ) {
$image_meta
->set_retry_count( $retry_count + 1 )
->save();
try {
Async_Operation::create(
$action->get_hook(),
$action->get_args(),
$action->get_queue(),
time() + ( $retry_count * self::ONE_MINUTE_IN_SECONDS ),
);
Logger::debug(
"Rescheduled image optimization for image ID {$attachment_id}. Retry attempt: " . ( $retry_count + 1 )
);
} catch ( Async_Operation_Exception $aoe ) {
Logger::debug(
"Failed to reschedule optimization for image ID {$attachment_id}: " . $aoe->getMessage()
);
}
} else {
$image_meta
->set_status( Image_Status::OPTIMIZATION_FAILED )
->set_retry_count( null )
->save();
Logger::debug( "Image optimization for image ID {$attachment_id} exceeded retry limit." );
}
}
public function __construct() {
add_action( 'action_scheduler_failed_action', [ $this, 'retry_failed_optimizations' ], 10, 2 );
}
}
@@ -0,0 +1,88 @@
<?php
namespace ImageOptimization\Modules\Optimization\Components;
use ImageOptimization\Classes\Async_Operation\Async_Operation_Hook;
use ImageOptimization\Classes\Image\{
Image,
Image_Meta,
Image_Optimization_Error_Type,
Image_Restore,
Image_Status
};
use ImageOptimization\Classes\Logger;
use ImageOptimization\Classes\Exceptions\Quota_Exceeded_Error;
use ImageOptimization\Modules\Optimization\Classes\Exceptions\Image_File_Already_Exists_Error;
use ImageOptimization\Modules\Optimization\Classes\Optimize_Image;
use Throwable;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Single_Optimization {
/** @async */
public function optimize_single_image( int $image_id ) {
try {
$oi = new Optimize_Image(
$image_id,
'manual',
);
$oi->optimize();
} catch ( Quota_Exceeded_Error $qe ) {
( new Image_Meta( $image_id ) )
->set_status( Image_Status::OPTIMIZATION_FAILED )
->set_error_type( Image_Optimization_Error_Type::QUOTA_EXCEEDED )
->save();
} catch ( Image_File_Already_Exists_Error $fe ) {
( new Image_Meta( $image_id ) )
->set_status( Image_Status::OPTIMIZATION_FAILED )
->set_error_type( Image_Optimization_Error_Type::FILE_ALREADY_EXISTS )
->save();
} catch ( Throwable $t ) {
Logger::log( Logger::LEVEL_ERROR, 'Optimization error. Reason: ' . $t->getMessage() );
Retry::maybe_retry_optimization( $image_id );
}
}
/** @async */
public function reoptimize_single_image( int $image_id ) {
try {
$image = new Image( $image_id );
if ( $image->can_be_restored() ) {
Image_Restore::restore( $image_id, true );
}
$oi = new Optimize_Image(
$image_id,
'manual',
null,
true
);
$oi->optimize();
} catch ( Quota_Exceeded_Error $qe ) {
( new Image_Meta( $image_id ) )
->set_status( Image_Status::REOPTIMIZING_FAILED )
->set_error_type( Image_Optimization_Error_Type::QUOTA_EXCEEDED )
->save();
} catch ( Image_File_Already_Exists_Error $fe ) {
( new Image_Meta( $image_id ) )
->set_status( Image_Status::REOPTIMIZING_FAILED )
->set_error_type( Image_Optimization_Error_Type::FILE_ALREADY_EXISTS )
->save();
} catch ( Throwable $t ) {
Logger::log( Logger::LEVEL_ERROR, 'Reoptimizing error. Reason: ' . $t->getMessage() );
Retry::maybe_retry_optimization( $image_id );
}
}
public function __construct() {
add_action( Async_Operation_Hook::OPTIMIZE_SINGLE, [ $this, 'optimize_single_image' ], 3 );
add_action( Async_Operation_Hook::REOPTIMIZE_SINGLE, [ $this, 'reoptimize_single_image' ] );
}
}
@@ -0,0 +1,109 @@
<?php
namespace ImageOptimization\Modules\Optimization\Components;
use ImageOptimization\Classes\Async_Operation\{
Async_Operation,
Async_Operation_Hook,
Async_Operation_Queue,
};
use ImageOptimization\Classes\Image\{
Exceptions\Invalid_Image_Exception,
Image_Meta,
Image_Optimization_Error_Type,
Image_Status
};
use ImageOptimization\Classes\Logger;
use ImageOptimization\Classes\Exceptions\Quota_Exceeded_Error;
use ImageOptimization\Modules\Optimization\Classes\Exceptions\Image_File_Already_Exists_Error;
use ImageOptimization\Modules\Optimization\Classes\Exceptions\Image_Validation_Error;
use ImageOptimization\Modules\Optimization\Classes\Optimize_Image;
use ImageOptimization\Modules\Optimization\Classes\Validate_Image;
use ImageOptimization\Modules\Settings\Classes\Settings;
use Throwable;
use ImageOptimization\Plugin;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Upload_Optimization {
public function handle_upload( array $metadata, int $attachment_id, string $context ) {
if ( 'create' !== $context ) {
return $metadata;
}
if ( ! Settings::get( Settings::OPTIMIZE_ON_UPLOAD_OPTION_NAME ) ) {
return $metadata;
}
// @var ImageOptimizer/Modules/ConnectManager/Module
$module = Plugin::instance()->modules_manager->get_modules( 'connect-manager' );
if ( ! $module->connect_instance->is_connected() || ! $module->connect_instance->is_activated() ) {
return $metadata;
}
try {
Validate_Image::is_valid( $attachment_id );
} catch ( Invalid_Image_Exception | Image_Validation_Error $iie ) {
return $metadata;
}
$meta = new Image_Meta( $attachment_id );
try {
$meta
->set_status( Image_Status::OPTIMIZATION_IN_PROGRESS )
->save();
Async_Operation::create(
Async_Operation_Hook::OPTIMIZE_ON_UPLOAD,
[ 'attachment_id' => $attachment_id ],
Async_Operation_Queue::OPTIMIZE
);
} catch ( Throwable $t ) {
$meta
->set_status( Image_Status::OPTIMIZATION_FAILED )
->save();
}
return $metadata;
}
/** @async */
public function optimize_image_on_upload( int $image_id ) {
try {
$oi = new Optimize_Image(
$image_id,
'upload',
);
$oi->optimize();
} catch ( Quota_Exceeded_Error $qe ) {
( new Image_Meta( $image_id ) )
->set_status( Image_Status::OPTIMIZATION_FAILED )
->set_error_type( Image_Optimization_Error_Type::QUOTA_EXCEEDED )
->save();
} catch ( Image_File_Already_Exists_Error $fe ) {
( new Image_Meta( $image_id ) )
->set_status( Image_Status::OPTIMIZATION_FAILED )
->set_error_type( Image_Optimization_Error_Type::FILE_ALREADY_EXISTS )
->save();
} catch ( Throwable $t ) {
Logger::log( Logger::LEVEL_ERROR, 'Optimization error. Reason: ' . $t->getMessage() );
( new Image_Meta( $image_id ) )
->set_status( Image_Status::OPTIMIZATION_FAILED )
->set_error_type( Image_Optimization_Error_Type::GENERIC )
->save();
}
}
public function __construct() {
add_action( 'wp_generate_attachment_metadata', [ $this, 'handle_upload' ], 10, 3 );
add_action( Async_Operation_Hook::OPTIMIZE_ON_UPLOAD, [ $this, 'optimize_image_on_upload' ] );
}
}
@@ -0,0 +1,109 @@
<?php
namespace ImageOptimization\Modules\Optimization;
use ImageOptimization\Classes\Module_Base;
use ImageOptimization\Modules\Backups\Rest\Restore_Single;
use ImageOptimization\Modules\Optimization\Rest\Optimize_Single_Image;
use Throwable;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Module extends Module_Base {
public function get_name(): string {
return 'optimization';
}
public static function routes_list() : array {
return [
'Optimize_Single_Image',
'Optimize_Bulk',
'Get_Bulk_Optimization_Images',
'Optimization_Status',
'Cancel_Bulk_Optimization',
];
}
public static function component_list() : array {
return [
'Avif_Compatibility',
'Media_Control',
'Single_Optimization',
'Upload_Optimization',
'Bulk_Optimization',
'List_View_Pointer',
'Admin_Bulk_Actions',
'Admin_Filter',
'Retry',
'Actions_Cleanup',
];
}
/**
* Enqueue styles and scripts
*/
public function enqueue_scripts() {
$asset_file = include IMAGE_OPTIMIZATION_ASSETS_PATH . 'build/control.asset.php';
foreach ( $asset_file['dependencies'] as $style ) {
wp_enqueue_style( $style );
}
add_thickbox();
wp_enqueue_style(
'image-optimization-control',
$this->get_css_assets_url( 'control' ),
[],
IMAGE_OPTIMIZATION_VERSION,
);
wp_enqueue_script(
'image-optimization-control',
$this->get_js_assets_url( 'control' ),
$asset_file['dependencies'],
IMAGE_OPTIMIZATION_VERSION,
true
);
wp_set_script_translations( 'image-optimization-control', 'image-optimization' );
wp_localize_script(
'image-optimization-control',
'imageOptimizerControlSettings',
[
'optimizeSingleImageNonce' => wp_create_nonce( Optimize_Single_Image::NONCE_NAME ),
'restoreSingleImageNonce' => wp_create_nonce( Restore_Single::NONCE_NAME ),
]
);
}
public static function load_template( $path, $name, $args = [] ): bool {
$templates_path = sprintf(
'%s/templates/%s/%s.php',
dirname( __FILE__ ),
$path,
$name
);
try {
load_template( $templates_path, false, $args );
} catch ( Throwable $t ) {
return false;
}
return true;
}
/**
* Module constructor.
*/
public function __construct() {
$this->register_components();
$this->register_routes();
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_scripts' ] );
add_action( 'elementor/editor/after_enqueue_scripts', [ $this, 'enqueue_scripts' ] );
}
}
@@ -0,0 +1,64 @@
<?php
namespace ImageOptimization\Modules\Optimization\Rest;
use ImageOptimization\Modules\Optimization\Classes\{
Bulk_Optimization_Controller,
Route_Base,
};
use ImageOptimization\Modules\Oauth\Components\Connect;
use Throwable;
use WP_REST_Request;
use ImageOptimization\Plugin;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Cancel_Bulk_Optimization extends Route_Base {
const NONCE_NAME = 'image-optimization-cancel-bulk-optimization';
protected string $path = 'bulk/cancel';
public function get_name(): string {
return 'cancel-bulk-optimization';
}
public function get_methods(): array {
return [ 'POST' ];
}
public function POST( WP_REST_Request $request ) {
$this->verify_nonce_and_capability(
$request->get_param( self::NONCE_NAME ),
self::NONCE_NAME
);
if ( ! Plugin::instance()->modules_manager->get_modules( 'connect-manager' )->connect_instance->is_activated() ) {
return $this->respond_error_json([
'message' => esc_html__( 'Invalid activation', 'image-optimization' ),
'code' => 'unauthorized',
]);
}
try {
$is_in_progress = Bulk_Optimization_Controller::is_optimization_in_progress();
if ( ! $is_in_progress ) {
return $this->respond_error_json([
'message' => esc_html__( 'Bulk optimization is not in progress', 'image-optimization' ),
'code' => 'forbidden',
]);
}
Bulk_Optimization_Controller::delete_bulk_optimization();
return $this->respond_success_json();
} catch ( Throwable $t ) {
return $this->respond_error_json([
'message' => $t->getMessage(),
'code' => 'internal_server_error',
]);
}
}
}
@@ -0,0 +1,51 @@
<?php
namespace ImageOptimization\Modules\Optimization\Rest;
use ImageOptimization\Modules\Optimization\Classes\{
Bulk_Optimization_Controller,
Route_Base,
};
use Throwable;
use WP_REST_Request;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Get_Bulk_Optimization_Images extends Route_Base {
protected string $path = 'bulk/images';
public function get_name(): string {
return 'bulk-optimization-images';
}
public function get_methods(): array {
return [ 'GET' ];
}
public function GET( WP_REST_Request $request ) {
try {
$operation_id = $request->get_param( 'operation_id' );
if ( empty( $operation_id ) ) {
return $this->respond_error_json([
'message' => esc_html__( 'Missed operation id', 'image-optimization' ),
'code' => 'bad_request',
]);
}
$images = Bulk_Optimization_Controller::get_processed_images( $operation_id );
return $this->respond_success_json([
'images' => $images,
]);
} catch ( Throwable $t ) {
return $this->respond_error_json([
'message' => $t->getMessage(),
'code' => 'internal_server_error',
]);
}
}
}
@@ -0,0 +1,47 @@
<?php
namespace ImageOptimization\Modules\Optimization\Rest;
use ImageOptimization\Modules\Optimization\Classes\Optimization_Status as Optimization_Status_Controller;
use ImageOptimization\Modules\Optimization\Classes\Route_Base;
use Throwable;
use WP_REST_Request;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
class Optimization_Status extends Route_Base {
protected string $path = 'status';
public function get_name(): string {
return 'optimization-status';
}
public function get_methods(): array {
// There might be a huge amount of ids in some cases, so decided to keep it as POST
return [ 'POST' ];
}
public function POST( WP_REST_Request $request ) {
try {
$body = json_decode( $request->get_body() );
$image_ids = $body->image_ids ?? null;
if ( empty( $image_ids ) ) {
return $this->respond_success_json([
'status' => [],
]);
}
return $this->respond_success_json([
'status' => Optimization_Status_Controller::get_images_optimization_statuses( $image_ids ),
]);
} catch ( Throwable $t ) {
return $this->respond_error_json([
'message' => $t->getMessage(),
'code' => 'internal_server_error',
]);
}
}
}

Some files were not shown because too many files have changed in this diff Show More