| 1 |
<?php |
| 2 |
|
| 3 |
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure. |
| 4 |
namespace Yoast\WP\SEO\Bulk_Editor\Infrastructure\Updates; |
| 5 |
|
| 6 |
use WPSEO_Meta; |
| 7 |
use WPSEO_Utils; |
| 8 |
use Yoast\WP\SEO\Bulk_Editor\Application\Updates\Meta_Writer_Interface; |
| 9 |
use Yoast\WP\SEO\Bulk_Editor\Domain\Updates\Update_Type; |
| 10 |
use Yoast\WP\SEO\Helpers\Meta_Helper; |
| 11 |
|
| 12 |
/** |
| 13 |
* Persists a title, description and focus keyphrase to Yoast post meta. |
| 14 |
* |
| 15 |
* Writing through the meta helper triggers the post meta watcher, so the |
| 16 |
* indexable of the post is rebuilt through the normal flow. |
| 17 |
*/ |
| 18 |
class Meta_Writer implements Meta_Writer_Interface { |
| 19 |
|
| 20 |
/** |
| 21 |
* The meta helper. |
| 22 |
* |
| 23 |
* @var Meta_Helper |
| 24 |
*/ |
| 25 |
private $meta_helper; |
| 26 |
|
| 27 |
/** |
| 28 |
* The constructor. |
| 29 |
* |
| 30 |
* @param Meta_Helper $meta_helper The meta helper. |
| 31 |
*/ |
| 32 |
public function __construct( Meta_Helper $meta_helper ) { |
| 33 |
$this->meta_helper = $meta_helper; |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Writes the title for a post. |
| 38 |
* |
| 39 |
* @param Update_Type $type The appearance the title belongs to. |
| 40 |
* @param int $post_id The ID of the post. |
| 41 |
* @param string $title The title to write. |
| 42 |
* |
| 43 |
* @return void |
| 44 |
*/ |
| 45 |
public function write_title( Update_Type $type, int $post_id, string $title ): void { |
| 46 |
$key = $type->is_social() ? 'opengraph-title' : 'title'; |
| 47 |
$this->write( $key, $post_id, $title ); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Writes the description for a post. |
| 52 |
* |
| 53 |
* @param Update_Type $type The appearance the description belongs to. |
| 54 |
* @param int $post_id The ID of the post. |
| 55 |
* @param string $description The description to write. |
| 56 |
* |
| 57 |
* @return void |
| 58 |
*/ |
| 59 |
public function write_description( Update_Type $type, int $post_id, string $description ): void { |
| 60 |
$key = $type->is_social() ? 'opengraph-description' : 'metadesc'; |
| 61 |
$this->write( $key, $post_id, $description ); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Writes the focus keyphrase for a post. The focus keyphrase is channel-agnostic, |
| 66 |
* so it does not depend on the update type. |
| 67 |
* |
| 68 |
* @param int $post_id The ID of the post. |
| 69 |
* @param string $focus_keyphrase The focus keyphrase to write. |
| 70 |
* |
| 71 |
* @return void |
| 72 |
*/ |
| 73 |
public function write_focus_keyphrase( int $post_id, string $focus_keyphrase ): void { |
| 74 |
$this->write( 'focuskw', $post_id, $focus_keyphrase ); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Sanitizes and persists a value under the given meta key. |
| 79 |
* |
| 80 |
* @param string $key The meta key (without prefix) to store the value under. |
| 81 |
* @param int $post_id The ID of the post. |
| 82 |
* @param string $value The value to write. |
| 83 |
* |
| 84 |
* @return void |
| 85 |
*/ |
| 86 |
private function write( string $key, int $post_id, string $value ): void { |
| 87 |
$this->meta_helper->set_value( $key, $this->sanitize( $key, $value ), $post_id ); |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Sanitizes a value the same way the post editor sanitizes meta values. |
| 92 |
* |
| 93 |
* Registered fields are routed through the canonical meta sanitizer so the |
| 94 |
* field-specific handling and the `wpseo_sanitize_post_meta_*` filter run, matching |
| 95 |
* a normal post save. Fields that are not registered (for example the Open Graph |
| 96 |
* fields when social appearance is disabled) have no canonical sanitize callback, so |
| 97 |
* they fall back to plain text sanitization. |
| 98 |
* |
| 99 |
* @param string $key The meta key (without prefix) the value is stored under. |
| 100 |
* @param string $value The value to sanitize. |
| 101 |
* |
| 102 |
* @return string The sanitized value. |
| 103 |
*/ |
| 104 |
private function sanitize( string $key, string $value ): string { |
| 105 |
$meta_key = WPSEO_Meta::$meta_prefix . $key; |
| 106 |
|
| 107 |
if ( isset( WPSEO_Meta::$fields_index[ $meta_key ] ) ) { |
| 108 |
return WPSEO_Meta::sanitize_post_meta( $value, $meta_key ); |
| 109 |
} |
| 110 |
|
| 111 |
return WPSEO_Utils::sanitize_text_field( \trim( $value ) ); |
| 112 |
} |
| 113 |
} |
| 114 |
|