PluginProbe
Autoptimize / 2.4.1
Autoptimize v2.4.1
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 2.4.1, at classes/autoptimizeToolbar.php

151 lines 6.1 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 add_action( 'wp_loaded', array( $this, 'load_toolbar' ) );
21 }
22
23 public function load_toolbar()
24 {
25 // Check permissions and that toolbar is not hidden via filter.
26 if ( current_user_can( 'manage_options' ) && apply_filters( 'autoptimize_filter_toolbar_show', true ) ) {
27
28 // Create a handler for the AJAX toolbar requests.
29 add_action( 'wp_ajax_autoptimize_delete_cache', array( $this, 'delete_cache' ) );
30
31 // Load custom styles, scripts and menu only when needed.
32 if ( is_admin_bar_showing() ) {
33 if ( is_admin() ) {
34 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
35 } else {
36 add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
37 }
38
39 // Add the Autoptimize Toolbar to the Admin bar.
40 add_action( 'admin_bar_menu', array( $this, 'add_toolbar' ), 100 );
41 }
42 }
43 }
44
45 public function add_toolbar()
46 {
47 global $wp_admin_bar;
48
49 // Retrieve the Autoptimize Cache Stats information.
50 $stats = autoptimizeCache::stats();
51
52 // Set the Max Size recommended for cache files.
53 $max_size = apply_filters( 'autoptimize_filter_cachecheck_maxsize', 512 * 1024 * 1024 );
54
55 // Retrieve the current Total Files in cache.
56 $files = $stats[0];
57 // Retrieve the current Total Size of the cache.
58 $bytes = $stats[1];
59 $size = $this->format_filesize( $bytes );
60
61 // Calculate the percentage of cache used.
62 $percentage = ceil( $bytes / $max_size * 100 );
63 if ( $percentage > 100 ) {
64 $percentage = 100;
65 }
66
67 /**
68 * We define the type of color indicator for the current state of cache size:
69 * - "green" if the size is less than 80% of the total recommended.
70 * - "orange" if over 80%.
71 * - "red" if over 100%.
72 */
73 $color = ( 100 == $percentage ) ? 'red' : ( ( $percentage > 80 ) ? 'orange' : 'green' );
74
75 // Create or add new items into the Admin Toolbar.
76 // Main "Autoptimize" node.
77 $wp_admin_bar->add_node( array(
78 'id' => 'autoptimize',
79 'title' => '<span class="ab-icon"></span><span class="ab-label">' . __( 'Autoptimize', 'autoptimize' ) . '</span>',
80 'href' => admin_url( 'options-general.php?page=autoptimize' ),
81 'meta' => array( 'class' => 'bullet-' . $color ),
82 ));
83
84 // "Cache Info" node.
85 $wp_admin_bar->add_node( array(
86 'id' => 'autoptimize-cache-info',
87 'title' => '<p>' . __( 'Cache Info', 'autoptimize' ) . '</p>' .
88 '<div class="autoptimize-radial-bar" percentage="' . $percentage . '">' .
89 '<div class="autoptimize-circle">' .
90 '<div class="mask full"><div class="fill bg-' . $color . '"></div></div>' .
91 '<div class="mask half"><div class="fill bg-' . $color . '"></div></div>' .
92 '<div class="shadow"></div>' .
93 '</div>' .
94 '<div class="inset"><div class="percentage"><div class="numbers ' . $color . '">' . $percentage . '%</div></div></div>' .
95 '</div>' .
96 '<table>' .
97 '<tr><td>' . __( 'Size', 'autoptimize' ) . ':</td><td class="size ' . $color . '">' . $size . '</td></tr>' .
98 '<tr><td>' . __( 'Files', 'autoptimize' ) . ':</td><td class="files white">' . $files . '</td></tr>' .
99 '</table>',
100 'parent' => 'autoptimize',
101 ));
102
103 // "Delete Cache" node.
104 $wp_admin_bar->add_node( array(
105 'id' => 'autoptimize-delete-cache',
106 'title' => __( 'Delete Cache', 'autoptimize' ),
107 'parent' => 'autoptimize',
108 ));
109 }
110
111 public function delete_cache()
112 {
113 check_ajax_referer( 'ao_delcache_nonce', 'nonce' );
114
115 $result = false;
116 if ( current_user_can( 'manage_options' ) ) {
117 // We call the function for cleaning the Autoptimize cache.
118 $result = autoptimizeCache::clearall();
119 }
120
121 wp_send_json( $result );
122 }
123
124 public function enqueue_scripts()
125 {
126 // Autoptimize Toolbar Styles.
127 wp_enqueue_style( 'autoptimize-toolbar', plugins_url( '/static/toolbar.css', __FILE__ ), array(), AUTOPTIMIZE_PLUGIN_VERSION, 'all' );
128
129 // Autoptimize Toolbar Javascript.
130 wp_enqueue_script( 'autoptimize-toolbar', plugins_url( '/static/toolbar.js', __FILE__ ), array( 'jquery' ), AUTOPTIMIZE_PLUGIN_VERSION, true );
131
132 // Localizes a registered script with data for a JavaScript variable.
133 // Needed for the AJAX to work properly on the frontend.
134 wp_localize_script( 'autoptimize-toolbar', 'autoptimize_ajax_object', array(
135 'ajaxurl' => admin_url( 'admin-ajax.php' ),
136 '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;"' ),
137 'dismiss_msg' => __( 'Dismiss this notice.' ),
138 'nonce' => wp_create_nonce( 'ao_delcache_nonce' ),
139 ) );
140 }
141
142 public function format_filesize( $bytes, $decimals = 2 )
143 {
144 $units = array( 'B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB' );
145
146 for ( $i = 0; ( $bytes / 1024) > 0.9; $i++, $bytes /= 1024 ) {} // @codingStandardsIgnoreLine
147
148 return sprintf( "%1.{$decimals}f %s", round( $bytes, $decimals ), $units[ $i ] );
149 }
150 }
151