Making WordPress.org


Ignore:
Timestamp:
09/16/2020 10:07:05 PM (5 years ago)
Author:
coreymckrill
Message:

WordPress.org Learn: Sync with GitHub

https://github.com/WordPress/learn/compare/41d65294a2e67cf35a5b799d179f2e6f53b58b48...fb8968a2cbd50fc4c1a8764586731e903577d3f3

File:
1 edited

Legend:

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

    r10237 r10272  
    55use DateTime, DateInterval;
    66use WP_Post;
     7use function WordPressdotorg\Locales\{ get_locales_with_english_names };
    78
    89defined( 'WPINC' ) || die();
     
    5960            'type'              => 'string',
    6061            'single'            => true,
    61             'sanitize_callback' => '', // todo
     62            'default'           => 'en_US',
     63            'sanitize_callback' => __NAMESPACE__ . '\sanitize_locale',
    6264            'show_in_rest'      => true,
    6365        )
     
    7173            'type'              => 'string',
    7274            'single'            => false,
    73             'sanitize_callback' => '', // todo
    74             'show_in_rest'      => true,
    75         )
    76     );
     75            'sanitize_callback' => __NAMESPACE__ . '\sanitize_locale',
     76            'show_in_rest'      => true,
     77        )
     78    );
     79}
     80
     81/**
     82 * Sanitize a locale value.
     83 *
     84 * @param string $meta_value
     85 * @param string $meta_key
     86 * @param string $object_type
     87 * @param string $object_subtype
     88 *
     89 * @return string
     90 */
     91function sanitize_locale( $meta_value, $meta_key, $object_type, $object_subtype ) {
     92    if ( 'wporg_workshop' !== $object_subtype ) {
     93        return $meta_value;
     94    }
     95
     96    $meta_value = trim( $meta_value );
     97    $locales = array_keys( get_locales_with_english_names() );
     98
     99    if ( ! in_array( $meta_value, $locales, true ) ) {
     100        return '';
     101    }
     102
     103    return $meta_value;
    77104}
    78105
     
    127154
    128155/**
     156 * Get a list of locales that are associated with at least one workshop.
     157 *
     158 * @param string $meta_key
     159 * @param string $label_language
     160 *
     161 * @return array
     162 */
     163function get_available_workshop_locales( $meta_key, $label_language = 'english' ) {
     164    global $wpdb;
     165
     166    $results = $wpdb->get_col( $wpdb->prepare(
     167        "
     168            SELECT DISTINCT meta_value
     169            FROM $wpdb->postmeta
     170            WHERE meta_key = %s
     171            ORDER BY meta_value ASC
     172        ",
     173        $meta_key
     174    ) );
     175
     176    if ( empty( $results ) ) {
     177        return array();
     178    }
     179
     180    $available_locales = array_fill_keys( $results, '' );
     181
     182    $locale_fn = "\WordPressdotorg\Locales\get_locales_with_{$label_language}_names";
     183    $locales   = $locale_fn();
     184
     185    return array_intersect_key( $locales, $available_locales );
     186}
     187
     188/**
    129189 * Add meta boxes to the Edit Workshop screen.
    130190 *
     
    156216function render_metabox_workshop_details( WP_Post $post ) {
    157217    $duration_interval = get_workshop_duration( $post, 'interval' );
     218    $locales           = get_locales_with_english_names();
    158219    $captions          = get_post_meta( $post->ID, 'video_caption_language' ) ?: array();
    159220
     
    199260    update_post_meta( $post_id, 'video_language', $video_language );
    200261
    201     $video_caption_language = filter_input( INPUT_POST, 'video-caption-language' );
    202     $captions               = array_map( 'trim', explode( ',', $video_caption_language ) );
    203     delete_post_meta( $post_id, 'video_caption_language' );
    204     foreach ( $captions as $caption ) {
    205         add_post_meta( $post_id, 'video_caption_language', $caption );
    206     }
    207 }
     262    $captions = filter_input( INPUT_POST, 'video-caption-language', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY );
     263    if ( is_array( $captions ) ) {
     264        delete_post_meta( $post_id, 'video_caption_language' );
     265        foreach ( $captions as $caption ) {
     266            add_post_meta( $post_id, 'video_caption_language', $caption );
     267        }
     268    }
     269}
Note: See TracChangeset for help on using the changeset viewer.