Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
luizbills committed Apr 29, 2022
0 parents commit c82af08
Show file tree
Hide file tree
Showing 10 changed files with 435 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/wp-build/
/vendor/
/node_modules/
155 changes: 155 additions & 0 deletions classes/Core/Helper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
<?php

namespace Your_Namespace\Core;

use Your_Namespace\Core\Helpers as h;

final class Helpers {
protected static $config = [];

public static function config_set ( $key, $value ) {
$key = mb_strtoupper( $key );
h::throw_if( isset( h::$config[ $key ] ), __CLASS__ . ": Key \"$key\" has already been assigned. No key can be assigned more than once." );
h::$config[ $key ] = $value;
}

public static function config_get ( $key = false, $default = false ) {
if ( ! $key ) {
return h::$config; // return all keys
}
$key = mb_strtoupper( $key );
if ( isset( h::$config[ $key ] ) ) return h::$config[ $key ];
h::throw_if( false === $default, __CLASS__ . ": Undefined config key: $key" );
return $default;
}

// PLUGIN SLUG AND PREFIX HELPER
public static function get_slug () {
return h::config_get( 'SLUG' );
}

public static function prefix ( $appends = '' ) {
return h::config_get( 'PREFIX' ) . $appends;
}

// PLUGIN VERSION
public function get_plugin_version () {
$data = \get_file_data( h::config_get( 'FILE' ), [ 'Version' ] );
return $data[0];
}

// DATABASE OPTIONS
public static function update_option ( $key, $value ) {
if ( null === $value || false === $value ) {
return \delete_option( h::prefix( $key ) );
}
return \update_option( h::prefix( $key ), $value );
}

public static function get_option ( $key, $default = false ) {
return \get_option( h::prefix( $key ), $default );
}

// CACHE/TRANSIENTS
public static function set_transient ( $transient, $value, $expiration = 0 ) {
$key = h::get_transient_key( $transient );
if ( null === $value || false === $value ) {
return \delete_transient( $key );
}
if ( is_callable( $value ) ) {
$value = \call_user_func( $value );
}
return \set_transient( $key, $value, $expiration );
}

public static function get_transient ( $transient, $default = false ) {
$key = h::get_transient_key( $transient );
$value = \get_transient( $key );
return null === $value || false === $value ? $default : $value;
}

public static function remember ( $transient, $expiration, $callback ) {
$key = h::get_transient_key( $transient );
$value = h::get_transient( $key );
if ( null === $value ) {
$value = call_user_func( $callback );
\set_transient( $key, $value, $expiration );
}
return $value;
}

public static function get_transient_key ( $transient ) {
return h::prefix( $transient ) . '_' . h::get_plugin_version();
}

// ARRAY HELPERS
public static function array_get ( $arr, $key, $default = false ) {
// usage #1: `h::array_get( $arr, 'x' ); // $arr['x']`
// usage #2: `h::array_get( $arr, [ 'x', 'y' ] ); // $arr['x']['y']`
$keys = is_array( $key ) ? $key : [ $key ];
foreach ( $keys as $k ) {
if ( is_array( $arr ) && isset( $arr[ $k ] ) ) {
$arr = $arr[ $k ];
} else {
return $default;
}
}
return $arr;
}

// DEBUG HELPERS
public static function throw_if ( $condition, $message, $error_code = -1, $exception_class = null ) {
if ( $condition ) {
if ( \is_callable( $message ) ) {
$message = $message();
}
if ( ! $exception_class || ! class_exists( $exception_class ) ) {
$exception_class = \RuntimeException::class;
}
throw new $exception_class( $message, (int) $error_code );
}
}

public static function get_wp_error_message ( $wp_error, $code = '' ) {
return \is_wp_error( $wp_error ) ? $wp_error->get_error_message( $code ) : '';
}

public static function dd ( $value, $pre = true ) {
if ( $pre ) echo '<pre>';
var_dump( $value );
if ( $pre ) echo '</pre>';
die;
}

public static function log () {
if ( ! defined( 'WP_DEBUG_LOG' ) || ! WP_DEBUG_LOG ) return;
$output = [];
foreach ( func_get_args() as $arg ) {
$value = '';
if ( is_object( $arg ) || is_array( $arg ) ) {
$value = print_r( $arg, true );
}
elseif ( is_bool( $arg ) ) {
$value = $arg ? 'bool(true)' : 'bool(false)';
}
elseif ( '' === $arg ) {
$value = 'empty_string';
}
elseif ( null === $arg ) {
$value = 'NULL';
}
else {
$value = $arg;
}
$output[] = $value;
}
$slug = h::get_slug();
array_merge( [ "[$slug]" ], $output );
error_log( implode( ' ', $output ) );
}

// == YOUR CUSTOM HELPERS (ALWAYS STATIC) ==
// public static function foo () {
// return 'bar';
// }
}
168 changes: 168 additions & 0 deletions classes/Core/Main.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
<?php

namespace Your_Namespace\Core;

use Your_Namespace\Core\Helpers as h;

