Making WordPress.org

Changeset 12608


Ignore:
Timestamp:
06/01/2023 02:48:05 AM (3 years ago)
Author:
dd32
Message:

API: Helpscout: Handle both the result of the Conversation API, and that of the payload sent with dynamic widgets.

Add a method to find the human readable mailbox name.

Location:
sites/trunk/api.wordpress.org/public_html/dotorg/helpscout
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/api.wordpress.org/public_html/dotorg/helpscout/common.php

    r12607 r12608  
    104104 */
    105105function get_user_email_for_email( $request ) {
    106         $email   = $request->customer->email ?? false;
    107         $subject = $request->ticket->subject ?? '';
    108         $user    = get_user_by( 'email', $email );
     106        $customer = $request->customer ?? ( $request->primaryCustomer ?? false );
     107        $email    = $customer->email ?? false;
     108        $subject  = $request->ticket->subject ?? ( $request->subject ?? '' );
     109        $user     = get_user_by( 'email', $email );
     110        $email_id = $request->ticket->id ?? ( $request->id ?? false );
    109111
    110112        // If this is related to a slack user, fetch their details instead.
    … …  
    117119
    118120        // If the customer object has alternative emails listed, check to see if they have a profile.
    119         if ( ! $user && ! empty( $request->customer->emails ) ) {
    120                 $user = get_user_from_emails( $request->customer->emails );
     121        if ( ! $user && ! empty( $customer->emails ) ) {
     122                $user = get_user_from_emails( $customer->emails );
    121123        }
    122124
    … …  
    127129
    128130        // Determine if this is a bounce, and if so, find out who for.
    129         if ( ! $user && $email && isset( $request->ticket->id ) ) {
    130                 $from          = strtolower( implode( ' ', array_filter( [ $email, ( $request->customer->fname ?? false ), ( $request->customer->lname ?? false ) ] ) ) );
     131        if ( ! $user && $email && $email_id ) {
     132                $from          = strtolower( implode( ' ', array_filter( [ $email, ( $customer->fname ?? false ), ( $customer->first ?? false ), ( $customer->lname ?? false ), ( $customer->last ?? false ) ] ) ) );
    131133                $subject_lower = strtolower( $subject );
    132134
    … …  
    146148
    147149                        // Fetch the email.
    148                         $email_obj = get_email_thread( $request->ticket->id ?? 0 );
     150                        $email_obj = get_email_thread( $email_id );
    149151                        if ( ! empty( $email_obj->_embedded->threads ) ) {
    150152                                $attachment_api_urls = [];
    … …  
    252254 */
    253255function get_plugin_or_theme_from_email( $request ) {
    254         $subject = $request->ticket->subject ?? '';
     256        $subject = $request->subject ?? ( $request->ticket->subject ?? '' );
    255257
    256258        $possible = [
    … …  
    261263        // Reported themes, shortcut, assume the slug is the title.. since it always is..
    262264        if ( str_starts_with( $subject, 'Reported Theme:' ) ) {
    263                 $possible['themes'][] = sanitize_title_with_dashes( trim( explode( ':', $request->ticket->subject )[1] ) );
     265                $possible['themes'][] = sanitize_title_with_dashes( trim( explode( ':', $subject )[1] ) );
    264266        }
    265267
    … …  
    281283
    282284        // Often a slug is mentioned in the title, so let's try to extract that.
    283         if ( preg_match_all( '!(?P<slug>[a-z0-9\-]{10,})!', $subject, $m ) ) {
     285        if ( preg_match_all( '!\b(?P<slug>[a-z0-9\-]{10,})!', $subject, $m ) ) {
    284286                $possible['plugins'] = array_merge( $possible['plugins'], $m['slug'] );
    285287                $possible['themes']  = array_merge( $possible['themes'],  $m['slug'] );
    … …  
    392394        return $user;
    393395}
     396
     397/**
     398 * Find the human-readable name for a mailbox ID.
     399 *
     400 * @param int|object $mailbox_id_or_request Mailbox ID or request object.
     401 */
     402function get_mailbox_name( $mailbox_id_or_request ) {
     403        $constants = [
     404                'data',
     405                'jobs',
     406                'openverse',
     407                'password_resets',
     408                'photos',
     409                'plugins',
     410                'themes',
     411        ];
     412
     413        $mailbox_id = $mailbox_id_or_request->mailboxId ?? $mailbox_id_or_request;
     414        if ( ! $mailbox_id || ! is_numeric( $mailbox_id ) ) {
     415                return 0;
     416        }
     417
     418        foreach ( $constants as $constant ) {
     419                if ( constant( 'HELPSCOUT_' . strtoupper( $constant ) . '_MAILBOXID' ) == $mailbox_id ) {
     420                        return $constant;
     421                }
     422        }
     423
     424        // Fetch the mailbox..
     425        $mailbox = cached_helpscout_get( "/mailboxes/{$mailbox_id}" );
     426        if ( ! $mailbox ) {
     427                return $mailbox_id;
     428        }
     429
     430        return sanitize_title( $mailbox->name );
     431}
  • sites/trunk/api.wordpress.org/public_html/dotorg/helpscout/profile.php

    r12607 r12608  
    2525
    2626                // When the Displayed account email doesn't match the email being displayed, output the user email address too.
    27                 if ( ! empty( $request->customer->email ) && $request->customer->email !== $user->user_email ) {
     27                if ( ! empty( $request->customer->email ) && strcasecmp( $request->customer->email, $user->user_email ) ) {
    2828                        $html .= '<p>Account Email: ' . esc_html( $user->user_email ) . '</p>';
    2929                }
    … …  
    5858                                esc_url( add_query_arg( 's', urlencode( $u->user_email ), 'https://login.wordpress.org/wp-admin/admin.php?page=user-registrations&s=' ) ),
    5959                                esc_html( $u->user_login ) . (
    60                                         $request->customer->email == $u->user_email ? '' : ' (' . esc_html( $u->user_email ) . ')'
     60                                        strcasecmp( $request->customer->email, $u->user_email ) ? ' (' . esc_html( $u->user_email ) . ')' : ''
    6161                                ),
    6262                                esc_html( $match_status( $u ) )
Note: See TracChangeset for help on using the changeset viewer.