Making WordPress.org

Changeset 12464


Ignore:
Timestamp:
03/13/2023 06:19:16 AM (4 years ago)
Author:
dd32
Message:

Helpscout: When an email comes into plugins@ or themes@ and the email author doesn't have any plugins/themes, see if they've mentioned one and link to that instead.

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

    r12307 r12464  
    108108}
    109109
     110function get_plugin_or_theme_from_email( $request ) {
     111        $subject = $request->ticket->subject ?? '';
     112
     113        $possible = [
     114                'themes' => [],
     115                'plugins' => []
     116        ];
     117
     118        // Fetch the email.
     119        $email_obj = Helpscout_API::api( '/v2/conversations/' . $request->ticket->id . '?embed=threads' );
     120        if ( ! empty( $email_obj->_embedded->threads ) ) {
     121                foreach ( $email_obj->_embedded->threads as $thread ) {
     122                        if ( 'customer' !== $thread->type ) {
     123                                continue;
     124                        }
     125
     126                        // Extract matches from the email.
     127                        $email_body = strip_tags( str_replace( '<br>', "\n", $thread->body ) );
     128
     129                        if ( ! preg_match_all( '!/(?<type>plugins|themes)/(?P<slug>[a-z0-9-]+)/?!im', $email_body, $m ) ) {
     130                                preg_match_all( '!(?P<type>Plugin|Theme):\s*(?P<slug>[a-z0-9-]+)$!im', $email_body, $m );
     131                        }
     132
     133                        if ( $m ) {
     134                                foreach ( $m[0] as $i => $match ) {
     135                                        $type = strtolower( $m['type'][ $i ] );
     136                                        $slug = strtolower( $m['slug'][ $i ] );
     137                                        $possible[ $type ][] = $slug;
     138                                }
     139                        }
     140                }
     141        }
     142
     143        return $possible;
     144}
     145
    110146// HelpScout sends json data in the POST, so grab it from the input directly.
    111147$HTTP_RAW_POST_DATA = file_get_contents( 'php://input' );
  • sites/trunk/api.wordpress.org/public_html/dotorg/helpscout/plugins-themes.php

    r11907 r12464  
    1212$user  = get_user_by( 'email', $email );
    1313
    14 if ( ! $user ) {
    15         echo json_encode( array( 'html' => $html ) );
    16         die();
    17 }
    18 
    1914foreach ( [
    2015        /* site_id => [ textual singular, post_type ] */
    … …  
    2419        list( $type, $post_type ) = $details;
    2520
     21        $is_this_inbox = ( "{$type}s@wordpress.org" === $request->mailbox->email );
     22
    2623        switch_to_blog( $site_id );
    2724
    2825        $slugs = [];
    29         if ( 'plugin' === $type ) {
     26
     27        if ( 'plugin' === $type && $user ) {
    3028                // Committer to a plugin.
    31                 $slugs = $wpdb->get_col( $wpdb->prepare( 'SELECT path FROM `' . PLUGINS_TABLE_PREFIX . 'svn_access' . '` WHERE user = %s', $user->user_login ) );
    32                 $slugs = array_filter( array_map( function( $slug ) {
    33                         return ltrim( $slug, '/' ) ?: false;
    34                 }, $slugs ) );
     29                $committer_plugins = $wpdb->get_col( $wpdb->prepare( 'SELECT path FROM `' . PLUGINS_TABLE_PREFIX . 'svn_access' . '` WHERE user = %s', $user->user_login ) );
     30                array_map(
     31                        function( $slug ) use( $slugs ) {
     32                                $slug = ltrim( $slug, '/' );
     33                                if ( $slug ) {
     34                                        $slugs[] = $slug;
     35                                }
     36                        },
     37                        $committer_plugins
     38                );
    3539
    3640                // TODO: Would be nice to pull for support reps too, but that's less common.
    … …  
    4549        }
    4650
    47         $slugs    = $slugs ? '"' . implode( '", "', array_map( 'esc_sql', $slugs ) ) . '"' : '';
    48         $or_slugs = $slugs ? "OR post_name IN( {$slugs} )" : '';
     51        $lookup_by_user = $user ? $user->ID : false;
     52        $counts         = false;
     53        $sources        = [
     54                'by_email_author'
     55        ];
     56        if ( $is_this_inbox ) {
     57                $sources[] = 'check_email';
     58        }
     59        foreach ( $sources as $source ) {
     60                if ( 'check_email' === $source ) {
     61                        // Check the email for a plugin/theme name.
     62                        $mentioned = get_plugin_or_theme_from_email( $request );
     63                        if ( empty( $mentioned[ "{$type}s"] ) ) {
     64                                break;
     65                        }
    4966
    50         $counts = $wpdb->get_results( $wpdb->prepare(
    51                 "SELECT post_status, COUNT(*) as count, group_concat( ID ORDER BY post_title ) as ids, group_concat( post_title ORDER BY post_title SEPARATOR ', ' ) as titles
    52                 FROM $wpdb->posts
    53                 WHERE post_type = %s AND ( post_author = %s {$or_slugs} )
    54                 GROUP BY post_status
    55                 ORDER BY FIELD( post_status, 'new', 'pending', 'publish', 'disabled', 'delisted', 'closed', 'approved', 'suspended', 'rejected', 'draft' )",
    56                 $post_type,
    57                 $user->ID
    58         ) );
     67                        $lookup_by_user = false;
     68                        $slugs          = $mentioned[ "{$type}s" ];
     69                }
     70
     71                $slugs       = $slugs          ? '"' . implode( '", "', array_map( 'esc_sql', array_unique( $slugs ) ) ) . '"' : '';
     72                $or_slugs    = $slugs          ? "post_name IN( {$slugs} )" : '';
     73                $post_author = $lookup_by_user ? $wpdb->prepare( "post_author = %s", $lookup_by_user ) : '';
     74
     75                $where = implode( ' OR ', array_filter( [ $post_author, $or_slugs ] ) );
     76
     77                if ( ! $where ) {
     78                        continue;
     79                }
     80
     81                $counts = $wpdb->get_results( $wpdb->prepare(
     82                        "SELECT post_status, COUNT(*) as count, group_concat( ID ORDER BY post_title ) as ids, group_concat( post_title ORDER BY post_title SEPARATOR ', ' ) as titles
     83                        FROM $wpdb->posts
     84                        WHERE post_type = %s AND ( {$where} )
     85                        GROUP BY post_status
     86                        ORDER BY FIELD( post_status, 'new', 'pending', 'publish', 'disabled', 'delisted', 'closed', 'approved', 'suspended', 'rejected', 'draft' )",
     87                        $post_type,
     88                ) );
     89
     90                if ( $counts && 'check_email' === $source ) {
     91                        $html .= '<p><strong>Note: ' . ucwords( $type ) . ' may not be authored the email author.</strong><p>';
     92                }
     93
     94                if ( $counts ) {
     95                        break;
     96                }
     97        }
    5998
    6099        if ( $counts ) {
    … …  
    73112                $html .= sprintf(
    74113                        '<p><a href="%s" title="%s">%s</a></p>',
    75                         add_query_arg( [ 'post_type' => $post_type, 'author' => $user->ID ], admin_url( 'edit.php' ) ),
     114                        add_query_arg( [ 'post_type' => $post_type, 'author' => $user->ID ?? '' ], admin_url( 'edit.php' ) ),
    76115                        esc_attr( $post_statii ),
    77116                        ucwords( _n( "$total $type", "{$total} {$type}s", $total ) ) // Real bad internationalisation where internationalisation will never be used.
    … …  
    79118
    80119                // plugins@ and themes@ - expand and provide direct links.
    81                 if ( "{$type}s@wordpress.org" === $request->mailbox->email ) {
     120                if ( $is_this_inbox ) {
    82121                        $html .= '<ul>';
    83122                        foreach ( $ids as $post_id ) {
Note: See TracChangeset for help on using the changeset viewer.