'', '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' => $params['field_name'], 'editor_height' => 200)); ?>
/> ?
/>
/> ?
/>

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)); 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(); return $attch->ID; } $image_filename = wp_unique_filename($targetDir, $image_name); $image_filepath = $targetDir . '/' . $image_filename; $url = str_replace(" ", "%20", trim(pmxi_convert_encoding($img_url))); $is_base64_images_allowed = apply_filters("wp_all_import_is_base64_images_allowed", true, $url, false); if ( $file_type == 'images' and base64_encode(base64_decode($url)) == $url and $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))); 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], array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG, IMAGETYPE_BMP))) { $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 = $image_name; $image_filepath = $targetDir . '/' . $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; // valdate 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( ! ($image_info = apply_filters('pmxi_getimagesize', @getimagesize($image_filepath), $image_filepath)) or ! in_array($image_info[2], array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG, IMAGETYPE_BMP))) { $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); } else { $logger and call_user_func($logger, sprintf(__('- Image `%s` has been successfully found', 'wp_all_import_plugin'), $wpai_image_path)); $result = true; } } } } 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)); } $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( ($image_info = apply_filters('pmxi_getimagesize', @getimagesize($image_filepath), $image_filepath)) and in_array($image_info[2], array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG, IMAGETYPE_BMP))) { $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( ! ($image_info = apply_filters('pmxi_getimagesize', @getimagesize($image_filepath), $image_filepath)) or ! in_array($image_info[2], array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG, IMAGETYPE_BMP))) { $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); } else { $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 )) { $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 and $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 . '/' . $image_filename)); } else{ $logger and call_user_func($logger, sprintf(__('- Creating an attachment for file `%s`', 'wp_all_import_plugin'), $targetUrl . '/' . $image_filename)); } $attachment = array( 'post_mime_type' => ($file_type == 'images') ? image_type_to_mime_type($image_info[2]) : $wp_filetype['type'], 'guid' => $targetUrl . '/' . $image_filename, 'post_title' => $image_filename, 'post_content' => '', ); 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 . '/' . $image_filename)); return $attid; } } else return $result; } }