Making WordPress.org

Changeset 11823


Ignore:
Timestamp:
05/10/2022 04:03:43 AM (3 years ago)
Author:
dd32
Message:

HelpScout: Show the correct user details for bounced emails.

Location:
sites/trunk/api.wordpress.org/public_html/dotorg/helpscout
Files:
1 added
5 edited

Legend:

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

    r11636 r11823  
    11<?php
     2use WordPressdotorg\API\HelpScout\API as Helpscout_API;
    23
    3 $wp_init_host = 'https://wordpress.org/';
     4$wp_init_host = 'https://api.wordpress.org/';
    45$base_dir = dirname( dirname( __DIR__ ) );
    56require( $base_dir . '/wp-init.php' );
     7
     8include_once __DIR__ . '/class-helpscout.php';
    69
    710// function to verify signature from HelpScout
     
    1417
    1518    return hash_equals( $signature, $calculated );
     19}
     20
     21function get_user_email_for_email( $request ) {
     22    $email   = $request->customer->email ?? false;
     23    $subject = $request->ticket->subject ?? '';
     24    $user    = get_user_by( 'email', $email );
     25
     26    // Ignore @wordpress.org "users", unless it's literally the only match (The ?? $email fallback at the end).
     27    if ( $user && str_ends_with( $user->user_email, '@wordpress.org' ) ) {
     28        $user = false;
     29    }
     30
     31    // If this is related to a slack user, fetch their details instead.
     32    if (
     33        false !== stripos( $email, 'slack' ) &&
     34        preg_match( '/(\S+)@chat.wordpress.org/i', $subject, $m )
     35    ) {
     36        $user = get_user_by( 'slug', $m[1] );
     37    }
     38
     39    // Determine if this is a bounce, and if so, find out who for.
     40    if ( ! $user && $email && isset( $request->ticket->id ) ) {
     41        $from = strtolower( $email . ' ' . ( $request->customer->fname ?? '' ) . ' ' . $request->customers->lname );
     42        if (
     43            str_contains( $from, 'mail delivery' ) ||
     44            str_contains( $from, 'postmaster' ) ||
     45            str_contains( strtolower( $subject ), 'undelivered mail' ) ||
     46            str_contains( strtolower( $subject ), 'returned to sender')
     47        ) {
     48            // Fetch the email.
     49            $email_obj = Helpscout_API::api( '/v2/conversations/' . $request->ticket->id . '?embed=threads' );
     50            if ( ! empty( $email_obj->_embedded->threads ) ) {
     51                foreach ( $email_obj->_embedded->threads as $thread ) {
     52                    if ( 'customer' !== $thread->type ) {
     53                        continue;
     54                    }
     55
     56                    // Extract emails from the mailer-daemon.
     57                    $email_body = strip_tags( str_replace( '<br>', "\n", $thread->body ) );
     58
     59                    // Extract `To:`, `X-Orig-To:`, and fallback to all emails.
     60                    $emails = [];
     61                    if ( preg_match( '!^(x-orig-to:|to:)\s*(.+@.+)$!im', $email_body, $m ) ) {
     62                        $emails = [ trim( $m[2], '<> ' ) ];
     63                    } else {
     64                        // Ugly regex for emails, but it's good for mailer-daemon emails.
     65                        if ( preg_match_all( '![^\s;"]+@[^\s;&"]+\.[^\s;&"]+!', $email_body, $m ) ) {
     66                            $emails = array_unique( array_diff( $m[0], [ $request->mailbox->email ] ) );
     67                        }
     68                    }
     69
     70                    foreach ( $emails as $maybe_email ) {
     71                        $user = get_user_by( 'email', $maybe_email );
     72                        if ( $user ) {
     73                            break;
     74                        }
     75                    }
     76                }
     77            }
     78        }
     79    }
     80
     81    return $user->user_email ?? $email;
    1682}
    1783
     
    3197
    3298// get the info from HS
    33 $data = json_decode( $data );
    34 
    35 // If this is related to a slack user, fetch their details instead.
    36 if (
    37     isset ( $data->customer->email, $data->ticket->subject ) &&
    38     false !== stripos( $data->customer->email, 'slack' ) &&
    39     preg_match( '/(\S+)@chat.wordpress.org/i', $data->ticket->subject, $m )
    40 ) {
    41     $user = get_user_by( 'slug', $m[1] );
    42     if ( $user ) {
    43         $data->customer->email = $user->user_email;
    44     }
    45 }
    46 
    47 return $data;
     99return json_decode( $data );
  • sites/trunk/api.wordpress.org/public_html/dotorg/helpscout/dpo.php

    r10957 r11823  
    1414// default empty output
    1515$html  = '';
    16 $email = $request->customer->email;
     16$email = get_user_email_for_email( $request );
    1717
    1818$html .= sprintf(
  • sites/trunk/api.wordpress.org/public_html/dotorg/helpscout/forums.php

    r11296 r11823  
    66
    77// default empty output
    8 $html = '';
     8$html  = '';
    99// look up profile url by email
    10 $user = get_user_by( 'email', $request->customer->email );
     10$email = get_user_email_for_email( $request );
     11$user  = get_user_by( 'email', $email );
    1112
    1213// Include Notes
  • sites/trunk/api.wordpress.org/public_html/dotorg/helpscout/plugins-themes.php

    r11296 r11823  
    77// default empty output
    88$html = '';
     9
    910// look up profile url by email
    10 $user = get_user_by( 'email', $request->customer->email );
     11$email = get_user_email_for_email( $request );
     12$user  = get_user_by( 'email', $email );
     13
    1114if ( ! $user ) {
    1215    echo json_encode( array( 'html' => $html ) );
  • sites/trunk/api.wordpress.org/public_html/dotorg/helpscout/profile.php

    r11296 r11823  
    88$html = '';
    99$user = false;
     10$email = get_user_email_for_email( $request );
    1011
    1112// Look up a user based on email address
    12 if ( isset ( $request->customer->email ) ) {
     13if ( $email ) {
    1314
    1415    // look up profile url by email
    15     $user = get_user_by( 'email', $request->customer->email );
     16    $user = get_user_by( 'email', $email );
     17
    1618    if ( isset( $user->user_nicename ) ) {
    1719        $html .= '<p>Profile: <a href="https://profiles.wordpress.org/' . $user->user_nicename . '/">'. $user->user_nicename .'</a></p>';
Note: See TracChangeset for help on using the changeset viewer.