| 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\Responsive\Files\Frontend; |
| 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 |
new Svg_Handler(); |
| 40 |
new Json_Handler(); |
| 41 |
} |
| 42 |
|
| 43 |
public function get( $class, $args ) { |
| 44 |
$id = $class . '-' . wp_json_encode( $args ); |
| 45 |
|
| 46 |
if ( ! isset( $this->files[ $id ] ) ) { |
| 47 |
// Create an instance from dynamic args length. |
| 48 |
$reflection_class = new \ReflectionClass( $class ); |
| 49 |
$this->files[ $id ] = $reflection_class->newInstanceArgs( $args ); |
| 50 |
} |
| 51 |
|
| 52 |
return $this->files[ $id ]; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* On post delete. |
| 57 |
* |
| 58 |
* Delete post CSS immediately after a post is deleted from the database. |
| 59 |
* |
| 60 |
* Fired by `deleted_post` action. |
| 61 |
* |
| 62 |
* @since 1.2.0 |
| 63 |
* @access public |
| 64 |
* |
| 65 |
* @param string $post_id Post ID. |
| 66 |
*/ |
| 67 |
public function on_delete_post( $post_id ) { |
| 68 |
if ( ! Utils::is_post_support( $post_id ) ) { |
| 69 |
return; |
| 70 |
} |
| 71 |
|
| 72 |
$css_file = Post_CSS::create( $post_id ); |
| 73 |
|
| 74 |
$css_file->delete(); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* On export post meta. |
| 79 |
* |
| 80 |
* When exporting data using WXR, skip post CSS file meta key. This way the |
| 81 |
* export won't contain the post CSS file data used by Elementor. |
| 82 |
* |
| 83 |
* Fired by `wxr_export_skip_postmeta` filter. |
| 84 |
* |
| 85 |
* @since 1.2.0 |
| 86 |
* @access public |
| 87 |
* |
| 88 |
* @param bool $skip Whether to skip the current post meta. |
| 89 |
* @param string $meta_key Current meta key. |
| 90 |
* |
| 91 |
* @return bool Whether to skip the post CSS meta. |
| 92 |
*/ |
| 93 |
public function on_export_post_meta( $skip, $meta_key ) { |
| 94 |
if ( Post_CSS::META_KEY === $meta_key ) { |
| 95 |
$skip = true; |
| 96 |
} |
| 97 |
|
| 98 |
return $skip; |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Clear cache. |
| 103 |
* |
| 104 |
* Delete all meta containing files data. And delete the actual |
| 105 |
* files from the upload directory. |
| 106 |
* |
| 107 |
* @since 1.2.0 |
| 108 |
* @access public |
| 109 |
*/ |
| 110 |
public function clear_cache() { |
| 111 |
// Delete files. |
| 112 |
$path = Base::get_base_uploads_dir() . Base::DEFAULT_FILES_DIR . '*'; |
| 113 |
|
| 114 |
foreach ( glob( $path ) as $file_path ) { |
| 115 |
unlink( $file_path ); |
| 116 |
} |
| 117 |
|
| 118 |
delete_post_meta_by_key( Post_CSS::META_KEY ); |
| 119 |
|
| 120 |
delete_option( Global_CSS::META_KEY ); |
| 121 |
delete_option( Frontend::META_KEY ); |
| 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 register_ajax_actions( Ajax $ajax ) { |
| 134 |
$ajax->register_ajax_action( 'enable_unfiltered_files_upload', [ $this, 'ajax_unfiltered_files_upload' ] ); |
| 135 |
} |
| 136 |
|
| 137 |
public function ajax_unfiltered_files_upload() { |
| 138 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 139 |
return; |
| 140 |
} |
| 141 |
|
| 142 |
update_option( Files_Upload_Handler::OPTION_KEY, 1 ); |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Register actions. |
| 147 |
* |
| 148 |
* Register filters and actions for the files manager. |
| 149 |
* |
| 150 |
* @since 1.2.0 |
| 151 |
* @access private |
| 152 |
*/ |
| 153 |
private function register_actions() { |
| 154 |
add_action( 'deleted_post', [ $this, 'on_delete_post' ] ); |
| 155 |
|
| 156 |
// Ajax. |
| 157 |
add_action( 'elementor/ajax/register_actions', [ $this, 'register_ajax_actions' ] ); |
| 158 |
|
| 159 |
add_filter( 'wxr_export_skip_postmeta', [ $this, 'on_export_post_meta' ], 10, 2 ); |
| 160 |
} |
| 161 |
} |
| 162 |
|