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