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