The conditionals. */ public static function get_conditionals() { return [ Not_Admin_Ajax_Conditional::class, Admin_Conditional::class, Migrations_Conditional::class ]; } /** * Indexable_Post_Type_Change_Watcher constructor. * * @param Options_Helper $options The options helper. * @param Indexing_Helper $indexing_helper The indexing helper. * @param Post_Type_Helper $post_type_helper The post_typehelper. * @param Yoast_Notification_Center $notification_center The notification center. * @param Indexable_Helper $indexable_helper The indexable helper. */ public function __construct( Options_Helper $options, Indexing_Helper $indexing_helper, Post_Type_Helper $post_type_helper, Yoast_Notification_Center $notification_center, Indexable_Helper $indexable_helper ) { $this->options = $options; $this->indexing_helper = $indexing_helper; $this->post_type_helper = $post_type_helper; $this->notification_center = $notification_center; $this->indexable_helper = $indexable_helper; } /** * Initializes the integration. * * This is the place to register hooks and filters. * * @return void */ public function register_hooks() { \add_action( 'admin_init', [ $this, 'check_post_types_public_availability' ] ); } /** * Checks if one or more post types change visibility. * * @return void */ public function check_post_types_public_availability() { // We have to make sure this is just a plain http request, no ajax/REST. if ( \wp_is_json_request() ) { return; } $public_post_types = $this->post_type_helper->get_indexable_post_types(); $last_known_public_post_types = $this->options->get( 'last_known_public_post_types', [] ); // Initializing the option on the first run. if ( empty( $last_known_public_post_types ) ) { $this->options->set( 'last_known_public_post_types', $public_post_types ); return; } // We look for new public post types. $newly_made_public_post_types = \array_diff( $public_post_types, $last_known_public_post_types ); // We look for post types that from public have been made private. $newly_made_non_public_post_types = \array_diff( $last_known_public_post_types, $public_post_types ); // Nothing to be done if no changes has been made to post types. if ( empty( $newly_made_public_post_types ) && ( empty( $newly_made_non_public_post_types ) ) ) { return; } // Update the list of last known public post types in the database. $this->options->set( 'last_known_public_post_types', $public_post_types ); // There are new post types that have been made public. if ( $newly_made_public_post_types ) { // Force a notification requesting to start the SEO data optimization. \delete_transient( Indexable_Post_Indexation_Action::UNINDEXED_COUNT_TRANSIENT ); \delete_transient( Indexable_Post_Indexation_Action::UNINDEXED_LIMITED_COUNT_TRANSIENT ); $this->indexing_helper->set_reason( Indexing_Reasons::REASON_POST_TYPE_MADE_PUBLIC ); \do_action( 'new_public_post_type_notifications', $newly_made_public_post_types ); } // There are post types that have been made private. if ( $newly_made_non_public_post_types && $this->indexable_helper->should_index_indexables() ) { // Schedule a cron job to remove all the posts whose post type has been made private. $cleanup_not_yet_scheduled = ! \wp_next_scheduled( Cleanup_Integration::START_HOOK ); if ( $cleanup_not_yet_scheduled ) { \wp_schedule_single_event( ( \time() + ( \MINUTE_IN_SECONDS * 5 ) ), Cleanup_Integration::START_HOOK ); } \do_action( 'clean_new_public_post_type_notifications', $newly_made_non_public_post_types ); } } }