PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 27.5
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v27.5
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 27.5, at src/helpers/indexable-to-postmeta-helper.php

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