PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 4.7.0
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v4.7.0
4.7.0 4.6.1 4.6.0 4.5.5 4.5.4 4.5.3 4.5.2 3.2.20 3.2.21 3.2.22 3.2.3 3.2.5 3.2.6 3.2.7 3.2.9 3.3.0 3.3.1 3.3.2 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.7.0 3.7.1 All 111 releases
← All changes | includes/class-updraft-smush-manager.php +42 -67 4.5.3 → 4.7.0 View file →
@@ -82,10 +82,8 @@
82 82
83 83 add_filter('manage_media_columns', array($this, 'manage_media_columns'));
84 84 add_action('manage_media_custom_column', array($this, 'manage_media_custom_column'), 10, 2);
85 85
86 - add_filter('is_protected_meta', array($this, 'is_protected_meta'), 10, 3);
87 -
88 86 // clean backup images cron action.
89 87 add_action('wpo_smush_clear_backup_images', array($this, 'clear_backup_images'));
90 88
91 89 // add filter for already compressed images by EWWW Image Optimizer.
@@ -156,17 +154,20 @@
156 154 * @return string
157 155 */
158 156 public function get_media_smush_column_content($attachment_id) {
159 157 $file = get_attached_file($attachment_id);
158 + if (false === $file) {
159 + return '';
160 + }
160 161 $ext = WPO_Image_Utils::get_extension($file);
161 162 $allowed_extensions = WPO_Image_Utils::get_allowed_extensions();
162 163
163 - $compressed = get_post_meta($attachment_id, 'smush-complete', true) ? true : false;
164 - $has_backup = get_post_meta($attachment_id, 'original-file', true) ? true : false;
164 + $compressed = get_post_meta($attachment_id, '_wpo-smush-complete', true) ? true : false;
165 + $has_backup = get_post_meta($attachment_id, '_wpo-original-file', true) ? true : false;
165 166
166 - $smush_info = get_post_meta($attachment_id, 'smush-info', true);
167 - $smush_stats = get_post_meta($attachment_id, 'smush-stats', true);
168 - $marked = get_post_meta($attachment_id, 'smush-marked');
167 + $smush_info = get_post_meta($attachment_id, '_wpo-smush-info', true);
168 + $smush_stats = get_post_meta($attachment_id, '_wpo-smush-stats', true);
169 + $marked = get_post_meta($attachment_id, '_wpo-smush-marked');
169 170
170 171 $extract = array(
171 172 'blog_id' => get_current_blog_id(),
172 173 'post_id' => $attachment_id,
@@ -221,9 +222,9 @@
221 222 if (empty($nonce) || empty($subaction)) {
222 223 die('Security check failed');
223 224 }
224 225
225 - if (!current_user_can(WP_Optimize()->capability_required())) {
226 + if (!WP_Optimize()->current_user_can()) {
226 227 die('You are not allowed to run this command.');
227 228 }
228 229
229 230 $allowed_commands = Updraft_Smush_Manager_Commands::get_allowed_ajax_commands();
@@ -366,9 +367,9 @@
366 367 */
367 368 public function restore_single_image($image_id, $blog_id) {
368 369
369 370 $switched_blog = false;
370 - if (is_multisite() && current_user_can('manage_network_options')) {
371 + if (is_multisite() && WP_Optimize()->current_user_can('manage_network_options')) {
371 372 switch_to_blog($blog_id);
372 373 $switched_blog = true;
373 374 } elseif (is_multisite() && get_current_blog_id() != $blog_id) {
374 375 return new WP_Error('restore_backup_wrong_blog_id', __('The blog ID provided does not match the current blog.', 'wp-optimize'));
@@ -376,9 +377,9 @@
376 377
377 378 $error = false;
378 379
379 380 $image_path = get_attached_file($image_id);
380 - $backup_path = get_post_meta($image_id, 'original-file', true);
381 + $backup_path = get_post_meta($image_id, '_wpo-original-file', true);
381 382 $uploads_dir = wp_upload_dir();
382 383 $uploads_basedir = realpath($uploads_dir['basedir']);
383 384
384 385 if (false === $uploads_basedir) {
@@ -422,24 +423,24 @@
422 423 }
423 424 }
424 425
425 426 $backup_path = realpath($backup_path);
426 -
427 - if (!$backup_path || 0 !== strpos($backup_path, $uploads_basedir)) {
427 + if (!$backup_path || 0 !== strpos(trailingslashit(dirname($backup_path)), $uploads_basedir)) {
428 428 if ($switched_blog) {
429 429 restore_current_blog();
430 430 }
431 - return new WP_Error('restore_backup_issue', __('The backup file path is not correct.', 'wp-optimize'));
431 + return new WP_Error('restore_failed', __('The backup file path seems suspicious, and could not be deleted.', 'wp-optimize'));
432 432 }
433 433
434 +
434 435 if (!is_file($backup_path)) {
435 436 // Delete information about backup.
436 - delete_post_meta($image_id, 'original-file');
437 + delete_post_meta($image_id, '_wpo-original-file');
437 438 $error = new WP_Error('restore_backup_not_found', __('The backup was not found; it may have been deleted or was already restored', 'wp-optimize'));
438 439 } elseif (!wp_is_writable($image_path)) {
439 - $error = new WP_Error('restore_failed', __('The destination could not be written to.', 'wp-optimize').' '.__("Please check your folder's permissions", 'wp-optimize'));
440 + $error = new WP_Error('restore_failed', __('The destination could not be written to.', 'wp-optimize').' '.__("Please check your folder's permissions", 'wp-optimize'));
440 441 } elseif (!copy($backup_path, $image_path)) {
441 - $error = new WP_Error('restore_failed', __('The file could not be copied; check your PHP error logs for details', 'wp-optimize'));
442 + $error = new WP_Error('restore_failed', __('The file could not be copied; check your PHP error logs for details', 'wp-optimize'));
442 443 } elseif (!WP_Optimize_Utils::wp_delete_file($backup_path)) {
443 444 // translators: %s is the backup file path
444 445 $error = new WP_Error('restore_failed', sprintf(__('The backup file %s could not be deleted.', 'wp-optimize'), $backup_path));
445 446 }
@@ -446,12 +447,12 @@
446 447
447 448 if (!$error) {
448 449 // if backup image deleted successfully
449 450 // then delete from attachment meta associated smush data
450 - delete_post_meta($image_id, 'smush-complete');
451 - delete_post_meta($image_id, 'smush-stats');
452 - delete_post_meta($image_id, 'original-file');
453 - delete_post_meta($image_id, 'smush-info');
451 + delete_post_meta($image_id, '_wpo-smush-complete');
452 + delete_post_meta($image_id, '_wpo-smush-stats');
453 + delete_post_meta($image_id, '_wpo-original-file');
454 + delete_post_meta($image_id, '_wpo-smush-info');
454 455 }
455 456
456 457 if ($switched_blog) {
457 458 restore_current_blog();
@@ -494,9 +495,9 @@
494 495 $processed = 0;
495 496
496 497 if ($restore_backup) {
497 498 // get post ids those have backup meta field.
498 - $image_ids = $wpdb->get_results($wpdb->prepare("SELECT post_id FROM `{$wpdb->postmeta}` WHERE meta_key = 'original-file' LIMIT %d;", $images_limit), ARRAY_A);
499 + $image_ids = $wpdb->get_results($wpdb->prepare("SELECT post_id FROM `{$wpdb->postmeta}` WHERE meta_key = '_wpo-original-file' LIMIT %d;", $images_limit), ARRAY_A);
499 500
500 501 if (!empty($image_ids)) {
501 502 // run restore function for each found image.
502 503 foreach ($image_ids as $image) {
@@ -540,9 +541,9 @@
540 541 }
541 542
542 543 if ($result['completed']) {
543 544
544 - $smushed_images_count = $wpdb->get_var("SELECT COUNT(*) FROM `{$wpdb->postmeta}` WHERE meta_key='smush-complete' AND meta_value=1");
545 + $smushed_images_count = $wpdb->get_var("SELECT COUNT(*) FROM `{$wpdb->postmeta}` WHERE meta_key='_wpo-smush-complete' AND meta_value=1");
545 546 $result['smushed_images_count'] = $smushed_images_count;
546 547
547 548 if ($delete_only_backups_meta) {
548 549 if (is_multisite()) {
@@ -576,9 +577,9 @@
576 577 // clear all metas for smushed images after work completed.
577 578 // if $delete_only_backup_meta set to true then all meta fields was deleted in restore_single_image()
578 579 // and we don't need to delete metas for other images.
579 580 if (!$delete_only_backups_meta) {
580 - $wpdb->query("DELETE FROM `{$wpdb->postmeta}` WHERE meta_key IN ('smush-complete', 'smush-stats', 'original-file', 'smush-info');");
581 + $wpdb->query("DELETE FROM `{$wpdb->postmeta}` WHERE meta_key IN ('_wpo-smush-complete', '_wpo-smush-stats', '_wpo-original-file', '_wpo-smush-info');");
581 582 }
582 583 }
583 584
584 585 if (is_multisite()) {
@@ -695,12 +696,12 @@
695 696 }
696 697
697 698 if (is_multisite()) {
698 699 switch_to_blog($task->get_option('blog_id', 1));
699 - $stats = get_post_meta($attachment_id, 'smush-stats', true);
700 + $stats = get_post_meta($attachment_id, '_wpo-smush-stats', true);
700 701 restore_current_blog();
701 702 } else {
702 - $stats = get_post_meta($attachment_id, 'smush-stats', true);
703 + $stats = get_post_meta($attachment_id, '_wpo-smush-stats', true);
703 704 }
704 705
705 706 if (isset($stats['sizes-info'])) {
706 707
@@ -856,14 +857,14 @@
856 857 * @param WP_Post $post - a post object
857 858 */
858 859 public function render_smush_metabox($post) {
859 860
860 - $compressed = (bool) get_post_meta($post->ID, 'smush-complete', true);
861 - $has_backup = (bool) get_post_meta($post->ID, 'original-file', true);
861 + $compressed = (bool) get_post_meta($post->ID, '_wpo-smush-complete', true);
862 + $has_backup = (bool) get_post_meta($post->ID, '_wpo-original-file', true);
862 863
863 - $smush_info = get_post_meta($post->ID, 'smush-info', true);
864 - $smush_stats = get_post_meta($post->ID, 'smush-stats', true);
865 - $marked = get_post_meta($post->ID, 'smush-marked');
864 + $smush_info = get_post_meta($post->ID, '_wpo-smush-info', true);
865 + $smush_stats = get_post_meta($post->ID, '_wpo-smush-stats', true);
866 + $marked = get_post_meta($post->ID, '_wpo-smush-marked');
866 867
867 868 $options = Updraft_Smush_Manager()->get_smush_options();
868 869
869 870 $file = get_attached_file($post->ID);
@@ -1132,12 +1133,12 @@
1132 1133
1133 1134 foreach ($images as $image) {
1134 1135 if (is_multisite()) {
1135 1136 switch_to_blog($image['blog_id']);
1136 - $stats[] = get_post_meta($image['attachment_id'], 'smush-complete', true) ? 'success' : 'fail';
1137 + $stats[] = get_post_meta($image['attachment_id'], '_wpo-smush-complete', true) ? 'success' : 'fail';
1137 1138 restore_current_blog();
1138 1139 } else {
1139 - $stats[] = get_post_meta($image['attachment_id'], 'smush-complete', true) ? 'success' : 'fail';
1140 + $stats[] = get_post_meta($image['attachment_id'], '_wpo-smush-complete', true) ? 'success' : 'fail';
1140 1141 }
1141 1142 }
1142 1143
1143 1144 return array_count_values($stats);
@@ -1205,9 +1206,9 @@
1205 1206 $js_variables['cancel'] = esc_html__('Cancel', 'wp-optimize');
1206 1207 $js_variables['cancelling'] = esc_html__('Cancelling...', 'wp-optimize');
1207 1208 $js_variables['images_restored_successfully'] = esc_html__('The images were restored successfully', 'wp-optimize');
1208 1209 $js_variables['logo_src'] = esc_url(WPO_PLUGIN_URL.'images/notices/wp_optimize_logo.png');
1209 -
1210 +
1210 1211 wp_enqueue_script('block-ui-js', WPO_PLUGIN_URL.'includes/blockui/jquery.blockUI'.$min_or_not.'.js', array('jquery'), $enqueue_version);
1211 1212 wp_enqueue_script('wp-optimize-heartbeat-js', WPO_PLUGIN_URL.'js/heartbeat'.$min_or_not_internal.'.js', array('jquery'), $enqueue_version);
1212 1213 wp_localize_script('wp-optimize-heartbeat-js', 'wpo_heartbeat_ajax', array(
1213 1214 'ajaxurl' => admin_url('admin-ajax.php'),
@@ -1642,9 +1643,9 @@
1642 1643 *
1643 1644 * @return bool
1644 1645 */
1645 1646 public function is_compressed($attachment_id) {
1646 - return (true === (bool) get_post_meta($attachment_id, 'smush-complete', true));
1647 + return (true === (bool) get_post_meta($attachment_id, '_wpo-smush-complete', true));
1647 1648 }
1648 1649
1649 1650 /**
1650 1651 * Returns true if multisite
@@ -1660,9 +1661,13 @@
1660 1661 *
1661 1662 * @param int $post_id - WordPress Post ID
1662 1663 */
1663 1664 public function unscheduled_original_file_deletion($post_id) {
1664 - $the_original_file = get_post_meta($post_id, 'original-file', true);
1665 + $the_original_file = get_post_meta($post_id, '_wpo-original-file', true);
1666 + // If no original file meta exists, there is nothing to delete.
1667 + if (empty($the_original_file)) {
1668 + return;
1669 + }
1665 1670 $uploads_dir = wp_get_upload_dir();
1666 1671 $uploads_basedir = realpath($uploads_dir['basedir']);
1667 1672 if (!$uploads_basedir) return;
1668 1673 $uploads_basedir = trailingslashit($uploads_basedir);
@@ -1708,14 +1713,14 @@
1708 1713 'relation' => 'AND',
1709 1714 array(
1710 1715 'relation' => 'OR',
1711 1716 array(
1712 - 'key' => 'smush-complete',
1717 + 'key' => '_wpo-smush-complete',
1713 1718 'compare' => '!=',
1714 1719 'value' => '1',
1715 1720 ),
1716 1721 array(
1717 - 'key' => 'smush-complete',
1722 + 'key' => '_wpo-smush-complete',
1718 1723 'compare' => 'NOT EXISTS',
1719 1724 'value' => '',
1720 1725 ),
1721 1726 ),
@@ -1756,38 +1761,8 @@
1756 1761 'compare' => 'NOT EXISTS',
1757 1762 'value' => '',
1758 1763 ),
1759 1764 );
1760 - }
1761 -
1762 - /**
1763 - * Protects smush meta keys
1764 - *
1765 - * @param bool $protected Whether the key is considered protected
1766 - * @param string $meta_key Metadata key
1767 - * @param string $meta_type Type of metadata object
1768 - *
1769 - * @return bool
1770 - */
1771 - public function is_protected_meta($protected, $meta_key, $meta_type) {
1772 - if ('post' !== $meta_type) {
1773 - return $protected;
1774 - }
1775 -
1776 - $keys = array(
1777 - 'smush-complete',
1778 - 'smush-marked',
1779 - 'smush-info',
1780 - 'smush-stats',
1781 - 'original-file',
1782 - 'wpo-webp-conversion-complete',
1783 - );
1784 -
1785 - if (in_array($meta_key, $keys, true)) {
1786 - return true;
1787 - }
1788 -
1789 - return $protected;
1790 1765 }
1791 1766 }
1792 1767
1793 1768 /**