diff --git a/Functionality/Admin/.gitkeep b/Functionality/Admin/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/Includes/AssetsLoader.php b/Includes/AssetsLoader.php deleted file mode 100644 index 7417760..0000000 --- a/Includes/AssetsLoader.php +++ /dev/null @@ -1,116 +0,0 @@ -scripts = []; - $this->styles = []; - $this->admin_scripts = []; - $this->admin_styles = []; - - add_action('wp_enqueue_scripts', [$this, 'loadAssets'], 100); - add_action('admin_enqueue_scripts', [$this, 'loadAdminAssets'], 100); - } - - // Clone not allowed - private function __clone() - { - } - - public static function getInstance() - { - if (is_null(self::$instance)) { - self::$instance = new AssetsLoader(); - } - return self::$instance; - } - - public function addStyle(string $name, string $path, callable $condition = null, array $dependencies = []) - { - $this->styles[] = [ - 'name' => $name, - 'path' => $path, - 'condition' => $condition, - 'dependencies' => $dependencies - ]; - } - - public function addScript(string $name, string $path, callable $condition = null, array $dependencies = [], $localize = false) - { - $this->scripts[] = [ - 'name' => $name, - 'path' => $path, - 'condition' => $condition, - 'dependencies' => $dependencies, - 'localize' => $localize - ]; - } - - public function addAdminStyle(string $name, string $path, callable $condition = null, array $dependencies = []) - { - $this->admin_styles[] = [ - 'name' => $name, - 'path' => $path, - 'condition' => $condition, - 'dependencies' => $dependencies - ]; - } - - public function addAdminScript(string $name, string $path, callable $condition = null, array $dependencies = [], $localize = false) - { - $this->admin_scripts[] = [ - 'name' => $name, - 'path' => $path, - 'condition' => $condition, - 'dependencies' => $dependencies, - 'localize' => $localize - ]; - } - - public function loadAssets($handler) - { - foreach ($this->styles as $style) { - if (is_null($style['condition']) || call_user_func($style['condition'], $handler)) { - wp_register_style("plugin-placeholder/{$style['name']}", $style['path'], $style['dependencies'], PLUGIN_PLACEHOLDER_VERSION); - } - } - - foreach ($this->scripts as $script) { - if (is_null($script['condition']) || call_user_func($script['condition'], $handler)) { - wp_register_script("plugin-placeholder/{$script['name']}", $script['path'], $script['dependencies'], PLUGIN_PLACEHOLDER_VERSION); - - if ($script['localize']) { - wp_localize_script("plugin-placeholder/{$script['name']}", $script['localize']['name'], $script['localize']['args']); - } - } - } - } - - public function loadAdminAssets($handler) - { - foreach ($this->admin_styles as $style) { - if (is_null($style['condition']) || call_user_func($style['condition'], $handler)) { - wp_register_style("plugin-placeholder/{$style['name']}", $style['path'], $style['dependencies'], PLUGIN_PLACEHOLDER_VERSION); - } - } - - foreach ($this->admin_scripts as $script) { - if (is_null($script['condition']) || call_user_func($script['condition'], $handler)) { - wp_register_script("plugin-placeholder/{$script['name']}", $script['path'], $script['dependencies'], PLUGIN_PLACEHOLDER_VERSION); - - if ($script['localize']) { - wp_localize_script("plugin-placeholder/{$script['name']}", $script['localize']['name'], $script['localize']['args']); - } - } - } - } -} diff --git a/Includes/Loader.php b/Includes/Loader.php index 2946e73..95574d6 100644 --- a/Includes/Loader.php +++ b/Includes/Loader.php @@ -4,13 +4,8 @@ class Loader { - protected $plugin_name; - protected $plugin_version; - public function __construct() { - $this->plugin_version = defined('PLUGIN_PLACEHOLDER_VERSION') ? PLUGIN_PLACEHOLDER_VERSION : '1.0.0'; - $this->plugin_name = 'plugin-placeholder'; $this->loadDependencies(); add_action('plugins_loaded', [$this, 'loadPluginTextdomain']); @@ -18,17 +13,33 @@ public function __construct() private function loadDependencies() { + //FUNCTIONALITY CLASSES foreach (glob(PLUGIN_PLACEHOLDER_PATH . 'Functionality/*.php') as $filename) { $class_name = '\\PluginPlaceholder\Functionality\\' . basename($filename, '.php'); if (class_exists($class_name)) { try { - new $class_name($this->plugin_name, $this->plugin_version); + new $class_name(PLUGIN_PLACEHOLDER_NAME, PLUGIN_PLACEHOLDER_VERSION); } catch (\Throwable $e) { pb_log($e); continue; } } } + + //ADMIN FUNCTIONALITY + if( is_admin() ) { + foreach (glob(PLUGIN_PLACEHOLDER_PATH . 'Functionality/Admin/*.php') as $filename) { + $class_name = '\\PluginPlaceholder\Functionality\Admin\\' . basename($filename, '.php'); + if (class_exists($class_name)) { + try { + new $class_name(PLUGIN_PLACEHOLDER_NAME, PLUGIN_PLACEHOLDER_VERSION); + } catch (\Throwable $e) { + pb_log($e); + continue; + } + } + } + } } public function loadPluginTextdomain() diff --git a/plugin-placeholder.php b/plugin-placeholder.php index dd1c705..721c0bc 100644 --- a/plugin-placeholder.php +++ b/plugin-placeholder.php @@ -19,6 +19,7 @@ } // PLUGIN CONSTANTS +define('PLUGIN_PLACEHOLDER_NAME', 'plugin-placeholder'); define('PLUGIN_PLACEHOLDER_VERSION', '1.0.0'); define('PLUGIN_PLACEHOLDER_PATH', plugin_dir_path(__FILE__)); define('PLUGIN_PLACEHOLDER_BASENAME', plugin_basename(__FILE__));