| 1 |
<?php |
| 2 |
namespace Imagify\Bulk; |
| 3 |
|
| 4 |
defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' ); |
| 5 |
|
| 6 |
/** |
| 7 |
* Class to use for bulk for custom folders. |
| 8 |
* |
| 9 |
* @since 1.9 |
| 10 |
* @author Grégory Viguier |
| 11 |
*/ |
| 12 |
class CustomFolders extends AbstractBulk { |
| 13 |
|
| 14 |
/** |
| 15 |
* Context "short name". |
| 16 |
* |
| 17 |
* @var string |
| 18 |
* @since 1.9 |
| 19 |
* @access protected |
| 20 |
* @author Grégory Viguier |
| 21 |
*/ |
| 22 |
protected $context = 'custom-folders'; |
| 23 |
|
| 24 |
/** |
| 25 |
* Get all unoptimized media ids. |
| 26 |
* |
| 27 |
* @since 1.9 |
| 28 |
* @access public |
| 29 |
* @author Grégory Viguier |
| 30 |
* |
| 31 |
* @param int $optimization_level The optimization level. |
| 32 |
* @return array A list of unoptimized media. Array keys are media IDs prefixed with an underscore character, array values are the main file’s URL. |
| 33 |
*/ |
| 34 |
public function get_unoptimized_media_ids( $optimization_level ) { |
| 35 |
@set_time_limit( 0 ); |
| 36 |
|
| 37 |
/** |
| 38 |
* Get the folders from DB. |
| 39 |
*/ |
| 40 |
$folders = \Imagify_Custom_Folders::get_folders( array( |
| 41 |
'active' => true, |
| 42 |
) ); |
| 43 |
|
| 44 |
if ( ! $folders ) { |
| 45 |
return []; |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Triggered before getting file IDs. |
| 50 |
* |
| 51 |
* @since 1.7 |
| 52 |
* @author Grégory Viguier |
| 53 |
* |
| 54 |
* @param array $folders An array of folders data. |
| 55 |
* @param int $optimization_level The optimization level that will be used for the optimization. |
| 56 |
*/ |
| 57 |
do_action( 'imagify_bulk_optimize_files_before_get_files', $folders, $optimization_level ); |
| 58 |
|
| 59 |
/** |
| 60 |
* Get the files from DB, and from the folders. |
| 61 |
*/ |
| 62 |
$files = \Imagify_Custom_Folders::get_files_from_folders( $folders, [ |
| 63 |
'optimization_level' => $optimization_level, |
| 64 |
] ); |
| 65 |
|
| 66 |
if ( ! $files ) { |
| 67 |
return []; |
| 68 |
} |
| 69 |
|
| 70 |
// We need to output file URLs. |
| 71 |
foreach ( $files as $k => $file ) { |
| 72 |
$files[ $k ] = esc_url( \Imagify_Files_Scan::remove_placeholder( $file['path'], 'url' ) ); |
| 73 |
} |
| 74 |
|
| 75 |
return $files; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Get ids of all optimized media without webp versions. |
| 80 |
* |
| 81 |
* @since 1.9 |
| 82 |
* @since 1.9.5 The method doesn't return the IDs directly anymore. |
| 83 |
* @access public |
| 84 |
* @author Grégory Viguier |
| 85 |
* |
| 86 |
* @return array { |
| 87 |
* @type array $ids A list of media IDs. |
| 88 |
* @type array $errors { |
| 89 |
* @type array $no_file_path A list of media IDs. |
| 90 |
* @type array $no_backup A list of media IDs. |
| 91 |
* } |
| 92 |
* } |
| 93 |
*/ |
| 94 |
public function get_optimized_media_ids_without_webp() { |
| 95 |
global $wpdb; |
| 96 |
|
| 97 |
@set_time_limit( 0 ); |
| 98 |
|
| 99 |
$files_table = \Imagify_Files_DB::get_instance()->get_table_name(); |
| 100 |
$folders_table = \Imagify_Folders_DB::get_instance()->get_table_name(); |
| 101 |
$mime_types = \Imagify_DB::get_mime_types( 'image' ); |
| 102 |
$webp_suffix = constant( imagify_get_optimization_process_class_name( 'custom-folders' ) . '::WEBP_SUFFIX' ); |
| 103 |
$files = $wpdb->get_results( $wpdb->prepare( // WPCS: unprepared SQL ok. |
| 104 |
" |
| 105 |
SELECT fi.file_id, fi.path |
| 106 |
FROM $files_table as fi |
| 107 |
INNER JOIN $folders_table AS fo |
| 108 |
ON ( fi.folder_id = fo.folder_id ) |
| 109 |
WHERE |
| 110 |
fi.mime_type IN ( $mime_types ) |
| 111 |
AND ( fi.status = 'success' OR fi.status = 'already_optimized' ) |
| 112 |
AND ( fi.data NOT LIKE %s OR fi.data IS NULL ) |
| 113 |
ORDER BY fi.file_id DESC", |
| 114 |
'%' . $wpdb->esc_like( $webp_suffix . '";a:4:{s:7:"success";b:1;' ) . '%' |
| 115 |
) ); |
| 116 |
|
| 117 |
$wpdb->flush(); |
| 118 |
unset( $mime_types, $files_table, $folders_table, $webp_suffix ); |
| 119 |
|
| 120 |
$data = [ |
| 121 |
'ids' => [], |
| 122 |
'errors' => [ |
| 123 |
'no_file_path' => [], |
| 124 |
'no_backup' => [], |
| 125 |
], |
| 126 |
]; |
| 127 |
|
| 128 |
if ( ! $files ) { |
| 129 |
return $data; |
| 130 |
} |
| 131 |
|
| 132 |
foreach ( $files as $file ) { |
| 133 |
$file_id = absint( $file->file_id ); |
| 134 |
|
| 135 |
if ( empty( $file->path ) ) { |
| 136 |
// Problem. |
| 137 |
$data['errors']['no_file_path'][] = $file_id; |
| 138 |
continue; |
| 139 |
} |
| 140 |
|
| 141 |
$file_path = \Imagify_Files_Scan::remove_placeholder( $file->path ); |
| 142 |
$backup_path = \Imagify_Custom_Folders::get_file_backup_path( $file_path ); |
| 143 |
|
| 144 |
if ( ! $this->filesystem->exists( $backup_path ) ) { |
| 145 |
// No backup, no webp. |
| 146 |
$data['errors']['no_backup'][] = $file_id; |
| 147 |
continue; |
| 148 |
} |
| 149 |
|
| 150 |
$data['ids'][] = $file_id; |
| 151 |
} // End foreach(). |
| 152 |
|
| 153 |
return $data; |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Tell if there are optimized media without webp versions. |
| 158 |
* |
| 159 |
* @since 1.9 |
| 160 |
* @access public |
| 161 |
* @author Grégory Viguier |
| 162 |
* |
| 163 |
* @return int The number of media. |
| 164 |
*/ |
| 165 |
public function has_optimized_media_without_webp() { |
| 166 |
global $wpdb; |
| 167 |
|
| 168 |
$files_table = \Imagify_Files_DB::get_instance()->get_table_name(); |
| 169 |
$folders_table = \Imagify_Folders_DB::get_instance()->get_table_name(); |
| 170 |
$mime_types = \Imagify_DB::get_mime_types( 'image' ); |
| 171 |
$webp_suffix = constant( imagify_get_optimization_process_class_name( 'custom-folders' ) . '::WEBP_SUFFIX' ); |
| 172 |
|
| 173 |
return (int) $wpdb->get_var( $wpdb->prepare( // WPCS: unprepared SQL ok. |
| 174 |
" |
| 175 |
SELECT COUNT(fi.file_id) |
| 176 |
FROM $files_table as fi |
| 177 |
INNER JOIN $folders_table AS fo |
| 178 |
ON ( fi.folder_id = fo.folder_id ) |
| 179 |
WHERE |
| 180 |
fi.mime_type IN ( $mime_types ) |
| 181 |
AND ( fi.status = 'success' OR fi.status = 'already_optimized' ) |
| 182 |
AND ( fi.data NOT LIKE %s OR fi.data IS NULL )", |
| 183 |
'%' . $wpdb->esc_like( $webp_suffix . '";a:4:{s:7:"success";b:1;' ) . '%' |
| 184 |
) ); |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Get the context data. |
| 189 |
* |
| 190 |
* @since 1.9 |
| 191 |
* @access public |
| 192 |
* @author Grégory Viguier |
| 193 |
* |
| 194 |
* @return array { |
| 195 |
* The formated data. |
| 196 |
* |
| 197 |
* @type string $count-optimized Number of media optimized. |
| 198 |
* @type string $count-errors Number of media having an optimization error, with a link to the page listing the optimization errors. |
| 199 |
* @type string $optimized-size Optimized filesize. |
| 200 |
* @type string $original-size Original filesize. |
| 201 |
* } |
| 202 |
*/ |
| 203 |
public function get_context_data() { |
| 204 |
$data = array( |
| 205 |
'count-optimized' => \Imagify_Files_Stats::count_optimized_files(), |
| 206 |
'count-errors' => \Imagify_Files_Stats::count_error_files(), |
| 207 |
'optimized-size' => \Imagify_Files_Stats::get_optimized_size(), |
| 208 |
'original-size' => \Imagify_Files_Stats::get_original_size(), |
| 209 |
'errors_url' => get_imagify_admin_url( 'folder-errors', $this->context ), |
| 210 |
); |
| 211 |
|
| 212 |
return $this->format_context_data( $data ); |
| 213 |
} |
| 214 |
} |
| 215 |
|