PluginProbe ʕ •ᴥ•ʔ
WP All Import – Drag & Drop Import for CSV, XML, Excel & Google Sheets / 3.9.4
WP All Import – Drag & Drop Import for CSV, XML, Excel & Google Sheets v3.9.4
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
XmlStreamReader 2 years ago partner-discount-sdk 1 year ago api.php 4 years ago arraytoxml.php 8 years ago chunk.php 1 year ago config.php 2 years ago download.php 13 years ago error.php 2 years ago handler.php 9 months ago helper.php 8 years ago input.php 7 years ago nested.php 4 years ago rapidaddon.php 9 months ago render.php 4 years ago session.php 9 months ago upload.php 9 months ago zip.php 10 years ago
api.php
651 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 esc_attr($params['field_name']); ?>"><?php echo wp_kses_post($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 esc_attr($params['field_name']); ?>" id="<?php echo sanitize_title(esc_attr($params['field_name'])); ?>" value="<?php echo esc_attr($params['field_value']); ?>" style="width:100%;"/>
59 <?php
60 break;
61 case 'enum':
62
63 $is_set_with_xpath_visible = true;
64 foreach ($params['enum_values'] as $key => $value): ?>
65 <div class="form-field wpallimport-radio-field wpallimport-<?php echo sanitize_title(esc_attr($params['field_name'])); ?>_<?php echo sanitize_title(esc_attr($key)); ?>">
66 <input type="radio" id="<?php echo sanitize_title(esc_attr($params['field_name'])); ?>_<?php echo sanitize_title(esc_attr($key)); ?>" class="switcher" name="<?php echo esc_attr($params['field_name']); ?>" value="<?php echo esc_attr($key); ?>" <?php echo $key == $params['field_value'] ? 'checked="checked"': '' ?>/>
67 <?php
68 $label = '';
69 $tooltip = '';
70 if (is_array($value)){
71 $label = array_shift($value);
72 }
73 else{
74 $label = $value;
75 }
76 ?>
77 <label for="<?php echo sanitize_title(esc_attr($params['field_name'])); ?>_<?php echo sanitize_title(esc_attr($key)); ?>"><?php echo wp_kses_post($label); ?></label>
78 <?php
79 if (is_array($value) and ! empty($value)){
80 foreach ($value as $k => $p) {
81 if ( ! is_array($p)){
82 ?>
83 <a href="#help" class="wpallimport-help" title="<?php echo esc_attr($p); ?>" style="position: relative; top: -2px;">?</a>
84 <?php
85 break;
86 }
87 }
88 }
89 ?>
90 <?php
91 if (! empty($params['sub_fields'][$key])){
92 ?>
93 <div class="switcher-target-<?php echo sanitize_title(esc_attr($params['field_name'])); ?>_<?php echo esc_attr($key); ?>">
94 <div class="input sub_input">
95 <?php
96 foreach ($params['sub_fields'][$key] as $sub_field) {
97 PMXI_API::add_field($sub_field['type'], $sub_field['label'], $sub_field['params']);
98 }
99 ?>
100 </div>
101 </div>
102 <?php
103 $is_set_with_xpath_visible = false;
104 }
105 ?>
106
107 </div>
108 <?php endforeach;?>
109 <?php if ( $is_set_with_xpath_visible ): ?>
110 <div class="form-field wpallimport-radio-field wpallimport-<?php echo sanitize_title(esc_attr($params['field_name'])); ?>_xpath">
111 <input type="radio" id="<?php echo sanitize_title(esc_attr($params['field_name'])); ?>_xpath" class="switcher" name="<?php echo esc_attr($params['field_name']); ?>" value="xpath" <?php echo 'xpath' === $params['field_value'] ? 'checked="checked"': '' ?>/>
112 <label for="<?php echo sanitize_title(esc_attr($params['field_name'])); ?>_xpath"><?php _e('Set with XPath', 'wp_all_import_plugin' )?></label>
113 <span class="wpallimport-clear"></span>
114 <div class="switcher-target-<?php echo sanitize_title(esc_attr($params['field_name'])); ?>_xpath set_with_xpath">
115 <span class="wpallimport-slide-content" style="padding-left:0px;">
116 <table class="form-table custom-params" style="max-width:none; border:none;">
117 <tr class="form-field">
118 <td class="wpallimport-enum-input-wrapper">
119 <input type="text" class="smaller-text" name="<?php echo esc_attr($params['addon_prefix']);?>[xpaths][<?php echo esc_attr($params['field_key']); ?>]" value="<?php echo esc_attr($params['xpath']) ?>"/>
120 </td>
121 <td class="action">
122
123 <?php if ($params['mapping']): ?>
124
125 <?php $custom_mapping_rules = (!empty($params['mapping_rules'])) ? json_decode($params['mapping_rules'], true) : false; ?>
126
127 <div class="input wpallimport-custom-fields-actions">
128 <a href="javascript:void(0);" class="wpallimport-cf-options"><?php _e('Field Options...', 'wp_all_import_plugin'); ?></a>
129 <ul id="wpallimport-cf-menu-<?php echo sanitize_title(esc_attr($params['field_name']));?>" class="wpallimport-cf-menu">
130 <li class="<?php echo ( ! empty($custom_mapping_rules) ) ? 'active' : ''; ?>">
131 <a href="javascript:void(0);" class="set_mapping pmxi_cf_mapping" rel="cf_mapping_<?php echo sanitize_title(esc_attr($params['field_name'])); ?>"><?php _e('Mapping', 'wp_all_import_plugin'); ?></a>
132 </li>
133 </ul>
134 </div>
135 <div id="cf_mapping_<?php echo sanitize_title(esc_attr($params['field_name'])); ?>" class="custom_type" rel="mapping" style="display:none;">
136 <fieldset>
137 <table cellpadding="0" cellspacing="5" class="cf-form-table" rel="cf_mapping_<?php echo sanitize_title(esc_attr($params['field_name'])); ?>">
138 <thead>
139 <tr>
140 <td><?php _e('In Your File', 'wp_all_import_plugin') ?></td>
141 <td><?php _e('Translated To', 'wp_all_import_plugin') ?></td>
142 <td>&nbsp;</td>
143 </tr>
144 </thead>
145 <tbody>
146 <?php
147 if ( ! empty($custom_mapping_rules) and is_array($custom_mapping_rules)){
148
149 foreach ($custom_mapping_rules as $key => $value) {
150
151 $k = $key;
152
153 if (is_array($value)){
154 $keys = array_keys($value);
155 $k = $keys[0];
156 }
157
158 ?>
159 <tr class="form-field">
160 <td>
161 <input type="text" class="mapping_from widefat" value="<?php echo esc_textarea($k); ?>">
162 </td>
163 <td>
164 <input type="text" class="mapping_to widefat" value="<?php echo esc_textarea((is_array($value)) ? $value[$k] : $value); ?>">
165 </td>
166 <td class="action remove">
167 <a href="#remove" style="right:-10px;"></a>
168 </td>
169 </tr>
170 <?php
171 }
172 }
173 else{
174 if ( ! empty($params['enum_values']) and is_array($params['enum_values'])){
175 foreach ($params['enum_values'] as $key => $value){
176 ?>
177 <tr class="form-field">
178 <td>
179 <input type="text" class="mapping_from widefat">
180 </td>
181 <td>
182 <input type="text" class="mapping_to widefat" value="<?php echo esc_attr($key); ?>">
183 </td>
184 <td class="action remove">
185 <a href="#remove" style="right:-10px;"></a>
186 </td>
187 </tr>
188 <?php
189 }
190 } else {
191 ?>
192 <tr class="form-field">
193 <td>
194 <input type="text" class="mapping_from widefat">
195 </td>
196 <td>
197 <input type="text" class="mapping_to widefat">
198 </td>
199 <td class="action remove">
200 <a href="#remove" style="right:-10px;"></a>
201 </td>
202 </tr>
203 <?php
204 }
205 }
206 ?>
207 <tr class="form-field template">
208 <td>
209 <input type="text" class="mapping_from widefat">
210 </td>
211 <td>
212 <input type="text" class="mapping_to widefat">
213 </td>
214 <td class="action remove">
215 <a href="#remove" style="right:-10px;"></a>
216 </td>
217 </tr>
218 <tr>
219 <td colspan="3">
220 <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>
221 </td>
222 </tr>
223 <tr>
224 <td colspan="3">
225 <div class="wrap" style="position:relative;">
226 <a class="save_popup save_mr" href="javascript:void(0);"><?php _e('Save Rules', 'wp_all_import_plugin'); ?></a>
227 </div>
228 </td>
229 </tr>
230 </tbody>
231 </table>
232 <input type="hidden" class="pmre_mapping_rules" name="<?php echo esc_attr($params['addon_prefix']);?>[mapping][<?php echo esc_attr($params['field_key']); ?>]" value="<?php if (!empty($params['mapping_rules'])) echo esc_attr($params['mapping_rules']); ?>"/>
233 </fieldset>
234 </div>
235 <?php endif; ?>
236 </td>
237 </tr>
238 </table>
239 </span>
240 </div>
241 </div>
242 <?php endif; ?>
243 <?php
244 break;
245
246 case 'textarea':
247 ?>
248 <textarea name="<?php echo esc_attr($params['field_name']); ?>" id="<?php echo sanitize_title(esc_attr($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>
249 <?php
250 break;
251
252 case 'wp_editor':
253 ?>
254 <div id="<?php echo ( user_can_richedit() ? 'postdivrich' : 'postdiv' ) . sanitize_title(esc_attr($params['field_name'])); ?>" class="postarea">
255 <?php wp_editor( empty($params['field_value']) ? '' : esc_textarea($params['field_value']), sanitize_title(esc_attr($params['field_name'])), array(
256 'teeny' => true,
257 'media_buttons' => false,
258 'textarea_name' => esc_attr($params['field_name']),
259 'editor_height' => 200));
260 ?>
261 </div>
262 <?php
263 break;
264
265 case 'image':
266 ?>
267 <div class="input">
268 <div class="input" style="margin: 0px;">
269 <input type="radio" name="<?php echo esc_attr($params['addon_prefix']);?>[download_image][<?php echo esc_attr($params['field_key']);?>]" value="yes" id="<?php echo sanitize_title(esc_attr($params['field_name'])); ?>_yes" <?php echo ("yes" == $params['download_image']) ? 'checked="checked"' : '';?>/>
270 <label for="<?php echo sanitize_title(esc_attr($params['field_name'])); ?>_yes"><?php _e('Download image hosted elsewhere'); ?></label>
271 <a href="#help" class="wpallimport-help" title="<?php _e('http:// or https://', 'wp_all_import_plugin') ?>" style="position: relative; top: -2px;">?</a>
272 </div>
273 <div class="input" style="margin: 0px;">
274 <?php $wp_uploads = wp_upload_dir(); ?>
275 <input type="radio" name="<?php echo esc_attr($params['addon_prefix']);?>[download_image][<?php echo esc_attr($params['field_key']);?>]" value="no" id="<?php echo sanitize_title(esc_attr($params['field_name'])); ?>_no" <?php echo ("yes" != $params['download_image']) ? 'checked="checked"' : '';?>/>
276 <label for="<?php echo sanitize_title(esc_attr($params['field_name'])); ?>_no"><?php printf(__('Use image(s) currently uploaded in %s', 'wp_all_import_plugin'), esc_url($wp_uploads['basedir'] . DIRECTORY_SEPARATOR . PMXI_Plugin::FILES_DIRECTORY . DIRECTORY_SEPARATOR)); ?></label>
277 </div>
278 <div class="input">
279 <input type="text" name="<?php echo esc_attr($params['field_name']); ?>" style="width:100%;" placeholder="" value="<?php echo esc_attr($params['field_value']); ?>"/>
280 </div>
281 </div>
282 <?php
283 break;
284
285 case 'file':
286 ?>
287 <div class="input">
288 <div class="input" style="margin: 0px;">
289 <input type="radio" name="<?php echo esc_attr($params['addon_prefix']);?>[download_image][<?php echo esc_attr($params['field_key']);?>]" value="yes" id="<?php echo sanitize_title(esc_attr($params['field_name'])); ?>_yes" <?php echo ("yes" == $params['download_image']) ? 'checked="checked"' : '';?>/>
290 <label for="<?php echo sanitize_title(esc_attr($params['field_name'])); ?>_yes"><?php _e('Download file hosted elsewhere'); ?></label>
291 <a href="#help" class="wpallimport-help" title="<?php _e('http:// or https://', 'wp_all_import_plugin') ?>" style="position: relative; top: -2px;">?</a>
292 </div>
293 <div class="input" style="margin: 0px;">
294 <?php $wp_uploads = wp_upload_dir(); ?>
295 <input type="radio" name="<?php echo esc_attr($params['addon_prefix']);?>[download_image][<?php echo esc_attr($params['field_key']);?>]" value="no" id="<?php echo sanitize_title(esc_attr($params['field_name'])); ?>_no" <?php echo ("yes" != $params['download_image']) ? 'checked="checked"' : '';?>/>
296 <label for="<?php echo sanitize_title(esc_attr($params['field_name'])); ?>_no"><?php printf(__('Use file(s) currently uploaded in %s', 'wp_all_import_plugin'), esc_url($wp_uploads['basedir'] . DIRECTORY_SEPARATOR . PMXI_Plugin::FILES_DIRECTORY . DIRECTORY_SEPARATOR)); ?></label>
297 </div>
298 <div class="input">
299 <input type="text" name="<?php echo esc_attr($params['field_name']); ?>" style="width:100%;" placeholder="" value="<?php echo esc_attr($params['field_value']); ?>"/>
300 </div>
301 </div>
302 <?php
303 break;
304
305 case 'accordion':
306
307 $is_full_width = true;
308 if ( ! empty($params['sub_fields']) ){
309 foreach ($params['sub_fields'] as $sub_field) {
310 if (!empty($sub_field[0]['params']['is_main_field'])){
311 PMXI_API::add_field($sub_field[0]['type'], $sub_field[0]['label'], $sub_field[0]['params']);
312 $is_full_width = false;
313 break;
314 }
315 }
316 }
317
318 $in_the_bottom = $params['in_the_bottom'];
319
320 $styles = ($is_full_width and $in_the_bottom) ? 'wpallimport-full-with-bottom' : '';
321
322 if ( ! $in_the_bottom and $is_full_width ) $styles = 'wpallimport-full-with-not-bottom';
323
324 ?>
325 <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 esc_attr($styles); ?>">
326 <div class="wpallimport-content-section <?php echo (($is_full_width and !$in_the_bottom) ? 'rad4' : 'wpallimport-bottom-radius');?>">
327 <div class="wpallimport-collapsed-header">
328 <h3 style="color:#40acad;"><?php echo wp_kses_post($label); ?></h3>
329 </div>
330 <div class="wpallimport-collapsed-content" style="padding: 0;">
331 <div class="wpallimport-collapsed-content-inner">
332
333 <?php
334 if ( ! empty($params['sub_fields']) ){
335 foreach ($params['sub_fields'] as $sub_field) {
336 if ( empty($sub_field[0]['params']['is_main_field']) ) {
337 PMXI_API::add_field($sub_field[0]['type'], $sub_field[0]['label'], $sub_field[0]['params']);
338 }
339 }
340 }
341 ?>
342
343 </div>
344 </div>
345 </div>
346 </div>
347 <?php
348 break;
349 }
350 if ( ! $params['in_the_bottom'] ){
351 ?>
352 </div>
353 <?php
354 }
355 echo ob_get_clean();
356 }
357
358 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'){
359
360 include( WP_ALL_IMPORT_ROOT_DIR . '/views/admin/import/template/_featured_template.php' );
361
362 }
363
364 public static function upload_image($pid, $img_url, $download_images, $logger, $create_image = false, $image_name = "", $file_type = 'images', $check_existing = true, $articleData = false, $importData = false) {
365
366 if (empty($img_url)) return false;
367
368 $bn = wp_all_import_sanitize_filename(urldecode(basename($img_url)));
369
370 if ($image_name == ""){
371 $img_ext = pmxi_getExtensionFromStr($img_url);
372 $default_extension = pmxi_getExtension($bn);
373 if ($img_ext == "") $img_ext = pmxi_get_remote_image_ext($img_url);
374 // generate local file name
375 $image_name = apply_filters("wp_all_import_api_image_filename", urldecode(sanitize_file_name((($img_ext) ? str_replace("." . $default_extension, "", $bn) : $bn))) . (("" != $img_ext) ? '.' . $img_ext : ''), $img_url, $pid);
376 }
377
378 $current_xml_node = false;
379 if (!empty($importData['current_xml_node'])) {
380 $current_xml_node = $importData['current_xml_node'];
381 }
382 $import_id = false;
383 if (!empty($importData['import'])) {
384 $import_id = $importData['import']->id;
385 }
386
387 $uploads = wp_upload_dir();
388 $uploads = apply_filters('wp_all_import_images_uploads_dir', $uploads, $articleData, $current_xml_node, $import_id);
389
390 $targetDir = $uploads['path'];
391 $targetUrl = $uploads['url'];
392 $download_image = true;
393 $result = false;
394 $wp_filetype = false;
395 $attch = false;
396
397 if ( $check_existing ) {
398 // Trying to find existing image in hash table.
399 if ("yes" == $download_images ) {
400 $logger and call_user_func($logger, sprintf(__('- Searching for existing image `%s` by URL...', 'wp_all_import_plugin'), rawurldecode($img_url)));
401 $imageList = new PMXI_Image_List();
402 $attch = $imageList->getExistingImageByUrl($img_url);
403 if ($attch) {
404 $logger and call_user_func($logger, sprintf(__('Existing image was found by URL `%s`...', 'wp_all_import_plugin'), $img_url));
405 // Attach media to current post if it's currently unattached.
406 if (empty($attch->post_parent)) {
407 wp_update_post(
408 array(
409 'ID' => $attch->ID,
410 'post_parent' => $pid
411 )
412 );
413 }
414 return $attch->ID;
415 }
416 }
417
418 if (empty($attch)) {
419 $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));
420 $attch = wp_all_import_get_image_from_gallery($image_name, $targetDir, $file_type);
421 }
422
423 if (!empty($attch)) {
424 $logger and call_user_func($logger, sprintf(__('- Existing image was found by `_wp_attached_file` ...', 'wp_all_import_plugin'), $img_url));
425 $imageRecord = new PMXI_Image_Record();
426 $imageRecord->getBy(array(
427 'attachment_id' => $attch->ID
428 ));
429 $imageRecord->isEmpty() and $imageRecord->set(array(
430 'attachment_id' => $attch->ID,
431 'image_url' => $img_url,
432 'image_filename' => $image_name
433 ))->insert();
434 // Attach media to current post if it's currently unattached.
435 if (empty($attch->post_parent)) {
436 wp_update_post(
437 array(
438 'ID' => $attch->ID,
439 'post_parent' => $pid
440 )
441 );
442 }
443 return $attch->ID;
444 }
445 }
446
447 $image_filename = wp_unique_filename($targetDir, $image_name);
448 $image_filepath = $targetDir . '/' . $image_filename;
449
450 $url = str_replace(" ", "%20", trim($img_url));
451
452 $is_base64_images_allowed = apply_filters("wp_all_import_is_base64_images_allowed", true, $url, false);
453
454 if ( $file_type == 'images' && base64_encode(base64_decode($url)) == $url && $is_base64_images_allowed ) {
455 $image_name = md5($url) . '.jpg';
456 // Search existing attachment.
457 $attch = wp_all_import_get_image_from_gallery($image_name, $targetDir, $file_type);
458 if (empty($attch)) {
459 $logger and call_user_func($logger, sprintf(__('- <b>WARNING</b>: Image %s not found in media gallery.', 'wp_all_import_plugin'), trim($image_name)));
460 } else {
461 $logger and call_user_func($logger, sprintf(__('- Using existing image `%s`...', 'wp_all_import_plugin'), trim($image_name)));
462 // Attach media to current post if it's currently unattached.
463 if (empty($attch->post_parent)) {
464 wp_update_post(
465 array(
466 'ID' => $attch->ID,
467 'post_parent' => $pid
468 )
469 );
470 }
471 return $attch->ID;
472 }
473
474 if ("yes" == $download_images) {
475 $img = @imagecreatefromstring(base64_decode($url));
476 if ($img) {
477 $image_filename = $image_name;
478 $logger and call_user_func($logger, __('- found base64_encoded image', 'wp_all_import_plugin'));
479 $image_filepath = $targetDir . '/' . $image_filename;
480 imagejpeg($img, $image_filepath);
481 if ( ! ($image_info = apply_filters('pmxi_getimagesize', @getimagesize($image_filepath), $image_filepath)) or ! in_array($image_info[2], wp_all_import_supported_image_types())) {
482 $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));
483 } else {
484 $result = true;
485 $download_image = false;
486 }
487 }
488 }
489 }
490
491 // Do not download images.
492 if ( "yes" != $download_images ) {
493
494 $image_filename = wp_unique_filename($targetDir, basename($image_name));
495 $image_filepath = $targetDir . '/' . basename($image_filename);
496
497 $wpai_uploads = $uploads['basedir'] . DIRECTORY_SEPARATOR . PMXI_Plugin::FILES_DIRECTORY . DIRECTORY_SEPARATOR;
498 $wpai_image_path = $wpai_uploads . str_replace('%20', ' ', $url);
499
500 $logger and call_user_func($logger, sprintf(__('- Searching for existing image `%s` in `%s` folder', 'wp_all_import_plugin'), $wpai_image_path, $wpai_uploads));
501
502 if ( @file_exists($wpai_image_path) and @copy( $wpai_image_path, $image_filepath )){
503 $download_image = false;
504 // Validate import attachments.
505 if ($file_type == 'files') {
506 if ( ! $wp_filetype = wp_check_filetype(basename($image_filepath), null )) {
507 $logger and call_user_func($logger, sprintf(__('- <b>WARNING</b>: Can\'t detect attachment file type %s', 'wp_all_import_plugin'), trim($image_filepath)));
508 @unlink($image_filepath);
509 }
510 else {
511 $result = true;
512 $logger and call_user_func($logger, sprintf(__('- File `%s` has been successfully found', 'wp_all_import_plugin'), $wpai_image_path));
513 }
514 }
515 // Validate import images.
516 elseif ($file_type == 'images') {
517 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())) {
518 $logger and call_user_func($logger, sprintf(__('- Image `%s` has been successfully found', 'wp_all_import_plugin'), $wpai_image_path));
519 $result = true;
520 } else {
521 $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));
522 @unlink($image_filepath);
523 }
524 }
525 }
526 }
527
528 if ( $download_image ) {
529
530 if ($file_type == 'images') {
531 $logger and call_user_func($logger, sprintf(__('- Downloading image from `%s`', 'wp_all_import_plugin'), $url));
532 } elseif ($file_type == 'files') {
533 $logger and call_user_func($logger, sprintf(__('- Downloading file from `%s`', 'wp_all_import_plugin'), $url));
534 }
535
536 if ( ! preg_match('%^(http|ftp)s?://%i', $url) ) {
537 $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));
538 return false;
539 }
540
541 $request = get_file_curl($url, $image_filepath);
542
543 if ( (is_wp_error($request) or $request === false) and ! @file_put_contents($image_filepath, @file_get_contents($url))) {
544 @unlink($image_filepath); // delete file since failed upload may result in empty file created
545 } else {
546
547 if ($file_type == 'images') {
548 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())) {
549 $result = true;
550 $logger and call_user_func($logger, sprintf(__('- Image `%s` has been successfully downloaded', 'wp_all_import_plugin'), $url));
551 }
552 }
553 elseif ($file_type == 'files'){
554 if ( $wp_filetype = wp_check_filetype(basename($image_filepath), null )) {
555 $result = true;
556 $logger and call_user_func($logger, sprintf(__('- File `%s` has been successfully downloaded', 'wp_all_import_plugin'), $url));
557 }
558 }
559 }
560
561 if ( ! $result ) {
562
563 $request = get_file_curl($url, $image_filepath);
564
565 if ( (is_wp_error($request) or $request === false) and ! @file_put_contents($image_filepath, @file_get_contents($url))) {
566 $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));
567 @unlink($image_filepath); // delete file since failed upload may result in empty file created
568 } else {
569
570 if ($file_type == 'images') {
571 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())) {
572 $result = true;
573 $logger and call_user_func($logger, sprintf(__('- Image `%s` has been successfully downloaded', 'wp_all_import_plugin'), $url));
574 } else {
575 $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));
576 @unlink($image_filepath);
577 }
578 }
579 elseif ($file_type == 'files') {
580 if ( ! $wp_filetype = wp_check_filetype(basename($image_filepath), null )) {
581 $logger and call_user_func($logger, sprintf(__('- <b>WARNING</b>: Can\'t detect attachment file type %s', 'wp_all_import_plugin'), trim($url)));
582 @unlink($image_filepath);
583 } else {
584 $result = true;
585 $logger and call_user_func($logger, sprintf(__('- File `%s` has been successfully found', 'wp_all_import_plugin'), $url));
586 }
587 }
588 }
589 }
590 }
591
592 if ( $create_image && $result ) {
593
594 // you must first include the image.php file
595 // for the function wp_generate_attachment_metadata() to work
596 require_once(ABSPATH . 'wp-admin/includes/image.php');
597
598 if ($file_type == 'images') {
599 $logger and call_user_func($logger, sprintf(__('- Creating an attachment for image `%s`', 'wp_all_import_plugin'), $targetUrl . '/' . basename($image_filename)));
600 } else {
601 $logger and call_user_func($logger, sprintf(__('- Creating an attachment for file `%s`', 'wp_all_import_plugin'), $targetUrl . '/' . basename($image_filename)));
602 }
603
604 $file_mime_type = empty($wp_filetype['type']) ? '' : $wp_filetype['type'];
605 if ($file_type == 'images' && !empty($image_info)) {
606 $file_mime_type = image_type_to_mime_type($image_info[2]);
607 }
608 $file_mime_type = apply_filters('wp_all_import_image_mime_type', $file_mime_type, $image_filepath);
609 $attachment = [
610 'post_mime_type' => $file_mime_type,
611 'guid' => $targetUrl . '/' . basename($image_filename),
612 'post_title' => basename($image_filename),
613 'post_content' => '',
614 ];
615 if (!empty($articleData['post_author'])) {
616 $attachment['post_author'] = $articleData['post_author'];
617 }
618 if ($file_type == 'images' and ($image_meta = wp_read_image_metadata($image_filepath))) {
619 if (trim($image_meta['title']) && ! is_numeric(sanitize_title($image_meta['title'])))
620 $attachment['post_title'] = $image_meta['title'];
621 if (trim($image_meta['caption']))
622 $attachment['post_content'] = $image_meta['caption'];
623 }
624 remove_all_actions('add_attachment');
625 $attid = wp_insert_attachment($attachment, $image_filepath, $pid);
626
627 if (is_wp_error($attid)) {
628 $logger and call_user_func($logger, __('- <b>WARNING</b>', 'wp_all_import_plugin') . ': ' . $attid->get_error_message());
629 return false;
630 } else {
631 /** Fires once an attachment has been added. */
632 do_action( 'wp_all_import_add_attachment', $attid );
633 wp_update_attachment_metadata($attid, wp_generate_attachment_metadata($attid, $image_filepath));
634 $imageRecord = new PMXI_Image_Record();
635 $imageRecord->getBy(array(
636 'attachment_id' => $attid
637 ));
638 $imageRecord->isEmpty() and $imageRecord->set(array(
639 'attachment_id' => $attid,
640 'image_url' => $img_url,
641 'image_filename' => $image_filename
642 ))->insert();
643 $logger and call_user_func($logger, sprintf(__('- Attachment has been successfully created for image `%s`', 'wp_all_import_plugin'), $targetUrl . '/' . basename($image_filename)));
644 return $attid;
645 }
646 }
647
648 return $result;
649 }
650 }
651