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

Yoast / wordpress-seo / dd6e866a9e6d253114633104d9e3858d807178ba

19 Jun 2024 10:03AM UTC coverage: 48.628% (-4.3%) from 52.936%
dd6e866a9e6d253114633104d9e3858d807178ba

push

github

web-flow
Merge pull request #21431 from Yoast/21429-update-copy-in-the-introduction-and-consent-modals

Updates the copy for the introduction and consent modals

7441 of 13454 branches covered (55.31%)

Branch coverage included in aggregate %.

0 of 3 new or added lines in 2 files covered. (0.0%)

3718 existing lines in 107 files now uncovered.

25100 of 53464 relevant lines covered (46.95%)

62392.47 hits per line

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

98.59
/src/repositories/seo-links-repository.php
1
<?php
2

3
namespace Yoast\WP\SEO\Repositories;
4

5
use Yoast\WP\Lib\Model;
6
use Yoast\WP\Lib\ORM;
7
use Yoast\WP\SEO\Models\SEO_Links;
8

9
/**
10
 * Class SEO_Links_Repository.
11
 */
12
class SEO_Links_Repository {
13

14
        /**
15
         * Starts a query for this repository.
16
         *
17
         * @return ORM
18
         */
19
        public function query() {
2✔
20
                return Model::of_type( 'SEO_Links' );
2✔
21
        }
22

23
        /**
24
         * Finds all SEO Links by post ID.
25
         *
26
         * @param int $post_id The post ID.
27
         *
28
         * @return SEO_Links[] The SEO Links.
29
         */
30
        public function find_all_by_post_id( $post_id ) {
2✔
31
                return $this->query()
2✔
32
                        ->where( 'post_id', $post_id )
2✔
33
                        ->find_many();
2✔
34
        }
35

36
        /**
37
         * Finds all SEO Links by indexable ID.
38
         *
39
         * @param int $indexable_id The indexable ID.
40
         *
41
         * @return SEO_Links[] The SEO Links.
42
         */
43
        public function find_all_by_indexable_id( $indexable_id ) {
2✔
44
                return $this->query()
2✔
45
                        ->where( 'indexable_id', $indexable_id )
2✔
46
                        ->find_many();
2✔
47
        }
48

49
        /**
50
         * Retrieves an SEO Link by url.
51
         *
52
         * @param string $url The SEO Link's url.
53
         *
54
         * @return SEO_Links|false The SEO Link, or false if none found.
55
         */
56
        public function find_one_by_url( $url ) {
2✔
57
                return $this->query()
2✔
58
                        ->select( 'target_post_id' )
2✔
59
                        ->where( 'url', $url )
2✔
60
                        ->find_one();
2✔
61
        }
62

63
        /**
64
         * Retrieves all SEO Links by target post ID.
65
         *
66
         * @param string $target_post_id The SEO Link's target post ID.
67
         *
68
         * @return SEO_Links[] The SEO Links.
69
         */
70
        public function find_all_by_target_post_id( $target_post_id ) {
2✔
71
                return $this->query()
2✔
72
                        ->where( 'target_post_id', $target_post_id )
2✔
73
                        ->find_many();
2✔
74
        }
75

76
        /**
77
         * Updates the ID of the target indexable of a link.
78
         *
79
         * @param int $link_id             The ID of the link to be updated.
80
         * @param int $target_indexable_id The ID of the target indexable.
81
         *
82
         * @return bool Whether or not the update was succeful.
83
         */
84
        public function update_target_indexable_id( $link_id, $target_indexable_id ) {
2✔
85
                return (bool) $this->query()
2✔
86
                        ->set( 'target_indexable_id', $target_indexable_id )
2✔
87
                        ->where( 'id', $link_id )
2✔
88
                        ->update_many();
2✔
89
        }
90

91
        /**
92
         * Clears all SEO Links by post ID.
93
         *
94
         * @param int $post_id The post ID.
95
         *
96
         * @return bool Whether or not the delete was succesfull.
97
         */
98
        public function delete_all_by_post_id( $post_id ) {
2✔
99
                return $this->query()
2✔
100
                        ->where( 'post_id', $post_id )
2✔
101
                        ->delete_many();
2✔
102
        }
103

104
        /**
105
         * Clears all SEO Links by post ID where the indexable id is null.
106
         *
107
         * @param int $post_id The post ID.
108
         *
109
         * @return bool Whether or not the delete was succesfull.
110
         */
111
        public function delete_all_by_post_id_where_indexable_id_null( $post_id ) {
2✔
112
                return $this->query()
2✔
113
                        ->where( 'post_id', $post_id )
2✔
114
                        ->where_null( 'indexable_id' )
2✔
115
                        ->delete_many();
2✔
116
        }
117

118
        /**
119
         * Clears all SEO Links by indexable ID.
120
         *
121
         * @param int $indexable_id The indexable ID.
122
         *
123
         * @return bool Whether or not the delete was succesfull.
124
         */
125
        public function delete_all_by_indexable_id( $indexable_id ) {
2✔
126
                return $this->query()
2✔
127
                        ->where( 'indexable_id', $indexable_id )
2✔
128
                        ->delete_many();
2✔
129
        }
130

131
        /**
132
         * Returns incoming link counts for a number of posts.
133
         *
134
         * @param array $post_ids The post IDs.
135
         *
136
         * @return array An array of associative arrays, each containing a post id and incoming property.
137
         */
138
        public function get_incoming_link_counts_for_post_ids( $post_ids ) {
2✔
139
                return $this->query()
2✔
140
                        ->select_expr( 'COUNT( id )', 'incoming' )
2✔
141
                        ->select( 'target_post_id', 'post_id' )
2✔
142
                        ->where_in( 'target_post_id', $post_ids )
2✔
143
                        ->group_by( 'target_post_id' )
2✔
144
                        ->find_array();
2✔
145
        }
146

147
        /**
148
         * Returns incoming link counts for a number of indexables.
149
         *
150
         * @param array $indexable_ids The indexable IDs.
151
         *
152
         * @return array An array of associative arrays, each containing a indexable id and incoming property.
153
         */
154
        public function get_incoming_link_counts_for_indexable_ids( $indexable_ids ) {
6✔
155
                if ( empty( $indexable_ids ) ) {
6✔
UNCOV
156
                        return [];
×
157
                }
158

159
                // This query only returns ID's with an incoming count > 0. We need to restore any ID's with 0 incoming links later.
160
                $indexable_counts = $this->query()
6✔
161
                        ->select_expr( 'COUNT( id )', 'incoming' )
6✔
162
                        ->select( 'target_indexable_id' )
6✔
163
                        ->where_in( 'target_indexable_id', $indexable_ids )
6✔
164
                        ->group_by( 'target_indexable_id' )
6✔
165
                        ->find_array();
6✔
166

167
                // If the above query fails, do not update anything.
168
                if ( ! \is_array( $indexable_counts ) ) {
6✔
169
                        return [];
2✔
170
                }
171

172
                // Get all ID's returned from the query and set them as keys for easy access.
173
                $returned_ids = \array_flip( \array_column( $indexable_counts, 'target_indexable_id' ) );
4✔
174

175
                // Loop over the original ID's and search them in the returned ID's. If they don't exist, add them with an incoming count of 0.
176
                foreach ( $indexable_ids as $id ) {
4✔
177
                        // Cast the ID to string, as the arrays only contain stringified versions of the ID.
178
                        $id = \strval( $id );
4✔
179
                        if ( isset( $returned_ids[ $id ] ) === false ) {
4✔
180
                                $indexable_counts[] = [
2✔
181
                                        'incoming'            => '0',
2✔
182
                                        'target_indexable_id' => $id,
2✔
183
                                ];
1✔
184
                        }
185
                }
186

187
                return $indexable_counts;
4✔
188
        }
189

190
        /**
191
         * Deletes all seo links for the given ids.
192
         *
193
         * @param int[] $ids The seo link ids.
194
         *
195
         * @return bool Whether or not the delete was succesfull.
196
         */
197
        public function delete_many_by_id( $ids ) {
2✔
198
                return $this->query()
2✔
199
                        ->where_in( 'id', $ids )
2✔
200
                        ->delete_many();
2✔
201
        }
202

203
        /**
204
         * Insert multiple seo links.
205
         *
206
         * @param SEO_Links[] $links The seo links to be inserted.
207
         *
208
         * @return bool Whether or not the insert was succesfull.
209
         */
210
        public function insert_many( $links ) {
2✔
211
                return $this->query()
2✔
212
                        ->insert_many( $links );
2✔
213
        }
214
}
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

© 2025 Coveralls, Inc