| 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\Integrations\Integration_Interface; |
| 8 |
use Yoast\WP\SEO\Repositories\Indexable_Repository; |
| 9 |
|
| 10 |
/** |
| 11 |
* Watches an Author to save the meta information to an Indexable when updated. |
| 12 |
*/ |
| 13 |
class Indexable_Author_Watcher implements Integration_Interface { |
| 14 |
|
| 15 |
/** |
| 16 |
* The indexable repository. |
| 17 |
* |
| 18 |
* @var Indexable_Repository |
| 19 |
*/ |
| 20 |
protected $repository; |
| 21 |
|
| 22 |
/** |
| 23 |
* The indexable builder. |
| 24 |
* |
| 25 |
* @var Indexable_Builder |
| 26 |
*/ |
| 27 |
protected $builder; |
| 28 |
|
| 29 |
/** |
| 30 |
* Returns the conditionals based on which this loadable should be active. |
| 31 |
* |
| 32 |
* @return array |
| 33 |
*/ |
| 34 |
public static function get_conditionals() { |
| 35 |
return [ Migrations_Conditional::class ]; |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Indexable_Author_Watcher constructor. |
| 40 |
* |
| 41 |
* @param Indexable_Repository $repository The repository to use. |
| 42 |
* @param Indexable_Builder $builder The builder to use. |
| 43 |
*/ |
| 44 |
public function __construct( Indexable_Repository $repository, Indexable_Builder $builder ) { |
| 45 |
$this->repository = $repository; |
| 46 |
$this->builder = $builder; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Initializes the integration. |
| 51 |
* |
| 52 |
* This is the place to register hooks and filters. |
| 53 |
*/ |
| 54 |
public function register_hooks() { |
| 55 |
\add_action( 'user_register', [ $this, 'build_indexable' ], \PHP_INT_MAX ); |
| 56 |
\add_action( 'profile_update', [ $this, 'build_indexable' ], \PHP_INT_MAX ); |
| 57 |
\add_action( 'deleted_user', [ $this, 'delete_indexable' ] ); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Deletes user meta. |
| 62 |
* |
| 63 |
* @param int $user_id User ID to delete the metadata of. |
| 64 |
* |
| 65 |
* @return void |
| 66 |
*/ |
| 67 |
public function delete_indexable( $user_id ) { |
| 68 |
$indexable = $this->repository->find_by_id_and_type( $user_id, 'user', false ); |
| 69 |
|
| 70 |
if ( ! $indexable ) { |
| 71 |
return; |
| 72 |
} |
| 73 |
|
| 74 |
$indexable->delete(); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Saves user meta. |
| 79 |
* |
| 80 |
* @param int $user_id User ID. |
| 81 |
* |
| 82 |
* @return void |
| 83 |
*/ |
| 84 |
public function build_indexable( $user_id ) { |
| 85 |
$indexable = $this->repository->find_by_id_and_type( $user_id, 'user', false ); |
| 86 |
$indexable = $this->builder->build_for_id_and_type( $user_id, 'user', $indexable ); |
| 87 |
|
| 88 |
if ( $indexable ) { |
| 89 |
$indexable->object_last_modified = \max( $indexable->object_last_modified, \current_time( 'mysql' ) ); |
| 90 |
$indexable->save(); |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
|