← All changes
|
src/integrations/watchers/indexable-author-watcher.php
+55
-7
18.1
→
trunk
View file →
| @@ -3,8 +3,9 @@ | ||
| 3 | 3 | namespace Yoast\WP\SEO\Integrations\Watchers; |
| 4 | 4 | |
| 5 | 5 | use Yoast\WP\SEO\Builders\Indexable_Builder; |
| 6 | 6 | use Yoast\WP\SEO\Conditionals\Migrations_Conditional; |
| 7 | +use Yoast\WP\SEO\Helpers\Indexable_Helper; | |
| 7 | 8 | use Yoast\WP\SEO\Integrations\Integration_Interface; |
| 8 | 9 | use Yoast\WP\SEO\Repositories\Indexable_Repository; |
| 9 | 10 | |
| 10 | 11 | /** |
| @@ -26,8 +27,15 @@ | ||
| 26 | 27 | */ |
| 27 | 28 | protected $builder; |
| 28 | 29 | |
| 29 | 30 | /** |
| 31 | + * The indexable helper. | |
| 32 | + * | |
| 33 | + * @var Indexable_Helper | |
| 34 | + */ | |
| 35 | + private $indexable_helper; | |
| 36 | + | |
| 37 | + /** | |
| 30 | 38 | * Returns the conditionals based on which this loadable should be active. |
| 31 | 39 | * |
| 32 | 40 | * @return array |
| 33 | 41 | */ |
| @@ -37,14 +45,20 @@ | ||
| 37 | 45 | |
| 38 | 46 | /** |
| 39 | 47 | * Indexable_Author_Watcher constructor. |
| 40 | 48 | * |
| 41 | - * @param Indexable_Repository $repository The repository to use. | |
| 42 | - * @param Indexable_Builder $builder The builder to use. | |
| 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. | |
| 43 | 52 | */ |
| 44 | - public function __construct( Indexable_Repository $repository, Indexable_Builder $builder ) { | |
| 45 | - $this->repository = $repository; | |
| 46 | - $this->builder = $builder; | |
| 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; | |
| 47 | 61 | } |
| 48 | 62 | |
| 49 | 63 | /** |
| 50 | 64 | * Initializes the integration. |
| @@ -49,13 +63,15 @@ | ||
| 49 | 63 | /** |
| 50 | 64 | * Initializes the integration. |
| 51 | 65 | * |
| 52 | 66 | * This is the place to register hooks and filters. |
| 67 | + * | |
| 68 | + * @return void | |
| 53 | 69 | */ |
| 54 | 70 | public function register_hooks() { |
| 55 | 71 | \add_action( 'user_register', [ $this, 'build_indexable' ], \PHP_INT_MAX ); |
| 56 | 72 | \add_action( 'profile_update', [ $this, 'build_indexable' ], \PHP_INT_MAX ); |
| 57 | - \add_action( 'deleted_user', [ $this, 'delete_indexable' ] ); | |
| 73 | + \add_action( 'deleted_user', [ $this, 'handle_user_delete' ], 10, 2 ); | |
| 58 | 74 | } |
| 59 | 75 | |
| 60 | 76 | /** |
| 61 | 77 | * Deletes user meta. |
| @@ -71,8 +87,9 @@ | ||
| 71 | 87 | return; |
| 72 | 88 | } |
| 73 | 89 | |
| 74 | 90 | $indexable->delete(); |
| 91 | + \do_action( 'wpseo_indexable_deleted', $indexable ); | |
| 75 | 92 | } |
| 76 | 93 | |
| 77 | 94 | /** |
| 78 | 95 | * Saves user meta. |
| @@ -86,8 +103,39 @@ | ||
| 86 | 103 | $indexable = $this->builder->build_for_id_and_type( $user_id, 'user', $indexable ); |
| 87 | 104 | |
| 88 | 105 | if ( $indexable ) { |
| 89 | 106 | $indexable->object_last_modified = \max( $indexable->object_last_modified, \current_time( 'mysql' ) ); |
| 90 | - $indexable->save(); | |
| 107 | + $this->indexable_helper->save_indexable( $indexable ); | |
| 91 | 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(); | |
| 92 | 140 | } |
| 93 | 141 | } |