| @@ -81,8 +81,20 @@ | ||
| 81 | 81 | 'meta_robots_adv' => [ |
| 82 | 82 | 'post_meta_key' => 'meta-robots-adv', |
| 83 | 83 | 'map_method' => 'robots_adv_map', |
| 84 | 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 | + ], | |
| 85 | 97 | ]; |
| 86 | 98 | |
| 87 | 99 | /** |
| 88 | 100 | * Indexable_To_Postmeta_Helper constructor. |
| @@ -95,33 +107,87 @@ | ||
| 95 | 107 | |
| 96 | 108 | /** |
| 97 | 109 | * Creates postmeta from a Yoast indexable. |
| 98 | 110 | * |
| 99 | - * @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. | |
| 100 | 115 | * |
| 101 | 116 | * @return void |
| 102 | 117 | */ |
| 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 ); | |
| 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 ); | |
| 106 | 121 | } |
| 107 | 122 | } |
| 108 | 123 | |
| 109 | 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 | + /** | |
| 110 | 148 | * Uses a simple set_value for non-empty data. |
| 111 | 149 | * |
| 112 | 150 | * @param Indexable $indexable The Yoast indexable. |
| 113 | 151 | * @param string $post_meta_key The post_meta key that will be populated. |
| 114 | 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. | |
| 115 | 154 | * |
| 116 | 155 | * @return void |
| 117 | 156 | */ |
| 118 | - public function simple_map( $indexable, $post_meta_key, $indexable_column ) { | |
| 119 | - 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 | + } | |
| 120 | 165 | return; |
| 121 | 166 | } |
| 122 | 167 | |
| 123 | - $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 ); | |
| 169 | + } | |
| 170 | + | |
| 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 | + } | |
| 124 | 190 | } |
| 125 | 191 | |
| 126 | 192 | /** |
| 127 | 193 | * Map social image data only if social image is explicitly set. |