PluginProbe ʕ •ᴥ•ʔ
WooCommerce Square / 3.8.1
WooCommerce Square v3.8.1
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
289 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 $fix_error = __( 'Please mark product as un-synced and save, then synced again.', 'woocommerce-square' );
76
77 if ( isset( $_REQUEST['product_id'] ) && ( $product = wc_get_product( $_REQUEST['product_id'] ) ) ) {
78
79 try {
80
81 $product = Product::update_stock_from_square( $product );
82
83 wp_send_json_success( $product->get_stock_quantity() );
84
85 } catch ( \Exception $exception ) {
86
87 /* translators: Placeholders: %1$s = error message, %2$s = help text */
88 wp_send_json_error( sprintf( __( 'Unable to fetch inventory: %1$s. %2$s', 'woocommerce-square' ), $exception->getMessage(), $fix_error ) );
89 }
90 }
91
92 /* translators: Placeholders: %s = help text */
93 wp_send_json_error( sprintf( __( 'Error finding item in Square. %s', 'woocommerce-square' ), $fix_error ) );
94 }
95
96
97 /**
98 * Starts importing products from Square.
99 *
100 * @internal
101 *
102 * @since 2.0.0
103 */
104 public function import_products_from_square() {
105
106 check_ajax_referer( 'import-products-from-square', 'security' );
107
108 $started = wc_square()->get_sync_handler()->start_product_import( ( ! empty( $_POST['update_during_import'] ) && 'true' === $_POST['update_during_import'] ) );
109
110 if ( ! $started ) {
111 wp_send_json_error( __( 'Could not start import. Please try again.', 'woocommerce-square' ) );
112 }
113
114 wp_send_json_success( __( 'Your products are being imported in the background! This may take some time to complete.', 'woocommerce-square' ) );
115 }
116
117
118 /**
119 * Starts syncing products with Square.
120 *
121 * @internal
122 *
123 * @since 2.0.0
124 */
125 public function sync_products_with_square() {
126
127 check_ajax_referer( 'sync-products-with-square', 'security' );
128
129 $started = wc_square()->get_sync_handler()->start_manual_sync();
130
131 if ( ! $started ) {
132 wp_send_json_error();
133 }
134
135 wp_send_json_success();
136 }
137
138
139 /**
140 * Handles sync records actions.
141 *
142 * @internal
143 *
144 * @since 2.0.0
145 */
146 public function handle_sync_records() {
147
148 check_ajax_referer( 'handle-sync-with-square-records', 'security' );
149
150 $error = '';
151
152 if ( isset( $_POST['id'], $_POST['handle'] ) ) {
153
154 $id = $_POST['id'];
155 $action = $_POST['handle'];
156
157 if ( 'all' === $id && 'delete' === $action ) {
158
159 $outcome = Records::clean_records();
160 $error = esc_html__( 'Could not delete records.', 'woocommerce-square' );
161
162 } elseif ( is_string( $id ) && '' !== $id ) {
163
164 switch ( $action ) {
165
166 case 'delete':
167 $outcome = Records::delete_record( $id );
168 $error = esc_html__( 'Could not delete record.', 'woocommerce-square' );
169
170 break;
171
172 case 'resolve':
173 if ( $record = Records::get_record( $id ) ) {
174 $record->resolve();
175 $outcome = $record->save();
176 }
177
178 $error = esc_html__( 'Could not resolve record.', 'woocommerce-square' );
179
180 break;
181
182 case 'unsync':
183 $record = Records::get_record( $id );
184
185 if ( $record && ( $product = $record->get_product() ) ) {
186 $record->resolve();
187 $outcome = Product::unset_synced_with_square( $product ) && $record->save();
188 }
189
190 $error = esc_html__( 'Could not unsync product.', 'woocommerce-square' );
191
192 break;
193 }
194 }
195
196 if ( ! empty( $outcome ) ) {
197 wp_send_json_success( $outcome );
198 }
199 }
200
201 /* translators: Placeholder: %s - error message */
202 wp_send_json_error( sprintf( __( 'An error occurred. %s', 'woocommerce-square' ), $error ) );
203 }
204
205
206 /**
207 * Gets a sync job status.
208 *
209 * Also bumps the job progression (useful for when background processing isn't available).
210 *
211 * @internal
212 *
213 * @since 2.0.0
214 */
215 public function get_sync_with_square_job_status() {
216
217 check_ajax_referer( 'get-sync-with-square-status', 'security' );
218
219 $job_id = isset( $_POST['job_id'] ) ? $_POST['job_id'] : null;
220
221 if ( $job_id && ( $handler = wc_square()->get_background_job_handler() ) ) {
222
223 try {
224
225 if ( $job_in_progress = $handler->get_job( $job_id ) ) {
226
227 $result = array(
228 'action' => $job_in_progress->action,
229 'id' => $job_in_progress->id,
230 'job_products_count' => count( $job_in_progress->product_ids ),
231 'percentage' => ( (float) count( $job_in_progress->processed_product_ids ) / max( 1, count( $job_in_progress->product_ids ) ) ) * 100,
232 'imported_products_count' => count( $job_in_progress->processed_product_ids ),
233 'updated_products_count' => count( $job_in_progress->updated_product_ids ),
234 'skipped_products_count' => count( $job_in_progress->skipped_products ),
235 'status' => $job_in_progress->status,
236 );
237
238 wp_send_json_success( $result );
239 }
240 } catch ( \Exception $e ) {
241
242 wp_send_json_error( $e->getMessage() );
243 }
244 }
245
246 /* translators: Placeholder: %s - sync job ID */
247 wp_send_json_error( sprintf( esc_html__( 'No sync job in progress found %s', 'woocommerce-square' ), is_string( $job_id ) ? $job_id : null ) );
248 }
249
250 /**
251 * Get sync status, variable status, and edit url for product
252 *
253 * Used to manipulate quick edit menu for product
254 *
255 * @since 2.1.6
256 */
257 public function get_quick_edit_product_details() {
258
259 check_ajax_referer( 'get-quick-edit-product-details', 'security' );
260
261 if ( isset( $_POST['product_id'] ) && ( $product = wc_get_product( $_POST['product_id'] ) ) ) {
262
263 $is_variable = $product->is_type( 'variable' );
264
265 if ( ! Product::has_sku( $product ) ) {
266 if ( $is_variable ) {
267 wp_send_json_error( 'missing_variation_sku' );
268 } else {
269 wp_send_json_error( 'missing_sku' );
270 }
271 }
272
273 $is_synced_with_square = Product::is_synced_with_square( $product ) ? 'yes' : 'no';
274 $is_woocommerce_sor = wc_square()->get_settings_handler()->is_system_of_record_woocommerce();
275
276 wp_send_json_success(
277 array(
278 'edit_url' => $is_woocommerce_sor ? get_edit_post_link( $_POST['product_id'] ) : null,
279 'i18n' => $is_woocommerce_sor ? __( 'Stock must be fetched from Square before editing stock quantity', 'woocommerce-square' ) : null,
280 'is_synced_with_square' => $is_synced_with_square,
281 'is_variable' => $is_variable,
282 )
283 );
284 }
285 wp_send_json_error( 'invalid_product' );
286 }
287
288 }
289