• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Yoast / wordpress-seo / 6987097851

25 Nov 2023 04:49AM UTC coverage: 49.206% (-0.1%) from 49.302%
6987097851

push

github

web-flow
Merge pull request #20878 from Yoast/JRF/ghactions-minor-tweak

GH Actions: update a few links in inline comments

15305 of 31104 relevant lines covered (49.21%)

4.03 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

83.87
/src/values/images.php
1
<?php
2

3
namespace Yoast\WP\SEO\Values;
4

5
use Yoast\WP\SEO\Helpers\Image_Helper;
6
use Yoast\WP\SEO\Helpers\Url_Helper;
7

8
/**
9
 * Class Images
10
 *
11
 * Value object for the Images.
12
 */
13
class Images {
14

15
        /**
16
         * The image size.
17
         *
18
         * @var string
19
         */
20
        public $image_size = 'full';
21

22
        /**
23
         * Holds the images that have been put out as image.
24
         *
25
         * @var array
26
         */
27
        protected $images = [];
28

29
        /**
30
         * The image helper.
31
         *
32
         * @var Image_Helper
33
         */
34
        protected $image;
35

36
        /**
37
         * The URL helper.
38
         *
39
         * @var Url_Helper
40
         */
41
        protected $url;
42

43
        /**
44
         * Images constructor.
45
         *
46
         * @codeCoverageIgnore
47
         *
48
         * @param Image_Helper $image The image helper.
49
         * @param Url_Helper   $url   The url helper.
50
         */
51
        public function __construct( Image_Helper $image, Url_Helper $url ) {
52
                $this->image = $image;
53
                $this->url   = $url;
54
        }
55

56
        /**
57
         * Adds an image to the list by image ID.
58
         *
59
         * @param int $image_id The image ID to add.
60
         *
61
         * @return void
62
         */
63
        public function add_image_by_id( $image_id ) {
4✔
64
                $image = $this->image->get_attachment_image_source( $image_id, $this->image_size );
4✔
65
                if ( $image ) {
4✔
66
                        $this->add_image( $image );
2✔
67
                }
68
        }
2✔
69

70
        /**
71
         * Adds an image to the list by image ID.
72
         *
73
         * @param string $image_meta JSON encoded image meta.
74
         *
75
         * @return void
76
         */
77
        public function add_image_by_meta( $image_meta ) {
×
78
                $this->add_image( (array) \json_decode( $image_meta ) );
×
79
        }
80

81
        /**
82
         * Return the images array.
83
         *
84
         * @return array The images.
85
         */
86
        public function get_images() {
4✔
87
                return $this->images;
4✔
88
        }
89

90
        /**
91
         * Check whether we have images or not.
92
         *
93
         * @return bool True if we have images, false if we don't.
94
         */
95
        public function has_images() {
4✔
96
                return ! empty( $this->images );
4✔
97
        }
98

99
        /**
100
         * Adds an image based on a given URL.
101
         *
102
         * @param string $url The given URL.
103
         *
104
         * @return number|null Returns the found image ID if it exists. Otherwise -1.
105
         *                     If the URL is empty we return null.
106
         */
107
        public function add_image_by_url( $url ) {
6✔
108
                if ( empty( $url ) ) {
6✔
109
                        return null;
4✔
110
                }
111

112
                $image_id = $this->image->get_attachment_by_url( $url );
2✔
113

114
                if ( $image_id ) {
2✔
115
                        $this->add_image_by_id( $image_id );
2✔
116

117
                        return $image_id;
2✔
118
                }
119

120
                $this->add_image( $url );
×
121

122
                return -1;
×
123
        }
124

125
        /**
126
         * Adds an image to the list of images.
127
         *
128
         * @param string|array $image Image array.
129
         *
130
         * @return void
131
         */
132
        public function add_image( $image ) {
10✔
133
                if ( \is_string( $image ) ) {
10✔
134
                        $image = [ 'url' => $image ];
2✔
135
                }
136

137
                if ( ! \is_array( $image ) || empty( $image['url'] ) || ! \is_string( $image['url'] ) ) {
10✔
138
                        return;
2✔
139
                }
140

141
                if ( $this->url->is_relative( $image['url'] ) && $image['url'][0] === '/' ) {
8✔
142
                        $image['url'] = $this->url->build_absolute_url( $image['url'] );
×
143
                }
144

145
                if ( \array_key_exists( $image['url'], $this->images ) ) {
8✔
146
                        return;
2✔
147
                }
148

149
                $this->images[ $image['url'] ] = $image;
8✔
150
        }
4✔
151
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc