Skip to content
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

Merged
merged 36 commits into from
Jul 31, 2017
Merged

Conversation

andizer
Copy link

@andizer andizer commented Jul 24, 2017

Makes the message dismissible.

Fixes #38

Copy link
Contributor

@moorscode moorscode left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return $versionToConvert;
}

return implode( '.', explode( '.', $versionToConvert, -1 ) );
Copy link
Contributor

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 ) );

* @return bool
*/
public function isDismissible() {
return version_compare( $this->currentVersion, $this->storage->get(), '>' );
Copy link
Contributor

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.

* @param string $dismissedVersion The value to save.
*/
public function set( $dismissedVersion ) {
update_option( 'whip_dismissed', $dismissedVersion );
Copy link
Contributor

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;
}

}
Copy link
Contributor

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' );
Copy link
Contributor

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


$this->assertFalse( $dismisser->isDismissible() );
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing new-line


class MessageDismisserTest extends PHPUnit_Framework_TestCase {

public function testDismiss() {
Copy link
Contributor

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..

$this->assertEquals( '4.8' , $storage->get() );
}

public function testIsDismissed() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See ^

$this->assertTrue( $dismisser->isDismissible() );
}

public function testIsDismissibleNotNeeded() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See ^

/**
* A class to dismiss messages
*/
final class Whip_MessageDismisser {
Copy link
Contributor

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.

Copy link
Contributor

@moorscode moorscode left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some small details.

*
* @return string The major version number.
*/
private function toMajorVersion( $versionToConvert ) {
Copy link
Contributor

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.

private $storage;

/** @var int */
private $currentVersion;
Copy link
Contributor

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

class Whip_MessageDismisser {

/** @var Whip_DismissStorage */
private $storage;
Copy link
Contributor

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

);
}

}
Copy link
Contributor

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' ),
Copy link
Contributor

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' )
Copy link
Contributor

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.

Copy link
Contributor

@schlessera schlessera left a 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.

/**
* Whip_MessageDismisser constructor.
*
* @param int $currentVersion The current version of the installation.
Copy link
Contributor

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.

*
* @param string $dismissedVersion The value to save.
*
* @return bool True when successfull.
Copy link
Contributor

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' ) ) {
Copy link
Contributor

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' ),
Copy link
Contributor

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.

);
}

}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing new line.



/**
* A class to dismiss messages
Copy link
Contributor

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
Copy link
Contributor

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_Listener
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code style: Missing period

array( '4.7', '4.8', true ),
array( '4.7', '4.8.1', true ),
array( '4.7.1', '4.8.1', true ),
);
Copy link
Contributor

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).

);
}

}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing new line.

}

/**
* Checks if the dismissed version is lower then the installation version.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lower than*

/**
* Checks if the dismissed version is lower then the installation version.
*
* @return bool True when saved version is bigger then the current version.
Copy link
Contributor

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 ) {
Copy link
Contributor

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 );
Copy link
Contributor

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 */
Copy link
Contributor

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() . '">',
Copy link
Contributor

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.

$config = include dirname( __FILE__ ) . '/../configs/default.php';
$checker = new Whip_RequirementsChecker( $config );

$dismisser = new Whip_MessageDismisser( $wp_version, new Whip_WPDismissOption() );

if ( ! $dismisser->isDismissed() ) {
Copy link
Contributor

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.

@moorscode moorscode merged commit 0f38cde into master Jul 31, 2017
@moorscode moorscode deleted the stories/38-make-message-dismissible branch July 31, 2017 08:53
@moorscode moorscode added this to the 1.1.0 milestone Aug 1, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants