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

Show notice every 4 weeks #43

Merged
merged 17 commits into from
Aug 4, 2017
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
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 till dismiss state expires.
* @param Whip_DismissStorage $storage The storage object handling storage of the time.
*/
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 ) );
}
}
12 changes: 6 additions & 6 deletions src/Whip_WPDismissOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,26 @@ class Whip_WPDismissOption implements Whip_DismissStorage {
/**
* 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;
}

}
4 changes: 1 addition & 3 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,7 +25,7 @@ function whip_wp_check_versions( $requirements ) {
return;
}

$dismisser = new Whip_MessageDismisser( $wp_version, new Whip_WPDismissOption() );
$dismisser = new Whip_MessageDismisser( time(), WEEK_IN_SECONDS * 4, new Whip_WPDismissOption() );

$presenter = new Whip_WPMessagePresenter( $checker->getMostRecentMessage(), $dismisser );
$presenter->register_hooks();
Expand Down
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
*/
public function get();

Expand Down
2 changes: 1 addition & 1 deletion src/presenters/Whip_WPMessagePresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function renderMessage() {

/* 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' ),
__( '<p>%1$sRemind me again after 4 weeks.%2$s</p>', 'wordpress' ),
Copy link
Contributor

Choose a reason for hiding this comment

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

Please remove the <p> tags from the translation.

Copy link
Contributor

Choose a reason for hiding this comment

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

Please change after to in

'<a href="' . $dismissListener->getDismissURL() . '">',
'</a>'
);
Expand Down
59 changes: 37 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,47 @@ 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()
* @covers Whip_MessageDismisser::getThreshold()
*/
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