Making WordPress.org


Ignore:
Timestamp:
10/09/2024 10:22:13 PM (2 years ago)
Author:
coffee2code
Message:

Photo Directory, Rejection: Create admin dashboard widget for simple rejection stats.

  • Register and display the "Rejection Stats" admin dashboard widget
  • Create count_rejections_per_reason() to return a count of rejections for each reason type.
  • Change get_rejection_reasons() to exclude the approval item as a rejection reason by default. It only applies to the dropdown.

Fixes #7796.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/photo-directory/inc/rejection.php

    r14056 r14103  
    169169                // Use JS to inject rejected post status into post submit box.
    170170                add_action( 'admin_footer',                            [ __CLASS__, 'output_js_to_modify_post_status_in_submitbox_dropdown' ] );
     171
     172                // Register dashboard widget.
     173                add_action( 'wp_dashboard_setup',                      [ __CLASS__, 'dashboard_setup' ] );
    171174        }
    172175
    … …  
    311314         *                       is the specific attribute of the reason to return.
    312315         *                       Empty string returns all data for reason. Default ''.
     316         * @param bool   $include_approval Optional. Should the approval entry (which is
     317         *                       technically not a rejection) be included? Default false.
    313318         * @return array|string
    314319         */
    315         public static function get_rejection_reasons( $reason = '', $field = '' ) {
     320        public static function get_rejection_reasons( $reason = '', $field = '', $include_approval = false ) {
    316321                // Return all reasons if one wasn't specified.
    317322                if ( ! $reason ) {
    318323                        $reasons = self::$rejection_reasons;
     324                        if ( ! $include_approval ) {
     325                                unset( $reasons[''] );
     326                        }
    319327                        uasort( $reasons, function( $a, $b ) {
    320328                                return strcmp( $a['label'], $b['label'] );
    … …  
    367375
    368376                return $rejection_reason;
     377        }
     378
     379        /**
     380         * Returns a count of rejections for each rejection reason.
     381         *
     382         * @return array Associate array of rejection reason keys and their respective rejection counts.
     383         */
     384        public static function count_rejections_per_reason() {
     385                global $wpdb;
     386
     387                $reasons = self::get_rejection_reasons();
     388                $reasons_keys = array_keys( $reasons );
     389
     390                $results = $wpdb->get_results( $wpdb->prepare(
     391                        "SELECT meta_value AS rejection_reason, COUNT(*) AS count FROM $wpdb->postmeta WHERE meta_key = %s GROUP BY meta_value",
     392                        'rejected_reason'
     393                ) );
     394
     395                $return = [];
     396                foreach ( $results as $row ) {
     397                        $return[ $row->rejection_reason ] = $row->count;
     398                }
     399
     400                return $return;
    369401        }
    370402
    … …  
    9821014                        disabled( true, $is_disabled, false )
    9831015                );
    984                 foreach ( self::get_rejection_reasons() as $reason => $args ) {
     1016                foreach ( self::get_rejection_reasons( '', '', true ) as $reason => $args ) {
    9851017                        printf(
    9861018                                '<option value="%s"%s>%s</option>' . "\n",
    … …  
    13041336        }
    13051337
     1338        /**
     1339         * Registers the admin dashboard.
     1340         */
     1341        public static function dashboard_setup() {
     1342                if ( current_user_can( 'edit_photos' ) ) {
     1343                        wp_add_dashboard_widget(
     1344                                'dashboard_photo_rejections',
     1345                                __( 'Rejection Stats', 'wporg-photos' ),
     1346                                [ __CLASS__, 'dashboard_photo_rejections' ],
     1347                                null,
     1348                                null,
     1349                                'column3'
     1350                        );
     1351                }
     1352        }
     1353
     1354        /**
     1355         * Outputs the photo rejection stats dashboard widget.
     1356         */
     1357        public static function dashboard_photo_rejections() {
     1358                echo '<div class="main">';
     1359
     1360                // Get list of all rejection types.
     1361                $rejection_reasons = self::get_rejection_reasons();
     1362                ksort( $rejection_reasons );
     1363                $rejection_reasons_counts = self::count_rejections_per_reason();
     1364
     1365                // Omit submission errors since they aren't true rejections.
     1366                unset( $rejection_reasons['submission-error'] );
     1367                unset( $rejection_reasons_counts['submission-error'] );
     1368
     1369                echo '<table id="dashboard-photo-rejection-stats" class="wp-list-table widefat fixed striped table-view-list">';
     1370                echo '<thead><tr>';
     1371                echo '<th>' . __( 'Rejection reason', 'wporg-photos' ) . '</th>';
     1372                echo '<th class="col-num col-num-rejected" title="' . esc_attr__( 'Number of photos rejected', 'wporg-photos' ) . '"><span class="dashicons dashicons-thumbs-down"></span></th>';
     1373                echo '<th class="col-num col-percent-rejected" title="' . esc_attr( 'Percentage of overall rejections', 'wporg-photos' ) . '">%</th>';
     1374                echo '</tr></thead>';
     1375                echo '<tbody>';
     1376
     1377                $total_rejections = array_sum( $rejection_reasons_counts );
     1378
     1379                foreach ( $rejection_reasons as $reason => $data ) {
     1380                        $data['count'] = $rejection_reasons_counts[ $reason ];
     1381
     1382                        echo '<tr>';
     1383                        echo '<td title="' . esc_attr( $data['label'] ) . '">' . esc_html( $reason ) . '</td>';
     1384                        echo '<td>' . number_format_i18n( $data['count'] ) . '</td>';
     1385                        echo '<td>' . round( ( $data['count'] / $total_rejections ) * 100, 2 ) . '%</td>';
     1386                        echo "</tr>\n";
     1387                }
     1388
     1389                echo '<tr class="row-sum"><td>' . __( 'Total', 'wporg-photos' ) . '</td><td>' . number_format_i18n( $total_rejections ) . '</td><td>100%</td></tr>';
     1390                echo '</tbody></table>';
     1391                echo '</div>';
     1392        }
     1393
    13061394}
    13071395
Note: See TracChangeset for help on using the changeset viewer.