Making WordPress.org

Changeset 14701


Ignore:
Timestamp:
03/12/2026 03:43:47 AM (3 months ago)
Author:
dd32
Message:

Plugin Directory: Stats: Gate HelpScout queries and display behind HELPSCOUT_PLUGINS_MAILBOXID constant.

Wrap HelpScout DB queries, API calls, and the Help Scout Queue Stats display section in defined() checks
to avoid errors when the HelpScout tables and API are not available.
Also adds null-safe defaults for the in_queue_pending_why stats.

For local-development environments.

See https://github.com/WordPress/wordpress.org/pull/555

File:
1 edited

Legend:

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

    r13063 r14701  
    155155
    156156        // # Break down the plugins in the queue, based on if we're still waiting on a reply.
    157         $stats['in_queue_pending_why'] = $wpdb->get_row( $wpdb->prepare(
    158             'SELECT
    159                 SUM( IF( active = 0 AND closed > 0, 1, 0 ) ) AS `author`,
    160                 SUM( IF( active > 0, 1, 0 ) ) AS `reviewer`,
    161                 SUM( IF( active = 0 AND closed = 0, 1, 0 ) ) AS `noemail`
    162             FROM (
    163                 SELECT
    164                     p.post_name,
    165                     SUM( IF( emails.status = "closed", 1, 0 ) ) AS `closed`,
    166                     SUM( IF( emails.status = "active", 1, 0 ) ) AS `active`
    167                 FROM %i p
    168                     LEFT JOIN %i meta ON meta.meta_key = "plugins" AND meta.meta_value = p.post_name
    169                     LEFT JOIN %i emails ON meta.helpscout_id = emails.id
    170                 WHERE p.post_status = "pending"
    171                 GROUP BY p.ID
    172             ) subquery',
    173             $wpdb->posts,
    174             "{$wpdb->base_prefix}helpscout_meta",
    175             "{$wpdb->base_prefix}helpscout",
    176         ), ARRAY_A );
    177 
    178         $stats['in_queue_pending_why'] = array_map( 'intval', $stats['in_queue_pending_why'] );
     157        // Requires the helpscout tables to exist.
     158        if ( defined( 'HELPSCOUT_PLUGINS_MAILBOXID' ) ) {
     159            $stats['in_queue_pending_why'] = $wpdb->get_row( $wpdb->prepare(
     160                'SELECT
     161                    SUM( IF( active = 0 AND closed > 0, 1, 0 ) ) AS `author`,
     162                    SUM( IF( active > 0, 1, 0 ) ) AS `reviewer`,
     163                    SUM( IF( active = 0 AND closed = 0, 1, 0 ) ) AS `noemail`
     164                FROM (
     165                    SELECT
     166                        p.post_name,
     167                        SUM( IF( emails.status = "closed", 1, 0 ) ) AS `closed`,
     168                        SUM( IF( emails.status = "active", 1, 0 ) ) AS `active`
     169                    FROM %i p
     170                        LEFT JOIN %i meta ON meta.meta_key = "plugins" AND meta.meta_value = p.post_name
     171                        LEFT JOIN %i emails ON meta.helpscout_id = emails.id
     172                    WHERE p.post_status = "pending"
     173                    GROUP BY p.ID
     174                ) subquery',
     175                $wpdb->posts,
     176                "{$wpdb->base_prefix}helpscout_meta",
     177                "{$wpdb->base_prefix}helpscout",
     178            ), ARRAY_A );
     179
     180            $stats['in_queue_pending_why'] = array_map( 'intval', $stats['in_queue_pending_why'] ?? [] );
     181        }
    179182
    180183        // # of plugins currently in the queue (new + pending)
     
    200203        // --------------
    201204
    202         $start_datetime = gmdate( 'Y-m-d\T00:00:00\Z', strtotime( $args['date'] ) - ( $args['num_days'] * DAY_IN_SECONDS ) );
    203         $end_datetime   = gmdate( 'Y-m-d\T23:59:59\Z', strtotime( $args['date'] ) );
    204 
    205         $api_payload = [
    206             'start'     => $start_datetime,
    207             'end'       => $end_datetime,
    208             'mailboxes' => HELPSCOUT_PLUGINS_MAILBOXID,
    209         ];
    210        
    211         $company_report  = HelpScout::api( '/v2/reports/company', $api_payload );
    212         $mailbox_overall = HelpScout::api( '/v2/reports/conversations', $api_payload );
    213         $email_report    = HelpScout::api( '/v2/reports/email', $api_payload );
    214 
    215         // If any of the API's are unavailable, make it obvious that the requests have failed, but returning 0's for everything.
    216         if ( ! $company_report || ! $mailbox_overall || ! $email_report ) {
    217             $company_report = $mailbox_overall = $email_report = false;
    218         }
    219 
    220         $stats['helpscout_queue_total_conversations']     = $mailbox_overall->current->totalConversations ?? 0;
    221         $stats['helpscout_queue_new_conversations']       = $mailbox_overall->current->newConversations ?? 0;
    222         $stats['helpscout_queue_customers']               = $mailbox_overall->current->customers ?? 0;
    223         $stats['helpscout_queue_conversations_per_day']   = $mailbox_overall->current->conversationsPerDay ?? 0;
    224         $stats['helpscout_queue_busiest_day']             = gmdate( 'l', strtotime( 'Sunday +' . ( $mailbox_overall->busiestDay->day ?? 0 ) . ' days' ) ); // Hacky? but works
    225         $stats['helpscout_queue_messages_received']       = $mailbox_overall->current->messagesReceived ?? 0;
    226         $stats['helpscout_queue_replies_sent']            = $company_report->current->totalReplies;
    227         $stats['helpscout_queue_emails_created']          = $email_report->current->volume->emailsCreated ?? 0;
     205        if ( defined( 'HELPSCOUT_PLUGINS_MAILBOXID' ) ) {
     206            $start_datetime = gmdate( 'Y-m-d\T00:00:00\Z', strtotime( $args['date'] ) - ( $args['num_days'] * DAY_IN_SECONDS ) );
     207            $end_datetime   = gmdate( 'Y-m-d\T23:59:59\Z', strtotime( $args['date'] ) );
     208
     209            $api_payload = [
     210                'start'     => $start_datetime,
     211                'end'       => $end_datetime,
     212                'mailboxes' => HELPSCOUT_PLUGINS_MAILBOXID,
     213            ];
     214
     215            $company_report  = HelpScout::api( '/v2/reports/company', $api_payload );
     216            $mailbox_overall = HelpScout::api( '/v2/reports/conversations', $api_payload );
     217            $email_report    = HelpScout::api( '/v2/reports/email', $api_payload );
     218
     219            // If any of the API's are unavailable, make it obvious that the requests have failed, but returning 0's for everything.
     220            if ( ! $company_report || ! $mailbox_overall || ! $email_report ) {
     221                $company_report = $mailbox_overall = $email_report = false;
     222            }
     223
     224            $stats['helpscout_queue_total_conversations']     = $mailbox_overall->current->totalConversations ?? 0;
     225            $stats['helpscout_queue_new_conversations']       = $mailbox_overall->current->newConversations ?? 0;
     226            $stats['helpscout_queue_customers']               = $mailbox_overall->current->customers ?? 0;
     227            $stats['helpscout_queue_conversations_per_day']   = $mailbox_overall->current->conversationsPerDay ?? 0;
     228            $stats['helpscout_queue_busiest_day']             = gmdate( 'l', strtotime( 'Sunday +' . ( $mailbox_overall->busiestDay->day ?? 0 ) . ' days' ) ); // Hacky? but works
     229            $stats['helpscout_queue_messages_received']       = $mailbox_overall->current->messagesReceived ?? 0;
     230            $stats['helpscout_queue_replies_sent']            = $company_report->current->totalReplies;
     231            $stats['helpscout_queue_emails_created']          = $email_report->current->volume->emailsCreated ?? 0;
     232        }
    228233
    229234        return $stats;
     
    529534                printf(
    530535                    __( '→ (pending; waiting on author)* : %d', 'wporg-plugins' ),
    531                     esc_html( $stats['in_queue_pending_why']['author'] )
     536                    esc_html( $stats['in_queue_pending_why']['author'] ?? 0 )
    532537                );
    533538            ?>
     
    538543                printf(
    539544                    __( '→ (pending; waiting on reviewer)* : %d', 'wporg-plugins' ),
    540                     esc_html( $stats['in_queue_pending_why']['reviewer'] )
    541                 );
    542             ?>
    543             </li>
    544             <?php if ( $stats['in_queue_pending_why']['noemail'] ) : ?>
     545                    esc_html( $stats['in_queue_pending_why']['reviewer'] ?? 0 )
     546                );
     547            ?>
     548            </li>
     549            <?php if ( ! empty( $stats['in_queue_pending_why']['noemail'] ) ) : ?>
    545550                <li>
    546551                <?php
     
    555560        </ul>
    556561
     562        <?php if ( defined( 'HELPSCOUT_PLUGINS_MAILBOXID' ) ) : ?>
    557563        <h3><?php _e( 'Help Scout Queue Stats', 'wporg-plugins' ); ?></h3>
    558564
     
    623629            </li>
    624630        </ul>
     631        <?php endif; ?>
    625632
    626633        <ul style="font-style:italic;">
Note: See TracChangeset for help on using the changeset viewer.