| 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-taxomomy' => [ '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-taxomomy'; |
| 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 |
* @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 ) { |
| 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 |
|