final class Main {
protected static $classes_to_load = [];
protected static $dependencies = [];

// == CORE HELPERS ==
public static function run_plugin ( $main_file ) {
h::throw_if(
h::config_get( 'FILE', false ),
__CLASS__ . ' already inialized'
);

h::throw_if(
! file_exists( $main_file ),
'Invalid plugin main file path'
);

$root = dirname( $main_file );
h::config_set( 'FILE', $main_file );
h::config_set( 'ROOT', $root );

if ( file_exists( $root . '/config.php' ) ) {
$values = include $root . '/config.php';
foreach ( $values as $k => $v ) {
if ( 'SLUG' === \mb_strtoupper( $k ) ) {
$v = \sanitize_title_with_dashes( $v );
}
h::config_set( $k, $v );
}
$slug = h::config_get( 'slug' );
if ( ! h::config_get( 'prefix', false ) ) {
h::config_set( 'prefix', str_replace( '-', '_', $slug ) . '_' );
}
}

if ( \file_exists( $root . '/composer.json' ) ) {
$json_raw = \file_get_contents( $root . '/composer.json' );
$composer = \json_decode( $json_raw, true );
$php_version = $composer ? h::array_get( $composer, [ 'require', 'php' ], '' ) : false;
if ( $php_version ) {
$php_version = \preg_replace( '/[^0-9\.]/', '', $php_version );
h::config_set( 'REQUIRED_PHP_VERSION', $php_version );
}
}

if ( file_exists( $root . '/dependencies.php' ) ) {
self::register_dependencies();
} else {
h::config_set( 'PLUGIN_ACTIVATED', true );
}

if ( file_exists( $root . '/loader.php' ) ) {
self::setup_loader();
}

self::load_plugin_textdomain( 'your_text_domain' );
}

public static function load_plugin_textdomain ( $text_domain ) {
$dir = h::config_get( 'LANGUAGES_DIR', 'languages' );
\load_plugin_textdomain(
$text_domain,
false,
\dirname( \plugin_basename( h::config_get( 'FILE' ) ) ) . "/$dir/"
);
}

// == DEPENDECIES CHECKER ==
public static function register_dependencies () {
self::$dependencies[] = include_once $root . '/dependencies.php';
h::throw_if(
! is_array( self::$dependencies ),
h::config_get( 'ROOT' ) . '/dependencies.php must return an Array'
);
\add_action( 'plugins_loaded', [ __CLASS__, 'check_dependencies' ], 0 );
}

public static function check_dependencies () {
$errors = [];
foreach ( self::$dependencies as $key => $dep ) {
$check = is_callable( $dep['check'] ) ? call_user_func( $dep['check'] ) : $dep['check'];
if ( ! $check ) {
$errors[ $key ] = $dep['message'];
}
}
if ( count( $errors ) > 0 ) {
\add_action( 'admin_notices', function () use ( $errors ) {
$allowed_html = [
'a' => [
'href' => [],
'title' => [],
],
'br' => [],
'em' => [],
'strong' => [],
];
list( $plugin_name ) = \get_file_data( h::config_get( 'file' ), [ 'plugin name' ] );
echo "<div class='notice notice-error'><p>Não foi possível ativar o plugin <strong>$plugin_name</strong>. Siga as instruções abaixo:";
foreach ( $errors as $error_message ) {
$line = \sprintf(
'<br>%s• %s',
\str_repeat( '&nbsp;', 8 ),
$error_message
);
echo \wp_kses( $line, $allowed_html );
}
echo '</p></div>';
} );
}
h::config_set( 'PLUGIN_ACTIVATED', count( $errors ) === 0 );
}

public static function has_dependencies () {
return count( self::$dependencies ) > 0;
}

// CLASS LOADER
public static function load_classes () {
if ( ! h::config_get( 'PLUGIN_ACTIVATED', null ) ) return;
if ( ! self::$classes_to_load ) return;
$main_file = h::config_get( 'FILE' );
foreach ( self::$classes_to_load as $item ) {
$class_name = $item[0];
$priority = $item[1];
h::throw_if(
! \class_exists( $class_name ),
'class ' . $class_name . ' does not exist'
);
$instance = new $class_name();
if ( \method_exists( $class_name, '__boot' ) ) {
$instance->__boot();
}
if ( \method_exists( $class_name, '__init' ) ) {
\add_action( 'init', [ $instance, '__init' ], $priority );
}
if ( \method_exists( $class_name, '__activation' ) ) {
\register_activation_hook( $main_file, [ $instance, '__activation' ] );
}
if ( \method_exists( $class_name, '__deactivation' ) ) {
\register_deactivation_hook( $main_file, [ $instance, '__deactivation' ] );
}
if ( \method_exists( $class_name, '__uninstall' ) ) {
\register_uninstall_hook( $main_file, [ $instance, '__uninstall' ] );
}
}
self::$classes_to_load = null;
}

protected static function setup_loader () {
$classes = include_once $root . '/loader.php';

foreach ( $classes as $class ) {
if ( ! is_array( $class ) ) {
$class = [ $class, 10 ];
}
self::$classes_to_load[] = $class;
}
usort( self::$classes_to_load, function ( $a, $b ) {
return $a[1] > $b[1];
});
\add_action( 'plugins_loaded', [ __CLASS__, 'load_classes' ], 10 );
}
}
23 changes: 23 additions & 0 deletions classes/Sample_Class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Your_Namespace;

use Your_Namespace\Core\Helpers as h;

final class Sample_Class {
public function __root () {
h::log( 'called on "plugins_loaded" hook action' );
}
public function __init () {
h::log( 'called on "init" hook action' );
}
public function __activation () {
h::log( 'called when the plugin is activated' );
}
public function __deactivation () {
h::log( 'called when the plugin is deactivated' );
}
public function __uninstall () {
h::log( 'called when the plugin is uninstalled' );
}
}
13 changes: 13 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"require": {
"php": "^7.4"
},
"require-dev": {},
"autoload": {
"psr-4": {
"Your_Namespace\\": [
"classes/"
]
}
}
}
7 changes: 7 additions & 0 deletions config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

return [
'SLUG' => 'your_plugin_slug',
'PREFIX' => 'your_plugin_prefix',
'LANGUAGES_DIR' => 'languages'
];
Loading

0 comments on commit c82af08

Please sign in to comment.