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 |