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