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

Big 2.0 release is almost ready to go #43

Open
wants to merge 33 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
cb190a9
add .gitattributes
afragen Jul 5, 2016
45e47f7
Class structure for notices
coenjacobs Apr 6, 2017
086bc76
Implement new notices class structure
coenjacobs Apr 6, 2017
db60469
Fixed typo missing quote in notice string
coenjacobs Apr 6, 2017
6775397
Interface methods should be public
coenjacobs Apr 6, 2017
646da4e
Update README file to reflect 5.6 minimum
coenjacobs Jul 14, 2017
8edd16d
Fixed tests by testing new notice object
coenjacobs Jul 16, 2017
f03f74b
Move mocked WordPress functions to spec bootstrap file
coenjacobs Jul 16, 2017
7b5b429
Cleaned up spec namespace formatting
coenjacobs Jul 16, 2017
0e420e5
Merge pull request #41 from WPupdatePHP/notices-refactor
coenjacobs Jul 16, 2017
dbeb00f
Basic translator class for pushing plugin strings in
coenjacobs Aug 11, 2017
b8971e4
Spec for the new translator class
coenjacobs Aug 11, 2017
ff58147
Introduce the translator logic in all notices
coenjacobs Aug 11, 2017
7693dd5
Updated specs to use new translator strings
coenjacobs Aug 11, 2017
7cd9152
Fix indenting
coenjacobs Aug 11, 2017
02ef4a9
Suggest Mozart to install this library with
coenjacobs Aug 11, 2017
01767a2
Added travis configuration file
coenjacobs Aug 11, 2017
0d4df26
Make plugin name mandatory argument in library constructor
coenjacobs Aug 14, 2017
1b29111
Merge pull request #42 from WPupdatePHP/translations
coenjacobs Aug 14, 2017
cba6d16
Merge remote-tracking branch 'afragen/add-gitattributes' into big-2.0…
coenjacobs Aug 14, 2017
11bd745
Replace translator spec with phpunit test
coenjacobs Aug 17, 2017
4f01a02
Fall back to PHP 5.2 compatible phpunit version
coenjacobs Aug 18, 2017
cdbbd1d
PHP 5.2 compatible Travis configuration
coenjacobs Aug 24, 2017
02d7f9b
Merge pull request #44 from WPupdatePHP/phpunit
coenjacobs Aug 25, 2017
8e733dc
Checker class that handles all check arguments
coenjacobs Aug 25, 2017
e777767
Implementation of basic version checks
coenjacobs Aug 25, 2017
304b7aa
Tests to verify the version checks work
coenjacobs Aug 25, 2017
612083c
New check method on main object to start checks
coenjacobs Aug 25, 2017
e79e0ab
Move match_version property to central version checker
coenjacobs Aug 25, 2017
1f42bcf
Remove example arguments array comments
coenjacobs Aug 25, 2017
97bc2c1
Updated readme for new arguments and check method
coenjacobs Aug 25, 2017
4b5c7c0
Readme now promotes installing this library through Composer/Mozart
coenjacobs Aug 25, 2017
d926bea
Merge pull request #45 from WPupdatePHP/dynamic-checks
coenjacobs Aug 25, 2017
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
6 changes: 6 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
* text=auto

.gitattributes export-ignore
.gitignore export-ignore
composer.json export-ignore
/spec export-ignore
27 changes: 27 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
language: php
dist: trusty
php:
- '7.1'
- '7.0'
- '5.6'
- '5.5'
- '5.4'

matrix:
fast_finish: true
include:
- php: 5.2
dist: precise
- php: 5.3
dist: precise


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

