PluginProbe
Imsanity / 2.5.0
Imsanity v2.5.0
2.9.4 2.9.3 2.9.2 trunk 2.4.3 2.5.0 2.6.0 2.7.0 2.7.2 2.8.0 2.8.1 2.8.2 2.8.3 2.8.4 2.8.5 2.8.6 2.8.7 2.9.0 2.9.1
imsanity / ajax.php

ajax.php in Imsanity 2.5.0, at ajax.php

260 lines 8.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Imsanity AJAX functions.
4 *
5 * @package Imsanity
6 */
7
8 add_action( 'wp_ajax_imsanity_get_images', 'imsanity_get_images' );
9 add_action( 'wp_ajax_imsanity_resize_image', 'imsanity_resize_image' );
10
11 /**
12 * Verifies that the current user has administrator permission and, if not,
13 * renders a json warning and dies
14 */
15 function imsanity_verify_permission() {
16 if ( ! current_user_can( 'activate_plugins' ) ) {
17 die(
18 json_encode(
19 array(
20 'success' => false,
21 'message' => esc_html__( 'Administrator permission is required', 'imsanity' ),
22 )
23 )
24 );
25 }
26 if ( ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'imsanity-bulk' ) ) {
27 die(
28 json_encode(
29 array(
30 'success' => false,
31 'message' => esc_html__( 'Access token has expired, please reload the page.', 'imsanity' ),
32 )
33 )
34 );
35 }
36 }
37
38
39 /**
40 * Searches for up to 250 images that are candidates for resize and renders them
41 * to the browser as a json array, then dies
42 */
43 function imsanity_get_images() {
44 imsanity_verify_permission();
45
46 global $wpdb;
47 $offset = 0;
48 $limit = apply_filters( 'imsanity_attachment_query_limit', 3000 );
49 $results = array();
50 $maxw = imsanity_get_option( 'imsanity_max_width', IMSANITY_DEFAULT_MAX_WIDTH );
51 $maxh = imsanity_get_option( 'imsanity_max_height', IMSANITY_DEFAULT_MAX_HEIGHT );
52 $count = 0;
53
54 $images = $wpdb->get_results( $wpdb->prepare( "SELECT metas.meta_value as file_meta,metas.post_id as ID FROM $wpdb->postmeta metas INNER JOIN $wpdb->posts posts ON posts.ID = metas.post_id WHERE posts.post_type = 'attachment' AND posts.post_mime_type LIKE %s AND posts.post_mime_type != 'image/bmp' AND metas.meta_key = '_wp_attachment_metadata' ORDER BY ID DESC LIMIT %d,%d", '%image%', $offset, $limit ) );
55 while ( $images ) {
56
57 foreach ( $images as $image ) {
58 $imagew = false;
59 $imageh = false;
60
61 $meta = unserialize( $image->file_meta );
62
63 // If "noresize" is included in the filename then we will bypass imsanity scaling.
64 if ( ! empty( $meta['file'] ) && strpos( $meta['file'], 'noresize' ) !== false ) {
65 continue;
66 }
67
68 // Let folks filter the allowed mime-types for resizing.
69 $allowed_types = apply_filters( 'imsanity_allowed_mimes', array( 'image/png', 'image/gif', 'image/jpeg' ), $meta['file'] );
70 if ( is_string( $allowed_types ) ) {
71 $allowed_types = array( $allowed_types );
72 } elseif ( ! is_array( $allowed_types ) ) {
73 $allowed_types = array();
74 }
75 $ftype = imsanity_quick_mimetype( $meta['file'] );
76 if ( ! in_array( $ftype, $allowed_types, true ) ) {
77 continue;
78 }
79
80 if ( imsanity_get_option( 'imsanity_deep_scan', false ) ) {
81 $file_path = imsanity_attachment_path( $meta, $image->ID, '', false );
82 if ( $file_path ) {
83 list( $imagew, $imageh ) = getimagesize( $file_path );
84 }
85 }
86 if ( empty( $imagew ) || empty( $imageh ) ) {
87 $imagew = $meta['width'];
88 $imageh = $meta['height'];
89 }
90
91 if ( $imagew > $maxw || $imageh > $maxh ) {
92 $count++;
93
94 $results[] = array(
95 'id' => $image->ID,
96 'width' => $imagew,
97 'height' => $imageh,
98 'file' => $meta['file'],
99 );
100 }
101
102 // Make sure we only return a limited number of records so we don't overload the ajax features.
103 if ( $count >= IMSANITY_AJAX_MAX_RECORDS ) {
104 break 2;
105 }
106 }
107 $offset += $limit;
108 $images = $wpdb->get_results( $wpdb->prepare( "SELECT metas.meta_value as file_meta,metas.post_id as ID FROM $wpdb->postmeta metas INNER JOIN $wpdb->posts posts ON posts.ID = metas.post_id WHERE posts.post_type = 'attachment' AND posts.post_mime_type LIKE %s AND posts.post_mime_type != 'image/bmp' AND metas.meta_key = '_wp_attachment_metadata' ORDER BY ID DESC LIMIT %d,%d", '%image%', $offset, $limit ) );
109 } // endwhile
110 die( json_encode( $results ) );
111 }
112
113 /**
114 * Resizes the image with the given id according to the configured max width and height settings
115 * renders a json response indicating success/failure and dies
116 */
117 function imsanity_resize_image() {
118 imsanity_verify_permission();
119
120 global $wpdb;
121
122 $id = (int) $_POST['id'];
123
124 if ( ! $id ) {
125 die(
126 json_encode(
127 array(
128 'success' => false,
129 'message' => esc_html__( 'Missing ID Parameter', 'imsanity' ),
130 )
131 )
132 );
133 }
134
135 $meta = wp_get_attachment_metadata( $id );
136
137 if ( $meta && is_array( $meta ) ) {
138 $uploads = wp_upload_dir();
139 $oldpath = imsanity_attachment_path( $meta['file'], $id, '', false );
140 if ( empty( $oldpath ) || ! is_writable( $oldpath ) ) {
141 /* translators: %s: File-name of the image */
142 $msg = sprintf( esc_html__( '%s is not writable', 'imsanity' ), $meta['file'] );
143 die(
144 json_encode(
145 array(
146 'success' => false,
147 'message' => $msg,
148 )
149 )
150 );
151 }
152
153 $maxw = imsanity_get_option( 'imsanity_max_width', IMSANITY_DEFAULT_MAX_WIDTH );
154 $maxh = imsanity_get_option( 'imsanity_max_height', IMSANITY_DEFAULT_MAX_HEIGHT );
155
156 // method one - slow but accurate, get file size from file itself.
157 list( $oldw, $oldh ) = getimagesize( $oldpath );
158 // method two - get file size from meta, fast but resize will fail if meta is out of sync.
159 if ( ! $oldw || ! $oldh ) {
160 $oldw = $meta['width'];
161 $oldh = $meta['height'];
162 }
163
164 if ( ( $oldw > $maxw && $maxw > 0 ) || ( $oldh > $maxh && $maxh > 0 ) ) {
165 $quality = imsanity_get_option( 'imsanity_quality', IMSANITY_DEFAULT_QUALITY );
166
167 if ( $maxw > 0 && $maxh > 0 && $oldw >= $maxw && $oldh >= $maxh && ( $oldh > $maxh || $oldw > $maxw ) && apply_filters( 'imsanity_crop_image', false ) ) {
168 $neww = $maxw;
169 $newh = $maxh;
170 } else {
171 list( $neww, $newh ) = wp_constrain_dimensions( $oldw, $oldh, $maxw, $maxh );
172 }
173
174 $resizeresult = imsanity_image_resize( $oldpath, $neww, $newh, apply_filters( 'imsanity_crop_image', false ), null, null, $quality );
175
176 if ( $resizeresult && ! is_wp_error( $resizeresult ) ) {
177 $newpath = $resizeresult;
178
179 if ( $newpath !== $oldpath && is_file( $newpath ) && filesize( $newpath ) < filesize( $oldpath ) ) {
180 // we saved some file space. remove original and replace with resized image.
181 unlink( $oldpath );
182 rename( $newpath, $oldpath );
183 $meta['width'] = $neww;
184 $meta['height'] = $newh;
185
186 wp_update_attachment_metadata( $id, $meta );
187
188 $results = array(
189 'success' => true,
190 'id' => $id,
191 /* translators: %s: File-name of the image */
192 'message' => sprintf( esc_html__( 'OK: %s', 'imsanity' ), $oldpath ),
193 );
194 } elseif ( $newpath !== $oldpath ) {
195 // the resized image is actually bigger in filesize (most likely due to jpg quality).
196 // keep the old one and just get rid of the resized image.
197 if ( is_file( $newpath ) ) {
198 unlink( $newpath );
199 }
200 $results = array(
201 'success' => false,
202 'id' => $id,
203 /* translators: 1: File-name of the image 2: the error message, translated elsewhere */
204 'message' => sprintf( esc_html__( 'ERROR: %1$s (%2$s)', 'imsanity' ), $oldpath, esc_html__( 'Resized image was larger than the original', 'imsanity' ) ),
205 );
206 } else {
207 $results = array(
208 'success' => false,
209 'id' => $id,
210 /* translators: 1: File-name of the image 2: the error message, translated elsewhere */
211 'message' => sprintf( esc_html__( 'ERROR: %1$s (%2$s)', 'imsanity' ), $oldpath, esc_html__( 'Unknown error, resizing function returned the same filename', 'imsanity' ) ),
212 );
213 }
214 } elseif ( false === $resizeresult ) {
215 $results = array(
216 'success' => false,
217 'id' => $id,
218 /* translators: 1: File-name of the image 2: the error message, translated elsewhere */
219 'message' => sprintf( esc_html__( 'ERROR: %1$s (%2$s)', 'imsanity' ), $oldpath, esc_html__( 'wp_get_image_editor missing', 'imsanity' ) ),
220 );
221 } else {
222 $results = array(
223 'success' => false,
224 'id' => $id,
225 /* translators: 1: File-name of the image 2: the error message, translated elsewhere */
226 'message' => sprintf( esc_html__( 'ERROR: %1$s (%2$s)', 'imsanity' ), $oldpath, htmlentities( $resizeresult->get_error_message() ) ),
227 );
228 }
229 } else {
230 $results = array(
231 'success' => true,
232 'id' => $id,
233 /* translators: %s: File-name of the image */
234 'message' => sprintf( esc_html__( 'SKIPPED: %s (Resize not required)', 'imsanity' ), $oldpath ) . " -- $oldw x $oldh",
235 );
236 if ( empty( $meta['width'] ) || empty( $meta['height'] ) ) {
237 if ( empty( $meta['width'] ) || $meta['width'] > $oldw ) {
238 $meta['width'] = $oldw;
239 }
240 if ( empty( $meta['height'] ) || $meta['height'] > $oldh ) {
241 $meta['height'] = $oldh;
242 }
243 wp_update_attachment_metadata( $id, $meta );
244 }
245 }
246 } else {
247 $results = array(
248 'success' => false,
249 'id' => $id,
250 /* translators: %s: ID number of the image */
251 'message' => sprintf( esc_html__( 'ERROR: Attachment with ID of %d not found', 'imsanity' ), intval( $id ) ),
252 );
253 }
254
255 // If there is a quota we need to reset the directory size cache so it will re-calculate.
256 delete_transient( 'dirsize_cache' );
257
258 die( json_encode( $results ) );
259 }
260