diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9b326e4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/wp-build/ +/vendor/ +/node_modules/ \ No newline at end of file diff --git a/classes/Core/Helper.php b/classes/Core/Helper.php new file mode 100644 index 0000000..7332c5f --- /dev/null +++ b/classes/Core/Helper.php @@ -0,0 +1,155 @@ +get_error_message( $code ) : ''; + } + + public static function dd ( $value, $pre = true ) { + if ( $pre ) echo '
';
+        var_dump( $value );
+        if ( $pre ) echo '
'; + 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'; + // } +} diff --git a/classes/Core/Main.php b/classes/Core/Main.php new file mode 100644 index 0000000..68d1841 --- /dev/null +++ b/classes/Core/Main.php @@ -0,0 +1,168 @@ + $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 "

Não foi possível ativar o plugin $plugin_name. Siga as instruções abaixo:"; + foreach ( $errors as $error_message ) { + $line = \sprintf( + '
%s• %s', + \str_repeat( ' ', 8 ), + $error_message + ); + echo \wp_kses( $line, $allowed_html ); + } + echo '

'; + } ); + } + 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 ); + } +} \ No newline at end of file diff --git a/classes/Sample_Class.php b/classes/Sample_Class.php new file mode 100644 index 0000000..0f86645 --- /dev/null +++ b/classes/Sample_Class.php @@ -0,0 +1,23 @@ + 'your_plugin_slug', + 'PREFIX' => 'your_plugin_prefix', + 'LANGUAGES_DIR' => 'languages' +]; diff --git a/dependencies.php b/dependencies.php new file mode 100644 index 0000000..d558534 --- /dev/null +++ b/dependencies.php @@ -0,0 +1,25 @@ + [ + 'message' => function () { + $req_version = h::config_get( 'REQUIRED_PHP_VERSION', false ); + return "Atualize o PHP para a versão $req_version ou superior"; + }, + 'check' => function () { + $req_version = h::config_get( 'REQUIRED_PHP_VERSION', false ); + $serv_version = \preg_replace( '/[^0-9\.]/', '', PHP_VERSION ); + return \version_compare( $serv_version, $req_version, '>=' ); + } + ], + 'woocommerce' => [ + 'message' => __( 'You need install and activate the WooCommerce plugin.', '' ), + 'check' => function () { + $req_version = h::config_get( 'REQUIRED_PHP_VERSION', '' ); + $server_version = $req_version ? \preg_replace( '/[^0-9\.]/', '', PHP_VERSION ) : false; + return $req_version && $server_version ? \version_compare( $server_version, $req_version, '>=' ) : true; + } + ], +]; diff --git a/languages/.gitkeep b/languages/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/loader.php b/loader.php new file mode 100644 index 0000000..3133a5f --- /dev/null +++ b/loader.php @@ -0,0 +1,7 @@ +$plugin_name", + '
' . \esc_html( $e->getMessage() ) . '' + ); + echo "

$message

"; + } ); +}