script:
- if [[ "$TRAVIS_PHP_VERSION" == "5.2" ]]; then phpunit; else ./vendor/bin/phpunit; fi
61 changes: 44 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,66 @@ We recommend installing the library using [Composer](https://getcomposer.org/),
composer require wpupdatephp/wp-update-php
```

Another option is to download the [class file](https://github.com/WPupdatePHP/wp-update-php/blob/master/src/WPUpdatePhp.php) manually.
To prevent collisions, you can install this library inside your own prefixed class names (eg `CJ_WPUpdatePhp`), using [Mozart](https://github.com/coenjacobs/mozart) and we **highly recommend doing this** as it's currently the only way to prevent hard to debug conflicts.

## Usage
Usage of this library depends on how you start your plugin. The core `does_it_meet_required_php_version` method does all the checking for you and adds an admin notice in case the version requirement is not met.
Usage of this library depends on how you start your plugin. The core `check()` method does all the checking for you. You can output an admin notice in case the version requirement is not met.

For example, when you start your plugin by instantiating a new object, you should wrap a conditional check around it.
For example, when you start your plugin by instantiating a new object, you should wrap a conditional check around it.

_Example:_

```php
$updatePhp = new WPUpdatePhp( '5.4.0' );

if ( $updatePhp->does_it_meet_required_php_version() ) {
$arguments = array(
'php' => array(
array(
'version' => '7.0.0',
'required' => false,
),
array(
'version' => '5.6.0',
'required' => true,
),
),
);
$updatePhp = new WPUpdatePhp( 'Plugin Name', $arguments );
$updatePhp->check();

if ( $updatePhp->passes() ) {
// Instantiate new object here
}

// The version check has failed, an admin notice has been thrown
// The version check has failed, an admin notice can be shown
```

## Including the library file
Adding the library via Composer has preference. The Composer autoloader will automatically take care of preventing including two classes with the same name.

In case you want to include the file manually, please wrap the include or require call in a [`class_exists`](http://php.net/class_exists) conditional, like so:
## Available arguments
The full list of available arguments:

```php
if ( ! class_exists( 'WPUpdatePhp' ) ) {
// do the file include or require here
}
$arguments = array(
'wordpress' => array(
array(
'version' => '3.7.1',
'required' => false,
),
array(
'version' => '3.5.0',
'required' => true,
),
),
'php' => array(
array(
'version' => '7.0.0',
'required' => false,
),
array(
'version' => '5.6.0',
'required' => true,
),
),
);
```

## Setting the name of the plugin
The notice that will be thrown can also contain the name of the plugin. Use the `set_plugin_name( $name )` method on the `WPUpdatePhp` object to provide the name. This call needs to be made before the `does_it_meet_required_php_version()` method is called to check versions.

## License
(GPLv2 license or later)

Expand Down
23 changes: 21 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,30 @@
"issues": "https://github.com/wpupdatephp/wp-update-php/issues",
"source": "https://github.com/wpupdatephp/wp-update-php"
},
"require": {
"php": ">=5.2.4",
"xrstf/composer-php52": "^1.0"
},
"require-dev": {
"phpspec/phpspec": "~2.0"
"phpspec/phpspec": "~2.0",
"phpunit/phpunit": "^3.6.12"
},
"suggest": {
"coenjacobs/mozart": "Easily wrap this library with your own prefix, to prevent collisions when multiple plugins use this library"
},
"autoload": {
"classmap": ["src"]
},
"license": "GPL-2.0+"
"license": "GPL-2.0+",
"scripts": {
"post-install-cmd": [
"xrstf\\Composer52\\Generator::onPostInstallCmd"
],
"post-update-cmd": [
"xrstf\\Composer52\\Generator::onPostInstallCmd"
],
"post-autoload-dump": [
"xrstf\\Composer52\\Generator::onPostInstallCmd"
]
}
}
17 changes: 17 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="tests/bootstrap.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false">
<testsuites>
<testsuite name="unit">
<directory>./tests/unit/</directory>
</testsuite>
</testsuites>
</phpunit>
45 changes: 0 additions & 45 deletions spec/WPUpdatePhpSpec.php

This file was deleted.

51 changes: 51 additions & 0 deletions src/Checker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

class WPUP_Checker
{
/** @var array */
public $checks = array();

/** @var array */
public $arguments = array();

/** @var array */
public $failures = array();

/** @var array */
public $recommendations = array();

public function __construct($arguments = array())
{
$this->arguments = $arguments;
}

public function setup()
{
$this->checks = array(
new WPUP_PhpVersion($this),
new WPUP_WordPressVersion($this),
);

foreach($this->checks as $check) {
$check->setup();
}
}

public function check()
{
/** @var WPUP_Check_Interface $check */
foreach($this->checks as $check) {
$check->check();
}
}

public function reportFail($failure)
{
$this->failures[] = $failure;
}

public function reportRecommendation($recommendation)
{
$this->recommendations[] = $recommendation;
}
}
8 changes: 8 additions & 0 deletions src/Checks/Interface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

interface WPUP_Check
{
public function __construct(WPUP_Checker $checker);
public function setup();
public function check();
}
12 changes: 12 additions & 0 deletions src/Checks/PhpVersion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

class WPUP_PhpVersion extends WPUP_VersionCheck
{
/** @var string */
public $arguments_key = 'php';

public function setup()
{
$this->match_version = PHP_VERSION;
}
}
61 changes: 61 additions & 0 deletions src/Checks/VersionCheck.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

abstract class WPUP_VersionCheck implements WPUP_Check
{
/** @var WPUP_Checker */
public $checker;

/** @var array */
public $arguments;

/** @var string */
public $arguments_key;

/** @var string */
public $match_version;

public function __construct(WPUP_Checker $checker)
{
$this->arguments = $checker->arguments;
$this->checker = $checker;
}

public function checkArguments($arguments, $key)
{
if (!isset($arguments[$key])) {
return false;
}

if (!is_array($arguments[$key])) {
return false;
}

return true;
}

public function check()
{
if ( ! $this->checkArguments($this->arguments, $this->arguments_key )) {
return;
}

$this->checkVersions($this->arguments[$this->arguments_key]);
}

public function checkVersions($arguments)
{
foreach ($arguments as $argument) {
if (!is_array($argument)) {
continue;
}

if (version_compare($this->match_version, $argument['version']) === -1) {
if ($argument['required'] === true) {
$this->checker->reportFail($argument);
} else {
$this->checker->reportRecommendation($argument);
}
}
}
}
}
13 changes: 13 additions & 0 deletions src/Checks/WordPressVersion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

class WPUP_WordPressVersion extends WPUP_VersionCheck
{
/** @var string */
public $arguments_key = 'wordpress';

public function setup()
{
global $wp_version;
$this->match_version = $wp_version;
}
}
30 changes: 30 additions & 0 deletions src/Notices/Abstract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

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

/** @var WPUP_Translator */
protected $translator;

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

/**
* @param $translator WPUP_Translator
*/
public function setTranslator($translator )
{
$this->translator = $translator;
}

public function display()
{
return '<div class="error">' . $this->getNoticeText() . '</div>';
}
}
Loading