| @@ -4,10 +4,11 @@ | ||
| 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 | + } | |
| 10 | 11 | |
| 11 | 12 | add_rewrite_tag( '%lang%', $regex, 'lang=' ); |
| 12 | 13 | |
| 13 | 14 | $old_regex = bogo_get_prop( 'lang_rewrite_regex' ); |
| @@ -37,15 +38,25 @@ | ||
| 37 | 38 | global $wp_rewrite; |
| 38 | 39 | |
| 39 | 40 | $permastruct = $wp_rewrite->permalink_structure; |
| 40 | 41 | |
| 41 | - if ( ! apache_mod_loaded( 'mod_rewrite', true ) && ! iis7_supports_permalinks() ) | |
| 42 | - $permastruct = preg_replace( '#^/index\.php#', '/index.php/%lang%', $permastruct ); | |
| 43 | - elseif ( is_multisite() && ! is_subdomain_install() && is_main_site() ) | |
| 44 | - $permastruct = preg_replace( '#^/blog#', '/%lang%/blog', $permastruct ); | |
| 45 | - else | |
| 46 | - $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() ); | |
| 47 | 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 | + | |
| 48 | 59 | $extra = bogo_generate_rewrite_rules( $permastruct, array( |
| 49 | 60 | 'ep_mask' => EP_PERMALINK, |
| 50 | 61 | 'paged' => false ) ); |
| 51 | 62 | |
| @@ -170,17 +181,31 @@ | ||
| 170 | 181 | |
| 171 | 182 | function bogo_rewrite_rules_array( $rules ) { |
| 172 | 183 | global $wp_rewrite; |
| 173 | 184 | |
| 185 | + $lang_regex = bogo_get_lang_regex(); | |
| 186 | + | |
| 187 | + // REST rewrite rules | |
| 188 | + if ( function_exists( 'rest_get_url_prefix' ) ) { | |
| 189 | + $rest_url_prefix = rest_get_url_prefix(); | |
| 190 | + | |
| 191 | + $extra_rules = array( | |
| 192 | + "^{$lang_regex}/{$rest_url_prefix}/?$" | |
| 193 | + => 'index.php?lang=$matches[1]&rest_route=/', | |
| 194 | + "^{$lang_regex}/{$rest_url_prefix}/(.*)?" | |
| 195 | + => 'index.php?lang=$matches[1]&rest_route=/$matches[2]' ); | |
| 196 | + | |
| 197 | + $rules = $extra_rules + $rules; | |
| 198 | + } | |
| 199 | + | |
| 174 | 200 | $post_types = array_diff( |
| 175 | 201 | (array) bogo_localizable_post_types(), |
| 176 | 202 | get_post_types( array( '_builtin' => true ) ) ); |
| 177 | 203 | |
| 178 | - if ( empty( $post_types ) ) | |
| 204 | + if ( empty( $post_types ) ) { | |
| 179 | 205 | return $rules; |
| 206 | + } | |
| 180 | 207 | |
| 181 | - $lang_regex = bogo_get_lang_regex(); | |
| 182 | - | |
| 183 | 208 | foreach ( $post_types as $post_type ) { |
| 184 | 209 | if ( ! $post_type_obj = get_post_type_object( $post_type ) ) |
| 185 | 210 | continue; |
| 186 | 211 | |
| @@ -291,8 +316,9 @@ | ||
| 291 | 316 | $feedregex = $wp_rewrite->feed_base . '/' . $feedregex2; |
| 292 | 317 | $trackbackregex = 'trackback/?$'; |
| 293 | 318 | $pageregex = $wp_rewrite->pagination_base . '/?([0-9]{1,})/?$'; |
| 294 | 319 | $commentregex = 'comment-page-([0-9]{1,})/?$'; |
| 320 | + $embedregex = 'embed/?$'; | |
| 295 | 321 | |
| 296 | 322 | if ( $endpoints ) { |
| 297 | 323 | $ep_query_append = array(); |
| 298 | 324 | |
| @@ -308,14 +334,16 @@ | ||
| 308 | 334 | $num_tokens = count( $tokens[0] ); |
| 309 | 335 | $index = $wp_rewrite->index; |
| 310 | 336 | $feedindex = $index; |
| 311 | 337 | $trackbackindex = $index; |
| 338 | + $embedindex = $index; | |
| 312 | 339 | |
| 313 | 340 | for ( $i = 0; $i < $num_tokens; ++$i ) { |
| 314 | - if ( 0 < $i ) | |
| 341 | + if ( 0 < $i ) { | |
| 315 | 342 | $queries[$i] = $queries[$i - 1] . '&'; |
| 316 | - else | |
| 343 | + } else { | |
| 317 | 344 | $queries[$i] = ''; |
| 345 | + } | |
| 318 | 346 | |
| 319 | 347 | $query_token = |
| 320 | 348 | str_replace( $wp_rewrite->rewritecode, $wp_rewrite->queryreplace, $tokens[0][$i] ) |
| 321 | 349 | . $wp_rewrite->preg_index( $i + 1 ); |
| @@ -324,10 +352,11 @@ | ||
| 324 | 352 | } |
| 325 | 353 | |
| 326 | 354 | $structure = $permalink_structure; |
| 327 | 355 | |
| 328 | - if ( $front != '/' ) | |
| 356 | + if ( $front != '/' ) { | |
| 329 | 357 | $structure = str_replace( $front, '', $structure ); |
| 358 | + } | |
| 330 | 359 | |
| 331 | 360 | $structure = trim( $structure, '/' ); |
| 332 | 361 | |
| 333 | 362 | $dirs = $walk_dirs ? explode( '/', $structure ) : array( $structure ); |
| @@ -390,24 +419,28 @@ | ||
| 390 | 419 | } |
| 391 | 420 | |
| 392 | 421 | $rewrite = array(); |
| 393 | 422 | |
| 394 | - if ( $feed ) | |
| 423 | + if ( $feed ) { | |
| 395 | 424 | $rewrite = array( $feedmatch => $feedquery, $feedmatch2 => $feedquery2 ); |
| 425 | + } | |
| 396 | 426 | |
| 397 | - if ( $paged ) | |
| 427 | + if ( $paged ) { | |
| 398 | 428 | $rewrite = array_merge( $rewrite, array( $pagematch => $pagequery ) ); |
| 429 | + } | |
| 399 | 430 | |
| 400 | - if ( EP_PAGES & $ep_mask || EP_PERMALINK & $ep_mask ) | |
| 431 | + if ( EP_PAGES & $ep_mask || EP_PERMALINK & $ep_mask ) { | |
| 401 | 432 | $rewrite = array_merge( $rewrite, array( $commentmatch => $commentquery ) ); |
| 402 | - else if ( EP_ROOT & $ep_mask && get_option( 'page_on_front' ) ) | |
| 433 | + } elseif ( EP_ROOT & $ep_mask && get_option( 'page_on_front' ) ) { | |
| 403 | 434 | $rewrite = array_merge( $rewrite, array( $rootcommentmatch => $rootcommentquery ) ); |
| 435 | + } | |
| 404 | 436 | |
| 405 | 437 | if ( $endpoints ) { |
| 406 | 438 | foreach ( (array) $ep_query_append as $regex => $ep ) { |
| 407 | - if ( $ep[0] & $ep_mask || $ep[0] & $ep_mask_specific ) | |
| 439 | + if ( $ep[0] & $ep_mask || $ep[0] & $ep_mask_specific ) { | |
| 408 | 440 | $rewrite[$match . $regex] = $index . '?' . $query |
| 409 | 441 | . $ep[1] . $wp_rewrite->preg_index( $num_toks + 2 ); |
| 442 | + } | |
| 410 | 443 | } |
| 411 | 444 | } |
| 412 | 445 | |
| 413 | 446 | if ( $num_toks ) { |
| @@ -424,10 +457,11 @@ | ||
| 424 | 457 | && strpos( $struct, '%minute%' ) !== false |
| 425 | 458 | && strpos( $struct, '%second%' ) !== false ) ) { |
| 426 | 459 | $post = true; |
| 427 | 460 | |
| 428 | - if ( strpos( $struct, '%pagename%' ) !== false ) | |
| 461 | + if ( strpos( $struct, '%pagename%' ) !== false ) { | |
| 429 | 462 | $page = true; |
| 463 | + } | |
| 430 | 464 | } |
| 431 | 465 | |
| 432 | 466 | if ( ! $post ) { |
| 433 | 467 | foreach ( get_post_types( array( '_builtin' => false ) ) as $ptype ) { |
| @@ -441,8 +475,12 @@ | ||
| 441 | 475 | |
| 442 | 476 | if ( $post ) { |
| 443 | 477 | $trackbackmatch = $match . $trackbackregex; |
| 444 | 478 | $trackbackquery = $trackbackindex . '?' . $query . '&tb=1'; |
| 479 | + | |
| 480 | + $embedmatch = $match . $embedregex; | |
| 481 | + $embedquery = $embedindex . '?' . $query . '&embed=true'; | |
| 482 | + | |
| 445 | 483 | $match = rtrim( $match, '/' ); |
| 446 | 484 | $submatchbase = preg_replace( '/\(([^?].+?)\)/', '(?:$1)', $match ); |
| 447 | 485 | |
| 448 | 486 | $sub1 = $submatchbase . '/([^/]+)/'; |
| @@ -449,8 +487,9 @@ | ||
| 449 | 487 | $sub1tb = $sub1 . $trackbackregex; |
| 450 | 488 | $sub1feed = $sub1 . $feedregex; |
| 451 | 489 | $sub1feed2 = $sub1 . $feedregex2; |
| 452 | 490 | $sub1comment = $sub1 . $commentregex; |
| 491 | + $sub1embed = $sub1 . $embedregex; | |
| 453 | 492 | |
| 454 | 493 | $sub2 = $submatchbase . '/attachment/([^/]+)/'; |
| 455 | 494 | $sub2tb = $sub2 . $trackbackregex; |
| 456 | 495 | $sub2feed = $sub2 . $feedregex; |
| @@ -455,13 +494,15 @@ | ||
| 455 | 494 | $sub2tb = $sub2 . $trackbackregex; |
| 456 | 495 | $sub2feed = $sub2 . $feedregex; |
| 457 | 496 | $sub2feed2 = $sub2 . $feedregex2; |
| 458 | 497 | $sub2comment = $sub2 . $commentregex; |
| 498 | + $sub2embed = $sub2 . $embedregex; | |
| 459 | 499 | |
| 460 | 500 | $subquery = $index . '?attachment=' . $wp_rewrite->preg_index( 1 ); |
| 461 | 501 | $subtbquery = $subquery . '&tb=1'; |
| 462 | 502 | $subfeedquery = $subquery . '&feed=' . $wp_rewrite->preg_index( 2 ); |
| 463 | 503 | $subcommentquery = $subquery . '&cpage=' . $wp_rewrite->preg_index( 2 ); |
| 504 | + $subembedquery = $subquery . '&embed=true'; | |
| 464 | 505 | |
| 465 | 506 | if ( ! empty( $endpoints ) ) { |
| 466 | 507 | foreach ( (array) $ep_query_append as $regex => $ep ) { |
| 467 | 508 | if ( $ep[0] & EP_ATTACHMENT ) { |
| @@ -488,8 +529,10 @@ | ||
| 488 | 529 | |
| 489 | 530 | if ( $post ) { |
| 490 | 531 | $rewrite = array_merge( array( $trackbackmatch => $trackbackquery ), $rewrite ); |
| 491 | 532 | |
| 533 | + $rewrite = array_merge( array( $embedmatch => $embedquery ), $rewrite ); | |
| 534 | + | |
| 492 | 535 | if ( ! $page ) { |
| 493 | 536 | $rewrite = array_merge( $rewrite, array( |
| 494 | 537 | $sub1 => $subquery, |
| 495 | 538 | $sub1tb => $subtbquery, |
| @@ -494,9 +537,10 @@ | ||
| 494 | 537 | $sub1 => $subquery, |
| 495 | 538 | $sub1tb => $subtbquery, |
| 496 | 539 | $sub1feed => $subfeedquery, |
| 497 | 540 | $sub1feed2 => $subfeedquery, |
| 498 | - $sub1comment => $subcommentquery ) ); | |
| 541 | + $sub1comment => $subcommentquery, | |
| 542 | + $sub1embed => $subembedquery ) ); | |
| 499 | 543 | } |
| 500 | 544 | |
| 501 | 545 | $rewrite = array_merge( array( |
| 502 | 546 | $sub2 => $subquery, |
| @@ -502,9 +546,10 @@ | ||
| 502 | 546 | $sub2 => $subquery, |
| 503 | 547 | $sub2tb => $subtbquery, |
| 504 | 548 | $sub2feed => $subfeedquery, |
| 505 | 549 | $sub2feed2 => $subfeedquery, |
| 506 | - $sub2comment => $subcommentquery ), $rewrite ); | |
| 550 | + $sub2comment => $subcommentquery, | |
| 551 | + $sub2embed => $subembedquery ), $rewrite ); | |
| 507 | 552 | } |
| 508 | 553 | } |
| 509 | 554 | |
| 510 | 555 | $post_rewrite = array_merge( $rewrite, $post_rewrite ); |
| @@ -511,6 +556,4 @@ | ||
| 511 | 556 | } |
| 512 | 557 | |
| 513 | 558 | return $post_rewrite; |
| 514 | 559 | } |
| 515 | - | |
| 516 | -?> | |