XmlStreamReader
3 weeks ago
partner-discount-sdk
3 weeks ago
api.php
3 weeks ago
arraytoxml.php
3 weeks ago
chunk.php
3 weeks ago
config.php
2 years ago
download.php
3 weeks ago
error.php
3 weeks ago
handler.php
3 weeks ago
helper.php
3 weeks ago
input.php
3 weeks ago
nested.php
3 weeks ago
rapidaddon.php
3 weeks ago
render.php
3 weeks ago
session.php
9 months ago
upload.php
3 weeks ago
zip.php
10 years ago
api.php
688 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 esc_attr(sanitize_title($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 esc_attr(sanitize_title($params['field_name'])); ?>_<?php echo esc_attr(sanitize_title($key)); ?>"> |
| 66 | <input type="radio" id="<?php echo esc_attr(sanitize_title($params['field_name'])); ?>_<?php echo esc_attr(sanitize_title($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 esc_attr(sanitize_title($params['field_name'])); ?>_<?php echo esc_attr(sanitize_title($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 esc_attr(sanitize_title($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 esc_attr(sanitize_title($params['field_name'])); ?>_xpath"> |
| 111 | <input type="radio" id="<?php echo esc_attr(sanitize_title($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 esc_attr(sanitize_title($params['field_name'])); ?>_xpath"><?php esc_html_e('Set with XPath', 'wp-all-import' )?></label> |
| 113 | <span class="wpallimport-clear"></span> |
| 114 | <div class="switcher-target-<?php echo esc_attr(sanitize_title($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 esc_html_e('Field Options...', 'wp-all-import'); ?></a> |
| 129 | <ul id="wpallimport-cf-menu-<?php echo esc_attr(sanitize_title($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 esc_attr(sanitize_title($params['field_name'])); ?>"><?php esc_html_e('Mapping', 'wp-all-import'); ?></a> |
| 132 | </li> |
| 133 | </ul> |
| 134 | </div> |
| 135 | <div id="cf_mapping_<?php echo esc_attr(sanitize_title($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 esc_attr(sanitize_title($params['field_name'])); ?>"> |
| 138 | <thead> |
| 139 | <tr> |
| 140 | <td><?php esc_html_e('In Your File', 'wp-all-import') ?></td> |
| 141 | <td><?php esc_html_e('Translated To', 'wp-all-import') ?></td> |
| 142 | <td> </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 esc_attr_e('Add Another', 'wp-all-import')?>" class="action add-new-key add-new-entry"><?php esc_html_e('Add Another', 'wp-all-import') ?></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 esc_html_e('Save Rules', 'wp-all-import'); ?></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 esc_attr(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> |
| 249 | <?php |
| 250 | break; |
| 251 | |
| 252 | case 'wp_editor': |
| 253 | ?> |
| 254 | <div id="<?php echo esc_attr(( user_can_richedit() ? 'postdivrich' : 'postdiv' ) . sanitize_title($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 esc_attr(sanitize_title($params['field_name'])); ?>_yes" <?php echo ("yes" == $params['download_image']) ? 'checked="checked"' : '';?>/> |
| 270 | <label for="<?php echo esc_attr(sanitize_title($params['field_name'])); ?>_yes"><?php esc_html_e('Download image hosted elsewhere', 'wp-all-import'); ?></label> |
| 271 | <a href="#help" class="wpallimport-help" title="<?php esc_attr_e('http:// or https://', 'wp-all-import') ?>" 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 esc_attr(sanitize_title($params['field_name'])); ?>_no" <?php echo ("yes" != $params['download_image']) ? 'checked="checked"' : '';?>/> |
| 276 | <label for="<?php echo esc_attr(sanitize_title($params['field_name'])); ?>_no"><?php |
| 277 | printf( |
| 278 | /* translators: %s: uploads directory path */ |
| 279 | esc_html__('Use image(s) currently uploaded in %s', 'wp-all-import'), |
| 280 | esc_url($wp_uploads['basedir'] . DIRECTORY_SEPARATOR . PMXI_Plugin::FILES_DIRECTORY . DIRECTORY_SEPARATOR) |
| 281 | ); ?></label> |
| 282 | </div> |
| 283 | <div class="input"> |
| 284 | <input type="text" name="<?php echo esc_attr($params['field_name']); ?>" style="width:100%;" placeholder="" value="<?php echo esc_attr($params['field_value']); ?>"/> |
| 285 | </div> |
| 286 | </div> |
| 287 | <?php |
| 288 | break; |
| 289 | |
| 290 | case 'file': |
| 291 | ?> |
| 292 | <div class="input"> |
| 293 | <div class="input" style="margin: 0px;"> |
| 294 | <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 esc_attr(sanitize_title($params['field_name'])); ?>_yes" <?php echo ("yes" == $params['download_image']) ? 'checked="checked"' : '';?>/> |
| 295 | <label for="<?php echo esc_attr(sanitize_title($params['field_name'])); ?>_yes"><?php esc_html_e('Download file hosted elsewhere', 'wp-all-import'); ?></label> |
| 296 | <a href="#help" class="wpallimport-help" title="<?php esc_attr_e('http:// or https://', 'wp-all-import') ?>" style="position: relative; top: -2px;">?</a> |
| 297 | </div> |
| 298 | <div class="input" style="margin: 0px;"> |
| 299 | <?php $wp_uploads = wp_upload_dir(); ?> |
| 300 | <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 esc_attr(sanitize_title($params['field_name'])); ?>_no" <?php echo ("yes" != $params['download_image']) ? 'checked="checked"' : '';?>/> |
| 301 | <label for="<?php echo esc_attr(sanitize_title($params['field_name'])); ?>_no"><?php |
| 302 | printf( |
| 303 | /* translators: %s: uploads directory path */ |
| 304 | esc_html__('Use file(s) currently uploaded in %s', 'wp-all-import'), |
| 305 | esc_url($wp_uploads['basedir'] . DIRECTORY_SEPARATOR . PMXI_Plugin::FILES_DIRECTORY . DIRECTORY_SEPARATOR) |
| 306 | ); ?></label> |
| 307 | </div> |
| 308 | <div class="input"> |
| 309 | <input type="text" name="<?php echo esc_attr($params['field_name']); ?>" style="width:100%;" placeholder="" value="<?php echo esc_attr($params['field_value']); ?>"/> |
| 310 | </div> |
| 311 | </div> |
| 312 | <?php |
| 313 | break; |
| 314 | |
| 315 | case 'accordion': |
| 316 | |
| 317 | $is_full_width = true; |
| 318 | if ( ! empty($params['sub_fields']) ){ |
| 319 | foreach ($params['sub_fields'] as $sub_field) { |
| 320 | if (!empty($sub_field[0]['params']['is_main_field'])){ |
| 321 | PMXI_API::add_field($sub_field[0]['type'], $sub_field[0]['label'], $sub_field[0]['params']); |
| 322 | $is_full_width = false; |
| 323 | break; |
| 324 | } |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | $in_the_bottom = $params['in_the_bottom']; |
| 329 | |
| 330 | $styles = ($is_full_width and $in_the_bottom) ? 'wpallimport-full-with-bottom' : ''; |
| 331 | |
| 332 | if ( ! $in_the_bottom and $is_full_width ) $styles = 'wpallimport-full-with-not-bottom'; |
| 333 | |
| 334 | ?> |
| 335 | <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); ?>"> |
| 336 | <div class="wpallimport-content-section <?php echo (($is_full_width and !$in_the_bottom) ? 'rad4' : 'wpallimport-bottom-radius');?>"> |
| 337 | <div class="wpallimport-collapsed-header"> |
| 338 | <h3 style="color:#40acad;"><?php echo wp_kses_post($label); ?></h3> |
| 339 | </div> |
| 340 | <div class="wpallimport-collapsed-content" style="padding: 0;"> |
| 341 | <div class="wpallimport-collapsed-content-inner"> |
| 342 | |
| 343 | <?php |
| 344 | if ( ! empty($params['sub_fields']) ){ |
| 345 | foreach ($params['sub_fields'] as $sub_field) { |
| 346 | if ( empty($sub_field[0]['params']['is_main_field']) ) { |
| 347 | PMXI_API::add_field($sub_field[0]['type'], $sub_field[0]['label'], $sub_field[0]['params']); |
| 348 | } |
| 349 | } |
| 350 | } |
| 351 | ?> |
| 352 | |
| 353 | </div> |
| 354 | </div> |
| 355 | </div> |
| 356 | </div> |
| 357 | <?php |
| 358 | break; |
| 359 | } |
| 360 | if ( ! $params['in_the_bottom'] ){ |
| 361 | ?> |
| 362 | </div> |
| 363 | <?php |
| 364 | } |
| 365 | echo ob_get_clean(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Buffered HTML is escaped inline above. |
| 366 | } |
| 367 | |
| 368 | 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'){ |
| 369 | |
| 370 | include( WP_ALL_IMPORT_ROOT_DIR . '/views/admin/import/template/_featured_template.php' ); |
| 371 | |
| 372 | } |
| 373 | |
| 374 | 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) { |
| 375 | |
| 376 | if (empty($img_url)) return false; |
| 377 | |
| 378 | $bn = wp_all_import_sanitize_filename(urldecode(basename($img_url))); |
| 379 | |
| 380 | if ($image_name == ""){ |
| 381 | $img_ext = pmxi_getExtensionFromStr($img_url); |
| 382 | $default_extension = pmxi_getExtension($bn); |
| 383 | if ($img_ext == "") $img_ext = pmxi_get_remote_image_ext($img_url); |
| 384 | // generate local file name |
| 385 | // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound |
| 386 | $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); |
| 387 | } |
| 388 | |
| 389 | $current_xml_node = false; |
| 390 | if (!empty($importData['current_xml_node'])) { |
| 391 | $current_xml_node = $importData['current_xml_node']; |
| 392 | } |
| 393 | $import_id = false; |
| 394 | if (!empty($importData['import'])) { |
| 395 | $import_id = $importData['import']->id; |
| 396 | } |
| 397 | |
| 398 | $uploads = wp_upload_dir(); |
| 399 | // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound |
| 400 | $uploads = apply_filters('wp_all_import_images_uploads_dir', $uploads, $articleData, $current_xml_node, $import_id); |
| 401 | |
| 402 | $targetDir = $uploads['path']; |
| 403 | $targetUrl = $uploads['url']; |
| 404 | $download_image = true; |
| 405 | $result = false; |
| 406 | $wp_filetype = false; |
| 407 | $attch = false; |
| 408 | |
| 409 | if ( $check_existing ) { |
| 410 | // Trying to find existing image in hash table. |
| 411 | if ("yes" == $download_images ) { |
| 412 | /* translators: see placeholders in the string below */ |
| 413 | $logger and call_user_func($logger, sprintf(__('- Searching for existing image `%s` by URL...', 'wp-all-import'), rawurldecode($img_url))); |
| 414 | $imageList = new PMXI_Image_List(); |
| 415 | $attch = $imageList->getExistingImageByUrl($img_url); |
| 416 | if ($attch) { |
| 417 | /* translators: see placeholders in the string below */ |
| 418 | $logger and call_user_func($logger, sprintf(__('Existing image was found by URL `%s`...', 'wp-all-import'), $img_url)); |
| 419 | // Attach media to current post if it's currently unattached. |
| 420 | if (empty($attch->post_parent)) { |
| 421 | wp_update_post( |
| 422 | array( |
| 423 | 'ID' => $attch->ID, |
| 424 | 'post_parent' => $pid |
| 425 | ) |
| 426 | ); |
| 427 | } |
| 428 | return $attch->ID; |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | if (empty($attch)) { |
| 433 | /* translators: 1: image URL, 2: image filename */ |
| 434 | $logger and call_user_func($logger, sprintf(__('- Searching for existing image `%1$s` by `_wp_attached_file` `%2$s`...', 'wp-all-import'), $img_url, $image_name)); |
| 435 | $attch = wp_all_import_get_image_from_gallery($image_name, $targetDir, $file_type); |
| 436 | } |
| 437 | |
| 438 | if (!empty($attch)) { |
| 439 | $logger and call_user_func($logger, sprintf(__('- Existing image was found by `_wp_attached_file` ...', 'wp-all-import'), $img_url)); |
| 440 | $imageRecord = new PMXI_Image_Record(); |
| 441 | $imageRecord->getBy(array( |
| 442 | 'attachment_id' => $attch->ID |
| 443 | )); |
| 444 | $imageRecord->isEmpty() and $imageRecord->set(array( |
| 445 | 'attachment_id' => $attch->ID, |
| 446 | 'image_url' => $img_url, |
| 447 | 'image_filename' => $image_name |
| 448 | ))->insert(); |
| 449 | // Attach media to current post if it's currently unattached. |
| 450 | if (empty($attch->post_parent)) { |
| 451 | wp_update_post( |
| 452 | array( |
| 453 | 'ID' => $attch->ID, |
| 454 | 'post_parent' => $pid |
| 455 | ) |
| 456 | ); |
| 457 | } |
| 458 | return $attch->ID; |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | $image_filename = wp_unique_filename($targetDir, $image_name); |
| 463 | $image_filepath = $targetDir . '/' . $image_filename; |
| 464 | |
| 465 | $url = str_replace(" ", "%20", trim($img_url)); |
| 466 | |
| 467 | // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound |
| 468 | $is_base64_images_allowed = apply_filters("wp_all_import_is_base64_images_allowed", true, $url, false); |
| 469 | |
| 470 | if ( $file_type == 'images' && base64_encode(base64_decode($url)) == $url && $is_base64_images_allowed ) { |
| 471 | $image_name = md5($url) . '.jpg'; |
| 472 | // Search existing attachment. |
| 473 | $attch = wp_all_import_get_image_from_gallery($image_name, $targetDir, $file_type); |
| 474 | if (empty($attch)) { |
| 475 | /* translators: see placeholders in the string below */ |
| 476 | $logger and call_user_func($logger, sprintf(__('- <b>WARNING</b>: Image %s not found in media gallery.', 'wp-all-import'), trim($image_name))); |
| 477 | } else { |
| 478 | /* translators: see placeholders in the string below */ |
| 479 | $logger and call_user_func($logger, sprintf(__('- Using existing image `%s`...', 'wp-all-import'), trim($image_name))); |
| 480 | // Attach media to current post if it's currently unattached. |
| 481 | if (empty($attch->post_parent)) { |
| 482 | wp_update_post( |
| 483 | array( |
| 484 | 'ID' => $attch->ID, |
| 485 | 'post_parent' => $pid |
| 486 | ) |
| 487 | ); |
| 488 | } |
| 489 | return $attch->ID; |
| 490 | } |
| 491 | |
| 492 | if ("yes" == $download_images) { |
| 493 | $img = @imagecreatefromstring(base64_decode($url)); |
| 494 | if ($img) { |
| 495 | $image_filename = $image_name; |
| 496 | $logger and call_user_func($logger, __('- found base64_encoded image', 'wp-all-import')); |
| 497 | $image_filepath = $targetDir . '/' . $image_filename; |
| 498 | imagejpeg($img, $image_filepath); |
| 499 | if ( ! ($image_info = apply_filters('pmxi_getimagesize', @getimagesize($image_filepath), $image_filepath)) or ! in_array($image_info[2], wp_all_import_supported_image_types())) { |
| 500 | /* translators: see placeholders in the string below */ |
| 501 | $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'), $image_filepath)); |
| 502 | } else { |
| 503 | $result = true; |
| 504 | $download_image = false; |
| 505 | } |
| 506 | } |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | // Do not download images. |
| 511 | if ( "yes" != $download_images ) { |
| 512 | |
| 513 | $image_filename = wp_unique_filename($targetDir, basename($image_name)); |
| 514 | $image_filepath = $targetDir . '/' . basename($image_filename); |
| 515 | |
| 516 | $wpai_uploads = $uploads['basedir'] . DIRECTORY_SEPARATOR . PMXI_Plugin::FILES_DIRECTORY . DIRECTORY_SEPARATOR; |
| 517 | $wpai_image_path = $wpai_uploads . str_replace('%20', ' ', $url); |
| 518 | |
| 519 | /* translators: 1: image path, 2: uploads folder path */ |
| 520 | $logger and call_user_func($logger, sprintf(__('- Searching for existing image `%1$s` in `%2$s` folder', 'wp-all-import'), $wpai_image_path, $wpai_uploads)); |
| 521 | |
| 522 | if ( @file_exists($wpai_image_path) and @copy( $wpai_image_path, $image_filepath )){ |
| 523 | $download_image = false; |
| 524 | // Validate import attachments. |
| 525 | if ($file_type == 'files') { |
| 526 | if ( ! $wp_filetype = wp_check_filetype(basename($image_filepath), null )) { |
| 527 | /* translators: see placeholders in the string below */ |
| 528 | $logger and call_user_func($logger, sprintf(__('- <b>WARNING</b>: Can\'t detect attachment file type %s', 'wp-all-import'), trim($image_filepath))); |
| 529 | wp_delete_file($image_filepath); |
| 530 | } |
| 531 | else { |
| 532 | $result = true; |
| 533 | /* translators: see placeholders in the string below */ |
| 534 | $logger and call_user_func($logger, sprintf(__('- File `%s` has been successfully found', 'wp-all-import'), $wpai_image_path)); |
| 535 | } |
| 536 | } |
| 537 | // Validate import images. |
| 538 | elseif ($file_type == 'images') { |
| 539 | 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())) { |
| 540 | /* translators: see placeholders in the string below */ |
| 541 | $logger and call_user_func($logger, sprintf(__('- Image `%s` has been successfully found', 'wp-all-import'), $wpai_image_path)); |
| 542 | $result = true; |
| 543 | } else { |
| 544 | /* translators: see placeholders in the string below */ |
| 545 | $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'), $image_filepath)); |
| 546 | wp_delete_file($image_filepath); |
| 547 | } |
| 548 | } |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | if ( $download_image ) { |
| 553 | |
| 554 | if ($file_type == 'images') { |
| 555 | /* translators: see placeholders in the string below */ |
| 556 | $logger and call_user_func($logger, sprintf(__('- Downloading image from `%s`', 'wp-all-import'), $url)); |
| 557 | } elseif ($file_type == 'files') { |
| 558 | /* translators: see placeholders in the string below */ |
| 559 | $logger and call_user_func($logger, sprintf(__('- Downloading file from `%s`', 'wp-all-import'), $url)); |
| 560 | } |
| 561 | |
| 562 | if ( ! preg_match('%^(http|ftp)s?://%i', $url) ) { |
| 563 | $logger and call_user_func($logger, /* translators: 1: source URL, 2: local destination path */ sprintf(__('- <b>WARNING</b>: File %1$s cannot be saved locally as %2$s', 'wp-all-import'), $url, $image_filepath)); |
| 564 | return false; |
| 565 | } |
| 566 | |
| 567 | $request = get_file_curl($url, $image_filepath); |
| 568 | |
| 569 | if ( (is_wp_error($request) or $request === false) and ! @file_put_contents($image_filepath, @file_get_contents($url))) { |
| 570 | wp_delete_file($image_filepath); // delete file since failed upload may result in empty file created |
| 571 | } else { |
| 572 | |
| 573 | if ($file_type == 'images') { |
| 574 | 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())) { |
| 575 | $result = true; |
| 576 | /* translators: see placeholders in the string below */ |
| 577 | $logger and call_user_func($logger, sprintf(__('- Image `%s` has been successfully downloaded', 'wp-all-import'), $url)); |
| 578 | } |
| 579 | } |
| 580 | elseif ($file_type == 'files'){ |
| 581 | if ( $wp_filetype = wp_check_filetype(basename($image_filepath), null )) { |
| 582 | $result = true; |
| 583 | /* translators: see placeholders in the string below */ |
| 584 | $logger and call_user_func($logger, sprintf(__('- File `%s` has been successfully downloaded', 'wp-all-import'), $url)); |
| 585 | } |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | if ( ! $result ) { |
| 590 | |
| 591 | $request = get_file_curl($url, $image_filepath); |
| 592 | |
| 593 | if ( (is_wp_error($request) or $request === false) and ! @file_put_contents($image_filepath, @file_get_contents($url))) { |
| 594 | $logger and call_user_func($logger, /* translators: 1: source URL, 2: local destination path */ sprintf(__('- <b>WARNING</b>: File %1$s cannot be saved locally as %2$s', 'wp-all-import'), $url, $image_filepath)); |
| 595 | wp_delete_file($image_filepath); // delete file since failed upload may result in empty file created |
| 596 | } else { |
| 597 | |
| 598 | if ($file_type == 'images') { |
| 599 | 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())) { |
| 600 | $result = true; |
| 601 | /* translators: see placeholders in the string below */ |
| 602 | $logger and call_user_func($logger, sprintf(__('- Image `%s` has been successfully downloaded', 'wp-all-import'), $url)); |
| 603 | } else { |
| 604 | /* translators: see placeholders in the string below */ |
| 605 | $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'), $url)); |
| 606 | wp_delete_file($image_filepath); |
| 607 | } |
| 608 | } |
| 609 | elseif ($file_type == 'files') { |
| 610 | if ( ! $wp_filetype = wp_check_filetype(basename($image_filepath), null )) { |
| 611 | /* translators: see placeholders in the string below */ |
| 612 | $logger and call_user_func($logger, sprintf(__('- <b>WARNING</b>: Can\'t detect attachment file type %s', 'wp-all-import'), trim($url))); |
| 613 | wp_delete_file($image_filepath); |
| 614 | } else { |
| 615 | $result = true; |
| 616 | /* translators: see placeholders in the string below */ |
| 617 | $logger and call_user_func($logger, sprintf(__('- File `%s` has been successfully found', 'wp-all-import'), $url)); |
| 618 | } |
| 619 | } |
| 620 | } |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | if ( $create_image && $result ) { |
| 625 | |
| 626 | // you must first include the image.php file |
| 627 | // for the function wp_generate_attachment_metadata() to work |
| 628 | require_once(ABSPATH . 'wp-admin/includes/image.php'); |
| 629 | |
| 630 | if ($file_type == 'images') { |
| 631 | /* translators: see placeholders in the string below */ |
| 632 | $logger and call_user_func($logger, sprintf(__('- Creating an attachment for image `%s`', 'wp-all-import'), $targetUrl . '/' . basename($image_filename))); |
| 633 | } else { |
| 634 | /* translators: see placeholders in the string below */ |
| 635 | $logger and call_user_func($logger, sprintf(__('- Creating an attachment for file `%s`', 'wp-all-import'), $targetUrl . '/' . basename($image_filename))); |
| 636 | } |
| 637 | |
| 638 | $file_mime_type = empty($wp_filetype['type']) ? '' : $wp_filetype['type']; |
| 639 | if ($file_type == 'images' && !empty($image_info)) { |
| 640 | $file_mime_type = image_type_to_mime_type($image_info[2]); |
| 641 | } |
| 642 | // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound |
| 643 | $file_mime_type = apply_filters('wp_all_import_image_mime_type', $file_mime_type, $image_filepath); |
| 644 | $attachment = [ |
| 645 | 'post_mime_type' => $file_mime_type, |
| 646 | 'guid' => $targetUrl . '/' . basename($image_filename), |
| 647 | 'post_title' => basename($image_filename), |
| 648 | 'post_content' => '', |
| 649 | ]; |
| 650 | if (!empty($articleData['post_author'])) { |
| 651 | $attachment['post_author'] = $articleData['post_author']; |
| 652 | } |
| 653 | if ($file_type == 'images' and ($image_meta = wp_read_image_metadata($image_filepath))) { |
| 654 | if (trim($image_meta['title']) && ! is_numeric(sanitize_title($image_meta['title']))) |
| 655 | $attachment['post_title'] = $image_meta['title']; |
| 656 | if (trim($image_meta['caption'])) |
| 657 | $attachment['post_content'] = $image_meta['caption']; |
| 658 | } |
| 659 | remove_all_actions('add_attachment'); |
| 660 | $attid = wp_insert_attachment($attachment, $image_filepath, $pid); |
| 661 | |
| 662 | if (is_wp_error($attid)) { |
| 663 | $logger and call_user_func($logger, __('- <b>WARNING</b>', 'wp-all-import') . ': ' . $attid->get_error_message()); |
| 664 | return false; |
| 665 | } else { |
| 666 | /** Fires once an attachment has been added. */ |
| 667 | // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound |
| 668 | do_action( 'wp_all_import_add_attachment', $attid ); |
| 669 | wp_update_attachment_metadata($attid, wp_generate_attachment_metadata($attid, $image_filepath)); |
| 670 | $imageRecord = new PMXI_Image_Record(); |
| 671 | $imageRecord->getBy(array( |
| 672 | 'attachment_id' => $attid |
| 673 | )); |
| 674 | $imageRecord->isEmpty() and $imageRecord->set(array( |
| 675 | 'attachment_id' => $attid, |
| 676 | 'image_url' => $img_url, |
| 677 | 'image_filename' => $image_filename |
| 678 | ))->insert(); |
| 679 | /* translators: see placeholders in the string below */ |
| 680 | $logger and call_user_func($logger, sprintf(__('- Attachment has been successfully created for image `%s`', 'wp-all-import'), $targetUrl . '/' . basename($image_filename))); |
| 681 | return $attid; |
| 682 | } |
| 683 | } |
| 684 | |
| 685 | return $result; |
| 686 | } |
| 687 | } |
| 688 |