| 1 |
<?php |
| 2 |
|
| 3 |
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure. |
| 4 |
namespace Yoast\WP\SEO\Bulk_Editor\Application\Updates; |
| 5 |
|
| 6 |
use Exception; |
| 7 |
use Yoast\WP\SEO\Bulk_Editor\Domain\Updates\Post_Score_Update; |
| 8 |
use Yoast\WP\SEO\Bulk_Editor\Domain\Updates\Update_Error; |
| 9 |
use Yoast\WP\SEO\Bulk_Editor\Domain\Updates\Update_Result; |
| 10 |
use Yoast\WP\SEO\Bulk_Editor\Domain\Updates\Update_Result_Collection; |
| 11 |
use YoastSEO_Vendor\Psr\Log\LoggerAwareInterface; |
| 12 |
use YoastSEO_Vendor\Psr\Log\LoggerAwareTrait; |
| 13 |
use YoastSEO_Vendor\Psr\Log\NullLogger; |
| 14 |
|
| 15 |
/** |
| 16 |
* Persists the per-field scores the bulk editor computes after a save, independently per post. |
| 17 |
*/ |
| 18 |
class Score_Updater implements LoggerAwareInterface { |
| 19 |
|
| 20 |
use LoggerAwareTrait; |
| 21 |
|
| 22 |
/** |
| 23 |
* The meta key (without prefix) the SEO title score is stored under. |
| 24 |
* |
| 25 |
* @var string |
| 26 |
*/ |
| 27 |
public const SEO_TITLE_SCORE_KEY = 'seo_title_score'; |
| 28 |
|
| 29 |
/** |
| 30 |
* The meta key (without prefix) the meta description score is stored under. |
| 31 |
* |
| 32 |
* @var string |
| 33 |
*/ |
| 34 |
public const META_DESCRIPTION_SCORE_KEY = 'meta_description_score'; |
| 35 |
|
| 36 |
/** |
| 37 |
* The post access checker. |
| 38 |
* |
| 39 |
* @var Post_Access_Checker_Interface |
| 40 |
*/ |
| 41 |
private $post_access_checker; |
| 42 |
|
| 43 |
/** |
| 44 |
* The meta writer. |
| 45 |
* |
| 46 |
* @var Meta_Writer_Interface |
| 47 |
*/ |
| 48 |
private $meta_writer; |
| 49 |
|
| 50 |
/** |
| 51 |
* The constructor. |
| 52 |
* |
| 53 |
* @param Post_Access_Checker_Interface $post_access_checker The post access checker. |
| 54 |
* @param Meta_Writer_Interface $meta_writer The meta writer. |
| 55 |
*/ |
| 56 |
public function __construct( Post_Access_Checker_Interface $post_access_checker, Meta_Writer_Interface $meta_writer ) { |
| 57 |
$this->post_access_checker = $post_access_checker; |
| 58 |
$this->meta_writer = $meta_writer; |
| 59 |
$this->logger = new NullLogger(); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Applies the given score updates. Updates are applied independently: one failing update does |
| 64 |
* not block the others. |
| 65 |
* |
| 66 |
* @param array<Post_Score_Update> $updates The score updates to apply. |
| 67 |
* |
| 68 |
* @return Update_Result_Collection The result per score update. |
| 69 |
*/ |
| 70 |
public function update( array $updates ): Update_Result_Collection { |
| 71 |
$results = new Update_Result_Collection(); |
| 72 |
|
| 73 |
foreach ( $updates as $update ) { |
| 74 |
$results->add( $this->apply( $update ) ); |
| 75 |
} |
| 76 |
|
| 77 |
return $results; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Applies a single score update. |
| 82 |
* |
| 83 |
* @param Post_Score_Update $update The score update to apply. |
| 84 |
* |
| 85 |
* @return Update_Result The result of the update. |
| 86 |
*/ |
| 87 |
private function apply( Post_Score_Update $update ): Update_Result { |
| 88 |
$post_id = $update->get_post_id(); |
| 89 |
|
| 90 |
if ( ! $this->post_access_checker->exists( $post_id ) ) { |
| 91 |
return Update_Result::for_failure( $post_id, Update_Error::NOT_FOUND ); |
| 92 |
} |
| 93 |
|
| 94 |
if ( ! $this->post_access_checker->is_supported_type( $post_id ) ) { |
| 95 |
return Update_Result::for_failure( $post_id, Update_Error::INVALID_POST_TYPE ); |
| 96 |
} |
| 97 |
|
| 98 |
if ( ! $this->post_access_checker->can_edit( $post_id ) ) { |
| 99 |
return Update_Result::for_failure( $post_id, Update_Error::FORBIDDEN ); |
| 100 |
} |
| 101 |
|
| 102 |
try { |
| 103 |
if ( $update->has_seo_title_score() ) { |
| 104 |
$this->meta_writer->write_score( $post_id, self::SEO_TITLE_SCORE_KEY, $update->get_seo_title_score() ); |
| 105 |
} |
| 106 |
|
| 107 |
if ( $update->has_meta_description_score() ) { |
| 108 |
$this->meta_writer->write_score( $post_id, self::META_DESCRIPTION_SCORE_KEY, $update->get_meta_description_score() ); |
| 109 |
} |
| 110 |
} catch ( Exception $exception ) { |
| 111 |
$this->logger->warning( |
| 112 |
'Bulk score update failed to save post {post_id}: {error}', |
| 113 |
[ |
| 114 |
'post_id' => $post_id, |
| 115 |
'error' => $exception->getMessage(), |
| 116 |
], |
| 117 |
); |
| 118 |
|
| 119 |
return Update_Result::for_failure( $post_id, Update_Error::SAVE_FAILED ); |
| 120 |
} |
| 121 |
|
| 122 |
return Update_Result::for_success( $post_id ); |
| 123 |
} |
| 124 |
} |
| 125 |
|