1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* FooGallery Album Rewrite Rules |
4
|
|
|
*/ |
5
|
|
|
if (!class_exists('FooGallery_Album_Rewrite_Rules')) { |
6
|
|
|
|
7
|
|
|
class FooGallery_Album_Rewrite_Rules { |
8
|
|
|
|
9
|
|
|
function __construct() { |
10
|
|
|
add_action( 'init', array( $this, 'add_gallery_endpoint' ) ); |
11
|
|
|
add_filter( 'redirect_canonical', array( $this, 'disable_canonical_redirect_for_front_page' ), 10, 2 ); |
12
|
|
|
add_action( 'update_option_page_on_front', array( $this, 'flush_rules' ) ); |
13
|
|
|
} |
14
|
|
|
|
15
|
|
|
function add_gallery_endpoint() { |
16
|
|
|
$gallery_slug = foogallery_album_gallery_url_slug(); |
17
|
|
|
|
18
|
|
|
// Ensures the $query_vars['item'] is available |
19
|
|
|
add_rewrite_tag( "%{$gallery_slug}%", '([^&]+)' ); |
20
|
|
|
|
21
|
|
|
// Requires flushing endpoints whenever the front page is switched to a different page |
22
|
|
|
$page_on_front = get_option( 'page_on_front' ); |
23
|
|
|
|
24
|
|
|
// Match the front page and pass item value as a query var. |
25
|
|
|
add_rewrite_rule( "^{$gallery_slug}/([^/]*)/?", 'index.php?page_id='.$page_on_front.'&'.$gallery_slug.'=$matches[1]', 'top' ); |
26
|
|
|
// Match non-front page pages. |
27
|
|
|
add_rewrite_rule( "^(.*)/{$gallery_slug}/([^/]*)/?", 'index.php?pagename=$matches[1]&static=true&'.$gallery_slug.'=$matches[2]', 'top' ); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
// http://wordpress.stackexchange.com/a/220484/52463 |
31
|
|
|
// In order to keep WordPress from forcing a redirect to the canonical |
32
|
|
|
// home page, the redirect needs to be disabled. |
33
|
|
|
function disable_canonical_redirect_for_front_page( $redirect_url, $requested_url ) { |
|
|
|
|
34
|
|
|
if ( is_page() && $front_page = get_option( 'page_on_front' ) ) { |
35
|
|
|
if ( is_page( $front_page ) ) { |
36
|
|
|
$redirect_url = false; |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
return $redirect_url; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
function flush_rules() { |
|
|
|
|
44
|
|
|
flush_rewrite_rules(); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.