| 1 |
<?php |
| 2 |
namespace Elementor\Core\Files; |
| 3 |
|
| 4 |
use Elementor\Core\Common\Modules\Ajax\Module as Ajax; |
| 5 |
use Elementor\Core\Files\CSS\Global_CSS; |
| 6 |
use Elementor\Core\Files\CSS\Post as Post_CSS; |
| 7 |
use Elementor\Core\Page_Assets\Data_Managers\Base as Page_Assets_Data_Manager; |
| 8 |
use Elementor\Core\Responsive\Files\Frontend; |
| 9 |
use Elementor\Plugin; |
| 10 |
use Elementor\Utils; |
| 11 |
|
| 12 |
if ( ! defined( 'ABSPATH' ) ) { |
| 13 |
exit; // Exit if accessed directly. |
| 14 |
} |
| 15 |
|
| 16 |
/** |
| 17 |
* Elementor files manager. |
| 18 |
* |
| 19 |
* Elementor files manager handler class is responsible for creating files. |
| 20 |
* |
| 21 |
* @since 1.2.0 |
| 22 |
*/ |
| 23 |
class Manager { |
| 24 |
|
| 25 |
private $files = []; |
| 26 |
|
| 27 |
/** |
| 28 |
* Files manager constructor. |
| 29 |
* |
| 30 |
* Initializing the Elementor files manager. |
| 31 |
* |
| 32 |
* @since 1.2.0 |
| 33 |
* @access public |
| 34 |
*/ |
| 35 |
public function __construct() { |
| 36 |
$this->register_actions(); |
| 37 |
} |
| 38 |
|
| 39 |
public function get( $class, $args ) { |
| 40 |
$id = $class . '-' . wp_json_encode( $args ); |
| 41 |
|
| 42 |
if ( ! isset( $this->files[ $id ] ) ) { |
| 43 |
// Create an instance from dynamic args length. |
| 44 |
$reflection_class = new \ReflectionClass( $class ); |
| 45 |
$this->files[ $id ] = $reflection_class->newInstanceArgs( $args ); |
| 46 |
} |
| 47 |
|
| 48 |
return $this->files[ $id ]; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* On post delete. |
| 53 |
* |
| 54 |
* Delete post CSS immediately after a post is deleted from the database. |
| 55 |
* |
| 56 |
* Fired by `deleted_post` action. |
| 57 |
* |
| 58 |
* @since 1.2.0 |
| 59 |
* @access public |
| 60 |
* |
| 61 |
* @param string $post_id Post ID. |
| 62 |
*/ |
| 63 |
public function on_delete_post( $post_id ) { |
| 64 |
if ( ! Utils::is_post_support( $post_id ) ) { |
| 65 |
return; |
| 66 |
} |
| 67 |
|
| 68 |
$css_file = Post_CSS::create( $post_id ); |
| 69 |
|
| 70 |
$css_file->delete(); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* On export post meta. |
| 75 |
* |
| 76 |
* When exporting data using WXR, skip post CSS file meta key. This way the |
| 77 |
* export won't contain the post CSS file data used by Elementor. |
| 78 |
* |
| 79 |
* Fired by `wxr_export_skip_postmeta` filter. |
| 80 |
* |
| 81 |
* @since 1.2.0 |
| 82 |
* @access public |
| 83 |
* |
| 84 |
* @param bool $skip Whether to skip the current post meta. |
| 85 |
* @param string $meta_key Current meta key. |
| 86 |
* |
| 87 |
* @return bool Whether to skip the post CSS meta. |
| 88 |
*/ |
| 89 |
public function on_export_post_meta( $skip, $meta_key ) { |
| 90 |
if ( Post_CSS::META_KEY === $meta_key ) { |
| 91 |
$skip = true; |
| 92 |
} |
| 93 |
|
| 94 |
return $skip; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Clear cache. |
| 99 |
* |
| 100 |
* Delete all meta containing files data. And delete the actual |
| 101 |
* files from the upload directory. |
| 102 |
* |
| 103 |
* @since 1.2.0 |
| 104 |
* @access public |
| 105 |
*/ |
| 106 |
public function clear_cache() { |
| 107 |
// Delete files. |
| 108 |
$path = Base::get_base_uploads_dir() . Base::DEFAULT_FILES_DIR . '*'; |
| 109 |
|
| 110 |
foreach ( glob( $path ) as $file_path ) { |
| 111 |
unlink( $file_path ); |
| 112 |
} |
| 113 |
|
| 114 |
delete_post_meta_by_key( Post_CSS::META_KEY ); |
| 115 |
|
| 116 |
delete_option( Global_CSS::META_KEY ); |
| 117 |
delete_option( Frontend::META_KEY ); |
| 118 |
|
| 119 |
$this->reset_assets_data(); |
| 120 |
|
| 121 |
/** |
| 122 |
* Elementor clear files. |
| 123 |
* |
| 124 |
* Fires after Elementor clears files |
| 125 |
* |
| 126 |
* @since 2.1.0 |
| 127 |
*/ |
| 128 |
do_action( 'elementor/core/files/clear_cache' ); |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Register Ajax Actions |
| 133 |
* |
| 134 |
* Deprecated - use the Uploads Manager instead. |
| 135 |
* |
| 136 |
* @deprecated 3.5.0 |
| 137 |
* |
| 138 |
* @param Ajax $ajax |
| 139 |
*/ |
| 140 |
public function register_ajax_actions( Ajax $ajax ) { |
| 141 |
Plugin::$instance->modules_manager->get_modules( 'dev-tools' )->deprecation->deprecated_function( __METHOD__, '3.5.0' ); |
| 142 |
|
| 143 |
Plugin::$instance->uploads_manager->register_ajax_actions( $ajax ); |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Ajax Unfiltered Files Upload |
| 148 |
* |
| 149 |
* Deprecated - use the Uploads Manager instead. |
| 150 |
* |
| 151 |
* @deprecated 3.5.0 |
| 152 |
*/ |
| 153 |
public function ajax_unfiltered_files_upload() { |
| 154 |
Plugin::$instance->modules_manager->get_modules( 'dev-tools' )->deprecation->deprecated_function( __METHOD__, '3.5.0' ); |
| 155 |
|
| 156 |
Plugin::$instance->uploads_manager->enable_unfiltered_files_upload(); |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Register actions. |
| 161 |
* |
| 162 |
* Register filters and actions for the files manager. |
| 163 |
* |
| 164 |
* @since 1.2.0 |
| 165 |
* @access private |
| 166 |
*/ |
| 167 |
private function register_actions() { |
| 168 |
add_action( 'deleted_post', [ $this, 'on_delete_post' ] ); |
| 169 |
|
| 170 |
add_filter( 'wxr_export_skip_postmeta', [ $this, 'on_export_post_meta' ], 10, 2 ); |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* Reset Assets Data. |
| 175 |
* |
| 176 |
* Reset the page assets data. |
| 177 |
* |
| 178 |
* @since 3.3.0 |
| 179 |
* @access private |
| 180 |
*/ |
| 181 |
private function reset_assets_data() { |
| 182 |
delete_option( Page_Assets_Data_Manager::ASSETS_DATA_KEY ); |
| 183 |
} |
| 184 |
} |
| 185 |
|