PluginProbe ʕ •ᴥ•ʔ
Pods – Custom Content Types and Fields / 3.3.5
Pods – Custom Content Types and Fields v3.3.5
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 4 months ago access.php 4 months ago classes.php 4 months ago compatibility.php 4 months ago data.php 4 months ago forms.php 4 months ago general.php 4 months ago media.php 4 months ago
media.php
583 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 } else {
37 if ( false === strpos( $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 ) {
240 $filename = explode( '?', $url );
241 $filename = $filename[0];
242
243 $filename = explode( '#', $filename );
244 $filename = $filename[0];
245
246 $filename = substr( $filename, ( strrpos( $filename, '/' ) ) + 1 );
247
248 $title = substr( $filename, 0, ( strrpos( $filename, '.' ) ) );
249
250 $uploads = wp_upload_dir( current_time( 'mysql' ) );
251
252 if ( ! ( $uploads && false === $uploads['error'] ) ) {
253 if ( $strict ) {
254 throw new Exception( esc_html( sprintf( 'Attachment import failed, uploads directory has a problem: %s', wp_json_encode( $uploads, JSON_PRETTY_PRINT ) ) ) );
255 }
256
257 return 0;
258 }
259
260 $filename = wp_unique_filename( $uploads['path'], $filename );
261 $new_file = $uploads['path'] . '/' . $filename;
262
263 if ( ! copy( $url, $new_file ) ) {
264 if ( $strict ) {
265 throw new Exception( esc_html( sprintf( 'Attachment import failed, could not copy file from %s to %s', $url, $new_file ) ) );
266 }
267
268 return 0;
269 }
270
271 $stat = stat( dirname( $new_file ) );
272 $perms = $stat['mode'] & 0000666;
273
274 WP_Filesystem();
275
276 /** @var WP_Filesystem_Base $wp_filesystem */
277 global $wp_filesystem;
278
279 $wp_filesystem->chmod( $new_file, $perms );
280
281 $wp_filetype = wp_check_filetype( $filename );
282
283 if ( ! $wp_filetype['type'] || ! $wp_filetype['ext'] ) {
284 if ( $strict ) {
285 throw new Exception( esc_html( sprintf( 'Attachment import failed, filetype check failed: %s', wp_json_encode( $wp_filetype, JSON_PRETTY_PRINT ) ) ) );
286 }
287
288 return 0;
289 }
290
291 $attachment = array(
292 'post_mime_type' => $wp_filetype['type'],
293 'guid' => $uploads['url'] . '/' . $filename,
294 'post_parent' => null,
295 'post_title' => $title,
296 'post_content' => '',
297 );
298
299 /** @var int|WP_Error $attachment_id */
300 $attachment_id = wp_insert_attachment( $attachment, $new_file, $post_parent );
301
302 if ( is_wp_error( $attachment_id ) ) {
303 if ( $strict ) {
304 throw new Exception( esc_html( sprintf( 'Attachment import failed, wp_insert_attachment failed: %s', wp_json_encode( $attachment_id, JSON_PRETTY_PRINT ) ) ) );
305 }
306
307 return 0;
308 }
309
310 require_once ABSPATH . 'wp-admin/includes/media.php';
311 require_once ABSPATH . 'wp-admin/includes/image.php';
312
313 wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $new_file ) );
314
315 if ( 0 < $post_parent && $featured ) {
316 update_post_meta( $post_parent, '_thumbnail_id', $attachment_id );
317 }
318
319 return $attachment_id;
320 }
321
322 /**
323 * Resize an image on if it doesn't exist.
324 *
325 * @param int $attachment_id Attachment ID.
326 * @param string|array $size Size to be generated.
327 *
328 * @return boolean Image generation result.
329 *
330 * @since 2.7.23
331 */
332 function pods_maybe_image_resize( $attachment_id, $size ) {
333 if ( 'full' !== $size ) {
334 $full = wp_get_attachment_image_src( $attachment_id, 'full' );
335
336 if ( ! empty( $full[0] ) ) {
337 $size = pods_parse_image_size( $size );
338
339 $src = wp_get_attachment_image_src( $attachment_id, $size );
340
341 if ( empty( $src[0] ) || $full[0] == $src[0] ) {
342 return pods_image_resize( $attachment_id, $size );
343 }
344 }
345 }
346
347 // No resize needed.
348 return true;
349 }
350
351 /**
352 * Resize an image on demand.
353 *
354 * @param int $attachment_id Attachment ID.
355 * @param string|array $size Size to be generated.
356 *
357 * @return boolean Image generation result.
358 *
359 * @since 2.3.0
360 */
361 function pods_image_resize( $attachment_id, $size ) {
362 $size_data = array();
363
364 if ( ! is_array( $size ) ) {
365 // Basic image size string
366 global $wp_image_sizes;
367
368 if ( isset( $wp_image_sizes[ $size ] ) && ! empty( $wp_image_sizes[ $size ] ) ) {
369 // Registered image size
370 $size_data = $wp_image_sizes[ $size ];
371 } elseif ( preg_match( '/[0-9]+x[0-9]+/', $size ) || preg_match( '/[0-9]+x[0-9]+x[0-1]/', $size ) ) {
372 // Custom on-the-fly image size
373 $size = explode( 'x', $size );
374
375 $size_data = array(
376 'width' => (int) $size[0],
377 'height' => (int) $size[1],
378 'crop' => (int) ( isset( $size[2] ) ? $size[2] : 1 ),
379 );
380
381 $size = $size_data['width'] . 'x' . $size_data['height'];
382 }
383 } elseif ( 2 <= count( $size ) ) {
384 // Image size array
385 if ( isset( $size['width'] ) ) {
386 $size_data = $size;
387 } else {
388 $size_data = array(
389 'width' => (int) $size[0],
390 'height' => (int) $size[1],
391 'crop' => (int) ( isset( $size[2] ) ? $size[2] : 1 ),
392 );
393 }
394
395 $size = $size_data['width'] . 'x' . $size_data['height'];
396 }//end if
397
398 if ( empty( $size_data ) ) {
399 return false;
400 }
401
402 require_once ABSPATH . 'wp-admin/includes/image.php';
403
404 $attachment = get_post( $attachment_id );
405 $file = get_attached_file( $attachment_id );
406
407 if ( $file && file_exists( $file ) ) {
408 $metadata = wp_get_attachment_metadata( $attachment_id );
409
410 if ( ! empty( $metadata ) && preg_match( '!^image/!', get_post_mime_type( $attachment ) ) && file_is_displayable_image( $file ) ) {
411 $editor = wp_get_image_editor( $file );
412
413 if ( ! is_wp_error( $editor ) ) {
414 $metadata['sizes'] = array_merge( $metadata['sizes'], $editor->multi_resize( array( $size => $size_data ) ) );
415
416 wp_update_attachment_metadata( $attachment_id, $metadata );
417
418 return true;
419 }
420 }
421 }
422
423 return false;
424 }
425
426 /**
427 * Output an audio field as a video player.
428 *
429 * @uses wp_audio_shortcode()
430 *
431 * @since 2.5.0
432 *
433 * @param string|array|int $url The URL string, an array of post information, or an attachment ID.
434 * @param bool|array $args Optional. Additional arguments to pass to wp_audio_shortcode().
435 *
436 * @return string
437 */
438 function pods_audio( $url, $args = false ) {
439 // Support arrays.
440 if ( is_array( $url ) ) {
441 $url = pods_v( 'ID', $url );
442 }
443
444 // Support IDs.
445 if ( is_numeric( $url ) ) {
446 $url = wp_get_attachment_url( (int) $url );
447 }
448
449 if ( empty( $url ) || ! is_string( $url ) ) {
450 return '';
451 }
452
453 $audio_args = array(
454 'src' => $url,
455 );
456
457 if ( is_array( $args ) ) {
458 $audio_args = array_merge( $audio_args, $args );
459 }
460
461 return wp_audio_shortcode( $audio_args );
462 }
463
464 /**
465 * Output a video field as a video player.
466 *
467 * @uses wp_video_shortcode()
468 *
469 * @since 2.5.0
470 *
471 * @param string|array|int $url The URL string, an array of post information, or an attachment ID.
472 * @param bool|array $args Optional. Additional arguments to pass to wp_video_shortcode().
473 *
474 * @return string
475 */
476 function pods_video( $url, $args = false ) {
477 // Support arrays.
478 if ( is_array( $url ) ) {
479 $url = pods_v( 'ID', $url );
480 }
481
482 // Support IDs.
483 if ( is_numeric( $url ) ) {
484 $url = wp_get_attachment_url( (int) $url );
485 }
486
487 if ( empty( $url ) || ! is_string( $url ) ) {
488 return '';
489 }
490
491 $video_args = array(
492 'src' => $url,
493 );
494
495 if ( is_array( $args ) ) {
496 $video_args = array_merge( $video_args, $args );
497 }
498
499 return wp_video_shortcode( $video_args );
500 }
501
502 /**
503 * Get the image URL for a post for a specific pod field.
504 *
505 * @since 2.7.28
506 *
507 * @param string $field_name The field name.
508 * @param string $size The image size to use.
509 * @param int $default The default image ID to use if not found.
510 *
511 * @return string The image URL for a post for a specific pod field.
512 */
513 function pods_image_url_for_post( $field_name, $size = 'full', $default = 0 ) {
514 // pods_field() will auto-detect the post type / post ID.
515 $value = pods_field( null, null, $field_name, true );
516
517 // No value found.
518 if ( empty( $value ) ) {
519 if ( $default ) {
520 // Maybe return default if it's set.
521 return pods_image_url( $default, $size );
522 } else {
523 // No value, no default to show.
524 return '';
525 }
526 }
527
528 if ( is_numeric( $value ) ) {
529 $attachment_id = $value;
530 } elseif ( is_array( $value ) && isset( $value['ID'] ) ) {
531 $attachment_id = $value['ID'];
532 } elseif ( $default ) {
533 // Maybe return default if it's set.
534 return pods_image_url( $default, $size );
535 } else {
536 // Unexpected value, no default to show.
537 return '';
538 }
539
540 return pods_image_url( $attachment_id, $size, $default );
541 }
542
543 /**
544 * Get the image HTML for a post for a specific pod field.
545 *
546 * @since 2.7.28
547 *
548 * @param string $field_name The field name.
549 * @param string $size The image size to use.
550 * @param int $default The default image ID to use if not found.
551 *
552 * @return string The image HTML for a post for a specific pod field.
553 */
554 function pods_image_for_post( $field_name, $size = 'full', $default = 0 ) {
555 // pods_field() will auto-detect the post type / post ID.
556 $value = pods_field( null, null, $field_name, true );
557
558 // No value found.
559 if ( empty( $value ) ) {
560 if ( $default ) {
561 // Maybe return default if it's set.
562 return pods_image( $default, $size );
563 } else {
564 // No value, no default to show.
565 return '';
566 }
567 }
568
569 if ( is_numeric( $value ) ) {
570 $attachment_id = $value;
571 } elseif ( is_array( $value ) && isset( $value['ID'] ) ) {
572 $attachment_id = $value['ID'];
573 } elseif ( $default ) {
574 // Maybe return default if it's set.
575 return pods_image( $default, $size );
576 } else {
577 // Unexpected value, no default to show.
578 return '';
579 }
580
581 return pods_image( $attachment_id, $size, $default );
582 }
583