PluginProbe
Elementor Website Builder – more than just a page builder / 3.21.5
Elementor Website Builder – more than just a page builder v3.21.5
4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 4.0.7 All 451 releases
elementor / includes / preview.php

preview.php in Elementor Website Builder – more than just a page builder 3.21.5, at includes/preview.php

352 lines 7.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Elementor;
3
4 use Elementor\Core\Base\App;
5 use Elementor\Core\Settings\Manager as SettingsManager;
6
7 if ( ! defined( 'ABSPATH' ) ) {
8 exit; // Exit if accessed directly.
9 }
10
11 /**
12 * Elementor preview.
13 *
14 * Elementor preview handler class is responsible for initializing Elementor in
15 * preview mode.
16 *
17 * @since 1.0.0
18 */
19 class Preview extends App {
20
21 /**
22 * Is Preview.
23 *
24 * Holds a flag if current request is a preview.
25 * The flag is not related to a specific post or edit permissions.
26 *
27 * @since 2.9.5
28 * @access private
29 *
30 * @var bool Is Preview.
31 */
32
33 private $is_preview;
34
35 /**
36 * Post ID.
37 *
38 * Holds the ID of the current post being previewed.
39 *
40 * @since 1.0.0
41 * @access private
42 *
43 * @var int Post ID.
44 */
45 private $post_id;
46
47 /**
48 * Get module name.
49 *
50 * Retrieve the module name.
51 *
52 * @since 3.0.0
53 * @access public
54 * @abstract
55 *
56 * @return string Module name.
57 */
58 public function get_name() {
59 return 'preview';
60 }
61
62 /**
63 * Init.
64 *
65 * Initialize Elementor preview mode.
66 *
67 * Fired by `template_redirect` action.
68 *
69 * @since 1.0.0
70 * @access public
71 */
72 public function init() {
73 if ( is_admin() || ! $this->is_preview_mode() ) {
74 return;
75 }
76
77 if ( isset( $_GET['preview-debug'] ) ) {
78 register_shutdown_function( function () {
79 $e = error_get_last();
80 if ( $e ) {
81 echo '<div id="elementor-preview-debug-error"><pre>';
82 Utils::print_unescaped_internal_string( $e['message'] );
83 echo '</pre></div>';
84 }
85 } );
86 }
87
88 $this->post_id = get_the_ID();
89 $this->is_preview = true;
90
91 // Don't redirect to permalink.
92 remove_action( 'template_redirect', 'redirect_canonical' );
93
94 // Compatibility with Yoast SEO plugin when 'Removes unneeded query variables from the URL' enabled.
95 // TODO: Move this code to `includes/compatibility.php`.
96 if ( class_exists( 'WPSEO_Frontend' ) ) {
97 remove_action( 'template_redirect', [ \WPSEO_Frontend::get_instance(), 'clean_permalink' ], 1 );
98 }
99
100 // Disable the WP admin bar in preview mode.
101 add_filter( 'show_admin_bar', '__return_false' );
102
103 add_action( 'wp_enqueue_scripts', function() {
104 $this->enqueue_styles();
105 $this->enqueue_scripts();
106 } );
107
108 add_filter( 'the_content', [ $this, 'builder_wrapper' ], 999999 );
109
110 add_action( 'wp_footer', [ $this, 'wp_footer' ] );
111
112 // Avoid Cloudflare's Rocket Loader lazy load the editor iframe
113 add_filter( 'script_loader_tag', [ $this, 'rocket_loader_filter' ], 10, 3 );
114
115 // Tell to WP Cache plugins do not cache this request.
116 Utils::do_not_cache();
117
118 /**
119 * Preview init.
120 *
121 * Fires on Elementor preview init, after Elementor preview has finished
122 * loading but before any headers are sent.
123 *
124 * @since 1.0.0
125 *
126 * @param Preview $this The current preview.
127 */
128 do_action( 'elementor/preview/init', $this );
129 }
130
131 /**
132 * Retrieve post ID.
133 *
134 * Get the ID of the current post.
135 *
136 * @since 1.8.0
137 * @access public
138 *
139 * @return int Post ID.
140 */
141 public function get_post_id() {
142 return $this->post_id;
143 }
144
145 /**
146 * Is Preview.
147 *
148 * Whether current request is the elementor preview iframe.
149 * The flag is not related to a specific post or edit permissions.
150 *
151 * @since 2.9.5
152 * @access public
153 *
154 * @return bool
155 */
156 public function is_preview() {
157 return $this->is_preview;
158 }
159
160 /**
161 * Whether preview mode is active.
162 *
163 * Used to determine whether we are in the preview mode (iframe).
164 *
165 * @since 1.0.0
166 * @access public
167 *
168 * @param int $post_id Optional. Post ID. Default is `0`.
169 *
170 * @return bool Whether preview mode is active.
171 */
172 public function is_preview_mode( $post_id = 0 ) {
173 if ( ! isset( $_GET['elementor-preview'] ) ) {
174 return false;
175 }
176
177 if ( empty( $post_id ) ) {
178 $post_id = get_the_ID();
179 }
180
181 if ( ! User::is_current_user_can_edit( $post_id ) ) {
182 return false;
183 }
184
185 if ( $post_id !== (int) $_GET['elementor-preview'] ) {
186 return false;
187 }
188
189 return true;
190 }
191
192 /**
193 * Builder wrapper.
194 *
195 * Used to add an empty HTML wrapper for the builder, the javascript will add
196 * the content later.
197 *
198 * @since 1.0.0
199 * @access public
200 *
201 * @param string $content The content of the builder.
202 *
203 * @return string HTML wrapper for the builder.
204 */
205 public function builder_wrapper( $content ) {
206 if ( get_the_ID() === $this->post_id ) {
207 $document = Plugin::$instance->documents->get( $this->post_id );
208
209 $attributes = $document->get_container_attributes();
210
211 $content = '<div ' . Utils::render_html_attributes( $attributes ) . '></div>';
212 }
213
214 return $content;
215 }
216
217 /**
218 * Enqueue preview styles.
219 *
220 * Registers all the preview styles and enqueues them.
221 *
222 * Fired by `wp_enqueue_scripts` action.
223 *
224 * @since 1.0.0
225 * @access private
226 */
227 private function enqueue_styles() {
228 // Hold-on all jQuery plugins after all HTML markup render.
229 wp_add_inline_script( 'jquery-migrate', 'jQuery.holdReady( true );' );
230
231 Plugin::$instance->frontend->enqueue_styles();
232
233 Plugin::$instance->widgets_manager->enqueue_widgets_styles();
234
235 $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
236
237 $direction_suffix = is_rtl() ? '-rtl' : '';
238
239 wp_register_style(
240 'elementor-select2',
241 ELEMENTOR_ASSETS_URL . 'lib/e-select2/css/e-select2' . $suffix . '.css',
242 [],
243 '4.0.6-rc.1'
244 );
245
246 wp_register_style(
247 'editor-preview',
248 ELEMENTOR_ASSETS_URL . 'css/editor-preview' . $direction_suffix . $suffix . '.css',
249 [
250 'elementor-select2',
251 ],
252 ELEMENTOR_VERSION
253 );
254
255 wp_enqueue_style(
256 'e-theme-ui-light',
257 $this->get_css_assets_url( 'theme-light' ),
258 [],
259 ELEMENTOR_VERSION
260 );
261
262 wp_enqueue_style( 'editor-preview' );
263
264 // Handle the 'wp audio' in editor preview.
265 wp_enqueue_style( 'wp-mediaelement' );
266
267 /**
268 * Preview enqueue styles.
269 *
270 * Fires after Elementor preview styles are enqueued.
271 *
272 * @since 1.0.0
273 */
274 do_action( 'elementor/preview/enqueue_styles' );
275 }
276
277 /**
278 * Enqueue preview scripts.
279 *
280 * Registers all the preview scripts and enqueues them.
281 *
282 * Fired by `wp_enqueue_scripts` action.
283 *
284 * @since 1.5.4
285 * @access private
286 */
287 private function enqueue_scripts() {
288 Plugin::$instance->frontend->register_scripts();
289
290 Plugin::$instance->widgets_manager->enqueue_widgets_scripts();
291
292 $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
293
294 wp_enqueue_script(
295 'elementor-inline-editor',
296 ELEMENTOR_ASSETS_URL . 'lib/inline-editor/js/inline-editor' . $suffix . '.js',
297 [],
298 ELEMENTOR_VERSION,
299 true
300 );
301
302 // Handle the 'wp audio' in editor preview.
303 wp_enqueue_script( 'wp-mediaelement' );
304
305 /**
306 * Preview enqueue scripts.
307 *
308 * Fires after Elementor preview scripts are enqueued.
309 *
310 * @since 1.5.4
311 */
312 do_action( 'elementor/preview/enqueue_scripts' );
313 }
314
315 public function rocket_loader_filter( $tag, $handle, $src ) {
316 return str_replace( '<script', '<script data-cfasync="false"', $tag );
317 }
318
319 /**
320 * Elementor Preview footer scripts and styles.
321 *
322 * Handle styles and scripts from frontend.
323 *
324 * Fired by `wp_footer` action.
325 *
326 * @since 2.0.9
327 * @access public
328 */
329 public function wp_footer() {
330 $frontend = Plugin::$instance->frontend;
331 if ( $frontend->has_elementor_in_page() ) {
332 // Has header/footer/widget-template - enqueue all style/scripts/fonts.
333 $frontend->wp_footer();
334 } else {
335 // Enqueue only scripts.
336 $frontend->enqueue_scripts();
337 }
338 }
339
340 /**
341 * Preview constructor.
342 *
343 * Initializing Elementor preview.
344 *
345 * @since 1.0.0
346 * @access public
347 */
348 public function __construct() {
349 add_action( 'template_redirect', [ $this, 'init' ], 0 );
350 }
351 }
352