| 1 |
<?php |
| 2 |
|
| 3 |
add_action( 'init', 'bogo_add_rewrite_tags' ); |
| 4 |
|
| 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 ); |
| 9 |
|
| 10 |
if ( empty( $langs ) ) |
| 11 |
return; |
| 12 |
|
| 13 |
$lang_rewrite_regex = '(' . implode( '|', $langs ) . ')'; |
| 14 |
|
| 15 |
add_rewrite_tag( '%lang%', $lang_rewrite_regex, 'lang=' ); |
| 16 |
|
| 17 |
$old_regex = bogo_get_prop( 'lang_rewrite_regex' ); |
| 18 |
|
| 19 |
if ( $lang_rewrite_regex != $old_regex ) { |
| 20 |
bogo_set_prop( 'lang_rewrite_regex', $lang_rewrite_regex ); |
| 21 |
flush_rewrite_rules(); |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
add_filter( 'root_rewrite_rules', 'bogo_root_rewrite_rules' ); |
| 26 |
|
| 27 |
function bogo_root_rewrite_rules( $root_rewrite ) { |
| 28 |
global $wp_rewrite; |
| 29 |
|
| 30 |
$permastruct = trailingslashit( $wp_rewrite->root ) . '%lang%/'; |
| 31 |
|
| 32 |
$extra = bogo_generate_rewrite_rules( $permastruct, array( |
| 33 |
'ep_mask' => EP_ROOT ) ); |
| 34 |
|
| 35 |
return array_merge( $extra, $root_rewrite ); |
| 36 |
} |
| 37 |
|
| 38 |
add_filter( 'post_rewrite_rules', 'bogo_post_rewrite_rules' ); |
| 39 |
|
| 40 |
function bogo_post_rewrite_rules( $post_rewrite ) { |
| 41 |
global $wp_rewrite; |
| 42 |
|
| 43 |
$permastruct = $wp_rewrite->permalink_structure; |
| 44 |
|
| 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 ); |
| 51 |
|
| 52 |
$extra = bogo_generate_rewrite_rules( $permastruct, array( |
| 53 |
'ep_mask' => EP_PERMALINK, |
| 54 |
'paged' => false ) ); |
| 55 |
|
| 56 |
return array_merge( $extra, $post_rewrite ); |
| 57 |
} |
| 58 |
|
| 59 |
add_filter( 'date_rewrite_rules', 'bogo_date_rewrite_rules' ); |
| 60 |
|
| 61 |
function bogo_date_rewrite_rules( $date_rewrite ) { |
| 62 |
global $wp_rewrite; |
| 63 |
|
| 64 |
$permastruct = $wp_rewrite->get_date_permastruct(); |
| 65 |
|
| 66 |
$permastruct = preg_replace( |
| 67 |
'#^' . $wp_rewrite->front . '#', |
| 68 |
'/%lang%' . $wp_rewrite->front, |
| 69 |
$permastruct ); |
| 70 |
|
| 71 |
$extra = bogo_generate_rewrite_rules( $permastruct, array( |
| 72 |
'ep_mask' => EP_DATE ) ); |
| 73 |
|
| 74 |
return array_merge( $extra, $date_rewrite ); |
| 75 |
} |
| 76 |
|
| 77 |
add_filter( 'comments_rewrite_rules', 'bogo_comments_rewrite_rules' ); |
| 78 |
|
| 79 |
function bogo_comments_rewrite_rules( $comments_rewrite ) { |
| 80 |
global $wp_rewrite; |
| 81 |
|
| 82 |
$permastruct = trailingslashit( $wp_rewrite->root ) . '%lang%/' . $wp_rewrite->comments_base; |
| 83 |
|
| 84 |
$extra = bogo_generate_rewrite_rules( $permastruct, array( |
| 85 |
'ep_mask' => EP_COMMENTS, |
| 86 |
'forcomments' => true, |
| 87 |
'walk_dirs' => false ) ); |
| 88 |
|
| 89 |
return array_merge( $extra, $comments_rewrite ); |
| 90 |
} |
| 91 |
|
| 92 |
add_filter( 'search_rewrite_rules', 'bogo_search_rewrite_rules' ); |
| 93 |
|
| 94 |
function bogo_search_rewrite_rules( $search_rewrite ) { |
| 95 |
global $wp_rewrite; |
| 96 |
|
| 97 |
$permastruct = trailingslashit( $wp_rewrite->root ) . '%lang%/' |
| 98 |
. $wp_rewrite->search_base . '/%search%'; |
| 99 |
|
| 100 |
$extra = bogo_generate_rewrite_rules( $permastruct, array( |
| 101 |
'ep_mask' => EP_SEARCH ) ); |
| 102 |
|
| 103 |
return array_merge( $extra, $search_rewrite ); |
| 104 |
} |
| 105 |
|
| 106 |
add_filter( 'author_rewrite_rules', 'bogo_author_rewrite_rules' ); |
| 107 |
|
| 108 |
function bogo_author_rewrite_rules( $author_rewrite ) { |
| 109 |
global $wp_rewrite; |
| 110 |
|
| 111 |
$permastruct = $wp_rewrite->get_author_permastruct(); |
| 112 |
|
| 113 |
$permastruct = preg_replace( |
| 114 |
'#^' . $wp_rewrite->front . '#', |
| 115 |
'/%lang%' . $wp_rewrite->front, |
| 116 |
$permastruct ); |
| 117 |
|
| 118 |
$extra = bogo_generate_rewrite_rules( $permastruct, array( |
| 119 |
'ep_mask' => EP_AUTHORS ) ); |
| 120 |
|
| 121 |
return array_merge( $extra, $author_rewrite ); |
| 122 |
} |
| 123 |
|
| 124 |
add_filter( 'page_rewrite_rules', 'bogo_page_rewrite_rules' ); |
| 125 |
|
| 126 |
function bogo_page_rewrite_rules( $page_rewrite ) { |
| 127 |
global $wp_rewrite; |
| 128 |
|
| 129 |
$wp_rewrite->add_rewrite_tag( '%pagename%', '(.?.+?)', 'pagename=' ); |
| 130 |
$permastruct = trailingslashit( $wp_rewrite->root ) . '%lang%/%pagename%'; |
| 131 |
|
| 132 |
$extra = bogo_generate_rewrite_rules( $permastruct, array( |
| 133 |
'ep_mask' => EP_PAGES, |
| 134 |
'walk_dirs' => false ) ); |
| 135 |
|
| 136 |
return array_merge( $extra, $page_rewrite ); |
| 137 |
} |
| 138 |
|
| 139 |
add_filter( 'category_rewrite_rules', 'bogo_category_rewrite_rules' ); |
| 140 |
|
| 141 |
function bogo_category_rewrite_rules( $category_rewrite ) { |
| 142 |
return bogo_taxonomy_rewrite_rules( $category_rewrite, 'category', EP_CATEGORIES ); |
| 143 |
} |
| 144 |
|
| 145 |
add_filter( 'post_tag_rewrite_rules', 'bogo_post_tag_rewrite_rules' ); |
| 146 |
|
| 147 |
function bogo_post_tag_rewrite_rules( $post_tag_rewrite ) { |
| 148 |
return bogo_taxonomy_rewrite_rules( $post_tag_rewrite, 'post_tag', EP_TAGS ); |
| 149 |
} |
| 150 |
|
| 151 |
add_filter( 'post_format_rewrite_rules', 'bogo_post_format_rewrite_rules' ); |
| 152 |
|
| 153 |
function bogo_post_format_rewrite_rules( $post_format_rewrite ) { |
| 154 |
return bogo_taxonomy_rewrite_rules( $post_format_rewrite, 'post_format' ); |
| 155 |
} |
| 156 |
|
| 157 |
function bogo_taxonomy_rewrite_rules( $taxonomy_rewrite, $taxonomy, $ep_mask = EP_NONE ) { |
| 158 |
global $wp_rewrite; |
| 159 |
|
| 160 |
$permastruct = $wp_rewrite->get_extra_permastruct( $taxonomy ); |
| 161 |
|
| 162 |
$permastruct = preg_replace( |
| 163 |
'#^' . $wp_rewrite->front . '#', |
| 164 |
'/%lang%' . $wp_rewrite->front, |
| 165 |
$permastruct ); |
| 166 |
|
| 167 |
$extra = bogo_generate_rewrite_rules( $permastruct, array( |
| 168 |
'ep_mask' => $ep_mask ) ); |
| 169 |
|
| 170 |
return array_merge( $extra, $taxonomy_rewrite ); |
| 171 |
} |
| 172 |
|
| 173 |
function bogo_generate_rewrite_rules( $permalink_structure, $args = '' ) { |
| 174 |
global $wp_rewrite; |
| 175 |
|
| 176 |
$defaults = array( |
| 177 |
'ep_mask' => EP_NONE, |
| 178 |
'paged' => true, |
| 179 |
'feed' => true, |
| 180 |
'forcomments' => false, |
| 181 |
'walk_dirs' => true, |
| 182 |
'endpoints' => true ); |
| 183 |
|
| 184 |
$args = wp_parse_args( $args, $defaults ); |
| 185 |
|
| 186 |
extract( $args, EXTR_SKIP ); |
| 187 |
|
| 188 |
$feedregex2 = '(' . implode( '|', $wp_rewrite->feeds ) . ')/?$'; |
| 189 |
$feedregex = $wp_rewrite->feed_base . '/' . $feedregex2; |
| 190 |
$trackbackregex = 'trackback/?$'; |
| 191 |
$pageregex = $wp_rewrite->pagination_base . '/?([0-9]{1,})/?$'; |
| 192 |
$commentregex = 'comment-page-([0-9]{1,})/?$'; |
| 193 |
|
| 194 |
if ( $endpoints ) { |
| 195 |
$ep_query_append = array(); |
| 196 |
|
| 197 |
foreach ( (array) $wp_rewrite->endpoints as $endpoint ) { |
| 198 |
$epmatch = $endpoint[1] . '(/(.*))?/?$'; |
| 199 |
$epquery = '&' . $endpoint[1] . '='; |
| 200 |
$ep_query_append[$epmatch] = array( $endpoint[0], $epquery ); |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
$front = substr( $permalink_structure, 0, strpos( $permalink_structure, '%' ) ); |
| 205 |
preg_match_all( '/%.+?%/', $permalink_structure, $tokens ); |
| 206 |
$num_tokens = count( $tokens[0] ); |
| 207 |
$index = $wp_rewrite->index; |
| 208 |
$feedindex = $index; |
| 209 |
$trackbackindex = $index; |
| 210 |
|
| 211 |
for ( $i = 0; $i < $num_tokens; ++$i ) { |
| 212 |
if ( 0 < $i ) |
| 213 |
$queries[$i] = $queries[$i - 1] . '&'; |
| 214 |
else |
| 215 |
$queries[$i] = ''; |
| 216 |
|
| 217 |
$query_token = |
| 218 |
str_replace( $wp_rewrite->rewritecode, $wp_rewrite->queryreplace, $tokens[0][$i] ) |
| 219 |
. $wp_rewrite->preg_index( $i + 1 ); |
| 220 |
|
| 221 |
$queries[$i] .= $query_token; |
| 222 |
} |
| 223 |
|
| 224 |
$structure = $permalink_structure; |
| 225 |
|
| 226 |
if ( $front != '/' ) |
| 227 |
$structure = str_replace( $front, '', $structure ); |
| 228 |
|
| 229 |
$structure = trim( $structure, '/' ); |
| 230 |
|
| 231 |
$dirs = $walk_dirs ? explode( '/', $structure ) : array( $structure ); |
| 232 |
$num_dirs = count( $dirs ); |
| 233 |
|
| 234 |
$front = preg_replace( '|^/+|', '', $front ); |
| 235 |
|
| 236 |
$post_rewrite = array(); |
| 237 |
$struct = $front; |
| 238 |
|
| 239 |
for ( $j = 0; $j < $num_dirs; ++$j ) { |
| 240 |
$struct .= $dirs[$j] . '/'; |
| 241 |
$struct = ltrim( $struct, '/' ); |
| 242 |
$match = str_replace( $wp_rewrite->rewritecode, $wp_rewrite->rewritereplace, $struct); |
| 243 |
$num_toks = preg_match_all( '/%.+?%/', $struct, $toks ); |
| 244 |
|
| 245 |
$query = ( isset( $queries ) && is_array( $queries ) && ! empty( $num_toks ) ) |
| 246 |
? $queries[$num_toks - 1] : ''; |
| 247 |
|
| 248 |
switch ( $dirs[$j] ) { |
| 249 |
case '%year%': |
| 250 |
$ep_mask_specific = EP_YEAR; |
| 251 |
break; |
| 252 |
case '%monthnum%': |
| 253 |
$ep_mask_specific = EP_MONTH; |
| 254 |
break; |
| 255 |
case '%day%': |
| 256 |
$ep_mask_specific = EP_DAY; |
| 257 |
break; |
| 258 |
default: |
| 259 |
$ep_mask_specific = EP_NONE; |
| 260 |
} |
| 261 |
|
| 262 |
$pagematch = $match . $pageregex; |
| 263 |
$pagequery = $index . '?' . $query |
| 264 |
. '&paged=' . $wp_rewrite->preg_index( $num_toks + 1 ); |
| 265 |
|
| 266 |
$commentmatch = $match . $commentregex; |
| 267 |
$commentquery = $index . '?' . $query |
| 268 |
. '&cpage=' . $wp_rewrite->preg_index( $num_toks + 1 ); |
| 269 |
|
| 270 |
if ( get_option( 'page_on_front' ) ) { |
| 271 |
$rootcommentmatch = $match . $commentregex; |
| 272 |
$rootcommentquery = $index . '?' . $query |
| 273 |
. '&page_id=' . get_option( 'page_on_front' ) |
| 274 |
. '&cpage=' . $wp_rewrite->preg_index( $num_toks + 1 ); |
| 275 |
} |
| 276 |
|
| 277 |
$feedmatch = $match . $feedregex; |
| 278 |
$feedquery = $feedindex . '?' . $query |
| 279 |
. '&feed=' . $wp_rewrite->preg_index( $num_toks + 1 ); |
| 280 |
|
| 281 |
$feedmatch2 = $match . $feedregex2; |
| 282 |
$feedquery2 = $feedindex . '?' . $query |
| 283 |
. '&feed=' . $wp_rewrite->preg_index( $num_toks + 1 ); |
| 284 |
|
| 285 |
if ( $forcomments ) { |
| 286 |
$feedquery .= '&withcomments=1'; |
| 287 |
$feedquery2 .= '&withcomments=1'; |
| 288 |
} |
| 289 |
|
| 290 |
$rewrite = array(); |
| 291 |
|
| 292 |
if ( $feed ) |
| 293 |
$rewrite = array( $feedmatch => $feedquery, $feedmatch2 => $feedquery2 ); |
| 294 |
|
| 295 |
if ( $paged ) |
| 296 |
$rewrite = array_merge( $rewrite, array( $pagematch => $pagequery ) ); |
| 297 |
|
| 298 |
if ( EP_PAGES & $ep_mask || EP_PERMALINK & $ep_mask ) |
| 299 |
$rewrite = array_merge( $rewrite, array( $commentmatch => $commentquery ) ); |
| 300 |
else if ( EP_ROOT & $ep_mask && get_option( 'page_on_front' ) ) |
| 301 |
$rewrite = array_merge( $rewrite, array( $rootcommentmatch => $rootcommentquery ) ); |
| 302 |
|
| 303 |
if ( $endpoints ) { |
| 304 |
foreach ( (array) $ep_query_append as $regex => $ep ) { |
| 305 |
if ( $ep[0] & $ep_mask || $ep[0] & $ep_mask_specific ) |
| 306 |
$rewrite[$match . $regex] = $index . '?' . $query |
| 307 |
. $ep[1] . $wp_rewrite->preg_index( $num_toks + 2 ); |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
if ( $num_toks ) { |
| 312 |
$post = false; |
| 313 |
$page = false; |
| 314 |
|
| 315 |
if ( strpos( $struct, '%postname%' ) !== false |
| 316 |
|| strpos( $struct, '%post_id%' ) !== false |
| 317 |
|| strpos( $struct, '%pagename%' ) !== false |
| 318 |
|| ( strpos( $struct, '%year%' ) !== false |
| 319 |
&& strpos( $struct, '%monthnum%' ) !== false |
| 320 |
&& strpos( $struct, '%day%' ) !== false |
| 321 |
&& strpos( $struct, '%hour%' ) !== false |
| 322 |
&& strpos( $struct, '%minute%' ) !== false |
| 323 |
&& strpos( $struct, '%second%' ) !== false ) ) { |
| 324 |
$post = true; |
| 325 |
|
| 326 |
if ( strpos( $struct, '%pagename%' ) !== false ) |
| 327 |
$page = true; |
| 328 |
} |
| 329 |
|
| 330 |
if ( ! $post ) { |
| 331 |
foreach ( get_post_types( array( '_builtin' => false ) ) as $ptype ) { |
| 332 |
if ( strpos( $struct, "%$ptype%" ) !== false ) { |
| 333 |
$post = true; |
| 334 |
$page = is_post_type_hierarchical( $ptype ); |
| 335 |
break; |
| 336 |
} |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
if ( $post ) { |
| 341 |
$trackbackmatch = $match . $trackbackregex; |
| 342 |
$trackbackquery = $trackbackindex . '?' . $query . '&tb=1'; |
| 343 |
$match = rtrim( $match, '/' ); |
| 344 |
$submatchbase = preg_replace( '/\(([^?].+?)\)/', '(?:$1)', $match ); |
| 345 |
|
| 346 |
$sub1 = $submatchbase . '/([^/]+)/'; |
| 347 |
$sub1tb = $sub1 . $trackbackregex; |
| 348 |
$sub1feed = $sub1 . $feedregex; |
| 349 |
$sub1feed2 = $sub1 . $feedregex2; |
| 350 |
$sub1comment = $sub1 . $commentregex; |
| 351 |
|
| 352 |
$sub2 = $submatchbase . '/attachment/([^/]+)/'; |
| 353 |
$sub2tb = $sub2 . $trackbackregex; |
| 354 |
$sub2feed = $sub2 . $feedregex; |
| 355 |
$sub2feed2 = $sub2 . $feedregex2; |
| 356 |
$sub2comment = $sub2 . $commentregex; |
| 357 |
|
| 358 |
$subquery = $index . '?attachment=' . $wp_rewrite->preg_index( 1 ); |
| 359 |
$subtbquery = $subquery . '&tb=1'; |
| 360 |
$subfeedquery = $subquery . '&feed=' . $wp_rewrite->preg_index( 2 ); |
| 361 |
$subcommentquery = $subquery . '&cpage=' . $wp_rewrite->preg_index( 2 ); |
| 362 |
|
| 363 |
if ( ! empty( $endpoints ) ) { |
| 364 |
foreach ( (array) $ep_query_append as $regex => $ep ) { |
| 365 |
if ( $ep[0] & EP_ATTACHMENT ) { |
| 366 |
$rewrite[$sub1 . $regex] = |
| 367 |
$subquery . $ep[1] . $wp_rewrite->preg_index( 2 ); |
| 368 |
$rewrite[$sub2 . $regex] = |
| 369 |
$subquery . $ep[1] . $wp_rewrite->preg_index( 2 ); |
| 370 |
} |
| 371 |
} |
| 372 |
} |
| 373 |
|
| 374 |
$sub1 .= '?$'; |
| 375 |
$sub2 .= '?$'; |
| 376 |
|
| 377 |
$match = $match . '(/[0-9]+)?/?$'; |
| 378 |
$query = $index . '?' . $query |
| 379 |
. '&page=' . $wp_rewrite->preg_index( $num_toks + 1 ); |
| 380 |
} else { |
| 381 |
$match .= '?$'; |
| 382 |
$query = $index . '?' . $query; |
| 383 |
} |
| 384 |
|
| 385 |
$rewrite = array_merge( $rewrite, array( $match => $query ) ); |
| 386 |
|
| 387 |
if ( $post ) { |
| 388 |
$rewrite = array_merge( array( $trackbackmatch => $trackbackquery ), $rewrite ); |
| 389 |
|
| 390 |
if ( ! $page ) { |
| 391 |
$rewrite = array_merge( $rewrite, array( |
| 392 |
$sub1 => $subquery, |
| 393 |
$sub1tb => $subtbquery, |
| 394 |
$sub1feed => $subfeedquery, |
| 395 |
$sub1feed2 => $subfeedquery, |
| 396 |
$sub1comment => $subcommentquery ) ); |
| 397 |
} |
| 398 |
|
| 399 |
$rewrite = array_merge( array( |
| 400 |
$sub2 => $subquery, |
| 401 |
$sub2tb => $subtbquery, |
| 402 |
$sub2feed => $subfeedquery, |
| 403 |
$sub2feed2 => $subfeedquery, |
| 404 |
$sub2comment => $subcommentquery ), $rewrite ); |
| 405 |
} |
| 406 |
} |
| 407 |
|
| 408 |
$post_rewrite = array_merge( $rewrite, $post_rewrite ); |
| 409 |
} |
| 410 |
|
| 411 |
return $post_rewrite; |
| 412 |
} |
| 413 |
|
| 414 |
?> |