GithubHelp home page GithubHelp logo

wp-php-rv's Introduction

WP PHP vX.x+ (Version Check w/ Dashboard Notice)

Stub for WordPress themes/plugins that require PHP vX.x+ (i.e. a minimum version that you define).


Example Usage in a Typical WordPress Theme/Plugin File

<?php
/*
	Plugin Name: My Plugin
	Plugin URI: http://example.com/my-plugin
	Description: Example plugin.
	Author: Example Author.
	Version: 0.1-alpha
	Author URI: http://example.com
	Text Domain: my-plugin
*/
$GLOBALS['wp_php_rv'] = '5.3'; // Require PHP vX.x+ (you configure this).
if(require('wp-php-rv/src/includes/check.php')) // `true` if running PHP vX.x+.
	require dirname(__FILE__).'/my-plugin-code.php'; // It's OK to load your plugin.
else wp_php_rv_notice(); // Creates a nice PHP vX.x+ dashboard notice for the site owner.

Alternate Approach

The check.php file will automatically return true upon using include() or require() in your scripts; i.e., iff the installation site is running PHP vX.x+ (as configured by $GLOBALS['wp_php_rv']). Otherwise it returns false. Therefore, the simplest way to run your check is to use if(require('wp-php-rv/src/includes/check.php')). However, you could also choose to do it this way.

<?php
$GLOBALS['wp_php_rv'] = '5.3'; // Require PHP vX.x+.
require 'wp-php-rv/src/includes/check.php'; // Include.

if(wp_php_rv()) // `true` if running PHP vX.x+.
	require dirname(__FILE__).'/my-plugin-code.php'; // It's OK to load your plugin.
else wp_php_rv_notice(); // Creates a nice PHP vX.x+ dashboard notice for the site owner.

Dashboard Notice that Calls your Software by Name

<?php
$GLOBALS['wp_php_rv'] = '5.3'; // Require PHP vX.x+.
if(require('wp-php-rv/src/includes/check.php')) // `true` if running PHP vX.x+.
	require dirname(__FILE__).'/my-plugin-code.php'; // It's OK to load your plugin.
else wp_php_rv_notice('My Plugin'); // Dashboard notice mentions your software specifically.

Note: If you omit the $brand_name argument, a default value is used instead. The default value is ucwords('[calling file basedir]'); e.g., if /my-plugin/stub.php calls wp-php-rv/src/includes/check.php, the default $software_name automatically becomes My Plugin. Nice!


What if multiple themes/plugins use this?

This is fine! :-) The wp-php-rv/src/includes/check.php file uses function_exists() as a wrapper; which allows it to be included any number of times, and by any number of plugins; and also from any number of locations. The only thing to remember, is that you MUST be sure to define $GLOBALS['wp_php_rv'] each time; i.e., each time you include('wp-php-rv/src/includes/check.php') or require('wp-php-rv/src/includes/check.php').

The point here, is that $GLOBALS['wp_php_rv'] defines a PHP version that is specific to your plugin requirements, so it should be defined explicitly by each plugin developer before they include('wp-php-rv/src/includes/check.php') or require('wp-php-rv/src/includes/check.php').


Can this just go at the top of my existing theme/plugin file?

No, there are two important things to remember.

  1. Don't forget to bundle a copy of the websharks/wp-php-rv repo with your theme/plugin. All you really need is the /src directory.
  2. Don't leave your existing code in the same file. Use this in a stub file that checks for PHP vX.x+ first (as seen in the examples above), BEFORE loading your code which depends on PHP vX.x+. Why? If you put a PHP vX.x+ check at the top of an existing PHP file, and that particular PHP file happens to contain code which is only valid in PHP v5.2 (for instance), it may still trigger a syntax error. For this reason, you should move your code into a separate file and create a stub file that checks for the existence of PHP vX.x+ first.

Can I test for required PHP extensions too?

Yes, $GLOBALS['wp_php_rv'] can be either a string with a required version, or an array with both a required version and a nested array of required PHP extensions. The easiest way to show how this works is by example (as seen below). Note that your array of required PHP extensions must be compatible with PHP's extension_loaded() function.

<?php
$GLOBALS['wp_php_rv']['min'] = '5.3';
$GLOBALS['wp_php_rv']['extensions'] = array('curl', 'mbstring');

if(require('wp-php-rv/src/includes/check.php')) // `true` if running PHP vX.x+ w/ all required extensions.
	require dirname(__FILE__).'/my-plugin-code.php'; // It's OK to load your plugin.
else wp_php_rv_notice('My Plugin'); // Dashboard notice mentions your software specifically.

What else can I test for with this system?

A compatible OS, a compatible PHP version, required bits, required PHP functions, required PHP extensions, and a compatible WP version.

<?php
$GLOBALS['wp_php_rv']['os'] = 'nix'; // Requires a Unix-like OS.
// This is one of two operating system identifiers (always in lowercase): `nix` or `win`
// As far as WP PHP RV is concerned, their OS is either `nix` (Unix-like) or `win` (Windows).
// If you only want to support Unix-like systems, set this to: `nix`, making Windows incompatible.
// If you only want to support Windows systems, set this to: `win`, making others incompatible.

$GLOBALS['wp_php_rv']['min'] = '5.3'; // Minimum PHP version.
$GLOBALS['wp_php_rv']['max'] = '7.0.4'; // Max compatible PHP version, if applicable.

