PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 4.14.2
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v4.14.2
5.11.1 5.11.0 5.10.2 5.10.1 trunk 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.3.2 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.1.0 4.1.1 4.1.2 4.1.3 4.10.0 4.11.0 4.12.0 4.13.0 4.13.2 4.13.3 4.13.4 4.13.5 4.14.0 4.14.1 4.14.2 4.15.0 4.15.1 4.15.2 4.15.3 4.2.0 4.3.0 4.3.1 4.4.1 4.4.2 4.5.0 4.6.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.10.0 5.2.0 5.2.1 5.2.2 5.3.0 5.3.1 5.3.2 5.3.3 5.6.0 5.6.1 5.7.0 5.7.1 5.8.0 5.8.1 5.8.2
matomo / classes / WpMatomo / Site / Sync / SyncConfig.php
matomo / classes / WpMatomo / Site / Sync Last commit date
SyncConfig.php 4 years ago
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