Changeset 12795
- Timestamp:
- 08/02/2023 09:43:38 AM (17 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/wordpress.org/public_html/wp-content/plugins/support-forums/inc/class-moderators.php
r12747 r12795 71 71 add_action( 'bbp_unapproved_topic', array( $this, 'store_moderator_username' ) ); 72 72 add_action( 'bbp_unapproved_reply', array( $this, 'store_moderator_username' ) ); 73 74 // Allow moderators to post as @moderator. 75 add_action( 'bbp_theme_before_reply_form_subscription', array( $this, 'form_add_post_as_anon_mod' ) ); 76 add_filter( 'bbp_new_reply_pre_insert', array( $this, 'bbp_new_reply_pre_insert' ) ); 77 add_action( 'bbp_theme_before_reply_author_details', array( $this, 'show_anon_mod_name' ) ); 78 add_filter( 'user_has_cap', array( $this, 'anon_moderator_user_has_cap' ), 10, 4 ); 73 79 } 74 80 … … 1110 1116 return $topic_status; 1111 1117 } 1118 1119 /** 1120 * Add a checkbox to the reply form to allow moderators to post anonymously. 1121 */ 1122 public function form_add_post_as_anon_mod() { 1123 if ( ! current_user_can( 'moderate' ) ) { 1124 return; 1125 } 1126 1127 $moderator_user = get_user_by( 'slug', 'moderator' ); 1128 if ( bbp_is_reply_edit() && $moderator_user->ID !== bbp_get_reply_author_id() ) { 1129 return; 1130 } 1131 1132 ?> 1133 1134 <p> 1135 <label> 1136 <input type="checkbox" name="post_as_anon_moderator" <?php disabled( true, bbp_is_reply_edit() ); checked( $moderator_user->ID, bbp_get_reply_author_id() ) ?>> 1137 <?php esc_html_e( 'Post this reply anonymously as @moderator.', 'wporg-forums' ); ?> 1138 </label> 1139 </p> 1140 1141 <?php 1142 } 1143 1144 /** 1145 * Overwrite the reply author if required. 1146 * 1147 * @param array $post_data The reply data. 1148 * @return array The filtered reply data. 1149 */ 1150 public function bbp_new_reply_pre_insert( $post_data ) { 1151 if ( ! current_user_can( 'moderate' ) || empty( $_POST['post_as_anon_moderator'] ) ) { 1152 return $post_data; 1153 } 1154 1155 // Overwrite the author. 1156 $post_data['post_author'] = get_user_by( 'slug', 'moderator' )->ID; 1157 1158 // Record the real user in the post meta. 1159 $post_data['meta_input'] ??= []; 1160 $post_data['meta_input'][ self::MODERATOR_META ] = get_current_user_id(); 1161 1162 return $post_data; 1163 } 1164 1165 /** 1166 * Display the moderator's name (to other moderators) if the reply was posted anonymously. 1167 */ 1168 function show_anon_mod_name() { 1169 if ( ! current_user_can( 'moderate' ) ) { 1170 return; 1171 } 1172 1173 $moderator_user = get_user_by( 'slug', 'moderator' ); 1174 if ( $moderator_user->ID !== bbp_get_reply_author_id() ) { 1175 return; 1176 } 1177 1178 $user = get_user_by( 'id', get_post_meta( bbp_get_reply_id(), self::MODERATOR_META, true ) ); 1179 1180 printf( 1181 '<em>' . __( 'Posted by <a href="%s">@%s</a>.', 'wporg-forums' ) . '</em><br/>', 1182 esc_url( bbp_get_user_profile_url( $user->ID ) ), 1183 esc_html( $user->user_nicename ) 1184 ); 1185 } 1186 1187 /** 1188 * Pretend the @moderator user can moderate, except when they're logged in. 1189 */ 1190 public function anon_moderator_user_has_cap( $allcaps, $caps, $args, $user ) { 1191 if ( 1192 $user && 1193 [ 'moderate' === $caps ] && 1194 'moderator' === $user->user_nicename && 1195 $user->ID !== get_current_user_id() 1196 ) { 1197 $allcaps['moderate'] = true; 1198 } 1199 1200 return $allcaps; 1201 } 1112 1202 }
Note: See TracChangeset
for help on using the changeset viewer.