wpal_db = $wpal_db; } /** * Logs a user activity into the activity log table. * * Inserts a record of a user’s activity (e.g., login, logout, post actions) into the external * database using the WPAL_DB class. Includes error handling to catch database insertion * failures and returns a WP_Error on failure when WP_DEBUG is enabled. * * @since 1.0.0 * @param int $user_id User ID associated with the activity. * @param string $type Type of activity (e.g., 'user', 'post'). * @param string $detail Detailed description of the activity. * @param string $key Unique session or activity key for tracking. * @return bool|WP_Error True on success, WP_Error on failure. */ public function wpal_activity_log( $user_id, $type, $detail, $key ) { $logged = $this->wpal_db->wpal_insertActivity( $user_id, $type, $detail, $key ); if ( ! $logged ) { if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log error_log( 'WPAL_LOG: Failed to log activity for user ID ' . $user_id ); } return new WP_Error( 'db_activity_exception', 'Database error occurred.'); } return true; } /** * Logs user session data into the sessions table. * * Records session details, including the session token, user IP address, user agent, and * associated user ID, in the external database. Includes error handling to catch insertion * failures and returns a WP_Error on failure when WP_DEBUG is enabled. * * @since 1.0.0 * @param string $session_token Unique session token for tracking. * @param string $user_ip User’s IP address (e.g., from HTTP_X_FORWARDED_FOR or REMOTE_ADDR). * @param string $user_agent User’s browser user-agent string. * @param int $user_id User ID associated with the session. * @return bool|WP_Error True on success, WP_Error on failure. */ public function wpal_sessions_log( $session_token, $user_ip, $user_agent, $user_id ) { $logged = $this->wpal_db->wpal_insertSession( $session_token, $user_ip, $user_agent, $user_id ); if ( ! $logged ) { if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log error_log( 'WPAL_LOG: Failed to log session for user ID ' . $user_id ); } return new WP_Error( 'db_sessions_exception', 'Database error occurred.'); } return true; } /** * Logs user registration or profile data into the users table. * * Inserts user profile details (ID, username, email, role) into the external database during * registration or profile updates. Includes error handling to catch insertion failures and * returns a WP_Error on failure when WP_DEBUG is enabled. * * @since 1.0.0 * @param int $user_id User ID of the registered or updated user. * @param string $user_name Username of the user. * @param string $user_email Email address of the user. * @param string $user_role User’s role (e.g., 'administrator', 'subscriber'). * @return bool|WP_Error True on success, WP_Error on failure. */ public function wpal_user_log( $user_id, $user_name, $user_email, $user_role ) { $logged = $this->wpal_db->wpal_insertUser( $user_id, $user_name, $user_email, $user_role ); if ( ! $logged ) { if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log error_log( 'WPAL_LOG: Failed to log user data for user ID ' . $user_id ); } return new WP_Error( 'db_user_exception', 'Database error occurred.'); } return true; } /** * Updates the status of a user in the users table. * * Modifies the user’s status (e.g., active or deleted) in the external database. Includes * error handling to catch update failures and returns a WP_Error on failure when * WP_DEBUG is enabled. Typically used to mark users as deleted. * * @since 1.0.0 * @param int $user_id User ID to update. * @param string $user_status New status for the user (e.g., '0' for deleted, '1' for active). * @return bool|WP_Error True on success, WP_Error on failure. */ public function wpal_user_update( $user_id, $user_status ) { $updated = $this->wpal_db->wpal_updateUser( $user_id, $user_status ); if ( ! $updated ) { if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log error_log( 'WPAL_LOG: Failed to update user status for user ID ' . $user_id ); } return new WP_Error( 'db_user_exception', 'Database error occurred.'); } return true; } } ?>