| @@ -5,9 +5,9 @@ | ||
| 5 | 5 | use Templately\Core\Importer\WPImport; |
| 6 | 6 | use Templately\Utils\Helper; |
| 7 | 7 | |
| 8 | 8 | class GutenbergHelper extends ImportHelper { |
| 9 | - const IMAGE_URL_PATTERN = '/(https?:\/\/[^\s<>"\']+\.(jpg|jpeg|gif|png|svg|webp|ico|tiff|tif)(?:\?[^\s<>"\']*)?)/i'; | |
| 9 | + const IMAGE_URL_PATTERN = '/(http(s?):)([\/|.|\w|\s|-])*\.(?:jpg|gif|png)/'; | |
| 10 | 10 | |
| 11 | 11 | private $template_settings = []; |
| 12 | 12 | private $forms = []; |
| 13 | 13 | /** |
| @@ -33,12 +33,9 @@ | ||
| 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, [ | |
| 38 | - 'session_id' => $this->session_id, | |
| 39 | - 'fetch_attachments' => true, | |
| 40 | - ] ); | |
| 37 | + $this->wp_importer = new WPImport( null, ['fetch_attachments' => true] ); | |
| 41 | 38 | |
| 42 | 39 | $parsed_blocks = parse_blocks( $template_json['content'] ); |
| 43 | 40 | if ( ! empty( $extra_content ) ) { |
| 44 | 41 | $this->prepare_form_data( $extra_content, $template_settings ); |
| @@ -45,18 +42,13 @@ | ||
| 45 | 42 | } |
| 46 | 43 | if ( isset($this->template_settings['__attachments']) ) { |
| 47 | 44 | $this->process_images(); |
| 48 | 45 | // 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)){ | |
| 51 | 46 | uksort( $this->wp_importer->url_remap, function ( $a, $b ) { |
| 52 | 47 | // Return the difference in length between two strings. |
| 53 | 48 | return strlen( $b ) - strlen( $a ); |
| 54 | 49 | } ); |
| 55 | 50 | } |
| 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 | - } | |
| 59 | 51 | $this->replace( $parsed_blocks, $request_params ); |
| 60 | 52 | $this->content = wp_slash( serialize_blocks( $parsed_blocks ) ); |
| 61 | 53 | |
| 62 | 54 | return $this; |
| @@ -119,26 +111,17 @@ | ||
| 119 | 111 | $block["attrs"]["widthRange"] = $request_params['logo_size']; |
| 120 | 112 | unset($block["attrs"]["widthUnit"]); |
| 121 | 113 | if (isset($block['attrs']['blockMeta']['desktop']) && is_string($block['attrs']['blockMeta']['desktop'])) { |
| 122 | 114 | $meta = $block['attrs']['blockMeta']['desktop']; |
| 123 | - $size = $request_params['logo_size']; | |
| 115 | + $regex = '/\.image-wrapper\s*{\s*width:\s*(\d+)px;/'; | |
| 124 | 116 | |
| 125 | - // Update width inside .image-wrapper { ... } (old EB format) | |
| 126 | - // and .image-wrapper img { ... } (new EB format) — both must be handled | |
| 127 | - $meta = preg_replace( | |
| 128 | - '/(\.image-wrapper(?:\s+img)?\s*\{[^}]*)width\s*:\s*\d+px\s*;/', | |
| 129 | - '${1}width:' . $size . 'px;', | |
| 130 | - $meta | |
| 131 | - ); | |
| 117 | + if (preg_match($regex, $meta, $matches)) { | |
| 118 | + $replaced = preg_replace($regex, '.image-wrapper { width:' . $request_params['logo_size'] . 'px;', $meta); | |
| 132 | 119 | |
| 133 | - // Update width inside .eb-image-wrapper-inner { ... } | |
| 134 | - $meta = preg_replace( | |
| 135 | - '/(\.eb-image-wrapper-inner\s*\{[^}]*)width\s*:\s*\d+px\s*;/', | |
| 136 | - '${1}width:' . $size . 'px;', | |
| 137 | - $meta | |
| 138 | - ); | |
| 139 | - | |
| 140 | - $block['attrs']['blockMeta']['desktop'] = $meta; | |
| 120 | + if ($replaced) { | |
| 121 | + $block['attrs']['blockMeta']['desktop'] = $replaced; | |
| 122 | + } | |
| 123 | + } | |
| 141 | 124 | } |
| 142 | 125 | |
| 143 | 126 | $this->sse_log('prepare', 'Updated Site Logo', 1, 'eventLog'); |
| 144 | 127 | } |
| @@ -144,35 +127,8 @@ | ||
| 144 | 127 | } |
| 145 | 128 | } |
| 146 | 129 | } |
| 147 | 130 | |
| 148 | - private function replace_fluent_cart_store_settings($settings) { | |
| 149 | - $map_post_ids = $this->map_post_ids; | |
| 150 | - $saved_options = $settings; | |
| 151 | - $mapped_keys = [ | |
| 152 | - 'checkout_page_id', | |
| 153 | - 'custom_payment_page_id', | |
| 154 | - 'registration_page_id', | |
| 155 | - 'login_page_id', | |
| 156 | - 'cart_page_id', | |
| 157 | - 'receipt_page_id', | |
| 158 | - 'shop_page_id', | |
| 159 | - 'customer_profile_page_id', | |
| 160 | - ]; | |
| 161 | - | |
| 162 | - foreach ($mapped_keys as $key) { | |
| 163 | - $old_id = $saved_options[$key] ?? null; | |
| 164 | - if (!empty($old_id) && isset($map_post_ids[$old_id])) { | |
| 165 | - $saved_options[$key] = $map_post_ids[$old_id]; | |
| 166 | - } | |
| 167 | - else if(isset($saved_options[$key])){ | |
| 168 | - $saved_options[$key] = 0; | |
| 169 | - } | |
| 170 | - } | |
| 171 | - | |
| 172 | - update_option('fluent_cart_store_settings', $saved_options); | |
| 173 | - } | |
| 174 | - | |
| 175 | 131 | private function replace_archive_id( $menu_id ) { |
| 176 | 132 | if ( ! empty( $this->imported_data['archive_settings'] ) ) { |
| 177 | 133 | $post = get_post( $menu_id ); |
| 178 | 134 | $blocks = parse_blocks( $post->post_content ); |
| @@ -179,19 +135,12 @@ | ||
| 179 | 135 | $changed = false; |
| 180 | 136 | foreach ( $blocks as &$block ) { |
| 181 | 137 | $this->sse_log('query', 'Finalizing archive settings, just a moment...', 1, 'eventLog'); |
| 182 | 138 | |
| 183 | - if (isset($block['attrs']['id'])) { | |
| 184 | - $blockId = $block['attrs']['id']; | |
| 185 | - $archiveSettings = $this->imported_data['archive_settings']; | |
| 186 | - | |
| 187 | - $archiveIds = $archiveSettings[$blockId] ?? false; | |
| 188 | - | |
| 189 | - if ($archiveIds) { | |
| 190 | - $block['attrs']['id'] = $archiveIds['page_id']; | |
| 191 | - $block['attrs']['url'] = get_the_permalink($archiveIds['page_id']); | |
| 192 | - $changed = true; | |
| 193 | - } | |
| 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'] ); | |
| 194 | 143 | } |
| 195 | 144 | } |
| 196 | 145 | if ( $changed ) { |
| 197 | 146 | wp_update_post( [ |
| @@ -395,27 +344,8 @@ | ||
| 395 | 344 | if ($post_data === null) { |
| 396 | 345 | continue; |
| 397 | 346 | } |
| 398 | 347 | |
| 399 | - foreach ($sizes as $key => $size) { | |
| 400 | - if ($base_url == $size) { | |
| 401 | - unset($sizes[$key]); | |
| 402 | - continue; | |
| 403 | - } | |
| 404 | - | |
| 405 | - // $size url string; get width height from the image url | |
| 406 | - preg_match('/-(\d+)x(\d+)\.[a-zA-Z]+$/', $size, $matches); | |
| 407 | - if (isset($matches[1]) && isset($matches[2])) { | |
| 408 | - $sizes[$key] = [ | |
| 409 | - 'file' => basename($size), | |
| 410 | - 'width' => $matches[1], | |
| 411 | - 'height' => $matches[2] | |
| 412 | - ]; | |
| 413 | - } else { | |
| 414 | - unset($sizes[$key]); | |
| 415 | - } | |
| 416 | - } | |
| 417 | - | |
| 418 | 348 | // Check if the attachment already exists |
| 419 | 349 | $attachment_id = $this->wp_importer->process_attachment($post_data, $base_url, $sizes); |
| 420 | 350 | // If there was an error, handle it here |
| 421 | 351 | if (is_wp_error($attachment_id)) { |
| @@ -427,9 +357,31 @@ | ||
| 427 | 357 | } |
| 428 | 358 | } |
| 429 | 359 | |
| 430 | 360 | private function prepare_post_data($post_id, $image_url) { |
| 431 | - return Utils::prepare_post_data($image_url, $post_id, [$this, 'sse_log']); | |
| 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; | |
| 432 | 384 | } |
| 433 | 385 | |
| 434 | 386 | public function sse_log( $type, $message, $progress = 1, $action = 'updateLog', $status = null ) { |
| 435 | 387 | if ($this->shouldLog) { |