-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
preview.php
267 lines (230 loc) · 5.61 KB
/
preview.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
<?php
namespace Elementor;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
/**
* Elementor preview.
*
* Elementor preview handler class is responsible for initializing Elementor in
* preview mode.
*
* @since 1.0.0
*/
class Preview {
/**
* Post ID.
*
* Holds the ID of the current post being previewed.
*
* @since 1.0.0
* @access private
*
* @var int Post ID.
*/
private $post_id;
/**
* Init.
*
* Initialize Elementor preview mode.
*
* Fired by `template_redirect` action.
*
* @since 1.0.0
* @access public
*/
public function init() {
if ( is_admin() || ! $this->is_preview_mode() ) {
return;
}
$this->post_id = get_the_ID();
// Compatibility with Yoast SEO plugin when 'Removes unneeded query variables from the URL' enabled.
// TODO: Move this code to `includes/compatibility.php`.
if ( class_exists( 'WPSEO_Frontend' ) ) {
remove_action( 'template_redirect', [ \WPSEO_Frontend::get_instance(), 'clean_permalink' ], 1 );
}
// Disable the WP admin bar in preview mode.
add_filter( 'show_admin_bar', '__return_false' );
add_action( 'wp_enqueue_scripts', function() {
$this->enqueue_styles();
$this->enqueue_scripts();
} );
add_filter( 'the_content', [ $this, 'builder_wrapper' ], 999999 );
add_action( 'wp_footer', [ $this, 'wp_footer' ] );
// Tell to WP Cache plugins do not cache this request.
Utils::do_not_cache();
/**
* Preview init.
*
* Fires on Elementor preview init, after Elementor preview has finished
* loading but before any headers are sent.
*
* @since 1.0.0
*
* @param Preview $this The current preview.
*/
do_action( 'elementor/preview/init', $this );
}
/**
* Retrieve post ID.
*
* Get the ID of the current post.
*
* @since 1.8.0
* @access public
*
* @return int Post ID.
*/
public function get_post_id() {
return $this->post_id;
}
/**
* Whether preview mode is active.
*
* Used to determine whether we are in the preview mode (iframe).
*
* @since 1.0.0
* @access public
*
* @param int $post_id Optional. Post ID. Default is `0`.
*
* @return bool Whether preview mode is active.
*/
public function is_preview_mode( $post_id = 0 ) {
if ( empty( $post_id ) ) {
$post_id = get_the_ID();
}
if ( ! User::is_current_user_can_edit( $post_id ) ) {
return false;
}
if ( ! isset( $_GET['elementor-preview'] ) || $post_id !== (int) $_GET['elementor-preview'] ) {
return false;
}
return true;
}
/**
* Builder wrapper.
*
* Used to add an empty HTML wrapper for the builder, the javascript will add
* the content later.
*
* @since 1.0.0
* @access public
*
* @param string $content The content of the builder.
*
* @return string HTML wrapper for the builder.
*/
public function builder_wrapper( $content ) {
if ( get_the_ID() === $this->post_id ) {
$classes = 'elementor-edit-mode';
$document = Plugin::$instance->documents->get( $this->post_id );
if ( $document ) {
$classes .= ' ' . $document->get_container_classes();
}
$content = '<div id="elementor" class="' . $classes . '"></div>';
}
return $content;
}
/**
* Enqueue preview styles.
*
* Registers all the preview styles and enqueues them.
*
* Fired by `wp_enqueue_scripts` action.
*
* @since 1.0.0
* @access private
*/
private function enqueue_styles() {
// Hold-on all jQuery plugins after all HTML markup render.
wp_add_inline_script( 'jquery-migrate', 'jQuery.holdReady( true );' );
Plugin::$instance->frontend->enqueue_styles();
$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
$direction_suffix = is_rtl() ? '-rtl' : '';
wp_register_style(
'elementor-select2',
ELEMENTOR_ASSETS_URL . 'lib/e-select2/css/e-select2' . $suffix . '.css',
[],
'4.0.5'
);
wp_register_style(
'editor-preview',
ELEMENTOR_ASSETS_URL . 'css/editor-preview' . $direction_suffix . $suffix . '.css',
[
'elementor-select2',
],
ELEMENTOR_VERSION
);
wp_enqueue_style( 'editor-preview' );
/**
* Preview enqueue styles.
*
* Fires after Elementor preview styles are enqueued.
*
* @since 1.0.0
*/
do_action( 'elementor/preview/enqueue_styles' );
}
/**
* Enqueue preview scripts.
*
* Registers all the preview scripts and enqueues them.
*
* Fired by `wp_enqueue_scripts` action.
*
* @since 1.5.4
* @access private
*/
private function enqueue_scripts() {
Plugin::$instance->frontend->register_scripts();
Plugin::$instance->widgets_manager->enqueue_widgets_scripts();
$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
wp_enqueue_script(
'elementor-inline-editor',
ELEMENTOR_ASSETS_URL . 'lib/inline-editor/js/inline-editor' . $suffix . '.js',
[],
'',
true
);
/**
* Preview enqueue scripts.
*
* Fires after Elementor preview scripts are enqueued.
*
* @since 1.5.4
*/
do_action( 'elementor/preview/enqueue_scripts' );
}
/**
* Elementor Preview footer scripts and styles.
*
* Handle styles and scripts from frontend.
*
* Fired by `wp_footer` action.
*
* @since 2.0.9
* @access public
*/
public function wp_footer() {
$frontend = Plugin::$instance->frontend;
if ( $frontend->has_elementor_in_page() ) {
// Has header/footer/widget-template - enqueue all style/scripts/fonts.
$frontend->wp_footer();
} else {
// Enqueue only scripts.
$frontend->enqueue_scripts();
}
}
/**
* Preview constructor.
*
* Initializing Elementor preview.
*
* @since 1.0.0
* @access public
*/
public function __construct() {
add_action( 'template_redirect', [ $this, 'init' ], 0 );
}
}