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

Notices refactor to multiple classes #41

Merged
merged 8 commits into from
Jul 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions phpspec.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bootstrap: spec/bootstrap.php
18 changes: 18 additions & 0 deletions spec/WPUP/Minimum/NoticeSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace spec;

use PhpSpec\ObjectBehavior;

class WPUP_Minimum_NoticeSpec extends ObjectBehavior
{
function let()
{
$this->beConstructedWith('5.4.0', 'Test Plugin');
}

function it_adds_plugin_name_to_admin_notice()
{
$this->getNoticeText()->shouldMatch('/Test Plugin/i');
}
}
18 changes: 18 additions & 0 deletions spec/WPUP/Recommended/NoticeSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace spec;

use PhpSpec\ObjectBehavior;

class WPUP_Recommended_NoticeSpec extends ObjectBehavior
{
function let()
{
$this->beConstructedWith('5.4.0', 'Test Plugin');
}

function it_adds_plugin_name_to_admin_notice()
{
$this->getNoticeText()->shouldMatch('/Test Plugin/i');
}
}
52 changes: 17 additions & 35 deletions spec/WPUpdatePhpSpec.php
Original file line number Diff line number Diff line change
@@ -1,45 +1,27 @@
<?php

namespace {
function add_action( $hook, $callback ) {
return true;
}

function is_admin() {
return true;
}
}

namespace spec {

use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
namespace spec;

class WPUpdatePhpSpec extends ObjectBehavior {
function let() {
$this->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 );
}
}
13 changes: 13 additions & 0 deletions spec/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

function esc_url( $string ) {
return $string;
}

function add_action( $hook, $callback ) {
return true;
}

function is_admin() {
return true;
}
19 changes: 19 additions & 0 deletions src/Notices/Abstract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

abstract class WPUP_Notice implements WPUP_Notice_Interface
{
protected $version;
protected $plugin_name;
protected $url = 'http://wpupdatephp.com/update/';

public function __construct( $version, $plugin_name = NULL )
{
$this->version = $version;
$this->plugin_name = $plugin_name;
}

public function display()
{
return '<div class="error">' . $this->getNoticeText() . '</div>';
}
}
6 changes: 6 additions & 0 deletions src/Notices/Interface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

interface WPUP_Notice_Interface
{
public function getNoticeText();
}
11 changes: 11 additions & 0 deletions src/Notices/Minimum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

class WPUP_Minimum_Notice extends WPUP_Notice
{
public 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 <a href="' . esc_url( $this->url ) . '">how you can update</a>.';
}
}
11 changes: 11 additions & 0 deletions src/Notices/Recommended.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

class WPUP_Recommended_Notice extends WPUP_Notice
{
public function getNoticeText()
{
$plugin_name = $this->plugin_name ? $this->plugin_name : 'This plugin';

return $plugin_name . ' recommends a PHP version higher than ' . $this->version . '. Read more information about <a href="' . esc_url( $this->url ) . '">how you can update</a>.';
}
}
56 changes: 4 additions & 52 deletions src/WPUpdatePhp.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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 '<p>' . $this->plugin_name . ' recommends a PHP version higher than ' . $this->recommended_version . '. Read more information about <a href="http://www.wpupdatephp.com/update/">how you can update</a>.</p>';
} else {
return '<p>This plugin recommends a PHP version higher than ' . $this->recommended_version . '. Read more information about <a href="http://www.wpupdatephp.com/update/">how you can update</a>.</p>';
}
}

if ( ! empty( $this->plugin_name ) ) {
return '<p>Unfortunately, ' . $this->plugin_name . ' cannot run on PHP versions older than ' . $this->minimum_version . '. Read more information about <a href="http://www.wpupdatephp.com/update/">how you can update</a>.</p>';
} else {
return '<p>Unfortunately, this plugin cannot run on PHP versions older than ' . $this->minimum_version . '. Read more information about <a href="http://www.wpupdatephp.com/update/">how you can update</a>.</p>';
}
}

/**
* 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 '<div class="error">';
echo $this->get_admin_notice( 'minimum' );
echo '</div>';
}

/**
* 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 '<div class="error">';
echo $this->get_admin_notice( 'recommended' );
echo '</div>';
}
}