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
← All changes | src/helpers/indexable-to-postmeta-helper.php +87 -22 18.3 → trunk View file →
@@ -5,9 +5,8 @@
5 5 use Yoast\WP\SEO\Models\Indexable;
6 6
7 7 /**
8 8 * A helper object to map indexable data to postmeta.
9 - * @phpcs:disable Yoast.NamingConventions.ObjectNameDepth.MaxExceeded
10 9 */
11 10 class Indexable_To_Postmeta_Helper {
12 11
13 12 /**
@@ -82,8 +81,20 @@
82 81 'meta_robots_adv' => [
83 82 'post_meta_key' => 'meta-robots-adv',
84 83 'map_method' => 'robots_adv_map',
85 84 ],
85 + 'is_cornerstone' => [
86 + 'post_meta_key' => 'is_cornerstone',
87 + 'map_method' => 'cornerstone_map',
88 + ],
89 + 'schema_page_type' => [
90 + 'post_meta_key' => 'schema_page_type',
91 + 'map_method' => 'simple_map',
92 + ],
93 + 'schema_article_type' => [
94 + 'post_meta_key' => 'schema_article_type',
95 + 'map_method' => 'simple_map',
96 + ],
86 97 ];
87 98
88 99 /**
89 100 * Indexable_To_Postmeta_Helper constructor.
@@ -96,36 +107,90 @@
96 107
97 108 /**
98 109 * Creates postmeta from a Yoast indexable.
99 110 *
100 - * @param Indexable $indexable The Yoast indexable.
111 + * @param Indexable $indexable The Yoast indexable.
112 + * @param bool $delete_empty Whether an empty indexable value should delete the post meta
113 + * instead of being left untouched. Defaults to false so importers,
114 + * which only fill in missing data, keep their set-only behaviour.
101 115 *
102 - * @return void.
116 + * @return void
103 117 */
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 );
118 + public function map_to_postmeta( $indexable, $delete_empty = false ) {
119 + foreach ( \array_keys( $this->yoast_to_postmeta ) as $indexable_column ) {
120 + $this->map_column_to_postmeta( $indexable, $indexable_column, $delete_empty );
107 121 }
108 122 }
109 123
110 124 /**
125 + * Creates postmeta from a single indexable column.
126 + *
127 + * @param Indexable $indexable The Yoast indexable.
128 + * @param string $indexable_column The indexable column to map.
129 + * @param bool $delete_empty Whether an empty value should delete the post meta key.
130 + *
131 + * @return void
132 + */
133 + public function map_column_to_postmeta( $indexable, $indexable_column, $delete_empty = false ) {
134 + // The advanced-robots flags are persisted together under one meta key, so they cascade through it.
135 + if ( \in_array( $indexable_column, [ 'is_robots_noimageindex', 'is_robots_noarchive', 'is_robots_nosnippet' ], true ) ) {
136 + $indexable_column = 'meta_robots_adv';
137 + }
138 +
139 + if ( ! isset( $this->yoast_to_postmeta[ $indexable_column ] ) ) {
140 + return;
141 + }
142 +
143 + $map_info = $this->yoast_to_postmeta[ $indexable_column ];
144 + \call_user_func( [ $this, $map_info['map_method'] ], $indexable, $map_info['post_meta_key'], $indexable_column, $delete_empty );
145 + }
146 +
147 + /**
111 148 * Uses a simple set_value for non-empty data.
112 149 *
113 150 * @param Indexable $indexable The Yoast indexable.
114 151 * @param string $post_meta_key The post_meta key that will be populated.
115 152 * @param string $indexable_column The indexable data that will be mapped to post_meta.
153 + * @param bool $delete_empty Whether an empty value should delete the post meta key.
116 154 *
117 - * @return void.
155 + * @return void
118 156 */
119 - public function simple_map( $indexable, $post_meta_key, $indexable_column ) {
120 - if ( empty( $indexable->{$indexable_column} ) ) {
157 + public function simple_map( $indexable, $post_meta_key, $indexable_column, $delete_empty = false ) {
158 + $value = $indexable->{$indexable_column};
159 +
160 + // Strict emptiness check because empty() would treat the literal string '0' as empty and drop it.
161 + if ( $value === null || $value === '' ) {
162 + if ( $delete_empty ) {
163 + $this->meta->delete( $post_meta_key, $indexable->object_id );
164 + }
121 165 return;
122 166 }
123 167
124 - $this->meta->set_value( $post_meta_key, $indexable->{$indexable_column}, $indexable->object_id );
168 + $this->meta->set_value( $post_meta_key, $value, $indexable->object_id );
125 169 }
126 170
127 171 /**
172 + * Maps the cornerstone flag, which is stored as '1' when on and absent when off.
173 + *
174 + * @param Indexable $indexable The Yoast indexable.
175 + * @param string $post_meta_key The post_meta key that will be populated.
176 + * @param string $indexable_column The indexable data that will be mapped to post_meta.
177 + * @param bool $delete_empty Whether a disabled flag should delete the post meta key.
178 + *
179 + * @return void
180 + */
181 + public function cornerstone_map( $indexable, $post_meta_key, $indexable_column, $delete_empty = false ) {
182 + if ( $indexable->is_cornerstone === true ) {
183 + $this->meta->set_value( $post_meta_key, '1', $indexable->object_id );
184 + return;
185 + }
186 +
187 + if ( $delete_empty ) {
188 + $this->meta->delete( $post_meta_key, $indexable->object_id );
189 + }
190 + }
191 +
192 + /**
128 193 * Map social image data only if social image is explicitly set.
129 194 *
130 195 * @param Indexable $indexable The Yoast indexable.
131 196 * @param string $post_meta_key The post_meta key that will be populated.
@@ -130,9 +195,9 @@
130 195 * @param Indexable $indexable The Yoast indexable.
131 196 * @param string $post_meta_key The post_meta key that will be populated.
132 197 * @param string $indexable_column The indexable data that will be mapped to post_meta.
133 198 *
134 - * @return void.
199 + * @return void
135 200 */
136 201 public function social_image_map( $indexable, $post_meta_key, $indexable_column ) {
137 202 if ( empty( $indexable->{$indexable_column} ) ) {
138 203 return;
@@ -159,15 +224,15 @@
159 224
160 225 /**
161 226 * 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 227 *
163 - * @param Indexable $indexable The Yoast indexable.
164 - * @param string $post_meta_key The post_meta key that will be populated.
228 + * @param Indexable $indexable The Yoast indexable.
229 + * @param string $post_meta_key The post_meta key that will be populated.
165 230 *
166 - * @return void.
231 + * @return void
167 232 */
168 233 public function noindex_map( $indexable, $post_meta_key ) {
169 - if ( \is_null( $indexable->is_robots_noindex ) ) {
234 + if ( $indexable->is_robots_noindex === null ) {
170 235 $this->meta->delete( $post_meta_key, $indexable->object_id );
171 236 return;
172 237 }
173 238
@@ -182,15 +247,15 @@
182 247
183 248 /**
184 249 * 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 250 *
186 - * @param Indexable $indexable The Yoast indexable.
187 - * @param string $post_meta_key The post_meta key that will be populated.
251 + * @param Indexable $indexable The Yoast indexable.
252 + * @param string $post_meta_key The post_meta key that will be populated.
188 253 *
189 - * @return void.
254 + * @return void
190 255 */
191 256 public function nofollow_map( $indexable, $post_meta_key ) {
192 - if ( \is_null( $indexable->is_robots_nofollow ) || $indexable->is_robots_nofollow === false ) {
257 + if ( $indexable->is_robots_nofollow === null || $indexable->is_robots_nofollow === false ) {
193 258 $this->meta->delete( $post_meta_key, $indexable->object_id );
194 259 }
195 260
196 261 if ( $indexable->is_robots_nofollow === true ) {
@@ -200,12 +265,12 @@
200 265
201 266 /**
202 267 * 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 268 *
204 - * @param Indexable $indexable The Yoast indexable.
205 - * @param string $post_meta_key The post_meta key that will be populated.
269 + * @param Indexable $indexable The Yoast indexable.
270 + * @param string $post_meta_key The post_meta key that will be populated.
206 271 *
207 - * @return void.
272 + * @return void
208 273 */
209 274 public function robots_adv_map( $indexable, $post_meta_key ) {
210 275 $adv_settings_to_be_imported = [];
211 276 $no_adv_settings = true;