PluginProbe
Additional Terms Lite for WooCommerce / 1.5.0
Additional Terms Lite for WooCommerce v1.5.0
1.7.3 1.7.2.1 trunk 1.0.1 1.0.2 1.1.0 1.2.0 1.2.1 1.2.2 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.4.0 1.4.1 1.5.0 1.5.1 1.5.2 1.6.0 1.6.1 1.6.2 1.6.3 All 35 releases
woo-additional-terms / includes / class-wat-checkout-block-integration.php

class-wat-checkout-block-integration.php in Additional Terms Lite for WooCommerce 1.5.0, at includes/class-wat-checkout-block-integration.php

211 lines 6.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Register the "Additional Terms" block for use in the Checkout Block offered by "WooCommerce Blocks".
4 *
5 * @link https://mypreview.one/woo-additional-terms
6 * @author MyPreview (Github: @mahdiyazdani, @gooklani, @mypreview)
7 * @since 1.5.0
8 *
9 * @package woo-additional-terms
10 * @subpackage woo-additional-terms/includes
11 */
12
13 use Automattic\WooCommerce\Blocks\Integrations\IntegrationInterface;
14 use Automattic\WooCommerce\Blocks\StoreApi\Schemas\CheckoutSchema;
15 use Automattic\WooCommerce\StoreApi\Schemas\ExtendSchema;
16 use Automattic\WooCommerce\StoreApi\StoreApi;
17
18 defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
19
20 if ( ! class_exists( 'WAT_Checkout_Block_Integration' ) ) :
21
22 /**
23 * The checkout block integration class.
24 */
25 class WAT_Checkout_Block_Integration implements IntegrationInterface {
26
27 /**
28 * The name of the integration.
29 *
30 * @since 1.5.0
31 * @return string
32 */
33 public function get_name() {
34 return '_woo_additional_terms';
35 }
36
37 /**
38 * When called invokes any initialization/setup for the integration.
39 *
40 * @since 1.5.0
41 * @return void
42 */
43 public function initialize() {
44 $this->register_frontend_scripts();
45 $this->register_editor_scripts();
46 $this->register_editor_blocks();
47 $this->extend_store_api();
48 add_filter( '__experimental_woocommerce_blocks_add_data_attributes_to_block', array( $this, 'add_attributes_to_frontend_blocks' ) );
49 add_action( 'woocommerce_store_api_checkout_update_order_from_request', array( $this, 'save_terms_acceptance' ), 10, 2 );
50 }
51
52 /**
53 * Registers all the static resources for the front-end.
54 *
55 * @since 1.5.0
56 * @return void
57 */
58 public function register_frontend_scripts() {
59 wp_register_script( WOO_ADDITIONAL_TERMS_SLUG . '-checkout', trailingslashit( WOO_ADDITIONAL_TERMS_DIR_URL ) . 'assets/js/' . WOO_ADDITIONAL_TERMS_MIN_DIR . 'checkout.js', array( 'react', 'wp-compose', 'wp-data', 'wp-element', 'wp-i18n', 'wc-blocks-data-store', 'wc-blocks-checkout', 'wc-settings' ), WOO_ADDITIONAL_TERMS_VERSION, true );
60 }
61
62 /**
63 * Registers all the static resources for the editor.
64 *
65 * @since 1.5.0
66 * @return void
67 */
68 public function register_editor_scripts() {
69 wp_register_script( WOO_ADDITIONAL_TERMS_SLUG . '-editor', trailingslashit( WOO_ADDITIONAL_TERMS_DIR_URL ) . 'assets/js/' . WOO_ADDITIONAL_TERMS_MIN_DIR . 'block.js', array( 'react', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-element', 'wp-i18n', 'wp-primitives', 'wc-blocks-checkout', 'wc-settings' ), WOO_ADDITIONAL_TERMS_VERSION, true );
70 }
71
72 /**
73 * Returns an array containing the handles of any scripts registered by our extension.
74 *
75 * @since 1.5.0
76 * @return array
77 */
78 public function get_script_handles() {
79 return array( WOO_ADDITIONAL_TERMS_SLUG . '-checkout' );
80 }
81
82 /**
83 * Returns an array containing the handles of any editor scripts registered by our extension.
84 *
85 * @since 1.5.0
86 * @return array
87 */
88 public function get_editor_script_handles() {
89 return array( WOO_ADDITIONAL_TERMS_SLUG . '-editor' );
90 }
91
92 /**
93 * Returns an associative array containing any data we want to be available to the scripts on the front-end.
94 *
95 * @since 1.5.0
96 * @return array
97 */
98 public function get_script_data() {
99 $notice = (string) get_option( '_woo_additional_terms_notice', '' );
100 $page_id = get_option( '_woo_additional_terms_page_id', null );
101
102 if (
103 $page_id
104 && ! empty( $page_id )
105 && ! empty( $notice )
106 && false !== strpos( $notice, '[additional-terms]' )
107 ) {
108 $notice = str_replace( '[additional-terms]', sprintf( '<a href="%s" class="woo-additional-terms__link" target="_blank" rel="noopener noreferrer nofollow">%s</a>', esc_url( get_permalink( $page_id ) ), esc_html( get_the_title( $page_id ) ) ), $notice );
109 }
110
111 $data = array(
112 'notice' => wp_kses(
113 $notice,
114 array(
115 'a' => array(
116 'href' => array(),
117 'class' => array(),
118 'target' => array(),
119 'rel' => array(),
120 ),
121 )
122 ),
123 'content' => Woo_Additional_Terms::terms_page_content( $page_id, false ),
124 );
125
126 return $data;
127 }
128
129 /**
130 * Register editor block.
131 *
132 * @since 1.5.0
133 * @return void
134 */
135 public function register_editor_blocks() {}
136
137 /**
138 * This allows dynamic (JS) blocks to access attributes in the frontend.
139 *
140 * @since 1.5.0
141 * @param array $allowed_blocks List of allowed blocks.
142 * @return array
143 */
144 public function add_attributes_to_frontend_blocks( $allowed_blocks ) {
145 $allowed_blocks[] = 'mypreview/woo-additional-terms';
146 return $allowed_blocks;
147 }
148
149 /**
150 * Fires after an order saved into the database.
151 * We will update the post meta
152 *
153 * @since 1.5.0
154 * @param WC_Order $order Order ID or order object.
155 * @param WP_REST_Request $request The API request currently being processed.
156 * @return void
157 * @phpcs:disable WordPress.Security.NonceVerification.Missing
158 */
159 public function save_terms_acceptance( $order, $request ) {
160 $acceptance = null;
161
162 if ( isset( $request['extensions'], $request['extensions']['_woo_additional_terms'], $request['extensions']['_woo_additional_terms']['wat_checkbox'] ) ) {
163 $acceptance = '1';
164 }
165
166 if ( $acceptance ) {
167 $order->update_meta_data( '_woo_additional_terms', $acceptance );
168 }
169 }
170
171 /**
172 * Add schema Store API to support posted data.
173 * Registers the checkout endpoint extension to inform our frontend component about the result of
174 * the validity of the additional terms checkbox and react accordingly.
175 *
176 * @since 1.5.0
177 * @return void
178 */
179 public function extend_store_api() {
180 $extend = StoreApi::container()->get(
181 ExtendSchema::class
182 );
183
184 $extend->register_endpoint_data(
185 array(
186 'endpoint' => CheckoutSchema::IDENTIFIER,
187 'namespace' => $this->get_name(),
188 'schema_callback' => function() {
189 return array(
190 'wat_checkbox' => array(
191 'type' => 'boolean',
192 'context' => array(),
193 'arg_options' => array(
194 'validate_callback' => function( $value ) {
195 if ( ! is_bool( $value ) ) {
196 /* translators: %s: Render the type of the variable. */
197 return new \WP_Error( 'api-error', sprintf( esc_html__( 'Value of field %s was posted with incorrect data type.', 'woo-additional-terms' ), gettype( $value ) ) );
198 }
199 return true;
200 },
201 ),
202 ),
203 );
204 },
205 )
206 );
207 }
208
209 }
210 endif;
211