| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Services\Importing; |
| 4 |
|
| 5 |
/** |
| 6 |
* Handles AISOEO replacevars. |
| 7 |
*/ |
| 8 |
class Aioseo_Replacevar_Handler { |
| 9 |
|
| 10 |
/** |
| 11 |
* Mapping between the AiOSEO replace vars and the Yoast replace vars. |
| 12 |
* |
| 13 |
* @var array |
| 14 |
* |
| 15 |
* @see https://yoast.com/help/list-available-snippet-variables-yoast-seo/ |
| 16 |
*/ |
| 17 |
protected $replace_vars_map = [ |
| 18 |
// They key is the AiOSEO replace var, the value is the Yoast replace var (see class-wpseo-replace-vars). |
| 19 |
'#archive_title' => '%%archive_title%%', |
| 20 |
'#archive_date' => '%%date%%', |
| 21 |
'#attachment_caption' => '%%caption%%', |
| 22 |
'#author_bio' => '%%user_description%%', |
| 23 |
'#author_first_name' => '%%author_first_name%%', |
| 24 |
'#author_last_name' => '%%author_last_name%%', |
| 25 |
'#author_name' => '%%name%%', |
| 26 |
'#blog_title' => '%%sitename%%', // Same with #site_title. |
| 27 |
'#breadcrumb_taxonomy_title' => '', // Empty string, as AIOSEO shows nothing for that tag. |
| 28 |
'#breadcrumb_separator' => '', // Empty string, as AIOSEO shows nothing for that tag. |
| 29 |
'#categories' => '%%category%%', |
| 30 |
'#current_date' => '%%currentdate%%', |
| 31 |
'#current_day' => '%%currentday%%', |
| 32 |
'#current_month' => '%%currentmonth%%', |
| 33 |
'#current_year' => '%%currentyear%%', |
| 34 |
'#parent_title' => '%%parent_title%%', |
| 35 |
'#page_number' => '%%pagenumber%%', |
| 36 |
'#permalink' => '%%permalink%%', |
| 37 |
'#post_content' => '%%post_content%%', |
| 38 |
'#post_date' => '%%date%%', |
| 39 |
'#post_day' => '%%post_day%%', |
| 40 |
'#post_month' => '%%post_month%%', |
| 41 |
'#post_title' => '%%title%%', |
| 42 |
'#post_year' => '%%post_year%%', |
| 43 |
'#post_excerpt_only' => '%%excerpt_only%%', |
| 44 |
'#post_excerpt' => '%%excerpt%%', |
| 45 |
'#search_term' => '%%searchphrase%%', |
| 46 |
'#separator_sa' => '%%sep%%', |
| 47 |
'#site_title' => '%%sitename%%', |
| 48 |
'#tagline' => '%%sitedesc%%', |
| 49 |
'#taxonomy_title' => '%%category_title%%', |
| 50 |
'#taxonomy_description' => '%%term_description%%', |
| 51 |
]; |
| 52 |
|
| 53 |
/** |
| 54 |
* Edits the replace_vars map of the class. |
| 55 |
* |
| 56 |
* @param string $aioseo_var The AIOSEO replacevar. |
| 57 |
* @param string $yoast_var The Yoast replacevar. |
| 58 |
* |
| 59 |
* @return void. |
| 60 |
*/ |
| 61 |
public function compose_map( $aioseo_var, $yoast_var ) { |
| 62 |
$map = $this->replace_vars_map; |
| 63 |
|
| 64 |
$map[ $aioseo_var ] = $yoast_var; |
| 65 |
|
| 66 |
$this->replace_vars_map = $map; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Transforms AIOSEO replacevars into Yoast replacevars. |
| 71 |
* |
| 72 |
* @param string $aioseo_replacevar The AIOSEO replacevar. |
| 73 |
* |
| 74 |
* @return string The Yoast replacevar. |
| 75 |
*/ |
| 76 |
public function transform( $aioseo_replacevar ) { |
| 77 |
$yoast_replacevar = \str_replace( \array_keys( $this->replace_vars_map ), \array_values( $this->replace_vars_map ), $aioseo_replacevar ); |
| 78 |
|
| 79 |
// Transform the '#custom_field-<custom_field>' tags into '%%cf_<custom_field>%%' ones. |
| 80 |
$yoast_replacevar = preg_replace_callback( |
| 81 |
'/#custom_field-([a-zA-Z0-9_-]+)/', |
| 82 |
function ( $cf_matches ) { |
| 83 |
return '%%cf_' . $cf_matches[1] . '%%'; |
| 84 |
}, |
| 85 |
$yoast_replacevar |
| 86 |
); |
| 87 |
|
| 88 |
// Transform the '#tax_name-<custom-tax-name>' tags into '%%ct_<custom-tax-name>%%' ones. |
| 89 |
$yoast_replacevar = preg_replace_callback( |
| 90 |
'/#tax_name-([a-zA-Z0-9_-]+)/', |
| 91 |
function ( $ct_matches ) { |
| 92 |
return '%%ct_' . $ct_matches[1] . '%%'; |
| 93 |
}, |
| 94 |
$yoast_replacevar |
| 95 |
); |
| 96 |
|
| 97 |
return $yoast_replacevar; |
| 98 |
} |
| 99 |
} |
| 100 |
|