| 1 |
<?php |
| 2 |
/** |
| 3 |
* WooCommerce checkout editor block. |
| 4 |
* |
| 5 |
* @author MyPreview (Github: @mahdiyazdani, @gooklani, @mypreview) |
| 6 |
* |
| 7 |
* @since 1.5.0 |
| 8 |
* |
| 9 |
* @package woo-additional-terms |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace Woo_Additional_Terms\WooCommerce\Block; |
| 13 |
|
| 14 |
use WP_Error; |
| 15 |
use WC_Order; |
| 16 |
use WP_REST_Request; |
| 17 |
use Automattic\WooCommerce\Blocks; |
| 18 |
use Automattic\WooCommerce\StoreApi; |
| 19 |
|
| 20 |
/** |
| 21 |
* Checkout block class. |
| 22 |
*/ |
| 23 |
class Block implements Blocks\Integrations\IntegrationInterface { |
| 24 |
|
| 25 |
/** |
| 26 |
* The name of the integration. |
| 27 |
* |
| 28 |
* @since 1.5.0 |
| 29 |
* |
| 30 |
* @return string |
| 31 |
*/ |
| 32 |
public function get_name() { |
| 33 |
|
| 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 |
* |
| 42 |
* @return void |
| 43 |
*/ |
| 44 |
public function initialize() { |
| 45 |
|
| 46 |
$this->extend_store_api(); |
| 47 |
|
| 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_acceptance' ), 10, 2 ); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* This allows dynamic (JS) blocks to access attributes in the frontend. |
| 54 |
* |
| 55 |
* @since 1.5.0 |
| 56 |
* |
| 57 |
* @param array $allowed_blocks List of allowed blocks. |
| 58 |
* |
| 59 |
* @return array |
| 60 |
*/ |
| 61 |
public function add_attributes_to_frontend_blocks( $allowed_blocks ) { |
| 62 |
|
| 63 |
$allowed_blocks[] = 'mypreview/woo-additional-terms'; |
| 64 |
|
| 65 |
return $allowed_blocks; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Fires after an order saved into the database. |
| 70 |
* We will update the post meta |
| 71 |
* |
| 72 |
* @since 1.5.0 |
| 73 |
* |
| 74 |
* @param WC_Order $order Order ID or order object. |
| 75 |
* @param WP_REST_Request $request The API request currently being processed. |
| 76 |
* |
| 77 |
* @return void |
| 78 |
*/ |
| 79 |
public function save_acceptance( $order, $request ) { |
| 80 |
|
| 81 |
if ( ! isset( $request['extensions'], $request['extensions']['_woo_additional_terms'] ) ) { |
| 82 |
return; |
| 83 |
} |
| 84 |
|
| 85 |
// Save the additional terms checkbox value as order meta. |
| 86 |
$order->update_meta_data( '_woo_additional_terms', wc_bool_to_string( isset( $request['extensions']['_woo_additional_terms']['wat_checkbox'] ) ) ); |
| 87 |
|
| 88 |
// If the additional terms checkbox is checked, add note that customer accepted if not add note tha customer didn't accept. |
| 89 |
if ( isset( $request['extensions']['_woo_additional_terms']['wat_checkbox'] ) && wc_string_to_bool( $request['extensions']['_woo_additional_terms']['wat_checkbox'] ) ) { |
| 90 |
$order->add_order_note( |
| 91 |
__( 'Customer accepted the additional terms.', 'woo-additional-terms' ) |
| 92 |
); |
| 93 |
|
| 94 |
return; |
| 95 |
} |
| 96 |
|
| 97 |
$order->add_order_note( |
| 98 |
__( 'Customer did not accept the additional terms.', 'woo-additional-terms' ) |
| 99 |
); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Returns an array containing the handles of any scripts registered by our extension. |
| 104 |
* |
| 105 |
* @since 1.5.0 |
| 106 |
* |
| 107 |
* @return array |
| 108 |
*/ |
| 109 |
public function get_script_handles() { |
| 110 |
|
| 111 |
return array( 'woo-additional-terms', 'woo-additional-terms-checkout' ); |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Returns an array containing the handles of any editor scripts registered by our extension. |
| 116 |
* |
| 117 |
* @since 1.5.0 |
| 118 |
* |
| 119 |
* @return array |
| 120 |
*/ |
| 121 |
public function get_editor_script_handles() { |
| 122 |
|
| 123 |
return array( 'woo-additional-terms-block' ); |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Returns an associative array containing any data we want to be available to the scripts. |
| 128 |
* |
| 129 |
* @since 1.5.0 |
| 130 |
* |
| 131 |
* @return array |
| 132 |
*/ |
| 133 |
public function get_script_data() { |
| 134 |
|
| 135 |
$status = woo_additional_terms()->service( 'options' )->get( 'status', '' ); |
| 136 |
|
| 137 |
// Bail early, in case settings status is not enabled. |
| 138 |
if ( ! wc_string_to_bool( $status ) ) { |
| 139 |
return array(); |
| 140 |
} |
| 141 |
|
| 142 |
// Enqueue the frontend styles. |
| 143 |
wp_enqueue_style( 'woo-additional-terms' ); |
| 144 |
|
| 145 |
return array( |
| 146 |
'is_required' => wc_string_to_bool( woo_additional_terms()->service( 'options' )->get( 'required', 'no' ) ), |
| 147 |
'display_action' => woo_additional_terms()->service( 'options' )->get( 'action', 'embed' ), |
| 148 |
'page_content' => woo_additional_terms()->service( 'terms' )->get( 'content' ), |
| 149 |
'checkbox_label' => woo_additional_terms()->service( 'terms' )->get( 'label' ), |
| 150 |
'error_message' => woo_additional_terms()->service( 'options' )->get( 'error', __( 'Please accept the additional terms to continue.', 'woo-additional-terms' ) ), |
| 151 |
); |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Add schema Store API to support posted data. |
| 156 |
* Registers the checkout endpoint extension to inform our frontend component about the result of |
| 157 |
* the validity of the additional terms checkbox and react accordingly. |
| 158 |
* |
| 159 |
* @since 1.5.0 |
| 160 |
* |
| 161 |
* @throws Exception When the schema callback is not callable. |
| 162 |
* |
| 163 |
* @return void |
| 164 |
*/ |
| 165 |
public function extend_store_api() { |
| 166 |
|
| 167 |
$extend = StoreApi\StoreApi::container()->get( StoreApi\Schemas\ExtendSchema::class ); |
| 168 |
|
| 169 |
$extend->register_endpoint_data( |
| 170 |
array( |
| 171 |
'endpoint' => Blocks\StoreApi\Schemas\CheckoutSchema::IDENTIFIER, |
| 172 |
'namespace' => $this->get_name(), |
| 173 |
'schema_callback' => fn() => array( |
| 174 |
'wat_checkbox' => array( |
| 175 |
'type' => 'boolean', |
| 176 |
'context' => array(), |
| 177 |
'arg_options' => array( |
| 178 |
'validate_callback' => function( $value ) { |
| 179 |
|
| 180 |
if ( ! is_bool( $value ) ) { |
| 181 |
/* translators: %s: Render the type of the variable. */ |
| 182 |
return new WP_Error( 'api-error', sprintf( esc_html__( 'Value of field %s was posted with incorrect data type.', 'woo-additional-terms' ), gettype( $value ) ) ); |
| 183 |
} |
| 184 |
|
| 185 |
return true; |
| 186 |
}, |
| 187 |
), |
| 188 |
), |
| 189 |
), |
| 190 |
) |
| 191 |
); |
| 192 |
} |
| 193 |
} |
| 194 |
|