Yoast\WP\SEO\Integrations\Admin
Indexing_Notification_Integration{} Yoast 1.0
Class Indexing_Notification_Integration.
Хуков нет.
Возвращает
Null. Ничего.
Использование
$Indexing_Notification_Integration = new Indexing_Notification_Integration(); // use class methods
Методы
- __construct(
- get_conditionals()
- get_presenter( $reason )
- maybe_cleanup_notification()
- maybe_create_notification()
- notification()
- register_hooks()
- should_show_notification()
Заметки
- Пакет: Yoast\WP\SEO\Integrations\Admin
Код Indexing_Notification_Integration{} Indexing Notification Integration{} Yoast 15.6.2
<?php
class Indexing_Notification_Integration implements Integration_Interface {
/**
* The notification ID.
*/
const NOTIFICATION_ID = 'wpseo-reindex';
/**
* Represents the reason that the indexing process failed and should be tried again.
*
* @deprecated 15.3
*/
const REASON_INDEXING_FAILED = Indexing_Reasons::REASON_INDEXING_FAILED;
/**
* Represents the reason that the permalink settings are changed.
*
* @deprecated 15.3
*/
const REASON_PERMALINK_SETTINGS = Indexing_Reasons::REASON_PERMALINK_SETTINGS;
/**
* Represents the reason that the category base is changed.
*
* @deprecated 15.3
*/
const REASON_CATEGORY_BASE_PREFIX = Indexing_Reasons::REASON_CATEGORY_BASE_PREFIX;
/**
* Represents the reason that the tag base is changed.
*
* @deprecated 15.3
*/
const REASON_TAG_BASE_PREFIX = Indexing_Reasons::REASON_TAG_BASE_PREFIX;
/**
* Represents the reason that the home url option is changed.
*
* @deprecated 15.3
*/
const REASON_HOME_URL_OPTION = Indexing_Reasons::REASON_HOME_URL_OPTION;
/**
* The Yoast notification center.
*
* @var Yoast_Notification_Center
*/
protected $notification_center;
/**
* The product helper.
*
* @var Product_Helper
*/
protected $product_helper;
/**
* The current page helper.
*
* @var Current_Page_Helper
*/
protected $page_helper;
/**
* The short link helper.
*
* @var Short_Link_Helper
*/
protected $short_link_helper;
/**
* The notification helper.
*
* @var Notification_Helper
*/
protected $notification_helper;
/**
* The indexing helper.
*
* @var Indexing_Helper
*/
protected $indexing_helper;
/**
* Indexing_Notification_Integration constructor.
*
* @param Yoast_Notification_Center $notification_center The notification center.
* @param Product_Helper $product_helper The product helper.
* @param Current_Page_Helper $page_helper The current page helper.
* @param Short_Link_Helper $short_link_helper The short link helper.
* @param Notification_Helper $notification_helper The notification helper.
* @param Indexing_Helper $indexing_helper The indexing helper.
*/
public function __construct(
Yoast_Notification_Center $notification_center,
Product_Helper $product_helper,
Current_Page_Helper $page_helper,
Short_Link_Helper $short_link_helper,
Notification_Helper $notification_helper,
Indexing_Helper $indexing_helper
) {
$this->notification_center = $notification_center;
$this->product_helper = $product_helper;
$this->page_helper = $page_helper;
$this->short_link_helper = $short_link_helper;
$this->notification_helper = $notification_helper;
$this->indexing_helper = $indexing_helper;
}
/**
* Initializes the integration.
*
* Adds hooks and jobs to cleanup or add the notification when necessary.
*
* @return void
*/
public function register_hooks() {
if ( $this->page_helper->get_current_yoast_seo_page() === 'wpseo_dashboard' ) {
\add_action( 'admin_init', [ $this, 'maybe_cleanup_notification' ] );
}
if ( $this->indexing_helper->has_reason() ) {
\add_action( 'admin_init', [ $this, 'maybe_create_notification' ] );
}
\add_action( self::NOTIFICATION_ID, [ $this, 'maybe_create_notification' ] );
}
/**
* Returns the conditionals based on which this loadable should be active.
*
* @return array The conditionals.
*/
public static function get_conditionals() {
return [
Admin_Conditional::class,
];
}
/**
* Checks whether the notification should be shown and adds
* it to the notification center if this is the case.
*/
public function maybe_create_notification() {
if ( ! $this->should_show_notification() ) {
return;
}
if ( ! $this->notification_center->get_notification_by_id( self::NOTIFICATION_ID ) ) {
$notification = $this->notification();
$this->notification_helper->restore_notification( $notification );
$this->notification_center->add_notification( $notification );
}
}
/**
* Checks whether the notification should not be shown anymore and removes
* it from the notification center if this is the case.
*/
public function maybe_cleanup_notification() {
$notification = $this->notification_center->get_notification_by_id( self::NOTIFICATION_ID );
if ( $notification === null ) {
return;
}
if ( $this->should_show_notification() ) {
return;
}
$this->notification_center->remove_notification_by_id( self::NOTIFICATION_ID );
}
/**
* Checks whether the notification should be shown.
*
* @return bool If the notification should be shown.
*/
protected function should_show_notification() {
// Don't show a notification if the indexing has already been started earlier.
if ( $this->indexing_helper->get_started() > 0 ) {
return false;
}
// Never show a notification when nothing should be indexed.
return $this->indexing_helper->get_filtered_unindexed_count() > 0;
}
/**
* Returns an instance of the notification.
*
* @return Yoast_Notification The notification to show.
*/
protected function notification() {
$reason = $this->indexing_helper->get_reason();
$presenter = $this->get_presenter( $reason );
return new Yoast_Notification(
$presenter,
[
'type' => Yoast_Notification::WARNING,
'id' => self::NOTIFICATION_ID,
'capabilities' => 'wpseo_manage_options',
'priority' => 0.8,
]
);
}
/**
* Gets the presenter to use to show the notification.
*
* @param string $reason The reason for the notification.
*
* @return Indexing_Failed_Notification_Presenter|Indexing_Notification_Presenter
*/
protected function get_presenter( $reason ) {
if ( $reason === Indexing_Reasons::REASON_INDEXING_FAILED ) {
$presenter = new Indexing_Failed_Notification_Presenter( $this->product_helper );
}
else {
$total_unindexed = $this->indexing_helper->get_filtered_unindexed_count();
$presenter = new Indexing_Notification_Presenter( $this->short_link_helper, $total_unindexed, $reason );
}
return $presenter;
}
}