abstracts
3 months ago
admin
3 months ago
frontend
1 month ago
integrations
1 month ago
v3
3 months ago
vendor
6 days ago
views
8 months ago
class-simplesalestax.php
6 days ago
class-sst-addresses.php
1 month ago
class-sst-ajax.php
3 months ago
class-sst-assets.php
6 months ago
class-sst-blocks-integration.php
1 month ago
class-sst-blocks.php
1 year ago
class-sst-certificates.php
6 days ago
class-sst-install.php
6 months ago
class-sst-logger.php
5 months ago
class-sst-marketplaces.php
6 months ago
class-sst-order-controller.php
3 months ago
class-sst-order.php
1 month ago
class-sst-origin-address.php
8 months ago
class-sst-product.php
3 months ago
class-sst-rate-limit.php
5 months ago
class-sst-settings.php
3 months ago
class-sst-shipping.php
3 years ago
class-sst-taxcloud-v3-api.php
3 months ago
class-sst-taxcloud-v3.php
3 months ago
class-sst-tic.php
2 years ago
class-sst-updater.php
3 years ago
sst-compatibility-functions.php
4 months ago
sst-functions.php
6 days ago
sst-message-functions.php
3 years ago
sst-update-functions.php
11 months ago
class-sst-blocks-integration.php
235 lines
| 1 | <?php |
| 2 | |
| 3 | if ( ! defined( 'ABSPATH' ) ) { |
| 4 | exit; |
| 5 | } |
| 6 | |
| 7 | use Automattic\WooCommerce\Blocks\Integrations\IntegrationInterface; |
| 8 | |
| 9 | /** |
| 10 | * Class for integrating with WooCommerce Blocks |
| 11 | */ |
| 12 | class SST_Blocks_Integration implements IntegrationInterface { |
| 13 | |
| 14 | /** |
| 15 | * The name of the integration. |
| 16 | * |
| 17 | * @return string |
| 18 | */ |
| 19 | public function get_name() { |
| 20 | return 'simple-sales-tax'; |
| 21 | } |
| 22 | |
| 23 | /** |
| 24 | * When called invokes any initialization/setup for the integration. |
| 25 | */ |
| 26 | public function initialize() { |
| 27 | $this->register_frontend_scripts(); |
| 28 | $this->register_editor_scripts(); |
| 29 | $this->register_block_styles(); |
| 30 | |
| 31 | add_filter( |
| 32 | 'the_content', |
| 33 | array( $this, 'force_exemption_block' ), |
| 34 | 1 |
| 35 | ); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Returns an array of script handles to enqueue in the frontend context. |
| 40 | * |
| 41 | * @return string[] |
| 42 | */ |
| 43 | public function get_script_handles() { |
| 44 | return array( 'sst-tax-exemption-block-frontend' ); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Returns an array of script handles to enqueue in the editor context. |
| 49 | * |
| 50 | * @return string[] |
| 51 | */ |
| 52 | public function get_editor_script_handles() { |
| 53 | return array( 'sst-tax-exemption-block-editor' ); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * An array of key, value pairs of data made available to the block on the client side. |
| 58 | * |
| 59 | * @return array |
| 60 | */ |
| 61 | public function get_script_data() { |
| 62 | $certificates = SST_Certificates::get_certificates_formatted(); |
| 63 | $options = array( |
| 64 | 'new' => 'Add new certificate', |
| 65 | ); |
| 66 | |
| 67 | foreach ( $certificates as $cert ) { |
| 68 | $options[ $cert['CertificateID'] ] = $cert['Description']; |
| 69 | } |
| 70 | |
| 71 | $selected = WC()->session |
| 72 | ? WC()->session->get( 'sst_certificate_id', '' ) |
| 73 | : ''; |
| 74 | |
| 75 | return array( |
| 76 | 'showExemptionForm' => sst_should_show_tax_exemption_form(), |
| 77 | 'certificateOptions' => $options, |
| 78 | 'selectedCertificate' => $selected, |
| 79 | 'isUserLoggedIn' => is_user_logged_in(), |
| 80 | 'myAccountEndpointUrl' => wc_get_account_endpoint_url( 'exemption-certificates' ), |
| 81 | ); |
| 82 | } |
| 83 | |
| 84 | public function register_block_styles() { |
| 85 | add_action( 'enqueue_block_assets', array( $this, 'enqueue_block_styles' ) ); |
| 86 | } |
| 87 | |
| 88 | public function enqueue_block_styles() { |
| 89 | $style_path = 'build/style-tax-exemption-block.css'; |
| 90 | $style_url = SST()->url( $style_path ); |
| 91 | |
| 92 | wp_enqueue_style( |
| 93 | 'sst-tax-exemption-block', |
| 94 | $style_url, |
| 95 | [], |
| 96 | $this->get_file_version( $style_path ) |
| 97 | ); |
| 98 | } |
| 99 | |
| 100 | public function register_editor_scripts() { |
| 101 | $script_url = SST()->url( 'build/tax-exemption-block.js' ); |
| 102 | $script_asset_path = SST()->path( 'build/tax-exemption-block.asset.php' ); |
| 103 | $script_asset = file_exists( $script_asset_path ) |
| 104 | ? require $script_asset_path |
| 105 | : array( |
| 106 | 'dependencies' => array(), |
| 107 | 'version' => $this->get_file_version( $script_asset_path ), |
| 108 | ); |
| 109 | |
| 110 | wp_register_script( |
| 111 | 'sst-tax-exemption-block-editor', |
| 112 | $script_url, |
| 113 | $script_asset['dependencies'], |
| 114 | $script_asset['version'], |
| 115 | true |
| 116 | ); |
| 117 | |
| 118 | wp_set_script_translations( |
| 119 | 'sst-tax-exemption-block-editor', |
| 120 | 'simple-sales-tax', |
| 121 | SST()->path( 'languages' ) |
| 122 | ); |
| 123 | } |
| 124 | |
| 125 | public function register_frontend_scripts() { |
| 126 | $script_url = SST()->url( 'build/tax-exemption-block-frontend.js' ); |
| 127 | $script_asset_path = SST()->path( 'build/tax-exemption-block-frontend.asset.php' ); |
| 128 | $script_asset = file_exists( $script_asset_path ) |
| 129 | ? require $script_asset_path |
| 130 | : array( |
| 131 | 'dependencies' => array(), |
| 132 | 'version' => $this->get_file_version( $script_asset_path ), |
| 133 | ); |
| 134 | |
| 135 | wp_register_script( |
| 136 | 'sst-tax-exemption-block-frontend', |
| 137 | $script_url, |
| 138 | $script_asset['dependencies'], |
| 139 | $script_asset['version'], |
| 140 | true |
| 141 | ); |
| 142 | |
| 143 | wp_set_script_translations( |
| 144 | 'sst-tax-exemption-block-frontend', |
| 145 | 'simple-sales-tax', |
| 146 | SST()->path( 'languages' ) |
| 147 | ); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Get the file modified time as a cache buster if we're in dev mode. |
| 152 | * |
| 153 | * @param string $file Local path to the file. |
| 154 | * @return string The cache buster value to use for the given file. |
| 155 | */ |
| 156 | protected function get_file_version( $file ) { |
| 157 | $file_path = SST()->path( $file ); |
| 158 | if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG && file_exists( $file_path ) ) { |
| 159 | return filemtime( $file_path ); |
| 160 | } |
| 161 | return SST()->version; |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Inserts the exemption block into the WooCommerce checkout fields block. |
| 166 | * |
| 167 | * @param array &$blocks Checkout page blocks |
| 168 | */ |
| 169 | protected function insert_exemption_block( &$blocks ) { |
| 170 | $exemption_block = array( |
| 171 | 'blockName' => 'simple-sales-tax/tax-exemption', |
| 172 | 'attrs' => array(), |
| 173 | 'innerBlocks' => array(), |
| 174 | 'innerHTML' => '<div data-block-name="simple-sales-tax/tax-exemption" class="wp-block-simple-sales-tax-tax-exemption"></div>', |
| 175 | 'innerContent' => array( |
| 176 | '<div data-block-name="simple-sales-tax/tax-exemption" class="wp-block-simple-sales-tax-tax-exemption"></div>', |
| 177 | ), |
| 178 | ); |
| 179 | |
| 180 | $insert_before = array( |
| 181 | 'woocommerce/checkout-terms-block', |
| 182 | 'woocommerce/checkout-actions-block', |
| 183 | ); |
| 184 | |
| 185 | foreach ( $blocks as &$block ) { |
| 186 | $block_name = $block['blockName']; |
| 187 | |
| 188 | if ( $block_name === 'woocommerce/checkout-fields-block' ) { |
| 189 | $insert_key = count( $block['innerBlocks'] ); |
| 190 | |
| 191 | foreach ( $block['innerBlocks'] as $key => $inner_block ) { |
| 192 | if ( in_array( $inner_block['blockName'], $insert_before ) ) { |
| 193 | $insert_key = $key; |
| 194 | break; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | array_splice( |
| 199 | $block['innerBlocks'], |
| 200 | $insert_key, |
| 201 | 0, |
| 202 | array( $exemption_block ) |
| 203 | ); |
| 204 | |
| 205 | return $blocks; |
| 206 | } else { |
| 207 | $block['innerBlocks'] = $this->insert_exemption_block( |
| 208 | $block['innerBlocks'] |
| 209 | ); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | return $blocks; |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Force exemption block into checkout page markup after payment block. |
| 218 | */ |
| 219 | public function force_exemption_block( $content ) { |
| 220 | if ( ! has_block( 'woocommerce/checkout' ) ) { |
| 221 | return $content; |
| 222 | } |
| 223 | |
| 224 | if ( has_block( 'simple-sales-tax/tax-exemption' ) ) { |
| 225 | return $content; |
| 226 | } |
| 227 | |
| 228 | $blocks = parse_blocks( $content ); |
| 229 | $new_blocks = $this->insert_exemption_block( $blocks ); |
| 230 | |
| 231 | return serialize_blocks( $new_blocks ); |
| 232 | } |
| 233 | |
| 234 | } |
| 235 |