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

Yoast / wordpress-seo / 99ff5821fbe8444b259463501ff00132570d3061

25 Mar 2025 09:23AM UTC coverage: 52.446% (+3.7%) from 48.71%
99ff5821fbe8444b259463501ff00132570d3061

Pull #21958

github

web-flow
Merge d493347a3 into facbdded4
Pull Request #21958: Improve function words list for Farsi

7990 of 14101 branches covered (56.66%)

Branch coverage included in aggregate %.

20 of 20 new or added lines in 1 file covered. (100.0%)

1567 existing lines in 41 files now uncovered.

29816 of 57984 relevant lines covered (51.42%)

41124.2 hits per line

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

85.71
/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
         */
58
        public function get() {
4✔
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
                 */
64
                $replacement_variables = apply_filters(
4✔
65
                        'wpseo_editor_specific_replace_vars',
4✔
66
                        $this->replacement_variables
4✔
67
                );
4✔
68

69
                if ( ! is_array( $replacement_variables ) ) {
4✔
70
                        $replacement_variables = $this->replacement_variables;
2✔
71
                }
72

73
                return array_filter( $replacement_variables, 'is_array' );
4✔
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
         */
85
        public function get_generic( $replacement_variables ) {
2✔
86
                $shared_variables = array_diff(
2✔
87
                        $this->extract_names( $replacement_variables ),
2✔
88
                        $this->get_unique_replacement_variables()
2✔
89
                );
2✔
90

91
                return array_values( $shared_variables );
2✔
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
         */
101
        public function determine_for_term( $taxonomy ) {
8✔
102
                $replacement_variables = $this->get();
8✔
103
                if ( array_key_exists( $taxonomy, $replacement_variables ) ) {
8✔
104
                        return $taxonomy;
6✔
105
                }
106

107
                return 'term-in-custom-taxonomy';
2✔
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
         */
117
        public function determine_for_post( $post ) {
8✔
118
                if ( $post instanceof WP_Post === false ) {
8✔
119
                        return 'post';
2✔
120
                }
121

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

127
                return 'custom_post_type';
2✔
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
         */
138
        public function determine_for_post_type( $post_type, $fallback = 'custom_post_type' ) {
6✔
139
                if ( ! $this->has_for_page_type( $post_type ) ) {
6✔
140
                        return $fallback;
4✔
141
                }
142

143
                return $post_type;
2✔
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
         */
154
        public function determine_for_archive( $name, $fallback = 'custom-post-type_archive' ) {
10✔
155
                $page_type = $name . '_archive';
10✔
156

157
                if ( ! $this->has_for_page_type( $page_type ) ) {
10✔
158
                        return $fallback;
8✔
159
                }
160

161
                return $page_type;
2✔
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
         */
172
        protected function add_for_page_types( array $page_types, array $replacement_variables_to_add ) {
4✔
173
                if ( empty( $replacement_variables_to_add ) ) {
4✔
174
                        return;
4✔
175
                }
176

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

180
                $this->replacement_variables = array_merge_recursive( $replacement_variables, $replacement_variables_to_add );
2✔
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
         */
190
        protected function extract_names( $replacement_variables ) {
2✔
191
                $extracted_names = [];
2✔
192

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

198
                        $extracted_names[] = $replacement_variable['name'];
2✔
199
                }
200

201
                return $extracted_names;
2✔
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
         */
211
        protected function has_for_page_type( $page_type ) {
4✔
212
                $replacement_variables = $this->get();
4✔
213

214
                return ( ! empty( $replacement_variables[ $page_type ] ) && is_array( $replacement_variables[ $page_type ] ) );
4✔
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
         */
222
        protected function get_unique_replacement_variables() {
2✔
223
                $merged_replacement_variables = call_user_func_array( 'array_merge', array_values( $this->get() ) );
2✔
224

225
                return array_unique( $merged_replacement_variables );
2✔
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