[ handle, name ]. Handle is the user's nicename. */ public static function people() { $out = array(); $handles = Nodebook_Team::handles(); foreach ( Nodebook_Team::users() as $id => $name ) { $handle = isset( $handles[ $id ] ) ? (string) $handles[ $id ] : ''; if ( '' === $handle ) { $u = get_userdata( (int) $id ); $handle = $u ? (string) $u->user_nicename : ''; } if ( '' !== $handle ) { $out[ (int) $id ] = array( $handle, (string) $name ); } } return $out; } /** * Who a text mentions: user IDs. "@sam" matches the handle; a first name works too when only one teammate has it. * * @param string $text Note. */ public static function find( $text ) { if ( false === strpos( (string) $text, '@' ) || ! preg_match_all( '/(?:^|[\s(,;])@([\p{L}\p{N}][\p{L}\p{N}._\-]{0,59})/u', (string) $text, $m ) ) { return array(); } $people = self::people(); $first = array(); foreach ( $people as $id => $p ) { $fn = strtolower( remove_accents( (string) strtok( $p[1], ' ' ) ) ); $first[ $fn ][] = $id; } $ids = array(); foreach ( $m[1] as $word ) { $w = strtolower( remove_accents( rtrim( $word, '.-_' ) ) ); foreach ( $people as $id => $p ) { if ( strtolower( $p[0] ) === $w ) { $ids[] = $id; continue 2; } } if ( isset( $first[ $w ] ) && 1 === count( $first[ $w ] ) ) { $ids[] = $first[ $w ][0]; } } return array_values( array_unique( $ids ) ); } /** * A note or logged contact moment: tell the people it mentions. * * @param int $id Activity. * @param array $row Activity row. */ public static function on_activity( $id, $row ) { if ( ! in_array( $row['type'], array( 'note', 'touch' ), true ) || ! (int) $row['contact_id'] || self::quiet() ) { return; } $data = json_decode( (string) $row['data'], true ); $text = is_array( $data ) ? (string) ( $data['text'] ?? '' ) : ''; $by = (int) $row['user_id']; foreach ( self::find( $text ) as $uid ) { if ( $uid !== $by ) { self::notify( $uid, 'mention', (int) $row['contact_id'], $by, $text, (int) $id ); } } } /** * Someone gave a teammate a next step. * * @param int $id Task. * @param array $row Task row. */ public static function on_task( $id, $row ) { $to = (int) $row['assignee_id']; $by = (int) $row['created_by']; if ( ! $to || ! $by || $to === $by || self::quiet() ) { return; } self::notify( $to, 'task', (int) $row['contact_id'], $by, (string) $row['title'], (int) $id, (string) ( $row['due_at'] ?? '' ) ); } private static function quiet() { return class_exists( 'Nodebook_Sample' ) && Nodebook_Sample::$running; } /** * Add to someone's "For you" and email them. * * @param int $uid Who to tell. * @param string $kind mention|task. * @param int $contact Contact. * @param int $by Who did it. * @param string $text Note or task title. * @param int $ref Activity or task ID. * @param string $due Task due (GMT). */ private static function notify( $uid, $kind, $contact, $by, $text, $ref, $due = '' ) { $user = get_userdata( (int) $uid ); if ( ! $user || ! user_can( $user, Nodebook_Admin::cap() ) ) { return; } $items = get_user_meta( (int) $uid, self::META, true ); $items = is_array( $items ) ? $items : array(); array_unshift( $items, array( 'kind' => $kind, 'contact' => (int) $contact, 'by' => (int) $by, 'text' => mb_substr( (string) $text, 0, 400 ), 'ref' => (int) $ref, 'at' => time(), ) ); update_user_meta( (int) $uid, self::META, array_slice( $items, 0, self::KEEP ) ); /** * Whether to email a teammate about a mention or a next step given to them. * * @param bool $send Default true. * @param string $kind mention|task. * @param WP_User $user Who would get it. */ if ( ! apply_filters( 'nodebook_team_email', true, $kind, $user ) ) { return; } $c = $contact ? Nodebook_Contacts::get( (int) $contact ) : null; $name = $c ? Nodebook_Contacts::display_name( $c ) : ''; $who = Nodebook_Team::name( (int) $by ); $who = '' !== $who ? $who : __( 'Someone', 'nodebook' ); $url = $contact ? Nodebook_Admin::url( 'nodebook', array( 'action' => 'view', 'id' => (int) $contact, ) ) : Nodebook_Admin::url( 'nodebook-today' ); if ( 'task' === $kind ) { /* translators: 1: teammate, 2: contact name */ $subject = $name ? sprintf( __( '%1$s gave you a next step: %2$s', 'nodebook' ), $who, $name ) : sprintf( __( '%1$s gave you a next step', 'nodebook' ), $who ); $line = $text . ( '' !== $due ? ' · ' . Nodebook_Tasks::due_label( $due ) : '' ); } else { /* translators: 1: teammate, 2: contact name */ $subject = $name ? sprintf( __( '%1$s mentioned you on %2$s', 'nodebook' ), $who, $name ) : sprintf( __( '%1$s mentioned you', 'nodebook' ), $who ); $line = $text; } $html = '
' . esc_html( $subject ) . '
' . '' . nl2br( esc_html( $line ) ) . '' . '
' . esc_html( $name ? __( 'Open in Nodebook', 'nodebook' ) : __( 'Open Today', 'nodebook' ) ) . '