| 1 |
<?php |
| 2 |
|
| 3 |
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure. |
| 4 |
namespace Yoast\WP\SEO\Alerts\User_Interface\Default_SEO_Data; |
| 5 |
|
| 6 |
use Yoast\WP\SEO\Conditionals\No_Conditionals; |
| 7 |
use Yoast\WP\SEO\Helpers\Options_Helper; |
| 8 |
use Yoast\WP\SEO\Integrations\Integration_Interface; |
| 9 |
use Yoast\WP\SEO\Models\Indexable; |
| 10 |
use Yoast\WP\SEO\Repositories\Indexable_Repository; |
| 11 |
|
| 12 |
/** |
| 13 |
* This handles the process of checking for non-default SEO in the latest content and updating the relevant options right away. |
| 14 |
*/ |
| 15 |
class Default_SEO_Data_Watcher implements Integration_Interface { |
| 16 |
|
| 17 |
use No_Conditionals; |
| 18 |
|
| 19 |
/** |
| 20 |
* The indexable repository. |
| 21 |
* |
| 22 |
* @var Indexable_Repository |
| 23 |
*/ |
| 24 |
private $indexable_repository; |
| 25 |
|
| 26 |
/** |
| 27 |
* The options helper. |
| 28 |
* |
| 29 |
* @var Options_Helper |
| 30 |
*/ |
| 31 |
private $options_helper; |
| 32 |
|
| 33 |
/** |
| 34 |
* Constructor. |
| 35 |
* |
| 36 |
* @param Indexable_Repository $indexable_repository The indexable repository. |
| 37 |
* @param Options_Helper $options_helper The options helper. |
| 38 |
*/ |
| 39 |
public function __construct( |
| 40 |
Indexable_Repository $indexable_repository, |
| 41 |
Options_Helper $options_helper |
| 42 |
) { |
| 43 |
$this->indexable_repository = $indexable_repository; |
| 44 |
$this->options_helper = $options_helper; |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Registers the hooks with WordPress. |
| 49 |
* |
| 50 |
* @return void |
| 51 |
*/ |
| 52 |
public function register_hooks() { |
| 53 |
\add_action( 'wpseo_saved_indexable', [ $this, 'check_for_default_seo_data' ], 10, 1 ); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Checks for default SEO data in the saved indexable and the most recently modified posts. |
| 58 |
* |
| 59 |
* @param Indexable $saved_indexable The saved indexable. |
| 60 |
* |
| 61 |
* @return void |
| 62 |
*/ |
| 63 |
public function check_for_default_seo_data( $saved_indexable ): void { |
| 64 |
// We have activated this feature only for posts for now. |
| 65 |
if ( $saved_indexable->object_type !== 'post' || $saved_indexable->object_sub_type !== 'post' ) { |
| 66 |
return; |
| 67 |
} |
| 68 |
|
| 69 |
// If the title or description is null, it means it's not default SEO data so let's reset the options. |
| 70 |
if ( $saved_indexable->title !== null ) { |
| 71 |
$this->options_helper->set( 'default_seo_title', [] ); |
| 72 |
} |
| 73 |
|
| 74 |
if ( $saved_indexable->description !== null ) { |
| 75 |
$this->options_helper->set( 'default_seo_meta_desc', [] ); |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
|