Preview
3 days ago
views
3 days ago
Editor.php
3 weeks ago
Iframe.php
2 months ago
TheFrontend.php
2 months ago
TheFrontendHooks.php
2 months ago
TheFrontend.php
166 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Front end post hooks and content controller |
| 5 | * |
| 6 | * @package kirki |
| 7 | */ |
| 8 | |
| 9 | namespace Kirki\Frontend; |
| 10 | |
| 11 | if ( ! defined( 'ABSPATH' ) ) { |
| 12 | exit; // Exit if accessed directly. |
| 13 | } |
| 14 | |
| 15 | use Kirki\Frontend\Preview\Preview; |
| 16 | use Kirki\HelperFunctions; |
| 17 | use Kirki\Admin\EditWithButton; |
| 18 | |
| 19 | /** |
| 20 | * TheFrontend class |
| 21 | */ |
| 22 | class TheFrontend { |
| 23 | |
| 24 | /** |
| 25 | * Flag for where from call this instance |
| 26 | */ |
| 27 | private $call_from = 'front-end'; |
| 28 | private $staging_version = false; |
| 29 | private $assets_should_load = false; |
| 30 | |
| 31 | /** |
| 32 | * Collect kirki type data header, footer, content, popups |
| 33 | */ |
| 34 | private $kirki_type_html_data = array( |
| 35 | 'content' => '', |
| 36 | 'popups' => '', |
| 37 | ); |
| 38 | /** |
| 39 | * Initialize the class |
| 40 | * |
| 41 | * @param string $call_from where from call this instance. |
| 42 | * @return void |
| 43 | */ |
| 44 | public function __construct( $call_from = 'front-end', $staging_version = false ) { |
| 45 | if ( $call_from === 'iframe' ) { |
| 46 | remove_all_filters( 'the_content' ); |
| 47 | } |
| 48 | $this->staging_version = $staging_version; |
| 49 | |
| 50 | if ($this->staging_version) { |
| 51 | add_filter('show_admin_bar', '__return_false'); |
| 52 | } |
| 53 | |
| 54 | add_action( 'wp', array( $this, 'collect_all_kirki_type_data' ) ); |
| 55 | add_action( 'wp_head', array( $this, 'add_inside_head_tag' ) ); |
| 56 | add_filter( 'the_content', array( $this, 'replace_content' ), PHP_INT_MAX ); |
| 57 | add_action( 'wp_footer', array( $this, 'add_before_body_tag_end' ) ); |
| 58 | |
| 59 | //phpcs:ignore WordPress.Security.NonceVerification.Missing,WordPress.Security.NonceVerification.Recommended,WordPress.Security.ValidatedSanitizedInput.MissingUnslash,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 60 | $action = HelperFunctions::sanitize_text( isset( $_GET['action'] ) ? $_GET['action'] : null ); |
| 61 | if ( 'front-end' === $call_from && 'kirki' === $action ) { |
| 62 | $call_from = 'editor'; |
| 63 | } |
| 64 | if ( 'front-end' === $call_from ) { |
| 65 | new EditWithButton(); |
| 66 | } |
| 67 | |
| 68 | new TheFrontendHooks( $call_from ); |
| 69 | |
| 70 | $this->call_from = $call_from; |
| 71 | } |
| 72 | /** |
| 73 | * Collect kirki related custom sections html string |
| 74 | * |
| 75 | * @return void |
| 76 | */ |
| 77 | public function collect_all_kirki_type_data() { |
| 78 | $this->get_current_post_related_dependency(); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Get Post preview related data. |
| 83 | * this function is called for enqueueing the icon assets and get only used style blocks |
| 84 | * and also get the html string of the preview |
| 85 | * |
| 86 | * @return void |
| 87 | */ |
| 88 | public function get_current_post_related_dependency() { |
| 89 | $d = HelperFunctions::is_kirki_type_data( null, $this->staging_version ); |
| 90 | $this->assets_should_load = ( $d && ( ! empty( $d['blocks'] ) || ! empty( $d['styles'] ) ) ); |
| 91 | |
| 92 | $GLOBALS['kirki_assets_should_load_for_request'] = $this->assets_should_load; |
| 93 | |
| 94 | if ( $this->assets_should_load ) { |
| 95 | $params = array( |
| 96 | 'blocks' => $d['blocks'], |
| 97 | 'style_blocks' => $d['styles'], |
| 98 | 'root' => 'root', |
| 99 | ); |
| 100 | |
| 101 | $this->kirki_type_html_data['content'] = HelperFunctions::get_html_using_preview_script( $params ); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | |
| 106 | /** |
| 107 | * Add elements style to the header |
| 108 | * |
| 109 | * @return void |
| 110 | */ |
| 111 | public function add_inside_head_tag() { |
| 112 | $s = ''; |
| 113 | $s .= Preview::getSeoMetaTags(); |
| 114 | $s .= Preview::getHeadCustomCode(); |
| 115 | $s .= HelperFunctions::get_custom_fonts_tags(); |
| 116 | // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 117 | echo $s; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Replace the content with our kirki data |
| 122 | * |
| 123 | * @param string $content wp content. |
| 124 | * |
| 125 | * @return the_contnet |
| 126 | */ |
| 127 | public function replace_content( $content ) { |
| 128 | // $flag = false; |
| 129 | if ( 'iframe' === $this->call_from ) { |
| 130 | return '<div id="' . 'kirki-builder"></div>'; |
| 131 | } |
| 132 | |
| 133 | if ( $this->kirki_type_html_data['content'] ) { |
| 134 | $content = $this->kirki_type_html_data['content']; |
| 135 | } |
| 136 | // Decode HTML entities. Example: [gravityform id="1"] → [gravityform id="1"] |
| 137 | $content = html_entity_decode( $content, ENT_NOQUOTES | ENT_HTML5, 'UTF-8' ); |
| 138 | |
| 139 | // Run shortcode manually |
| 140 | $content = do_shortcode( $content ); |
| 141 | |
| 142 | if ( 'migration' === $this->call_from ) { |
| 143 | return '<div id="' . 'kirki-builder-migration">' . $content . '</div>'; |
| 144 | } |
| 145 | return $content; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Add script tag to the footer |
| 150 | * |
| 151 | * @return void |
| 152 | */ |
| 153 | public function add_before_body_tag_end() { // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 154 | $s = ''; |
| 155 | if ( 'iframe' !== $this->call_from ) { |
| 156 | $s = HelperFunctions::get_page_popups_html(); |
| 157 | } |
| 158 | |
| 159 | if ( $this->kirki_type_html_data['content'] ) { |
| 160 | $s .= Preview::getBodyCustomCode(); |
| 161 | } |
| 162 | // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 163 | echo $s; |
| 164 | } |
| 165 | } |
| 166 |