PluginProbe
Additional Terms Lite for WooCommerce / 1.5.2
Additional Terms Lite for WooCommerce v1.5.2
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.2, at includes/class-wat-checkout-block-integration.php

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