| @@ -1,0 +1,83 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace ABlocks\import; | |
| 4 | + | |
| 5 | +use ABlocks\import\ImageParsers\ablocks\CounterParser; | |
| 6 | +use ABlocks\import\ImageParsers\ablocks\DividerParser; | |
| 7 | +use ABlocks\import\ImageParsers\ablocks\FlipBoxParser; | |
| 8 | +use ABlocks\import\ImageParsers\ablocks\IconParser; | |
| 9 | +use ABlocks\import\ImageParsers\ablocks\ImageComparisonParser; | |
| 10 | +use ABlocks\import\ImageParsers\ablocks\ImageHotspotParser; | |
| 11 | +use ABlocks\import\ImageParsers\ablocks\ImageParser; | |
| 12 | +use ABlocks\import\ImageParsers\ablocks\ImageScrollParser; | |
| 13 | +use ABlocks\import\ImageParsers\ablocks\ModalParser; | |
| 14 | +use ABlocks\import\ImageParsers\ablocks\TabsParser; | |
| 15 | +use ABlocks\import\ImageParsers\ablocks\VideoParser; | |
| 16 | +use ABlocks\import\ImageParsers\AbstractImageParser; | |
| 17 | +use ABlocks\import\ImageParsers\CommonParser; | |
| 18 | +use ABlocks\import\ImageParsers\wp\CoreImageParser; | |
| 19 | +use ABlocks\import\ImageParsers\wp\GroupParser; | |
| 20 | + | |
| 21 | +class ImageParserFactory { | |
| 22 | + | |
| 23 | + /** | |
| 24 | + * @var array<string, AbstractImageParser> $parsers Contain all parsers. | |
| 25 | + */ | |
| 26 | + private array $parsers = array(); | |
| 27 | + | |
| 28 | + private function __construct() { | |
| 29 | + $this->set_parsers(); | |
| 30 | + } | |
| 31 | + | |
| 32 | + private function set_parsers() { | |
| 33 | + $this->parsers = apply_filters( 'ablocks/register_image_parsers', array( | |
| 34 | + 'core/group' => GroupParser::class, | |
| 35 | + 'core/image' => CoreImageParser::class, | |
| 36 | + 'ablocks/image' => ImageParser::class, | |
| 37 | + 'ablocks/image-comparison' => ImageComparisonParser::class, | |
| 38 | + 'ablocks/video' => VideoParser::class, | |
| 39 | + 'ablocks/tabs' => TabsParser::class, | |
| 40 | + 'ablocks/image-scroll' => ImageScrollParser::class, | |
| 41 | + 'ablocks/modal' => ModalParser::class, | |
| 42 | + 'ablocks/flip-box' => FlipBoxParser::class, | |
| 43 | + 'ablocks/image-hotspot' => ImageHotspotParser::class, | |
| 44 | + ) ); | |
| 45 | + } | |
| 46 | + | |
| 47 | + public static function instance() { | |
| 48 | + static $instance = false; | |
| 49 | + | |
| 50 | + if ( ! $instance ) { | |
| 51 | + $instance = new self(); | |
| 52 | + } | |
| 53 | + | |
| 54 | + return $instance; | |
| 55 | + } | |
| 56 | + | |
| 57 | + public function parse( array $blocks ): array { | |
| 58 | + $parsed_blocks = array(); | |
| 59 | + foreach ( $blocks as $block ) { | |
| 60 | + if ( count( $block['innerBlocks'] ) > 0 ) { | |
| 61 | + $block['innerBlocks'] = $this->parse( $block['innerBlocks'] ); | |
| 62 | + } | |
| 63 | + | |
| 64 | + $parsed_blocks[] = $this->process( $block ); | |
| 65 | + } | |
| 66 | + | |
| 67 | + return $parsed_blocks; | |
| 68 | + } | |
| 69 | + | |
| 70 | + private function process( array $block ): array { | |
| 71 | + $block = ( new CommonParser( $block ) )->do_parse(); | |
| 72 | + | |
| 73 | + if ( ! array_key_exists( $block['blockName'], $this->parsers ) ) { | |
| 74 | + return $block; | |
| 75 | + } | |
| 76 | + | |
| 77 | + /** @var AbstractImageParser $parser */ | |
| 78 | + $parser = new $this->parsers[ $block['blockName'] ]( $block ); | |
| 79 | + | |
| 80 | + return $parser->do_parse(); | |
| 81 | + } | |
| 82 | +} | |
| 83 | + | |