PluginProbe
ezCache / 1.3
ezCache v1.3
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, at includes/BackgroundProcesses/ConvertWebpProcess.php

161 lines 4.5 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( "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( 'WebP Background Processor: invalid transient data received' );
104 return;
105 }
106
107 foreach ( $data as $cache_file => $images ) {
108 $contents = file_get_contents( $cache_file );
109
110 foreach ( $images as $image_data ) {
111 $contents = str_replace( $image_data['image_url'], $image_data['webp_url'], $contents );
112 }
113
114 @file_put_contents( $cache_file, $contents );
115 @file_put_contents( $cache_file . '.gz', gzencode( $contents, 6, FORCE_GZIP ) );
116 }
117
118 $wpdb->query( "OPTIMIZE TABLE `{$wpdb->prefix}ezcache_webp_images`" );
119 }
120
121 /**
122 * Save a link to replace in a cached file
123 *
124 * @param string $cache_file
125 * @param string $image_url
126 * @param string $webp_url
127 */
128 protected function append_link_to_replace( $cache_file, $image_url, $webp_url ) {
129 $transient = get_site_option( $this->action . '_reprocess_queue' );
130 if ( ! $transient ) {
131 $transient = [];
132 }
133
134 if ( ! isset( $transient[ $cache_file ] ) ) {
135 $transient[ $cache_file ] = [];
136 }
137
138 $transient[ $cache_file ][] = [
139 'image_url' => $image_url,
140 'webp_url' => $webp_url,
141 ];
142
143 update_site_option( $this->action . '_reprocess_queue', $transient );
144 }
145
146 /**
147 * Get the links we need to replace in the cached file
148 *
149 * @return bool|array
150 */
151 protected function get_links_to_replace() {
152 $transient = get_site_option( $this->action . '_reprocess_queue' );
153 if ( ! $transient ) {
154 return false;
155 }
156
157 update_site_option( $this->action . '_reprocess_queue', [] );
158 return $transient;
159 }
160 }
161