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