| @@ -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 ); |
| @@ -106,9 +109,9 @@ | ||
| 106 | 109 | |
| 107 | 110 | if ( ! empty( $block['innerBlocks'] ) ) { |
| 108 | 111 | $this->replace( $block['innerBlocks'], $request_params ); |
| 109 | 112 | } |
| 110 | - else if(!empty($request_params['logo']) && isset($request_params['logo_size']) && $request_params['logo_size'] && $block['blockName'] === "essential-blocks/advanced-image" && $block["attrs"]["imgSource"] === "site-logo"){ | |
| 113 | + else if(!empty($request_params['logo']['id']) && isset($request_params['logo_size']) && $request_params['logo_size'] && $block['blockName'] === "essential-blocks/advanced-image" && $block["attrs"]["imgSource"] === "site-logo"){ | |
| 111 | 114 | $block["attrs"]["widthRange"] = $request_params['logo_size']; |
| 112 | 115 | unset($block["attrs"]["widthUnit"]); |
| 113 | 116 | if (isset($block['attrs']['blockMeta']['desktop']) && is_string($block['attrs']['blockMeta']['desktop'])) { |
| 114 | 117 | $meta = $block['attrs']['blockMeta']['desktop']; |
| @@ -135,12 +138,19 @@ | ||
| 135 | 138 | $changed = false; |
| 136 | 139 | foreach ( $blocks as &$block ) { |
| 137 | 140 | $this->sse_log('query', 'Finalizing archive settings, just a moment...', 1, 'eventLog'); |
| 138 | 141 | |
| 139 | - if ( isset( $block['attrs']['id'] ) && $block['attrs']['id'] === $this->imported_data['archive_settings']['archive_id'] ) { | |
| 140 | - $changed = true; | |
| 141 | - $block['attrs']['id'] = $this->imported_data['archive_settings']['page_id']; | |
| 142 | - $block['attrs']['url'] = get_the_permalink( $this->imported_data['archive_settings']['page_id'] ); | |
| 142 | + if (isset($block['attrs']['id'])) { | |
| 143 | + $blockId = $block['attrs']['id']; | |
| 144 | + $archiveSettings = $this->imported_data['archive_settings']; | |
| 145 | + | |
| 146 | + $archiveIds = $archiveSettings[$blockId] ?? false; | |
| 147 | + | |
| 148 | + if ($archiveIds) { | |
| 149 | + $block['attrs']['id'] = $archiveIds['page_id']; | |
| 150 | + $block['attrs']['url'] = get_the_permalink($archiveIds['page_id']); | |
| 151 | + $changed = true; | |
| 152 | + } | |
| 143 | 153 | } |
| 144 | 154 | } |
| 145 | 155 | if ( $changed ) { |
| 146 | 156 | wp_update_post( [ |
| @@ -344,8 +354,27 @@ | ||
| 344 | 354 | if ($post_data === null) { |
| 345 | 355 | continue; |
| 346 | 356 | } |
| 347 | 357 | |
| 358 | + foreach ($sizes as $key => $size) { | |
| 359 | + if ($base_url == $size) { | |
| 360 | + unset($sizes[$key]); | |
| 361 | + continue; | |
| 362 | + } | |
| 363 | + | |
| 364 | + // $size url string; get width height from the image url | |
| 365 | + preg_match('/-(\d+)x(\d+)\.[a-zA-Z]+$/', $size, $matches); | |
| 366 | + if (isset($matches[1]) && isset($matches[2])) { | |
| 367 | + $sizes[$key] = [ | |
| 368 | + 'file' => basename($size), | |
| 369 | + 'width' => $matches[1], | |
| 370 | + 'height' => $matches[2] | |
| 371 | + ]; | |
| 372 | + } else { | |
| 373 | + unset($sizes[$key]); | |
| 374 | + } | |
| 375 | + } | |
| 376 | + | |
| 348 | 377 | // Check if the attachment already exists |
| 349 | 378 | $attachment_id = $this->wp_importer->process_attachment($post_data, $base_url, $sizes); |
| 350 | 379 | // If there was an error, handle it here |
| 351 | 380 | if (is_wp_error($attachment_id)) { |
| @@ -357,31 +386,9 @@ | ||
| 357 | 386 | } |
| 358 | 387 | } |
| 359 | 388 | |
| 360 | 389 | private function prepare_post_data($post_id, $image_url) { |
| 361 | - $filetype = wp_check_filetype(basename($image_url)); | |
| 362 | - if (!$filetype['type']) { | |
| 363 | - $this->sse_log('prepare', 'Error: Unable to determine the file type.', -1, 'eventLog'); | |
| 364 | - return null; | |
| 365 | - } | |
| 366 | - | |
| 367 | - $post_data = array( | |
| 368 | - 'post_title' => basename($image_url), | |
| 369 | - 'post_content' => '', | |
| 370 | - 'post_status' => 'inherit', | |
| 371 | - 'post_mime_type' => $filetype['type'], | |
| 372 | - 'guid' => $image_url, | |
| 373 | - 'post_parent' => $post_id, // Set the parent post | |
| 374 | - ); | |
| 375 | - | |
| 376 | - if (preg_match('%wp-content/uploads/([0-9]{4}/[0-9]{2})%', $image_url, $matches)) { | |
| 377 | - $post_data['upload_date'] = $matches[1]; | |
| 378 | - } | |
| 379 | - else{ | |
| 380 | - $post_data['upload_date'] = date('Y/m'); | |
| 381 | - } | |
| 382 | - | |
| 383 | - return $post_data; | |
| 390 | + return Utils::prepare_post_data($image_url, $post_id, [$this, 'sse_log']); | |
| 384 | 391 | } |
| 385 | 392 | |
| 386 | 393 | public function sse_log( $type, $message, $progress = 1, $action = 'updateLog', $status = null ) { |
| 387 | 394 | if ($this->shouldLog) { |