PluginProbe
ezCache / 1.3.11
ezCache v1.3.11
2.6.5 2.6.4 2.6.2 2.6.3 2.6.1 2.6.0 2.5.6 2.5.5 2.5.4 2.5.3 2.5.2 2.5.1 2.5 2.2.1 2.2.2 trunk 1.2 1.2.1 1.2.2 1.2.3 1.2.4 1.3 1.3.1 1.3.10 1.3.11 All 49 releases
ezcache / includes / BackgroundProcesses / ConvertWebpProcess.php

ConvertWebpProcess.php in ezCache 1.3.11, at includes/BackgroundProcesses/ConvertWebpProcess.php

178 lines 4.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 ) || filesize( $webp_path ) <= 2 || stripos( file_get_contents( $webp_path ), '"success":false' ) ) {
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' ) || stripos( $response['data'], '"success":false' ) ) {
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( "ezCache WebP Background Processor: " . (is_wp_error( $response ) ? $response->get_error_message() : $response['data']) );
63
64 // delete the broken file
65 if ( file_exists( $webp_path ) ) {
66 unlink( $webp_path );
67 }
68
69 return false;
70 }
71
72 file_put_contents( $webp_path, $response['data'] );
73 }
74
75 $original_size = filesize( $image_path );
76 $webp_size = filesize( $webp_path );
77
78 $wpdb->query(
79 $wpdb->prepare(
80 "UPDATE `{$wpdb->prefix}ezcache_webp_images` SET `status` = %s, `original_size` = %d, `webp_size` = %d WHERE `id` = %d",
81 [ 'completed', $original_size, $webp_size, $row_id ]
82 )
83 );
84
85 $this->append_link_to_replace( $cache_file, $image_url, $webp_url );
86
87 // return false to remove the item from the queue
88 return false;
89 }
90
91 /**
92 * Complete
93 *
94 * Override if applicable, but ensure that the below actions are
95 * performed, or, call parent::complete().
96 */
97 protected function complete() {
98 global $wpdb;
99 parent::complete();
100
101 $data = $this->get_links_to_replace();
102 if ( ! $data ) {
103 error_log( 'ezCache WebP Background Processor: invalid transient data received' );
104 return;
105 }
106
107 foreach ( $data as $cache_file => $images ) {
108 if ( ! file_exists( $cache_file ) ) {
109 // File has been deleted? maybe the cache got cleared before the process finished
110 continue;
111 }
112
113 $handle = @fopen( $cache_file, 'c+' );
114
115 if ( $handle && @flock( $handle, LOCK_EX ) ) {
116 $contents = gzdecode( fread( $handle, filesize( $cache_file ) ) );
117
118 foreach ( $images as $image_data ) {
119 $contents = str_replace( $image_data['image_url'], $image_data['webp_url'], $contents );
120 }
121
122 ftruncate( $handle, 0 );
123 fseek( $handle, 0 );
124 fwrite( $handle, gzencode( $contents, 6, FORCE_GZIP ) );
125 flock( $handle, LOCK_UN );
126 } else {
127 error_log( "ezCache WebP Background Processor: Could not get a lock on {$cache_file}, failed updating WebP image URLs" );
128 }
129
130 if ( $handle ) {
131 fclose( $handle );
132 }
133 }
134
135 $wpdb->query( "OPTIMIZE TABLE `{$wpdb->prefix}ezcache_webp_images`" );
136 }
137
138 /**
139 * Save a link to replace in a cached file
140 *
141 * @param string $cache_file
142 * @param string $image_url
143 * @param string $webp_url
144 */
145 protected function append_link_to_replace( $cache_file, $image_url, $webp_url ) {
146 $transient = get_site_option( $this->action . '_reprocess_queue' );
147 if ( ! $transient ) {
148 $transient = [];
149 }
150
151 if ( ! isset( $transient[ $cache_file ] ) ) {
152 $transient[ $cache_file ] = [];
153 }
154
155 $transient[ $cache_file ][] = [
156 'image_url' => $image_url,
157 'webp_url' => $webp_url,
158 ];
159
160 update_site_option( $this->action . '_reprocess_queue', $transient );
161 }
162
163 /**
164 * Get the links we need to replace in the cached file
165 *
166 * @return bool|array
167 */
168 protected function get_links_to_replace() {
169 $transient = get_site_option( $this->action . '_reprocess_queue' );
170 if ( ! $transient ) {
171 return false;
172 }
173
174 update_site_option( $this->action . '_reprocess_queue', [] );
175 return $transient;
176 }
177 }
178