class-auxels-admin-assets.php
8 years ago
class-auxels-import-parser.php
8 years ago
class-auxels-import.php
8 years ago
class-auxin-admin-dashboard.php
8 years ago
class-auxin-dependency-sorting.php
8 years ago
class-auxin-import.php
8 years ago
class-auxin-install.php
8 years ago
class-auxin-master-nav-menu-admin.php
8 years ago
class-auxin-permalink.php
8 years ago
class-auxin-plugin-requirements.php
8 years ago
class-auxin-post-type-base.php
8 years ago
class-auxin-siteorigin-widget.php
8 years ago
class-auxin-walker-nav-menu-back.php
8 years ago
class-auxin-widget-indie.php
8 years ago
class-auxin-widget-shortcode-map.php
8 years ago
class-auxin-widget.php
8 years ago
class-auxin-permalink.php
269 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 |
| 9 | * @link http://averta.net/phlox/ |
| 10 | * @copyright (c) 2010-2017 |
| 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"; |
| 24 | public $theme_name = "averta"; |
| 25 | public $option_group = "permalink"; |
| 26 | public $default_post_types = array(); |
| 27 | |
| 28 | |
| 29 | function __construct() { |
| 30 | if( defined('THEME_ID' ) ) $this->prefix = THEME_ID; |
| 31 | if( defined('THEME_NAME') ) $this->theme_name = THEME_NAME; |
| 32 | |
| 33 | $this->default_post_types = auxin_registered_post_types(true ); |
| 34 | } |
| 35 | |
| 36 | |
| 37 | |
| 38 | public function setup(){ |
| 39 | // setup hooks |
| 40 | add_action( 'admin_init', array( $this, 'extend_permalinks_page' ) ); |
| 41 | add_action( 'admin_init', array( $this, 'flush_rewrite_rules_queue') ); |
| 42 | add_action( 'load-options-permalink.php' , array( $this, 'on_permalink_page' ) ); |
| 43 | } |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | public function on_permalink_page(){ |
| 49 | |
| 50 | if( ! $this->default_post_types ){ |
| 51 | return; |
| 52 | } |
| 53 | |
| 54 | $this->add_section(); |
| 55 | |
| 56 | foreach ( $this->default_post_types as $post_type ) { |
| 57 | $this->add_update_hooks( $post_type ); |
| 58 | $this->add_posttype_fields( $post_type ); |
| 59 | } |
| 60 | |
| 61 | // store posted custom permalink slugs |
| 62 | foreach ( $this->default_post_types as $post_type ) { |
| 63 | |
| 64 | if( isset( $_POST['submit'] ) && isset( $_POST['_wp_http_referer'] ) ){ |
| 65 | |
| 66 | if( strpos( $_POST['_wp_http_referer'],'options-permalink.php' ) !== FALSE ) { |
| 67 | |
| 68 | $single_option_name = $this->prefix.'_'.$post_type.'_structure'; |
| 69 | |
| 70 | // get post type structure |
| 71 | $structure = trim( esc_attr( $_POST[ $single_option_name ] ) ); |
| 72 | |
| 73 | // default permalink structure |
| 74 | if( ! $structure ) $structure = $post_type; |
| 75 | |
| 76 | $structure = trim( $structure, '/' ); |
| 77 | |
| 78 | set_theme_mod( $single_option_name, $structure ); |
| 79 | |
| 80 | // get post type object if available |
| 81 | $post_type_object = get_post_type_object( $post_type ); |
| 82 | |
| 83 | // if post type has archive enabled |
| 84 | if( ! empty( $post_type_object ) && $post_type_object->has_archive ){ |
| 85 | |
| 86 | $archive_option_name = $this->prefix.'_'.$post_type.'_archive_structure'; |
| 87 | |
| 88 | // get post type structure |
| 89 | $structure = trim( esc_attr( $_POST[$archive_option_name] ) ); |
| 90 | |
| 91 | // default permalink structure |
| 92 | if( ! $structure ) $structure = $post_type."/all"; |
| 93 | |
| 94 | $structure = trim( $structure, '/' ); |
| 95 | |
| 96 | set_theme_mod( $archive_option_name, $structure ); |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | public function pending_rewrite_rules(){ |
| 108 | set_theme_mod( $this->prefix."_pending_rewrite_rules", 1 ); |
| 109 | } |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | public function flush_rewrite_rules_queue () { |
| 115 | |
| 116 | if( get_theme_mod( $this->prefix."_pending_rewrite_rules", 1 ) ){ |
| 117 | flush_rewrite_rules(); |
| 118 | set_theme_mod( $this->prefix."_pending_rewrite_rules", 0 ); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | public function extend_permalinks_page(){ |
| 126 | // Get enabled post types of theme |
| 127 | $this->set_current_post_types(); |
| 128 | // This method fires for just one time |
| 129 | $this->set_default_permalink_slugs(); |
| 130 | } |
| 131 | |
| 132 | |
| 133 | |
| 134 | private function set_current_post_types(){ |
| 135 | $auxin_active_post_types = auxin_get_possible_post_types(true); |
| 136 | $this->default_post_types = array_keys( $auxin_active_post_types ); |
| 137 | } |
| 138 | |
| 139 | |
| 140 | |
| 141 | private function add_section(){ |
| 142 | |
| 143 | add_settings_section( |
| 144 | 'auxin_posttypes_permalink_setting_section', |
| 145 | sprintf( '<hr /><br />'.__( '%s Permalink Setting', 'auxin-elements' ), $this->theme_name ), |
| 146 | array( $this, 'posttypes_permalink_section_callback_function' ), |
| 147 | $this->option_group |
| 148 | ); |
| 149 | } |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | private function set_default_permalink_slugs(){ |
| 155 | |
| 156 | if( get_theme_mod( $this->prefix.'_permalink_options_initialized', 0 ) ) |
| 157 | return; |
| 158 | |
| 159 | |
| 160 | foreach ( $this->default_post_types as $post_type ) { |
| 161 | |
| 162 | $single_option_name = $this->prefix.'_'.$post_type.'_structure'; |
| 163 | |
| 164 | // get post type structure |
| 165 | $structure = get_theme_mod( $single_option_name, '' ); |
| 166 | |
| 167 | // default permalink structure |
| 168 | if( ! $structure ) { |
| 169 | $structure = ( strpos( $post_type, 'axi_' ) !== FALSE ) ? trim( $post_type, 'axi_' ) : $post_type; |
| 170 | } |
| 171 | |
| 172 | $structure = trim( $structure, '/' ); |
| 173 | set_theme_mod( $single_option_name, $structure ); |
| 174 | |
| 175 | // get post type object if available |
| 176 | $post_type_object = get_post_type_object( $post_type ); |
| 177 | |
| 178 | // if post type has archive enabled |
| 179 | if( ! empty( $post_type_object ) && $post_type_object->has_archive ){ |
| 180 | |
| 181 | $archive_option_name = $this->prefix.'_'.$post_type.'_archive_structure'; |
| 182 | |
| 183 | // get post type structure |
| 184 | $structure = get_theme_mod( $archive_option_name, '' ); |
| 185 | |
| 186 | // default permalink structure |
| 187 | if( ! $structure ) { |
| 188 | $structure = ( strpos( $post_type, 'axi_' ) !== FALSE ) ? trim( $post_type, 'axi_' ) : $post_type; |
| 189 | $structure .= '/all'; |
| 190 | } |
| 191 | |
| 192 | $structure = trim( $structure, '/' ); |
| 193 | set_theme_mod( $archive_option_name, $structure ); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | set_theme_mod( $this->prefix.'_permalink_options_initialized', 1 ); |
| 198 | } |
| 199 | |
| 200 | |
| 201 | |
| 202 | public function add_update_hooks( $post_type ){ |
| 203 | add_action( 'update_option_'.$this->prefix.'_'.$post_type.'_structure' , array( $this, 'pending_rewrite_rules' ), 10, 2 ); |
| 204 | |
| 205 | if( $this->post_type_has_archive( $post_type ) ){ |
| 206 | add_action( 'update_option_'.$this->prefix.'_'.$post_type.'_archive_structure' , array( $this, 'pending_rewrite_rules' ), 10, 2 ); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 213 | private function add_posttype_fields( $post_type ){ |
| 214 | |
| 215 | add_settings_field( 'auxin_'.$post_type.'_structure', |
| 216 | sprintf(__('Setting for <strong>%s single</strong> page', 'auxin-elements' ), $post_type ), |
| 217 | array( $this, 'posttypes_permalink_fields_callback_function' ), |
| 218 | $this->option_group, |
| 219 | 'auxin_posttypes_permalink_setting_section', |
| 220 | array( 'post_type' => $post_type, 'is_archive' => 'no' ) |
| 221 | ); |
| 222 | |
| 223 | register_setting( $this->option_group,'auxin_'.$post_type.'_structure' ); |
| 224 | |
| 225 | if( $this->post_type_has_archive( $post_type ) ){ |
| 226 | |
| 227 | add_settings_field( 'auxin_'.$post_type.'_archive_structure', |
| 228 | sprintf(__('Setting for <strong>%s archive</strong> page ', 'auxin-elements' ), $post_type), |
| 229 | array($this, 'posttypes_permalink_fields_callback_function'), |
| 230 | $this->option_group, |
| 231 | 'auxin_posttypes_permalink_setting_section', |
| 232 | array('post_type' => $post_type, 'is_archive' => 'yes') |
| 233 | ); |
| 234 | |
| 235 | register_setting( $this->option_group,'auxin_'.$post_type.'_archive_structure' ); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | |
| 240 | |
| 241 | public function posttypes_permalink_section_callback_function(){ |
| 242 | _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' ); |
| 243 | echo "<br /><br />"; |
| 244 | } |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | public function posttypes_permalink_fields_callback_function( $options ) { |
| 250 | |
| 251 | $post_type_obj = get_post_type_object( $options['post_type'] ); |
| 252 | |
| 253 | $suffix = $options['is_archive'] == 'yes' ? '_archive_structure' : '_structure'; |
| 254 | $output_suffix = $options['is_archive'] == 'yes' ? '' : '<code>/' . __( 'sample-post', 'auxin-elements' ).'/</code>'; |
| 255 | |
| 256 | $option_id = $this->prefix.'_'.$options['post_type'].$suffix; |
| 257 | $val = get_theme_mod( $option_id ); |
| 258 | |
| 259 | printf( '<code>%1$s/</code><input id="%2$s" name="%2$s" type="text" value="%3$s" />%4$s', home_url(), $option_id, $val, $output_suffix ); |
| 260 | } |
| 261 | |
| 262 | |
| 263 | private function post_type_has_archive( $post_type ){ |
| 264 | $post_type_object = get_post_type_object( $post_type ); |
| 265 | return ! empty( $post_type_object ) && $post_type_object->has_archive; |
| 266 | } |
| 267 | |
| 268 | } |
| 269 |