Making WordPress.org

Changeset 11711


Ignore:
Timestamp:
03/28/2022 09:00:52 PM (3 years ago)
Author:
coreymckrill
Message:

Add optional attributes to the wpcredits shortcode

Adds class and separator attributes to the shortcode callback function so that
the string of props names can better be styled to match the new design for the
WordPress.org News site.

The defaults for these attributes are set so that all existing uses of the
shortcode on the News site will be able to match the design without having to go
back and edit a lot of posts.

File:
1 edited

Legend:

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

    r11344 r11711  
    55add_shortcode( 'wpcredits', 'wporg_wordpress_credits_shortcode' );
    66
    7 function wporg_wordpress_credits_shortcode( $attrs, $content = null ) {
     7/**
     8 * Shortcode callback to render a list of props names, given a WP version.
     9 *
     10 * Example: [wpcredits 5.9 separator="bullet"]
     11 *
     12 * @param array $attrs
     13 *     @type string            Required. Nameless parameter specifying the WP version. Must be the first parameter.
     14 *     @type string $class     Optional. Space-separated list of class names to add to the container element.
     15 *     @type string $exclude   Optional. Comma-separated list of props names to exclude.
     16 *     @type string $separator Optional. Style of separator to use between props names. 'bullet' or 'comma'. Default 'bullet'.
     17 *
     18 * @return string
     19 */
     20function wporg_wordpress_credits_shortcode( $attrs ) {
     21    $attrs = wp_parse_args(
     22        $attrs,
     23        array(
     24            'class'     => 'is-style-wporg-props-long alignfull',
     25            'exclude'   => '',
     26            'separator' => 'bullet',
     27        )
     28    );
     29
    830    if ( ! isset( $attrs[0] ) ) {
    931        return '';
     
    2547    }
    2648
    27     if ( isset( $attrs['exclude'] ) ) {
     49    if ( ! empty( $attrs['exclude'] ) ) {
    2850        $exclude = explode( ',', strtolower( $attrs['exclude'] ) );
    2951        $props = array_diff_key( $props, array_flip( $exclude ) );
     
    3658    }
    3759
    38     return '<p>' . wp_sprintf( '%l.', $output ) . '</p>';
     60    $container_atts = '';
     61    if ( ! empty( $attrs['class'] ) ) {
     62        $container_atts .= ' class="' . esc_attr( $attrs['class'] ) . '"';
     63    }
     64
     65    $content = '';
     66    switch ( $attrs['separator'] ) {
     67        case 'comma':
     68        default:
     69            $content .= wp_sprintf( '%l.', $output );
     70            break;
     71
     72        case 'bullet':
     73            $content .= implode( ' · ', $output );
     74            break;
     75    }
     76
     77    return "<p{$container_atts}>" . $content . '</p>';
    3978}
Note: See TracChangeset for help on using the changeset viewer.