PageRenderTime 16ms CodeModel.GetById 10ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/wordpress-seo/admin/links/class-link-notifier.php

https://bitbucket.org/carloskikea/helpet
PHP | 127 lines | 67 code | 16 blank | 44 comment | 7 complexity | 26f0d3c87b2ac31c2920c9b7c2539c5f MD5 | raw file
  1<?php
  2/**
  3 * WPSEO plugin file.
  4 *
  5 * @package WPSEO\Admin
  6 */
  7
  8/**
  9 * Represents the notifier for adding link indexing notification to the dashboard.
 10 */
 11class WPSEO_Link_Notifier {
 12
 13	const NOTIFICATION_ID = 'wpseo-reindex-links';
 14
 15	/**
 16	 * Registers all hooks to WordPress
 17	 */
 18	public function register_hooks() {
 19		if ( filter_input( INPUT_GET, 'page' ) === 'wpseo_dashboard' ) {
 20			add_action( 'admin_init', array( $this, 'cleanup_notification' ) );
 21		}
 22
 23		if ( ! wp_next_scheduled( self::NOTIFICATION_ID ) ) {
 24			wp_schedule_event( time(), 'daily', self::NOTIFICATION_ID );
 25		}
 26
 27		add_action( self::NOTIFICATION_ID, array( $this, 'manage_notification' ) );
 28	}
 29
 30	/**
 31	 * Removes the notification when it is set and the amount of unindexed items is lower than the threshold.
 32	 */
 33	public function cleanup_notification() {
 34		if ( ! $this->has_notification() || $this->requires_notification() ) {
 35			return;
 36		}
 37
 38		$this->remove_notification( $this->get_notification() );
 39	}
 40
 41	/**
 42	 * Adds the notification when it isn't set already and the amount of unindexed items is greater than the set.
 43	 * threshold.
 44	 */
 45	public function manage_notification() {
 46		if ( $this->has_notification() || ! $this->requires_notification() ) {
 47			return;
 48		}
 49
 50		$this->add_notification( $this->get_notification() );
 51	}
 52
 53	/**
 54	 * Checks if the notification has been set already.
 55	 *
 56	 * @return bool True when there is a notification.
 57	 */
 58	public function has_notification() {
 59		$notification = Yoast_Notification_Center::get()->get_notification_by_id( self::NOTIFICATION_ID );
 60
 61		return $notification instanceof Yoast_Notification;
 62	}
 63
 64	/**
 65	 * Adds a notification to the notification center.
 66	 *
 67	 * @param Yoast_Notification $notification The notification to add.
 68	 */
 69	protected function add_notification( Yoast_Notification $notification ) {
 70		Yoast_Notification_Center::get()->add_notification( $notification );
 71	}
 72
 73	/**
 74	 * Removes the notification from the notification center.
 75	 *
 76	 * @param Yoast_Notification $notification The notification to remove.
 77	 */
 78	protected function remove_notification( Yoast_Notification $notification ) {
 79		Yoast_Notification_Center::get()->remove_notification( $notification );
 80	}
 81
 82	/**
 83	 * Returns an instance of the notification.
 84	 *
 85	 * @return Yoast_Notification The notification to show.
 86	 */
 87	protected function get_notification() {
 88		return new Yoast_Notification(
 89			sprintf(
 90				/* translators: 1: link to yoast.com post about internal linking suggestion. 2: is anchor closing. 3: button to the recalculation option. 4: closing button */
 91				__(
 92					'To make sure all the links in your texts are counted, we need to analyze all your texts.
 93					All you have to do is press the following button and we\'ll go through all your texts for you.
 94
 95					%3$sCount links%4$s
 96
 97					The Text link counter feature provides insights in how many links are found in your text and how many links are referring to your text. This is very helpful when you are improving your %1$sinternal linking%2$s.',
 98					'wordpress-seo'
 99				),
100				'<a href="' . WPSEO_Shortlinker::get( 'https://yoa.st/15m' ) . '" target="_blank">',
101				'</a>',
102				'<button type="button" id="noticeRunLinkIndex" class="button">',
103				'</button>'
104			),
105			array(
106				'type'         => Yoast_Notification::WARNING,
107				'id'           => self::NOTIFICATION_ID,
108				'capabilities' => 'wpseo_manage_options',
109				'priority'     => 0.8,
110			)
111		);
112	}
113
114	/**
115	 * Checks if the unindexed threshold is exceeded.
116	 *
117	 * @return bool True when the threshold is exceeded.
118	 */
119	protected function requires_notification() {
120		$post_types = apply_filters( 'wpseo_link_count_post_types', WPSEO_Post_Type::get_accessible_post_types() );
121		if ( ! is_array( $post_types ) ) {
122			return false;
123		}
124
125		return WPSEO_Link_Query::has_unprocessed_posts( $post_types );
126	}
127}