Making WordPress.org

Changeset 10926


Ignore:
Timestamp:
04/26/2021 11:09:28 PM (5 years ago)
Author:
coreymckrill
Message:

WordPress.org Learn: Sync with GitHub

​https://github.com/WordPress/learn/commit/5133da6d34c524f7b4685400c8359842153fbf17

Location:
sites/trunk/wordpress.org/public_html/wp-content
Files:
6 edited

Legend:

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

    r10873 r10926  
    33namespace WPOrg_Learn\Admin;
    44
     5use WP_Query;
     6use function WordPressdotorg\Locales\get_locale_name_from_code;
     7use function WPOrg_Learn\Post_Meta\get_available_workshop_locales;
     8
    59defined( 'WPINC' ) || die();
    610
    … …  
    913 */
    1014add_action( 'admin_notices', __NAMESPACE__ . '\show_term_translation_notice' );
     15add_filter( 'manage_wporg_workshop_posts_columns', __NAMESPACE__ . '\add_workshop_list_table_columns' );
     16add_action( 'manage_wporg_workshop_posts_custom_column', __NAMESPACE__ . '\render_workshop_list_table_columns', 10, 2 );
     17add_filter( 'manage_edit-wporg_workshop_sortable_columns', __NAMESPACE__ . '\add_workshop_list_table_sortable_columns' );
     18add_action( 'restrict_manage_posts', __NAMESPACE__ . '\add_workshop_list_table_filters', 10, 2 );
     19add_action( 'pre_get_posts', __NAMESPACE__ . '\handle_workshop_list_table_filters' );
    1120
    1221/**
    … …  
    5665        <?php
    5766}
     67
     68/**
     69 * Add additional columns to the post list table for workshops.
     70 *
     71 * @param array $columns
     72 *
     73 * @return array
     74 */
     75function add_workshop_list_table_columns( $columns ) {
     76        $columns = array_slice( $columns, 0, -2, true )
     77                                + array( 'video_language' => __( 'Language', 'wporg-learn' ) )
     78                                + array( 'video_caption_language' => __( 'Captions', 'wporg-learn' ) )
     79                                + array_slice( $columns, -2, 2, true );
     80
     81        return $columns;
     82}
     83
     84/**
     85 * Render the cell contents for the additional columns in the post list table for workshops.
     86 *
     87 * @param string $column_name
     88 * @param int    $post_id
     89 *
     90 * @return void
     91 */
     92function render_workshop_list_table_columns( $column_name, $post_id ) {
     93        $post = get_post( $post_id );
     94
     95        switch ( $column_name ) {
     96                case 'video_language':
     97                        printf(
     98                                '%s [%s]',
     99                                esc_html( get_locale_name_from_code( $post->video_language, 'english' ) ),
     100                                esc_html( $post->video_language )
     101                        );
     102                        break;
     103                case 'video_caption_language':
     104                        $captions = get_post_meta( $post->ID, 'video_caption_language' );
     105
     106                        echo esc_html( implode(
     107                                ', ',
     108                                array_map(
     109                                        function( $caption_lang ) {
     110                                                return get_locale_name_from_code( $caption_lang, 'english' );
     111                                        },
     112                                        $captions
     113                                )
     114                        ) );
     115                        break;
     116        }
     117}
     118
     119/**
     120 * Make additional columns sortable.
     121 *
     122 * @param array $sortable_columns
     123 *
     124 * @return array
     125 */
     126function add_workshop_list_table_sortable_columns( $sortable_columns ) {
     127        $sortable_columns['video_language'] = 'video_language';
     128
     129        return $sortable_columns;
     130}
     131
     132/**
     133 * Add filtering controls for the workshops list table.
     134 *
     135 * @param string $post_type
     136 * @param string $which
     137 *
     138 * @return void
     139 */
     140function add_workshop_list_table_filters( $post_type, $which ) {
     141        if ( 'wporg_workshop' !== $post_type || 'top' !== $which ) {
     142                return;
     143        }
     144
     145        $available_locales = get_available_workshop_locales( 'video_language', 'english', false );
     146        $language          = filter_input( INPUT_GET, 'language', FILTER_SANITIZE_STRING );
     147
     148        ?>
     149        <label for="filter-by-language" class="screen-reader-text">
     150                <?php esc_html_e( 'Filter by language', 'wporg-learn' ); ?>
     151        </label>
     152        <select id="filter-by-language" name="language">
     153                <option value=""<?php selected( ! $language ); ?>><?php esc_html_e( 'Any language', 'wporg-learn' ); ?></option>
     154                <?php foreach ( $available_locales as $code => $name ) : ?>
     155                        <option value="<?php echo esc_attr( $code ); ?>"<?php selected( $code, $language ); ?>>
     156                                <?php
     157                                printf(
     158                                        '%s [%s]',
     159                                        esc_html( $name ),
     160                                        esc_html( $code )
     161                                );
     162                                ?>
     163                        </option>
     164                <?php endforeach; ?>
     165        </select>
     166        <?php
     167}
     168
     169/**
     170 * Alter the query to include workshop list table filters.
     171 *
     172 * @param WP_Query $query
     173 *
     174 * @return void
     175 */
     176function handle_workshop_list_table_filters( WP_Query $query ) {
     177        if ( ! is_admin() ) {
     178                return;
     179        }
     180
     181        $current_screen = get_current_screen();
     182
     183        if ( 'edit-wporg_workshop' === $current_screen->id ) {
     184                $language = filter_input( INPUT_GET, 'language', FILTER_SANITIZE_STRING );
     185
     186                if ( $language ) {
     187                        $meta_query = $query->get( 'meta_query', array() );
     188
     189                        if ( ! empty( $meta_query ) ) {
     190                                $meta_query = array(
     191                                        'relation' => 'AND',
     192                                        $meta_query,
     193                                );
     194                        }
     195
     196                        $meta_query[] = array(
     197                                'key'   => 'video_language',
     198                                'value' => $language,
     199                        );
     200
     201                        $query->set( 'meta_query', $meta_query );
     202                }
     203
     204                if ( 'video_language' === $query->get( 'orderby' ) ) {
     205                        $query->set( 'meta_key', 'video_language' );
     206                        $query->set( 'orderby', 'meta_value' );
     207                }
     208        }
     209}
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-learn/inc/post-meta.php

    r10866 r10926  
    218218 * Get a list of locales that are associated with at least one workshop.
    219219 *
     220 * Optionally only published workshops.
     221 *
    220222 * @param string $meta_key
    221223 * @param string $label_language
     224 * @param bool   $published_only
    222225 *
    223226 * @return array
    224227 */
    225 function get_available_workshop_locales( $meta_key, $label_language = 'english' ) {
     228function get_available_workshop_locales( $meta_key, $label_language = 'english', $published_only = true ) {
    226229        global $wpdb;
    227230
     231        $and_post_status = '';
     232        if ( $published_only ) {
     233                $and_post_status = "AND posts.post_status = 'publish'";
     234        }
     235
    228236        $results = $wpdb->get_col( $wpdb->prepare(
     237                // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- $and_post_status contains no user input.
    229238                "
    230                         SELECT DISTINCT meta_value
    231                         FROM $wpdb->postmeta
    232                         WHERE meta_key = %s
    233                         ORDER BY meta_value ASC
     239                        SELECT DISTINCT postmeta.meta_value
     240                        FROM {$wpdb->postmeta} postmeta
     241                                JOIN {$wpdb->posts} posts ON posts.ID = postmeta.post_id $and_post_status
     242                        WHERE postmeta.meta_key = %s
    234243                ",
    235244                $meta_key
     245                // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
    236246        ) );
    237247
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-learn/inc/post-type.php

    r10866 r10926  
    1111 */
    1212add_action( 'init', __NAMESPACE__ . '\register' );
    13 add_filter( 'manage_wporg_workshop_posts_columns', __NAMESPACE__ . '\add_workshop_list_table_columns' );
    14 add_action( 'manage_wporg_workshop_posts_custom_column', __NAMESPACE__ . '\render_workshop_list_table_columns', 10, 2 );
    1513add_filter( 'jetpack_copy_post_post_types', __NAMESPACE__ . '\jetpack_copy_post_post_types' );
    1614add_filter( 'jetpack_sitemap_post_types', __NAMESPACE__ . '\jetpack_sitemap_post_types' );
    … …  
    198196
    199197/**
    200  * Add additional columns to the post list table for workshops.
    201  *
    202  * @param array $columns
    203  *
    204  * @return array
    205  */
    206 function add_workshop_list_table_columns( $columns ) {
    207         $columns = array_slice( $columns, 0, -2, true )
    208                                 + array( 'video_language' => __( 'Language', 'wporg-learn' ) )
    209                                 + array( 'video_caption_language' => __( 'Captions', 'wporg-learn' ) )
    210                                 + array_slice( $columns, -2, 2, true );
    211 
    212         return $columns;
    213 }
    214 
    215 /**
    216  * Render the cell contents for the additional columns in the post list table for workshops.
    217  *
    218  * @param string $column_name
    219  * @param int    $post_id
    220  *
    221  * @return void
    222  */
    223 function render_workshop_list_table_columns( $column_name, $post_id ) {
    224         $post = get_post( $post_id );
    225 
    226         switch ( $column_name ) {
    227                 case 'video_language':
    228                         echo esc_html( get_locale_name_from_code( $post->video_language, 'english' ) );
    229                         break;
    230                 case 'video_caption_language':
    231                         $captions = get_post_meta( $post->ID, 'video_caption_language' );
    232 
    233                         echo esc_html( implode(
    234                                 ', ',
    235                                 array_map(
    236                                         function( $caption_lang ) {
    237                                                 return get_locale_name_from_code( $caption_lang, 'english' );
    238                                         },
    239                                         $captions
    240                                 )
    241                         ) );
    242                         break;
    243         }
    244 }
    245 
    246 /**
    247198 * Enable Jetpack's Copy Post module for more post types.
    248199 *
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-learn/views/form-workshop-application.php

    r10378 r10926  
    245245                                        <?php foreach ( get_locales_with_native_names() as $code => $name ) : ?>
    246246                                                <option value="<?php echo esc_attr( $code ); ?>" <?php selected( $code, $form['language'] ); ?>>
    247                                                         <?php echo esc_html( $name ); ?>
     247                                                        <?php
     248                                                        printf(
     249                                                                '%s [%s]',
     250                                                                esc_html( $name ),
     251                                                                esc_html( $code ),
     252                                                        );
     253                                                        ?>
    248254                                                </option>
    249255                                        <?php endforeach; ?>
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-learn-2020/functions.php

    r10710 r10926  
    290290}
    291291add_action( 'pre_get_posts', 'wporg_archive_modify_query' );
     292
     293/**
     294 * Modify the workshop post type archive query to prioritize workshops in the user's locale.
     295 *
     296 * In order to show all workshops, but with the ones that are presented/captioned in the user's locale shown first, we
     297 * need to modify the posts query in ways that can't be done through the WP_Query or WP_Meta_Query APIs. Instead, here,
     298 * we're filtering the individual clauses of the query to add the pieces we need.
     299 *
     300 * Examples, slightly truncated for simplicity:
     301 *
     302 * Before:
     303 * SELECT SQL_CALC_FOUND_ROWS wp_posts.ID
     304 * FROM wp_posts
     305 * WHERE 1=1
     306 * AND wp_posts.post_type = 'wporg_workshop'
     307 * ORDER BY wp_posts.post_date DESC
     308 *
     309 * After:
     310 * SELECT SQL_CALC_FOUND_ROWS wp_posts.*,
     311 *   MAX( IF( pmeta.meta_key = 'video_language' AND pmeta.meta_value LIKE 'art_%', 1, 0 ) ) AS has_language,
     312 *   MAX( IF( pmeta.meta_key = 'video_caption_language' AND pmeta.meta_value LIKE 'art_%', 1, 0 ) ) AS has_caption
     313 * FROM wp_posts
     314 * INNER JOIN wp_postmeta pmeta ON ( wp_posts.ID = pmeta.post_id )
     315 * WHERE 1=1
     316 * AND wp_posts.post_type = 'wporg_workshop'
     317 * GROUP BY wp_posts.ID
     318 * ORDER BY has_language DESC, has_caption DESC, wp_posts.post_date DESC
     319 *
     320 * @param array    $clauses
     321 * @param WP_Query $query
     322 *
     323 * @return array
     324 */
     325function wporg_archive_query_prioritize_locale( $clauses, $query ) {
     326        if ( ! $query->is_post_type_archive( 'wporg_workshop' ) || is_admin() ) {
     327                return $clauses;
     328        }
     329
     330        global $wpdb;
     331
     332        $locale      = get_locale();
     333        $locale_root = preg_replace( '#^([a-z]{2,3}_?)[a-zA-Z_-]*#', '$1', $locale, -1, $count );
     334
     335        if ( $count ) {
     336                /**
     337                 * $clauses['fields'] contains the SELECT part of the query.
     338                 *
     339                 * The extra fields clauses are calculated fields that will contain a `1` if the workshop post row has a postmeta
     340                 * value that matches the locale root. The MAX() and the groupby clause below ensure that all the rows for a
     341                 * given workshop are consolidated into one with the highest value in the calculated column. Without the
     342                 * grouping, there would be a separate row for each postmeta value for each workshop post.
     343                 */
     344                $clauses['fields']  .= ",
     345                        MAX( IF( pmeta.meta_key = 'video_language' AND pmeta.meta_value LIKE '{$locale_root}%', 1, 0 ) ) AS has_language
     346                ";
     347                $clauses['fields']  .= ",
     348                        MAX( IF( pmeta.meta_key = 'video_caption_language' AND pmeta.meta_value LIKE '{$locale_root}%', 1, 0 ) ) AS has_caption
     349                ";
     350                $clauses['join']    .= " INNER JOIN {$wpdb->postmeta} pmeta ON ( {$wpdb->posts}.ID = pmeta.post_id )";
     351                // This orderby clause ensures that the workshops are sorted by the values in the calculated columns first.
     352                $clauses['orderby'] = 'has_language DESC, has_caption DESC, ' . $clauses['orderby'];
     353
     354                if ( false === strpos( $clauses['groupby'], "{$wpdb->posts}.ID" ) ) {
     355                        $clauses['groupby'] = "{$wpdb->posts}.ID";
     356                }
     357        }
     358
     359        return $clauses;
     360}
     361add_filter( 'posts_clauses', 'wporg_archive_query_prioritize_locale', 10, 2 );
    292362
    293363/**
    … …  
    344414        if ( is_array( $filters ) ) {
    345415                $filters = array_filter( $filters );
     416
     417                // If both language and captions filters are set, we assume an "OR" relationship.
     418                if ( isset( $filters['captions'], $filters['language'] ) ) {
     419                        $meta_query[] = array(
     420                                'relation' => 'OR',
     421                                array(
     422                                        'key'   => $entity_map['captions'],
     423                                        'value' => $filters['captions'],
     424                                ),
     425                                array(
     426                                        'key'   => $entity_map['language'],
     427                                        'value' => $filters['language'],
     428                                ),
     429                        );
     430
     431                        unset( $filters['captions'], $filters['language'] );
     432                }
    346433
    347434                foreach ( $filters as $filter_name => $filter_value ) {
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-learn-2020/template-parts/component-workshop-filters.php

    r10511 r10926  
    5757                                                                <?php selected( $item_value, filter_input( INPUT_GET, $bucket['name'] ) ); ?>
    5858                                                        >
    59                                                                 <?php echo esc_html( $item_label ); ?>
     59                                                                <?php if ( in_array( $bucket['name'], array( 'language', 'captions' ) ) ) :
     60                                                                        printf(
     61                                                                                '%s [%s]',
     62                                                                                esc_html( $item_label ),
     63                                                                                esc_html( $item_value ),
     64                                                                        );
     65                                                                else :
     66                                                                        echo esc_html( $item_label );
     67                                                                endif; ?>
    6068                                                        </option>
    6169                                                <?php endforeach; ?>
Note: See TracChangeset for help on using the changeset viewer.