| 1 |
<?php |
| 2 |
/** |
| 3 |
* Module Logs User 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_User |
| 15 |
*/ |
| 16 |
class Connector_User extends Log_Connector { |
| 17 |
/** |
| 18 |
* Connector slug |
| 19 |
* |
| 20 |
* @var string |
| 21 |
*/ |
| 22 |
public $name = 'users'; |
| 23 |
|
| 24 |
|
| 25 |
/** |
| 26 |
* Actions registered for this connector |
| 27 |
* |
| 28 |
* @var array |
| 29 |
*/ |
| 30 |
public $actions = array( |
| 31 |
'mainwp_user_action', |
| 32 |
); |
| 33 |
|
| 34 |
/** |
| 35 |
* Return translated connector label |
| 36 |
* |
| 37 |
* @return string Translated connector label |
| 38 |
*/ |
| 39 |
public function get_label() { |
| 40 |
return esc_html__( 'Users', 'mainwp' ); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Return translated action term labels |
| 45 |
* |
| 46 |
* @return array Action terms label translation |
| 47 |
*/ |
| 48 |
public function get_action_labels() { |
| 49 |
return array( |
| 50 |
'created' => esc_html__( 'Created', 'mainwp' ), |
| 51 |
'update' => esc_html__( 'Updated', 'mainwp' ), |
| 52 |
'delete' => esc_html__( 'Deleted', 'mainwp' ), |
| 53 |
'update_password' => esc_html__( 'Updated password', 'mainwp' ), |
| 54 |
'change_role' => esc_html__( 'Change role', 'mainwp' ), |
| 55 |
); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Return translated context labels |
| 60 |
* |
| 61 |
* @return array Context label translations |
| 62 |
*/ |
| 63 |
public function get_context_labels() { |
| 64 |
return array( |
| 65 |
'users' => esc_html__( 'Users', 'mainwp' ), |
| 66 |
); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Log user actions. |
| 71 |
* |
| 72 |
* @action mainwp_user_action |
| 73 |
* |
| 74 |
* @param object $website website data. |
| 75 |
* @param string $pAction post action. |
| 76 |
* @param array $data data array. |
| 77 |
*/ |
| 78 |
public function callback_mainwp_user_action( $website, $pAction, $data ) { |
| 79 |
|
| 80 |
if ( empty( $website ) || ! is_object( $website ) || empty( $website->id ) || ! is_array( $data ) ) { |
| 81 |
return; |
| 82 |
} |
| 83 |
|
| 84 |
$action = ''; |
| 85 |
|
| 86 |
$userId = 0; |
| 87 |
|
| 88 |
if ( isset( $data['user_id'] ) ) { |
| 89 |
$userId = intval( $data['user_id'] ); |
| 90 |
} |
| 91 |
|
| 92 |
if ( empty( $userId ) ) { |
| 93 |
return; |
| 94 |
} |
| 95 |
|
| 96 |
$display_name = isset( $data['display_name'] ) ? $data['display_name'] : ''; |
| 97 |
|
| 98 |
$args = array( |
| 99 |
'display_name' => $display_name, |
| 100 |
'roles' => isset( $data['roles'] ) ? $data['roles'] : '', |
| 101 |
'user_id' => $userId, |
| 102 |
); |
| 103 |
|
| 104 |
// translators: %1$s: User display name. |
| 105 |
$message = esc_html_x( |
| 106 |
'User: %1$s', |
| 107 |
'1: User display name', |
| 108 |
'mainwp' |
| 109 |
); |
| 110 |
|
| 111 |
if ( 'created' === $pAction ) { |
| 112 |
$action = 'created'; |
| 113 |
} elseif ( 'delete' === $pAction ) { |
| 114 |
$action = 'delete'; |
| 115 |
} elseif ( 'changeRole' === $pAction ) { |
| 116 |
$action = 'change_role'; |
| 117 |
$args['old_roles'] = isset( $data['old_roles'] ) ? $data['old_roles'] : ''; |
| 118 |
} elseif ( 'newadminpassword' === $pAction ) { |
| 119 |
$action = 'update_password'; |
| 120 |
} elseif ( 'update_password' === $pAction ) { |
| 121 |
$action = 'update_password'; |
| 122 |
} elseif ( 'update_user' === $pAction ) { |
| 123 |
$action = 'update'; |
| 124 |
} else { |
| 125 |
return; |
| 126 |
} |
| 127 |
|
| 128 |
$state = 1; |
| 129 |
|
| 130 |
$this->log( |
| 131 |
$message, |
| 132 |
$args, |
| 133 |
$website->id, |
| 134 |
'users', |
| 135 |
$action, |
| 136 |
$state |
| 137 |
); |
| 138 |
} |
| 139 |
} |
| 140 |
|