From 45e47f7cbfc7f93538ae6f3c89b3af8b1c13bb87 Mon Sep 17 00:00:00 2001 From: Coen Jacobs Date: Thu, 6 Apr 2017 09:57:10 +0200 Subject: [PATCH 1/8] Class structure for notices --- src/Notices/Abstract.php | 19 +++++++++++++++++++ src/Notices/Interface.php | 6 ++++++ src/Notices/Minimum.php | 11 +++++++++++ src/Notices/Recommended.php | 11 +++++++++++ 4 files changed, 47 insertions(+) create mode 100644 src/Notices/Abstract.php create mode 100644 src/Notices/Interface.php create mode 100644 src/Notices/Minimum.php create mode 100644 src/Notices/Recommended.php diff --git a/src/Notices/Abstract.php b/src/Notices/Abstract.php new file mode 100644 index 0000000..70c55ff --- /dev/null +++ b/src/Notices/Abstract.php @@ -0,0 +1,19 @@ +version = $version; + $this->plugin_name = $plugin_name; + } + + public function display() + { + return '
' . $this->getNoticeText() . '
'; + } +} diff --git a/src/Notices/Interface.php b/src/Notices/Interface.php new file mode 100644 index 0000000..dd04b5f --- /dev/null +++ b/src/Notices/Interface.php @@ -0,0 +1,6 @@ +plugin_name ? $this->plugin_name : 'this plugin'; + + return 'Unfortunately, ' . $plugin_name . ' cannot run on PHP versions older than ' . $this->version . '. Read more information about how you can update. + } +} diff --git a/src/Notices/Recommended.php b/src/Notices/Recommended.php new file mode 100644 index 0000000..6ee1a08 --- /dev/null +++ b/src/Notices/Recommended.php @@ -0,0 +1,11 @@ +plugin_name ? $this->plugin_name : 'This plugin'; + + return $plugin_name . ' recommends a PHP version higher than ' . $this->version . '. Read more information about how you can update.'; + } +} From 086bc766e411e9d53abe594fc844c51bc5e152d2 Mon Sep 17 00:00:00 2001 From: Coen Jacobs Date: Thu, 6 Apr 2017 10:03:14 +0200 Subject: [PATCH 2/8] Implement new notices class structure --- src/WPUpdatePhp.php | 56 ++++----------------------------------------- 1 file changed, 4 insertions(+), 52 deletions(-) diff --git a/src/WPUpdatePhp.php b/src/WPUpdatePhp.php index 16a21bb..a33e461 100644 --- a/src/WPUpdatePhp.php +++ b/src/WPUpdatePhp.php @@ -52,7 +52,8 @@ public function does_it_meet_required_php_version( $version = PHP_VERSION ) { return true; } - $this->load_version_notice( array( $this, 'minimum_admin_notice' ) ); + $notice = new WPUP_Minimum_Notice( $this->minimum_version, $this->plugin_name ); + $this->load_version_notice( array( $notice, 'display' ) ); return false; } @@ -69,7 +70,8 @@ public function does_it_meet_recommended_php_version( $version = PHP_VERSION ) { return true; } - $this->load_version_notice( array( $this, 'recommended_admin_notice' ) ); + $notice = new WPUP_Recommended_Notice( $this->recommended_version, $this->plugin_name ); + $this->load_version_notice( array( $notice, 'display' ) ); return false; } @@ -95,54 +97,4 @@ private function load_version_notice( $callback ) { add_action( 'network_admin_notices', $callback ); } } - - /** - * Return the string to be shown in the admin notice. - * - * This is based on the level (`recommended` or default `minimum`) of the - * notice. This will also add the plugin name to the notice string, if set. - * - * @param string $level Optional. Admin notice level, `recommended` or `minimum`. - * Default is `minimum`. - * @return string - */ - public function get_admin_notice( $level = 'minimum' ) { - if ( 'recommended' === $level ) { - if ( ! empty( $this->plugin_name ) ) { - return '

' . $this->plugin_name . ' recommends a PHP version higher than ' . $this->recommended_version . '. Read more information about how you can update.

'; - } else { - return '

This plugin recommends a PHP version higher than ' . $this->recommended_version . '. Read more information about how you can update.

'; - } - } - - if ( ! empty( $this->plugin_name ) ) { - return '

Unfortunately, ' . $this->plugin_name . ' cannot run on PHP versions older than ' . $this->minimum_version . '. Read more information about how you can update.

'; - } else { - return '

Unfortunately, this plugin cannot run on PHP versions older than ' . $this->minimum_version . '. Read more information about how you can update.

'; - } - } - - /** - * Method hooked into admin_notices when minimum required PHP version is not - * available to show this in a notice. - * - * @hook admin_notices - */ - public function minimum_admin_notice() { - echo '
'; - echo $this->get_admin_notice( 'minimum' ); - echo '
'; - } - - /** - * Method hooked into admin_notices when recommended PHP version is not - * available to show this in a notice. - * - * @hook admin_notices - */ - public function recommended_admin_notice() { - echo '
'; - echo $this->get_admin_notice( 'recommended' ); - echo '
'; - } } From db604693c09a1f1551a1982685ec23a8f1a41c1a Mon Sep 17 00:00:00 2001 From: Coen Jacobs Date: Thu, 6 Apr 2017 10:10:41 +0200 Subject: [PATCH 3/8] Fixed typo missing quote in notice string --- src/Notices/Minimum.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Notices/Minimum.php b/src/Notices/Minimum.php index c36efe4..57eea34 100644 --- a/src/Notices/Minimum.php +++ b/src/Notices/Minimum.php @@ -6,6 +6,6 @@ protected function getNoticeText() { $plugin_name = $this->plugin_name ? $this->plugin_name : 'this plugin'; - return 'Unfortunately, ' . $plugin_name . ' cannot run on PHP versions older than ' . $this->version . '. Read more information about how you can update. + return 'Unfortunately, ' . $plugin_name . ' cannot run on PHP versions older than ' . $this->version . '. Read more information about how you can update.'; } } From 67753974418e7379fb33c7036c93140ca9005528 Mon Sep 17 00:00:00 2001 From: Coen Jacobs Date: Thu, 6 Apr 2017 10:13:03 +0200 Subject: [PATCH 4/8] Interface methods should be public --- src/Notices/Interface.php | 2 +- src/Notices/Minimum.php | 2 +- src/Notices/Recommended.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Notices/Interface.php b/src/Notices/Interface.php index dd04b5f..28166fd 100644 --- a/src/Notices/Interface.php +++ b/src/Notices/Interface.php @@ -2,5 +2,5 @@ interface WPUP_Notice_Interface { - protected function getNoticeText(); + public function getNoticeText(); } diff --git a/src/Notices/Minimum.php b/src/Notices/Minimum.php index 57eea34..5c20c95 100644 --- a/src/Notices/Minimum.php +++ b/src/Notices/Minimum.php @@ -2,7 +2,7 @@ class WPUP_Minimum_Notice extends WPUP_Notice { - protected function getNoticeText() + public function getNoticeText() { $plugin_name = $this->plugin_name ? $this->plugin_name : 'this plugin'; diff --git a/src/Notices/Recommended.php b/src/Notices/Recommended.php index 6ee1a08..2b969ce 100644 --- a/src/Notices/Recommended.php +++ b/src/Notices/Recommended.php @@ -2,7 +2,7 @@ class WPUP_Recommended_Notice extends WPUP_Notice { - protected function getNoticeText() + public function getNoticeText() { $plugin_name = $this->plugin_name ? $this->plugin_name : 'This plugin'; From 646da4e8df1b06a95b191f48e39ed744db3e3c17 Mon Sep 17 00:00:00 2001 From: Coen Jacobs Date: Fri, 14 Jul 2017 18:39:15 +0200 Subject: [PATCH 5/8] Update README file to reflect 5.6 minimum --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 62d8dc5..0974c68 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ For example, when you start your plugin by instantiating a new object, you shoul _Example:_ ```php -$updatePhp = new WPUpdatePhp( '5.4.0' ); +$updatePhp = new WPUpdatePhp( '5.6.0' ); if ( $updatePhp->does_it_meet_required_php_version() ) { // Instantiate new object here From 8edd16ddce9e2c7324d860e171259aab6e3fa6cc Mon Sep 17 00:00:00 2001 From: Coen Jacobs Date: Sun, 16 Jul 2017 15:44:14 +0200 Subject: [PATCH 6/8] Fixed tests by testing new notice object --- spec/WPUP/Minimum/NoticeSpec.php | 25 +++++++++++++++++++++++++ spec/WPUP/Recommended/NoticeSpec.php | 19 +++++++++++++++++++ spec/WPUpdatePhpSpec.php | 7 ------- 3 files changed, 44 insertions(+), 7 deletions(-) create mode 100644 spec/WPUP/Minimum/NoticeSpec.php create mode 100644 spec/WPUP/Recommended/NoticeSpec.php diff --git a/spec/WPUP/Minimum/NoticeSpec.php b/spec/WPUP/Minimum/NoticeSpec.php new file mode 100644 index 0000000..92241a6 --- /dev/null +++ b/spec/WPUP/Minimum/NoticeSpec.php @@ -0,0 +1,25 @@ +beConstructedWith('5.4.0', 'Test Plugin'); + } + + function it_adds_plugin_name_to_admin_notice() + { + $this->getNoticeText()->shouldMatch('/Test Plugin/i'); + } + } +} diff --git a/spec/WPUP/Recommended/NoticeSpec.php b/spec/WPUP/Recommended/NoticeSpec.php new file mode 100644 index 0000000..3bed0f5 --- /dev/null +++ b/spec/WPUP/Recommended/NoticeSpec.php @@ -0,0 +1,19 @@ +beConstructedWith('5.4.0', 'Test Plugin'); + } + + function it_adds_plugin_name_to_admin_notice() + { + $this->getNoticeText()->shouldMatch('/Test Plugin/i'); + } + } +} diff --git a/spec/WPUpdatePhpSpec.php b/spec/WPUpdatePhpSpec.php index 96fdeee..2007e19 100644 --- a/spec/WPUpdatePhpSpec.php +++ b/spec/WPUpdatePhpSpec.php @@ -13,7 +13,6 @@ function is_admin() { namespace spec { use PhpSpec\ObjectBehavior; - use Prophecy\Argument; class WPUpdatePhpSpec extends ObjectBehavior { function let() { @@ -35,11 +34,5 @@ function it_will_not_run_on_old_version() { function it_fails_the_recommended_version() { $this->does_it_meet_recommended_php_version( '5.2.9' )->shouldReturn( false ); } - - function it_adds_plugin_name_to_admin_notice() { - $this->set_plugin_name( 'Test Plugin' ); - $this->get_admin_notice()->shouldMatch('/Test Plugin/i'); - $this->get_admin_notice( 'recommended' )->shouldMatch('/Test Plugin/i'); - } } } \ No newline at end of file From f03f74be3be519c379bdcd61344254dbd4192c5d Mon Sep 17 00:00:00 2001 From: Coen Jacobs Date: Sun, 16 Jul 2017 15:56:28 +0200 Subject: [PATCH 7/8] Move mocked WordPress functions to spec bootstrap file --- phpspec.yml | 1 + spec/WPUP/Minimum/NoticeSpec.php | 6 ------ spec/WPUpdatePhpSpec.php | 10 ---------- spec/bootstrap.php | 13 +++++++++++++ 4 files changed, 14 insertions(+), 16 deletions(-) create mode 100644 phpspec.yml create mode 100644 spec/bootstrap.php diff --git a/phpspec.yml b/phpspec.yml new file mode 100644 index 0000000..ccfd7f7 --- /dev/null +++ b/phpspec.yml @@ -0,0 +1 @@ +bootstrap: spec/bootstrap.php \ No newline at end of file diff --git a/spec/WPUP/Minimum/NoticeSpec.php b/spec/WPUP/Minimum/NoticeSpec.php index 92241a6..6e1d8df 100644 --- a/spec/WPUP/Minimum/NoticeSpec.php +++ b/spec/WPUP/Minimum/NoticeSpec.php @@ -1,11 +1,5 @@ Date: Sun, 16 Jul 2017 15:57:31 +0200 Subject: [PATCH 8/8] Cleaned up spec namespace formatting --- spec/WPUP/Minimum/NoticeSpec.php | 23 +++++++++--------- spec/WPUP/Recommended/NoticeSpec.php | 23 +++++++++--------- spec/WPUpdatePhpSpec.php | 35 ++++++++++++++-------------- 3 files changed, 39 insertions(+), 42 deletions(-) diff --git a/spec/WPUP/Minimum/NoticeSpec.php b/spec/WPUP/Minimum/NoticeSpec.php index 6e1d8df..c18c59f 100644 --- a/spec/WPUP/Minimum/NoticeSpec.php +++ b/spec/WPUP/Minimum/NoticeSpec.php @@ -1,19 +1,18 @@ beConstructedWith('5.4.0', 'Test Plugin'); - } + $this->beConstructedWith('5.4.0', 'Test Plugin'); + } - function it_adds_plugin_name_to_admin_notice() - { - $this->getNoticeText()->shouldMatch('/Test Plugin/i'); - } + function it_adds_plugin_name_to_admin_notice() + { + $this->getNoticeText()->shouldMatch('/Test Plugin/i'); } -} +} \ No newline at end of file diff --git a/spec/WPUP/Recommended/NoticeSpec.php b/spec/WPUP/Recommended/NoticeSpec.php index 3bed0f5..e8355e4 100644 --- a/spec/WPUP/Recommended/NoticeSpec.php +++ b/spec/WPUP/Recommended/NoticeSpec.php @@ -1,19 +1,18 @@ beConstructedWith('5.4.0', 'Test Plugin'); - } + $this->beConstructedWith('5.4.0', 'Test Plugin'); + } - function it_adds_plugin_name_to_admin_notice() - { - $this->getNoticeText()->shouldMatch('/Test Plugin/i'); - } + function it_adds_plugin_name_to_admin_notice() + { + $this->getNoticeText()->shouldMatch('/Test Plugin/i'); } -} +} \ No newline at end of file diff --git a/spec/WPUpdatePhpSpec.php b/spec/WPUpdatePhpSpec.php index db1abbd..54ccb66 100644 --- a/spec/WPUpdatePhpSpec.php +++ b/spec/WPUpdatePhpSpec.php @@ -1,28 +1,27 @@ beConstructedWith( '5.4.0', '5.3.0' ); - } +class WPUpdatePhpSpec extends ObjectBehavior { + function let() { + $this->beConstructedWith( '5.4.0', '5.3.0' ); + } - function it_can_run_on_minimum_version() { - $this->does_it_meet_required_php_version( '5.4.0' )->shouldReturn( true ); - } + function it_can_run_on_minimum_version() { + $this->does_it_meet_required_php_version( '5.4.0' )->shouldReturn( true ); + } - function it_passes_the_recommended_version() { - $this->does_it_meet_recommended_php_version( '5.3.0' )->shouldReturn( true ); - } + function it_passes_the_recommended_version() { + $this->does_it_meet_recommended_php_version( '5.3.0' )->shouldReturn( true ); + } - function it_will_not_run_on_old_version() { - $this->does_it_meet_required_php_version( '5.2.4' )->shouldReturn( false ); - } + function it_will_not_run_on_old_version() { + $this->does_it_meet_required_php_version( '5.2.4' )->shouldReturn( false ); + } - function it_fails_the_recommended_version() { - $this->does_it_meet_recommended_php_version( '5.2.9' )->shouldReturn( false ); - } + function it_fails_the_recommended_version() { + $this->does_it_meet_recommended_php_version( '5.2.9' )->shouldReturn( false ); } } \ No newline at end of file