| 1 |
<?php |
| 2 |
namespace Upress\EzCache\BackgroundProcesses; |
| 3 |
|
| 4 |
use Upress\EzCache\LicenseApi; |
| 5 |
use Upress\EzCache\WebpApi; |
| 6 |
use \WP_Background_Process; |
| 7 |
|
| 8 |
class ConvertWebpProcess extends WP_Background_Process { |
| 9 |
/** |
| 10 |
* @var string |
| 11 |
*/ |
| 12 |
protected $action = 'ezcache_convert_images_to_webp'; |
| 13 |
|
| 14 |
/** |
| 15 |
* Task |
| 16 |
* |
| 17 |
* Override this method to perform any actions required on each |
| 18 |
* queue item. Return the modified item for further processing |
| 19 |
* in the next pass through. Or, return false to remove the |
| 20 |
* item from the queue. |
| 21 |
* |
| 22 |
* @param mixed $item Queue item to iterate over. |
| 23 |
* |
| 24 |
* @return mixed |
| 25 |
*/ |
| 26 |
protected function task( $item ) { |
| 27 |
global $wpdb; |
| 28 |
|
| 29 |
$row_id = $item['row_id']; |
| 30 |
$image_url = $item['image_url']; |
| 31 |
$image_path = $item['image_path']; |
| 32 |
$cache_file = $item['cache_file']; |
| 33 |
$webp_url = $item['image_webp_url']; |
| 34 |
$webp_path = $item['image_webp_path']; |
| 35 |
|
| 36 |
$image = $wpdb->get_row( |
| 37 |
$wpdb->prepare( |
| 38 |
"SELECT * FROM `{$wpdb->prefix}ezcache_webp_images` WHERE `id` = %s LIMIT 1", |
| 39 |
[ $row_id ] |
| 40 |
) |
| 41 |
); |
| 42 |
|
| 43 |
if ( $image && 'completed' == $image->status ) { |
| 44 |
// we need to update the cache file |
| 45 |
$this->append_link_to_replace( $cache_file, $image_url, $webp_url ); |
| 46 |
return false; |
| 47 |
} |
| 48 |
|
| 49 |
// only download the file if we don't have it locally |
| 50 |
if ( ! file_exists( $webp_path ) ) { |
| 51 |
$license = new LicenseApi(); |
| 52 |
$converter = new WebpApi( $license->get_license_key() ); |
| 53 |
$response = $converter->convert( $image_path ); |
| 54 |
|
| 55 |
if ( is_wp_error( $response ) || stripos( $response['info']['content_type'], 'json' ) ) { |
| 56 |
$wpdb->query( |
| 57 |
$wpdb->prepare( |
| 58 |
"UPDATE `{$wpdb->prefix}ezcache_webp_images` SET `status` = %s WHERE `id` = %d", |
| 59 |
[ 'failed', $row_id ] |
| 60 |
) |
| 61 |
); |
| 62 |
error_log( "WebP Background Processor: " . (is_wp_error( $response ) ? $response->get_error_message() : $response['data']) ); |
| 63 |
|
| 64 |
return false; |
| 65 |
} |
| 66 |
|
| 67 |
file_put_contents( $webp_path, $response['data'] ); |
| 68 |
} |
| 69 |
|
| 70 |
$original_size = filesize( $image_path ); |
| 71 |
$webp_size = filesize( $webp_path ); |
| 72 |
|
| 73 |
$wpdb->query( |
| 74 |
$wpdb->prepare( |
| 75 |
"UPDATE `{$wpdb->prefix}ezcache_webp_images` SET `status` = %s, `original_size` = %d, `webp_size` = %d WHERE `id` = %d", |
| 76 |
[ 'completed', $original_size, $webp_size, $row_id ] |
| 77 |
) |
| 78 |
); |
| 79 |
|
| 80 |
$this->append_link_to_replace( $cache_file, $image_url, $webp_url ); |
| 81 |
|
| 82 |
sleep(5); |
| 83 |
|
| 84 |
// return false to remove the item from the queue |
| 85 |
return false; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Complete |
| 90 |
* |
| 91 |
* Override if applicable, but ensure that the below actions are |
| 92 |
* performed, or, call parent::complete(). |
| 93 |
*/ |
| 94 |
protected function complete() { |
| 95 |
global $wpdb; |
| 96 |
parent::complete(); |
| 97 |
|
| 98 |
$data = $this->get_links_to_replace(); |
| 99 |
if ( ! $data ) { |
| 100 |
error_log( 'WebP Background Processor: invalid transient data received' ); |
| 101 |
return; |
| 102 |
} |
| 103 |
|
| 104 |
foreach ( $data as $cache_file => $images ) { |
| 105 |
$contents = file_get_contents( $cache_file ); |
| 106 |
|
| 107 |
foreach ( $images as $image_data ) { |
| 108 |
$contents = str_replace( $image_data['image_url'], $image_data['webp_url'], $contents ); |
| 109 |
} |
| 110 |
|
| 111 |
@file_put_contents( $cache_file, $contents ); |
| 112 |
@file_put_contents( $cache_file . '.gz', gzencode( $contents, 6, FORCE_GZIP ) ); |
| 113 |
} |
| 114 |
|
| 115 |
$wpdb->query( "OPTIMIZE TABLE `{$wpdb->prefix}ezcache_webp_images`" ); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Save a link to replace in a cached file |
| 120 |
* |
| 121 |
* @param string $cache_file |
| 122 |
* @param string $image_url |
| 123 |
* @param string $webp_url |
| 124 |
*/ |
| 125 |
protected function append_link_to_replace( $cache_file, $image_url, $webp_url ) { |
| 126 |
$transient = get_site_option( $this->action . '_reprocess_queue' ); |
| 127 |
if ( ! $transient ) { |
| 128 |
$transient = []; |
| 129 |
} |
| 130 |
|
| 131 |
if ( ! isset( $transient[ $cache_file ] ) ) { |
| 132 |
$transient[ $cache_file ] = []; |
| 133 |
} |
| 134 |
|
| 135 |
$transient[ $cache_file ][] = [ |
| 136 |
'image_url' => $image_url, |
| 137 |
'webp_url' => $webp_url, |
| 138 |
]; |
| 139 |
|
| 140 |
update_site_option( $this->action . '_reprocess_queue', $transient ); |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Get the links we need to replace in the cached file |
| 145 |
* |
| 146 |
* @return bool|array |
| 147 |
*/ |
| 148 |
protected function get_links_to_replace() { |
| 149 |
$transient = get_site_option( $this->action . '_reprocess_queue' ); |
| 150 |
if ( ! $transient ) { |
| 151 |
return false; |
| 152 |
} |
| 153 |
|
| 154 |
update_site_option( $this->action . '_reprocess_queue', [] ); |
| 155 |
return $transient; |
| 156 |
} |
| 157 |
} |
| 158 |
|