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