| 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 Exception; |
| 17 |
use WP_REST_Request; |
| 18 |
use Automattic\WooCommerce\Blocks; |
| 19 |
use Automattic\WooCommerce\StoreApi; |
| 20 |
use Woo_Additional_Terms\Admin; |
| 21 |
|
| 22 |
/** |
| 23 |
* Checkout block class. |
| 24 |
*/ |
| 25 |
class Block implements Blocks\Integrations\IntegrationInterface { |
| 26 |
|
| 27 |
/** |
| 28 |
* The name of the integration. |
| 29 |
* |
| 30 |
* @since 1.5.0 |
| 31 |
* |
| 32 |
* @return string |
| 33 |
*/ |
| 34 |
public function get_name() { |
| 35 |
|
| 36 |
return '_woo_additional_terms'; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* When called invokes any initialization/setup for the integration. |
| 41 |
* |
| 42 |
* @since 1.5.0 |
| 43 |
* |
| 44 |
* @return void |
| 45 |
*/ |
| 46 |
public function initialize() { |
| 47 |
|
| 48 |
$this->extend_store_api(); |
| 49 |
|
| 50 |
add_filter( '__experimental_woocommerce_blocks_add_data_attributes_to_block', array( $this, 'add_attributes_to_frontend_blocks' ) ); |
| 51 |
add_action( 'woocommerce_store_api_checkout_update_order_from_request', array( $this, 'process_acceptance' ), 10, 2 ); |
| 52 |
add_action( 'woo_additional_terms_checkout_save_acceptance', array( $this, 'save_acceptance' ), 10, 2 ); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* This allows dynamic (JS) blocks to access attributes in the frontend. |
| 57 |
* |
| 58 |
* @since 1.5.0 |
| 59 |
* |
| 60 |
* @param array $allowed_blocks List of allowed blocks. |
| 61 |
* |
| 62 |
* @return array |
| 63 |
*/ |
| 64 |
public function add_attributes_to_frontend_blocks( $allowed_blocks ) { |
| 65 |
|
| 66 |
$allowed_blocks[] = 'mypreview/woo-additional-terms'; |
| 67 |
|
| 68 |
return $allowed_blocks; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Fires after an order saved into the database. |
| 73 |
* We will update the post meta. |
| 74 |
* |
| 75 |
* @since 1.5.0 |
| 76 |
* |
| 77 |
* @param WC_Order $order Order ID or order object. |
| 78 |
* @param WP_REST_Request $request The API request currently being processed. |
| 79 |
* |
| 80 |
* @return void |
| 81 |
*/ |
| 82 |
public function process_acceptance( $order, $request ) { |
| 83 |
|
| 84 |
if ( ! isset( $request['extensions'], $request['extensions']['_woo_additional_terms'] ) ) { |
| 85 |
return; |
| 86 |
} |
| 87 |
|
| 88 |
$has_accepted = empty( $request['extensions']['_woo_additional_terms']['data'] ) ? null : wc_string_to_bool( $request['extensions']['_woo_additional_terms']['data'] ); |
| 89 |
|
| 90 |
/** |
| 91 |
* Fires after additional terms submissions is about to be saved. |
| 92 |
* |
| 93 |
* @since 1.6.1 |
| 94 |
* |
| 95 |
* @param null|bool $has_accepted Whether the additional terms has been accepted. |
| 96 |
* @param WC_Order $order Order object. |
| 97 |
*/ |
| 98 |
do_action( 'woo_additional_terms_checkout_save_acceptance', $has_accepted, $order ); |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Stores additional terms submissions after a new order being processed. |
| 103 |
* |
| 104 |
* @since 1.6.1 |
| 105 |
* |
| 106 |
* @param null|bool $has_accepted Whether the additional terms has been accepted. |
| 107 |
* @param WC_Order $order Order object. |
| 108 |
* |
| 109 |
* @return void |
| 110 |
*/ |
| 111 |
public function save_acceptance( $has_accepted, $order ) { |
| 112 |
|
| 113 |
// Leave if the order is not an instance of WC_Order. |
| 114 |
if ( ! $order instanceof WC_Order || is_null( $has_accepted ) ) { |
| 115 |
return; |
| 116 |
} |
| 117 |
|
| 118 |
// Add order note based on acceptance status. |
| 119 |
$note_message = $has_accepted |
| 120 |
? __( 'Customer accepted the additional terms.', 'woo-additional-terms' ) |
| 121 |
: __( 'Customer did not accept the additional terms.', 'woo-additional-terms' ); |
| 122 |
|
| 123 |
$order->add_order_note( $note_message ); |
| 124 |
|
| 125 |
// Save the additional terms checkbox value as order meta. |
| 126 |
update_post_meta( $order->get_id(), Admin\Order::META_KEY, wc_bool_to_string( $has_accepted ) ); |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Returns an array containing the handles of any scripts registered by our extension. |
| 131 |
* |
| 132 |
* @since 1.5.0 |
| 133 |
* |
| 134 |
* @return array |
| 135 |
*/ |
| 136 |
public function get_script_handles() { |
| 137 |
|
| 138 |
return array( 'woo-additional-terms', 'woo-additional-terms-checkout' ); |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Returns an array containing the handles of any editor scripts registered by our extension. |
| 143 |
* |
| 144 |
* @since 1.5.0 |
| 145 |
* |
| 146 |
* @return array |
| 147 |
*/ |
| 148 |
public function get_editor_script_handles() { |
| 149 |
|
| 150 |
return array( 'woo-additional-terms-block' ); |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Returns an associative array containing any data we want to be available to the scripts. |
| 155 |
* |
| 156 |
* @since 1.5.0 |
| 157 |
* |
| 158 |
* @return array |
| 159 |
*/ |
| 160 |
public function get_script_data() { |
| 161 |
|
| 162 |
$status = woo_additional_terms()->service( 'options' )->get( 'status', '' ); |
| 163 |
|
| 164 |
// Bail early, in case settings status is not enabled. |
| 165 |
if ( ! wc_string_to_bool( $status ) ) { |
| 166 |
return array(); |
| 167 |
} |
| 168 |
|
| 169 |
// Enqueue the frontend styles. |
| 170 |
wp_enqueue_style( 'woo-additional-terms' ); |
| 171 |
|
| 172 |
return array( |
| 173 |
'is_required' => wc_string_to_bool( woo_additional_terms()->service( 'options' )->get( 'required', 'no' ) ), |
| 174 |
'display_action' => woo_additional_terms()->service( 'options' )->get( 'action', 'embed' ), |
| 175 |
'page_content' => woo_additional_terms()->service( 'terms' )->get( 'content' ), |
| 176 |
'checkbox_label' => woo_additional_terms()->service( 'terms' )->get( 'label' ), |
| 177 |
'error_message' => woo_additional_terms()->service( 'options' )->get( 'error', __( 'Please accept the additional terms to continue.', 'woo-additional-terms' ) ), |
| 178 |
); |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* Add schema Store API to support posted data. |
| 183 |
* Registers the checkout endpoint extension to inform our frontend component about the result of |
| 184 |
* the validity of the additional terms checkbox and react accordingly. |
| 185 |
* |
| 186 |
* @since 1.5.0 |
| 187 |
* |
| 188 |
* @throws Exception When the schema callback is not callable. |
| 189 |
* |
| 190 |
* @return void |
| 191 |
*/ |
| 192 |
public function extend_store_api() { |
| 193 |
|
| 194 |
$extend = StoreApi\StoreApi::container()->get( StoreApi\Schemas\ExtendSchema::class ); |
| 195 |
|
| 196 |
$extend->register_endpoint_data( |
| 197 |
array( |
| 198 |
'endpoint' => Blocks\StoreApi\Schemas\CheckoutSchema::IDENTIFIER, |
| 199 |
'namespace' => $this->get_name(), |
| 200 |
'schema_callback' => fn() => array( |
| 201 |
'data' => array( |
| 202 |
'type' => 'string', |
| 203 |
'context' => array(), |
| 204 |
'arg_options' => array( |
| 205 |
'validate_callback' => function( $value ) { |
| 206 |
|
| 207 |
if ( ! is_string( $value ) ) { |
| 208 |
/* translators: %s: Render the type of the variable. */ |
| 209 |
return new WP_Error( 'api-error', sprintf( esc_html__( 'Value of field %s was posted with incorrect data type.', 'woo-additional-terms' ), gettype( $value ) ) ); |
| 210 |
} |
| 211 |
|
| 212 |
return true; |
| 213 |
}, |
| 214 |
), |
| 215 |
), |
| 216 |
), |
| 217 |
) |
| 218 |
); |
| 219 |
} |
| 220 |
} |
| 221 |
|