PluginProbe ʕ •ᴥ•ʔ
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). / 2.2.15
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). v2.2.15
3.0.3 3.0.2 3.0.1 trunk 2.2.14 2.2.15 2.2.16 2.2.17 2.2.18 2.2.19 2.3.0 2.3.1 2.3.10 2.3.11 2.3.12 2.3.13 2.3.14 2.3.2 2.3.3 2.3.4 2.3.5 2.3.6 2.3.7 2.3.8 2.3.9 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.5.0 2.5.1 2.5.2 2.6.0 2.6.1 2.6.2 2.6.3 2.6.4 2.6.5 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 2.7.8 2.7.9 2.7.91 2.7.92 2.7.93 2.8.0 2.8.1 2.8.2 2.8.3 2.8.4 2.8.5 2.9.0 2.9.1 2.9.2 2.9.3 3.0.0
commercebird / includes / woo-functions.php
commercebird / includes Last commit date
classes 1 year ago sync 1 year ago woo-functions.php 1 year ago
woo-functions.php
590 lines
1 <?php
2
3 /**
4 * All WooCommerce related functions.
5 *
6 * @category WooCommerce
7 * @package CommerceBird
8 * @author Fawad Tiemoerie <info@roadmapstudios.com>
9 * @license GNU General Public License v3.0
10 * @link https://commercebird.com
11 */
12
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit;
15 }
16
17 /**
18 * Helper functions to ensure correct handling of Data being transferred via rest api
19 */
20
21 function cmbird_clear_product_cache( $object, $request, $is_creating ) {
22 if ( ! $is_creating ) {
23 $product_id = $object->get_id();
24 $zoho_inventory_access_token = get_option( 'cmbird_zoho_inventory_access_token' );
25 $cmbird_zi_product_sync = get_option( 'cmbird_zoho_disable_product_sync_status' );
26 if ( ! empty( $zoho_inventory_access_token ) && ! $cmbird_zi_product_sync ) {
27 $product_handler = new CMBIRD_Products_ZI_Export();
28 $product_handler->cmbird_zi_product_sync( $product_id );
29 }
30 wc_delete_product_transients( $product_id );
31 }
32 }
33 add_action( 'woocommerce_rest_insert_product_object', 'cmbird_clear_product_cache', 10, 3 );
34
35
36
37 /**
38 * Function to update the Contact in Zoho when customer updates address on frontend
39 * @param $user_id
40 */
41 function cmbird_update_contact_via_accountpage( $user_id ) {
42 $zoho_inventory_access_token = get_option( 'cmbird_zoho_inventory_access_token' );
43 if ( ! empty( $zoho_inventory_access_token ) ) {
44 $contact_class_handle = new CMBIRD_Contact_ZI();
45 $contact_class_handle->cmbird_contact_update_function( $user_id );
46
47 global $wpdb;
48 $meta_key_search = 'zi_contact_person_id%';
49 $query = $wpdb->get_results(
50 $wpdb->prepare(
51 "SELECT meta_key, meta_value
52 FROM {$wpdb->usermeta}
53 WHERE user_id = %d
54 AND meta_key LIKE %s",
55 $user_id,
56 $meta_key_search
57 )
58 );
59 if ( ! empty( $query ) ) {
60 // Usermeta keys containing 'zi_contact_person_id' exist.
61 foreach ( $query as $row ) {
62 $contact_class_handle->cmbird_update_contact_person( $user_id, $row->meta_value );
63 }
64 } else {
65 return;
66 }
67 } else {
68 return;
69 }
70 }
71 add_action( 'profile_update', 'cmbird_update_contact_via_accountpage' );
72
73 /**
74 * Function to be called by hook when new product is added in WooCommerce.
75 *
76 * @param $product_id
77 * @return void
78 */
79 add_action( 'woocommerce_update_product', 'cmbird_zi_product_sync_class', 10, 1 );
80 add_action( 'wp_ajax_zoho_admin_product_sync', 'cmbird_zi_product_sync_class' );
81 function cmbird_zi_product_sync_class( $product_id ) {
82 if ( ! is_admin() ) {
83 return;
84 }
85 if ( ! $product_id ) {
86 // Check nonce for security
87 if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'zoho_admin_product_sync' ) ) {
88 wp_send_json_error( 'Nonce verification failed' );
89 } else {
90 $product_id = isset( $_POST['post_id'] ) ? intval( $_POST['post_id'] ) : 0;
91 if ( ! $product_id ) {
92 wp_send_json_error( 'Invalid Product ID' );
93 }
94 }
95 }
96 $zoho_inventory_access_token = get_option( 'cmbird_zoho_inventory_access_token' );
97 // return if zoho inventory access token is empty
98 if ( empty( $zoho_inventory_access_token ) ) {
99 return;
100 }
101 $cmbird_zi_product_sync = get_option( 'cmbird_zoho_disable_product_sync_status' );
102 if ( ! $cmbird_zi_product_sync ) {
103 $product_handler = new CMBIRD_Products_ZI_Export();
104 $product_handler->cmbird_zi_product_sync( $product_id );
105 } else {
106 $zi_item_id = get_post_meta( $product_id, 'zi_item_id', true );
107 if ( ! empty( $zi_item_id ) ) {
108 $product_handler = new CMBIRD_Products_ZI_Export();
109 $product_handler->cmbird_zi_product_put( $product_id, $zi_item_id );
110 }
111 }
112 // if its variable product but without variations, then sync it.
113 $product = wc_get_product( $product_id );
114 if ( $product->is_type( 'variable' ) ) {
115 $variations = $product->get_available_variations();
116 if ( isset( $variations ) && count( $variations ) === 0 ) {
117 $zi_product_id = get_post_meta( $product_id, 'zi_item_id', true );
118 if ( ! empty( $zi_product_id ) ) {
119 $product_handler = new CMBIRD_Products_ZI();
120 $product_handler->import_variable_product_variations( $zi_product_id, $product_id );
121 }
122 }
123 }
124 }
125
126 /**
127 * Bulk-action to sync products from WooCommerce to Zoho
128 *
129 * @param: $bulk_array
130 * @return: $bulk_array
131 */
132 add_filter( 'bulk_actions-edit-product', 'cmbird_zi_sync_all_items_to_zoho' );
133 function cmbird_zi_sync_all_items_to_zoho( $bulk_array ) {
134 $bulk_array['sync_item_to_zoho'] = 'Sync to Zoho';
135 return $bulk_array;
136 }
137
138 add_filter( 'handle_bulk_actions-edit-product', 'cmbird_zi_sync_all_items_to_zoho_handler', 10, 3 );
139 function cmbird_zi_sync_all_items_to_zoho_handler( $redirect, $action, $object_ids ) {
140 // let's remove query args first
141 $redirect = remove_query_arg( 'sync_item_to_zoho_done', $redirect );
142
143 // do something for "Make Draft" bulk action
144 if ( 'sync_item_to_zoho' === $action ) {
145
146 foreach ( $object_ids as $post_id ) {
147 $product_handler = new CMBIRD_Products_ZI_Export();
148 $product_handler->cmbird_zi_product_sync( $post_id );
149 }
150
151 // do not forget to add query args to URL because we will show notices later
152 $redirect = add_query_arg( 'sync_item_to_zoho_done', count( $object_ids ), $redirect );
153
154 }
155
156 return $redirect;
157 }
158
159 // output the message of bulk action
160 add_action( 'admin_notices', 'cmbird_sync_item_to_zoho_notices' );
161 function cmbird_sync_item_to_zoho_notices() {
162 // verify nonce
163 if ( ! isset( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST['_wpnonce'] ) ), 'bulk-edit-products' ) ) {
164 return;
165 }
166 if ( ! empty( $_REQUEST['sync_item_to_zoho_done'] ) ) {
167 echo '<div id="message" class="updated notice is-dismissible">
168 <p>Products Synced. If product is not synced, please click on Edit Product to see the API response.</p>
169 </div>';
170 }
171 }
172
173 /**
174 * Function to be called by ajax hook when unmap button called.
175 * This function remove zoho mapped id.
176 */
177 add_action( 'wp_ajax_zi_product_unmap_hook', 'cmbird_zi_product_unmap_hook' );
178 function cmbird_zi_product_unmap_hook( $product_id ) {
179 // verify Nonce
180 if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'zi_product_unmap_hook' ) ) {
181 wp_send_json_error( 'Nonce verification failed' );
182 }
183 if ( ! $product_id && isset( $_POST['product_id'] ) ) {
184 $product_id = sanitize_text_field( wp_unslash( $_POST['product_id'] ) );
185 }
186
187 if ( $product_id ) {
188 $product = wc_get_product( $product_id );
189 // If this is variable items then unmap all of it's variations.
190 if ( $product->is_type( 'variable' ) ) {
191 $variations = $product->get_available_variations();
192 if ( isset( $variations ) && count( $variations ) > 0 ) {
193 foreach ( $variations as $child ) {
194 delete_post_meta( $child['variation_id'], 'zi_item_id' );
195 delete_post_meta( $child['variation_id'], 'zi_account_id' );
196 delete_post_meta( $child['variation_id'], 'zi_account_name' );
197 delete_post_meta( $child['variation_id'], 'zi_category_id' );
198 delete_post_meta( $child['variation_id'], 'zi_inventory_account_id' );
199 delete_post_meta( $child['variation_id'], 'zi_purchase_account_id' );
200 }
201 }
202 }
203 delete_post_meta( $product_id, 'zi_item_id' );
204 delete_post_meta( $product_id, 'zi_account_id' );
205 delete_post_meta( $product_id, 'zi_account_name' );
206 delete_post_meta( $product_id, 'zi_category_id' );
207 delete_post_meta( $product_id, 'zi_inventory_account_id' );
208 delete_post_meta( $product_id, 'zi_purchase_account_id' );
209 // update message
210 update_post_meta( $product_id, 'zi_product_errmsg', 'Product is Unmapped' );
211 }
212 }
213
214 /**
215 * Function to be called by ajax hook when unmap button called.
216 * This function remove zoho mapped id.
217 */
218 add_action( 'wp_ajax_zi_customer_unmap_hook', 'cmbird_zi_customer_unmap_hook' );
219 function cmbird_zi_customer_unmap_hook( $order_id ) {
220 // verify Nonce
221 if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'zi_customer_unmap_hook' ) ) {
222 wp_send_json_error( 'Nonce verification failed' );
223 }
224 if ( ! $order_id && isset( $_POST['order_id'] ) ) {
225 $order_id = sanitize_text_field( wp_unslash( $_POST['order_id'] ) );
226 }
227
228 $order = wc_get_order( $order_id );
229 $customer_id = $order->get_user_id();
230
231 if ( $customer_id ) {
232 delete_user_meta( $customer_id, 'zi_contact_id' );
233 delete_user_meta( $customer_id, 'zi_contact_persons_id' );
234 delete_user_meta( $customer_id, 'zi_contactperson_id_0' );
235 delete_user_meta( $customer_id, 'zi_contactperson_id_1' );
236 delete_user_meta( $customer_id, 'zi_currency_code' );
237 delete_user_meta( $customer_id, 'zi_currency_id' );
238 delete_user_meta( $customer_id, 'zi_created_time' );
239 delete_user_meta( $customer_id, 'zi_last_modified_time' );
240 delete_user_meta( $customer_id, 'zi_primary_contact_id' );
241 delete_user_meta( $customer_id, 'zi_billing_address_id' );
242 delete_user_meta( $customer_id, 'zi_shipping_address_id' );
243
244 $order->add_order_note( 'Zoho Sync: Customer is now unmapped. Please try syncing the order again' );
245 $order->save();
246 }
247 }
248
249 /**
250 * Add WordPress Meta box to show sync response
251 */
252 function cmbird_product_metabox() {
253 $zoho_inventory_access_token = get_option( 'cmbird_zoho_inventory_access_token' );
254 if ( ! $zoho_inventory_access_token ) {
255 return;
256 }
257 add_meta_box(
258 'zoho-product-sync',
259 __( 'Zoho Inventory', 'commercebird' ),
260 'cmbird_product_metabox_callback',
261 'product',
262 'side',
263 'high'
264 );
265 }
266 function cmbird_product_metabox_callback( $post ) {
267 $response = get_post_meta( $post->ID, 'zi_product_errmsg' );
268 echo 'API Response: ' . esc_html( implode( $response ) ) . '<br>';
269 // Generate nonce
270 $nonce = wp_create_nonce( 'zoho_admin_product_sync' );
271 $nonce_unmap = wp_create_nonce( 'zi_product_unmap_hook' );
272 $post_id = $post->ID;
273 echo '<br><a href="javascript:void(0)" style="width:100%; text-align: center;" class="button button-primary" onclick="zoho_admin_product_ajax(' . esc_attr( $post_id ) . ', \'' . esc_attr( $nonce ) . '\')">Sync Product</a>';
274 echo '<br><a href="javascript:void(0)" style="margin-top:10px; background:#b32d2e; border-color: #b32d2e; width:100%; text-align: center;" class="button button-primary" onclick="zoho_admin_unmap_product_ajax(' . esc_attr( $post_id ) . ', \'' . esc_attr( $nonce_unmap ) . '\')">Unmap this Product</a>';
275 $product = wc_get_product( $post->ID );
276 $product_type = $product->get_type();
277 if ( 'variable' === $product_type || 'variable-subscription' === $product_type ) {
278 echo '<p class="howto" style="color:#b32d2e;"><strong>Important : </strong> Please ensure all variations have price and SKU</p>';
279 }
280 // echo the zi_category_id
281 $zi_category_id = get_post_meta( $post->ID, 'zi_category_id', true );
282 if ( $zi_category_id ) {
283 echo '<p class="howto"><strong>Zoho Category: </strong>' . esc_html( $zi_category_id ) . '</p>';
284 }
285 // make api call to get the zoho item details
286 $zi_item_id = get_post_meta( $post->ID, 'zi_item_id', true );
287 if ( $zi_item_id ) {
288 $zoho_inventory_oid = get_option( 'cmbird_zoho_inventory_organization_id' );
289 $zoho_inventory_url = get_option( 'cmbird_zoho_inventory_url' );
290 $urlitem = "{$zoho_inventory_url}inventory/v1/items/{$zi_item_id}?organization_id=$zoho_inventory_oid";
291 // fwrite( $fd, PHP_EOL . 'URL : ' . $urlitem );
292
293 $execute_curl_call = new CMBIRD_API_Handler_Zoho();
294 $json = $execute_curl_call->execute_curl_call_get( $urlitem );
295 $code = (int) property_exists( $json, 'code' ) ? $json->code : '0';
296 if ( '0' === $code || 0 === $code ) {
297 // echo the item details here that are in array called "item"
298 $item = $json->item;
299 $actual_available_stock = (int) property_exists( $item, 'actual_available_stock' ) ? $item->actual_available_stock : '0';
300 $rate = (int) property_exists( $item, 'rate' ) ? $item->rate : '0';
301 echo '<p class="howto"><strong>Rate: </strong>' . esc_html( $rate ) . '</p>';
302 // echo the actual_available_stock
303 echo '<p class="howto"><strong>Available Stock: </strong>' . esc_html( $actual_available_stock ) . '</p>';
304 }
305 }
306 }
307 add_action( 'add_meta_boxes', 'cmbird_product_metabox' );
308
309
310 /**
311 * Add Zoho and Exact Item IDs to Product and Variations
312 * @return void
313 */
314 add_action( 'woocommerce_product_options_pricing', 'cmbird_item_id_field' );
315 add_action( 'woocommerce_variation_options_pricing', 'cmbird_item_id_variation_field', 10, 3 );
316 function cmbird_item_id_field() {
317 woocommerce_wp_text_input(
318 array(
319 'id' => '_cost_price',
320 'class' => 'readonly',
321 'wrapper_class' => 'form-row',
322 'label' => esc_html__( 'Cost Price', 'commercebird' ),
323 'data_type' => 'price',
324 'description' => esc_html__( 'You can edit this via the CommerceBird App', 'commercebird' ),
325 )
326 );
327 woocommerce_wp_text_input(
328 array(
329 'id' => 'eo_item_id',
330 'label' => esc_html__( 'Exact Item ID', 'commercebird' ),
331 'class' => 'readonly',
332 'desc_tip' => true,
333 'description' => esc_html__( 'This is the Exact Item ID of this product. You cannot change this', 'commercebird' ),
334 )
335 );
336 woocommerce_wp_text_input(
337 array(
338 'id' => 'zi_item_id',
339 'label' => esc_html__( 'Zoho Item ID', 'commercebird' ),
340 'class' => 'readonly',
341 'desc_tip' => true,
342 'description' => esc_html__( 'This is the Zoho Item ID of this product. You cannot change this', 'commercebird' ),
343 )
344 );
345 }
346 function cmbird_item_id_variation_field( $loop, $variation_data, $variation ) {
347 woocommerce_wp_text_input(
348 array(
349 'id' => 'cost_price[' . $loop . ']',
350 'class' => 'readonly',
351 'wrapper_class' => 'form-row',
352 'data_type' => 'price',
353 'label' => esc_html__( 'Cost Price', 'commercebird' ),
354 'value' => get_post_meta( $variation->ID, '_cost_price', true ),
355 'description' => esc_html__( 'You can edit this via the CommerceBird App', 'commercebird' ),
356 )
357 );
358 woocommerce_wp_text_input(
359 array(
360 'id' => 'eo_item_id[' . $loop . ']',
361 'class' => 'readonly',
362 'label' => esc_html__( 'Exact Item ID', 'commercebird' ),
363 'value' => get_post_meta( $variation->ID, 'eo_item_id', true ),
364 'desc_tip' => true,
365 'description' => esc_html__( 'This is the Exact Item ID of this product. You cannot change this', 'commercebird' ),
366 )
367 );
368 woocommerce_wp_text_input(
369 array(
370 'id' => 'zi_item_id[' . $loop . ']',
371 'class' => 'readonly',
372 'label' => esc_html__( 'Zoho Item ID', 'commercebird' ),
373 'value' => get_post_meta( $variation->ID, 'zi_item_id', true ),
374 'desc_tip' => true,
375 'description' => esc_html__( 'This is the Zoho Item ID of this product. You cannot change this', 'commercebird' ),
376 )
377 );
378 }
379
380 /**
381 * Adds 'Zoho Sync' column header to 'Orders' page immediately after 'Total' column.
382 *
383 * @param string[] $columns
384 * @return string[] $new_columns
385 */
386 function cmbird_zi_sync_column_orders_overview( $columns ) {
387 $zoho_inventory_access_token = get_option( 'cmbird_zoho_inventory_access_token' );
388 if ( empty( $zoho_inventory_access_token ) ) {
389 return $columns;
390 }
391 $new_columns = array();
392 foreach ( $columns as $column_name => $column_info ) {
393
394 $new_columns[ $column_name ] = $column_info;
395
396 if ( 'order_total' === $column_name ) {
397 $new_columns['zoho_sync'] = __( 'Zoho Sync', 'commercebird' );
398 }
399 }
400 return $new_columns;
401 }
402 add_filter( 'manage_woocommerce_page_wc-orders_columns', 'cmbird_zi_sync_column_orders_overview', 20 );
403
404 /**
405 * Adding Sync Status for Orders Column
406 *
407 * @param string $column Column name.
408 * @param int $order_id $order id.
409 * @return void
410 */
411 function cmbird_zi_add_zoho_orders_content( $column, $order_id ) {
412 $zi_url = get_option( 'cmbird_zoho_inventory_url' );
413 $zi_visit_url = str_replace( 'www.zohoapis', 'inventory.zoho', $zi_url );
414 switch ( $column ) {
415 case 'zoho_sync':
416 // Get custom order meta data.
417 $order = wc_get_order( $order_id );
418 $zi_order_id = $order->get_meta( 'zi_salesorder_id', true, 'edit' );
419 $url = $zi_visit_url . 'app#/salesorders/' . $zi_order_id;
420 if ( $zi_order_id ) {
421 echo '<span class="dashicons dashicons-yes-alt" style="color:green;"></span><a href="' . esc_url( $url ) . '" target="_blank"> <span class="dashicons dashicons-external" style="color:green;"></span> </a>';
422 } else {
423 echo '<span class="dashicons dashicons-dismiss" style="color:red;"></span>';
424 }
425 unset( $order );
426 break;
427 }
428 }
429 add_action( 'manage_woocommerce_page_wc-orders_custom_column', 'cmbird_zi_add_zoho_orders_content', 20, 2 );
430
431 /**
432 * Adds 'Zoho Sync' column content.
433 *
434 * @param string[] $column name of column being displayed
435 */
436 function cmbird_zi_add_zoho_column_content( $column ) {
437 global $post;
438 $post_type = get_post_type( $post );
439
440 if ( 'zoho_sync' === $column && 'product' === $post_type ) {
441 $product_id = $post->ID;
442 $zi_product_id = get_post_meta( $product_id, 'zi_item_id' );
443 if ( $zi_product_id ) {
444 echo '<span class="dashicons dashicons-yes-alt" style="color:green;"></span>';
445 } else {
446 echo '<span class="dashicons dashicons-dismiss" style="color:red;"></span>';
447 }
448 }
449 }
450 add_action( 'manage_product_posts_custom_column', 'cmbird_zi_add_zoho_column_content' );
451
452 /**
453 * Adds 'Zoho Sync' column header to 'Products' page.
454 *
455 * @param string[] $columns
456 * @return string[] $new_columns
457 */
458 function cmbird_zi_sync_column_products_overview( $columns ) {
459 $zoho_inventory_access_token = get_option( 'cmbird_zoho_inventory_access_token' );
460 if ( empty( $zoho_inventory_access_token ) ) {
461 return $columns;
462 }
463 $new_columns = array();
464
465 foreach ( $columns as $column_name => $column_info ) {
466
467 $new_columns[ $column_name ] = $column_info;
468
469 if ( 'product_cat' === $column_name ) {
470 $new_columns['zoho_sync'] = __( 'Zoho Sync', 'commercebird' );
471 }
472 }
473
474 return $new_columns;
475 }
476 add_filter( 'manage_edit-product_columns', 'cmbird_zi_sync_column_products_overview', 20 );
477
478 /**
479 * Make 'Zoho Sync' column filterable.
480 */
481 function cmbird_zi_sync_column_filterable() {
482 global $typenow;
483
484 if ( 'product' === $typenow ) {
485 // code here
486 $value = isset( $_GET['zoho_sync_filter'] ) ? sanitize_text_field( wp_unslash( $_GET['zoho_sync_filter'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
487
488 echo '<select name="zoho_sync_filter">';
489 echo '<option value="">Zoho Sync Filter</option>';
490
491 // Count synced products
492 $synced_count = new WP_Query(
493 array(
494 'post_type' => 'product',
495 'meta_query' => array(
496 array(
497 'key' => 'zi_item_id',
498 'compare' => 'EXISTS',
499 ),
500 ),
501 'fields' => 'ids',
502 )
503 );
504 $synced_count = $synced_count->found_posts;
505 $synced_label = 'Synced';
506 if ( $synced_count > 0 ) {
507 $synced_label .= ' (' . $synced_count . ')';
508 }
509 echo '<option value="synced" ' . selected( $value, 'synced', false ) . '>' . esc_html( $synced_label ) . '</option>';
510
511 // Count not synced products
512 $not_synced_count = new WP_Query(
513 array(
514 'post_type' => 'product',
515 'meta_query' => array(
516 array(
517 'key' => 'zi_item_id',
518 'compare' => 'NOT EXISTS',
519 ),
520 ),
521 'fields' => 'ids',
522 )
523 );
524 $not_synced_count = $not_synced_count->found_posts;
525 $not_synced_label = 'Not Synced';
526 if ( $not_synced_count > 0 ) {
527 $not_synced_label .= ' (' . $not_synced_count . ')';
528 }
529 echo '<option value="not_synced" ' . selected( $value, 'not_synced', false ) . '>' . esc_html( $not_synced_label ) . '</option>';
530
531 echo '</select>';
532 }
533 }
534 add_action( 'restrict_manage_posts', 'cmbird_zi_sync_column_filterable' );
535
536 /**
537 * Modify the product query based on the filter.
538 *
539 * @param WP_Query $query The query object.
540 */
541 function cmbird_zi_sync_column_filter_query( $query ) {
542 global $typenow, $pagenow;
543
544 if ( $typenow === 'product' && $pagenow === 'edit.php' && isset( $_GET['zoho_sync_filter'] ) && $_GET['zoho_sync_filter'] !== '' ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
545 $value = sanitize_text_field( wp_unslash( $_GET['zoho_sync_filter'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
546
547 $meta_query = array();
548
549 if ( $value === 'synced' ) {
550 $meta_query[] = array(
551 'key' => 'zi_item_id',
552 'compare' => 'EXISTS',
553 );
554 } elseif ( $value === 'not_synced' ) {
555 $meta_query[] = array(
556 'relation' => 'OR',
557 array(
558 'key' => 'zi_item_id',
559 'compare' => 'NOT EXISTS',
560 ),
561 array(
562 'key' => 'zi_item_id',
563 'value' => '',
564 'compare' => '=',
565 ),
566 );
567 }
568
569 $query->set( 'meta_query', $meta_query );
570 }
571 }
572 add_action( 'pre_get_posts', 'cmbird_zi_sync_column_filter_query' );
573
574 /**
575 * Change Action Scheduler default purge to 1 week
576 * @return int
577 */
578 function cmbird_action_scheduler_purge() {
579 return WEEK_IN_SECONDS;
580 }
581 add_filter( 'action_scheduler_retention_period', 'cmbird_action_scheduler_purge' );
582
583 add_filter(
584 'action_scheduler_default_cleaner_statuses',
585 function ($statuses) {
586 $statuses[] = ActionScheduler_Store::STATUS_FAILED;
587 return $statuses;
588 }
589 );
590