Changeset 1535968
- Timestamp:
- 11/17/2016 11:20:18 PM (9 years ago)
- Location:
- shortcode-locator
- Files:
-
- 9 added
- 2 deleted
- 2 edited
-
tags/1.1 (added)
-
tags/1.1/includes (added)
-
tags/1.1/includes/class-shortcode-locator-settings.php (added)
-
tags/1.1/includes/class-shortcode-locator-table.php (added)
-
tags/1.1/readme.txt (added)
-
tags/1.1/shortcode-locator.php (added)
-
trunk/includes (added)
-
trunk/includes/class-shortcode-locator-settings.php (added)
-
trunk/includes/class-shortcode-locator-table.php (added)
-
trunk/readme.txt (modified) (3 diffs)
-
trunk/shortcode-locator-settings.php (deleted)
-
trunk/shortcode-locator-table.php (deleted)
-
trunk/shortcode-locator.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
shortcode-locator/trunk/readme.txt
r1106154 r1535968 3 3 Tags: shortcode, find content, post types 4 4 Requires at least: 4.0.0 5 Tested up to: 4. 1.16 Stable tag: 4. 1.15 Tested up to: 4.6.1 6 Stable tag: 4.6.1 7 7 8 8 Quickly locate what and where shortcodes are being used. … … 15 15 == Installation == 16 16 = Requirements = 17 * WordPress version 4.0.0 and later (tested at 4. 1.1)17 * WordPress version 4.0.0 and later (tested at 4.6.1) 18 18 19 19 = Installation = … … 28 28 29 29 == Changelog == 30 = v1.1 = 31 * Rewrote entire plugin. 30 32 = v1.0.1 = 31 33 * Made post title and type columns sortable -
shortcode-locator/trunk/shortcode-locator.php
r1106152 r1535968 1 1 <?php 2 3 /* 4 Plugin Name: Shortcode Locator 5 Plugin URI: http://travislop.es/plugins/shortcode-locator/ 6 Description: Quickly locate what and where shortcodes are being used 7 Version: 1.0.1 8 Author: travislopes 9 Author URI: http://travislop.es 10 */ 11 12 class Shortcode_Locator { 13 14 public static $admin_page_slug = 'shortcode_locator'; 15 public static $basename; 16 public static $post_types = array(); 17 public static $settings; 18 private static $admin_page_title = 'Shortcode Locator'; 19 private static $_instance = null; 20 21 public static function get_instance() { 22 23 if ( self::$_instance == null ) 24 self::$_instance = new Shortcode_Locator(); 25 26 return self::$_instance; 27 28 } 29 30 public function __construct() { 31 32 /* Set basename */ 33 self::$basename = plugin_basename( __FILE__ ); 34 35 /* Include settings page class */ 36 include_once 'shortcode-locator-settings.php'; 37 38 /* Assign settings to this class */ 39 self::$settings = Shortcode_Locator_Settings::get_settings(); 40 41 /* Register admin page */ 42 add_action( 'admin_menu', array( __CLASS__, 'register_admin_page' ) ); 43 44 /* Add and render shortcode column to selected post types */ 45 foreach( self::$settings['display_column'] as $post_type ) { 46 47 add_filter( 'manage_'. $post_type .'_posts_columns', array( __CLASS__, 'add_shortcode_post_column' ) ); 48 add_filter( 'manage_'. $post_type .'_posts_custom_column', array( __CLASS__, 'render_shortcode_post_column' ), 10, 2 ); 49 2 /** 3 Plugin Name: Shortcode Locator 4 Plugin URI: http://travislop.es/plugins/shortcode-locator/ 5 Description: Quickly locate what and where shortcodes are being used 6 Version: 1.1 7 Author: travislopes 8 Author URI: http://travislop.es 9 **/ 10 11 /** 12 * Shortcode Locator. 13 * 14 * @since 1.0 15 * @author Travis Lopes 16 * @copyright Copyright (c) 2016, Travis Lopes 17 */ 18 class Shortcode_Locator { 19 20 /** 21 * Defines the plugin slug. 22 * 23 * @since 1.1 24 * @access public 25 * @var string $_slug The slug used for this plugin. 26 */ 27 public $slug = 'shortcode_locator'; 28 29 /** 30 * Defines the full path to this class file. 31 * 32 * @since 1.1 33 * @access protected 34 * @var string $full_path The full path. 35 */ 36 protected $full_path = __FILE__; 37 38 /** 39 * Get instance of this class. 40 * 41 * @since 1.0 42 * @access public 43 * @static 44 * 45 * @return $_instance 46 */ 47 private static $_instance = null; 48 49 /** 50 * Get instance of this class. 51 * 52 * @since 1.0 53 * @access public 54 * @static 55 * 56 * @return $_instance 57 */ 58 public static function get_instance() { 59 60 if ( null === self::$_instance ) { 61 self::$_instance = new self; 62 } 63 64 return self::$_instance; 65 66 } 67 68 /** 69 * Constructor. 70 * 71 * @since 1.0 72 * @access public 73 */ 74 public function __construct() { 75 76 // Load required classes. 77 require_once 'includes/class-shortcode-locator-settings.php'; 78 require_once 'includes/class-shortcode-locator-table.php'; 79 80 // Initialize settings. 81 $settings = shortcode_locator_settings(); 82 83 // Register plugin action links. 84 add_filter( 'plugin_action_links_'. plugin_basename( $this->full_path ), array( $this, 'plugin_action_links' ) ); 85 86 // Register plugin pages. 87 add_action( 'admin_menu', array( $this, 'register_plugin_page' ) ); 88 add_action( 'admin_menu', array( $settings, 'register_settings_page' ) ); 89 90 // Add shortcode post column. 91 add_filter( 'manage_posts_columns', array( $this, 'add_posts_column' ), 10, 2 ); 92 add_action( 'manage_posts_custom_column', array( $this, 'render_posts_column' ), 10, 2 ); 93 94 } 95 96 97 98 99 100 // # SETTINGS ------------------------------------------------------------------------------------------------------ 101 102 /** 103 * Links display on the pluginspage. 104 * 105 * @since 1.1 106 * @access public 107 * @param array $links An array of plugin action links. 108 * 109 * @return array 110 */ 111 public function plugin_action_links( $links ) { 112 113 // Prepare plugin links. 114 $plugin_links = array( '<a href="'. admin_url( 'options-general.php?page='. shortcode_locator_settings()->page_slug ) .'">' . esc_html__( 'Settings', 'shortcode-locator' ) . '</a>' ); 115 116 return array_merge( $plugin_links, $links ); 117 118 } 119 120 121 122 123 124 // # PLUGIN PAGE --------------------------------------------------------------------------------------------------- 125 126 /** 127 * Register plugin page. 128 * 129 * @since 1.0 130 * @access public 131 */ 132 public function register_plugin_page() { 133 134 // Add plugin page to Tools menu. 135 add_submenu_page( 136 'tools.php', // Parent slug. 137 $this->plugin_page_title(), // Page title. 138 $this->plugin_page_title(), // Menu title. 139 'edit_posts', // Capability. 140 $this->slug, // Menu slug. 141 array( $this, 'render_plugin_page' ) // Page function. 142 ); 143 144 } 145 146 /** 147 * Render and display plugin page. 148 * 149 * @since 1.0 150 * @access public 151 */ 152 public function render_plugin_page() { 153 154 // Start page. 155 echo '<div class="wrap">'; 156 157 // Display page title. 158 echo '<h2>'. $this->plugin_page_title() .'</h2>'; 159 160 // Display shortcodes table. 161 $table = new Shortcode_Locator_Table(); 162 $table->prepare_items(); 163 $table->display(); 164 165 // End page. 166 echo '</div>'; 167 168 } 169 170 /** 171 * Prepares plugin page title. 172 * 173 * @since 1.1 174 * @access public 175 * 176 * @return string 177 */ 178 public function plugin_page_title() { 179 180 return esc_html__( 'Shortcode Locator', 'shortcode-locator' ); 181 182 } 183 184 185 186 187 188 // # POSTS PAGE ---------------------------------------------------------------------------------------------------- 189 190 /** 191 * Add shortcode column to posts list. 192 * 193 * @since 1.0 194 * @access public 195 * 196 * @param array $columns An array of column names. 197 * @param string $post_type The post type slug. 198 * 199 * @return array 200 */ 201 public function add_posts_column( $columns, $post_type ) { 202 203 // Get allowed post types. 204 $allowed_post_types = shortcode_locator_settings()->get_setting( 'display_column' ); 205 206 // If this post type is not on the display columns list, return columns. 207 if ( ! in_array( $post_type, $allowed_post_types ) ) { 208 return $columns; 209 } 210 211 // Add columns. 212 $columns['shortcodes'] = esc_html__( 'Shortcodes Located', 'shortcode-locator' ); 213 214 return $columns; 215 216 } 217 218 /** 219 * Display shortcode column on posts list. 220 * 221 * @since 1.0 222 * @access public 223 * 224 * @param string $column The name of the column to display. 225 * @param int $post_id The current post ID. 226 */ 227 public function render_posts_column( $column, $post_id ) { 228 229 // Get post content. 230 $post_content = get_post_field( 'post_content', $post_id ); 231 232 // Get shortcodes in post. 233 $shortcodes = $this->get_shortcodes_for_post( $post_content ); 234 235 // If no shortcodes were found, return. 236 if ( empty( $shortcodes ) ) { 237 return; 238 } 239 240 // Escape shortcodes. 241 $shortcodes = array_map( 'esc_html', $shortcodes ); 242 243 // Display shortcodes. 244 echo implode( '<br />', $shortcodes ); 245 246 } 247 248 /** 249 * Get available post types. 250 * 251 * @since 1.1 252 * @access public 253 * 254 * @return array 255 */ 256 public function get_post_types() { 257 258 // Initialize post types array. 259 $post_types = array(); 260 261 // Define excluded post types. 262 $excluded_post_types = array( 'attachment', 'revision', 'nav_menu_item' ); 263 $excluded_post_types = apply_filters( 'shortcode_locator_excluded_post_types', $excluded_post_types ); 264 265 // Get registered post types. 266 $registered_post_types = get_post_types( array(), 'objects' ); 267 268 // Loop through registered post types. 269 foreach ( $registered_post_types as $post_type ) { 270 271 // If post type is excluded, skip it. 272 if ( in_array( $post_type->name, $excluded_post_types ) ) { 273 continue; 50 274 } 51 52 /* Set available post types */ 53 add_action( 'admin_init', array( __CLASS__, 'set_post_types' ) ); 54 55 } 56 57 /* Register admin page */ 58 function register_admin_page() { 59 60 add_submenu_page( 'tools.php', self::$admin_page_title, self::$admin_page_title, 'edit_posts', self::$admin_page_slug, array( __CLASS__, 'render_admin_page' ) ); 61 62 } 63 64 /* Render admin page */ 65 function render_admin_page() { 66 67 /* Load table class */ 68 include_once 'shortcode-locator-table.php'; 69 70 /* Open page */ 71 echo '<div class="wrap">'; 72 73 /* Page title */ 74 echo '<h2>'. self::$admin_page_title .'</h2>'; 75 76 /* Display table */ 77 $shortcode_locator_table = new Shortcode_Locator_Table(); 78 $shortcode_locator_table->prepare_items(); 79 $shortcode_locator_table->display(); 80 81 /* Close page */ 82 echo '</div>'; 83 84 } 85 86 /* Add shortcode column to selected post types */ 87 function add_shortcode_post_column( $columns ) { 88 89 $columns['shortcodes'] = 'Shortcodes Located'; 90 return $columns; 91 92 } 93 94 /* Render shortcode column to selected post types */ 95 function render_shortcode_post_column( $column, $post_id ) { 96 97 /* Get shortcodes in post */ 98 $shortcodes_located = self::get_shortcodes_for_post( get_post_field( 'post_content', $post_id ) ); 99 100 /* Display if found */ 101 if ( ! empty( $shortcodes_located ) ) 102 echo implode( '<br />', $shortcodes_located ); 103 104 } 105 106 /* Set available post types */ 107 function set_post_types() { 108 109 /* Set list of post types to exclude */ 110 $excluded_post_types = apply_filters( 'shortcode_locator_excluded_post_types', array( 'attachment', 'revision', 'nav_menu_item' ) ); 111 112 /* Get registered post types */ 113 $registered_post_types = get_post_types( array(), 'objects' ); 114 115 /* Loop through registered post types and push to class array if not excluded */ 116 foreach( $registered_post_types as $post_type ) { 117 118 if( ! in_array( $post_type->name, $excluded_post_types ) ) 119 self::$post_types[$post_type->name] = $post_type->label; 120 121 } 122 123 } 124 125 /* Get shortcodes for post */ 126 function get_shortcodes_for_post( $post_content, $shortcode = null ) { 127 128 /* Get shortcode regex */ 129 $shortcode_regex = ( is_null( $shortcode ) ) ? get_shortcode_regex() : self::get_specific_shortcode_regex( $shortcode ); 130 131 /* Search for shortcodes */ 132 preg_match_all( '/'. $shortcode_regex .'/', $post_content, $shortcodes_located ); 133 134 /* Loop through the shortcodes located array and push them to a separate array */ 135 $shortcodes = array(); 136 foreach( $shortcodes_located as $child_array ) { 137 138 foreach( $child_array as $key => $value ) { 139 140 if ( ! isset( $shortcodes[$key] ) ) 141 $shortcodes[$key] = array(); 142 143 $shortcodes[$key][] = $value; 144 145 } 146 147 } 148 149 /* Loop through the shortcodes again and put together shortcode string */ 150 $shortcode_strings = array(); 151 foreach( $shortcodes as &$shortcode ) { 152 153 $shortcode_string = '['. $shortcode[2]; 154 155 /* If arguments exist, add them to the shortcode string */ 156 if ( ! empty( $shortcode[3] ) ) 157 $shortcode_string .= $shortcode[3]; 158 159 /* Close shortcode string */ 160 $shortcode_string .= ']'; 161 162 /* Add to array */ 163 $shortcode_strings[] = $shortcode_string; 164 165 } 166 167 /* Return found shortcodes */ 168 return $shortcode_strings; 169 170 } 171 172 /* Get regex for specific shortcode */ 173 function get_specific_shortcode_regex( $shortcode ) { 174 175 return 176 '\\[' // Opening bracket 177 . '(\\[?)' // 1: Optional second opening bracket for escaping shortcodes: [[tag]] 178 . "($shortcode)" // 2: Shortcode name 179 . '(?![\\w-])' // Not followed by word character or hyphen 180 . '(' // 3: Unroll the loop: Inside the opening shortcode tag 181 . '[^\\]\\/]*' // Not a closing bracket or forward slash 182 . '(?:' 183 . '\\/(?!\\])' // A forward slash not followed by a closing bracket 184 . '[^\\]\\/]*' // Not a closing bracket or forward slash 185 . ')*?' 186 . ')' 187 . '(?:' 188 . '(\\/)' // 4: Self closing tag ... 189 . '\\]' // ... and closing bracket 190 . '|' 191 . '\\]' // Closing bracket 192 . '(?:' 193 . '(' // 5: Unroll the loop: Optionally, anything between the opening and closing shortcode tags 194 . '[^\\[]*+' // Not an opening bracket 195 . '(?:' 196 . '\\[(?!\\/\\2\\])' // An opening bracket not followed by the closing shortcode tag 197 . '[^\\[]*+' // Not an opening bracket 198 . ')*+' 199 . ')' 200 . '\\[\\/\\2\\]' // Closing shortcode tag 201 . ')?' 202 . ')' 203 . '(\\]?)'; // 6: Optional second closing brocket for escaping shortcodes: [[tag]] 204 205 206 } 207 208 } 209 210 Shortcode_Locator::get_instance(); 275 276 // Add post type to return array. 277 $post_types[ $post_type->name ] = $post_type->label; 278 279 } 280 281 return $post_types; 282 283 } 284 285 286 287 288 289 // # HELPERS ------------------------------------------------------------------------------------------------------- 290 291 /** 292 * Get shortcodes within post content. 293 * 294 * @since 1.0 295 * @access public 296 * 297 * @param string $post_content Post content. 298 * @param string $shortcode Shortcode to search for. Defaults to null. 299 * 300 * @return array 301 */ 302 public function get_shortcodes_for_post( $post_content, $shortcode = null ) { 303 304 // Initialize shortcodes return array. 305 $shortcodes = array(); 306 307 // Prepare shortcode regex. 308 $shortcode_regex = $shortcode ? $this->get_shortcode_regex( $shortcode ) : get_shortcode_regex(); 309 310 // Locate shortcodes. 311 preg_match_all( '/'. $shortcode_regex .'/', $post_content, $located ); 312 313 // If no shortcodes were located, return. 314 if ( empty( $located[0] ) ) { 315 return $shortcodes; 316 } 317 318 // Loop through located shortcodes. 319 foreach ( $located[0] as $i => $shortcode_string ) { 320 321 // Add to return array. 322 $shortcodes[] = $shortcode_string; 323 324 } 325 326 return $shortcodes; 327 328 } 329 330 /** 331 * Get regular expression for a shortcode. 332 * 333 * @since 1.0 334 * @access public 335 * @param string $shortcode Shortcode name. 336 * 337 * @return string 338 */ 339 public function get_shortcode_regex( $shortcode ) { 340 341 return 342 '\\[' // Opening bracket 343 . '(\\[?)' // 1: Optional second opening bracket for escaping shortcodes: [[tag]] 344 . "($shortcode)" // 2: Shortcode name 345 . '(?![\\w-])' // Not followed by word character or hyphen 346 . '(' // 3: Unroll the loop: Inside the opening shortcode tag 347 . '[^\\]\\/]*' // Not a closing bracket or forward slash 348 . '(?:' 349 . '\\/(?!\\])' // A forward slash not followed by a closing bracket 350 . '[^\\]\\/]*' // Not a closing bracket or forward slash 351 . ')*?' 352 . ')' 353 . '(?:' 354 . '(\\/)' // 4: Self closing tag ... 355 . '\\]' // ... and closing bracket 356 . '|' 357 . '\\]' // Closing bracket 358 . '(?:' 359 . '(' // 5: Unroll the loop: Optionally, anything between the opening and closing shortcode tags 360 . '[^\\[]*+' // Not an opening bracket 361 . '(?:' 362 . '\\[(?!\\/\\2\\])' // An opening bracket not followed by the closing shortcode tag 363 . '[^\\[]*+' // Not an opening bracket 364 . ')*+' 365 . ')' 366 . '\\[\\/\\2\\]' // Closing shortcode tag 367 . ')?' 368 . ')' 369 . '(\\]?)'; // 6: Optional second closing brocket for escaping shortcodes: [[tag]] 370 371 } 372 373 } 374 375 /** 376 * Returns an instance of the Shortcode_Locator class. 377 * 378 * @see Shortcode_Locator::get_instance() 379 * 380 * @return object Shortcode_Locator 381 */ 382 function shortcode_locator() { 383 return Shortcode_Locator::get_instance(); 384 } 385 386 shortcode_locator();
Note: See TracChangeset
for help on using the changeset viewer.