Making WordPress.org


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

HelpScout: Show the correct user details for bounced emails.

File:
1 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 );
Note: See TracChangeset for help on using the changeset viewer.