File: /medikors/www/eng/wp-content/plugins/happy-elementor-addons-pro/classes/class.retainer.php
<?php
namespace Happy_Addons_Pro;
defined('ABSPATH') || die();
class Asset_Retainer {
public static function init() {
add_action( 'wp_ajax_ha-retainer', [ __CLASS__, 'init_retainer' ] );
}
public static function init_retainer() {
if ( wp_verify_nonce( $_POST['identifier'], 'ha-retainer' ) ) {
add_filter( 'upload_mimes', [ __CLASS__, 'allow_svg_mime_type' ] );
$new_ids = [];
foreach ( $_POST['data'] as $image ) {
$old_id = $image['id'];
$file = $image['url'];
$file = esc_url( $file );
$file_parts = pathinfo( $file );
if ( in_array( $file_parts['extension'], ['jpg', 'jpeg', 'svg', 'gif', 'png'] ) ) {
$new_id = self::add_attachment_from_url( $file );
$new_ids[] = [
'old' => $old_id,
'new' => $new_id
];
}
}
echo( json_encode( $new_ids ) );
}
die();
}
public static function allow_svg_mime_type( $mimes ) {
$mimes['svg'] = 'image/svg+xml';
$mimes['svgz'] = 'image/svg+xml';
return $mimes;
}
private static function add_attachment_from_url( $file ) {
$file = str_replace( 'https://', 'http://', $file );
$file_hash = 'ha-asset-' . md5( $file );
if ( ! get_option( $file_hash ) ) {
$file_array = [];
$file_array['name'] = wp_basename( $file );
$file_array['tmp_name'] = download_url( $file );
// If error storing temporarily, return the error.
if ( ! is_wp_error( $file_array['tmp_name'] ) ) {
$id = media_handle_sideload( $file_array, 0, null );
update_option( $file_hash, trim( $id ) );
return $id;
}
} else {
return get_option( $file_hash );
}
}
}
Asset_Retainer::init();