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