GithubHelp home page GithubHelp logo

luizbills / wp-plugin-base Goto Github PK

View Code? Open in Web Editor NEW
13.0 3.0 1.0 323 KB

Powerful opinated boilerplate for WordPress plugins :electric_plug:

Home Page: https://github.com/luizbills/wp-plugin-base

License: GNU General Public License v3.0

PHP 82.43% Shell 17.57%
wordpress wordpress-boilerplate wordpress-development wordpress-plugin composer php classicpress

wp-plugin-base's People

Contributors

luizbills avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

nandogoulartt

wp-plugin-base's Issues

Auto run __activation and __deactivation methods

Proposal

<?php

namespace Your_Namespace;

use Your_Namespace\Helpers as h;

final class My_Class {
	public static function __activation () {
		// This STATIC method is called automatically on plugin activation.
	}

	public static function __deactivation () {
		// This STATIC method is called automatically on plugin deactivation (not uninstall/deletation).
	}
}

Put the most important helpers in core

Proposal #1

// /classes/Helpers.php
class Helpers extends Core\Helpers {
    // put your custom helpers here
}
// /core/Helpers.php
class Helpers {
    ...
}

Proposal #2

// /classes/Helpers.php
class Helpers {
    use Trait_Core_Helpers;
	use Trait_String_Helpers;
	use Trait_Template_Helpers;
	...
}

Add helpers for Data Validation

Expected usage API:

$data = [
	'name' => 'Luiz Bills',
	'nickname' => '',
	'age' => 55,
	'subscriber' => 'no'
];

$validation = h::validate(
	$data, 
	[
		'name' => [ 'required', 'string', 'min: 3', 'max: 100' ],
		'nickname' => [ 'optional', 'string', 'min: 1', 'max: 25' ],
		'age' => [ 'required', 'int', 'min: 18', 'max: 150' ],
		'subscriber' => [ 'required', 'string', 'in_list: yes,no' ],
	]
);

if ( ! $validation->success ) {
	$errors = $validation->get_errors();
	// do something
}

Notes:

  • min and max will be used to validate numbers (as limits) or string (as length).
  • in_list receives a list of values separated by comma.

Expected validation rule callback examples:

/**
 * The "required" validation rule
 * 
 * @param string  $value
 * @param array   $rule_values
 * @return string|null The message error
 */
function validation_rule_required ( $value, $rule_values = [] ) {
	if ( $value !== null && $value !== "" ) {
		return '{field} is required.';
	}
}

/**
 * The "in_list" validation rule
 */
function validation_rule_in_list ( $value, $rule_values = [] ) {
	if ( ! in_array( $value, $rule_values ) ) {
		return sprintf( '{field} must be one of these: %s', implode( ', ', $rule_values ) );
	}
}

/**
 * The "min" validation rule
 */
function validation_rule_min ( $value, $rule_values = [] ) {
	$min = (int) $rules_values[0];
	if ( is_string( $value ) && mb_strlen( $value ) <  $min ) {
		return sprintf( '{field} must be more than or equal to %d characters', $min );
	} elseif ( is_numeric( $value ) && $value < $min ) {
		return sprintf( '{field} must be greater than or equal to %d', $min );
	}
}

/**
 * The "optional" validation rule
 */
function validation_rule_optional ( $value, $rule_values = [] ) {
	if ( $value === null || $value === "" ) {
		return '##skip_validation##'; // special flag to skip other validations for this field
	}
}

Expected extension API:

h::add_validation_rule( 
	'char', // rule name
	function ( $value, $rule_values = [] ) { // rule callback
		if ( ! is_string( $value) || mb_strlen( $value ) !== 1 ) {
			return '{field} must be only 1 character';
		}
	}
);

Create a script to upgrade the boilerplate core

The boilerplate's core is the /core folder. It will be interesting manually to have a simple and quick way to update the entire core with a single command, whenever you need to delete or edit files.

Command idea: composer run upgrade-core

// composer.json
...
"scripts": {
    ...
    "upgrade-core": "./scripts/upgrade-core Your_Namespace",
}
...

Add "shortcuts" to check dependencies

Something like this

// with shortcut
$deps[] = [
    'check' => 'function:WC',
    'message' => ...
];

// without shortcut (it will continue to be possible to pass a function to check)
$deps[] = [
    'check' => function () {
        return function_exists( 'WC' );
    }, 
    'message' => ...
];

Shortcuts ideas:

  • function:xxx checks if the "xxx" function exists.
  • class:xxx checks if the "xxx" class exists.
  • plugin:dir/file.php checks if the "dir/file.php" is a activated plugin with is_plugin_active() function.
  • const:XXX checks if the "XXX" constant exists with defined function.
  • extension:xxx checks if the "xxx" PHP extension is installed with extension_loaded function.
  • wordpress:xxx requires "xxx" as minimum WordPress version

Command to install addons

Create a folder named .addons and put PHP files (each file will be an addon installer). No addons will be installed by default. You will need to use the composer run install-addon {addon-slug} command.

Possibles addons:

  • esbuild: will setup esbuild to bundle your JS/CSS file.
  • phpstan: will setup PHPStan.
  • publisher: will setup a script to push your plugin to your WordPress svn.
  • make-pot: will setup a script to create the .pot file.

Check PHP version in main.php

Ask for the "Minimum PHP Version" in the install script (7.4 by default). Then, use this information to create a simple code in the main.php.

// example
try {
    $min_expected_version = "7.4";
    $current_version = \preg_replace( '/[^0-9\.]/', '', PHP_VERSION );
    if ( \version_compare( $current_version, $min_expected_version, '>=' ) ) {
        throw new \Error( 'an useful message to administrators' );
    };
} catche ( \Throwable $e ) {
    // display a admin notice (only for administrators) and return
}

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.