← All changes
|
includes/Core/Importer/Utils/GutenbergHelper.php
+10
-266
3.1.10
→
3.0.1
View file →
| @@ -1,30 +1,16 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace Templately\Core\Importer\Utils; |
| 4 | 4 | |
| 5 | -use Templately\Core\Importer\WPImport; | |
| 6 | -use Templately\Utils\Helper; | |
| 7 | - | |
| 8 | 5 | class GutenbergHelper extends ImportHelper { |
| 9 | - const IMAGE_URL_PATTERN = '/(http(s?):)([\/|.|\w|\s|-])*\.(?:jpg|gif|png)/'; | |
| 10 | - | |
| 11 | 6 | private $template_settings = []; |
| 12 | 7 | private $forms = []; |
| 13 | - /** | |
| 14 | - * @var WPImport | |
| 15 | - */ | |
| 16 | - private $wp_importer; | |
| 17 | 8 | |
| 18 | - public $shouldLog = true; // This is the class property to check | |
| 19 | - | |
| 20 | 9 | protected $content; |
| 21 | 10 | |
| 22 | 11 | protected $post_id; |
| 23 | 12 | |
| 24 | - protected static $attachment_ids = []; | |
| 25 | - | |
| 26 | - | |
| 27 | 13 | /** |
| 28 | 14 | * @param $template_json |
| 29 | 15 | * @param $template_settings |
| 30 | 16 | * @param $extra_content |
| @@ -30,26 +16,17 @@ | ||
| 30 | 16 | * @param $extra_content |
| 31 | 17 | * |
| 32 | 18 | * @return GutenbergHelper |
| 33 | 19 | */ |
| 34 | - public function prepare( $template_json, $template_settings, $extra_content = [], $request_params = [] ) { | |
| 20 | + public function prepare( $template_json, $template_settings, $extra_content = [] ) { | |
| 35 | 21 | $this->template_settings = $template_settings; |
| 36 | 22 | $this->post_id = $this->map_post_ids[ $template_settings['post_id'] ]; |
| 37 | - $this->wp_importer = new WPImport( null, ['fetch_attachments' => true] ); | |
| 38 | 23 | |
| 39 | 24 | $parsed_blocks = parse_blocks( $template_json['content'] ); |
| 40 | 25 | if ( ! empty( $extra_content ) ) { |
| 41 | 26 | $this->prepare_form_data( $extra_content, $template_settings ); |
| 42 | 27 | } |
| 43 | - if ( isset($this->template_settings['__attachments']) ) { | |
| 44 | - $this->process_images(); | |
| 45 | - // Make sure we do the longest urls first, in case one is a substring of another. | |
| 46 | - uksort( $this->wp_importer->url_remap, function ( $a, $b ) { | |
| 47 | - // Return the difference in length between two strings. | |
| 48 | - return strlen( $b ) - strlen( $a ); | |
| 49 | - } ); | |
| 50 | - } | |
| 51 | - $this->replace( $parsed_blocks, $request_params ); | |
| 28 | + $this->replace( $parsed_blocks ); | |
| 52 | 29 | $this->content = wp_slash( serialize_blocks( $parsed_blocks ) ); |
| 53 | 30 | |
| 54 | 31 | return $this; |
| 55 | 32 | } |
| @@ -77,9 +54,9 @@ | ||
| 77 | 54 | } |
| 78 | 55 | } |
| 79 | 56 | } |
| 80 | 57 | |
| 81 | - private function replace( &$blocks, $request_params = [] ) { | |
| 58 | + private function replace( &$blocks ) { | |
| 82 | 59 | foreach ( $blocks as &$block ) { |
| 83 | 60 | $this->sse_log('prepare', 'Preparing output for finalize, just a moment...', 1, 'eventLog'); |
| 84 | 61 | if ( $block['blockName'] === 'core/navigation' ) { |
| 85 | 62 | if ( ! empty( $block['attrs']['ref'] ) && array_key_exists( $block['attrs']['ref'], $this->map_post_ids ) ) { |
| @@ -86,46 +63,18 @@ | ||
| 86 | 63 | $block['attrs']['ref'] = $this->map_post_ids[ $block['attrs']['ref'] ]; |
| 87 | 64 | $this->replace_archive_id( $block['attrs']['ref'] ); |
| 88 | 65 | } |
| 89 | 66 | } |
| 90 | - if ( isset( $block['attrs']['blockId'] ) && array_key_exists( $block['attrs']['blockId'], $this->forms ) ) { | |
| 91 | - $blockId = $block['attrs']['blockId']; | |
| 92 | - $form = $this->forms[$blockId]; | |
| 93 | - $attr = $form['attr']; | |
| 94 | - $value = (string) $form['value']; | |
| 95 | - | |
| 96 | - $block['attrs'][$attr] = $value; | |
| 67 | + if ( isset( $block['blockId'] ) && array_key_exists( $block['blockId'], $this->forms ) ) { | |
| 68 | + $block['attrs'][ $this->forms[ $block['blockId'] ]['attr'] ] = $this->forms[ $block['blockId'] ]['value']; | |
| 97 | 69 | } |
| 98 | 70 | |
| 99 | 71 | if ( ! empty( $block['attrs']['queryData'] ) ) { |
| 100 | 72 | $this->replace_query_data( $block ); |
| 101 | 73 | } |
| 102 | - | |
| 103 | - if ( isset($this->template_settings['__attachments']) ) { | |
| 104 | - $block = $this->replace_attachment_url( $block ); | |
| 105 | - } | |
| 106 | - | |
| 107 | 74 | if ( ! empty( $block['innerBlocks'] ) ) { |
| 108 | - $this->replace( $block['innerBlocks'], $request_params ); | |
| 75 | + $this->replace( $block['innerBlocks'] ); | |
| 109 | 76 | } |
| 110 | - 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 | - $block["attrs"]["widthRange"] = $request_params['logo_size']; | |
| 112 | - unset($block["attrs"]["widthUnit"]); | |
| 113 | - if (isset($block['attrs']['blockMeta']['desktop']) && is_string($block['attrs']['blockMeta']['desktop'])) { | |
| 114 | - $meta = $block['attrs']['blockMeta']['desktop']; | |
| 115 | - $regex = '/\.image-wrapper\s*{\s*width:\s*(\d+)px;/'; | |
| 116 | - | |
| 117 | - if (preg_match($regex, $meta, $matches)) { | |
| 118 | - $replaced = preg_replace($regex, '.image-wrapper { width:' . $request_params['logo_size'] . 'px;', $meta); | |
| 119 | - | |
| 120 | - if ($replaced) { | |
| 121 | - $block['attrs']['blockMeta']['desktop'] = $replaced; | |
| 122 | - } | |
| 123 | - } | |
| 124 | - } | |
| 125 | - | |
| 126 | - $this->sse_log('prepare', 'Updated Site Logo', 1, 'eventLog'); | |
| 127 | - } | |
| 128 | 77 | } |
| 129 | 78 | } |
| 130 | 79 | |
| 131 | 80 | private function replace_archive_id( $menu_id ) { |
| @@ -135,19 +84,12 @@ | ||
| 135 | 84 | $changed = false; |
| 136 | 85 | foreach ( $blocks as &$block ) { |
| 137 | 86 | $this->sse_log('query', 'Finalizing archive settings, just a moment...', 1, 'eventLog'); |
| 138 | 87 | |
| 139 | - if (isset($block['attrs']['id'])) { | |
| 140 | - $blockId = $block['attrs']['id']; | |
| 141 | - $archiveSettings = $this->imported_data['archive_settings']; | |
| 142 | - | |
| 143 | - $archiveIds = $archiveSettings[$blockId] ?? false; | |
| 144 | - | |
| 145 | - if ($archiveIds) { | |
| 146 | - $block['attrs']['id'] = $archiveIds['page_id']; | |
| 147 | - $block['attrs']['url'] = get_the_permalink($archiveIds['page_id']); | |
| 148 | - $changed = true; | |
| 149 | - } | |
| 88 | + if ( isset( $block['attrs']['id'] ) && $block['attrs']['id'] === $this->imported_data['archive_settings']['archive_id'] ) { | |
| 89 | + $changed = true; | |
| 90 | + $block['attrs']['id'] = $this->imported_data['archive_settings']['page_id']; | |
| 91 | + $block['attrs']['url'] = get_the_permalink( $this->imported_data['archive_settings']['page_id'] ); | |
| 150 | 92 | } |
| 151 | 93 | } |
| 152 | 94 | if ( $changed ) { |
| 153 | 95 | wp_update_post( [ |
| @@ -204,203 +146,5 @@ | ||
| 204 | 146 | $block['attrs']['queryData'][$type] = json_encode($block['attrs']['queryData'][$type]); |
| 205 | 147 | } |
| 206 | 148 | } |
| 207 | 149 | } |
| 208 | - | |
| 209 | - public function replace_attachment_url($attrs) { | |
| 210 | - if ( !empty($attrs['blockName'] ) ) { | |
| 211 | - $attrs = $this->replace_attachment_ids($attrs); | |
| 212 | - } | |
| 213 | - | |
| 214 | - if (is_string($attrs)) { | |
| 215 | - foreach ($this->wp_importer->url_remap as $old_url => $new_url) { | |
| 216 | - if (strpos($attrs, $old_url) !== false) { | |
| 217 | - $attrs = str_replace($old_url, $new_url, $attrs); | |
| 218 | - } | |
| 219 | - } | |
| 220 | - } | |
| 221 | - else if (is_array($attrs)) { | |
| 222 | - foreach ($attrs as $key => $attr) { | |
| 223 | - $attrs[$key] = $this->replace_attachment_url($attr); | |
| 224 | - } | |
| 225 | - } | |
| 226 | - | |
| 227 | - return $attrs; | |
| 228 | - } | |
| 229 | - | |
| 230 | - protected function replace_attachment_ids($attrs){ | |
| 231 | - switch ($attrs['blockName']) { | |
| 232 | - case 'essential-blocks/slider': | |
| 233 | - $attrs = $this->processImages($attrs, 'images', 'imageId', 'url'); | |
| 234 | - break; | |
| 235 | - case 'essential-blocks/advanced-image': | |
| 236 | - $attrs = $this->processSingleImage($attrs, 'image'); | |
| 237 | - break; | |
| 238 | - case 'essential-blocks/parallax-slider': | |
| 239 | - $attrs = $this->processImages($attrs, 'sliderData', 'id', 'src'); | |
| 240 | - break; | |
| 241 | - case 'essential-blocks/image-gallery': | |
| 242 | - $attrs = $this->processImages($attrs, 'images', 'id', 'url'); | |
| 243 | - $attrs = $this->processImages($attrs, 'sources', 'id', 'url'); | |
| 244 | - break; | |
| 245 | - case 'core/image': | |
| 246 | - $attrs = $this->processCoreImage($attrs, 'id'); | |
| 247 | - break; | |
| 248 | - case 'core/media-text': | |
| 249 | - $attrs = $this->processCoreImage($attrs, 'mediaId'); | |
| 250 | - break; | |
| 251 | - case 'core/cover': | |
| 252 | - $attrs = $this->processSingleImage($attrs, 'url'); | |
| 253 | - break; | |
| 254 | - } | |
| 255 | - return $attrs; | |
| 256 | - } | |
| 257 | - | |
| 258 | - private function processImages($attrs, $imageKey, $idKey, $urlKey) { | |
| 259 | - if (!empty($attrs['attrs'][$imageKey])) { | |
| 260 | - foreach ($attrs['attrs'][$imageKey] as $key => &$image) { | |
| 261 | - $imageUrl = $image[$urlKey]; | |
| 262 | - if (!empty(self::$attachment_ids[$imageUrl])) { | |
| 263 | - $oldId = $image[$idKey]; | |
| 264 | - $newId = (int) self::$attachment_ids[$imageUrl]; | |
| 265 | - $image[$idKey] = $newId; | |
| 266 | - | |
| 267 | - // Update innerHTML and innerContent for 'essential-blocks/slider' | |
| 268 | - if ($attrs['blockName'] === 'essential-blocks/slider') { | |
| 269 | - $attrs = $this->updateInnerHTMLAndContent($attrs, "imageId":$oldId,"", "imageId":$newId,""); | |
| 270 | - } | |
| 271 | - } | |
| 272 | - } | |
| 273 | - } | |
| 274 | - return $attrs; | |
| 275 | - } | |
| 276 | - | |
| 277 | - private function processSingleImage($attrs, $imageKey) { | |
| 278 | - if (!empty(self::$attachment_ids[$attrs['attrs'][$imageKey]['url']])) { | |
| 279 | - $attrs['attrs'][$imageKey]['id'] = (int) self::$attachment_ids[$attrs['attrs'][$imageKey]['url']]; | |
| 280 | - } | |
| 281 | - return $attrs; | |
| 282 | - } | |
| 283 | - | |
| 284 | - private function processCoreImage($attrs, $idKey) { | |
| 285 | - preg_match(self::IMAGE_URL_PATTERN, $attrs['innerHTML'], $matches); | |
| 286 | - if (!empty($matches[0])) { | |
| 287 | - $url = $matches[0]; | |
| 288 | - if (!empty(self::$attachment_ids[$url])) { | |
| 289 | - $oldId = $attrs['attrs'][$idKey]; | |
| 290 | - $newId = (int) self::$attachment_ids[$url]; | |
| 291 | - $attrs['attrs'][$idKey] = $newId; | |
| 292 | - | |
| 293 | - // Replace old ID with new ID in innerHTML and innerContent | |
| 294 | - $attrs = $this->updateInnerHTMLAndContent($attrs, "wp-image-$oldId", "wp-image-$newId"); | |
| 295 | - } | |
| 296 | - } | |
| 297 | - return $attrs; | |
| 298 | - } | |
| 299 | - | |
| 300 | - private function updateInnerHTMLAndContent($attrs, $oldValue, $newValue) { | |
| 301 | - $attrs['innerHTML'] = str_replace($oldValue, $newValue, $attrs['innerHTML']); | |
| 302 | - if (is_array($attrs['innerContent'])) { | |
| 303 | - foreach ($attrs['innerContent'] as $i => $content) { | |
| 304 | - $attrs['innerContent'][$i] = str_replace($oldValue, $newValue, $content); | |
| 305 | - } | |
| 306 | - } else { | |
| 307 | - $attrs['innerContent'] = str_replace($oldValue, $newValue, $attrs['innerContent']); | |
| 308 | - } | |
| 309 | - return $attrs; | |
| 310 | - } | |
| 311 | - | |
| 312 | - /** | |
| 313 | - * Parses the images from the post content, processes them as attachments, and updates the post with the new attachment URL. | |
| 314 | - * @todo: Parse using parse_blocks instead of regex | |
| 315 | - * | |
| 316 | - * @param int $post_id The ID of the post. | |
| 317 | - * @param string $post_content The content of the post. | |
| 318 | - */ | |
| 319 | - public function parse_images($post_content) { | |
| 320 | - // Find all image URLs in the post content | |
| 321 | - preg_match_all(self::IMAGE_URL_PATTERN, $post_content, $matches); | |
| 322 | - | |
| 323 | - $urls = $matches[0]; | |
| 324 | - $organizedUrls = []; | |
| 325 | - | |
| 326 | - foreach ($urls as $url) { | |
| 327 | - // Remove the size suffix from the URL | |
| 328 | - $base_url = preg_replace('/-\d+x\d+(?=\.[a-zA-Z]+$)/', '', $url); | |
| 329 | - | |
| 330 | - // Check if the URL already exists in the array | |
| 331 | - if (!isset($organizedUrls[$base_url]) || !in_array($url, $organizedUrls[$base_url])) { | |
| 332 | - // Add the URL to the array under the base URL key | |
| 333 | - $organizedUrls[$base_url][] = $url; | |
| 334 | - } | |
| 335 | - } | |
| 336 | - | |
| 337 | - return $organizedUrls; | |
| 338 | - } | |
| 339 | - | |
| 340 | - public function process_images(){ | |
| 341 | - // Time to run the import! | |
| 342 | - set_time_limit( 0 ); | |
| 343 | - | |
| 344 | - $organizedUrls = $this->template_settings['__attachments']; | |
| 345 | - // For each base image URL... | |
| 346 | - foreach ($organizedUrls as $base_url => $sizes) { | |
| 347 | - // Prepare the post data for the attachment | |
| 348 | - $post_data = $this->prepare_post_data($this->post_id, $base_url); | |
| 349 | - | |
| 350 | - // If prepare_post_data returned null, skip this iteration | |
| 351 | - if ($post_data === null) { | |
| 352 | - continue; | |
| 353 | - } | |
| 354 | - | |
| 355 | - // Check if the attachment already exists | |
| 356 | - $attachment_id = $this->wp_importer->process_attachment($post_data, $base_url, $sizes); | |
| 357 | - // If there was an error, handle it here | |
| 358 | - if (is_wp_error($attachment_id)) { | |
| 359 | - $this->sse_log('error', $attachment_id->get_error_message(), -1, 'eventLog'); | |
| 360 | - } else { | |
| 361 | - $this->sse_log('success', "Imported attachment: $attachment_id", -1, 'eventLog'); | |
| 362 | - self::$attachment_ids[$base_url] = $attachment_id; | |
| 363 | - } | |
| 364 | - } | |
| 365 | - } | |
| 366 | - | |
| 367 | - 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; | |
| 391 | - } | |
| 392 | - | |
| 393 | - public function sse_log( $type, $message, $progress = 1, $action = 'updateLog', $status = null ) { | |
| 394 | - if ($this->shouldLog) { | |
| 395 | - parent::sse_log($type, $message, $progress, $action, $status); | |
| 396 | - } | |
| 397 | - else{ | |
| 398 | - Helper::log(func_get_args()); | |
| 399 | - } | |
| 400 | - } | |
| 401 | - | |
| 402 | - public function get_content(){ | |
| 403 | - return $this->content; | |
| 404 | - } | |
| 405 | - | |
| 406 | 150 | } |