Making WordPress.org

Changeset 11726


Ignore:
Timestamp:
03/31/2022 08:55:05 AM (2 years ago)
Author:
dd32
Message:

Plugin Directory: Admin: Stats: Retrieve email stats from Help Scout.

See #4306.

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

Legend:

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

    r7622 r11726  
    22namespace WordPressdotorg\Plugin_Directory\Admin\Tools;
    33use WordPressdotorg\Plugin_Directory\Template;
     4use WordPressdotorg\Plugin_Directory\Clients\HelpScout;
    45
    56/**
     
    5354     *     Array of stats.
    5455     *
    55      *     @type int $plugin_approve                     The number of plugins approved within the defined time interval.
    56      *     @type int $plugin_delist                      The number of plugins delisted within the defined time interval.
    57      *     @type int $plugin_new                         The number of plugins submitted within the defined time interval.
    58      *     @type int $plugin_reject                      The number of plugins rejected within the defined time interval.
    59      *     @type int $in_queue                           The number of plugins currently in the queue (new or pending).
    60      *     @type int $in_queue_new                       The number of new plugins currently in the queue.
    61      *     @type int $in_queue_pending                   The number of pending plugins currently in the queue.
    62      *     @type int $in_queue_from_time_window          The number of plugins currently in the queue submitted during the specified time window.
    63      *     @type int $in_queue_old                       The number of plugins currently in the queue that are older than "recently".
    64      *     @type int $supportpress_queue_total_open      The number of currently open support threads.
    65      *     @type int $supportpress_queue_total_open_old  The number of currently open support threads with no activity "recently".
    66      *     @type int $supportpress_queue_interval_all    The number of threads (from just within the specified time window).
    67      *     @type int $supportpress_queue_interval_closed The number of closed threads (from just withing the specified time window).
    68      *     @type int $supportpress_queue_interval_open   The number of open threads (from just withing the specified time window).
     56     *     @type int $plugin_approve                        The number of plugins approved within the defined time interval.
     57     *     @type int $plugin_delist                         The number of plugins delisted within the defined time interval.
     58     *     @type int $plugin_new                            The number of plugins submitted within the defined time interval.
     59     *     @type int $plugin_reject                         The number of plugins rejected within the defined time interval.
     60     *     @type int $in_queue                              The number of plugins currently in the queue (new or pending).
     61     *     @type int $in_queue_new                          The number of new plugins currently in the queue.
     62     *     @type int $in_queue_pending                      The number of pending plugins currently in the queue.
     63     *     @type int $in_queue_from_time_window             The number of plugins currently in the queue submitted during the specified time window.
     64     *     @type int $in_queue_old                          The number of plugins currently in the queue that are older than "recently".
     65     *     @type int $helpscout_queue_total_conversations   The number of ongoing Help Scout conversations.
     66     *     @type int $helpscout_queue_new_conversations     The number of new Help Scout conversations.
     67     *     @type int $helpscout_queue_customers             The number of unique Plugin authors contacted.
     68     *     @type int $helpscout_queue_conversations_per_day The number of Help Scout conversations per day.
     69     *     @type int $helpscout_queue_busiest_day           The busiest day in the Help Scout queue.
     70     *     @type int $helpscout_queue_messages_received     The number of emails received in HelpScout.
     71     *     @type int $helpscout_queue_replies_sent          The number of replies sent to emails.
     72     *     @type int $helpscout_queue_emails_created        The number of new outgoing conversations created.
    6973     * }
    7074     */
     
    7276        global $wpdb;
    7377
    74         $stats['as_of_date'] = strftime( '%Y-%m-%d', time() );
     78        $stats['as_of_date'] = gmdate( 'Y-m-d' );
    7579
    7680        $defaults = array(
     
    151155
    152156        // --------------
    153         // SupportPress Queue
    154         // --------------
    155         // # of currently open threads
    156         $stats['supportpress_queue_total_open'] = $wpdb->get_var(
    157             "SELECT COUNT(*) FROM plugins_support_threads WHERE state = 'open'"
    158         );
    159 
    160         // # of currently open threads with no activity in last 7 days
    161         $stats['supportpress_queue_total_open_old'] = $wpdb->get_var( $wpdb->prepare(
    162             "SELECT COUNT(*) FROM plugins_support_threads WHERE state = 'open' AND dt < DATE_SUB( NOW(), INTERVAL %d DAY )",
    163             $args['recentdays']
    164         ) );
    165 
    166         // # of total threads (from just those received during the time window)
    167         $stats['supportpress_queue_interval_all'] = $wpdb->get_var( $wpdb->prepare(
    168             "SELECT COUNT(*) FROM plugins_support_threads WHERE state IN ( 'closed', 'open' ) AND dt < %s AND dt > DATE_SUB( %s, INTERVAL %d DAY )",
    169             $args['date'],
    170             $args['date'],
    171             absint( $args['num_days'] ) + 1
    172         ) );
    173 
    174         // # of open and closed threads (from just those received during the time window)
    175         $sp_states = array( 'closed', 'open' );
    176         foreach ( $sp_states as $sp_state ) {
    177             $stats[ 'supportpress_queue_interval_' . $sp_state ] = $wpdb->get_var( $wpdb->prepare(
    178                 'SELECT COUNT(*) FROM plugins_support_threads WHERE state = %s AND dt < %s AND dt > DATE_SUB( %s, INTERVAL %d DAY )',
    179                 $sp_state,
    180                 $args['date'],
    181                 $args['date'],
    182                 absint( $args['num_days'] ) + 1
    183             ) );
    184         }
     157        // Help Scout Queue
     158        // --------------
     159
     160        $start_datetime = gmdate( 'Y-m-d\T00:00:00\Z', strtotime( $args['date'] ) - ( $args['num_days'] * DAY_IN_SECONDS ) );
     161        $end_datetime   = gmdate( 'Y-m-d\T23:59:59\Z', strtotime( $args['date'] ) );
     162
     163        $api_payload = [
     164            'start'     => $start_datetime,
     165            'end'       => $end_datetime,
     166            'mailboxes' => HELPSCOUT_PLUGINS_MAILBOXID,
     167        ];
     168       
     169        $company_report  = HelpScout::api( '/v2/reports/company', $api_payload );
     170        $mailbox_overall = HelpScout::api( '/v2/reports/conversations', $api_payload );
     171        $email_report    = HelpScout::api( '/v2/reports/email', $api_payload );
     172
     173        $stats['helpscout_queue_total_conversations']     = $mailbox_overall->current->totalConversations ?? 0;
     174        $stats['helpscout_queue_new_conversations']       = $mailbox_overall->current->newConversations ?? 0;
     175        $stats['helpscout_queue_customers']               = $mailbox_overall->current->customers ?? 0;
     176        $stats['helpscout_queue_conversations_per_day']   = $mailbox_overall->current->conversationsPerDay ?? 0;
     177        $stats['helpscout_queue_busiest_day']             = gmdate( 'l', strtotime( 'Sunday +' . $mailbox_overall->busiestDay->day . ' days' ) ); // Hacky? but works
     178        $stats['helpscout_queue_messages_received']       = $mailbox_overall->current->messagesReceived ?? 0;
     179        $stats['helpscout_queue_replies_sent']            = $company_report->current->totalReplies;
     180        $stats['helpscout_queue_emails_created']          = $email_report->current->volume->emailsCreated ?? 0;
    185181
    186182        return $stats;
     
    198194        $args = array();
    199195
    200         if ( isset( $_POST['date'] ) && preg_match( '/[0-9]{4}\-[0-9]{2}\-[0-9]{2}$/', $_POST['date'] ) ) {
    201             $args['date'] = $_POST['date'];
     196        if ( isset( $_REQUEST['date'] ) && preg_match( '/[0-9]{4}\-[0-9]{2}\-[0-9]{2}$/', $_REQUEST['date'] ) ) {
     197            $args['date'] = $_REQUEST['date'];
    202198        } else {
    203             $args['date'] = '';
     199            $args['date'] = gmdate( 'Y-m-d' );
    204200        }
    205201
    206         $args['num_days']   = empty( $_POST['days'] ) ? '' : absint( $_POST['days'] );
    207         $args['recentdays'] = empty( $_POST['recentdays'] ) ? '' : absint( $_POST['recentdays'] );
     202        $args['num_days']   = empty( $_REQUEST['days'] ) ? 7 : absint( $_REQUEST['days'] );
     203        $args['recentdays'] = empty( $_REQUEST['recentdays'] ) ? 7 : absint( $_REQUEST['recentdays'] );
    208204
    209205        $stats = $this->get_stats( $args );
    210206
    211         $date = strftime( '%Y-%m-%d', time() );
    212 
    213         $start_date = date( 'Y-m-d', strtotime( "-{$stats['num_days']} days", strtotime( $stats['date'] ) ) );
     207        $date = gmdate( 'Y-m-d' );
     208
     209        $start_date = gmdate( 'Y-m-d', strtotime( "-{$stats['num_days']} days", strtotime( $stats['date'] ) ) );
    214210        ?>
    215211
    216212        <div class="wrap stats-report">
    217213
    218         <h1><?php _e( 'Plugin Repository and SupportPress Stats Report', 'wporg-plugins' ); ?></h1>
    219 
    220         <form method="post">
     214        <h1><?php _e( 'Plugin Repository and email Stats Report', 'wporg-plugins' ); ?></h1>
     215
     216        <form method="get">
     217        <input type="hidden" name="action" value="<?php echo esc_attr( $_REQUEST['action'] ); ?>"/>
    221218        <table class="form-table"><tbody>
    222219        <tr><th scope="row"><label for="date"><?php _e( 'Date', 'wporg-plugins' ); ?></label></th><td>
     
    380377        </ul>
    381378
    382         <h3><?php _e( 'SupportPress Queue Stats', 'wporg-plugins' ); ?></h3>
     379        <h3><?php _e( 'Help Scout Queue Stats', 'wporg-plugins' ); ?></h3>
    383380
    384381        <ul style="font-family:Courier New;">
    385382            <li>
    386383            <?php
    387                 /* translators: %d: number of open tickets */
    388                 printf(
    389                     __( 'Total open tickets* : %d', 'wporg-plugins' ),
    390                     esc_html( $stats['supportpress_queue_total_open'] )
    391                 );
    392             ?>
    393             </li>
    394             <li>
    395             <?php
    396                 /* translators: 1: number of most recent days, 2: number of plugins with no activity */
    397                 printf(
    398                     __( ' &rarr; (with no activity in last %1$d days)** : %2$d', 'wporg-plugins' ),
    399                     esc_html( $stats['recentdays'] ),
    400                     esc_html( $stats['supportpress_queue_total_open_old'] )
    401                 );
    402             ?>
    403             </li>
    404             <li>
    405             <?php
    406                 /* translators: %d: number of most recent days */
    407                 printf(
    408                     __( 'Within defined %d day time window:', 'wporg-plugins' ),
    409                     esc_html( $stats['num_days'] )
    410                 );
    411                 ?>
    412                 <ul style="margin-left:20px;margin-top:0.5em;">
    413                     <li>
    414                     <?php
    415                         /* translators: %d: total number of plugins within defined time window */
    416                         printf(
    417                             __( 'Total : %d', 'wporg-plugins' ),
    418                             esc_html( $stats['supportpress_queue_interval_all'] )
    419                         );
    420                     ?>
    421                     </li>
    422                     <li>
    423                     <?php
    424                         /* translators: %d: number of closed plugins within defined time window */
    425                         printf(
    426                             __( 'Closed : %d', 'wporg-plugins' ),
    427                             esc_html( $stats['supportpress_queue_interval_closed'] )
    428                         );
    429                     ?>
    430                     </li>
    431                     <li>
    432                     <?php
    433                         /* translators: %d: number of open plugins within defined time window */
    434                         printf(
    435                             __( 'Open : %d', 'wporg-plugins' ),
    436                             esc_html( $stats['supportpress_queue_interval_open'] )
    437                         );
    438                     ?>
    439                     </li>
    440                 </ul>
     384                printf(
     385                    'Total Conversations: %d',
     386                    esc_html( $stats['helpscout_queue_total_conversations'] )
     387                );
     388            ?>
     389            </li>
     390            <li>
     391            <?php
     392                printf(
     393                    'New Conversations: %d',
     394                    esc_html( $stats['helpscout_queue_new_conversations'] )
     395                );
     396            ?>
     397            </li>
     398            <li>
     399            <?php
     400                printf(
     401                    'Customers: %d',
     402                    esc_html( $stats['helpscout_queue_customers'] )
     403                );
     404            ?>
     405            </li>
     406            <li>
     407            <?php
     408                printf(
     409                    'Conversations per Day: %d',
     410                    esc_html( $stats['helpscout_queue_conversations_per_day'] )
     411                );
     412            ?>
     413            </li>
     414            <li>
     415            <?php
     416                printf(
     417                    'Busiest Day: %s',
     418                    esc_html( $stats['helpscout_queue_busiest_day'] )
     419                );
     420            ?>
     421            </li>
     422            <li>
     423            <?php
     424                printf(
     425                    'Messages Received: %d',
     426                    esc_html( $stats['helpscout_queue_messages_received'] )
     427                );
     428            ?>
     429            </li>
     430            <li>
     431            <?php
     432                printf(
     433                    'Replies Sent: %d',
     434                    esc_html( $stats['helpscout_queue_replies_sent'] )
     435                );
     436            ?>
     437            </li>
     438            <li>
     439            <?php
     440                printf(
     441                    'Emails Created: %d',
     442                    esc_html( $stats['helpscout_queue_emails_created'] )
     443                );
     444            ?>
    441445            </li>
    442446        </ul>
Note: See TracChangeset for help on using the changeset viewer.