-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclass-elementor.php
389 lines (317 loc) · 12.3 KB
/
class-elementor.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
<?php
namespace SLCA\Elementor;
use wpCloud\StatelessMedia\Compatibility;
use wpCloud\StatelessMedia\Utility;
use wpCloud\StatelessMedia\Helper;
/**
* Class Elementor
*/
class Elementor extends Compatibility {
const STORAGE_PATH = 'elementor/';
protected $id = 'elementor';
protected $title = 'Elementor Website Builder';
protected $constant = 'WP_STATELESS_COMPATIBILITY_ELEMENTOR';
protected $description = 'Ensures compatibility with Elementor Website Builder. Sync css files generated by Elementor.';
protected $plugin_file = 'elementor/elementor.php';
protected $non_library_sync = true;
protected $filesystem = null;
/**
* @param $sm
*/
public function module_init($sm) {
if ( !class_exists('\WP_Filesystem_Direct') ) {
require_once(ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php');
require_once(ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php');
}
$this->filesystem = new \WP_Filesystem_Direct( false );
add_action('elementor/core/files/clear_cache', array($this, 'delete_elementor_files'));
add_action('save_post', array($this, 'delete_css_files'), 10, 3);
add_action('deleted_post', array($this, 'delete_css_files'));
add_action('sm::pre::sync::nonMediaFiles', array($this, 'filter_css_file'), 10, 2);
add_action('wp_ajax_elementor_pro_forms_send_form', array($this, 'remove_cache_busting'));
add_action('wp_ajax_nopriv_elementor_pro_forms_send_form', array($this, 'remove_cache_busting'));
add_action('elementor_pro/forms/process', array($this, 'process_files'), 20, 2);
add_filter('set_url_scheme', array($this, 'sync_rewrite_url'), 10, 3);
add_filter("elementor/settings/general/success_response_data", array($this, 'delete_global_css'), 10, 3);
add_filter('sm:sync::syncArgs', array($this, 'sync_args'), 10, 4);
add_filter('sm:sync::nonMediaFiles', array($this, 'get_sync_files'), 20);
add_filter('elementor_pro/forms/upload_path', array($this, 'get_upload_path'));
add_filter('elementor_pro/forms/upload_url', array($this, 'get_upload_file_url'));
add_filter('elementor_pro/icons_manager/custom_icons/dir', array($this, 'get_upload_path'));
add_filter('elementor_pro/icons_manager/custom_icons/url', array($this, 'get_upload_file_url'));
}
/**
* Get the position of 'elementor/' dir in the filename.
*
* @param $name
* @return bool
*/
protected function get_elementor_position($name) {
return strpos($name, self::STORAGE_PATH);
}
/**
* Skip cache busting for WPForms files.
*
* @param $skip
* @param $file
* @return bool
*/
public function remove_cache_busting() {
remove_filter('sanitize_file_name', ['wpCloud\StatelessMedia\Utility', 'randomize_filename'], 10);
}
/**
* Move file from on GCS from default position to the bucket root.
* @param $source
* @param $destination
*/
protected function possibly_move_file($source, $destination) {
$source = str_replace( ud_get_stateless_media()->get_gs_host(), ud_get_stateless_media()->get_gs_path(), $source );
if ( $source === $destination || !ud_get_stateless_media()->is_mode('stateless') ) {
return;
}
if ( $this->filesystem->exists($source) ) {
$this->filesystem->move($source, $destination, true);
}
}
/**
* Sync local elementor files to GCS.
* @param $url
* @param $_ Not used
* @param $__ Not used
* @return string
*/
public function sync_rewrite_url($url, $_, $__) {
try {
if ( $this->get_elementor_position($url) !== false) {
$wp_uploads_dir = wp_get_upload_dir();
$name = str_replace($wp_uploads_dir['baseurl'] . '/', '', $url);
if ($name != $url) {
$absolutePath = apply_filters('wp_stateless_addon_files_root', '');
$absolutePath .= '/' . $name;
$this->possibly_move_file($url, $absolutePath);
$name = apply_filters('wp_stateless_file_name', $name, 0);
do_action('sm:sync::syncFile', $name, $absolutePath);
if ( !ud_get_stateless_media()->is_mode( ['disabled', 'backup'] ) ) {
$url = ud_get_stateless_media()->get_gs_host() . '/' . $name;
}
}
}
} catch (\Exception $e) {
error_log($e->getMessage());
}
return $url;
}
/**
* To regenerate/delete files click Regenerate Files in
* Elementor >> Tools >> General >> Regenerate CSS
* All files will be deleted from GCS.
* And will be copied to GCS again on next page view.
*/
public function delete_elementor_files() {
do_action('sm:sync::deleteFiles', 'elementor/');
}
/**
* Delete GCS file on update/delete post.
* @param $post_ID
* @param null $post
* @param null $update
*/
public function delete_css_files($post_ID, $post = null, $update = null) {
if ($update || current_action() === 'deleted_post') {
$post_css = new \Elementor\Core\Files\CSS\Post($post_ID);
// elementor/ css/ 'post-' . $post_id . '.css'
$name = $post_css::UPLOADS_DIR . $post_css::DEFAULT_FILES_DIR . $post_css->get_file_name();
$name = apply_filters('wp_stateless_file_name', $name, 0);
do_action('sm:sync::deleteFile', $name);
}
}
/**
* Delete elementor global css file when global style is updated on Elementor Editor.
* @param $success_response_data
* @param $id
* @param $data
* @return mixed
*/
public function delete_global_css($success_response_data, $id, $data) {
try {
$post_css = new \Elementor\Core\Files\CSS\Global_CSS('global.css');
// elementor/ css/ 'global.css'
$name = $post_css::UPLOADS_DIR . $post_css::DEFAULT_FILES_DIR . $post_css->get_file_name();
$name = apply_filters('wp_stateless_file_name', $name, 0);
do_action('sm:sync::deleteFile', $name);
} catch (\Exception $e) {
}
// We are in filter so need to return the passed value.
return $success_response_data;
}
/**
* @param $name
* @param $absolutePath
*/
public function filter_css_file($name, $absolutePath) {
$upload_data = wp_upload_dir();
if (!empty($upload_data) && file_exists($absolutePath)) {
try {
if ( !function_exists('WP_Filesystem') ) {
require_once(ABSPATH . 'wp-admin/includes/file.php');
}
WP_Filesystem();
global $wp_filesystem;
$content = $wp_filesystem->get_contents($absolutePath);
if (!empty($upload_data['baseurl']) && !empty($content)) {
$baseurl = preg_replace('/https?:\/\//', '', $upload_data['baseurl']);
$root_dir = trim(ud_get_stateless_media()->get('sm.root_dir'), '/ '); // Remove any forward slash and empty space.
$root_dir = apply_filters("wp_stateless_handle_root_dir", $root_dir);
$root_dir = !empty($root_dir) ? ltrim($root_dir, '/') : '';
$image_host = trailingslashit( ud_get_stateless_media()->get_gs_host() ) . $root_dir;
$file_ext = ud_get_stateless_media()->replaceable_file_types();
preg_match_all('/(https?:\/\/' . str_replace('/', '\/', $baseurl) . ')\/(.+?)(' . $file_ext . ')/i', $content, $matches);
if (!empty($matches)) {
foreach ($matches[0] as $key => $match) {
$id = attachment_url_to_postid($match);
if (!empty($id)) {
Utility::add_media(null, $id, true);
}
}
}
$content = preg_replace('/(https?:\/\/' . str_replace('/', '\/', $baseurl) . ')\/(.+?)(' . $file_ext . ')/i', $image_host . '/$2$3', $content);
$wp_filesystem->put_contents($absolutePath, $content);
preg_match('/post-(\d+).css/', $name, $match);
if (!empty($match[1])) {
$_elementor_css = get_post_meta($match[1], '_elementor_css', true);
if (!empty($_elementor_css)) {
$_elementor_css['time'] = time();
}
}
}
} catch (\Exception $e) {
}
}
}
/**
* Update args when uploading/syncing file to GCS.
*
* @param array $args
* @param string $name
* @param string $file
* @param bool $force
*
* @return array
*/
public function sync_args($args, $name, $file, $force) {
if ( $this->get_elementor_position($name) !== 0 ) {
return $args;
}
if ( ud_get_stateless_media()->is_mode('stateless') ) {
$args['name_with_root'] = false;
}
$args['source'] = 'Elementor';
$args['source_version'] = defined( 'ELEMENTOR_VERSION' ) ? ELEMENTOR_VERSION : '';
return $args;
}
/**
* Get the list of files to sync.
*
* @param array $file_list
* @return array
*/
public function get_sync_files($file_list) {
if ( !method_exists('\wpCloud\StatelessMedia\Utility', 'get_files') ) {
Helper::log('The WP-Stateless version is outdated. Please update.');
return $file_list;
}
$dir = apply_filters('wp_stateless_addon_sync_files_path', '', self::STORAGE_PATH);
if (is_dir($dir)) {
// Getting all the files from dir recursively.
$files = Utility::get_files($dir);
// validating and adding to the $files array.
foreach ($files as $file) {
if (!file_exists($file)) {
continue;
}
// filter index.html, .htaccess files
$basename = basename($file);
if ( in_array($basename, array('index.html', '.htaccess')) ) {
continue;
}
$file = self::STORAGE_PATH . str_replace( $dir, '', wp_normalize_path($file) );
$file = trim($file, '/');
if ( !in_array($file, $file_list) ) {
$file_list[] = $file;
}
}
}
return $file_list;
}
/**
* Process files uploaded by Elementor forms.
*
* @param ElementorPro\Modules\Forms\Classes\Form_Record $record
* @param ElementorPro\Modules\Forms\Classes\Ajax_Handler $ajax_handler
*/
public function process_files($record, $ajax_handler) {
$files = $record->get( 'files' );
if ( empty( $files ) ) {
return;
}
$wp_uploads_dir = wp_get_upload_dir();
$fields = [];
foreach ( $files as $file_id => $file ) {
$urls = isset( $file['url'] ) && !empty( $file['url'] ) && is_array( $file['url'] ) ? $file['url'] : [];
$values = [];
$raw_values = [];
foreach ( $urls as $id => $url ) {
try {
$name = ud_get_stateless_media()->is_mode('stateless')
? str_replace(ud_get_stateless_media()->get_gs_host(), '', $url)
: str_replace($wp_uploads_dir['baseurl'] . '/', '', $url);
$absolutePath = apply_filters('wp_stateless_addon_files_root', '');
$absolutePath .= '/' . $name;
$name = apply_filters('wp_stateless_file_name', $name, 0);
do_action('sm:sync::syncFile', $name, $absolutePath);
$values[] = ud_get_stateless_media()->get_gs_host() . '/' . $name;
$raw_values[] = ud_get_stateless_media()->get_gs_path() . '/' . $name;
} catch (\Exception $e) {
Helper::log($e->getMessage());
}
}
$fields[ $file_id ] = [
'value' => implode(' , ', $values),
'raw_value' => implode(' , ', $raw_values),
];
}
// Update fields before saving form submission
if ( !ud_get_stateless_media()->is_mode( ['disabled', 'backup'] ) ) {
$form_fields = $record->get( 'fields' );
foreach ( $form_fields as $field_id => $field ) {
if ( isset( $fields[ $field_id ] ) ) {
$form_fields[ $field_id ]['value'] = $fields[ $field_id ]['value'];
$form_fields[ $field_id ]['raw_value'] = $fields[ $field_id ]['raw_value'];
}
}
$record->set( 'fields', $form_fields );
}
}
/**
* Override the upload path for Elementor forms in Stateless Mode.
*/
public function get_upload_path($path) {
if ( !ud_get_stateless_media()->is_mode('stateless') ) {
return $path;
}
$wp_uploads_dir = wp_get_upload_dir();
$path = str_replace( $wp_uploads_dir['basedir'], ud_get_stateless_media()->get_gs_path(), $path );
return $path;
}
/**
* Override the upload url for Elementor forms in Stateless Mode.
*/
public function get_upload_file_url($url) {
if ( !ud_get_stateless_media()->is_mode('stateless') ) {
return $url;
}
$wp_uploads_dir = wp_get_upload_dir();
$url = str_replace( $wp_uploads_dir['baseurl'], ud_get_stateless_media()->get_gs_host(), $url );
return $url;
}
}