Skip to content

Commit c0bf7e1

Browse files
Beta support for Checkout Block
* Checkout Block * Implements @wordpress/scripts for block building * Use asset file
1 parent 0e067ab commit c0bf7e1

File tree

9 files changed

+133
-1
lines changed

9 files changed

+133
-1
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
/node_modules/
12
/svn/
23
/vendor/
34
aplazame.latest.zip
45
composer.lock
56
docker-compose.override.yml
7+
package-lock.json

package.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"devDependencies": {
3+
"@wordpress/scripts": "^27.9.0"
4+
},
5+
"scripts": {
6+
"build": "wp-scripts build --webpack-src-dir=resources --output-path=plugin/resources",
7+
"start": "wp-scripts start --webpack-src-dir=resources --output-path=plugin/resources"
8+
}
9+
}

plugin/aplazame.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,22 @@ public function __construct( $apiBaseUri ) {
164164

165165
add_action( 'woocommerce_api_aplazame', array( $this, 'api_router' ) );
166166

167+
// Cart and Checkout Blocks
168+
add_action( 'woocommerce_blocks_loaded', array( $this, 'add_gateway_block') );
169+
add_action(
170+
'before_woocommerce_init',
171+
function() {
172+
if ( class_exists('\Automattic\WooCommerce\Utilities\FeaturesUtil') ) {
173+
\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'cart_checkout_blocks', __FILE__, true );
174+
}
175+
}
176+
);
177+
167178
// Declare HPOS compatibility
168179
add_action(
169180
'before_woocommerce_init',
170181
function () {
171-
if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) {
182+
if ( class_exists('\Automattic\WooCommerce\Utilities\FeaturesUtil') ) {
172183
\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true );
173184
}
174185
}
@@ -276,6 +287,20 @@ public function add_gateway( $methods ) {
276287
return $methods;
277288
}
278289

290+
public function add_gateway_block() {
291+
if( ! class_exists( 'Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType' ) ) {
292+
return;
293+
}
294+
295+
include_once 'classes/wc-aplazame-gateway-block.php';
296+
add_action(
297+
'woocommerce_blocks_payment_method_type_registration',
298+
function( Automattic\WooCommerce\Blocks\Payments\PaymentMethodRegistry $payment_method_registry ) {
299+
$payment_method_registry->register( new WC_Aplazame_Gateway_Blocks_Support );
300+
}
301+
);
302+
}
303+
279304
public function aplazameJs() {
280305

281306
Aplazame_Helpers::render_to_template( 'layout/header.php' );
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
if ( ! defined( 'ABSPATH' ) ) {
4+
exit;
5+
}
6+
7+
use Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType;
8+
9+
final class WC_Aplazame_Gateway_Blocks_Support extends AbstractPaymentMethodType {
10+
private $gateway;
11+
protected $name = WC_Aplazame::METHOD_ID;
12+
13+
public function initialize() {
14+
$this->settings = get_option( "woocommerce_{$this->name}_settings", [] );
15+
$gateways = WC()->payment_gateways->payment_gateways();
16+
$this->gateway = $gateways[ $this->name ];
17+
}
18+
19+
public function is_active() {
20+
return $this->gateway->is_available();
21+
}
22+
23+
public function get_payment_method_script_handles() {
24+
$asset_path = plugin_dir_path( __FILE__ ) . '../resources/payment-block.asset.php';
25+
$version = null;
26+
$dependencies = array();
27+
if( file_exists( $asset_path ) ) {
28+
$asset = require $asset_path;
29+
$version = isset( $asset[ 'version' ] ) ? $asset[ 'version' ] : $version;
30+
$dependencies = isset( $asset[ 'dependencies' ] ) ? $asset[ 'dependencies' ] : $dependencies;
31+
}
32+
wp_register_script(
33+
'wc-aplazame-blocks-integration',
34+
plugin_dir_url( __FILE__ ) . '../resources/payment-block.js',
35+
$dependencies,
36+
$version,
37+
true
38+
);
39+
return [ 'wc-aplazame-blocks-integration' ];
40+
}
41+
42+
public function get_payment_method_data() {
43+
return [
44+
'title' => $this->gateway->title,
45+
'description' => $this->gateway->settings['description'],
46+
];
47+
}
48+
}

plugin/resources/block.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"$schema": "https://schemas.wp.org/trunk/block.json",
3+
"apiVersion": 3,
4+
"name": "aplazame/payment-block",
5+
"version": "1.0.0",
6+
"title": "Aplazame Payment Block",
7+
"editorScript": "file:payment-block.js"
8+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php return array('dependencies' => array('react', 'wp-element', 'wp-html-entities'), 'version' => 'cd602efc1a4a409b88bc');

plugin/resources/payment-block.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

resources/block.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"$schema": "https://schemas.wp.org/trunk/block.json",
3+
"apiVersion": 3,
4+
"name": "aplazame/payment-block",
5+
"version": "1.0.0",
6+
"title": "Aplazame Payment Block",
7+
"editorScript": "file:payment-block.js"
8+
}

resources/payment-block.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { decodeEntities } from '@wordpress/html-entities';
2+
import { RawHTML } from '@wordpress/element';
3+
4+
const { registerPaymentMethod } = window.wc.wcBlocksRegistry
5+
const { getSetting } = window.wc.wcSettings
6+
const settings = getSetting( 'aplazame_data', {} )
7+
const label = decodeEntities( settings.title )
8+
9+
const Content = () => {
10+
return <RawHTML>{ settings.description || '' }</RawHTML>;
11+
}
12+
13+
const Label = ( props ) => {
14+
const { PaymentMethodLabel } = props.components
15+
return <PaymentMethodLabel text={ label } />
16+
}
17+
18+
registerPaymentMethod(
19+
{
20+
name: "aplazame",
21+
label: <Label />,
22+
content: <Content />,
23+
edit: <Content />,
24+
canMakePayment: () => true,
25+
ariaLabel: label,
26+
supports: {
27+
features: settings.supports,
28+
}
29+
}
30+
)

0 commit comments

Comments
 (0)