| @@ -1,7 +1,7 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -add_action( 'init', 'bogo_add_rewrite_tags', 10, 0 ); | |
| 3 | +add_action( 'init', 'bogo_add_rewrite_tags' ); | |
| 4 | 4 | |
| 5 | 5 | function bogo_add_rewrite_tags() { |
| 6 | 6 | $regex = bogo_get_lang_regex(); |
| 7 | 7 | |
| @@ -12,252 +12,331 @@ | ||
| 12 | 12 | add_rewrite_tag( '%lang%', $regex, 'lang=' ); |
| 13 | 13 | |
| 14 | 14 | $old_regex = bogo_get_prop( 'lang_rewrite_regex' ); |
| 15 | 15 | |
| 16 | - if ( $regex !== $old_regex ) { | |
| 16 | + if ( $regex != $old_regex ) { | |
| 17 | 17 | bogo_set_prop( 'lang_rewrite_regex', $regex ); |
| 18 | 18 | flush_rewrite_rules(); |
| 19 | 19 | } |
| 20 | 20 | } |
| 21 | 21 | |
| 22 | +add_filter( 'root_rewrite_rules', 'bogo_root_rewrite_rules' ); | |
| 22 | 23 | |
| 23 | -add_filter( 'rewrite_rules_array', 'bogo_rewrite_rules_array', 10, 1 ); | |
| 24 | +function bogo_root_rewrite_rules( $root_rewrite ) { | |
| 25 | + global $wp_rewrite; | |
| 24 | 26 | |
| 27 | + $permastruct = trailingslashit( $wp_rewrite->root ) . '%lang%/'; | |
| 28 | + | |
| 29 | + $extra = bogo_generate_rewrite_rules( $permastruct, array( | |
| 30 | + 'ep_mask' => EP_ROOT ) ); | |
| 31 | + | |
| 32 | + return array_merge( $extra, $root_rewrite ); | |
| 33 | +} | |
| 34 | + | |
| 35 | +add_filter( 'post_rewrite_rules', 'bogo_post_rewrite_rules' ); | |
| 36 | + | |
| 37 | +function bogo_post_rewrite_rules( $post_rewrite ) { | |
| 38 | + global $wp_rewrite; | |
| 39 | + | |
| 40 | + $permastruct = $wp_rewrite->permalink_structure; | |
| 41 | + | |
| 42 | + // from wp-admin/includes/misc.php | |
| 43 | + $got_rewrite = apply_filters( 'got_rewrite', | |
| 44 | + apache_mod_loaded( 'mod_rewrite', true ) ); | |
| 45 | + $got_url_rewrite = apply_filters( 'got_url_rewrite', | |
| 46 | + $got_rewrite || $GLOBALS['is_nginx'] || iis7_supports_permalinks() ); | |
| 47 | + | |
| 48 | + if ( ! $got_url_rewrite ) { | |
| 49 | + $permastruct = preg_replace( | |
| 50 | + '#^/index\.php#', '/index.php/%lang%', $permastruct ); | |
| 51 | + } elseif ( is_multisite() && ! is_subdomain_install() && is_main_site() ) { | |
| 52 | + $permastruct = preg_replace( | |
| 53 | + '#^/blog#', '/%lang%/blog', $permastruct ); | |
| 54 | + } else { | |
| 55 | + $permastruct = preg_replace( | |
| 56 | + '#^/#', '/%lang%/', $permastruct ); | |
| 57 | + } | |
| 58 | + | |
| 59 | + $extra = bogo_generate_rewrite_rules( $permastruct, array( | |
| 60 | + 'ep_mask' => EP_PERMALINK, | |
| 61 | + 'paged' => false ) ); | |
| 62 | + | |
| 63 | + return array_merge( $extra, $post_rewrite ); | |
| 64 | +} | |
| 65 | + | |
| 66 | +add_filter( 'date_rewrite_rules', 'bogo_date_rewrite_rules' ); | |
| 67 | + | |
| 68 | +function bogo_date_rewrite_rules( $date_rewrite ) { | |
| 69 | + global $wp_rewrite; | |
| 70 | + | |
| 71 | + $permastruct = $wp_rewrite->get_date_permastruct(); | |
| 72 | + | |
| 73 | + $permastruct = preg_replace( | |
| 74 | + '#^' . $wp_rewrite->front . '#', | |
| 75 | + '/%lang%' . $wp_rewrite->front, | |
| 76 | + $permastruct ); | |
| 77 | + | |
| 78 | + $extra = bogo_generate_rewrite_rules( $permastruct, array( | |
| 79 | + 'ep_mask' => EP_DATE ) ); | |
| 80 | + | |
| 81 | + return array_merge( $extra, $date_rewrite ); | |
| 82 | +} | |
| 83 | + | |
| 84 | +add_filter( 'comments_rewrite_rules', 'bogo_comments_rewrite_rules' ); | |
| 85 | + | |
| 86 | +function bogo_comments_rewrite_rules( $comments_rewrite ) { | |
| 87 | + global $wp_rewrite; | |
| 88 | + | |
| 89 | + $permastruct = trailingslashit( $wp_rewrite->root ) . '%lang%/' . $wp_rewrite->comments_base; | |
| 90 | + | |
| 91 | + $extra = bogo_generate_rewrite_rules( $permastruct, array( | |
| 92 | + 'ep_mask' => EP_COMMENTS, | |
| 93 | + 'forcomments' => true, | |
| 94 | + 'walk_dirs' => false ) ); | |
| 95 | + | |
| 96 | + return array_merge( $extra, $comments_rewrite ); | |
| 97 | +} | |
| 98 | + | |
| 99 | +add_filter( 'search_rewrite_rules', 'bogo_search_rewrite_rules' ); | |
| 100 | + | |
| 101 | +function bogo_search_rewrite_rules( $search_rewrite ) { | |
| 102 | + global $wp_rewrite; | |
| 103 | + | |
| 104 | + $permastruct = trailingslashit( $wp_rewrite->root ) . '%lang%/' | |
| 105 | + . $wp_rewrite->search_base . '/%search%'; | |
| 106 | + | |
| 107 | + $extra = bogo_generate_rewrite_rules( $permastruct, array( | |
| 108 | + 'ep_mask' => EP_SEARCH ) ); | |
| 109 | + | |
| 110 | + return array_merge( $extra, $search_rewrite ); | |
| 111 | +} | |
| 112 | + | |
| 113 | +add_filter( 'author_rewrite_rules', 'bogo_author_rewrite_rules' ); | |
| 114 | + | |
| 115 | +function bogo_author_rewrite_rules( $author_rewrite ) { | |
| 116 | + global $wp_rewrite; | |
| 117 | + | |
| 118 | + $permastruct = $wp_rewrite->get_author_permastruct(); | |
| 119 | + | |
| 120 | + $permastruct = preg_replace( | |
| 121 | + '#^' . $wp_rewrite->front . '#', | |
| 122 | + '/%lang%' . $wp_rewrite->front, | |
| 123 | + $permastruct ); | |
| 124 | + | |
| 125 | + $extra = bogo_generate_rewrite_rules( $permastruct, array( | |
| 126 | + 'ep_mask' => EP_AUTHORS ) ); | |
| 127 | + | |
| 128 | + return array_merge( $extra, $author_rewrite ); | |
| 129 | +} | |
| 130 | + | |
| 131 | +add_filter( 'page_rewrite_rules', 'bogo_page_rewrite_rules' ); | |
| 132 | + | |
| 133 | +function bogo_page_rewrite_rules( $page_rewrite ) { | |
| 134 | + global $wp_rewrite; | |
| 135 | + | |
| 136 | + $wp_rewrite->add_rewrite_tag( '%pagename%', '(.?.+?)', 'pagename=' ); | |
| 137 | + $permastruct = trailingslashit( $wp_rewrite->root ) . '%lang%/%pagename%'; | |
| 138 | + | |
| 139 | + $extra = bogo_generate_rewrite_rules( $permastruct, array( | |
| 140 | + 'ep_mask' => EP_PAGES, | |
| 141 | + 'walk_dirs' => false ) ); | |
| 142 | + | |
| 143 | + return array_merge( $extra, $page_rewrite ); | |
| 144 | +} | |
| 145 | + | |
| 146 | +add_filter( 'category_rewrite_rules', 'bogo_category_rewrite_rules' ); | |
| 147 | + | |
| 148 | +function bogo_category_rewrite_rules( $category_rewrite ) { | |
| 149 | + return bogo_taxonomy_rewrite_rules( $category_rewrite, 'category', EP_CATEGORIES ); | |
| 150 | +} | |
| 151 | + | |
| 152 | +add_filter( 'post_tag_rewrite_rules', 'bogo_post_tag_rewrite_rules' ); | |
| 153 | + | |
| 154 | +function bogo_post_tag_rewrite_rules( $post_tag_rewrite ) { | |
| 155 | + return bogo_taxonomy_rewrite_rules( $post_tag_rewrite, 'post_tag', EP_TAGS ); | |
| 156 | +} | |
| 157 | + | |
| 158 | +add_filter( 'post_format_rewrite_rules', 'bogo_post_format_rewrite_rules' ); | |
| 159 | + | |
| 160 | +function bogo_post_format_rewrite_rules( $post_format_rewrite ) { | |
| 161 | + return bogo_taxonomy_rewrite_rules( $post_format_rewrite, 'post_format' ); | |
| 162 | +} | |
| 163 | + | |
| 164 | +function bogo_taxonomy_rewrite_rules( $taxonomy_rewrite, $taxonomy, $ep_mask = EP_NONE ) { | |
| 165 | + global $wp_rewrite; | |
| 166 | + | |
| 167 | + $permastruct = $wp_rewrite->get_extra_permastruct( $taxonomy ); | |
| 168 | + | |
| 169 | + $permastruct = preg_replace( | |
| 170 | + '#^' . $wp_rewrite->front . '#', | |
| 171 | + '/%lang%' . $wp_rewrite->front, | |
| 172 | + $permastruct ); | |
| 173 | + | |
| 174 | + $extra = bogo_generate_rewrite_rules( $permastruct, array( | |
| 175 | + 'ep_mask' => $ep_mask ) ); | |
| 176 | + | |
| 177 | + return array_merge( $extra, $taxonomy_rewrite ); | |
| 178 | +} | |
| 179 | + | |
| 180 | +add_filter( 'rewrite_rules_array', 'bogo_rewrite_rules_array' ); | |
| 181 | + | |
| 25 | 182 | function bogo_rewrite_rules_array( $rules ) { |
| 26 | 183 | global $wp_rewrite; |
| 27 | 184 | |
| 28 | 185 | $lang_regex = bogo_get_lang_regex(); |
| 29 | 186 | |
| 30 | - $extra_rules = array(); | |
| 187 | + // REST rewrite rules | |
| 188 | + $rest_url_prefix = rest_get_url_prefix(); | |
| 31 | 189 | |
| 32 | - if ( $rest_url_prefix = rest_get_url_prefix() ) { | |
| 33 | - $extra_rules += array( | |
| 34 | - "{$lang_regex}/{$rest_url_prefix}/?$" | |
| 35 | - => 'index.php?lang=$matches[1]&rest_route=/', | |
| 36 | - "{$lang_regex}/{$rest_url_prefix}/(.*)?" | |
| 37 | - => 'index.php?lang=$matches[1]&rest_route=/$matches[2]', | |
| 38 | - "{$wp_rewrite->index}/{$lang_regex}/{$rest_url_prefix}/?$" | |
| 39 | - => 'index.php?lang=$matches[1]&rest_route=/', | |
| 40 | - "{$wp_rewrite->index}/{$lang_regex}/{$rest_url_prefix}/(.*)?" | |
| 41 | - => 'index.php?lang=$matches[1]&rest_route=/$matches[2]', | |
| 42 | - ); | |
| 190 | + $extra_rules = array( | |
| 191 | + "^{$lang_regex}/{$rest_url_prefix}/?$" | |
| 192 | + => 'index.php?lang=$matches[1]&rest_route=/', | |
| 193 | + "^{$lang_regex}/{$rest_url_prefix}/(.*)?" | |
| 194 | + => 'index.php?lang=$matches[1]&rest_route=/$matches[2]' ); | |
| 195 | + | |
| 196 | + $rules = $extra_rules + $rules; | |
| 197 | + | |
| 198 | + $post_types = array_diff( | |
| 199 | + (array) bogo_localizable_post_types(), | |
| 200 | + get_post_types( array( '_builtin' => true ) ) ); | |
| 201 | + | |
| 202 | + if ( empty( $post_types ) ) { | |
| 203 | + return $rules; | |
| 43 | 204 | } |
| 44 | 205 | |
| 45 | - $localizable_post_types = bogo_localizable_post_types(); | |
| 206 | + foreach ( $post_types as $post_type ) { | |
| 207 | + if ( ! $post_type_obj = get_post_type_object( $post_type ) ) | |
| 208 | + continue; | |
| 46 | 209 | |
| 47 | - foreach ( $localizable_post_types as $post_type ) { | |
| 48 | - if ( | |
| 49 | - ! $post_type_obj = get_post_type_object( $post_type ) or | |
| 50 | - false === $post_type_obj->rewrite | |
| 51 | - ) { | |
| 210 | + if ( false === $post_type_obj->rewrite ) | |
| 52 | 211 | continue; |
| 212 | + | |
| 213 | + $permastruct = $wp_rewrite->get_extra_permastruct( $post_type ); | |
| 214 | + | |
| 215 | + if ( $post_type_obj->rewrite['with_front'] ) { | |
| 216 | + $permastruct = preg_replace( | |
| 217 | + '#^' . $wp_rewrite->front . '#', | |
| 218 | + '/%lang%' . $wp_rewrite->front, | |
| 219 | + $permastruct ); | |
| 220 | + } else { | |
| 221 | + $permastruct = preg_replace( | |
| 222 | + '#^' . $wp_rewrite->root . '#', | |
| 223 | + '/%lang%/' . $wp_rewrite->root, | |
| 224 | + $permastruct ); | |
| 53 | 225 | } |
| 54 | 226 | |
| 227 | + $rules = array_merge( | |
| 228 | + bogo_generate_rewrite_rules( $permastruct, $post_type_obj->rewrite ), | |
| 229 | + $rules ); | |
| 230 | + | |
| 55 | 231 | if ( $post_type_obj->has_archive ) { |
| 56 | - if ( $post_type_obj->has_archive === true ) { | |
| 232 | + if ( $post_type_obj->has_archive === true ) | |
| 57 | 233 | $archive_slug = $post_type_obj->rewrite['slug']; |
| 58 | - } else { | |
| 234 | + else | |
| 59 | 235 | $archive_slug = $post_type_obj->has_archive; |
| 60 | - } | |
| 61 | 236 | |
| 62 | - if ( $post_type_obj->rewrite['with_front'] ) { | |
| 237 | + if ( $post_type_obj->rewrite['with_front'] ) | |
| 63 | 238 | $archive_slug = substr( $wp_rewrite->front, 1 ) . $archive_slug; |
| 64 | - } else { | |
| 239 | + else | |
| 65 | 240 | $archive_slug = $wp_rewrite->root . $archive_slug; |
| 66 | - } | |
| 67 | 241 | |
| 68 | - $extra_rules += array( | |
| 242 | + $extra_rules = array( | |
| 69 | 243 | "{$lang_regex}/{$archive_slug}/?$" |
| 70 | - => 'index.php?lang=$matches[1]&post_type=' . $post_type, | |
| 71 | - ); | |
| 244 | + => 'index.php?lang=$matches[1]&post_type=' . $post_type ); | |
| 72 | 245 | |
| 73 | - if ( $post_type_obj->rewrite['feeds'] and $wp_rewrite->feeds ) { | |
| 246 | + $rules = $extra_rules + $rules; | |
| 247 | + | |
| 248 | + if ( $post_type_obj->rewrite['feeds'] && $wp_rewrite->feeds ) { | |
| 74 | 249 | $feeds = '(' . trim( implode( '|', $wp_rewrite->feeds ) ) . ')'; |
| 75 | 250 | |
| 76 | - $extra_rules += array( | |
| 251 | + $extra_rules = array( | |
| 77 | 252 | "{$lang_regex}/{$archive_slug}/feed/$feeds/?$" |
| 78 | 253 | => 'index.php?lang=$matches[1]&post_type=' . $post_type . '&feed=$matches[2]', |
| 79 | 254 | "{$lang_regex}/{$archive_slug}/$feeds/?$" |
| 80 | - => 'index.php?lang=$matches[1]&post_type=' . $post_type . '&feed=$matches[2]', | |
| 81 | - ); | |
| 255 | + => 'index.php?lang=$matches[1]&post_type=' . $post_type . '&feed=$matches[2]' ); | |
| 256 | + | |
| 257 | + $rules = $extra_rules + $rules; | |
| 82 | 258 | } |
| 83 | 259 | |
| 84 | 260 | if ( $post_type_obj->rewrite['pages'] ) { |
| 85 | - $extra_rules += array( | |
| 86 | - "{$lang_regex}/{$archive_slug}/{$wp_rewrite->pagination_base}/([0-9]{1,})/?$" | |
| 87 | - => 'index.php?lang=$matches[1]&post_type=' . $post_type . '&paged=$matches[2]', | |
| 88 | - ); | |
| 261 | + $extra_rules = array( | |
| 262 | + "{$lang_regex}/{$archive_slug}/{$wp_rewrite->pagination_base}/([0-9]{1,})/?$" => 'index.php?lang=$matches[1]&post_type=' . $post_type . '&paged=$matches[2]' ); | |
| 263 | + | |
| 264 | + $rules = $extra_rules + $rules; | |
| 89 | 265 | } |
| 90 | 266 | } |
| 91 | 267 | |
| 92 | - $permastruct = $wp_rewrite->get_extra_permastruct( $post_type ); | |
| 93 | - $permastruct = bogo_add_lang_to_permastruct( $permastruct ); | |
| 268 | + foreach ( get_object_taxonomies( $post_type ) as $tax ) { | |
| 269 | + if ( ! $tax_obj = get_taxonomy( $tax ) ) | |
| 270 | + continue; | |
| 94 | 271 | |
| 95 | - $extra_rules += bogo_generate_rewrite_rules( | |
| 96 | - $permastruct, | |
| 97 | - $post_type_obj->rewrite | |
| 98 | - ); | |
| 99 | - } | |
| 272 | + if ( false === $tax_obj->rewrite ) | |
| 273 | + continue; | |
| 100 | 274 | |
| 101 | - $localizable_taxonomies = get_object_taxonomies( | |
| 102 | - $localizable_post_types, | |
| 103 | - 'objects' | |
| 104 | - ); | |
| 275 | + $permastruct = $wp_rewrite->get_extra_permastruct( $tax ); | |
| 105 | 276 | |
| 106 | - foreach ( $localizable_taxonomies as $taxonomy ) { | |
| 107 | - if ( empty( $taxonomy->rewrite ) ) { | |
| 108 | - continue; | |
| 277 | + if ( $tax_obj->rewrite['with_front'] ) { | |
| 278 | + $permastruct = preg_replace( | |
| 279 | + '#^' . $wp_rewrite->front . '#', | |
| 280 | + '/%lang%' . $wp_rewrite->front, | |
| 281 | + $permastruct ); | |
| 282 | + } else { | |
| 283 | + $permastruct = preg_replace( | |
| 284 | + '#^' . $wp_rewrite->root . '#', | |
| 285 | + '/%lang%/' . $wp_rewrite->root, | |
| 286 | + $permastruct ); | |
| 287 | + } | |
| 288 | + | |
| 289 | + $rules = array_merge( | |
| 290 | + bogo_generate_rewrite_rules( $permastruct, $tax_obj->rewrite ), | |
| 291 | + $rules ); | |
| 109 | 292 | } |
| 110 | - | |
| 111 | - $permastruct = $wp_rewrite->get_extra_permastruct( $taxonomy->name ); | |
| 112 | - $permastruct = bogo_add_lang_to_permastruct( $permastruct ); | |
| 113 | - | |
| 114 | - $extra_rules += bogo_generate_rewrite_rules( | |
| 115 | - $permastruct, | |
| 116 | - $taxonomy->rewrite | |
| 117 | - ); | |
| 118 | 293 | } |
| 119 | 294 | |
| 120 | - $root_rules = bogo_generate_rewrite_rules( | |
| 121 | - bogo_add_lang_to_permastruct( $wp_rewrite->root ), | |
| 122 | - array( 'ep_mask' => EP_ROOT ) | |
| 123 | - ); | |
| 124 | - | |
| 125 | - $comments_rules = bogo_generate_rewrite_rules( | |
| 126 | - bogo_add_lang_to_permastruct( | |
| 127 | - $wp_rewrite->root . $wp_rewrite->comments_base | |
| 128 | - ), | |
| 129 | - array( | |
| 130 | - 'ep_mask' => EP_COMMENTS, | |
| 131 | - 'forcomments' => true, | |
| 132 | - 'walk_dirs' => false, | |
| 133 | - ) | |
| 134 | - ); | |
| 135 | - | |
| 136 | - $search_rules = bogo_generate_rewrite_rules( | |
| 137 | - bogo_add_lang_to_permastruct( $wp_rewrite->get_search_permastruct() ), | |
| 138 | - array( 'ep_mask' => EP_SEARCH ) | |
| 139 | - ); | |
| 140 | - | |
| 141 | - $author_rules = bogo_generate_rewrite_rules( | |
| 142 | - bogo_add_lang_to_permastruct( $wp_rewrite->get_author_permastruct() ), | |
| 143 | - array( 'ep_mask' => EP_AUTHORS ) | |
| 144 | - ); | |
| 145 | - | |
| 146 | - $date_rules = bogo_generate_rewrite_rules( | |
| 147 | - bogo_add_lang_to_permastruct( $wp_rewrite->get_date_permastruct() ), | |
| 148 | - array( 'ep_mask' => EP_DATE ) | |
| 149 | - ); | |
| 150 | - | |
| 151 | - $post_rules = bogo_generate_rewrite_rules( | |
| 152 | - bogo_add_lang_to_permastruct( $wp_rewrite->permalink_structure ), | |
| 153 | - array( | |
| 154 | - 'ep_mask' => EP_PERMALINK, | |
| 155 | - 'paged' => false, | |
| 156 | - ) | |
| 157 | - ); | |
| 158 | - | |
| 159 | - $wp_rewrite->add_rewrite_tag( '%pagename%', '(.?.+?)', 'pagename=' ); | |
| 160 | - | |
| 161 | - $page_rules = bogo_generate_rewrite_rules( | |
| 162 | - bogo_add_lang_to_permastruct( $wp_rewrite->get_page_permastruct() ), | |
| 163 | - array( | |
| 164 | - 'ep_mask' => EP_PAGES, | |
| 165 | - 'walk_dirs' => false, | |
| 166 | - ) | |
| 167 | - ); | |
| 168 | - | |
| 169 | - if ( $wp_rewrite->use_verbose_page_rules ) { | |
| 170 | - $rules = array_merge( | |
| 171 | - $extra_rules, | |
| 172 | - $root_rules, | |
| 173 | - $comments_rules, | |
| 174 | - $search_rules, | |
| 175 | - $author_rules, | |
| 176 | - $date_rules, | |
| 177 | - $page_rules, | |
| 178 | - $post_rules, | |
| 179 | - $rules | |
| 180 | - ); | |
| 181 | - } else { | |
| 182 | - $rules = array_merge( | |
| 183 | - $extra_rules, | |
| 184 | - $root_rules, | |
| 185 | - $comments_rules, | |
| 186 | - $search_rules, | |
| 187 | - $author_rules, | |
| 188 | - $date_rules, | |
| 189 | - $post_rules, | |
| 190 | - $page_rules, | |
| 191 | - $rules | |
| 192 | - ); | |
| 193 | - } | |
| 194 | - | |
| 195 | 295 | return $rules; |
| 196 | 296 | } |
| 197 | 297 | |
| 198 | - | |
| 199 | -function bogo_add_lang_to_permastruct( $permastruct ) { | |
| 200 | - global $wp_rewrite; | |
| 201 | - | |
| 202 | - $root_quoted = preg_quote( $wp_rewrite->root ); | |
| 203 | - | |
| 204 | - $remains = preg_replace( "#^{$root_quoted}#", '', $permastruct ); | |
| 205 | - $remains = path_join( '%lang%', ltrim( $remains, '/' ) ); | |
| 206 | - | |
| 207 | - return path_join( $wp_rewrite->root, $remains ); | |
| 208 | -} | |
| 209 | - | |
| 210 | - | |
| 211 | 298 | function bogo_generate_rewrite_rules( $permalink_structure, $args = '' ) { |
| 212 | 299 | global $wp_rewrite; |
| 213 | 300 | |
| 214 | - $args = wp_parse_args( $args, array( | |
| 301 | + $defaults = array( | |
| 215 | 302 | 'ep_mask' => EP_NONE, |
| 216 | 303 | 'paged' => true, |
| 217 | 304 | 'feed' => true, |
| 218 | 305 | 'forcomments' => false, |
| 219 | 306 | 'walk_dirs' => true, |
| 220 | - 'endpoints' => true, | |
| 221 | - ) ); | |
| 307 | + 'endpoints' => true ); | |
| 222 | 308 | |
| 223 | - if ( ! str_contains( $permalink_structure, '%lang%' ) ) { | |
| 224 | - return array(); | |
| 225 | - } | |
| 309 | + $args = wp_parse_args( $args, $defaults ); | |
| 226 | 310 | |
| 311 | + extract( $args, EXTR_SKIP ); | |
| 312 | + | |
| 227 | 313 | $feedregex2 = '(' . implode( '|', $wp_rewrite->feeds ) . ')/?$'; |
| 228 | 314 | $feedregex = $wp_rewrite->feed_base . '/' . $feedregex2; |
| 229 | 315 | $trackbackregex = 'trackback/?$'; |
| 230 | 316 | $pageregex = $wp_rewrite->pagination_base . '/?([0-9]{1,})/?$'; |
| 231 | - $commentregex = $wp_rewrite->comments_pagination_base . '-([0-9]{1,})/?$'; | |
| 317 | + $commentregex = 'comment-page-([0-9]{1,})/?$'; | |
| 232 | 318 | $embedregex = 'embed/?$'; |
| 233 | 319 | |
| 234 | - if ( $args['endpoints'] ) { | |
| 320 | + if ( $endpoints ) { | |
| 235 | 321 | $ep_query_append = array(); |
| 236 | 322 | |
| 237 | 323 | foreach ( (array) $wp_rewrite->endpoints as $endpoint ) { |
| 238 | 324 | $epmatch = $endpoint[1] . '(/(.*))?/?$'; |
| 239 | - $epquery = '&' . $endpoint[2] . '='; | |
| 325 | + $epquery = '&' . $endpoint[1] . '='; | |
| 240 | 326 | $ep_query_append[$epmatch] = array( $endpoint[0], $epquery ); |
| 241 | 327 | } |
| 242 | 328 | } |
| 243 | 329 | |
| 244 | - $front = substr( | |
| 245 | - $permalink_structure, | |
| 246 | - 0, | |
| 247 | - strpos( $permalink_structure, '%' ) | |
| 248 | - ); | |
| 249 | - | |
| 330 | + $front = substr( $permalink_structure, 0, strpos( $permalink_structure, '%' ) ); | |
| 250 | 331 | preg_match_all( '/%.+?%/', $permalink_structure, $tokens ); |
| 251 | - | |
| 332 | + $num_tokens = count( $tokens[0] ); | |
| 252 | 333 | $index = $wp_rewrite->index; |
| 253 | 334 | $feedindex = $index; |
| 254 | 335 | $trackbackindex = $index; |
| 255 | 336 | $embedindex = $index; |
| 256 | 337 | |
| 257 | - $queries = array(); | |
| 258 | - | |
| 259 | - for ( $i = 0; $i < count( $tokens[0] ); ++$i ) { | |
| 338 | + for ( $i = 0; $i < $num_tokens; ++$i ) { | |
| 260 | 339 | if ( 0 < $i ) { |
| 261 | 340 | $queries[$i] = $queries[$i - 1] . '&'; |
| 262 | 341 | } else { |
| 263 | 342 | $queries[$i] = ''; |
| @@ -262,13 +341,11 @@ | ||
| 262 | 341 | } else { |
| 263 | 342 | $queries[$i] = ''; |
| 264 | 343 | } |
| 265 | 344 | |
| 266 | - $query_token = str_replace( | |
| 267 | - $wp_rewrite->rewritecode, | |
| 268 | - $wp_rewrite->queryreplace, | |
| 269 | - $tokens[0][$i] | |
| 270 | - ) . $wp_rewrite->preg_index( $i + 1 ); | |
| 345 | + $query_token = | |
| 346 | + str_replace( $wp_rewrite->rewritecode, $wp_rewrite->queryreplace, $tokens[0][$i] ) | |
| 347 | + . $wp_rewrite->preg_index( $i + 1 ); | |
| 271 | 348 | |
| 272 | 349 | $queries[$i] .= $query_token; |
| 273 | 350 | } |
| 274 | 351 | |
| @@ -273,17 +350,16 @@ | ||
| 273 | 350 | } |
| 274 | 351 | |
| 275 | 352 | $structure = $permalink_structure; |
| 276 | 353 | |
| 277 | - if ( '/' !== $front ) { | |
| 354 | + if ( $front != '/' ) { | |
| 278 | 355 | $structure = str_replace( $front, '', $structure ); |
| 279 | 356 | } |
| 280 | 357 | |
| 281 | 358 | $structure = trim( $structure, '/' ); |
| 282 | 359 | |
| 283 | - $dirs = $args['walk_dirs'] | |
| 284 | - ? explode( '/', $structure ) | |
| 285 | - : array( $structure ); | |
| 360 | + $dirs = $walk_dirs ? explode( '/', $structure ) : array( $structure ); | |
| 361 | + $num_dirs = count( $dirs ); | |
| 286 | 362 | |
| 287 | 363 | $front = preg_replace( '|^/+|', '', $front ); |
| 288 | 364 | |
| 289 | 365 | $post_rewrite = array(); |
| @@ -288,28 +364,17 @@ | ||
| 288 | 364 | |
| 289 | 365 | $post_rewrite = array(); |
| 290 | 366 | $struct = $front; |
| 291 | 367 | |
| 292 | - for ( $j = 0; $j < count( $dirs ); ++$j ) { | |
| 368 | + for ( $j = 0; $j < $num_dirs; ++$j ) { | |
| 293 | 369 | $struct .= $dirs[$j] . '/'; |
| 294 | 370 | $struct = ltrim( $struct, '/' ); |
| 295 | - | |
| 296 | - $match = str_replace( | |
| 297 | - $wp_rewrite->rewritecode, | |
| 298 | - $wp_rewrite->rewritereplace, | |
| 299 | - $struct | |
| 300 | - ); | |
| 301 | - | |
| 371 | + $match = str_replace( $wp_rewrite->rewritecode, $wp_rewrite->rewritereplace, $struct); | |
| 302 | 372 | $num_toks = preg_match_all( '/%.+?%/', $struct, $toks ); |
| 303 | 373 | |
| 304 | - if ( $num_toks < count( $tokens[0] ) and $toks[0] === array( '%lang%' ) ) { | |
| 305 | - continue; | |
| 306 | - } | |
| 374 | + $query = ( isset( $queries ) && is_array( $queries ) && ! empty( $num_toks ) ) | |
| 375 | + ? $queries[$num_toks - 1] : ''; | |
| 307 | 376 | |
| 308 | - $query = ( ! empty( $num_toks ) && isset( $queries[$num_toks - 1] ) ) | |
| 309 | - ? $queries[$num_toks - 1] | |
| 310 | - : ''; | |
| 311 | - | |
| 312 | 377 | switch ( $dirs[$j] ) { |
| 313 | 378 | case '%year%': |
| 314 | 379 | $ep_mask_specific = EP_YEAR; |
| 315 | 380 | break; |
| @@ -345,12 +410,9 @@ | ||
| 345 | 410 | $feedmatch2 = $match . $feedregex2; |
| 346 | 411 | $feedquery2 = $feedindex . '?' . $query |
| 347 | 412 | . '&feed=' . $wp_rewrite->preg_index( $num_toks + 1 ); |
| 348 | 413 | |
| 349 | - $embedmatch = $match . $embedregex; | |
| 350 | - $embedquery = $embedindex . '?' . $query . '&embed=true'; | |
| 351 | - | |
| 352 | - if ( $args['forcomments'] ) { | |
| 414 | + if ( $forcomments ) { | |
| 353 | 415 | $feedquery .= '&withcomments=1'; |
| 354 | 416 | $feedquery2 .= '&withcomments=1'; |
| 355 | 417 | } |
| 356 | 418 | |
| @@ -355,38 +417,25 @@ | ||
| 355 | 417 | } |
| 356 | 418 | |
| 357 | 419 | $rewrite = array(); |
| 358 | 420 | |
| 359 | - if ( $args['feed'] ) { | |
| 360 | - $rewrite = array( | |
| 361 | - $feedmatch => $feedquery, | |
| 362 | - $feedmatch2 => $feedquery2, | |
| 363 | - $embedmatch => $embedquery, | |
| 364 | - ); | |
| 421 | + if ( $feed ) { | |
| 422 | + $rewrite = array( $feedmatch => $feedquery, $feedmatch2 => $feedquery2 ); | |
| 365 | 423 | } |
| 366 | 424 | |
| 367 | - if ( $args['paged'] ) { | |
| 368 | - $rewrite = array_merge( | |
| 369 | - $rewrite, | |
| 370 | - array( $pagematch => $pagequery ) | |
| 371 | - ); | |
| 425 | + if ( $paged ) { | |
| 426 | + $rewrite = array_merge( $rewrite, array( $pagematch => $pagequery ) ); | |
| 372 | 427 | } |
| 373 | 428 | |
| 374 | - if ( EP_PAGES & $args['ep_mask'] or EP_PERMALINK & $args['ep_mask'] ) { | |
| 375 | - $rewrite = array_merge( | |
| 376 | - $rewrite, | |
| 377 | - array( $commentmatch => $commentquery ) | |
| 378 | - ); | |
| 379 | - } elseif ( EP_ROOT & $args['ep_mask'] and get_option( 'page_on_front' ) ) { | |
| 380 | - $rewrite = array_merge( | |
| 381 | - $rewrite, | |
| 382 | - array( $rootcommentmatch => $rootcommentquery ) | |
| 383 | - ); | |
| 429 | + if ( EP_PAGES & $ep_mask || EP_PERMALINK & $ep_mask ) { | |
| 430 | + $rewrite = array_merge( $rewrite, array( $commentmatch => $commentquery ) ); | |
| 431 | + } elseif ( EP_ROOT & $ep_mask && get_option( 'page_on_front' ) ) { | |
| 432 | + $rewrite = array_merge( $rewrite, array( $rootcommentmatch => $rootcommentquery ) ); | |
| 384 | 433 | } |
| 385 | 434 | |
| 386 | - if ( $args['endpoints'] ) { | |
| 435 | + if ( $endpoints ) { | |
| 387 | 436 | foreach ( (array) $ep_query_append as $regex => $ep ) { |
| 388 | - if ( $ep[0] & $args['ep_mask'] or $ep[0] & $ep_mask_specific ) { | |
| 437 | + if ( $ep[0] & $ep_mask || $ep[0] & $ep_mask_specific ) { | |
| 389 | 438 | $rewrite[$match . $regex] = $index . '?' . $query |
| 390 | 439 | . $ep[1] . $wp_rewrite->preg_index( $num_toks + 2 ); |
| 391 | 440 | } |
| 392 | 441 | } |
| @@ -395,22 +444,20 @@ | ||
| 395 | 444 | if ( $num_toks ) { |
| 396 | 445 | $post = false; |
| 397 | 446 | $page = false; |
| 398 | 447 | |
| 399 | - if ( | |
| 400 | - str_contains( $struct, '%postname%' ) or | |
| 401 | - str_contains( $struct, '%post_id%' ) or | |
| 402 | - str_contains( $struct, '%pagename%' ) or | |
| 403 | - str_contains( $struct, '%year%' ) and | |
| 404 | - str_contains( $struct, '%monthnum%' ) and | |
| 405 | - str_contains( $struct, '%day%' ) and | |
| 406 | - str_contains( $struct, '%hour%' ) and | |
| 407 | - str_contains( $struct, '%minute%' ) and | |
| 408 | - str_contains( $struct, '%second%' ) | |
| 409 | - ) { | |
| 448 | + if ( strpos( $struct, '%postname%' ) !== false | |
| 449 | + || strpos( $struct, '%post_id%' ) !== false | |
| 450 | + || strpos( $struct, '%pagename%' ) !== false | |
| 451 | + || ( strpos( $struct, '%year%' ) !== false | |
| 452 | + && strpos( $struct, '%monthnum%' ) !== false | |
| 453 | + && strpos( $struct, '%day%' ) !== false | |
| 454 | + && strpos( $struct, '%hour%' ) !== false | |
| 455 | + && strpos( $struct, '%minute%' ) !== false | |
| 456 | + && strpos( $struct, '%second%' ) !== false ) ) { | |
| 410 | 457 | $post = true; |
| 411 | 458 | |
| 412 | - if ( str_contains( $struct, '%pagename%' ) ) { | |
| 459 | + if ( strpos( $struct, '%pagename%' ) !== false ) { | |
| 413 | 460 | $page = true; |
| 414 | 461 | } |
| 415 | 462 | } |
| 416 | 463 | |
| @@ -415,9 +462,9 @@ | ||
| 415 | 462 | } |
| 416 | 463 | |
| 417 | 464 | if ( ! $post ) { |
| 418 | 465 | foreach ( get_post_types( array( '_builtin' => false ) ) as $ptype ) { |
| 419 | - if ( str_contains( $struct, "%$ptype%" ) ) { | |
| 466 | + if ( strpos( $struct, "%$ptype%" ) !== false ) { | |
| 420 | 467 | $post = true; |
| 421 | 468 | $page = is_post_type_hierarchical( $ptype ); |
| 422 | 469 | break; |
| 423 | 470 | } |
| @@ -453,15 +500,15 @@ | ||
| 453 | 500 | $subfeedquery = $subquery . '&feed=' . $wp_rewrite->preg_index( 2 ); |
| 454 | 501 | $subcommentquery = $subquery . '&cpage=' . $wp_rewrite->preg_index( 2 ); |
| 455 | 502 | $subembedquery = $subquery . '&embed=true'; |
| 456 | 503 | |
| 457 | - if ( ! empty( $args['endpoints'] ) ) { | |
| 504 | + if ( ! empty( $endpoints ) ) { | |
| 458 | 505 | foreach ( (array) $ep_query_append as $regex => $ep ) { |
| 459 | 506 | if ( $ep[0] & EP_ATTACHMENT ) { |
| 460 | 507 | $rewrite[$sub1 . $regex] = |
| 461 | - $subquery . $ep[1] . $wp_rewrite->preg_index( 3 ); | |
| 508 | + $subquery . $ep[1] . $wp_rewrite->preg_index( 2 ); | |
| 462 | 509 | $rewrite[$sub2 . $regex] = |
| 463 | - $subquery . $ep[1] . $wp_rewrite->preg_index( 3 ); | |
| 510 | + $subquery . $ep[1] . $wp_rewrite->preg_index( 2 ); | |
| 464 | 511 | } |
| 465 | 512 | } |
| 466 | 513 | } |
| 467 | 514 | |
| @@ -467,9 +514,9 @@ | ||
| 467 | 514 | |
| 468 | 515 | $sub1 .= '?$'; |
| 469 | 516 | $sub2 .= '?$'; |
| 470 | 517 | |
| 471 | - $match = $match . '(?:/([0-9]+))?/?$'; | |
| 518 | + $match = $match . '(/[0-9]+)?/?$'; | |
| 472 | 519 | $query = $index . '?' . $query |
| 473 | 520 | . '&page=' . $wp_rewrite->preg_index( $num_toks + 1 ); |
| 474 | 521 | } else { |
| 475 | 522 | $match .= '?$'; |
| @@ -478,43 +525,29 @@ | ||
| 478 | 525 | |
| 479 | 526 | $rewrite = array_merge( $rewrite, array( $match => $query ) ); |
| 480 | 527 | |
| 481 | 528 | if ( $post ) { |
| 482 | - $rewrite = array_merge( | |
| 483 | - array( $trackbackmatch => $trackbackquery ), | |
| 484 | - $rewrite | |
| 485 | - ); | |
| 529 | + $rewrite = array_merge( array( $trackbackmatch => $trackbackquery ), $rewrite ); | |
| 486 | 530 | |
| 487 | - $rewrite = array_merge( | |
| 488 | - array( $embedmatch => $embedquery ), | |
| 489 | - $rewrite | |
| 490 | - ); | |
| 531 | + $rewrite = array_merge( array( $embedmatch => $embedquery ), $rewrite ); | |
| 491 | 532 | |
| 492 | 533 | if ( ! $page ) { |
| 493 | - $rewrite = array_merge( | |
| 494 | - $rewrite, | |
| 495 | - array( | |
| 496 | - $sub1 => $subquery, | |
| 497 | - $sub1tb => $subtbquery, | |
| 498 | - $sub1feed => $subfeedquery, | |
| 499 | - $sub1feed2 => $subfeedquery, | |
| 500 | - $sub1comment => $subcommentquery, | |
| 501 | - $sub1embed => $subembedquery, | |
| 502 | - ) | |
| 503 | - ); | |
| 534 | + $rewrite = array_merge( $rewrite, array( | |
| 535 | + $sub1 => $subquery, | |
| 536 | + $sub1tb => $subtbquery, | |
| 537 | + $sub1feed => $subfeedquery, | |
| 538 | + $sub1feed2 => $subfeedquery, | |
| 539 | + $sub1comment => $subcommentquery, | |
| 540 | + $sub1embed => $subembedquery ) ); | |
| 504 | 541 | } |
| 505 | 542 | |
| 506 | - $rewrite = array_merge( | |
| 507 | - array( | |
| 508 | - $sub2 => $subquery, | |
| 509 | - $sub2tb => $subtbquery, | |
| 510 | - $sub2feed => $subfeedquery, | |
| 511 | - $sub2feed2 => $subfeedquery, | |
| 512 | - $sub2comment => $subcommentquery, | |
| 513 | - $sub2embed => $subembedquery, | |
| 514 | - ), | |
| 515 | - $rewrite | |
| 516 | - ); | |
| 543 | + $rewrite = array_merge( array( | |
| 544 | + $sub2 => $subquery, | |
| 545 | + $sub2tb => $subtbquery, | |
| 546 | + $sub2feed => $subfeedquery, | |
| 547 | + $sub2feed2 => $subfeedquery, | |
| 548 | + $sub2comment => $subcommentquery, | |
| 549 | + $sub2embed => $subembedquery ), $rewrite ); | |
| 517 | 550 | } |
| 518 | 551 | } |
| 519 | 552 | |
| 520 | 553 | $post_rewrite = array_merge( $rewrite, $post_rewrite ); |