PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 28.2
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v28.2
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 / bulk-updater.php

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

119 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_Update;
8 use Yoast\WP\SEO\Bulk_Editor\Domain\Updates\Post_Update_Collection;
9 use Yoast\WP\SEO\Bulk_Editor\Domain\Updates\Update_Error;
10 use Yoast\WP\SEO\Bulk_Editor\Domain\Updates\Update_Result;
11 use Yoast\WP\SEO\Bulk_Editor\Domain\Updates\Update_Result_Collection;
12 use Yoast\WP\SEO\Bulk_Editor\Domain\Updates\Update_Type;
13 use YoastSEO_Vendor\Psr\Log\LoggerAwareInterface;
14 use YoastSEO_Vendor\Psr\Log\LoggerAwareTrait;
15 use YoastSEO_Vendor\Psr\Log\NullLogger;
16
17 /**
18 * Applies a batch of post updates, independently per post.
19 */
20 class Bulk_Updater implements LoggerAwareInterface {
21
22 use LoggerAwareTrait;
23
24 /**
25 * The post access checker.
26 *
27 * @var Post_Access_Checker_Interface
28 */
29 private $post_access_checker;
30
31 /**
32 * The meta writer.
33 *
34 * @var Meta_Writer_Interface
35 */
36 private $meta_writer;
37
38 /**
39 * The constructor.
40 *
41 * @param Post_Access_Checker_Interface $post_access_checker The post access checker.
42 * @param Meta_Writer_Interface $meta_writer The meta writer.
43 */
44 public function __construct( Post_Access_Checker_Interface $post_access_checker, Meta_Writer_Interface $meta_writer ) {
45 $this->post_access_checker = $post_access_checker;
46 $this->meta_writer = $meta_writer;
47 $this->logger = new NullLogger();
48 }
49
50 /**
51 * Applies the given post updates. Updates are applied independently: one failing
52 * update does not block the others.
53 *
54 * @param Update_Type $type The appearance the updates target.
55 * @param Post_Update_Collection $updates The post updates to apply.
56 *
57 * @return Update_Result_Collection The result per post update.
58 */
59 public function update( Update_Type $type, Post_Update_Collection $updates ): Update_Result_Collection {
60 $results = new Update_Result_Collection();
61
62 foreach ( $updates->get() as $update ) {
63 $results->add( $this->apply( $type, $update ) );
64 }
65
66 return $results;
67 }
68
69 /**
70 * Applies a single post update.
71 *
72 * @param Update_Type $type The appearance the update targets.
73 * @param Post_Update $update The post update to apply.
74 *
75 * @return Update_Result The result of the update.
76 */
77 private function apply( Update_Type $type, Post_Update $update ): Update_Result {
78 $post_id = $update->get_post_id();
79
80 if ( ! $this->post_access_checker->exists( $post_id ) ) {
81 return Update_Result::for_failure( $post_id, Update_Error::NOT_FOUND );
82 }
83
84 if ( ! $this->post_access_checker->is_supported_type( $post_id ) ) {
85 return Update_Result::for_failure( $post_id, Update_Error::INVALID_POST_TYPE );
86 }
87
88 if ( ! $this->post_access_checker->can_edit( $post_id ) ) {
89 return Update_Result::for_failure( $post_id, Update_Error::FORBIDDEN );
90 }
91
92 try {
93 if ( $update->has_title() ) {
94 $this->meta_writer->write_title( $type, $post_id, $update->get_title() );
95 }
96
97 if ( $update->has_description() ) {
98 $this->meta_writer->write_description( $type, $post_id, $update->get_description() );
99 }
100
101 if ( $update->has_focus_keyphrase() ) {
102 $this->meta_writer->write_focus_keyphrase( $post_id, $update->get_focus_keyphrase() );
103 }
104 } catch ( Exception $exception ) {
105 $this->logger->warning(
106 'Bulk update failed to save post {post_id}: {error}',
107 [
108 'post_id' => $post_id,
109 'error' => $exception->getMessage(),
110 ],
111 );
112
113 return Update_Result::for_failure( $post_id, Update_Error::SAVE_FAILED );
114 }
115
116 return Update_Result::for_success( $post_id );
117 }
118 }
119