roles ); } function alba_board_card_members( $card_id ) { $ids = get_post_meta( $card_id, 'alba_card_members', true ); // Legacy cards start with their eligible assignee; saving makes membership explicit. if ( ! metadata_exists( 'post', $card_id, 'alba_card_members' ) ) { $ids = [ absint( get_post_field( 'post_author', $card_id ) ) ]; } $ids = is_array( $ids ) ? array_unique( array_filter( array_map( 'absint', $ids ) ) ) : []; return array_values( array_filter( $ids, 'alba_board_has_collaboration_role' ) ); } function alba_board_eligible_card_person( $card_id, $id ) { return alba_board_has_collaboration_role( $id ) && user_can( $id, 'edit_cards' ) && alba_board_can_view_shared_card( $card_id, $id ); } function alba_board_validate_members( $card_id, $ids ) { if ( ! is_array( $ids ) || count( $ids ) > 50 ) { return new WP_Error( 'invalid_members', __( 'Select up to 50 card members.', 'alba-board' ), [ 'status' => 400 ] ); } $members = []; foreach ( $ids as $id ) { if ( ! is_scalar( $id ) || ! ctype_digit( (string) $id ) || ! alba_board_eligible_card_person( $card_id, absint( $id ) ) ) { return new WP_Error( 'invalid_member', __( 'Each member must be a Board user who can access this card.', 'alba-board' ), [ 'status' => 400 ] ); } $members[] = absint( $id ); } return array_values( array_unique( $members ) ); } function alba_board_update_card_members( $card_id, $members ) { $old = alba_board_card_members( $card_id ); sort( $members ); sort( $old ); if ( $members === $old && metadata_exists( 'post', $card_id, 'alba_card_members' ) ) return; update_post_meta( $card_id, 'alba_card_members', $members ); do_action( 'alba_board_card_members_updated', $card_id, $members, $old ); $options = get_option( 'alba_board_notifications', [] ); if ( empty( $options['notify_on_card'] ) ) return; $post = get_post( $card_id ); foreach ( array_diff( $members, $old ) as $id ) { if ( $id === get_current_user_id() || ! alba_board_eligible_card_person( $card_id, $id ) ) continue; $user = get_userdata( $id ); /* translators: Card title. */ $subject = sprintf( __( 'You were added to card: %s', 'alba-board' ), $post->post_title ); alba_board_send_notification( $user->user_email, $subject, $user->display_name, $post->post_title, $post->post_content, alba_get_smart_card_url( $card_id ), 'new_card' ); } } /** Only newly appended metadata comments produce mention notifications. */ function alba_board_notify_comment_mentions( $meta_id, $card_id, $key, $comments ) { if ( 'alba_comments' !== $key || 'alba_card' !== get_post_type( $card_id ) || ! is_array( $comments ) ) return; $old = $GLOBALS['alba_board_mentions_before'][ $card_id ] ?? []; unset( $GLOBALS['alba_board_mentions_before'][ $card_id ] ); if ( ! alba_board_can_comment_card( $card_id ) && ! current_user_can( 'edit_card', $card_id ) ) return; // Updates/deletions of existing comments do not resend historical mentions. if ( count( $comments ) <= count( $old ) || array_slice( $comments, 0, count( $old ) ) !== $old ) return; $options = get_option( 'alba_board_notifications', [] ); $notify_mentions = ! isset( $options['notify_on_mention'] ) || ! empty( $options['notify_on_mention'] ); $notify_comments = ! isset( $options['notify_on_comment'] ) || ! empty( $options['notify_on_comment'] ); $post = get_post( $card_id ); foreach ( array_slice( $comments, count( $old ) ) as $comment ) { $text = isset( $comment['text'] ) && is_string( $comment['text'] ) ? $comment['text'] : ''; $recipients = $notify_comments ? array_fill_keys( alba_board_card_members( $card_id ), false ) : []; if ( $notify_mentions ) { preg_match_all( '/(?= 20 || ! alba_board_eligible_card_person( $card_id, $user->ID ) ) continue; $mentioned[ $user->ID ] = true; $recipients[ $user->ID ] = true; } } foreach ( $recipients as $id => $is_mention ) { if ( (int) $id === get_current_user_id() || ! alba_board_eligible_card_person( $card_id, $id ) ) continue; $user = get_userdata( $id ); /* translators: Card title. */ $subject = sprintf( $is_mention ? __( 'You were mentioned on card: %s', 'alba-board' ) : __( 'New comment on card: %s', 'alba-board' ), $post->post_title ); alba_board_send_notification( $user->user_email, $subject, $user->display_name, $post->post_title, $post->post_content, alba_get_smart_card_url( $card_id ), 'new_comment', $text ); } } } // Capture the old value, then notify only after successful persistence. add_action( 'update_post_meta', function( $meta_id, $card_id, $key, $value ) { if ( 'alba_comments' !== $key ) return; $old = get_post_meta( $card_id, $key, true ); $GLOBALS['alba_board_mentions_before'][ $card_id ] = is_array( $old ) ? $old : []; }, 10, 4 ); add_action( 'updated_post_meta', 'alba_board_notify_comment_mentions', 10, 4 ); add_action( 'added_post_meta', 'alba_board_notify_comment_mentions', 10, 4 ); /** Return plain text/name segments; never accept HTML from comment content. */ function alba_board_card_comment_segments( $text, $card_id ) { $text = (string) $text; preg_match_all( '/(? $offset ) $parts[] = [ 'text' => substr( $text, $offset, $at - $offset ), 'mention' => false ]; $login = ! empty( $match[1][0] ) ? $match[1][0] : ( $match[2][0] ?? '' ); $user = get_user_by( 'login', $login ); $allowed = $user && alba_board_eligible_card_person( $card_id, $user->ID ); $parts[] = [ 'text' => $allowed ? $user->display_name : $token, 'mention' => (bool) $allowed ]; $offset = $at + strlen( $token ); } if ( $offset < strlen( $text ) ) $parts[] = [ 'text' => substr( $text, $offset ), 'mention' => false ]; return $parts; } function alba_board_format_card_comment( $text, $card_id ) { $html = ''; foreach ( alba_board_card_comment_segments( $text, $card_id ) as $part ) { $escaped = esc_html( $part['text'] ); $html .= $part['mention'] ? '' . $escaped . '' : $escaped; } return $html; } function alba_board_render_card_members( $card_id ) { $can_manage = current_user_can( 'edit_card', $card_id ); echo '