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.
$_my_name = apply_filters( 'autoptimize_filter_settings_is_pro', false ) ? esc_html__( 'Autoptimize Pro', 'autoptimize' ) : esc_html__( 'Autoptimize', 'autoptimize' );
$wp_admin_bar->add_node(
array(
'id' => 'autoptimize',
'title' => '' . $_my_name . '',
'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' => '
' . esc_html__( 'CSS/ JS Cache Info', 'autoptimize' ) . '
' .
'' .
'' .
'' . esc_html__( 'Size', 'autoptimize' ) . ': | ' . $size . ' |
' .
'' . esc_html__( 'Files', 'autoptimize' ) . ': | ' . $files . ' |
' .
'
',
'parent' => 'autoptimize',
)
);
// "Delete Cache" node.
$wp_admin_bar->add_node(
array(
'id' => 'autoptimize-delete-cache',
'title' => esc_html__( 'Clear CSS/ JS 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.min.css', __FILE__ ), array(), AUTOPTIMIZE_PLUGIN_VERSION, 'all' );
// Autoptimize Toolbar Javascript.
wp_enqueue_script( 'autoptimize-toolbar', plugins_url( '/static/toolbar.min.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( esc_html__( 'Your Autoptimize cache might not have been purged successfully, please check on the %1$sAutoptimize settings page%2$s.', 'autoptimize' ), '', '' ),
'dismiss_msg' => esc_html__( '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 ] );
}
}