image-optimization
/
vendor
/
elementor
/
wp-one-package
/
src
/
Admin
/
Components
/
EditorUpdateNotification.php
EditorUpdateNotification.php in Image Optimizer – Compress Images and Convert to WebP or AVIF 1.7.5, at vendor/elementor/wp-one-package/src/Admin/Components/EditorUpdateNotification.php
| 1 | <?php |
| 2 | |
| 3 | namespace ElementorOne\Admin\Components; |
| 4 | |
| 5 | use ElementorOne\Common\SupportedPlugins; |
| 6 | |
| 7 | if ( ! defined( 'ABSPATH' ) ) { |
| 8 | exit; // Exit if accessed directly |
| 9 | } |
| 10 | |
| 11 | /** |
| 12 | * Class EditorUpdateNotification |
| 13 | * Handles the editor update notification |
| 14 | */ |
| 15 | class EditorUpdateNotification { |
| 16 | |
| 17 | const DISMISSED_OPTION_NAME = Fields::SETTING_PREFIX . 'editor_update_notification_dismissed'; |
| 18 | const SHOWN_NONCE_META_KEY = Fields::SETTING_PREFIX . 'editor_update_notification_shown_nonce'; |
| 19 | const EDITOR_PLUGIN_FILE = 'elementor/elementor.php'; |
| 20 | |
| 21 | /** |
| 22 | * Instance |
| 23 | * |
| 24 | * @var EditorUpdateNotification|null |
| 25 | */ |
| 26 | private static ?EditorUpdateNotification $instance = null; |
| 27 | |
| 28 | /** |
| 29 | * Get instance |
| 30 | * |
| 31 | * @return EditorUpdateNotification|null |
| 32 | */ |
| 33 | public static function instance(): ?EditorUpdateNotification { |
| 34 | if ( ! self::$instance ) { |
| 35 | self::$instance = new self(); |
| 36 | } |
| 37 | return self::$instance; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Constructor |
| 42 | * |
| 43 | * @return void |
| 44 | */ |
| 45 | private function __construct() { |
| 46 | add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_scripts' ] ); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Check if the Editor plugin is installed |
| 51 | * |
| 52 | * @return bool |
| 53 | */ |
| 54 | public function is_editor_installed(): bool { |
| 55 | if ( ! function_exists( 'get_plugins' ) ) { |
| 56 | require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 57 | } |
| 58 | |
| 59 | $all_plugins = get_plugins(); |
| 60 | |
| 61 | return isset( $all_plugins[ self::EDITOR_PLUGIN_FILE ] ); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Check if the Editor plugin is activated |
| 66 | * |
| 67 | * @return bool |
| 68 | */ |
| 69 | public function is_editor_activated(): bool { |
| 70 | if ( ! function_exists( 'is_plugin_active' ) ) { |
| 71 | require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 72 | } |
| 73 | |
| 74 | return is_plugin_active( self::EDITOR_PLUGIN_FILE ); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Check if the editor plugin has wp-one-package installed |
| 79 | * |
| 80 | * @return bool |
| 81 | */ |
| 82 | public function editor_has_wp_one_package(): bool { |
| 83 | global $wp_one_package_versions; |
| 84 | |
| 85 | return isset( $wp_one_package_versions[ SupportedPlugins::ELEMENTOR ] ); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Check if update notification has been dismissed |
| 90 | * |
| 91 | * @return bool |
| 92 | */ |
| 93 | public function is_notification_dismissed(): bool { |
| 94 | return (bool) get_option( self::DISMISSED_OPTION_NAME, false ); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Check if already shown recently |
| 99 | * |
| 100 | * @return bool |
| 101 | */ |
| 102 | private function is_shown_recently(): bool { |
| 103 | $user_id = get_current_user_id(); |
| 104 | |
| 105 | if ( ! $user_id ) { |
| 106 | return false; |
| 107 | } |
| 108 | |
| 109 | $stored_nonce = get_user_meta( $user_id, self::SHOWN_NONCE_META_KEY, true ); |
| 110 | |
| 111 | if ( empty( $stored_nonce ) ) { |
| 112 | return false; |
| 113 | } |
| 114 | |
| 115 | return false !== wp_verify_nonce( $stored_nonce, self::SHOWN_NONCE_META_KEY ); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Mark as shown |
| 120 | * |
| 121 | * @return void |
| 122 | */ |
| 123 | private function mark_as_shown(): void { |
| 124 | $user_id = get_current_user_id(); |
| 125 | |
| 126 | if ( ! $user_id ) { |
| 127 | return; |
| 128 | } |
| 129 | |
| 130 | update_user_meta( |
| 131 | $user_id, |
| 132 | self::SHOWN_NONCE_META_KEY, |
| 133 | wp_create_nonce( self::SHOWN_NONCE_META_KEY ) |
| 134 | ); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Check if we should show the update notification |
| 139 | * |
| 140 | * @return bool |
| 141 | */ |
| 142 | public function should_show_notification(): bool { |
| 143 | if ( $this->is_shown_recently() ) { |
| 144 | return false; |
| 145 | } |
| 146 | |
| 147 | if ( $this->is_notification_dismissed() ) { |
| 148 | return false; |
| 149 | } |
| 150 | |
| 151 | if ( ! $this->is_editor_installed() ) { |
| 152 | return false; |
| 153 | } |
| 154 | |
| 155 | if ( ! $this->is_editor_activated() ) { |
| 156 | return false; |
| 157 | } |
| 158 | |
| 159 | return ! $this->editor_has_wp_one_package(); |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Check if current page is an Elementor page |
| 164 | * |
| 165 | * @return bool |
| 166 | */ |
| 167 | private function is_elementor_page(): bool { |
| 168 | $current_screen = get_current_screen(); |
| 169 | |
| 170 | if ( ! $current_screen ) { |
| 171 | return false; |
| 172 | } |
| 173 | |
| 174 | $screen_id = $current_screen->id ?? ''; |
| 175 | $post_type = $current_screen->post_type ?? ''; |
| 176 | |
| 177 | $is_elementor_screen = strpos( $screen_id, 'elementor' ) !== false |
| 178 | || strpos( $screen_id, 'e-floating-buttons' ) !== false; |
| 179 | |
| 180 | $is_elementor_post_type = strpos( $post_type, 'elementor' ) !== false |
| 181 | || strpos( $post_type, 'e-floating-buttons' ) !== false; |
| 182 | |
| 183 | return $is_elementor_screen || $is_elementor_post_type; |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Maybe enqueue editor update notification assets |
| 188 | * |
| 189 | * @return void |
| 190 | */ |
| 191 | public function enqueue_scripts() { |
| 192 | if ( ! $this->is_elementor_page() ) { |
| 193 | return; |
| 194 | } |
| 195 | |
| 196 | if ( ! $this->should_show_notification() ) { |
| 197 | return; |
| 198 | } |
| 199 | |
| 200 | $asset_file = ELEMENTOR_ONE_ASSETS_PATH . 'editor.asset.php'; |
| 201 | $asset = file_exists( $asset_file ) ? include $asset_file : [ |
| 202 | 'dependencies' => [ 'wp-element', 'wp-i18n', 'wp-data', 'wp-core-data' ], |
| 203 | 'version' => '1.0.0', |
| 204 | ]; |
| 205 | |
| 206 | wp_enqueue_script( |
| 207 | 'elementor-one-editor-update-notification', |
| 208 | ELEMENTOR_ONE_ASSETS_URL . 'editor.js', |
| 209 | $asset['dependencies'], |
| 210 | $asset['version'], |
| 211 | true |
| 212 | ); |
| 213 | |
| 214 | wp_localize_script( |
| 215 | 'elementor-one-editor-update-notification', |
| 216 | 'elementorOneEditorUpdateNotification', |
| 217 | [ |
| 218 | 'pluginsUrl' => admin_url( 'plugins.php' ), |
| 219 | ] |
| 220 | ); |
| 221 | |
| 222 | $this->mark_as_shown(); |
| 223 | } |
| 224 | } |
| 225 |