| 1 |
<?php |
| 2 |
namespace Elementor\Core\Files; |
| 3 |
|
| 4 |
use Elementor\Core\Base\Document as Document_Base; |
| 5 |
use Elementor\Core\Base\Elements_Iteration_Actions\Assets; |
| 6 |
use Elementor\Core\Common\Modules\Ajax\Module as Ajax; |
| 7 |
use Elementor\Core\Files\CSS\Post as Post_CSS; |
| 8 |
use Elementor\Core\Page_Assets\Data_Managers\Base as Page_Assets_Data_Manager; |
| 9 |
use Elementor\Core\Responsive\Files\Frontend; |
| 10 |
use Elementor\Plugin; |
| 11 |
use Elementor\Utils; |
| 12 |
|
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; // Exit if accessed directly. |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Elementor files manager. |
| 19 |
* |
| 20 |
* Elementor files manager handler class is responsible for creating files. |
| 21 |
* |
| 22 |
* @since 1.2.0 |
| 23 |
*/ |
| 24 |
class Manager { |
| 25 |
|
| 26 |
private $files = []; |
| 27 |
|
| 28 |
/** |
| 29 |
* Files manager constructor. |
| 30 |
* |
| 31 |
* Initializing the Elementor files manager. |
| 32 |
* |
| 33 |
* @since 1.2.0 |
| 34 |
* @access public |
| 35 |
*/ |
| 36 |
public function __construct() { |
| 37 |
$this->register_actions(); |
| 38 |
} |
| 39 |
|
| 40 |
public function get( $class, $args ) { |
| 41 |
$id = $class . '-' . wp_json_encode( $args ); |
| 42 |
|
| 43 |
if ( ! isset( $this->files[ $id ] ) ) { |
| 44 |
// Create an instance from dynamic args length. |
| 45 |
$reflection_class = new \ReflectionClass( $class ); |
| 46 |
$this->files[ $id ] = $reflection_class->newInstanceArgs( $args ); |
| 47 |
} |
| 48 |
|
| 49 |
return $this->files[ $id ]; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* On post delete. |
| 54 |
* |
| 55 |
* Delete post CSS immediately after a post is deleted from the database. |
| 56 |
* |
| 57 |
* Fired by `deleted_post` action. |
| 58 |
* |
| 59 |
* @since 1.2.0 |
| 60 |
* @access public |
| 61 |
* |
| 62 |
* @param string $post_id Post ID. |
| 63 |
*/ |
| 64 |
public function on_delete_post( $post_id ) { |
| 65 |
if ( ! Utils::is_post_support( $post_id ) ) { |
| 66 |
return; |
| 67 |
} |
| 68 |
|
| 69 |
$css_file = Post_CSS::create( $post_id ); |
| 70 |
|
| 71 |
$css_file->delete(); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* On export post meta. |
| 76 |
* |
| 77 |
* When exporting data using WXR, skip post CSS file meta key. This way the |
| 78 |
* export won't contain the post CSS file data used by Elementor. |
| 79 |
* |
| 80 |
* Fired by `wxr_export_skip_postmeta` filter. |
| 81 |
* |
| 82 |
* @since 1.2.0 |
| 83 |
* @access public |
| 84 |
* |
| 85 |
* @param bool $skip Whether to skip the current post meta. |
| 86 |
* @param string $meta_key Current meta key. |
| 87 |
* |
| 88 |
* @return bool Whether to skip the post CSS meta. |
| 89 |
*/ |
| 90 |
public function on_export_post_meta( $skip, $meta_key ) { |
| 91 |
if ( Post_CSS::META_KEY === $meta_key ) { |
| 92 |
$skip = true; |
| 93 |
} |
| 94 |
|
| 95 |
return $skip; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Clear cache. |
| 100 |
* |
| 101 |
* Delete all meta containing files data. And delete the actual |
| 102 |
* files from the upload directory. |
| 103 |
* |
| 104 |
* @since 1.2.0 |
| 105 |
* @access public |
| 106 |
*/ |
| 107 |
public function clear_cache() { |
| 108 |
// Delete files. |
| 109 |
$path = Base::get_base_uploads_dir() . Base::DEFAULT_FILES_DIR . '*'; |
| 110 |
|
| 111 |
foreach ( glob( $path ) as $file_path ) { |
| 112 |
unlink( $file_path ); |
| 113 |
} |
| 114 |
|
| 115 |
delete_post_meta_by_key( Post_CSS::META_KEY ); |
| 116 |
delete_post_meta_by_key( Document_Base::CACHE_META_KEY ); |
| 117 |
delete_post_meta_by_key( Assets::ASSETS_META_KEY ); |
| 118 |
|
| 119 |
delete_option( Frontend::META_KEY ); |
| 120 |
|
| 121 |
$this->reset_assets_data(); |
| 122 |
|
| 123 |
/** |
| 124 |
* Elementor clear files. |
| 125 |
* |
| 126 |
* Fires after Elementor clears files |
| 127 |
* |
| 128 |
* @since 2.1.0 |
| 129 |
*/ |
| 130 |
do_action( 'elementor/core/files/clear_cache' ); |
| 131 |
} |
| 132 |
|
| 133 |
public function clear_custom_image_sizes() { |
| 134 |
if ( ! defined( 'BFITHUMB_UPLOAD_DIR' ) ) { |
| 135 |
return; |
| 136 |
} |
| 137 |
|
| 138 |
$upload_info = wp_upload_dir(); |
| 139 |
$upload_dir = $upload_info['basedir'] . '/' . BFITHUMB_UPLOAD_DIR; |
| 140 |
|
| 141 |
$path = $upload_dir . '/*'; |
| 142 |
|
| 143 |
foreach ( glob( $path ) as $file_path ) { |
| 144 |
unlink( $file_path ); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Register Ajax Actions |
| 150 |
* |
| 151 |
* Deprecated - use the Uploads Manager instead. |
| 152 |
* |
| 153 |
* @deprecated 3.5.0 |
| 154 |
* |
| 155 |
* @param Ajax $ajax |
| 156 |
*/ |
| 157 |
public function register_ajax_actions( Ajax $ajax ) { |
| 158 |
Plugin::$instance->modules_manager->get_modules( 'dev-tools' )->deprecation->deprecated_function( __METHOD__, '3.5.0' ); |
| 159 |
|
| 160 |
Plugin::$instance->uploads_manager->register_ajax_actions( $ajax ); |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Ajax Unfiltered Files Upload |
| 165 |
* |
| 166 |
* Deprecated - use the Uploads Manager instead. |
| 167 |
* |
| 168 |
* @deprecated 3.5.0 |
| 169 |
*/ |
| 170 |
public function ajax_unfiltered_files_upload() { |
| 171 |
Plugin::$instance->modules_manager->get_modules( 'dev-tools' )->deprecation->deprecated_function( __METHOD__, '3.5.0' ); |
| 172 |
|
| 173 |
Plugin::$instance->uploads_manager->enable_unfiltered_files_upload(); |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Register actions. |
| 178 |
* |
| 179 |
* Register filters and actions for the files manager. |
| 180 |
* |
| 181 |
* @since 1.2.0 |
| 182 |
* @access private |
| 183 |
*/ |
| 184 |
private function register_actions() { |
| 185 |
add_action( 'deleted_post', [ $this, 'on_delete_post' ] ); |
| 186 |
|
| 187 |
add_filter( 'wxr_export_skip_postmeta', [ $this, 'on_export_post_meta' ], 10, 2 ); |
| 188 |
|
| 189 |
add_action( 'update_option_home', function () { |
| 190 |
$this->reset_assets_data(); |
| 191 |
} ); |
| 192 |
|
| 193 |
add_action( 'update_option_siteurl', function () { |
| 194 |
$this->reset_assets_data(); |
| 195 |
} ); |
| 196 |
|
| 197 |
add_action( 'rest_api_init', [ $this, 'register_endpoints' ] ); |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* Reset Assets Data. |
| 202 |
* |
| 203 |
* Reset the page assets data. |
| 204 |
* |
| 205 |
* @since 3.3.0 |
| 206 |
* @access private |
| 207 |
*/ |
| 208 |
private function reset_assets_data() { |
| 209 |
delete_option( Page_Assets_Data_Manager::ASSETS_DATA_KEY ); |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* Generate CSS. |
| 214 |
* |
| 215 |
* Generates CSS for all posts built with Elementor. |
| 216 |
* |
| 217 |
* @since 3.25.0 |
| 218 |
* @access public |
| 219 |
*/ |
| 220 |
public function generate_css() { |
| 221 |
$batch_size = apply_filters( 'elementor/core/files/generate_css/batch_size', 100 ); |
| 222 |
$processed_posts = 0; |
| 223 |
|
| 224 |
while ( true ) { |
| 225 |
$args = [ |
| 226 |
'post_type' => get_post_types(), |
| 227 |
'posts_per_page' => $batch_size, |
| 228 |
'meta_query' => [ |
| 229 |
[ |
| 230 |
'key' => Document_Base::BUILT_WITH_ELEMENTOR_META_KEY, |
| 231 |
'compare' => 'EXISTS', |
| 232 |
], |
| 233 |
], |
| 234 |
'offset' => $processed_posts, |
| 235 |
'fields' => 'ids', |
| 236 |
]; |
| 237 |
|
| 238 |
$query = new \WP_Query( $args ); |
| 239 |
|
| 240 |
if ( empty( $query->posts ) ) { |
| 241 |
break; |
| 242 |
} |
| 243 |
|
| 244 |
foreach ( $query->posts as $post_id ) { |
| 245 |
$document = Plugin::$instance->documents->get_doc_for_frontend( $post_id ); |
| 246 |
|
| 247 |
if ( $document ) { |
| 248 |
$css_file = Post_CSS::create( $post_id ); |
| 249 |
$css_file->update(); |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
$processed_posts += $batch_size; |
| 254 |
} |
| 255 |
|
| 256 |
/** |
| 257 |
* Elementor Generate CSS files. |
| 258 |
* |
| 259 |
* Fires after Elementor generates new CSS files |
| 260 |
* |
| 261 |
* @since 3.25.0 |
| 262 |
*/ |
| 263 |
do_action( 'elementor/core/files/after_generate_css' ); |
| 264 |
} |
| 265 |
|
| 266 |
public function register_endpoints() { |
| 267 |
register_rest_route( |
| 268 |
'elementor/v1', |
| 269 |
'/cache', |
| 270 |
[ |
| 271 |
'methods' => \WP_REST_Server::DELETABLE, |
| 272 |
'callback' => [ $this, 'clear_cache' ], |
| 273 |
'permission_callback' => function() { |
| 274 |
return current_user_can( 'manage_options' ); |
| 275 |
}, |
| 276 |
] |
| 277 |
); |
| 278 |
} |
| 279 |
} |
| 280 |
|