luizbills / wp-plugin-base Goto Github PK
View Code? Open in Web Editor NEWPowerful opinated boilerplate for WordPress plugins :electric_plug:
Home Page: https://github.com/luizbills/wp-plugin-base
License: GNU General Public License v3.0
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
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).
}
}
Requires #9
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';
}
}
);
config.dev.php
when current environment is local
or development
. See: https://developer.wordpress.org/reference/functions/wp_get_environment_type/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 str_contains
in core/Traits/String_Helpers
.
Hello @luizbills!
๐ง the contents of my brain :)
I hope you will like it.
Move config.php
, dependencies.php
and loader.php
to a config
folder.
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 versionIt will be much better to maintain and improve.
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:
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
}
add_action_link ( $label, $href, $class = '' )
. See: https://developer.wordpress.org/reference/hooks/plugin_action_links_plugin_file/add_admin_notice ( $message, $type = 'error', $dismissible = false )
. See: https://developer.wordpress.org/reference/hooks/admin_notices/It will be useful to Singleton classes
<?php
// loader.php
return [
...
My_Class::get_instance(),
];
Throw a error and warns the user
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.