pryley /
castor-framework
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace GeminiLabs\Castor; |
||||
| 4 | |||||
| 5 | use GeminiLabs\Castor\Helpers\PostMeta; |
||||
| 6 | use GeminiLabs\Castor\Helpers\Utility; |
||||
| 7 | |||||
| 8 | class Image |
||||
| 9 | { |
||||
| 10 | public $image; |
||||
| 11 | |||||
| 12 | protected $postmeta; |
||||
| 13 | protected $utility; |
||||
| 14 | |||||
| 15 | public function __construct(PostMeta $postmeta, Utility $utility) |
||||
| 16 | { |
||||
| 17 | $this->postmeta = $postmeta; |
||||
| 18 | $this->utility = $utility; |
||||
| 19 | } |
||||
| 20 | |||||
| 21 | /** |
||||
| 22 | * @param int|string $attachment |
||||
| 23 | * |
||||
| 24 | * @return self |
||||
| 25 | */ |
||||
| 26 | public function get($attachment) |
||||
| 27 | { |
||||
| 28 | $attachment = $this->normalize($attachment); |
||||
| 29 | if ($attachment && $thumbnail = wp_get_attachment_image_src($attachment, 'thumbnail')) { |
||||
|
0 ignored issues
–
show
|
|||||
| 30 | $medium = $this->normalizeSrc(wp_get_attachment_image_src($attachment, 'medium'), $thumbnail); |
||||
|
0 ignored issues
–
show
It seems like
wp_get_attachment_image_...($attachment, 'medium') can also be of type false; however, parameter $image of GeminiLabs\Castor\Image::normalizeSrc() does only seem to accept array, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 31 | $large = $this->normalizeSrc(wp_get_attachment_image_src($attachment, 'large'), $medium); |
||||
| 32 | |||||
| 33 | $this->image = (object) [ |
||||
| 34 | 'alt' => wp_strip_all_tags(get_post_meta($attachment, '_wp_attachment_image_alt', true), true), |
||||
|
0 ignored issues
–
show
It seems like
get_post_meta($attachmen...hment_image_alt', true) can also be of type false; however, parameter $string of wp_strip_all_tags() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 35 | 'caption' => wp_get_attachment_caption($attachment), |
||||
| 36 | 'copyright' => wp_strip_all_tags(get_post_meta($attachment, '_copyright', true), true), |
||||
| 37 | 'ID' => $attachment, |
||||
| 38 | 'large' => $large, |
||||
| 39 | 'medium' => $medium, |
||||
| 40 | 'permalink' => get_attachment_link($attachment), |
||||
| 41 | 'thumbnail' => $this->normalizeSrc($thumbnail), |
||||
| 42 | ]; |
||||
| 43 | } |
||||
| 44 | return $this; |
||||
| 45 | } |
||||
| 46 | |||||
| 47 | public function render($size = 'large') |
||||
| 48 | { |
||||
| 49 | if ($this->image) { |
||||
| 50 | return wp_get_attachment_image($this->image->ID, $size); |
||||
| 51 | } |
||||
| 52 | } |
||||
| 53 | |||||
| 54 | protected function normalize($attachmentId) |
||||
| 55 | { |
||||
| 56 | if (!filter_var($attachmentId, FILTER_VALIDATE_INT)) { |
||||
| 57 | $attachmentId = $this->postmeta->get($attachmentId); |
||||
| 58 | } |
||||
| 59 | |||||
| 60 | $attachment = get_post($attachmentId); |
||||
| 61 | |||||
| 62 | if (is_null($attachment) || 'attachment' != $attachment->post_type) { |
||||
| 63 | return; |
||||
| 64 | } |
||||
| 65 | |||||
| 66 | return $attachment->ID; |
||||
| 67 | } |
||||
| 68 | |||||
| 69 | /** |
||||
| 70 | * @param mixed $fallback |
||||
| 71 | * |
||||
| 72 | * @return array |
||||
| 73 | */ |
||||
| 74 | protected function normalizeSrc(array $image, $fallback = false) |
||||
| 75 | { |
||||
| 76 | if (is_array($fallback) && count(array_diff($image, $fallback)) < 2) { |
||||
| 77 | $image = $fallback; |
||||
| 78 | } |
||||
| 79 | $image = array_pad($image, 3, ''); |
||||
| 80 | return [ |
||||
| 81 | 'url' => array_shift($image), |
||||
| 82 | 'width' => array_shift($image), |
||||
| 83 | 'height' => array_shift($image), |
||||
| 84 | ]; |
||||
| 85 | } |
||||
| 86 | } |
||||
| 87 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: