PluginProbe ʕ •ᴥ•ʔ
Pods – Custom Content Types and Fields / 2.8.23.4
Pods – Custom Content Types and Fields v2.8.23.4
2.8.23.4 2.9.19.4 3.0.10.4 3.1.4.2 3.2.8.3 3.3.9.1 trunk 1.14.8 2.7.31.3 2.8.23.3 2.9.19.3 3.0.10.3 3.1.4.1 3.2.0 3.2.1 3.2.1.1 3.2.2 3.2.4 3.2.5 3.2.6 3.2.7 3.2.7.1 3.2.8 3.2.8.1 3.2.8.2 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 3.3.5 3.3.6 3.3.7 3.3.8 3.3.9
pods / includes / media.php
pods / includes Last commit date
compatibility 2 weeks ago access.php 2 weeks ago classes.php 2 weeks ago data.php 2 weeks ago forms.php 2 weeks ago general.php 2 weeks ago media.php 2 weeks ago
media.php
533 lines
1 <?php
2 /**
3 * @package Pods\Global\Functions\Media
4 */
5
6 /**
7 * Get the Attachment ID for a specific image field.
8 *
9 * @param array|int|string $image The image field array, ID, or guid.
10 *
11 * @return int Attachment ID.
12 *
13 * @since 2.0.5
14 */
15 function pods_image_id_from_field( $image ) {
16 $id = 0;
17
18 if ( ! empty( $image ) ) {
19 if ( is_array( $image ) ) {
20 if ( isset( $image[0] ) ) {
21 $id = pods_image_id_from_field( $image[0] );
22 } elseif ( isset( $image['ID'] ) ) {
23 $id = $image['ID'];
24 } elseif ( isset( $image['guid'] ) ) {
25 $id = pods_image_id_from_field( $image['guid'] );
26 } elseif ( isset( $image['id'] ) ) {
27 $id = $image['id'];
28 } else {
29 $id = pods_image_id_from_field( current( $image ) );
30 }
31 } else {
32 if ( false === strpos( $image, '.' ) && is_numeric( $image ) ) {
33 $id = $image;
34
35 $the_post_type = get_post_type( $id );
36
37 if ( false === $the_post_type ) {
38 $id = 0;
39 } elseif ( 'attachment' !== $the_post_type ) {
40 $id = get_post_thumbnail_id( $id );
41 }
42 } else {
43 $guid = pods_query( "SELECT `ID` FROM @wp_posts WHERE `post_type` = 'attachment' AND `guid` = %s", array( $image ) );
44
45 if ( ! empty( $guid ) ) {
46 $id = $guid[0]->ID;
47 }
48 }
49 }//end if
50 }//end if
51
52 $id = (int) $id;
53
54 return $id;
55 }
56
57 /**
58 * Parse image size parameter to support custom image sizes.
59 *
60 * @param string|int[] $size
61 *
62 * @return string|int[]
63 *
64 * @since 2.7.23
65 */
66 function pods_parse_image_size( $size ) {
67
68 if ( ! is_array( $size ) ) {
69 if ( is_numeric( $size ) && ! has_image_size( $size ) ) {
70 // Square sizes.
71 $size = $size . 'x' . $size;
72 }
73 // Fix HTML entity for custom sizes.
74 $size = str_replace( '&#215;', 'x', $size );
75 }
76
77 return $size;
78 }
79
80 /**
81 * Check if an image size exists or is a valid custom format for a size.
82 *
83 * @param string|int[] $size
84 *
85 * @return bool
86 *
87 * @since 2.7.23
88 */
89 function pods_is_image_size( $size ) {
90
91 $valid = false;
92 $size = pods_parse_image_size( $size );
93
94 if ( is_array( $size ) ) {
95 // Custom array size format.
96 $valid = ( 2 <= count( $size ) && is_numeric( $size[0] ) && is_numeric( $size[1] ) );
97 } elseif ( is_numeric( $size ) ) {
98 // Numeric (square) size format.
99 $valid = true;
100 } elseif ( preg_match( '/[0-9]+x[0-9]+/', $size ) || preg_match( '/[0-9]+x[0-9]+x[0-1]/', $size ) ) {
101 // Custom size format.
102 $valid = true;
103 } else {
104 $sizes = get_intermediate_image_sizes();
105 // Not shown by default.
106 $sizes[] = 'full';
107 $sizes[] = 'original';
108 if ( in_array( $size, $sizes, true ) ) {
109 $valid = true;
110 }
111 }
112
113 return $valid;
114 }
115
116 /**
117 * Get the <img> HTML for a specific image field.
118 *
119 * @param array|int|string $image The image field array, ID, or guid.
120 * @param string|array $size Image size to use.
121 * @param int $default Default image to show if image not found, can be field array, ID, or guid.
122 * Passing `-1` prevents default filter.
123 * @param string|array $attributes <img> Attributes array or string (passed to wp_get_attachment_image).
124 * @param boolean $force Force generation of image (if custom size array provided).
125 *
126 * @return string <img> HTML or empty if image not found.
127 *
128 * @since 2.0.5
129 */
130 function pods_image( $image, $size = 'thumbnail', $default = 0, $attributes = '', $force = false ) {
131 if ( ! $default && -1 !== $default ) {
132 /**
133 * Filter for default value.
134 *
135 * Use to set a fallback image to be used when the image passed to pods_image can not be found. Will only take effect if $default is not set.
136 *
137 * @since 2.3.19
138 *
139 * @param array|int|string $default Default image to show if image not found, can be field array, ID, or guid.
140 */
141 $default = apply_filters( 'pods_image_default', $default );
142 }
143
144 $html = '';
145 $id = pods_image_id_from_field( $image );
146 $default = pods_image_id_from_field( $default );
147 $size = pods_parse_image_size( $size );
148
149 if ( 0 < $id ) {
150 if ( $force ) {
151 pods_maybe_image_resize( $id, $size );
152 }
153
154 $html = wp_get_attachment_image( $id, $size, true, $attributes );
155 }
156
157 if ( empty( $html ) && 0 < $default ) {
158 $html = pods_image( $default, $size, -1, $attributes, $force );
159 }
160
161 return $html;
162 }
163
164 /**
165 * Get the Image URL for a specific image field.
166 *
167 * @param array|int|string $image The image field array, ID, or guid.
168 * @param string|array $size Image size to use.
169 * @param int $default Default image to show if image not found, can be field array, ID, or guid.
170 * Passing `-1` prevents default filter.
171 * @param boolean $force Force generation of image (if custom size array provided).
172 *
173 * @return string Image URL or empty if image not found.
174 *
175 * @since 2.0.5
176 */
177 function pods_image_url( $image, $size = 'thumbnail', $default = 0, $force = false ) {
178 if ( ! $default && -1 !== $default ) {
179 /**
180 * Filter for default value.
181 *
182 * Use to set a fallback image to be used when the image passed to pods_image can not be found. Will only take effect if $default is not set.
183 *
184 * @since 2.7.23
185 *
186 * @param array|int|string $default Default image to show if image not found, can be field array, ID, or guid.
187 */
188 $default = apply_filters( 'pods_image_url_default', $default );
189 }
190
191 $url = '';
192 $id = pods_image_id_from_field( $image );
193 $default = pods_image_id_from_field( $default );
194 $size = pods_parse_image_size( $size );
195
196 if ( 0 < $id ) {
197 if ( $force ) {
198 pods_maybe_image_resize( $id, $size );
199 }
200
201 $src = wp_get_attachment_image_src( $id, $size );
202
203 if ( ! empty( $src ) ) {
204 $url = $src[0];
205 } else {
206 // Handle non-images
207 $attachment = get_post( $id );
208
209 if ( ! preg_match( '!^image/!', get_post_mime_type( $attachment ) ) ) {
210 $url = wp_get_attachment_url( $id );
211 }
212 }
213 }//end if
214
215 if ( empty( $url ) && 0 < $default ) {
216 $url = pods_image_url( $default, $size, -1, $force );
217 }//end if
218
219 return $url;
220 }
221
222 /**
223 * Import media from a specific URL, saving as an attachment.
224 *
225 * @param string $url URL to media for import.
226 * @param int $post_parent ID of post parent, default none.
227 * @param boolean $featured Whether to set it as the featured (post thumbnail) of the post parent.
228 * @param boolean $strict Whether to return errors upon failure.
229 *
230 * @return int Attachment ID.
231 *
232 * @since 2.3.0
233 */
234 function pods_attachment_import( $url, $post_parent = null, $featured = false, $strict = false ) {
235 // Only allow http(s).
236 $scheme = wp_parse_url( $url, PHP_URL_SCHEME );
237
238 if ( ! in_array( $scheme, [ 'http', 'https' ], true ) ) {
239 return $strict ? (int) pods_error( __( 'Invalid file URL.', 'pods' ) ) : 0;
240 }
241
242 require_once ABSPATH . 'wp-admin/includes/file.php';
243 require_once ABSPATH . 'wp-admin/includes/media.php';
244 require_once ABSPATH . 'wp-admin/includes/image.php';
245
246 $tmp = download_url( $url );
247
248 if ( is_wp_error( $tmp ) ) {
249 return $strict ? (int) pods_error( $tmp->get_error_message() ) : 0;
250 }
251
252 $file_array = [
253 'name' => basename( wp_parse_url( $url, PHP_URL_PATH ) ),
254 'tmp_name' => $tmp,
255 ];
256
257 $attachment_id = media_handle_sideload( $file_array, (int) $post_parent );
258
259 if ( is_wp_error( $attachment_id ) ) {
260 wp_delete_file( $tmp );
261
262 return $strict ? (int) pods_error( $attachment_id->get_error_message() ) : 0;
263 }
264
265 if ( 0 < $post_parent && $featured ) {
266 update_post_meta( $post_parent, '_thumbnail_id', $attachment_id );
267 }
268
269 return $attachment_id;
270 }
271
272 /**
273 * Resize an image on if it doesn't exist.
274 *
275 * @param int $attachment_id Attachment ID.
276 * @param string|array $size Size to be generated.
277 *
278 * @return boolean Image generation result.
279 *
280 * @since 2.7.23
281 */
282 function pods_maybe_image_resize( $attachment_id, $size ) {
283 if ( 'full' !== $size ) {
284 $full = wp_get_attachment_image_src( $attachment_id, 'full' );
285
286 if ( ! empty( $full[0] ) ) {
287 $size = pods_parse_image_size( $size );
288
289 $src = wp_get_attachment_image_src( $attachment_id, $size );
290
291 if ( empty( $src[0] ) || $full[0] == $src[0] ) {
292 return pods_image_resize( $attachment_id, $size );
293 }
294 }
295 }
296
297 // No resize needed.
298 return true;
299 }
300
301 /**
302 * Resize an image on demand.
303 *
304 * @param int $attachment_id Attachment ID.
305 * @param string|array $size Size to be generated.
306 *
307 * @return boolean Image generation result.
308 *
309 * @since 2.3.0
310 */
311 function pods_image_resize( $attachment_id, $size ) {
312 $size_data = array();
313
314 if ( ! is_array( $size ) ) {
315 // Basic image size string
316 global $wp_image_sizes;
317
318 if ( isset( $wp_image_sizes[ $size ] ) && ! empty( $wp_image_sizes[ $size ] ) ) {
319 // Registered image size
320 $size_data = $wp_image_sizes[ $size ];
321 } elseif ( preg_match( '/[0-9]+x[0-9]+/', $size ) || preg_match( '/[0-9]+x[0-9]+x[0-1]/', $size ) ) {
322 // Custom on-the-fly image size
323 $size = explode( 'x', $size );
324
325 $size_data = array(
326 'width' => (int) $size[0],
327 'height' => (int) $size[1],
328 'crop' => (int) ( isset( $size[2] ) ? $size[2] : 1 ),
329 );
330
331 $size = $size_data['width'] . 'x' . $size_data['height'];
332 }
333 } elseif ( 2 <= count( $size ) ) {
334 // Image size array
335 if ( isset( $size['width'] ) ) {
336 $size_data = $size;
337 } else {
338 $size_data = array(
339 'width' => (int) $size[0],
340 'height' => (int) $size[1],
341 'crop' => (int) ( isset( $size[2] ) ? $size[2] : 1 ),
342 );
343 }
344
345 $size = $size_data['width'] . 'x' . $size_data['height'];
346 }//end if
347
348 if ( empty( $size_data ) ) {
349 return false;
350 }
351
352 require_once ABSPATH . 'wp-admin/includes/image.php';
353
354 $attachment = get_post( $attachment_id );
355 $file = get_attached_file( $attachment_id );
356
357 if ( $file && file_exists( $file ) ) {
358 $metadata = wp_get_attachment_metadata( $attachment_id );
359
360 if ( ! empty( $metadata ) && preg_match( '!^image/!', get_post_mime_type( $attachment ) ) && file_is_displayable_image( $file ) ) {
361 $editor = wp_get_image_editor( $file );
362
363 if ( ! is_wp_error( $editor ) ) {
364 $metadata['sizes'] = array_merge( $metadata['sizes'], $editor->multi_resize( array( $size => $size_data ) ) );
365
366 wp_update_attachment_metadata( $attachment_id, $metadata );
367
368 return true;
369 }
370 }
371 }
372
373 return false;
374 }
375
376 /**
377 * Output an audio field as a video player.
378 *
379 * @uses wp_audio_shortcode()
380 *
381 * @since 2.5.0
382 *
383 * @param string|array|int $url The URL string, an array of post information, or an attachment ID.
384 * @param bool|array $args Optional. Additional arguments to pass to wp_audio_shortcode().
385 *
386 * @return string
387 */
388 function pods_audio( $url, $args = false ) {
389 // Support arrays.
390 if ( is_array( $url ) ) {
391 $url = pods_v( 'ID', $url );
392 }
393
394 // Support IDs.
395 if ( is_numeric( $url ) ) {
396 $url = wp_get_attachment_url( (int) $url );
397 }
398
399 if ( empty( $url ) || ! is_string( $url ) ) {
400 return '';
401 }
402
403 $audio_args = array(
404 'src' => $url,
405 );
406
407 if ( is_array( $args ) ) {
408 $audio_args = array_merge( $audio_args, $args );
409 }
410
411 return wp_audio_shortcode( $audio_args );
412 }
413
414 /**
415 * Output a video field as a video player.
416 *
417 * @uses wp_video_shortcode()
418 *
419 * @since 2.5.0
420 *
421 * @param string|array|int $url The URL string, an array of post information, or an attachment ID.
422 * @param bool|array $args Optional. Additional arguments to pass to wp_video_shortcode().
423 *
424 * @return string
425 */
426 function pods_video( $url, $args = false ) {
427 // Support arrays.
428 if ( is_array( $url ) ) {
429 $url = pods_v( 'ID', $url );
430 }
431
432 // Support IDs.
433 if ( is_numeric( $url ) ) {
434 $url = wp_get_attachment_url( (int) $url );
435 }
436
437 if ( empty( $url ) || ! is_string( $url ) ) {
438 return '';
439 }
440
441 $video_args = array(
442 'src' => $url,
443 );
444
445 if ( is_array( $args ) ) {
446 $video_args = array_merge( $video_args, $args );
447 }
448
449 return wp_video_shortcode( $video_args );
450 }
451
452 /**
453 * Get the image URL for a post for a specific pod field.
454 *
455 * @since 2.7.28
456 *
457 * @param string $field_name The field name.
458 * @param string $size The image size to use.
459 * @param int $default The default image ID to use if not found.
460 *
461 * @return string The image URL for a post for a specific pod field.
462 */
463 function pods_image_url_for_post( $field_name, $size = 'full', $default = 0 ) {
464 // pods_field() will auto-detect the post type / post ID.
465 $value = pods_field( null, null, $field_name, true );
466
467 // No value found.
468 if ( empty( $value ) ) {
469 if ( $default ) {
470 // Maybe return default if it's set.
471 return pods_image_url( $default, $size );
472 } else {
473 // No value, no default to show.
474 return '';
475 }
476 }
477
478 if ( is_numeric( $value ) ) {
479 $attachment_id = $value;
480 } elseif ( is_array( $value ) && isset( $value['ID'] ) ) {
481 $attachment_id = $value['ID'];
482 } elseif ( $default ) {
483 // Maybe return default if it's set.
484 return pods_image_url( $default, $size );
485 } else {
486 // Unexpected value, no default to show.
487 return '';
488 }
489
490 return pods_image_url( $attachment_id, $size, $default );
491 }
492
493 /**
494 * Get the image HTML for a post for a specific pod field.
495 *
496 * @since 2.7.28
497 *
498 * @param string $field_name The field name.
499 * @param string $size The image size to use.
500 * @param int $default The default image ID to use if not found.
501 *
502 * @return string The image HTML for a post for a specific pod field.
503 */
504 function pods_image_for_post( $field_name, $size = 'full', $default = 0 ) {
505 // pods_field() will auto-detect the post type / post ID.
506 $value = pods_field( null, null, $field_name, true );
507
508 // No value found.
509 if ( empty( $value ) ) {
510 if ( $default ) {
511 // Maybe return default if it's set.
512 return pods_image( $default, $size );
513 } else {
514 // No value, no default to show.
515 return '';
516 }
517 }
518
519 if ( is_numeric( $value ) ) {
520 $attachment_id = $value;
521 } elseif ( is_array( $value ) && isset( $value['ID'] ) ) {
522 $attachment_id = $value['ID'];
523 } elseif ( $default ) {
524 // Maybe return default if it's set.
525 return pods_image( $default, $size );
526 } else {
527 // Unexpected value, no default to show.
528 return '';
529 }
530
531 return pods_image( $attachment_id, $size, $default );
532 }
533