PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / trunk
MainWP Dashboard: Self-hosted WordPress Management for Agencies vtrunk
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 5.2.1 5.2.2 5.3 All 153 releases
mainwp / modules / logs / classes / class-log-connector.php

class-log-connector.php in MainWP Dashboard: Self-hosted WordPress Management for Agencies trunk, at modules/logs/classes/class-log-connector.php

251 lines 7.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * MainWP Database Site Actions
4 *
5 * This file handles all interactions with the Site Actions DB.
6 *
7 * @package MainWP/Dashboard
8 */
9
10 namespace MainWP\Dashboard\Module\Log;
11
12 // Exit if accessed directly.
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit;
15 }
16
17 /**
18 * Class Log_Connector
19 *
20 * @package MainWP\Dashboard
21 */
22 abstract class Log_Connector {
23 /**
24 * Connector slug
25 *
26 * @var string
27 */
28 public $name = null;
29
30 /**
31 * Actions registered for this connector
32 *
33 * @var array
34 */
35 public $actions = array();
36
37 /**
38 * Filter registered for this connector
39 *
40 * @var array
41 */
42 public $filters = array();
43
44 /**
45 * Holds connector registration status flag.
46 *
47 * @var bool
48 */
49 private $is_registered = false;
50
51 /**
52 * Is the connector currently registered?
53 *
54 * @return boolean
55 */
56 public function is_registered() {
57 return $this->is_registered;
58 }
59
60 /**
61 * Register all context hooks
62 */
63 public function register() { //phpcs:ignore -- overrided.
64
65 if ( $this->is_registered ) {
66 return;
67 }
68
69 foreach ( $this->actions as $action ) {
70 add_action( $action, array( $this, 'callback' ), 10, 99 );
71 }
72
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 }
78
79 $this->is_registered = true;
80 }
81
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.
89
90 // Call the real function.
91 if ( is_callable( $callback ) ) {
92 return call_user_func_array( $callback, func_get_args() );
93 }
94 }
95
96
97
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;
113
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 }
132
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 }
143
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 }
157
158 if ( ! is_array( $old_value ) ) {
159 return array_keys( $new_value );
160 }
161
162 if ( ! is_array( $new_value ) ) {
163 return array_keys( $old_value );
164 }
165
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 );
174
175 $result = array_keys( $diff );
176
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 ) );
181
182 $result = array_merge( $result, $unique_keys_old, $unique_keys_new );
183
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 );
194
195 $result = array_values( array_unique( $result ) );
196
197 if ( false === $deep ) {
198 return $result; // Return an numerical based array with changed TOP PARENT keys only.
199 }
200
201 $result = array_fill_keys( $result, null );
202
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 }
222
223 return $result;
224 }
225
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 }
250 }
251