| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; // Exit if accessed directly! |
| 4 |
} |
| 5 |
|
| 6 |
if ( ! class_exists( 'WPSC_GS_File_Attachments' ) ) : |
| 7 |
|
| 8 |
final class WPSC_GS_File_Attachments { |
| 9 |
|
| 10 |
/** |
| 11 |
* Initialize this class |
| 12 |
* |
| 13 |
* @return void |
| 14 |
*/ |
| 15 |
public static function init() { |
| 16 |
|
| 17 |
// User interface. |
| 18 |
add_action( 'wp_ajax_wpsc_get_gs_file_attachments', array( __CLASS__, 'load_settings_ui' ) ); |
| 19 |
add_action( 'wp_ajax_wpsc_set_gs_file_attachments', array( __CLASS__, 'save_settings' ) ); |
| 20 |
add_action( 'wp_ajax_wpsc_reset_gs_file_attachments', array( __CLASS__, 'reset_settings' ) ); |
| 21 |
|
| 22 |
// File upload js function. |
| 23 |
add_action( 'wpsc_js_ticket_form_functions', array( __CLASS__, 'js_upload_function' ) ); |
| 24 |
add_action( 'wpsc_js_my_profile_functions', array( __CLASS__, 'js_upload_function' ) ); |
| 25 |
add_action( 'wpsc_js_it_functions', array( __CLASS__, 'js_upload_function' ) ); |
| 26 |
add_action( 'wpsc_js_customer_list_functions', array( __CLASS__, 'js_upload_function' ) ); |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Reset settings |
| 31 |
* |
| 32 |
* @return void |
| 33 |
*/ |
| 34 |
public static function reset() { |
| 35 |
|
| 36 |
$allowed_extensions = 'jpg, jpeg, png, gif, pdf, doc, docx, ppt, pptx, pps, ppsx, odt, xls, xlsx, mp3, m4a, ogg, wav, mp4, m4v, mov, wmv, avi, mpg, ogv, 3gp, 3g2, zip, eml'; |
| 37 |
$file_attachments = apply_filters( |
| 38 |
'wpsc_gs_file_attachments', |
| 39 |
array( |
| 40 |
'attachments-max-filesize' => 20, |
| 41 |
'allowed-file-extensions' => $allowed_extensions, |
| 42 |
'allowed-file-ext-mimes' => self::get_mime_types_by_extensions( $allowed_extensions ), |
| 43 |
'mime-exceptions' => array(), |
| 44 |
'image-download-behaviour' => 'open-browser', |
| 45 |
) |
| 46 |
); |
| 47 |
update_option( 'wpsc-gs-file-attachments', $file_attachments ); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Settings user interface |
| 52 |
* |
| 53 |
* @return void |
| 54 |
*/ |
| 55 |
public static function load_settings_ui() { |
| 56 |
|
| 57 |
if ( ! WPSC_Functions::is_site_admin() ) { |
| 58 |
wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 ); |
| 59 |
} |
| 60 |
$settings = get_option( 'wpsc-gs-file-attachments', array() );?> |
| 61 |
|
| 62 |
<form action="#" onsubmit="return false;" class="wpsc-frm-gs-fa"> |
| 63 |
<div class="wpsc-dock-container"> |
| 64 |
<?php |
| 65 |
printf( |
| 66 |
/* translators: Click here to see the documentation */ |
| 67 |
esc_attr__( '%s to see the documentation!', 'supportcandy' ), |
| 68 |
'<a href="https://supportcandy.net/docs/file-attachments/" target="_blank">' . esc_attr__( 'Click here', 'supportcandy' ) . '</a>' |
| 69 |
); |
| 70 |
?> |
| 71 |
</div> |
| 72 |
<div class="wpsc-input-group"> |
| 73 |
<div class="label-container"> |
| 74 |
<label for=""><?php esc_attr_e( 'Attachment max file size(mb)', 'supportcandy' ); ?></label> |
| 75 |
</div> |
| 76 |
<input id="wpsc-attachfile" type="text" name="attachments-max-filesize" value="<?php echo intval( $settings['attachments-max-filesize'] ); ?>"> |
| 77 |
</div> |
| 78 |
<div class="wpsc-input-group"> |
| 79 |
<div class="label-container"> |
| 80 |
<label for=""><?php esc_attr_e( 'Allowed file extensions(comma separated)', 'supportcandy' ); ?></label> |
| 81 |
</div> |
| 82 |
<input id="wpsc-allowed-file" type="text" name="allowed-file-extensions" value="<?php echo esc_attr( $settings['allowed-file-extensions'] ); ?>"> |
| 83 |
</div> |
| 84 |
<div class="wpsc-input-group"> |
| 85 |
<div class="label-container"> |
| 86 |
<label for=""><?php esc_attr_e( 'MIME exceptions (One per line)', 'supportcandy' ); ?></label> |
| 87 |
</div> |
| 88 |
<small style="padding-bottom: 5px;"><?php esc_attr_e( 'Add files in format, extension: MIME type. For example, png: image/png.', 'supportcandy' ); ?></small> |
| 89 |
<?php $mime_exceptions = $settings['mime-exceptions'] ? implode( PHP_EOL, $settings['mime-exceptions'] ) : ''; ?> |
| 90 |
<textarea name="wpsc-mime-exceptions" rows="5"><?php echo esc_attr( $mime_exceptions ); ?></textarea> |
| 91 |
</div> |
| 92 |
<div class="wpsc-input-group"> |
| 93 |
<div class="label-container"> |
| 94 |
<label for=""><?php esc_attr_e( 'Download behaviour', 'supportcandy' ); ?></label> |
| 95 |
</div> |
| 96 |
<select id="wpsc-db" name="image-download-behaviour"> |
| 97 |
<option <?php selected( $settings['image-download-behaviour'], 'open-browser' ); ?> value="open-browser"><?php esc_attr_e( 'Try opening in browser', 'supportcandy' ); ?></option> |
| 98 |
<option <?php selected( $settings['image-download-behaviour'], 'download' ); ?> value="download"><?php esc_attr_e( 'Download', 'supportcandy' ); ?></option> |
| 99 |
</select> |
| 100 |
</div> |
| 101 |
<?php do_action( 'wpsc_gs_file_attachments' ); ?> |
| 102 |
<input type="hidden" name="action" value="wpsc_set_gs_file_attachments"> |
| 103 |
<input type="hidden" name="_ajax_nonce" value="<?php echo esc_attr( wp_create_nonce( 'wpsc_set_gs_file_attachments' ) ); ?>"> |
| 104 |
</form> |
| 105 |
<div class="setting-footer-actions"> |
| 106 |
<button |
| 107 |
class="wpsc-button normal primary margin-right" |
| 108 |
onclick="wpsc_set_gs_file_attachments(this);"> |
| 109 |
<?php esc_attr_e( 'Submit', 'supportcandy' ); ?></button> |
| 110 |
<button |
| 111 |
class="wpsc-button normal secondary" |
| 112 |
onclick="wpsc_reset_gs_file_attachments(this, '<?php echo esc_attr( wp_create_nonce( 'wpsc_reset_gs_file_attachments' ) ); ?>');"> |
| 113 |
<?php esc_attr_e( 'Reset default', 'supportcandy' ); ?></button> |
| 114 |
</div> |
| 115 |
<?php |
| 116 |
wp_die(); |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Save settings |
| 121 |
* |
| 122 |
* @return void |
| 123 |
*/ |
| 124 |
public static function save_settings() { |
| 125 |
|
| 126 |
if ( check_ajax_referer( 'wpsc_set_gs_file_attachments', '_ajax_nonce', false ) != 1 ) { |
| 127 |
wp_send_json_error( 'Unauthorized request!', 401 ); |
| 128 |
} |
| 129 |
|
| 130 |
if ( ! WPSC_Functions::is_site_admin() ) { |
| 131 |
wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 ); |
| 132 |
} |
| 133 |
|
| 134 |
$allowed_extensions = isset( $_POST['allowed-file-extensions'] ) ? sanitize_text_field( wp_unslash( $_POST['allowed-file-extensions'] ) ) : ''; |
| 135 |
|
| 136 |
$mime_exceptions = isset( $_POST['wpsc-mime-exceptions'] ) ? sanitize_textarea_field( wp_unslash( $_POST['wpsc-mime-exceptions'] ) ) : ''; |
| 137 |
$mime_exceptions = array_filter( array_map( 'sanitize_text_field', explode( PHP_EOL, $mime_exceptions ) ) ); |
| 138 |
|
| 139 |
$file_attachments = apply_filters( |
| 140 |
'wpsc_set_gs_file_attachments', |
| 141 |
array( |
| 142 |
'attachments-max-filesize' => isset( $_POST['attachments-max-filesize'] ) ? intval( $_POST['attachments-max-filesize'] ) : 20, |
| 143 |
'allowed-file-extensions' => $allowed_extensions, |
| 144 |
'allowed-file-ext-mimes' => self::get_mime_types_by_extensions( $allowed_extensions ), |
| 145 |
'mime-exceptions' => $mime_exceptions, |
| 146 |
'image-download-behaviour' => isset( $_POST['image-download-behaviour'] ) ? sanitize_text_field( wp_unslash( $_POST['image-download-behaviour'] ) ) : 'open-browser', |
| 147 |
) |
| 148 |
); |
| 149 |
update_option( 'wpsc-gs-file-attachments', $file_attachments ); |
| 150 |
wp_die(); |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Reset settings to default |
| 155 |
* |
| 156 |
* @return void |
| 157 |
*/ |
| 158 |
public static function reset_settings() { |
| 159 |
|
| 160 |
if ( check_ajax_referer( 'wpsc_reset_gs_file_attachments', '_ajax_nonce', false ) != 1 ) { |
| 161 |
wp_send_json_error( 'Unauthorized request!', 401 ); |
| 162 |
} |
| 163 |
|
| 164 |
if ( ! WPSC_Functions::is_site_admin() ) { |
| 165 |
wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 ); |
| 166 |
} |
| 167 |
self::reset(); |
| 168 |
wp_die(); |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Print file upload JS function with reCaptcha compatibility |
| 173 |
* |
| 174 |
* @return void |
| 175 |
*/ |
| 176 |
public static function js_upload_function() { |
| 177 |
|
| 178 |
$recaptcha = get_option( 'wpsc-recaptcha-settings' ); |
| 179 |
?> |
| 180 |
|
| 181 |
/** |
| 182 |
* Upload given file |
| 183 |
*/ |
| 184 |
function wpsc_file_upload(fileAttachment, uniqueId, slug, single = false) { |
| 185 |
|
| 186 |
var file_name_split = fileAttachment.name.split('.'); |
| 187 |
var file_extension = file_name_split[file_name_split.length-1]; |
| 188 |
file_extension = file_extension.toLowerCase(); |
| 189 |
<?php |
| 190 |
$file_settings = get_option( 'wpsc-gs-file-attachments' ); |
| 191 |
?> |
| 192 |
if (jQuery.inArray(file_extension, supportcandy.allowed_file_extensions) < 0) { |
| 193 |
alert("<?php esc_attr_e( 'Attached file type not allowed!', 'supportcandy' ); ?>"); |
| 194 |
return; |
| 195 |
} |
| 196 |
|
| 197 |
var current_filesize = fileAttachment.size/1000000; |
| 198 |
if (current_filesize > <?php echo intval( $file_settings['attachments-max-filesize'] ); ?>) { |
| 199 |
<?php /* translators: %1$s: attachment max file size in MB */ ?> |
| 200 |
alert("<?php printf( esc_attr__( 'File size exceeds allowed limit (%1$s MB)!', 'supportcandy' ), intval( $file_settings['attachments-max-filesize'] ) ); ?>"); |
| 201 |
return; |
| 202 |
} |
| 203 |
|
| 204 |
var htmlSnippet = jQuery('.wpsc-page-snippets .wpsc-editor-attachment').first().prop('outerHTML'); |
| 205 |
var attachmentContainer = jQuery('.wpsc-editor-attachment-container.' + uniqueId).append(htmlSnippet); |
| 206 |
var attachment = attachmentContainer.children().last(); |
| 207 |
attachment.find('.attachment-label').text(fileAttachment.name); |
| 208 |
attachment.find('.attachment-waiting').circleProgress({ |
| 209 |
startAngle: -Math.PI / 4 * 3, |
| 210 |
size: 20, |
| 211 |
value: 0.0, |
| 212 |
lineCap: 'round', |
| 213 |
fill: { gradient: ['#ff1e41', '#ff5f43'] } |
| 214 |
}); |
| 215 |
attachment.find('.attachment-remove').data({ single, uniqueid: uniqueId }); |
| 216 |
|
| 217 |
// Append form data |
| 218 |
var dataform = new FormData(); |
| 219 |
dataform.append('wpscFileAttachment', fileAttachment); |
| 220 |
dataform.append('action', 'wpsc_file_upload'); |
| 221 |
dataform.append('_ajax_nonce', '<?php echo esc_attr( wp_create_nonce( 'wpsc_file_upload' ) ); ?>'); |
| 222 |
|
| 223 |
<?php |
| 224 |
if ( $recaptcha['captcha-provider'] === 'google-recaptcha' && $recaptcha['recaptcha-version'] == 3 && $recaptcha['recaptcha-site-key'] && $recaptcha['recaptcha-secret-key'] ) { |
| 225 |
?> |
| 226 |
grecaptcha.ready(function() { |
| 227 |
grecaptcha.execute('<?php echo esc_attr( $recaptcha['recaptcha-site-key'] ); ?>', {action: 'file_upload'}).then(function(token) { |
| 228 |
dataform.append('g-recaptcha-response', token); |
| 229 |
jQuery.ajax({ |
| 230 |
url: supportcandy.ajax_url, |
| 231 |
type: 'POST', |
| 232 |
data: dataform, |
| 233 |
xhr: function () { |
| 234 |
var myXhr = jQuery.ajaxSettings.xhr(); |
| 235 |
if (myXhr.upload) { |
| 236 |
myXhr.upload.addEventListener('progress', wpscAttachmentUploadProgress, false); |
| 237 |
myXhr.upload.attachment = attachment; |
| 238 |
myXhr.attachment = attachment; |
| 239 |
myXhr.uniqueId = uniqueId; |
| 240 |
myXhr.single = single; |
| 241 |
} |
| 242 |
return myXhr; |
| 243 |
}, |
| 244 |
processData: false, |
| 245 |
contentType: false, |
| 246 |
error: function (data) { |
| 247 |
|
| 248 |
// Upload not accepted |
| 249 |
attachment.removeClass('upload-waiting'); |
| 250 |
attachment.addClass('upload-error'); |
| 251 |
}, |
| 252 |
success: function (response, textStatus, xhr) { |
| 253 |
if (xhr.status === 200) { |
| 254 |
// Upload created |
| 255 |
var attachmentInput = '<input type="hidden" name="' + slug + '[]" value="' + response.id + '"/>' + |
| 256 |
'<input type="hidden" name="wpsc_attachment_token[' + response.id + ']" value="' + response.token + '"/>'; |
| 257 |
attachment.append(attachmentInput); |
| 258 |
attachment.removeClass('upload-waiting'); |
| 259 |
attachment.addClass('upload-success'); |
| 260 |
|
| 261 |
if(single) { |
| 262 |
jQuery( 'input.' + uniqueId ).hide(); |
| 263 |
} |
| 264 |
} else { |
| 265 |
|
| 266 |
// Upload not accepted |
| 267 |
attachment.removeClass('upload-waiting'); |
| 268 |
attachment.addClass('upload-error'); |
| 269 |
} |
| 270 |
} |
| 271 |
}); |
| 272 |
}); |
| 273 |
}); |
| 274 |
<?php |
| 275 |
|
| 276 |
} else { |
| 277 |
?> |
| 278 |
|
| 279 |
jQuery.ajax({ |
| 280 |
url: supportcandy.ajax_url, |
| 281 |
type: 'POST', |
| 282 |
data: dataform, |
| 283 |
xhr: function () { |
| 284 |
var myXhr = jQuery.ajaxSettings.xhr(); |
| 285 |
if (myXhr.upload) { |
| 286 |
myXhr.upload.addEventListener('progress', wpscAttachmentUploadProgress, false); |
| 287 |
myXhr.upload.attachment = attachment; |
| 288 |
myXhr.attachment = attachment; |
| 289 |
myXhr.uniqueId = uniqueId; |
| 290 |
myXhr.single = single; |
| 291 |
} |
| 292 |
return myXhr; |
| 293 |
}, |
| 294 |
processData: false, |
| 295 |
contentType: false, |
| 296 |
error: function (data) { |
| 297 |
|
| 298 |
// Upload not accepted |
| 299 |
attachment.removeClass('upload-waiting'); |
| 300 |
attachment.addClass('upload-error'); |
| 301 |
}, |
| 302 |
success: function (response, textStatus, xhr) { |
| 303 |
if (xhr.status === 200) { |
| 304 |
// Upload created |
| 305 |
var attachmentInput = '<input type="hidden" name="' + slug + '[]" value="' + response.id + '"/>' + |
| 306 |
'<input type="hidden" name="wpsc_attachment_token[' + response.id + ']" value="' + response.token + '"/>'; |
| 307 |
attachment.append(attachmentInput); |
| 308 |
attachment.removeClass('upload-waiting'); |
| 309 |
attachment.addClass('upload-success'); |
| 310 |
|
| 311 |
if(single) { |
| 312 |
jQuery( 'input.' + uniqueId ).hide(); |
| 313 |
} |
| 314 |
} else { |
| 315 |
|
| 316 |
// Upload not accepted |
| 317 |
attachment.removeClass('upload-waiting'); |
| 318 |
attachment.addClass('upload-error'); |
| 319 |
} |
| 320 |
} |
| 321 |
}); |
| 322 |
<?php |
| 323 |
} |
| 324 |
?> |
| 325 |
|
| 326 |
} |
| 327 |
<?php |
| 328 |
} |
| 329 |
|
| 330 |
/** |
| 331 |
* Get MIME type extensions |
| 332 |
* |
| 333 |
* @param string $extensions - extension strings. |
| 334 |
* @return array |
| 335 |
*/ |
| 336 |
public static function get_mime_types_by_extensions( $extensions ) { |
| 337 |
|
| 338 |
require_once WPSC_ABSPATH . 'includes/class-wpsc-mime-types.php'; |
| 339 |
|
| 340 |
$exts = $extensions ? explode( ',', $extensions ) : array(); |
| 341 |
$exts = array_map( 'trim', $exts ); |
| 342 |
$mimes = array(); |
| 343 |
foreach ( $exts as $key => $value ) { |
| 344 |
$mime = WPSC_MIME_TYPES::get_mime_type( $value ); |
| 345 |
if ( $mime ) { |
| 346 |
$mimes[ $value ] = $mime; |
| 347 |
} |
| 348 |
} |
| 349 |
return $mimes; |
| 350 |
} |
| 351 |
} |
| 352 |
endif; |
| 353 |
|
| 354 |
WPSC_GS_File_Attachments::init(); |
| 355 |
|
| 356 |
|
| 357 |
|