value( 'enabled', true ) ) { return; } if ( $s->value( 'log_auth', true ) ) { add_action( 'wp_login', [ $this, 'on_login' ], 10, 2 ); add_action( 'wp_logout', [ $this, 'on_logout' ], 10, 1 ); add_action( 'wp_login_failed', [ $this, 'on_login_failed' ], 10, 1 ); } if ( $s->value( 'log_content', true ) ) { add_action( 'transition_post_status', [ $this, 'on_transition_post_status' ], 10, 3 ); add_action( 'before_delete_post', [ $this, 'on_delete_post' ], 10, 1 ); } if ( $s->value( 'log_plugins', true ) ) { add_action( 'activated_plugin', [ $this, 'on_plugin_activated' ], 10, 1 ); add_action( 'deactivated_plugin', [ $this, 'on_plugin_deactivated' ], 10, 1 ); add_action( 'switch_theme', [ $this, 'on_switch_theme' ], 10, 1 ); } if ( $s->value( 'log_users', true ) ) { add_action( 'user_register', [ $this, 'on_user_register' ], 10, 1 ); add_action( 'profile_update', [ $this, 'on_profile_update' ], 10, 1 ); add_action( 'delete_user', [ $this, 'on_delete_user' ], 10, 1 ); } } /** * Enregistre un évènement (avec contexte utilisateur courant). * * @param string $action Code d'action. * @param string $object_type Type d'objet concerné. * @param int $object_id Identifiant de l'objet. * @param string $description Description lisible. * @param int $user_id Utilisateur (0 = utilisateur courant). * @param string $user_login Identifiant (vide = utilisateur courant). * @return void */ private function record( string $action, string $object_type, int $object_id, string $description, int $user_id = 0, string $user_login = '' ): void { if ( 0 === $user_id && '' === $user_login ) { $current = wp_get_current_user(); if ( $current && $current->exists() ) { $user_id = (int) $current->ID; $user_login = (string) $current->user_login; } } Aiforya_Al_Repository::get_instance()->insert( [ 'event_time' => current_time( 'mysql' ), 'user_id' => $user_id, 'user_login' => $user_login, 'action' => $action, 'object_type' => $object_type, 'object_id' => $object_id, 'description' => $description, 'ip' => $this->client_ip(), ] ); } /** * @return string */ private function client_ip(): string { $ip = isset( $_SERVER['REMOTE_ADDR'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ) : ''; return ( '' !== $ip && filter_var( $ip, FILTER_VALIDATE_IP ) ) ? $ip : ''; } // --------------------------------------------------------------- Auth. /** * @param string $user_login Identifiant. * @param mixed $user WP_User. * @return void */ public function on_login( $user_login, $user = null ): void { $uid = is_object( $user ) && isset( $user->ID ) ? (int) $user->ID : 0; $this->record( 'user_login', 'user', $uid, __( 'Successful login', 'aiforya-activity-log' ), $uid, (string) $user_login ); } /** * @param int $user_id Utilisateur. * @return void */ public function on_logout( $user_id = 0 ): void { $uid = (int) $user_id; $u = $uid > 0 ? get_userdata( $uid ) : false; $this->record( 'user_logout', 'user', $uid, __( 'Logout', 'aiforya-activity-log' ), $uid, $u ? (string) $u->user_login : '' ); } /** * @param string $username Identifiant tenté. * @return void */ public function on_login_failed( $username ): void { $this->record( 'login_failed', 'user', 0, __( 'Failed login', 'aiforya-activity-log' ), 0, (string) $username ); } // ------------------------------------------------------------ Content. /** * @param string $new_status Nouveau statut. * @param string $old_status Ancien statut. * @param mixed $post WP_Post. * @return void */ public function on_transition_post_status( $new_status, $old_status, $post ): void { if ( ! is_object( $post ) || ! isset( $post->post_type ) ) { return; } if ( in_array( $post->post_type, [ 'revision', 'nav_menu_item' ], true ) ) { return; } if ( 'auto-draft' === $new_status || ( 'auto-draft' === $old_status && 'draft' === $new_status ) ) { return; } if ( wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) { return; } $title = get_the_title( $post ) ? get_the_title( $post ) : ( '#' . (int) $post->ID ); if ( 'publish' === $new_status && 'publish' !== $old_status ) { $this->record( 'post_published', (string) $post->post_type, (int) $post->ID, sprintf( /* translators: %s: content title. */ /* translators: %s : valeur insérée dynamiquement. */ __( 'Published: %s', 'aiforya-activity-log' ), $title ) ); } elseif ( 'trash' === $new_status ) { $this->record( 'post_trashed', (string) $post->post_type, (int) $post->ID, sprintf( /* translators: %s: content title. */ /* translators: %s : valeur insérée dynamiquement. */ __( 'Moved to trash: %s', 'aiforya-activity-log' ), $title ) ); } elseif ( 'publish' === $new_status && 'publish' === $old_status ) { $this->record( 'post_updated', (string) $post->post_type, (int) $post->ID, sprintf( /* translators: %s: content title. */ /* translators: %s : valeur insérée dynamiquement. */ __( 'Edited: %s', 'aiforya-activity-log' ), $title ) ); } } /** * @param int $post_id Identifiant. * @return void */ public function on_delete_post( $post_id ): void { $post = get_post( (int) $post_id ); if ( ! $post || in_array( $post->post_type, [ 'revision', 'nav_menu_item' ], true ) ) { return; } $title = get_the_title( $post ) ? get_the_title( $post ) : ( '#' . (int) $post_id ); $this->record( 'post_deleted', (string) $post->post_type, (int) $post_id, sprintf( /* translators: %s: content title. */ /* translators: %s : valeur insérée dynamiquement. */ __( 'Permanently deleted: %s', 'aiforya-activity-log' ), $title ) ); } // ------------------------------------------------------- Plugins/theme. /** * @param string $plugin Chemin de l'extension. * @return void */ public function on_plugin_activated( $plugin ): void { $this->record( 'plugin_activated', 'plugin', 0, sprintf( /* translators: %s: plugin file. */ /* translators: %s : valeur insérée dynamiquement. */ __( 'Plugin activated: %s', 'aiforya-activity-log' ), (string) $plugin ) ); } /** * @param string $plugin Chemin de l'extension. * @return void */ public function on_plugin_deactivated( $plugin ): void { $this->record( 'plugin_deactivated', 'plugin', 0, sprintf( /* translators: %s: plugin file. */ /* translators: %s : valeur insérée dynamiquement. */ __( 'Plugin deactivated: %s', 'aiforya-activity-log' ), (string) $plugin ) ); } /** * @param string $new_name Nom du thème. * @return void */ public function on_switch_theme( $new_name ): void { $this->record( 'theme_switched', 'theme', 0, sprintf( /* translators: %s: theme name. */ /* translators: %s : valeur insérée dynamiquement. */ __( 'Theme activated: %s', 'aiforya-activity-log' ), (string) $new_name ) ); } // --------------------------------------------------------------- Users. /** * @param int $user_id Utilisateur. * @return void */ public function on_user_register( $user_id ): void { $u = get_userdata( (int) $user_id ); $this->record( 'user_registered', 'user', (int) $user_id, sprintf( /* translators: %s: user login. */ /* translators: %s : valeur insérée dynamiquement. */ __( 'Account created: %s', 'aiforya-activity-log' ), $u ? (string) $u->user_login : ( '#' . (int) $user_id ) ) ); } /** * @param int $user_id Utilisateur. * @return void */ public function on_profile_update( $user_id ): void { $u = get_userdata( (int) $user_id ); $this->record( 'user_updated', 'user', (int) $user_id, sprintf( /* translators: %s: user login. */ /* translators: %s : valeur insérée dynamiquement. */ __( 'Account updated: %s', 'aiforya-activity-log' ), $u ? (string) $u->user_login : ( '#' . (int) $user_id ) ) ); } /** * @param int $user_id Utilisateur. * @return void */ public function on_delete_user( $user_id ): void { $u = get_userdata( (int) $user_id ); $this->record( 'user_deleted', 'user', (int) $user_id, sprintf( /* translators: %s: user login. */ /* translators: %s : valeur insérée dynamiquement. */ __( 'Account deleted: %s', 'aiforya-activity-log' ), $u ? (string) $u->user_login : ( '#' . (int) $user_id ) ) ); } } endif;