Plugin Directory

Changeset 2641357


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

Updated WP coding standards.

Location:
thinker-sidebar-shortcode/tags/1.0.0
Files:
2 edited

Legend:

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

    r1833032 r2641357  
    1 === Thinker Sidebar Shortcode ===
     1=== Sidebar Shortcode ===
     2Contributors: thinkerwebdesign
    23Tags: sidebar shortcode, sidebar, shortcode, widget area, widget area shortcode, shortcode for sidebar, sidebar in page, sidebar in post, sidebar via shortcode, sidebar by shortcode, sidebar from shortcode, sidebar per shortcode
    34Donate link: http://www.thinkerwebdesign.com/thinker-sidebar-shortcode-plugin/
    45Requires at least: 3.4+
    5 Tested up to: 4.9
     6Tested up to: 5.8
    67Stable tag: trunk
    78License: GPLv3
     
    2021
    2122**Method 1 - (Recommended Use: Add a sidebar using a sidebar Name.)**
     23
    2224A sidebar Name can be found in the `Appearance > Widgets` section of your WordPress Admin Area.
    23 Example uses:
    24 [sidebar name="your-sidebar-name"]
    25 [sidebar name="your-sidebar-name" class="custom-class"]
    26 [sidebar name="your-sidebar-name" class="custom-class-1 custom-class-2 custom-class-3"]
    2725
    28 **Method 2 (Advanced WP Users: Add a sidebar using a sidebar ID.)**
    29 A sidebar ID can be found in your theme`s register_sidebar functions, usually in the theme`s functions.php file.
    30 Example uses:
    31 [sidebar id="your-sidebar-id"]
    32 [sidebar id="your-sidebar-id" class="custom-class"]
    33 [sidebar id="your-sidebar-id" class="custom-class-1 custom-class-2 custom-class-3"]
     26*Example uses:*
     27
     28* `[sidebar name="your-sidebar-name"]`
     29* `[sidebar name="your-sidebar-name" class="custom-class"]`
     30* `[sidebar name="your-sidebar-name" class="custom-class-1 custom-class-2 custom-class-3"]`
     31
     32**Method 2 - (Advanced WP Users: Add a sidebar using a sidebar ID.)**
     33
     34A sidebar ID can be found in your theme's `register_sidebar` functions, usually in the theme's `functions.php` file.
     35
     36*Example uses:*
     37
     38* `[sidebar id="your-sidebar-id"]`
     39* `[sidebar id="your-sidebar-id" class="custom-class"]`
     40* `[sidebar id="your-sidebar-id" class="custom-class-1 custom-class-2 custom-class-3"]`
    3441
    3542**General Notes**
     43
    3644* The spelling and capitalization of a shortcode Name or ID must exactly match that of the desired sidebar.
    3745* Definition of an active sidebar: An active sidebar is a sidebar that contains widgets.
    38 * A [sidebar] shortcode without an active sidebar Name or active sidebar ID displays nothing.
     46* A `[sidebar]` shortcode without an active sidebar Name or active sidebar ID displays nothing.
    3947* An active sidebar ID overrides a sidebar Name if both are present in the same shortcode.
    4048* There is no limit to the number of shortcodes that can be used on the same page or post.
     
    4250
    4351**HTML Class Notes**
     52
    4453* Separate multiple custom classes using a space character. Examples shown in `Method` sections above.
    4554* There is no limit to the number of custom classes that can be used.
     
    6978= How can a sidebar ID be found? =
    7079
    71 Advanced WP users can find sidebar IDs in the active theme`s register_sidebar functions, usually in the theme`s functions.php file.
     80Advanced WP users can find sidebar IDs in the active theme's `register_sidebar` functions, usually in the theme's `functions.php` file.
    7281See the `Method` sections and `Screenshots` section for details.
    73 See also: https://codex.wordpress.org/Function_Reference/register_sidebar
     82See also: [https://codex.wordpress.org/Function_Reference/register_sidebar](https://codex.wordpress.org/Function_Reference/register_sidebar)
    7483
    7584= How many custom classes can be used? =
  • thinker-sidebar-shortcode/tags/1.0.0/thinker-sidebar-shortcode.php

    r1833032 r2641357  
    11<?php
    2 
    32/**
    43 * Thinker Sidebar Shortcode
     
    1110 *
    1211 * @wordpress-plugin
    13  * Plugin Name:       Thinker Sidebar Shortcode
     12 * Plugin Name:       Sidebar Shortcode
    1413 * Plugin URI:        http://www.thinkerwebdesign.com/thinker-sidebar-shortcode-plugin/
    1514 * Description:       Add sidebars to WP posts and pages via shortcodes.
     
    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.