• 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

2.41
/admin/formatter/class-post-metabox-formatter.php
1
<?php
2
/**
3
 * WPSEO plugin file.
4
 *
5
 * @package WPSEO\Admin\Formatter
6
 */
7

8
/**
9
 * This class provides data for the post metabox by return its values for localization.
10
 */
11
class WPSEO_Post_Metabox_Formatter implements WPSEO_Metabox_Formatter_Interface {
12

13
        /**
14
         * Holds the WordPress Post.
15
         *
16
         * @var WP_Post
17
         */
18
        private $post;
19

20
        /**
21
         * The permalink to follow.
22
         *
23
         * @var string
24
         */
25
        private $permalink;
26

27
        /**
28
         * Whether we must return social templates values.
29
         *
30
         * @var bool
31
         */
32
        private $use_social_templates = false;
33

34
        /**
35
         * Constructor.
36
         *
37
         * @param WP_Post|array $post      Post object.
38
         * @param array         $options   Title options to use.
39
         * @param string        $structure The permalink to follow.
40
         */
UNCOV
41
        public function __construct( $post, array $options, $structure ) {
×
UNCOV
42
                $this->post      = $post;
×
UNCOV
43
                $this->permalink = $structure;
×
44

UNCOV
45
                $this->use_social_templates = $this->use_social_templates();
×
46
        }
47

48
        /**
49
         * Determines whether the social templates should be used.
50
         *
51
         * @return bool Whether the social templates should be used.
52
         */
53
        public function use_social_templates() {
×
54
                return WPSEO_Options::get( 'opengraph', false ) === true;
×
55
        }
56

57
        /**
58
         * Returns the translated values.
59
         *
60
         * @return array
61
         */
UNCOV
62
        public function get_values() {
×
63

64
                $values = [
UNCOV
65
                        'search_url'          => $this->search_url(),
×
UNCOV
66
                        'post_edit_url'       => $this->edit_url(),
×
UNCOV
67
                        'base_url'            => $this->base_url_for_js(),
×
UNCOV
68
                        'metaDescriptionDate' => '',
×
69
                ];
70

UNCOV
71
                if ( $this->post instanceof WP_Post ) {
×
UNCOV
72
                        $keyword_usage = $this->get_focus_keyword_usage();
×
73

74
                        $values_to_set = [
UNCOV
75
                                'keyword_usage'               => $keyword_usage,
×
UNCOV
76
                                'keyword_usage_post_types'    => $this->get_post_types_for_all_ids( $keyword_usage ),
×
UNCOV
77
                                'title_template'              => $this->get_title_template(),
×
UNCOV
78
                                'title_template_no_fallback'  => $this->get_title_template( false ),
×
UNCOV
79
                                'metadesc_template'           => $this->get_metadesc_template(),
×
UNCOV
80
                                'metaDescriptionDate'         => $this->get_metadesc_date(),
×
UNCOV
81
                                'first_content_image'         => $this->get_image_url(),
×
UNCOV
82
                                'social_title_template'       => $this->get_social_title_template(),
×
UNCOV
83
                                'social_description_template' => $this->get_social_description_template(),
×
UNCOV
84
                                'social_image_template'       => $this->get_social_image_template(),
×
UNCOV
85
                                'isInsightsEnabled'           => $this->is_insights_enabled(),
×
86
                        ];
87

UNCOV
88
                        $values = ( $values_to_set + $values );
×
89
                }
90

91
                /**
92
                 * Filter: 'wpseo_post_edit_values' - Allows changing the values Yoast SEO uses inside the post editor.
93
                 *
94
                 * @param array   $values The key-value map Yoast SEO uses inside the post editor.
95
                 * @param WP_Post $post   The post opened in the editor.
96
                 */
UNCOV
97
                return apply_filters( 'wpseo_post_edit_values', $values, $this->post );
×
98
        }
99

100
        /**
101
         * Gets the image URL for the post's social preview.
102
         *
103
         * @return string|null The image URL for the social preview.
104
         */
105
        protected function get_image_url() {
6✔
106
                return WPSEO_Image_Utils::get_first_usable_content_image_for_post( $this->post->ID );
6✔
107
        }
108

109
        /**
110
         * Returns the url to search for keyword for the post.
111
         *
112
         * @return string
113
         */
UNCOV
114
        private function search_url() {
×
UNCOV
115
                return admin_url( 'edit.php?seo_kw_filter={keyword}' );
×
116
        }
117

118
        /**
119
         * Returns the url to edit the taxonomy.
120
         *
121
         * @return string
122
         */
UNCOV
123
        private function edit_url() {
×
UNCOV
124
                return admin_url( 'post.php?post={id}&action=edit' );
×
125
        }
126

127
        /**
128
         * Returns a base URL for use in the JS, takes permalink structure into account.
129
         *
130
         * @return string
131
         */
UNCOV
132
        private function base_url_for_js() {
×
UNCOV
133
                global $pagenow;
×
134

135
                // The default base is the home_url.
UNCOV
136
                $base_url = home_url( '/', null );
×
137

UNCOV
138
                if ( $pagenow === 'post-new.php' ) {
×
UNCOV
139
                        return $base_url;
×
140
                }
141

142
                // If %postname% is the last tag, just strip it and use that as a base.
UNCOV
143
                if ( preg_match( '#%postname%/?$#', $this->permalink ) === 1 ) {
×
UNCOV
144
                        $base_url = preg_replace( '#%postname%/?$#', '', $this->permalink );
×
145
                }
146

147
                // If %pagename% is the last tag, just strip it and use that as a base.
UNCOV
148
                if ( preg_match( '#%pagename%/?$#', $this->permalink ) === 1 ) {
×
UNCOV
149
                        $base_url = preg_replace( '#%pagename%/?$#', '', $this->permalink );
×
150
                }
151

UNCOV
152
                return $base_url;
×
153
        }
154

155
        /**
156
         * Counts the number of given keywords used for other posts other than the given post_id.
157
         *
158
         * @return array The keyword and the associated posts that use it.
159
         */
UNCOV
160
        private function get_focus_keyword_usage() {
×
UNCOV
161
                $keyword = WPSEO_Meta::get_value( 'focuskw', $this->post->ID );
×
UNCOV
162
                $usage   = [ $keyword => $this->get_keyword_usage_for_current_post( $keyword ) ];
×
163

164
                /**
165
                 * Allows enhancing the array of posts' that share their focus keywords with the post's related keywords.
166
                 *
167
                 * @param array $usage   The array of posts' ids that share their focus keywords with the post.
168
                 * @param int   $post_id The id of the post we're finding the usage of related keywords for.
169
                 */
UNCOV
170
                return apply_filters( 'wpseo_posts_for_related_keywords', $usage, $this->post->ID );
×
171
        }
172

173
        /**
174
         * Retrieves the post types for the given post IDs.
175
         *
176
         * @param array $post_ids_per_keyword An associative array with keywords as keys and an array of post ids where those keywords are used.
177
         * @return array The post types for the given post IDs.
178
         */
179
        private function get_post_types_for_all_ids( $post_ids_per_keyword ) {
×
180

181
                $post_type_per_keyword_result = [];
×
182
                foreach ( $post_ids_per_keyword as $keyword => $post_ids ) {
×
183
                        $post_type_per_keyword_result[ $keyword ] = WPSEO_Meta::post_types_for_ids( $post_ids );
×
184
                }
185

186
                return $post_type_per_keyword_result;
×
187
        }
188

189
        /**
190
         * Gets the keyword usage for the current post and the specified keyword.
191
         *
192
         * @param string $keyword The keyword to check the usage of.
193
         *
194
         * @return array The post IDs which use the passed keyword.
195
         */
196
        protected function get_keyword_usage_for_current_post( $keyword ) {
×
197
                return WPSEO_Meta::keyword_usage( $keyword, $this->post->ID );
×
198
        }
199

200
        /**
201
         * Retrieves the title template.
202
         *
203
         * @param bool $fallback Whether to return the hardcoded fallback if the template value is empty.
204
         *
205
         * @return string The title template.
206
         */
UNCOV
207
        private function get_title_template( $fallback = true ) {
×
UNCOV
208
                $title = $this->get_template( 'title' );
×
209

UNCOV
210
                if ( $title === '' && $fallback === true ) {
×
UNCOV
211
                        return '%%title%% %%page%% %%sep%% %%sitename%%';
×
212
                }
213

UNCOV
214
                return $title;
×
215
        }
216

217
        /**
218
         * Retrieves the metadesc template.
219
         *
220
         * @return string The metadesc template.
221
         */
UNCOV
222
        private function get_metadesc_template() {
×
UNCOV
223
                return $this->get_template( 'metadesc' );
×
224
        }
225

226
        /**
227
         * Retrieves the social title template.
228
         *
229
         * @return string The social title template.
230
         */
231
        private function get_social_title_template() {
×
232
                if ( $this->use_social_templates ) {
×
233
                        return $this->get_social_template( 'title' );
×
234
                }
235

236
                return '';
×
237
        }
238

239
        /**
240
         * Retrieves the social description template.
241
         *
242
         * @return string The social description template.
243
         */
244
        private function get_social_description_template() {
×
245
                if ( $this->use_social_templates ) {
×
246
                        return $this->get_social_template( 'description' );
×
247
                }
248

249
                return '';
×
250
        }
251

252
        /**
253
         * Retrieves the social image template.
254
         *
255
         * @return string The social description template.
256
         */
257
        private function get_social_image_template() {
×
258
                if ( $this->use_social_templates ) {
×
259
                        return $this->get_social_template( 'image-url' );
×
260
                }
261

262
                return '';
×
263
        }
264

265
        /**
266
         * Retrieves a template.
267
         *
268
         * @param string $template_option_name The name of the option in which the template you want to get is saved.
269
         *
270
         * @return string
271
         */
UNCOV
272
        private function get_template( $template_option_name ) {
×
UNCOV
273
                $needed_option = $template_option_name . '-' . $this->post->post_type;
×
274

UNCOV
275
                if ( WPSEO_Options::get( $needed_option, '' ) !== '' ) {
×
276
                        return WPSEO_Options::get( $needed_option );
×
277
                }
278

UNCOV
279
                return '';
×
280
        }
281

282
        /**
283
         * Retrieves a social template.
284
         *
285
         * @param string $template_option_name The name of the option in which the template you want to get is saved.
286
         *
287
         * @return string
288
         */
289
        private function get_social_template( $template_option_name ) {
×
290
                /**
291
                 * Filters the social template value for a given post type.
292
                 *
293
                 * @param string $template             The social template value, defaults to empty string.
294
                 * @param string $template_option_name The subname of the option in which the template you want to get is saved.
295
                 * @param string $post_type            The name of the post type.
296
                 */
297
                return apply_filters( 'wpseo_social_template_post_type', '', $template_option_name, $this->post->post_type );
×
298
        }
299

300
        /**
301
         * Determines the date to be displayed in the snippet preview.
302
         *
303
         * @return string
304
         */
UNCOV
305
        private function get_metadesc_date() {
×
UNCOV
306
                return YoastSEO()->helpers->date->format_translated( $this->post->post_date, 'M j, Y' );
×
307
        }
308

309
        /**
310
         * Determines whether the insights feature is enabled for this post.
311
         *
312
         * @return bool
313
         */
314
        protected function is_insights_enabled() {
×
315
                return WPSEO_Options::get( 'enable_metabox_insights', false );
×
316
        }
317
}
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