• 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

0.0
/admin/class-admin-editor-specific-replace-vars.php
1
<?php
2
/**
3
 * WPSEO plugin file.
4
 *
5
 * @package WPSEO\Admin
6
 */
7

8
/**
9
 * Determines the editor specific replacement variables.
10
 */
11
class WPSEO_Admin_Editor_Specific_Replace_Vars {
12

13
        /**
14
         * Holds the editor specific replacements variables.
15
         *
16
         * @var array The editor specific replacement variables.
17
         */
18
        protected $replacement_variables = [
19
                // Posts types.
20
                'page'                     => [ 'id', 'pt_single', 'pt_plural', 'parent_title' ],
21
                'post'                     => [ 'id', 'term404', 'pt_single', 'pt_plural' ],
22
                // Custom post type.
23
                'custom_post_type'         => [ 'id', 'term404', 'pt_single', 'pt_plural', 'parent_title' ],
24
                // Settings - archive pages.
25
                'custom-post-type_archive' => [ 'pt_single', 'pt_plural' ],
26

27
                // Taxonomies.
28
                'category'                 => [ 'term_title', 'term_description', 'category_description', 'parent_title', 'term_hierarchy' ],
29
                'post_tag'                 => [ 'term_title', 'term_description', 'tag_description' ],
30
                'post_format'              => [ 'term_title' ],
31
                // Custom taxonomy.
32
                'term-in-custom-taxonomy'  => [ 'term_title', 'term_description', 'category_description', 'parent_title', 'term_hierarchy' ],
33

34
                // Settings - special pages.
35
                'search'                   => [ 'searchphrase' ],
36
        ];
37

38
        /**
39
         * WPSEO_Admin_Editor_Specific_Replace_Vars constructor.
40
         */
41
        public function __construct() {
×
42
                $this->add_for_page_types(
×
43
                        [ 'page', 'post', 'custom_post_type' ],
×
44
                        WPSEO_Custom_Fields::get_custom_fields()
×
UNCOV
45
                );
×
46

47
                $this->add_for_page_types(
×
48
                        [ 'post', 'term-in-custom-taxonomy' ],
×
49
                        WPSEO_Custom_Taxonomies::get_custom_taxonomies()
×
UNCOV
50
                );
×
51
        }
52

53
        /**
54
         * Retrieves the editor specific replacement variables.
55
         *
56
         * @return array The editor specific replacement variables.
57
         */
UNCOV
58
        public function get() {
×
59
                /**
60
                 * Filter: Adds the possibility to add extra editor specific replacement variables.
61
                 *
62
                 * @param array $replacement_variables Array of editor specific replace vars.
63
                 */
UNCOV
64
                $replacement_variables = apply_filters(
×
UNCOV
65
                        'wpseo_editor_specific_replace_vars',
×
UNCOV
66
                        $this->replacement_variables
×
UNCOV
67
                );
×
68

UNCOV
69
                if ( ! is_array( $replacement_variables ) ) {
×
UNCOV
70
                        $replacement_variables = $this->replacement_variables;
×
71
                }
72

UNCOV
73
                return array_filter( $replacement_variables, 'is_array' );
×
74
        }
75

76
        /**
77
         * Retrieves the generic replacement variable names.
78
         *
79
         * Which are the replacement variables without the editor specific ones.
80
         *
81
         * @param array $replacement_variables Possibly generic replacement variables.
82
         *
83
         * @return array The generic replacement variable names.
84
         */
UNCOV
85
        public function get_generic( $replacement_variables ) {
×
UNCOV
86
                $shared_variables = array_diff(
×
UNCOV
87
                        $this->extract_names( $replacement_variables ),
×
UNCOV
88
                        $this->get_unique_replacement_variables()
×
UNCOV
89
                );
×
90

UNCOV
91
                return array_values( $shared_variables );
×
92
        }
93

94
        /**
95
         * Determines the page type of the current term.
96
         *
97
         * @param string $taxonomy The taxonomy name.
98
         *
99
         * @return string The page type.
100
         */
UNCOV
101
        public function determine_for_term( $taxonomy ) {
×
UNCOV
102
                $replacement_variables = $this->get();
×
UNCOV
103
                if ( array_key_exists( $taxonomy, $replacement_variables ) ) {
×
UNCOV
104
                        return $taxonomy;
×
105
                }
106

UNCOV
107
                return 'term-in-custom-taxonomy';
×
108
        }
109

110
        /**
111
         * Determines the page type of the current post.
112
         *
113
         * @param WP_Post $post A WordPress post instance.
114
         *
115
         * @return string The page type.
116
         */
UNCOV
117
        public function determine_for_post( $post ) {
×
UNCOV
118
                if ( $post instanceof WP_Post === false ) {
×
UNCOV
119
                        return 'post';
×
120
                }
121

UNCOV
122
                $replacement_variables = $this->get();
×
UNCOV
123
                if ( array_key_exists( $post->post_type, $replacement_variables ) ) {
×
UNCOV
124
                        return $post->post_type;
×
125
                }
126

UNCOV
127
                return 'custom_post_type';
×
128
        }
129

130
        /**
131
         * Determines the page type for a post type.
132
         *
133
         * @param string $post_type The name of the post_type.
134
         * @param string $fallback  The page type to fall back to.
135
         *
136
         * @return string The page type.
137
         */
UNCOV
138
        public function determine_for_post_type( $post_type, $fallback = 'custom_post_type' ) {
×
UNCOV
139
                if ( ! $this->has_for_page_type( $post_type ) ) {
×
UNCOV
140
                        return $fallback;
×
141
                }
142

UNCOV
143
                return $post_type;
×
144
        }
145

146
        /**
147
         * Determines the page type for an archive page.
148
         *
149
         * @param string $name     The name of the archive.
150
         * @param string $fallback The page type to fall back to.
151
         *
152
         * @return string The page type.
153
         */
UNCOV
154
        public function determine_for_archive( $name, $fallback = 'custom-post-type_archive' ) {
×
UNCOV
155
                $page_type = $name . '_archive';
×
156

UNCOV
157
                if ( ! $this->has_for_page_type( $page_type ) ) {
×
UNCOV
158
                        return $fallback;
×
159
                }
160

UNCOV
161
                return $page_type;
×
162
        }
163

164
        /**
165
         * Adds the replavement variables for the given page types.
166
         *
167
         * @param array $page_types                   Page types to add variables for.
168
         * @param array $replacement_variables_to_add The variables to add.
169
         *
170
         * @return void
171
         */
UNCOV
172
        protected function add_for_page_types( array $page_types, array $replacement_variables_to_add ) {
×
UNCOV
173
                if ( empty( $replacement_variables_to_add ) ) {
×
UNCOV
174
                        return;
×
175
                }
176

UNCOV
177
                $replacement_variables_to_add = array_fill_keys( $page_types, $replacement_variables_to_add );
×
UNCOV
178
                $replacement_variables        = $this->replacement_variables;
×
179

UNCOV
180
                $this->replacement_variables = array_merge_recursive( $replacement_variables, $replacement_variables_to_add );
×
181
        }
182

183
        /**
184
         * Extracts the names from the given replacements variables.
185
         *
186
         * @param array $replacement_variables Replacement variables to extract the name from.
187
         *
188
         * @return array Extracted names.
189
         */
UNCOV
190
        protected function extract_names( $replacement_variables ) {
×
UNCOV
191
                $extracted_names = [];
×
192

UNCOV
193
                foreach ( $replacement_variables as $replacement_variable ) {
×
UNCOV
194
                        if ( empty( $replacement_variable['name'] ) ) {
×
UNCOV
195
                                continue;
×
196
                        }
197

UNCOV
198
                        $extracted_names[] = $replacement_variable['name'];
×
199
                }
200

UNCOV
201
                return $extracted_names;
×
202
        }
203

204
        /**
205
         * Returns whether the given page type has editor specific replace vars.
206
         *
207
         * @param string $page_type The page type to check.
208
         *
209
         * @return bool True if there are associated editor specific replace vars.
210
         */
UNCOV
211
        protected function has_for_page_type( $page_type ) {
×
UNCOV
212
                $replacement_variables = $this->get();
×
213

UNCOV
214
                return ( ! empty( $replacement_variables[ $page_type ] ) && is_array( $replacement_variables[ $page_type ] ) );
×
215
        }
216

217
        /**
218
         * Merges all editor specific replacement variables into one array and removes duplicates.
219
         *
220
         * @return array The list of unique editor specific replacement variables.
221
         */
UNCOV
222
        protected function get_unique_replacement_variables() {
×
UNCOV
223
                $merged_replacement_variables = call_user_func_array( 'array_merge', array_values( $this->get() ) );
×
224

UNCOV
225
                return array_unique( $merged_replacement_variables );
×
226
        }
227
}
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