PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.4
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.4
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 / helpers / indexable-to-postmeta-helper.php

indexable-to-postmeta-helper.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 18.4, at src/helpers/indexable-to-postmeta-helper.php

234 lines 6.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Yoast\WP\SEO\Helpers;
4
5 use Yoast\WP\SEO\Models\Indexable;
6
7 /**
8 * A helper object to map indexable data to postmeta.
9 * @phpcs:disable Yoast.NamingConventions.ObjectNameDepth.MaxExceeded
10 */
11 class Indexable_To_Postmeta_Helper {
12
13 /**
14 * The Meta helper.
15 *
16 * @var Meta_Helper
17 */
18 public $meta;
19
20 /**
21 * The map of yoast to post meta.
22 *
23 * @var array
24 */
25 protected $yoast_to_postmeta = [
26 'title' => [
27 'post_meta_key' => 'title',
28 'map_method' => 'simple_map',
29 ],
30 'description' => [
31 'post_meta_key' => 'metadesc',
32 'map_method' => 'simple_map',
33 ],
34 'open_graph_title' => [
35 'post_meta_key' => 'opengraph-title',
36 'map_method' => 'simple_map',
37 ],
38 'open_graph_description' => [
39 'post_meta_key' => 'opengraph-description',
40 'map_method' => 'simple_map',
41 ],
42 'twitter_title' => [
43 'post_meta_key' => 'twitter-title',
44 'map_method' => 'simple_map',
45 ],
46 'twitter_description' => [
47 'post_meta_key' => 'twitter-description',
48 'map_method' => 'simple_map',
49 ],
50 'canonical' => [
51 'post_meta_key' => 'canonical',
52 'map_method' => 'simple_map',
53 ],
54 'primary_focus_keyword' => [
55 'post_meta_key' => 'focuskw',
56 'map_method' => 'simple_map',
57 ],
58 'open_graph_image' => [
59 'post_meta_key' => 'opengraph-image',
60 'map_method' => 'social_image_map',
61 ],
62 'open_graph_image_id' => [
63 'post_meta_key' => 'opengraph-image-id',
64 'map_method' => 'social_image_map',
65 ],
66 'twitter_image' => [
67 'post_meta_key' => 'twitter-image',
68 'map_method' => 'social_image_map',
69 ],
70 'twitter_image_id' => [
71 'post_meta_key' => 'twitter-image-id',
72 'map_method' => 'social_image_map',
73 ],
74 'is_robots_noindex' => [
75 'post_meta_key' => 'meta-robots-noindex',
76 'map_method' => 'noindex_map',
77 ],
78 'is_robots_nofollow' => [
79 'post_meta_key' => 'meta-robots-nofollow',
80 'map_method' => 'nofollow_map',
81 ],
82 'meta_robots_adv' => [
83 'post_meta_key' => 'meta-robots-adv',
84 'map_method' => 'robots_adv_map',
85 ],
86 ];
87
88 /**
89 * Indexable_To_Postmeta_Helper constructor.
90 *
91 * @param Meta_Helper $meta The Meta helper.
92 */
93 public function __construct( Meta_Helper $meta ) {
94 $this->meta = $meta;
95 }
96
97 /**
98 * Creates postmeta from a Yoast indexable.
99 *
100 * @param Indexable $indexable The Yoast indexable.
101 *
102 * @return void.
103 */
104 public function map_to_postmeta( $indexable ) {
105 foreach ( $this->yoast_to_postmeta as $indexable_column => $map_info ) {
106 \call_user_func( [ $this, $map_info['map_method'] ], $indexable, $map_info['post_meta_key'], $indexable_column );
107 }
108 }
109
110 /**
111 * Uses a simple set_value for non-empty data.
112 *
113 * @param Indexable $indexable The Yoast indexable.
114 * @param string $post_meta_key The post_meta key that will be populated.
115 * @param string $indexable_column The indexable data that will be mapped to post_meta.
116 *
117 * @return void.
118 */
119 public function simple_map( $indexable, $post_meta_key, $indexable_column ) {
120 if ( empty( $indexable->{$indexable_column} ) ) {
121 return;
122 }
123
124 $this->meta->set_value( $post_meta_key, $indexable->{$indexable_column}, $indexable->object_id );
125 }
126
127 /**
128 * Map social image data only if social image is explicitly set.
129 *
130 * @param Indexable $indexable The Yoast indexable.
131 * @param string $post_meta_key The post_meta key that will be populated.
132 * @param string $indexable_column The indexable data that will be mapped to post_meta.
133 *
134 * @return void.
135 */
136 public function social_image_map( $indexable, $post_meta_key, $indexable_column ) {
137 if ( empty( $indexable->{$indexable_column} ) ) {
138 return;
139 }
140
141 switch ( $indexable_column ) {
142 case 'open_graph_image':
143 case 'open_graph_image_id':
144 $source = $indexable->open_graph_image_source;
145 break;
146 case 'twitter_image':
147 case 'twitter_image_id':
148 $source = $indexable->twitter_image_source;
149 break;
150 }
151
152 // Map the social image data only when the social image is explicitly set.
153 if ( $source === 'set-by-user' || $source === 'imported' ) {
154 $value = (string) $indexable->{$indexable_column};
155
156 $this->meta->set_value( $post_meta_key, $value, $indexable->object_id );
157 }
158 }
159
160 /**
161 * Deletes the noindex post_meta key if no noindex in the indexable. Populates the post_meta key appropriately if there is noindex in the indexable.
162 *
163 * @param Indexable $indexable The Yoast indexable.
164 * @param string $post_meta_key The post_meta key that will be populated.
165 *
166 * @return void.
167 */
168 public function noindex_map( $indexable, $post_meta_key ) {
169 if ( \is_null( $indexable->is_robots_noindex ) ) {
170 $this->meta->delete( $post_meta_key, $indexable->object_id );
171 return;
172 }
173
174 if ( $indexable->is_robots_noindex === false ) {
175 $this->meta->set_value( $post_meta_key, 2, $indexable->object_id );
176 }
177
178 if ( $indexable->is_robots_noindex === true ) {
179 $this->meta->set_value( $post_meta_key, 1, $indexable->object_id );
180 }
181 }
182
183 /**
184 * Deletes the nofollow post_meta key if no nofollow in the indexable or if nofollow is false. Populates the post_meta key appropriately if there is a true nofollow in the indexable.
185 *
186 * @param Indexable $indexable The Yoast indexable.
187 * @param string $post_meta_key The post_meta key that will be populated.
188 *
189 * @return void.
190 */
191 public function nofollow_map( $indexable, $post_meta_key ) {
192 if ( \is_null( $indexable->is_robots_nofollow ) || $indexable->is_robots_nofollow === false ) {
193 $this->meta->delete( $post_meta_key, $indexable->object_id );
194 }
195
196 if ( $indexable->is_robots_nofollow === true ) {
197 $this->meta->set_value( $post_meta_key, 1, $indexable->object_id );
198 }
199 }
200
201 /**
202 * Deletes the nofollow post_meta key if no nofollow in the indexable or if nofollow is false. Populates the post_meta key appropriately if there is a true nofollow in the indexable.
203 *
204 * @param Indexable $indexable The Yoast indexable.
205 * @param string $post_meta_key The post_meta key that will be populated.
206 *
207 * @return void.
208 */
209 public function robots_adv_map( $indexable, $post_meta_key ) {
210 $adv_settings_to_be_imported = [];
211 $no_adv_settings = true;
212
213 if ( $indexable->is_robots_noimageindex === true ) {
214 $adv_settings_to_be_imported[] = 'noimageindex';
215 $no_adv_settings = false;
216 }
217 if ( $indexable->is_robots_noarchive === true ) {
218 $adv_settings_to_be_imported[] = 'noarchive';
219 $no_adv_settings = false;
220 }
221 if ( $indexable->is_robots_nosnippet === true ) {
222 $adv_settings_to_be_imported[] = 'nosnippet';
223 $no_adv_settings = false;
224 }
225
226 if ( $no_adv_settings === true ) {
227 $this->meta->delete( $post_meta_key, $indexable->object_id );
228 return;
229 }
230
231 $this->meta->set_value( $post_meta_key, \implode( ',', $adv_settings_to_be_imported ), $indexable->object_id );
232 }
233 }
234