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