Skip to content

Commit

Permalink
Merge pull request #43 from Yoast/42-show-notice-every-4-weeks
Browse files Browse the repository at this point in the history
Show notice every 4 weeks
  • Loading branch information
jcomack authored Aug 4, 2017
2 parents 0f38cde + ee3220d commit a6823f4
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 75 deletions.
17 changes: 13 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
language: php
dist: trusty
php:
- '7.1'
- '7.0'
- '5.6'
- '5.5'
- '5.4'
- '5.3'
- '5.2'

matrix:
fast_finish: true
include:
- php: 5.2
dist: precise
- php: 5.3
dist: precise


install:
- phpenv local 5.3
- composer install
- phpenv local 5.6
- composer selfupdate 1.0.0 --no-interaction
- composer install --no-interaction
- if [[ "$TRAVIS_PHP_VERSION" == "5.2" ]]; then composer remove --dev phpunit/phpunit; fi
- phpenv local --unset

Expand Down
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
],
"post-autoload-dump": [
"xrstf\\Composer52\\Generator::onPostInstallCmd"
],
"test": [
"vendor/bin/phpunit"
]
}
}
39 changes: 15 additions & 24 deletions src/Whip_MessageDismisser.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php


/**
* A class to dismiss messages.
*/
Expand All @@ -10,45 +9,37 @@ class Whip_MessageDismisser {
protected $storage;

/** @var string */
protected $currentVersion;
protected $currentTime;

/** @var int */
protected $threshold;

/**
* Whip_MessageDismisser constructor.
*
* @param string $currentVersion The current version of the installation.
* @param Whip_DismissStorage $storage The storage object handling storage of versioning.
* @param int $currentTime The current time.
* @param int $threshold The number of seconds the message will be dismissed.
* @param Whip_DismissStorage $storage Storage object to manage the dismissal state.
*/
public function __construct( $currentVersion, Whip_DismissStorage $storage ) {
$this->currentVersion = $this->toMajorVersion( $currentVersion );
$this->storage = $storage;
public function __construct( $currentTime, $threshold, Whip_DismissStorage $storage ) {
$this->currentTime = $currentTime;
$this->threshold = $threshold;
$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 );
$this->storage->set( $this->currentTime );
}

/**
* Checks if the stored version is equal or higher than the version to check against.
* Checks if the current time is lower than the stored time extended by the threshold.
*
* @return bool True when saved version is equal or higher than the provided version.
* @return bool True when current time is lower than stored value + threshold.
*/
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 ) );
return ( $this->currentTime <= ( $this->storage->get() + $this->threshold ) );
}
}
14 changes: 7 additions & 7 deletions src/Whip_WPDismissOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,31 @@
class Whip_WPDismissOption implements Whip_DismissStorage {

/** @var string */
protected $optionName = 'whip_dismissed_for_wp_version';
protected $optionName = 'whip_dismiss_timestamp';

/**
* Saves the value to the options.
*
* @param string $dismissedVersion The value to save.
* @param int $dismissedValue The value to save.
*
* @return bool True when successful.
*/
public function set( $dismissedVersion ) {
return update_option( $this->optionName, $dismissedVersion );
public function set( $dismissedValue ) {
return update_option( $this->optionName, $dismissedValue );
}

/**
* Returns the value of the whip_dismissed option.
*
* @return string Returns the value of the option or an empty string when not set.
* @return int 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 0;
}

return $dismissedOption;
return (int) $dismissedOption;
}

}
9 changes: 5 additions & 4 deletions src/facades/wordpress.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ function whip_wp_check_versions( $requirements ) {
return;
}

global $wp_version;

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

Expand All @@ -27,9 +25,12 @@ function whip_wp_check_versions( $requirements ) {
return;
}

$dismisser = new Whip_MessageDismisser( $wp_version, new Whip_WPDismissOption() );
$dismissThreshold = WEEK_IN_SECONDS * 4;
$dismissMessage = __( 'Remind me again in 4 weeks.', 'wordpress' );

$dismisser = new Whip_MessageDismisser( time(), $dismissThreshold, new Whip_WPDismissOption() );

$presenter = new Whip_WPMessagePresenter( $checker->getMostRecentMessage(), $dismisser );
$presenter = new Whip_WPMessagePresenter( $checker->getMostRecentMessage(), $dismisser, $dismissMessage );
$presenter->register_hooks();
}
}
6 changes: 3 additions & 3 deletions src/interfaces/Whip_DismissStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ interface Whip_DismissStorage {
/**
* Saves the value.
*
* @param string $dismissedVersion The value to save.
* @param int $dismissedValue The value to save.
*
* @return bool True when successful.
*/
public function set( $dismissedVersion );
public function set( $dismissedValue );

/**
* Returns the value.
*
* @return string
* @return int The stored value.
*/
public function get();

Expand Down
27 changes: 16 additions & 11 deletions src/presenters/Whip_WPMessagePresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
*/
class Whip_WPMessagePresenter implements Whip_MessagePresenter {

/** @var string The string to show to dismiss the message. */
private $dismissMessage;

/** @var Whip_Message The message to be displayed */
private $message;

/** @var Whip_MessageDismisser */
Expand All @@ -13,12 +17,14 @@ class Whip_WPMessagePresenter implements Whip_MessagePresenter {
/**
* Whip_WPMessagePresenter constructor.
*
* @param Whip_Message $message The message to use in the presenter.
* @param Whip_MessageDismisser $dismisser Dismisser object.
* @param Whip_Message $message The message to use in the presenter.
* @param Whip_MessageDismisser $dismisser Dismisser object.
* @param string $dismissMessage The copy to show to dismiss the message.
*/
public function __construct( Whip_Message $message, Whip_MessageDismisser $dismisser ) {
$this->message = $message;
$this->dismisser = $dismisser;
public function __construct( Whip_Message $message, Whip_MessageDismisser $dismisser, $dismissMessage ) {
$this->message = $message;
$this->dismisser = $dismisser;
$this->dismissMessage = $dismissMessage;
}

/**
Expand Down Expand Up @@ -46,14 +52,13 @@ public function renderMessage() {
return;
}

/* translators: 1: is a link to dismiss url 2: closing link tag */
$dismissButton = sprintf(
__( '<p>%1$sRemind me again after the next WordPress release.%2$s</p>', 'wordpress' ),
'<a href="' . $dismissListener->getDismissURL() . '">',
'</a>'
'<a href="%2$s">%1$s</a>',
$this->dismissMessage,
$dismissListener->getDismissURL()
);

printf( '<div class="error">%1$s<p>%2$s</p></div>', $this->kses( $this->message->body() ), $dismissButton );
printf( '<div class="error"><p>%1$s</p><p>%2$s</p></div>', $this->kses( $this->message->body() ), $dismissButton );
}

/**
Expand All @@ -66,7 +71,7 @@ public function renderMessage() {
public function kses( $message ) {
return wp_kses( $message, array(
'a' => array(
'href' => true,
'href' => true,
'target' => true,
),
'strong' => true,
Expand Down
58 changes: 36 additions & 22 deletions tests/MessageDismisserTest.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,32 @@
<?php

if ( ! defined( 'WEEK_IN_SECONDS' ) ) {
define( 'WEEK_IN_SECONDS', 60 * 60 * 24 * 7 );
}


class Whip_DismissStorageMock implements Whip_DismissStorage {

/** @var string */
protected $dismissed = '';
/** @var int */
protected $dismissed = 0;

/**
* Saves the value.
*
* @param string $dismissedVersion The value to save.
* @param int $dismissedValue The value to save.
*
* @return boolean
*/
public function set( $dismissedVersion ) {
$this->dismissed = $dismissedVersion;
public function set( $dismissedValue ) {
$this->dismissed = $dismissedValue;

return true;
}

/**
* Returns the value.
*
* @return string
* @return int
*/
public function get() {
return $this->dismissed;
Expand All @@ -31,41 +40,46 @@ class MessageDismisserTest extends PHPUnit_Framework_TestCase {
* @covers Whip_MessageDismisser::dismiss()
*/
public function testDismiss() {
$storage = new Whip_DismissStorageMock();
$dismisser = new Whip_MessageDismisser( '4.8', $storage );
$currentTime = time();
$storage = new Whip_DismissStorageMock();
$dismisser = new Whip_MessageDismisser( $currentTime, WEEK_IN_SECONDS * 4, $storage );

$this->assertEquals( 0, $storage->get() );

$dismisser->dismiss();

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

/**
* @dataProvider versionNumbersProvider
*
* @param string $savedVersion The saved version number.
* @param string $currentVersion The current version number.
* @param bool $expected The expected value.
* @param int $savedTime The saved time.
* @param int $currentTime The current time.
* @param bool $expected The expected value.
*
* @covers Whip_MessageDismisser::__construct()
* @covers Whip_MessageDismisser::isDismissed()
* @covers Whip_MessageDismisser::toMajorVersion()
*/
public function testIsDismissibleWithVersions( $savedVersion, $currentVersion, $expected ) {
public function testIsDismissibleWithVersions( $savedTime, $currentTime, $expected ) {
$storage = new Whip_DismissStorageMock();
$storage->set( $savedVersion );
$dismisser = new Whip_MessageDismisser( $currentVersion, $storage );
$storage->set( $savedTime );
$dismisser = new Whip_MessageDismisser( $currentTime, WEEK_IN_SECONDS * 4, $storage );

$this->assertEquals( $expected, $dismisser->isDismissed() );
}

/**
* Provides array with test values.
*
* @return array
*/
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 ),
array( strtotime( "-2weeks" ), time(), true ),
array( strtotime( "-4weeks" ), time(), true ),
array( strtotime( "-6weeks" ), time(), false ),
array( time(), time(), true ),
);
}

Expand Down

0 comments on commit a6823f4

Please sign in to comment.