@@ -415,6 +415,34 @@ public function migrateMagentoToEverblog()
415
415
416
416
public function replaceAndDownloadImages ($ content )
417
417
{
418
+ // Convert WordPress [caption] shortcodes to Bootstrap 5 figure markup
419
+ $ content = preg_replace_callback (
420
+ '/\[caption[^\]]*\](<img[^>]+>)(.*?)\[\/caption\]/si ' ,
421
+ function ($ matches ) {
422
+ $ imgTag = $ matches [1 ];
423
+ $ caption = trim ($ matches [2 ]);
424
+ $ imgDom = new DOMDocument ();
425
+ @$ imgDom ->loadHTML ($ imgTag , LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD );
426
+ $ img = $ imgDom ->getElementsByTagName ('img ' )->item (0 );
427
+ if ($ img ) {
428
+ $ classes = $ img ->getAttribute ('class ' );
429
+ $ img ->setAttribute ('class ' , trim ($ classes . ' img-fluid figure-img ' ));
430
+ $ imgTag = $ imgDom ->saveHTML ($ img );
431
+ }
432
+ return '<figure class="figure text-center"> ' . $ imgTag
433
+ . '<figcaption class="figure-caption"> ' . $ caption . '</figcaption></figure> ' ;
434
+ },
435
+ $ content
436
+ );
437
+
438
+ // Replace common WordPress alignment classes with Bootstrap 5 ones
439
+ $ replace = [
440
+ 'aligncenter ' => 'mx-auto d-block ' ,
441
+ 'alignright ' => 'float-end ' ,
442
+ 'alignleft ' => 'float-start ' ,
443
+ ];
444
+ $ content = str_replace (array_keys ($ replace ), array_values ($ replace ), $ content );
445
+
418
446
// Remplacer {{media url="..."}}
419
447
$ pattern = '/\{\{media url="wysiwyg\/([^"]+)"\}\}/ ' ;
420
448
$ content = preg_replace_callback ($ pattern , function ($ matches ) {
@@ -452,6 +480,9 @@ public function replaceAndDownloadImages($content)
452
480
}
453
481
}
454
482
}
483
+ // Apply Bootstrap 5 responsive class to images
484
+ $ currentClass = $ img ->getAttribute ('class ' );
485
+ $ img ->setAttribute ('class ' , trim ($ currentClass . ' img-fluid ' ));
455
486
}
456
487
457
488
return $ dom ->saveHTML ();
0 commit comments