PluginProbe ʕ •ᴥ•ʔ
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization / 1.19.0
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization v1.19.0
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 4 months ago Settings 4 months ago Admin.php 4 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
426 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_flowbite_js', plugin_dir_url( NITROPACK_FILE ) . 'view/javascript/flowbite.min.js', array(), NITROPACK_VERSION, true );
172 wp_enqueue_script( 'nitropack_ui', plugin_dir_url( NITROPACK_FILE ) . 'view/javascript/nitropackUI.js', array(), NITROPACK_VERSION, true );
173
174 if ( get_nitropack()->isConnected() ) {
175 $passed_onboarding = get_option( 'nitropack-onboardingPassed' );
176 if ( ! $passed_onboarding ) {
177 wp_enqueue_script( 'nitropack_preview_site', plugin_dir_url( NITROPACK_FILE ) . 'view/javascript/preview_site.js', array( 'nitropack_flowbite_js' ), NITROPACK_VERSION, true );
178 wp_localize_script(
179 'nitropack_preview_site',
180 'np_onboarding',
181 array(
182 'nitro_plugin_url' => plugin_dir_url( NITROPACK_FILE ),
183 'select_mode' => esc_html__( 'Select Mode', 'nitropack' ),
184 'active_mode' => esc_html__( 'Active Mode', 'nitropack' ),
185 'switching_mode' => esc_html__( 'Switching Optimization Mode.', 'nitropack' ),
186 'est_cachewarmup_msg' => esc_html__( 'Estimating optimizations usage', 'nitropack' )
187 )
188 );
189 }
190 //dashboard page
191 if ( ! isset( $_GET['subpage'] ) ) {
192 wp_enqueue_script( 'nitropack_settings', plugin_dir_url( NITROPACK_FILE ) . 'view/javascript/np_settings.js', array(), NITROPACK_VERSION, true );
193 wp_localize_script(
194 'nitropack_settings',
195 'np_settings',
196 array_merge(
197 $this->localized_common_data(),
198 array(
199 'est_cachewarmup_msg' => esc_html__( 'Estimating optimizations usage', 'nitropack' ),
200 'quickSetupSaveUrl' => get_nitropack_integration_url( "quicksetup" ),
201 'testing_compression' => esc_html__( 'Testing current compression status', 'nitropack' ),
202 'compression_already_enabled' => esc_html__( 'Compression is already enabled on your server! There is no need to enable it in NitroPack.', 'nitropack' ),
203 'compression_not_detected' => esc_html__( 'No compression was detected! We will now enable it in NitroPack.', 'nitropack' ),
204 'compression_not_determined' => esc_html__( 'Could not determine compression status automatically. Please configure it manually.', 'nitropack' )
205 )
206 )
207 );
208 //select2
209 wp_register_script( 'np-select2', 'https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.9/js/select2.min.js', array( 'jquery' ), NITROPACK_VERSION, true );
210 wp_enqueue_script( 'np-select2' );
211 }
212 //system report page
213 if ( isset( $_GET['subpage'] ) && $_GET['subpage'] === 'system-report' ) {
214 wp_enqueue_script( 'nitropack_system_report', plugin_dir_url( NITROPACK_FILE ) . 'view/javascript/system_report.js', array(), NITROPACK_VERSION, true );
215 wp_localize_script(
216 'nitropack_system_report',
217 'np_system_report',
218 array_merge(
219 $this->localized_common_data(),
220 array(
221 'report_empty_options' => esc_html__( 'Please select at least one of the report options.', 'nitropack' ),
222 'report_success' => esc_html__( 'Report generated successfully.', 'nitropack' ),
223 'report_empty' => esc_html__( 'Response is empty. Report generation failed.', 'nitropack' ),
224 'report_error' => esc_html__( 'There was an error while generating the report.', 'nitropack' ),
225 )
226 )
227 );
228 }
229 }
230 }
231
232 /* Enqueue single post purge/invalidation script */
233 if ( get_nitropack()->isConnected() ) {
234 $CPTOptimization = Settings\CPTOptimization::getInstance();
235 $cacheableObjectTypes = $CPTOptimization->nitropack_get_cacheable_object_types();
236
237 $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;
238 $current_post_type = $screen && ! empty( $screen->post_type ) ? $screen->post_type : ( $GLOBALS['typenow'] ?? $GLOBALS['post_type'] ?? null );
239
240
241 /* Enqueue the post/page cache clearing script only on edit/add new post/page screens and only if the post type is cacheable. */
242 if ( in_array( $page, [ 'post.php', 'post-new.php', 'edit.php' ], true ) && in_array( $current_post_type, $cacheableObjectTypes, true ) ) {
243 $editor = get_option( "nitropack-canEditorClearCache" );
244 $allowed_capabilities = current_user_can( 'manage_options' ) || ( $editor && current_user_can( 'editor' ) );
245 if ( ! $allowed_capabilities ) {
246 return;
247 }
248 wp_enqueue_script( 'nitropack_post_clear_cache', NITROPACK_PLUGIN_DIR_URL . 'view/javascript/post_clear_cache.js?np_v=' . NITROPACK_VERSION, true );
249 wp_localize_script(
250 'nitropack_post_clear_cache',
251 'np_post_clear_cache',
252 array(
253 'nitroNonce' => wp_create_nonce( NITROPACK_NONCE ),
254 'nitro_plugin_url' => NITROPACK_PLUGIN_DIR_URL,
255 'working' => esc_html__( 'Working...', 'nitropack' ),
256 'success' => esc_html__( 'Success.', 'nitropack' ),
257 'error' => esc_html__( 'Error.', 'nitropack' )
258 )
259 );
260 }
261
262 }
263 // Elementor Tools page integration
264 if ( $page === 'elementor_page_elementor-tools' && get_nitropack()->isConnected() ) {
265 wp_enqueue_script(
266 'nitropack_elementor_integration',
267 plugin_dir_url( NITROPACK_FILE ) . 'view/javascript/elementor_cache_integration.js',
268 array( 'jquery' ),
269 NITROPACK_VERSION,
270 true
271 );
272
273 wp_localize_script(
274 'nitropack_elementor_integration',
275 'nitropack_elementor',
276 array(
277 'ajax_url' => admin_url( 'admin-ajax.php' ),
278 'nonce' => wp_create_nonce( 'nitropack_elementor_clear_cache' )
279 )
280 );
281 }
282 }
283
284 /**
285 * Render the admin topbar menu
286 * @param mixed $wp_admin_bar
287 * @return void
288 */
289 public function topbar_admin_menu( $wp_admin_bar ) {
290 if ( nitropack_is_amp_page() )
291 return;
292 $notifications = Notifications\Notifications::getInstance();
293 $counter_data = $notifications->admin_bar_notices_counter();
294
295 if ( ! get_nitropack()->isConnected() ) {
296 $node = array(
297 'id' => 'nitropack-top-menu',
298 'title' => '&nbsp;&nbsp;<i style="" class="circle nitro nitro-status nitro-status-not-connected" aria-hidden="true"></i>&nbsp;&nbsp;NitroPack is disconnected',
299 'href' => admin_url( 'admin.php?page=nitropack' ),
300 'meta' => array(
301 'class' => 'nitropack-menu'
302 )
303 );
304
305 $wp_admin_bar->add_node(
306 array(
307 'parent' => 'nitropack-top-menu',
308 'id' => 'optimizations-plugin-status',
309 'title' => __( 'Connect NitroPack&nbsp;&nbsp;', 'nitropack' ),
310 'href' => admin_url( 'admin.php?page=nitropack' ),
311 'meta' => array(
312 'class' => 'nitropack-plugin-status',
313 )
314 )
315 );
316 } else {
317 $title = '&nbsp;&nbsp;<i style="" class="circle nitro nitro-status nitro-status-' . $counter_data['status'] . '" aria-hidden="true"></i>&nbsp;&nbsp;NitroPack';
318 if ( $counter_data['status'] != "ok" ) {
319 $title .= ' <span class="circle-with-text nitro-color-issues" id="nitro-total-issues-count">' . ( $counter_data['issues'] + $counter_data['notifications'] ) . '</span>';
320 } else if ( $counter_data['notifications'] > 0 ) {
321 $title .= ' <span class="circle-with-text nitro-color-notification" id="nitro-total-issues-count">' . $counter_data['notifications'] . '</span>';
322 }
323 $node = array(
324 'id' => 'nitropack-top-menu',
325 'title' => $title,
326 'href' => admin_url( 'admin.php?page=nitropack' ),
327 'meta' => array(
328 'class' => 'nitropack-menu'
329 )
330 );
331
332 $settings_extra = '';
333 if ( $counter_data['notifications'] ) {
334 $settings_extra .= ' <span class="circle-with-text nitro-color-notification" id="nitro-notification-issues-count">' . $counter_data['notifications'] . '</span>';
335 }
336 $wp_admin_bar->add_node( array(
337 'id' => 'nitropack-top-menu-settings',
338 'parent' => 'nitropack-top-menu',
339 'title' => __( 'Settings', 'nitropack' ) . ' ' . $settings_extra . '',
340 'href' => admin_url( 'admin.php?page=nitropack' ),
341 'meta' => array(
342 'class' => 'nitropack-settings'
343 )
344
345 ) );
346
347 $wp_admin_bar->add_node(
348 array(
349 'id' => 'nitropack-top-menu-purge-entire-cache',
350 'parent' => 'nitropack-top-menu',
351 'title' => __( 'Purge Entire Cache', 'nitropack' ),
352 'href' => '#',
353 'meta' => array(
354 'class' => 'nitropack-purge-cache-entire-site',
355 ),
356 )
357 );
358 $wp_admin_bar->add_node(
359 array(
360 'id' => 'nitropack-top-menu-invalidate-entire-cache',
361 'parent' => 'nitropack-top-menu',
362 'title' => __( 'Invalidate Entire Cache', 'nitropack' ),
363 'href' => '#',
364 'meta' => array(
365 'class' => 'nitropack-invalidate-cache-entire-site',
366 ),
367 )
368 );
369
370 if ( ! is_admin() ) { // menu otions available when browsing front-end pages
371
372 $wp_admin_bar->add_node(
373 array(
374 'parent' => 'nitropack-top-menu',
375 'id' => 'optimizations-purge-cache',
376 'title' => __( 'Purge Current Page', 'nitropack' ),
377 'href' => "#",
378 'meta' => array(
379 'class' => 'nitropack-purge-cache',
380 )
381 )
382 );
383
384 $wp_admin_bar->add_node(
385 array(
386 'parent' => 'nitropack-top-menu',
387 'id' => 'optimizations-invalidate-cache',
388 'title' => __( 'Invalidate Current Page', 'nitropack' ),
389 'href' => "#",
390 'meta' => array(
391 'class' => 'nitropack-invalidate-cache',
392 )
393 )
394 );
395 }
396
397 if ( $counter_data['status'] != "ok" ) {
398 $wp_admin_bar->add_node(
399 array(
400 'parent' => 'nitropack-top-menu',
401 'id' => 'optimizations-plugin-status',
402 'title' => 'Issues <span class="circle-with-text nitro-color-issues">' . $counter_data['issues'] . '</span>',
403 'href' => admin_url( 'admin.php?page=nitropack' ),
404 'meta' => array(
405 'class' => 'nitropack-plugin-status',
406 )
407 )
408 );
409 }
410 }
411 $wp_admin_bar->add_node( $node );
412 }
413 public function nitropack_admin_bar_script( $hook ) {
414 if ( ! nitropack_is_amp_page() ) {
415 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 );
416 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 ) ) );
417 }
418 }
419
420
421 public function enqueue_topbar_admin_menu_stylesheet() {
422 if ( ! nitropack_is_amp_page() ) {
423 wp_enqueue_style( 'topbar_admin_menu_stylesheet', plugin_dir_url( NITROPACK_FILE ) . 'view/stylesheet/admin_bar_menu.min.css?np_v=' . NITROPACK_VERSION );
424 }
425 }
426 }