PageRenderTime 12ms CodeModel.GetById 2ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 1ms

/wp-content/plugins/wordpress-seo/admin/notifiers/class-configuration-notifier.php

https://bitbucket.org/carloskikea/helpet
PHP | 156 lines | 75 code | 18 blank | 63 comment | 5 complexity | 5c5e221b1d579f21cdabe22a49a465bd MD5 | raw file
  1<?php
  2/**
  3 * WPSEO plugin file.
  4 *
  5 * @package WPSEO\Admin\Notifiers
  6 */
  7
  8/**
  9 * Represents the logic for showing the notification.
 10 */
 11class WPSEO_Configuration_Notifier implements WPSEO_Listener {
 12	const META_NAME = 'wpseo-dismiss-configuration-notice';
 13	const META_VALUE = 'yes';
 14	/** @var bool */
 15	protected $show_notification;
 16
 17	/**
 18	 * Constructs the object by setting the show notification property based the given options.
 19	 */
 20	public function __construct() {
 21		$this->show_notification = WPSEO_Options::get( 'show_onboarding_notice', false );
 22	}
 23
 24	/**
 25	 * Returns the content of the notification.
 26	 *
 27	 * @return string A string with the notification HTML, or empty string when no notification is needed.
 28	 */
 29	public function notify() {
 30		if ( ! $this->show_notification() ) {
 31			return $this->re_run_notification();
 32		}
 33
 34		return $this->first_time_notification();
 35	}
 36
 37	/**
 38	 * Listens to an argument in the request URL. When triggered just set the notification to dismissed.
 39	 *
 40	 * @return void
 41	 */
 42	public function listen() {
 43		if ( ! $this->show_notification() || ! $this->dismissal_is_triggered() ) {
 44			return;
 45		}
 46
 47		$this->set_dismissed();
 48	}
 49
 50	/**
 51	 * Checks if the dismissal should be triggered.
 52	 *
 53	 * @return bool True when action has been triggered.
 54	 */
 55	protected function dismissal_is_triggered() {
 56		return filter_input( INPUT_GET, 'dismiss_get_started' ) === '1';
 57	}
 58
 59	/**
 60	 * Checks if the current user has dismissed the notification.
 61	 *
 62	 * @return bool True when the notification has been dismissed.
 63	 */
 64	protected function is_dismissed() {
 65		return get_user_meta( get_current_user_id(), self::META_NAME, true ) === self::META_VALUE;
 66	}
 67
 68	/**
 69	 * Sets the dismissed state for the current user.
 70	 *
 71	 * @return void
 72	 */
 73	protected function set_dismissed() {
 74		update_user_meta( get_current_user_id(), self::META_NAME, self::META_VALUE );
 75	}
 76
 77	/**
 78	 * Checks if the notification should be shown.
 79	 *
 80	 * @return bool True when notification should be shown.
 81	 */
 82	protected function show_notification() {
 83		return $this->show_notification && ! $this->is_dismissed();
 84	}
 85
 86	/**
 87	 * Returns the notification to re-run the config wizard.
 88	 *
 89	 * @return string The notification.
 90	 */
 91	private function re_run_notification() {
 92		$content = sprintf(
 93			/* translators: %1$s expands to Yoast SEO, %2$s is a link start tag to the Onboarding Wizard, %3$s is the link closing tag. */
 94			esc_html__( 'Want to make sure your %1$s settings are still OK? %2$sOpen the configuration wizard again%3$s to validate them.', 'wordpress-seo' ),
 95			'Yoast SEO',
 96			'<a href="' . esc_url( admin_url( 'admin.php?page=' . WPSEO_Configuration_Page::PAGE_IDENTIFIER ) ) . '">',
 97			'</a>'
 98		);
 99
100		return $this->notification( __( 'Check SEO configuration', 'wordpress-seo' ), $content );
101	}
102
103	/**
104	 * Returns the notification to start the config wizard for the first time.
105	 *
106	 * @return string The notification.
107	 */
108	private function first_time_notification() {
109		$content = sprintf(
110			/* translators: %1$s expands to Yoast SEO, %2$s is a link start tag to the Onboarding Wizard, %3$s is the link closing tag. */
111			esc_html__( 'Get started quickly with the %1$s %2$sconfiguration wizard%3$s!', 'wordpress-seo' ),
112			'Yoast SEO',
113			'<a href="' . esc_url( admin_url( 'admin.php?page=' . WPSEO_Configuration_Page::PAGE_IDENTIFIER ) ) . '">',
114			'</a>'
115		);
116
117		return $this->notification( __( 'First-time SEO configuration', 'wordpress-seo' ), $content, true );
118	}
119
120	/**
121	 * Returns a styled notification.
122	 *
123	 * @param string $title          Title for the notification.
124	 * @param string $content        Content for the notification.
125	 * @param bool   $show_dismissal Whether to show the dismiss button or not.
126	 *
127	 * @return string The styled notification.
128	 */
129	private function notification( $title, $content, $show_dismissal = false ) {
130		$notification  = '<div class="yoast-container yoast-container__configuration-wizard">';
131		$notification .= sprintf(
132			'<img src="%1$s" height="%2$s" width="%3$d"  />',
133			esc_url( plugin_dir_url( WPSEO_FILE ) . 'images/new-to-configuration-notice.svg' ),
134			60,
135			60
136		);
137		$notification .= '<div class="yoast-container__configuration-wizard--content">';
138		$notification .= '<h3>' . esc_html( $title ) . '</h3>';
139
140		$notification .= '<p>';
141		$notification .= $content;
142		$notification .= '</p>';
143
144		$notification .= '</div>';
145		if ( $show_dismissal ) {
146			$notification .= sprintf(
147				'<a href="%1$s" style="" class="button dismiss yoast-container__configuration-wizard--dismiss"><span class="screen-reader-text">%2$s</span><span class="dashicons dashicons-no-alt"></span></a>',
148				esc_url( admin_url( 'admin.php?page=wpseo_dashboard&amp;dismiss_get_started=1' ) ),
149				esc_html__( 'Dismiss this item.', 'wordpress-seo' )
150			);
151		}
152		$notification .= '</div>';
153
154		return $notification;
155	}
156}