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
wordpress-seo / src / bulk-editor / application / updates / score-updater.php

score-updater.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI trunk, at src/bulk-editor/application/updates/score-updater.php

125 lines 3.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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