| @@ -1,0 +1,42 @@ | ||
| 1 | +<?php | |
| 2 | +namespace ABlocks; | |
| 3 | + | |
| 4 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 5 | + exit; | |
| 6 | +} | |
| 7 | + | |
| 8 | +class PermalinkRewrite { | |
| 9 | + public static function init() { | |
| 10 | + $self = new self(); | |
| 11 | + add_filter( 'query_vars', array( $self, 'register_query_vars' ) ); | |
| 12 | + add_action( 'generate_rewrite_rules', array( $self, 'add_rewrite_rules' ) ); | |
| 13 | + } | |
| 14 | + public function register_query_vars( $query_vars ) { | |
| 15 | + $query_vars[] = 'ablocks_dashboard_page'; | |
| 16 | + $query_vars[] = 'ablocks_dashboard_sub_page'; | |
| 17 | + return $query_vars; | |
| 18 | + } | |
| 19 | + public function add_rewrite_rules( $wp_rewrite ) { | |
| 20 | + // Frontend Dashboard | |
| 21 | + $dashboard_page_id = (int) Helper::get_settings( 'frontend_dashboard_page' ); | |
| 22 | + if ( ! $dashboard_page_id ) { | |
| 23 | + return; | |
| 24 | + } | |
| 25 | + $dashboard_page_slug = get_post_field( 'post_name', $dashboard_page_id ); | |
| 26 | + $dashboard_pages = json_decode( get_option( ABLOCKS_FRONTEND_DASHBOARD_SUB_PAGES_SETTINGS_NAME, '{}' ), true ); | |
| 27 | + foreach ( $dashboard_pages as $dashboard_page ) { | |
| 28 | + $dashboard_key = $dashboard_page['slug']; | |
| 29 | + $new_rules[ "({$dashboard_page_slug})/{$dashboard_key}/?$" ] = 'index.php?pagename=' . $wp_rewrite->preg_index( 1 ) . '&ablocks_dashboard_page=' . $dashboard_key; | |
| 30 | + $new_rules[ "({$dashboard_page_slug})/{$dashboard_key}/(.+?)/?$" ] = 'index.php?pagename=' . $wp_rewrite->preg_index( 1 ) . '&ablocks_dashboard_page=' . $dashboard_key . '&ablocks_dashboard_sub_page=' . $wp_rewrite->preg_index( 2 ); | |
| 31 | + // Child Items | |
| 32 | + if ( isset( $dashboard_page['child_items'] ) && is_array( $dashboard_page['child_items'] ) ) { | |
| 33 | + foreach ( $dashboard_page['child_items'] as $child_key => $child_page ) { | |
| 34 | + $new_rules[ "({$dashboard_page_slug})/{$dashboard_key}/{$child_key}/?$" ] = 'index.php?pagename=' . $wp_rewrite->preg_index( 1 ) . '&ablocks_dashboard_page=' . $dashboard_key; | |
| 35 | + $new_rules[ "({$dashboard_page_slug})/{$dashboard_key}/{$child_key}/(.+?)/?$" ] = 'index.php?pagename=' . $wp_rewrite->preg_index( 1 ) . '&ablocks_dashboard_page=' . $dashboard_key . '&ablocks_dashboard_sub_page=' . $child_key; | |
| 36 | + } | |
| 37 | + } | |
| 38 | + } | |
| 39 | + | |
| 40 | + $wp_rewrite->rules = ( $new_rules ?? [] ) + $wp_rewrite->rules; | |
| 41 | + } | |
| 42 | +} | |