Skip to content

Commit a6823f4

Browse files
authored
Merge pull request #43 from Yoast/42-show-notice-every-4-weeks
Show notice every 4 weeks
2 parents 0f38cde + ee3220d commit a6823f4

File tree

8 files changed

+98
-75
lines changed

8 files changed

+98
-75
lines changed

.travis.yml

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
11
language: php
2+
dist: trusty
23
php:
34
- '7.1'
45
- '7.0'
56
- '5.6'
67
- '5.5'
78
- '5.4'
8-
- '5.3'
9-
- '5.2'
9+
10+
matrix:
11+
fast_finish: true
12+
include:
13+
- php: 5.2
14+
dist: precise
15+
- php: 5.3
16+
dist: precise
17+
1018

1119
install:
12-
- phpenv local 5.3
13-
- composer install
20+
- phpenv local 5.6
21+
- composer selfupdate 1.0.0 --no-interaction
22+
- composer install --no-interaction
1423
- if [[ "$TRAVIS_PHP_VERSION" == "5.2" ]]; then composer remove --dev phpunit/phpunit; fi
1524
- phpenv local --unset
1625

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
],
3333
"post-autoload-dump": [
3434
"xrstf\\Composer52\\Generator::onPostInstallCmd"
35+
],
36+
"test": [
37+
"vendor/bin/phpunit"
3538
]
3639
}
3740
}

src/Whip_MessageDismisser.php

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php
22

3-
43
/**
54
* A class to dismiss messages.
65
*/
@@ -10,45 +9,37 @@ class Whip_MessageDismisser {
109
protected $storage;
1110

1211
/** @var string */
13-
protected $currentVersion;
12+
protected $currentTime;
13+
14+
/** @var int */
15+
protected $threshold;
1416

1517
/**
1618
* Whip_MessageDismisser constructor.
1719
*
18-
* @param string $currentVersion The current version of the installation.
19-
* @param Whip_DismissStorage $storage The storage object handling storage of versioning.
20+
* @param int $currentTime The current time.
21+
* @param int $threshold The number of seconds the message will be dismissed.
22+
* @param Whip_DismissStorage $storage Storage object to manage the dismissal state.
2023
*/
21-
public function __construct( $currentVersion, Whip_DismissStorage $storage ) {
22-
$this->currentVersion = $this->toMajorVersion( $currentVersion );
23-
$this->storage = $storage;
24+
public function __construct( $currentTime, $threshold, Whip_DismissStorage $storage ) {
25+
$this->currentTime = $currentTime;
26+
$this->threshold = $threshold;
27+
$this->storage = $storage;
2428
}
2529

2630
/**
2731
* Saves the version number to the storage to indicate the message as being dismissed.
2832
*/
2933
public function dismiss() {
30-
$this->storage->set( $this->currentVersion );
34+
$this->storage->set( $this->currentTime );
3135
}
3236

3337
/**
34-
* Checks if the stored version is equal or higher than the version to check against.
38+
* Checks if the current time is lower than the stored time extended by the threshold.
3539
*
36-
* @return bool True when saved version is equal or higher than the provided version.
40+
* @return bool True when current time is lower than stored value + threshold.
3741
*/
3842
public function isDismissed() {
39-
return version_compare( $this->storage->get(), $this->currentVersion, '>=' );
40-
}
41-
42-
/**
43-
* Converts the version number to a major version number.
44-
*
45-
* @param string $versionToConvert The version to convert.
46-
*
47-
* @return string The major version number.
48-
*/
49-
protected function toMajorVersion( $versionToConvert ) {
50-
$parts = explode( '.', $versionToConvert, 3 );
51-
52-
return implode( '.', array_slice( $parts, 0, 2 ) );
43+
return ( $this->currentTime <= ( $this->storage->get() + $this->threshold ) );
5344
}
5445
}

src/Whip_WPDismissOption.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,31 @@
66
class Whip_WPDismissOption implements Whip_DismissStorage {
77

88
/** @var string */
9-
protected $optionName = 'whip_dismissed_for_wp_version';
9+
protected $optionName = 'whip_dismiss_timestamp';
1010

1111
/**
1212
* Saves the value to the options.
1313
*
14-
* @param string $dismissedVersion The value to save.
14+
* @param int $dismissedValue The value to save.
1515
*
1616
* @return bool True when successful.
1717
*/
18-
public function set( $dismissedVersion ) {
19-
return update_option( $this->optionName, $dismissedVersion );
18+
public function set( $dismissedValue ) {
19+
return update_option( $this->optionName, $dismissedValue );
2020
}
2121

2222
/**
2323
* Returns the value of the whip_dismissed option.
2424
*
25-
* @return string Returns the value of the option or an empty string when not set.
25+
* @return int Returns the value of the option or an empty string when not set.
2626
*/
2727
public function get() {
2828
$dismissedOption = get_option( $this->optionName );
2929
if ( ! $dismissedOption ) {
30-
return '';
30+
return 0;
3131
}
3232

33-
return $dismissedOption;
33+
return (int) $dismissedOption;
3434
}
3535

3636
}

src/facades/wordpress.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ function whip_wp_check_versions( $requirements ) {
1212
return;
1313
}
1414

15-
global $wp_version;
16-
1715
$config = include dirname( __FILE__ ) . '/../configs/default.php';
1816
$checker = new Whip_RequirementsChecker( $config );
1917

@@ -27,9 +25,12 @@ function whip_wp_check_versions( $requirements ) {
2725
return;
2826
}
2927

30-
$dismisser = new Whip_MessageDismisser( $wp_version, new Whip_WPDismissOption() );
28+
$dismissThreshold = WEEK_IN_SECONDS * 4;
29+
$dismissMessage = __( 'Remind me again in 4 weeks.', 'wordpress' );
30+
31+
$dismisser = new Whip_MessageDismisser( time(), $dismissThreshold, new Whip_WPDismissOption() );
3132

32-
$presenter = new Whip_WPMessagePresenter( $checker->getMostRecentMessage(), $dismisser );
33+
$presenter = new Whip_WPMessagePresenter( $checker->getMostRecentMessage(), $dismisser, $dismissMessage );
3334
$presenter->register_hooks();
3435
}
3536
}

