PluginProbe ʕ •ᴥ•ʔ
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization / 1.19.3
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization v1.19.3
1.19.8 1.19.7 1.19.6 1.19.5 trunk 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.11.0 1.12.0 1.13.0 1.14.0 1.15.0 1.15.1 1.15.2 1.15.3 1.16.0 1.16.1 1.16.2 1.16.3 1.16.4 1.16.5 1.16.6 1.16.7 1.16.8 1.17.0 1.17.6 1.17.7 1.17.8 1.17.9 1.18.0 1.18.1 1.18.2 1.18.3 1.18.4 1.18.5 1.18.6 1.18.7 1.18.8 1.18.9 1.19.0 1.19.1 1.19.2 1.19.3 1.19.4 1.3.19 1.3.20 1.4.0 1.4.1 1.5.0 1.5.1 1.5.10 1.5.11 1.5.12 1.5.13 1.5.14 1.5.15 1.5.16 1.5.17 1.5.18 1.5.19 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.5.9 1.6.0 1.6.1 1.7.0 1.7.1 1.8.0 1.8.1 1.8.3 1.9.0 1.9.1 1.9.2
nitropack / classes / WordPress / Admin.php
nitropack / classes / WordPress Last commit date
Notifications 3 months ago Settings 3 months ago Admin.php 3 months ago CLI.php 6 months ago Config.php 1 year ago ConflictingPlugins.php 10 months ago Cron.php 1 year ago Invalidations.php 4 months ago NitroPack.php 4 months ago Settings.php 4 months ago
Admin.php
424 lines
1 <?php
2
3 namespace NitroPack\WordPress;
4
5 /**
6 * Core WordPress admin functionality for NitroPack plugin
7 */
8 class Admin {
9
10 private static $instance = null;
11
12 private function __construct() {
13 add_action( 'admin_menu', [ $this, 'menu' ] );
14 add_filter( 'parent_file', [ $this, 'highlight_submenus' ] );
15 add_filter( 'plugin_action_links_' . NITROPACK_BASENAME, [ $this, 'nitropack_action_links' ] );
16 add_action( 'admin_enqueue_scripts', [ $this, 'load_nitropack_scripts_styles' ] );
17
18 //display admin topbar menu on non admin pages as well
19 add_action( 'init', function () {
20 if ( current_user_can( 'manage_options' ) ) {
21 // Enqueue admin bar menu custom stylesheet
22 add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_topbar_admin_menu_stylesheet' ] );
23 add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_topbar_admin_menu_stylesheet' ] );
24
25 // Enqueue admin menu custom javascript
26 add_action( 'wp_enqueue_scripts', [ $this, 'nitropack_admin_bar_script' ] );
27 add_action( 'admin_enqueue_scripts', [ $this, 'nitropack_admin_bar_script' ] );
28
29 // Add our admin menu bar entry
30 add_action( 'admin_bar_menu', [ $this, 'topbar_admin_menu' ], PHP_INT_MAX - 10 );
31 }
32 } );
33 }
34
35 public static function getInstance() {
36 if ( null === self::$instance ) {
37 self::$instance = new self();
38 }
39 return self::$instance;
40 }
41
42 /**
43 * Render NitroPack admin templates.
44 * Templates: Connect, Dashboard, System Report
45 */
46 public function templates() {
47 if ( ! current_user_can( 'manage_options' ) ) {
48 wp_die( __( 'You do not have sufficient permissions to access this page.', 'nitropack' ) );
49 }
50
51 if ( get_nitropack()->isConnected() ) {
52 $helpLabels = [ 'wordpress_plugin_help' ];
53 if ( get_nitropack()->getDistribution() == "oneclick" ) {
54 $helpLabels = [ 'wordpress_plugin_help_oneclick' ];
55 $oneClickVendorWidget = apply_filters( "nitropack_oneclick_vendor_widget", "" );
56 include plugin_dir_path( NITROPACK_FILE ) . nitropack_trailingslashit( 'view' ) . 'oneclick.php';
57 } else {
58 include plugin_dir_path( NITROPACK_FILE ) . nitropack_trailingslashit( 'view' ) . 'admin.php';
59 }
60
61 } else {
62 if ( get_nitropack()->getDistribution() == "oneclick" ) {
63 $oneClickConnectUrl = apply_filters( "nitropack_oneclick_connect_url", "" );
64 include plugin_dir_path( NITROPACK_FILE ) . nitropack_trailingslashit( 'view' ) . 'connect-oneclick.php';
65 } else {
66 include plugin_dir_path( NITROPACK_FILE ) . nitropack_trailingslashit( 'view' ) . 'connect.php';
67 }
68 }
69 }
70 /**
71 * Add submenu pages under NitroPack menu
72 * @return void
73 */
74 public function menu() {
75 global $submenu;
76
77 add_menu_page(
78 'NitroPack Options',
79 'NitroPack',
80 'manage_options',
81 'nitropack',
82 [ $this, 'templates' ],
83 'dashicons-performance',
84 25
85 );
86 if ( get_nitropack()->getDistribution() !== "oneclick" ) {
87 add_submenu_page(
88 'nitropack',
89 'System Report',
90 'System Report',
91 'manage_options',
92 'admin.php?page=nitropack&subpage=system-report'
93 );
94 }
95 if ( isset( $submenu['nitropack'] ) ) {
96 foreach ( $submenu['nitropack'] as &$item ) {
97 if ( $item[0] === 'NitroPack' ) {
98 $item[0] = 'Dashboard';
99 }
100 }
101 }
102 }
103
104 /**
105 * Hightlight submenu items when on a subpage
106 * @param mixed $parent_file
107 */
108 public function highlight_submenus( $parent_file ) {
109 if ( isset( $_GET['page'] ) && isset( $_GET['subpage'] ) ) {
110 global $submenu_file;
111 $submenu_file = 'admin.php?page=' . $_GET['page'] . '&subpage=' . $_GET['subpage'];
112 }
113
114 return $parent_file;
115 }
116
117 /**
118 * Display action links in Plugins => NitroPack
119 * @param mixed $links
120 * @return array
121 */
122 public function nitropack_action_links( $links ) {
123 $nitroLinks = array(
124 '<a href="https://support.nitropack.io/hc/en-us/categories/360005122034-Frequently-Asked-Questions-FAQs-" target="_blank" rel="noopener noreferrer">FAQ</a>',
125 '<a href="https://support.nitropack.io/hc/en-us" target="_blank" rel="noopener noreferrer">Docs</a>',
126 '<a href="https://support.nitropack.io/hc/en-us/requests/new" target="_blank" rel="noopener noreferrer">Support</a>',
127 );
128
129 if ( get_nitropack()->getDistribution() == "oneclick" ) {
130 $nitroLinks = apply_filters( "nitropack_oneclick_action_links", $nitroLinks );
131 }
132
133 array_unshift( $nitroLinks, '<a href="' . admin_url( 'admin.php?page=nitropack' ) . '" rel="noopener noreferrer">Settings</a>' );
134
135 return array_merge( $nitroLinks, $links );
136 }
137
138 /**
139 * Summary of localized_common_data
140 * @return array
141 */
142 private function localized_common_data() {
143 $data = [
144 'nitroNonce' => wp_create_nonce( NITROPACK_NONCE ),
145 'nitro_plugin_url' => plugin_dir_url( NITROPACK_FILE ),
146 'error_msg' => esc_html__( 'Something went wrong.', 'nitropack' ),
147 'success_msg' => esc_html__( 'Settings updated.', 'nitropack' ),
148 ];
149 return $data;
150 }
151 /**
152 * Load assets (js/css)
153 * @param mixed $page
154 * @return void
155 */
156 public function load_nitropack_scripts_styles( $page ) {
157 //global WP
158 wp_enqueue_style( 'nitropack-notifications', plugin_dir_url( NITROPACK_FILE ) . 'view/stylesheet/nitro-notifications.min.css', array(), NITROPACK_VERSION );
159 wp_enqueue_script( 'nitropack_notices_js', plugin_dir_url( NITROPACK_FILE ) . 'view/javascript/np_notices.js', array(), NITROPACK_VERSION, true );
160 wp_localize_script( 'nitropack_notices_js', 'nitropack_notices_vars', array(
161 'nonce' => wp_create_nonce( NITROPACK_NONCE ),
162 ) );
163 //plugin only
164 if ( $page === 'toplevel_page_nitropack' ) {
165 //css
166 wp_enqueue_style( 'nitropack', plugin_dir_url( NITROPACK_FILE ) . 'view/stylesheet/style.min.css', array(), NITROPACK_VERSION );
167 wp_enqueue_style( 'nitropack-connect', plugin_dir_url( NITROPACK_FILE ) . 'view/stylesheet/connect.min.css', array(), NITROPACK_VERSION );
168 //json animations
169 wp_enqueue_script( 'lottie', 'https://cdnjs.cloudflare.com/ajax/libs/lottie-web/5.12.2/lottie.min.js', array(), null, false );
170 //js
171 wp_enqueue_script( 'nitropack_ui', plugin_dir_url( NITROPACK_FILE ) . 'view/javascript/nitropackUI.js', array(), NITROPACK_VERSION, true );
172 if ( get_nitropack()->isConnected() ) {
173 $passed_onboarding = get_option( 'nitropack-onboardingPassed' );
174 if ( ! $passed_onboarding ) {
175 wp_enqueue_script( 'nitropack_preview_site', plugin_dir_url( NITROPACK_FILE ) . 'view/javascript/preview_site.js', array( 'nitropack_ui' ), NITROPACK_VERSION, true );
176 wp_localize_script(
177 'nitropack_preview_site',
178 'np_onboarding',
179 array(
180 'nitro_plugin_url' => plugin_dir_url( NITROPACK_FILE ),
181 'select_mode' => esc_html__( 'Select Mode', 'nitropack' ),
182 'active_mode' => esc_html__( 'Active Mode', 'nitropack' ),
183 'switching_mode' => esc_html__( 'Switching Optimization Mode.', 'nitropack' ),
184 'est_cachewarmup_msg' => esc_html__( 'Estimating optimizations usage', 'nitropack' )
185 )
186 );
187 }
188 //dashboard page
189 if ( ! isset( $_GET['subpage'] ) ) {
190 wp_enqueue_script( 'nitropack_settings', plugin_dir_url( NITROPACK_FILE ) . 'view/javascript/np_settings.js', array(), NITROPACK_VERSION, true );
191 wp_localize_script(
192 'nitropack_settings',
193 'np_settings',
194 array_merge(
195 $this->localized_common_data(),
196 array(
197 'est_cachewarmup_msg' => esc_html__( 'Estimating optimizations usage', 'nitropack' ),
198 'quickSetupSaveUrl' => get_nitropack_integration_url( "quicksetup" ),
199 'testing_compression' => esc_html__( 'Testing current compression status', 'nitropack' ),
200 'compression_already_enabled' => esc_html__( 'Compression is already enabled on your server! There is no need to enable it in NitroPack.', 'nitropack' ),
201 'compression_not_detected' => esc_html__( 'No compression was detected! We will now enable it in NitroPack.', 'nitropack' ),
202 'compression_not_determined' => esc_html__( 'Could not determine compression status automatically. Please configure it manually.', 'nitropack' )
203 )
204 )
205 );
206 //select2
207 wp_register_script( 'np-select2', 'https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.9/js/select2.min.js', array( 'jquery' ), NITROPACK_VERSION, true );
208 wp_enqueue_script( 'np-select2' );
209 }
210 //system report page
211 if ( isset( $_GET['subpage'] ) && $_GET['subpage'] === 'system-report' ) {
212 wp_enqueue_script( 'nitropack_system_report', plugin_dir_url( NITROPACK_FILE ) . 'view/javascript/system_report.js', array(), NITROPACK_VERSION, true );
213 wp_localize_script(
214 'nitropack_system_report',
215 'np_system_report',
216 array_merge(
217 $this->localized_common_data(),
218 array(
219 'report_empty_options' => esc_html__( 'Please select at least one of the report options.', 'nitropack' ),
220 'report_success' => esc_html__( 'Report generated successfully.', 'nitropack' ),
221 'report_empty' => esc_html__( 'Response is empty. Report generation failed.', 'nitropack' ),
222 'report_error' => esc_html__( 'There was an error while generating the report.', 'nitropack' ),
223 )
224 )
225 );
226 }
227 }
228 }
229
230 /* Enqueue single post purge/invalidation script */
231 if ( get_nitropack()->isConnected() ) {
232 $CPTOptimization = Settings\CPTOptimization::getInstance();
233 $cacheableObjectTypes = $CPTOptimization->nitropack_get_cacheable_object_types();
234
235 $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;
236 $current_post_type = $screen && ! empty( $screen->post_type ) ? $screen->post_type : ( $GLOBALS['typenow'] ?? $GLOBALS['post_type'] ?? null );
237
238
239 /* Enqueue the post/page cache clearing script only on edit/add new post/page screens and only if the post type is cacheable. */
240 if ( in_array( $page, [ 'post.php', 'post-new.php', 'edit.php' ], true ) && in_array( $current_post_type, $cacheableObjectTypes, true ) ) {
241 $editor = get_option( "nitropack-canEditorClearCache" );
242 $allowed_capabilities = current_user_can( 'manage_options' ) || ( $editor && current_user_can( 'editor' ) );
243 if ( ! $allowed_capabilities ) {
244 return;
245 }
246 wp_enqueue_script( 'nitropack_post_clear_cache', NITROPACK_PLUGIN_DIR_URL . 'view/javascript/post_clear_cache.js?np_v=' . NITROPACK_VERSION, true );
247 wp_localize_script(
248 'nitropack_post_clear_cache',
249 'np_post_clear_cache',
250 array(
251 'nitroNonce' => wp_create_nonce( NITROPACK_NONCE ),
252 'nitro_plugin_url' => NITROPACK_PLUGIN_DIR_URL,
253 'working' => esc_html__( 'Working...', 'nitropack' ),
254 'success' => esc_html__( 'Success.', 'nitropack' ),
255 'error' => esc_html__( 'Error.', 'nitropack' )
256 )
257 );
258 }
259
260 }
261 // Elementor Tools page integration
262 if ( $page === 'elementor_page_elementor-tools' && get_nitropack()->isConnected() ) {
263 wp_enqueue_script(
264 'nitropack_elementor_integration',
265 plugin_dir_url( NITROPACK_FILE ) . 'view/javascript/elementor_cache_integration.js',
266 array( 'jquery' ),
267 NITROPACK_VERSION,
268 true
269 );
270
271 wp_localize_script(
272 'nitropack_elementor_integration',
273 'nitropack_elementor',
274 array(
275 'ajax_url' => admin_url( 'admin-ajax.php' ),
276 'nonce' => wp_create_nonce( 'nitropack_elementor_clear_cache' )
277 )
278 );
279 }
280 }
281
282 /**
283 * Render the admin topbar menu
284 * @param mixed $wp_admin_bar
285 * @return void
286 */
287 public function topbar_admin_menu( $wp_admin_bar ) {
288 if ( nitropack_is_amp_page() )
289 return;
290 $notifications = Notifications\Notifications::getInstance();
291 $counter_data = $notifications->admin_bar_notices_counter();
292
293 if ( ! get_nitropack()->isConnected() ) {
294 $node = array(
295 'id' => 'nitropack-top-menu',
296 'title' => '&nbsp;&nbsp;<i style="" class="circle nitro nitro-status nitro-status-not-connected" aria-hidden="true"></i>&nbsp;&nbsp;NitroPack is disconnected',
297 'href' => admin_url( 'admin.php?page=nitropack' ),
298 'meta' => array(
299 'class' => 'nitropack-menu'
300 )
301 );
302
303 $wp_admin_bar->add_node(
304 array(
305 'parent' => 'nitropack-top-menu',
306 'id' => 'optimizations-plugin-status',
307 'title' => __( 'Connect NitroPack&nbsp;&nbsp;', 'nitropack' ),
308 'href' => admin_url( 'admin.php?page=nitropack' ),
309 'meta' => array(
310 'class' => 'nitropack-plugin-status',
311 )
312 )
313 );
314 } else {
315 $title = '&nbsp;&nbsp;<i style="" class="circle nitro nitro-status nitro-status-' . $counter_data['status'] . '" aria-hidden="true"></i>&nbsp;&nbsp;NitroPack';
316 if ( $counter_data['status'] != "ok" ) {
317 $title .= ' <span class="circle-with-text nitro-color-issues" id="nitro-total-issues-count">' . ( $counter_data['issues'] + $counter_data['notifications'] ) . '</span>';
318 } else if ( $counter_data['notifications'] > 0 ) {
319 $title .= ' <span class="circle-with-text nitro-color-notification" id="nitro-total-issues-count">' . $counter_data['notifications'] . '</span>';
320 }
321 $node = array(
322 'id' => 'nitropack-top-menu',
323 'title' => $title,
324 'href' => admin_url( 'admin.php?page=nitropack' ),
325 'meta' => array(
326 'class' => 'nitropack-menu'
327 )
328 );
329
330 $settings_extra = '';
331 if ( $counter_data['notifications'] ) {
332 $settings_extra .= ' <span class="circle-with-text nitro-color-notification" id="nitro-notification-issues-count">' . $counter_data['notifications'] . '</span>';
333 }
334 $wp_admin_bar->add_node( array(
335 'id' => 'nitropack-top-menu-settings',
336 'parent' => 'nitropack-top-menu',
337 'title' => __( 'Settings', 'nitropack' ) . ' ' . $settings_extra . '',
338 'href' => admin_url( 'admin.php?page=nitropack' ),
339 'meta' => array(
340 'class' => 'nitropack-settings'
341 )
342
343 ) );
344
345 $wp_admin_bar->add_node(
346 array(
347 'id' => 'nitropack-top-menu-purge-entire-cache',
348 'parent' => 'nitropack-top-menu',
349 'title' => __( 'Purge Entire Cache', 'nitropack' ),
350 'href' => '#',
351 'meta' => array(
352 'class' => 'nitropack-purge-cache-entire-site',
353 ),
354 )
355 );
356 $wp_admin_bar->add_node(
357 array(
358 'id' => 'nitropack-top-menu-invalidate-entire-cache',
359 'parent' => 'nitropack-top-menu',
360 'title' => __( 'Invalidate Entire Cache', 'nitropack' ),
361 'href' => '#',
362 'meta' => array(
363 'class' => 'nitropack-invalidate-cache-entire-site',
364 ),
365 )
366 );
367
368 if ( ! is_admin() ) { // menu otions available when browsing front-end pages
369
370 $wp_admin_bar->add_node(
371 array(
372 'parent' => 'nitropack-top-menu',
373 'id' => 'optimizations-purge-cache',
374 'title' => __( 'Purge Current Page', 'nitropack' ),
375 'href' => "#",
376 'meta' => array(
377 'class' => 'nitropack-purge-cache',
378 )
379 )
380 );
381
382 $wp_admin_bar->add_node(
383 array(
384 'parent' => 'nitropack-top-menu',
385 'id' => 'optimizations-invalidate-cache',
386 'title' => __( 'Invalidate Current Page', 'nitropack' ),
387 'href' => "#",
388 'meta' => array(
389 'class' => 'nitropack-invalidate-cache',
390 )
391 )
392 );
393 }
394
395 if ( $counter_data['status'] != "ok" ) {
396 $wp_admin_bar->add_node(
397 array(
398 'parent' => 'nitropack-top-menu',
399 'id' => 'optimizations-plugin-status',
400 'title' => 'Issues <span class="circle-with-text nitro-color-issues">' . $counter_data['issues'] . '</span>',
401 'href' => admin_url( 'admin.php?page=nitropack' ),
402 'meta' => array(
403 'class' => 'nitropack-plugin-status',
404 )
405 )
406 );
407 }
408 }
409 $wp_admin_bar->add_node( $node );
410 }
411 public function nitropack_admin_bar_script( $hook ) {
412 if ( ! nitropack_is_amp_page() ) {
413 wp_enqueue_script( 'topbar_admin_menu_script', plugin_dir_url( NITROPACK_FILE ) . 'view/javascript/admin_bar_menu.js?np_v=' . NITROPACK_VERSION, [ 'jquery' ], false, true );
414 wp_localize_script( 'topbar_admin_menu_script', 'frontendajax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ), 'nitroNonce' => wp_create_nonce( NITROPACK_NONCE ), 'nitro_plugin_url' => plugin_dir_url( NITROPACK_FILE ) ) );
415 }
416 }
417
418
419 public function enqueue_topbar_admin_menu_stylesheet() {
420 if ( ! nitropack_is_amp_page() ) {
421 wp_enqueue_style( 'topbar_admin_menu_stylesheet', plugin_dir_url( NITROPACK_FILE ) . 'view/stylesheet/admin_bar_menu.min.css?np_v=' . NITROPACK_VERSION );
422 }
423 }
424 }