| @@ -5,13 +5,12 @@ | ||
| 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 | - private $attachment_options = []; | |
| 14 | 13 | /** |
| 15 | 14 | * @var WPImport |
| 16 | 15 | */ |
| 17 | 16 | private $wp_importer; |
| @@ -34,12 +33,12 @@ | ||
| 34 | 33 | */ |
| 35 | 34 | public function prepare( $template_json, $template_settings, $extra_content = [], $request_params = [] ) { |
| 36 | 35 | $this->template_settings = $template_settings; |
| 37 | 36 | $this->post_id = $this->map_post_ids[ $template_settings['post_id'] ]; |
| 38 | - $this->wp_importer = new WPImport( null, array_merge( [ | |
| 37 | + $this->wp_importer = new WPImport( null, [ | |
| 39 | 38 | 'session_id' => $this->session_id, |
| 40 | 39 | 'fetch_attachments' => true, |
| 41 | - ], $this->attachment_options ) ); | |
| 40 | + ] ); | |
| 42 | 41 | |
| 43 | 42 | $parsed_blocks = parse_blocks( $template_json['content'] ); |
| 44 | 43 | if ( ! empty( $extra_content ) ) { |
| 45 | 44 | $this->prepare_form_data( $extra_content, $template_settings ); |
| @@ -46,18 +45,13 @@ | ||
| 46 | 45 | } |
| 47 | 46 | if ( isset($this->template_settings['__attachments']) ) { |
| 48 | 47 | $this->process_images(); |
| 49 | 48 | // Make sure we do the longest urls first, in case one is a substring of another. |
| 50 | - } | |
| 51 | - if(!empty($this->wp_importer->url_remap)){ | |
| 52 | 49 | uksort( $this->wp_importer->url_remap, function ( $a, $b ) { |
| 53 | 50 | // Return the difference in length between two strings. |
| 54 | 51 | return strlen( $b ) - strlen( $a ); |
| 55 | 52 | } ); |
| 56 | 53 | } |
| 57 | - if(!empty($template_settings["page_settings"]["fluent_cart_store_settings"])){ | |
| 58 | - $this->replace_fluent_cart_store_settings($template_settings["page_settings"]["fluent_cart_store_settings"]); | |
| 59 | - } | |
| 60 | 54 | $this->replace( $parsed_blocks, $request_params ); |
| 61 | 55 | $this->content = wp_slash( serialize_blocks( $parsed_blocks ) ); |
| 62 | 56 | |
| 63 | 57 | return $this; |
| @@ -62,20 +56,8 @@ | ||
| 62 | 56 | |
| 63 | 57 | return $this; |
| 64 | 58 | } |
| 65 | 59 | |
| 66 | - /** | |
| 67 | - * Configure optional limits for best-effort attachment imports. | |
| 68 | - * | |
| 69 | - * @param array $options Attachment request timeout, retry count, and deadline. | |
| 70 | - * @return self | |
| 71 | - */ | |
| 72 | - public function set_attachment_options( array $options ): self { | |
| 73 | - $this->attachment_options = $options; | |
| 74 | - | |
| 75 | - return $this; | |
| 76 | - } | |
| 77 | - | |
| 78 | 60 | public function update() { |
| 79 | 61 | $this->sse_log('update', 'Updating prepared data, just a moment...', 1, 'eventLog'); |
| 80 | 62 | wp_update_post( [ |
| 81 | 63 | 'ID' => $this->post_id, |
| @@ -127,31 +109,22 @@ | ||
| 127 | 109 | |
| 128 | 110 | if ( ! empty( $block['innerBlocks'] ) ) { |
| 129 | 111 | $this->replace( $block['innerBlocks'], $request_params ); |
| 130 | 112 | } |
| 131 | - 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"){ | |
| 132 | 114 | $block["attrs"]["widthRange"] = $request_params['logo_size']; |
| 133 | 115 | unset($block["attrs"]["widthUnit"]); |
| 134 | 116 | if (isset($block['attrs']['blockMeta']['desktop']) && is_string($block['attrs']['blockMeta']['desktop'])) { |
| 135 | 117 | $meta = $block['attrs']['blockMeta']['desktop']; |
| 136 | - $size = $request_params['logo_size']; | |
| 118 | + $regex = '/\.image-wrapper\s*{\s*width:\s*(\d+)px;/'; | |
| 137 | 119 | |
| 138 | - // Update width inside .image-wrapper { ... } (old EB format) | |
| 139 | - // and .image-wrapper img { ... } (new EB format) — both must be handled | |
| 140 | - $meta = preg_replace( | |
| 141 | - '/(\.image-wrapper(?:\s+img)?\s*\{[^}]*)width\s*:\s*\d+px\s*;/', | |
| 142 | - '${1}width:' . $size . 'px;', | |
| 143 | - $meta | |
| 144 | - ); | |
| 120 | + if (preg_match($regex, $meta, $matches)) { | |
| 121 | + $replaced = preg_replace($regex, '.image-wrapper { width:' . $request_params['logo_size'] . 'px;', $meta); | |
| 145 | 122 | |
| 146 | - // Update width inside .eb-image-wrapper-inner { ... } | |
| 147 | - $meta = preg_replace( | |
| 148 | - '/(\.eb-image-wrapper-inner\s*\{[^}]*)width\s*:\s*\d+px\s*;/', | |
| 149 | - '${1}width:' . $size . 'px;', | |
| 150 | - $meta | |
| 151 | - ); | |
| 152 | - | |
| 153 | - $block['attrs']['blockMeta']['desktop'] = $meta; | |
| 123 | + if ($replaced) { | |
| 124 | + $block['attrs']['blockMeta']['desktop'] = $replaced; | |
| 125 | + } | |
| 126 | + } | |
| 154 | 127 | } |
| 155 | 128 | |
| 156 | 129 | $this->sse_log('prepare', 'Updated Site Logo', 1, 'eventLog'); |
| 157 | 130 | } |
| @@ -157,35 +130,8 @@ | ||
| 157 | 130 | } |
| 158 | 131 | } |
| 159 | 132 | } |
| 160 | 133 | |
| 161 | - private function replace_fluent_cart_store_settings($settings) { | |
| 162 | - $map_post_ids = $this->map_post_ids; | |
| 163 | - $saved_options = $settings; | |
| 164 | - $mapped_keys = [ | |
| 165 | - 'checkout_page_id', | |
| 166 | - 'custom_payment_page_id', | |
| 167 | - 'registration_page_id', | |
| 168 | - 'login_page_id', | |
| 169 | - 'cart_page_id', | |
| 170 | - 'receipt_page_id', | |
| 171 | - 'shop_page_id', | |
| 172 | - 'customer_profile_page_id', | |
| 173 | - ]; | |
| 174 | - | |
| 175 | - foreach ($mapped_keys as $key) { | |
| 176 | - $old_id = $saved_options[$key] ?? null; | |
| 177 | - if (!empty($old_id) && isset($map_post_ids[$old_id])) { | |
| 178 | - $saved_options[$key] = $map_post_ids[$old_id]; | |
| 179 | - } | |
| 180 | - else if(isset($saved_options[$key])){ | |
| 181 | - $saved_options[$key] = 0; | |
| 182 | - } | |
| 183 | - } | |
| 184 | - | |
| 185 | - update_option('fluent_cart_store_settings', $saved_options); | |
| 186 | - } | |
| 187 | - | |
| 188 | 134 | private function replace_archive_id( $menu_id ) { |
| 189 | 135 | if ( ! empty( $this->imported_data['archive_settings'] ) ) { |
| 190 | 136 | $post = get_post( $menu_id ); |
| 191 | 137 | $blocks = parse_blocks( $post->post_content ); |
| @@ -268,14 +214,12 @@ | ||
| 268 | 214 | $attrs = $this->replace_attachment_ids($attrs); |
| 269 | 215 | } |
| 270 | 216 | |
| 271 | 217 | if (is_string($attrs)) { |
| 272 | - if ( ! empty( $this->wp_importer->url_remap ) ) { | |
| 273 | - $attrs = str_replace( | |
| 274 | - array_keys( $this->wp_importer->url_remap ), | |
| 275 | - array_values( $this->wp_importer->url_remap ), | |
| 276 | - $attrs | |
| 277 | - ); | |
| 218 | + foreach ($this->wp_importer->url_remap as $old_url => $new_url) { | |
| 219 | + if (strpos($attrs, $old_url) !== false) { | |
| 220 | + $attrs = str_replace($old_url, $new_url, $attrs); | |
| 221 | + } | |
| 278 | 222 | } |
| 279 | 223 | } |
| 280 | 224 | else if (is_array($attrs)) { |
| 281 | 225 | foreach ($attrs as $key => $attr) { |
| @@ -333,11 +277,10 @@ | ||
| 333 | 277 | return $attrs; |
| 334 | 278 | } |
| 335 | 279 | |
| 336 | 280 | private function processSingleImage($attrs, $imageKey) { |
| 337 | - $image = $attrs['attrs'][$imageKey] ?? null; | |
| 338 | - if ( is_array( $image ) && ! empty( $image['url'] ) && ! empty( self::$attachment_ids[ $image['url'] ] ) ) { | |
| 339 | - $attrs['attrs'][$imageKey]['id'] = (int) self::$attachment_ids[ $image['url'] ]; | |
| 281 | + if (!empty(self::$attachment_ids[$attrs['attrs'][$imageKey]['url']])) { | |
| 282 | + $attrs['attrs'][$imageKey]['id'] = (int) self::$attachment_ids[$attrs['attrs'][$imageKey]['url']]; | |
| 340 | 283 | } |
| 341 | 284 | return $attrs; |
| 342 | 285 | } |
| 343 | 286 | |
| @@ -401,18 +344,10 @@ | ||
| 401 | 344 | // Time to run the import! |
| 402 | 345 | set_time_limit( 0 ); |
| 403 | 346 | |
| 404 | 347 | $organizedUrls = $this->template_settings['__attachments']; |
| 405 | - $deadline = ! empty( $this->attachment_options['attachment_deadline'] ) | |
| 406 | - ? (float) $this->attachment_options['attachment_deadline'] | |
| 407 | - : null; | |
| 408 | 348 | // For each base image URL... |
| 409 | 349 | foreach ($organizedUrls as $base_url => $sizes) { |
| 410 | - if ( $deadline && microtime( true ) >= $deadline ) { | |
| 411 | - $this->sse_log( 'warning', 'Skipping remaining attachments after the import time limit.', -1, 'eventLog' ); | |
| 412 | - break; | |
| 413 | - } | |
| 414 | - | |
| 415 | 350 | // Prepare the post data for the attachment |
| 416 | 351 | $post_data = $this->prepare_post_data($this->post_id, $base_url); |
| 417 | 352 | |
| 418 | 353 | // If prepare_post_data returned null, skip this iteration |
| @@ -467,5 +402,5 @@ | ||
| 467 | 402 | public function get_content(){ |
| 468 | 403 | return $this->content; |
| 469 | 404 | } |
| 470 | 405 | |
| 471 | -} | |
| 406 | +} | |