| @@ -18,10 +18,12 @@ | ||
| 18 | 18 | 'link' => Interpreters\Link::class, |
| 19 | 19 | 'image' => Interpreters\Image::class, |
| 20 | 20 | ]; |
| 21 | 21 | protected string $content; |
| 22 | - public function __construct( string $content ) { | |
| 22 | + protected array $context; | |
| 23 | + public function __construct( string $content, $context = [] ) { | |
| 23 | 24 | $this->content = $content; |
| 25 | + $this->context = $context; | |
| 24 | 26 | $this->parse();// exit; |
| 25 | 27 | } |
| 26 | 28 | public function parse() : void { |
| 27 | 29 | $this->content = preg_replace_callback( |
| @@ -29,10 +31,14 @@ | ||
| 29 | 31 | [ $this, 'apply_changes' ], |
| 30 | 32 | $this->content |
| 31 | 33 | ); |
| 32 | 34 | } |
| 33 | - public function apply_changes( array $matches ) : string { | |
| 34 | - return is_null( $ins = ( new ArgumentParser( ...$this->filter( $matches ), ...[ $this->interpreters ] ) )->interpret() ) ? '' : $ins->content(); | |
| 35 | + public function apply_changes( array $matches ): string { | |
| 36 | + $filtered = $this->filter( $matches ); // returns an array | |
| 37 | + $args = array_merge( $filtered, [ $this->interpreters, $this->context ] ); | |
| 38 | + $parser = new ArgumentParser( ...$args ); | |
| 39 | + $ins = $parser->interpret(); | |
| 40 | + return is_null( $ins ) ? '' : $ins->content(); | |
| 35 | 41 | } |
| 36 | 42 | |
| 37 | 43 | public function filter( array $matches ) : array { |
| 38 | 44 | $is_richtext = false; |
| @@ -46,9 +52,16 @@ | ||
| 46 | 52 | $is_richtext |
| 47 | 53 | ]; |
| 48 | 54 | } |
| 49 | 55 | |
| 50 | - public static function init( string $content ) : string { | |
| 51 | - $ins = new self( $content ); | |
| 56 | + public static function init( string $content, $block, $instance ) : string { | |
| 57 | + // This filter runs for EVERY block on the page (including core and | |
| 58 | + // third-party blocks). The regex in parse() is expensive, so skip it | |
| 59 | + // unless the block actually contains a dynamic-content marker. | |
| 60 | + if ( false === strpos( $content, 'ablocks_dc' ) | |
| 61 | + && false === strpos( $content, 'ablocks-richtext-dynamic-content' ) ) { | |
| 62 | + return $content; | |
| 63 | + } | |
| 64 | + $ins = new self( $content, $instance->context ); | |
| 52 | 65 | return $ins->content; |
| 53 | 66 | } |
| 54 | 67 | } |