Changeset 2824088
- Timestamp:
- 11/25/2022 10:24:23 AM (3 years ago)
- Location:
- wp-permastructure/trunk
- Files:
-
- 3 edited
-
. (modified) (1 prop)
-
readme.txt (modified) (1 diff)
-
wp-permastructure.php (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
wp-permastructure/trunk
-
Property
svn:ignore
set to
.git
-
Property
svn:ignore
set to
-
wp-permastructure/trunk/readme.txt
r1279638 r2824088 5 5 Requires at least: 3.3 6 6 Tested up to: 4.0 7 Stable tag: 1.4 .27 Stable tag: 1.4 8 8 9 9 Adds the ability to configure permalinks for custom post types using rewrite tags like %post_id% and %author%. -
wp-permastructure/trunk/wp-permastructure.php
r1279638 r2824088 4 4 Plugin URI: https://github.com/interconnectit/wp-permastructure 5 5 Description: Adds the ability to define permalink structures for any custom post type using rewrite tags. 6 Version: 1.4. 26 Version: 1.4.3 7 7 Author: Robert O'Rourke 8 8 Author URI: http://interconnectit.com … … 20 20 * 21 21 * register_post_type( 'my_type', array( 22 * ...23 * 'rewrite' => array( 'permastruct' => '/%custom_taxonomy%/%author%/%postname%/' ),24 * ...22 * ... 23 * 'rewrite' => array( 'permastruct' => '/%custom_taxonomy%/%author%/%postname%/' ), 24 * ... 25 25 * ) ); 26 26 * … … 39 39 */ 40 40 41 if ( ! class_exists( 'wp_permastructure' ) ) { 42 43 add_action( 'init', array( 'wp_permastructure', 'instance' ), 0 ); 44 45 class wp_permastructure { 46 47 public $settings_section = 'wp_permastructure'; 48 49 /** 50 * @var protect endpoints from being vaped if a category/tag slug is set 51 */ 52 protected $endpoints; 53 54 /** 55 * Reusable object instance. 56 * 57 * @type object 58 */ 59 protected static $instance = null; 60 61 /** 62 * Creates a new instance. Called on 'init'. 63 * May be used to access class methods from outside. 64 * 65 * @see __construct() 66 * @return void 67 */ 68 public static function instance() { 69 null === self :: $instance AND self :: $instance = new self; 70 return self :: $instance; 71 } 72 73 74 public function __construct() { 75 76 // Late init to protect endpoints 77 add_action( 'init', array( $this, 'late_init' ), 999 ); 78 79 // Settings fields on permalinks page 80 add_action( 'admin_init', array( $this, 'admin_init' ) ); 81 82 // add our new peramstructs to the rewrite rules 83 add_filter( 'post_rewrite_rules', array( $this, 'add_permastructs' ) ); 84 85 // parse the generated links - enable custom taxonomies for built in post links 86 add_filter( 'post_link', array( $this, 'parse_permalinks' ), 10, 3 ); 87 add_filter( 'post_type_link', array( $this, 'parse_permalinks' ), 10, 4 ); 88 89 // that's it! 90 91 } 92 93 94 public function late_init() { 95 global $wp_rewrite; 96 $this->endpoints = $wp_rewrite->endpoints; 97 } 98 99 100 /** 101 * Add the settings UI 102 */ 103 public function admin_init() { 104 105 add_settings_section( 106 $this->settings_section, 107 __( 'Custom post type permalink settings' ), 108 array( $this, 'settings_section' ), 109 'permalink' 110 ); 111 112 foreach( get_post_types( array( '_builtin' => false, 'public' => true ), 'objects' ) as $type ) { 113 $id = $type->name . '_permalink_structure'; 114 115 register_setting( 'permalink', $id, array( $this, 'sanitize_permalink' ) ); 116 add_settings_field( 117 $id, 118 __( $type->label . ' permalink structure' ), 119 array( $this, 'permalink_field' ), 120 'permalink', 121 $this->settings_section, 122 array( 'id' => $id ) 123 ); 124 } 125 126 } 127 128 129 public function settings_section() { 130 131 } 132 133 134 public function permalink_field( $args ) { 135 echo '<input type="text" class="regular-text code" value="' . esc_attr( get_option( $args[ 'id' ] ) ) . '" id="' . $args[ 'id' ] . '" name="' . $args[ 'id' ] . '" />'; 136 } 137 138 139 /** 140 * Runs a simple sanitisation of the custom post type permalink structures 141 * and adds an error if no post ID or post name present 142 * 143 * @param string $permalink The permalink structure 144 * 145 * @return string Sanitised permalink structure 146 */ 147 public function sanitize_permalink( $permalink ) { 148 if ( ! empty( $permalink ) && ! preg_match( '/%(post_id|postname)%/', $permalink ) ) 149 add_settings_error( 'permalink_structure', 10, __( 'Permalink structures must contain at least the <code>%post_id%</code> or <code>%postname%</code>.' ) ); 150 151 $filtered = wp_check_invalid_utf8( $permalink ); 152 153 if ( strpos($filtered, '<') !== false ) { 154 $filtered = wp_pre_kses_less_than( $filtered ); 155 // This will strip extra whitespace for us. 156 $filtered = wp_strip_all_tags( $filtered, true ); 157 } else { 158 $filtered = trim( preg_replace('/[\r\n\t ]+/', ' ', $filtered) ); 159 } 160 161 return preg_replace( '/[^a-zA-Z0-9\/\%_-]*/', '', $filtered ); 162 } 163 164 165 /** 166 * This function removes unnecessary rules and adds in the new rules 167 * 168 * @param array $rules The rewrite rules array for post permalinks 169 * 170 * @return array The modified rules array 171 */ 172 public function add_permastructs( $rules ) { 173 global $wp_rewrite; 174 175 // restore endpoints 176 if ( empty( $wp_rewrite->endpoints ) && ! empty( $this->endpoints ) ) 177 $wp_rewrite->endpoints = $this->endpoints; 178 179 $permastruct = $wp_rewrite->permalink_structure; 180 $permastructs = array( $permastruct => array( 'post' ) ); 181 182 // force page rewrite to bottom 183 $wp_rewrite->use_verbose_page_rules = false; 184 185 // get permastructs foreach custom post type and group any that use the same struct 186 foreach( get_post_types( array( '_builtin' => false, 'public' => true ), 'objects' ) as $type ) { 187 // add/override the custom permalink structure if set in options 188 $post_type_permastruct = get_option( $type->name . '_permalink_structure' ); 189 if ( $post_type_permastruct && ! empty( $post_type_permastruct ) ) { 190 if ( ! is_array( $type->rewrite ) ) 191 $type->rewrite = array(); 192 $type->rewrite[ 'permastruct' ] = $post_type_permastruct; 193 } 194 195 // check we have a custom permalink structure 196 if ( ! is_array( $type->rewrite ) || ! isset( $type->rewrite[ 'permastruct' ] ) ) 197 continue; 198 199 // remove default struct rules 200 add_filter( $type->name . '_rewrite_rules', create_function( '$rules', 'return array();' ), 11 ); 201 202 if ( ! isset( $permastructs[ $type->rewrite[ 'permastruct' ] ] ) ) 203 $permastructs[ $type->rewrite[ 'permastruct' ] ] = array(); 204 205 $permastructs[ $type->rewrite[ 'permastruct' ] ][] = $type->name; 206 } 207 208 $rules = array(); 209 210 // add our permastructs scoped to the post types - overwriting any keys that already exist 211 foreach( $permastructs as $struct => $post_types ) { 212 213 // if a struct is %postname% only then we need page rules first - if not found wp tries again with later rules 214 if ( preg_match( '/^\/?%postname%\/?$/', $struct ) ) 215 $wp_rewrite->use_verbose_page_rules = true; 216 217 // get rewrite rules without walking dirs 218 $post_type_rules_temp = $wp_rewrite->generate_rewrite_rules( $struct, EP_PERMALINK, false, true, false, false, true ); 219 foreach( $post_type_rules_temp as $regex => $query ) { 220 if ( preg_match( '/(&|\?)(cpage|attachment|p|name|pagename)=/', $query ) ) { 221 $post_type_query = ( count( $post_types ) < 2 ? '&post_type=' . $post_types[ 0 ] : '&post_type[]=' . join( '&post_type[]=', array_unique( $post_types ) ) ); 222 $rules[ $regex ] = $query . ( preg_match( '/(&|\?)(attachment|pagename)=/', $query ) ? '' : $post_type_query ); 223 } else 224 unset( $rules[ $regex ] ); 225 } 226 227 } 228 229 return $rules; 230 } 231 232 233 /** 234 * Generic version of standard permalink parsing function. Adds support for 235 * custom taxonomies as well as the standard %author% etc... 236 * 237 * @param string $post_link The post URL 238 * @param object $post The post object 239 * @param bool $leavename Passed to pre_post_link filter 240 * @param bool $sample Used in admin if generating an example permalink 241 * 242 * @return string The parsed permalink 243 */ 244 public function parse_permalinks( $post_link, WP_Post $post, $leavename, $sample = false ) { 245 246 // Make a stupid request and we'll do nothing. 247 if ( !post_type_exists( $post->post_type ) ) 248 return $post_link; 249 250 $rewritecode = array( 251 '%year%', 252 '%monthnum%', 253 '%day%', 254 '%hour%', 255 '%minute%', 256 '%second%', 257 $leavename? '' : '%postname%', 258 '%post_id%', 259 '%author%', 260 $leavename? '' : '%pagename%', 261 ); 262 263 $taxonomies = get_object_taxonomies( $post->post_type ); 264 265 foreach( $taxonomies as $taxonomy ) 266 $rewritecode[] = '%' . $taxonomy . '%'; 267 268 if ( is_object($post) && isset($post->filter) && 'sample' == $post->filter ) 269 $sample = true; 270 271 $post_type = get_post_type_object( $post->post_type ); 272 $permastruct = get_option( $post_type->name . '_permalink_structure' ); 273 274 // prefer option over default 275 if ( $permastruct && ! empty( $permastruct ) ) { 276 $permalink = $permastruct; 277 } elseif ( isset( $post_type->rewrite[ 'permastruct' ] ) && ! empty( $post_type->rewrite[ 'permastruct' ] ) ) { 278 $permalink = $post_type->rewrite[ 'permastruct' ]; 279 } else { 280 return $post_link; 281 } 282 283 $permalink = apply_filters('pre_post_link', $permalink, $post, $leavename); 284 285 if ( '' != $permalink && !in_array($post->post_status, array('draft', 'pending', 'auto-draft')) ) { 286 $unixtime = strtotime($post->post_date); 287 288 // add ability to use any taxonomies in post type permastruct 289 $replace_terms = array(); 290 foreach( $taxonomies as $taxonomy ) { 291 $term = ''; 292 $taxonomy_object = get_taxonomy( $taxonomy ); 293 if ( strpos($permalink, '%'. $taxonomy .'%') !== false ) { 294 $terms = get_the_terms( $post->ID, $taxonomy ); 295 if ( $terms ) { 296 usort($terms, '_usort_terms_by_ID'); // order by ID 297 $term = $terms[0]->slug; 298 if ( $taxonomy_object->hierarchical && $parent = $terms[0]->parent ) 299 $term = get_term_parents($parent, $taxonomy, false, '/', true) . $term; 300 } 301 // show default category in permalinks, without 302 // having to assign it explicitly 303 if ( empty( $term ) && $taxonomy == 'category' ) { 304 $default_category = get_category( get_option( 'default_category' ) ); 305 $term = is_wp_error( $default_category ) ? '' : $default_category->slug; 306 } 307 308 } 309 $replace_terms[ $taxonomy ] = $term; 310 } 311 312 $author = ''; 313 if ( strpos($permalink, '%author%') !== false ) { 314 $authordata = get_userdata($post->post_author); 315 $author = $authordata->user_nicename; 316 } 317 318 $date = explode(" ",date('Y m d H i s', $unixtime)); 319 $rewritereplace = 320 array( 321 $date[0], 322 $date[1], 323 $date[2], 324 $date[3], 325 $date[4], 326 $date[5], 327 $post->post_name, 328 $post->ID, 329 $author, 330 $post->post_name, 331 ); 332 foreach( $taxonomies as $taxonomy ) 333 $rewritereplace[] = $replace_terms[ $taxonomy ]; 334 $permalink = home_url( str_replace($rewritecode, $rewritereplace, $permalink) ); 335 $permalink = user_trailingslashit($permalink, 'single'); 336 } else { // if they're not using the fancy permalink option 337 $permalink = home_url('?p=' . $post->ID); 338 } 339 340 return $permalink; 341 } 41 if ( !class_exists( 'wp_permastructure' ) ) { 42 43 add_action( 'init', array( 'wp_permastructure', 'instance' ), 0 ); 44 45 class wp_permastructure { 46 47 public $settings_section = 'wp_permastructure'; 48 49 /** 50 * @var protect endpoints from being vaped if a category/tag slug is set 51 */ 52 protected $endpoints; 53 54 /** 55 * Reusable object instance. 56 * 57 * @type object 58 */ 59 protected static $instance = null; 60 61 /** 62 * Creates a new instance. Called on 'init'. 63 * May be used to access class methods from outside. 64 * 65 * @see __construct() 66 * @return void 67 */ 68 public static function instance() { 69 null === self:: $instance AND self:: $instance = new self; 70 71 return self:: $instance; 72 } 73 74 75 public function __construct() { 76 77 // Late init to protect endpoints 78 add_action( 'init', array( $this, 'late_init' ), 999 ); 79 80 // Settings fields on permalinks page 81 add_action( 'admin_init', array( $this, 'admin_init' ) ); 82 83 // add our new peramstructs to the rewrite rules 84 add_filter( 'post_rewrite_rules', array( $this, 'add_permastructs' ) ); 85 86 // parse the generated links - enable custom taxonomies for built in post links 87 add_filter( 'post_link', array( $this, 'parse_permalinks' ), 10, 3 ); 88 add_filter( 'post_type_link', array( $this, 'parse_permalinks' ), 10, 4 ); 89 90 // that's it! 91 92 } 93 94 95 public function late_init() { 96 global $wp_rewrite; 97 $this->endpoints = $wp_rewrite->endpoints; 98 } 99 100 101 /** 102 * Add the settings UI 103 */ 104 public function admin_init() { 105 106 add_settings_section( 107 $this->settings_section, 108 __( 'Custom post type permalink settings' ), 109 array( $this, 'settings_section' ), 110 'permalink' 111 ); 112 113 foreach ( get_post_types( array( '_builtin' => false, 'public' => true ), 'objects' ) as $type ) { 114 $id = $type->name . '_permalink_structure'; 115 116 register_setting( 'permalink', $id, array( $this, 'sanitize_permalink' ) ); 117 add_settings_field( 118 $id, 119 __( $type->label . ' permalink structure' ), 120 array( $this, 'permalink_field' ), 121 'permalink', 122 $this->settings_section, 123 array( 'id' => $id ) 124 ); 125 } 126 127 } 128 129 130 public function settings_section() { 131 132 } 133 134 135 public function permalink_field( $args ) { 136 echo '<input type="text" class="regular-text code" value="' . esc_attr( get_option( $args[ 'id' ] ) ) . '" id="' . $args[ 'id' ] . '" name="' . $args[ 'id' ] . '" />'; 137 } 138 139 140 /** 141 * Runs a simple sanitisation of the custom post type permalink structures 142 * and adds an error if no post ID or post name present 143 * 144 * @param string $permalink The permalink structure 145 * 146 * @return string Sanitised permalink structure 147 */ 148 public function sanitize_permalink( $permalink ) { 149 if ( !empty( $permalink ) && !preg_match( '/%(post_id|postname)%/', $permalink ) ) { 150 add_settings_error( 'permalink_structure', 10, __( 'Permalink structures must contain at least the <code>%post_id%</code> or <code>%postname%</code>.' ) ); 151 } 152 153 $filtered = wp_check_invalid_utf8( $permalink ); 154 155 if ( strpos( $filtered, '<' ) !== false ) { 156 $filtered = wp_pre_kses_less_than( $filtered ); 157 // This will strip extra whitespace for us. 158 $filtered = wp_strip_all_tags( $filtered, true ); 159 } 160 else { 161 $filtered = trim( preg_replace( '/[\r\n\t ]+/', ' ', $filtered ) ); 162 } 163 164 return preg_replace( '/[^a-zA-Z0-9\/\%_-]*/', '', $filtered ); 165 } 166 167 168 /** 169 * This function removes unnecessary rules and adds in the new rules 170 * 171 * @param array $rules The rewrite rules array for post permalinks 172 * 173 * @return array The modified rules array 174 */ 175 public function add_permastructs( $rules ) { 176 global $wp_rewrite; 177 178 // restore endpoints 179 if ( empty( $wp_rewrite->endpoints ) && !empty( $this->endpoints ) ) { 180 $wp_rewrite->endpoints = $this->endpoints; 181 } 182 183 $permastruct = $wp_rewrite->permalink_structure; 184 $permastructs = array( $permastruct => array( 'post' ) ); 185 186 // force page rewrite to bottom 187 $wp_rewrite->use_verbose_page_rules = false; 188 189 // get permastructs foreach custom post type and group any that use the same struct 190 foreach ( get_post_types( array( '_builtin' => false, 'public' => true ), 'objects' ) as $type ) { 191 // add/override the custom permalink structure if set in options 192 $post_type_permastruct = get_option( $type->name . '_permalink_structure' ); 193 if ( $post_type_permastruct && !empty( $post_type_permastruct ) ) { 194 if ( !is_array( $type->rewrite ) ) { 195 $type->rewrite = array(); 196 } 197 $type->rewrite[ 'permastruct' ] = $post_type_permastruct; 198 } 199 200 // check we have a custom permalink structure 201 if ( !is_array( $type->rewrite ) || !isset( $type->rewrite[ 'permastruct' ] ) ) { 202 continue; 203 } 204 205 // remove default struct rules 206 add_filter( $type->name . '_rewrite_rules', function( $rules ) { 207 return array(); 208 }, 11 ); 209 210 if ( !isset( $permastructs[ $type->rewrite[ 'permastruct' ] ] ) ) { 211 $permastructs[ $type->rewrite[ 'permastruct' ] ] = array(); 212 } 213 214 $permastructs[ $type->rewrite[ 'permastruct' ] ][] = $type->name; 215 } 216 217 $rules = array(); 218 219 // add our permastructs scoped to the post types - overwriting any keys that already exist 220 foreach ( $permastructs as $struct => $post_types ) { 221 222 // if a struct is %postname% only then we need page rules first - if not found wp tries again with later rules 223 if ( preg_match( '/^\/?%postname%\/?$/', $struct ) ) { 224 $wp_rewrite->use_verbose_page_rules = true; 225 } 226 227 // get rewrite rules without walking dirs 228 $post_type_rules_temp = $wp_rewrite->generate_rewrite_rules( $struct, EP_PERMALINK, false, true, false, false, true ); 229 foreach ( $post_type_rules_temp as $regex => $query ) { 230 if ( preg_match( '/(&|\?)(cpage|attachment|p|name|pagename)=/', $query ) ) { 231 $post_type_query = ( count( $post_types ) < 2 ? '&post_type=' . $post_types[ 0 ] : '&post_type[]=' . join( '&post_type[]=', array_unique( $post_types ) ) ); 232 $rules[ $regex ] = $query . ( preg_match( '/(&|\?)(attachment|pagename)=/', $query ) ? '' : $post_type_query ); 233 } 234 else { 235 unset( $rules[ $regex ] ); 236 } 237 } 238 239 } 240 241 return $rules; 242 } 243 244 245 /** 246 * Generic version of standard permalink parsing function. Adds support for 247 * custom taxonomies as well as the standard %author% etc... 248 * 249 * @param string $post_link The post URL 250 * @param WP_Post $post The post object 251 * @param bool $leavename Passed to pre_post_link filter 252 * @param bool $sample Used in admin if generating an example permalink 253 * 254 * @return string The parsed permalink 255 */ 256 public function parse_permalinks( $post_link, $post, $leavename, $sample = false ) { 257 // Yoast Sitemap plug-in doesn't pass a WP_Post object causing a fatal, so we'll check for it and return. 258 if ( !is_a( $post, 'WP_Post' ) ) { 259 return $post_link; 260 } 261 262 // Make a stupid request and we'll do nothing. 263 if ( !post_type_exists( $post->post_type ) ) { 264 return $post_link; 265 } 266 267 $rewritecode = array( 268 '%year%', 269 '%monthnum%', 270 '%day%', 271 '%hour%', 272 '%minute%', 273 '%second%', 274 $leavename ? '' : '%postname%', 275 '%post_id%', 276 '%author%', 277 $leavename ? '' : '%pagename%', 278 ); 279 280 $taxonomies = get_object_taxonomies( $post->post_type ); 281 282 foreach ( $taxonomies as $taxonomy ) { 283 $rewritecode[] = '%' . $taxonomy . '%'; 284 } 285 286 if ( is_object( $post ) && isset( $post->filter ) && 'sample' == $post->filter ) { 287 $sample = true; 288 } 289 290 $post_type = get_post_type_object( $post->post_type ); 291 $permastruct = get_option( $post_type->name . '_permalink_structure' ); 292 293 // prefer option over default 294 if ( $permastruct && !empty( $permastruct ) ) { 295 $permalink = $permastruct; 296 } 297 elseif ( isset( $post_type->rewrite[ 'permastruct' ] ) && !empty( $post_type->rewrite[ 'permastruct' ] ) ) { 298 $permalink = $post_type->rewrite[ 'permastruct' ]; 299 } 300 else { 301 return $post_link; 302 } 303 304 $permalink = apply_filters( 'pre_post_link', $permalink, $post, $leavename ); 305 306 if ( '' != $permalink && !in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) ) ) { 307 $unixtime = strtotime( $post->post_date ); 308 309 // add ability to use any taxonomies in post type permastruct 310 $replace_terms = array(); 311 foreach ( $taxonomies as $taxonomy ) { 312 $term = ''; 313 $taxonomy_object = get_taxonomy( $taxonomy ); 314 if ( strpos( $permalink, '%' . $taxonomy . '%' ) !== false ) { 315 $terms = get_the_terms( $post->ID, $taxonomy ); 316 if ( $terms ) { 317 usort( $terms, '_usort_terms_by_ID' ); // order by ID 318 $term = $terms[ 0 ]->slug; 319 if ( $taxonomy_object->hierarchical && $parent = $terms[ 0 ]->parent ) { 320 $term = get_term_parents( $parent, $taxonomy, false, '/', true ) . $term; 321 } 322 } 323 // show default category in permalinks, without 324 // having to assign it explicitly 325 if ( empty( $term ) && $taxonomy == 'category' ) { 326 $default_category = get_category( get_option( 'default_category' ) ); 327 $term = is_wp_error( $default_category ) ? '' : $default_category->slug; 328 } 329 330 } 331 $replace_terms[ $taxonomy ] = $term; 332 } 333 334 $author = ''; 335 if ( strpos( $permalink, '%author%' ) !== false ) { 336 $authordata = get_userdata( $post->post_author ); 337 $author = $authordata->user_nicename; 338 } 339 340 $date = explode( " ", date( 'Y m d H i s', $unixtime ) ); 341 $rewritereplace = 342 array( 343 $date[ 0 ], 344 $date[ 1 ], 345 $date[ 2 ], 346 $date[ 3 ], 347 $date[ 4 ], 348 $date[ 5 ], 349 $post->post_name, 350 $post->ID, 351 $author, 352 $post->post_name, 353 ); 354 foreach ( $taxonomies as $taxonomy ) { 355 $rewritereplace[] = $replace_terms[ $taxonomy ]; 356 } 357 $permalink = home_url( str_replace( $rewritecode, $rewritereplace, $permalink ) ); 358 $permalink = user_trailingslashit( $permalink, 'single' ); 359 } 360 else { // if they're not using the fancy permalink option 361 $permalink = home_url( '?p=' . $post->ID ); 362 } 363 364 return $permalink; 365 } 366 367 } 342 368 343 369 } 344 370 345 } 346 347 if ( ! function_exists( 'get_term_parents' ) ) { 348 349 /** 350 * Retrieve term parents with separator. 351 * 352 * @param int $id Term ID. 353 * @param string $taxonomy The taxonomy the term belongs to. 354 * @param bool $link Optional, default is false. Whether to format with link. 355 * @param string $separator Optional, default is '/'. How to separate categories. 356 * @param bool $nicename Optional, default is false. Whether to use nice name for display. 357 * @param array $visited Optional. Already linked to categories to prevent duplicates. 358 * @return string 359 */ 360 function get_term_parents( $id, $taxonomy, $link = false, $separator = '/', $nicename = false, $visited = array() ) { 361 $chain = ''; 362 $parent = get_term( $id, $taxonomy ); 363 if ( is_wp_error( $parent ) ) 364 return $parent; 365 366 if ( $nicename ) 367 $name = $parent->slug; 368 else 369 $name = $parent->cat_name; 370 371 if ( $parent->parent && ( $parent->parent != $parent->term_id ) && !in_array( $parent->parent, $visited ) ) { 372 $visited[] = $parent->parent; 373 $chain .= get_term_parents( $parent->parent, $taxonomy, $link, $separator, $nicename, $visited ); 374 } 375 376 if ( $link ) 377 $chain .= '<a href="' . get_term_link( $parent->term_id, $taxonomy ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $parent->name ) ) . '">'.$name.'</a>' . $separator; 378 else 379 $chain .= $name.$separator; 380 return $chain; 381 } 371 if ( !function_exists( 'get_term_parents' ) ) { 372 373 /** 374 * Retrieve term parents with separator. 375 * 376 * @param int $id Term ID. 377 * @param string $taxonomy The taxonomy the term belongs to. 378 * @param bool $link Optional, default is false. Whether to format with link. 379 * @param string $separator Optional, default is '/'. How to separate categories. 380 * @param bool $nicename Optional, default is false. Whether to use nice name for display. 381 * @param array $visited Optional. Already linked to categories to prevent duplicates. 382 * 383 * @return string 384 */ 385 function get_term_parents( 386 $id, 387 $taxonomy, 388 $link = false, 389 $separator = '/', 390 $nicename = false, 391 $visited = array() 392 ) { 393 $chain = ''; 394 $parent = get_term( $id, $taxonomy ); 395 if ( is_wp_error( $parent ) ) { 396 return $parent; 397 } 398 399 if ( $nicename ) { 400 $name = $parent->slug; 401 } 402 else { 403 $name = $parent->cat_name; 404 } 405 406 if ( $parent->parent && ( $parent->parent != $parent->term_id ) && !in_array( $parent->parent, $visited ) ) { 407 $visited[] = $parent->parent; 408 $chain .= get_term_parents( $parent->parent, $taxonomy, $link, $separator, $nicename, $visited ); 409 } 410 411 if ( $link ) { 412 $chain .= '<a href="' . get_term_link( $parent->term_id, $taxonomy ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $parent->name ) ) . '">' . $name . '</a>' . $separator; 413 } 414 else { 415 $chain .= $name . $separator; 416 } 417 418 return $chain; 419 } 382 420 383 421 } … … 387 425 * Patch for WP not saving settings registered to the permalinks page 388 426 */ 389 if ( ! function_exists( 'enable_permalinks_settings' ) ) { 390 391 // process the $_POST variable after all settings have been 392 // registered so they are whitelisted 393 add_action( 'admin_init', 'enable_permalinks_settings', 300 ); 394 395 function enable_permalinks_settings() { 396 global $new_whitelist_options; 397 398 // save hook for permalinks page 399 if ( isset( $_POST['permalink_structure'] ) || isset( $_POST['category_base'] ) ) { 400 check_admin_referer('update-permalink'); 401 402 $option_page = 'permalink'; 403 404 $capability = 'manage_options'; 405 $capability = apply_filters( "option_page_capability_{$option_page}", $capability ); 406 407 if ( !current_user_can( $capability ) ) 408 wp_die(__('Cheatin’ uh?')); 409 410 // get extra permalink options 411 $options = $new_whitelist_options[ $option_page ]; 412 413 if ( $options ) { 414 foreach ( $options as $option ) { 415 $option = trim($option); 416 $value = null; 417 if ( isset($_POST[$option]) ) 418 $value = $_POST[$option]; 419 if ( !is_array($value) ) 420 $value = trim($value); 421 $value = stripslashes_deep($value); 422 update_option( $option, $value ); 423 } 424 } 425 426 /** 427 * Handle settings errors 428 */ 429 set_transient('settings_errors', get_settings_errors(), 30); 430 } 431 } 427 if ( !function_exists( 'enable_permalinks_settings' ) ) { 428 429 // process the $_POST variable after all settings have been 430 // registered so they are whitelisted 431 add_action( 'admin_init', 'enable_permalinks_settings', 300 ); 432 433 function enable_permalinks_settings() { 434 global $new_whitelist_options; 435 436 // save hook for permalinks page 437 if ( isset( $_POST[ 'permalink_structure' ] ) || isset( $_POST[ 'category_base' ] ) ) { 438 check_admin_referer( 'update-permalink' ); 439 440 $option_page = 'permalink'; 441 442 $capability = 'manage_options'; 443 $capability = apply_filters( "option_page_capability_{$option_page}", $capability ); 444 445 if ( !current_user_can( $capability ) ) { 446 wp_die( __( 'Cheatin’ uh?' ) ); 447 } 448 449 // get extra permalink options 450 $options = $new_whitelist_options[ $option_page ]; 451 452 if ( $options ) { 453 foreach ( $options as $option ) { 454 $option = trim( $option ); 455 $value = null; 456 if ( isset( $_POST[ $option ] ) ) { 457 $value = $_POST[ $option ]; 458 } 459 if ( !is_array( $value ) ) { 460 $value = trim( $value ); 461 } 462 $value = stripslashes_deep( $value ); 463 update_option( $option, $value ); 464 } 465 } 466 467 /** 468 * Handle settings errors 469 */ 470 set_transient( 'settings_errors', get_settings_errors(), 30 ); 471 } 472 } 432 473 }
Note: See TracChangeset
for help on using the changeset viewer.