: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
public static function import_media(array $post, $blob = null, $generate = true) {
if (!empty($post['thumb'])) {
if (!current_user_can('upload_files')) {
$error = __('You aren`t allowed to upload file', 'themify');
$thumb = sanitize_text_field($post['thumb']);
$file_name = sanitize_file_name(wp_basename($thumb));
$type = wp_check_filetype( $file_name )['type'];
$sql = sprintf('`post_name` IN("%1$s","%1$s-1","%1$s-2","%1$s-3")',sanitize_title(pathinfo($file_name,PATHINFO_FILENAME)));
$query = $wpdb->get_row("SELECT ID,post_mime_type FROM {$wpdb->prefix}posts WHERE `post_type`='attachment' AND {$sql} AND `post_mime_type`='{$type}' LIMIT 1");
$isImage = 0 === strpos( $type, 'image/' );
$isVideo=$isImage===false && 0 === strpos( $type, 'video/' );
$attach_id = !empty($query) ? $query->ID : null;
if ($attach_id !== null) {
$duplicate = $isImage===true?wp_get_original_image_path($attach_id):get_attached_file( $attach_id );
if(!$duplicate && $isImage===true){
$duplicate=get_attached_file( $attach_id );
if ($duplicate && is_file($duplicate)) {
$size = (int) filesize($duplicate);
$mimeType = $query->post_mime_type;
unset($query, $sql,$type);
require_once ABSPATH . 'wp-admin/includes/image.php';
require_once ABSPATH . 'wp-admin/includes/file.php';
require_once ABSPATH . 'wp-admin/includes/media.php';
if ($blob['error'] > 0) {
$error = __('Uploading error', 'themify');
'tmp_name' => $blob['tmp_name'],
$tmp = download_url($thumb.'?polish=0');
$error = $tmp->get_error_message();
'size' => filesize($tmp),
$checked = wp_check_filetype_and_ext($file['tmp_name'], $file_name);
if (!empty($checked['type'])) {
$file['name'] = !empty($checked['proper_filename']) ? $checked['proper_filename'] : $file_name;
$isFileExist = isset($size) && $mimeType === $checked['type'] && abs($size - (int)$file['size']) < 15;
if ($isFileExist === true) {
$mbSize = (float) $size / MB_IN_BYTES;
if ($mbSize < 4) {//is below 4mb
$isFileExist = sha1_file($duplicate) === sha1_file($file['tmp_name']);
$maxCheck = 4 * MB_IN_BYTES; //check only the first 4mb
$i = (int) $maxCheck / $chunkSize;
$arr = self::compare_files($duplicate, $file['tmp_name'], $chunkSize);
foreach ($arr as $k => $v) {
if ($k === false || $v === false || $v !== $k) {
unset($maxCheck, $chunkSize, $i,$arr);
unset($duplicate, $mbSize);
$post_id = !empty($post['post_id']) ? (int) $post['post_id'] : 0;
$term_id = (!$post_id && !empty($post['term_id'])) ? (int) $post['term_id'] : 0;
if ($isFileExist === false) {
$check = function_exists('check_upload_size') ? check_upload_size($file) : array('error' => 0);
if ($check['error'] === 0) {
$attach_id = media_handle_sideload($file, $post_id);
$error = __('Uploading Error', 'themify');
} elseif (is_wp_error($attach_id)) {
$error = $attach_id->get_error_messages();
$error = $check['error'];
$error = __('Invalid file type', 'themify');
if (is_file($file['tmp_name'])) {
unlink($file['tmp_name']);
unset($file, $thumb,$tmp);
if ($error === '' && !empty($attach_id)) {
$attach_id=(int)$attach_id;
set_post_thumbnail($post_id, $attach_id);
} elseif ($term_id > 0) {
update_term_meta($term_id, 'thumbnail_id', $attach_id);
unset($post_id, $term_id);
$imageMeta = wp_get_attachment_metadata($attach_id);
if ( empty($imageMeta['file'])) {
$data = wp_get_attachment_image_src($attach_id, 'full');
$size = themify_get_image_size($src);
$imageSizes['_orig_'] = array('file' => $src, $w, 'height' => $h);
$imageSizes = !empty($imageMeta['sizes']) ? $imageMeta['sizes'] : array();
$imageSizes['_orig_'] = array('file' => basename($imageMeta['file']), 'width' => $imageMeta['width'], 'height' => $imageMeta['height']);
$folder = '/' . dirname($imageMeta['file']) . '/';
$baseUrl = themify_upload_dir('baseurl') . $folder;
if ($generate === true) {
foreach ($imageSizes as $v) {
themify_get_image_size($baseUrl . $v['file']);
themify_create_webp($baseUrl . $v['file']);
$src = $baseUrl . $imageSizes['_orig_']['file'];
$img = '<img src="' . $src . '" class="wp-image-' . $attach_id . '"';
if (!empty($imageSizes['_orig_']['width'])) {
$img .= ' width="' . $imageSizes['_orig_']['width'] . '" height="' . $imageSizes['_orig_']['height'] . '"';
$img = function_exists('wp_filter_content_tags') ? wp_filter_content_tags($img) : wp_make_content_images_responsive($img);
$src=wp_get_attachment_url($attach_id);
themify_get_video_size($src);
return array('html' => $img, 'id' => $attach_id, 'src' => $src);
Themify_Import_Helper::init();