$GLOBALS['wp_php_rv']['bits'] = 64; // e.g., 32 or 64 bit architecture.
$GLOBALS['wp_php_rv']['functions']  = array('eval'); // Functions (or constructs).
$GLOBALS['wp_php_rv']['extensions'] = array('curl', 'mbstring'); // See previous FAQ.

$GLOBALS['wp_php_rv']['wp']['min'] = '4.2'; // Minimum WP version.
$GLOBALS['wp_php_rv']['wp']['max'] = '4.5.2'; // Max compatible WP version, if applicable.

if(require('wp-php-rv/src/includes/check.php')) // `true` if no issue.
	require dirname(__FILE__).'/my-plugin-code.php'; // It's OK to load your plugin.
else wp_php_rv_notice('My Plugin'); // Dashboard notice mentions your software specifically.

PHP Compatibility

Please note that the WP PHP RV software itself requires PHP v5.2 or higher, the same as WordPress core.


Copyright: © 2015 WebSharks, Inc. (coded in the USA)

Released under the terms of the GNU General Public License.

wp-php-rv's People

Contributors

raamdev avatar

Stargazers

Andreas avatar

Watchers

James Cloos avatar Jason Caldwell avatar Cristián Lávaque avatar

Forkers

theperfectwill

wp-php-rv's Issues

Check for Installed PHP Extensions Too

A nice addition to this, would be the ability to require a certain set of PHP extensions too; i.e. mysqli,curl could be listed as requirements, in addition to having PHP vX.x+.

Warning: array_merge(): Argument #1 is not an array in src/includes/functions/utils.php on line 81

Got this while testing the WooCommerce Intercom plugin... not sure if this is a false-positive or an actual bug, but I'm opening an issue just in case. It looks like WooCommerce Intercom is configured to use the dev version of WP PHP RV, which is a bad thing (i.e., it's using an unreleased version). I see this was inherited from the WP Sharks Skeleton repo, which is also configured to have Composer install the dev version.

( ! ) Warning: array_merge(): Argument #1 is not an array in /srv/www/wordpress-default/wp-content/plugins/woocommerce-intercom-pro/src/vendor/websharks/wp-php-rv/src/includes/functions/utils.php on line 81
Call Stack
#   Time    Memory  Function    Location
1   0.2143  363648  {main}( )   .../plugin-install.php:0
2   0.2155  364320  require_once( '/srv/www/wordpress-default/wp-admin/admin.php' ) .../plugin-install.php:15
3   0.2162  364800  require_once( '/srv/www/wordpress-default/wp-load.php' )    .../admin.php:31
4   0.2168  365600  require_once( '/srv/www/wordpress-default/wp-config.php' )  .../wp-load.php:39
5   0.2171  367000  require_once( '/srv/www/wordpress-default/wp-settings.php' )    .../wp-config.php:56
6   0.3699  2980208 include_once( '/srv/www/wordpress-default/wp-content/plugins/wp-sharks-core/plugin.php' )   .../wp-settings.php:273
7   0.3748  2980856 require( '/srv/www/wordpress-default/wp-content/plugins/wp-sharks-core/src/vendor/websharks/wp-php-rv/src/includes/check.php' ) .../plugin.php:27
8   0.3748  2980480 wp_php_rv( )    .../check.php:17
9   0.3748  2980480 ___wp_php_rv_issue( )   .../checks.php:20
10  0.3748  2980480 ___wp_php_rv_can_call_func( string(4) ) .../checks.php:61
11  0.3749  2983576 array_merge ( null, array(21) ) .../utils.php:81

Allow OS Detection

It would be a nice addition if WP PHP RV could also be told to require a specific OS. For instance, some plugins may support a variety of platforms, but wish to exclude support for Windows servers.

Ability to Specify Feature-Specific Extension Requirements

It may be desirable, in some scenarios, to specify a list of PHP extensions required for specific features and if those extensions are disabled, provide a dismissible dashboard message that doesn't prevent the software from running.

For example, the xmlreader extension is required for the Quick Cache Pro Auto-Cache Engine feature, however if that extension has been disabled by a web host (it's enabled by default in PHP v5.1.2+), it would be nice to provide the site owner with this information, warning them that the Auto-Cache Engine will not work:

Warning: Auto-Cache Engine requires PHP extension xmlreader, but it is not installed or it has been disabled by your web host.

If desired, a plugin author could then combine WP-PHP-RV with some code that disables certain options panels (e.g., the Auto-Cache Engine options panel) when all the required extensions are not installed.

Related to #6 (Add Ability to Check for Disabled Functions).

Clarify upgrade message

To remove this message, please upgrade.

Should be:

To remove this message, please upgrade PHP.

Add Ability to Check for Disabled Functions

A nice addition to this would be to add the ability to check for disabled functions like ini_set() or eval(). Functions in PHP can be disabled either via INI disable_functions or suhosin.executor.func.blacklist. Some hosting companies use these flags to limit functionality as a security measure.

Along with checking things of this nature, it would be nice if WP PHP RV supported the ability for a developer to allow certain scenarios to be dismissed by a site owner; e.g. if ini_set() is disabled, we could issue a dismissable notice where a site owner can choose to ignore the warning, and the software would be capable of working with or without this available.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.