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 / integrations / watchers / indexable-author-watcher.php

indexable-author-watcher.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI trunk, at src/integrations/watchers/indexable-author-watcher.php

142 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Yoast\WP\SEO\Integrations\Watchers;
4
5 use Yoast\WP\SEO\Builders\Indexable_Builder;
6 use Yoast\WP\SEO\Conditionals\Migrations_Conditional;
7 use Yoast\WP\SEO\Helpers\Indexable_Helper;
8 use Yoast\WP\SEO\Integrations\Integration_Interface;
9 use Yoast\WP\SEO\Repositories\Indexable_Repository;
10
11 /**
12 * Watches an Author to save the meta information to an Indexable when updated.
13 */
14 class Indexable_Author_Watcher implements Integration_Interface {
15
16 /**
17 * The indexable repository.
18 *
19 * @var Indexable_Repository
20 */
21 protected $repository;
22
23 /**
24 * The indexable builder.
25 *
26 * @var Indexable_Builder
27 */
28 protected $builder;
29
30 /**
31 * The indexable helper.
32 *
33 * @var Indexable_Helper
34 */
35 private $indexable_helper;
36
37 /**
38 * Returns the conditionals based on which this loadable should be active.
39 *
40 * @return array
41 */
42 public static function get_conditionals() {
43 return [ Migrations_Conditional::class ];
44 }
45
46 /**
47 * Indexable_Author_Watcher constructor.
48 *
49 * @param Indexable_Repository $repository The repository to use.
50 * @param Indexable_Helper $indexable_helper The indexable helper.
51 * @param Indexable_Builder $builder The builder to use.
52 */
53 public function __construct(
54 Indexable_Repository $repository,
55 Indexable_Helper $indexable_helper,
56 Indexable_Builder $builder
57 ) {
58 $this->repository = $repository;
59 $this->indexable_helper = $indexable_helper;
60 $this->builder = $builder;
61 }
62
63 /**
64 * Initializes the integration.
65 *
66 * This is the place to register hooks and filters.
67 *
68 * @return void
69 */
70 public function register_hooks() {
71 \add_action( 'user_register', [ $this, 'build_indexable' ], \PHP_INT_MAX );
72 \add_action( 'profile_update', [ $this, 'build_indexable' ], \PHP_INT_MAX );
73 \add_action( 'deleted_user', [ $this, 'handle_user_delete' ], 10, 2 );
74 }
75
76 /**
77 * Deletes user meta.
78 *
79 * @param int $user_id User ID to delete the metadata of.
80 *
81 * @return void
82 */
83 public function delete_indexable( $user_id ) {
84 $indexable = $this->repository->find_by_id_and_type( $user_id, 'user', false );
85
86 if ( ! $indexable ) {
87 return;
88 }
89
90 $indexable->delete();
91 \do_action( 'wpseo_indexable_deleted', $indexable );
92 }
93
94 /**
95 * Saves user meta.
96 *
97 * @param int $user_id User ID.
98 *
99 * @return void
100 */
101 public function build_indexable( $user_id ) {
102 $indexable = $this->repository->find_by_id_and_type( $user_id, 'user', false );
103 $indexable = $this->builder->build_for_id_and_type( $user_id, 'user', $indexable );
104
105 if ( $indexable ) {
106 $indexable->object_last_modified = \max( $indexable->object_last_modified, \current_time( 'mysql' ) );
107 $this->indexable_helper->save_indexable( $indexable );
108 }
109 }
110
111 /**
112 * Handles the case in which an author is deleted.
113 *
114 * @param int $user_id User ID.
115 * @param int|null $new_user_id The ID of the user the old author's posts are reassigned to.
116 *
117 * @return void
118 */
119 public function handle_user_delete( $user_id, $new_user_id = null ) {
120 if ( $new_user_id !== null ) {
121 $this->maybe_reassign_user_indexables( $user_id, $new_user_id );
122 }
123
124 $this->delete_indexable( $user_id );
125 }
126
127 /**
128 * Reassigns the indexables of a user to another user.
129 *
130 * @param int $user_id The user ID.
131 * @param int $new_user_id The user ID to reassign the indexables to.
132 *
133 * @return void
134 */
135 public function maybe_reassign_user_indexables( $user_id, $new_user_id ) {
136 $this->repository->query()
137 ->set( 'author_id', $new_user_id )
138 ->where( 'author_id', $user_id )
139 ->update_many();
140 }
141 }
142