'', 'enum_values' => array(), 'mapping' => false, 'field_key' => '', 'mapping_rules' => array(), 'xpath' => '', 'field_name' => '', 'field_value' => '', 'addon_prefix' => '', 'sub_fields' => array(), 'is_main_field' => false, 'in_the_bottom' => false ); ob_start(); if ($label != "" and $field_type != "accordion"){ ?> ?
$value): ?>
/> $p) { if ( ! is_array($p)){ ?> ?
/>
true, 'media_buttons' => false, 'textarea_name' => esc_attr($params['field_name']), 'editor_height' => 200)); ?>
/> ?
/>
/> ?
/>

id; } $uploads = wp_upload_dir(); $uploads = apply_filters('wp_all_import_images_uploads_dir', $uploads, $articleData, $current_xml_node, $import_id); $targetDir = $uploads['path']; $targetUrl = $uploads['url']; $download_image = true; $result = false; $wp_filetype = false; $attch = false; if ( $check_existing ) { // Trying to find existing image in hash table. if ("yes" == $download_images ) { $logger and call_user_func($logger, sprintf(__('- Searching for existing image `%s` by URL...', 'wp_all_import_plugin'), rawurldecode($img_url))); $imageList = new PMXI_Image_List(); $attch = $imageList->getExistingImageByUrl($img_url); if ($attch) { $logger and call_user_func($logger, sprintf(__('Existing image was found by URL `%s`...', 'wp_all_import_plugin'), $img_url)); // Attach media to current post if it's currently unattached. if (empty($attch->post_parent)) { wp_update_post( array( 'ID' => $attch->ID, 'post_parent' => $pid ) ); } return $attch->ID; } } if (empty($attch)) { $logger and call_user_func($logger, sprintf(__('- Searching for existing image `%s` by `_wp_attached_file` `%s`...', 'wp_all_import_plugin'), $img_url, $image_name)); $attch = wp_all_import_get_image_from_gallery($image_name, $targetDir, $file_type); } if (!empty($attch)) { $logger and call_user_func($logger, sprintf(__('- Existing image was found by `_wp_attached_file` ...', 'wp_all_import_plugin'), $img_url)); $imageRecord = new PMXI_Image_Record(); $imageRecord->getBy(array( 'attachment_id' => $attch->ID )); $imageRecord->isEmpty() and $imageRecord->set(array( 'attachment_id' => $attch->ID, 'image_url' => $img_url, 'image_filename' => $image_name ))->insert(); // Attach media to current post if it's currently unattached. if (empty($attch->post_parent)) { wp_update_post( array( 'ID' => $attch->ID, 'post_parent' => $pid ) ); } return $attch->ID; } } $image_filename = wp_unique_filename($targetDir, $image_name); $image_filepath = $targetDir . '/' . $image_filename; $url = str_replace(" ", "%20", trim($img_url)); $is_base64_images_allowed = apply_filters("wp_all_import_is_base64_images_allowed", true, $url, false); if ( $file_type == 'images' && base64_encode(base64_decode($url)) == $url && $is_base64_images_allowed ) { $image_name = md5($url) . '.jpg'; // Search existing attachment. $attch = wp_all_import_get_image_from_gallery($image_name, $targetDir, $file_type); if (empty($attch)) { $logger and call_user_func($logger, sprintf(__('- WARNING: Image %s not found in media gallery.', 'wp_all_import_plugin'), trim($image_name))); } else { $logger and call_user_func($logger, sprintf(__('- Using existing image `%s`...', 'wp_all_import_plugin'), trim($image_name))); // Attach media to current post if it's currently unattached. if (empty($attch->post_parent)) { wp_update_post( array( 'ID' => $attch->ID, 'post_parent' => $pid ) ); } return $attch->ID; } if ("yes" == $download_images) { $img = @imagecreatefromstring(base64_decode($url)); if ($img) { $image_filename = $image_name; $logger and call_user_func($logger, __('- found base64_encoded image', 'wp_all_import_plugin')); $image_filepath = $targetDir . '/' . $image_filename; imagejpeg($img, $image_filepath); if ( ! ($image_info = apply_filters('pmxi_getimagesize', @getimagesize($image_filepath), $image_filepath)) or ! in_array($image_info[2], wp_all_import_supported_image_types())) { $logger and call_user_func($logger, sprintf(__('- WARNING: File %s is not a valid image and cannot be set as featured one', 'wp_all_import_plugin'), $image_filepath)); } else { $result = true; $download_image = false; } } } } // Do not download images. if ( "yes" != $download_images ) { $image_filename = wp_unique_filename($targetDir, basename($image_name)); $image_filepath = $targetDir . '/' . basename($image_filename); $wpai_uploads = $uploads['basedir'] . DIRECTORY_SEPARATOR . PMXI_Plugin::FILES_DIRECTORY . DIRECTORY_SEPARATOR; $wpai_image_path = $wpai_uploads . str_replace('%20', ' ', $url); $logger and call_user_func($logger, sprintf(__('- Searching for existing image `%s` in `%s` folder', 'wp_all_import_plugin'), $wpai_image_path, $wpai_uploads)); if ( @file_exists($wpai_image_path) and @copy( $wpai_image_path, $image_filepath )){ $download_image = false; // Validate import attachments. if ($file_type == 'files') { if ( ! $wp_filetype = wp_check_filetype(basename($image_filepath), null )) { $logger and call_user_func($logger, sprintf(__('- WARNING: Can\'t detect attachment file type %s', 'wp_all_import_plugin'), trim($image_filepath))); @unlink($image_filepath); } else { $result = true; $logger and call_user_func($logger, sprintf(__('- File `%s` has been successfully found', 'wp_all_import_plugin'), $wpai_image_path)); } } // Validate import images. elseif ($file_type == 'images') { if ( preg_match('%\W(svg)$%i', wp_all_import_basename($image_filepath)) or $image_info = apply_filters('pmxi_getimagesize', @getimagesize($image_filepath), $image_filepath) and in_array($image_info[2], wp_all_import_supported_image_types())) { $logger and call_user_func($logger, sprintf(__('- Image `%s` has been successfully found', 'wp_all_import_plugin'), $wpai_image_path)); $result = true; } else { $logger and call_user_func($logger, sprintf(__('- WARNING: File %s is not a valid image and cannot be set as featured one', 'wp_all_import_plugin'), $image_filepath)); @unlink($image_filepath); } } } } if ( $download_image ) { if ($file_type == 'images') { $logger and call_user_func($logger, sprintf(__('- Downloading image from `%s`', 'wp_all_import_plugin'), $url)); } elseif ($file_type == 'files') { $logger and call_user_func($logger, sprintf(__('- Downloading file from `%s`', 'wp_all_import_plugin'), $url)); } if ( ! preg_match('%^(http|ftp)s?://%i', $url) ) { $logger and call_user_func($logger, sprintf(__('- WARNING: File %s cannot be saved locally as %s', 'wp_all_import_plugin'), $url, $image_filepath)); return false; } $request = get_file_curl($url, $image_filepath); if ( (is_wp_error($request) or $request === false) and ! @file_put_contents($image_filepath, @file_get_contents($url))) { @unlink($image_filepath); // delete file since failed upload may result in empty file created } else { if ($file_type == 'images') { if ( preg_match('%\W(svg)$%i', wp_all_import_basename($image_filepath)) or $image_info = apply_filters('pmxi_getimagesize', @getimagesize($image_filepath), $image_filepath) and in_array($image_info[2], wp_all_import_supported_image_types())) { $result = true; $logger and call_user_func($logger, sprintf(__('- Image `%s` has been successfully downloaded', 'wp_all_import_plugin'), $url)); } } elseif ($file_type == 'files'){ if ( $wp_filetype = wp_check_filetype(basename($image_filepath), null )) { $result = true; $logger and call_user_func($logger, sprintf(__('- File `%s` has been successfully downloaded', 'wp_all_import_plugin'), $url)); } } } if ( ! $result ) { $request = get_file_curl($url, $image_filepath); if ( (is_wp_error($request) or $request === false) and ! @file_put_contents($image_filepath, @file_get_contents($url))) { $logger and call_user_func($logger, sprintf(__('- WARNING: File %s cannot be saved locally as %s', 'wp_all_import_plugin'), $url, $image_filepath)); @unlink($image_filepath); // delete file since failed upload may result in empty file created } else { if ($file_type == 'images') { if ( preg_match('%\W(svg)$%i', wp_all_import_basename($image_filepath)) or $image_info = apply_filters('pmxi_getimagesize', @getimagesize($image_filepath), $image_filepath) and in_array($image_info[2], wp_all_import_supported_image_types())) { $result = true; $logger and call_user_func($logger, sprintf(__('- Image `%s` has been successfully downloaded', 'wp_all_import_plugin'), $url)); } else { $logger and call_user_func($logger, sprintf(__('- WARNING: File %s is not a valid image and cannot be set as featured one', 'wp_all_import_plugin'), $url)); @unlink($image_filepath); } } elseif ($file_type == 'files') { if ( ! $wp_filetype = wp_check_filetype(basename($image_filepath), null )) { $logger and call_user_func($logger, sprintf(__('- WARNING: Can\'t detect attachment file type %s', 'wp_all_import_plugin'), trim($url))); @unlink($image_filepath); } else { $result = true; $logger and call_user_func($logger, sprintf(__('- File `%s` has been successfully found', 'wp_all_import_plugin'), $url)); } } } } } if ( $create_image && $result ) { // you must first include the image.php file // for the function wp_generate_attachment_metadata() to work require_once(ABSPATH . 'wp-admin/includes/image.php'); if ($file_type == 'images') { $logger and call_user_func($logger, sprintf(__('- Creating an attachment for image `%s`', 'wp_all_import_plugin'), $targetUrl . '/' . basename($image_filename))); } else { $logger and call_user_func($logger, sprintf(__('- Creating an attachment for file `%s`', 'wp_all_import_plugin'), $targetUrl . '/' . basename($image_filename))); } $file_mime_type = empty($wp_filetype['type']) ? '' : $wp_filetype['type']; if ($file_type == 'images' && !empty($image_info)) { $file_mime_type = image_type_to_mime_type($image_info[2]); } $file_mime_type = apply_filters('wp_all_import_image_mime_type', $file_mime_type, $image_filepath); $attachment = [ 'post_mime_type' => $file_mime_type, 'guid' => $targetUrl . '/' . basename($image_filename), 'post_title' => basename($image_filename), 'post_content' => '', ]; if (!empty($articleData['post_author'])) { $attachment['post_author'] = $articleData['post_author']; } if ($file_type == 'images' and ($image_meta = wp_read_image_metadata($image_filepath))) { if (trim($image_meta['title']) && ! is_numeric(sanitize_title($image_meta['title']))) $attachment['post_title'] = $image_meta['title']; if (trim($image_meta['caption'])) $attachment['post_content'] = $image_meta['caption']; } remove_all_actions('add_attachment'); $attid = wp_insert_attachment($attachment, $image_filepath, $pid); if (is_wp_error($attid)) { $logger and call_user_func($logger, __('- WARNING', 'wp_all_import_plugin') . ': ' . $attid->get_error_message()); return false; } else { /** Fires once an attachment has been added. */ do_action( 'wp_all_import_add_attachment', $attid ); wp_update_attachment_metadata($attid, wp_generate_attachment_metadata($attid, $image_filepath)); $imageRecord = new PMXI_Image_Record(); $imageRecord->getBy(array( 'attachment_id' => $attid )); $imageRecord->isEmpty() and $imageRecord->set(array( 'attachment_id' => $attid, 'image_url' => $img_url, 'image_filename' => $image_filename ))->insert(); $logger and call_user_func($logger, sprintf(__('- Attachment has been successfully created for image `%s`', 'wp_all_import_plugin'), $targetUrl . '/' . basename($image_filename))); return $attid; } } return $result; } }