Skip to content

Commit d006a23

Browse files
committed
Update
1 parent 273ca6d commit d006a23

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+3796
-2129
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name":"jakiboy/vanilleplugin",
3-
"version":"1.0.7",
3+
"version":"1.0.8",
44
"type":"library",
55
"description":"WordPress Plugin Framework",
66
"keywords":[

src/VanillePluginConfig.php

Lines changed: 122 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,10 @@ protected static function getStatic() : object
8484
}
8585

8686
/**
87-
* Set plugin config.
87+
* Init plugin config.
8888
*
8989
* @access protected
9090
* @return void
91-
* @throws ConfigurationException
9291
*/
9392
protected function initConfig()
9493
{
@@ -144,7 +143,7 @@ protected function parseConfig()
144143
}
145144

146145
/**
147-
* Reset config objects.
146+
* Reset config object.
148147
*
149148
* @access protected
150149
* @return void
@@ -165,9 +164,13 @@ protected function getConfig(?string $key = null)
165164
{
166165
$this->initConfig();
167166
if ( $key ) {
168-
return $this->global->{$key} ?? null;
167+
$data = $this->global->{$key} ?? null;
168+
169+
} else {
170+
$data = $this->global;
169171
}
170-
return $this->global;
172+
$this->resetConfig();
173+
return $data;
171174
}
172175

173176
/**
@@ -189,14 +192,47 @@ protected function updateConfig(array $options = [], $args = 64|128|256) : bool
189192
}
190193
}
191194

192-
$data['routes'] = (object)$data['routes'];
193-
$data['cron'] = (object)$data['cron'];
194-
$data['assets'] = (object)$data['assets'];
195+
$data['cron'] = (object)$data['cron'];
196+
$data['hooks'] = (object)$data['hooks'];
197+
$data['settings'] = (object)$data['settings'];
198+
$data['inputs'] = (object)$data['inputs'];
199+
$data['assets'] = (object)$data['assets'];
200+
195201
$data = $this->formatJson($data, $args);
196-
197202
return $this->writeFile($this->getRoot($this->config), $data);
198203
}
199204

205+
/**
206+
* Load configuration file.
207+
*
208+
* @access protected
209+
* @param string $config
210+
* @param bool $isArray
211+
* @return mixed
212+
*/
213+
protected function loadConfig(string $config, bool $isArray = false)
214+
{
215+
$value = false;
216+
$dir = dirname($this->getRoot($this->config));
217+
218+
if ( $this->cacheable ) {
219+
$key = $this->applyPrefix($config);
220+
if ( !($value = $this->getTransient($key)) ) {
221+
if ( $this->isFile( ($json = "{$dir}/{$config}.json") ) ) {
222+
$value = $this->decodeJson($this->readfile($json), $isArray);
223+
}
224+
$this->setTransient($key, $value, 0);
225+
}
226+
227+
} else {
228+
if ( $this->isFile( ($json = "{$dir}/{$config}.json") ) ) {
229+
$value = $this->decodeJson($this->readfile($json), $isArray);
230+
}
231+
}
232+
233+
return $value;
234+
}
235+
200236
/**
201237
* Set global config path.
202238
*
@@ -210,7 +246,7 @@ protected function setConfigPath($path = '/global.json')
210246
}
211247

212248
/**
213-
* Get dynamic root.
249+
* Get dynamic relative root.
214250
*
215251
* @access protected
216252
* @param string $sub
@@ -229,7 +265,7 @@ protected function getRoot(?string $sub = null) : string
229265
}
230266

231267
/**
232-
* Get dynamic namespace.
268+
* Get dynamic namespace by root.
233269
*
234270
* @access protected
235271
* @return string
@@ -408,7 +444,7 @@ protected function getTempPath(?string $sub = null) : string
408444
}
409445

410446
/**
411-
* Get static expire.
447+
* Get static TTL.
412448
*
413449
* @access protected
414450
* @return int
@@ -419,6 +455,32 @@ protected function getExpireIn() : int
419455
$data = $config->ttl ?? 0;
420456
return (int)$data;
421457
}
458+
459+
/**
460+
* Get static timeout.
461+
*
462+
* @access protected
463+
* @return int
464+
*/
465+
protected function getTimeout() : int
466+
{
467+
$config = $this->getConfig('options');
468+
$data = $config->timeout ?? 0;
469+
return (int)$data;
470+
}
471+
472+
/**
473+
* Get static secret key.
474+
*
475+
* @access protected
476+
* @return string
477+
*/
478+
protected function getSecret() : string
479+
{
480+
$config = $this->getConfig('options');
481+
$data = $config->secret ?? 0;
482+
return (string)$data;
483+
}
422484

