PluginActiveEvents.php
3 weeks ago
PluginDeactivateEvents.php
3 weeks ago
PluginInitEvents.php
1 month ago
PluginLoadedEvents.php
3 weeks ago
PluginShortcode.php
2 months ago
TemplateRedirection.php
1 month ago
TemplateRedirection.php
277 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Plugin update events handler |
| 5 | * |
| 6 | * @package kirki |
| 7 | */ |
| 8 | |
| 9 | namespace Kirki\Manager; |
| 10 | |
| 11 | use Kirki\Ajax\Page; |
| 12 | use Kirki\Ajax\Taxonomy; |
| 13 | use Kirki\Ajax\Users; |
| 14 | use Kirki\HelperFunctions; |
| 15 | |
| 16 | if ( ! defined( 'ABSPATH' ) ) { |
| 17 | exit; // Exit if accessed directly. |
| 18 | } |
| 19 | |
| 20 | /** |
| 21 | * Do some task on WordPress template redirection |
| 22 | */ |
| 23 | class TemplateRedirection { |
| 24 | |
| 25 | |
| 26 | /** |
| 27 | * Initilize the class |
| 28 | * |
| 29 | * @return void |
| 30 | */ |
| 31 | public function __construct() { |
| 32 | /* update the the content with our kirki data if page is kirki */ |
| 33 | add_filter( 'template_include', array( $this, 'load_page_template_to_check_staging' ), PHP_INT_MAX ); |
| 34 | |
| 35 | add_action( 'login_form_login', array( $this, 'redirect_to_custom_login_if_has_kirki_utility_page' ) ); |
| 36 | add_action( 'login_form_register', array( $this, 'redirect_to_custom_register_if_has_kirki_utility_page' ) ); |
| 37 | add_action( 'login_form_lostpassword', array( $this, 'redirect_to_custom_lostpassword_if_has_kirki_utility_page' ) ); |
| 38 | add_action( 'login_form_rp', array( $this, 'redirect_to_custom_rp_if_has_kirki_utility_page' ) ); |
| 39 | add_action( 'login_form_resetpass', array( $this, 'redirect_to_custom_resetpass_if_has_kirki_utility_page' ) ); |
| 40 | |
| 41 | add_filter( 'post_link', array( $this, 'custom_utility_page_link' ), 10, 2 ); |
| 42 | add_filter( 'query_vars', array( $this, 'kirki_utility_pages_query_vars' ) ); |
| 43 | } |
| 44 | |
| 45 | public function custom_utility_page_link( $permalink, $post ) { |
| 46 | if ( $post && $post->post_type =='kirki_utility' ) { |
| 47 | return home_url( '/' . $post->post_name ); |
| 48 | } |
| 49 | return $permalink; |
| 50 | } |
| 51 | public function redirect_to_custom_login_if_has_kirki_utility_page() { |
| 52 | $this->check_and_redirect_to_custom_utility_page( 'login' ); |
| 53 | } |
| 54 | public function redirect_to_custom_register_if_has_kirki_utility_page() { |
| 55 | $this->check_and_redirect_to_custom_utility_page( 'sign_up' ); |
| 56 | } |
| 57 | public function redirect_to_custom_lostpassword_if_has_kirki_utility_page() { |
| 58 | $this->check_and_redirect_to_custom_utility_page( 'forgot_password' ); |
| 59 | } |
| 60 | public function redirect_to_custom_rp_if_has_kirki_utility_page() { |
| 61 | $this->check_and_redirect_to_custom_utility_page( 'reset_password' ); |
| 62 | } |
| 63 | public function redirect_to_custom_resetpass_if_has_kirki_utility_page() { |
| 64 | $this->check_and_redirect_to_custom_utility_page( 'forgot_password' ); |
| 65 | } |
| 66 | |
| 67 | private function check_and_redirect_to_custom_utility_page( $type ) { |
| 68 | $utility_pages = Page::fetch_list('kirki_utility', true, array( 'publish' ) ); |
| 69 | foreach ( $utility_pages as $key => $page ) { |
| 70 | $utility_page_type = $page['utility_page_type']; |
| 71 | if ( $utility_page_type == $type ) { |
| 72 | // Get current GET parameters |
| 73 | $query_args = $_GET; |
| 74 | |
| 75 | // Base redirect URL |
| 76 | $url = site_url( '/' . $page['slug'] ); |
| 77 | |
| 78 | // Append existing GET params if any |
| 79 | if ( ! empty( $query_args ) ) { |
| 80 | $url = add_query_arg( $query_args, $url ); |
| 81 | } |
| 82 | |
| 83 | wp_redirect( $url ); |
| 84 | exit; // Always call exit after wp_redirect to stop execution |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | |
| 90 | public function load_page_template_to_check_staging( $original ) { |
| 91 | $staging_version = isset( $_GET['staging_version'] ) ? intval( HelperFunctions::sanitize_text( $_GET['staging_version'] ) ) : false; |
| 92 | $nonce = HelperFunctions::sanitize_text( isset( $_GET['nonce'] ) ? $_GET['nonce'] : 'false' ); |
| 93 | $preview_staging_version = ( $staging_version && wp_verify_nonce( $nonce, 'kirki_preview_staging_nonce' ) ) ? $staging_version : false; |
| 94 | return $this->load_page_template( $original, $preview_staging_version ); |
| 95 | } |
| 96 | |
| 97 | public function kirki_utility_pages_query_vars( $vars ) { |
| 98 | $vars[] = 'kirki_utility_page_type'; |
| 99 | $vars[] = 'kirki_utility_page_id'; |
| 100 | return $vars; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Load kirki page template |
| 105 | * it will include the template file insted of original template file |
| 106 | * $loadForIframe = true if load for iframe |
| 107 | * |
| 108 | * @param string $original wp action for template file load. |
| 109 | * @return string template name. |
| 110 | */ |
| 111 | public function load_page_template( $original, $staging_version = false ) { |
| 112 | $original = HelperFunctions::normalize_kirki_full_canvas_template_path($original); |
| 113 | $post_id = HelperFunctions::get_post_id_if_possible_from_url(); |
| 114 | $post = null; |
| 115 | if ( $post_id ) { |
| 116 | // this is for template and utility preview |
| 117 | $post = get_post( $post_id ); |
| 118 | if ( $post && $post->post_type ==='kirki_template' ) { |
| 119 | // this is for template preview.(kirki_template post type) |
| 120 | $template_content = apply_filters( 'the_content', get_the_content( null, false, $post_id ) ); |
| 121 | |
| 122 | $custom_data = array( |
| 123 | 'kirki_template_content' => $template_content, // Example: Get the current post ID |
| 124 | 'kirki_template_id' => $post->ID, |
| 125 | ); |
| 126 | // Set a global variable with custom data to make it available in the template |
| 127 | set_query_var('kirki_custom_data', $custom_data ); |
| 128 | |
| 129 | if ( file_exists( HelperFunctions::get_kirki_full_canvas_template_path() ) ) { |
| 130 | $original = HelperFunctions::get_kirki_full_canvas_template_path(); |
| 131 | } |
| 132 | } elseif ( $post && $post->post_type ==='kirki_utility' ) { |
| 133 | // this is for template preview.(kirki_template post type) |
| 134 | $custom_post_content = apply_filters( 'the_content', get_the_content( null, false, $post_id ) ); |
| 135 | |
| 136 | $custom_data = array( |
| 137 | 'kirki_custom_post_content' => $custom_post_content, // Example: Get the current post ID |
| 138 | 'kirki_custom_post_id' => $post->ID, |
| 139 | ); |
| 140 | // Set a global variable with custom data to make it available in the template |
| 141 | set_query_var('kirki_custom_data', $custom_data ); |
| 142 | |
| 143 | if ( file_exists( HelperFunctions::get_kirki_full_canvas_template_path() ) ) { |
| 144 | $original = HelperFunctions::get_kirki_full_canvas_template_path(); |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | if ( $original !== HelperFunctions::get_kirki_full_canvas_template_path() ) { |
| 150 | $context = HelperFunctions::get_current_page_context(); // {id, type} |
| 151 | if ( ! empty( $context ) ) { |
| 152 | switch ( $context['type'] ) { |
| 153 | case 'user':{ |
| 154 | $user = Users::get_user_by_id( $context['id'] ); |
| 155 | $template = HelperFunctions::find_template_for_this_context( 'user', $user ); |
| 156 | if ( $template ) { |
| 157 | $options = array(); |
| 158 | $options['user'] = $user; |
| 159 | $original = $this->set_template_or_post_content_for_kirki_full_canvas_template( $template, $options, $original, $staging_version ); |
| 160 | } |
| 161 | break; |
| 162 | } |
| 163 | case 'post':{ |
| 164 | $template = HelperFunctions::find_template_for_this_context( 'post', $post ); |
| 165 | if ( $template ) { |
| 166 | $options = array(); |
| 167 | $original = $this->set_template_or_post_content_for_kirki_full_canvas_template( $template, $options, $original, $staging_version ); |
| 168 | } else { |
| 169 | if ( is_page() ) { |
| 170 | $template_name = get_post_meta( $post_id, '_wp_page_template', true ); |
| 171 | $template_name = HelperFunctions::normalize_kirki_full_canvas_template_path($template_name); |
| 172 | if ( ! empty( $template_name ) && $template_name !== $original && HelperFunctions::get_kirki_full_canvas_template_path() === $template_name ) { |
| 173 | add_action( 'wp_enqueue_scripts', array( new HelperFunctions(), 'remove_theme_style' ) ); // we can remove it. need to rethink. dequeue_all_except_my_plugin |
| 174 | $original = $template_name; |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | break; |
| 179 | } |
| 180 | case 'term': { |
| 181 | $term = Taxonomy::get_term_by_id( $context['id'] ); |
| 182 | |
| 183 | $template = HelperFunctions::find_template_for_this_context( 'term', $term ); |
| 184 | |
| 185 | if ( $template ) { |
| 186 | $options = array(); |
| 187 | $options['term'] = $term; |
| 188 | $original = $this->set_template_or_post_content_for_kirki_full_canvas_template( $template, $options, $original, $staging_version ); |
| 189 | } |
| 190 | break; |
| 191 | } |
| 192 | |
| 193 | case '404':{ |
| 194 | $template = HelperFunctions::find_utility_page_for_this_context( '404', 'type' ); |
| 195 | $options = array(); |
| 196 | $original = $this->set_template_or_post_content_for_kirki_full_canvas_template( $template, $options, $original, $staging_version ); |
| 197 | break; |
| 198 | } |
| 199 | case 'kirki_utility':{ |
| 200 | $kirki_utility_page_type = $context['kirki_utility_page_type']; |
| 201 | $kirki_utility_page_id = $context['kirki_utility_page_id']; |
| 202 | if ( HelperFunctions::check_utility_page_visibility_condition( $kirki_utility_page_type ) ) { |
| 203 | $template = HelperFunctions::find_utility_page_for_this_context( $kirki_utility_page_id, 'id' ); |
| 204 | $options = array(); |
| 205 | $original = $this->set_template_or_post_content_for_kirki_full_canvas_template( $template, $options, $original, $staging_version ); |
| 206 | } |
| 207 | break; |
| 208 | } |
| 209 | default:{ |
| 210 | break; |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | global $kirki_custom_header, $kirki_custom_footer; |
| 217 | $kirki_custom_header = HelperFunctions::get_page_custom_section( 'header' ); |
| 218 | $kirki_custom_footer = HelperFunctions::get_page_custom_section( 'footer' ); |
| 219 | |
| 220 | if ( $original !== HelperFunctions::get_kirki_full_canvas_template_path() ) { |
| 221 | if ( $kirki_custom_header || $kirki_custom_footer ) { |
| 222 | // $original = HelperFunctions::get_kirki_full_canvas_template_path(); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | return $original; |
| 227 | } |
| 228 | private function set_template_or_post_content_for_kirki_full_canvas_template( $template, $options, $original_template_path, $staging_version = false ) { |
| 229 | if ( ! $template ) { |
| 230 | return $original_template_path; |
| 231 | } |
| 232 | |
| 233 | $kirki_data = HelperFunctions::is_kirki_type_data( $template['id'], $staging_version ); |
| 234 | |
| 235 | if ( $kirki_data ) { |
| 236 | |
| 237 | $params = array( |
| 238 | 'blocks' => $kirki_data['blocks'], |
| 239 | 'style_blocks' => $kirki_data['styles'], |
| 240 | 'root' => 'body', |
| 241 | 'post_id' => $template['id'], |
| 242 | 'options' => $options, |
| 243 | ); |
| 244 | |
| 245 | $template_content = HelperFunctions::get_html_using_preview_script( $params ); |
| 246 | // $template_content .= HelperFunctions::get_page_popups_html(); //need to check all popup front end render logic |
| 247 | $custom_data = array(); |
| 248 | |
| 249 | if ( $template['post_type'] ==='kirki_utility' ) { |
| 250 | $custom_data = array( |
| 251 | 'kirki_custom_post_content' => $template_content, // Example: Get the current post ID |
| 252 | 'kirki_custom_post_id' => $template['id'], |
| 253 | ); |
| 254 | } else { |
| 255 | $custom_data = array( |
| 256 | 'kirki_template_content' => $template_content, // Example: Get the current post ID |
| 257 | 'kirki_template_id' => $template['id'], |
| 258 | ); |
| 259 | } |
| 260 | |
| 261 | // Set a global variable with custom data to make it available in the template |
| 262 | set_query_var('kirki_custom_data', $custom_data ); |
| 263 | |
| 264 | add_filter( |
| 265 | 'kirki_assets_should_load', |
| 266 | function () { |
| 267 | return true; |
| 268 | } |
| 269 | ); |
| 270 | if ( file_exists( HelperFunctions::get_kirki_full_canvas_template_path() ) ) { |
| 271 | return HelperFunctions::get_kirki_full_canvas_template_path(); |
| 272 | } |
| 273 | } |
| 274 | return $original_template_path; |
| 275 | } |
| 276 | } |
| 277 |