PluginProbe
AliNext – WooCommerce Dropshipping Plugin for AliExpress / trunk
AliNext – WooCommerce Dropshipping Plugin for AliExpress vtrunk
ali2woo-lite / includes / classes / controller / ProductServiceController.php

ProductServiceController.php in AliNext – WooCommerce Dropshipping Plugin for AliExpress trunk, at includes/classes/controller/ProductServiceController.php

185 lines 8.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Description of ProductServiceController
4 *
5 * @author Ali2Woo Team
6 *
7 * @autoload: a2wl_admin_init
8 *
9 * @ajax: true
10 */
11
12 namespace AliNext_Lite;;
13
14 use Pages;
15 use WC_Meta_Box_Product_Data;
16 use WC_Product_Factory;
17
18 class ProductServiceController {
19
20 public function __construct() {
21 add_action("before_delete_post", array($this, 'delete_post_action'), 10, 1);
22 add_action("woocommerce_save_product_variation", array($this, 'save_product_variation'), 10, 2);
23
24 add_action('wp_ajax_woocommerce_save_attributes', [$this, 'woocommerce_save_attributes'], 0);
25 }
26
27 public function delete_post_action($post_id) {
28 // first, delete all post images
29 $post_type = get_post_type($post_id);
30
31 if(!a2wl_check_defined('A2WL_DO_NOT_USE_TRANSACTION') &&
32 !isset($GLOBALS['a2wl_delete_post_register_shutdown']) &&
33 in_array($post_type, array('product','product_variation'))
34 ){
35 $GLOBALS['a2wl_delete_post_register_shutdown'] = 1;
36
37 wp_defer_term_counting(true);
38 wp_defer_comment_counting(true );
39 $GLOBALS['wpdb']->query('SET autocommit = 0;');
40
41 register_shutdown_function(function(){
42 unset($GLOBALS['a2wl_delete_post_register_shutdown']);
43
44 $GLOBALS['wpdb']->query('COMMIT;');
45 wp_defer_term_counting(false);
46 wp_defer_comment_counting(false);
47 });
48 }
49
50 Utils::delete_post_images($post_id);
51
52 //second, mark variation as skip
53 if ($post_type == 'product_variation' && !isset($GLOBALS['a2wl_autodelete_variaton_lock'])) {
54 $variation = new \WC_Product_Variation($post_id);
55 $parent_id = $variation->get_parent_id();
56 $a2wl_skip_meta = get_post_meta($parent_id, "_a2w_skip_meta", true);
57 $a2wl_skip_meta = $a2wl_skip_meta ? $a2wl_skip_meta : array('skip_vars' => array(), 'skip_images' => array());
58
59 $external_variation_id = get_post_meta($post_id, "external_variation_id", true);
60 if (!in_array($external_variation_id, $a2wl_skip_meta['skip_vars'])) {
61 $a2wl_skip_meta['skip_vars'][] = $external_variation_id;
62 }
63 update_post_meta($parent_id, "_a2w_skip_meta", $a2wl_skip_meta);
64
65 $this->update_aliexpress_sku_props($parent_id, $post_id);
66 }
67 }
68
69 public function save_product_variation($variation_id, $i) {
70 $variation = new \WC_Product_Variation($variation_id);
71 $parent_id = $variation->get_parent_id();
72 $a2wl_skip_meta = get_post_meta($parent_id, "_a2w_skip_meta", true);
73 $a2wl_skip_meta = $a2wl_skip_meta ? $a2wl_skip_meta : array('skip_vars' => array(), 'skip_images' => array());
74
75 $external_variation_id = get_post_meta($variation_id, "external_variation_id", true);
76 if (in_array($variation->get_status(), array('publish', false))) {
77 $a2wl_skip_meta['skip_vars'] = array_diff($a2wl_skip_meta['skip_vars'], array($external_variation_id));
78 } else {
79 if (!in_array($external_variation_id, $a2wl_skip_meta['skip_vars'])) {
80 $a2wl_skip_meta['skip_vars'][] = $external_variation_id;
81 }
82 }
83 update_post_meta($parent_id, "_a2w_skip_meta", $a2wl_skip_meta);
84
85 $this->update_aliexpress_sku_props($parent_id);
86 }
87
88 private function update_aliexpress_sku_props($product_id, $skip_ids = array()){
89 $skip_ids = empty($skip_ids)?array():(is_array($skip_ids)?$skip_ids:array($skip_ids));
90 $product = new \WC_Product_Variable($product_id);
91 if ($product) {
92 $var_ids = $product->get_visible_children();
93 foreach($var_ids as $var_id){
94 if(!in_array($var_id, $skip_ids)){
95 $aliexpress_sku_props = get_post_meta($var_id, "_aliexpress_sku_props", true);
96 if($aliexpress_sku_props){
97 update_post_meta($product_id, '_aliexpress_sku_props', $aliexpress_sku_props);
98 break;
99 }
100 }
101 }
102 }
103 }
104 public function woocommerce_save_attributes(): void
105 {
106 check_ajax_referer('save-attributes', 'security');
107
108 if (!PageGuardHelper::canAccessPage(Pages::IMPORT_LIST)) {
109 wp_die(-1);
110 }
111
112 $product_id = absint($_POST['post_id']);
113
114 $original_variations_attributes = get_post_meta(
115 $product_id, '_a2w_original_variations_attributes',
116 true
117 );
118
119 if ($original_variations_attributes) {
120 parse_str($_POST['data'], $data);
121 $attributes = WC_Meta_Box_Product_Data::prepare_attributes($data);
122
123 $product_type = !empty($_POST['product_type']) ? wc_clean($_POST['product_type']) : 'simple';
124 $classname = WC_Product_Factory::get_product_classname($product_id, $product_type);
125 $product = new $classname($product_id);
126 $product_attributes = $product->get_attributes();
127
128 // update old simple variation attributes names (if it changed)
129 $need_update = false;
130 foreach ($product_attributes as $pa) {
131 $pa->get_position();
132 if ($pa->get_variation() && !$pa->is_taxonomy()) {
133 foreach ($attributes as $a) {
134 if ($pa->get_position() === $a->get_position() && $pa->get_name() !== $a->get_name()) {
135 $new_name = sanitize_title($a->get_name());
136 $old_name = sanitize_title($pa->get_name());
137 if (isset($original_variations_attributes[$old_name])) {
138 $original_variations_attributes[$new_name] = $original_variations_attributes[$old_name];
139 $original_variations_attributes[$new_name]['current_name'] = $a->get_name();
140 unset($original_variations_attributes[$old_name]);
141 }
142
143 global $wpdb;
144 $sql = "UPDATE $wpdb->postmeta pm, $wpdb->posts p SET pm.meta_key=%s " .
145 "WHERE p.ID = pm.post_id AND p.post_parent=%d " .
146 "AND pm.meta_key=%s";
147 $wpdb->query(
148 // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
149 $wpdb->prepare(
150 // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
151 $sql,
152 esc_sql("attribute_" . $new_name),
153 $product->get_id(),
154 esc_sql("attribute_" . $old_name)
155 )
156 );
157
158 $need_update = true;
159 break;
160 }
161 }
162 }
163 }
164
165 if ($need_update) {
166 update_post_meta($product_id, '_a2w_original_variations_attributes', $original_variations_attributes);
167 }
168
169 // build delete variation attributes array
170 foreach ($attributes as $product_attr) {
171 if ($product_attr->get_variation()) {
172 foreach ($original_variations_attributes as $key => $values) {
173 if (sanitize_title($product_attr->get_name()) == (($product_attr->is_taxonomy() ? 'pa_' : '') . $key)) {
174 unset($original_variations_attributes[$key]);
175 break;
176 }
177 }
178 }
179 }
180
181 update_post_meta($product_id, '_a2w_deleted_variations_attributes', $original_variations_attributes);
182 }
183 }
184 }
185