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