PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 6.2.2
MainWP Dashboard: Self-hosted WordPress Management for Agencies v6.2.2
6.2.3 6.2.2 6.2.1 6.2 6.1.8 6.1.7 6.1.6 6.1.5 6.1.4 6.1.3 6.1.2 6.1.1 6.1 6.0.12 6.0.11 4.6.0.1 5.0 5.0.1 5.0.2 5.0.3 5.0.3.1 5.0.3.2 5.1 5.1.1 5.2 All 156 releases
← All changes | modules/logs/classes/class-log-connector.php +202 -175 5.0.1 → 6.2.2 View file →
@@ -8,8 +8,13 @@
8 8 */
9 9
10 10 namespace MainWP\Dashboard\Module\Log;
11 11
12 +// Exit if accessed directly.
13 +if ( ! defined( 'ABSPATH' ) ) {
14 + exit;
15 +}
16 +
12 17 /**
13 18 * Class Log_Connector
14 19 *
15 20 * @package MainWP\Dashboard
@@ -14,210 +19,232 @@
14 19 *
15 20 * @package MainWP\Dashboard
16 21 */
17 22 abstract class Log_Connector {
18 - /**
19 - * Connector slug
20 - *
21 - * @var string
22 - */
23 - public $name = null;
23 + /**
24 + * Connector slug
25 + *
26 + * @var string
27 + */
28 + public $name = null;
24 29
25 - /**
26 - * Actions registered for this connector
27 - *
28 - * @var array
29 - */
30 - public $actions = array();
30 + /**
31 + * Actions registered for this connector
32 + *
33 + * @var array
34 + */
35 + public $actions = array();
31 36
37 + /**
38 + * Filter registered for this connector
39 + *
40 + * @var array
41 + */
42 + public $filters = array();
32 43
33 - /**
34 - * Holds connector registration status flag.
35 - *
36 - * @var bool
37 - */
38 - private $is_registered = false;
44 + /**
45 + * Holds connector registration status flag.
46 + *
47 + * @var bool
48 + */
49 + private $is_registered = false;
39 50
40 - /**
41 - * Is the connector currently registered?
42 - *
43 - * @return boolean
44 - */
45 - public function is_registered() {
46 - return $this->is_registered;
47 - }
51 + /**
52 + * Is the connector currently registered?
53 + *
54 + * @return boolean
55 + */
56 + public function is_registered() {
57 + return $this->is_registered;
58 + }
48 59
49 - /**
50 - * Register all context hooks
51 - */
52 - public function register() { //phpcs:ignore -- overrided.
60 + /**
61 + * Register all context hooks
62 + */
63 + public function register() { //phpcs:ignore -- overrided.
53 64
54 - if ( $this->is_registered ) {
55 - return;
56 - }
65 + if ( $this->is_registered ) {
66 + return;
67 + }
57 68
58 - foreach ( $this->actions as $action ) {
59 - add_action( $action, array( $this, 'callback' ), 10, 99 );
60 - }
69 + foreach ( $this->actions as $action ) {
70 + add_action( $action, array( $this, 'callback' ), 10, 99 );
71 + }
61 72
62 - $this->is_registered = true;
63 - }
73 + if ( ! empty( $this->filters ) && is_array( $this->filters ) ) {
74 + foreach ( $this->filters as $filter ) {
75 + add_action( $filter, array( $this, 'callback' ), 10, 99 );
76 + }
77 + }
64 78
65 - /**
66 - * Callback for all registered hooks throughout Log
67 - * Looks for a class method with the convention: "callback_{action name}"
68 - */
69 - public function callback() {
70 - $action = current_filter();
71 - $callback = array( $this, 'callback_' . preg_replace( '/[^A-Za-z0-9_\-]/', '_', $action ) ); // to fix A-Z charater in callback name.
79 + $this->is_registered = true;
80 + }
72 81
73 - // Call the real function.
74 - if ( is_callable( $callback ) ) {
75 - return call_user_func_array( $callback, func_get_args() );
76 - }
77 - }
82 + /**
83 + * Callback for all registered hooks throughout Log
84 + * Looks for a class method with the convention: "callback_{action name}"
85 + */
86 + public function callback() {
87 + $action = current_filter();
88 + $callback = array( $this, 'callback_' . preg_replace( '/[^A-Za-z0-9_\-]/', '_', $action ) ); // to fix A-Z charater in callback name.
78 89
90 + // Call the real function.
91 + if ( is_callable( $callback ) ) {
92 + return call_user_func_array( $callback, func_get_args() );
93 + }
94 + }
79 95
80 96
81 - /**
82 - * Log handler
83 - *
84 - * @param string $message sprintf-ready error message string.
85 - * @param array $args sprintf (and extra) arguments to use.
86 - * @param int $site_id Target site id.
87 - * @param string $context Context of the event.
88 - * @param string $action Action of the event.
89 - * @param int|null $state action status: null - N/A, 0 - failed, 1 - success.
90 - * @param int $user_id User responsible for the event.
91 - *
92 - * @return bool
93 - */
94 - public function log( $message, $args, $site_id, $context, $action, $state = null, $user_id = null ) {
95 - $connector = $this->name;
96 97
97 - $data = apply_filters(
98 - 'mainwp_module_log_data',
99 - compact( 'connector', 'message', 'args', 'site_id', 'context', 'action', 'state', 'user_id' )
100 - );
101 - if ( ! $data ) {
102 - return false;
103 - } else {
104 - $connector = $data['connector'];
105 - $message = $data['message'];
106 - $args = $data['args'];
107 - $site_id = $data['site_id'];
108 - $context = $data['context'];
109 - $action = $data['action'];
110 - $user_id = $data['user_id'];
111 - $state = $data['state'];
112 - }
113 - $created_timestamp = null;
114 - return call_user_func_array( array( Log_Manager::instance()->log, 'log' ), compact( 'connector', 'message', 'args', 'site_id', 'context', 'action', 'state', 'user_id' ) );
115 - }
98 + /**
99 + * Log handler
100 + *
101 + * @param string $message sprintf-ready error message string.
102 + * @param array $args sprintf (and extra) arguments to use.
103 + * @param int $site_id Target site id.
104 + * @param string $context Context of the event.
105 + * @param string $action Action of the event.
106 + * @param int|null $state action status: null - N/A, 0 - failed, 1 - success.
107 + * @param int $user_id User responsible for the event.
108 + *
109 + * @return bool
110 + */
111 + public function log( $message, $args, $site_id, $context, $action, $state = null, $user_id = null ) {
112 + $connector = $this->name;
116 113
117 - /**
118 - * Compare two values and return changed keys if they are arrays
119 - *
120 - * @param mixed $old_value Value before change.
121 - * @param mixed $new_value Value after change.
122 - * @param bool|int $deep Get array children changes keys as well, not just parents.
123 - *
124 - * @return array
125 - */
126 - public function get_changed_keys( $old_value, $new_value, $deep = false ) {
127 - if ( ! is_array( $old_value ) && ! is_array( $new_value ) ) {
128 - return array();
129 - }
114 + $data = apply_filters(
115 + 'mainwp_module_log_data',
116 + compact( 'connector', 'message', 'args', 'site_id', 'context', 'action', 'state', 'user_id' )
117 + );
118 + if ( ! $data ) {
119 + return false;
120 + } else {
121 + $connector = $data['connector'];
122 + $message = $data['message'];
123 + $args = $data['args'];
124 + $site_id = $data['site_id'];
125 + $context = $data['context'];
126 + $action = $data['action'];
127 + $user_id = $data['user_id'];
128 + $state = $data['state'];
129 + }
130 + return call_user_func_array( array( Log_Manager::instance()->log, 'log' ), compact( 'connector', 'message', 'args', 'site_id', 'context', 'action', 'state', 'user_id' ) );
131 + }
130 132
131 - if ( ! is_array( $old_value ) ) {
132 - return array_keys( $new_value );
133 - }
133 + /**
134 + * Log record.
135 + *
136 + * @param array $recordarr sprintf (and extra) arguments to use.
137 + *
138 + * @return bool|WP_Error True if updated, otherwise false|WP_Error
139 + */
140 + public function log_record( $recordarr ) { //phpcs:ignore -- NOSONAR - compatible.
141 + return call_user_func_array( array( Log_Manager::instance()->log, 'log_record' ), compact( 'recordarr' ) );
142 + }
134 143
135 - if ( ! is_array( $new_value ) ) {
136 - return array_keys( $old_value );
137 - }
144 + /**
145 + * Compare two values and return changed keys if they are arrays
146 + *
147 + * @param mixed $old_value Value before change.
148 + * @param mixed $new_value Value after change.
149 + * @param bool|int $deep Get array children changes keys as well, not just parents.
150 + *
151 + * @return array
152 + */
153 + public function get_changed_keys( $old_value, $new_value, $deep = false ) { //phpcs:ignore -- NOSONAR - complex.
154 + if ( ! is_array( $old_value ) && ! is_array( $new_value ) ) {
155 + return array();
156 + }
138 157
139 - $diff = array_udiff_assoc(
140 - $old_value,
141 - $new_value,
142 - function ( $value1, $value2 ) {
143 - // Compare potentially complex nested arrays.
144 - return wp_json_encode( $value1 ) !== wp_json_encode( $value2 );
145 - }
146 - );
158 + if ( ! is_array( $old_value ) ) {
159 + return array_keys( $new_value );
160 + }
147 161
148 - $result = array_keys( $diff );
162 + if ( ! is_array( $new_value ) ) {
163 + return array_keys( $old_value );
164 + }
149 165
150 - // find unexisting keys in old or new value.
151 - $common_keys = array_keys( array_intersect_key( $old_value, $new_value ) );
152 - $unique_keys_old = array_values( array_diff( array_keys( $old_value ), $common_keys ) );
153 - $unique_keys_new = array_values( array_diff( array_keys( $new_value ), $common_keys ) );
166 + $diff = array_udiff_assoc(
167 + $old_value,
168 + $new_value,
169 + function ( $value1, $value2 ) {
170 + // Compare potentially complex nested arrays.
171 + return wp_json_encode( $value1 ) !== wp_json_encode( $value2 );
172 + }
173 + );
154 174
155 - $result = array_merge( $result, $unique_keys_old, $unique_keys_new );
175 + $result = array_keys( $diff );
156 176
157 - // remove numeric indexes.
158 - $result = array_filter(
159 - $result,
160 - function ( $value ) {
161 - // @codingStandardsIgnoreStart
162 - // check if is not valid number (is_int, is_numeric and ctype_digit are not enough)
163 - return (string) (int) $value !== (string) $value;
164 - // @codingStandardsIgnoreEnd
165 - }
166 - );
177 + // find unexisting keys in old or new value.
178 + $common_keys = array_keys( array_intersect_key( $old_value, $new_value ) );
179 + $unique_keys_old = array_values( array_diff( array_keys( $old_value ), $common_keys ) );
180 + $unique_keys_new = array_values( array_diff( array_keys( $new_value ), $common_keys ) );
167 181
168 - $result = array_values( array_unique( $result ) );
182 + $result = array_merge( $result, $unique_keys_old, $unique_keys_new );
169 183
170 - if ( false === $deep ) {
171 - return $result; // Return an numerical based array with changed TOP PARENT keys only.
172 - }
184 + // remove numeric indexes.
185 + $result = array_filter(
186 + $result,
187 + function ( $value ) {
188 + // @codingStandardsIgnoreStart
189 + // check if is not valid number (is_int, is_numeric and ctype_digit are not enough)
190 + return (string) (int) $value !== (string) $value;
191 + // @codingStandardsIgnoreEnd
192 + }
193 + );
173 194
174 - $result = array_fill_keys( $result, null );
195 + $result = array_values( array_unique( $result ) );
175 196
176 - foreach ( $result as $key => $val ) {
177 - if ( in_array( $key, $unique_keys_old, true ) ) {
178 - $result[ $key ] = false; // Removed.
179 - } elseif ( in_array( $key, $unique_keys_new, true ) ) {
180 - $result[ $key ] = true; // Added.
181 - } elseif ( $deep ) { // Changed, find what changed, only if we're allowed to explore a new level.
182 - if ( is_array( $old_value[ $key ] ) && is_array( $new_value[ $key ] ) ) {
183 - $inner = array();
184 - $parent = $key;
185 - --$deep;
186 - $changed = $this->get_changed_keys( $old_value[ $key ], $new_value[ $key ], $deep );
187 - foreach ( $changed as $child => $change ) {
188 - $inner[ $parent . '::' . $child ] = $change;
189 - }
190 - $result[ $key ] = 0; // Changed parent which has a changed children.
191 - $result = array_merge( $result, $inner );
192 - }
193 - }
194 - }
197 + if ( false === $deep ) {
198 + return $result; // Return an numerical based array with changed TOP PARENT keys only.
199 + }
195 200
196 - return $result;
197 - }
201 + $result = array_fill_keys( $result, null );
198 202
199 - /**
200 - * Handle sanitize POST data.
201 - *
202 - * @param array $data input data.
203 - *
204 - * @return array
205 - */
206 - protected function sanitize_data( $data ) {
207 - if ( ! is_array( $data ) ) {
208 - return array();
209 - }
203 + foreach ( $result as $key => $val ) {
204 + if ( in_array( $key, $unique_keys_old, true ) ) {
205 + $result[ $key ] = false; // Removed.
206 + } elseif ( in_array( $key, $unique_keys_new, true ) ) {
207 + $result[ $key ] = true; // Added.
208 + } elseif ( $deep ) { // Changed, find what changed, only if we're allowed to explore a new level.
209 + if ( is_array( $old_value[ $key ] ) && is_array( $new_value[ $key ] ) ) {
210 + $inner = array();
211 + $parent = $key;
212 + --$deep;
213 + $changed = $this->get_changed_keys( $old_value[ $key ], $new_value[ $key ], $deep );
214 + foreach ( $changed as $child => $change ) {
215 + $inner[ $parent . '::' . $child ] = $change;
216 + }
217 + $result[ $key ] = 0; // Changed parent which has a changed children.
218 + $result = array_merge( $result, $inner );
219 + }
220 + }
221 + }
210 222
211 - // Sanitize all record values.
212 - return array_map(
213 - function ( $value ) {
214 - if ( ! is_array( $value ) ) {
215 - return wp_strip_all_tags( $value );
216 - }
223 + return $result;
224 + }
217 225
218 - return $value;
219 - },
220 - $data
221 - );
222 - }
226 + /**
227 + * Handle sanitize POST data.
228 + *
229 + * @param array $data input data.
230 + *
231 + * @return array
232 + */
233 + protected function sanitize_data( $data ) {
234 + if ( ! is_array( $data ) ) {
235 + return array();
236 + }
237 +
238 + // Sanitize all record values.
239 + return array_map(
240 + function ( $value ) {
241 + if ( ! is_array( $value ) ) {
242 + return wp_strip_all_tags( $value );
243 + }
244 +
245 + return $value;
246 + },
247 + $data
248 + );
249 + }
223 250 }