| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('ABSPATH')) die('Access denied.'); |
| 4 |
|
| 5 |
if (!class_exists('Updraft_Task_Manager_1_4')) require_once(WPO_PLUGIN_MAIN_PATH . 'vendor/team-updraft/common-libs/src/updraft-tasks/class-updraft-task-manager.php'); |
| 6 |
|
| 7 |
if (!class_exists('WPO_Webp_Task_Manager')) : |
| 8 |
|
| 9 |
class WPO_Webp_Task_Manager extends Updraft_Task_Manager_1_4 { |
| 10 |
|
| 11 |
/** |
| 12 |
* @var WPO_Webp_Task_Manager |
| 13 |
*/ |
| 14 |
static protected $_instance = null; |
| 15 |
|
| 16 |
/** |
| 17 |
* Logs a message using the WebP optimization instance. |
| 18 |
* |
| 19 |
* @param string $message The message to log. |
| 20 |
* @param string $error_type Optional. The type of error. Default 'info'. |
| 21 |
*/ |
| 22 |
public function log($message, $error_type = 'info') { |
| 23 |
$webp_instance = WP_Optimize()->get_webp_instance(); |
| 24 |
$webp_instance->log($message, $error_type); |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Instance of WP_Optimize_Webp_Task_Manager. |
| 29 |
* |
| 30 |
* @return self |
| 31 |
*/ |
| 32 |
public static function get_instance(): self { |
| 33 |
if (empty(self::$_instance)) { |
| 34 |
self::$_instance = new self(); |
| 35 |
} |
| 36 |
|
| 37 |
return self::$_instance; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Convert already compressed images to webp format |
| 42 |
*/ |
| 43 |
public function webp_convert_compressed_images() { |
| 44 |
$task_type = 'webp-convert-compressed-images-task'; |
| 45 |
|
| 46 |
$creating_tasks_semaphore = new Updraft_Semaphore_3_0('wpo_' . $task_type); |
| 47 |
$lock = $creating_tasks_semaphore->lock(); |
| 48 |
if (!$lock) return; |
| 49 |
|
| 50 |
if (is_multisite()) { |
| 51 |
$sites = WP_Optimize()->get_sites(); |
| 52 |
foreach ($sites as $site) { |
| 53 |
$blog_id = $site->blog_id; |
| 54 |
switch_to_blog($blog_id); |
| 55 |
$this->create_webp_convert_compressed_image_task($task_type, $blog_id); |
| 56 |
restore_current_blog(); |
| 57 |
} |
| 58 |
} else { |
| 59 |
$this->create_webp_convert_compressed_image_task($task_type, 1); |
| 60 |
} |
| 61 |
|
| 62 |
$creating_tasks_semaphore->release(); |
| 63 |
|
| 64 |
$this->process_queue($task_type); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Creates tasks for converting compressed images to WebP format |
| 69 |
* |
| 70 |
* @param string $task_type The type identifier for the conversion task |
| 71 |
* @param int $blog_id The ID of the blog/site where the images should be converted |
| 72 |
* |
| 73 |
* @return void |
| 74 |
*/ |
| 75 |
private function create_webp_convert_compressed_image_task(string $task_type, int $blog_id) { |
| 76 |
$this->clean_up_old_tasks($task_type); |
| 77 |
$images = $this->get_compressed_images_to_convert(); |
| 78 |
foreach ($images as $image) { |
| 79 |
$blog_info = ', Blog ID : '. $blog_id; |
| 80 |
$description = 'Webp Conversion of Compressed Image with ID - ' . $image['post_id'] . $blog_info; |
| 81 |
$options = array( |
| 82 |
'attachment_id' => $image['post_id'], |
| 83 |
'blog_id' => $blog_id, |
| 84 |
'attachment_source' => $image['source'], |
| 85 |
'anonymous_user_allowed' => (defined('DOING_CRON') && DOING_CRON) || (defined('WP_CLI') && WP_CLI) |
| 86 |
); |
| 87 |
WPO_Webp_Convert_Image_Task::create_task($task_type, $description, $options); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Get compressed images to convert |
| 93 |
* |
| 94 |
* @return array[] Array of arrays containing: |
| 95 |
* 'post_id' (int) The ID of the attachment post |
| 96 |
* 'source' (string) The file path of the attachment |
| 97 |
*/ |
| 98 |
private function get_compressed_images_to_convert(): array { |
| 99 |
$args = array( |
| 100 |
'meta_query' => $this->get_compressed_images_meta_query(), |
| 101 |
'post_type' => 'attachment', |
| 102 |
'numberposts' => 50, |
| 103 |
); |
| 104 |
|
| 105 |
$page = 1; |
| 106 |
$webp_converter = new WPO_WebP_Convert(); |
| 107 |
$posts = 0; |
| 108 |
$query_is_not_empty = true; |
| 109 |
$filtered_post_ids = array(); |
| 110 |
while ($posts < 50 && $query_is_not_empty) { |
| 111 |
$args['paged'] = $page; |
| 112 |
$query = get_posts($args); |
| 113 |
if (empty($query)) $query_is_not_empty = false; |
| 114 |
|
| 115 |
foreach ($query as $post) { |
| 116 |
$source = get_attached_file($post->ID); |
| 117 |
if (false === $source) continue; |
| 118 |
$destination = $webp_converter->get_destination_path($source); |
| 119 |
if (file_exists($destination)) { |
| 120 |
// A way of backfilling already webp converted images |
| 121 |
update_post_meta($post->ID, 'wpo-webp-conversion-complete', true); |
| 122 |
continue; |
| 123 |
} |
| 124 |
|
| 125 |
$filtered_post_ids[] = array( |
| 126 |
'post_id' => $post->ID, |
| 127 |
'source' => $source |
| 128 |
); |
| 129 |
$posts++; |
| 130 |
} |
| 131 |
$page++; |
| 132 |
}; |
| 133 |
|
| 134 |
return $filtered_post_ids; |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Meta query array for getting compressed images |
| 139 |
* |
| 140 |
* @return array |
| 141 |
*/ |
| 142 |
private function get_compressed_images_meta_query(): array { |
| 143 |
return array( |
| 144 |
'relation' => 'AND', |
| 145 |
array( |
| 146 |
'key' => 'smush-complete', |
| 147 |
'compare' => '=', |
| 148 |
'value' => '1', |
| 149 |
), |
| 150 |
// Check if the image is not already converted to webp |
| 151 |
array( |
| 152 |
'relation' => 'OR', |
| 153 |
array( |
| 154 |
'key' => 'wpo-webp-conversion-complete', |
| 155 |
'compare' => 'NOT EXISTS', |
| 156 |
'value' => '', |
| 157 |
), |
| 158 |
array( |
| 159 |
'key' => 'wpo-webp-conversion-complete', |
| 160 |
'compare' => '!=', |
| 161 |
'value' => '1', |
| 162 |
) |
| 163 |
), |
| 164 |
); |
| 165 |
} |
| 166 |
} |
| 167 |
endif; |
| 168 |
|