PluginProbe ʕ •ᴥ•ʔ
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization / 1.19.6
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization v1.19.6
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 2 months ago Settings 1 month ago Admin.php 1 month ago CLI.php 2 weeks ago Config.php 1 year ago ConflictingPlugins.php 10 months ago Connect.php 2 weeks ago Cron.php 1 year ago Invalidations.php 2 months ago NitroPack.php 2 weeks ago Settings.php 4 months ago
Admin.php
423 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 ) . 'assets/css/nitro-notifications.min.css', array(), NITROPACK_VERSION );
159 wp_enqueue_script( 'nitropack_notices_js', plugin_dir_url( NITROPACK_FILE ) . 'assets/js/np_notices.min.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 ) . 'assets/css/style.min.css', array(), NITROPACK_VERSION );
167 wp_enqueue_style( 'nitropack-connect', plugin_dir_url( NITROPACK_FILE ) . 'assets/css/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 ) . 'assets/js/nitropackUI.min.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 ) . 'assets/js/preview_site.min.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_style( 'np-select', plugin_dir_url( NITROPACK_FILE ) . 'assets/css/np_select2.min.css', array( 'nitropack' ), NITROPACK_VERSION );
191 wp_enqueue_script( 'np-select', plugin_dir_url( NITROPACK_FILE ) . 'assets/js/np_select2.min.js', array( 'jquery' ), NITROPACK_VERSION, true );
192 wp_enqueue_script( 'nitropack_settings', plugin_dir_url( NITROPACK_FILE ) . 'assets/js/np_settings.min.js', array( 'np-select' ), 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 }
209 //system report page
210 if ( isset( $_GET['subpage'] ) && $_GET['subpage'] === 'system-report' ) {
211 wp_enqueue_script( 'nitropack_system_report', plugin_dir_url( NITROPACK_FILE ) . 'assets/js/system_report.min.js', array(), NITROPACK_VERSION, true );
212 wp_localize_script(
213 'nitropack_system_report',
214 'np_system_report',
215 array_merge(
216 $this->localized_common_data(),
217 array(
218 'report_empty_options' => esc_html__( 'Please select at least one of the report options.', 'nitropack' ),
219 'report_success' => esc_html__( 'Report generated successfully.', 'nitropack' ),
220 'report_empty' => esc_html__( 'Response is empty. Report generation failed.', 'nitropack' ),
221 'report_error' => esc_html__( 'There was an error while generating the report.', 'nitropack' ),
222 )
223 )
224 );
225 }
226 }
227 }
228
229 /* Enqueue single post purge/invalidation script */
230 if ( get_nitropack()->isConnected() ) {
231 $CPTOptimization = Settings\CPTOptimization::getInstance();
232 $cacheableObjectTypes = $CPTOptimization->nitropack_get_cacheable_object_types();
233
234 $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;
235 $current_post_type = $screen && ! empty( $screen->post_type ) ? $screen->post_type : ( $GLOBALS['typenow'] ?? $GLOBALS['post_type'] ?? null );
236
237
238 /* Enqueue the post/page cache clearing script only on edit/add new post/page screens and only if the post type is cacheable. */
239 if ( in_array( $page, [ 'post.php', 'post-new.php', 'edit.php' ], true ) && in_array( $current_post_type, $cacheableObjectTypes, true ) ) {
240 $editor = get_option( "nitropack-canEditorClearCache" );
241 $allowed_capabilities = current_user_can( 'manage_options' ) || ( $editor && current_user_can( 'editor' ) );
242 if ( ! $allowed_capabilities ) {
243 return;
244 }
245 wp_enqueue_script( 'nitropack_post_clear_cache', NITROPACK_PLUGIN_DIR_URL . 'assets/js/post_clear_cache.min.js?np_v=' . NITROPACK_VERSION, true );
246 wp_localize_script(
247 'nitropack_post_clear_cache',
248 'np_post_clear_cache',
249 array(
250 'nitroNonce' => wp_create_nonce( NITROPACK_NONCE ),
251 'nitro_plugin_url' => NITROPACK_PLUGIN_DIR_URL,
252 'working' => esc_html__( 'Working...', 'nitropack' ),
253 'success' => esc_html__( 'Success.', 'nitropack' ),
254 'error' => esc_html__( 'Error.', 'nitropack' )
255 )
256 );
257 }
258
259 }
260 // Elementor Tools page integration
261 if ( $page === 'elementor_page_elementor-tools' && get_nitropack()->isConnected() ) {
262 wp_enqueue_script(
263 'nitropack_elementor_integration',
264 plugin_dir_url( NITROPACK_FILE ) . 'assets/js/elementor_cache_integration.min.js',
265 array( 'jquery' ),
266 NITROPACK_VERSION,
267 true
268 );
269
270 wp_localize_script(
271 'nitropack_elementor_integration',
272 'nitropack_elementor',
273 array(
274 'ajax_url' => admin_url( 'admin-ajax.php' ),
275 'nonce' => wp_create_nonce( 'nitropack_elementor_clear_cache' )
276 )
277 );
278 }
279 }
280
281 /**
282 * Render the admin topbar menu
283 * @param mixed $wp_admin_bar
284 * @return void
285 */
286 public function topbar_admin_menu( $wp_admin_bar ) {
287 if ( nitropack_is_amp_page() )
288 return;
289 $notifications = Notifications\Notifications::getInstance();
290 $counter_data = $notifications->admin_bar_notices_counter();
291
292 if ( ! get_nitropack()->isConnected() ) {
293 $node = array(
294 'id' => 'nitropack-top-menu',
295 'title' => '&nbsp;&nbsp;<i style="" class="circle nitro nitro-status nitro-status-not-connected" aria-hidden="true"></i>&nbsp;&nbsp;NitroPack is disconnected',
296 'href' => admin_url( 'admin.php?page=nitropack' ),
297 'meta' => array(
298 'class' => 'nitropack-menu'
299 )
300 );
301
302 $wp_admin_bar->add_node(
303 array(
304 'parent' => 'nitropack-top-menu',
305 'id' => 'optimizations-plugin-status',
306 'title' => __( 'Connect NitroPack&nbsp;&nbsp;', 'nitropack' ),
307 'href' => admin_url( 'admin.php?page=nitropack' ),
308 'meta' => array(
309 'class' => 'nitropack-plugin-status',
310 )
311 )
312 );
313 } else {
314 $title = '&nbsp;&nbsp;<i style="" class="circle nitro nitro-status nitro-status-' . $counter_data['status'] . '" aria-hidden="true"></i>&nbsp;&nbsp;NitroPack';
315 if ( $counter_data['status'] != "ok" ) {
316 $title .= ' <span class="circle-with-text nitro-color-issues" id="nitro-total-issues-count">' . ( $counter_data['issues'] + $counter_data['notifications'] ) . '</span>';
317 } else if ( $counter_data['notifications'] > 0 ) {
318 $title .= ' <span class="circle-with-text nitro-color-notification" id="nitro-total-issues-count">' . $counter_data['notifications'] . '</span>';
319 }
320 $node = array(
321 'id' => 'nitropack-top-menu',
322 'title' => $title,
323 'href' => admin_url( 'admin.php?page=nitropack' ),
324 'meta' => array(
325 'class' => 'nitropack-menu'
326 )
327 );
328
329 $settings_extra = '';
330 if ( $counter_data['notifications'] ) {
331 $settings_extra .= ' <span class="circle-with-text nitro-color-notification" id="nitro-notification-issues-count">' . $counter_data['notifications'] . '</span>';
332 }
333 $wp_admin_bar->add_node( array(
334 'id' => 'nitropack-top-menu-settings',
335 'parent' => 'nitropack-top-menu',
336 'title' => __( 'Settings', 'nitropack' ) . ' ' . $settings_extra . '',
337 'href' => admin_url( 'admin.php?page=nitropack' ),
338 'meta' => array(
339 'class' => 'nitropack-settings'
340 )
341
342 ) );
343
344 $wp_admin_bar->add_node(
345 array(
346 'id' => 'nitropack-top-menu-purge-entire-cache',
347 'parent' => 'nitropack-top-menu',
348 'title' => __( 'Purge Entire Cache', 'nitropack' ),
349 'href' => '#',
350 'meta' => array(
351 'class' => 'nitropack-purge-cache-entire-site',
352 ),
353 )
354 );
355 $wp_admin_bar->add_node(
356 array(
357 'id' => 'nitropack-top-menu-invalidate-entire-cache',
358 'parent' => 'nitropack-top-menu',
359 'title' => __( 'Invalidate Entire Cache', 'nitropack' ),
360 'href' => '#',
361 'meta' => array(
362 'class' => 'nitropack-invalidate-cache-entire-site',
363 ),
364 )
365 );
366
367 if ( ! is_admin() ) { // menu otions available when browsing front-end pages
368
369 $wp_admin_bar->add_node(
370 array(
371 'parent' => 'nitropack-top-menu',
372 'id' => 'optimizations-purge-cache',
373 'title' => __( 'Purge Current Page', 'nitropack' ),
374 'href' => "#",
375 'meta' => array(
376 'class' => 'nitropack-purge-cache',
377 )
378 )
379 );
380
381 $wp_admin_bar->add_node(
382 array(
383 'parent' => 'nitropack-top-menu',
384 'id' => 'optimizations-invalidate-cache',
385 'title' => __( 'Invalidate Current Page', 'nitropack' ),
386 'href' => "#",
387 'meta' => array(
388 'class' => 'nitropack-invalidate-cache',
389 )
390 )
391 );
392 }
393
394 if ( $counter_data['status'] != "ok" ) {
395 $wp_admin_bar->add_node(
396 array(
397 'parent' => 'nitropack-top-menu',
398 'id' => 'optimizations-plugin-status',
399 'title' => 'Issues <span class="circle-with-text nitro-color-issues">' . $counter_data['issues'] . '</span>',
400 'href' => admin_url( 'admin.php?page=nitropack' ),
401 'meta' => array(
402 'class' => 'nitropack-plugin-status',
403 )
404 )
405 );
406 }
407 }
408 $wp_admin_bar->add_node( $node );
409 }
410 public function nitropack_admin_bar_script( $hook ) {
411 if ( ! nitropack_is_amp_page() ) {
412 wp_enqueue_script( 'topbar_admin_menu_script', plugin_dir_url( NITROPACK_FILE ) . 'assets/js/admin_bar_menu.min.js?np_v=' . NITROPACK_VERSION, [ 'jquery' ], false, true );
413 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 ) ) );
414 }
415 }
416
417
418 public function enqueue_topbar_admin_menu_stylesheet() {
419 if ( ! nitropack_is_amp_page() ) {
420 wp_enqueue_style( 'topbar_admin_menu_stylesheet', plugin_dir_url( NITROPACK_FILE ) . 'assets/css/admin_bar_menu.min.css?np_v=' . NITROPACK_VERSION );
421 }
422 }
423 }