format_filesize($bytes);
// We calculated 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 = ( $percentage == 100 ) ? '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' ) . '
' .
'' .
'' .
'| ' . __( "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(), time(), 'all' );
// Autoptimize Toolbar Javascript
wp_enqueue_script( 'autoptimize-toolbar', plugins_url( '/static/toolbar.js', __FILE__ ), array( 'jquery' ), time(), true );
// Localizes a registered script with data for a JavaScript variable. (We need this for the AJAX work properly in the front-end mode)
wp_localize_script( 'autoptimize-toolbar', 'autoptimize_ajax_object', array(
'ajaxurl' => admin_url( 'admin-ajax.php' ),
'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) {}
return sprintf( "%1.{$decimals}f %s", round( $bytes, $decimals ), $units[$i] );
}
}