423485
/**
424486
* Get static view path.
@@ -505,6 +567,7 @@ protected function getAjax() : object
505567
if ( !($data = $this->loadConfig('ajax')) ) {
506568
$data = $this->global->ajax ?? [];
507569
}
570+
$this->resetConfig();
508571
return (object)$data;
509572
}
510573

@@ -544,6 +607,7 @@ protected function getPluginRoles() : array
544607
if ( !($data = $this->loadConfig('roles', true)) ) {
545608
$data = $this->global->roles ?? [];
546609
}
610+
$this->resetConfig();
547611
return (array)$data;
548612
}
549613

@@ -559,22 +623,24 @@ protected function getCron() : array
559623
if ( !($data = $this->loadConfig('cron', true)) ) {
560624
$data = $this->global->cron ?? [];
561625
}
626+
$this->resetConfig();
562627
return (array)$data;
563628
}
564629

565630
/**
566631
* Get api routes.
567632
*
568633
* @access protected
569-
* @return object
634+
* @return array
570635
*/
571-
protected function getRoutes() : object
636+
protected function getRoutes() : array
572637
{
573638
$this->initConfig();
574-
if ( !($data = $this->loadConfig('routes')) ) {
639+
if ( !($data = $this->loadConfig('routes', true)) ) {
575640
$data = $this->global->routes ?? [];
576641
}
577-
return (object)$data;
642+
$this->resetConfig();
643+
return (array)$data;
578644
}
579645

580646
/**
@@ -589,6 +655,7 @@ protected function getRequirements() : object
589655
if ( !($data = $this->loadConfig('requirements')) ) {
590656
$data = $this->global->requirements ?? [];
591657
}
658+
$this->resetConfig();
592659
return (object)$data;
593660
}
594661

@@ -604,6 +671,23 @@ protected function getHooks() : array
604671
if ( !($data = $this->loadConfig('hooks', true)) ) {
605672
$data = $this->global->hooks ?? [];
606673
}
674+
$this->resetConfig();
675+
return (array)$data;
676+
}
677+
678+
/**
679+
* Get settings inputs.
680+
*
681+
* @access protected
682+
* @return array
683+
*/
684+
protected function getInputs() : array
685+
{
686+
$this->initConfig();
687+
if ( !($data = $this->loadConfig('inputs', true)) ) {
688+
$data = $this->global->inputs ?? [];
689+
}
690+
$this->resetConfig();
607691
return (array)$data;
608692
}
609693

@@ -619,6 +703,7 @@ protected function getSettings() : array
619703
if ( !($data = $this->loadConfig('settings', true)) ) {
620704
$data = $this->global->settings ?? [];
621705
}
706+
$this->resetConfig();
622707
return (array)$data;
623708
}
624709

@@ -651,6 +736,7 @@ protected function getAssets() : array
651736
if ( !($data = $this->loadConfig('assets', true)) ) {
652737
$data = $this->global->assets ?? [];
653738
}
739+
$this->resetConfig();
654740
return (array)$data;
655741
}
656742

@@ -728,6 +814,7 @@ protected function getStrings() : array
728814
if ( !($data = $this->loadConfig('strings', true)) ) {
729815
$data = $this->global->strings ?? [];
730816
}
817+
$this->resetConfig();
731818
return (array)$data;
732819
}
733820

@@ -784,34 +871,31 @@ protected function getEnv() : string
784871
}
785872

786873
/**
787-
* Load configuration file.
874+
* Get plugin remote server.
788875
*
789876
* @access protected
790-
* @param string $config
791-
* @param bool $isArray
792-
* @return mixed
877+
* @return array
793878
*/
794-
protected function loadConfig(string $config, bool $isArray = false)
879+
protected function getRemoteServer() : array
795880
{
796-
$value = false;
797-
$dir = dirname($this->getRoot($this->config));
798-
799-
if ( $this->cacheable ) {
800-
$key = $this->applyPrefix($config);
801-
if ( !($value = $this->getTransient($key)) ) {
802-
if ( $this->isFile( ($json = "{$dir}/{$config}.json") ) ) {
803-
$value = $this->decodeJson($this->readfile($json), $isArray);
804-
}
805-
$this->setTransient($key, $value, 0);
806-
}
807-
808-
} else {
809-
if ( $this->isFile( ($json = "{$dir}/{$config}.json") ) ) {
810-
$value = $this->decodeJson($this->readfile($json), $isArray);
811-
}
881+
$this->initConfig();
882+
if ( !($data = $this->loadConfig('remote', true)) ) {
883+
$data = $this->global->remote ?? [];
812884
}
885+
$this->resetConfig();
886+
return (array)$data;
887+
}
813888

814-
return $value;
889+
/**
890+
* Get plugin remote server host.
891+
*
892+
* @access protected
893+
* @return string
894+
*/
895+
protected function getHost() : string
896+
{
897+
$host = $this->getRemoteServer()['host'] ?? false;
898+
return $host ?: '';
815899
}
816900

817901
/**

0 commit comments

Comments
 (0)