From 22d4b04b9824a266448ba16a1b21beab6b6016a8 Mon Sep 17 00:00:00 2001 From: Jakiboy Date: Wed, 19 Jun 2024 00:16:33 +0100 Subject: [PATCH] Update --- .gitattributes | 3 + composer.json | 2 +- src/VanillePluginConfig.php | 249 +++++++++++++++++-------------- src/VanillePluginOption.php | 64 +++++--- src/inc/Arrayify.php | 36 ++--- src/inc/Stringify.php | 25 ++++ src/inc/TypeCheck.php | 4 +- src/int/AdminHelperInterface.php | 8 + src/lib/API.php | 16 +- src/lib/Ajax.php | 3 +- src/lib/Backup.php | 3 + src/lib/Cache.php | 136 ++++++++++------- src/lib/Cron.php | 13 +- src/lib/Database.php | 37 ----- src/lib/Hook.php | 3 + src/lib/Loader.php | 3 + src/lib/Migrate.php | 41 ++++- src/lib/Orm.php | 3 + src/lib/Queue.php | 18 ++- src/lib/Requirement.php | 3 + src/lib/Rewrite.php | 12 +- src/lib/Shortcode.php | 31 ++-- src/lib/Updater.php | 3 + src/tr/TraitFormattable.php | 22 +++ src/tr/TraitMigratable.php | 17 ++- src/tr/TraitTranslatable.php | 54 ++++--- tests/AmpTest.php | 2 +- tests/CacheTest.php | 2 +- tests/EditorTest.php | 2 +- tests/TranslatorTest.php | 2 +- 30 files changed, 507 insertions(+), 310 deletions(-) delete mode 100644 src/lib/Database.php diff --git a/.gitattributes b/.gitattributes index a337ebf7..571e686a 100644 --- a/.gitattributes +++ b/.gitattributes @@ -3,5 +3,8 @@ /.github export-ignore .gitignore export-ignore .gitattributes export-ignore +CODE-OF-CONDUCT.md export-ignore +CONTRIBUTING.md export-ignore +SECURITY.md export-ignore _config.yml export-ignore logo.png export-ignore \ No newline at end of file diff --git a/composer.json b/composer.json index 84ce155e..6b619c05 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name":"jakiboy/vanilleplugin", - "version":"1.0.3", + "version":"1.0.4", "type":"library", "description":"WordPress Plugin Framework", "keywords":[ diff --git a/src/VanillePluginConfig.php b/src/VanillePluginConfig.php index ed645e1f..dd482474 100644 --- a/src/VanillePluginConfig.php +++ b/src/VanillePluginConfig.php @@ -15,7 +15,6 @@ namespace VanillePlugin; use VanillePlugin\exc\ConfigurationException; -use VanillePlugin\inc\Arrayify; /** * Define base configuration used by plugin. @@ -93,7 +92,7 @@ protected static function getStatic() : object */ protected function initConfig() { - if ( $this->global ) { + if ( isset($this->global) ) { return; } @@ -176,22 +175,25 @@ protected function getConfig(?string $key = null) * @access protected * @param array $options * @param int $args - * @return void + * @return bool */ - protected function updateConfig(array $options = [], $args = 64|128|256) + protected function updateConfig(array $options = [], $args = 64|128|256) : bool { $json = $this->getRoot($this->config); - $update = $this->parseJson($json, true); + $data = $this->parseJson($json, true); + foreach ($options as $option => $value) { - if ( isset($update['options'][$option]) ) { - $update['options'][$option] = $value; + if ( isset($data['options'][$option]) ) { + $data['options'][$option] = $value; } } - $update['routes'] = (object)$update['routes']; - $update['cron'] = (object)$update['cron']; - $update['assets'] = (object)$update['assets']; - $update = $this->formatJson($update, $args); - $this->writeFile($this->getRoot($this->config), $update); + + $data['routes'] = (object)$data['routes']; + $data['cron'] = (object)$data['cron']; + $data['assets'] = (object)$data['assets']; + $data = $this->formatJson($data, $args); + + return $this->writeFile($this->getRoot($this->config), $data); } /** @@ -319,9 +321,9 @@ protected function getPrefix(bool $sep = true) : string */ protected function getAsset() : string { - $paths = $this->getConfig('path'); - $asset = $paths->asset ?? '/assets'; - return "/{$this->getNameSpace()}{$asset}"; + $config = $this->getConfig('path'); + $data = $config->asset ?? '/assets'; + return "/{$this->getNameSpace()}{$data}"; } /** @@ -332,9 +334,9 @@ protected function getAsset() : string */ protected function getAssetUrl() : string { - $paths = $this->getConfig('path'); - $asset = $paths->asset ?? '/assets'; - return "{$this->getBaseUrl()}{$asset}"; + $config = $this->getConfig('path'); + $data = $config->asset ?? '/assets'; + return "{$this->getBaseUrl()}{$data}"; } /** @@ -346,9 +348,9 @@ protected function getAssetUrl() : string */ protected function getAssetPath(?string $sub = null) : string { - $paths = $this->getConfig('path'); - $asset = $paths->asset ?? '/assets'; - $path = $this->getRoot($asset); + $config = $this->getConfig('path'); + $data = $config->asset ?? '/assets'; + $path = $this->getRoot($data); if ( $sub ) { $path .= "/{$sub}"; } @@ -364,9 +366,9 @@ protected function getAssetPath(?string $sub = null) : string */ protected function getMigratePath(?string $sub = null) : string { - $paths = $this->getConfig('path'); - $migrate = $paths->migrate ?? '/migrate'; - $path = $this->getRoot($migrate); + $config = $this->getConfig('path'); + $data = $config->migrate ?? '/migrate'; + $path = $this->getRoot($data); if ( $sub ) { $path .= "/{$sub}"; } @@ -381,9 +383,9 @@ protected function getMigratePath(?string $sub = null) : string */ protected function getCachePath() : string { - $paths = $this->getConfig('path'); - $cache = $paths->cache ?? '/cache'; - return $this->getRoot($cache); + $config = $this->getConfig('path'); + $data = $config->cache ?? '/cache'; + return $this->getRoot($data); } /** @@ -395,9 +397,9 @@ protected function getCachePath() : string */ protected function getTempPath(?string $sub = null) : string { - $paths = $this->getConfig('path'); - $temp = $paths->temp ?? '/temp'; - $path = $this->getRoot($temp); + $config = $this->getConfig('path'); + $data = $config->temp ?? '/temp'; + $path = $this->getRoot($data); if ( $sub ) { $path .= "/{$sub}"; } @@ -412,9 +414,9 @@ protected function getTempPath(?string $sub = null) : string */ protected function getExpireIn() : int { - $options = $this->getConfig('options'); - $ttl = $options->ttl ?? 0; - return (int)$ttl; + $config = $this->getConfig('options'); + $data = $config->ttl ?? 0; + return (int)$data; } /** @@ -425,9 +427,9 @@ protected function getExpireIn() : int */ protected function getViewPath() : string { - $paths = $this->getConfig('path'); - $view = $paths->view ?? '/view'; - return $this->getRoot($view); + $config = $this->getConfig('path'); + $data = $config->view ?? '/view'; + return $this->getRoot($data); } /** @@ -438,9 +440,9 @@ protected function getViewPath() : string */ protected function getLoggerPath() : string { - $paths = $this->getConfig('path'); - $logs = $paths->logs ?? '/logs'; - return $this->getRoot($logs); + $config = $this->getConfig('path'); + $data = $config->logs ?? '/logs'; + return $this->getRoot($data); } /** @@ -451,9 +453,9 @@ protected function getLoggerPath() : string */ protected function getViewExtension() : string { - $options = $this->getConfig('options'); - $extension = $options->view->extension ?? '.html'; - return (string)$extension; + $config = $this->getConfig('options'); + $data = $config->view->extension ?? '.html'; + return (string)$data; } /** @@ -499,10 +501,10 @@ protected function getBaseUrl() : string protected function getAjax() : object { $this->initConfig(); - if ( !($ajax = $this->loadConfig('ajax')) ) { - $ajax = $this->global->ajax ?? []; + if ( !($data = $this->loadConfig('ajax')) ) { + $data = $this->global->ajax ?? []; } - return (object)$ajax; + return (object)$data; } /** @@ -513,8 +515,8 @@ protected function getAjax() : object */ protected function getAdminAjax() : array { - $ajax = $this->getAjax()->admin ?? []; - return (array)$ajax; + $data = $this->getAjax()->admin ?? []; + return (array)$data; } /** @@ -525,8 +527,8 @@ protected function getAdminAjax() : array */ protected function getFrontAjax() : array { - $ajax = $this->getAjax()->front ?? []; - return (array)$ajax; + $data = $this->getAjax()->front ?? []; + return (array)$data; } /** @@ -538,10 +540,10 @@ protected function getFrontAjax() : array protected function getPluginRoles() : array { $this->initConfig(); - if ( !($roles = $this->loadConfig('roles', true)) ) { - $roles = $this->global->roles ?? []; + if ( !($data = $this->loadConfig('roles', true)) ) { + $data = $this->global->roles ?? []; } - return (array)$roles; + return (array)$data; } /** @@ -553,10 +555,10 @@ protected function getPluginRoles() : array protected function getCron() : array { $this->initConfig(); - if ( !($cron = $this->loadConfig('cron', true)) ) { - $cron = $this->global->cron ?? []; + if ( !($data = $this->loadConfig('cron', true)) ) { + $data = $this->global->cron ?? []; } - return (array)$cron; + return (array)$data; } /** @@ -568,10 +570,10 @@ protected function getCron() : array protected function getRoutes() : object { $this->initConfig(); - if ( !($routes = $this->loadConfig('routes')) ) { - $routes = $this->global->routes ?? []; + if ( !($data = $this->loadConfig('routes')) ) { + $data = $this->global->routes ?? []; } - return (object)$routes; + return (object)$data; } /** @@ -583,10 +585,10 @@ protected function getRoutes() : object protected function getRequirements() : object { $this->initConfig(); - if ( !($requirements = $this->loadConfig('requirements')) ) { - $requirements = $this->global->requirements ?? []; + if ( !($data = $this->loadConfig('requirements')) ) { + $data = $this->global->requirements ?? []; } - return (object)$requirements; + return (object)$data; } /** @@ -598,10 +600,10 @@ protected function getRequirements() : object protected function getHooks() : array { $this->initConfig(); - if ( !($hooks = $this->loadConfig('hooks', true)) ) { - $hooks = $this->global->hooks ?? []; + if ( !($data = $this->loadConfig('hooks', true)) ) { + $data = $this->global->hooks ?? []; } - return (array)$hooks; + return (array)$data; } /** @@ -613,10 +615,10 @@ protected function getHooks() : array protected function getSettings() : array { $this->initConfig(); - if ( !($settings = $this->loadConfig('settings', true)) ) { - $settings = $this->global->settings ?? []; + if ( !($data = $this->loadConfig('settings', true)) ) { + $data = $this->global->settings ?? []; } - return (array)$settings; + return (array)$data; } /** @@ -627,13 +629,13 @@ protected function getSettings() : array */ protected function getGroupSettings() : array { - $groups = []; + $data = []; foreach ($this->getSettings() as $group) { $name = $group['group']; unset($group['group']); - $groups[$name][] = $group; + $data[$name][] = $group; } - return (array)$groups; + return (array)$data; } /** @@ -645,10 +647,10 @@ protected function getGroupSettings() : array protected function getAssets() : array { $this->initConfig(); - if ( !($assets = $this->loadConfig('assets', true)) ) { - $assets = $this->global->assets ?? []; + if ( !($data = $this->loadConfig('assets', true)) ) { + $data = $this->global->assets ?? []; } - return (array)$assets; + return (array)$data; } /** @@ -659,9 +661,8 @@ protected function getAssets() : array */ protected function getRemoteAssets() : array { - $assets = $this->getAssets(); - $remote = $assets['remote'] ?? []; - return (array)$remote; + $data = $this->getAssets()['remote'] ?? []; + return (array)$data; } /** @@ -672,9 +673,8 @@ protected function getRemoteAssets() : array */ protected function getLocalAssets() : array { - $assets = $this->getAssets(); - $local = $assets['local'] ?? []; - return (array)$local; + $data = $this->getAssets()['local'] ?? []; + return (array)$data; } /** @@ -687,14 +687,14 @@ protected function getLocalAssets() : array */ protected function getAdminAssets(string $type, string $env = 'main') : array { - $assets = $this->getLocalAssets()['admin'] ?? []; - $assets = $this->mapArray(function($item) use ($type, $env) { + $data = $this->getLocalAssets()['admin'] ?? []; + $data = $this->mapArray(function($item) use ($type, $env) { if ( $item['type'] === $type && $item['env'] === $env ) { return $item['path']; } - }, $assets); + }, $data); - return Arrayify::filter($assets); + return $this->filterArray($data); } /** @@ -705,14 +705,14 @@ protected function getAdminAssets(string $type, string $env = 'main') : array */ protected function getFrontAssets(string $type, string $env = 'main') : array { - $assets = $this->getLocalAssets()['front'] ?? []; - $assets = $this->mapArray(function($item) use ($type, $env) { + $data = $this->getLocalAssets()['front'] ?? []; + $data = $this->mapArray(function($item) use ($type, $env) { if ( $item['type'] === $type && $item['env'] === $env ) { return $item['path']; } - }, $assets); + }, $data); - return Arrayify::filter($assets); + return $this->filterArray($data); } /** @@ -724,10 +724,10 @@ protected function getFrontAssets(string $type, string $env = 'main') : array protected function getStrings() : array { $this->initConfig(); - if ( !($strings = $this->loadConfig('strings', true)) ) { - $strings = $this->global->strings ?? []; + if ( !($data = $this->loadConfig('strings', true)) ) { + $data = $this->global->strings ?? []; } - return (array)$strings; + return (array)$data; } /** @@ -738,9 +738,9 @@ protected function getStrings() : array */ protected function isMultilingual() : bool { - $options = $this->getConfig('options'); - $multilingual = $options->multilingual ?? false; - return (bool)$multilingual; + $config = $this->getConfig('options'); + $data = $config->multilingual ?? false; + return (bool)$data; } /** @@ -751,9 +751,9 @@ protected function isMultilingual() : bool */ protected function allowedMultisite() : bool { - $options = $this->getConfig('options'); - $multisite = $options->multisite ?? false; - return (bool)$multisite; + $config = $this->getConfig('options'); + $data = $config->multisite ?? false; + return (bool)$data; } /** @@ -764,9 +764,9 @@ protected function allowedMultisite() : bool */ protected function hasDebug() : bool { - $options = $this->getConfig('options'); - $debug = $options->debug ?? false; - return (bool)$debug; + $config = $this->getConfig('options'); + $data = $config->debug ?? false; + return (bool)$data; } /** @@ -777,9 +777,9 @@ protected function hasDebug() : bool */ protected function getEnv() : string { - $options = $this->getConfig('options'); - $env = $options->environment ?? 'dev'; - return (string)$env; + $config = $this->getConfig('options'); + $data = $config->environment ?? 'dev'; + return (string)$data; } /** @@ -881,32 +881,53 @@ public function hasMultilingual() : bool } /** - * Get plugin language. + * Get plugin current language (locale). * * @access protected - * @param bool $country + * @param bool $cache + * @param bool $parse * @return string */ - protected function getLang(bool $country = true) : string + protected function getLang(bool $cache = true, bool $parse = false) : string { + // Get from cache + if ( $cache ) { + if ( ($lang = $this->getCacheLang($parse)) ) { + return $lang; + } + } + // Get from system - $locale = $default = $this->getLocale(); + $lang = $default = $this->getLocale(); // Get from third-party if ( $this->hasMultilingual() ) { - $locale = $this->getTranslatorLocale(); + $lang = $this->getTranslatorLocale(); } // Get from default - if ( !$locale ) { - $locale = $default; + if ( !$lang ) { + $lang = $default; } - // Convert to country code - if ( $country ) { - return $this->getTranslatorCountry($locale); - } + // Normalize + $lang = $this->normalizeLocale($lang); + return ($parse) ? $this->parseLang($lang) : $lang; + } - return $this->getLanguage($locale); + /** + * Get plugin cache lang. + * + * @access protected + * @param bool $parse + * @return mixed + */ + protected function getCacheLang(bool $parse = false) + { + $key = "{$this->getNameSpace()}-lang"; + if ( ($lang = $this->getTransient($key)) ) { + return ($parse) ? $this->parseLang($lang) : $lang; + } + return false; } } diff --git a/src/VanillePluginOption.php b/src/VanillePluginOption.php index 4f9c3ba3..ffb5120b 100644 --- a/src/VanillePluginOption.php +++ b/src/VanillePluginOption.php @@ -176,7 +176,7 @@ protected function hasPluginShortcode() : bool */ protected function registerPluginOption(string $group, string $key, array $args = [], $multiling = null) { - $lang = $this->setOptionLanguage($multiling); + $lang = $this->getOptionLang($multiling); $group = $this->applyPrefix($group); $key = $this->applyPrefix("{$key}{$lang}"); $this->registerOption($group, $key, $args); @@ -190,7 +190,7 @@ protected function registerPluginOption(string $group, string $key, array $args */ protected function addPluginOption(string $key, $value, $multiling = null) : bool { - $lang = $this->setOptionLanguage($multiling); + $lang = $this->getOptionLang($multiling); $key = $this->applyPrefix("{$key}{$lang}"); return $this->addOption($key, $value); } @@ -204,7 +204,7 @@ protected function addPluginOption(string $key, $value, $multiling = null) : boo */ protected function getPluginOption(string $key, string $type = 'array', $default = false, $multiling = null) { - $lang = $this->setOptionLanguage($multiling); + $lang = $this->getOptionLang($multiling); $key = $this->applyPrefix("{$key}{$lang}"); $value = $this->stripSlash( $this->getOption($key, $default) @@ -237,7 +237,7 @@ protected function getPluginOption(string $key, string $type = 'array', $default */ protected function updatePluginOption(string $key, $value, $multiling = null) : bool { - $lang = $this->setOptionLanguage($multiling); + $lang = $this->getOptionLang($multiling); $key = $this->applyPrefix("{$key}{$lang}"); return $this->updateOption($key, $value); } @@ -250,29 +250,46 @@ protected function updatePluginOption(string $key, $value, $multiling = null) : */ protected function removePluginOption(string $key, $multiling = null) : bool { - $lang = $this->setOptionLanguage($multiling); + $lang = $this->getOptionLang($multiling); $key = $this->applyPrefix("{$key}{$lang}"); return $this->removeOption($key); } /** - * Set plugin option language. + * Get plugin option language. * * @access protected - * @param mixed $multiling + * @param bool $multiling * @return mixed */ - protected function setOptionLanguage($multiling = null) + protected function getOptionLang(?bool $multiling = null) { $lang = null; if ( $this->hasMultilingual() ) { if ( $multiling !== false ) { - $multiling = "-{$this->getLang()}"; + $lang = "-{$this->getLang()}"; } } return $lang; } + /** + * Set plugin transient lang. + * [Action: head]. + * [Action: admin-init]. + * + * @access protected + * @return bool + */ + protected function setLang() : bool + { + if ( $this->hasMultilingual() ) { + $lang = $this->getLang(false); + return $this->setPluginTransient('lang', $lang, 0); + } + return false; + } + /** * Check settings save action (No JS). * @@ -307,14 +324,20 @@ protected function removePluginOptions() : bool */ protected function addPluginMenuPage(array $settings = []) : string { + $name = $this->getPluginName(); + $name = $this->applyPluginFilter('menu-name', $name); + $icon = $this->applyPluginFilter('menu-icon', 'admin-plugins'); + $cap = $this->applyPluginFilter('menu-cap', $this->applySufix('manage')); + $pos = $this->applyPluginFilter('menu-pos', 20); + $settings = $this->mergeArray([ - 'title' => "{$this->getPluginName()} Dashboard", - 'menu' => $this->getPluginName(), - 'cap' => $this->applySufix('manage'), + 'title' => "{$name} Dashboard", + 'menu' => $name, + 'cap' => $cap, 'slug' => $this->getNameSpace(), 'callback' => [$this, 'index'], - 'icon' => 'admin-plugins', - 'position' => 20 + 'icon' => $icon, + 'position' => $pos ], $settings); $settings['title'] = $this->trans($settings['title']); @@ -331,18 +354,21 @@ protected function addPluginMenuPage(array $settings = []) : string */ protected function addPluginSubMenuPage(array $settings = []) { + $name = $this->getPluginName(); + $name = $this->applyPluginFilter('menu-name', $name); + $cap = $this->applyPluginFilter('menu-cap', $this->applySufix('manage')); + $settings = $this->mergeArray([ 'parent' => $this->getNameSpace(), - 'title' => "{$this->getPluginName()} {menu}", - 'menu' => $this->getPluginName(), - 'cap' => $this->applySufix('manage'), + 'title' => "{$name} {menu}", + 'menu' => $name, + 'cap' => $cap, 'slug' => $this->getNameSpace(), 'callback' => [$this, 'index'], 'icon' => false ], $settings); $settings['menu'] = $this->trans($settings['menu']); - $settings['title'] = $this->trans( $this->replaceString('{menu}', $settings['menu'], $settings['title']) ); @@ -363,7 +389,7 @@ protected function addPluginSubMenuPage(array $settings = []) protected function resetPluginSubMenu(?string $title = null, ?string $icon = null) { $parent = $this->getNameSpace(); - $title = $this->trans($title); + $title = $this->trans($title); $this->resetSubMenuPage($parent, $title, $icon); } diff --git a/src/inc/Arrayify.php b/src/inc/Arrayify.php index cb5be5ed..d21cdfb7 100644 --- a/src/inc/Arrayify.php +++ b/src/inc/Arrayify.php @@ -18,7 +18,7 @@ final class Arrayify { /** * Check array item. - * + * * @access public * @param mixed $item * @param array $array @@ -31,7 +31,7 @@ public static function inArray($item, array $array) : bool /** * Merge arrays. - * + * * @access public * @param array $array * @param array $arrays @@ -44,7 +44,7 @@ public static function merge(array $array, array $arrays) : array /** * Push array. - * + * * @access public * @param array $array * @param mixed $values @@ -57,7 +57,7 @@ public static function push(array &$array, $values) : int /** * Combine array. - * + * * @access public * @param array $keys * @param array $values @@ -70,7 +70,7 @@ public static function combine(array $keys, array $values) : array /** * Map array. - * + * * @access public * @param callable $callback * @param array $array @@ -87,7 +87,7 @@ public static function map($callback, array $array, ?array $arrays = null) /** * Shift array. - * + * * @access public * @param array $array * @return mixed @@ -99,7 +99,7 @@ public static function shift(array &$array) /** * Pop array. - * + * * @access public * @param array $array * @return array @@ -111,7 +111,7 @@ public static function pop(array &$array) : array /** * Get array diff. - * + * * @access public * @param array $array * @param array $arrays @@ -124,7 +124,7 @@ public static function diff(array $array, array $arrays) : array /** * Check array key. - * + * * @access public * @param mixed $key * @param array $array @@ -137,7 +137,7 @@ public static function hasKey($key, array $array) : bool /** * Get array keys. - * + * * @access public * @param array $array * @return array @@ -162,7 +162,7 @@ public static function fillKeys(array $array, $values) : array /** * Get array values. - * + * * @access public * @param array $array * @return array @@ -174,7 +174,7 @@ public static function values(array $array) : array /** * Randomize array. - * + * * @access public * @param array $array * @param int $num @@ -187,7 +187,7 @@ public static function rand(array $array, int $num = 1) /** * Slice array. - * + * * @access public * @param array $array * @param int $offset @@ -202,7 +202,7 @@ public static function slice(array $array, int $offset, ?int $length = null, boo /** * Filter array. - * + * * @access public * @param array $array * @param callable $callback @@ -219,7 +219,7 @@ public static function filter(array $array, $callback = null, int $mode = 0) : a /** * Format array key case. - * + * * @access public * @param array $array * @param int $case @@ -232,7 +232,7 @@ public static function formatKeyCase(array $array, int $case = CASE_LOWER) : arr /** * Walk recursive array. - * + * * @access public * @param mixed $array * @param callable $callback @@ -246,7 +246,7 @@ public static function recursive(&$array, $callback, $arg = null) : bool /** * Unique array. - * + * * @access public * @param array $array * @param int $flags @@ -259,7 +259,7 @@ public static function unique(array $array, int $flags = SORT_STRING) : array /** * Unique arrays. - * + * * @access public * @param array $array * @return array diff --git a/src/inc/Stringify.php b/src/inc/Stringify.php index 6303a293..e7b3883b 100644 --- a/src/inc/Stringify.php +++ b/src/inc/Stringify.php @@ -174,6 +174,31 @@ public static function capitalize(string $string) : string return ucfirst(self::lowercase($string)); } + /** + * Camelcase string. + * + * @access public + * @param string $string + * @return string + */ + public static function camelcase(string $string) : string + { + $string = explode('-', self::slugify($string)); + $string = Arrayify::values( + Arrayify::filter($string) + ); + $first = $string[0] ?? ''; + $string = Arrayify::map(function($val) use ($first) { + if ( $val === $first ) { + return $val; + } + return self::capitalize($val); + }, $string); + return self::undash( + implode('', $string) + ); + } + /** * Slugify string. * diff --git a/src/inc/TypeCheck.php b/src/inc/TypeCheck.php index 3e47a73c..6b5df224 100644 --- a/src/inc/TypeCheck.php +++ b/src/inc/TypeCheck.php @@ -274,11 +274,11 @@ public static function hasInterface($class, string $interface, bool $short = tru * Check method. * * @access public - * @param object $object + * @param mixed $object * @param string $method * @return bool */ - public static function hasMethod(object $object, string $method) : bool + public static function hasMethod($object, string $method) : bool { return method_exists($object, $method); } diff --git a/src/int/AdminHelperInterface.php b/src/int/AdminHelperInterface.php index bddb524d..1ef8f731 100644 --- a/src/int/AdminHelperInterface.php +++ b/src/int/AdminHelperInterface.php @@ -45,6 +45,14 @@ function deactivate(); */ function upgrade(); + /** + * Plugin load action. + * [Action: {plugin}-load]. + * + * @return void + */ + function load(); + /** * Plugin action links. * [Filter: {plugin}-action]. diff --git a/src/lib/API.php b/src/lib/API.php index 7be02173..7967e6c5 100644 --- a/src/lib/API.php +++ b/src/lib/API.php @@ -62,7 +62,7 @@ public function __construct(string $method = 'GET', array $args = [], ?string $b // Set debug status $this->hasDebug = $this->hasDebug(); - // Reset plugin config + // Reset config $this->resetConfig(); } @@ -212,4 +212,18 @@ public function forceDisableSSL() { $this->args['sslverify'] = false; } + + /** + * Instance API. + * + * @access public + * @param string $name + * @param string $path + * @param mixed $args + * @return mixed + */ + public static function i(string $name, $path = 'api', ...$args) + { + return (new Loader())->i($path, $name, $args); + } } diff --git a/src/lib/Ajax.php b/src/lib/Ajax.php index 3622439c..b01d3fd1 100644 --- a/src/lib/Ajax.php +++ b/src/lib/Ajax.php @@ -56,7 +56,7 @@ public function __construct(AjaxInterface $callable) $this->initFrontActions(); } - // Reset plugin config + // Reset config $this->resetConfig(); } @@ -67,6 +67,7 @@ public function callback() { foreach ($this->actions as $action) { if ( $this->isAction($action) ) { + $action = $this->camelcase($action); $this->callable->{$action}(); } } diff --git a/src/lib/Backup.php b/src/lib/Backup.php index 1ebb2b10..95fb1f93 100644 --- a/src/lib/Backup.php +++ b/src/lib/Backup.php @@ -37,6 +37,9 @@ public function __construct() { // Init orm parent::__construct(); + + // Reset config + $this->resetConfig(); } /** diff --git a/src/lib/Cache.php b/src/lib/Cache.php index 00a0c5a0..6639696c 100644 --- a/src/lib/Cache.php +++ b/src/lib/Cache.php @@ -29,8 +29,10 @@ final class Cache /** * @access private * @var string FILECACHE, File cache + * @var string THIRDCACHE, Third cache */ - private const FILECACHE = 'VanilleCache\Cache'; + private const FILECACHE = 'VanilleCache\Cache'; + private const THIRDCACHE = 'VanilleThird\Cache'; /** * @access private @@ -44,9 +46,18 @@ final class Cache /** * Init cache path. + * + * @param string $key + * @param string $tag */ - public function __construct() + public function __construct(?string $key = null, ?string $tag = null) { + // Set cache key + $this->setKey($key); + + // Set cache tag + $this->setTag($tag); + // Set cache path $this->path[] = $this->getTempPath(); $this->path[] = $this->getCachePath(); @@ -55,49 +66,27 @@ public function __construct() $this->resetConfig(); } - /** - * Set cache key. - * - * @access public - * @param string $key - * @return object - */ - public function setKey(string $key) : self - { - $this->key = $key; - $this->tag = $this->getTag($key); - - if ( ThirdCache::isActive() ) { - $this->key = $this->applyNamespace($this->key); - $this->tag = $this->applyNamespace($this->tag); - } - - return $this; - } - /** * Get cache value. - * + * * @access public * @param mixed $default * @return mixed - * @throws CacheException */ public function get($default = null) { - $data = null; - if ( !$this->key ) { throw new CacheException( CacheException::undefinedCacheKey() ); } - if ( ThirdCache::isActive() ) { + $data = null; + + if ( $this->hasThirdCache() ) { $data = ObjectCache::get($this->key); - } - if ( $this->isType('class', self::FILECACHE) ) { + } elseif ( $this->hasFileCache() ) { $data = (new FileCache())->get($this->key); } @@ -113,7 +102,6 @@ public function get($default = null) * * @access public * @return bool - * @throws CacheException */ public function isCached() : bool { @@ -123,11 +111,11 @@ public function isCached() : bool ); } - if ( ThirdCache::isActive() ) { + if ( $this->hasThirdCache() ) { return (ObjectCache::get($this->key) !== false); } - if ( $this->isType('class', self::FILECACHE) ) { + if ( $this->hasFileCache() ) { return (new FileCache())->setKey($this->key)->isCached(); } @@ -141,7 +129,6 @@ public function isCached() : bool * @param mixed $value * @param int $ttl * @return bool - * @throws CacheException */ public function set($value, ?int $ttl = null) : bool { @@ -151,11 +138,11 @@ public function set($value, ?int $ttl = null) : bool ); } - if ( ThirdCache::isActive() ) { - return ObjectCache::set($this->key, $value, $this->tag, (int)$ttl); + if ( $this->hasThirdCache() ) { + return ObjectCache::set($this->key, $value, (string)$this->tag, (int)$ttl); } - if ( $this->isType('class', self::FILECACHE) ) { + if ( $this->hasFileCache() ) { return (new FileCache())->setKey($this->key)->set($value, $this->tag, $ttl); } @@ -164,10 +151,9 @@ public function set($value, ?int $ttl = null) : bool /** * Delete cache. - * + * * @access public * @return bool - * @throws CacheException */ public function delete() : bool { @@ -177,11 +163,11 @@ public function delete() : bool ); } - if ( ThirdCache::isActive() ) { + if ( $this->hasThirdCache() ) { return ObjectCache::delete($this->key); } - if ( $this->isType('class', self::FILECACHE) ) { + if ( $this->hasFileCache() ) { return (new FileCache())->delete($this->key); } @@ -190,14 +176,14 @@ public function delete() : bool /** * Delete cache by tag(s). - * + * * @access public * @param mixed $tag * @return bool */ public function deleteByTag($tag) : bool { - if ( $this->isType('class', self::FILECACHE) ) { + if ( $this->hasFileCache() ) { return (new FileCache())->deleteByTag($tag); } } @@ -213,12 +199,12 @@ public function purge(bool $force = false) : bool { $stauts = 0; - if ( ThirdCache::isActive() ) { + if ( $this->hasThirdCache() ) { $stauts += (int)ThirdCache::purge(); $stauts += (int)ObjectCache::purge(); } - if ( $this->isType('class', self::FILECACHE) ) { + if ( $this->hasFileCache() ) { $stauts += (int)(new FileCache())->purge(); } @@ -270,15 +256,63 @@ public function generateKey(string $item, array $args = []) : string } /** - * Get cache tag. - * - * @access private + * Set cache key. + * + * @access public * @param string $key - * @return string + * @return object + */ + public function setKey(?string $key = null) : self + { + if ( $key ) { + $this->key = $this->applyNamespace($key); + } + return $this; + } + + /** + * Set cache tag. + * + * @access public + * @param string $tag + * @return object + */ + public function setTag(?string $tag = null) : self + { + if ( $tag ) { + $this->tag = $tag; + + } else { + if ( $this->key ) { + $tag = explode('-', $this->key); + $this->tag = $tag[1] ?? $this->getNameSpace(); + } + } + return $this; + } + + /** + * Check third cache. + * + * @access public + * @return bool + */ + public function hasThirdCache() : bool + { + if ( $this->isType('class', self::THIRDCACHE) ) { + return ThirdCache::isActive(); + } + return false; + } + + /** + * Check file cache. + * + * @access public + * @return bool */ - private function getTag(string $key) : string + public function hasFileCache() : bool { - $tag = explode('-', $key); - return $tag[0] ?? $this->getNameSpace(); + return $this->isType('class', self::FILECACHE); } } diff --git a/src/lib/Cron.php b/src/lib/Cron.php index 08532db7..65845a71 100644 --- a/src/lib/Cron.php +++ b/src/lib/Cron.php @@ -36,9 +36,13 @@ class Cron implements CronInterface */ public function __construct() { + // Add schedulers actions foreach ($this->getCron() as $scheduler) { $this->addSchedulerAction($scheduler); } + + // Reset config + $this->resetConfig(); } /** @@ -157,14 +161,7 @@ protected function sanitizeActions() if ( !isset($action['callable']) && isset($action['name']) ) { - $name = explode('-', $this->slugify($action['name'])); - if ( count($name) == 2 ) { - $name[1] = $this->capitalize($name[1]); - } - - $callable = implode('', $name); - $callable = $this->undash($callable); - + $callable = $this->camelcase($action['name']); if ( $this->hasObject('method', $this, $callable) ) { $this->actions[$key]['callable'] = [$this, $callable]; diff --git a/src/lib/Database.php b/src/lib/Database.php deleted file mode 100644 index 20dd10af..00000000 --- a/src/lib/Database.php +++ /dev/null @@ -1,37 +0,0 @@ - - * @link : https://jakiboy.github.io/VanillePlugin/ - * @license : MIT - * - * This file if a part of VanillePlugin Framework. - */ - -declare(strict_types=1); - -namespace VanillePlugin\lib; - -/** - * Database dynamic autoloader. - */ -class Database -{ - use \VanillePlugin\VanillePluginOption; - - /** - * Load database table. - * - * @access public - * @param string $name - * @param mixed $args - * @return mixed - */ - public function load(string $name, ...$args) - { - $path = $this->applyPluginFilter('database-path', 'db'); - return (new Loader())->i($path, $name, $args); - } -} diff --git a/src/lib/Hook.php b/src/lib/Hook.php index c577246c..74e3645e 100644 --- a/src/lib/Hook.php +++ b/src/lib/Hook.php @@ -49,6 +49,9 @@ public function __construct(string $filter = self::FILTER, string $option = self $this->filter = $filter; $this->option = $option; $this->group = self::GROUP; + + // Reset config + $this->resetConfig(); } /** diff --git a/src/lib/Loader.php b/src/lib/Loader.php index ac4b8a76..613198a8 100644 --- a/src/lib/Loader.php +++ b/src/lib/Loader.php @@ -40,6 +40,9 @@ public function __construct(string $baseDir = self::BASEDIR, string $pattern = s { $this->baseDir = $this->format($baseDir); $this->pattern = $pattern; + + // Reset config + $this->resetConfig(); } /** diff --git a/src/lib/Migrate.php b/src/lib/Migrate.php index d6c9723f..e76a44b4 100644 --- a/src/lib/Migrate.php +++ b/src/lib/Migrate.php @@ -27,11 +27,11 @@ final class Migrate extends Orm * @var string ADD * @var string ALTER */ - private const LOCK = 'migrate.lock'; - private const UPGRADE = 'upgrade.sql'; + private const LOCK = 'migrate.lock'; + private const UPGRADE = 'upgrade.sql'; private const UNINSTALL = 'uninstall.sql'; - private const ADD = '/ADD\s`(.*)`\s/s'; - private const ALTER = '/ALTER\sTABLE\s`(.*)`\sADD/s'; + private const ADD = '/ADD\s`(.*)`\s/s'; + private const ALTER = '/ALTER\sTABLE\s`(.*)`\sADD/s'; /** * Init migrate. @@ -40,15 +40,18 @@ public function __construct() { // Init orm parent::__construct(); + + // Reset config + $this->resetConfig(); } /** - * Create plugin database tables. + * Install plugin database tables. * * @access public * @return bool */ - public function table() : bool + public function install() : bool { if ( $this->isMigrated() ) { return false; @@ -73,6 +76,18 @@ public function table() : bool return (bool)$count; } + /** + * Rebuild plugin database tables. + * + * @access public + * @return bool + */ + public function rebuild() : bool + { + $this->unlock(); + return $this->install(); + } + /** * Remove plugin database tables. * @@ -223,6 +238,20 @@ public function unlock() : bool ); } + /** + * Instance database table. + * + * @access public + * @param string $name + * @param string $path + * @param mixed $args + * @return mixed + */ + public static function i(string $name, $path = 'db', ...$args) + { + return (new Loader())->i($path, $name, $args); + } + /** * Parse table name for upgrade. * diff --git a/src/lib/Orm.php b/src/lib/Orm.php index d7555909..11cf1637 100644 --- a/src/lib/Orm.php +++ b/src/lib/Orm.php @@ -54,6 +54,9 @@ public function __construct() if ( !$this->hasDebug() ) { $this->silent(); } + + // Reset config + $this->resetConfig(); } /** diff --git a/src/lib/Queue.php b/src/lib/Queue.php index 8d1b60a5..255939ff 100644 --- a/src/lib/Queue.php +++ b/src/lib/Queue.php @@ -23,9 +23,17 @@ class Queue use \VanillePlugin\VanillePluginConfig, \VanillePlugin\tr\TraitCacheable; + /** + * Reset config. + */ + public function __construct() + { + $this->resetConfig(); + } + /** * Add item to queue. - * + * * @access public * @param string $item * @param string $name @@ -40,7 +48,7 @@ public function add(string $item, string $name = 'in') : bool /** * Check whether item in queue. - * + * * @access public * @param string $item * @param string $name @@ -53,7 +61,7 @@ public function has(string $item, string $name = 'in') : bool /** * Delete queue. - * + * * @access public * @param string $name * @return bool @@ -66,7 +74,7 @@ public function delete(string $name = 'in') : bool /** * Get queue. - * + * * @access private * @param string $name * @return array @@ -83,7 +91,7 @@ private function get(string $name = 'in') : array /** * Set queue. - * + * * @access private * @param array $queue * @param string $name diff --git a/src/lib/Requirement.php b/src/lib/Requirement.php index 6a1e4c74..5bd32a4a 100644 --- a/src/lib/Requirement.php +++ b/src/lib/Requirement.php @@ -70,6 +70,9 @@ public function __construct() 'required' => 'Required' ] ]); + + // Reset config + $this->resetConfig(); } /** diff --git a/src/lib/Rewrite.php b/src/lib/Rewrite.php index 2317c39e..e1b6711d 100644 --- a/src/lib/Rewrite.php +++ b/src/lib/Rewrite.php @@ -65,11 +65,11 @@ public function setRules(string $rules) * @param string $regex * @param mixed $query * @param string $after - * @return void + * @return mixed */ public function addRules(string $regex, $query, string $after = 'bottom') { - add_rewrite_rule($regex, $query, $after); + return add_rewrite_rule($regex, $query, $after); } /** @@ -81,11 +81,11 @@ public function addRules(string $regex, $query, string $after = 'bottom') * @param string $name * @param int $places * @param mixed $query - * @return void + * @return mixed */ public function addEndpoint(string $name, int $places = 8191, $query = true) { - add_rewrite_endpoint($name, $places, $query); + return add_rewrite_endpoint($name, $places, $query); } /** @@ -162,11 +162,11 @@ public function hasRules(string $rules) : bool * * @access public * @param bool $force - * @return void + * @return mixed */ public static function flush(bool $force = true) { - flush_rewrite_rules($force); + return flush_rewrite_rules($force); } /** diff --git a/src/lib/Shortcode.php b/src/lib/Shortcode.php index 7c778e29..7d434a11 100644 --- a/src/lib/Shortcode.php +++ b/src/lib/Shortcode.php @@ -46,6 +46,9 @@ public function __construct(?CallableInterface $callable = null) $callable->getCallables() ); } + + // Reset config + $this->resetConfig(); } /** @@ -144,6 +147,20 @@ public function setAtts(array $atts = []) } } + /** + * Instance shortcode. + * + * @access public + * @param string $name + * @param string $path + * @param mixed $args + * @return mixed + */ + public static function i(string $name, $path = 'shortcode', ...$args) + { + return (new Loader())->i($path, $name, $args); + } + /** * Show shortcode error. * @@ -457,18 +474,4 @@ protected function isEnabled(array $atts, string $attr) : bool } return false; } - - /** - * Load shortcode part. - * - * @access protected - * @param string $name - * @param mixed $args - * @return mixed - */ - protected function load(string $name, ...$args) - { - $path = $this->applyPluginFilter('shortcode-path', 'shortcode'); - return (new Loader())->i($path, $name, $args); - } } diff --git a/src/lib/Updater.php b/src/lib/Updater.php index 0d3fd3cd..efaf13af 100644 --- a/src/lib/Updater.php +++ b/src/lib/Updater.php @@ -134,6 +134,9 @@ public function __construct(?string $host = null, array $args = []) * @property count 1 */ $this->addFilter('http_request_args', [$this, 'filterArgs'], 20); + + // Reset config + $this->resetConfig(); } /** diff --git a/src/tr/TraitFormattable.php b/src/tr/TraitFormattable.php index d2088ad8..f037a70d 100644 --- a/src/tr/TraitFormattable.php +++ b/src/tr/TraitFormattable.php @@ -100,6 +100,17 @@ protected function capitalize(string $string) : string return Stringify::capitalize($string); } + /** + * Camelcase string. + * + * @access protected + * @inheritdoc + */ + protected function camelcase(string $string) : string + { + return Stringify::camelcase($string); + } + /** * Slugify string. * @@ -404,6 +415,17 @@ protected function formatKeyCase(array $array, int $case = CASE_LOWER) : array return Arrayify::formatKeyCase($array, $case); } + /** + * Map array. + * + * @access protected + * @inheritdoc + */ + protected function map($callback, array $array, ?array $arrays = null) + { + return Arrayify::map($callback, $array, $arrays); + } + /** * Decode JSON. * diff --git a/src/tr/TraitMigratable.php b/src/tr/TraitMigratable.php index 5b23192c..12c511d4 100644 --- a/src/tr/TraitMigratable.php +++ b/src/tr/TraitMigratable.php @@ -19,14 +19,25 @@ trait TraitMigratable { /** - * Create plugin database tables. + * Install plugin database tables. * * @access protected * @inheritdoc */ - protected function migrateTables() : bool + protected function installTables() : bool { - return (new Migrate())->table(); + return (new Migrate())->install(); + } + + /** + * Rebuild plugin database tables. + * + * @access protected + * @inheritdoc + */ + protected function rebuildTables() : bool + { + return (new Migrate())->rebuild(); } /** diff --git a/src/tr/TraitTranslatable.php b/src/tr/TraitTranslatable.php index 0142a16b..d138de6b 100644 --- a/src/tr/TraitTranslatable.php +++ b/src/tr/TraitTranslatable.php @@ -14,14 +14,16 @@ namespace VanillePlugin\tr; -use VanillePlugin\inc\GlobalConst; +use VanillePlugin\inc\{ + GlobalConst, Stringify +}; use VanilleThird\Translator; trait TraitTranslatable { /** * Get site locale. - * + * * @access protected * @inheritdoc */ @@ -32,7 +34,7 @@ protected function getLocale($user = null) : string /** * Get translator locale. - * + * * @access protected * @inheritdoc */ @@ -40,37 +42,49 @@ protected function getTranslatorLocale() : string { return (string)Translator::getLocale(); } - + /** - * Get normalized country code. - * + * Check whether translator is active. + * * @access protected * @inheritdoc */ - protected function getTranslatorCountry(string $locale) : string + protected function hasTranslator() : bool { - return Translator::getCountry($locale); + return (bool)Translator::isActive(); } - + /** - * Get normalized language code. + * Normalize locale. * - * @access protected - * @inheritdoc + * @access public + * @param string $locale + * @return string */ - protected function getLanguage(string $locale) : string + public static function normalizeLocale(string $locale) : string { - return Translator::getLanguage($locale); + $locale = Stringify::slugify($locale); + $locale = Stringify::replace('_', '-', $locale); + if ( !Stringify::contains($locale, '-') ) { + $locale = "{$locale}-{$locale}"; + } + return $locale; } - + /** - * Check whether translator is active (functional). + * Parse lang. * - * @access protected - * @inheritdoc + * @access public + * @param string $locale + * @return string */ - protected function hasTranslator() : bool + public static function parseLang(string $locale) : string { - return (bool)Translator::isActive(); + if ( Stringify::contains($locale, '-') ) { + if ( ($locale = explode('-', $locale)) ) { + $locale = $locale[0] ?? ''; + } + } + return $locale; } } diff --git a/tests/AmpTest.php b/tests/AmpTest.php index 1798f772..11e1e791 100644 --- a/tests/AmpTest.php +++ b/tests/AmpTest.php @@ -10,7 +10,7 @@ * This file if a part of VanillePlugin Framework. */ -use VanillePlugin\third\AMP; +use VanilleThird\AMP; use PHPUnit\Framework\TestCase; final class AmpTest extends TestCase diff --git a/tests/CacheTest.php b/tests/CacheTest.php index 75e8692f..0da534e0 100644 --- a/tests/CacheTest.php +++ b/tests/CacheTest.php @@ -10,7 +10,7 @@ * This file if a part of VanillePlugin Framework. */ -use VanillePlugin\third\Cache; +use VanilleThird\Cache; use PHPUnit\Framework\TestCase; final class CacheTest extends TestCase diff --git a/tests/EditorTest.php b/tests/EditorTest.php index 38273a4d..ceea90ab 100644 --- a/tests/EditorTest.php +++ b/tests/EditorTest.php @@ -10,7 +10,7 @@ * This file if a part of VanillePlugin Framework. */ -use VanillePlugin\third\Editor; +use VanilleThird\Editor; use PHPUnit\Framework\TestCase; final class EditorTest extends TestCase diff --git a/tests/TranslatorTest.php b/tests/TranslatorTest.php index e670096f..e92ef1c8 100644 --- a/tests/TranslatorTest.php +++ b/tests/TranslatorTest.php @@ -10,7 +10,7 @@ * This file if a part of VanillePlugin Framework. */ -use VanillePlugin\third\Translator; +use VanilleThird\Translator; use PHPUnit\Framework\TestCase; final class TranslatorTest extends TestCase