Update Speedycache to Latest Version!
SpeedyCache Pro depends on the free version of SpeedyCache, so you need to update the free version to use SpeedyCache without any issue.
Install Now
';
}
function speedycache_pro_img_stats(){
check_ajax_referer('speedycache_ajax_nonce', 'security');
if(!current_user_can('manage_options')){
wp_die('Must be admin');
}
if(!class_exists('\SpeedyCache\Image')){
wp_send_json_error(__('The file required to Process Image optimization is not present', 'speedycache'));
}
$res = \SpeedyCache\Image::statics_data();
wp_send_json($res);
}
function speedycache_pro_optimize_image(){
check_ajax_referer('speedycache_ajax_nonce', 'security');
if(!current_user_can('manage_options')){
wp_die('Must be admin');
}
if(!class_exists('\SpeedyCache\Image')){
wp_send_json_error(__('The file required to Process Image optimization is not present', 'speedycache'));
}
$id = null;
if(!empty($_POST['img_id'])){
$id = (int) sanitize_text_field(wp_unslash($_POST['img_id']));
if(empty($id)){
wp_send_json_error('Empty Image ID');
}
}
$res = \SpeedyCache\Image::optimize_single($id);
$res[1] = isset($res[1]) ? $res[1] : '';
$res[2] = isset($res[2]) ? $res[2] : '';
$res[3] = isset($res[3]) ? $res[3] : '';
$response = array(
'message' => $res[0],
'success' => $res[1],
'id' => $res[2],
'percentage' => $res[3],
);
wp_send_json($response);
}
function speedycache_pro_save_img_settings(){
check_ajax_referer('speedycache_ajax_nonce', 'security');
if(!current_user_can('manage_options')){
wp_die('Must be admin');
}
global $speedycache;
$settings = speedycache_optpost('settings');
foreach($settings as $key => $setting){
$new_key = str_replace('img_', '', $key);
$settings[$new_key] = $setting;
unset($settings[$key]);
}
$speedycache->image['settings'] = $settings;
if(update_option('speedycache_img', $speedycache->image['settings'])){
wp_send_json_success();
}
wp_send_json_error();
}
function speedycache_pro_list_imgs(){
check_ajax_referer('speedycache_ajax_nonce', 'security');
if(!current_user_can('manage_options')){
wp_die('Must be admin');
}
$query_images_args = array();
$query_images_args['offset'] = intval(speedycache_optget('page')) * intval(speedycache_optget('per_page'));
$query_images_args['order'] = 'DESC';
$query_images_args['orderby'] = 'ID';
$query_images_args['post_type'] = 'attachment';
$query_images_args['post_mime_type'] = array('image/jpeg', 'image/png', 'image/gif');
$query_images_args['post_status'] = 'inherit';
$query_images_args['posts_per_page'] = speedycache_optget('per_page');
$query_images_args['meta_query'] = array(
array(
'key' => 'speedycache_optimisation',
'compare' => 'EXISTS'
)
);
$query_images_args['s'] = speedycache_optget('search');
if(!empty($_GET['filter'])){
if(speedycache_optget('filter') == 'error_code'){
$filter = array(
'key' => 'speedycache_optimisation',
'value' => base64_encode('"error_code"'),
'compare' => 'LIKE'
);
$filter_second = array(
'key' => 'speedycache_optimisation',
'compare' => 'NOT LIKE'
);
array_push($query_images_args['meta_query'], $filter);
array_push($query_images_args['meta_query'], $filter_second);
}
}
$result = array(
'content' => \SpeedyCache\Image::list_content($query_images_args),
'result_count' => \SpeedyCache\Image::count_query($query_images_args)
);
wp_send_json($result);
}
function speedycache_pro_revert_img(){
check_ajax_referer('speedycache_ajax_nonce', 'security');
if(!current_user_can('manage_options')){
wp_die('Must Be admin');
}
global $speedycache;
if(!empty($_GET['id'])){
$speedycache->image['id'] = (int) speedycache_optget('id');
}
wp_send_json(\SpeedyCache\Image::revert());
}
function speedycache_pro_revert_all_imgs(){
check_ajax_referer('speedycache_ajax_nonce', 'security');
if(!current_user_can('manage_options')){
wp_die('Must be admin');
}
\SpeedyCache\Image::revert_all();
}
function speedycache_pro_verify_license(){
if(!wp_verify_nonce($_GET['security'], 'speedycache_license')){
wp_send_json_error(__('Security Check Failed', 'speedycache'));
}
if(!current_user_can('manage_options')){
wp_send_json_error(__('You do not have required permission.', 'speedycache'));
}
global $speedycache;
$license = sanitize_key($_GET['license']);
if(empty($license)){
wp_send_json_error(__('The license key was not submitted', 'speedycache'));
}
$resp = wp_remote_get(SPEEDYCACHE_API.'license.php?license='.$license.'&url='.rawurlencode(site_url()), array('timeout' => 30));
if(!is_array($resp)){
wp_send_json_error(__('The response was malformed '.var_export($resp, true), 'speedycache'));
}
$json = json_decode($resp['body'], true);
// Save the License
if(empty($json['license'])){
wp_send_json_error(__('The license key is invalid', 'speedycache'));
}
$speedycache->license = $json;
update_option('speedycache_license', $json, false);
wp_send_json_success();
}
function speedycache_pro_enqueue_admin_scripts(){
wp_enqueue_style('speedycache-admin-pro', SPEEDYCACHE_PRO_URL . '/assets/css/admin.css', [], SPEEDYCACHE_PRO_VERSION);
wp_enqueue_script('speedycache-admin-pro', SPEEDYCACHE_PRO_URL . '/assets/js/admin.js', [], SPEEDYCACHE_PRO_VERSION);
wp_localize_script('speedycache-admin-pro', 'speedycache_pro_ajax', [
'url' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('speedycache_pro_ajax_nonce'),
'premium' => defined('SPEEDYCACHE_PRO'),
]);
}
// Sets speedycache settings to our predefined defaults.
function speedycache_pro_copy_test_settings(){
check_ajax_referer('speedycache_pro_ajax_nonce', 'security');
if(!current_user_can('manage_options')){
wp_die('Must be admin');
}
global $speedycache;
$test_settings = ['minify_html' => true, 'delay_js' => true, 'render_blocking' => true, 'minify_js' => true, 'critical_images' => true, 'lazy_load' => true, 'delay_js_mode' => 'selected', 'delay_js_scripts' => ['fbevents.js', 'google-analytics.com', 'adsbygoogle.js', 'googletagmanager.com', 'fbq(', "ga( '", "ga('", '/gtm.js', '/gtag/js', 'gtag(', '/gtm-', '/gtm.']];
$speedycache->options = array_merge($speedycache->options, $test_settings);
update_option('speedycache_options', $speedycache->options);
delete_option('speedycache_test_results');
wp_send_json_success(__('Settings applied successfully.', 'speedycache-pro'));
}
function speedycache_pro_dismiss_test_notice(){
check_ajax_referer('speedycache_pro_ajax_nonce', 'security');
if(!current_user_can('manage_options')){
wp_die('Must be admin');
}
delete_option('speedycache_test_results');
}
function speedycache_pro_schedule_test_event() {
$install_time = get_option('speedycache_free_installed', 0);
$event_executed = get_option('speedycache_test_executed', 0);
if($install_time && !$event_executed){
if(($install_time + WEEK_IN_SECONDS) < time()){
$event_time = time() + DAY_IN_SECONDS;
} else {
$event_time = $install_time + WEEK_IN_SECONDS;
}
if(!wp_next_scheduled('speedycache_test_event')){
wp_schedule_single_event($event_time, 'speedycache_test_event');
}
}
}
function speedycache_pro_test_results_notice() {
if(!current_user_can('manage_options')){
return;
}
$current_screen = get_current_screen();
if(!isset($current_screen->id) || strpos($current_screen->id, 'speedycache') === false){
return;
}
$test_results = get_option('speedycache_test_results');
if(empty($test_results)){
return;
}
$old_score = $test_results['old_score'];
$new_score = $test_results['new_score'];
$stroke_old = !empty($old_score) ? 100 - $old_score : 0;
$stroke_new = !empty($new_score) ? 100 - $new_score : 0;
$old_color = speedycache_pro_get_score_color($old_score);
$new_color = speedycache_pro_get_score_color($new_score);
echo '
'.esc_html__('Speed Test Results:', 'speedycache-pro').'
'.esc_attr($old_score).'
'.esc_html__('Before Optimization', 'speedycache-pro').'
'.esc_attr($new_score).'
'.esc_html__('After Optimization', 'speedycache-pro').'
'.esc_html__('Want to enable the SpeedyCache settings used for this test?', 'speedycache-pro').'
'.esc_html__('Enable Now', 'speedycache-pro').'
'.esc_html__('Dismiss this notice.', 'speedycache-pro').'
';
}
function speedycache_pro_plugins_expired($plugins){
global $speedycache;
if(!empty($speedycache->license) && empty($speedycache->license['active'])){
$plugins[] = 'SpeedyCache';
}
return $plugins;
}
function speedycache_pro_notices(){
global $speedycache;
if(!current_user_can('activate_plugins')){
return;
}
$current_screen = get_current_screen();
// Test result notice
if(isset($current_screen->id) && strpos($current_screen->id, 'speedycache') !== false){
speedycache_pro_test_results_notice();
}
// If the license is active then we do not need to show any notice.
if(!empty($speedycache->license) && empty($speedycache->license['active'])){
speedycache_pro_expiry_notice();
}
}
function speedycache_pro_expiry_notice(){
global $speedycache;
// The combined notice for all Softaculous plugin to show that the license has expired
$dismissed_at = get_option('softaculous_expired_licenses', 0);
$expired_plugins = apply_filters('softaculous_expired_licenses', []);
if(
!empty($expired_plugins) &&
is_array($expired_plugins) &&
!defined('SOFTACULOUS_EXPIRY_LICENSES') &&
(empty($dismissed_at) || ($dismissed_at + WEEK_IN_SECONDS) < time())
){
define('SOFTACULOUS_EXPIRY_LICENSES', true); // To make sure other plugins don't return a Notice
echo '
'.sprintf(__('Your %1$s plugin license has %2$sExpired%3$s. Please renew your license for uninterrupted updates and support.', 'speedycache-pro'),
esc_html(implode(', ', $expired_plugins)),
'',
' '
). '
';
wp_register_script('speedycache-pro-expiry-notice', '', array('jquery'), SPEEDYCACHE_PRO_VERSION, true );
wp_enqueue_script('speedycache-pro-expiry-notice');
wp_add_inline_script('speedycache-pro-expiry-notice', '
jQuery(document).ready(function(){
jQuery("#speedycache-pro-expiry-notice").on("click", ".notice-dismiss", function(e){
e.preventDefault();
let target = jQuery(e.target);
let jEle = target.closest("#speedycache-pro-expiry-notice");
jEle.slideUp();
jQuery.post("'.admin_url('admin-ajax.php').'", {
security : "'.wp_create_nonce('speedycache_expiry_notice').'",
action: "speedycache_pro_dismiss_expired_licenses",
}, function(res){
if(!res["success"]){
alert(res["data"]);
}
}).fail(function(data){
alert("There seems to be some issue dismissing this alert");
});
});
})');
}
}
function speedycache_pro_get_score_color($score) {
$score_color_map = [
0 => ['#c00', '#c003', '#c00'], // Red
50 => ['#fa3', '#ffa50036', '#fa3'], // Orange
90 => ['#0c6', '#00cc663b', '#080'], // Green
];
if ($score >= 0 && $score < 50) {
return $score_color_map[0];
} elseif ($score >= 50 && $score < 90) {
return $score_color_map[50];
} else {
return $score_color_map[90];
}
}
function speedycache_pro_media_metabox($post){
if(empty($post)){
return;
}
$allowed_img_types = [
'image/png',
'image/jpg',
'image/jpeg',
];
if(!in_array($post->post_mime_type, $allowed_img_types)){
return;
}
add_meta_box(
'speedycache-optm-img',
__('SpeedyCache Image Optimization', 'speedycache-pro'),
'speedycache_pro_img_optm_metabox',
'attachment',
'side',
'default'
);
}
function speedycache_pro_img_optm_metabox($post){
if(empty($post)){
echo 'No Post data';
return;
}
$optimized_data = get_post_meta($post->ID, 'speedycache_optimisation', true);
if(!empty($optimized_data)){
$optimized_data = base64_decode($optimized_data);
$optimized_data = json_decode($optimized_data, true);
if(!empty($optimized_data) && !empty($optimized_data[0]) && !empty($optimized_data[0]['file']) && file_exists($optimized_data[0]['file'])){
esc_html_e('Image has already been optimized', 'speedycache-pro');
return;
}
}
echo ''.esc_html__('Optimize this Image', 'speedycache-pro').' ';
wp_register_script('speedycache-img-optm-meta-box', '', array('jquery'), '', true);
wp_enqueue_script('speedycache-img-optm-meta-box');
wp_add_inline_script('speedycache-img-optm-meta-box', 'jQuery(document).ready(function(){
jQuery("#speedycache-optm-attachment").on("click", function(e){
e.preventDefault();
let attachment_id = jQuery(e.target).data("id"),
nonce = "'.wp_create_nonce('speedycache_ajax_nonce').'";
jQuery.ajax({
url : "'.esc_url(admin_url('admin-ajax.php')).'",
method : "POST",
data : {
"action":"speedycache_optimize_image_ajax_request",
"security": nonce,
"img_id": attachment_id,
},
success: function(res){
if(res.success){
window.location.reload();
}
}
});
})
})');
}
function speedycache_pro_dismiss_expired_licenses(){
check_admin_referer('speedycache_expiry_notice', 'security');
if(!current_user_can('activate_plugins')){
wp_send_json_error(__('You do not have required access to do this action', 'speedycache-pro'));
}
update_option('softaculous_expired_licenses', time());
wp_send_json_success();
}