SyncConfig.php
128 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Matomo - free/libre analytics platform |
| 4 | * |
| 5 | * @link https://matomo.org |
| 6 | * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 7 | * @package matomo |
| 8 | */ |
| 9 | |
| 10 | namespace WpMatomo\Site\Sync; |
| 11 | |
| 12 | use Piwik\Config as PiwikConfig; |
| 13 | use WpMatomo; |
| 14 | use WpMatomo\Bootstrap; |
| 15 | use WpMatomo\Logger; |
| 16 | use WpMatomo\ScheduledTasks; |
| 17 | use WpMatomo\Settings; |
| 18 | |
| 19 | if ( ! defined( 'ABSPATH' ) ) { |
| 20 | exit; // if accessed directly |
| 21 | } |
| 22 | |
| 23 | class SyncConfig { |
| 24 | |
| 25 | /** |
| 26 | * @var Logger |
| 27 | */ |
| 28 | private $logger; |
| 29 | |
| 30 | /** |
| 31 | * @var Settings |
| 32 | */ |
| 33 | private $settings; |
| 34 | |
| 35 | public function __construct( Settings $settings ) { |
| 36 | $this->logger = new Logger(); |
| 37 | $this->settings = $settings; |
| 38 | } |
| 39 | |
| 40 | public function sync_config_for_current_site() { |
| 41 | if ( $this->settings->is_network_enabled() ) { |
| 42 | $config = PiwikConfig::getInstance(); |
| 43 | $has_change = false; |
| 44 | foreach ( $this->get_all() as $category => $keys ) { |
| 45 | $cat = $config->{$category}; |
| 46 | if ( empty( $cat ) ) { |
| 47 | $cat = []; |
| 48 | } |
| 49 | |
| 50 | if ( empty( $keys ) && ! empty( $cat ) ) { |
| 51 | // need to unset all values |
| 52 | $has_change = true; |
| 53 | $config->{$category} = []; |
| 54 | } |
| 55 | |
| 56 | if ( ! empty( $keys ) ) { |
| 57 | foreach ( $keys as $key => $value ) { |
| 58 | // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison |
| 59 | if ( ! isset( $cat[ $key ] ) || $cat[ $key ] != $value ) { |
| 60 | $has_change = true; |
| 61 | $cat[ $key ] = $value; |
| 62 | $config->{$category} = $cat; |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | if ( $has_change ) { |
| 68 | $config->forceSave(); |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | private function get_all() { |
| 74 | $options = $this->settings->get_global_option( Settings::NETWORK_CONFIG_OPTIONS ); |
| 75 | |
| 76 | if ( empty( $options ) || ! is_array( $options ) ) { |
| 77 | $options = []; |
| 78 | } |
| 79 | |
| 80 | return $options; |
| 81 | } |
| 82 | |
| 83 | public function get_config_value( $group, $key ) { |
| 84 | if ( $this->settings->is_network_enabled() ) { |
| 85 | $config = $this->get_all(); |
| 86 | if ( isset( $config[ $group ][ $key ] ) ) { |
| 87 | return $config[ $group ][ $key ]; |
| 88 | } |
| 89 | } else { |
| 90 | Bootstrap::do_bootstrap(); |
| 91 | $config = PiwikConfig::getInstance(); |
| 92 | $the_group = $config->{$group}; |
| 93 | if ( ! empty( $the_group ) && isset( $the_group[ $key ] ) ) { |
| 94 | return $the_group[ $key ]; |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | public function set_config_value( $group, $key, $value ) { |
| 100 | if ( $this->settings->is_network_enabled() ) { |
| 101 | $config = $this->get_all(); |
| 102 | |
| 103 | if ( ! isset( $config[ $group ] ) ) { |
| 104 | $config[ $group ] = []; |
| 105 | } |
| 106 | $config[ $group ][ $key ] = $value; |
| 107 | |
| 108 | $this->settings->apply_changes( |
| 109 | [ |
| 110 | Settings::NETWORK_CONFIG_OPTIONS => $config, |
| 111 | ] |
| 112 | ); |
| 113 | // need to update all config files |
| 114 | wp_schedule_single_event( time() + 5, ScheduledTasks::EVENT_SYNC ); |
| 115 | } elseif ( ! WpMatomo::is_safe_mode() ) { |
| 116 | Bootstrap::do_bootstrap(); |
| 117 | $config = PiwikConfig::getInstance(); |
| 118 | $the_group = $config->{$group}; |
| 119 | if ( empty( $the_group ) ) { |
| 120 | $the_group = []; |
| 121 | } |
| 122 | $the_group[ $key ] = $value; |
| 123 | $config->{$group} = $the_group; |
| 124 | $config->forceSave(); |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 |