-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Makes message dismissible #39
Changes from 30 commits
a6e6b6d
7b30ee0
c657a0a
17e4545
39a04aa
b9c407e
e2be0ea
d1ac35c
397310b
3347deb
c56b087
aff4b54
512dd7e
6f21b08
9ee70a8
81007d9
8554d0e
1673f18
3e1bd3e
74ad764
1462d05
07d258e
bf68c2b
d63b3e5
23a6923
f686b7a
d04d63f
1256f22
0e8afdb
5ec43b1
9ce712d
4cc0640
a45d142
bbe91cf
67eb369
67fcef0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 dismissed version is lower then the installation version. | ||
* | ||
* @return bool True when saved version is bigger then the current version. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. bigger than* |
||
*/ | ||
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 ) ); | ||
} | ||
} |
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 $option_name = '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->option_name, $dismissedVersion ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
|
||
/** | ||
* 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->option_name ); | ||
if ( ! $dismissedOption ) { | ||
return ''; | ||
} | ||
|
||
return $dismissedOption; | ||
} | ||
|
||
} |
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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing argument & description. |
||
* | ||
* @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 static function get_dismissurl() { | ||
return sprintf( | ||
admin_url( 'index.php?action=%1$s&nonce=%2$s' ), | ||
self::ACTION_NAME, | ||
wp_create_nonce( self::ACTION_NAME ) | ||
); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,9 +12,18 @@ function whip_wp_check_versions( $requirements ) { | |
return; | ||
} | ||
|
||
global $wp_version; | ||
|
||
|
||
$config = include dirname( __FILE__ ) . '/../configs/default.php'; | ||
$checker = new Whip_RequirementsChecker( $config ); | ||
|
||
$dismisser = new Whip_MessageDismisser( $wp_version, new Whip_WPDismissOption() ); | ||
|
||
if ( ! $dismisser->isDismissed() ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This does not read logically: "If the message has not been dismissed, stop execution." |
||
return; | ||
} | ||
|
||
foreach ( $requirements as $component => $versionComparison ) { | ||
$checker->addRequirement( Whip_VersionRequirement::fromCompareString( $component, $versionComparison ) ); | ||
} | ||
|
@@ -26,6 +35,6 @@ function whip_wp_check_versions( $requirements ) { | |
} | ||
|
||
$presenter = new Whip_WPMessagePresenter( $checker->getMostRecentMessage() ); | ||
$presenter->register_hooks(); | ||
$presenter->register_hooks( $dismisser ); | ||
} | ||
} |
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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also add a return value with type boolean, to be able to confirm save has been successful. |
||
* | ||
* @return bool True when successful. | ||
*/ | ||
public function set( $dismissedVersion ); | ||
|
||
/** | ||
* Returns the value. | ||
* | ||
* @return string | ||
*/ | ||
public function get(); | ||
|
||
} |
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(); | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,20 +19,33 @@ public function __construct( Whip_Message $message ) { | |
/** | ||
* Registers hooks to WordPress. This is a separate function so you can | ||
* control when the hooks are registered. | ||
* | ||
* @param Whip_MessageDismisser $dismisser Dismisser object. | ||
*/ | ||
public function register_hooks() { | ||
public function register_hooks( Whip_MessageDismisser $dismisser ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
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; | ||
} | ||
|
||
$dismissListener = new Whip_WPMessageDismissListener( $dismisser ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The listener is only needed if the message is actually being rendered, so it should only be created/registered in the |
||
$dismissListener->listen(); | ||
} | ||
|
||
/** | ||
* Renders the messages present in the global to notices. | ||
*/ | ||
public function renderMessage() { | ||
printf( '<div class="error">%s</div>', $this->kses( $this->message->body() ) ); | ||
/* translators: 1: is a link to dismiss url 2: closing link tag */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here I'd start by creating the listener and registering it. |
||
$dismiss_button = sprintf( | ||
__( '<p>%1$sRemind me again after the next WordPress release.%2$s</p>', 'wordpress' ), | ||
'<a href="' . Whip_WPMessageDismissListener::get_dismissurl() . '">', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As we have access to the actual instance of |
||
'</a>' | ||
); | ||
|
||
printf( '<div class="error">%1$s<p>%2$s</p></div>', $this->kses( $this->message->body() ), $dismiss_button ); | ||
} | ||
|
||
/** | ||
|
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 = ''; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing description |
||
|
||
/** | ||
* 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() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing documentation ( |
||
$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', false ), | ||
array( '4.8', '4.8.1', false ), | ||
array( '4.7', '4.8', true ), | ||
array( '4.7', '4.8.1', true ), | ||
array( '4.7.1', '4.8.1', true ), | ||
array( '4.7', '4.7-alpha', false ), | ||
); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also test with intermediary versions, like |
||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lower than*