PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / trunk
Yoast SEO – Advanced SEO with real-time guidance and built-in AI vtrunk
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 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 All 129 releases
wordpress-seo / src / services / importing / aioseo / aioseo-replacevar-service.php

aioseo-replacevar-service.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI trunk, at src/services/importing/aioseo/aioseo-replacevar-service.php

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