PluginProbe ʕ •ᴥ•ʔ
WooCommerce Square / 3.8.2
WooCommerce Square v3.8.2
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 / AJAX.php
woocommerce-square / includes Last commit date
API 3 years ago Admin 3 years ago Emails 3 years ago Framework 3 years ago Gateway 3 years ago Handlers 3 years ago Sync 3 years ago Utilities 3 years ago AJAX.php 3 years ago API.php 3 years ago Admin.php 3 years ago Functions.php 3 years ago Gateway.php 3 years ago Lifecycle.php 3 years ago Plugin.php 3 years ago Settings.php 3 years ago
AJAX.php
315 lines
1 <?php
2 /**
3 * WooCommerce Square
4 *
5 * This source file is subject to the GNU General Public License v3.0
6 * that is bundled with this package in the file license.txt.
7 * It is also available through the world-wide-web at this URL:
8 * http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0 or later
9 * If you did not receive a copy of the license and are unable to
10 * obtain it through the world-wide-web, please send an email
11 * to license@woocommerce.com so we can send you a copy immediately.
12 *
13 * DISCLAIMER
14 *
15 * Do not edit or add to this file if you wish to upgrade WooCommerce Square to newer
16 * versions in the future. If you wish to customize WooCommerce Square for your
17 * needs please refer to https://docs.woocommerce.com/document/woocommerce-square/
18 *
19 * @author WooCommerce
20 * @copyright Copyright: (c) 2019, Automattic, Inc.
21 * @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0 or later
22 */
23
24 namespace WooCommerce\Square;
25
26 defined( 'ABSPATH' ) || exit;
27
28 use WooCommerce\Square\Handlers\Product;
29 use WooCommerce\Square\Sync\Records;
30
31 /**
32 * AJAX handler.
33 *
34 * @since 2.0.0
35 */
36 class AJAX {
37
38
39 /**
40 * Adds AJAX action callbacks.
41 *
42 * @since 2.0.0
43 */
44 public function __construct() {
45
46 // check an individual product sync status
47 add_action( 'wp_ajax_wc_square_get_quick_edit_product_details', array( $this, 'get_quick_edit_product_details' ) );
48
49 // fetch product stock from Square
50 add_action( 'wp_ajax_wc_square_fetch_product_stock_with_square', array( $this, 'fetch_product_stock_with_square' ) );
51
52 add_action( 'wp_ajax_wc_square_import_products_from_square', array( $this, 'import_products_from_square' ) );
53
54 // sync all products with Square
55 add_action( 'wp_ajax_wc_square_sync_products_with_square', array( $this, 'sync_products_with_square' ) );
56
57 // handle sync records
58 add_action( 'wp_ajax_wc_square_handle_sync_records', array( $this, 'handle_sync_records' ) );
59
60 // get the status of a sync job
61 add_action( 'wp_ajax_wc_square_get_sync_with_square_status', array( $this, 'get_sync_with_square_job_status' ) );
62 }
63
64 /**
65 * Fetches product stock data from Square.
66 *
67 * @internal
68 *
69 * @since 2.0.0
70 */
71 public function fetch_product_stock_with_square() {
72
73 check_ajax_referer( 'fetch-product-stock-with-square', 'security' );
74
75 if ( ! current_user_can( 'edit_products' ) ) {
76 wp_send_json_error( __( 'Invalid permissions.', 'woocommerce-square' ) );
77 }
78
79 $fix_error = __( 'Please mark product as un-synced and save, then synced again.', 'woocommerce-square' );
80
81 if ( isset( $_REQUEST['product_id'] ) && ( $product = wc_get_product( $_REQUEST['product_id'] ) ) ) {
82
83 try {
84
85 $product = Product::update_stock_from_square( $product );
86
87 wp_send_json_success( $product->get_stock_quantity() );
88
89 } catch ( \Exception $exception ) {
90
91 /* translators: Placeholders: %1$s = error message, %2$s = help text */
92 wp_send_json_error( sprintf( __( 'Unable to fetch inventory: %1$s. %2$s', 'woocommerce-square' ), $exception->getMessage(), $fix_error ) );
93 }
94 }
95
96 /* translators: Placeholders: %s = help text */
97 wp_send_json_error( sprintf( __( 'Error finding item in Square. %s', 'woocommerce-square' ), $fix_error ) );
98 }
99
100
101 /**
102 * Starts importing products from Square.
103 *
104 * @internal
105 *
106 * @since 2.0.0
107 */
108 public function import_products_from_square() {
109
110 check_ajax_referer( 'import-products-from-square', 'security' );
111
112 // The edit_others_shop_orders capability is used to determine if the user can access these settings.
113 if ( ! current_user_can( 'edit_others_shop_orders' ) ) {
114 wp_send_json_error( __( 'Could not start import. Invalid permissions.', 'woocommerce-square' ) );
115 }
116
117 $started = wc_square()->get_sync_handler()->start_product_import( ( ! empty( $_POST['update_during_import'] ) && 'true' === $_POST['update_during_import'] ) );
118
119 if ( ! $started ) {
120 wp_send_json_error( __( 'Could not start import. Please try again.', 'woocommerce-square' ) );
121 }
122
123 wp_send_json_success( __( 'Your products are being imported in the background! This may take some time to complete.', 'woocommerce-square' ) );
124 }
125
126
127 /**
128 * Starts syncing products with Square.
129 *
130 * @internal
131 *
132 * @since 2.0.0
133 */
134 public function sync_products_with_square() {
135
136 check_ajax_referer( 'sync-products-with-square', 'security' );
137
138 // The edit_others_shop_orders capability is used to determine if the user can access these settings.
139 if ( ! current_user_can( 'edit_others_shop_orders' ) ) {
140 wp_send_json_error();
141 }
142
143 $started = wc_square()->get_sync_handler()->start_manual_sync();
144
145 if ( ! $started ) {
146 wp_send_json_error();
147 }
148
149 wp_send_json_success();
150 }
151
152
153 /**
154 * Handles sync records actions.
155 *
156 * @internal
157 *
158 * @since 2.0.0
159 */
160 public function handle_sync_records() {
161
162 check_ajax_referer( 'handle-sync-with-square-records', 'security' );
163
164 if ( ! current_user_can( 'edit_others_shop_orders' ) ) {
165 wp_send_json_error( __( 'An error occurred. Invalid permissions.', 'woocommerce-square' ) );
166 }
167
168 $error = '';
169
170 if ( isset( $_POST['id'], $_POST['handle'] ) ) {
171
172 $id = $_POST['id'];
173 $action = $_POST['handle'];
174
175 if ( 'all' === $id && 'delete' === $action ) {
176
177 $outcome = Records::clean_records();
178 $error = esc_html__( 'Could not delete records.', 'woocommerce-square' );
179
180 } elseif ( is_string( $id ) && '' !== $id ) {
181
182 switch ( $action ) {
183
184 case 'delete':
185 $outcome = Records::delete_record( $id );
186 $error = esc_html__( 'Could not delete record.', 'woocommerce-square' );
187
188 break;
189
190 case 'resolve':
191 if ( $record = Records::get_record( $id ) ) {
192 $record->resolve();
193 $outcome = $record->save();
194 }
195
196 $error = esc_html__( 'Could not resolve record.', 'woocommerce-square' );
197
198 break;
199
200 case 'unsync':
201 $record = Records::get_record( $id );
202
203 if ( $record && ( $product = $record->get_product() ) ) {
204 $record->resolve();
205 $outcome = Product::unset_synced_with_square( $product ) && $record->save();
206 }
207
208 $error = esc_html__( 'Could not unsync product.', 'woocommerce-square' );
209
210 break;
211 }
212 }
213
214 if ( ! empty( $outcome ) ) {
215 wp_send_json_success( $outcome );
216 }
217 }
218
219 /* translators: Placeholder: %s - error message */
220 wp_send_json_error( sprintf( __( 'An error occurred. %s', 'woocommerce-square' ), $error ) );
221 }
222
223
224 /**
225 * Gets a sync job status.
226 *
227 * Also bumps the job progression (useful for when background processing isn't available).
228 *
229 * @internal
230 *
231 * @since 2.0.0
232 */
233 public function get_sync_with_square_job_status() {
234
235 check_ajax_referer( 'get-sync-with-square-status', 'security' );
236
237 if ( ! current_user_can( 'edit_others_shop_orders' ) ) {
238 wp_send_json_error( __( 'An error occurred. Invalid permissions.', 'woocommerce-square' ) );
239 }
240
241 $job_id = isset( $_POST['job_id'] ) ? $_POST['job_id'] : null;
242
243 if ( $job_id && ( $handler = wc_square()->get_background_job_handler() ) ) {
244
245 try {
246
247 if ( $job_in_progress = $handler->get_job( $job_id ) ) {
248
249 $result = array(
250 'action' => $job_in_progress->action,
251 'id' => $job_in_progress->id,
252 'job_products_count' => count( $job_in_progress->product_ids ),
253 'percentage' => ( (float) count( $job_in_progress->processed_product_ids ) / max( 1, count( $job_in_progress->product_ids ) ) ) * 100,
254 'imported_products_count' => count( $job_in_progress->processed_product_ids ),
255 'updated_products_count' => count( $job_in_progress->updated_product_ids ),
256 'skipped_products_count' => count( $job_in_progress->skipped_products ),
257 'status' => $job_in_progress->status,
258 );
259
260 wp_send_json_success( $result );
261 }
262 } catch ( \Exception $e ) {
263
264 wp_send_json_error( $e->getMessage() );
265 }
266 }
267
268 /* translators: Placeholder: %s - sync job ID */
269 wp_send_json_error( sprintf( esc_html__( 'No sync job in progress found %s', 'woocommerce-square' ), is_string( $job_id ) ? $job_id : null ) );
270 }
271
272 /**
273 * Get sync status, variable status, and edit url for product
274 *
275 * Used to manipulate quick edit menu for product
276 *
277 * @since 2.1.6
278 */
279 public function get_quick_edit_product_details() {
280
281 check_ajax_referer( 'get-quick-edit-product-details', 'security' );
282
283 if ( ! current_user_can( 'edit_products' ) ) {
284 wp_send_json_error( 'invalid_permission' );
285 }
286
287 if ( isset( $_POST['product_id'] ) && ( $product = wc_get_product( $_POST['product_id'] ) ) ) {
288
289 $is_variable = $product->is_type( 'variable' );
290
291 if ( ! Product::has_sku( $product ) ) {
292 if ( $is_variable ) {
293 wp_send_json_error( 'missing_variation_sku' );
294 } else {
295 wp_send_json_error( 'missing_sku' );
296 }
297 }
298
299 $is_synced_with_square = Product::is_synced_with_square( $product ) ? 'yes' : 'no';
300 $is_woocommerce_sor = wc_square()->get_settings_handler()->is_system_of_record_woocommerce();
301
302 wp_send_json_success(
303 array(
304 'edit_url' => $is_woocommerce_sor ? get_edit_post_link( $_POST['product_id'] ) : null,
305 'i18n' => $is_woocommerce_sor ? __( 'Stock must be fetched from Square before editing stock quantity', 'woocommerce-square' ) : null,
306 'is_synced_with_square' => $is_synced_with_square,
307 'is_variable' => $is_variable,
308 )
309 );
310 }
311 wp_send_json_error( 'invalid_product' );
312 }
313
314 }
315