src/interfaces/Whip_DismissStorage.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@ interface Whip_DismissStorage {
88
/**
99
* Saves the value.
1010
*
11-
* @param string $dismissedVersion The value to save.
11+
* @param int $dismissedValue The value to save.
1212
*
1313
* @return bool True when successful.
1414
*/
15-
public function set( $dismissedVersion );
15+
public function set( $dismissedValue );
1616

1717
/**
1818
* Returns the value.
1919
*
20-
* @return string
20+
* @return int The stored value.
2121
*/
2222
public function get();
2323

src/presenters/Whip_WPMessagePresenter.php

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
*/
66
class Whip_WPMessagePresenter implements Whip_MessagePresenter {
77

8+
/** @var string The string to show to dismiss the message. */
9+
private $dismissMessage;
10+
11+
/** @var Whip_Message The message to be displayed */
812
private $message;
913

1014
/** @var Whip_MessageDismisser */
@@ -13,12 +17,14 @@ class Whip_WPMessagePresenter implements Whip_MessagePresenter {
1317
/**
1418
* Whip_WPMessagePresenter constructor.
1519
*
16-
* @param Whip_Message $message The message to use in the presenter.
17-
* @param Whip_MessageDismisser $dismisser Dismisser object.
20+
* @param Whip_Message $message The message to use in the presenter.
21+
* @param Whip_MessageDismisser $dismisser Dismisser object.
22+
* @param string $dismissMessage The copy to show to dismiss the message.
1823
*/
19-
public function __construct( Whip_Message $message, Whip_MessageDismisser $dismisser ) {
20-
$this->message = $message;
21-
$this->dismisser = $dismisser;
24+
public function __construct( Whip_Message $message, Whip_MessageDismisser $dismisser, $dismissMessage ) {
25+
$this->message = $message;
26+
$this->dismisser = $dismisser;
27+
$this->dismissMessage = $dismissMessage;
2228
}
2329

2430
/**
@@ -46,14 +52,13 @@ public function renderMessage() {
4652
return;
4753
}
4854

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

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

5964
/**
@@ -66,7 +71,7 @@ public function renderMessage() {
6671
public function kses( $message ) {
6772
return wp_kses( $message, array(
6873
'a' => array(
69-
'href' => true,
74+
'href' => true,
7075
'target' => true,
7176
),
7277
'strong' => true,

tests/MessageDismisserTest.php

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,32 @@
11
<?php
22

3+
if ( ! defined( 'WEEK_IN_SECONDS' ) ) {
4+
define( 'WEEK_IN_SECONDS', 60 * 60 * 24 * 7 );
5+
}
6+
7+
38
class Whip_DismissStorageMock implements Whip_DismissStorage {
49

5-
/** @var string */
6-
protected $dismissed = '';
10+
/** @var int */
11+
protected $dismissed = 0;
712

813
/**
914
* Saves the value.
1015
*
11-
* @param string $dismissedVersion The value to save.
16+
* @param int $dismissedValue The value to save.
17+
*
18+
* @return boolean
1219
*/
13-
public function set( $dismissedVersion ) {
14-
$this->dismissed = $dismissedVersion;
20+
public function set( $dismissedValue ) {
21+
$this->dismissed = $dismissedValue;
22+
23+
return true;
1524
}
1625

1726
/**
1827
* Returns the value.
1928
*
20-
* @return string
29+
* @return int
2130
*/
2231
public function get() {
2332
return $this->dismissed;
@@ -31,41 +40,46 @@ class MessageDismisserTest extends PHPUnit_Framework_TestCase {
3140
* @covers Whip_MessageDismisser::dismiss()
3241
*/
3342
public function testDismiss() {
34-
$storage = new Whip_DismissStorageMock();
35-
$dismisser = new Whip_MessageDismisser( '4.8', $storage );
43+
$currentTime = time();
44+
$storage = new Whip_DismissStorageMock();
45+
$dismisser = new Whip_MessageDismisser( $currentTime, WEEK_IN_SECONDS * 4, $storage );
46+
47+
$this->assertEquals( 0, $storage->get() );
3648

3749
$dismisser->dismiss();
3850

39-
$this->assertEquals( '4.8' , $storage->get() );
51+
$this->assertEquals( $currentTime, $storage->get() );
4052
}
4153

4254
/**
4355
* @dataProvider versionNumbersProvider
4456
*
45-
* @param string $savedVersion The saved version number.
46-
* @param string $currentVersion The current version number.
47-
* @param bool $expected The expected value.
57+
* @param int $savedTime The saved time.
58+
* @param int $currentTime The current time.
59+
* @param bool $expected The expected value.
4860
*
4961
* @covers Whip_MessageDismisser::__construct()
5062
* @covers Whip_MessageDismisser::isDismissed()
51-
* @covers Whip_MessageDismisser::toMajorVersion()
5263
*/
53-
public function testIsDismissibleWithVersions( $savedVersion, $currentVersion, $expected ) {
64+
public function testIsDismissibleWithVersions( $savedTime, $currentTime, $expected ) {
5465
$storage = new Whip_DismissStorageMock();
55-
$storage->set( $savedVersion );
56-
$dismisser = new Whip_MessageDismisser( $currentVersion, $storage );
66+
$storage->set( $savedTime );
67+
$dismisser = new Whip_MessageDismisser( $currentTime, WEEK_IN_SECONDS * 4, $storage );
5768

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

72+
/**
73+
* Provides array with test values.
74+
*
75+
* @return array
76+
*/
6177
public function versionNumbersProvider() {
6278
return array(
63-
array( '4.8', '4.8', true ),
64-
array( '4.8', '4.8.1', true ),
65-
array( '4.7', '4.8', false ),
66-
array( '4.7', '4.8.1', false ),
67-
array( '4.7.1', '4.8.1', false ),
68-
array( '4.7', '4.7-alpha', true ),
79+
array( strtotime( "-2weeks" ), time(), true ),
80+
array( strtotime( "-4weeks" ), time(), true ),
81+
array( strtotime( "-6weeks" ), time(), false ),
82+
array( time(), time(), true ),
6983
);
7084
}
7185

0 commit comments

Comments
 (0)