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