| @@ -33,9 +33,12 @@ | ||
| 33 | 33 | */ |
| 34 | 34 | public function prepare( $template_json, $template_settings, $extra_content = [], $request_params = [] ) { |
| 35 | 35 | $this->template_settings = $template_settings; |
| 36 | 36 | $this->post_id = $this->map_post_ids[ $template_settings['post_id'] ]; |
| 37 | - $this->wp_importer = new WPImport( null, ['fetch_attachments' => true] ); | |
| 37 | + $this->wp_importer = new WPImport( null, [ | |
| 38 | + 'session_id' => $this->session_id, | |
| 39 | + 'fetch_attachments' => true, | |
| 40 | + ] ); | |
| 38 | 41 | |
| 39 | 42 | $parsed_blocks = parse_blocks( $template_json['content'] ); |
| 40 | 43 | if ( ! empty( $extra_content ) ) { |
| 41 | 44 | $this->prepare_form_data( $extra_content, $template_settings ); |
| @@ -42,13 +45,18 @@ | ||
| 42 | 45 | } |
| 43 | 46 | if ( isset($this->template_settings['__attachments']) ) { |
| 44 | 47 | $this->process_images(); |
| 45 | 48 | // Make sure we do the longest urls first, in case one is a substring of another. |
| 49 | + } | |
| 50 | + if(!empty($this->wp_importer->url_remap)){ | |
| 46 | 51 | uksort( $this->wp_importer->url_remap, function ( $a, $b ) { |
| 47 | 52 | // Return the difference in length between two strings. |
| 48 | 53 | return strlen( $b ) - strlen( $a ); |
| 49 | 54 | } ); |
| 50 | 55 | } |
| 56 | + if(!empty($template_settings["page_settings"]["fluent_cart_store_settings"])){ | |
| 57 | + $this->replace_fluent_cart_store_settings($template_settings["page_settings"]["fluent_cart_store_settings"]); | |
| 58 | + } | |
| 51 | 59 | $this->replace( $parsed_blocks, $request_params ); |
| 52 | 60 | $this->content = wp_slash( serialize_blocks( $parsed_blocks ) ); |
| 53 | 61 | |
| 54 | 62 | return $this; |
| @@ -127,8 +135,35 @@ | ||
| 127 | 135 | } |
| 128 | 136 | } |
| 129 | 137 | } |
| 130 | 138 | |
| 139 | + private function replace_fluent_cart_store_settings($settings) { | |
| 140 | + $map_post_ids = $this->map_post_ids; | |
| 141 | + $saved_options = $settings; | |
| 142 | + $mapped_keys = [ | |
| 143 | + 'checkout_page_id', | |
| 144 | + 'custom_payment_page_id', | |
| 145 | + 'registration_page_id', | |
| 146 | + 'login_page_id', | |
| 147 | + 'cart_page_id', | |
| 148 | + 'receipt_page_id', | |
| 149 | + 'shop_page_id', | |
| 150 | + 'customer_profile_page_id', | |
| 151 | + ]; | |
| 152 | + | |
| 153 | + foreach ($mapped_keys as $key) { | |
| 154 | + $old_id = $saved_options[$key] ?? null; | |
| 155 | + if (!empty($old_id) && isset($map_post_ids[$old_id])) { | |
| 156 | + $saved_options[$key] = $map_post_ids[$old_id]; | |
| 157 | + } | |
| 158 | + else if(isset($saved_options[$key])){ | |
| 159 | + $saved_options[$key] = 0; | |
| 160 | + } | |
| 161 | + } | |
| 162 | + | |
| 163 | + update_option('fluent_cart_store_settings', $saved_options); | |
| 164 | + } | |
| 165 | + | |
| 131 | 166 | private function replace_archive_id( $menu_id ) { |
| 132 | 167 | if ( ! empty( $this->imported_data['archive_settings'] ) ) { |
| 133 | 168 | $post = get_post( $menu_id ); |
| 134 | 169 | $blocks = parse_blocks( $post->post_content ); |
| @@ -351,8 +386,27 @@ | ||
| 351 | 386 | if ($post_data === null) { |
| 352 | 387 | continue; |
| 353 | 388 | } |
| 354 | 389 | |
| 390 | + foreach ($sizes as $key => $size) { | |
| 391 | + if ($base_url == $size) { | |
| 392 | + unset($sizes[$key]); | |
| 393 | + continue; | |
| 394 | + } | |
| 395 | + | |
| 396 | + // $size url string; get width height from the image url | |
| 397 | + preg_match('/-(\d+)x(\d+)\.[a-zA-Z]+$/', $size, $matches); | |
| 398 | + if (isset($matches[1]) && isset($matches[2])) { | |
| 399 | + $sizes[$key] = [ | |
| 400 | + 'file' => basename($size), | |
| 401 | + 'width' => $matches[1], | |
| 402 | + 'height' => $matches[2] | |
| 403 | + ]; | |
| 404 | + } else { | |
| 405 | + unset($sizes[$key]); | |
| 406 | + } | |
| 407 | + } | |
| 408 | + | |
| 355 | 409 | // Check if the attachment already exists |
| 356 | 410 | $attachment_id = $this->wp_importer->process_attachment($post_data, $base_url, $sizes); |
| 357 | 411 | // If there was an error, handle it here |
| 358 | 412 | if (is_wp_error($attachment_id)) { |
| @@ -364,31 +418,9 @@ | ||
| 364 | 418 | } |
| 365 | 419 | } |
| 366 | 420 | |
| 367 | 421 | private function prepare_post_data($post_id, $image_url) { |
| 368 | - $filetype = wp_check_filetype(basename($image_url)); | |
| 369 | - if (!$filetype['type']) { | |
| 370 | - $this->sse_log('prepare', 'Error: Unable to determine the file type.', -1, 'eventLog'); | |
| 371 | - return null; | |
| 372 | - } | |
| 373 | - | |
| 374 | - $post_data = array( | |
| 375 | - 'post_title' => basename($image_url), | |
| 376 | - 'post_content' => '', | |
| 377 | - 'post_status' => 'inherit', | |
| 378 | - 'post_mime_type' => $filetype['type'], | |
| 379 | - 'guid' => $image_url, | |
| 380 | - 'post_parent' => $post_id, // Set the parent post | |
| 381 | - ); | |
| 382 | - | |
| 383 | - if (preg_match('%wp-content/uploads/([0-9]{4}/[0-9]{2})%', $image_url, $matches)) { | |
| 384 | - $post_data['upload_date'] = $matches[1]; | |
| 385 | - } | |
| 386 | - else{ | |
| 387 | - $post_data['upload_date'] = date('Y/m'); | |
| 388 | - } | |
| 389 | - | |
| 390 | - return $post_data; | |
| 422 | + return Utils::prepare_post_data($image_url, $post_id, [$this, 'sse_log']); | |
| 391 | 423 | } |
| 392 | 424 | |
| 393 | 425 | public function sse_log( $type, $message, $progress = 1, $action = 'updateLog', $status = null ) { |
| 394 | 426 | if ($this->shouldLog) { |