PluginProbe ʕ •ᴥ•ʔ
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 27.5
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v27.5
27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 19.1 19.10 19.11 19.12 19.13 19.14 19.2 19.3 19.4 19.5 19.5.1 19.6 19.6.1 19.7 19.7.1 19.7.2 19.8 19.9 20.0 20.1 20.10 20.11 20.12 20.13 20.2 20.2.1 20.3 20.4 20.5 20.6 20.7 20.8 20.9 21.0 21.1 21.2 21.3 21.4 21.5 21.6 21.7 21.8 21.8.1 21.9 21.9.1 22.0 22.1 22.2 22.3 22.4 22.5 22.6 22.7 22.8 22.9 23.0 23.1 23.2 23.3 23.4 23.5 23.6 23.7 23.8 23.9 24.0 24.1 24.2 24.3 24.4 24.5 24.6 24.7 24.8 24.8.1 24.9 25.0 25.1 25.2 25.3 25.3.1 25.4 25.5 25.6 25.7 25.8 25.9 26.0 26.1 26.1.1 26.2 26.3 26.4 26.5 26.6 26.7 26.8 26.9 27.0 27.1 27.1.1 27.2 27.3 27.4
wordpress-seo / admin / class-admin-recommended-replace-vars.php
wordpress-seo / admin Last commit date
ajax 2 years ago capabilities 1 year ago endpoints 2 years ago exceptions 3 months ago filters 3 months ago formatter 1 year ago google_search_console 3 months ago import 3 months ago listeners 8 years ago menu 3 months ago metabox 3 months ago notifiers 3 months ago pages 3 months ago roles 3 months ago services 3 months ago statistics 3 months ago taxonomy 3 months ago tracking 3 months ago views 3 months ago watchers 3 months ago admin-settings-changed-listener.php 2 years ago ajax.php 3 months ago class-admin-asset-analysis-worker-location.php 3 months ago class-admin-asset-dev-server-location.php 3 months ago class-admin-asset-location.php 8 years ago class-admin-asset-manager.php 3 months ago class-admin-asset-seo-location.php 4 years ago class-admin-editor-specific-replace-vars.php 3 months ago class-admin-gutenberg-compatibility-notification.php 3 months ago class-admin-help-panel.php 3 months ago class-admin-init.php 3 months ago class-admin-recommended-replace-vars.php 2 years ago class-admin-user-profile.php 7 months ago class-admin-utils.php 3 months ago class-admin.php 3 months ago class-asset.php 1 year ago class-bulk-description-editor-list-table.php 3 months ago class-bulk-editor-list-table.php 3 months ago class-bulk-title-editor-list-table.php 3 months ago class-collector.php 1 year ago class-config.php 3 months ago class-database-proxy.php 3 months ago class-export.php 3 months ago class-expose-shortlinks.php 7 months ago class-gutenberg-compatibility.php 1 month ago class-meta-columns.php 3 months ago class-my-yoast-proxy.php 3 months ago class-option-tab.php 4 years ago class-option-tabs-formatter.php 3 months ago class-option-tabs.php 2 years ago class-paper-presenter.php 5 years ago class-plugin-availability.php 3 months ago class-plugin-conflict.php 2 years ago class-premium-popup.php 1 year ago class-premium-upsell-admin-block.php 3 months ago class-primary-term-admin.php 3 months ago class-product-upsell-notice.php 3 months ago class-remote-request.php 2 years ago class-schema-person-upgrade-notification.php 3 months ago class-suggested-plugins.php 3 months ago class-wincher-dashboard-widget.php 3 months ago class-yoast-columns.php 3 months ago class-yoast-dashboard-widget.php 3 months ago class-yoast-form.php 3 months ago class-yoast-input-validation.php 3 months ago class-yoast-network-admin.php 3 months ago class-yoast-network-settings-api.php 3 months ago class-yoast-notification-center.php 3 months ago class-yoast-notification.php 3 months ago class-yoast-notifications.php 3 months ago class-yoast-plugin-conflict.php 3 months ago index.php 10 years ago interface-collection.php 7 years ago interface-installable.php 8 years ago
class-admin-recommended-replace-vars.php
206 lines
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 ) {
53 $recommended_replace_vars = $this->get_recommended_replacevars();
54 if ( array_key_exists( $taxonomy, $recommended_replace_vars ) ) {
55 return $taxonomy;
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 ) {
69 if ( $post instanceof WP_Post === false ) {
70 return 'post';
71 }
72
73 if ( $post->post_type === 'page' && $this->is_homepage( $post ) ) {
74 return 'homepage';
75 }
76
77 $recommended_replace_vars = $this->get_recommended_replacevars();
78 if ( array_key_exists( $post->post_type, $recommended_replace_vars ) ) {
79 return $post->post_type;
80 }
81
82 return 'custom_post_type';
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' ) {
94 $page_type = $post_type;
95 $recommended_replace_vars = $this->get_recommended_replacevars();
96 $has_recommended_replacevars = $this->has_recommended_replace_vars( $recommended_replace_vars, $page_type );
97
98 if ( ! $has_recommended_replacevars ) {
99 return $fallback;
100 }
101
102 return $page_type;
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' ) {
114 $page_type = $name . '_archive';
115 $recommended_replace_vars = $this->get_recommended_replacevars();
116 $has_recommended_replacevars = $this->has_recommended_replace_vars( $recommended_replace_vars, $page_type );
117
118 if ( ! $has_recommended_replacevars ) {
119 return $fallback;
120 }
121
122 return $page_type;
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 ) {
133 $recommended_replace_vars = $this->get_recommended_replacevars();
134 $has_recommended_replace_vars = $this->has_recommended_replace_vars( $recommended_replace_vars, $page_type );
135
136 if ( ! $has_recommended_replace_vars ) {
137 return [];
138 }
139
140 return $recommended_replace_vars[ $page_type ];
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 * @param 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 ) {
192 if ( $post instanceof WP_Post === false ) {
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;
201 $page_on_front = (int) get_option( 'page_on_front' );
202
203 return get_option( 'show_on_front' ) === 'page' && $page_on_front === $post_id;
204 }
205 }
206