PluginProbe
Autoptimize / 3.1.7
Autoptimize v3.1.7
2.2.2 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.5.0 2.5.1 2.6.0 2.6.1 2.6.2 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 2.7.8 All 107 releases
autoptimize / classes / autoptimizeToolbar.php

autoptimizeToolbar.php in Autoptimize 3.1.7, at classes/autoptimizeToolbar.php

168 lines 6.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Handles toolbar-related stuff.
4 */
5
6 if ( ! defined( 'ABSPATH' ) ) {
7 exit;
8 }
9
10 class autoptimizeToolbar
11 {
12 public function __construct()
13 {
14 // If Cache is not available we don't add the toolbar.
15 if ( ! autoptimizeCache::cacheavail() ) {
16 return;
17 }
18
19 // Load admin toolbar feature once WordPress, all plugins, and the theme are fully loaded and instantiated.
20 if ( is_admin() ) {
21 add_action( 'wp_loaded', array( $this, 'load_toolbar' ) );
22 } else {
23 // to avoid AMP complaining about the AMP conditional being checked too early.
24 add_action( 'wp', array( $this, 'load_toolbar' ) );
25 }
26 }
27
28 public function load_toolbar()
29 {
30 // Check permissions and that toolbar is not hidden via filter.
31 if ( current_user_can( 'manage_options' ) && apply_filters( 'autoptimize_filter_toolbar_show', true ) && ! autoptimizeMain::is_amp_markup( '' ) ) {
32
33 // Create a handler for the AJAX toolbar requests.
34 add_action( 'wp_ajax_autoptimize_delete_cache', array( $this, 'delete_cache' ) );
35
36 // Load custom styles, scripts and menu only when needed.
37 if ( is_admin_bar_showing() ) {
38 if ( is_admin() ) {
39 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
40 } else {
41 add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
42 }
43
44 // Add the Autoptimize Toolbar to the Admin bar.
45 add_action( 'admin_bar_menu', array( $this, 'add_toolbar' ), 100 );
46 }
47 }
48 }
49
50 public function add_toolbar()
51 {
52 global $wp_admin_bar;
53
54 // Retrieve the Autoptimize Cache Stats information.
55 $stats = autoptimizeCache::stats();
56
57 // Set the Max Size recommended for cache files.
58 $max_size = apply_filters( 'autoptimize_filter_cachecheck_maxsize', 512 * 1024 * 1024 );
59
60 // Retrieve the current Total Files in cache.
61 $files = $stats[0];
62 // Retrieve the current Total Size of the cache.
63 $bytes = $stats[1];
64 $size = $this->format_filesize( $bytes );
65
66 // Calculate the percentage of cache used.
67 $percentage = ceil( $bytes / $max_size * 100 );
68 if ( $percentage > 100 ) {
69 $percentage = 100;
70 }
71
72 /**
73 * We define the type of color indicator for the current state of cache size:
74 * - "green" if the size is less than 80% of the total recommended.
75 * - "orange" if over 80%.
76 * - "red" if over 100%.
77 */
78 $color = ( 100 == $percentage ) ? 'red' : ( ( $percentage > 80 ) ? 'orange' : 'green' );
79
80 // Create or add new items into the Admin Toolbar.
81 // Main "Autoptimize" node.
82 $_my_name = apply_filters( 'autoptimize_filter_settings_is_pro', false ) ? __( 'Autoptimize Pro', 'autoptimize' ) : __( 'Autoptimize', 'autoptimize' );
83 $wp_admin_bar->add_node(
84 array(
85 'id' => 'autoptimize',
86 'title' => '<span class="ab-icon"></span><span class="ab-label">' . $_my_name . '</span>',
87 'href' => admin_url( 'options-general.php?page=autoptimize' ),
88 'meta' => array( 'class' => 'bullet-' . $color ),
89 )
90 );
91
92 // "Cache Info" node.
93 $wp_admin_bar->add_node(
94 array(
95 'id' => 'autoptimize-cache-info',
96 'title' => '<p>' . __( 'CSS/ JS Cache Info', 'autoptimize' ) . '</p>' .
97 '<div class="autoptimize-radial-bar" percentage="' . $percentage . '">' .
98 '<div class="autoptimize-circle">' .
99 '<div class="mask full"><div class="fill bg-' . $color . '"></div></div>' .
100 '<div class="mask half"><div class="fill bg-' . $color . '"></div></div>' .
101 '<div class="shadow"></div>' .
102 '</div>' .
103 '<div class="inset"><div class="percentage"><div class="numbers ' . $color . '">' . $percentage . '%</div></div></div>' .
104 '</div>' .
105 '<table>' .
106 '<tr><td>' . __( 'Size', 'autoptimize' ) . ':</td><td class="size ' . $color . '">' . $size . '</td></tr>' .
107 '<tr><td>' . __( 'Files', 'autoptimize' ) . ':</td><td class="files white">' . $files . '</td></tr>' .
108 '</table>',
109 'parent' => 'autoptimize',
110 )
111 );
112
113 // "Delete Cache" node.
114 $wp_admin_bar->add_node(
115 array(
116 'id' => 'autoptimize-delete-cache',
117 'title' => __( 'Clear CSS/ JS Cache', 'autoptimize' ),
118 'parent' => 'autoptimize',
119 )
120 );
121 }
122
123 public function delete_cache()
124 {
125 check_ajax_referer( 'ao_delcache_nonce', 'nonce' );
126
127 $result = false;
128 if ( current_user_can( 'manage_options' ) ) {
129 // We call the function for cleaning the Autoptimize cache.
130 $result = autoptimizeCache::clearall();
131 }
132
133 wp_send_json( $result );
134 }
135
136 public function enqueue_scripts()
137 {
138 // Autoptimize Toolbar Styles.
139 wp_enqueue_style( 'autoptimize-toolbar', plugins_url( '/static/toolbar.min.css', __FILE__ ), array(), AUTOPTIMIZE_PLUGIN_VERSION, 'all' );
140
141 // Autoptimize Toolbar Javascript.
142 wp_enqueue_script( 'autoptimize-toolbar', plugins_url( '/static/toolbar.min.js', __FILE__ ), array( 'jquery' ), AUTOPTIMIZE_PLUGIN_VERSION, true );
143
144 // Localizes a registered script with data for a JavaScript variable.
145 // Needed for the AJAX to work properly on the frontend.
146 wp_localize_script(
147 'autoptimize-toolbar',
148 'autoptimize_ajax_object',
149 array(
150 'ajaxurl' => admin_url( 'admin-ajax.php' ),
151 // translators: links to the Autoptimize settings page.
152 'error_msg' => sprintf( __( 'Your Autoptimize cache might not have been purged successfully, please check on the <a href=%s>Autoptimize settings page</a>.', 'autoptimize' ), admin_url( 'options-general.php?page=autoptimize' ) . ' style="white-space:nowrap;"' ),
153 'dismiss_msg' => __( 'Dismiss this notice.' ),
154 'nonce' => wp_create_nonce( 'ao_delcache_nonce' ),
155 )
156 );
157 }
158
159 public function format_filesize( $bytes, $decimals = 2 )
160 {
161 $units = array( 'B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB' );
162
163 for ( $i = 0; ( $bytes / 1024) > 0.9; $i++, $bytes /= 1024 ) {} // @codingStandardsIgnoreLine
164
165 return sprintf( "%1.{$decimals}f %s", round( $bytes, $decimals ), $units[ $i ] );
166 }
167 }
168