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 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 new file mode 100644 index 0000000..c18c59f --- /dev/null +++ b/spec/WPUP/Minimum/NoticeSpec.php @@ -0,0 +1,18 @@ +beConstructedWith('5.4.0', 'Test Plugin'); + } + + 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 new file mode 100644 index 0000000..e8355e4 --- /dev/null +++ b/spec/WPUP/Recommended/NoticeSpec.php @@ -0,0 +1,18 @@ +beConstructedWith('5.4.0', 'Test Plugin'); + } + + 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 96fdeee..54ccb66 100644 --- a/spec/WPUpdatePhpSpec.php +++ b/spec/WPUpdatePhpSpec.php @@ -1,45 +1,27 @@ beConstructedWith( '5.4.0', '5.3.0' ); - } +use PhpSpec\ObjectBehavior; - function it_can_run_on_minimum_version() { - $this->does_it_meet_required_php_version( '5.4.0' )->shouldReturn( true ); - } +class WPUpdatePhpSpec extends ObjectBehavior { + function let() { + $this->beConstructedWith( '5.4.0', '5.3.0' ); + } - function it_passes_the_recommended_version() { - $this->does_it_meet_recommended_php_version( '5.3.0' )->shouldReturn( true ); - } + function it_can_run_on_minimum_version() { + $this->does_it_meet_required_php_version( '5.4.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_passes_the_recommended_version() { + $this->does_it_meet_recommended_php_version( '5.3.0' )->shouldReturn( true ); + } - function it_fails_the_recommended_version() { - $this->does_it_meet_recommended_php_version( '5.2.9' )->shouldReturn( false ); - } + function it_will_not_run_on_old_version() { + $this->does_it_meet_required_php_version( '5.2.4' )->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'); - } + function it_fails_the_recommended_version() { + $this->does_it_meet_recommended_php_version( '5.2.9' )->shouldReturn( false ); } } \ No newline at end of file diff --git a/spec/bootstrap.php b/spec/bootstrap.php new file mode 100644 index 0000000..9bc4061 --- /dev/null +++ b/spec/bootstrap.php @@ -0,0 +1,13 @@ +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..28166fd --- /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..2b969ce --- /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.'; + } +} 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 '
'; - } }