| 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 |
use MainWP\Dashboard\MainWP_Execution_Helper; |
| 13 |
use MainWP\Dashboard\MainWP_DB; |
| 14 |
use MainWP\Dashboard\MainWP_Utility; |
| 15 |
|
| 16 |
/** |
| 17 |
* Class Log_Manager |
| 18 |
* |
| 19 |
* @package MainWP\Dashboard |
| 20 |
*/ |
| 21 |
class Log_Manager { |
| 22 |
|
| 23 |
/** |
| 24 |
* Version |
| 25 |
* |
| 26 |
* @const string Plugin version number. |
| 27 |
* */ |
| 28 |
const VERSION = '5.0.0'; |
| 29 |
|
| 30 |
/** |
| 31 |
* Log_Admin |
| 32 |
* |
| 33 |
* @var \MainWP\Dashboard\Module\Log\Log_Admin Admin class. |
| 34 |
* */ |
| 35 |
public $admin; |
| 36 |
|
| 37 |
/** |
| 38 |
* MainWP_Execution_Helper |
| 39 |
* |
| 40 |
* @var \MainWP\Dashboard\MainWP_Execution_Helper class. |
| 41 |
* */ |
| 42 |
public $executor; |
| 43 |
|
| 44 |
/** |
| 45 |
* Holds Instance of settings object |
| 46 |
* |
| 47 |
* @var Log_Settings |
| 48 |
*/ |
| 49 |
public $settings; |
| 50 |
|
| 51 |
/** |
| 52 |
* Log_Connectors |
| 53 |
* |
| 54 |
* @var \MainWP\Dashboard\Module\Log\Log_Connectors Connectors class. |
| 55 |
* */ |
| 56 |
public $connectors; |
| 57 |
|
| 58 |
/** |
| 59 |
* Log_DB |
| 60 |
* |
| 61 |
* @var \MainWP\Dashboard\Module\Log\Log_DB DB Class. |
| 62 |
* */ |
| 63 |
public $db; |
| 64 |
|
| 65 |
/** |
| 66 |
* Log |
| 67 |
* |
| 68 |
* @var \MainWP\Dashboard\Module\Log\Log Log Class. |
| 69 |
* */ |
| 70 |
public $log; |
| 71 |
|
| 72 |
/** |
| 73 |
* Log_Install class. |
| 74 |
* |
| 75 |
* @var \MainWP\Dashboard\Module\Log\Log_Install Install class. |
| 76 |
* */ |
| 77 |
public $install; |
| 78 |
|
| 79 |
/** |
| 80 |
* Locations. |
| 81 |
* |
| 82 |
* @var array URLs and Paths used by the plugin. |
| 83 |
*/ |
| 84 |
public $locations = array(); |
| 85 |
|
| 86 |
/** |
| 87 |
* Last log created. |
| 88 |
* |
| 89 |
* @var int last time. |
| 90 |
* */ |
| 91 |
public static $last_non_mainwp_action_last_object_id = null; |
| 92 |
|
| 93 |
/** |
| 94 |
* Protected static variable to hold the single instance of the class. |
| 95 |
* |
| 96 |
* @var mixed Default null |
| 97 |
*/ |
| 98 |
protected static $instance = null; |
| 99 |
|
| 100 |
/** |
| 101 |
* Return the single instance of the class. |
| 102 |
* |
| 103 |
* @return mixed $instance The single instance of the class. |
| 104 |
*/ |
| 105 |
public static function instance() { |
| 106 |
if ( is_null( static::$instance ) ) { |
| 107 |
static::$instance = new self(); |
| 108 |
} |
| 109 |
return static::$instance; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Plugin constructor. |
| 114 |
* |
| 115 |
* Run each time the class is called. |
| 116 |
*/ |
| 117 |
public function __construct() { |
| 118 |
|
| 119 |
$mod_log_dir = MAINWP_MODULES_DIR . 'logs/'; |
| 120 |
$this->locations = array( |
| 121 |
'dir' => $mod_log_dir, |
| 122 |
'url' => MAINWP_MODULES_URL . 'logs/', |
| 123 |
'inc_dir' => $mod_log_dir . 'includes/', |
| 124 |
'class_dir' => $mod_log_dir . 'classes/', |
| 125 |
); |
| 126 |
|
| 127 |
spl_autoload_register( array( $this, 'autoload' ) ); |
| 128 |
|
| 129 |
// Load helper functions. |
| 130 |
require_once $this->locations['inc_dir'] . 'functions.php'; // NOSONAR - WP compatible. |
| 131 |
|
| 132 |
$driver = new Log_DB_Driver_WPDB(); |
| 133 |
$this->db = new Log_DB( $driver ); |
| 134 |
$this->executor = MainWP_Execution_Helper::instance(); |
| 135 |
$this->settings = new Log_Settings( $this ); |
| 136 |
|
| 137 |
// Load logger class. |
| 138 |
$this->log = new Log( $this ); |
| 139 |
|
| 140 |
// Load settings and connectors after widgets_init and before the default init priority. |
| 141 |
add_action( 'init', array( $this, 'init' ), 9 ); |
| 142 |
|
| 143 |
// Change DB driver after plugin loaded if any add-ons want to replace. |
| 144 |
add_action( 'plugins_loaded', array( $this, 'plugins_loaded' ), 20 ); |
| 145 |
|
| 146 |
// Load admin area classes. |
| 147 |
if ( is_admin() || ( defined( 'WP_CLI' ) && WP_CLI ) ) { |
| 148 |
$this->admin = new Log_Admin( $this ); |
| 149 |
} elseif ( defined( 'DOING_CRON' ) && DOING_CRON ) { |
| 150 |
$this->admin = new Log_Admin( $this, $driver ); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Autoloader for classes. |
| 156 |
* |
| 157 |
* @param string $class_name class name. |
| 158 |
*/ |
| 159 |
public function autoload( $class_name ) { |
| 160 |
|
| 161 |
if ( ! preg_match( '/^(?P<namespace>.+)\\\\(?P<autoload>[^\\\\]+)$/', $class_name, $matches ) ) { |
| 162 |
return; |
| 163 |
} |
| 164 |
|
| 165 |
static $reflection; |
| 166 |
|
| 167 |
if ( empty( $reflection ) ) { |
| 168 |
$reflection = new \ReflectionObject( $this ); |
| 169 |
} |
| 170 |
|
| 171 |
if ( $reflection->getNamespaceName() !== $matches['namespace'] ) { |
| 172 |
return; |
| 173 |
} |
| 174 |
|
| 175 |
$autoload_name = $matches['autoload']; |
| 176 |
$autoload_dir = \trailingslashit( $this->locations['dir'] ); |
| 177 |
$load_dirs = array( |
| 178 |
'classes' => 'class', |
| 179 |
'pages' => 'page', |
| 180 |
'widgets' => 'widget', |
| 181 |
); |
| 182 |
foreach ( $load_dirs as $dir => $prefix ) { |
| 183 |
$dir = $dir . '/'; |
| 184 |
$autoload_path = sprintf( '%s%s%s-%s.php', $autoload_dir, $dir, $prefix, strtolower( str_replace( '_', '-', $autoload_name ) ) ); |
| 185 |
if ( is_readable( $autoload_path ) ) { |
| 186 |
require_once $autoload_path; // NOSONAR - WP compatible. |
| 187 |
return; |
| 188 |
} |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
/** |
| 194 |
* Get internal connectors. |
| 195 |
*/ |
| 196 |
public function get_internal_connectors() { |
| 197 |
return array( |
| 198 |
'compact', |
| 199 |
); |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* Load Log_Connectors. |
| 204 |
* |
| 205 |
* @action init |
| 206 |
* |
| 207 |
* @uses \MainWP\Dashboard\Module\Log\Log_Connectors |
| 208 |
*/ |
| 209 |
public function init() { |
| 210 |
$this->connectors = new Log_Connectors( $this ); |
| 211 |
} |
| 212 |
|
| 213 |
/** |
| 214 |
* Getter for the version number. |
| 215 |
* |
| 216 |
* @return string |
| 217 |
*/ |
| 218 |
public function get_version() { |
| 219 |
return static::VERSION; |
| 220 |
} |
| 221 |
|
| 222 |
/** |
| 223 |
* Change plugin database driver in case driver plugin loaded after logs. |
| 224 |
* |
| 225 |
* @uses \MainWP\Dashboard\Module\Log\Log_DB |
| 226 |
* @uses \MainWP\Dashboard\Module\Log\Log_DB_Driver_WPDB |
| 227 |
*/ |
| 228 |
public function plugins_loaded() { |
| 229 |
// Load DB helper interface/class. |
| 230 |
$driver_class = '\MainWP\Dashboard\Module\Log\Log_DB_Driver_WPDB'; |
| 231 |
|
| 232 |
if ( class_exists( $driver_class ) ) { |
| 233 |
$driver = new $driver_class(); |
| 234 |
$this->db = new Log_DB( $driver ); |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
/** |
| 240 |
* Method sync_log_site_actions(). |
| 241 |
* |
| 242 |
* Sync site actions data. |
| 243 |
* |
| 244 |
* @param int $site_id site id. |
| 245 |
* @param array $sync_actions action data. |
| 246 |
* @param object $website website data. |
| 247 |
* |
| 248 |
* @return bool |
| 249 |
*/ |
| 250 |
public function sync_log_site_actions( $site_id, $sync_actions, $website ) { // phpcs:ignore -- NOSONAR - complex. |
| 251 |
|
| 252 |
if ( empty( $sync_actions ) || ! is_array( $sync_actions ) ) { |
| 253 |
return false; |
| 254 |
} |
| 255 |
|
| 256 |
MainWP_Utility::array_sort_existed_keys( $sync_actions, 'created', SORT_NUMERIC ); |
| 257 |
|
| 258 |
foreach ( $sync_actions as $index => $data ) { |
| 259 |
$object_id = sanitize_text_field( $index ); |
| 260 |
|
| 261 |
if ( ! is_array( $data ) || empty( $data['action_user'] ) || empty( $data['created'] ) || empty( $object_id ) ) { |
| 262 |
continue; |
| 263 |
} |
| 264 |
|
| 265 |
if ( null === static::$last_non_mainwp_action_last_object_id ) { |
| 266 |
static::$last_non_mainwp_action_last_object_id = (int) MainWP_DB::instance()->get_website_option( $website, 'non_mainwp_action_last_object_id', 0 ); |
| 267 |
} |
| 268 |
|
| 269 |
if ( (int) $data['created'] <= static::$last_non_mainwp_action_last_object_id ) { |
| 270 |
continue; |
| 271 |
} |
| 272 |
|
| 273 |
if ( $this->db->is_site_action_log_existed( $website->id, $object_id ) ) { |
| 274 |
continue; |
| 275 |
} |
| 276 |
|
| 277 |
$user_meta = array(); |
| 278 |
$meta_data = array(); |
| 279 |
|
| 280 |
if ( isset( $data['meta_data'] ) && is_array( $data['meta_data'] ) ) { |
| 281 |
$meta_data = $data['meta_data']; |
| 282 |
if ( isset( $meta_data['user_meta'] ) && is_array( $meta_data['user_meta'] ) ) { |
| 283 |
$user_meta = $meta_data['user_meta']; |
| 284 |
unset( $meta_data['user_meta'] ); |
| 285 |
} elseif ( isset( $meta_data['meta_data'] ) && ! empty( $meta_data['meta_data'] ) && is_array( $meta_data['meta_data'] ) ) { |
| 286 |
$user_meta = $meta_data['meta_data']; |
| 287 |
unset( $meta_data['meta_data'] ); |
| 288 |
} |
| 289 |
$meta_data['user_meta_json'] = wp_json_encode( $user_meta ); |
| 290 |
} |
| 291 |
|
| 292 |
$sum = ! empty( $meta_data['name'] ) ? esc_html( $meta_data['name'] ) : 'WP Core'; |
| 293 |
$sum .= ' '; |
| 294 |
$sum .= 'wordpress' !== $data['context'] ? esc_html( ucfirst( rtrim( $data['context'], 's' ) ) ) : 'WordPress'; //phpcs:ignore -- wordpress text. |
| 295 |
|
| 296 |
if ( isset( $user_meta['wp_user_id'] ) ) { |
| 297 |
$user_id = ! empty( $user_meta['wp_user_id'] ) ? sanitize_text_field( $user_meta['wp_user_id'] ) : 0; |
| 298 |
} elseif ( ! empty( $user_meta['user_id'] ) ) { // to compatible with old child actions data. |
| 299 |
$user_id = sanitize_text_field( $user_meta['user_id'] ); |
| 300 |
} |
| 301 |
|
| 302 |
$actions_mapping = array( |
| 303 |
'installed' => 'install', |
| 304 |
'deleted' => 'delete', |
| 305 |
'activated' => 'activate', |
| 306 |
'deactivated' => 'deactivate', |
| 307 |
); |
| 308 |
|
| 309 |
$contexts_mapping = array( |
| 310 |
'wordpress' => 'core', |
| 311 |
); |
| 312 |
|
| 313 |
$action = isset( $actions_mapping[ $data['action'] ] ) ? $actions_mapping[ $data['action'] ] : $data['action']; |
| 314 |
$context = isset( $contexts_mapping[ $data['context'] ] ) ? $contexts_mapping[ $data['context'] ] : $data['context']; |
| 315 |
|
| 316 |
$record_mapping = array( |
| 317 |
'site_id' => $site_id, |
| 318 |
'object_id' => $object_id, |
| 319 |
'user_id' => $user_id, |
| 320 |
'created' => $data['created'], |
| 321 |
'item' => $sum, |
| 322 |
'context' => $context, |
| 323 |
'action' => $action, |
| 324 |
'state' => 1, |
| 325 |
'duration' => isset( $data['duration'] ) ? sanitize_text_field( $data['duration'] ) : 0, // sanitize_text_field for seconds. |
| 326 |
'meta' => $meta_data, |
| 327 |
); |
| 328 |
|
| 329 |
do_action( 'mainwp_sync_site_log_install_actions', $website, $record_mapping, $object_id ); |
| 330 |
} |
| 331 |
|
| 332 |
return true; |
| 333 |
} |
| 334 |
} |
| 335 |
|