| 1 |
<?php |
| 2 |
|
| 3 |
defined( 'ABSPATH' ) || die(); |
| 4 |
|
| 5 |
use Elementor\Control_Base_Multiple; |
| 6 |
|
| 7 |
/** |
| 8 |
* Sellkit File Uploader. |
| 9 |
* |
| 10 |
* A Sellkit File Uploader Control will let you upload any type of file in a customizable location. |
| 11 |
* |
| 12 |
* @since 1.5.0 |
| 13 |
* |
| 14 |
* @param string $label Optional. The label that appears above of the field. Default is empty. |
| 15 |
* @param string $title Optional. The field title that appears on mouse hover. Default is empty. |
| 16 |
* @param string $description Optional. The description that appears below the field. Default is empty. |
| 17 |
* @param bool $show_label Optional. Whether to display the label. Default is true. |
| 18 |
* @param bool $label_block Optional. Whether to display the label in a separate line. Default is true. |
| 19 |
* @param string $separator Optional. Set the position of the control separator. |
| 20 |
* Available values are 'default', 'before', 'after' and 'none'. 'default' will position the separator |
| 21 |
* depending on the control type. 'before' / 'after' will position the separator before/after the control. |
| 22 |
* 'none' will hide the separator. Default is 'default'. |
| 23 |
* |
| 24 |
* @return array Including file's string properties Name and Path : [ 'name' => '', 'path' => '']. |
| 25 |
*/ |
| 26 |
class Sellkit_Elementor_Controls_File_Uploader extends Control_Base_Multiple { |
| 27 |
|
| 28 |
/** |
| 29 |
* File uplodaer control constructor. |
| 30 |
* |
| 31 |
* Adds ajax hook for file upload. |
| 32 |
* |
| 33 |
* @since 1.5.0 |
| 34 |
*/ |
| 35 |
public function __construct() { |
| 36 |
parent::__construct(); |
| 37 |
|
| 38 |
// Register AJAX action. |
| 39 |
add_action( 'wp_ajax_sellkit_control_file_upload', [ $this, 'handle_file_upload' ] ); |
| 40 |
|
| 41 |
// Define nonce for file upload ajax action. |
| 42 |
add_action( 'elementor/editor/after_enqueue_scripts', function() { |
| 43 |
wp_localize_script( |
| 44 |
'sellkit-editor', |
| 45 |
'sellkitNonceEditorFileUploader', |
| 46 |
[ wp_create_nonce( 'sellkit_fileuploader_editor' ) ] |
| 47 |
); |
| 48 |
}, 20 ); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Get type of the control, used in declaring "type" when creating Elementor control. |
| 53 |
* |
| 54 |
* @return string |
| 55 |
* |
| 56 |
* @since 1.5.0 |
| 57 |
*/ |
| 58 |
public function get_type() { |
| 59 |
return 'sellkit_file_uploader'; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Get array of default values for this control. |
| 64 |
* |
| 65 |
* @return array |
| 66 |
* |
| 67 |
* @since 1.5.0 |
| 68 |
*/ |
| 69 |
public function get_default_value() { |
| 70 |
return [ |
| 71 |
'files' => [], |
| 72 |
]; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Get array of default settings for this control. |
| 77 |
* |
| 78 |
* @return array |
| 79 |
* |
| 80 |
* @since 1.5.0 |
| 81 |
*/ |
| 82 |
protected function get_default_settings() { |
| 83 |
return [ |
| 84 |
'label_block' => false, |
| 85 |
]; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Echo the template of this control in the Elementor's editor panel. |
| 90 |
* |
| 91 |
* @since 1.5.0 |
| 92 |
*/ |
| 93 |
public function content_template() { |
| 94 |
?> |
| 95 |
<div class="elementor-control-field sellkit-control-file-uploader"> |
| 96 |
<label class="elementor-control-title">{{{ data.label }}}</label> |
| 97 |
<div class="elementor-control-input-wrapper"> |
| 98 |
<div class="elementor-button-wrapper"> |
| 99 |
<button type="button" class="elementor-button elementor-button-default elementor-repeater-add sellkit-control-file-uploader-button"> |
| 100 |
<input |
| 101 |
type="file" |
| 102 |
data-max-upload-limit="<?php echo esc_attr( wp_max_upload_size() ); ?>" |
| 103 |
data-ajax-url="<?php echo esc_attr( admin_url( 'admin-ajax.php' ) ); ?>" |
| 104 |
class="sellkit-control-file-uploader-input"/> |
| 105 |
<i class="fa fa-upload" aria-hidden="true"></i> |
| 106 |
<?php echo esc_html__( 'Upload File', 'sellkit' ); ?> |
| 107 |
</button> |
| 108 |
<div class="sellkit-control-file-uploader-progress"> |
| 109 |
<?php echo esc_html__( 'Uploading...', 'sellkit' ); ?> |
| 110 |
<span class="fa fa-spinner fa-spin"></span> |
| 111 |
</div> |
| 112 |
<div class="sellkit-control-file-uploader-value"> |
| 113 |
<span></span> |
| 114 |
<span class="fa fa-trash"></span> |
| 115 |
</div> |
| 116 |
</div> |
| 117 |
</div> |
| 118 |
</div> |
| 119 |
<# if ( data.description ) { #> |
| 120 |
<div class="elementor-control-field-description">{{{ data.description }}}</div> |
| 121 |
<# } #> |
| 122 |
<div class="sellkit-control-file-uploader-warning"> |
| 123 |
<div class="elementor-panel-alert elementor-panel-alert-danger"> |
| 124 |
<ul> |
| 125 |
<li class="sellkit-control-file-uploader-warning-size"> |
| 126 |
<?php esc_html_e( 'Maximum allowed file size is', 'sellkit' ); ?> |
| 127 |
<strong><?php echo esc_html( round( wp_max_upload_size() / ( 1024 * 1024 ), 2 ) ); ?> |
| 128 |
<?php |
| 129 |
/* translators: here "MB" means megabytes */ |
| 130 |
echo esc_html__( 'MB', 'sellkit' ); |
| 131 |
?> |
| 132 |
</strong> |
| 133 |
</li> |
| 134 |
</ul> |
| 135 |
</div> |
| 136 |
</div> |
| 137 |
<?php |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Handles uploading of the selected file by answering the AJAX call. |
| 142 |
* |
| 143 |
* @since 1.5.0 |
| 144 |
*/ |
| 145 |
public static function handle_file_upload() { |
| 146 |
check_ajax_referer( 'sellkit_fileuploader_editor', 'nonce' ); |
| 147 |
|
| 148 |
if ( ! function_exists( 'wp_handle_upload' ) ) { |
| 149 |
require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 150 |
} |
| 151 |
|
| 152 |
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput |
| 153 |
$movefile = wp_handle_upload( $_FILES['file'], |
| 154 |
[ |
| 155 |
'test_form' => false, |
| 156 |
'mimes' => get_allowed_mime_types(), |
| 157 |
'unique_filename_callback' => [ self::class, 'rename_uploading_file' ], |
| 158 |
] |
| 159 |
); |
| 160 |
|
| 161 |
if ( $movefile && ! isset( $movefile['error'] ) ) { |
| 162 |
wp_send_json_success( |
| 163 |
[ |
| 164 |
'path' => $movefile['file'], |
| 165 |
'name' => pathinfo( $movefile['file'], PATHINFO_BASENAME ), |
| 166 |
] |
| 167 |
); |
| 168 |
} |
| 169 |
|
| 170 |
wp_send_json_error( $movefile['error'] ); |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* Used in override propery 'unique_filename_callback' of wp_handle_upload function. |
| 175 |
* |
| 176 |
* @param string $dir File directory. |
| 177 |
* @param string $name File name. |
| 178 |
* @param string $ext File extension. |
| 179 |
* @return string File name. |
| 180 |
* |
| 181 |
* @since 1.5.0 |
| 182 |
* @access public |
| 183 |
* @static |
| 184 |
* @SuppressWarnings(PHPMD.UnusedFormalParameter) |
| 185 |
*/ |
| 186 |
public static function rename_uploading_file( $dir, $name, $ext ) { |
| 187 |
return str_replace( $ext, '', $name ) . '__' . uniqid() . $ext; |
| 188 |
} |
| 189 |
} |
| 190 |
|