Making WordPress.org

Changeset 14426


Ignore:
Timestamp:
04/16/2025 08:50:08 PM (7 months ago)
Author:
coffee2code
Message:

Photo Directory, Admin: Reimplement appending author column submission stats as a custom 'Author' column.

  • Replaces existing 'Author' column.
  • Renames add_published_photos_count_to_author to get_author_submission_stats().

Props mujuonly, nilovelez, otto42, coffee2code.

File:
1 edited

Legend:

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

    r14387 r14426  
    1010class Admin {
    1111
     12    const COL_NAME_AUTHOR = 'wporg-photo-author';
    1213    const COL_NAME_FLAG  = 'wporg-flags';
    1314    const COL_NAME_PHOTO = 'wporg-photo';
     
    2728        add_action( 'load-edit.php',                           [ __CLASS__, 'add_admin_css' ] );
    2829        add_action( 'load-post.php',                           [ __CLASS__, 'add_admin_css' ] );
     30        add_filter( "manage_{$post_type}_posts_columns",       [ __CLASS__, 'add_author_column' ] );
     31        add_action( "manage_{$post_type}_posts_custom_column", [ __CLASS__, 'handle_author_column_data' ], 10, 2 );
    2932        add_filter( "manage_{$post_type}_posts_columns",       [ __CLASS__, 'add_photo_column' ] );
    3033        add_action( "manage_{$post_type}_posts_custom_column", [ __CLASS__, 'handle_photo_column_data' ], 10, 2 );
     
    3336        add_filter( "manage_edit-{$post_type}_columns",        [ __CLASS__, 'remove_columns_from_pending_photos' ] );
    3437        add_filter( 'post_row_actions',                        [ __CLASS__, 'add_post_action_photo_links' ], 10, 2 );
    35         add_filter( 'the_author',                              [ __CLASS__, 'add_published_photos_count_to_author' ] );
    3638        add_filter( 'use_block_editor_for_post_type',          [ __CLASS__, 'disable_block_editor' ], 10, 2 );
    3739        add_action( 'admin_notices',                           [ __CLASS__, 'add_notice_to_photo_media_if_pending' ] );
     
    325327            ( empty( $_GET['post_status'] ) || in_array( $_GET['post_status'], $post_statuses ) )
    326328        );
     329    }
     330
     331    /**
     332     * Replaces default 'Author' column with a custom 'Author' column that shows
     333     * typical author column data as well as additional data.
     334     *
     335     * As of WP 6.8, it no longer easy to modify the existing 'Author' column to
     336     * add more data, so this adds a custom column to replace the 'Author' column.
     337     *
     338     * @param  array $posts_columns Array of post column titles.
     339     * @return array The $posts_columns array with the photo author column added.
     340     */
     341    public static function add_author_column( $posts_columns ) {
     342        if ( ! self::should_include_photo_column() ) {
     343            return $posts_columns;
     344        }
     345
     346        // Position after existing 'Author' column.
     347        $pos = array_search( 'author', array_keys( $posts_columns ) );
     348
     349        if ( ! $pos ) {
     350            // Else, position the column after the 'Title' column (where 'Author' is normally located).
     351            $pos = array_search( 'title', array_keys( $posts_columns ) );
     352        }
     353
     354        if ( $pos ) {
     355            $pos++;
     356            $posts_columns = array_slice( $posts_columns, 0, $pos, true )
     357                + [ self::COL_NAME_AUTHOR => __( 'Author', 'wporg-photos' ) ]
     358                + array_slice( $posts_columns, $pos, count( $posts_columns ) - 1, true );
     359        } else {
     360            $posts_columns[ self::COL_NAME_AUTHOR ] = __( 'Author', 'wporg-photos' );
     361        }
     362
     363        // Remove the existing 'Author' column.
     364        unset( $posts_columns['author'] );
     365
     366        return $posts_columns;
     367    }
     368
     369    /**
     370     * Outputs the photo author column content for the post.
     371     *
     372     * @param string $column_name The name of the column.
     373     * @param int    $post_id     The id of the post being displayed.
     374     */
     375    public static function handle_author_column_data( $column_name, $post_id ) {
     376        if ( self::COL_NAME_AUTHOR !== $column_name ) {
     377            return;
     378        }
     379
     380        $post = get_post( $post_id );
     381
     382        // Get the same 'Author' column content as generated by core.
     383        $list_table = _get_list_table( 'WP_Posts_List_Table' );
     384        $core_html = $list_table->column_author( $post );
     385
     386        /**
     387         * Fires before any photo author column content is output.
     388         *
     389         * @param WP_Post The post object.
     390         */
     391        do_action( 'photo_author_column_data_start', $post );
     392
     393        // Output core's default author column data for this post.
     394        echo $core_html;
     395
     396        /**
     397         * Fires after the default author column content is output.
     398         *
     399         * @param WP_Post The post object.
     400         */
     401        do_action( 'photo_author_column_data_after_author', $post );
     402
     403        // Output author submission counts.
     404        echo self::get_author_submission_stats( (int) get_post_field( 'post_author', $post_id ) );
     405
     406        /**
     407         * Fires at the end of the photo author column content.
     408         *
     409         * @param WP_Post The post object.
     410         */
     411        do_action( 'photo_author_column_data_end', $post );
    327412    }
    328413
     
    808893
    809894    /**
    810      * Appends the count of the published photos to author names in photo post
    811      * listings.
    812      *
    813      * @param string $display_name The author's display name.
     895     * Returns hyperlinked statistics on the author's submission counts (published,
     896     * pending, rejected, etc) intended to be shown to moderators.
     897     *
     898     * @param int $author_id Author ID.
    814899     * @return string
    815900     */
    816     public static function add_published_photos_count_to_author( $display_name ) {
    817         global $authordata;
    818 
    819         if ( ! is_admin() || ! self::should_include_photo_column( true ) ) {
    820             return $display_name;
    821         }
    822 
    823         // Close link to contributor's listing of photos.
    824         $display_name .= '</a>';
    825 
     901    public static function get_author_submission_stats( $author_id ) {
    826902        $post_type     = Registrations::get_post_type();
    827903        $reject_status = Rejection::get_post_status();
     904        $stats_display = '';
    828905
    829906        // Show number of approved photos.
     
    831908            'post_type'   => $post_type,
    832909            'post_status' => 'publish',
    833             'author'      => $authordata->ID,
     910            'author'      => $author_id,
    834911        ], 'edit.php' );
    835         $display_name .= '<div class="user-approved-count">'
     912        $stats_display .= '<div class="user-approved-count">'
    836913        . sprintf(
    837914            __( 'Approved: <strong>%s</strong>', 'wporg-photos' ),
     
    846923                'post_type'   => $post_type,
    847924                'post_status' => 'publish',
    848                 'author'      => $authordata->ID,
     925                'author'      => $author_id,
    849926            ], 'edit.php' );
    850             $display_name .= '<div class="user-approved-today-count">'
     927            $stats_display .= '<div class="user-approved-today-count">'
    851928                . sprintf(
    852929                    __( '&#x21AA; (today): %s', 'wporg-photos' ),
     
    862939                'post_type'   => $post_type,
    863940                'post_status' => 'pending',
    864                 'author'      => $authordata->ID,
     941                'author'      => $author_id,
    865942            ], 'edit.php' );
    866943
    867             $display_name .= '<div class="user-pending-count">'
     944            $stats_display .= '<div class="user-pending-count">'
    868945                . sprintf(
    869946                    __( 'Pending: <strong>%s</strong>', 'wporg-photos' ),
     
    874951
    875952        // Show number of rejected photos.
    876         $rejection_count = User::count_rejected_photos( $authordata->ID );
     953        $rejection_count = User::count_rejected_photos( $author_id );
    877954        if ( $rejection_count ) {
    878955            $rejected_link = add_query_arg( [
    879956                'post_type'   => $post_type,
    880957                'post_status' => $reject_status,
    881                 'author'      => $authordata->ID,
     958                'author'      => $author_id,
    882959            ], 'edit.php' );
    883             $display_name .= '<div class="user-rejected-count">'
     960            $stats_display .= '<div class="user-rejected-count">'
    884961                . sprintf(
    885962                    /* translators: %s: Count of user rejections linked to listing of their rejections. */
     
    896973                'post_type'   => $post_type,
    897974                'post_status' => $reject_status,
    898                 'author'      => $authordata->ID,
     975                'author'      => $author_id,
    899976            ], 'edit.php' );
    900             $display_name .= '<div class="user-rejected-today-count">'
     977            $stats_display .= '<div class="user-rejected-today-count">'
    901978                . sprintf(
    902979                    __( '&#x21AA; (today): %s', 'wporg-photos' ),
     
    906983        }
    907984
    908         // Prevent unbalanced tag.
    909         $display_name .= '<a>';
    910 
    911         return $display_name;
     985        return $stats_display;
    912986    }
    913987
     
    13161390     */
    13171391    public static function add_contributor_ip_column( $posts_columns ) {
    1318         $pos = array_search( 'author', array_keys( $posts_columns ) );
     1392        $pos = array_search( self::COL_NAME_AUTHOR, array_keys( $posts_columns ) );
    13191393
    13201394        if ( $pos ) {
     
    13221396            $posts_columns = array_slice( $posts_columns, 0, $pos, true )
    13231397                + [ self::COL_NAME_CONTRIBUTOR_IP => __( 'Contributor IP', 'wporg-photos' ) ]
    1324                 + array_slice( $posts_columns, 3, count( $posts_columns ) - 1, true );
     1398                + array_slice( $posts_columns, $pos, count( $posts_columns ) - 1, true );
    13251399        } else {
    13261400            $posts_columns[ self::COL_NAME_CONTRIBUTOR_IP ] = __( 'Contributor IP', 'wporg-photos' );
Note: See TracChangeset for help on using the changeset viewer.