diff --git a/src/Whip_MessageDismisser.php b/src/Whip_MessageDismisser.php new file mode 100644 index 0000000..dd49da9 --- /dev/null +++ b/src/Whip_MessageDismisser.php @@ -0,0 +1,54 @@ +currentVersion = $this->toMajorVersion( $currentVersion ); + $this->storage = $storage; + } + + /** + * Saves the version number to the storage to indicate the message as being dismissed. + */ + public function dismiss() { + $this->storage->set( $this->currentVersion ); + } + + /** + * Checks if the stored version is equal or higher than the version to check against. + * + * @return bool True when saved version is equal or higher than the provided version. + */ + public function isDismissed() { + return version_compare( $this->storage->get(), $this->currentVersion, '>=' ); + } + + /** + * Converts the version number to a major version number. + * + * @param string $versionToConvert The version to convert. + * + * @return string The major version number. + */ + protected function toMajorVersion( $versionToConvert ) { + $parts = explode( '.', $versionToConvert, 3 ); + + return implode( '.', array_slice( $parts, 0, 2 ) ); + } +} diff --git a/src/Whip_RequirementsChecker.php b/src/Whip_RequirementsChecker.php index 36bc56f..17ceaef 100644 --- a/src/Whip_RequirementsChecker.php +++ b/src/Whip_RequirementsChecker.php @@ -21,7 +21,7 @@ class Whip_RequirementsChecker { * @param array $configuration The configuration to check. * @param string $textdomain The text domain to use for translations. */ - public function __construct( $configuration = array(), $textdomain = 'wordpress-seo' ) { + public function __construct( $configuration = array(), $textdomain = 'wordpress' ) { $this->requirements = array(); $this->configuration = new Whip_Configuration( $configuration ); $this->messageMananger = new Whip_MessagesManager(); diff --git a/src/Whip_WPDismissOption.php b/src/Whip_WPDismissOption.php new file mode 100644 index 0000000..f9caccd --- /dev/null +++ b/src/Whip_WPDismissOption.php @@ -0,0 +1,36 @@ +optionName, $dismissedVersion ); + } + + /** + * Returns the value of the whip_dismissed option. + * + * @return string Returns the value of the option or an empty string when not set. + */ + public function get() { + $dismissedOption = get_option( $this->optionName ); + if ( ! $dismissedOption ) { + return ''; + } + + return $dismissedOption; + } + +} diff --git a/src/Whip_WPMessageDismissListener.php b/src/Whip_WPMessageDismissListener.php new file mode 100644 index 0000000..01a3d87 --- /dev/null +++ b/src/Whip_WPMessageDismissListener.php @@ -0,0 +1,51 @@ +dismisser = $dismisser; + } + + /** + * Listens to a GET request to fetch the required attributes. + * + * @return void + */ + public function listen() { + $action = filter_input( INPUT_GET, 'action' ); + $nonce = filter_input( INPUT_GET, 'nonce' ); + + if ( $action === self::ACTION_NAME && wp_verify_nonce( $nonce, self::ACTION_NAME ) ) { + $this->dismisser->dismiss(); + } + } + + /** + * Creates an url for dismissing the notice. + * + * @return string The url for dismissing the message. + */ + public function getDismissURL() { + return sprintf( + admin_url( 'index.php?action=%1$s&nonce=%2$s' ), + self::ACTION_NAME, + wp_create_nonce( self::ACTION_NAME ) + ); + } + +} diff --git a/src/facades/wordpress.php b/src/facades/wordpress.php index dc50dd8..d0c0e64 100644 --- a/src/facades/wordpress.php +++ b/src/facades/wordpress.php @@ -12,6 +12,8 @@ function whip_wp_check_versions( $requirements ) { return; } + global $wp_version; + $config = include dirname( __FILE__ ) . '/../configs/default.php'; $checker = new Whip_RequirementsChecker( $config ); @@ -25,7 +27,9 @@ function whip_wp_check_versions( $requirements ) { return; } - $presenter = new Whip_WPMessagePresenter( $checker->getMostRecentMessage() ); + $dismisser = new Whip_MessageDismisser( $wp_version, new Whip_WPDismissOption() ); + + $presenter = new Whip_WPMessagePresenter( $checker->getMostRecentMessage(), $dismisser ); $presenter->register_hooks(); } } diff --git a/src/interfaces/Whip_DismissStorage.php b/src/interfaces/Whip_DismissStorage.php new file mode 100644 index 0000000..08c0e8f --- /dev/null +++ b/src/interfaces/Whip_DismissStorage.php @@ -0,0 +1,24 @@ +message = $message; + public function __construct( Whip_Message $message, Whip_MessageDismisser $dismisser ) { + $this->message = $message; + $this->dismisser = $dismisser; } /** * Registers hooks to WordPress. This is a separate function so you can * control when the hooks are registered. + * */ public function register_hooks() { global $whip_admin_notices_added; + if ( null === $whip_admin_notices_added || ! $whip_admin_notices_added ) { add_action( 'admin_notices', array( $this, 'renderMessage' ) ); $whip_admin_notices_added = true; @@ -32,7 +39,21 @@ public function register_hooks() { * Renders the messages present in the global to notices. */ public function renderMessage() { - printf( '
%s
', $this->kses( $this->message->body() ) ); + $dismissListener = new Whip_WPMessageDismissListener( $this->dismisser ); + $dismissListener->listen(); + + if ( $this->dismisser->isDismissed() ) { + return; + } + + /* translators: 1: is a link to dismiss url 2: closing link tag */ + $dismissButton = sprintf( + __( '

%1$sRemind me again after the next WordPress release.%2$s

', 'wordpress' ), + '', + '' + ); + + printf( '
%1$s

%2$s

', $this->kses( $this->message->body() ), $dismissButton ); } /** diff --git a/tests/MessageDismisserTest.php b/tests/MessageDismisserTest.php new file mode 100644 index 0000000..526e41f --- /dev/null +++ b/tests/MessageDismisserTest.php @@ -0,0 +1,72 @@ +dismissed = $dismissedVersion; + } + + /** + * Returns the value. + * + * @return string + */ + public function get() { + return $this->dismissed; + } +} + +class MessageDismisserTest extends PHPUnit_Framework_TestCase { + + /** + * @covers Whip_MessageDismisser::__construct() + * @covers Whip_MessageDismisser::dismiss() + */ + public function testDismiss() { + $storage = new Whip_DismissStorageMock(); + $dismisser = new Whip_MessageDismisser( '4.8', $storage ); + + $dismisser->dismiss(); + + $this->assertEquals( '4.8' , $storage->get() ); + } + + /** + * @dataProvider versionNumbersProvider + * + * @param string $savedVersion The saved version number. + * @param string $currentVersion The current version number. + * @param bool $expected The expected value. + * + * @covers Whip_MessageDismisser::__construct() + * @covers Whip_MessageDismisser::isDismissed() + * @covers Whip_MessageDismisser::toMajorVersion() + */ + public function testIsDismissibleWithVersions( $savedVersion, $currentVersion, $expected ) { + $storage = new Whip_DismissStorageMock(); + $storage->set( $savedVersion ); + $dismisser = new Whip_MessageDismisser( $currentVersion, $storage ); + + $this->assertEquals( $expected, $dismisser->isDismissed() ); + } + + public function versionNumbersProvider() { + return array( + array( '4.8', '4.8', true ), + array( '4.8', '4.8.1', true ), + array( '4.7', '4.8', false ), + array( '4.7', '4.8.1', false ), + array( '4.7.1', '4.8.1', false ), + array( '4.7', '4.7-alpha', true ), + ); + } + +}