Admin.php
9 years ago
FlushRules.php
9 years ago
GetArchives.php
9 years ago
Option.php
9 years ago
Permalink.php
9 years ago
Rewrite.php
9 years ago
Setting.php
9 years ago
Rewrite.php
273 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | /** |
| 5 | * |
| 6 | * Add Rewrite Rules |
| 7 | * |
| 8 | * @package Custom_Post_Type_Permalinks |
| 9 | * @version 1.0.3 |
| 10 | * @since 0.9.4 |
| 11 | * */ |
| 12 | class CPTP_Module_Rewrite extends CPTP_Module { |
| 13 | |
| 14 | |
| 15 | public function add_hook() { |
| 16 | add_action( 'parse_request', array( $this, 'parse_request' ) ); |
| 17 | add_action( 'registered_post_type', array( $this, 'register_post_type_rules' ), 10, 2 ); |
| 18 | add_action( 'registered_taxonomy', array( $this, 'register_taxonomy_rules' ), 10, 3 ); |
| 19 | } |
| 20 | |
| 21 | |
| 22 | /** |
| 23 | * |
| 24 | * register_post_type_rules |
| 25 | * ** add rewrite tag for Custom Post Type. |
| 26 | * |
| 27 | * @version 1.1 |
| 28 | * @since 0.9 |
| 29 | * |
| 30 | * @param string $post_type |
| 31 | * @param object $args |
| 32 | */ |
| 33 | |
| 34 | public function register_post_type_rules( $post_type, $args ) { |
| 35 | |
| 36 | /** @var WP_Rewrite $wp_rewrite */ |
| 37 | global $wp_rewrite; |
| 38 | |
| 39 | if ( $args->_builtin or ! $args->publicly_queryable ) { |
| 40 | return; |
| 41 | } |
| 42 | |
| 43 | if ( false === $args->rewrite ) { |
| 44 | return; |
| 45 | } |
| 46 | |
| 47 | $permalink = CPTP_Util::get_permalink_structure( $post_type ); |
| 48 | |
| 49 | if ( ! $permalink ) { |
| 50 | $permalink = CPTP_DEFAULT_PERMALINK; |
| 51 | } |
| 52 | |
| 53 | $permalink = '%' . $post_type . '_slug%' . $permalink; |
| 54 | $permalink = str_replace( '%postname%', '%' . $post_type . '%', $permalink ); |
| 55 | |
| 56 | add_rewrite_tag( '%' . $post_type . '_slug%', '(' . $args->rewrite['slug'] . ')', 'post_type=' . $post_type . '&slug=' ); |
| 57 | |
| 58 | $taxonomies = CPTP_Util::get_taxonomies( true ); |
| 59 | foreach ( $taxonomies as $taxonomy => $objects ) : |
| 60 | $wp_rewrite->add_rewrite_tag( "%$taxonomy%", '(.+?)', "$taxonomy=" ); |
| 61 | endforeach; |
| 62 | |
| 63 | $rewrite_args = $args->rewrite; |
| 64 | if ( ! is_array( $rewrite_args ) ) { |
| 65 | $rewrite_args = array( 'with_front' => $args->rewrite ); |
| 66 | } |
| 67 | |
| 68 | $slug = $args->rewrite['slug']; |
| 69 | if ( $args->has_archive ) { |
| 70 | if ( is_string( $args->has_archive ) ) { |
| 71 | $slug = $args->has_archive; |
| 72 | }; |
| 73 | |
| 74 | if ( $args->rewrite['with_front'] ) { |
| 75 | $slug = substr( $wp_rewrite->front, 1 ) . $slug; |
| 76 | } |
| 77 | |
| 78 | $date_front = CPTP_Util::get_date_front( $post_type ); |
| 79 | |
| 80 | add_rewrite_rule( $slug . $date_front . '/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/feed/(feed|rdf|rss|rss2|atom)/?$', 'index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&feed=$matches[4]&post_type=' . $post_type, 'top' ); |
| 81 | add_rewrite_rule( $slug . $date_front . '/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/(feed|rdf|rss|rss2|atom)/?$', 'index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&feed=$matches[4]&post_type=' . $post_type, 'top' ); |
| 82 | add_rewrite_rule( $slug . $date_front . '/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/page/?([0-9]{1,})/?$', 'index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&paged=$matches[4]&post_type=' . $post_type, 'top' ); |
| 83 | add_rewrite_rule( $slug . $date_front . '/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/?$', 'index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&post_type=' . $post_type, 'top' ); |
| 84 | add_rewrite_rule( $slug . $date_front . '/([0-9]{4})/([0-9]{1,2})/feed/(feed|rdf|rss|rss2|atom)/?$', 'index.php?year=$matches[1]&monthnum=$matches[2]&feed=$matches[3]&post_type=' . $post_type, 'top' ); |
| 85 | add_rewrite_rule( $slug . $date_front . '/([0-9]{4})/([0-9]{1,2})/(feed|rdf|rss|rss2|atom)/?$', 'index.php?year=$matches[1]&monthnum=$matches[2]&feed=$matches[3]&post_type=' . $post_type, 'top' ); |
| 86 | add_rewrite_rule( $slug . $date_front . '/([0-9]{4})/([0-9]{1,2})/page/?([0-9]{1,})/?$', 'index.php?year=$matches[1]&monthnum=$matches[2]&paged=$matches[3]&post_type=' . $post_type, 'top' ); |
| 87 | add_rewrite_rule( $slug . $date_front . '/([0-9]{4})/([0-9]{1,2})/?$', 'index.php?year=$matches[1]&monthnum=$matches[2]&post_type=' . $post_type, 'top' ); |
| 88 | add_rewrite_rule( $slug . $date_front . '/([0-9]{4})/feed/(feed|rdf|rss|rss2|atom)/?$', 'index.php?year=$matches[1]&feed=$matches[2]&post_type=' . $post_type, 'top' ); |
| 89 | add_rewrite_rule( $slug . $date_front . '/([0-9]{4})/(feed|rdf|rss|rss2|atom)/?$', 'index.php?year=$matches[1]&feed=$matches[2]&post_type=' . $post_type, 'top' ); |
| 90 | add_rewrite_rule( $slug . $date_front . '/([0-9]{4})/page/?([0-9]{1,})/?$', 'index.php?year=$matches[1]&paged=$matches[2]&post_type=' . $post_type, 'top' ); |
| 91 | add_rewrite_rule( $slug . $date_front . '/([0-9]{4})/?$', 'index.php?year=$matches[1]&post_type=' . $post_type, 'top' ); |
| 92 | add_rewrite_rule( $slug . '/author/([^/]+)/page/?([0-9]{1,})/?$', 'index.php?author_name=$matches[1]&paged=$matches[2]&post_type=' . $post_type, 'top' ); |
| 93 | add_rewrite_rule( $slug . '/author/([^/]+)/?$', 'index.php?author_name=$matches[1]&post_type=' . $post_type, 'top' ); |
| 94 | |
| 95 | if ( in_array( 'category', $args->taxonomies ) ) { |
| 96 | |
| 97 | $category_base = get_option( 'category_base' ); |
| 98 | if ( ! $category_base ) { |
| 99 | $category_base = 'category'; |
| 100 | } |
| 101 | |
| 102 | add_rewrite_rule( $slug . '/' . $category_base . '/([^/]+)/page/?([0-9]{1,})/?$', 'index.php?category_name=$matches[1]&paged=$matches[2]&post_type=' . $post_type, 'top' ); |
| 103 | add_rewrite_rule( $slug . '/' . $category_base . '/([^/]+)/?$', 'index.php?category_name=$matches[1]&post_type=' . $post_type, 'top' ); |
| 104 | |
| 105 | } |
| 106 | |
| 107 | do_action( 'CPTP_registered_' . $post_type . '_rules', $args, $slug ); |
| 108 | } |
| 109 | |
| 110 | $rewrite_args['walk_dirs'] = false; |
| 111 | add_permastruct( $post_type, $permalink, $rewrite_args ); |
| 112 | |
| 113 | } |
| 114 | |
| 115 | |
| 116 | /** |
| 117 | * |
| 118 | * register_taxonomy_rules |
| 119 | * |
| 120 | * @param string $taxonomy |
| 121 | * @param array|string $object_type |
| 122 | * @param array|WP_Taxonomy $args |
| 123 | * |
| 124 | * @return void |
| 125 | */ |
| 126 | public function register_taxonomy_rules( $taxonomy, $object_type, $args ) { |
| 127 | global $wp_rewrite; |
| 128 | |
| 129 | /* for 4.7 */ |
| 130 | $args = (array) $args; |
| 131 | |
| 132 | if ( CPTP_Util::get_no_taxonomy_structure() ) { |
| 133 | return; |
| 134 | } |
| 135 | |
| 136 | if ( ! empty( $args['_builtin'] ) ) { |
| 137 | return; |
| 138 | } |
| 139 | |
| 140 | if ( false === $args['rewrite'] ) { |
| 141 | return; |
| 142 | } |
| 143 | |
| 144 | $post_types = $args['object_type']; |
| 145 | foreach ( $post_types as $post_type ) : |
| 146 | $post_type_obj = get_post_type_object( $post_type ); |
| 147 | if ( ! empty( $post_type_obj->rewrite['slug'] ) ) { |
| 148 | $slug = $post_type_obj->rewrite['slug']; |
| 149 | } else { |
| 150 | $slug = $post_type; |
| 151 | } |
| 152 | |
| 153 | if ( ! empty( $post_type_obj->has_archive ) && is_string( $post_type_obj->has_archive ) ) { |
| 154 | $slug = $post_type_obj->has_archive; |
| 155 | }; |
| 156 | |
| 157 | if ( ! empty( $post_type_obj->rewrite['with_front'] ) ) { |
| 158 | $slug = substr( $wp_rewrite->front, 1 ) . $slug; |
| 159 | } |
| 160 | |
| 161 | if ( 'category' == $taxonomy ) { |
| 162 | $taxonomy_slug = ( $cb = get_option( 'category_base' ) ) ? $cb : $taxonomy; |
| 163 | $taxonomy_key = 'category_name'; |
| 164 | } else { |
| 165 | // Edit by [Xiphe] |
| 166 | if ( isset( $args['rewrite']['slug'] ) ) { |
| 167 | $taxonomy_slug = $args['rewrite']['slug']; |
| 168 | } else { |
| 169 | $taxonomy_slug = $taxonomy; |
| 170 | } |
| 171 | // [Xiphe] stop |
| 172 | $taxonomy_key = $taxonomy; |
| 173 | } |
| 174 | |
| 175 | $rules = array( |
| 176 | // feed. |
| 177 | array( |
| 178 | 'regex' => '%s/(.+?)/feed/(feed|rdf|rss|rss2|atom)/?$', |
| 179 | 'redirect' => "index.php?{$taxonomy_key}=\$matches[1]&feed=\$matches[2]", |
| 180 | ), |
| 181 | array( |
| 182 | 'regex' => '%s/(.+?)/(feed|rdf|rss|rss2|atom)/?$', |
| 183 | 'redirect' => "index.php?{$taxonomy_key}=\$matches[1]&feed=\$matches[2]", |
| 184 | ), |
| 185 | // year |
| 186 | array( |
| 187 | 'regex' => '%s/(.+?)/date/([0-9]{4})/?$', |
| 188 | 'redirect' => "index.php?{$taxonomy_key}=\$matches[1]&year=\$matches[2]", |
| 189 | ), |
| 190 | array( |
| 191 | 'regex' => '%s/(.+?)/date/([0-9]{4})/page/?([0-9]{1,})/?$', |
| 192 | 'redirect' => "index.php?{$taxonomy_key}=\$matches[1]&year=\$matches[2]&paged=\$matches[3]", |
| 193 | ), |
| 194 | // monthnum |
| 195 | array( |
| 196 | 'regex' => '%s/(.+?)/date/([0-9]{4})/([0-9]{1,2})/?$', |
| 197 | 'redirect' => "index.php?{$taxonomy_key}=\$matches[1]&year=\$matches[2]&monthnum=\$matches[3]", |
| 198 | ), |
| 199 | array( |
| 200 | 'regex' => '%s/(.+?)/date/([0-9]{4})/([0-9]{1,2})/page/?([0-9]{1,})/?$', |
| 201 | 'redirect' => "index.php?{$taxonomy_key}=\$matches[1]&year=\$matches[2]&monthnum=\$matches[3]&paged=\$matches[4]", |
| 202 | ), |
| 203 | // day |
| 204 | array( |
| 205 | 'regex' => '%s/(.+?)/date/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/?$', |
| 206 | 'redirect' => "index.php?{$taxonomy_key}=\$matches[1]&year=\$matches[2]&monthnum=\$matches[3]&day=\$matches[4]", |
| 207 | ), |
| 208 | array( |
| 209 | 'regex' => '%s/(.+?)/date/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/page/?([0-9]{1,})/?$', |
| 210 | 'redirect' => "index.php?{$taxonomy_key}=\$matches[1]&year=\$matches[2]&monthnum=\$matches[3]&day=\$matches[4]&paged=\$matches[5]", |
| 211 | ), |
| 212 | // paging |
| 213 | array( |
| 214 | 'regex' => '%s/(.+?)/page/?([0-9]{1,})/?$', |
| 215 | 'redirect' => "index.php?{$taxonomy_key}=\$matches[1]&paged=\$matches[2]", |
| 216 | ), |
| 217 | // tax archive. |
| 218 | array( |
| 219 | 'regex' => '%s/(.+?)/?$', |
| 220 | 'redirect' => "index.php?{$taxonomy_key}=\$matches[1]", |
| 221 | ), |
| 222 | ); |
| 223 | |
| 224 | // no post_type slug. |
| 225 | foreach ( $rules as $rule ) { |
| 226 | $regex = sprintf( $rule['regex'], "{$taxonomy_slug}" ); |
| 227 | $redirect = $rule['redirect']; |
| 228 | add_rewrite_rule( $regex, $redirect, 'top' ); |
| 229 | } |
| 230 | |
| 231 | if ( get_option( 'add_post_type_for_tax' ) ) { |
| 232 | foreach ( $rules as $rule ) { |
| 233 | $regex = sprintf( $rule['regex'], "{$slug}/{$taxonomy_slug}" ); |
| 234 | $redirect = $rule['redirect'] . "&post_type={$post_type}"; |
| 235 | add_rewrite_rule( $regex, $redirect, 'top' ); |
| 236 | } |
| 237 | } else { |
| 238 | foreach ( $rules as $rule ) { |
| 239 | $regex = sprintf( $rule['regex'], "{$slug}/{$taxonomy_slug}" ); |
| 240 | $redirect = $rule['redirect']; |
| 241 | add_rewrite_rule( $regex, $redirect, 'top' ); |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | do_action( 'CPTP_registered_' . $taxonomy . '_rules', $object_type, $args, $taxonomy_slug ); |
| 246 | |
| 247 | endforeach; |
| 248 | } |
| 249 | |
| 250 | |
| 251 | /** |
| 252 | * |
| 253 | * Fix taxonomy = parent/child => taxonomy => child |
| 254 | * |
| 255 | * @since 0.9.3 |
| 256 | * |
| 257 | * @param WP $obj |
| 258 | */ |
| 259 | public function parse_request( $obj ) { |
| 260 | $taxes = CPTP_Util::get_taxonomies(); |
| 261 | foreach ( $taxes as $key => $tax ) { |
| 262 | if ( isset( $obj->query_vars[ $tax ] ) ) { |
| 263 | if ( false !== strpos( $obj->query_vars[ $tax ], '/' ) ) { |
| 264 | $query_vars = explode( '/', $obj->query_vars[ $tax ] ); |
| 265 | if ( is_array( $query_vars ) ) { |
| 266 | $obj->query_vars[ $tax ] = array_pop( $query_vars ); |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | } |
| 271 | } |
| 272 | } |
| 273 |