| 1 |
<?php |
| 2 |
/** |
| 3 |
* Module Logs Installer connector class. |
| 4 |
* |
| 5 |
* @package MainWP\Dashboard |
| 6 |
* @version 4.5.1 |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace MainWP\Dashboard\Module\Log; |
| 10 |
|
| 11 |
defined( 'ABSPATH' ) || exit; |
| 12 |
|
| 13 |
/** |
| 14 |
* Class Connector_Installer |
| 15 |
* |
| 16 |
* @package MainWP\Dashboard |
| 17 |
*/ |
| 18 |
class Connector_Installer extends Log_Connector { |
| 19 |
|
| 20 |
/** |
| 21 |
* Connector name. |
| 22 |
* |
| 23 |
* @var string Connector slug. |
| 24 |
* */ |
| 25 |
public $name = 'installer'; |
| 26 |
|
| 27 |
/** |
| 28 |
* Actions names. |
| 29 |
* |
| 30 |
* @var array Actions registered for this connector. |
| 31 |
* */ |
| 32 |
public $actions = array( |
| 33 |
'mainwp_install_update_actions', |
| 34 |
'mainwp_install_plugin_action', // call child function: plugin_action. |
| 35 |
'mainwp_install_theme_action', // call child function: theme_action. |
| 36 |
); |
| 37 |
|
| 38 |
|
| 39 |
/** |
| 40 |
* Return translated connector label. |
| 41 |
* |
| 42 |
* @return string Translated connector label. |
| 43 |
*/ |
| 44 |
public function get_label() { |
| 45 |
return esc_html__( 'Installer', 'mainwp' ); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Return translated action labels. |
| 50 |
* |
| 51 |
* @return array Action label translations. |
| 52 |
*/ |
| 53 |
public function get_action_labels() { |
| 54 |
return array( |
| 55 |
'install' => esc_html__( 'Installed', 'mainwp' ), |
| 56 |
'activate' => esc_html__( 'Activated', 'mainwp' ), |
| 57 |
'deactivate' => esc_html__( 'Deactivated', 'mainwp' ), |
| 58 |
'delete' => esc_html__( 'Deleted', 'mainwp' ), |
| 59 |
'updated' => esc_html__( 'Updated', 'mainwp' ), |
| 60 |
); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Return translated context labels. |
| 65 |
* |
| 66 |
* @return array Context label translations. |
| 67 |
*/ |
| 68 |
public function get_context_labels() { |
| 69 |
return array( |
| 70 |
'plugins' => esc_html__( 'Plugins', 'mainwp' ), |
| 71 |
'themes' => esc_html__( 'Themes', 'mainwp' ), |
| 72 |
'core' => esc_html__( 'Core', 'mainwp' ), |
| 73 |
'translation' => esc_html__( 'Translation', 'mainwp' ), |
| 74 |
); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Log plugin|theme installations. |
| 79 |
* |
| 80 |
* @action mainwp_install_update_actions. |
| 81 |
* |
| 82 |
* @param object $website website. |
| 83 |
* @param string $pAction install action. |
| 84 |
* @param array $data data result. |
| 85 |
* @param string $type action type. |
| 86 |
* @param mixed $post_data post data (option). |
| 87 |
* @param bool $upload true|false install by upload. |
| 88 |
*/ |
| 89 |
public function callback_mainwp_install_update_actions( $website, $pAction, $data, $type, $post_data = array(), $upload = false ) { //phpcs:ignore -- NOSONAR - complex method. |
| 90 |
|
| 91 |
if ( empty( $website ) || empty( $pAction ) || ! is_string( $pAction ) || ! is_array( $data ) ) { |
| 92 |
return; |
| 93 |
} |
| 94 |
|
| 95 |
if ( ! in_array( $pAction, array( 'install', 'updated' ), true ) ) { |
| 96 |
return; |
| 97 |
} |
| 98 |
|
| 99 |
$context = ''; |
| 100 |
|
| 101 |
if ( 'plugin' === $type ) { |
| 102 |
$context = 'plugin'; |
| 103 |
} elseif ( 'theme' === $type ) { |
| 104 |
$context = 'theme'; |
| 105 |
} elseif ( 'trans' === $type ) { |
| 106 |
$context = 'translation'; |
| 107 |
} elseif ( 'core' === $type ) { |
| 108 |
$context = 'core'; |
| 109 |
} else { |
| 110 |
return; |
| 111 |
} |
| 112 |
|
| 113 |
$logs_args = array(); |
| 114 |
|
| 115 |
$action = $pAction; |
| 116 |
$duration = 0; |
| 117 |
$created = 0; |
| 118 |
|
| 119 |
$args = array(); |
| 120 |
|
| 121 |
if ( 'install' === $action ) { |
| 122 |
|
| 123 |
$installed_items = isset( $data['install_items'] ) ? $data['install_items'] : array(); |
| 124 |
|
| 125 |
if ( ! is_array( $installed_items ) ) { |
| 126 |
$installed_items = array(); |
| 127 |
} |
| 128 |
|
| 129 |
if ( empty( $installed_items ) ) { |
| 130 |
return false; |
| 131 |
} |
| 132 |
|
| 133 |
foreach ( $installed_items as $item ) { |
| 134 |
$item = $this->sanitize_data( $item ); |
| 135 |
if ( ! empty( $item ) && isset( $item['name'] ) ) { |
| 136 |
$args = array( |
| 137 |
'name' => $item['name'], |
| 138 |
'version' => $item['version'], |
| 139 |
'upload' => $upload ? 1 : 0, |
| 140 |
'extra_info' => $item, |
| 141 |
); |
| 142 |
$logs_args[] = $args; |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
if ( empty( $logs_args ) ) { |
| 147 |
return false; |
| 148 |
} |
| 149 |
|
| 150 |
$message = '%1$s'; |
| 151 |
|
| 152 |
} elseif ( 'updated' === $action && ( in_array( $type, array( 'plugin', 'theme', 'trans' ) ) ) ) { |
| 153 |
if ( isset( $data['duration'] ) ) { |
| 154 |
$duration = $data['duration']; |
| 155 |
} |
| 156 |
|
| 157 |
if ( isset( $data['created'] ) ) { |
| 158 |
$created = $data['created']; |
| 159 |
} |
| 160 |
|
| 161 |
$updated_data = isset( $data['updated_data'] ) ? $data['updated_data'] : array(); |
| 162 |
|
| 163 |
if ( ! is_array( $updated_data ) ) { |
| 164 |
$updated_data = array(); |
| 165 |
} |
| 166 |
|
| 167 |
$logs_args = array(); |
| 168 |
|
| 169 |
foreach ( $updated_data as $item ) { |
| 170 |
$item = $this->sanitize_data( $item ); |
| 171 |
if ( ! empty( $item ) && isset( $item['name'] ) ) { |
| 172 |
$args = array( |
| 173 |
'name' => $item['name'], |
| 174 |
); |
| 175 |
if ( isset( $item['version'] ) ) { |
| 176 |
$args['version'] = $item['version']; |
| 177 |
} |
| 178 |
$save_info = $item; |
| 179 |
if ( ! empty( $item['rollback'] ) ) { |
| 180 |
$save_info['rollback_info'] = $item; |
| 181 |
} |
| 182 |
$args['extra_info'] = $save_info; |
| 183 |
$logs_args[] = $args; |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
if ( empty( $logs_args ) ) { |
| 188 |
return false; |
| 189 |
} |
| 190 |
|
| 191 |
if ( 'plugin' === $type || 'theme' === $type || 'trans' === $type ) { |
| 192 |
$message = '%1$s'; |
| 193 |
} else { |
| 194 |
return; |
| 195 |
} |
| 196 |
} elseif ( 'updated' === $action && 'core' === $type ) { |
| 197 |
$message = '%1$s'; |
| 198 |
$args = array( |
| 199 |
'name' => 'WordPress', // label. |
| 200 |
'version' => isset( $data['version'] ) ? $data['version'] : '', |
| 201 |
'old_version' => isset( $data['old_version'] ) ? $data['old_version'] : '', |
| 202 |
'error' => isset( $data['error'] ) ? $data['error'] : '', |
| 203 |
); |
| 204 |
$logs_args[] = $args; |
| 205 |
|
| 206 |
} else { |
| 207 |
return false; |
| 208 |
} |
| 209 |
|
| 210 |
$count_bulk = count( $logs_args ); |
| 211 |
foreach ( $logs_args as $args ) { |
| 212 |
$args['duration_bulk'] = $count_bulk; |
| 213 |
|
| 214 |
if ( ! empty( $duration ) ) { |
| 215 |
$args['duration'] = $duration; |
| 216 |
} |
| 217 |
|
| 218 |
if ( ! empty( $created ) ) { |
| 219 |
$args['created'] = $created; |
| 220 |
} |
| 221 |
|
| 222 |
$state = 1; |
| 223 |
if ( isset( $args['extra_info'] ) && is_array( $args['extra_info'] ) ) { |
| 224 |
if ( isset( $args['extra_info']['success'] ) ) { |
| 225 |
$state = ! empty( $args['extra_info']['success'] ) ? 1 : 0; |
| 226 |
} |
| 227 |
$args['extra_info'] = wp_json_encode( $args['extra_info'] ); |
| 228 |
} |
| 229 |
$this->log( |
| 230 |
$message, |
| 231 |
$args, |
| 232 |
$website->id, |
| 233 |
$context, |
| 234 |
$action, |
| 235 |
$state |
| 236 |
); |
| 237 |
} |
| 238 |
|
| 239 |
return true; |
| 240 |
} |
| 241 |
|
| 242 |
/** |
| 243 |
* Log plugin actions. |
| 244 |
* |
| 245 |
* @action mainwp_install_plugin_action. |
| 246 |
* |
| 247 |
* @param array $website website. |
| 248 |
* @param string $plugin_act plugin action: activate, deactivate, delete. |
| 249 |
* @param array $params params array. |
| 250 |
* @param array $action_data response data. |
| 251 |
*/ |
| 252 |
public function callback_mainwp_install_plugin_action( $website, $plugin_act, $params, $action_data ) { // phpcs:ignore -- NOSONAR - complex method. |
| 253 |
|
| 254 |
if ( empty( $website ) || ! is_object( $website ) || empty( $website->id ) || empty( $plugin_act ) || ! is_string( $plugin_act ) || ! is_array( $params ) ) { |
| 255 |
return; |
| 256 |
} |
| 257 |
|
| 258 |
if ( ! in_array( $plugin_act, array( 'activate', 'delete', 'deactivate' ), true ) ) { |
| 259 |
return; |
| 260 |
} |
| 261 |
|
| 262 |
if ( ! is_array( $action_data ) ) { |
| 263 |
return; |
| 264 |
} |
| 265 |
|
| 266 |
$logs_args = array(); |
| 267 |
|
| 268 |
foreach ( $action_data as $item ) { |
| 269 |
$item = $this->sanitize_data( $item ); |
| 270 |
if ( ! empty( $item ) && isset( $item['name'] ) ) { |
| 271 |
$log_arg = array( |
| 272 |
'name' => $item['name'], |
| 273 |
'version' => $item['version'], |
| 274 |
'slug' => $item['slug'], |
| 275 |
); |
| 276 |
if ( ! empty( $item['created'] ) ) { |
| 277 |
$log_arg['created'] = $item['created']; |
| 278 |
} |
| 279 |
$logs_args[] = $log_arg; |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
if ( empty( $logs_args ) ) { |
| 284 |
return; |
| 285 |
} |
| 286 |
|
| 287 |
$action = $plugin_act; |
| 288 |
$context = 'plugin'; |
| 289 |
|
| 290 |
$message = '%1$s'; |
| 291 |
|
| 292 |
$state = 1; |
| 293 |
|
| 294 |
foreach ( $logs_args as $args ) { |
| 295 |
$args['duration_bulk'] = is_array( $params ) && ! empty( $params['duration_bulk'] ) ? $params['duration_bulk'] : count( $logs_args ); |
| 296 |
$this->log( |
| 297 |
$message, |
| 298 |
$args, |
| 299 |
$website->id, |
| 300 |
$context, |
| 301 |
$action, |
| 302 |
$state |
| 303 |
); |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
/** |
| 308 |
* Log themes actions. |
| 309 |
* |
| 310 |
* @action mainwp_install_theme_action. |
| 311 |
* |
| 312 |
* @param array $website website. |
| 313 |
* @param string $theme_act theme action: activate, delete. |
| 314 |
* @param array $params params array. |
| 315 |
* @param array $action_data response data. |
| 316 |
*/ |
| 317 |
public function callback_mainwp_install_theme_action( $website, $theme_act, $params, $action_data ) { |
| 318 |
|
| 319 |
if ( empty( $website ) || ! is_object( $website ) || empty( $website->id ) || empty( $theme_act ) || ! is_string( $theme_act ) || ! is_array( $params ) ) { |
| 320 |
return; |
| 321 |
} |
| 322 |
|
| 323 |
if ( ! in_array( $theme_act, array( 'activate', 'delete', 'deactivate' ), true ) ) { |
| 324 |
return; |
| 325 |
} |
| 326 |
|
| 327 |
if ( ! is_array( $action_data ) ) { |
| 328 |
return; |
| 329 |
} |
| 330 |
|
| 331 |
$logs_args = array(); |
| 332 |
|
| 333 |
foreach ( $action_data as $item ) { |
| 334 |
$item = $this->sanitize_data( $item ); |
| 335 |
if ( ! empty( $item ) && isset( $item['name'] ) ) { |
| 336 |
$logs_args[] = array( |
| 337 |
'name' => $item['name'], |
| 338 |
'version' => $item['version'], |
| 339 |
'slug' => $item['slug'], |
| 340 |
); |
| 341 |
} |
| 342 |
} |
| 343 |
|
| 344 |
if ( empty( $logs_args ) ) { |
| 345 |
return; |
| 346 |
} |
| 347 |
|
| 348 |
$action = $theme_act; |
| 349 |
$context = 'theme'; |
| 350 |
|
| 351 |
$message = '%1$s'; |
| 352 |
|
| 353 |
$state = 1; |
| 354 |
|
| 355 |
$count_bulk = count( $logs_args ); |
| 356 |
foreach ( $logs_args as $args ) { |
| 357 |
$args['duration_bulk'] = $count_bulk; |
| 358 |
$this->log( |
| 359 |
$message, |
| 360 |
$args, |
| 361 |
$website->id, |
| 362 |
$context, |
| 363 |
$action, |
| 364 |
$state |
| 365 |
); |
| 366 |
} |
| 367 |
} |
| 368 |
} |
| 369 |
|