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