Making WordPress.org

Changeset 6609


Ignore:
Timestamp:
02/12/2018 10:23:05 PM (7 years ago)
Author:
iandunn
Message:

WordCamp Helpers: Add helper to generate a Select2 dropdown of WordCamps.

Location:
sites/trunk/wordcamp.org/public_html/wp-content/mu-plugins
Files:
65 added
2 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordcamp.org/public_html/wp-content/mu-plugins/helpers-wcpt.php

    r6599 r6609  
    77 */
    88
     9
     10/**
     11 * Retrieve `wordcamp` posts and their metadata.
     12 *
     13 * @param array $args Optional. Extra arguments to pass to `get_posts()`.
     14 *
     15 * @return array
     16 */
     17function get_wordcamps( $args = array() ) {
     18    $args = wp_parse_args( $args, array(
     19        'post_type'   => WCPT_POST_TYPE_ID,
     20        'post_status' => 'any',
     21        'orderby'     => 'ID',
     22        'numberposts' => -1,
     23        'perm'        => 'readable',
     24    ) );
     25
     26    $wordcamps = get_posts( $args );
     27
     28    foreach ( $wordcamps as &$wordcamp ) {
     29        $wordcamp->meta = get_post_custom( $wordcamp->ID );
     30    }
     31
     32    return $wordcamps;
     33}
    934
    1035/**
     
    175200    return absint( $duration_days );
    176201}
     202
     203/**
     204 * Get a <select> dropdown of `wordcamp` posts with a select2 UI.
     205 *
     206 * The calling plugin is responsible for validating and processing the form, this just outputs a single field.
     207 *
     208 * @param string $name          The `name` attribute for the `select` element
     209 * @param array  $query_options Optional. Extra arguments to pass to `get_posts()`. Defaults to the values in `get_wordcamps()`.
     210 *
     211 * @return string The HTML for the <select> list.
     212 */
     213function get_wordcamp_dropdown( $name = 'wordcamp_id', $query_options = array() ) {
     214    $wordcamps = get_wordcamps( $query_options );
     215
     216    wp_enqueue_script( 'select2' );
     217    wp_enqueue_style(  'select2' );
     218
     219    ob_start();
     220
     221    ?>
     222
     223    <select name="<?php echo esc_attr( $name ); ?>" class="select2">
     224        <option value=""><?php _e( 'Select a WordCamp', 'wordcamporg' ); ?></option>
     225        <option value=""></option>
     226
     227        <?php foreach ( $wordcamps as $wordcamp ) : ?>
     228            <option value="<?php echo esc_attr( $wordcamp->ID ); ?>">
     229                <?php
     230
     231                echo esc_html( $wordcamp->post_title );
     232                if ( ! empty( $wordcamp->meta['Start Date (YYYY-mm-dd)'][0] ) ) {
     233                    echo ' ' . esc_html( date( 'Y', $wordcamp->meta['Start Date (YYYY-mm-dd)'][0] ) );
     234                }
     235
     236                ?>
     237            </option>
     238        <?php endforeach; ?>
     239    </select>
     240
     241    <script>
     242        jQuery( document ).ready( function() {
     243            jQuery( '.select2' ).select2();
     244        } );
     245    </script>
     246
     247    <?php
     248
     249    return ob_get_clean();
     250}
  • sites/trunk/wordcamp.org/public_html/wp-content/mu-plugins/wcorg-misc.php

    r6576 r6609  
    282282}
    283283add_action( 'login_form_lostpassword', 'wcorg_reset_passwords_at_wporg' );
     284
     285/**
     286 * Register scripts and styles.
     287 */
     288function wcorg_register_scripts() {
     289    /*
     290     * Select2 can be removed if/when it's bundled with Core, see #31696-core.
     291     * If the handle changes, we'll need to update any of our plugins that are using it.
     292     */
     293    wp_register_script(
     294        'select2',
     295        plugins_url( '/includes/select2/js/select2.min.js', __FILE__ ),
     296        array( 'jquery' ),
     297        '4.0.5',
     298        true
     299    );
     300
     301    wp_register_style(
     302        'select2',
     303        plugins_url( '/includes/select2/css/select2.min.css', __FILE__ ),
     304        array(),
     305        '4.0.5'
     306    );
     307}
     308add_action( 'wp_enqueue_scripts',    'wcorg_register_scripts' );
     309add_action( 'admin_enqueue_scripts', 'wcorg_register_scripts' );
Note: See TracChangeset for help on using the changeset viewer.