| 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(), |
| 45 |
); |
| 46 |
|
| 47 |
$this->add_for_page_types( |
| 48 |
[ 'post', 'term-in-custom-taxonomy' ], |
| 49 |
WPSEO_Custom_Taxonomies::get_custom_taxonomies(), |
| 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() { |
| 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( |
| 65 |
'wpseo_editor_specific_replace_vars', |
| 66 |
$this->replacement_variables, |
| 67 |
); |
| 68 |
|
| 69 |
if ( ! is_array( $replacement_variables ) ) { |
| 70 |
$replacement_variables = $this->replacement_variables; |
| 71 |
} |
| 72 |
|
| 73 |
return array_filter( $replacement_variables, 'is_array' ); |
| 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 ) { |
| 86 |
$shared_variables = array_diff( |
| 87 |
$this->extract_names( $replacement_variables ), |
| 88 |
$this->get_unique_replacement_variables(), |
| 89 |
); |
| 90 |
|
| 91 |
return array_values( $shared_variables ); |
| 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 ) { |
| 102 |
$replacement_variables = $this->get(); |
| 103 |
if ( array_key_exists( $taxonomy, $replacement_variables ) ) { |
| 104 |
return $taxonomy; |
| 105 |
} |
| 106 |
|
| 107 |
return 'term-in-custom-taxonomy'; |
| 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 ) { |
| 118 |
if ( $post instanceof WP_Post === false ) { |
| 119 |
return 'post'; |
| 120 |
} |
| 121 |
|
| 122 |
$replacement_variables = $this->get(); |
| 123 |
if ( array_key_exists( $post->post_type, $replacement_variables ) ) { |
| 124 |
return $post->post_type; |
| 125 |
} |
| 126 |
|
| 127 |
return 'custom_post_type'; |
| 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' ) { |
| 139 |
if ( ! $this->has_for_page_type( $post_type ) ) { |
| 140 |
return $fallback; |
| 141 |
} |
| 142 |
|
| 143 |
return $post_type; |
| 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' ) { |
| 155 |
$page_type = $name . '_archive'; |
| 156 |
|
| 157 |
if ( ! $this->has_for_page_type( $page_type ) ) { |
| 158 |
return $fallback; |
| 159 |
} |
| 160 |
|
| 161 |
return $page_type; |
| 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 ) { |
| 173 |
if ( empty( $replacement_variables_to_add ) ) { |
| 174 |
return; |
| 175 |
} |
| 176 |
|
| 177 |
$replacement_variables_to_add = array_fill_keys( $page_types, $replacement_variables_to_add ); |
| 178 |
$replacement_variables = $this->replacement_variables; |
| 179 |
|
| 180 |
$this->replacement_variables = array_merge_recursive( $replacement_variables, $replacement_variables_to_add ); |
| 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 ) { |
| 191 |
$extracted_names = []; |
| 192 |
|
| 193 |
foreach ( $replacement_variables as $replacement_variable ) { |
| 194 |
if ( empty( $replacement_variable['name'] ) ) { |
| 195 |
continue; |
| 196 |
} |
| 197 |
|
| 198 |
$extracted_names[] = $replacement_variable['name']; |
| 199 |
} |
| 200 |
|
| 201 |
return $extracted_names; |
| 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 ) { |
| 212 |
$replacement_variables = $this->get(); |
| 213 |
|
| 214 |
return ( ! empty( $replacement_variables[ $page_type ] ) && is_array( $replacement_variables[ $page_type ] ) ); |
| 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() { |
| 223 |
$merged_replacement_variables = call_user_func_array( 'array_merge', array_values( $this->get() ) ); |
| 224 |
|
| 225 |
return array_unique( $merged_replacement_variables ); |
| 226 |
} |
| 227 |
} |
| 228 |
|