-
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
Conversation
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.
⛑
src/Whip_MessageDismisser.php
Outdated
return $versionToConvert; | ||
} | ||
|
||
return implode( '.', explode( '.', $versionToConvert, -1 ) ); |
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.
It would be safer to specify the actual number of elements needed.
This would make the method a little bit more complex, but the substr_count
can be removed. Which is possibly more performance intensive than just exploding and imploding.
$parts = explode( '.', $versionToConvert, 3 );
return implode( '.', array_slice( $parts, 0, 2 ) );
src/Whip_MessageDismisser.php
Outdated
* @return bool | ||
*/ | ||
public function isDismissible() { | ||
return version_compare( $this->currentVersion, $this->storage->get(), '>' ); |
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.
This does not reflect the comment of the method, please swap the versions and do a lower then
comparison.
src/Whip_WPDismissOption.php
Outdated
* @param string $dismissedVersion The value to save. | ||
*/ | ||
public function set( $dismissedVersion ) { | ||
update_option( 'whip_dismissed', $dismissedVersion ); |
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.
The option name is not very descriptive of the value it will contain.
Please consider renaming this to something like: whip_dismissed_for_wp_version
return $dismissedOption; | ||
} | ||
|
||
} |
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.
Missing new-line
*/ | ||
public function listen() { | ||
$action = filter_input( INPUT_GET, 'action' ); | ||
$nonce = filter_input(INPUT_GET, 'nonce' ); |
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.
Missing space before INPUT_GET
tests/MessageDismisserTest.php
Outdated
|
||
$this->assertFalse( $dismisser->isDismissible() ); | ||
} | ||
} |
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.
Missing new-line
tests/MessageDismisserTest.php
Outdated
|
||
class MessageDismisserTest extends PHPUnit_Framework_TestCase { | ||
|
||
public function testDismiss() { |
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.
Missing documentation (@covers
) - sorry user for pinging you..
tests/MessageDismisserTest.php
Outdated
$this->assertEquals( '4.8' , $storage->get() ); | ||
} | ||
|
||
public function testIsDismissed() { |
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.
See ^
tests/MessageDismisserTest.php
Outdated
$this->assertTrue( $dismisser->isDismissible() ); | ||
} | ||
|
||
public function testIsDismissibleNotNeeded() { |
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.
See ^
src/Whip_MessageDismisser.php
Outdated
/** | ||
* A class to dismiss messages | ||
*/ | ||
final class Whip_MessageDismisser { |
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.
This cannot be a final class without an abstract
class or interface
implementation.
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.
Some small details.
src/Whip_MessageDismisser.php
Outdated
* | ||
* @return string The major version number. | ||
*/ | ||
private function toMajorVersion( $versionToConvert ) { |
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.
The access should be protected
for non-final classes.
src/Whip_MessageDismisser.php
Outdated
private $storage; | ||
|
||
/** @var int */ | ||
private $currentVersion; |
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.
The access should be protected for non-final classes
src/Whip_MessageDismisser.php
Outdated
class Whip_MessageDismisser { | ||
|
||
/** @var Whip_DismissStorage */ | ||
private $storage; |
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.
The access should be protected for non-final classes
); | ||
} | ||
|
||
} |
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.
Missing new-line
*/ | ||
public static function get_dismissurl() { | ||
return sprintf( | ||
admin_url( 'index.php?action=whip_dismiss&nonce=%1$s' ), |
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.
To avoid typos, consider moving the action
value to a class variable.
public static function get_dismissurl() { | ||
return sprintf( | ||
admin_url( 'index.php?action=whip_dismiss&nonce=%1$s' ), | ||
wp_create_nonce( 'whip_dismiss' ) |
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.
The avoid typos, consider moving the nonce value to a class variable.
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.
Mostly nitpicking, apart from the place where the dismiss listener is being registered. It is currently being registered regardless of whether a notice is present. It should ideally be registered within the code that registers the presenter hooks, through dependency injection.
src/Whip_MessageDismisser.php
Outdated
/** | ||
* Whip_MessageDismisser constructor. | ||
* | ||
* @param int $currentVersion The current version of the installation. |
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.
This seems to accept a string
, not an int
.
src/Whip_WPDismissOption.php
Outdated
* | ||
* @param string $dismissedVersion The value to save. | ||
* | ||
* @return bool True when successfull. |
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.
Typo: successful
$action = filter_input( INPUT_GET, 'action' ); | ||
$nonce = filter_input( INPUT_GET, 'nonce' ); | ||
|
||
if ( $action === 'whip_dismiss' && wp_verify_nonce( $nonce, 'whip_dismiss' ) ) { |
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.
I'd put the action into a class constant.
*/ | ||
public static function get_dismissurl() { | ||
return sprintf( | ||
admin_url( 'index.php?action=whip_dismiss&nonce=%1$s' ), |
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.
Action into class constant here as well.
); | ||
} | ||
|
||
} |
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.
Missing new line.
src/Whip_MessageDismisser.php
Outdated
|
||
|
||
/** | ||
* A class to dismiss messages |
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.
Code style: Missing period
<?php | ||
|
||
/** | ||
* Interface Whip_DismissStorage |
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.
Code style: Missing period
src/interfaces/Whip_Listener.php
Outdated
<?php | ||
|
||
/** | ||
* Interface Whip_Listener |
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.
Code style: Missing period
tests/MessageDismisserTest.php
Outdated
array( '4.7', '4.8', true ), | ||
array( '4.7', '4.8.1', true ), | ||
array( '4.7.1', '4.8.1', true ), | ||
); |
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.
Also test with intermediary versions, like 4.9-alpha
or similar, as people might be running beta or test versions (or -gulp- use custom-patched versions of Core).
tests/MessageDismisserTest.php
Outdated
); | ||
} | ||
|
||
} |
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.
Missing new line.
src/Whip_MessageDismisser.php
Outdated
} | ||
|
||
/** | ||
* Checks if the dismissed version is lower then the installation version. |
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*
src/Whip_MessageDismisser.php
Outdated
/** | ||
* 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 comment
The reason will be displayed to describe this comment to others. Learn more.
bigger than*
*/ | ||
public function register_hooks() { | ||
public function register_hooks( Whip_MessageDismisser $dismisser ) { |
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.
The $dismisser
should be injected through the constructor as a 2nd argument and stored as a property.
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 comment
The 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 renderMessage()
method.
} | ||
|
||
/** | ||
* 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 comment
The reason will be displayed to describe this comment to others. Learn more.
Here I'd start by creating the listener and registering it.
/* translators: 1: is a link to dismiss url 2: closing link tag */ | ||
$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 comment
The reason will be displayed to describe this comment to others. Learn more.
As we have access to the actual instance of $dismissListener
now, we can get rid of the static access here.
src/facades/wordpress.php
Outdated
$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 comment
The 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."
It seems that the function is returning the inverted expected value. False should be True.
Move the `isDismissed` to the presenter
Makes the message dismissible.
Fixes #38