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