| 1 |
<?php |
| 2 |
/** |
| 3 |
* WooCommerce checkout editor block. |
| 4 |
* |
| 5 |
* @since 1.5.0 |
| 6 |
* |
| 7 |
* @package woo-additional-terms |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace Woo_Additional_Terms\WooCommerce\Block; |
| 11 |
|
| 12 |
use Automattic\WooCommerce\Blocks\Integrations\IntegrationInterface; |
| 13 |
|
| 14 |
/** |
| 15 |
* Checkout block class. |
| 16 |
*/ |
| 17 |
class Block implements IntegrationInterface { |
| 18 |
|
| 19 |
/** |
| 20 |
* The name of the integration. |
| 21 |
* |
| 22 |
* @since 1.5.0 |
| 23 |
* |
| 24 |
* @return string |
| 25 |
*/ |
| 26 |
const NAME = '_woo_additional_terms'; |
| 27 |
|
| 28 |
/** |
| 29 |
* The name of the block. |
| 30 |
* |
| 31 |
* @since 1.6.7 |
| 32 |
* |
| 33 |
* @return string |
| 34 |
*/ |
| 35 |
const IDENTIFIER = 'mypreview/woo-additional-terms'; |
| 36 |
|
| 37 |
/** |
| 38 |
* The name of the integration. |
| 39 |
* |
| 40 |
* @since 1.5.0 |
| 41 |
* |
| 42 |
* @return string |
| 43 |
*/ |
| 44 |
public function get_name() { |
| 45 |
|
| 46 |
return self::NAME; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* When called invokes any initialization/setup for the integration. |
| 51 |
* |
| 52 |
* @since 1.5.0 |
| 53 |
* |
| 54 |
* @return void |
| 55 |
*/ |
| 56 |
public function initialize() { |
| 57 |
|
| 58 |
add_filter( '__experimental_woocommerce_blocks_add_data_attributes_to_block', array( $this, 'add_attributes_to_frontend_blocks' ) ); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* This allows dynamic (JS) blocks to access attributes in the frontend. |
| 63 |
* |
| 64 |
* @since 1.5.0 |
| 65 |
* |
| 66 |
* @param array $allowed_blocks List of allowed blocks. |
| 67 |
* |
| 68 |
* @return array |
| 69 |
*/ |
| 70 |
public function add_attributes_to_frontend_blocks( $allowed_blocks ) { |
| 71 |
|
| 72 |
$allowed_blocks[] = self::IDENTIFIER; |
| 73 |
|
| 74 |
return $allowed_blocks; |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Returns an array containing the handles of any scripts registered by our extension. |
| 79 |
* |
| 80 |
* @since 1.5.0 |
| 81 |
* |
| 82 |
* @return array |
| 83 |
*/ |
| 84 |
public function get_script_handles() { |
| 85 |
|
| 86 |
return array( 'woo-additional-terms', 'woo-additional-terms-checkout' ); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Returns an array containing the handles of any editor scripts registered by our extension. |
| 91 |
* |
| 92 |
* @since 1.5.0 |
| 93 |
* |
| 94 |
* @return array |
| 95 |
*/ |
| 96 |
public function get_editor_script_handles() { |
| 97 |
|
| 98 |
return array( 'woo-additional-terms-block' ); |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Returns an associative array containing any data we want to be available to the scripts. |
| 103 |
* |
| 104 |
* @since 1.5.0 |
| 105 |
* |
| 106 |
* @return array |
| 107 |
*/ |
| 108 |
public function get_script_data() { |
| 109 |
|
| 110 |
$status = woo_additional_terms()->service( 'options' )->get( 'status', '' ); |
| 111 |
|
| 112 |
// Bail early, in case settings status is not enabled. |
| 113 |
if ( ! wc_string_to_bool( $status ) ) { |
| 114 |
return array(); |
| 115 |
} |
| 116 |
|
| 117 |
// Enqueue the frontend styles. |
| 118 |
wp_enqueue_style( 'woo-additional-terms' ); |
| 119 |
|
| 120 |
return array( |
| 121 |
'is_required' => wc_string_to_bool( woo_additional_terms()->service( 'options' )->get( 'required', 'no' ) ), |
| 122 |
'display_action' => woo_additional_terms()->service( 'options' )->get( 'action', 'embed' ), |
| 123 |
'page_content' => woo_additional_terms()->service( 'terms' )->get( 'content' ), |
| 124 |
'checkbox_label' => woo_additional_terms()->service( 'terms' )->get( 'label' ), |
| 125 |
'error_message' => woo_additional_terms()->service( 'terms' )->get( 'error' ), |
| 126 |
); |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Get the file modified time as a cache buster if we're in dev mode. |
| 131 |
* |
| 132 |
* @since 1.5.0 |
| 133 |
* |
| 134 |
* @param string $file Local path to the file. |
| 135 |
* |
| 136 |
* @return string |
| 137 |
*/ |
| 138 |
protected function get_file_version( $file ) { |
| 139 |
|
| 140 |
if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG && file_exists( $file ) ) { |
| 141 |
return filemtime( $file ); |
| 142 |
} |
| 143 |
|
| 144 |
return woo_additional_terms()->get_version(); |
| 145 |
} |
| 146 |
} |
| 147 |
|