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 +65 -38 4.5.2 → 4.7.0 View file →
@@ -154,17 +154,20 @@
154 154 * @return string
155 155 */
156 156 public function get_media_smush_column_content($attachment_id) {
157 157 $file = get_attached_file($attachment_id);
158 + if (false === $file) {
159 + return '';
160 + }
158 161 $ext = WPO_Image_Utils::get_extension($file);
159 162 $allowed_extensions = WPO_Image_Utils::get_allowed_extensions();
160 163
161 - $compressed = get_post_meta($attachment_id, 'smush-complete', true) ? true : false;
162 - $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;
163 166
164 - $smush_info = get_post_meta($attachment_id, 'smush-info', true);
165 - $smush_stats = get_post_meta($attachment_id, 'smush-stats', true);
166 - $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');
167 170
168 171 $extract = array(
169 172 'blog_id' => get_current_blog_id(),
170 173 'post_id' => $attachment_id,
@@ -219,9 +222,9 @@
219 222 if (empty($nonce) || empty($subaction)) {
220 223 die('Security check failed');
221 224 }
222 225
223 - if (!current_user_can(WP_Optimize()->capability_required())) {
226 + if (!WP_Optimize()->current_user_can()) {
224 227 die('You are not allowed to run this command.');
225 228 }
226 229
227 230 $allowed_commands = Updraft_Smush_Manager_Commands::get_allowed_ajax_commands();
@@ -364,9 +367,9 @@
364 367 */
365 368 public function restore_single_image($image_id, $blog_id) {
366 369
367 370 $switched_blog = false;
368 - if (is_multisite() && current_user_can('manage_network_options')) {
371 + if (is_multisite() && WP_Optimize()->current_user_can('manage_network_options')) {
369 372 switch_to_blog($blog_id);
370 373 $switched_blog = true;
371 374 } elseif (is_multisite() && get_current_blog_id() != $blog_id) {
372 375 return new WP_Error('restore_backup_wrong_blog_id', __('The blog ID provided does not match the current blog.', 'wp-optimize'));
@@ -374,15 +377,23 @@
374 377
375 378 $error = false;
376 379
377 380 $image_path = get_attached_file($image_id);
378 - $backup_path = get_post_meta($image_id, 'original-file', true);
379 -
381 + $backup_path = get_post_meta($image_id, '_wpo-original-file', true);
382 + $uploads_dir = wp_upload_dir();
383 + $uploads_basedir = realpath($uploads_dir['basedir']);
384 +
385 + if (false === $uploads_basedir) {
386 + if ($switched_blog) {
387 + restore_current_blog();
388 + }
389 + return new WP_Error('restore_backup_issue', __('The uploads base directory is not correct.', 'wp-optimize'));
390 + }
391 +
392 + $uploads_basedir = trailingslashit($uploads_basedir);
393 +
380 394 // If the file doesn't exist, check if it's relative
381 395 if (!is_file($backup_path)) {
382 - $uploads_dir = wp_upload_dir();
383 - $uploads_basedir = trailingslashit($uploads_dir['basedir']);
384 -
385 396 if (is_file($uploads_basedir . $backup_path)) {
386 397 $backup_path = $uploads_basedir . $backup_path;
387 398 }
388 399 }
@@ -411,16 +422,25 @@
411 422 $backup_path = $uploads_basedir . $backup_path;
412 423 }
413 424 }
414 425
426 + $backup_path = realpath($backup_path);
427 + if (!$backup_path || 0 !== strpos(trailingslashit(dirname($backup_path)), $uploads_basedir)) {
428 + if ($switched_blog) {
429 + restore_current_blog();
430 + }
431 + return new WP_Error('restore_failed', __('The backup file path seems suspicious, and could not be deleted.', 'wp-optimize'));
432 + }
433 +
434 +
415 435 if (!is_file($backup_path)) {
416 436 // Delete information about backup.
417 - delete_post_meta($image_id, 'original-file');
437 + delete_post_meta($image_id, '_wpo-original-file');
418 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'));
419 439 } elseif (!wp_is_writable($image_path)) {
420 - $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'));
421 441 } elseif (!copy($backup_path, $image_path)) {
422 - $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'));
423 443 } elseif (!WP_Optimize_Utils::wp_delete_file($backup_path)) {
424 444 // translators: %s is the backup file path
425 445 $error = new WP_Error('restore_failed', sprintf(__('The backup file %s could not be deleted.', 'wp-optimize'), $backup_path));
426 446 }
@@ -427,12 +447,12 @@
427 447
428 448 if (!$error) {
429 449 // if backup image deleted successfully
430 450 // then delete from attachment meta associated smush data
431 - delete_post_meta($image_id, 'smush-complete');
432 - delete_post_meta($image_id, 'smush-stats');
433 - delete_post_meta($image_id, 'original-file');
434 - 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');
435 455 }
436 456
437 457 if ($switched_blog) {
438 458 restore_current_blog();
@@ -475,9 +495,9 @@
475 495 $processed = 0;
476 496
477 497 if ($restore_backup) {
478 498 // get post ids those have backup meta field.
479 - $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);
480 500
481 501 if (!empty($image_ids)) {
482 502 // run restore function for each found image.
483 503 foreach ($image_ids as $image) {
@@ -521,9 +541,9 @@
521 541 }
522 542
523 543 if ($result['completed']) {
524 544
525 - $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");
526 546 $result['smushed_images_count'] = $smushed_images_count;
527 547
528 548 if ($delete_only_backups_meta) {
529 549 if (is_multisite()) {
@@ -557,9 +577,9 @@
557 577 // clear all metas for smushed images after work completed.
558 578 // if $delete_only_backup_meta set to true then all meta fields was deleted in restore_single_image()
559 579 // and we don't need to delete metas for other images.
560 580 if (!$delete_only_backups_meta) {
561 - $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');");
562 582 }
563 583 }
564 584
565 585 if (is_multisite()) {
@@ -676,12 +696,12 @@
676 696 }
677 697
678 698 if (is_multisite()) {
679 699 switch_to_blog($task->get_option('blog_id', 1));
680 - $stats = get_post_meta($attachment_id, 'smush-stats', true);
700 + $stats = get_post_meta($attachment_id, '_wpo-smush-stats', true);
681 701 restore_current_blog();
682 702 } else {
683 - $stats = get_post_meta($attachment_id, 'smush-stats', true);
703 + $stats = get_post_meta($attachment_id, '_wpo-smush-stats', true);
684 704 }
685 705
686 706 if (isset($stats['sizes-info'])) {
687 707
@@ -837,14 +857,14 @@
837 857 * @param WP_Post $post - a post object
838 858 */
839 859 public function render_smush_metabox($post) {
840 860
841 - $compressed = (bool) get_post_meta($post->ID, 'smush-complete', true);
842 - $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);
843 863
844 - $smush_info = get_post_meta($post->ID, 'smush-info', true);
845 - $smush_stats = get_post_meta($post->ID, 'smush-stats', true);
846 - $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');
847 867
848 868 $options = Updraft_Smush_Manager()->get_smush_options();
849 869
850 870 $file = get_attached_file($post->ID);
@@ -1113,12 +1133,12 @@
1113 1133
1114 1134 foreach ($images as $image) {
1115 1135 if (is_multisite()) {
1116 1136 switch_to_blog($image['blog_id']);
1117 - $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';
1118 1138 restore_current_blog();
1119 1139 } else {
1120 - $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';
1121 1141 }
1122 1142 }
1123 1143
1124 1144 return array_count_values($stats);
@@ -1186,9 +1206,9 @@
1186 1206 $js_variables['cancel'] = esc_html__('Cancel', 'wp-optimize');
1187 1207 $js_variables['cancelling'] = esc_html__('Cancelling...', 'wp-optimize');
1188 1208 $js_variables['images_restored_successfully'] = esc_html__('The images were restored successfully', 'wp-optimize');
1189 1209 $js_variables['logo_src'] = esc_url(WPO_PLUGIN_URL.'images/notices/wp_optimize_logo.png');
1190 -
1210 +
1191 1211 wp_enqueue_script('block-ui-js', WPO_PLUGIN_URL.'includes/blockui/jquery.blockUI'.$min_or_not.'.js', array('jquery'), $enqueue_version);
1192 1212 wp_enqueue_script('wp-optimize-heartbeat-js', WPO_PLUGIN_URL.'js/heartbeat'.$min_or_not_internal.'.js', array('jquery'), $enqueue_version);
1193 1213 wp_localize_script('wp-optimize-heartbeat-js', 'wpo_heartbeat_ajax', array(
1194 1214 'ajaxurl' => admin_url('admin-ajax.php'),
@@ -1623,9 +1643,9 @@
1623 1643 *
1624 1644 * @return bool
1625 1645 */
1626 1646 public function is_compressed($attachment_id) {
1627 - return (true === (bool) get_post_meta($attachment_id, 'smush-complete', true));
1647 + return (true === (bool) get_post_meta($attachment_id, '_wpo-smush-complete', true));
1628 1648 }
1629 1649
1630 1650 /**
1631 1651 * Returns true if multisite
@@ -1641,12 +1661,19 @@
1641 1661 *
1642 1662 * @param int $post_id - WordPress Post ID
1643 1663 */
1644 1664 public function unscheduled_original_file_deletion($post_id) {
1645 - $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 + }
1646 1670 $uploads_dir = wp_get_upload_dir();
1647 - $the_original_file = trailingslashit($uploads_dir['basedir']) . $the_original_file;
1648 - if ('' != $the_original_file && file_exists($the_original_file)) {
1671 + $uploads_basedir = realpath($uploads_dir['basedir']);
1672 + if (!$uploads_basedir) return;
1673 + $uploads_basedir = trailingslashit($uploads_basedir);
1674 + $the_original_file = realpath($uploads_basedir . $the_original_file);
1675 + if ($the_original_file && 0 === strpos($the_original_file, $uploads_basedir) && file_exists($the_original_file)) {
1649 1676 wp_delete_file($the_original_file);
1650 1677 }
1651 1678 }
1652 1679
@@ -1686,14 +1713,14 @@
1686 1713 'relation' => 'AND',
1687 1714 array(
1688 1715 'relation' => 'OR',
1689 1716 array(
1690 - 'key' => 'smush-complete',
1717 + 'key' => '_wpo-smush-complete',
1691 1718 'compare' => '!=',
1692 1719 'value' => '1',
1693 1720 ),
1694 1721 array(
1695 - 'key' => 'smush-complete',
1722 + 'key' => '_wpo-smush-complete',
1696 1723 'compare' => 'NOT EXISTS',
1697 1724 'value' => '',
1698 1725 ),
1699 1726 ),