@@ -26,6 +26,10 @@ public function module_init($sm){
2626 add_action ( 'save_post ' , array ($ this , 'delete_css_files ' ), 10 , 3 );
2727 add_action ( 'deleted_post ' , array ($ this , 'delete_css_files ' ) );
2828 add_filter ( "elementor/settings/general/success_response_data " , array ($ this , 'delete_global_css ' ), 10 , 3 );
29+ // return file names
30+ add_filter ( 'sm:sync::nonMediaFiles ' , array ($ this , 'sync_non_media_files ' ) );
31+ add_action ( 'sm::pre::sync::nonMediaFiles ' , array ($ this , 'filter_css_file ' ), 10 , 2 );
32+
2933 }
3034
3135 /**
@@ -109,6 +113,67 @@ public function delete_global_css($success_response_data, $id, $data){
109113 return $ success_response_data ;
110114 }
111115
116+ /**
117+ *
118+ */
119+ public function sync_non_media_files ($ files ){
120+ $ wp_uploads_dir = wp_get_upload_dir ();
121+ $ base_dir = wp_normalize_path ($ wp_uploads_dir ['basedir ' ] . '/ ' );
122+ $ dir = $ base_dir . \Elementor \Core \Files \CSS \Post::UPLOADS_DIR . \Elementor \Core \Files \CSS \Post::DEFAULT_FILES_DIR ;
123+ $ file_list = list_files ($ dir );
124+
125+ foreach ($ file_list as $ file ){
126+ $ files [] = str_replace ($ base_dir , '' , wp_normalize_path ($ file ));
127+ }
128+ return $ files ;
129+ }
130+
131+ /**
132+ * @param $content
133+ * @return mixed
134+ */
135+ public function filter_css_file ( $ name , $ absolutePath ) {
136+
137+ if ( $ upload_data = wp_upload_dir () && file_exists ($ absolutePath )) {
138+ try {
139+ $ content = file_get_contents ($ absolutePath );
140+
141+ if ( !empty ( $ upload_data ['baseurl ' ] ) && !empty ( $ content ) ) {
142+ $ baseurl = preg_replace ('/https?:\/\// ' ,'' ,$ upload_data ['baseurl ' ]);
143+ $ root_dir = trim ( ud_get_stateless_media ()->get ( 'sm.root_dir ' ), '/ ' ); // Remove any forward slash and empty space.
144+ $ root_dir = !empty ( $ root_dir ) ? $ root_dir . '/ ' : '' ;
145+ $ image_host = ud_get_stateless_media ()->get_gs_host () . $ root_dir ;
146+ $ file_ext = ud_get_stateless_media ()->replaceable_file_types ();
147+
148+ preg_match_all ( '/(https?:\/\/ ' .str_replace ('/ ' , '\/ ' , $ baseurl ).')\/(.+?)( ' .$ file_ext .')/i ' , $ content , $ matches );
149+ if (!empty ($ matches )){
150+ foreach ($ matches [0 ] as $ key => $ match ){
151+ $ id = attachment_url_to_postid ($ match );
152+ if (!empty ($ id )){
153+ Utility::add_media (null , $ id , true );
154+ }
155+ }
156+ }
157+
158+ $ content = preg_replace ( '/(https?:\/\/ ' .str_replace ('/ ' , '\/ ' , $ baseurl ).')\/(.+?)( ' .$ file_ext .')/i ' , $ image_host .'/$2$3 ' , $ content );
159+ file_put_contents ($ absolutePath , $ content );
160+ preg_match ('/post-(\d+).css/ ' , $ name , $ match );
161+
162+ if (!empty ($ match [1 ])){
163+ $ _elementor_css = get_post_meta ($ match [1 ], '_elementor_css ' , true );
164+ if (!empty ($ _elementor_css )){
165+ $ _elementor_css ['time ' ] = time ();
166+ }
167+ }
168+ }
169+ }
170+ catch (Exception $ e ){
171+
172+ }
173+
174+ }
175+ }
176+
112177
113178 }
114179
0 commit comments