Making WordPress.org

Changeset 13650


Ignore:
Timestamp:
05/02/2024 07:05:00 AM (7 months ago)
Author:
dd32
Message:

Plugin Directory: Centralise the Helpscout email table query code.

See #7385.

Location:
sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/admin/metabox/class-helpscout.php

    r13202 r13650  
    1515class HelpScout {
    1616    public static function display() {
    17         global $wpdb;
    1817        $post = get_post();
    1918
    20         // Trim off the rejected prefix/suffix.
    21         $slug   = preg_replace( '/(^rejected-|-rejected(-\d)?$)/i', '', $post->post_name );
    22 
    2319        // If the slug is not set, we can't query HelpScout.
    24         if ( ! $post->post_name || ! $slug ) {
     20        if ( ! $post->post_name ) {
    2521            echo 'Invalid Slug, cannot query emails.';
    2622            return;
    2723        }
    2824
    29         $emails = $wpdb->get_results( $wpdb->prepare(
    30             "SELECT emails.*
    31                 FROM %i emails
    32                     JOIN %i meta ON emails.id = meta.helpscout_id
    33                 WHERE meta.meta_key = 'plugins' AND meta.meta_value IN( %s, %s )
    34                 ORDER BY `created` DESC",
    35             "{$wpdb->base_prefix}helpscout",
    36             "{$wpdb->base_prefix}helpscout_meta",
    37             $slug,
    38             $post->post_name
    39         ) );
     25        $emails = Tools::get_helpscout_emails( $post );
    4026
    4127        echo '<table class="widefat striped helpscout-emails">';
     
    6551                sprintf(
    6652                    '<a href="%s" title="%s">%s</a>',
    67                     esc_url( 'https://secure.helpscout.net/conversation/' . $email->id . '/' . $email->number ),
     53                    esc_url( $email->url ),
    6854                    esc_attr( $email->preview ),
    6955                    esc_html( $subject )
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/api/routes/class-plugin-review.php

    r13649 r13650  
    11<?php
    22namespace WordPressdotorg\Plugin_Directory\API\Routes;
    3 use WordPressdotorg\Plugin_Directory\Shortcodes\Upload_Handler;
    43use WordPressdotorg\Plugin_Directory\API\Base;
    54use WordPressdotorg\Plugin_Directory\Template;
     5use WordPressdotorg\Plugin_Directory\Tools;
    66use WP_Error;
    77use WP_REST_Server;
     
    9191            'post_status'   => $post->post_status,
    9292            'edit_url'      => add_query_arg( [ 'action' => 'edit', 'post' => $post->ID ], admin_url( 'post.php' ) ),
    93             'helpscout'     => null, // Link to Review email (or most recent open email)
     93            'helpscout'     => null, // Most recent email details.
    9494            'submitter'     => [
    9595                'user_login' => $submitter->user_login,
     
    106106            $details['download_link'] = null;
    107107            $details['preview_link']  = null;
     108            $details['helpscout']     = Tools::get_helpscout_emails( $post, [ 'subject' => 'Review in Progress:', 'limit' => 1 ] );
     109        } else {
     110            $details['helpscout']     = Tools::get_helpscout_emails( $post, [ 'limit' => 1 ] );
    108111        }
    109112
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/class-tools.php

    r13577 r13650  
    678678        return 200 === wp_remote_retrieve_response_code( $request );
    679679    }
     680
     681    /**
     682     * Fetch the list of known Helpscout emails for a given post.
     683     *
     684     * @param \WP_Post $post The post to fetch emails for.
     685     * @return array
     686     */
     687    public static function get_helpscout_emails( $post, $filters = [] ) {
     688        global $wpdb;
     689
     690        $limit = 999;
     691        if ( isset( $filters['limit'] ) ) {
     692            $limit = absint( $filters['limit'] );
     693            unset( $filters['limit'] );
     694        }
     695
     696        // Trim off the rejected prefix/suffix.
     697        $slug   = preg_replace( '/(^rejected-|-rejected(-\d)?$)/i', '', $post->post_name );
     698        $wheres = '';
     699
     700        foreach ( $filters as $key => $value ) {
     701            $wheres .= $wpdb->prepare( 'AND emails.%i LIKE %s ', $key, '%' . $value . '%' );
     702        }
     703
     704        $emails = $wpdb->get_results( $wpdb->prepare(
     705            "SELECT emails.*
     706                FROM %i emails
     707                    JOIN %i meta ON emails.id = meta.helpscout_id
     708                WHERE meta.meta_key = 'plugins' AND meta.meta_value IN( %s, %s )
     709                    $wheres
     710                ORDER BY `created` DESC
     711                LIMIT %d",
     712            "{$wpdb->base_prefix}helpscout",
     713            "{$wpdb->base_prefix}helpscout_meta",
     714            $slug,
     715            $post->post_name,
     716            $limit
     717        ) );
     718
     719        foreach ( $emails as &$email ) {
     720            $email->url = 'https://secure.helpscout.net/conversation/' . $email->id . '/' . $email->number;
     721        }
     722
     723        if ( 1 === $limit ) {
     724            return reset( $emails );
     725        }
     726
     727        return $emails;
     728    }
    680729}
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/shortcodes/class-upload-handler.php

    r13649 r13650  
    738738     */
    739739    public static function find_review_email( $post ) {
    740         global $wpdb;
    741 
    742740        if ( ! in_array( $post->post_status, [ 'new', 'pending' ] ) || ! $post->post_name ) {
    743741            return false;
    744742        }
    745743
    746         // Find the latest email for this plugin that looks like a review email.
    747         return $wpdb->get_row( $wpdb->prepare(
    748             "SELECT emails.*
    749                 FROM %i emails
    750                     JOIN %i meta ON emails.id = meta.helpscout_id
    751                 WHERE meta.meta_key = 'plugins' AND meta.meta_value = %s
    752                     AND emails.subject LIKE %s
    753                 ORDER BY `created` DESC
    754                 LIMIT 1",
    755             "{$wpdb->base_prefix}helpscout",
    756             "{$wpdb->base_prefix}helpscout_meta",
    757             $post->post_name,
    758             '%Review in Progress:%' // The subject line of the review email.
    759         ) );
     744        return Tools::get_helpscout_emails( $post, [ 'subject' => 'Review in Progress:', 'limit' => 1 ] );
    760745    }
    761746
Note: See TracChangeset for help on using the changeset viewer.