PluginProbe ʕ •ᴥ•ʔ
WooCommerce Square / 4.8.0
WooCommerce Square v4.8.0
5.5.0 5.4.3 5.4.2 5.4.1 5.4.0 trunk 1.0.25 1.0.26 1.0.27 1.0.28 1.0.29 1.0.30 1.0.31 1.0.32 1.0.33 1.0.34 1.0.35 1.0.36 1.0.37 1.0.38 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.4.0 2.4.1 2.5.0 2.5.1 2.5.2 2.5.3 2.6.0 2.7.0 2.8.0 2.9.0 2.9.1 3.0.0 3.0.1 3.0.2 3.0.3 3.1.0 3.2.0 3.3.0 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.6.1 3.7.0 3.7.1 3.8.0 3.8.1 3.8.2 3.8.3 3.9.0 4.0.0 4.1.0 4.2.0 4.2.1 4.2.2 4.2.3 4.3.0 4.3.1 4.3.2 4.4.0 4.4.1 4.4.2 4.5.0 4.5.1 4.5.2 4.6.0 4.6.1 4.6.2 4.6.3 4.6.4 4.7.0 4.7.1 4.7.2 4.7.3 4.7.4 4.8.0 4.8.1 4.8.2 4.8.3 4.8.4 4.8.5 4.8.6 4.8.7 4.8.8 4.9.0 4.9.1 4.9.2 4.9.3 4.9.4 4.9.5 4.9.6 4.9.7 4.9.8 4.9.9 5.0.0 5.0.1 5.1.0 5.1.1 5.1.2 5.2.0 5.3.0 5.3.1 5.3.2 5.3.3
woocommerce-square / includes / Admin / Product_Editor_Compatibility.php
woocommerce-square / includes / Admin Last commit date
Analytics 2 years ago Rest 2 years ago Privacy.php 3 years ago Product_Editor_Compatibility.php 1 year ago Settings_Page.php 3 years ago Sync_Page.php 3 years ago
Product_Editor_Compatibility.php
242 lines
1 <?php
2
3 namespace WooCommerce\Square\Admin;
4
5 use Automattic\WooCommerce\Utilities\FeaturesUtil;
6 use Automattic\WooCommerce\Admin\Features\ProductBlockEditor\BlockRegistry;
7 use Automattic\WooCommerce\Internal\Admin\Features\ProductBlockEditor\ProductTemplates\ProductBlock;
8 use Automattic\WooCommerce\Internal\Admin\Features\ProductBlockEditor\ProductTemplates\Section;
9 use WooCommerce\Square\Handlers\Product;
10
11 class Product_Editor_Compatibility {
12 public function __construct() {
13 if ( ! FeaturesUtil::feature_is_enabled( 'product_block_editor' ) ) {
14 return;
15 }
16
17 if ( wc_square()->get_settings_handler()->is_system_of_record_disabled() ) {
18 return;
19 }
20
21 add_action(
22 'init',
23 array( $this, 'register_custom_blocks' )
24 );
25
26 add_filter(
27 'woocommerce_rest_prepare_product_object',
28 array( $this, 'add_data_to_product_response' ),
29 10,
30 2
31 );
32
33 add_action(
34 'woocommerce_rest_insert_product_object',
35 array( $this, 'process_data_before_save' ),
36 10,
37 2
38 );
39
40 add_action(
41 'woocommerce_block_template_area_product-form_after_add_block_basic-details',
42 array( $this, 'add_sync_with_square_control' )
43 );
44
45 add_action(
46 'woocommerce_block_template_area_product-form_after_add_block_product-track-stock',
47 array( $this, 'add_inventory_control' ),
48 );
49
50 add_action(
51 'woocommerce_block_template_after_add_block',
52 array( $this, 'remove_core_blocks' )
53 );
54 }
55
56 /**
57 * Registers the custom product field blocks.
58 */
59 public function register_custom_blocks() {
60 if ( isset( $_GET['page'] ) && 'wc-admin' === $_GET['page'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
61 BlockRegistry::get_instance()->register_block_type_from_metadata( WC_SQUARE_PLUGIN_PATH . '/build/admin/product-blocks/stock-management-field' );
62 BlockRegistry::get_instance()->register_block_type_from_metadata( WC_SQUARE_PLUGIN_PATH . '/build/admin/product-blocks/stock-quantity-field' );
63 BlockRegistry::get_instance()->register_block_type_from_metadata( WC_SQUARE_PLUGIN_PATH . '/build/admin/product-blocks/sync-with-square-field' );
64 }
65 }
66
67 /**
68 * Adds Square product meta to the product response.
69 *
70 * @param \WP_REST_Response $response The response object.
71 * @param \WC_Product $product The product object.
72 *
73 * @return WP_REST_Response
74 */
75 public function add_data_to_product_response( $response, $product ) {
76 $data = $response->get_data();
77 $data['is_sync_enabled'] = wc_square()->get_settings_handler()->is_product_sync_enabled();
78 $data['is_inventory_sync_enabled'] = wc_square()->get_settings_handler()->is_inventory_sync_enabled();
79 $data['is_square_synced'] = Product::is_synced_with_square( $product );
80 $data['edit_link'] = Product::get_product_edit_link( $product );
81 $data['sor'] = wc_square()->get_settings_handler()->get_system_of_record();
82 $data['fetch_stock_nonce'] = wp_create_nonce( 'fetch-product-stock-with-square' );
83 $data['stock_quantity'] = $product->get_stock_quantity();
84 $data['is_gift_card'] = Product::is_gift_card( $product ) ? 'yes' : 'no';
85 $response->set_data( $data );
86
87 return $response;
88 }
89
90 /**
91 * Saves custom data to product metadata.
92 *
93 * @param \WC_Product $product The product object.
94 * @param \WP_REST_Request $request The request object.
95 */
96 public function process_data_before_save( $product, $request ) {
97 $is_square_synced = $request->get_param( 'is_square_synced' );
98 $stock_quantity = $request->get_param( 'stock_quantity' );
99 $is_gift_card = $request->get_param( 'is_gift_card' );
100 $is_virtual = $request->get_param( 'virtual' );
101
102 if ( ! is_null( $is_square_synced ) ) {
103 if ( $is_square_synced ) {
104 Product::set_synced_with_square( $product, 'yes' );
105 } else {
106 Product::set_synced_with_square( $product, 'no' );
107 }
108 }
109
110 if ( ! is_null( $stock_quantity ) && ! empty( $stock_quantity ) ) {
111 $product->set_stock_quantity( $stock_quantity );
112 }
113
114 if ( 'yes' === $is_gift_card && ( is_null( $is_virtual ) || $is_virtual ) ) {
115 $product->update_meta_data( Product::SQUARE_GIFT_CARD_KEY, 'yes' );
116 $product->set_virtual( true );
117 $product->set_sold_individually( 'yes' );
118 $product->set_tax_status( 'none' );
119 } elseif ( 'no' === $is_gift_card || ! $is_virtual ) {
120 $product->delete_meta_data( Product::SQUARE_GIFT_CARD_KEY );
121 $product->set_virtual( false );
122 $product->set_sold_individually( 'no' );
123 $product->set_tax_status( 'taxable' );
124 }
125
126 $product->save();
127 }
128
129 /**
130 * Adds the sync with Square control to the product editor.
131 *
132 * @param Section $basic_details_field The basic details field block.
133 */
134 public function add_sync_with_square_control( $basic_details_field ) {
135 $gift_card_settings = get_option( 'woocommerce_gift_cards_pay_settings', array() );
136
137 if ( isset( $gift_card_settings['enabled'] ) && 'yes' === $gift_card_settings['enabled'] ) {
138 $basic_details_field->add_block(
139 array(
140 'id' => '_square_gift_card',
141 'blockName' => 'woocommerce/product-checkbox-field',
142 'attributes' => array(
143 'title' => __( 'Square Gift Card', 'woocommerce-square' ),
144 'label' => __( 'Enable to create this product as a gift card', 'woocommerce-square' ),
145 'property' => 'is_gift_card',
146 'checkedValue' => 'yes',
147 'uncheckedValue' => 'no',
148 ),
149 'hideConditions' => array(
150 array(
151 'expression' => '"simple" !== editedProduct.type && "variable" !== editedProduct.type',
152 ),
153 ),
154 )
155 );
156 }
157
158 $basic_details_field->add_block(
159 array(
160 'id' => '_wc_square_synced',
161 'blockName' => 'woocommerce-square/sync-with-square-field',
162 'attributes' => array(
163 'title' => __( 'Sync with Square', 'woocommerce-square' ),
164 ),
165 'hideConditions' => array(
166 array(
167 'expression' => '!editedProduct.is_sync_enabled || editedProduct.is_gift_card === "yes"',
168 ),
169 ),
170 )
171 );
172 }
173
174 /**
175 * Adds the inventory control to the product editor.
176 *
177 * @param Section $sku_field The SKU field block.
178 */
179 public function add_inventory_control( $sku_field ) {
180 /**
181 * Inventory section block.
182 *
183 * @var Section $parent
184 */
185 $parent = $sku_field->get_parent();
186
187 $is_inventory_sync_enabled = wc_square()->get_settings_handler()->is_inventory_sync_enabled();
188
189 if ( ! $is_inventory_sync_enabled ) {
190 return;
191 }
192
193 $parent->add_block(
194 array(
195 'id' => '_wc_square_stock_management_field',
196 'blockName' => 'woocommerce-square/stock-management-field',
197 'attributes' => array(
198 'disabled' => 'yes' !== get_option( 'woocommerce_manage_stock' ),
199 'disabledCopy' => sprintf(
200 /* translators: %1$s: Learn more link opening tag. %2$s: Learn more link closing tag.*/
201 __( 'Per your %1$sstore settings%2$s, inventory management is <strong>disabled</strong>.', 'woocommerce-square' ),
202 '<a href="' . admin_url( 'admin.php?page=wc-settings&tab=products&section=inventory' ) . '" target="_blank" rel="noreferrer">',
203 '</a>'
204 ),
205 ),
206 )
207 );
208
209 $parent->add_block(
210 array(
211 'id' => '_wc_square_stock_quantity_field',
212 'blockName' => 'woocommerce-square/stock-quantity-field',
213 )
214 );
215 }
216
217 /**
218 * Removes the manage stock and stock quantity blocks from the product editor.
219 *
220 * @param ProductBlock $block Core product blocks.
221 */
222 public function remove_core_blocks( $block ) {
223 $blocks_to_remove = array();
224
225 if ( wc_square()->get_settings_handler()->is_inventory_sync_enabled() ) {
226 $blocks_to_remove[] = 'product-inventory-quantity';
227 $blocks_to_remove[] = 'product-track-stock';
228 }
229
230 /* Square classic product editor modifies a few core product meta fields functionaity.
231 * For example, conditionally disabling, hiding and even modifying the core product meta fields.
232 *
233 * This is not possible with the current block editor implementation. However, the new product editor
234 * allows to replace core blocks with custom implementation. This is not the best way to do it,
235 * but it's a workaround.
236 */
237 if ( in_array( $block->get_id(), $blocks_to_remove, true ) ) {
238 $block->remove();
239 }
240 }
241 }
242