• 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

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

8
/**
9
 * Determines the recommended replacement variables based on the context.
10
 */
11
class WPSEO_Admin_Recommended_Replace_Vars {
12

13
        /**
14
         * The recommended replacement variables.
15
         *
16
         * @var array
17
         */
18
        protected $recommended_replace_vars = [
19
                // Posts types.
20
                'page'                     => [ 'sitename', 'title', 'sep', 'primary_category' ],
21
                'post'                     => [ 'sitename', 'title', 'sep', 'primary_category' ],
22
                // Homepage.
23
                'homepage'                 => [ 'sitename', 'sitedesc', 'sep' ],
24
                // Custom post type.
25
                'custom_post_type'         => [ 'sitename', 'title', 'sep' ],
26

27
                // Taxonomies.
28
                'category'                 => [ 'sitename', 'term_title', 'sep', 'term_hierarchy' ],
29
                'post_tag'                 => [ 'sitename', 'term_title', 'sep' ],
30
                'post_format'              => [ 'sitename', 'term_title', 'sep', 'page' ],
31

32
                // Custom taxonomy.
33
                'term-in-custom-taxonomy'  => [ 'sitename', 'term_title', 'sep', 'term_hierarchy' ],
34

35
                // Settings - archive pages.
36
                'author_archive'           => [ 'sitename', 'title', 'sep', 'page' ],
37
                'date_archive'             => [ 'sitename', 'sep', 'date', 'page' ],
38
                'custom-post-type_archive' => [ 'sitename', 'title', 'sep', 'page' ],
39

40
                // Settings - special pages.
41
                'search'                   => [ 'sitename', 'searchphrase', 'sep', 'page' ],
42
                '404'                      => [ 'sitename', 'sep' ],
43
        ];
44

45
        /**
46
         * Determines the page type of the current term.
47
         *
48
         * @param string $taxonomy The taxonomy name.
49
         *
50
         * @return string The page type.
51
         */
52
        public function determine_for_term( $taxonomy ) {
12✔
53
                $recommended_replace_vars = $this->get_recommended_replacevars();
12✔
54
                if ( array_key_exists( $taxonomy, $recommended_replace_vars ) ) {
12✔
55
                        return $taxonomy;
12✔
56
                }
57

58
                return 'term-in-custom-taxonomy';
×
59
        }
60

61
        /**
62
         * Determines the page type of the current post.
63
         *
64
         * @param WP_Post $post A WordPress post instance.
65
         *
66
         * @return string The page type.
67
         */
68
        public function determine_for_post( $post ) {
20✔
69
                if ( $post instanceof WP_Post === false ) {
20✔
70
                        return 'post';
4✔
71
                }
72

73
                if ( $post->post_type === 'page' && $this->is_homepage( $post ) ) {
16✔
74
                        return 'homepage';
4✔
75
                }
76

77
                $recommended_replace_vars = $this->get_recommended_replacevars();
12✔
78
                if ( array_key_exists( $post->post_type, $recommended_replace_vars ) ) {
12✔
79
                        return $post->post_type;
8✔
80
                }
81

82
                return 'custom_post_type';
4✔
83
        }
84

85
        /**
86
         * Determines the page type for a post type.
87
         *
88
         * @param string $post_type The name of the post_type.
89
         * @param string $fallback  The page type to fall back to.
90
         *
91
         * @return string The page type.
92
         */
93
        public function determine_for_post_type( $post_type, $fallback = 'custom_post_type' ) {
12✔
94
                $page_type                   = $post_type;
12✔
95
                $recommended_replace_vars    = $this->get_recommended_replacevars();
12✔
96
                $has_recommended_replacevars = $this->has_recommended_replace_vars( $recommended_replace_vars, $page_type );
12✔
97

98
                if ( ! $has_recommended_replacevars ) {
12✔
99
                        return $fallback;
8✔
100
                }
101

102
                return $page_type;
4✔
103
        }
104

105
        /**
106
         * Determines the page type for an archive page.
107
         *
108
         * @param string $name     The name of the archive.
109
         * @param string $fallback The page type to fall back to.
110
         *
111
         * @return string The page type.
112
         */
113
        public function determine_for_archive( $name, $fallback = 'custom-post-type_archive' ) {
16✔
114
                $page_type                   = $name . '_archive';
16✔
115
                $recommended_replace_vars    = $this->get_recommended_replacevars();
16✔
116
                $has_recommended_replacevars = $this->has_recommended_replace_vars( $recommended_replace_vars, $page_type );
16✔
117

118
                if ( ! $has_recommended_replacevars ) {
16✔
119
                        return $fallback;
8✔
120
                }
121

122
                return $page_type;
8✔
123
        }
124

125
        /**
126
         * Retrieves the recommended replacement variables for the given page type.
127
         *
128
         * @param string $page_type The page type.
129
         *
130
         * @return array The recommended replacement variables.
131
         */
132
        public function get_recommended_replacevars_for( $page_type ) {
60✔
133
                $recommended_replace_vars     = $this->get_recommended_replacevars();
60✔
134
                $has_recommended_replace_vars = $this->has_recommended_replace_vars( $recommended_replace_vars, $page_type );
60✔
135

136
                if ( ! $has_recommended_replace_vars ) {
60✔
137
                        return [];
8✔
138
                }
139

140
                return $recommended_replace_vars[ $page_type ];
52✔
141
        }
142

143
        /**
144
         * Retrieves the recommended replacement variables.
145
         *
146
         * @return array The recommended replacement variables.
147
         */
148
        public function get_recommended_replacevars() {
×
149
                /**
150
                 * Filter: Adds the possibility to add extra recommended replacement variables.
151
                 *
152
                 * @api array $additional_replace_vars Empty array to add the replacevars to.
153
                 */
154
                $recommended_replace_vars = apply_filters( 'wpseo_recommended_replace_vars', $this->recommended_replace_vars );
×
155

156
                if ( ! is_array( $recommended_replace_vars ) ) {
×
157
                        return $this->recommended_replace_vars;
×
158
                }
159

160
                return $recommended_replace_vars;
×
161
        }
162

163
        /**
164
         * Returns whether the given page type has recommended replace vars.
165
         *
166
         * @param array  $recommended_replace_vars The recommended replace vars
167
         *                                         to check in.
168
         * @param string $page_type                The page type to check.
169
         *
170
         * @return bool True if there are associated recommended replace vars.
171
         */
172
        private function has_recommended_replace_vars( $recommended_replace_vars, $page_type ) {
×
173
                if ( ! isset( $recommended_replace_vars[ $page_type ] ) ) {
×
174
                        return false;
×
175
                }
176

177
                if ( ! is_array( $recommended_replace_vars[ $page_type ] ) ) {
×
178
                        return false;
×
179
                }
180

181
                return true;
×
182
        }
183

184
        /**
185
         * Determines whether or not a post is the homepage.
186
         *
187
         * @param WP_Post $post The WordPress global post object.
188
         *
189
         * @return bool True if the given post is the homepage.
190
         */
191
        private function is_homepage( $post ) {
4✔
192
                if ( $post instanceof WP_Post === false ) {
4✔
193
                        return false;
×
194
                }
195

196
                /*
197
                 * The page on front returns a string with normal WordPress interaction, while the post ID is an int.
198
                 * This way we make sure we always compare strings.
199
                 */
200
                $post_id       = (int) $post->ID;
4✔
201
                $page_on_front = (int) get_option( 'page_on_front' );
4✔
202

203
                return get_option( 'show_on_front' ) === 'page' && $page_on_front === $post_id;
4✔
204
        }
205
}
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