2017 © Pedro Peláez
 

library wp-requirements

Helper for declaring dependencies in a WordPress plugin.

image

ssnepenthe/wp-requirements

Helper for declaring dependencies in a WordPress plugin.

  • Friday, April 14, 2017
  • by ssnepenthe
  • Repository
  • 1 Watchers
  • 2 Stars
  • 435 Installations
  • PHP
  • 6 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 1 % Grown

The README.md

wp-requirements

Helper for declaring dependencies in a WordPress plugin., (*1)

Requirements

WordPress, PHP 5.3 or later and Composer., (*2)

Installation

Install using Composer:, (*3)

$ composer require ssnepenthe/wp-requirements

Usage

This package provides a simple method for ensuring that WordPress plugins fail gracefully down to PHP 5.3., (*4)

Create a checker instance in your main plugin file (e.g. my-plugin/my-plugin.php):, (*5)

use WP_Requirements\Plugin_Checker;

$checker = new Plugin_Checker( 'My Awesome Plugin', __FILE__ );

Where the first parameter is the name of your plugin (used for notifications when requirements are not met) and the second parameter is the path to your main plugin file (used to deactivate the plugin)., (*6)

Then add any number of requirements. All of the following are valid:, (*7)

// Verify that the Debug_Bar class exists - an indirect way of verifying that the Debug_Bar plugin is active.
$checker->class_exists( 'Debug_Bar' );

// Verify that the DOM extension is loaded.
$checker->extension_loaded( 'dom' );

// Verify that the cmb2_bootstrap() function exists - an indirect way of verifying that the CMB2 plugin is active.
$checker->function_exists( 'cmb2_bootstrap' );

// Verify that the server has PHP 5.4 or greater.
$checker->php_at_least( '5.4' );

// Verify that Hello Dolly is active.
// First parameter is plugin path relative to the plugin directory.
// Second parameter is plugin name used for label when requirement is not met.
$checker->plugin_active( 'hello.php', 'Hello Dolly' );

// Verify that the server has WordPress 4.7 or greater.
$checker->wp_at_least( '4.7' );

// Check any arbitrary condition.
// First parameter is a closure that should return true when the requirement is met, false otherwise.
// Second parameter is a message to display when the requirement is not met. Note that it will be prefixed with '{plugin name} deactivated: ' when it is displayed.
$checker->add_check(
    function() {
        return defined( 'SOME_CONSTANT' ) && SOME_CONSTANT;
    },
    'SOME_CONSTANT must be defined and truthy'
);

The Plugin_Checker class also provides a fluent interface:, (*8)

use WP_Requirements\Plugin_Checker;

$checker = Plugin_Checker::make( 'My Awesome Plugin', __FILE__ )
    ->function_exists( 'cmb2_bootstrap' )
    ->php_at_least( '5.6' )
    ->wp_at_least( '4.7' );

Finally, verify all requirements are met and bootstrap your plugin accordingly., (*9)

if ( $checker->requirements_met() ) {
    // Whatever logic is required to bootstrap your plugin.
    // This should mostly take place outside of this file to minimize risk of errors when requirements are not met.
    $plugin = new My_Awesome_Plugin_Bootstrap;
    $plugin->init();
} else {
    // This method hooks in to 'admin_notices' to inform the user which requirements weren't met and 'admin_init' to actually deactivate the plugin.
    $checker->deactivate_and_notify();
}

The Versions

14/04 2017

dev-master

9999999-dev

Helper for declaring dependencies in a WordPress plugin.

  Sources   Download

GPL-2.0

The Requires

  • php >=5.3

 

14/04 2017

0.1.0

0.1.0.0

Helper for declaring dependencies in a WordPress plugin.

  Sources   Download

GPL-2.0

The Requires

  • php >=5.3