class-auxels-admin-assets.php
1 year ago
class-auxels-archive-menu-links.php
1 year ago
class-auxels-envato-elements.php
1 year ago
class-auxels-import-parser.php
3 years ago
class-auxels-import.php
3 years ago
class-auxels-search-post-type.php
6 months ago
class-auxels-wc-attribute-nav-menu.php
1 year ago
class-auxin-admin-dashboard.php
1 year ago
class-auxin-demo-importer.php
6 months ago
class-auxin-dependency-sorting.php
3 years ago
class-auxin-import.php
1 year ago
class-auxin-install.php
1 year ago
class-auxin-master-nav-menu-admin.php
1 year ago
class-auxin-page-template.php
1 year ago
class-auxin-permalink.php
1 year ago
class-auxin-plugin-requirements.php
3 years ago
class-auxin-post-type-base.php
1 year ago
class-auxin-svg-support-allowedattributes.php
7 years ago
class-auxin-svg-support-allowedtags.php
7 years ago
class-auxin-svg-support.php
1 year ago
class-auxin-walker-nav-menu-back.php
1 year ago
class-auxin-welcome-sections.php
1 year ago
class-auxin-welcome.php
6 months ago
class-auxin-whitelabel.php
3 years ago
class-auxin-widget-indie.php
1 year ago
class-auxin-widget-shortcode-map.php
11 months ago
class-auxin-widget.php
1 year ago
class-auxin-permalink.php
423 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Class to add permalink setting for post types of theme |
| 4 | * |
| 5 | * |
| 6 | * @package Auxin |
| 7 | * @license LICENSE.txt |
| 8 | * @author averta |
| 9 | * @link http://phlox.pro/ |
| 10 | * @copyright (c) 2010-2025 averta |
| 11 | */ |
| 12 | |
| 13 | // no direct access allowed |
| 14 | if ( ! defined('ABSPATH') ) { |
| 15 | die(); |
| 16 | } |
| 17 | |
| 18 | /** |
| 19 | * |
| 20 | */ |
| 21 | class Auxin_Permalink { |
| 22 | |
| 23 | public $prefix = "auxin_permalink"; |
| 24 | public $theme_name = "averta"; |
| 25 | public $theme_id = "averta"; |
| 26 | public $option_group = "permalink"; |
| 27 | public $default_post_types = array(); |
| 28 | |
| 29 | |
| 30 | public function __construct() { |
| 31 | |
| 32 | if( defined('THEME_NAME_I18N') ) $this->theme_name = THEME_NAME_I18N; |
| 33 | if( defined('THEME_ID' ) ) $this->theme_id = THEME_ID; |
| 34 | |
| 35 | $this->default_post_types = auxin_registered_post_types(true ); |
| 36 | } |
| 37 | |
| 38 | |
| 39 | /** |
| 40 | * Register and init with hooks |
| 41 | * |
| 42 | * @return void |
| 43 | */ |
| 44 | public function setup(){ |
| 45 | // setup hooks |
| 46 | add_action( 'admin_init', array( $this, 'extend_permalinks_page' ) ); |
| 47 | add_action( 'admin_init', array( $this, 'flush_rewrite_rules_queue') ); |
| 48 | add_action( 'load-options-permalink.php' , array( $this, 'on_permalink_page' ), 15 ); |
| 49 | } |
| 50 | |
| 51 | |
| 52 | /** |
| 53 | * Triggers on permalink setting page |
| 54 | * |
| 55 | * @return void |
| 56 | */ |
| 57 | public function on_permalink_page(){ |
| 58 | |
| 59 | if( ! $this->default_post_types ){ |
| 60 | return; |
| 61 | } |
| 62 | |
| 63 | // add auxin permalink fields section |
| 64 | $this->add_section(); |
| 65 | |
| 66 | foreach ( $this->default_post_types as $post_type ) { |
| 67 | |
| 68 | $this->add_update_hooks( $post_type ); |
| 69 | $this->add_posttype_fields( $post_type ); |
| 70 | |
| 71 | // store posted custom permalink slugs |
| 72 | if( isset( $_POST['submit'] ) && isset( $_POST['_wp_http_referer'] ) ){ |
| 73 | |
| 74 | if( strpos( $_POST['_wp_http_referer'],'options-permalink.php' ) !== false ) { |
| 75 | $this->store_permalink_options( $post_type ); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | |
| 83 | /** |
| 84 | * Store the permalink options for a post type |
| 85 | * |
| 86 | * @param string $posty_type The post type |
| 87 | * @return void |
| 88 | */ |
| 89 | private function store_permalink_options( $post_type ){ |
| 90 | |
| 91 | $single_option_name = $this->get_structure( array( 'post_type' => $post_type, 'page_type' => 'single' ) ); |
| 92 | |
| 93 | // get post type structure |
| 94 | $structure = trim( sanitize_text_field( $_POST[ $single_option_name ] ) ); |
| 95 | |
| 96 | // default permalink structure |
| 97 | if( ! $structure ) $structure = $post_type; |
| 98 | |
| 99 | $structure = trim( $structure, '/' ); |
| 100 | |
| 101 | set_theme_mod( $single_option_name, $structure ); |
| 102 | |
| 103 | // get post type object if available |
| 104 | $post_type_object = get_post_type_object( $post_type ); |
| 105 | |
| 106 | // if post type has archive enabled |
| 107 | if( ! empty( $post_type_object ) && $post_type_object->has_archive ){ |
| 108 | |
| 109 | $archive_option_name = $this->get_structure( array( 'post_type' => $post_type, 'page_type' => 'archive' ) ); |
| 110 | |
| 111 | // get post type structure |
| 112 | $structure = trim( sanitize_text_field( $_POST[$archive_option_name] ) ); |
| 113 | |
| 114 | // default permalink structure |
| 115 | if( ! $structure ) $structure = $post_type."/all"; |
| 116 | |
| 117 | $structure = trim( $structure, '/' ); |
| 118 | |
| 119 | set_theme_mod( $archive_option_name, $structure ); |
| 120 | } |
| 121 | |
| 122 | if( $taxonomies = get_object_taxonomies( $post_type, 'objects' ) ){ |
| 123 | |
| 124 | foreach ( $taxonomies as $tax => $tax_object ) { |
| 125 | |
| 126 | if( empty( $tax_object->rewrite ) || empty( $tax_object->rewrite['slug'] ) ){ |
| 127 | continue; |
| 128 | } |
| 129 | |
| 130 | $tax_option_name = $this->get_structure( array( 'post_type' => $post_type, 'page_type' => $tax ) ); |
| 131 | |
| 132 | // get post type structure |
| 133 | $structure = trim( sanitize_text_field( $_POST[ $tax_option_name ] ) ); |
| 134 | |
| 135 | // default permalink structure |
| 136 | if( ! $structure ) $structure = $tax_object->rewrite['slug']; |
| 137 | |
| 138 | $structure = trim( $structure, '/' ); |
| 139 | |
| 140 | set_theme_mod( $tax_option_name, $structure ); |
| 141 | } |
| 142 | |
| 143 | } |
| 144 | |
| 145 | } |
| 146 | |
| 147 | |
| 148 | /** |
| 149 | * Flushes the pending rewrite rules |
| 150 | * |
| 151 | */ |
| 152 | public function pending_rewrite_rules( $mod_value ){ |
| 153 | set_theme_mod( $this->prefix."_pending_rewrite_rules", 1 ); |
| 154 | |
| 155 | return $mod_value; |
| 156 | } |
| 157 | |
| 158 | |
| 159 | /** |
| 160 | * Flushes the queue of rewrite rules |
| 161 | * |
| 162 | * @return void |
| 163 | */ |
| 164 | public function flush_rewrite_rules_queue () { |
| 165 | if( get_theme_mod( $this->prefix."_pending_rewrite_rules", 1 ) ){ |
| 166 | flush_rewrite_rules(); |
| 167 | set_theme_mod( $this->prefix."_pending_rewrite_rules", 0 ); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | |
| 172 | /** |
| 173 | * Extends the permalink page by adding new fields for post types |
| 174 | * |
| 175 | * @return void |
| 176 | */ |
| 177 | public function extend_permalinks_page(){ |
| 178 | // Get enabled post types of theme |
| 179 | $this->set_current_post_types(); |
| 180 | // This method fires for just one time |
| 181 | $this->set_default_permalink_slugs(); |
| 182 | } |
| 183 | |
| 184 | |
| 185 | private function set_current_post_types(){ |
| 186 | $auxin_active_post_types = auxin_get_possible_post_types(true); |
| 187 | $this->default_post_types = array_keys( $auxin_active_post_types ); |
| 188 | } |
| 189 | |
| 190 | |
| 191 | /** |
| 192 | * Adds new section in permalink page |
| 193 | */ |
| 194 | private function add_section(){ |
| 195 | |
| 196 | add_settings_section( |
| 197 | 'auxin_posttypes_permalink_setting_section', |
| 198 | sprintf( '<hr /><br />'.__( '%s Permalink Setting', 'auxin-elements' ), $this->theme_name ), |
| 199 | array( $this, 'posttypes_permalink_section_callback_function' ), |
| 200 | $this->option_group |
| 201 | ); |
| 202 | } |
| 203 | |
| 204 | |
| 205 | |
| 206 | /** |
| 207 | * Sets the default permalink slugs |
| 208 | * |
| 209 | */ |
| 210 | private function set_default_permalink_slugs(){ |
| 211 | |
| 212 | if( get_theme_mod( $this->prefix.'_permalink_options_initialized', 0 ) ) |
| 213 | return; |
| 214 | |
| 215 | |
| 216 | foreach ( $this->default_post_types as $post_type ) { |
| 217 | |
| 218 | $single_option_name = $this->get_structure( array( 'post_type' => $post_type, 'page_type' => 'single' ) ); |
| 219 | |
| 220 | // get post type structure |
| 221 | $structure = get_theme_mod( $single_option_name, '' ); |
| 222 | |
| 223 | // default permalink structure |
| 224 | if( ! $structure ) { |
| 225 | $structure = ( strpos( $post_type, 'aux_' ) !== FALSE ) ? trim( $post_type, 'aux_' ) : $post_type; |
| 226 | } |
| 227 | |
| 228 | $structure = trim( $structure, '/' ); |
| 229 | set_theme_mod( $single_option_name, $structure ); |
| 230 | |
| 231 | // get post type object if available |
| 232 | $post_type_object = get_post_type_object( $post_type ); |
| 233 | |
| 234 | // if post type has archive enabled |
| 235 | if( ! empty( $post_type_object ) && $post_type_object->has_archive ){ |
| 236 | |
| 237 | $archive_option_name = $this->get_structure( array( 'post_type' => $post_type, 'page_type' => 'archive' ) ); |
| 238 | |
| 239 | // get post type structure |
| 240 | $structure = get_theme_mod( $archive_option_name, '' ); |
| 241 | |
| 242 | // default permalink structure |
| 243 | if( ! $structure ) { |
| 244 | $structure = ( strpos( $post_type, 'aux_' ) !== FALSE ) ? trim( $post_type, 'aux_' ) : $post_type; |
| 245 | $structure .= '/all'; |
| 246 | } |
| 247 | |
| 248 | $structure = trim( $structure, '/' ); |
| 249 | set_theme_mod( $archive_option_name, $structure ); |
| 250 | } |
| 251 | |
| 252 | if( $taxonomies = get_object_taxonomies( $post_type, 'objects' ) ){ |
| 253 | |
| 254 | foreach ( $taxonomies as $tax => $tax_object ) { |
| 255 | |
| 256 | if( empty( $tax_object->rewrite ) || empty( $tax_object->rewrite['slug'] ) ){ |
| 257 | continue; |
| 258 | } |
| 259 | |
| 260 | $tax_option_name = $this->get_structure( array( 'post_type' => $post_type, 'page_type' => $tax ) ); |
| 261 | |
| 262 | // get post type structure |
| 263 | $structure = get_theme_mod( $tax_option_name, '' ); |
| 264 | |
| 265 | // default permalink structure |
| 266 | if( ! $structure ) { |
| 267 | $structure = $tax_object->rewrite['slug']; |
| 268 | } |
| 269 | |
| 270 | $structure = trim( $structure, '/' ); |
| 271 | set_theme_mod( $tax_option_name, $structure ); |
| 272 | } |
| 273 | |
| 274 | } |
| 275 | |
| 276 | } |
| 277 | |
| 278 | set_theme_mod( $this->prefix.'_permalink_options_initialized', 1 ); |
| 279 | } |
| 280 | |
| 281 | |
| 282 | /** |
| 283 | * Flushes the queued rewrite rules |
| 284 | * |
| 285 | * @param string $post_type The post type name |
| 286 | */ |
| 287 | public function add_update_hooks( $post_type ){ |
| 288 | $hook_suffix = $this->get_structure( array( 'post_type' => $post_type, 'page_type' => 'single' ) ); |
| 289 | add_filter( 'pre_set_theme_mod_'. $hook_suffix , array( $this, 'pending_rewrite_rules' ), 10, 2 ); |
| 290 | |
| 291 | if( $this->post_type_has_archive( $post_type ) ){ |
| 292 | $hook_suffix = $this->get_structure( array( 'post_type' => $post_type, 'page_type' => 'archive' ) ); |
| 293 | add_filter( 'pre_set_theme_mod_'. $hook_suffix , array( $this, 'pending_rewrite_rules' ), 10, 2 ); |
| 294 | } |
| 295 | |
| 296 | if( $taxonomies = get_object_taxonomies( $post_type, 'objects' ) ){ |
| 297 | foreach ( $taxonomies as $tax => $tax_object ) { |
| 298 | if( empty( $tax_object->rewrite ) || empty( $tax_object->rewrite['slug'] ) ){ |
| 299 | continue; |
| 300 | } |
| 301 | $hook_suffix = $this->get_structure( array( 'post_type' => $post_type, 'page_type' => $tax ) ); |
| 302 | add_filter( 'pre_set_theme_mod_'. $hook_suffix , array( $this, 'pending_rewrite_rules' ), 10, 2 ); |
| 303 | } |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | |
| 308 | |
| 309 | private function add_posttype_fields( $post_type ){ |
| 310 | |
| 311 | $post_type_label = $post_type; |
| 312 | |
| 313 | if( $post_object = get_post_type_object( $post_type ) ){ |
| 314 | $post_type_label = $post_object->labels->singular_name; |
| 315 | } |
| 316 | |
| 317 | add_settings_field( 'auxin_'.$post_type.'_structure', |
| 318 | sprintf(__('%s base', 'auxin-elements' ), $post_type_label ), |
| 319 | array( $this, 'posttypes_permalink_fields_callback_function' ), |
| 320 | $this->option_group, |
| 321 | 'auxin_posttypes_permalink_setting_section', |
| 322 | array( 'post_type' => $post_type, 'page_type' => 'single' ) |
| 323 | ); |
| 324 | |
| 325 | register_setting( $this->option_group,'auxin_'.$post_type.'_structure' ); |
| 326 | |
| 327 | if( $this->post_type_has_archive( $post_type ) ){ |
| 328 | |
| 329 | add_settings_field( 'auxin_'.$post_type.'_archive_structure', |
| 330 | sprintf(__('%s archive base', 'auxin-elements' ), $post_type_label ), |
| 331 | array( $this, 'posttypes_permalink_fields_callback_function'), |
| 332 | $this->option_group, |
| 333 | 'auxin_posttypes_permalink_setting_section', |
| 334 | array( 'post_type' => $post_type, 'page_type' => 'archive' ) |
| 335 | ); |
| 336 | |
| 337 | register_setting( $this->option_group,'auxin_'.$post_type.'_archive_structure' ); |
| 338 | } |
| 339 | |
| 340 | if( $taxonomies = get_object_taxonomies( $post_type, 'objects' ) ){ |
| 341 | |
| 342 | foreach ( $taxonomies as $tax => $tax_object ) { |
| 343 | if( empty( $tax_object->rewrite ) || empty( $tax_object->rewrite['slug'] ) ){ |
| 344 | continue; |
| 345 | } |
| 346 | add_settings_field( |
| 347 | "auxin_{$post_type}_{$tax}_structure", |
| 348 | sprintf(__('%s base', 'auxin-elements' ), $tax_object->label ), |
| 349 | array( $this, 'posttypes_permalink_fields_callback_function'), |
| 350 | $this->option_group, |
| 351 | 'auxin_posttypes_permalink_setting_section', |
| 352 | array( 'post_type' => $post_type, 'page_type' => $tax ) |
| 353 | ); |
| 354 | |
| 355 | register_setting( $this->option_group, "auxin_{$post_type}_{$tax}_structure" ); |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | } |
| 360 | |
| 361 | /** |
| 362 | * Get permalink option structure |
| 363 | * |
| 364 | * @param array $args |
| 365 | * @return string The permalink structure |
| 366 | */ |
| 367 | private function get_structure( $args ){ |
| 368 | $defaults = array( |
| 369 | 'post_type' => '', |
| 370 | 'page_type' => 'single' // single, archive, $taxonomy |
| 371 | ); |
| 372 | $args = wp_parse_args( $args, $defaults ); |
| 373 | |
| 374 | if( empty( $args['post_type'] ) ){ |
| 375 | _doing_it_wrong( __FUNCTION__, 'Post type is required.', '2' ); |
| 376 | } |
| 377 | |
| 378 | if( empty( $args['suffix'] ) ){ |
| 379 | $args['suffix'] = 'single' == $args['page_type'] ? '' : '_'.$args['page_type']; |
| 380 | } |
| 381 | |
| 382 | return str_replace('-', '_', $this->prefix.'_'. $args['post_type']. $args['suffix'] .'_structure' ); |
| 383 | } |
| 384 | |
| 385 | /** |
| 386 | * Generates the section for permalink options |
| 387 | * |
| 388 | * @return void |
| 389 | */ |
| 390 | public function posttypes_permalink_section_callback_function(){ |
| 391 | esc_html_e('These settings control the permalinks used for theme\'s post types. These settings only apply when <strong>not using "default" permalink structure.</strong>.', 'auxin-elements' ); |
| 392 | echo "<br /><br />"; |
| 393 | } |
| 394 | |
| 395 | /** |
| 396 | * Generates the input field for permalink options |
| 397 | * |
| 398 | * @param array $args |
| 399 | * @return void |
| 400 | */ |
| 401 | public function posttypes_permalink_fields_callback_function( $args ) { |
| 402 | $output_suffix = $args['page_type'] !== 'single' ? '' : '<code>/' . esc_html__( 'sample-post', 'auxin-elements' ).'/</code>'; |
| 403 | |
| 404 | $option_id = $this->get_structure( $args ); |
| 405 | $val = get_theme_mod( $option_id ); |
| 406 | |
| 407 | printf( '<code>%1$s/</code><input id="%2$s" name="%2$s" type="text" value="%3$s" />%4$s', esc_url( home_url() ), esc_attr( $option_id ), esc_attr( $val ), $output_suffix ); |
| 408 | } |
| 409 | |
| 410 | /** |
| 411 | * Whether the post type has archive page or not |
| 412 | * |
| 413 | * @param string $post_type The post type name |
| 414 | * @return bool The post type has archive or not |
| 415 | */ |
| 416 | private function post_type_has_archive( $post_type ){ |
| 417 | $post_type_object = get_post_type_object( $post_type ); |
| 418 | return ! empty( $post_type_object ) && $post_type_object->has_archive; |
| 419 | } |
| 420 | |
| 421 | } |
| 422 | |
| 423 |