| 1 |
<?php |
| 2 |
/* |
| 3 |
* This file is part of the ManageWP Worker plugin. |
| 4 |
* |
| 5 |
* (c) ManageWP LLC <contact@managewp.com> |
| 6 |
* |
| 7 |
* For the full copyright and license information, please view the LICENSE |
| 8 |
* file that was distributed with this source code. |
| 9 |
*/ |
| 10 |
|
| 11 |
/** |
| 12 |
* Class MWP_Configuration_Service |
| 13 |
* This class is service provider for configuration object. |
| 14 |
* It include get/save operations, with singleton to lower database hits. |
| 15 |
* |
| 16 |
* @package src\MWP\Configuration |
| 17 |
*/ |
| 18 |
class MWP_Configuration_Service |
| 19 |
{ |
| 20 |
/** |
| 21 |
* @var MWP_Configuration_Conf |
| 22 |
*/ |
| 23 |
public static $configuration; |
| 24 |
|
| 25 |
/** |
| 26 |
* Returns configuration instance. This is singleton |
| 27 |
* |
| 28 |
* @return MWP_Configuration_Conf |
| 29 |
*/ |
| 30 |
public function getConfiguration() |
| 31 |
{ |
| 32 |
if (!self::$configuration) { |
| 33 |
/** @handled function fix */ |
| 34 |
$configuration = get_option("mwp_worker_configuration"); |
| 35 |
$path = realpath(dirname(__FILE__)."/../../../worker.json"); |
| 36 |
if (empty($configuration) && file_exists($path)) { |
| 37 |
$json = file_get_contents($path); |
| 38 |
$configuration = json_decode($json, true); |
| 39 |
/** @handled function fix */ |
| 40 |
update_option("mwp_worker_configuration", $configuration); |
| 41 |
} |
| 42 |
|
| 43 |
self::$configuration = new MWP_Configuration_Conf($configuration); |
| 44 |
} |
| 45 |
|
| 46 |
return self::$configuration; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Reloads configuration from database, update internal singleton and returns reload object |
| 51 |
* |
| 52 |
* @return MWP_Configuration_Conf |
| 53 |
*/ |
| 54 |
public function reloadConfiguration() |
| 55 |
{ |
| 56 |
/** @handled function fix */ |
| 57 |
self::$configuration = new MWP_Configuration_Conf(get_option("mwp_worker_configuration")); |
| 58 |
|
| 59 |
return self::$configuration; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Save to database configuration instance |
| 64 |
* |
| 65 |
* @param MWP_Configuration_Conf $configuration |
| 66 |
*/ |
| 67 |
public function saveConfiguration(MWP_Configuration_Conf $configuration) |
| 68 |
{ |
| 69 |
self::$configuration = $configuration; |
| 70 |
$data = $configuration->toArray(); |
| 71 |
if (array_key_exists("master_cron_url", $data) && !empty($data['master_cron_url'])) { |
| 72 |
/** @handled function fix */ |
| 73 |
update_option("mwp_worker_configuration", $data); |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
|