-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from Yoast/stories/38-make-message-dismissible
Makes message dismissible
- Loading branch information
Showing
9 changed files
with
283 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
|
||
/** | ||
* A class to dismiss messages. | ||
*/ | ||
class Whip_MessageDismisser { | ||
|
||
/** @var Whip_DismissStorage */ | ||
protected $storage; | ||
|
||
/** @var string */ | ||
protected $currentVersion; | ||
|
||
/** | ||
* Whip_MessageDismisser constructor. | ||
* | ||
* @param string $currentVersion The current version of the installation. | ||
* @param Whip_DismissStorage $storage The storage object handling storage of versioning. | ||
*/ | ||
public function __construct( $currentVersion, Whip_DismissStorage $storage ) { | ||
$this->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 ) ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
/** | ||
* Represents the WordPress option for saving the dismissed messages. | ||
*/ | ||
class Whip_WPDismissOption implements Whip_DismissStorage { | ||
|
||
/** @var string */ | ||
protected $optionName = 'whip_dismissed_for_wp_version'; | ||
|
||
/** | ||
* Saves the value to the options. | ||
* | ||
* @param string $dismissedVersion The value to save. | ||
* | ||
* @return bool True when successful. | ||
*/ | ||
public function set( $dismissedVersion ) { | ||
return update_option( $this->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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
/** | ||
* Listener for dismissing a message. | ||
*/ | ||
class Whip_WPMessageDismissListener implements Whip_Listener { | ||
|
||
const ACTION_NAME = 'whip_dismiss'; | ||
|
||
/** | ||
* @var Whip_MessageDismisser | ||
*/ | ||
protected $dismisser; | ||
|
||
/** | ||
* Sets the dismisser attribute. | ||
* | ||
* @param Whip_MessageDismisser $dismisser The object for dismissing a message. | ||
*/ | ||
public function __construct( Whip_MessageDismisser $dismisser ) { | ||
$this->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 ) | ||
); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
/** | ||
* Interface Whip_DismissStorage. | ||
*/ | ||
interface Whip_DismissStorage { | ||
|
||
/** | ||
* Saves the value. | ||
* | ||
* @param string $dismissedVersion The value to save. | ||
* | ||
* @return bool True when successful. | ||
*/ | ||
public function set( $dismissedVersion ); | ||
|
||
/** | ||
* Returns the value. | ||
* | ||
* @return string | ||
*/ | ||
public function get(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
/** | ||
* Interface Whip_Listener. | ||
*/ | ||
interface Whip_Listener { | ||
|
||
/** | ||
* Method that should implement the listen functionality. | ||
* | ||
* @return void | ||
*/ | ||
public function listen(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
class Whip_DismissStorageMock implements Whip_DismissStorage { | ||
|
||
/** @var string */ | ||
protected $dismissed = ''; | ||
|
||
/** | ||
* Saves the value. | ||
* | ||
* @param string $dismissedVersion The value to save. | ||
*/ | ||
public function set( $dismissedVersion ) { | ||
$this->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 ), | ||
); | ||
} | ||
|
||
} |