format_filesize( $bytes ); // Calculate the percentage of cache used. $percentage = ceil( $bytes / $max_size * 100 ); if ( $percentage > 100 ) { $percentage = 100; } /** * We define the type of color indicator for the current state of cache size: * - "green" if the size is less than 80% of the total recommended. * - "orange" if over 80%. * - "red" if over 100%. */ $color = ( 100 == $percentage ) ? 'red' : ( ( $percentage > 80 ) ? 'orange' : 'green' ); // Create or add new items into the Admin Toolbar. // Main "Autoptimize" node. $wp_admin_bar->add_node( array( 'id' => 'autoptimize', 'title' => '' . __( 'Autoptimize', 'autoptimize' ) . '', 'href' => admin_url( 'options-general.php?page=autoptimize' ), 'meta' => array( 'class' => 'bullet-' . $color ), )); // "Cache Info" node. $wp_admin_bar->add_node( array( 'id' => 'autoptimize-cache-info', 'title' => '

' . __( 'Cache Info', 'autoptimize' ) . '

' . '
' . '
' . '
' . '
' . '
' . '
' . '
' . $percentage . '%
' . '
' . '' . '' . '' . '
' . __( 'Size', 'autoptimize' ) . ':' . $size . '
' . __( 'Files', 'autoptimize' ) . ':' . $files . '
', 'parent' => 'autoptimize', )); // "Delete Cache" node. $wp_admin_bar->add_node( array( 'id' => 'autoptimize-delete-cache', 'title' => __( 'Delete Cache', 'autoptimize' ), 'parent' => 'autoptimize', )); } public function delete_cache() { check_ajax_referer( 'ao_delcache_nonce', 'nonce' ); $result = false; if ( current_user_can( 'manage_options' ) ) { // We call the function for cleaning the Autoptimize cache. $result = autoptimizeCache::clearall(); } wp_send_json( $result ); } public function enqueue_scripts() { // Autoptimize Toolbar Styles. wp_enqueue_style( 'autoptimize-toolbar', plugins_url( '/static/toolbar.css', __FILE__ ), array(), AUTOPTIMIZE_PLUGIN_VERSION, 'all' ); // Autoptimize Toolbar Javascript. wp_enqueue_script( 'autoptimize-toolbar', plugins_url( '/static/toolbar.js', __FILE__ ), array( 'jquery' ), AUTOPTIMIZE_PLUGIN_VERSION, true ); // Localizes a registered script with data for a JavaScript variable. // Needed for the AJAX to work properly on the frontend. wp_localize_script( 'autoptimize-toolbar', 'autoptimize_ajax_object', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ), // translators: links to the Autoptimize settings page. 'error_msg' => sprintf( __( 'Your Autoptimize cache might not have been purged successfully, please check on the Autoptimize settings page.', 'autoptimize' ), admin_url( 'options-general.php?page=autoptimize' ) . ' style="white-space:nowrap;"' ), 'dismiss_msg' => __( 'Dismiss this notice.' ), 'nonce' => wp_create_nonce( 'ao_delcache_nonce' ), ) ); } public function format_filesize( $bytes, $decimals = 2 ) { $units = array( 'B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB' ); for ( $i = 0; ( $bytes / 1024) > 0.9; $i++, $bytes /= 1024 ) {} // @codingStandardsIgnoreLine return sprintf( "%1.{$decimals}f %s", round( $bytes, $decimals ), $units[ $i ] ); } }