Preview
4 weeks ago
views
1 month ago
Editor.php
2 weeks ago
Iframe.php
2 months ago
TheFrontend.php
2 months ago
TheFrontendHooks.php
2 months ago
TheFrontendHooks.php
179 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Front end post hooks and content controller |
| 4 | * |
| 5 | * @package kirki |
| 6 | */ |
| 7 | |
| 8 | namespace Kirki\Frontend; |
| 9 | |
| 10 | if ( ! defined( 'ABSPATH' ) ) { |
| 11 | exit; // Exit if accessed directly. |
| 12 | } |
| 13 | |
| 14 | use Kirki\Ajax\Page; |
| 15 | use Kirki\Frontend\Preview\Preview; |
| 16 | use Kirki\HelperFunctions; |
| 17 | |
| 18 | /** |
| 19 | * TheFrontendHooks class |
| 20 | */ |
| 21 | class TheFrontendHooks { |
| 22 | |
| 23 | /** |
| 24 | * Flag for where from call this instance |
| 25 | */ |
| 26 | private $call_from = 'front-end'; |
| 27 | |
| 28 | public function __construct( $call_from ) { |
| 29 | $this->call_from = $call_from; |
| 30 | add_action( 'wp_enqueue_scripts', array( $this, 'load_assets' ) ); |
| 31 | add_action( 'wp_body_open', array( $this, 'load_custom_header' ), 1, 1 ); // call if template has get_header method. |
| 32 | add_action( 'wp_footer', array( $this, 'load_custom_footer' ), 1, 1 ); // call if template has get_header method. |
| 33 | add_action( 'wp_footer', array( $this, 'add_before_body_tag_end' ) ); |
| 34 | add_action( 'template_redirect', array( $this, 'may_be_change_header_footer' ) ); |
| 35 | } |
| 36 | |
| 37 | private function should_load_assets() { |
| 38 | $should_load = false; |
| 39 | |
| 40 | /** |
| 41 | * Allow TemplateRedirection (or others) to force-load assets when the current |
| 42 | * request is being served by a kirki template. |
| 43 | */ |
| 44 | $should_load = $should_load || (bool) apply_filters( 'kirki_assets_should_load', false ); |
| 45 | |
| 46 | // TheFrontend sets this on the 'wp' action after running is_kirki_type_data(). |
| 47 | if ( isset( $GLOBALS['kirki_assets_should_load_for_request'] ) ) { |
| 48 | $should_load = $should_load || (bool) $GLOBALS['kirki_assets_should_load_for_request']; |
| 49 | } |
| 50 | |
| 51 | // If custom header/footer is present, frontend needs kirki assets. |
| 52 | global $kirki_custom_header, $kirki_custom_footer; |
| 53 | $kirki_custom_header = HelperFunctions::get_page_custom_section( 'header' ); |
| 54 | $kirki_custom_footer = HelperFunctions::get_page_custom_section( 'footer' ); |
| 55 | |
| 56 | $has_custom_header = is_string( $kirki_custom_header ) ? ( '' !== trim( $kirki_custom_header ) ) : ! empty( $kirki_custom_header ); |
| 57 | $has_custom_footer = is_string( $kirki_custom_footer ) ? ( '' !== trim( $kirki_custom_footer ) ) : ! empty( $kirki_custom_footer ); |
| 58 | if ( $has_custom_header || $has_custom_footer ) { |
| 59 | $should_load = true; |
| 60 | } |
| 61 | |
| 62 | // If current page is a kirki template, load assets. |
| 63 | $template_data = HelperFunctions::get_template_data_if_current_page_is_kirki_template(); |
| 64 | if ( $template_data ) { |
| 65 | $should_load = true; |
| 66 | } |
| 67 | |
| 68 | return $should_load; |
| 69 | } |
| 70 | |
| 71 | public function may_be_change_header_footer() { |
| 72 | ob_start( array( $this, 'check_and_change_header_and_footer' ) ); |
| 73 | } |
| 74 | public function check_and_change_header_and_footer( $html ) { |
| 75 | global $kirki_custom_header, $kirki_custom_footer; |
| 76 | if ( $kirki_custom_header ) { |
| 77 | $html = preg_replace( |
| 78 | '#<header(?![^>]*\sdata-kirki=["\']).*?>.*?</header>#is', |
| 79 | ' |
| 80 | <!-- kirki-custom-header will |
| 81 | be loaded -->', |
| 82 | $html |
| 83 | ); |
| 84 | } |
| 85 | if ( $kirki_custom_footer ) { |
| 86 | $html = preg_replace( |
| 87 | '#<footer(?![^>]*\sdata-kirki=["\']).*?>.*?</footer>#is', |
| 88 | ' |
| 89 | <!-- kirki-custom-footer will be loaded |
| 90 | -->', |
| 91 | $html |
| 92 | ); |
| 93 | } |
| 94 | return $html; |
| 95 | } |
| 96 | |
| 97 | public function load_assets() { |
| 98 | if ( ! $this->should_load_assets() ) { |
| 99 | return; |
| 100 | } |
| 101 | |
| 102 | wp_enqueue_script( 'kirki', KIRKI_ASSETS_URL . 'js/kirki.min.js', array( 'wp-i18n' ), KIRKI_VERSION, true ); |
| 103 | wp_enqueue_style( 'kirki', KIRKI_ASSETS_URL . 'css/kirki.min.css', null, KIRKI_VERSION ); |
| 104 | |
| 105 | $this->load_variables(); |
| 106 | } |
| 107 | |
| 108 | private function load_variables(){ |
| 109 | $variable_post_id = HelperFunctions::get_post_id_if_possible_from_url(); |
| 110 | $variable_mode = Page::get_variable_mode($variable_post_id); |
| 111 | $variable_css = Preview::getVariableCssCode('global', ':root', $variable_mode, false); |
| 112 | wp_add_inline_style( 'kirki', $variable_css ); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Load custom header |
| 117 | * |
| 118 | * @param string $header header text. |
| 119 | * @return void |
| 120 | */ |
| 121 | public function load_custom_header( $header ) { |
| 122 | // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 123 | echo HelperFunctions::get_page_custom_section( 'header' ); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Load custom footer |
| 128 | * |
| 129 | * @param string $footer footer text. |
| 130 | * @return void |
| 131 | */ |
| 132 | public function load_custom_footer( $footer ) { |
| 133 | // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 134 | echo HelperFunctions::get_page_custom_section( 'footer' ); |
| 135 | } |
| 136 | |
| 137 | public function add_before_body_tag_end() { |
| 138 | // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 139 | $s = ''; |
| 140 | |
| 141 | $template_data = HelperFunctions::get_template_data_if_current_page_is_kirki_template(); |
| 142 | $context = HelperFunctions::get_current_page_context(); |
| 143 | $post_id = HelperFunctions::get_post_id_if_possible_from_url(); |
| 144 | $url_arr = HelperFunctions::get_post_url_arr_from_post_id( |
| 145 | $post_id, |
| 146 | array( |
| 147 | 'ajax_url' => true, |
| 148 | 'rest_url' => true, |
| 149 | 'site_url' => true, |
| 150 | 'nonce' => true, |
| 151 | ) |
| 152 | ); |
| 153 | $s .= HelperFunctions::get_view_port_lists(); |
| 154 | $s .= HelperFunctions::get_kirki_css_variables_data(); |
| 155 | $s .= '<script id="' .'kirki-api-and-nonce"> |
| 156 | window.wp_kirki = { |
| 157 | ajaxUrl: "' . esc_url( $url_arr['ajax_url'] ) . '", |
| 158 | restUrl: "' . esc_url( $url_arr['rest_url'] ) . '", |
| 159 | siteUrl: "' . esc_url( $url_arr['site_url'] ) . '", |
| 160 | apiVersion: "v1", |
| 161 | postId: "' . esc_attr( $post_id ) . '", |
| 162 | nonce: "' . esc_attr( $url_arr['nonce'] ) . '", |
| 163 | call_from: "' . esc_attr( $this->call_from ) . '", |
| 164 | templateId: "' . esc_attr( $template_data ? $template_data['template_id'] : false ) . '", |
| 165 | context: ' . json_encode( $context ) . ' |
| 166 | }; |
| 167 | </script>'; |
| 168 | |
| 169 | // smooth scroll |
| 170 | if ( 'iframe' !== $this->call_from ) { |
| 171 | $s .= HelperFunctions::get_smooth_scroll_script(); |
| 172 | } |
| 173 | |
| 174 | // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 175 | echo $s; |
| 176 | } |
| 177 | |
| 178 | } |
| 179 |