diff --git a/modules/ppcp-paylater-block/block.json b/modules/ppcp-paylater-block/block.json index 0af6bf118..022c0352b 100644 --- a/modules/ppcp-paylater-block/block.json +++ b/modules/ppcp-paylater-block/block.json @@ -49,6 +49,5 @@ }, "textdomain": "woocommerce-paypal-payments", "editorScript": "ppcp-paylater-block", - "editorStyle": "file:./assets/css/edit.css", - "render": "file:./src/PayLaterBlockRender.php" + "editorStyle": "file:./assets/css/edit.css" } diff --git a/modules/ppcp-paylater-block/services.php b/modules/ppcp-paylater-block/services.php index 24b1f0443..0b6e02ee5 100644 --- a/modules/ppcp-paylater-block/services.php +++ b/modules/ppcp-paylater-block/services.php @@ -9,10 +9,11 @@ declare(strict_types=1); namespace WooCommerce\PayPalCommerce\PayLaterBlock; +use WooCommerce\PayPalCommerce\PayLaterBlock\PayLaterBlockRenderer; use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface; return array( - 'paylater-block.url' => static function ( ContainerInterface $container ): string { + 'paylater-block.url' => static function ( ContainerInterface $container ): string { /** * Cannot return false for this path. * @@ -23,4 +24,7 @@ return array( dirname( realpath( __FILE__ ), 3 ) . '/woocommerce-paypal-payments.php' ); }, + 'paylater-block.renderer' => static function (): PayLaterBlockRenderer { + return new PayLaterBlockRenderer(); + }, ); diff --git a/modules/ppcp-paylater-block/src/PayLaterBlockModule.php b/modules/ppcp-paylater-block/src/PayLaterBlockModule.php index 223ccd972..47d281a62 100644 --- a/modules/ppcp-paylater-block/src/PayLaterBlockModule.php +++ b/modules/ppcp-paylater-block/src/PayLaterBlockModule.php @@ -99,7 +99,23 @@ class PayLaterBlockModule implements ModuleInterface { * * @psalm-suppress PossiblyFalseArgument */ - register_block_type( dirname( realpath( __FILE__ ), 2 ) ); + register_block_type( + dirname( realpath( __FILE__ ), 2 ), + array( + 'render_callback' => function ( $attributes ) use ( $c ) { + $renderer = $c->get( 'paylater-block.renderer' ); + ob_start(); + // phpcs:ignore -- No need to escape it, the PayLaterBlockRenderer class is responsible for escaping. + echo $renderer->render( + // phpcs:ignore + $attributes, + // phpcs:ignore + $c + ); + return ob_get_clean(); + }, + ) + ); }, 20 ); diff --git a/modules/ppcp-paylater-block/src/PayLaterBlockRender.php b/modules/ppcp-paylater-block/src/PayLaterBlockRender.php deleted file mode 100644 index cbb8611fb..000000000 --- a/modules/ppcp-paylater-block/src/PayLaterBlockRender.php +++ /dev/null @@ -1,47 +0,0 @@ -'; - -$processor = new \WP_HTML_Tag_Processor( $html ); - -if ( $processor->next_tag( 'div' ) ) { - $layout = esc_attr( $attributes['layout'] ) ?? 'text'; - - if ( 'flex' === $layout ) { - $processor->set_attribute( 'data-pp-style-layout', 'flex' ); - $processor->set_attribute( 'data-pp-style-color', esc_attr( $attributes['flexColor'] ) ?? '' ); - $processor->set_attribute( 'data-pp-style-ratio', esc_attr( $attributes['flexRatio'] ) ?? '' ); - } else { - $processor->set_attribute( 'data-pp-style-layout', 'text' ); - $processor->set_attribute( 'data-pp-style-logo-type', esc_attr( $attributes['logo'] ) ?? '' ); - $processor->set_attribute( 'data-pp-style-logo-position', esc_attr( $attributes['position'] ) ?? '' ); - $processor->set_attribute( 'data-pp-style-text-color', esc_attr( $attributes['color'] ) ?? '' ); - $processor->set_attribute( 'data-pp-style-text-size', esc_attr( $attributes['size'] ) ?? '' ); - } - - if ( ( $attributes['placement'] ?? 'auto' ) !== 'auto' ) { - $processor->set_attribute( 'data-pp-placement', esc_attr( $attributes['placement'] ) ); - } -} - -$updated_html = (string) $processor; -?> - -
> - -
diff --git a/modules/ppcp-paylater-block/src/PayLaterBlockRenderer.php b/modules/ppcp-paylater-block/src/PayLaterBlockRenderer.php new file mode 100644 index 000000000..ace4c0e3f --- /dev/null +++ b/modules/ppcp-paylater-block/src/PayLaterBlockRenderer.php @@ -0,0 +1,65 @@ +get( 'wcgateway.settings.status' ) ) ) { + + $html = '
'; + + $processor = new \WP_HTML_Tag_Processor( $html ); + + if ( $processor->next_tag( 'div' ) ) { + $layout = $attributes['layout'] ?? 'text'; + + if ( 'flex' === $layout ) { + $processor->set_attribute( 'data-pp-style-layout', 'flex' ); + $processor->set_attribute( 'data-pp-style-color', esc_attr( $attributes['flexColor'] ?? '' ) ); + $processor->set_attribute( 'data-pp-style-ratio', esc_attr( $attributes['flexRatio'] ?? '' ) ); + } else { + $processor->set_attribute( 'data-pp-style-layout', 'text' ); + $processor->set_attribute( 'data-pp-style-logo-type', esc_attr( $attributes['logo'] ?? '' ) ); + $processor->set_attribute( 'data-pp-style-logo-position', esc_attr( $attributes['position'] ?? '' ) ); + $processor->set_attribute( 'data-pp-style-text-color', esc_attr( $attributes['color'] ?? '' ) ); + $processor->set_attribute( 'data-pp-style-text-size', esc_attr( $attributes['size'] ?? '' ) ); + } + + if ( ( $attributes['placement'] ?? 'auto' ) !== 'auto' ) { + $processor->set_attribute( 'data-pp-placement', esc_attr( $attributes['placement'] ) ); + } + } + + $updated_html = (string) $processor; + + return sprintf( + '
%2$s
', + wp_kses_data( get_block_wrapper_attributes() ), + $updated_html + ); + } + return ''; + } +}