PluginProbe
Image Optimizer – Compress Images and Convert to WebP or AVIF / 1.7.4
Image Optimizer – Compress Images and Convert to WebP or AVIF v1.7.4
1.7.7 1.7.6 1.7.5 1.7.4 trunk 1.0.0 1.0.1 1.0.2 1.1.0 1.2.0 1.2.1 1.3.0 1.4.0 1.4.1 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 All 33 releases
image-optimization / vendor / elementor / wp-one-package / src / Admin / Components / Page.php

Page.php in Image Optimizer – Compress Images and Convert to WebP or AVIF 1.7.4, at vendor/elementor/wp-one-package/src/Admin/Components/Page.php

192 lines 4.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 // phpcs:ignoreFile WordPress.WP.I18n.TextDomainMismatch
3
4 namespace ElementorOne\Admin\Components;
5
6 use ElementorOne\Admin\Helpers\Utils;
7
8 if ( ! defined( 'ABSPATH' ) ) {
9 exit; // Exit if accessed directly.
10 }
11
12 /**
13 * Class Page
14 * Handles admin menu registration, page rendering, and related filters
15 */
16 class Page {
17
18 const UPGRADE_PAGE_SLUG = 'elementor-one-upgrade';
19 const UPGRADE_PAGE_URL = 'https://go.elementor.com/go-pro-upgrade-one-wp-menu/';
20
21 /**
22 * Instance
23 * @var Page|null
24 */
25 private static ?Page $instance = null;
26
27 /**
28 * Get instance
29 * @return Page|null
30 */
31 public static function instance(): ?Page {
32 if ( ! self::$instance ) {
33 self::$instance = new self();
34 }
35 return self::$instance;
36 }
37
38 /**
39 * Get admin page
40 * @return string
41 */
42 private function get_admin_page(): string {
43 return Utils::get_one_connect()->get_config( 'admin_page' );
44 }
45
46 /**
47 * Render top bar
48 */
49 public function render_top_bar() {
50 global $current_screen;
51
52 if ( "toplevel_page_{$this->get_admin_page()}" === $current_screen->id ) {
53 ?>
54 <div id="elementor-home-app-top-bar"></div>
55 <?php
56
57 // Remove all admin header actions
58 remove_all_actions( 'network_admin_notices' );
59 remove_all_actions( 'user_admin_notices' );
60 remove_all_actions( 'all_admin_notices' );
61 remove_all_actions( 'admin_notices' );
62 }
63 }
64
65 /**
66 * Render app
67 */
68 public function render_app() {
69 ?>
70 <div id="elementor-home-app"></div>
71 <?php
72 }
73
74 /**
75 * Register page
76 */
77 public function register_page() {
78 global $submenu;
79
80 $admin_page = $this->get_admin_page();
81
82 // Add main menu item
83 add_menu_page(
84 __( 'Elementor', 'elementor' ),
85 __( 'Elementor', 'elementor' ),
86 'manage_options',
87 $admin_page,
88 [ $this, 'render_app' ],
89 '',
90 2
91 );
92
93 if ( current_user_can( 'manage_options' ) ) {
94 $submenu[ $admin_page ][0] = [
95 __( 'Home', 'elementor' ),
96 'manage_options',
97 "admin.php?page={$admin_page}",
98 ];
99 }
100 }
101
102 /**
103 * Add upgrade menu item
104 * @return void
105 */
106 public function upgrade_menu_item() {
107 $is_one_connected = Utils::get_one_connect()->utils()->is_connected();
108 $upgrade_available = apply_filters( 'elementor_one/upgrade_available', ! $is_one_connected );
109
110 if ( ! $upgrade_available ) {
111 return;
112 }
113
114 $admin_page = $this->get_admin_page();
115
116 add_submenu_page(
117 $admin_page,
118 '',
119 __( 'Upgrade', 'elementor' ),
120 'manage_options',
121 self::UPGRADE_PAGE_SLUG,
122 '__return_empty_string',
123 999
124 );
125 }
126
127 /**
128 * Submenu file filter
129 * @param string $submenu_file
130 * @return string
131 */
132 public function submenu_file_filter( $submenu_file ) {
133 global $parent_file, $current_screen;
134
135 $admin_page = $this->get_admin_page();
136
137 if ( "toplevel_page_{$admin_page}" === $current_screen->id ) {
138 $parent_file = $admin_page;
139 $submenu_file = "admin.php?page={$admin_page}";
140 }
141
142 return $submenu_file;
143 }
144
145 /**
146 * Disable Elementor top bar
147 * @param bool $is_active
148 * @param WP_Screen $current_screen
149 * @return bool
150 */
151 public function disable_elementor_top_bar( $is_active, $current_screen ) {
152 if ( "toplevel_page_{$this->get_admin_page()}" === $current_screen->id ) {
153 return false;
154 }
155 return $is_active;
156 }
157
158 /**
159 * Handle upgrade redirect
160 * @return void
161 */
162 public function handle_upgrade_redirect() {
163 global $plugin_page;
164
165 if ( self::UPGRADE_PAGE_SLUG === $plugin_page ) {
166 wp_redirect( self::get_upgrade_url() ); // phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect
167 exit;
168 }
169 }
170
171 /**
172 * Get the Elementor One upgrade URL.
173 * @return string
174 */
175 public static function get_upgrade_url(): string {
176 return apply_filters( 'elementor_one/upgrade_url', self::UPGRADE_PAGE_URL );
177 }
178
179 /**
180 * Page constructor
181 * @return void
182 */
183 private function __construct() {
184 add_action( 'admin_menu', [ $this, 'register_page' ], 1 );
185 add_action( 'admin_menu', [ $this, 'upgrade_menu_item' ], PHP_INT_MAX );
186 add_action( 'in_admin_header', [ $this, 'render_top_bar' ], 1 );
187 add_action( 'admin_init', [ $this, 'handle_upgrade_redirect' ] );
188 add_filter( 'submenu_file', [ $this, 'submenu_file_filter' ] );
189 add_filter( 'elementor/admin-top-bar/is-active', [ $this, 'disable_elementor_top_bar' ], 10, 2 );
190 }
191 }
192