PluginProbe ʕ •ᴥ•ʔ
WP All Import – Drag & Drop Import for CSV, XML, Excel & Google Sheets / 3.3.2
WP All Import – Drag & Drop Import for CSV, XML, Excel & Google Sheets v3.3.2
4.1.1 3.9.5 3.9.6 4.0.0 4.0.1 4.1.0 trunk 2.12 2.13 2.14 3.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.2.0 3.2.1 3.2.2 3.2.3 3.2.4 3.2.5 3.2.6 3.2.7 3.2.8 3.2.9 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 3.3.5 3.3.6 3.3.7 3.3.8 3.3.9 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.6 3.4.7 3.4.8 3.4.9 3.5.0 3.5.1 3.5.2 3.5.3 3.5.4 3.5.5 3.5.6 3.5.7 3.5.8 3.5.9 3.6.0 3.6.1 3.6.2 3.6.3 3.6.4 3.6.5 3.6.6 3.6.7 3.6.8 3.6.9 3.7.0 3.7.1 3.7.2 3.7.3 3.7.3-beta-1.0 3.7.4 3.7.4-beta-1.0 3.7.5 3.7.6 3.7.7 3.7.8 3.7.9 3.8.0 3.9.0 3.9.1 3.9.2 3.9.3 3.9.4
wp-all-import / classes / api.php
wp-all-import / classes Last commit date
PHPExcel 10 years ago PHPExcel.php 10 years ago api.php 10 years ago arraytoxml.php 10 years ago chunk.php 10 years ago config.php 10 years ago download.php 10 years ago handler.php 10 years ago helper.php 10 years ago input.php 10 years ago render.php 10 years ago session.php 10 years ago upload.php 10 years ago
api.php
526 lines
1 <?php
2 /**
3 * API
4 *
5 * Generates html code for fields
6 *
7 *
8 * @package default
9 * @author Max Tsiplyakov
10 */
11 class PMXI_API
12 {
13 /**
14 * Function for generating html code for fields
15 * @param string $field_type simple, enum or textarea
16 * @param string $label field label
17 * @param array $params contains field params like tooltip, enum_values, mapping, field_name, field_value
18 */
19 public static function add_field( $field_type = 'simple', $label = '', $params = array()){
20
21 $params += array(
22 'tooltip' => '',
23 'enum_values' => array(),
24 'mapping' => false,
25 'field_key' => '',
26 'mapping_rules' => array(),
27 'xpath' => '',
28 'field_name' => '',
29 'field_value' => '',
30 'addon_prefix' => '',
31 'sub_fields' => array(),
32 'is_main_field' => false,
33 'in_the_bottom' => false
34 );
35
36 ob_start();
37 if ($label != "" and $field_type != "accordion"){
38 ?>
39 <label for="<?php echo sanitize_title($params['field_name']); ?>"><?php echo $label;?></label>
40 <?php
41 }
42 if ( ! empty($params['tooltip'])){
43 ?>
44 <a href="#help" class="wpallimport-help" title="<?php echo esc_attr($params['tooltip']); ?>" style="position: relative; top: -2px;">?</a>
45 <?php
46 }
47
48 if ( ! $params['in_the_bottom'] ){
49 ?>
50 <div class="input">
51 <?php
52 }
53
54 switch ($field_type){
55
56 case 'simple':
57 ?>
58 <input type="text" name="<?php echo $params['field_name']; ?>" id="<?php echo sanitize_title($params['field_name']); ?>" value="<?php echo esc_attr($params['field_value']); ?>" style="width:100%;"/>
59 <?php
60 break;
61
62 case 'enum':
63
64 $is_set_with_xpath_visible = true;
65 foreach ($params['enum_values'] as $key => $value): ?>
66 <div class="form-field wpallimport-radio-field wpallimport-<?php echo sanitize_title($params['field_name']); ?>_<?php echo $key; ?>">
67 <input type="radio" id="<?php echo sanitize_title($params['field_name']); ?>_<?php echo $key; ?>" class="switcher" name="<?php echo $params['field_name']; ?>" value="<?php echo $key; ?>" <?php echo $key == $params['field_value'] ? 'checked="checked"': '' ?>/>
68 <?php
69 $label = '';
70 $tooltip = '';
71 if (is_array($value)){
72 $label = array_shift($value);
73 }
74 else{
75 $label = $value;
76 }
77 ?>
78 <label for="<?php echo sanitize_title($params['field_name']); ?>_<?php echo $key; ?>"><?php echo $label; ?></label>
79 <?php
80 if (is_array($value) and ! empty($value)){
81 foreach ($value as $k => $p) {
82 if ( ! is_array($p)){
83 ?>
84 <a href="#help" class="wpallimport-help" title="<?php echo esc_attr($p); ?>" style="position: relative; top: -2px;">?</a>
85 <?php
86 break;
87 }
88 }
89 }
90 ?>
91 <?php
92 if (! empty($params['sub_fields'][$key])){
93 ?>
94 <div class="switcher-target-<?php echo sanitize_title($params['field_name']); ?>_<?php echo $key; ?>">
95 <div class="input sub_input">
96 <?php
97 foreach ($params['sub_fields'][$key] as $sub_field) {
98 PMXI_API::add_field($sub_field['type'], $sub_field['label'], $sub_field['params']);
99 }
100 ?>
101 </div>
102 </div>
103 <?php
104 $is_set_with_xpath_visible = false;
105 }
106 ?>
107
108 </div>
109 <?php endforeach;?>
110 <?php if ( $is_set_with_xpath_visible ): ?>
111 <div class="form-field wpallimport-radio-field wpallimport-<?php echo sanitize_title($params['field_name']); ?>_xpath">
112 <input type="radio" id="<?php echo sanitize_title($params['field_name']); ?>_xpath" class="switcher" name="<?php echo $params['field_name']; ?>" value="xpath" <?php echo 'xpath' === $params['field_value'] ? 'checked="checked"': '' ?>/>
113 <label for="<?php echo sanitize_title($params['field_name']); ?>_xpath"><?php _e('Set with XPath', 'wp_all_import_plugin' )?></label>
114 <span class="wpallimport-clear"></span>
115 <div class="switcher-target-<?php echo sanitize_title($params['field_name']); ?>_xpath set_with_xpath">
116 <span class="wpallimport-slide-content" style="padding-left:0px;">
117 <table class="form-table custom-params" style="max-width:none; border:none;">
118 <tr class="form-field">
119 <td class="wpallimport-enum-input-wrapper">
120 <input type="text" class="smaller-text" name="<?php echo $params['addon_prefix'];?>[xpaths][<?php echo $params['field_key']; ?>]" value="<?php echo esc_attr($params['xpath']) ?>"/>
121 </td>
122 <td class="action">
123
124 <?php if ($params['mapping']): ?>
125
126 <?php $custom_mapping_rules = (!empty($params['mapping_rules'])) ? json_decode($params['mapping_rules'], true) : false; ?>
127
128 <div class="input wpallimport-custom-fields-actions">
129 <a href="javascript:void(0);" class="wpallimport-cf-options"><?php _e('Field Options...', 'wp_all_import_plugin'); ?></a>
130 <ul id="wpallimport-cf-menu-<?php echo sanitize_title($params['field_name']);?>" class="wpallimport-cf-menu">
131 <li class="<?php echo ( ! empty($custom_mapping_rules) ) ? 'active' : ''; ?>">
132 <a href="javascript:void(0);" class="set_mapping pmxi_cf_mapping" rel="cf_mapping_<?php echo sanitize_title($params['field_name']); ?>"><?php _e('Mapping', 'wp_all_import_plugin'); ?></a>
133 </li>
134 </ul>
135 </div>
136 <div id="cf_mapping_<?php echo sanitize_title($params['field_name']); ?>" class="custom_type" rel="mapping" style="display:none;">
137 <fieldset>
138 <table cellpadding="0" cellspacing="5" class="cf-form-table" rel="cf_mapping_<?php echo sanitize_title($params['field_name']); ?>">
139 <thead>
140 <tr>
141 <td><?php _e('In Your File', 'wp_all_import_plugin') ?></td>
142 <td><?php _e('Translated To', 'wp_all_import_plugin') ?></td>
143 <td>&nbsp;</td>
144 </tr>
145 </thead>
146 <tbody>
147 <?php
148 if ( ! empty($custom_mapping_rules) and is_array($custom_mapping_rules)){
149
150 foreach ($custom_mapping_rules as $key => $value) {
151
152 $k = $key;
153
154 if (is_array($value)){
155 $keys = array_keys($value);
156 $k = $keys[0];
157 }
158
159 ?>
160 <tr class="form-field">
161 <td>
162 <input type="text" class="mapping_from widefat" value="<?php echo esc_textarea($k); ?>">
163 </td>
164 <td>
165 <input type="text" class="mapping_to widefat" value="<?php echo esc_textarea((is_array($value)) ? $value[$k] : $value); ?>">
166 </td>
167 <td class="action remove">
168 <a href="#remove" style="right:-10px;"></a>
169 </td>
170 </tr>
171 <?php
172 }
173 }
174 else{
175 if ( ! empty($params['enum_values']) and is_array($params['enum_values'])){
176 foreach ($params['enum_values'] as $key => $value){
177 ?>
178 <tr class="form-field">
179 <td>
180 <input type="text" class="mapping_from widefat">
181 </td>
182 <td>
183 <input type="text" class="mapping_to widefat" value="<?php echo $key; ?>">
184 </td>
185 <td class="action remove">
186 <a href="#remove" style="right:-10px;"></a>
187 </td>
188 </tr>
189 <?php
190 }
191 } else {
192 ?>
193 <tr class="form-field">
194 <td>
195 <input type="text" class="mapping_from widefat">
196 </td>
197 <td>
198 <input type="text" class="mapping_to widefat">
199 </td>
200 <td class="action remove">
201 <a href="#remove" style="right:-10px;"></a>
202 </td>
203 </tr>
204 <?php
205 }
206 }
207 ?>
208 <tr class="form-field template">
209 <td>
210 <input type="text" class="mapping_from widefat">
211 </td>
212 <td>
213 <input type="text" class="mapping_to widefat">
214 </td>
215 <td class="action remove">
216 <a href="#remove" style="right:-10px;"></a>
217 </td>
218 </tr>
219 <tr>
220 <td colspan="3">
221 <a href="javascript:void(0);" title="<?php _e('Add Another', 'wp_all_import_plugin')?>" class="action add-new-key add-new-entry"><?php _e('Add Another', 'wp_all_import_plugin') ?></a>
222 </td>
223 </tr>
224 <tr>
225 <td colspan="3">
226 <div class="wrap" style="position:relative;">
227 <a class="save_popup save_mr" href="javascript:void(0);"><?php _e('Save Rules', 'wp_all_import_plugin'); ?></a>
228 </div>
229 </td>
230 </tr>
231 </tbody>
232 </table>
233 <input type="hidden" class="pmre_mapping_rules" name="<?php echo $params['addon_prefix'];?>[mapping][<?php echo $params['field_key']; ?>]" value="<?php if (!empty($params['mapping_rules'])) echo esc_html($params['mapping_rules']); ?>"/>
234 </fieldset>
235 </div>
236 <?php endif; ?>
237 </td>
238 </tr>
239 </table>
240 </span>
241 </div>
242 </div>
243 <?php endif; ?>
244 <?php
245 break;
246
247 case 'textarea':
248 ?>
249 <textarea name="<?php echo $params['field_name']; ?>" id="<?php echo sanitize_title($params['field_name']); ?>" class="rad4 newline" style="height: 70px;margin: 5px 0;padding-top: 5px;width: 70%;"><?php echo esc_attr($params['field_value']); ?></textarea>
250 <?php
251 break;
252
253 case 'image':
254 ?>
255 <div class="input">
256 <div class="input" style="margin: 0px;">
257 <input type="radio" name="<?php echo $params['addon_prefix'];?>[download_image][<?php echo $params['field_key'];?>]" value="yes" id="<?php echo sanitize_title($params['field_name']); ?>_yes" <?php echo ("yes" == $params['download_image']) ? 'checked="checked"' : '';?>/>
258 <label for="<?php echo sanitize_title($params['field_name']); ?>_yes"><?php _e('Download image hosted elsewhere'); ?></label>
259 <a href="#help" class="wpallimport-help" title="<?php _e('http:// or https://', 'wp_all_import_plugin') ?>" style="position: relative; top: -2px;">?</a>
260 </div>
261 <div class="input" style="margin: 0px;">
262 <?php $wp_uploads = wp_upload_dir(); ?>
263 <input type="radio" name="<?php echo $params['addon_prefix'];?>[download_image][<?php echo $params['field_key'];?>]" value="no" id="<?php echo sanitize_title($params['field_name']); ?>_no" <?php echo ("yes" != $params['download_image']) ? 'checked="checked"' : '';?>/>
264 <label for="<?php echo sanitize_title($params['field_name']); ?>_no"><?php printf(__('Use image(s) currently uploaded in %s', 'wp_all_import_plugin'), $wp_uploads['basedir'] . DIRECTORY_SEPARATOR . PMXI_Plugin::FILES_DIRECTORY . DIRECTORY_SEPARATOR); ?></label>
265 </div>
266 <div class="input">
267 <input type="text" name="<?php echo $params['field_name']; ?>" style="width:100%;" placeholder="" value="<?php echo esc_attr($params['field_value']); ?>"/>
268 </div>
269 </div>
270 <?php
271 break;
272
273 case 'file':
274 ?>
275 <div class="input">
276 <div class="input" style="margin: 0px;">
277 <input type="radio" name="<?php echo $params['addon_prefix'];?>[download_image][<?php echo $params['field_key'];?>]" value="yes" id="<?php echo sanitize_title($params['field_name']); ?>_yes" <?php echo ("yes" == $params['download_image']) ? 'checked="checked"' : '';?>/>
278 <label for="<?php echo sanitize_title($params['field_name']); ?>_yes"><?php _e('Download file hosted elsewhere'); ?></label>
279 <a href="#help" class="wpallimport-help" title="<?php _e('http:// or https://', 'wp_all_import_plugin') ?>" style="position: relative; top: -2px;">?</a>
280 </div>
281 <div class="input" style="margin: 0px;">
282 <?php $wp_uploads = wp_upload_dir(); ?>
283 <input type="radio" name="<?php echo $params['addon_prefix'];?>[download_image][<?php echo $params['field_key'];?>]" value="no" id="<?php echo sanitize_title($params['field_name']); ?>_no" <?php echo ("yes" != $params['download_image']) ? 'checked="checked"' : '';?>/>
284 <label for="<?php echo sanitize_title($params['field_name']); ?>_no"><?php printf(__('Use file(s) currently uploaded in %s', 'wp_all_import_plugin'), $wp_uploads['basedir'] . DIRECTORY_SEPARATOR . PMXI_Plugin::FILES_DIRECTORY . DIRECTORY_SEPARATOR); ?></label>
285 </div>
286 <div class="input">
287 <input type="text" name="<?php echo $params['field_name']; ?>" style="width:100%;" placeholder="" value="<?php echo esc_attr($params['field_value']); ?>"/>
288 </div>
289 </div>
290 <?php
291 break;
292
293 case 'accordion':
294
295 $is_full_width = true;
296 if ( ! empty($params['sub_fields']) ){
297 foreach ($params['sub_fields'] as $sub_field) {
298 if ($sub_field[0]['params']['is_main_field']){
299 PMXI_API::add_field($sub_field[0]['type'], $sub_field[0]['label'], $sub_field[0]['params']);
300 $is_full_width = false;
301 break;
302 }
303 }
304 }
305
306 $in_the_bottom = $params['in_the_bottom'];
307
308 $styles = ($is_full_width and $in_the_bottom) ? 'wpallimport-full-with-bottom' : '';
309
310 if ( ! $in_the_bottom and $is_full_width ) $styles = 'wpallimport-full-with-not-bottom';
311
312 ?>
313 <div class="wpallimport-collapsed closed wpallimport-section <?php echo (($in_the_bottom and $is_full_width) ? 'wpallimport-sub-options-full-width' : 'wpallimport-sub-options'); echo ((!$is_full_width) ? ' wpallimport-dependent-options' : '');?> <?php echo $styles; ?>">
314 <div class="wpallimport-content-section <?php echo (($is_full_width and !$in_the_bottom) ? 'rad4' : 'wpallimport-bottom-radius');?>">
315 <div class="wpallimport-collapsed-header">
316 <h3 style="color:#40acad;"><?php echo $label; ?></h3>
317 </div>
318 <div class="wpallimport-collapsed-content" style="padding: 0;">
319 <div class="wpallimport-collapsed-content-inner">
320
321 <?php
322 if ( ! empty($params['sub_fields']) ){
323 foreach ($params['sub_fields'] as $sub_field) {
324 if ( ! $sub_field[0]['params']['is_main_field'])
325 PMXI_API::add_field($sub_field[0]['type'], $sub_field[0]['label'], $sub_field[0]['params']);
326 }
327 }
328 ?>
329
330 </div>
331 </div>
332 </div>
333 </div>
334 <?php
335 break;
336 }
337 if ( ! $params['in_the_bottom'] ){
338 ?>
339 </div>
340 <?php
341 }
342 echo ob_get_clean();
343 }
344
345 public static function add_additional_images_section( $section_title, $section_slug, $post, $post_type = '', $section_is_show_hints = true, $section_is_show_warning = false, $section_type = 'images'){
346
347 include( WP_ALL_IMPORT_ROOT_DIR . '/views/admin/import/template/_featured_template.php' );
348
349 }
350
351 public static function upload_image($pid, $img_url, $download_images, $logger, $create_image = false, $image_name = "", $file_type = 'images'){
352
353 if (empty($img_url)) return false;
354
355 $url = str_replace(" ", "%20", trim($img_url));
356 $bn = preg_replace('/[\\?|&].*/', '', basename($url));
357
358 if ($image_name == ""){
359 $img_ext = pmxi_getExtensionFromStr($url);
360 $default_extension = pmxi_getExtension($bn);
361 if ($img_ext == "") $img_ext = pmxi_get_remote_image_ext($url);
362 $image_name = urldecode(sanitize_file_name(($img_ext) ? str_replace("." . $default_extension, "", $bn) : $bn)) . (("" != $img_ext) ? '.' . $img_ext : '');
363 }
364
365 $uploads = wp_upload_dir();
366 $targetDir = $uploads['path'];
367 $targetUrl = $uploads['url'];
368 $download_image = true;
369 $result = false;
370 $wp_filetype = false;
371
372 global $wpdb;
373
374 $attch = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM " . $wpdb->posts . " WHERE (post_title = %s OR post_title = %s) AND post_type = %s;", $image_name, preg_replace('/\\.[^.\\s]{3,4}$/', '', $image_name), "attachment" ) );
375
376 if ( $attch != null ){
377
378 return $attch->ID;
379
380 }
381
382 $image_filename = wp_unique_filename($targetDir, $image_name);
383 $image_filepath = $targetDir . '/' . $image_filename;
384
385 // do not download images
386 if ( "yes" != $download_images ){
387
388 $image_filename = $image_name;
389 $image_filepath = $targetDir . '/' . $image_filename;
390
391 $wpai_uploads = $uploads['basedir'] . DIRECTORY_SEPARATOR . PMXI_Plugin::FILES_DIRECTORY . DIRECTORY_SEPARATOR;
392 $wpai_image_path = $wpai_uploads . str_replace('%20', ' ', $url);
393
394 $logger and call_user_func($logger, sprintf(__('- Searching for existing image `%s` in `%s` folder', 'wp_all_import_plugin'), $wpai_image_path, $wpai_uploads));
395
396 if ( @file_exists($wpai_image_path) and @copy( $wpai_image_path, $image_filepath )){
397 $download_image = false;
398 // valdate import attachments
399 if ($file_type == 'files'){
400 if( ! $wp_filetype = wp_check_filetype(basename($image_filepath), null )) {
401 $logger and call_user_func($logger, sprintf(__('- <b>WARNING</b>: Can\'t detect attachment file type %s', 'wp_all_import_plugin'), trim($image_filepath)));
402 $logger and !$is_cron and PMXI_Plugin::$session->warnings++;
403 @unlink($image_filepath);
404 }
405 else {
406 $result = true;
407 $logger and call_user_func($logger, sprintf(__('- File `%s` has been successfully found', 'wp_all_import_plugin'), $wpai_image_path));
408 }
409 }
410 // validate import images
411 elseif($file_type == 'images'){
412 if( ! ($image_info = @getimagesize($image_filepath)) or ! in_array($image_info[2], array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG))) {
413 $logger and call_user_func($logger, sprintf(__('- <b>WARNING</b>: File %s is not a valid image and cannot be set as featured one', 'wp_all_import_plugin'), $image_filepath));
414 @unlink($image_filepath);
415 } else {
416 $logger and call_user_func($logger, sprintf(__('- Image `%s` has been successfully found', 'wp_all_import_plugin'), $wpai_image_path));
417 $result = true;
418 }
419 }
420 }
421 }
422
423 if ($download_image){
424
425 if ($file_type == 'images'){
426 $logger and call_user_func($logger, sprintf(__('- Downloading image from `%s`', 'wp_all_import_plugin'), $url));
427 }
428 elseif ($file_type == 'files') {
429 $logger and call_user_func($logger, sprintf(__('- Downloading file from `%s`', 'wp_all_import_plugin'), $url));
430 }
431
432 $request = get_file_curl($url, $image_filepath);
433
434 if ( (is_wp_error($request) or $request === false) and ! @file_put_contents($image_filepath, @file_get_contents($url))) {
435 @unlink($image_filepath); // delete file since failed upload may result in empty file created
436 } else{
437
438 if($file_type == 'images'){
439 if( ($image_info = @getimagesize($image_filepath)) and in_array($image_info[2], array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG))) {
440 $result = true;
441 $logger and call_user_func($logger, sprintf(__('- Image `%s` has been successfully downloaded', 'wp_all_import_plugin'), $url));
442 }
443 }
444 elseif($file_type == 'files'){
445 if( $wp_filetype = wp_check_filetype(basename($image_filepath), null )) {
446 $result = true;
447 $logger and call_user_func($logger, sprintf(__('- File `%s` has been successfully downloaded', 'wp_all_import_plugin'), $url));
448 }
449 }
450
451 }
452
453 if ( ! $result ){
454 $url = str_replace(" ", "%20", trim(pmxi_convert_encoding($img_url)));
455
456 $request = get_file_curl($url, $image_filepath);
457
458 if ( (is_wp_error($request) or $request === false) and ! @file_put_contents($image_filepath, @file_get_contents($url))) {
459 $logger and call_user_func($logger, sprintf(__('- <b>WARNING</b>: File %s cannot be saved locally as %s', 'wp_all_import_plugin'), $url, $image_filepath));
460 @unlink($image_filepath); // delete file since failed upload may result in empty file created
461 } else{
462
463 if($file_type == 'images'){
464 if( ! ($image_info = @getimagesize($image_filepath)) or ! in_array($image_info[2], array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG))) {
465 $logger and call_user_func($logger, sprintf(__('- <b>WARNING</b>: File %s is not a valid image and cannot be set as featured one', 'wp_all_import_plugin'), $url));
466 @unlink($image_filepath);
467 } else {
468 $result = true;
469 $logger and call_user_func($logger, sprintf(__('- Image `%s` has been successfully downloaded', 'wp_all_import_plugin'), $url));
470 }
471 }
472 elseif($file_type == 'files'){
473 if( ! $wp_filetype = wp_check_filetype(basename($image_filepath), null )) {
474 $logger and call_user_func($logger, sprintf(__('- <b>WARNING</b>: Can\'t detect attachment file type %s', 'wp_all_import_plugin'), trim($url)));
475 @unlink($image_filepath);
476 }
477 else {
478 $result = true;
479 $logger and call_user_func($logger, sprintf(__('- File `%s` has been successfully found', 'wp_all_import_plugin'), $url));
480 }
481 }
482 }
483 }
484 }
485
486 if ($create_image and $result){
487
488 if($file_type == 'images'){
489 $logger and call_user_func($logger, sprintf(__('- Creating an attachment for image `%s`', 'wp_all_import_plugin'), $targetUrl . '/' . $image_filename));
490 }
491 else{
492 $logger and call_user_func($logger, sprintf(__('- Creating an attachment for file `%s`', 'wp_all_import_plugin'), $targetUrl . '/' . $image_filename));
493 }
494
495 $attachment = array(
496 'post_mime_type' => ($file_type == 'images') ? image_type_to_mime_type($image_info[2]) : $wp_filetype['type'],
497 'guid' => $targetUrl . '/' . $image_filename,
498 'post_title' => $image_filename,
499 'post_content' => '',
500 );
501 if ($file_type == 'images' and ($image_meta = wp_read_image_metadata($image_filepath))) {
502 if (trim($image_meta['title']) && ! is_numeric(sanitize_title($image_meta['title'])))
503 $attachment['post_title'] = $image_meta['title'];
504 if (trim($image_meta['caption']))
505 $attachment['post_content'] = $image_meta['caption'];
506 }
507
508 $attid = wp_insert_attachment($attachment, $image_filepath, $pid);
509
510 if (is_wp_error($attid)) {
511 $logger and call_user_func($logger, __('- <b>WARNING</b>', 'wp_all_import_plugin') . ': ' . $attid->get_error_message());
512 return false;
513 } else {
514 // you must first include the image.php file
515 // for the function wp_generate_attachment_metadata() to work
516 require_once(ABSPATH . 'wp-admin/includes/image.php');
517 wp_update_attachment_metadata($attid, wp_generate_attachment_metadata($attid, $image_filepath));
518 $logger and call_user_func($logger, sprintf(__('- Attachment has been successfully created for image `%s`', 'wp_all_import_plugin'), $targetUrl . '/' . $image_filename));
519 return $attid;
520 }
521
522 }
523 else return $result;
524 }
525 }
526