| 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 |
/** |
| 13 |
* Class Log_Connector |
| 14 |
* |
| 15 |
* @package MainWP\Dashboard |
| 16 |
*/ |
| 17 |
abstract class Log_Connector { |
| 18 |
/** |
| 19 |
* Connector slug |
| 20 |
* |
| 21 |
* @var string |
| 22 |
*/ |
| 23 |
public $name = null; |
| 24 |
|
| 25 |
/** |
| 26 |
* Actions registered for this connector |
| 27 |
* |
| 28 |
* @var array |
| 29 |
*/ |
| 30 |
public $actions = array(); |
| 31 |
|
| 32 |
|
| 33 |
/** |
| 34 |
* Holds connector registration status flag. |
| 35 |
* |
| 36 |
* @var bool |
| 37 |
*/ |
| 38 |
private $is_registered = false; |
| 39 |
|
| 40 |
/** |
| 41 |
* Is the connector currently registered? |
| 42 |
* |
| 43 |
* @return boolean |
| 44 |
*/ |
| 45 |
public function is_registered() { |
| 46 |
return $this->is_registered; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Register all context hooks |
| 51 |
*/ |
| 52 |
public function register() { //phpcs:ignore -- overrided. |
| 53 |
|
| 54 |
if ( $this->is_registered ) { |
| 55 |
return; |
| 56 |
} |
| 57 |
|
| 58 |
foreach ( $this->actions as $action ) { |
| 59 |
add_action( $action, array( $this, 'callback' ), 10, 99 ); |
| 60 |
} |
| 61 |
|
| 62 |
$this->is_registered = true; |
| 63 |
} |
| 64 |
|
| 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. |
| 72 |
|
| 73 |
// Call the real function. |
| 74 |
if ( is_callable( $callback ) ) { |
| 75 |
return call_user_func_array( $callback, func_get_args() ); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 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 |
$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 |
return call_user_func_array( array( Log_Manager::instance()->log, 'log' ), compact( 'connector', 'message', 'args', 'site_id', 'context', 'action', 'state', 'user_id' ) ); |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Compare two values and return changed keys if they are arrays |
| 118 |
* |
| 119 |
* @param mixed $old_value Value before change. |
| 120 |
* @param mixed $new_value Value after change. |
| 121 |
* @param bool|int $deep Get array children changes keys as well, not just parents. |
| 122 |
* |
| 123 |
* @return array |
| 124 |
*/ |
| 125 |
public function get_changed_keys( $old_value, $new_value, $deep = false ) { //phpcs:ignore -- NOSONAR - complex. |
| 126 |
if ( ! is_array( $old_value ) && ! is_array( $new_value ) ) { |
| 127 |
return array(); |
| 128 |
} |
| 129 |
|
| 130 |
if ( ! is_array( $old_value ) ) { |
| 131 |
return array_keys( $new_value ); |
| 132 |
} |
| 133 |
|
| 134 |
if ( ! is_array( $new_value ) ) { |
| 135 |
return array_keys( $old_value ); |
| 136 |
} |
| 137 |
|
| 138 |
$diff = array_udiff_assoc( |
| 139 |
$old_value, |
| 140 |
$new_value, |
| 141 |
function ( $value1, $value2 ) { |
| 142 |
// Compare potentially complex nested arrays. |
| 143 |
return wp_json_encode( $value1 ) !== wp_json_encode( $value2 ); |
| 144 |
} |
| 145 |
); |
| 146 |
|
| 147 |
$result = array_keys( $diff ); |
| 148 |
|
| 149 |
// find unexisting keys in old or new value. |
| 150 |
$common_keys = array_keys( array_intersect_key( $old_value, $new_value ) ); |
| 151 |
$unique_keys_old = array_values( array_diff( array_keys( $old_value ), $common_keys ) ); |
| 152 |
$unique_keys_new = array_values( array_diff( array_keys( $new_value ), $common_keys ) ); |
| 153 |
|
| 154 |
$result = array_merge( $result, $unique_keys_old, $unique_keys_new ); |
| 155 |
|
| 156 |
// remove numeric indexes. |
| 157 |
$result = array_filter( |
| 158 |
$result, |
| 159 |
function ( $value ) { |
| 160 |
// @codingStandardsIgnoreStart |
| 161 |
// check if is not valid number (is_int, is_numeric and ctype_digit are not enough) |
| 162 |
return (string) (int) $value !== (string) $value; |
| 163 |
// @codingStandardsIgnoreEnd |
| 164 |
} |
| 165 |
); |
| 166 |
|
| 167 |
$result = array_values( array_unique( $result ) ); |
| 168 |
|
| 169 |
if ( false === $deep ) { |
| 170 |
return $result; // Return an numerical based array with changed TOP PARENT keys only. |
| 171 |
} |
| 172 |
|
| 173 |
$result = array_fill_keys( $result, null ); |
| 174 |
|
| 175 |
foreach ( $result as $key => $val ) { |
| 176 |
if ( in_array( $key, $unique_keys_old, true ) ) { |
| 177 |
$result[ $key ] = false; // Removed. |
| 178 |
} elseif ( in_array( $key, $unique_keys_new, true ) ) { |
| 179 |
$result[ $key ] = true; // Added. |
| 180 |
} elseif ( $deep ) { // Changed, find what changed, only if we're allowed to explore a new level. |
| 181 |
if ( is_array( $old_value[ $key ] ) && is_array( $new_value[ $key ] ) ) { |
| 182 |
$inner = array(); |
| 183 |
$parent = $key; |
| 184 |
--$deep; |
| 185 |
$changed = $this->get_changed_keys( $old_value[ $key ], $new_value[ $key ], $deep ); |
| 186 |
foreach ( $changed as $child => $change ) { |
| 187 |
$inner[ $parent . '::' . $child ] = $change; |
| 188 |
} |
| 189 |
$result[ $key ] = 0; // Changed parent which has a changed children. |
| 190 |
$result = array_merge( $result, $inner ); |
| 191 |
} |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
return $result; |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* Handle sanitize POST data. |
| 200 |
* |
| 201 |
* @param array $data input data. |
| 202 |
* |
| 203 |
* @return array |
| 204 |
*/ |
| 205 |
protected function sanitize_data( $data ) { |
| 206 |
if ( ! is_array( $data ) ) { |
| 207 |
return array(); |
| 208 |
} |
| 209 |
|
| 210 |
// Sanitize all record values. |
| 211 |
return array_map( |
| 212 |
function ( $value ) { |
| 213 |
if ( ! is_array( $value ) ) { |
| 214 |
return wp_strip_all_tags( $value ); |
| 215 |
} |
| 216 |
|
| 217 |
return $value; |
| 218 |
}, |
| 219 |
$data |
| 220 |
); |
| 221 |
} |
| 222 |
} |
| 223 |
|