| @@ -2,23 +2,20 @@ | ||
| 2 | 2 | |
| 3 | 3 | add_action( 'init', 'bogo_add_rewrite_tags' ); |
| 4 | 4 | |
| 5 | 5 | function bogo_add_rewrite_tags() { |
| 6 | - $langs = array_keys( bogo_available_languages() ); | |
| 7 | - $langs = array_map( 'bogo_lang_slug', $langs ); | |
| 8 | - $langs = array_filter( $langs ); | |
| 6 | + $regex = bogo_get_lang_regex(); | |
| 9 | 7 | |
| 10 | - if ( empty( $langs ) ) | |
| 8 | + if ( empty( $regex ) ) { | |
| 11 | 9 | return; |
| 10 | + } | |
| 12 | 11 | |
| 13 | - $lang_rewrite_regex = '(' . implode( '|', $langs ) . ')'; | |
| 12 | + add_rewrite_tag( '%lang%', $regex, 'lang=' ); | |
| 14 | 13 | |
| 15 | - add_rewrite_tag( '%lang%', $lang_rewrite_regex, 'lang=' ); | |
| 16 | - | |
| 17 | 14 | $old_regex = bogo_get_prop( 'lang_rewrite_regex' ); |
| 18 | 15 | |
| 19 | - if ( $lang_rewrite_regex != $old_regex ) { | |
| 20 | - bogo_set_prop( 'lang_rewrite_regex', $lang_rewrite_regex ); | |
| 16 | + if ( $regex != $old_regex ) { | |
| 17 | + bogo_set_prop( 'lang_rewrite_regex', $regex ); | |
| 21 | 18 | flush_rewrite_rules(); |
| 22 | 19 | } |
| 23 | 20 | } |
| 24 | 21 | |
| @@ -41,15 +38,25 @@ | ||
| 41 | 38 | global $wp_rewrite; |
| 42 | 39 | |
| 43 | 40 | $permastruct = $wp_rewrite->permalink_structure; |
| 44 | 41 | |
| 45 | - if ( ! apache_mod_loaded( 'mod_rewrite', true ) && ! iis7_supports_permalinks() ) | |
| 46 | - $permastruct = preg_replace( '#^/index\.php#', '/index.php/%lang%', $permastruct ); | |
| 47 | - elseif ( is_multisite() && ! is_subdomain_install() && is_main_site() ) | |
| 48 | - $permastruct = preg_replace( '#^/blog#', '/%lang%/blog', $permastruct ); | |
| 49 | - else | |
| 50 | - $permastruct = preg_replace( '#^/#', '/%lang%/', $permastruct ); | |
| 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() ); | |
| 51 | 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 | + | |
| 52 | 59 | $extra = bogo_generate_rewrite_rules( $permastruct, array( |
| 53 | 60 | 'ep_mask' => EP_PERMALINK, |
| 54 | 61 | 'paged' => false ) ); |
| 55 | 62 | |
| @@ -169,8 +176,126 @@ | ||
| 169 | 176 | |
| 170 | 177 | return array_merge( $extra, $taxonomy_rewrite ); |
| 171 | 178 | } |
| 172 | 179 | |
| 180 | +add_filter( 'rewrite_rules_array', 'bogo_rewrite_rules_array' ); | |
| 181 | + | |
| 182 | +function bogo_rewrite_rules_array( $rules ) { | |
| 183 | + global $wp_rewrite; | |
| 184 | + | |
| 185 | + $lang_regex = bogo_get_lang_regex(); | |
| 186 | + | |
| 187 | + // REST rewrite rules | |
| 188 | + $rest_url_prefix = rest_get_url_prefix(); | |
| 189 | + | |
| 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; | |
| 204 | + } | |
| 205 | + | |
| 206 | + foreach ( $post_types as $post_type ) { | |
| 207 | + if ( ! $post_type_obj = get_post_type_object( $post_type ) ) | |
| 208 | + continue; | |
| 209 | + | |
| 210 | + if ( false === $post_type_obj->rewrite ) | |
| 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 ); | |
| 225 | + } | |
| 226 | + | |
| 227 | + $rules = array_merge( | |
| 228 | + bogo_generate_rewrite_rules( $permastruct, $post_type_obj->rewrite ), | |
| 229 | + $rules ); | |
| 230 | + | |
| 231 | + if ( $post_type_obj->has_archive ) { | |
| 232 | + if ( $post_type_obj->has_archive === true ) | |
| 233 | + $archive_slug = $post_type_obj->rewrite['slug']; | |
| 234 | + else | |
| 235 | + $archive_slug = $post_type_obj->has_archive; | |
| 236 | + | |
| 237 | + if ( $post_type_obj->rewrite['with_front'] ) | |
| 238 | + $archive_slug = substr( $wp_rewrite->front, 1 ) . $archive_slug; | |
| 239 | + else | |
| 240 | + $archive_slug = $wp_rewrite->root . $archive_slug; | |
| 241 | + | |
| 242 | + $extra_rules = array( | |
| 243 | + "{$lang_regex}/{$archive_slug}/?$" | |
| 244 | + => 'index.php?lang=$matches[1]&post_type=' . $post_type ); | |
| 245 | + | |
| 246 | + $rules = $extra_rules + $rules; | |
| 247 | + | |
| 248 | + if ( $post_type_obj->rewrite['feeds'] && $wp_rewrite->feeds ) { | |
| 249 | + $feeds = '(' . trim( implode( '|', $wp_rewrite->feeds ) ) . ')'; | |
| 250 | + | |
| 251 | + $extra_rules = array( | |
| 252 | + "{$lang_regex}/{$archive_slug}/feed/$feeds/?$" | |
| 253 | + => 'index.php?lang=$matches[1]&post_type=' . $post_type . '&feed=$matches[2]', | |
| 254 | + "{$lang_regex}/{$archive_slug}/$feeds/?$" | |
| 255 | + => 'index.php?lang=$matches[1]&post_type=' . $post_type . '&feed=$matches[2]' ); | |
| 256 | + | |
| 257 | + $rules = $extra_rules + $rules; | |
| 258 | + } | |
| 259 | + | |
| 260 | + if ( $post_type_obj->rewrite['pages'] ) { | |
| 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; | |
| 265 | + } | |
| 266 | + } | |
| 267 | + | |
| 268 | + foreach ( get_object_taxonomies( $post_type ) as $tax ) { | |
| 269 | + if ( ! $tax_obj = get_taxonomy( $tax ) ) | |
| 270 | + continue; | |
| 271 | + | |
| 272 | + if ( false === $tax_obj->rewrite ) | |
| 273 | + continue; | |
| 274 | + | |
| 275 | + $permastruct = $wp_rewrite->get_extra_permastruct( $tax ); | |
| 276 | + | |
| 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 ); | |
| 292 | + } | |
| 293 | + } | |
| 294 | + | |
| 295 | + return $rules; | |
| 296 | +} | |
| 297 | + | |
| 173 | 298 | function bogo_generate_rewrite_rules( $permalink_structure, $args = '' ) { |
| 174 | 299 | global $wp_rewrite; |
| 175 | 300 | |
| 176 | 301 | $defaults = array( |
| @@ -189,8 +314,9 @@ | ||
| 189 | 314 | $feedregex = $wp_rewrite->feed_base . '/' . $feedregex2; |
| 190 | 315 | $trackbackregex = 'trackback/?$'; |
| 191 | 316 | $pageregex = $wp_rewrite->pagination_base . '/?([0-9]{1,})/?$'; |
| 192 | 317 | $commentregex = 'comment-page-([0-9]{1,})/?$'; |
| 318 | + $embedregex = 'embed/?$'; | |
| 193 | 319 | |
| 194 | 320 | if ( $endpoints ) { |
| 195 | 321 | $ep_query_append = array(); |
| 196 | 322 | |
| @@ -206,14 +332,16 @@ | ||
| 206 | 332 | $num_tokens = count( $tokens[0] ); |
| 207 | 333 | $index = $wp_rewrite->index; |
| 208 | 334 | $feedindex = $index; |
| 209 | 335 | $trackbackindex = $index; |
| 336 | + $embedindex = $index; | |
| 210 | 337 | |
| 211 | 338 | for ( $i = 0; $i < $num_tokens; ++$i ) { |
| 212 | - if ( 0 < $i ) | |
| 339 | + if ( 0 < $i ) { | |
| 213 | 340 | $queries[$i] = $queries[$i - 1] . '&'; |
| 214 | - else | |
| 341 | + } else { | |
| 215 | 342 | $queries[$i] = ''; |
| 343 | + } | |
| 216 | 344 | |
| 217 | 345 | $query_token = |
| 218 | 346 | str_replace( $wp_rewrite->rewritecode, $wp_rewrite->queryreplace, $tokens[0][$i] ) |
| 219 | 347 | . $wp_rewrite->preg_index( $i + 1 ); |
| @@ -222,10 +350,11 @@ | ||
| 222 | 350 | } |
| 223 | 351 | |
| 224 | 352 | $structure = $permalink_structure; |
| 225 | 353 | |
| 226 | - if ( $front != '/' ) | |
| 354 | + if ( $front != '/' ) { | |
| 227 | 355 | $structure = str_replace( $front, '', $structure ); |
| 356 | + } | |
| 228 | 357 | |
| 229 | 358 | $structure = trim( $structure, '/' ); |
| 230 | 359 | |
| 231 | 360 | $dirs = $walk_dirs ? explode( '/', $structure ) : array( $structure ); |
| @@ -288,24 +417,28 @@ | ||
| 288 | 417 | } |
| 289 | 418 | |
| 290 | 419 | $rewrite = array(); |
| 291 | 420 | |
| 292 | - if ( $feed ) | |
| 421 | + if ( $feed ) { | |
| 293 | 422 | $rewrite = array( $feedmatch => $feedquery, $feedmatch2 => $feedquery2 ); |
| 423 | + } | |
| 294 | 424 | |
| 295 | - if ( $paged ) | |
| 425 | + if ( $paged ) { | |
| 296 | 426 | $rewrite = array_merge( $rewrite, array( $pagematch => $pagequery ) ); |
| 427 | + } | |
| 297 | 428 | |
| 298 | - if ( EP_PAGES & $ep_mask || EP_PERMALINK & $ep_mask ) | |
| 429 | + if ( EP_PAGES & $ep_mask || EP_PERMALINK & $ep_mask ) { | |
| 299 | 430 | $rewrite = array_merge( $rewrite, array( $commentmatch => $commentquery ) ); |
| 300 | - else if ( EP_ROOT & $ep_mask && get_option( 'page_on_front' ) ) | |
| 431 | + } elseif ( EP_ROOT & $ep_mask && get_option( 'page_on_front' ) ) { | |
| 301 | 432 | $rewrite = array_merge( $rewrite, array( $rootcommentmatch => $rootcommentquery ) ); |
| 433 | + } | |
| 302 | 434 | |
| 303 | 435 | if ( $endpoints ) { |
| 304 | 436 | foreach ( (array) $ep_query_append as $regex => $ep ) { |
| 305 | - if ( $ep[0] & $ep_mask || $ep[0] & $ep_mask_specific ) | |
| 437 | + if ( $ep[0] & $ep_mask || $ep[0] & $ep_mask_specific ) { | |
| 306 | 438 | $rewrite[$match . $regex] = $index . '?' . $query |
| 307 | 439 | . $ep[1] . $wp_rewrite->preg_index( $num_toks + 2 ); |
| 440 | + } | |
| 308 | 441 | } |
| 309 | 442 | } |
| 310 | 443 | |
| 311 | 444 | if ( $num_toks ) { |
| @@ -322,10 +455,11 @@ | ||
| 322 | 455 | && strpos( $struct, '%minute%' ) !== false |
| 323 | 456 | && strpos( $struct, '%second%' ) !== false ) ) { |
| 324 | 457 | $post = true; |
| 325 | 458 | |
| 326 | - if ( strpos( $struct, '%pagename%' ) !== false ) | |
| 459 | + if ( strpos( $struct, '%pagename%' ) !== false ) { | |
| 327 | 460 | $page = true; |
| 461 | + } | |
| 328 | 462 | } |
| 329 | 463 | |
| 330 | 464 | if ( ! $post ) { |
| 331 | 465 | foreach ( get_post_types( array( '_builtin' => false ) ) as $ptype ) { |
| @@ -339,8 +473,12 @@ | ||
| 339 | 473 | |
| 340 | 474 | if ( $post ) { |
| 341 | 475 | $trackbackmatch = $match . $trackbackregex; |
| 342 | 476 | $trackbackquery = $trackbackindex . '?' . $query . '&tb=1'; |
| 477 | + | |
| 478 | + $embedmatch = $match . $embedregex; | |
| 479 | + $embedquery = $embedindex . '?' . $query . '&embed=true'; | |
| 480 | + | |
| 343 | 481 | $match = rtrim( $match, '/' ); |
| 344 | 482 | $submatchbase = preg_replace( '/\(([^?].+?)\)/', '(?:$1)', $match ); |
| 345 | 483 | |
| 346 | 484 | $sub1 = $submatchbase . '/([^/]+)/'; |
| @@ -347,8 +485,9 @@ | ||
| 347 | 485 | $sub1tb = $sub1 . $trackbackregex; |
| 348 | 486 | $sub1feed = $sub1 . $feedregex; |
| 349 | 487 | $sub1feed2 = $sub1 . $feedregex2; |
| 350 | 488 | $sub1comment = $sub1 . $commentregex; |
| 489 | + $sub1embed = $sub1 . $embedregex; | |
| 351 | 490 | |
| 352 | 491 | $sub2 = $submatchbase . '/attachment/([^/]+)/'; |
| 353 | 492 | $sub2tb = $sub2 . $trackbackregex; |
| 354 | 493 | $sub2feed = $sub2 . $feedregex; |
| @@ -353,13 +492,15 @@ | ||
| 353 | 492 | $sub2tb = $sub2 . $trackbackregex; |
| 354 | 493 | $sub2feed = $sub2 . $feedregex; |
| 355 | 494 | $sub2feed2 = $sub2 . $feedregex2; |
| 356 | 495 | $sub2comment = $sub2 . $commentregex; |
| 496 | + $sub2embed = $sub2 . $embedregex; | |
| 357 | 497 | |
| 358 | 498 | $subquery = $index . '?attachment=' . $wp_rewrite->preg_index( 1 ); |
| 359 | 499 | $subtbquery = $subquery . '&tb=1'; |
| 360 | 500 | $subfeedquery = $subquery . '&feed=' . $wp_rewrite->preg_index( 2 ); |
| 361 | 501 | $subcommentquery = $subquery . '&cpage=' . $wp_rewrite->preg_index( 2 ); |
| 502 | + $subembedquery = $subquery . '&embed=true'; | |
| 362 | 503 | |
| 363 | 504 | if ( ! empty( $endpoints ) ) { |
| 364 | 505 | foreach ( (array) $ep_query_append as $regex => $ep ) { |
| 365 | 506 | if ( $ep[0] & EP_ATTACHMENT ) { |
| @@ -386,8 +527,10 @@ | ||
| 386 | 527 | |
| 387 | 528 | if ( $post ) { |
| 388 | 529 | $rewrite = array_merge( array( $trackbackmatch => $trackbackquery ), $rewrite ); |
| 389 | 530 | |
| 531 | + $rewrite = array_merge( array( $embedmatch => $embedquery ), $rewrite ); | |
| 532 | + | |
| 390 | 533 | if ( ! $page ) { |
| 391 | 534 | $rewrite = array_merge( $rewrite, array( |
| 392 | 535 | $sub1 => $subquery, |
| 393 | 536 | $sub1tb => $subtbquery, |
| @@ -392,9 +535,10 @@ | ||
| 392 | 535 | $sub1 => $subquery, |
| 393 | 536 | $sub1tb => $subtbquery, |
| 394 | 537 | $sub1feed => $subfeedquery, |
| 395 | 538 | $sub1feed2 => $subfeedquery, |
| 396 | - $sub1comment => $subcommentquery ) ); | |
| 539 | + $sub1comment => $subcommentquery, | |
| 540 | + $sub1embed => $subembedquery ) ); | |
| 397 | 541 | } |
| 398 | 542 | |
| 399 | 543 | $rewrite = array_merge( array( |
| 400 | 544 | $sub2 => $subquery, |
| @@ -400,9 +544,10 @@ | ||
| 400 | 544 | $sub2 => $subquery, |
| 401 | 545 | $sub2tb => $subtbquery, |
| 402 | 546 | $sub2feed => $subfeedquery, |
| 403 | 547 | $sub2feed2 => $subfeedquery, |
| 404 | - $sub2comment => $subcommentquery ), $rewrite ); | |
| 548 | + $sub2comment => $subcommentquery, | |
| 549 | + $sub2embed => $subembedquery ), $rewrite ); | |
| 405 | 550 | } |
| 406 | 551 | } |
| 407 | 552 | |
| 408 | 553 | $post_rewrite = array_merge( $rewrite, $post_rewrite ); |
| @@ -409,6 +554,4 @@ | ||
| 409 | 554 | } |
| 410 | 555 | |
| 411 | 556 | return $post_rewrite; |
| 412 | 557 | } |
| 413 | - | |
| 414 | -?> | |