| @@ -3,9 +3,17 @@ | ||
| 3 | 3 | namespace ImageOptimization\Modules\Settings; |
| 4 | 4 | |
| 5 | 5 | use ImageOptimization\Classes\Image\Image_Conversion_Option; |
| 6 | 6 | use ImageOptimization\Classes\Module_Base; |
| 7 | -use ImageOptimization\Modules\Settings\Classes\Settings; | |
| 7 | +use ImageOptimization\Modules\Settings\{ | |
| 8 | + Banners\Sale_Banner, | |
| 9 | + Banners\Elementor_Birthday_Banner, | |
| 10 | + Classes\Settings, | |
| 11 | +}; | |
| 12 | +use ImageOptimization\Modules\Stats\Classes\Optimization_Stats; | |
| 13 | +use ImageOptimization\Classes\Client\Client; | |
| 14 | +use ImageOptimization\Modules\ConnectManager\Components\Connect as Connect_Manager_Connect; | |
| 15 | +use ImageOptimization\Modules\Connect\Classes\Config; | |
| 8 | 16 | |
| 9 | 17 | if ( ! defined( 'ABSPATH' ) ) { |
| 10 | 18 | exit; // Exit if accessed directly |
| 11 | 19 | } |
| @@ -19,15 +27,15 @@ | ||
| 19 | 27 | public function get_name(): string { |
| 20 | 28 | return 'settings'; |
| 21 | 29 | } |
| 22 | 30 | |
| 23 | - public static function component_list() : array { | |
| 31 | + public static function component_list(): array { | |
| 24 | 32 | return [ |
| 25 | 33 | 'Settings_Pointer', |
| 26 | 34 | ]; |
| 27 | 35 | } |
| 28 | 36 | |
| 29 | - public static function get_options() : array { | |
| 37 | + public static function get_options(): array { | |
| 30 | 38 | return [ |
| 31 | 39 | 'compression_level' => [ 'default' => 'lossy' ], |
| 32 | 40 | 'optimize_on_upload' => [ |
| 33 | 41 | 'type' => 'boolean', |
| @@ -56,8 +64,21 @@ | ||
| 56 | 64 | 'custom_sizes' => [ |
| 57 | 65 | 'type' => 'string', |
| 58 | 66 | 'default' => 'all', |
| 59 | 67 | ], |
| 68 | + 'help_videos' => [ | |
| 69 | + 'type' => 'object', | |
| 70 | + 'show_in_rest' => [ | |
| 71 | + 'schema' => [ | |
| 72 | + 'type' => 'object', | |
| 73 | + 'additionalProperties' => true, | |
| 74 | + ], | |
| 75 | + ], | |
| 76 | + ], | |
| 77 | + 'migration_popup_dismissed' => [ | |
| 78 | + 'type' => 'boolean', | |
| 79 | + 'default' => false, | |
| 80 | + ], | |
| 60 | 81 | ]; |
| 61 | 82 | } |
| 62 | 83 | |
| 63 | 84 | public function register_options() { |
| @@ -80,8 +101,11 @@ | ||
| 80 | 101 | } |
| 81 | 102 | |
| 82 | 103 | public function render_app() { |
| 83 | 104 | ?> |
| 105 | + <?php Sale_Banner::get_banner( 'https://go.elementor.com/IO-BF-sale' ); ?> | |
| 106 | + <?php Elementor_Birthday_Banner::get_banner( 'https://go.elementor.com/IO-10th-bd-sale' ); ?> | |
| 107 | + | |
| 84 | 108 | <!-- The hack required to wrap WP notifications --> |
| 85 | 109 | <div class="wrap"> |
| 86 | 110 | <h1 style="display: none;" role="presentation"></h1> |
| 87 | 111 | </div> |
| @@ -90,18 +114,36 @@ | ||
| 90 | 114 | <?php |
| 91 | 115 | } |
| 92 | 116 | |
| 93 | 117 | public function register_page() { |
| 94 | - add_media_page( | |
| 95 | - __( 'Image Optimizer', 'image-optimization' ), | |
| 96 | - __( 'Image Optimizer', 'image-optimization' ), | |
| 118 | + add_submenu_page( | |
| 119 | + 'elementor-home', | |
| 120 | + __( 'Image Optimization', 'image-optimization' ), | |
| 121 | + __( 'Image Optimization', 'image-optimization' ), | |
| 97 | 122 | self::SETTING_CAPABILITY, |
| 98 | 123 | self::SETTING_BASE_SLUG, |
| 99 | 124 | [ $this, 'render_app' ], |
| 100 | - 6 | |
| 125 | + 50 | |
| 101 | 126 | ); |
| 127 | + | |
| 128 | + $this->add_menu_item_class( 'elementor-home', self::SETTING_BASE_SLUG, 'image-optimizer-menu' ); | |
| 102 | 129 | } |
| 103 | 130 | |
| 131 | + private function add_menu_item_class( string $parent_slug, string $menu_slug, string $class ) { | |
| 132 | + global $submenu; | |
| 133 | + | |
| 134 | + if ( ! isset( $submenu[ $parent_slug ] ) ) { | |
| 135 | + return; | |
| 136 | + } | |
| 137 | + | |
| 138 | + foreach ( $submenu[ $parent_slug ] as &$item ) { | |
| 139 | + if ( $item[2] === $menu_slug ) { | |
| 140 | + $item[4] = isset( $item[4] ) ? $item[4] . ' ' . $class : $class; | |
| 141 | + break; | |
| 142 | + } | |
| 143 | + } | |
| 144 | + } | |
| 145 | + | |
| 104 | 146 | /** |
| 105 | 147 | * The handler converts an old CONVERT_TO_WEBP option to the new CONVERT_TO_FORMAT option. |
| 106 | 148 | * TODO: [Stability] Remove this fallback after all users updated |
| 107 | 149 | * |
| @@ -124,8 +166,52 @@ | ||
| 124 | 166 | |
| 125 | 167 | delete_option( Settings::CONVERT_TO_WEBP_OPTION_NAME ); |
| 126 | 168 | } |
| 127 | 169 | |
| 170 | + /** | |
| 171 | + * The handler triggers stats recalculation on custom sizes update. | |
| 172 | + * | |
| 173 | + * @param $result | |
| 174 | + * @param $name | |
| 175 | + * | |
| 176 | + * @return void | |
| 177 | + */ | |
| 178 | + public function recalculate_stats_on_custom_sizes_update( $result, $name ) { | |
| 179 | + if ( Settings::CUSTOM_SIZES_OPTION_NAME === $name ) { | |
| 180 | + Optimization_Stats::get_image_stats( null, true ); | |
| 181 | + } | |
| 182 | + } | |
| 183 | + | |
| 184 | + public function cleanup_data() { | |
| 185 | + delete_transient( Client::SITE_INFO_TRANSIENT ); | |
| 186 | + delete_transient( Connect_Manager_Connect::STATUS_CHECK_TRANSIENT ); | |
| 187 | + } | |
| 188 | + | |
| 189 | + /** | |
| 190 | + * Register or update site data for One connect | |
| 191 | + * @throws Exception | |
| 192 | + */ | |
| 193 | + public function on_migration_run() { | |
| 194 | + $old_options = [ | |
| 195 | + 'image_optimizer_client_id', | |
| 196 | + 'image_optimizer_client_secret', | |
| 197 | + 'image_optimizer_home_url', | |
| 198 | + 'image_optimizer_access_token', | |
| 199 | + 'image_optimizer_token_id', | |
| 200 | + 'image_optimizer_refresh_token', | |
| 201 | + 'image_optimizer_user_access_token', | |
| 202 | + 'image_optimizer_owner_user_id', | |
| 203 | + 'image_optimizer_subscription_id', | |
| 204 | + Settings::SUBSCRIPTION_ID, | |
| 205 | + ]; | |
| 206 | + | |
| 207 | + $this->cleanup_data(); | |
| 208 | + | |
| 209 | + foreach ( $old_options as $option ) { | |
| 210 | + delete_option( $option ); | |
| 211 | + } | |
| 212 | + } | |
| 213 | + | |
| 128 | 214 | public function __construct() { |
| 129 | 215 | $this->register_components(); |
| 130 | 216 | |
| 131 | 217 | add_action( 'admin_init', [ $this, 'register_options' ] ); |
| @@ -130,7 +216,20 @@ | ||
| 130 | 216 | |
| 131 | 217 | add_action( 'admin_init', [ $this, 'register_options' ] ); |
| 132 | 218 | add_action( 'rest_api_init', [ $this, 'register_options' ] ); |
| 133 | 219 | add_action( 'admin_init', [ $this, 'maybe_migrate_legacy_conversion_option' ] ); |
| 134 | - add_action( 'admin_menu', [ $this, 'register_page' ] ); | |
| 220 | + add_action( 'admin_menu', [ $this, 'register_page' ], 99 ); | |
| 221 | + add_action( 'rest_pre_update_setting', [ $this, 'recalculate_stats_on_custom_sizes_update' ], 10, 2 ); | |
| 222 | + | |
| 223 | + add_action( 'elementor_one/' . Config::APP_PREFIX . '_connected', [ $this, 'cleanup_data' ] ); | |
| 224 | + add_action( 'elementor_one/' . Config::APP_PREFIX . '_disconnected', [ $this, 'cleanup_data' ] ); | |
| 225 | + add_action( 'elementor_one/' . Config::APP_PREFIX . '_migration_run', [ $this, 'on_migration_run' ] ); | |
| 226 | + | |
| 227 | + // Add action on switch domain for update access token | |
| 228 | + add_action( 'elementor_one/' . Config::APP_PREFIX . '_switched_domain', function( $facade ) { | |
| 229 | + $facade->service()->renew_access_token(); | |
| 230 | + } ); | |
| 231 | + add_action( 'elementor_one/switched_domain', function( $facade ) { | |
| 232 | + $facade->service()->renew_access_token(); | |
| 233 | + } ); | |
| 135 | 234 | } |
| 136 | 235 | } |