Plugin Directory

Changeset 2641351


Ignore:
Timestamp:
12/08/2021 04:00:37 PM (4 years ago)
Author:
thinkerwebdesign
Message:

Updated WP coding standards.

Location:
thinker-sidebar-shortcode/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • thinker-sidebar-shortcode/trunk/readme.txt

    r2191202 r2641351  
    44Donate link: http://www.thinkerwebdesign.com/thinker-sidebar-shortcode-plugin/
    55Requires at least: 3.4+
    6 Tested up to: 5.3
     6Tested up to: 5.8
    77Stable tag: trunk
    88License: GPLv3
  • thinker-sidebar-shortcode/trunk/thinker-sidebar-shortcode.php

    r1847350 r2641351  
    11<?php
    2 
    32/**
    43 * Thinker Sidebar Shortcode
     
    4544 * Contains the shortcode function and the action hook.
    4645 *
    47  * @since      1.0.0
    48  * @package    Thinker_Sidebar_Shortcode
     46 * @since   1.0.0
    4947 */
    5048class Thinker_Sidebar_Shortcode {
     
    5553     * Adds shortcode after WordPress has been initialized.
    5654     *
    57      * @since    1.0.0
     55     * @since 1.0.0
    5856     */
    5957    public function run() {
    60 
    6158        add_action( 'init', array( $this, 'register_shortcode' ) );
    62 
    6359    }
    6460
     
    6864     * Adds a hook for the [sidebar] shortcode tag.
    6965     *
    70      * @since     1.0.0
     66     * @since 1.0.0
    7167     */
    7268    public function register_shortcode() {
    73 
    7469        add_shortcode( 'sidebar', array( $this, 'shortcode' ) );
    75 
    7670    }
    7771
     
    8377     * If the shortcode matches a sidebar then verifies that the sidebar has widgets.
    8478     * Sanitizes the HTML classes and removes unneeded spaces before returning HTML.
    85      * If neither ID nor Name exactly matches an active sidebar then returns nothing.
     79     * If neither ID nor Name exactly matches an active sidebar then returns null.
    8680     *
    87      * @since     1.0.0
     81     * @since 1.0.0
    8882     *
    89      * @global    array    $wp_registered_sidebars
     83     * @global array $wp_registered_sidebars
    9084     *
    91      * @param     array     $atts     Shortcode attributes.
    92      * @param     string    $content
    93      * @return    string    $html     Displays sidebar.
     85     * @param  array $atts    Shortcode attributes.
     86     * @param  array $content Shortcode content. Default empty.
     87     * @return string         Displays sidebar.
    9488     */
    95     public function shortcode( $atts, $content = null ) {
    96 
     89    public function shortcode( $atts, $content = '' ) {
    9790        global $wp_registered_sidebars;
    9891
    99         if ( isset ( $wp_registered_sidebars ) ) {
     92        // Returns if no sidebars are registered or shortcode attributes are empty.
     93        if ( ! is_dynamic_sidebar() || empty( $wp_registered_sidebars ) || empty( $atts ) ) {
     94            return;
     95        }
    10096
    101             if ( is_array ( $atts ) ) {
     97        // Sets default shortcode attributes if missing from shortcode.
     98        $atts = shortcode_atts(
     99            array(
     100                'name'  => '',
     101                'id'    => '',
     102                'class' => '',
     103            ),
     104            $atts,
     105            'sidebar'
     106        );
    102107
    103                 // Sets default shortcode attributes if missing from shortcode.
    104                 $atts = shortcode_atts( array(
    105                     'name' => '',
    106                     'id' => '',
    107                     'class' => '',
    108                 ), $atts, 'sidebar' );
     108        // Sets variables from shortcode attributes.
     109        $name  = $atts['name'];
     110        $id    = $atts['id'];
     111        $class = $atts['class'];
    109112
    110                 // Sets name, id, and class variables from shortcode attributes.
    111                 $name = $atts['name'];
    112                 $id = $atts['id'];
    113                 $class = $atts['class'];
     113        // Gets Sidebar ID. Gives precedent to ID over Name.
     114        $sidebar_id = '';
     115        if ( $id && array_key_exists( $id, $wp_registered_sidebars ) ) {
     116            $sidebar_id = $id;
     117        } elseif ( $name ) {
     118            foreach ( $wp_registered_sidebars as $sidebar ) {
    114119
    115                 $sidebar_id = '';
    116 
    117                 // Checks if any registered sidebar has active widgets.
    118                 if ( is_dynamic_sidebar() ) {
    119 
    120                     // Gives precedent to ID over Name if shortcode ID exactly matches a sidebar.
    121                     if ( $id !='' && array_key_exists( $id, $wp_registered_sidebars ) ) {
    122 
    123                         $sidebar_id = $id;
    124 
    125                     } elseif ( $name !='' ) {
    126 
    127                         foreach ( $wp_registered_sidebars as $sidebar ) {
    128 
    129                             // If shortcode Name matches a sidebar then sets sidebar ID.
    130                             if ( $sidebar['name'] == $name ) {
    131 
    132                                 $sidebar_id = $sidebar['id'];
    133 
    134                             }
    135 
    136                         }
    137 
    138                     } else {
    139 
    140                         return;
    141 
    142                     }
    143                 }
    144 
    145                 // Verifies that the shortcode matched a sidebar and the sidebar is active.
    146                 if ( $sidebar_id !='' && is_active_sidebar( $sidebar_id ) ) {
    147 
    148                     $class_out = 'sidebar-shortcode';
    149 
    150                     // Sanitizes class by permitting only (letter,number,space,-,_) characters.
    151                     if ( !preg_match ( '/[^-_a-zA-Z0-9\pL]/u', $sidebar_id ) ) {
    152                         $class_out = $class_out . ' ' . $sidebar_id;
    153                     }
    154                     if ( $class !='' && !preg_match ( '/[^-_ a-zA-Z0-9\pL]/u', $class ) ) {
    155                         $class_out = $class_out . ' ' . $class;
    156                     }
    157 
    158                     // Removes leading, trailing, and extra spaces from HTML class attribute.
    159                     $class_out = trim( $class_out );
    160                     $class_out = preg_replace( '/\s+/', ' ', $class_out );
    161 
    162                     // Displays the Sidebar.
    163                     ob_start();
    164                     dynamic_sidebar( $sidebar_id );
    165                     $sidebar_out = ob_get_clean();
    166                     $html = '
    167                         <div class="' . $class_out . '" role="complementary">
    168                             <div class="sidebar-shortcode-content">
    169                                 ' . $sidebar_out . '
    170                             </div>
    171                         </div>
    172                     ';
    173                     return $html;
    174 
    175                 } else {
    176 
    177                     return;
    178 
     120                // If shortcode Name matches a sidebar then sets sidebar ID.
     121                if ( $sidebar['name'] === $name ) {
     122                    $sidebar_id = $sidebar['id'];
    179123                }
    180124            }
     125        }
     126
     127        // Verifies that the shortcode matched a sidebar and the sidebar is active.
     128        if ( $sidebar_id && is_active_sidebar( $sidebar_id ) ) {
     129
     130            // Sanitizes class by permitting only (letter,number,space,-,_) characters.
     131            $class_out = 'sidebar-shortcode';
     132            if ( ! preg_match( '/[^-_a-zA-Z0-9\pL]/u', $sidebar_id ) ) {
     133                $class_out = $class_out . ' ' . $sidebar_id;
     134            }
     135            if ( $class && ! preg_match( '/[^-_ a-zA-Z0-9\pL]/u', $class ) ) {
     136                $class_out = $class_out . ' ' . $class;
     137            }
     138
     139            // Removes leading, trailing, and extra spaces from HTML class attribute.
     140            $class_out = trim( $class_out );
     141            $class_out = preg_replace( '/\s+/', ' ', $class_out );
     142
     143            // Displays the Sidebar.
     144            ob_start();
     145            dynamic_sidebar( $sidebar_id );
     146            $sidebar_out = ob_get_clean();
     147            $html        = '
     148                <div class="' . $class_out . '" role="complementary">
     149                    <div class="sidebar-shortcode-content">
     150                        ' . $sidebar_out . '
     151                    </div>
     152                </div>
     153            ';
     154            return $html;
    181155        }
    182156    }
     
    189163 * registering the shortcode via the init action hook.
    190164 *
    191  * @since     1.0.0
     165 * @since 1.0.0
    192166 */
    193167function run_thinker_sidebar_shortcode() {
    194 
    195168    $plugin = new Thinker_Sidebar_Shortcode();
    196169    $plugin->run();
    197 
    198170}
    199171run_thinker_sidebar_shortcode();
    200 
Note: See TracChangeset for help on using the changeset viewer.