| 1 |
<?php |
| 2 |
defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' ); |
| 3 |
|
| 4 |
/** |
| 5 |
* Compat class for Enable Media Replace plugin. |
| 6 |
* |
| 7 |
* @since 1.8.4 |
| 8 |
* @deprecated |
| 9 |
*/ |
| 10 |
class Imagify_Enable_Media_Replace_Deprecated { |
| 11 |
|
| 12 |
/** |
| 13 |
* The attachment ID. |
| 14 |
* |
| 15 |
* @var int |
| 16 |
* @since 1.6.9 |
| 17 |
* @since 1.9 Deprecated |
| 18 |
* @deprecated |
| 19 |
*/ |
| 20 |
protected $attachment_id; |
| 21 |
|
| 22 |
/** |
| 23 |
* The attachment. |
| 24 |
* |
| 25 |
* @var Imagify_Attachment |
| 26 |
* @since 1.6.9 |
| 27 |
* @since 1.9 Deprecated |
| 28 |
* @deprecated |
| 29 |
*/ |
| 30 |
protected $attachment; |
| 31 |
|
| 32 |
/** |
| 33 |
* Tell if the attachment has data. |
| 34 |
* No data means not processed by Imagify, or restored. |
| 35 |
* |
| 36 |
* @var bool |
| 37 |
* @since 1.8.4 |
| 38 |
* @since 1.9 Deprecated |
| 39 |
* @deprecated |
| 40 |
*/ |
| 41 |
protected $attachment_has_data; |
| 42 |
|
| 43 |
/** |
| 44 |
* Filesystem object. |
| 45 |
* |
| 46 |
* @var object Imagify_Filesystem |
| 47 |
* @since 1.7.1 |
| 48 |
* @since 1.8.4 Deprecated |
| 49 |
* @deprecated |
| 50 |
*/ |
| 51 |
protected $filesystem; |
| 52 |
|
| 53 |
/** |
| 54 |
* Optimize the attachment files if the old ones were also optimized. |
| 55 |
* Delete the old backup file. |
| 56 |
* |
| 57 |
* @since 1.6.9 |
| 58 |
* @since 1.8.4 Deprecated. |
| 59 |
* @see $this->store_old_backup_path() |
| 60 |
* @deprecated |
| 61 |
* |
| 62 |
* @param string $return_url The URL the user will be redirected to. |
| 63 |
* @return string The same URL. |
| 64 |
*/ |
| 65 |
public function optimize( $return_url ) { |
| 66 |
_deprecated_function( get_class( $this ) . '::' . __FUNCTION__ . '()', '1.8.4' ); |
| 67 |
|
| 68 |
$attachment = $this->get_attachment(); |
| 69 |
|
| 70 |
if ( $attachment->get_data() ) { |
| 71 |
/** |
| 72 |
* The old images have been optimized in the past. |
| 73 |
*/ |
| 74 |
// Use the same otimization level for the new ones. |
| 75 |
$optimization_level = $attachment->get_optimization_level(); |
| 76 |
|
| 77 |
// Remove old optimization data. |
| 78 |
$attachment->delete_imagify_data(); |
| 79 |
|
| 80 |
// Optimize and overwrite the previous backup file if exists and needed. |
| 81 |
add_filter( 'imagify_backup_overwrite_backup', '__return_true', 42 ); |
| 82 |
$attachment->optimize( $optimization_level ); |
| 83 |
remove_filter( 'imagify_backup_overwrite_backup', '__return_true', 42 ); |
| 84 |
} |
| 85 |
|
| 86 |
$filesystem = Imagify_Filesystem::get_instance(); |
| 87 |
|
| 88 |
/** |
| 89 |
* Delete the old backup file. |
| 90 |
*/ |
| 91 |
if ( ! $this->old_backup_path || ! $filesystem->exists( $this->old_backup_path ) ) { |
| 92 |
// The user didn't choose to rename the files, or there is no old backup. |
| 93 |
$this->old_backup_path = null; |
| 94 |
return $return_url; |
| 95 |
} |
| 96 |
|
| 97 |
$new_backup_path = $attachment->get_raw_backup_path(); |
| 98 |
|
| 99 |
if ( $new_backup_path === $this->old_backup_path ) { |
| 100 |
// We don't want to delete the new backup. |
| 101 |
$this->old_backup_path = null; |
| 102 |
return $return_url; |
| 103 |
} |
| 104 |
|
| 105 |
// Finally, delete the old backup file. |
| 106 |
$filesystem->delete( $this->old_backup_path ); |
| 107 |
|
| 108 |
$this->old_backup_path = null; |
| 109 |
return $return_url; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* When the user chooses to change the file name, store the old backup file path. This path will be used later to delete the file. |
| 114 |
* |
| 115 |
* @since 1.6.9 |
| 116 |
* @since 1.9.10 Deprecated. |
| 117 |
* @deprecated |
| 118 |
* |
| 119 |
* @param string $new_filename The new file name. |
| 120 |
* @param string $current_path The current file path. |
| 121 |
* @param int $post_id The attachment ID. |
| 122 |
* @return string The same file name. |
| 123 |
*/ |
| 124 |
public function store_old_backup_path( $new_filename, $current_path, $post_id ) { |
| 125 |
_deprecated_function( get_class( $this ) . '::' . __FUNCTION__ . '()', '1.9.10' ); |
| 126 |
|
| 127 |
if ( ! $this->media_id || $post_id !== $this->media_id ) { |
| 128 |
return $new_filename; |
| 129 |
} |
| 130 |
|
| 131 |
$this->get_process(); |
| 132 |
|
| 133 |
if ( ! $this->process ) { |
| 134 |
$this->media_id = 0; |
| 135 |
return $new_filename; |
| 136 |
} |
| 137 |
|
| 138 |
$media = $this->process->get_media(); |
| 139 |
$backup_path = $media->get_backup_path(); |
| 140 |
|
| 141 |
if ( $backup_path ) { |
| 142 |
$this->old_backup_path = $backup_path; |
| 143 |
|
| 144 |
// Keep track of existing WebP files. |
| 145 |
$media_files = $media->get_media_files(); |
| 146 |
|
| 147 |
if ( $media_files ) { |
| 148 |
foreach ( $media_files as $media_file ) { |
| 149 |
$this->old_webp_paths[] = imagify_path_to_webp( $media_file['path'] ); |
| 150 |
} |
| 151 |
} |
| 152 |
} else { |
| 153 |
$this->media_id = 0; |
| 154 |
$this->old_backup_path = false; |
| 155 |
$this->old_webp_paths = []; |
| 156 |
} |
| 157 |
|
| 158 |
return $new_filename; |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Get the attachment. |
| 163 |
* |
| 164 |
* @since 1.6.9 |
| 165 |
* @since 1.9 Deprecated. |
| 166 |
* @deprecated |
| 167 |
* |
| 168 |
* @return object A Imagify_Attachment object (or any class extending it). |
| 169 |
*/ |
| 170 |
protected function get_attachment() { |
| 171 |
if ( $this->attachment ) { |
| 172 |
return $this->attachment; |
| 173 |
} |
| 174 |
|
| 175 |
_deprecated_function( get_class( $this ) . '::' . __FUNCTION__ . '()', '1.9', '\\Imagify\\ThirdParty\\EnableMediaReplace\\Main::get_instance()->get_process()' ); |
| 176 |
|
| 177 |
$this->attachment = get_imagify_attachment( 'wp', $this->attachment_id, 'enable_media_replace' ); |
| 178 |
|
| 179 |
return $this->attachment; |
| 180 |
} |
| 181 |
} |
| 182 |
|