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