Changeset 2641351
- Timestamp:
- 12/08/2021 04:00:37 PM (4 years ago)
- Location:
- thinker-sidebar-shortcode/trunk
- Files:
-
- 2 edited
-
readme.txt (modified) (1 diff)
-
thinker-sidebar-shortcode.php (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
-
thinker-sidebar-shortcode/trunk/readme.txt
r2191202 r2641351 4 4 Donate link: http://www.thinkerwebdesign.com/thinker-sidebar-shortcode-plugin/ 5 5 Requires at least: 3.4+ 6 Tested up to: 5. 36 Tested up to: 5.8 7 7 Stable tag: trunk 8 8 License: GPLv3 -
thinker-sidebar-shortcode/trunk/thinker-sidebar-shortcode.php
r1847350 r2641351 1 1 <?php 2 3 2 /** 4 3 * Thinker Sidebar Shortcode … … 45 44 * Contains the shortcode function and the action hook. 46 45 * 47 * @since 1.0.0 48 * @package Thinker_Sidebar_Shortcode 46 * @since 1.0.0 49 47 */ 50 48 class Thinker_Sidebar_Shortcode { … … 55 53 * Adds shortcode after WordPress has been initialized. 56 54 * 57 * @since 1.0.055 * @since 1.0.0 58 56 */ 59 57 public function run() { 60 61 58 add_action( 'init', array( $this, 'register_shortcode' ) ); 62 63 59 } 64 60 … … 68 64 * Adds a hook for the [sidebar] shortcode tag. 69 65 * 70 * @since 1.0.066 * @since 1.0.0 71 67 */ 72 68 public function register_shortcode() { 73 74 69 add_shortcode( 'sidebar', array( $this, 'shortcode' ) ); 75 76 70 } 77 71 … … 83 77 * If the shortcode matches a sidebar then verifies that the sidebar has widgets. 84 78 * Sanitizes the HTML classes and removes unneeded spaces before returning HTML. 85 * If neither ID nor Name exactly matches an active sidebar then returns n othing.79 * If neither ID nor Name exactly matches an active sidebar then returns null. 86 80 * 87 * @since 1.0.081 * @since 1.0.0 88 82 * 89 * @global array$wp_registered_sidebars83 * @global array $wp_registered_sidebars 90 84 * 91 * @param array $attsShortcode attributes.92 * @param string $content93 * @return string $htmlDisplays sidebar.85 * @param array $atts Shortcode attributes. 86 * @param array $content Shortcode content. Default empty. 87 * @return string Displays sidebar. 94 88 */ 95 public function shortcode( $atts, $content = null ) { 96 89 public function shortcode( $atts, $content = '' ) { 97 90 global $wp_registered_sidebars; 98 91 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 } 100 96 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 ); 102 107 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']; 109 112 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 ) { 114 119 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']; 179 123 } 180 124 } 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; 181 155 } 182 156 } … … 189 163 * registering the shortcode via the init action hook. 190 164 * 191 * @since 1.0.0165 * @since 1.0.0 192 166 */ 193 167 function run_thinker_sidebar_shortcode() { 194 195 168 $plugin = new Thinker_Sidebar_Shortcode(); 196 169 $plugin->run(); 197 198 170 } 199 171 run_thinker_sidebar_shortcode(); 200
Note: See TracChangeset
for help on using the changeset viewer.