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