Skip to content

Commit 648349f

Browse files
author
Md. Alimuzzaman Alim
committed
Delete GCS file on post update #235
1 parent dd56f0f commit 648349f

File tree

2 files changed

+55
-3
lines changed

2 files changed

+55
-3
lines changed

lib/classes/class-sync-non-media.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class SyncNonMedia {
1717

1818
public function __construct(){
1919
$this->registered_files = get_option('sm_synced_files', array());
20+
// print_r($this->registered_files);
2021
// Manual sync using sync tab.
2122
// called from ajax action_get_non_library_files_id
2223
// Return files to be manualy sync from sync tab.
@@ -60,7 +61,10 @@ public function add_file($file){
6061
* @param:
6162
* $name: Reletive path to upload dir.
6263
* $absolutePath: Full path of the file
63-
* $forced: Whether to force to move the file to GCS even it's already exists.
64+
* $forced: Type: bool/2; Whether to force to move the file to GCS even it's already exists.
65+
* true: Check whether it's already synced or not in database.
66+
* 2 (int): Force to overwrite on GCS
67+
*
6468
* @return:
6569
* $media: Media object returned from client->add_media() method.
6670
* @throws: Exception File not found
@@ -198,6 +202,7 @@ public function delete_file($file, $force = true){
198202
}
199203
// Removing file for GCS
200204
$this->client->remove_media($file);
205+
print_r($this->registered_files);
201206

202207
if($key = array_search($file, $this->registered_files)){
203208
if(isset($this->registered_files[$key])){

lib/classes/compatibility/elementor.php

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
* Plugin URI: https://wordpress.org/plugins/elementor/
55
*
66
* Compatibility Description: Ensures compatibility with Elementor.
7+
*
8+
* @Todo add manual sync to sync all elementor css file to GCS.
79
*
810
*/
911

@@ -19,15 +21,20 @@ class Elementor extends ICompatibility {
1921
protected $plugin_file = 'elementor/elementor.php';
2022

2123
public function module_init($sm){
22-
add_filter('set_url_scheme', array($this, 'sync_rewrite_url'), 10, 3);
24+
add_filter( 'set_url_scheme', array($this, 'sync_rewrite_url'), 10, 3 );
25+
add_action( 'elementor/core/files/clear_cache', array($this, 'delete_elementor_files') );
26+
add_action( 'save_post', array($this, 'delete_css_files'), 10, 3 );
27+
add_action( 'deleted_post', array($this, 'delete_css_files') );
2328
}
2429

30+
/**
31+
* Sync local elementor files to GCS.
32+
*/
2533
public function sync_rewrite_url($url, $scheme, $orig_scheme){
2634
try{
2735
if(strpos($url, 'elementor/') !== false){
2836
$wp_uploads_dir = wp_get_upload_dir();
2937
$name = str_replace($wp_uploads_dir['baseurl'] . '/', '', $url);
30-
3138
if($name != $url){
3239
$name = apply_filters( 'wp_stateless_file_name', $name);
3340
do_action('sm:sync::syncFile', $name, $wp_uploads_dir['basedir'] . '/' . $name);
@@ -42,6 +49,46 @@ public function sync_rewrite_url($url, $scheme, $orig_scheme){
4249
return $url;
4350
}
4451

52+
/**
53+
* To regenerate/delete files click Regenerate Files in
54+
* Elementor >> Tools >> General >> Regenerate CSS
55+
* All files will be deleted from GCS.
56+
* And will be copied to GCS again on next page view.
57+
*/
58+
public function delete_elementor_files(){
59+
do_action('sm:sync::deleteFiles', 'elementor/');
60+
}
61+
62+
/**
63+
* Delete GCS file on update/delete post.
64+
*/
65+
public function delete_css_files( $post_ID, $post = null, $update = null ){
66+
67+
if($update || current_action() === 'deleted_post'){
68+
$wp_uploads_dir = wp_get_upload_dir();
69+
70+
$post_css = new \Elementor\Core\Files\CSS\Post( $post_ID );
71+
72+
// elementor/ css/ 'post-' . $post_id . '.css'
73+
$name = $post_css::UPLOADS_DIR . $post_css::DEFAULT_FILES_DIR . $post_css->get_file_name();
74+
$name = apply_filters( 'wp_stateless_file_name', $name);
75+
// Removing status so that file will be generated next time.
76+
do_action('sm:sync::deleteFile', $name);
77+
78+
// $meta = $post_css->update();
79+
80+
// sync_file($name, $absolutePath, $forced = false);
81+
// here $forced = 2 mean Force to overwrite on GCS
82+
// do_action('sm:sync::syncFile', $name, $wp_uploads_dir['basedir'] . '/' . $name, 2);
83+
84+
// $meta = $post_css->get_meta();
85+
// $meta['status'] = '';
86+
// update_post_meta($post_ID, $post_css::META_KEY, $meta);
87+
88+
}
89+
}
90+
91+
4592
}
4693

4794
}

0 commit comments

Comments
 (0)