Making WordPress.org

Changeset 14905


Ignore:
Timestamp:
05/21/2026 03:08:51 AM (6 months ago)
Author:
dd32
Message:

API: Credits: Properly link to user nicenames.

The credits API "Groups" are keyed by user_login, not by user_nicename. This has meant that the links to profiles on credits.php was using the Login, which may include Spaces or other characters.

This updates the API such that it always outputs user_nicename for that field.

Cache sets are droped here as they added no value.

Props peterwilsoncc for noticing.
See #8253.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/api.wordpress.org/public_html/core/credits/wp-credits.php

    r14904 r14905  
    1111        public static $use_cache = true;
    1212        public static $set_cache = true;
    13         const cache_group = 'core-credits-api-2';
     13        const cache_group = 'core-credits-api-3';
    1414        const cache_life = 43200; // 12 hours
    1515
    … …  
    285285                        return $this->groups;
    286286
    287                 $groups = $this->groups();
    288                 $fetch_emails_from_user_cache = $fetch_emails_from_db = array();
     287                $groups        = $this->groups();
     288                $fetch_from_db = array();
    289289
    290290                foreach ( $groups as $group_slug => $group_data ) {
    … …  
    302302                                } elseif ( empty( $person[1] ) ) {
    303303                                        // array( 'Andrew Nacin' )
    304                                         $fetch_emails_from_user_cache[ $k ] = $group_slug;
     304                                        $fetch_from_db[ $k ] = $group_slug;
    305305                                } elseif ( $this->is_hashed( $person[1] ) ) {
    306306                                        // array( 'Andrew Nacin', 'gravatar hash' )
    … …  
    309309                                        // array( 'Andrew Nacin', 'Lead Developer' )
    310310                                        $new_data['title'] = $person[1];
    311                                         $fetch_emails_from_user_cache[ $k ] = $group_slug;
     311                                        $fetch_from_db[ $k ] = $group_slug;
    312312                                }
    313313
    … …  
    323323                }
    324324
    325                 if ( $fetch_emails_from_user_cache ) {
    326                         foreach ( $fetch_emails_from_user_cache as $username => $group ) {
    327                                 $user_id = wp_cache_get( $username, 'userlogins' );
    328                                 if ( $user_id ) {
    329                                         if ( $user_object = wp_cache_get( $user_id, 'users' ) ) {
    330                                                 $groups[ $group ]['data'][ $username ][1] = $this->hash( $user_object->user_email );
    331                                         } else {
    332                                                 $fetch_emails_from_db[ $username ] = $group;
    333                                         }
    334                                 } else {
    335                                         $fetch_emails_from_db[ $username ] = $group;
     325                if ( $fetch_from_db ) {
     326                        // Match keys to either user_login or user_nicename, case-insensitively,
     327                        // so version files can use any canonical form.
     328                        $in      = "'" . implode( "', '", array_map( array( $wpdb, '_real_escape' ), array_keys( $fetch_from_db ) ) ) . "'";
     329                        $fetched = $wpdb->get_results( "SELECT user_login, user_nicename, user_email FROM $wpdb->users WHERE user_login IN ($in) OR user_nicename IN ($in)" );
     330
     331                        $source_lookup = array();
     332                        foreach ( array_keys( $fetch_from_db ) as $src ) {
     333                                $source_lookup[ strtolower( $src ) ] = $src;
     334                        }
     335
     336                        $renames_by_group = array();
     337                        foreach ( $fetched as $row ) {
     338                                $src = $source_lookup[ strtolower( $row->user_login ) ]
     339                                        ?? $source_lookup[ strtolower( $row->user_nicename ) ]
     340                                        ?? null;
     341                                if ( ! $src ) {
     342                                        continue;
     343                                }
     344                                $group = $fetch_from_db[ $src ];
     345                                $groups[ $group ]['data'][ $src ][1] = $this->hash( $row->user_email );
     346                                if ( $row->user_nicename !== $src ) {
     347                                        $renames_by_group[ $group ][ $src ] = $row->user_nicename;
    336348                                }
    337349                        }
    338                         if ( $fetch_emails_from_db ) {
    339                                 // Match keys to either user_login or user_nicename, case-insensitively,
    340                                 // so version files can use any canonical form.
    341                                 $in = "'" . implode( "', '", array_map( array( $wpdb, '_real_escape' ), array_keys( $fetch_emails_from_db ) ) ) . "'";
    342                                 $fetched = $wpdb->get_results( "SELECT user_login, user_nicename, ID, user_email FROM $wpdb->users WHERE user_login IN ($in) OR user_nicename IN ($in)" );
    343 
    344                                 $source_lookup = array();
    345                                 foreach ( $fetch_emails_from_db as $src => $group_slug ) {
    346                                         $source_lookup[ strtolower( $src ) ] = $src;
     350
     351                        // Rekey entries so the array key and `username` field reflect user_nicename
     352                        // (the profiles.wordpress.org slug), not user_login, when they differ.
     353                        foreach ( $renames_by_group as $group => $renames ) {
     354                                $rekeyed = array();
     355                                foreach ( $groups[ $group ]['data'] as $key => $entry ) {
     356                                        $new_key  = $renames[ $key ] ?? $key;
     357                                        $entry[2] = $new_key;
     358                                        $rekeyed[ $new_key ] = $entry;
    347359                                }
    348 
    349                                 foreach ( $fetched as $row ) {
    350                                         $src = $source_lookup[ strtolower( $row->user_login ) ]
    351                                                 ?? $source_lookup[ strtolower( $row->user_nicename ) ]
    352                                                 ?? null;
    353                                         if ( ! $src ) {
    354                                                 continue;
    355                                         }
    356                                         $groups[ $fetch_emails_from_db[ $src ] ]['data'][ $src ][1] = $this->hash( $row->user_email );
    357                                         wp_cache_add( $src, $row->ID, 'userlogins' );
    358                                 }
     360                                $groups[ $group ]['data'] = $rekeyed;
    359361                        }
    360362                }
Note: See TracChangeset for help on using the changeset viewer.