PluginProbe ʕ •ᴥ•ʔ
WP Popular Posts / 5.0.2
WP Popular Posts v5.0.2
4.0.8 4.0.9 4.1.0 4.1.1 4.1.2 4.2.0 4.2.1 4.2.2 5.0.0 5.0.1 5.0.2 5.1.0 5.2.0 5.2.1 5.2.2 5.2.3 5.2.4 5.3.0 5.3.1 5.3.2 5.3.3 5.3.4 5.3.5 5.3.6 5.4.0 5.4.1 5.4.2 5.5.0 5.5.1 6.0.0 6.0.1 6.0.2 6.0.3 6.0.4 6.0.5 6.1.0 6.1.1 6.1.2 6.1.3 6.1.4 6.2.0 6.2.1 6.3.0 6.3.1 6.3.2 6.3.3 6.3.4 6.4.0 6.4.1 6.4.2 7.0.0 7.0.1 7.1.0 7.2.0 7.3.0 7.3.1 7.3.2 7.3.3 7.3.4 7.3.5 7.3.6 7.3.7 7.3.8 7.4.0 trunk 2.3.7 3.0.0 3.0.1 3.0.2 3.0.3 3.1.0 3.1.1 3.2.0 3.2.1 3.2.2 3.2.3 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 4.0.0 4.0.1 4.0.10 4.0.11 4.0.12 4.0.13 4.0.2 4.0.3 4.0.5 4.0.6
wordpress-popular-posts / src / Image.php
wordpress-popular-posts / src Last commit date
Activation 6 years ago Admin 6 years ago Container 6 years ago Front 6 years ago Moment 6 years ago Rest 6 years ago Widget 6 years ago Bootstrap.php 6 years ago Cache.php 6 years ago Helper.php 6 years ago I18N.php 6 years ago Image.php 6 years ago Output.php 6 years ago Query.php 6 years ago Settings.php 6 years ago Themer.php 6 years ago Translate.php 6 years ago WordPressPopularPosts.php 6 years ago deprecated.php 6 years ago template-tags.php 6 years ago
Image.php
796 lines
1 <?php
2 /**
3 * This class builds/retrieves the thumbnail image of each popular posts.
4 *
5 *
6 * @package WordPressPopularPosts
7 * @author Hector Cabrera <me@cabrerahector.com>
8 */
9
10 namespace WordPressPopularPosts;
11
12 class Image {
13
14 /**
15 * Default thumbnail.
16 *
17 * @since 2.2.0
18 * @var string
19 */
20 private $default_thumbnail = '';
21
22 /**
23 * Plugin uploads directory.
24 *
25 * @since 3.0.4
26 * @var array
27 */
28 private $uploads_dir = [];
29
30 /**
31 * Admin settings.
32 *
33 * @since 5.0.0
34 * @var array
35 */
36 private $admin_options = [];
37
38 /**
39 * Available image sizes.
40 *
41 * @since 5.0.0
42 * @var array
43 */
44 private $available_sizes = [];
45
46 /**
47 * Construct.
48 *
49 * @since 4.0.0
50 * @param array $admin_options
51 */
52 public function __construct(array $admin_options)
53 {
54 $this->admin_options = $admin_options;
55
56 // Set default thumbnail
57 $this->default_thumbnail = plugins_url() . "/wordpress-popular-posts/assets/images/no_thumb.jpg";
58
59 if ( $this->is_image_url($this->admin_options['tools']['thumbnail']['default']) )
60 $this->default_thumbnail = $this->admin_options['tools']['thumbnail']['default'];
61
62 // Set uploads folder
63 $wp_upload_dir = wp_get_upload_dir();
64 $this->uploads_dir['basedir'] = $wp_upload_dir['basedir'] . "/" . 'wordpress-popular-posts';
65 $this->uploads_dir['baseurl'] = $wp_upload_dir['baseurl'] . "/" . 'wordpress-popular-posts';
66
67 if ( ! is_dir($this->uploads_dir['basedir']) ) {
68 // Couldn't create the folder, store thumbnails in Uploads
69 if ( ! wp_mkdir_p($this->uploads_dir['basedir']) ) {
70 $this->uploads_dir['basedir'] = $wp_upload_dir['basedir'];
71 $this->uploads_dir['baseurl'] = $wp_upload_dir['baseurl'];
72 }
73 }
74 }
75
76 /**
77 * Get WPP's uploads folder.
78 *
79 * @since 4.0.0
80 * @access public
81 * @return array|bool
82 */
83 public function get_plugin_uploads_dir()
84 {
85 if ( is_array($this->uploads_dir) && ! empty($this->uploads_dir) )
86 return $this->uploads_dir;
87 return false;
88 }
89
90 /**
91 * Returns an image.
92 *
93 * @since 5.0.0
94 * @param \stdClass $post_object Post object
95 * @param array $size Image size (width & height)
96 * @param string $source Image source
97 * @param bool $crop Whether to crop the image or not
98 * @param string $build Whether to build the image or get an existing one
99 * @return string
100 */
101 public function get($post_object, $size, $source, $crop = true, $build = 'manual')
102 {
103 // Bail, $post_object is not an actual object
104 if ( false === $post_object instanceof \stdClass || ! isset($post_object->id) ) {
105 return '';
106 }
107
108 $alt = '';
109 $classes = ['wpp-thumbnail', 'wpp_' . $source];
110 $filename = $post_object->id . '-' . $source . '-' . $size[0] . 'x' . $size[1];
111 $cached = $this->exists($filename);
112
113 if ( $this->admin_options['tools']['thumbnail']['lazyload'] ) {
114 array_push($classes, 'wpp-lazyload');
115 }
116
117 // We have a thumbnail already, return it
118 if ( $cached ) {
119 $classes[] = 'wpp_cached_thumb';
120
121 /**
122 * Filters CSS classes assigned to the thumbnail
123 *
124 * @since 5.0.0
125 * @param array CSS classes
126 * @param int The post ID
127 * @return array The new CSS classes
128 */
129 $classes = apply_filters(
130 'wpp_thumbnail_class_attribute',
131 $classes,
132 $post_object->id
133 );
134
135 /**
136 * Filters CSS classes assigned to the thumbnail
137 *
138 * @since 5.0.0
139 * @param string Original ALT attribute
140 * @param int The post ID
141 * @return string The new ALT attribute
142 */
143 $alt = apply_filters(
144 'wpp_thumbnail_alt_attribute',
145 $this->get_alt_attribute($post_object->id, $source),
146 $post_object->id
147 );
148
149 return $this->render(
150 $cached,
151 $size,
152 is_array($classes) ? implode(' ', $classes) : 'wpp-thumbnail wpp_' . $source,
153 is_string($alt) ? $alt : ''
154 );
155 }
156
157 $thumb_url = null;
158
159 // Return image as-is, no need to create a new thumbnail
160 if (
161 ( 'custom_field' == $source && ! $this->admin_options['tools']['thumbnail']['resize'] )
162 || ( 'featured' == $source && 'predefined' == $build )
163 ){
164 /**
165 * Filters CSS classes assigned to the thumbnail
166 *
167 * @since 5.0.0
168 * @param array CSS classes
169 * @param int The post ID
170 * @return array The new CSS classes
171 */
172 $classes = apply_filters(
173 'wpp_thumbnail_class_attribute',
174 $classes,
175 $post_object->id
176 );
177
178 // Get custom field image URL
179 if ( 'custom_field' == $source && ! $this->admin_options['tools']['thumbnail']['resize'] ) {
180 $thumb_url = get_post_meta(
181 $post_object->id,
182 $this->admin_options['tools']['thumbnail']['field'],
183 true
184 );
185
186 if ( ! $thumb_url || ! $this->is_image_url($thumb_url) ) {
187 $thumb_url = null;
188 } else {
189 /**
190 * Filters CSS classes assigned to the thumbnail
191 *
192 * @since 5.0.0
193 * @param string Original ALT attribute
194 * @param int The post ID
195 * @return string The new ALT attribute
196 */
197 $alt = apply_filters(
198 'wpp_thumbnail_alt_attribute',
199 '',
200 $post_object->id
201 );
202 }
203 }
204 // Get Post Thumbnail
205 else {
206 if (
207 current_theme_supports('post-thumbnails')
208 && has_post_thumbnail($post_object->id)
209 ) {
210 // Find corresponding image size
211 $stock_size = null;
212 $images_sizes = $this->get_sizes();
213
214 foreach ( $images_sizes as $name => $attr ) :
215 if (
216 $attr['width'] == $size[0]
217 && $attr['height'] == $size[1]
218 && $attr['crop'] == $crop
219 ) {
220 $stock_size = $name;
221 break;
222 }
223 endforeach;
224
225 // Couldn't find a matching size so let's go with width/height combo instead
226 // (this should never happen but better safe than sorry!)
227 if ( null == $stock_size ) {
228 $stock_size = $size;
229 }
230
231 $featured_image = get_the_post_thumbnail(
232 $post_object->id,
233 $stock_size
234 );
235
236 if ( strpos($featured_image, 'class="') && is_array($classes) && ! empty($classes) )
237 $featured_image = str_replace('class="', 'class="'. esc_attr(implode(' ', $classes)) . ' ', $featured_image);
238
239 if ( $this->admin_options['tools']['thumbnail']['lazyload'] ) {
240 $featured_image = str_replace('src="', 'data-img-src="', $featured_image);
241 $featured_image = str_replace('srcset="', 'data-img-srcset="', $featured_image);
242 }
243
244 return $featured_image;
245 }
246 }
247 }
248 // Build a new thumbnail and return it
249 else {
250 $file_path = null;
251
252 if ( 'custom_field' == $source && $this->admin_options['tools']['thumbnail']['resize'] ) {
253 $thumb_url = get_post_meta(
254 $post_object->id,
255 $this->admin_options['tools']['thumbnail']['field'],
256 true
257 );
258
259 if ( $thumb_url && $this->is_image_url($thumb_url) ) {
260 $file_path = $this->url_to_path($thumb_url, $post_object->id);
261 }
262 } else {
263 $file_meta = $this->get_file_meta($post_object->id, $source);
264
265 if ( is_array($file_meta) && isset($file_meta['path']) ) {
266 $alt = isset($file_meta['alt']) ? $file_meta['alt'] : '';
267 $file_path = $file_meta['path'];
268 }
269 }
270
271 if ( $file_path ) {
272 $extension = pathinfo($file_path, PATHINFO_EXTENSION);
273 $thumb_url = $this->resize(
274 $file_path,
275 $filename . '.' . $extension,
276 $size,
277 $crop
278 );
279 }
280 }
281
282 if ( ! $thumb_url ) {
283 $classes[] = 'wpp_def_no_src';
284 $thumb_url = $this->get_default_url($post_object->id);
285 }
286
287 return $this->render(
288 $thumb_url,
289 $size,
290 is_array($classes) ? implode(' ', $classes) : 'wpp-thumbnail wpp_' . $source,
291 is_string($alt) ? $alt : ''
292 );
293 }
294
295 /**
296 * Checks whether a given thumbnail exists.
297 *
298 * @since 5.0.0
299 * @access private
300 * @param string $filename
301 * @return string|bool Full URL to image
302 */
303 private function exists($filename)
304 {
305 // Do we have thumbnail already?
306 $file = $this->resolve(trailingslashit($this->get_plugin_uploads_dir()['basedir']) . $filename);
307
308 if ( $file && is_file($file) ) {
309 $extension = pathinfo($file, PATHINFO_EXTENSION);
310 return trailingslashit($this->get_plugin_uploads_dir()['baseurl']) . $filename . '.' . $extension;
311 }
312
313 return false;
314 }
315
316 /**
317 * Resolves filename.
318 *
319 * @since 5.0.0
320 * @access private
321 * @author Ioan Chiriac
322 * @link https://stackoverflow.com/a/29468093/9131961
323 * @param string $name
324 * @return string|bool Resolved path, or false if not found
325 */
326 private function resolve($name)
327 {
328 $info = pathinfo($name);
329
330 // File already contains an extension, return it
331 if ( isset($info['extension']) && ! empty($info['extension']) ) {
332 return $name;
333 }
334
335 $filename = $info['filename'];
336 $len = strlen($filename);
337
338 // open the folder
339 $dh = opendir($info['dirname']);
340
341 if ( ! $dh ) {
342 return false;
343 }
344
345 // scan each file in the folder
346 while ( ($file = readdir($dh)) !== false ) {
347 if ( strncmp($file, $filename, $len) === 0 ) {
348 if ( strlen($name) > $len ) {
349 // if name contains a directory part
350 $name = substr($name, 0, strlen($name) - $len) . $file;
351 } else {
352 // if the name is at the path root
353 $name = $file;
354 }
355
356 closedir($dh);
357 return $name;
358 }
359 }
360
361 // file not found
362 closedir($dh);
363 return false;
364 }
365
366 /**
367 * Retrieves local path to image.
368 *
369 * @since 5.0.0
370 * @access private
371 * @param string $url
372 * @param integer $post_ID
373 * @return string|boolean Path to image, or false if not found
374 */
375 private function url_to_path($url, $post_ID = null)
376 {
377 if ( $this->is_image_url($url) ) {
378 $attachment_id = $this->get_attachment_id($url);
379
380 // Image is hosted locally
381 if ( $attachment_id ) {
382 return get_attached_file($attachment_id);
383 }
384
385 // Image hosted elsewhere?
386 if ( $post_ID && Helper::is_number($post_ID) )
387 return $this->fetch_external_image($post_ID, $url);
388 }
389
390 return false;
391 }
392
393 /**
394 * Gets image meta.
395 *
396 * @since 5.0.0
397 * @access private
398 * @param int $id Post ID
399 * @param string $source Image source
400 * @return array|bool
401 */
402 private function get_file_meta($id, $source)
403 {
404 // get thumbnail path from the Featured Image
405 if ( "featured" == $source ) {
406 if ( $thumbnail_id = get_post_thumbnail_id($id) ) {
407 // image path
408 return [
409 'path' => get_attached_file($thumbnail_id),
410 'alt' => get_post_meta($thumbnail_id, '_wp_attachment_image_alt', true)
411 ];
412 }
413 }
414 // get thumbnail path from first image attachment
415 elseif ( "first_attachment" == $source ) {
416 $args = [
417 'numberposts' => 1,
418 'order' => 'ASC',
419 'post_parent' => $id,
420 'post_type' => 'attachment',
421 'post_mime_type' => 'image'
422 ];
423 $post_attachments = get_children($args);
424
425 if ( ! empty($post_attachments) ) {
426 $first_img = array_shift($post_attachments);
427
428 return [
429 'path' => get_attached_file($first_img->ID),
430 'alt' => get_post_meta($first_img->ID, '_wp_attachment_image_alt', true)
431 ];
432 }
433 }
434 // get thumbnail path from post content
435 elseif ( "first_image" == $source ) {
436 /** @var wpdb $wpdb */
437 global $wpdb;
438
439 if ( $content = $wpdb->get_var("SELECT post_content FROM {$wpdb->posts} WHERE ID = {$id};") ) {
440 // at least one image has been found
441 if ( preg_match('/<img[^>]+>/i', $content, $img) ) {
442 // get img src attribute from the first image found
443 preg_match('/(src)="([^"]*)"/i', $img[0], $src_attr);
444
445 if ( isset($src_attr[2]) && ! empty($src_attr[2]) ) {
446 // get img alt attribute from the first image found
447 $alt = '';
448 preg_match('/(alt)="([^"]*)"/i', $img[0], $alt_attr);
449
450 if ( isset($alt_attr[2]) && !empty($alt_attr[2]) ) {
451 $alt = $alt_attr[2];
452 }
453
454 // image from Media Library
455 if ( $attachment_id = $this->get_attachment_id($src_attr[2]) ) {
456 return [
457 'path' => get_attached_file($attachment_id),
458 'alt' => $alt
459 ];
460 } // external image?
461 else {
462 return [
463 'path' => $this->fetch_external_image($id, $src_attr[2]),
464 'alt' => $alt
465 ];
466 }
467 }
468 }
469 }
470 }
471
472 return false;
473 }
474
475 /**
476 * Gets image ALT attribute.
477 *
478 * @since 5.0.0
479 * @access private
480 * @param int $id Post ID
481 * @param string $source Image source
482 * @return string
483 */
484 private function get_alt_attribute($id, $source)
485 {
486 $alt = '';
487
488 // get thumbnail path from the Featured Image
489 if ( "featured" == $source ) {
490 if ( $thumbnail_id = get_post_thumbnail_id($id) ) {
491 // image path
492 $alt = get_post_meta($thumbnail_id, '_wp_attachment_image_alt', true);
493 }
494 }
495 // get thumbnail path from first image attachment
496 elseif ( "first_attachment" == $source ) {
497 $args = [
498 'numberposts' => 1,
499 'order' => 'ASC',
500 'post_parent' => $id,
501 'post_type' => 'attachment',
502 'post_mime_type' => 'image'
503 ];
504 $post_attachments = get_children($args);
505
506 if ( ! empty($post_attachments) ) {
507 $first_img = array_shift($post_attachments);
508 $alt = get_post_meta($first_img->ID, '_wp_attachment_image_alt', true);
509 }
510 }
511 // get thumbnail path from post content
512 elseif ( "first_image" == $source ) {
513 /** @var wpdb $wpdb */
514 global $wpdb;
515
516 if ( $content = $wpdb->get_var("SELECT post_content FROM {$wpdb->posts} WHERE ID = {$id};") ) {
517 // at least one image has been found
518 if ( preg_match('/<img[^>]+>/i', $content, $img) ) {
519 // get img alt attribute from the first image found
520 preg_match('/(alt)="([^"]*)"/i', $img[0], $alt_attr);
521
522 if ( isset($alt_attr[2]) && !empty($alt_attr[2]) ) {
523 $alt = $alt_attr[2];
524 }
525 }
526 }
527 }
528
529 return $alt;
530 }
531
532 /**
533 * Get the Attachment ID for a given image URL.
534 *
535 * @since 3.0.0
536 * @access private
537 * @author Frankie Jarrett
538 * @link http://frankiejarrett.com/get-an-attachment-id-by-url-in-wordpress/
539 * @param string $url
540 * @return int|null
541 */
542 private function get_attachment_id($url)
543 {
544 $url = Helper::add_scheme(
545 $url,
546 is_ssl() ? 'https://' : 'http://'
547 );
548
549 // Split the $url into two parts with the wp-content directory as the separator.
550 $parse_url = explode(parse_url(WP_CONTENT_URL, PHP_URL_PATH), $url);
551
552 // Get the host of the current site and the host of the $url, ignoring www.
553 $this_host = str_ireplace('www.', '', parse_url(home_url(), PHP_URL_HOST));
554 $file_host = str_ireplace('www.', '', parse_url($url, PHP_URL_HOST));
555
556 // Return nothing if there aren't any $url parts or if the current host and $url host do not match.
557 if (
558 ! isset($parse_url[1])
559 || empty($parse_url[1])
560 || ($this_host != $file_host)
561 ) {
562 return false;
563 }
564
565 // Now we're going to quickly search the DB for any attachment GUID with a partial path match.
566 // Example: /uploads/2013/05/test-image.jpg
567 global $wpdb;
568
569 if ( ! $attachment = $wpdb->get_col($wpdb->prepare("SELECT ID FROM {$wpdb->prefix}posts WHERE guid RLIKE %s;", $parse_url[1])) ) {
570 // Maybe it's a resized image, so try to get the full one
571 $parse_url[1] = preg_replace('/-[0-9]{1,4}x[0-9]{1,4}\.(jpg|jpeg|png|gif|bmp)$/i', '.$1', $parse_url[1]);
572 $attachment = $wpdb->get_col($wpdb->prepare("SELECT ID FROM {$wpdb->prefix}posts WHERE guid RLIKE %s;", $parse_url[1]));
573 }
574
575 // Returns null if no attachment is found.
576 return isset($attachment[0]) ? $attachment[0] : NULL;
577 }
578
579 /**
580 * Fetchs external images.
581 *
582 * @since 2.3.3
583 * @access private
584 * @param int $id Post ID.
585 * @param string $url Image url.
586 * @return string|bool Image path, or false on failure.
587 */
588 private function fetch_external_image($id, $url)
589 {
590 $full_image_path = trailingslashit($this->get_plugin_uploads_dir()['basedir']) . "{$id}_" . sanitize_file_name(rawurldecode(wp_basename($url)));
591
592 // if the file exists already, return URL and path
593 if ( file_exists($full_image_path) )
594 return $full_image_path;
595
596 $url = Helper::add_scheme(
597 $url,
598 is_ssl() ? 'https://' : 'http://'
599 );
600
601 $accepted_status_codes = [200, 301, 302];
602 $response = wp_remote_head($url, ['timeout' => 5, 'sslverify' => false]);
603
604 if (
605 ! is_wp_error($response)
606 && in_array(wp_remote_retrieve_response_code($response), $accepted_status_codes)
607 ) {
608 require_once(ABSPATH . 'wp-admin/includes/file.php');
609
610 $url = str_replace('https://', 'http://', $url);
611 $tmp = download_url($url);
612
613 // File was downloaded successfully
614 if ( ! is_wp_error($tmp) ) {
615 // Determine image type
616 if ( function_exists('exif_imagetype') ) {
617 $image_type = exif_imagetype($tmp);
618 } else {
619 $image_type = getimagesize($tmp);
620 $image_type = ( isset($image_type[2]) ) ? $image_type[2] : NULL;
621 }
622
623 // Valid image, save it
624 if ( in_array($image_type, [IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG]) ) {
625 // move file to Uploads
626 if ( @rename($tmp, $full_image_path) ) {
627 // borrowed from WP - set correct file permissions
628 $stat = stat(dirname($full_image_path));
629 $perms = $stat['mode'] & 0000644;
630 @chmod($full_image_path, $perms);
631
632 return $full_image_path;
633 }
634 }
635
636 // Invalid file, remove it
637 @unlink($tmp);
638 }
639 }
640
641 return false;
642 }
643
644 /**
645 * Resizes image.
646 *
647 * @since 3.0.0
648 * @access private
649 * @param string $path Image path
650 * @param string $filename Image filename
651 * @param array $size Image size
652 * @param bool $crop Whether to crop the image or not
653 * @return string|bool Image URL on success, false on error
654 */
655 private function resize($path, $filename, $size, $crop = true)
656 {
657 $image = wp_get_image_editor($path);
658
659 // valid image, create thumbnail
660 if ( ! is_wp_error($image) ) {
661 /**
662 * Hook to change the image compression quality of WPP's thumbnails.
663 * @since 4.2.1
664 */
665 $quality = apply_filters('wpp_thumbnail_compression_quality', null);
666
667 if ( ! ctype_digit($quality) )
668 $quality = null; // Fallback to core's default
669
670 $image->set_quality($quality);
671
672 $image->resize($size[0], $size[1], $crop);
673 $new_img = $image->save(trailingslashit($this->get_plugin_uploads_dir()['basedir']) . $filename);
674
675 if ( ! is_wp_error($new_img) )
676 return trailingslashit($this->get_plugin_uploads_dir()['baseurl']) . $filename;
677 }
678
679 return false;
680 }
681
682 /**
683 * Render image tag.
684 *
685 * @since 3.0.0
686 * @access public
687 * @param string $src Image URL
688 * @param array $dimension Image's width and height
689 * @param string $class CSS class
690 * @param object $alt Alternative text
691 * @param string $error Error, if the image could not be created
692 * @return string
693 */
694 public function render($src, $size, $class, $alt = '', $error = null)
695 {
696 $img_tag = '';
697
698 if ( $error ) {
699 $img_tag = '<!-- ' . $error . ' --> ';
700 }
701
702 if ( $this->admin_options['tools']['thumbnail']['lazyload'] ) {
703 $src = 'data-img-src="' . esc_url(is_ssl() ? str_ireplace("http://", "https://", $src) : $src) . '"';
704 } else {
705 $src = 'src="' . esc_url(is_ssl() ? str_ireplace("http://", "https://", $src) : $src) . '"';
706 }
707
708 $img_tag .= '<img ' . $src . ' width="' . $size[0] . '" height="' . $size[1] . '" alt="' . esc_attr($alt) . '" class="' . esc_attr($class) . '" />';
709
710 return apply_filters('wpp_render_image', $img_tag);
711 }
712
713 /**
714 * Gets list of available thumbnails sizes
715 *
716 * @since 3.2.0
717 * @link http://codex.wordpress.org/Function_Reference/get_intermediate_image_sizes
718 * @param string $size
719 * @return array|bool
720 */
721 public function get_sizes($size = '')
722 {
723 if ( ! is_array($this->available_sizes) || empty($this->available_sizes) ) {
724 global $_wp_additional_image_sizes;
725
726 $this->available_sizes = [];
727 $get_intermediate_image_sizes = get_intermediate_image_sizes();
728
729 // Create the full array with sizes and crop info
730 foreach( $get_intermediate_image_sizes as $_size ) {
731 if ( in_array($_size, ['thumbnail', 'medium', 'large']) ) {
732 $this->available_sizes[$_size]['width'] = get_option($_size . '_size_w');
733 $this->available_sizes[$_size]['height'] = get_option($_size . '_size_h');
734 $this->available_sizes[$_size]['crop'] = (bool) get_option($_size . '_crop');
735 } elseif ( isset($_wp_additional_image_sizes[$_size]) ) {
736 $this->available_sizes[$_size] = [
737 'width' => $_wp_additional_image_sizes[$_size]['width'],
738 'height' => $_wp_additional_image_sizes[$_size]['height'],
739 'crop' => $_wp_additional_image_sizes[$_size]['crop']
740 ];
741 }
742 }
743 }
744
745 // Get only 1 size if found
746 if ( $size ) {
747 if ( isset($this->available_sizes[$size]) ) {
748 return $this->available_sizes[$size];
749 }
750 return false;
751 }
752
753 return $this->available_sizes;
754 }
755
756 /**
757 * Returns the URL of the default thumbnail image.
758 *
759 * @since 5.0.0
760 * @param int|null
761 * @return string
762 */
763 public function get_default_url($post_ID = null)
764 {
765 if ( has_filter('wpp_default_thumbnail_url') ) {
766 $default_thumbnail_url = apply_filters('wpp_default_thumbnail_url', $this->default_thumbnail, $post_ID);
767
768 if ( $default_thumbnail_url != $this->default_thumbnail && $this->is_image_url($default_thumbnail_url) )
769 return $default_thumbnail_url;
770 }
771
772 return $this->default_thumbnail;
773 }
774
775 /**
776 * Checks whether an URL points to an actual image.
777 *
778 * @since 5.0.0
779 * @access private
780 * @param string
781 * @return array|bool
782 */
783 private function is_image_url($url)
784 {
785 if ( ! filter_var($url, FILTER_VALIDATE_URL) )
786 return false;
787
788 // sanitize URL, just in case
789 $image_url = esc_url($url);
790 // remove querystring
791 preg_match('/[^\?]+\.(jpg|JPG|jpe|JPE|jpeg|JPEG|gif|GIF|png|PNG)/', $image_url, $matches);
792
793 return ( is_array($matches) && ! empty($matches) ) ? $matches : false;
794 }
795 }
796