PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / trunk
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF vtrunk
2.3.4 2.3.3 2.3.2 2.3.1 2.3.0 2.2.9 2.2.8 trunk 1.10 1.3.3 1.3.4 1.3.5 1.3.5.1 1.3.5.2 1.3.6 1.3.6.1 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.5 All 103 releases
← All changes | inc/functions/process.php +44 -149 1.5 → trunk View file →
@@ -1,171 +1,66 @@
1 1 <?php
2 -defined( 'ABSPATH' ) or die( 'Cheatin\' uh?' );
2 +defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' );
3 3
4 4 /**
5 - * Process an image with Imagify.
5 + * Run an async job to optimize images in background.
6 6 *
7 - * @since 1.0
7 + * @param array $body Contains the usual $_POST.
8 8 *
9 - * @param string $file_path Absolute path to the image file.
10 - * @param bool $backup Force a backup of the original file.
11 - * @param int $optimization_level The optimization level (2=ultra, 1=aggressive, 0=normal).
12 - * @param array $resize The resize parameters (with & height).
13 - * @param bool $keep_exif To keep exif data or not
14 - * @return obj|array Error message | Optimized image data
9 + * @since 1.4
15 10 */
16 -function do_imagify( $file_path, $args = array() ) {
17 - $errors = new WP_Error();
18 - $args = array_merge(
19 - array(
20 - 'backup' => get_imagify_option( 'backup', false ),
21 - 'optimization_level' => get_imagify_option( 'optimization_level', 1 ),
22 - 'resize' => array(),
23 - 'keep_exif' => get_imagify_option( 'exif', false ),
24 - 'context' => 'wp',
25 - 'original_size' => 0
26 - ),
27 - $args
28 - );
29 -
11 +function imagify_do_async_job( $body ) {
12 + $args = [
13 + 'timeout' => 0.01,
14 + 'blocking' => false,
15 + 'body' => $body,
16 + 'cookies' => isset( $_COOKIE ) && is_array( $_COOKIE ) ? $_COOKIE : [],
17 + 'sslverify' => apply_filters( 'https_local_ssl_verify', false ),
18 + ];
19 +
30 20 /**
31 - * Filter the attachment path
21 + * Filter the arguments used to launch an async job.
32 22 *
33 - * @since 1.2
23 + * @since 1.6.6
24 + * @author Grégory Viguier
34 25 *
35 - * @param string $file_path The attachment path
26 + * @param array $args An array of arguments passed to wp_remote_post().
36 27 */
37 - $file_path = apply_filters( 'imagify_file_path', $file_path );
38 -
39 - // Check if the Imagify servers & the API are accessible
40 - if ( ! is_imagify_servers_up() ) {
41 - $errors->add( 'api_server_down', __( 'Sorry, our servers are temporarily unaccessible. Please, try again in a couple of minutes.', 'imagify' ) );
42 - return $errors;
43 - }
44 -
45 - // Check if external HTTP requests are blocked.
46 - if ( is_imagify_blocked() ) {
47 - $errors->add( 'http_block_external', __( 'External HTTP requests are blocked', 'imagify' ) );
48 - return $errors;
49 - }
50 -
51 - // Check that file path isn't empty
52 - if ( empty( $file_path ) ) {
53 - $errors->add( 'empty_path', __( 'File path is empty', 'imagify' ) );
54 - return $errors;
55 - }
28 + $args = apply_filters( 'imagify_do_async_job_args', $args );
56 29
57 - // Check that the file exists
58 - if ( ! file_exists( $file_path ) || ! is_file( $file_path ) ) {
59 - $errors->add( 'file_not_found', sprintf( __( 'Could not find %s', 'imagify' ), $file_path ) );
60 - }
61 -
62 - // Check that the file is writable
63 - if ( ! is_writable( dirname( $file_path ) ) ) {
64 - $errors->add( 'not_writable', sprintf( __( "%s is not writable", 'imagify' ), dirname( $file_path ) ) );
65 - return $errors;
66 - }
67 -
68 - // Get file size
69 - $file_size = file_exists( $file_path ) ? filesize( $file_path ) : 0;
70 -
71 - // Check that file exists
72 - if ( 0 == $file_size ) {
73 - $errors->add( 'image_not_found', sprintf( __( 'Skipped (%s), image not found.', 'imagify' ), size_format( $file_size ) ) );
74 - return $errors;
75 - }
76 -
77 30 /**
78 - * Fires before to optimize the Image with Imagify.
79 - *
80 - * @since 1.0
81 - *
82 - * @param string $file_path Absolute path to the image file.
83 - * @param bool $backup Force a backup of the original file.
84 - */
85 - do_action( 'before_do_imagify', $file_path, $args['backup'] );
31 + * It can be a XML-RPC request. The problem is that XML-RPC doesn't use cookies.
32 + */
33 + if ( defined( 'XMLRPC_REQUEST' ) && get_current_user_id() ) {
34 + /**
35 + * In old WP versions, the field "option_name" in the wp_options table was limited to 64 characters.
36 + * From 64, remove 19 characters for "_transient_timeout_" = 45.
37 + * Then remove 12 characters for "imagify_rpc_" (transient name) = 33.
38 + * Luckily, a md5 is 32 characters long.
39 + */
40 + $rpc_id = md5( maybe_serialize( $body ) );
86 41
87 - // Send image for optimization and fetch the response
88 - $response = upload_imagify_image(
89 - array(
90 - 'image' => curl_file_create( $file_path ),
91 - 'data' => json_encode(
92 - array(
93 - 'aggressive' => ( 1 === (int) $args['optimization_level'] ) ? true : false,
94 - 'ultra' => ( 2 === (int) $args['optimization_level'] ) ? true : false,
95 - 'resize' => $args['resize'],
96 - 'keep_exif' => $args['keep_exif'],
97 - 'context' => $args['context'],
98 - 'original_size' => $args['original_size']
99 - )
100 - )
101 - )
102 - );
42 + // Send the request to our RPC bridge instead.
43 + $args['body']['imagify_rpc_action'] = $args['body']['action'];
44 + $args['body']['action'] = 'imagify_rpc';
45 + $args['body']['imagify_rpc_id'] = $rpc_id;
46 + $args['body']['imagify_rpc_nonce'] = wp_create_nonce( 'imagify_rpc_' . $rpc_id );
103 47
104 - // Check status code
105 - if( is_wp_error( $response ) ) {
106 - $errors->add( 'api_error', $response->get_error_message() );
107 - return $errors;
48 + // Since we can't send cookies to keep the user logged in, store the user ID in a transient.
49 + set_transient( 'imagify_rpc_' . $rpc_id, get_current_user_id(), 30 );
108 50 }
109 51
110 - // Create a backup file
111 - if ( 'wp' === $args['context'] && $args['backup'] ) {
112 - $backup_path = get_imagify_attachment_backup_path( $file_path );
113 - $backup_path_info = pathinfo( $backup_path );
52 + $url = admin_url( 'admin-ajax.php' );
114 53
115 - wp_mkdir_p( $backup_path_info['dirname'] );
116 -
117 - // TO DO - check and send a error message if the backup can't be created
118 - @copy( $file_path, $backup_path );
119 - imagify_chmod_file( $backup_path );
120 - }
121 -
122 - if ( ! function_exists( 'download_url' ) ) {
123 - require( ABSPATH . 'wp-admin/includes/file.php' );
124 - }
125 -
126 - $temp_file = download_url( $response->image );
127 -
128 - if ( is_wp_error( $temp_file ) ) {
129 - $errors->add( 'temp_file_not_found', $temp_file->get_error_message() );
130 - return $errors;
131 - }
132 -
133 - @rename( $temp_file, $file_path );
134 - imagify_chmod_file( $file_path );
135 -
136 - // If temp file still exists, delete it
137 - if ( file_exists( $temp_file ) ) {
138 - unlink( $temp_file );
139 - }
140 -
141 54 /**
142 - * Fires after to optimize the Image with Imagify.
55 + * Filter the URL to use for async jobs.
143 56 *
144 - * @since 1.0
57 + * @since 1.9.5
58 + * @author Grégory Viguier
145 59 *
146 - * @param string $file_path Absolute path to the image file.
147 - * @param bool $backup Force a backup of the original file.
148 - */
149 - do_action( 'after_do_imagify', $file_path, $args['backup'] );
60 + * @param string $url An URL.
61 + * @param array $args An array of arguments passed to wp_remote_post().
62 + */
63 + $url = apply_filters( 'imagify_async_job_url', $url, $args );
150 64
151 - return $response;
65 + wp_remote_post( $url, $args );
152 66 }
153 -
154 -/**
155 - * Run an async job to optimize images in background
156 - *
157 - * @param $body (array) Contains the usual $_POST
158 - *
159 - * @since 1.4
160 - **/
161 -function imagify_do_async_job( $body ) {
162 - $args = array(
163 - 'timeout' => 0.01,
164 - 'blocking' => false,
165 - 'body' => $body,
166 - 'cookies' => $_COOKIE,
167 - 'sslverify' => apply_filters( 'https_local_ssl_verify', false ),
168 - );
169 -
170 - wp_remote_post( admin_url( 'admin-ajax.php' ), $args );
171 -}