PluginProbe
WP-Stateless – Google Cloud Storage / 3.2.1
WP-Stateless – Google Cloud Storage v3.2.1
4.4.3 2.1.7 2.1.8 2.1.9 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.3.0 2.3.1 2.3.2 3.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.2.0 3.2.1 3.2.2 All 62 releases
wp-stateless / lib / classes / compatibility / elementor.php

elementor.php in WP-Stateless – Google Cloud Storage 3.2.1, at lib/classes/compatibility/elementor.php

159 lines 6.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Plugin Name: Elementor Page Builder
5 * Plugin URI: https://wordpress.org/plugins/elementor/
6 *
7 * Compatibility Description: Ensures compatibility with Elementor.
8 *
9 * @Todo add manual sync to sync all elementor css file to GCS.
10 *
11 */
12
13 namespace wpCloud\StatelessMedia {
14
15 if (!class_exists('wpCloud\StatelessMedia\Elementor')) {
16
17 class Elementor extends ICompatibility {
18 protected $id = 'elementor';
19 protected $title = 'Elementor Page Builder';
20 protected $constant = 'WP_STATELESS_COMPATIBILITY_ELEMENTOR';
21 protected $description = 'Ensures compatibility with Elementor Page Builder. Sync css files generated by Elementor.';
22 protected $plugin_file = 'elementor/elementor.php';
23 protected $non_library_sync = true;
24
25 /**
26 * @param $sm
27 */
28 public function module_init($sm) {
29 add_filter('set_url_scheme', array($this, 'sync_rewrite_url'), 10, 3);
30 add_action('elementor/core/files/clear_cache', array($this, 'delete_elementor_files'));
31 add_action('save_post', array($this, 'delete_css_files'), 10, 3);
32 add_action('deleted_post', array($this, 'delete_css_files'));
33 add_filter("elementor/settings/general/success_response_data", array($this, 'delete_global_css'), 10, 3);
34 add_action('sm::pre::sync::nonMediaFiles', array($this, 'filter_css_file'), 10, 2);
35 }
36
37 /**
38 * Sync local elementor files to GCS.
39 * @param $url
40 * @param $scheme
41 * @param $orig_scheme
42 * @return string
43 */
44 public function sync_rewrite_url($url, $scheme, $orig_scheme) {
45 try {
46 if (strpos($url, 'elementor/') !== false) {
47 $wp_uploads_dir = wp_get_upload_dir();
48 $name = str_replace($wp_uploads_dir['baseurl'] . '/', '', $url);
49 if ($name != $url) {
50 $absolutePath = $wp_uploads_dir['basedir'] . '/' . $name;
51 $name = apply_filters('wp_stateless_file_name', $name, 0);
52 do_action('sm:sync::syncFile', $name, $absolutePath);
53
54 if (!in_array(ud_get_stateless_media()->get('sm.mode'), ['disabled', 'backup'])) {
55 $url = ud_get_stateless_media()->get_gs_host() . '/' . $name;
56 }
57 }
58 }
59 } catch (\Exception $e) {
60 // @todo maybe log the exception.
61 }
62 // We are in filter so need to return the passed value.
63 return $url;
64 }
65
66 /**
67 * To regenerate/delete files click Regenerate Files in
68 * Elementor >> Tools >> General >> Regenerate CSS
69 * All files will be deleted from GCS.
70 * And will be copied to GCS again on next page view.
71 */
72 public function delete_elementor_files() {
73 do_action('sm:sync::deleteFiles', 'elementor/');
74 }
75
76 /**
77 * Delete GCS file on update/delete post.
78 * @param $post_ID
79 * @param null $post
80 * @param null $update
81 */
82 public function delete_css_files($post_ID, $post = null, $update = null) {
83 if ($update || current_action() === 'deleted_post') {
84 $post_css = new \Elementor\Core\Files\CSS\Post($post_ID);
85
86 // elementor/ css/ 'post-' . $post_id . '.css'
87 $name = $post_css::UPLOADS_DIR . $post_css::DEFAULT_FILES_DIR . $post_css->get_file_name();
88 $name = apply_filters('wp_stateless_file_name', $name, 0);
89 do_action('sm:sync::deleteFile', $name);
90 }
91 }
92
93 /**
94 * Delete elementor global css file when global style is updated on Elementor Editor.
95 * @param $success_response_data
96 * @param $id
97 * @param $data
98 * @return mixed
99 */
100 public function delete_global_css($success_response_data, $id, $data) {
101 try {
102 $post_css = new \Elementor\Core\Files\CSS\Global_CSS('global.css');
103 // elementor/ css/ 'global.css'
104 $name = $post_css::UPLOADS_DIR . $post_css::DEFAULT_FILES_DIR . $post_css->get_file_name();
105 $name = apply_filters('wp_stateless_file_name', $name, 0);
106 do_action('sm:sync::deleteFile', $name);
107 } catch (\Exception $e) {
108 // @todo maybe log the exception.
109 }
110 // We are in filter so need to return the passed value.
111 return $success_response_data;
112 }
113
114 /**
115 * @param $name
116 * @param $absolutePath
117 */
118 public function filter_css_file($name, $absolutePath) {
119 if ($upload_data = wp_upload_dir() && file_exists($absolutePath)) {
120 try {
121 $content = file_get_contents($absolutePath);
122
123 if (!empty($upload_data['baseurl']) && !empty($content)) {
124 $baseurl = preg_replace('/https?:\/\//', '', $upload_data['baseurl']);
125 $root_dir = trim(ud_get_stateless_media()->get('sm.root_dir'), '/ '); // Remove any forward slash and empty space.
126 $root_dir = apply_filters("wp_stateless_handle_root_dir", $root_dir);
127 $root_dir = !empty($root_dir) ? $root_dir . '/' : '';
128 $image_host = ud_get_stateless_media()->get_gs_host() . $root_dir;
129 $file_ext = ud_get_stateless_media()->replaceable_file_types();
130
131 preg_match_all('/(https?:\/\/' . str_replace('/', '\/', $baseurl) . ')\/(.+?)(' . $file_ext . ')/i', $content, $matches);
132 if (!empty($matches)) {
133 foreach ($matches[0] as $key => $match) {
134 $id = attachment_url_to_postid($match);
135 if (!empty($id)) {
136 Utility::add_media(null, $id, true);
137 }
138 }
139 }
140
141 $content = preg_replace('/(https?:\/\/' . str_replace('/', '\/', $baseurl) . ')\/(.+?)(' . $file_ext . ')/i', $image_host . '/$2$3', $content);
142 file_put_contents($absolutePath, $content);
143 preg_match('/post-(\d+).css/', $name, $match);
144
145 if (!empty($match[1])) {
146 $_elementor_css = get_post_meta($match[1], '_elementor_css', true);
147 if (!empty($_elementor_css)) {
148 $_elementor_css['time'] = time();
149 }
150 }
151 }
152 } catch (\Exception $e) {
153 }
154 }
155 }
156 }
157 }
158 }
159