Changeset 12749 for sites/trunk/wordpress.org/public_html/wp-content/plugins/support-forums/inc/class-audit-log.php
- Timestamp:
- 07/20/2023 04:01:22 AM (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/wordpress.org/public_html/wp-content/plugins/support-forums/inc/class-audit-log.php
r12746 r12749 2 2 3 3 namespace WordPressdotorg\Forums; 4 use WP_User; 4 5 5 6 class Audit_Log { … … 11 12 */ 12 13 var $edited_notes = []; 14 15 /** 16 * Keeps track of the role changes for each user. 17 * 18 * @var array 19 */ 20 var $last_role_change = []; 13 21 14 22 function __construct() { 15 23 // Audit-log entries for Term subscriptions (add/remove) 16 add_action( 'wporg_bbp_add_user_term_subscription', array( $this, 'term_subscriptions' ), 10, 2 ); 17 add_action( 'wporg_bbp_remove_user_term_subscription', array( $this, 'term_subscriptions' ), 10, 2 ); 24 add_action( 'wporg_bbp_add_user_term_subscription', [ $this, 'term_subscriptions' ], 10, 2 ); 25 add_action( 'wporg_bbp_remove_user_term_subscription', [ $this, 'term_subscriptions' ], 10, 2 ); 26 27 // Add a user note for forum role changes. 28 add_action( 'add_user_role', [ $this, 'monitor_role_changes' ], 10, 2 ); 29 add_action( 'remove_user_role', [ $this, 'monitor_role_changes' ], 10, 2 ); 30 add_filter( 'bbp_set_user_role', [ $this, 'bbp_set_user_role' ], 10, 3 ); 31 32 // Add a user note when flagging/unflagging a user. 33 add_action( 'wporg_bbp_flag_user', [ $this, 'log_user_flag_changes' ], 10, 1 ); 34 add_action( 'wporg_bbp_unflag_user', [ $this, 'log_user_flag_changes' ], 10, 1 ); 18 35 19 36 // Slack logs for Moderation notes. 20 37 add_action( 'wporg_bbp_note_added', [ $this, 'forums_notes_added' ], 10, 2 ); 38 add_action( 'shutdown', [ $this, 'forums_notes_added_shutdown' ] ); 21 39 } 22 40 … … 51 69 } 52 70 71 72 /** 73 * Keep track of the role changes that happen via bbp_set_user_role. 74 * 75 * TODO: Check if this is still needed, bbPress might pass context via bbp_set_user_role. 76 */ 77 function monitor_role_changes( $user_id, $role ) { 78 $this->last_role_change[ $user_id ] ??= []; 79 $this->last_role_change[ $user_id ][ current_filter() ] = $role; 80 } 81 82 /** 83 * Monitor for role changes, and log as appropriate. 84 * 85 * NOTE: This is also triggered on Locale forums. We may need to revisit that at some point. 86 */ 87 function bbp_set_user_role( $new_role, $user_id, WP_User $user ) { 88 $previous_role = $this->last_role_change[ $user_id ][ 'remove_user_role' ] ?? false; 89 90 // For the purposes of this function, a previous participant role is irrelevant. 91 if ( bbp_get_participant_role() == $previous_role ) { 92 $previous_role = false; 93 } 94 95 $monitored_roles = [ 96 bbp_get_keymaster_role(), 97 bbp_get_moderator_role(), 98 // bbp_get_participant_role(), // Not monitoring for changes involving only this role. 99 bbp_get_spectator_role(), 100 bbp_get_blocked_role() 101 ]; 102 103 if ( 104 // If the role change is not one we're monitoring, bail. 105 ! array_intersect( $monitored_roles, [ $new_role, $previous_role ] ) || 106 // If we can't detect any change, bail. 107 $new_role === $previous_role 108 ) { 109 return $new_role; // We're on a filter. 110 } 111 112 // Determine what triggered this change. 113 $where_from = ! ms_is_switched() ? home_url( '/' ) : $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; 114 $where_from = explode( '?', $where_from )[0]; 115 $where_from = preg_replace( '!^(?:https?://)?(.+?)/?[^/]*$!i', '$1', $where_from ); 116 117 // Add a user note about this action. 118 $note_text = sprintf( 119 'Forum role changed to %s%s%s.', 120 get_role( $new_role )->name, 121 $previous_role ? sprintf( ' from %s', get_role( $previous_role )->name ) : '', 122 $where_from ? sprintf( ' via %s', $where_from ) : '' 123 ); 124 125 // Used in wporg-login to add context. 126 $note_text = apply_filters( 'wporg_bbp_forum_role_changed_note_text', $note_text, $user ); 127 128 // Add a user note about this action. 129 Plugin::get_instance()->user_notes->add_user_note_or_update_previous( 130 $user->ID, 131 $note_text 132 ); 133 134 // It's a filter, so we need to return the value. 135 return $new_role; 136 } 137 138 /** 139 * Add a user note when a user is flagged / unflagged. 140 * 141 * @param int $user_id 142 */ 143 function log_user_flag_changes( $user_id ) { 144 $flag_action = ( 'wporg_bbp_flag_user' === current_action() ) ? 'flagged' : 'unflagged'; 145 146 $note_text = "User {$flag_action}."; 147 148 Plugin::get_instance()->user_notes->add_user_note_or_update_previous( 149 $user_id, 150 $note_text 151 ); 152 } 153 53 154 /** 54 155 * Record Note changes to Slack. … … 60 161 $this->edited_notes[ $user_id ] ??= []; 61 162 $this->edited_notes[ $user_id ][] = $note_id; 62 63 if ( ! has_action( 'shutdown', [ $this, 'forums_notes_added_shutdown' ] ) ) {64 add_action( 'shutdown', [ $this, 'forums_notes_added_shutdown' ] );65 }66 163 } 67 164 … … 70 167 */ 71 168 function forums_notes_added_shutdown() { 72 if ( ! function_exists( 'notify_slack' ) || ! defined( 'FORUMS_MODACTIONS_SLACK_CHANNEL' ) ) {169 if ( ! $this->edited_notes || ! function_exists( 'notify_slack' ) || ! defined( 'FORUMS_MODACTIONS_SLACK_CHANNEL' ) ) { 73 170 return; 74 171 }
Note:
See TracChangeset
for help on using the changeset viewer.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)