YITH Essential Kit for WooCommerce #1 - Version 1.1.13

Version Description

  • Update: YITH WooCommerce Multi Vendor
Download this release

Release Info

Developer yithemes
Plugin Icon 128x128 YITH Essential Kit for WooCommerce #1
Version 1.1.13
Comparing to
See all releases

Code changes from version 1.1.12 to 1.1.13

README.txt CHANGED
@@ -4,7 +4,7 @@ Contributors: yithemes
4
  Tags: woocommerce,jetpack,woocommerce jetpack, essential, kit, plugins, plugin, product, e-commerce, ecommerce, commerce, shop, yith, yit, yithemes,yith woocommerce colors and labels variations, colors and labels variations,colors and labels variations jetpack,yith woocommerce zoom magnifier,zoom magnifier,zoom magnifier jetpack,yith woocommerce wishlist,wishlist,wishlist jetpack,yith woocommerce compare,compare,compare jetpack,yith woocommerce ajax product filter,ajax product filter,ajax product filter jetpack,yith woocommerce ajax search,ajax search,ajax search jetpack,yith woocommerce catalog mode,catalog mode,catalog mode jetpack,yith woocommerce order tracking,order tracking,order tracking jetpack, yith woocommerce advanced reviews,advanced reviews,advanced reviews jetpack,yith woocommerce badges management,badges management,badges management jetpack,yith woocommerce cart messages,cart messages,cart messages jetpack,yith woocommerce review reminder,review reminder,review reminder jetpack,yith woocommerce quick view,quick view,quick view jetpack,yith live chat,live chat,live chat jetpack,yith woocommerce eu vat,eu vat,eu vat jetpack,yith woocommerce mailchimp,mailchimp,mailchimp jetpack,yith woocommerce stripe,stripe, stripe jetpack,yith infinite scrolling,infinite scrolling,infinite scrolling jetpack,yith woocommerce request a quote,request a quote,request a quote jetpack,yith woocommerce waiting list,waiting list,waiting list jetpack,yith woocommerce tab manager,tab manager,tab manager jetpack,yith woocommerce multi vendor,multi vendor,multi vendor jetpack,product vendor,products vendor,products vendor jetpack, booster for woocommerce
5
  Requires at least: 3.5.1
6
  Tested up to: 4.2.4
7
- Stable tag: 1.1.12
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
@@ -151,6 +151,10 @@ YITH Essential Kit for WooCommerce #1 will add a new submenu called "YITH Essent
151
 
152
  == Changelog ==
153
 
 
 
 
 
154
  = 1.1.12 =
155
 
156
  * Fixed: YITH WooCommerce Authorized Net issue
4
  Tags: woocommerce,jetpack,woocommerce jetpack, essential, kit, plugins, plugin, product, e-commerce, ecommerce, commerce, shop, yith, yit, yithemes,yith woocommerce colors and labels variations, colors and labels variations,colors and labels variations jetpack,yith woocommerce zoom magnifier,zoom magnifier,zoom magnifier jetpack,yith woocommerce wishlist,wishlist,wishlist jetpack,yith woocommerce compare,compare,compare jetpack,yith woocommerce ajax product filter,ajax product filter,ajax product filter jetpack,yith woocommerce ajax search,ajax search,ajax search jetpack,yith woocommerce catalog mode,catalog mode,catalog mode jetpack,yith woocommerce order tracking,order tracking,order tracking jetpack, yith woocommerce advanced reviews,advanced reviews,advanced reviews jetpack,yith woocommerce badges management,badges management,badges management jetpack,yith woocommerce cart messages,cart messages,cart messages jetpack,yith woocommerce review reminder,review reminder,review reminder jetpack,yith woocommerce quick view,quick view,quick view jetpack,yith live chat,live chat,live chat jetpack,yith woocommerce eu vat,eu vat,eu vat jetpack,yith woocommerce mailchimp,mailchimp,mailchimp jetpack,yith woocommerce stripe,stripe, stripe jetpack,yith infinite scrolling,infinite scrolling,infinite scrolling jetpack,yith woocommerce request a quote,request a quote,request a quote jetpack,yith woocommerce waiting list,waiting list,waiting list jetpack,yith woocommerce tab manager,tab manager,tab manager jetpack,yith woocommerce multi vendor,multi vendor,multi vendor jetpack,product vendor,products vendor,products vendor jetpack, booster for woocommerce
5
  Requires at least: 3.5.1
6
  Tested up to: 4.2.4
7
+ Stable tag: 1.1.13
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
151
 
152
  == Changelog ==
153
 
154
+ = 1.1.13 =
155
+
156
+ * Update: YITH WooCommerce Multi Vendor
157
+
158
  = 1.1.12 =
159
 
160
  * Fixed: YITH WooCommerce Authorized Net issue
init.php CHANGED
@@ -7,7 +7,7 @@ Text Domain: yith-essential-kit-for-woocommerce-1
7
  Domain Path: /languages/
8
  Author: YIThemes
9
  Author URI: http://yithemes.com/
10
- Version: 1.1.12
11
  */
12
 
13
  if ( ! defined( 'ABSPATH' ) ) {
7
  Domain Path: /languages/
8
  Author: YIThemes
9
  Author URI: http://yithemes.com/
10
+ Version: 1.1.13
11
  */
12
 
13
  if ( ! defined( 'ABSPATH' ) ) {
modules/yith-woocommerce-product-vendors/assets/css/admin.css CHANGED
@@ -72,6 +72,16 @@ select#tax-input-yith_shop_vendor {
72
  z-index: 999;
73
  }
74
 
 
 
 
 
 
 
 
 
 
 
75
  .taxonomy-yith_shop_vendor table.wp-list-table .column-name,
76
  .taxonomy-yith_shop_vendor table.wp-list-table .column-description,
77
  .taxonomy-yith_shop_vendor table.wp-list-table .column-slug
@@ -156,7 +166,8 @@ select#tax-input-yith_shop_vendor {
156
  .widefat .column-enable_sales mark:after,
157
  .widefat .column-suborder mark:after,
158
  .widefat .column-vat mark:after,
159
- .single-orders.suborders-list .suborder-info mark:after {
 
160
  font-family: WooCommerce;
161
  speak: none;
162
  font-weight: 400;
@@ -241,6 +252,12 @@ select#tax-input-yith_shop_vendor {
241
  color: #ffba00
242
  }
243
 
 
 
 
 
 
 
244
  .widefat .column-suborder mark.completed:after,
245
  .single-orders.suborders-list .suborder-info mark.completed:after {
246
  content: "\e015";
72
  z-index: 999;
73
  }
74
 
75
+ /* Add-ons tab */
76
+
77
+ #yith_wpv_panel_add-ons #plugin-fw-wc h3,
78
+ #yith_wpv_panel_add-ons #plugin-fw-wc > p {
79
+ display: inline-block;
80
+ }
81
+
82
+
83
+ /* Orders */
84
+
85
  .taxonomy-yith_shop_vendor table.wp-list-table .column-name,
86
  .taxonomy-yith_shop_vendor table.wp-list-table .column-description,
87
  .taxonomy-yith_shop_vendor table.wp-list-table .column-slug
166
  .widefat .column-enable_sales mark:after,
167
  .widefat .column-suborder mark:after,
168
  .widefat .column-vat mark:after,
169
+ .single-orders.suborders-list .suborder-info mark:after,
170
+ .widefat .column-order_status mark.trash:after{
171
  font-family: WooCommerce;
172
  speak: none;
173
  font-weight: 400;
252
  color: #ffba00
253
  }
254
 
255
+ .widefat .column-suborder mark.trash:after,
256
+ .widefat .column-order_status mark.trash:after {
257
+ content: "\e602";
258
+ color: #a00
259
+ }
260
+
261
  .widefat .column-suborder mark.completed:after,
262
  .single-orders.suborders-list .suborder-info mark.completed:after {
263
  content: "\e015";
modules/yith-woocommerce-product-vendors/assets/js/admin.js CHANGED
@@ -78,6 +78,7 @@
78
  //Vendors options deps
79
  var vendor_name_style = $('#yith_wpv_vendor_name_style'),
80
  vendor_order_refund = $('#yith_wpv_vendors_option_order_management');
 
81
  $('#yith_wpv_enable_product_amount').yith_wpv_option_deps( '#yith_wpv_vendors_product_limit', 'checkbox', undefined, false );
82
  $('#yith_wpv_report_abuse_link').yith_wpv_option_deps( '#yith_wpv_report_abuse_link_text', 'select', 'none', false );
83
  vendor_name_style.yith_wpv_option_deps( '#yith_vendors_color_name', 'select', 'theme', true );
@@ -85,6 +86,7 @@
85
  $('#yith_wpv_vendors_my_account_registration').yith_wpv_option_deps( '#yith_wpv_vendors_my_account_registration_auto_approve', 'checkbox', undefined, false );
86
  vendor_order_refund.yith_wpv_option_deps( '#yith_wpv_vendors_option_order_synchronization', 'checkbox', undefined, false );
87
  vendor_order_refund.yith_wpv_option_deps( '#yith_wpv_vendors_option_order_refund_synchronization', 'checkbox', undefined, false );
 
88
 
89
  // Vendor taxonomy table
90
  var tax_table = $( '#the-list');
78
  //Vendors options deps
79
  var vendor_name_style = $('#yith_wpv_vendor_name_style'),
80
  vendor_order_refund = $('#yith_wpv_vendors_option_order_management');
81
+
82
  $('#yith_wpv_enable_product_amount').yith_wpv_option_deps( '#yith_wpv_vendors_product_limit', 'checkbox', undefined, false );
83
  $('#yith_wpv_report_abuse_link').yith_wpv_option_deps( '#yith_wpv_report_abuse_link_text', 'select', 'none', false );
84
  vendor_name_style.yith_wpv_option_deps( '#yith_vendors_color_name', 'select', 'theme', true );
86
  $('#yith_wpv_vendors_my_account_registration').yith_wpv_option_deps( '#yith_wpv_vendors_my_account_registration_auto_approve', 'checkbox', undefined, false );
87
  vendor_order_refund.yith_wpv_option_deps( '#yith_wpv_vendors_option_order_synchronization', 'checkbox', undefined, false );
88
  vendor_order_refund.yith_wpv_option_deps( '#yith_wpv_vendors_option_order_refund_synchronization', 'checkbox', undefined, false );
89
+ $('#yith_vendors_show_gravatar_image').yith_wpv_option_deps( '#yith_vendors_gravatar_image_size', 'select', 'disabled', false );
90
 
91
  // Vendor taxonomy table
92
  var tax_table = $( '#the-list');
modules/yith-woocommerce-product-vendors/includes/class.yith-orders.php CHANGED
@@ -7,7 +7,7 @@
7
  * It is also available through the world-wide-web at this URL:
8
  * http://www.gnu.org/licenses/gpl-3.0.txt
9
  */
10
- if ( ! defined( 'ABSPATH' ) ) {
11
  exit( 'Direct access forbidden.' );
12
  }
13
 
@@ -20,7 +20,7 @@ if ( ! defined( 'ABSPATH' ) ) {
20
  * @author Your Inspiration Themes
21
  *
22
  */
23
- if ( ! class_exists( 'YITH_Orders' ) ) {
24
 
25
  class YITH_Orders {
26
 
@@ -35,49 +35,49 @@ if ( ! class_exists( 'YITH_Orders' ) ) {
35
  /**
36
  * Constructor
37
  */
38
- public function __construct() {
39
- add_action( 'woocommerce_checkout_update_order_meta', array( $this, 'check_suborder' ), 10, 2 );
40
 
41
  /* Prevent Multiple Email Notifications for Suborders */
42
- add_filter( 'woocommerce_email_recipient_new_order', array( $this, 'woocommerce_email_recipient_new_order' ), 10, 2 );
43
- add_filter( 'woocommerce_email_recipient_cancelled_order', array( $this, 'woocommerce_email_recipient_new_order' ), 10, 2 );
44
- add_filter( 'woocommerce_email_enabled_customer_processing_order', array( $this, 'woocommerce_email_enabled_new_order' ), 10, 2 );
45
- add_filter( 'woocommerce_email_enabled_customer_completed_order', array( $this, 'woocommerce_email_enabled_new_order' ), 10, 2 );
46
- add_filter( 'woocommerce_email_enabled_customer_partially_refunded_order', array( $this, 'woocommerce_email_enabled_new_order' ), 10, 2 );
47
- add_filter( 'woocommerce_email_enabled_customer_refunded_order', array( $this, 'woocommerce_email_enabled_new_order' ), 10, 2 );
48
 
49
- add_action( 'woocommerce_order_refunded', array( $this, 'order_refunded' ), 10, 2 );
50
- add_action( 'woocommerce_refund_deleted', array( $this, 'refund_deleted' ), 10, 2 );
51
 
52
- add_filter( 'woocommerce_attribute_label', array( $this, 'commissions_attribute_label' ), 10, 3 );
53
 
54
  /* Single Order Page for Vendor */
55
- add_filter( 'wc_order_is_editable', array( $this, 'vendor_single_order_page' ) );
56
 
57
- add_filter( 'woocommerce_attribute_label', array( $this, 'commissions_attribute_label' ), 10, 3 );
58
 
59
  /* Single Order Page for Vendor */
60
- add_filter( 'wc_order_is_editable', array( $this, 'vendor_single_order_page' ) );
61
 
62
- /* Order Item Meta */
63
- add_action( 'woocommerce_hidden_order_itemmeta', array( $this, 'hidden_order_itemmeta' ) );
64
 
65
  /* Order Table */
66
- add_filter( 'manage_shop_order_posts_columns', array( $this, 'shop_order_columns' ) );
67
- add_action( 'manage_shop_order_posts_custom_column', array( $this, 'render_shop_order_columns' ) );
68
 
69
  /* Order MetaBoxes */
70
- add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ), 30 );
71
 
72
- $sync_enabled = get_option( 'yith_wpv_vendors_option_order_synchronization', 'yes' );
73
 
74
- if( $sync_enabled ){
75
  /* SubOrder Sync */
76
- add_action( 'woocommerce_order_status_changed', array( $this, 'suborder_status_synchronization' ), 10, 3 );
77
  /* Order Meta Synchronization */
78
- add_action( 'woocommerce_process_shop_order_meta', array( $this, 'suborder_meta_synchronization' ), 65, 2 );
79
  /* Commission Synchronization */
80
- add_action( 'yith_wcmv_after_single_register_commission', array( $this, 'register_commission_to_parent_order' ), 10, 4 );
81
 
82
  /**
83
  * Other Ajax Action:
@@ -85,26 +85,26 @@ if ( ! class_exists( 'YITH_Orders' ) ) {
85
  * load_order_items
86
  * woocommerce_EVENT => nopriv
87
  */
88
- $ajax_events = array(
89
- 'add_order_item' => false,
90
  /*'add_order_fee' => false,*/
91
  /*'add_order_shipping' => false,*/
92
- 'add_order_tax' => false,
93
- 'remove_order_item' => false,
94
- 'remove_order_tax' => false,
95
- 'reduce_order_item_stock' => false,
96
- 'increase_order_item_stock' => false,
97
  /*'add_order_item_meta' => false, */
98
- 'remove_order_item_meta' => false,
99
- 'calc_line_taxes' => false,
100
- 'save_order_items' => false,
101
- 'add_order_note' => false,
102
- 'delete_order_note' => false,
103
  );
104
 
105
  foreach ( $ajax_events as $ajax_event => $nopriv ) {
106
- add_action( "wp_ajax_woocommerce_{$ajax_event}", array( __CLASS__, $ajax_event ), 5 );
107
- $nopriv && add_action( "wp_ajax_nopriv_woocommerce_{$ajax_event}", array( __CLASS__, $ajax_event, 5 ) );
108
  }
109
  }
110
  }
@@ -119,40 +119,38 @@ if ( ! class_exists( 'YITH_Orders' ) ) {
119
  * @since 1.6
120
  * @return void
121
  */
122
- public function check_suborder( $parent_order_id, $posted ) {
123
- $parent_order = wc_get_order( $parent_order_id );
124
- $items = $parent_order->get_items();
125
- $products_by_vendor = array();
126
- $suborder_ids = array();
127
 
128
  //check for vendor product
129
  foreach ( $items as $item ) {
130
- $vendor = yith_get_vendor( $item['product_id'], 'product' );
131
- if ( $vendor->is_valid() ) {
132
- $products_by_vendor[$vendor->id][] = $item;
133
  }
134
  }
135
 
136
- $vendor_count = count( $products_by_vendor );
137
 
138
  //Vendor's items ? NO
139
  if ( $vendor_count == 0 ) {
140
  return false;
141
- }
142
-
143
- //Vendor's items ? YES
144
  else {
145
  //add sub-order to parent
146
- update_post_meta( $parent_order_id, 'has_sub_order', true );
147
 
148
  foreach ( $products_by_vendor as $vendor_id => $vendor_products ) {
149
  //create sub-orders
150
- $suborder_ids[] = $this->create_suborder( $parent_order, $vendor_id, $vendor_products, $posted );
151
  }
152
 
153
- if( ! empty( $suborder_ids ) ){
154
- foreach( $suborder_ids as $suborder_id ){
155
- do_action( 'yith_wcmv_checkout_order_processed', $suborder_id );
156
  }
157
  }
158
  }
@@ -171,43 +169,43 @@ if ( ! class_exists( 'YITH_Orders' ) ) {
171
  * @since 1.6
172
  * @return void
173
  */
174
- public function create_suborder( $parent_order, $vendor_id, $vendor_products, $posted ) {
175
  /** @var $parent_order WC_Order */
176
- $vendor = yith_get_vendor( $vendor_id, 'vendor' );
177
- $order_data = apply_filters( 'woocommerce_new_order_data', array(
178
  'post_type' => 'shop_order',
179
- 'post_title' => sprintf( __( 'Order – %s', 'woocommerce' ), strftime( _x( '%b %d, %Y @ %I:%M %p', 'Order date parsed by strftime', 'woocommerce' ) ) ),
180
- 'post_status' => 'wc-' . apply_filters( 'woocommerce_default_order_status', 'pending' ),
181
  'ping_status' => 'closed',
182
- 'post_excerpt' => isset( $posted['order_comments'] ) ? $posted['order_comments'] : '',
183
- 'post_author' => $vendor->get_owner(),
184
  'post_parent' => $parent_order->id,
185
- 'post_password' => uniqid( 'order_' ) // Protects the post just in case
186
  )
187
  );
188
 
189
- $suborder_id = wp_insert_post( $order_data );
190
- $suborder = wc_get_order( $suborder_id );
191
- $parent_line_items = $parent_order->get_items( 'line_item' );
192
 
193
- if ( ! empty( $suborder_id ) && ! is_wp_error( $suborder_id ) ) {
194
  $order_total = $order_tax = $discount = 0;
195
- $product_ids = array();
196
 
197
  // now insert line items
198
  foreach ( $vendor_products as $item ) {
199
- $order_total += (float) $item['line_total'];
200
- $order_tax += (float) $item['line_tax'];
201
- $product_ids[] = $item['product_id'];
202
 
203
- $item_id = wc_add_order_item( $suborder_id, array(
204
- 'order_item_name' => $item['name'],
205
- 'order_item_type' => 'line_item'
206
  )
207
  );
208
 
209
  if ( $item_id ) {
210
- $metakeys = array(
211
  'qty',
212
  'tax_class',
213
  'product_id',
@@ -216,7 +214,7 @@ if ( ! class_exists( 'YITH_Orders' ) ) {
216
  'line_total',
217
  'line_tax',
218
  'line_subtotal_tax',
219
- 'line_tax_data'
220
  );
221
 
222
  foreach ( $metakeys as $key ) {
@@ -229,13 +227,13 @@ if ( ! class_exists( 'YITH_Orders' ) ) {
229
  * @see woocommerce/includes/wc-order-functions.php:449
230
  * @use wp-includes/meta.php:31
231
  */
232
- wc_add_order_item_meta( $item_id, "_{$key}", maybe_unserialize( $item[$key] ) );
233
  if ( 'product_id' == $key ) {
234
  foreach ( $parent_line_items as $line_item_id => $line_item_value ) {
235
  //@internal $key == 'product_id'
236
- if ( $item[$key] == $line_item_value[$key] ) {
237
  // add line item to retrieve simply the parent line_item_id
238
- wc_add_order_item_meta( $item_id, '_parent_line_item_id', $line_item_id );
239
  break;
240
  }
241
  }
@@ -244,11 +242,11 @@ if ( ! class_exists( 'YITH_Orders' ) ) {
244
  }
245
 
246
  //Calculate Discount
247
- $discount += ( $item['line_subtotal'] - $item['line_total'] );
248
  }
249
 
250
  //Billing order meta
251
- $billing_order_meta = array(
252
  'billing_country',
253
  'billing_first_name',
254
  'billing_last_name',
@@ -259,16 +257,16 @@ if ( ! class_exists( 'YITH_Orders' ) ) {
259
  'billing_state',
260
  'billing_postcode',
261
  'billing_email',
262
- 'billing_phone'
263
  );
264
 
265
  foreach ( $billing_order_meta as $order_meta_key ) {
266
  $meta_key = '_' . $order_meta_key;
267
- update_post_meta( $suborder_id, $meta_key, $parent_order->$order_meta_key );
268
  }
269
 
270
  //Shipping order meta
271
- $shipping_order_meta = array(
272
  'shipping_country',
273
  'shipping_first_name',
274
  'shipping_last_name',
@@ -277,12 +275,12 @@ if ( ! class_exists( 'YITH_Orders' ) ) {
277
  'shipping_address_2',
278
  'shipping_city',
279
  'shipping_state',
280
- 'shipping_postcode'
281
  );
282
 
283
  foreach ( $shipping_order_meta as $order_meta_key ) {
284
  $meta_key = '_' . $order_meta_key;
285
- update_post_meta( $suborder_id, $meta_key, $parent_order->$order_meta_key );
286
  }
287
 
288
  //Shipping
@@ -290,23 +288,23 @@ if ( ! class_exists( 'YITH_Orders' ) ) {
290
  $shipping_cost = 0;
291
 
292
  //Coupons
293
- $order_coupons = $parent_order->get_used_coupons();
294
  if ( ! empty( $order_coupons ) ) {
295
  foreach ( $order_coupons as $order_coupon ) {
296
  $coupon = new WC_Coupon( $order_coupon );
297
 
298
- if ( $coupon && in_array( $product_ids, $coupon->product_ids ) ) {
299
- $order_item_id = wc_add_order_item( $suborder_id, array(
300
  'order_item_name' => $order_coupon,
301
- 'order_item_type' => 'coupon'
302
  )
303
  );
304
 
305
  // Add line item meta
306
  if ( $order_item_id ) {
307
- $order_item_value = isset( WC()->cart->coupon_discount_amounts[$order_coupon] ) ? WC()->cart->coupon_discount_amounts[$order_coupon] : 0;
308
  $meta_key = 'discount_amount';
309
- wc_add_order_item_meta( $order_item_id, $meta_key, $order_item_value );
310
  }
311
  }
312
  }
@@ -315,45 +313,45 @@ if ( ! class_exists( 'YITH_Orders' ) ) {
315
  //Calculate Total
316
  $order_in_total = $order_total + $shipping_cost + $order_tax;
317
 
318
- $totals = array(
319
- 'shipping' => wc_format_decimal( $shipping_cost ),
320
- 'cart_discount' => wc_format_decimal( $discount ),
321
  'cart_discount_tax' => 0,
322
- 'tax' => wc_format_decimal( $order_tax ),
323
  'order_shipping_tax' => 0,
324
- 'total' => wc_format_decimal( $order_in_total ),
325
  );
326
 
327
  //Set tax. N.B.: needs total to works
328
- if ( function_exists( 'WC' ) && is_a( WC()->cart, 'WC_Cart' ) ) {
329
  /** @var WC_Cart $cart */
330
- $_cart = WC()->cart;
331
- foreach ( array_keys( $_cart->taxes + $_cart->shipping_taxes ) as $tax_rate_id ) {
332
- if ( $_cart && $tax_rate_id && apply_filters( 'woocommerce_cart_remove_taxes_zero_rate_id', 'zero-rated' ) !== $tax_rate_id ) {
333
- $suborder->add_tax( $tax_rate_id, $totals['tax'], $totals['order_shipping_tax'] );
334
  }
335
  }
336
  }
337
 
338
  //Set totals
339
  foreach ( $totals as $meta_key => $meta_value ) {
340
- $suborder->set_total( $meta_value, $meta_key );
341
  }
342
 
343
  //Set other order meta
344
- $order_meta = array(
345
  '_payment_method' => $parent_order->payment_method,
346
  '_payment_method_title' => $parent_order->payment_method_title,
347
- '_order_key' => apply_filters( 'woocommerce_generate_order_key', uniqid( 'order_' ) ),
348
  '_customer_user' => $parent_order->customer_user,
349
  '_prices_include_tax' => $parent_order->prices_include_tax,
350
- '_order_currency' => get_post_meta( $parent_order->id, '_order_currency', true ),
351
- '_customer_ip_address' => get_post_meta( $parent_order->id, '_customer_ip_address', true ),
352
- '_customer_user_agent' => get_post_meta( $parent_order->id, '_customer_user_agent', true )
353
  );
354
 
355
  foreach ( $order_meta as $meta_key => $meta_value ) {
356
- update_post_meta( $suborder_id, $meta_key, $meta_value );
357
  }
358
  }
359
 
@@ -374,18 +372,18 @@ if ( ! class_exists( 'YITH_Orders' ) ) {
374
  * @since 1.6
375
  * @return void
376
  */
377
- public function suborder_status_synchronization( $parent_order_id, $old_status, $new_status ) {
378
  //Check if order have sub-order
379
- if ( wp_get_post_parent_id( $parent_order_id ) ) {
380
  return false;
381
  }
382
 
383
- $suborder_ids = self::get_suborder( $parent_order_id );
384
  if ( ! empty( $suborder_ids ) ) {
385
  foreach ( $suborder_ids as $suborder_id ) {
386
  /** @var $suborder WC_Order */
387
- $suborder = wc_get_order( $suborder_id );
388
- $suborder->update_status( $new_status, _x( 'Update by admin: ', 'Order note', 'yith_wc_product_vendors' ) );
389
  }
390
  }
391
  }
@@ -403,27 +401,27 @@ if ( ! class_exists( 'YITH_Orders' ) ) {
403
  * @since 1.6
404
  * @return void
405
  */
406
- public function suborder_meta_synchronization( $parent_order_id, $parent_order ) {
407
  //Check if order have sub-order
408
- if ( wp_get_post_parent_id( $parent_order_id ) ) {
409
  return false;
410
  }
411
 
412
  /** @var $suborder WC_Order */
413
  /** @var $parent_order WC_Order */
414
- $suborder_ids = self::get_suborder( $parent_order_id );
415
- $parent_order = wc_get_order( $parent_order_id );
416
  if ( ! empty( $suborder_ids ) ) {
417
  foreach ( $suborder_ids as $suborder_id ) {
418
- $suborder = wc_get_order( $suborder_id );
419
- $child_items = array_keys( $suborder->get_items() );
420
- $_post = $_POST;
421
- $_post['order_item_id'] = $child_items;
422
- $suborder_line_total = 0;
423
 
424
  foreach ( $child_items as $child_items_id ) {
425
- $parent_item_id = $suborder->get_item_meta( $child_items_id, '_parent_line_item_id' );
426
- $parent_item_id = absint( array_shift( $parent_item_id ) );
427
  foreach ( $_post as $meta_key => $meta_value ) {
428
  //TODO: Shipping Cost
429
  switch ( $meta_key ) {
@@ -433,16 +431,16 @@ if ( ! class_exists( 'YITH_Orders' ) ) {
433
  case 'order_item_qty':
434
  case 'refund_line_total':
435
  case 'refund_order_item_qty':
436
- if ( isset( $_post[$meta_key][$parent_item_id] ) ) {
437
- $_post[$meta_key][$child_items_id] = $_post[$meta_key][$parent_item_id];
438
- unset( $_post[$meta_key][$parent_item_id] );
439
  }
440
  break;
441
 
442
  case 'shipping_cost':
443
- if ( isset( $_post[$meta_key][$parent_item_id] ) ) {
444
- $_post[$meta_key][$child_items_id] = 0;
445
- unset( $_post[$meta_key][$parent_item_id] );
446
  }
447
  break;
448
  default: //nothing to do
@@ -451,24 +449,25 @@ if ( ! class_exists( 'YITH_Orders' ) ) {
451
  }
452
 
453
  //Calculate Order Total
454
- if ( isset( $_post['line_total'][$child_items_id] ) ) {
455
- $suborder_line_total += wc_format_decimal( $_post['line_total'][$child_items_id] );
456
  }
457
  }
458
 
459
  //New Order Total
460
- $_post['_order_total'] = wc_format_decimal( $suborder_line_total );
461
 
462
  /**
463
  * Don't use save method by WC_Meta_Box_Order_Items class because I need to filter the POST information
464
  * use wc_save_order_items( $order_id, $items ) function directly.
 
465
  * @see WC_Meta_Box_Order_Items::save( $suborder_id, $suborder ); in woocommerce\includes\admin\meta-boxes\class-wc-meta-box-order-items.php:45
466
  * @see wc_save_order_items( $order_id, $items ); in woocommerce\includes\admin\wc-admin-functions.php:176
467
  */
468
- wc_save_order_items( $suborder_id, $_post );
469
- WC_Meta_Box_Order_Downloads::save( $suborder_id, $suborder );
470
- WC_Meta_Box_Order_Data::save( $suborder_id, $suborder );
471
- WC_Meta_Box_Order_Actions::save( $suborder_id, $suborder );
472
  }
473
  }
474
  }
@@ -481,13 +480,13 @@ if ( ! class_exists( 'YITH_Orders' ) ) {
481
  *
482
  * @author Andrea Grillo <andrea.grillo@yithemes.com>
483
  * @since 1.6
484
- * @return void
485
  */
486
- public static function get_suborder( $parent_order_id = false ) {
487
- $suborder_ids = array();
488
  if ( $parent_order_id ) {
489
  global $wpdb;
490
- $suborder_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_parent=%d AND post_type=%s", absint( $parent_order_id ), 'shop_order' ) );
491
  }
492
 
493
  return $suborder_ids;
@@ -504,18 +503,16 @@ if ( ! class_exists( 'YITH_Orders' ) ) {
504
  * @since 1.6
505
  * @return int|bool The parent item id if exist, false otherwise
506
  */
507
- public static function get_parent_item_id( $suborder = false, $child_item_id ) {
508
- global$wpdb;
509
  $parent_item_id = false;
510
 
511
- if( ! $suborder ){
512
- $parent_item_id = $wpdb->get_var( $wpdb->prepare( "SELECT DISTINCT order_item_id FROM {$wpdb->order_itemmeta} WHERE meta_id=%d", $child_item_id ) );
513
  $parent_item_id = ! empty( $parent_item_id ) ? $parent_item_id : false;
514
- }
515
-
516
- else {
517
- $parent_item_id = $suborder->get_item_meta( $child_item_id, '_parent_line_item_id' );
518
- $parent_item_id = ! empty( $parent_item_id ) ? absint( array_shift( $parent_item_id ) ) : false;
519
  }
520
 
521
 
@@ -531,9 +528,10 @@ if ( ! class_exists( 'YITH_Orders' ) ) {
531
  * @since 1.6
532
  * @return int|bool The parent item id if exist, false otherwise
533
  */
534
- public static function get_child_item_id( $parent_item_id ) {
535
  global $wpdb;
536
- $child_item_id = $wpdb->get_var( $wpdb->prepare( "SELECT order_item_id FROM {$wpdb->order_itemmeta} WHERE meta_key=%s AND meta_value=%d", '_parent_line_item_id', absint( $parent_item_id ) ) );
 
537
  return $child_item_id;
538
  }
539
 
@@ -546,9 +544,10 @@ if ( ! class_exists( 'YITH_Orders' ) ) {
546
  * @since 1.6
547
  * @return int|bool The child item id if exist, false otherwise
548
  */
549
- public static function get_line_item_id_from_parent( $order_item_id ){
550
  global $wpdb;
551
- return $wpdb->get_var( $wpdb->prepare( "SELECT DISTINCT order_item_id FROM {$wpdb->order_itemmeta} WHERE meta_key=%s AND meta_value=%d", '_parent_line_item_id', $order_item_id ) );
 
552
  }
553
 
554
  /**
@@ -557,76 +556,74 @@ if ( ! class_exists( 'YITH_Orders' ) ) {
557
  * @author Andrea Grillo <andrea.grillo@yithemes.com>
558
  * @since 1.6
559
  * @return void
560
- * @access public static
561
  */
562
- public static function save_order_items() {
563
- check_ajax_referer( 'order-item', 'security' );
564
 
565
- if ( ! current_user_can( 'edit_shop_orders' ) ) {
566
  die( - 1 );
567
  }
568
 
569
- if ( isset( $_POST['order_id'] ) && isset( $_POST['items'] ) ) {
570
- $parent_order_id = absint( $_POST['order_id'] );
571
  //Check if order have sub-order
572
- if ( ! wp_get_post_parent_id( $parent_order_id ) ) {
573
  global $wpdb;
574
  // Parse the jQuery serialized items
575
  $_post = $_POST;
576
- parse_str( $_post['items'], $_post['items'] );
577
- $suborder_ids = self::get_suborder( $parent_order_id );
578
  foreach ( $suborder_ids as $suborder_id ) {
579
- $order_total = 0;
580
- $suborder = wc_get_order( $suborder_id );
581
- $child_items = array_keys( $suborder->get_items() );
582
- $_post['items']['order_item_id'] = $child_items;
583
  foreach ( $child_items as $child_item_id ) {
584
- $parent_item_id = self::get_parent_item_id( $suborder, $child_item_id );
585
- foreach ( $_post['items'] as $meta_key => $meta_value ) {
586
- if ( ! in_array( $meta_key, array( 'order_item_id', '_order_total' ) ) && isset( $_post['items'][$meta_key][$parent_item_id] ) ) {
587
- $_post['items'][$meta_key][$child_item_id] = $_post['items'][$meta_key][$parent_item_id];
588
- unset( $_post['items'][$meta_key][$parent_item_id] );
589
  }
590
  }
591
 
592
  /* === Calc Order Totals === */
593
- if ( ! empty( $_post['items']['line_total'][$child_item_id] ) ) {
594
- $order_total += wc_format_decimal( $_post['items']['line_total'][$child_item_id] );
595
- if( isset( $_post['items']['line_tax'][$child_item_id] ) ){
596
- $line_taxes = $_post['items']['line_tax'][$child_item_id];
597
- foreach( $line_taxes as $line_tax ){
598
- $order_total += wc_format_decimal( $line_tax );
599
  }
600
  }
601
  }
602
 
603
  /* === Calc Refund Totals === */
604
- if ( ! empty( $_post['items']['refund_line_total'][$child_item_id] ) ) {
605
- $order_total += wc_format_decimal( $_post['items']['refund_line_total'][$child_item_id] );
606
  }
607
  /* ======================== */
608
  }
609
 
610
  /* === Save Parent Meta === */
611
- $meta_keys = isset( $_post['items']['meta_key'] ) ? $_post['items']['meta_key'] : array();
612
- $meta_values = isset( $_post['items']['meta_value'] ) ? $_post['items']['meta_value'] : array();
613
  foreach ( $meta_keys as $meta_id => $meta_key ) {
614
- $meta_value = ( empty( $meta_values[$meta_id] ) && ! is_numeric( $meta_values[$meta_id] ) ) ? '' : $meta_values[$meta_id];
615
- $parent_order_item_id = self::get_parent_item_id( false, $meta_id );
616
- $child_order_item_id = self::get_child_item_id( $parent_order_item_id );
617
- '_child__commission_id' != $meta_key && wc_update_order_item_meta( $child_order_item_id, '_parent_' . $meta_key, '_commission_id' != $meta_key ? $meta_id : $meta_values[ $meta_id ] );
618
  }
619
  /* ======================== */
620
 
621
  // Add order total
622
- $_post['items']['_order_total'] = $order_total;
623
 
624
  // Save order items
625
- wc_save_order_items( $suborder_id, $_post['items'] );
626
  }
627
- }
628
-
629
- else {
630
  //is suborder
631
  //TODO: Suborder sub-routine
632
  }
@@ -639,29 +636,29 @@ if ( ! class_exists( 'YITH_Orders' ) ) {
639
  * @author Andrea Grillo <andrea.grillo@yithemes.com>
640
  * @since 1.6
641
  * @return void
642
- * @access public static
643
  */
644
- public static function remove_order_item() {
645
- check_ajax_referer( 'order-item', 'security' );
646
 
647
- if ( ! current_user_can( 'edit_shop_orders' ) ) {
648
  die( - 1 );
649
  }
650
 
651
- $order_item_ids = $_POST['order_item_ids'];
652
- if ( ! is_array( $order_item_ids ) && is_numeric( $order_item_ids ) ) {
653
- $order_item_ids = array( $order_item_ids );
654
  }
655
  //TODO: add check order_id if ( ! wp_get_post_parent_id( $parent_order_id ) ) {
656
- if ( sizeof( $order_item_ids ) > 0 ) {
657
  /** @var $wpdb wpdb */
658
  global $wpdb;
659
  foreach ( $order_item_ids as $order_item_id ) {
660
- $product_id = $wpdb->get_var( $wpdb->prepare( "SELECT DISTINCT meta_value FROM {$wpdb->order_itemmeta} WHERE meta_key=%s AND order_item_id=%d", '_product_id', absint( $order_item_id ) ) );
661
- $vendor = yith_get_vendor( $product_id, 'product' );
662
- if ( $vendor->is_valid() ) {
663
- $child_order_item_id = self::get_line_item_id_from_parent( $order_item_id );
664
- ! empty( $child_order_item_id ) && wc_delete_order_item( absint( $child_order_item_id ) );
665
  }
666
  }
667
  }
@@ -674,41 +671,41 @@ if ( ! class_exists( 'YITH_Orders' ) ) {
674
  * @author Andrea Grillo <andrea.grillo@yithemes.com>
675
  * @return void
676
  */
677
- public static function add_order_note() {
678
 
679
- check_ajax_referer( 'add-order-note', 'security' );
680
 
681
- if ( ! current_user_can( 'edit_shop_orders' ) ) {
682
  die( - 1 );
683
  }
684
 
685
- $post_id = absint( $_POST['post_id'] );
686
- $note = wp_kses_post( trim( stripslashes( $_POST['note'] ) ) );
687
- $note_type = $_POST['note_type'];
688
 
689
  $is_customer_note = $note_type == 'customer' ? 1 : 0;
690
 
691
  if ( $post_id > 0 ) {
692
- if ( ! wp_get_post_parent_id( $post_id ) ) {
693
  //Add the order note to parent order
694
- $order = wc_get_order( $post_id );
695
- $parent_note_id = $order->add_order_note( $note, $is_customer_note, true );
696
 
697
- echo '<li rel="' . esc_attr( $parent_note_id ) . '" class="note ';
698
  if ( $is_customer_note ) {
699
  echo 'customer-note';
700
  }
701
  echo '"><div class="note_content">';
702
- echo wpautop( wptexturize( $note ) );
703
- echo '</div><p class="meta"><a href="#" class="delete_note">' . __( 'Delete note', 'woocommerce' ) . '</a></p>';
704
  echo '</li>';
705
 
706
- $suborder_ids = self::get_suborder( $post_id );
707
  if ( ! empty( $suborder_ids ) ) {
708
  foreach ( $suborder_ids as $suborder_id ) {
709
- $suborder = wc_get_order( $suborder_id );
710
- $note_id = $suborder->add_order_note( _x( 'Update by admin: ', 'Order note', 'yith_wc_product_vendors' ) . $note, $is_customer_note, true );
711
- add_comment_meta( $note_id, 'parent_note_id', $parent_note_id );
712
  }
713
  }
714
  /**
@@ -716,9 +713,7 @@ if ( ! class_exists( 'YITH_Orders' ) ) {
716
  * Updated Parent and Child orders
717
  */
718
  die();
719
- }
720
-
721
- else {
722
  //is suborder
723
  //TODO: Suborder sub-routine
724
  }
@@ -732,20 +727,20 @@ if ( ! class_exists( 'YITH_Orders' ) ) {
732
  * @author Andrea Grillo <andrea.grillo@yithemes.com>
733
  * @return void
734
  */
735
- public static function delete_order_note() {
736
- check_ajax_referer( 'delete-order-note', 'security' );
737
 
738
- if ( ! current_user_can( 'edit_shop_orders' ) ) {
739
  die( - 1 );
740
  }
741
 
742
  global $wpdb;
743
- $parent_note_id = absint( $_POST['note_id'] );
744
- $note_ids = $wpdb->get_col( $wpdb->prepare( "SELECT comment_id FROM {$wpdb->commentmeta} WHERE meta_key=%s AND meta_value=%d", 'parent_note_id', $parent_note_id ) );
745
 
746
  if ( ! empty( $note_ids ) ) {
747
- foreach( $note_ids as $note_id ){
748
- wp_delete_comment( $note_id );
749
  }
750
  }
751
  }
@@ -753,209 +748,206 @@ if ( ! class_exists( 'YITH_Orders' ) ) {
753
  /**
754
  * Reduce order item stock
755
  */
756
- public static function reduce_order_item_stock() {
757
- self::order_item_stock( 'reduce' );
758
  }
759
 
760
- /**
761
  * Increase order item stock
762
  */
763
- public static function increase_order_item_stock() {
764
- self::order_item_stock( 'increase' );
765
  }
766
 
767
  /**
768
  * Reduce order item stock
769
  */
770
- public static function order_item_stock( $ajax_call_type ) {
771
- check_ajax_referer( 'order-item', 'security' );
772
 
773
- if ( ! current_user_can( 'edit_shop_orders' ) ) {
774
  die( - 1 );
775
  }
776
 
777
- $order_id = absint( $_POST['order_id'] );
778
- if ( ! wp_get_post_parent_id( $order_id ) ) {
779
- $order_item_ids = isset( $_POST['order_item_ids'] ) ? $_POST['order_item_ids'] : array();
780
- $order_item_qty = isset( $_POST['order_item_qty'] ) ? $_POST['order_item_qty'] : array();
781
- $order = wc_get_order( $order_id );
782
- $order_items = $order->get_items();
783
 
784
- if ( $order && ! empty( $order_items ) && sizeof( $order_item_ids ) > 0 ) {
785
 
786
  foreach ( $order_items as $item_id => $order_item ) {
787
  // Only reduce checked items
788
- if ( ! in_array( $item_id, $order_item_ids ) ) {
789
  continue;
790
  }
791
 
792
- $_product = $order->get_product_from_item( $order_item );
793
- $vendor = yith_get_vendor( $_product, 'product' );
794
- if ( $vendor->is_valid() && $_product->exists() && $_product->managing_stock() && isset( $order_item_qty[$item_id] ) && $order_item_qty[$item_id] > 0 ) {
795
  global $wpdb;
796
 
797
- $old_stock = $_product->get_stock_quantity();
798
- $child_order_item_id = self::get_line_item_id_from_parent( $item_id );
799
- $suborder_id = $wpdb->get_var( $wpdb->prepare( "SELECT DISTINCT order_id FROM {$wpdb->prefix}woocommerce_order_items WHERE order_item_id=%d", absint( $child_order_item_id ) ) );
800
- $suborder = wc_get_order( $suborder_id );
801
  $note = '';
802
- if( 'reduce' == $ajax_call_type ){
803
- $stock_change = apply_filters( 'woocommerce_reduce_order_stock_quantity', $order_item_qty[$item_id], $item_id );
804
- $new_stock = $old_stock - $stock_change;
805
- $note = sprintf( __( 'Item #%s stock reduced from %s to %s.', 'woocommerce' ), $order_item['product_id'], $old_stock, $new_stock );
806
- }
807
-
808
- elseif( 'increase' == $ajax_call_type ) {
809
- $stock_change = apply_filters( 'woocommerce_restore_order_stock_quantity', $order_item_qty[$item_id], $item_id );
810
- $new_stock = $old_stock + $stock_change;
811
- $note = sprintf( __( 'Item #%s stock increased from %s to %s.', 'woocommerce' ), $order_item['product_id'], $old_stock, $new_stock );
812
  }
813
 
814
- ! empty( $note ) && $suborder->add_order_note( $note );
815
  }
816
  }
817
  }
 
 
 
818
  }
819
-
820
- else {
821
- //is suborder
822
- //TODO: Suborder sub-routine
823
- }
824
  }
825
 
826
  /**
827
  * Remove order item meta
828
  */
829
- public static function remove_order_item_meta(){
830
  global $wpdb;
831
 
832
- check_ajax_referer( 'order-item', 'security' );
833
 
834
- if ( ! current_user_can( 'edit_shop_orders' ) ) {
835
  die( - 1 );
836
  }
837
 
838
- $parent_meta_id = absint( $_POST['meta_id'] );
839
- $parent_meta_key = $wpdb->get_var( $wpdb->prepare( "SELECT DISTINCT meta_key FROM {$wpdb->order_itemmeta} WHERE meta_id=%d", $parent_meta_id ) );
840
- $child_meta_id = $wpdb->get_var( $wpdb->prepare( "SELECT DISTINCT meta_id FROM {$wpdb->order_itemmeta} WHERE meta_value=%d AND meta_key=%s", $parent_meta_id, '_parent_' . $parent_meta_key ) );
841
- $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->order_itemmeta} WHERE meta_key=%s AND meta_id=%d", '_parent_' . $parent_meta_key, $child_meta_id ) );
842
  }
843
 
844
  /**
845
  * Add order item via ajax
846
  */
847
- public static function add_order_item() {
848
- check_ajax_referer( 'order-item', 'security' );
849
 
850
- if ( ! current_user_can( 'edit_shop_orders' ) ) {
851
  die( - 1 );
852
  }
853
 
854
- $item_to_add = sanitize_text_field( $_POST['item_to_add'] );
855
- $order_id = absint( $_POST['order_id'] );
856
 
857
- if ( ! wp_get_post_parent_id( $order_id ) ) {
858
  // Find the item
859
- if ( ! is_numeric( $item_to_add ) ) {
860
  die();
861
  }
862
 
863
- $post = get_post( $item_to_add );
864
 
865
  if ( ! $post || ( 'product' !== $post->post_type && 'product_variation' !== $post->post_type ) ) {
866
  die();
867
  }
868
 
869
- $_product = wc_get_product( $post->ID );
870
- $order = wc_get_order( $order_id );
871
- $order_taxes = $order->get_taxes();
872
  $class = 'new_row';
873
  $suborders_id = 0;
874
 
875
- $vendor = yith_get_vendor( $item_to_add, 'product' );
876
 
877
- if ( $vendor->is_valid() ) {
878
- $vendor_suborder_id = $vendor->get_orders( 'suborder' );
879
- $suborders_ids = self::get_suborder( $order_id );
880
- $suborder_id = array_intersect( $vendor_suborder_id, $suborders_ids );
881
 
882
- if ( is_array( $suborder_id ) && count( $suborder_id ) == 1 ) {
883
- $suborder_id = array_shift( $suborder_id );
884
  }
885
  }
886
 
 
 
 
 
887
  // Set values
888
- $item = array();
889
- $item_ids = array();
890
-
891
- $item['product_id'] = $_product->id;
892
- $item['variation_id'] = isset( $_product->variation_id ) ? $_product->variation_id : '';
893
- $item['variation_data'] = $item['variation_id'] ? $_product->get_variation_attributes() : '';
894
- $item['name'] = $_product->get_title();
895
- $item['tax_class'] = $_product->get_tax_class();
896
- $item['qty'] = 1;
897
- $item['line_subtotal'] = wc_format_decimal( $_product->get_price_excluding_tax() );
898
- $item['line_subtotal_tax'] = '';
899
- $item['line_total'] = wc_format_decimal( $_product->get_price_excluding_tax() );
900
- $item['line_tax'] = '';
901
- $item['type'] = 'line_item';
902
 
903
  // Add line item
904
-
905
- foreach ( array( 'parent_id' => $order_id, 'child_id' => $suborder_id ) as $type => $id ) {
906
- $item_ids[$type] = wc_add_order_item( $id, array(
907
- 'order_item_name' => $item['name'],
908
- 'order_item_type' => 'line_item'
909
  ) );
910
  }
911
 
912
- wc_add_order_item_meta( $item_ids['child_id'], '_parent_line_item_id', $item_ids['parent_id'] );
913
 
914
  foreach ( $item_ids as $key => $item_id ) {
915
  // Add line item meta
916
  if ( $item_id ) {
917
- wc_add_order_item_meta( $item_id, '_qty', $item['qty'] );
918
- wc_add_order_item_meta( $item_id, '_tax_class', $item['tax_class'] );
919
- wc_add_order_item_meta( $item_id, '_product_id', $item['product_id'] );
920
- wc_add_order_item_meta( $item_id, '_variation_id', $item['variation_id'] );
921
- wc_add_order_item_meta( $item_id, '_line_subtotal', $item['line_subtotal'] );
922
- wc_add_order_item_meta( $item_id, '_line_subtotal_tax', $item['line_subtotal_tax'] );
923
- wc_add_order_item_meta( $item_id, '_line_total', $item['line_total'] );
924
- wc_add_order_item_meta( $item_id, '_line_tax', $item['line_tax'] );
925
 
926
  // Since 2.2
927
- wc_add_order_item_meta( $item_id, '_line_tax_data', array( 'total' => array(), 'subtotal' => array() ) );
928
 
929
  // Store variation data in meta
930
- if ( $item['variation_data'] && is_array( $item['variation_data'] ) ) {
931
- foreach ( $item['variation_data'] as $key => $value ) {
932
- wc_add_order_item_meta( $item_id, str_replace( 'attribute_', '', $key ), $value );
933
  }
934
  }
935
 
936
- do_action( 'woocommerce_ajax_add_order_item_meta', $item_id, $item );
937
  }
938
  }
939
 
940
- $item['item_meta'] = $order->get_item_meta( $item_ids['parent_id'] );
941
- $item['item_meta_array'] = $order->get_item_meta_array( $item_ids['parent_id'] );
942
- $item = $order->expand_item_meta( $item );
943
- $item = apply_filters( 'woocommerce_ajax_order_item', $item, $item_ids['parent_id'] );
944
 
945
  /**
946
  * WooCommerce Template Hack:
947
  * Copy the parent item id into the variable $item_id
948
  */
949
- $item_id = $item_ids['parent_id'];
950
- include( WC()->plugin_path() . '/includes/admin/meta-boxes/views/html-order-item.php' );
951
 
952
  /**
953
  * Prevent call default WooCommerce add_order_item() method
954
  */
955
  die();
956
- }
957
-
958
- else {
959
  //is suborder
960
  //TODO: Suborder sub-routine
961
  }
@@ -964,35 +956,33 @@ if ( ! class_exists( 'YITH_Orders' ) ) {
964
  /**
965
  * Add commission id from parent to child order
966
  */
967
- public function register_commission_to_parent_order( $commission_id, $child_item_id, $key, $suborder ){
968
  // add line item to retrieve simply the commission associated (child order)
969
- $parent_item_id = self::get_parent_item_id( $suborder, $child_item_id );
970
- ! empty( $parent_item_id ) && wc_add_order_item_meta( $parent_item_id, '_child_' . $key, $commission_id );
971
  }
972
 
973
  /**
974
  * Add order tax column via ajax
975
  */
976
- public static function add_order_tax() {
977
- check_ajax_referer( 'order-item', 'security' );
978
 
979
- if ( ! current_user_can( 'edit_shop_orders' ) ) {
980
  die( - 1 );
981
  }
982
 
983
- $order_id = absint( $_POST['order_id'] );
984
 
985
- if ( ! wp_get_post_parent_id( $order_id ) ) {
986
- $rate_id = absint( $_POST['rate_id'] );
987
- $suborder_ids = self::get_suborder( $order_id );
988
 
989
- foreach( $suborder_ids as $suborder_id ){
990
- $suborder = ! empty( $suborder_id ) ? wc_get_order( absint( $suborder_id ) ) : false;
991
- $suborder && $suborder->add_tax( $rate_id, 0, 0 );
992
  }
993
- }
994
-
995
- else {
996
  //is suborder
997
  //TODO: Suborder sub-routine
998
  }
@@ -1001,275 +991,258 @@ if ( ! class_exists( 'YITH_Orders' ) ) {
1001
  /**
1002
  * Calc line tax
1003
  */
1004
- public static function calc_line_taxes() {
1005
- check_ajax_referer( 'calc-totals', 'security' );
1006
 
1007
- if ( ! current_user_can( 'edit_shop_orders' ) ) {
1008
  die( - 1 );
1009
  }
1010
 
1011
- $order_id = absint( $_POST['order_id'] );
1012
 
1013
- if ( ! wp_get_post_parent_id( $order_id ) ) {
1014
  $_post = $_POST;
1015
- $suborder_ids = self::get_suborder( $order_id );
1016
 
1017
  foreach ( $suborder_ids as $suborder_id ) {
1018
- self::add_line_taxes( $suborder_id );
1019
  }
1020
- }
1021
-
1022
- else {
1023
  //is suborder
1024
  //TODO: Suborder sub-routine
1025
  }
1026
  }
1027
 
1028
- public static function add_line_taxes( $order_id ) {
1029
  global $wpdb;
1030
 
1031
- check_ajax_referer( 'calc-totals', 'security' );
1032
 
1033
- if ( ! current_user_can( 'edit_shop_orders' ) ) {
1034
  die( - 1 );
1035
  }
1036
 
1037
  $tax = new WC_Tax();
1038
- $items = array();
1039
- $country = strtoupper( esc_attr( $_POST['country'] ) );
1040
- $state = strtoupper( esc_attr( $_POST['state'] ) );
1041
- $postcode = strtoupper( esc_attr( $_POST['postcode'] ) );
1042
- $city = wc_clean( esc_attr( $_POST['city'] ) );
1043
- $order = wc_get_order( absint( $order_id ) );
1044
- $taxes = array();
1045
- $shipping_taxes = array();
1046
 
1047
  // Parse the jQuery serialized items
1048
- parse_str( $_POST['items'], $items );
1049
 
1050
  // Prevent undefined warnings
1051
- if ( ! isset( $items['line_tax'] ) ) {
1052
- $items['line_tax'] = array();
1053
  }
1054
 
1055
- if ( ! isset( $items['line_subtotal_tax'] ) ) {
1056
- $items['line_subtotal_tax'] = array();
1057
  }
1058
 
1059
- $items['order_taxes'] = array();
1060
 
1061
  // Action
1062
- $items = apply_filters( 'woocommerce_ajax_calc_line_taxes', $items, $order_id, $country, $_POST );
1063
 
1064
  // Get items and fees taxes
1065
- if ( isset( $items['order_item_id'] ) ) {
1066
- $line_total = $line_subtotal = $order_item_tax_class = array();
1067
- foreach ( $items['order_item_id'] as $parent_item_id ) {
1068
- $parent_item_id = absint( $parent_item_id );
1069
- $item_id = self::get_child_item_id( $parent_item_id );
1070
 
1071
- if( empty( $item_id ) ){
1072
  //no current suborder items
1073
  continue;
1074
  }
1075
 
1076
- $line_total[$item_id] = isset( $items['line_total'][$parent_item_id] ) ? wc_format_decimal( $items['line_total'][$parent_item_id] ) : 0;
1077
- $line_subtotal[$item_id] = isset( $items['line_subtotal'][$parent_item_id] ) ? wc_format_decimal( $items['line_subtotal'][$parent_item_id] ) : $line_total[$parent_item_id];
1078
- $order_item_tax_class[$item_id] = isset( $items['order_item_tax_class'][$parent_item_id] ) ? sanitize_text_field( $items['order_item_tax_class'][$parent_item_id] ) : '';
1079
- $product_id = $order->get_item_meta( $item_id, '_product_id', true );
1080
 
1081
- $vendor = yith_get_vendor( $product_id, 'product' );
1082
 
1083
- if( ! $vendor->is_valid() ){
1084
  // no vnedor products
1085
  continue;
1086
  }
1087
 
1088
- $vendor_order_ids = $vendor->get_orders( 'suborder' );
1089
 
1090
- if( ! in_array( $order_id, $vendor_order_ids ) ){
1091
  // the current product isn't in the current suborder
1092
  continue;
1093
  }
1094
 
1095
  // Get product details
1096
- if ( get_post_type( $product_id ) == 'product' ) {
1097
- $_product = wc_get_product( $product_id );
1098
- $item_tax_status = $_product->get_tax_status();
1099
- }
1100
- else {
1101
  $item_tax_status = 'taxable';
1102
  }
1103
 
1104
- if ( '0' !== $order_item_tax_class[$item_id] && 'taxable' === $item_tax_status ) {
1105
- $tax_rates = WC_Tax::find_rates( array(
1106
  'country' => $country,
1107
  'state' => $state,
1108
  'postcode' => $postcode,
1109
  'city' => $city,
1110
- 'tax_class' => $order_item_tax_class[$item_id]
1111
  ) );
1112
 
1113
- $line_taxes = WC_Tax::calc_tax( $line_total[$item_id], $tax_rates, false );
1114
- $line_subtotal_taxes = WC_Tax::calc_tax( $line_subtotal[$item_id], $tax_rates, false );
1115
 
1116
  // Set the new line_tax
1117
  foreach ( $line_taxes as $_tax_id => $_tax_value ) {
1118
- $items['line_tax'][$item_id][$_tax_id] = $_tax_value;
1119
  }
1120
 
1121
  // Set the new line_subtotal_tax
1122
  foreach ( $line_subtotal_taxes as $_tax_id => $_tax_value ) {
1123
- $items['line_subtotal_tax'][$item_id][$_tax_id] = $_tax_value;
1124
  }
1125
 
1126
  // Sum the item taxes
1127
- foreach ( array_keys( $taxes + $line_taxes ) as $key ) {
1128
- $taxes[$key] = ( isset( $line_taxes[$key] ) ? $line_taxes[$key] : 0 ) + ( isset( $taxes[$key] ) ? $taxes[$key] : 0 );
1129
  }
1130
  }
1131
  }
1132
  }
1133
 
1134
  // Get shipping taxes
1135
- if ( isset( $items['shipping_method_id'] ) ) {
1136
- $matched_tax_rates = array();
1137
 
1138
- $tax_rates = WC_Tax::find_rates( array(
1139
  'country' => $country,
1140
  'state' => $state,
1141
  'postcode' => $postcode,
1142
  'city' => $city,
1143
- 'tax_class' => ''
1144
  ) );
1145
 
1146
  if ( $tax_rates ) {
1147
  foreach ( $tax_rates as $key => $rate ) {
1148
- if ( isset( $rate['shipping'] ) && 'yes' == $rate['shipping'] ) {
1149
- $matched_tax_rates[$key] = $rate;
1150
  }
1151
  }
1152
  }
1153
 
1154
- $shipping_cost = $shipping_taxes = array();
1155
 
1156
- foreach ( $items['shipping_method_id'] as $item_id ) {
1157
- $item_id = absint( $item_id );
1158
- $shipping_cost[$item_id] = isset( $items['shipping_cost'][$parent_item_id] ) ? wc_format_decimal( $items['shipping_cost'][$parent_item_id] ) : 0;
1159
- $_shipping_taxes = WC_Tax::calc_shipping_tax( $shipping_cost[$item_id], $matched_tax_rates );
1160
 
1161
  // Set the new shipping_taxes
1162
  foreach ( $_shipping_taxes as $_tax_id => $_tax_value ) {
1163
- $items['shipping_taxes'][$item_id][$_tax_id] = $_tax_value;
1164
 
1165
- $shipping_taxes[$_tax_id] = isset( $shipping_taxes[$_tax_id] ) ? $shipping_taxes[$_tax_id] + $_tax_value : $_tax_value;
1166
  }
1167
  }
1168
  }
1169
 
1170
  // Remove old tax rows
1171
- $order->remove_order_items( 'tax' );
1172
 
1173
  // Add tax rows
1174
- foreach ( array_keys( $taxes + $shipping_taxes ) as $tax_rate_id ) {
1175
- $order->add_tax( $tax_rate_id, isset( $taxes[$tax_rate_id] ) ? $taxes[$tax_rate_id] : 0, isset( $shipping_taxes[$tax_rate_id] ) ? $shipping_taxes[$tax_rate_id] : 0 );
1176
  }
1177
 
1178
  // Create the new order_taxes
1179
- foreach ( $order->get_taxes() as $tax_id => $tax_item ) {
1180
- $items['order_taxes'][$tax_id] = absint( $tax_item['rate_id'] );
1181
  }
1182
 
1183
  foreach ( $items as $meta_key => $meta_values ) {
1184
- if ( is_array( $meta_values ) ) {
1185
  foreach ( $meta_values as $key => $meta_value ) {
1186
  if ( 'order_taxes' == $meta_key ) {
1187
  continue;
1188
- }
1189
-
1190
- else if ( 'order_item_id' == $meta_key ) {
1191
- $child_item_id = self::get_child_item_id( $meta_value );
1192
  if ( $child_item_id ) {
1193
- $items[$meta_key][$key] = $child_item_id;
1194
- }
1195
-
1196
- else {
1197
- unset( $items[$meta_key][$key] );
1198
  }
1199
- }
1200
-
1201
- else if ( 'meta_key' == $meta_key || 'meta_value' == $meta_key ) {
1202
- unset( $items[$meta_key][$key] );
1203
- }
1204
-
1205
- else {
1206
  if ( 'line_tax' == $meta_key || 'line_subtotal_tax' == $meta_key || 'refund_line_tax' == $meta_key ) {
1207
- $line_tax_ids = $items[$meta_key];
1208
- $child_item_ids = array_keys( $order->get_items() );
1209
  foreach ( $line_tax_ids as $line_tax_id => $line_tax_value ) {
1210
- if ( ! in_array( $line_tax_id, $child_item_ids ) ) {
1211
- unset( $items[$meta_key][$line_tax_id] );
1212
  }
1213
  }
1214
- }
1215
-
1216
- else {
1217
- $child_item_id = self::get_child_item_id( $meta_value );
1218
  if ( $child_item_id ) {
1219
- $items[$meta_key][$child_item_id] = $items[$meta_key][$key];
1220
- unset( $items[$meta_key][$key] );
1221
  }
1222
  }
1223
  }
1224
  }
1225
- }
1226
-
1227
- else if ( '_order_total' == $meta_key ) {
1228
- $items['_order_total'] = $order->get_total();
1229
  }
1230
  }
1231
 
1232
- if( ! empty( $items['order_item_id'] ) ) {
1233
- wc_save_order_items( $order_id, $items );
1234
  }
1235
  }
1236
 
1237
  /**
1238
  * Remove an order tax
1239
  */
1240
- public static function remove_order_tax() {
1241
 
1242
- check_ajax_referer( 'order-item', 'security' );
1243
 
1244
- if ( ! current_user_can( 'edit_shop_orders' ) ) {
1245
  die( - 1 );
1246
  }
1247
 
1248
- $order_id = absint( $_POST['order_id'] );
1249
 
1250
- if ( ! wp_get_post_parent_id( $order_id ) ) {
1251
- $rate_id = absint( $_POST['rate_id'] );
1252
- $parent_order = wc_get_order( $order_id );
1253
- $parent_taxes = $parent_order->get_taxes();
1254
- $suborder_ids = self::get_suborder( $order_id );
1255
- $parent_tax_to_remove = $parent_taxes[ $rate_id ];
1256
 
1257
- foreach( $suborder_ids as $suborder_id ){
1258
- $suborder = wc_get_order( $suborder_id );
1259
- $suborder_taxes = $suborder->get_taxes();
1260
- foreach( $suborder_taxes as $suborder_tax_key => $suborder_tax_item ){
1261
- $suborder_tax_item['rate_id'] == $parent_tax_to_remove['rate_id']
1262
  &&
1263
- $suborder_tax_item['name'] == $parent_tax_to_remove['name']
1264
  &&
1265
- $suborder_tax_item['label'] == $parent_tax_to_remove['label']
1266
  &&
1267
- wc_delete_order_item( $suborder_tax_key );
1268
  }
1269
  }
1270
- }
1271
-
1272
- else {
1273
  //is suborder
1274
  //TODO: Suborder sub-routine
1275
  }
@@ -1278,15 +1251,15 @@ if ( ! class_exists( 'YITH_Orders' ) ) {
1278
  /**
1279
  * Check for new order email
1280
  */
1281
- public function woocommerce_email_enabled_new_order( $enabled, $object ){
1282
- return $enabled && is_a( $object, 'WC_Order' ) && wp_get_post_parent_id( $object->id ) != 0 ? false: $enabled;
1283
  }
1284
 
1285
  /**
1286
  * Check for email recipient
1287
  */
1288
- public function woocommerce_email_recipient_new_order( $recipient, $object ){
1289
- return ( $recipient == get_option('recipient') || $recipient == get_option('admin_email') ) && $object instanceof WC_Order && wp_get_post_parent_id( $object->id ) ? false: $recipient;
1290
  }
1291
 
1292
  /**
@@ -1296,94 +1269,94 @@ if ( ! class_exists( 'YITH_Orders' ) ) {
1296
  * @use woocommerce_order_refunded action
1297
  * @see woocommerce\includes\class-wc-ajax.php:2295
1298
  */
1299
- public function order_refunded( $order_id, $parent_refund_id ) {
1300
-
1301
- if ( ! wp_get_post_parent_id( $order_id ) ) {
1302
- $create_refund = true;
1303
- $refund = false;
1304
- $parent_line_item_refund = 0;
1305
- $refund_amount = wc_format_decimal( sanitize_text_field( $_POST['refund_amount'] ) );
1306
- $refund_reason = sanitize_text_field($_POST['refund_reason']);
1307
- $line_item_qtys = json_decode(sanitize_text_field(stripslashes($_POST['line_item_qtys'])), true);
1308
- $line_item_totals = json_decode(sanitize_text_field(stripslashes($_POST['line_item_totals'])), true);
1309
- $line_item_tax_totals = json_decode(sanitize_text_field(stripslashes($_POST['line_item_tax_totals'])), true);
1310
- $api_refund = $_POST['api_refund'] === 'true' ? true : false;
1311
- $restock_refunded_items = $_POST['restock_refunded_items'] === 'true' ? true : false;
1312
- $order = wc_get_order( $order_id );
1313
- $parent_order_total = wc_format_decimal( $order->get_total() );
1314
- $suborder_ids = self::get_suborder( $order_id );
1315
 
1316
  //calculate line items total from parent order
1317
- foreach ($line_item_totals as $item_id => $total) {
1318
- $parent_line_item_refund += wc_format_decimal($total);
1319
  }
1320
 
1321
- foreach( $suborder_ids as $suborder_id ){
1322
- $suborder = wc_get_order( $suborder_id );
1323
- $suborder_items_ids = array_keys( $suborder->get_items() );
1324
- $suborder_total = wc_format_decimal( $suborder->get_total() );
1325
- $max_refund = wc_format_decimal($suborder_total - $suborder->get_total_refunded());
1326
  $child_line_item_refund = 0;
1327
 
1328
  // Prepare line items which we are refunding
1329
- $line_items = array();
1330
- $item_ids = array_unique(array_merge(array_keys($line_item_qtys, $line_item_totals)));
1331
 
1332
- foreach ($item_ids as $item_id) {
1333
- $child_item_id = self::get_child_item_id( $item_id );
1334
- if( $child_item_id && in_array( $child_item_id, $suborder_items_ids ) ){
1335
- $line_items[$child_item_id] = array('qty' => 0, 'refund_total' => 0, 'refund_tax' => array());
1336
  }
1337
  }
1338
 
1339
- foreach ($line_item_qtys as $item_id => $qty) {
1340
- $child_item_id = self::get_child_item_id( $item_id );
1341
- if( $child_item_id && in_array( $child_item_id, $suborder_items_ids ) ){
1342
- $line_items[$child_item_id]['qty'] = max($qty, 0);
1343
  }
1344
  }
1345
 
1346
- foreach ($line_item_totals as $item_id => $total) {
1347
- $child_item_id = self::get_child_item_id( $item_id );
1348
- if( $child_item_id && in_array( $child_item_id, $suborder_items_ids ) ){
1349
- $total = wc_format_decimal( $total );
1350
  $child_line_item_refund += $total;
1351
- $line_items[$child_item_id]['refund_total'] = $total;
1352
  }
1353
  }
1354
 
1355
- foreach ($line_item_tax_totals as $item_id => $tax_totals) {
1356
- $child_item_id = self::get_child_item_id( $item_id );
1357
- if( $child_item_id && in_array( $child_item_id, $suborder_items_ids ) ){
1358
- $line_items[$child_item_id]['refund_tax'] = array_map('wc_format_decimal', $tax_totals);
1359
  }
1360
  }
1361
 
1362
  //calculate refund amount percentage
1363
  $suborder_refund_amount = ( ( ( $refund_amount - $parent_line_item_refund ) * $suborder_total ) / $parent_order_total );
1364
- $suborder_total_refund = wc_format_decimal($child_line_item_refund + $suborder_refund_amount) ;
1365
 
1366
- if ( ! $refund_amount || $max_refund < $suborder_total_refund || 0 > $suborder_total_refund) {
1367
  /**
1368
  * Invalid refund amount.
1369
  * Check if suborder total != 0 create a partial refund, exit otherwise
1370
  */
1371
- $surplus = wc_format_decimal( $suborder_total_refund - $max_refund );
1372
- $suborder_total_refund = $suborder_total_refund - $surplus;
1373
- $create_refund = $suborder_total_refund > 0 ? true : false;
1374
  }
1375
 
1376
- if( $create_refund ) {
1377
  // Create the refund object
1378
- $refund = wc_create_refund(array(
1379
  'amount' => $suborder_total_refund,
1380
  'reason' => $refund_reason,
1381
  'order_id' => $suborder->id,
1382
- 'line_items' => $line_items
1383
  )
1384
  );
1385
 
1386
- add_post_meta( $refund->id, '_parent_refund_id', $parent_refund_id );
1387
  }
1388
  }
1389
  }
@@ -1396,22 +1369,22 @@ if ( ! class_exists( 'YITH_Orders' ) ) {
1396
  * @use woocommerce_refund_deleted action
1397
  * @see woocommerce\includes\class-wc-ajax.php:2328
1398
  */
1399
- public static function refund_deleted( $refund_id, $parent_order_id ) {
1400
- check_ajax_referer( 'order-item', 'security' );
1401
 
1402
- if ( ! current_user_can( 'edit_shop_orders' ) ) {
1403
- die(-1);
1404
  }
1405
 
1406
- if ( ! wp_get_post_parent_id( $parent_order_id ) ) {
1407
  global $wpdb;
1408
- $child_refund_ids = $wpdb->get_col( $wpdb->prepare( "SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key=%s AND meta_value=%s", '_parent_refund_id', $refund_id ) );
1409
 
1410
- foreach( $child_refund_ids as $child_refund_id ){
1411
- if ( $child_refund_id && 'shop_order_refund' === get_post_type( $child_refund_id ) ) {
1412
- $order_id = wp_get_post_parent_id( $child_refund_id );
1413
- wc_delete_shop_order_transients( $order_id );
1414
- wp_delete_post( $child_refund_id );
1415
  }
1416
  }
1417
  }
@@ -1426,20 +1399,21 @@ if ( ! class_exists( 'YITH_Orders' ) ) {
1426
  *
1427
  * @return string The label value
1428
  */
1429
- public function commissions_attribute_label( $attribute_label, $meta_key, $product = false ) {
1430
  global $pagenow;
1431
 
1432
- if ( $product && 'post.php' == $pagenow && isset( $_GET['post'] ) && $order = wc_get_order( $_GET['post'] ) ) {
1433
- $line_items = $order->get_items( 'line_item' );
1434
- $item_meta_key = wp_get_post_parent_id( $order->id ) ? '_commission_id' : '_child__commission_id';
1435
  foreach ( $line_items as $line_item_id => $line_item ) {
1436
- if ( $line_item['product_id'] == $product->id ) {
1437
- $commission_id = wc_get_order_item_meta( $line_item_id, $item_meta_key, true );
1438
- $admin_url = YITH_Commission( $commission_id )->get_view_url( 'admin' );
1439
- $attribute_label = $item_meta_key == $meta_key ? sprintf( "<a href='%s' class='%s'>" . __( 'commission_id', 'yith_wc_product_vendors' ) . '</a>', $admin_url, 'commission-id-label' ) : $attribute_label;
1440
  }
1441
  }
1442
  }
 
1443
  return $attribute_label;
1444
  }
1445
 
@@ -1447,22 +1421,26 @@ if ( ! class_exists( 'YITH_Orders' ) ) {
1447
  * Filter the edit order page for vendors
1448
  *
1449
  * @author Andrea Grillo <andrea.grillo@yithemes.com>
1450
- * @since 1.6
 
1451
  * @param $check
 
1452
  * @return bool
1453
  */
1454
- public function vendor_single_order_page( $check ){
1455
  global $theorder;
1456
- $vendor = yith_get_vendor( 'current', 'user' );
1457
- $is_ajax = defined( 'DOING_AJAX' ) && DOING_AJAX;
1458
- $is_order_details = is_admin() && ! $is_ajax && 'shop_order' == get_current_screen()->id;
1459
 
1460
- if( ( $vendor->is_valid() || ( $vendor->is_super_user() && is_object( $theorder ) && wp_get_post_parent_id( $theorder->id ) ) ) && $is_order_details && 'wc_order_is_editable' == current_filter() ){
1461
  $check = false;
1462
  }
 
1463
  return $check;
1464
  }
1465
- /**
 
1466
  * Filters meta to hide, to add to the list item order meta added by author class
1467
  *
1468
  * @param $to_hidden Array of order_item_meta meta_key to hide
@@ -1471,12 +1449,12 @@ if ( ! class_exists( 'YITH_Orders' ) ) {
1471
  * @since 1.0
1472
  * @author Andrea Grillo <andrea.grillo@yithemes.com>
1473
  */
1474
- public function hidden_order_itemmeta( $to_hidden ) {
1475
- if( ! defined( 'WP_DEBUG' ) || ( defined( 'WP_DEBUG' ) && ! WP_DEBUG ) ){
1476
  $to_hidden[] = '_parent_line_item_id';
1477
  }
1478
 
1479
- if ( ! apply_filters( 'yith_show_commissions_order_item_meta', YITH_Commissions()->show_order_item_meta ) ) {
1480
  $to_hidden[] = '_commission_id';
1481
  $to_hidden[] = '_parent__commission_id';
1482
  }
@@ -1491,13 +1469,22 @@ if ( ! class_exists( 'YITH_Orders' ) ) {
1491
  *
1492
  * @return string The label value
1493
  */
1494
- public function shop_order_columns( $order_columns ) {
1495
- $vendor = yith_get_vendor( 'current', 'user' );
1496
- if ( $vendor->is_super_user() ) {
1497
- $suborder = array( 'suborder' => _x( 'Suborders', 'Admin: Order table column', 'yith_wc_product_vendors' ) );
1498
- $ref_pos = array_search( 'order_title', array_keys( $order_columns ) );
1499
- $order_columns = array_slice( $order_columns, 0, $ref_pos + 1, true ) + $suborder + array_slice( $order_columns, $ref_pos + 1, count( $order_columns ) - 1, true );
 
 
 
 
 
 
 
 
1500
  }
 
1501
  return $order_columns;
1502
  }
1503
 
@@ -1506,72 +1493,79 @@ if ( ! class_exists( 'YITH_Orders' ) ) {
1506
  *
1507
  * @param string $column
1508
  */
1509
- public function render_shop_order_columns( $column ) {
1510
  global $post, $the_order;
1511
 
1512
  if ( empty( $the_order ) || $the_order->id != $post->ID ) {
1513
- $_the_order = wc_get_order( $post->ID );
1514
- }
1515
-
1516
- else {
1517
  $_the_order = $the_order;
1518
  }
1519
 
1520
  switch ( $column ) {
1521
  case 'suborder' :
1522
- $suborder_ids = self::get_suborder( $_the_order->id );
1523
 
1524
  if ( $suborder_ids ) {
1525
  foreach ( $suborder_ids as $suborder_id ) {
1526
- $suborder = wc_get_order( $suborder_id );
1527
- $vendor = yith_get_vendor( $suborder->post->post_author, 'user' );
1528
- $order_uri = esc_url( 'post.php?post=' . absint( $suborder_id ) . '&action=edit' );
1529
-
1530
- printf( '<mark class="%s tips" data-tip="%s">%s</mark> <strong><a href="%s">#%s</a></strong> <small class="yith-wcmv-suborder-owner">(%s %s)</small><br/>',
1531
- sanitize_title( $suborder->get_status() ),
1532
- wc_get_order_status_name( $suborder->get_status() ),
1533
- wc_get_order_status_name( $suborder->get_status() ),
1534
  $order_uri,
1535
  $suborder_id,
1536
- __( 'in', 'Order table details', 'yith_wc_product_vendors' ),
1537
  $vendor->name
1538
  );
 
 
1539
  }
 
 
 
 
 
 
 
 
 
 
1540
  }
1541
 
1542
  else {
1543
  echo '<span class="na">&ndash;</span>';
1544
  }
1545
-
1546
  break;
1547
  }
1548
  }
1549
 
1550
- /**
1551
  * Add suborder metaboxes for Vendors order
1552
  *
1553
  * @return void
1554
- * @author Andrea Grillo <andrea.grillo@yithemes.com>
1555
  */
1556
- public function add_meta_boxes() {
1557
- if( 'shop_order' != get_current_screen()->id ){
1558
  return;
1559
  }
1560
 
1561
  global $post;
1562
- $vendor = yith_get_vendor( 'current', 'user' );
1563
- $has_suborder = self::get_suborder( absint( $post->ID ) );
1564
- $is_suborder = wp_get_post_parent_id( absint( $post->ID ) );
1565
-
1566
- if( $vendor->is_super_user() ){
1567
- if( $has_suborder ){
1568
- $metabox_suborder_description = _x( 'Suborders', 'Admin: Single order page. Suborder details box', 'yith_wc_product_vendors' ) . ' <span class="tips" data-tip="' . esc_attr__( 'Note: from this box you can monitor the status of suborders associated to individual vendors.', 'woocommerce' ) . '">[?]</span>';
1569
- add_meta_box( 'woocommerce-suborders', $metabox_suborder_description, array( $this, 'output' ), 'shop_order', 'side', 'core', array( 'metabox' => 'suborders' ) );
1570
- }
1571
-
1572
- else if( $is_suborder ){
1573
- $metabox_parent_order_description = _x( 'Parent order', 'Admin: Single order page. Parent order details box', 'yith_wc_product_vendors' );
1574
- add_meta_box( 'woocommerce-parent-order', $metabox_parent_order_description, array( $this, 'output' ), 'shop_order', 'side', 'high', array( 'metabox' => 'parent-order' ) );
1575
  }
1576
  }
1577
  }
@@ -1585,23 +1579,23 @@ if ( ! class_exists( 'YITH_Orders' ) ) {
1585
  * @return void
1586
  * @author Andrea Grillo <andrea.grillo@yithemes.com>
1587
  */
1588
- public function output( $post, $param ) {
1589
- switch ( $param['args']['metabox'] ) {
1590
  case 'suborders':
1591
- $suborder_ids = self::get_suborder( absint( $post->ID ) );
1592
  echo '<ul class="suborders-list single-orders">';
1593
  foreach ( $suborder_ids as $suborder_id ) {
1594
- $suborder = wc_get_order( absint( $suborder_id ) );
1595
- $vendor = yith_get_vendor( $suborder->post->post_author, 'user' );
1596
- $suborder_uri = esc_url( 'post.php?post=' . absint( $suborder_id ) . '&action=edit' );
1597
  echo '<li class="suborder-info">';
1598
- printf( '<mark class="%s tips" data-tip="%s">%s</mark> <strong><a href="%s">#%s</a></strong> <small class="single-order yith-wcmv-suborder-owner">%s %s</small><br/>',
1599
- sanitize_title( $suborder->get_status() ),
1600
- wc_get_order_status_name( $suborder->get_status() ),
1601
- wc_get_order_status_name( $suborder->get_status() ),
1602
  $suborder_uri,
1603
  $suborder_id,
1604
- __( 'in', 'Order table details', 'yith_wc_product_vendors' ),
1605
  $vendor->name
1606
  );
1607
  echo '<li>';
@@ -1610,11 +1604,73 @@ if ( ! class_exists( 'YITH_Orders' ) ) {
1610
  break;
1611
 
1612
  case 'parent-order':
1613
- $parent_order_id = wp_get_post_parent_id( absint( $post->ID ) );
1614
- $parent_order_uri = esc_url( 'post.php?post=' . absint( $parent_order_id ) . '&action=edit' );
1615
- printf( '<a href="%s">&#8592; %s</a>', $parent_order_uri, _x( 'Return to main order', 'Admin: single order page. Link to parent order', 'yith_wc_product_vendors' ) );
1616
  break;
1617
  }
1618
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1619
  }
1620
  }
7
  * It is also available through the world-wide-web at this URL:
8
  * http://www.gnu.org/licenses/gpl-3.0.txt
9
  */
10
+ if ( ! defined ( 'ABSPATH' ) ) {
11
  exit( 'Direct access forbidden.' );
12
  }
13
 
20
  * @author Your Inspiration Themes
21
  *
22
  */
23
+ if ( ! class_exists ( 'YITH_Orders' ) ) {
24
 
25
  class YITH_Orders {
26
 
35
  /**
36
  * Constructor
37
  */
38
+ public function __construct () {
39
+ add_action ( 'woocommerce_checkout_update_order_meta', array ( $this, 'check_suborder' ), 10, 2 );
40
 
41
  /* Prevent Multiple Email Notifications for Suborders */
42
+ add_filter ( 'woocommerce_email_recipient_new_order', array ( $this, 'woocommerce_email_recipient_new_order' ), 10, 2 );
43
+ add_filter ( 'woocommerce_email_recipient_cancelled_order', array ( $this, 'woocommerce_email_recipient_new_order' ), 10, 2 );
44
+ add_filter ( 'woocommerce_email_enabled_customer_processing_order', array ( $this, 'woocommerce_email_enabled_new_order' ), 10, 2 );
45
+ add_filter ( 'woocommerce_email_enabled_customer_completed_order', array ( $this, 'woocommerce_email_enabled_new_order' ), 10, 2 );
46
+ add_filter ( 'woocommerce_email_enabled_customer_partially_refunded_order', array ( $this, 'woocommerce_email_enabled_new_order' ), 10, 2 );
47
+ add_filter ( 'woocommerce_email_enabled_customer_refunded_order', array ( $this, 'woocommerce_email_enabled_new_order' ), 10, 2 );
48
 
49
+ add_action ( 'woocommerce_order_refunded', array ( $this, 'order_refunded' ), 10, 2 );
50
+ add_action ( 'woocommerce_refund_deleted', array ( $this, 'refund_deleted' ), 10, 2 );
51
 
52
+ add_filter ( 'woocommerce_attribute_label', array ( $this, 'commissions_attribute_label' ), 10, 3 );
53
 
54
  /* Single Order Page for Vendor */
55
+ add_filter ( 'wc_order_is_editable', array ( $this, 'vendor_single_order_page' ) );
56
 
57
+ add_filter ( 'woocommerce_attribute_label', array ( $this, 'commissions_attribute_label' ), 10, 3 );
58
 
59
  /* Single Order Page for Vendor */
60
+ add_filter ( 'wc_order_is_editable', array ( $this, 'vendor_single_order_page' ) );
61
 
62
+ /* Order Item Meta */
63
+ add_action ( 'woocommerce_hidden_order_itemmeta', array ( $this, 'hidden_order_itemmeta' ) );
64
 
65
  /* Order Table */
66
+ add_filter ( 'manage_shop_order_posts_columns', array ( $this, 'shop_order_columns' ) );
67
+ add_action ( 'manage_shop_order_posts_custom_column', array ( $this, 'render_shop_order_columns' ) );
68
 
69
  /* Order MetaBoxes */
70
+ add_action ( 'add_meta_boxes', array ( $this, 'add_meta_boxes' ), 30 );
71
 
72
+ $sync_enabled = get_option ( 'yith_wpv_vendors_option_order_synchronization', 'yes' );
73
 
74
+ if ( $sync_enabled ) {
75
  /* SubOrder Sync */
76
+ add_action ( 'woocommerce_order_status_changed', array ( $this, 'suborder_status_synchronization' ), 10, 3 );
77
  /* Order Meta Synchronization */
78
+ add_action ( 'woocommerce_process_shop_order_meta', array ( $this, 'suborder_meta_synchronization' ), 65, 2 );
79
  /* Commission Synchronization */
80
+ add_action ( 'yith_wcmv_after_single_register_commission', array ( $this, 'register_commission_to_parent_order' ), 10, 4 );
81
 
82
  /**
83
  * Other Ajax Action:
85
  * load_order_items
86
  * woocommerce_EVENT => nopriv
87
  */
88
+ $ajax_events = array (
89
+ 'add_order_item' => false,
90
  /*'add_order_fee' => false,*/
91
  /*'add_order_shipping' => false,*/
92
+ 'add_order_tax' => false,
93
+ 'remove_order_item' => false,
94
+ 'remove_order_tax' => false,
95
+ 'reduce_order_item_stock' => false,
96
+ 'increase_order_item_stock' => false,
97
  /*'add_order_item_meta' => false, */
98
+ 'remove_order_item_meta' => false,
99
+ 'calc_line_taxes' => false,
100
+ 'save_order_items' => false,
101
+ 'add_order_note' => false,
102
+ 'delete_order_note' => false,
103
  );
104
 
105
  foreach ( $ajax_events as $ajax_event => $nopriv ) {
106
+ add_action ( "wp_ajax_woocommerce_{$ajax_event}", array ( __CLASS__, $ajax_event ), 5 );
107
+ $nopriv && add_action ( "wp_ajax_nopriv_woocommerce_{$ajax_event}", array ( __CLASS__, $ajax_event, 5 ) );
108
  }
109
  }
110
  }
119
  * @since 1.6
120
  * @return void
121
  */
122
+ public function check_suborder ( $parent_order_id, $posted ) {
123
+ $parent_order = wc_get_order ( $parent_order_id );
124
+ $items = $parent_order->get_items ();
125
+ $products_by_vendor = array ();
126
+ $suborder_ids = array ();
127
 
128
  //check for vendor product
129
  foreach ( $items as $item ) {
130
+ $vendor = yith_get_vendor ( $item[ 'product_id' ], 'product' );
131
+ if ( $vendor->is_valid () ) {
132
+ $products_by_vendor[ $vendor->id ][] = $item;
133
  }
134
  }
135
 
136
+ $vendor_count = count ( $products_by_vendor );
137
 
138
  //Vendor's items ? NO
139
  if ( $vendor_count == 0 ) {
140
  return false;
141
+ } //Vendor's items ? YES
 
 
142
  else {
143
  //add sub-order to parent
144
+ update_post_meta ( $parent_order_id, 'has_sub_order', true );
145
 
146
  foreach ( $products_by_vendor as $vendor_id => $vendor_products ) {
147
  //create sub-orders
148
+ $suborder_ids[] = $this->create_suborder ( $parent_order, $vendor_id, $vendor_products, $posted );
149
  }
150
 
151
+ if ( ! empty( $suborder_ids ) ) {
152
+ foreach ( $suborder_ids as $suborder_id ) {
153
+ do_action ( 'yith_wcmv_checkout_order_processed', $suborder_id );
154
  }
155
  }
156
  }
169
  * @since 1.6
170
  * @return void
171
  */
172
+ public function create_suborder ( $parent_order, $vendor_id, $vendor_products, $posted ) {
173
  /** @var $parent_order WC_Order */
174
+ $vendor = yith_get_vendor ( $vendor_id, 'vendor' );
175
+ $order_data = apply_filters ( 'woocommerce_new_order_data', array (
176
  'post_type' => 'shop_order',
177
+ 'post_title' => sprintf ( __ ( 'Order &ndash; %s', 'woocommerce' ), strftime ( _x ( '%b %d, %Y @ %I:%M %p', 'Order date parsed by strftime', 'woocommerce' ) ) ),
178
+ 'post_status' => 'wc-' . apply_filters ( 'woocommerce_default_order_status', 'pending' ),
179
  'ping_status' => 'closed',
180
+ 'post_excerpt' => isset( $posted[ 'order_comments' ] ) ? $posted[ 'order_comments' ] : '',
181
+ 'post_author' => $vendor->get_owner (),
182
  'post_parent' => $parent_order->id,
183
+ 'post_password' => uniqid ( 'order_' ) // Protects the post just in case
184
  )
185
  );
186
 
187
+ $suborder_id = wp_insert_post ( $order_data );
188
+ $suborder = wc_get_order ( $suborder_id );
189
+ $parent_line_items = $parent_order->get_items ( 'line_item' );
190
 
191
+ if ( ! empty( $suborder_id ) && ! is_wp_error ( $suborder_id ) ) {
192
  $order_total = $order_tax = $discount = 0;
193
+ $product_ids = array ();
194
 
195
  // now insert line items
196
  foreach ( $vendor_products as $item ) {
197
+ $order_total += (float)$item[ 'line_total' ];
198
+ $order_tax += (float)$item[ 'line_tax' ];
199
+ $product_ids[] = $item[ 'product_id' ];
200
 
201
+ $item_id = wc_add_order_item ( $suborder_id, array (
202
+ 'order_item_name' => $item[ 'name' ],
203
+ 'order_item_type' => 'line_item',
204
  )
205
  );
206
 
207
  if ( $item_id ) {
208
+ $metakeys = array (
209
  'qty',
210
  'tax_class',
211
  'product_id',
214
  'line_total',
215
  'line_tax',
216
  'line_subtotal_tax',
217
+ 'line_tax_data',
218
  );
219
 
220
  foreach ( $metakeys as $key ) {
227
  * @see woocommerce/includes/wc-order-functions.php:449
228
  * @use wp-includes/meta.php:31
229
  */
230
+ wc_add_order_item_meta ( $item_id, "_{$key}", maybe_unserialize ( $item[ $key ] ) );
231
  if ( 'product_id' == $key ) {
232
  foreach ( $parent_line_items as $line_item_id => $line_item_value ) {
233
  //@internal $key == 'product_id'
234
+ if ( $item[ $key ] == $line_item_value[ $key ] ) {
235
  // add line item to retrieve simply the parent line_item_id
236
+ wc_add_order_item_meta ( $item_id, '_parent_line_item_id', $line_item_id );
237
  break;
238
  }
239
  }
242
  }
243
 
244
  //Calculate Discount
245
+ $discount += ( $item[ 'line_subtotal' ] - $item[ 'line_total' ] );
246
  }
247
 
248
  //Billing order meta
249
+ $billing_order_meta = array (
250
  'billing_country',
251
  'billing_first_name',
252
  'billing_last_name',
257
  'billing_state',
258
  'billing_postcode',
259
  'billing_email',
260
+ 'billing_phone',
261
  );
262
 
263
  foreach ( $billing_order_meta as $order_meta_key ) {
264
  $meta_key = '_' . $order_meta_key;
265
+ update_post_meta ( $suborder_id, $meta_key, $parent_order->$order_meta_key );
266
  }
267
 
268
  //Shipping order meta
269
+ $shipping_order_meta = array (
270
  'shipping_country',
271
  'shipping_first_name',
272
  'shipping_last_name',
275
  'shipping_address_2',
276
  'shipping_city',
277
  'shipping_state',
278
+ 'shipping_postcode',
279
  );
280
 
281
  foreach ( $shipping_order_meta as $order_meta_key ) {
282
  $meta_key = '_' . $order_meta_key;
283
+ update_post_meta ( $suborder_id, $meta_key, $parent_order->$order_meta_key );
284
  }
285
 
286
  //Shipping
288
  $shipping_cost = 0;
289
 
290
  //Coupons
291
+ $order_coupons = $parent_order->get_used_coupons ();
292
  if ( ! empty( $order_coupons ) ) {
293
  foreach ( $order_coupons as $order_coupon ) {
294
  $coupon = new WC_Coupon( $order_coupon );
295
 
296
+ if ( $coupon && is_array( $coupon->product_ids ) && in_array ( $product_ids, $coupon->product_ids ) ) {
297
+ $order_item_id = wc_add_order_item ( $suborder_id, array (
298
  'order_item_name' => $order_coupon,
299
+ 'order_item_type' => 'coupon',
300
  )
301
  );
302
 
303
  // Add line item meta
304
  if ( $order_item_id ) {
305
+ $order_item_value = isset( WC ()->cart->coupon_discount_amounts[ $order_coupon ] ) ? WC ()->cart->coupon_discount_amounts[ $order_coupon ] : 0;
306
  $meta_key = 'discount_amount';
307
+ wc_add_order_item_meta ( $order_item_id, $meta_key, $order_item_value );
308
  }
309
  }
310
  }
313
  //Calculate Total
314
  $order_in_total = $order_total + $shipping_cost + $order_tax;
315
 
316
+ $totals = array (
317
+ 'shipping' => wc_format_decimal ( $shipping_cost ),
318
+ 'cart_discount' => wc_format_decimal ( $discount ),
319
  'cart_discount_tax' => 0,
320
+ 'tax' => wc_format_decimal ( $order_tax ),
321
  'order_shipping_tax' => 0,
322
+ 'total' => wc_format_decimal ( $order_in_total ),
323
  );
324
 
325
  //Set tax. N.B.: needs total to works
326
+ if ( function_exists ( 'WC' ) && is_a ( WC ()->cart, 'WC_Cart' ) ) {
327
  /** @var WC_Cart $cart */
328
+ $_cart = WC ()->cart;
329
+ foreach ( array_keys ( $_cart->taxes + $_cart->shipping_taxes ) as $tax_rate_id ) {
330
+ if ( $_cart && $tax_rate_id && apply_filters ( 'woocommerce_cart_remove_taxes_zero_rate_id', 'zero-rated' ) !== $tax_rate_id ) {
331
+ $suborder->add_tax ( $tax_rate_id, $totals[ 'tax' ], $totals[ 'order_shipping_tax' ] );
332
  }
333
  }
334
  }
335
 
336
  //Set totals
337
  foreach ( $totals as $meta_key => $meta_value ) {
338
+ $suborder->set_total ( $meta_value, $meta_key );
339
  }
340
 
341
  //Set other order meta
342
+ $order_meta = array (
343
  '_payment_method' => $parent_order->payment_method,
344
  '_payment_method_title' => $parent_order->payment_method_title,
345
+ '_order_key' => apply_filters ( 'woocommerce_generate_order_key', uniqid ( 'order_' ) ),
346
  '_customer_user' => $parent_order->customer_user,
347
  '_prices_include_tax' => $parent_order->prices_include_tax,
348
+ '_order_currency' => get_post_meta ( $parent_order->id, '_order_currency', true ),
349
+ '_customer_ip_address' => get_post_meta ( $parent_order->id, '_customer_ip_address', true ),
350
+ '_customer_user_agent' => get_post_meta ( $parent_order->id, '_customer_user_agent', true ),
351
  );
352
 
353
  foreach ( $order_meta as $meta_key => $meta_value ) {
354
+ update_post_meta ( $suborder_id, $meta_key, $meta_value );
355
  }
356
  }
357
 
372
  * @since 1.6
373
  * @return void
374
  */
375
+ public function suborder_status_synchronization ( $parent_order_id, $old_status, $new_status ) {
376
  //Check if order have sub-order
377
+ if ( wp_get_post_parent_id ( $parent_order_id ) ) {
378
  return false;
379
  }
380
 
381
+ $suborder_ids = self::get_suborder ( $parent_order_id );
382
  if ( ! empty( $suborder_ids ) ) {
383
  foreach ( $suborder_ids as $suborder_id ) {
384
  /** @var $suborder WC_Order */
385
+ $suborder = wc_get_order ( $suborder_id );
386
+ $suborder->update_status ( $new_status, _x ( 'Update by admin: ', 'Order note', 'yith_wc_product_vendors' ) );
387
  }
388
  }
389
  }
401
  * @since 1.6
402
  * @return void
403
  */
404
+ public function suborder_meta_synchronization ( $parent_order_id, $parent_order ) {
405
  //Check if order have sub-order
406
+ if ( wp_get_post_parent_id ( $parent_order_id ) ) {
407
  return false;
408
  }
409
 
410
  /** @var $suborder WC_Order */
411
  /** @var $parent_order WC_Order */
412
+ $suborder_ids = self::get_suborder ( $parent_order_id );
413
+ $parent_order = wc_get_order ( $parent_order_id );
414
  if ( ! empty( $suborder_ids ) ) {
415
  foreach ( $suborder_ids as $suborder_id ) {
416
+ $suborder = wc_get_order ( $suborder_id );
417
+ $child_items = array_keys ( $suborder->get_items () );
418
+ $_post = $_POST;
419
+ $_post[ 'order_item_id' ] = $child_items;
420
+ $suborder_line_total = 0;
421
 
422
  foreach ( $child_items as $child_items_id ) {
423
+ $parent_item_id = $suborder->get_item_meta ( $child_items_id, '_parent_line_item_id' );
424
+ $parent_item_id = absint ( array_shift ( $parent_item_id ) );
425
  foreach ( $_post as $meta_key => $meta_value ) {
426
  //TODO: Shipping Cost
427
  switch ( $meta_key ) {
431
  case 'order_item_qty':
432
  case 'refund_line_total':
433
  case 'refund_order_item_qty':
434
+ if ( isset( $_post[ $meta_key ][ $parent_item_id ] ) ) {
435
+ $_post[ $meta_key ][ $child_items_id ] = $_post[ $meta_key ][ $parent_item_id ];
436
+ unset( $_post[ $meta_key ][ $parent_item_id ] );
437
  }
438
  break;
439
 
440
  case 'shipping_cost':
441
+ if ( isset( $_post[ $meta_key ][ $parent_item_id ] ) ) {
442
+ $_post[ $meta_key ][ $child_items_id ] = 0;
443
+ unset( $_post[ $meta_key ][ $parent_item_id ] );
444
  }
445
  break;
446
  default: //nothing to do
449
  }
450
 
451
  //Calculate Order Total
452
+ if ( isset( $_post[ 'line_total' ][ $child_items_id ] ) ) {
453
+ $suborder_line_total += wc_format_decimal ( $_post[ 'line_total' ][ $child_items_id ] );
454
  }
455
  }
456
 
457
  //New Order Total
458
+ $_post[ '_order_total' ] = wc_format_decimal ( $suborder_line_total );
459
 
460
  /**
461
  * Don't use save method by WC_Meta_Box_Order_Items class because I need to filter the POST information
462
  * use wc_save_order_items( $order_id, $items ) function directly.
463
+ *
464
  * @see WC_Meta_Box_Order_Items::save( $suborder_id, $suborder ); in woocommerce\includes\admin\meta-boxes\class-wc-meta-box-order-items.php:45
465
  * @see wc_save_order_items( $order_id, $items ); in woocommerce\includes\admin\wc-admin-functions.php:176
466
  */
467
+ wc_save_order_items ( $suborder_id, $_post );
468
+ WC_Meta_Box_Order_Downloads::save ( $suborder_id, $suborder );
469
+ WC_Meta_Box_Order_Data::save ( $suborder_id, $suborder );
470
+ WC_Meta_Box_Order_Actions::save ( $suborder_id, $suborder );
471
  }
472
  }
473
  }
480
  *
481
  * @author Andrea Grillo <andrea.grillo@yithemes.com>
482
  * @since 1.6
483
+ * @return array
484
  */
485
+ public static function get_suborder ( $parent_order_id = false ) {
486
+ $suborder_ids = array ();
487
  if ( $parent_order_id ) {
488
  global $wpdb;
489
+ $suborder_ids = $wpdb->get_col ( $wpdb->prepare ( "SELECT ID FROM {$wpdb->posts} WHERE post_parent=%d AND post_type=%s", absint ( $parent_order_id ), 'shop_order' ) );
490
  }
491
 
492
  return $suborder_ids;
503
  * @since 1.6
504
  * @return int|bool The parent item id if exist, false otherwise
505
  */
506
+ public static function get_parent_item_id ( $suborder = false, $child_item_id ) {
507
+ global $wpdb;
508
  $parent_item_id = false;
509
 
510
+ if ( ! $suborder ) {
511
+ $parent_item_id = $wpdb->get_var ( $wpdb->prepare ( "SELECT DISTINCT order_item_id FROM {$wpdb->order_itemmeta} WHERE meta_id=%d", $child_item_id ) );
512
  $parent_item_id = ! empty( $parent_item_id ) ? $parent_item_id : false;
513
+ } else {
514
+ $parent_item_id = $suborder->get_item_meta ( $child_item_id, '_parent_line_item_id' );
515
+ $parent_item_id = ! empty( $parent_item_id ) ? absint ( array_shift ( $parent_item_id ) ) : false;
 
 
516
  }
517
 
518
 
528
  * @since 1.6
529
  * @return int|bool The parent item id if exist, false otherwise
530
  */
531
+ public static function get_child_item_id ( $parent_item_id ) {
532
  global $wpdb;
533
+ $child_item_id = $wpdb->get_var ( $wpdb->prepare ( "SELECT order_item_id FROM {$wpdb->order_itemmeta} WHERE meta_key=%s AND meta_value=%d", '_parent_line_item_id', absint ( $parent_item_id ) ) );
534
+
535
  return $child_item_id;
536
  }
537
 
544
  * @since 1.6
545
  * @return int|bool The child item id if exist, false otherwise
546
  */
547
+ public static function get_line_item_id_from_parent ( $order_item_id ) {
548
  global $wpdb;
549
+
550
+ return $wpdb->get_var ( $wpdb->prepare ( "SELECT DISTINCT order_item_id FROM {$wpdb->order_itemmeta} WHERE meta_key=%s AND meta_value=%d", '_parent_line_item_id', $order_item_id ) );
551
  }
552
 
553
  /**
556
  * @author Andrea Grillo <andrea.grillo@yithemes.com>
557
  * @since 1.6
558
  * @return void
559
+ * @access public static
560
  */
561
+ public static function save_order_items () {
562
+ check_ajax_referer ( 'order-item', 'security' );
563
 
564
+ if ( ! current_user_can ( 'edit_shop_orders' ) ) {
565
  die( - 1 );
566
  }
567
 
568
+ if ( isset( $_POST[ 'order_id' ] ) && isset( $_POST[ 'items' ] ) ) {
569
+ $parent_order_id = absint ( $_POST[ 'order_id' ] );
570
  //Check if order have sub-order
571
+ if ( ! wp_get_post_parent_id ( $parent_order_id ) ) {
572
  global $wpdb;
573
  // Parse the jQuery serialized items
574
  $_post = $_POST;
575
+ parse_str ( $_post[ 'items' ], $_post[ 'items' ] );
576
+ $suborder_ids = self::get_suborder ( $parent_order_id );
577
  foreach ( $suborder_ids as $suborder_id ) {
578
+ $order_total = 0;
579
+ $suborder = wc_get_order ( $suborder_id );
580
+ $child_items = array_keys ( $suborder->get_items () );
581
+ $_post[ 'items' ][ 'order_item_id' ] = $child_items;
582
  foreach ( $child_items as $child_item_id ) {
583
+ $parent_item_id = self::get_parent_item_id ( $suborder, $child_item_id );
584
+ foreach ( $_post[ 'items' ] as $meta_key => $meta_value ) {
585
+ if ( ! in_array ( $meta_key, array ( 'order_item_id', '_order_total' ) ) && isset( $_post[ 'items' ][ $meta_key ][ $parent_item_id ] ) ) {
586
+ $_post[ 'items' ][ $meta_key ][ $child_item_id ] = $_post[ 'items' ][ $meta_key ][ $parent_item_id ];
587
+ unset( $_post[ 'items' ][ $meta_key ][ $parent_item_id ] );
588
  }
589
  }
590
 
591
  /* === Calc Order Totals === */
592
+ if ( ! empty( $_post[ 'items' ][ 'line_total' ][ $child_item_id ] ) ) {
593
+ $order_total += wc_format_decimal ( $_post[ 'items' ][ 'line_total' ][ $child_item_id ] );
594
+ if ( isset( $_post[ 'items' ][ 'line_tax' ][ $child_item_id ] ) ) {
595
+ $line_taxes = $_post[ 'items' ][ 'line_tax' ][ $child_item_id ];
596
+ foreach ( $line_taxes as $line_tax ) {
597
+ $order_total += wc_format_decimal ( $line_tax );
598
  }
599
  }
600
  }
601
 
602
  /* === Calc Refund Totals === */
603
+ if ( ! empty( $_post[ 'items' ][ 'refund_line_total' ][ $child_item_id ] ) ) {
604
+ $order_total += wc_format_decimal ( $_post[ 'items' ][ 'refund_line_total' ][ $child_item_id ] );
605
  }
606
  /* ======================== */
607
  }
608
 
609
  /* === Save Parent Meta === */
610
+ $meta_keys = isset( $_post[ 'items' ][ 'meta_key' ] ) ? $_post[ 'items' ][ 'meta_key' ] : array ();
611
+ $meta_values = isset( $_post[ 'items' ][ 'meta_value' ] ) ? $_post[ 'items' ][ 'meta_value' ] : array ();
612
  foreach ( $meta_keys as $meta_id => $meta_key ) {
613
+ $meta_value = ( empty( $meta_values[ $meta_id ] ) && ! is_numeric ( $meta_values[ $meta_id ] ) ) ? '' : $meta_values[ $meta_id ];
614
+ $parent_order_item_id = self::get_parent_item_id ( false, $meta_id );
615
+ $child_order_item_id = self::get_child_item_id ( $parent_order_item_id );
616
+ '_child__commission_id' != $meta_key && wc_update_order_item_meta ( $child_order_item_id, '_parent_' . $meta_key, '_commission_id' != $meta_key ? $meta_id : $meta_values[ $meta_id ] );
617
  }
618
  /* ======================== */
619
 
620
  // Add order total
621
+ $_post[ 'items' ][ '_order_total' ] = $order_total;
622
 
623
  // Save order items
624
+ wc_save_order_items ( $suborder_id, $_post[ 'items' ] );
625
  }
626
+ } else {
 
 
627
  //is suborder
628
  //TODO: Suborder sub-routine
629
  }
636
  * @author Andrea Grillo <andrea.grillo@yithemes.com>
637
  * @since 1.6
638
  * @return void
639
+ * @access public static
640
  */
641
+ public static function remove_order_item () {
642
+ check_ajax_referer ( 'order-item', 'security' );
643
 
644
+ if ( ! current_user_can ( 'edit_shop_orders' ) ) {
645
  die( - 1 );
646
  }
647
 
648
+ $order_item_ids = $_POST[ 'order_item_ids' ];
649
+ if ( ! is_array ( $order_item_ids ) && is_numeric ( $order_item_ids ) ) {
650
+ $order_item_ids = array ( $order_item_ids );
651
  }
652
  //TODO: add check order_id if ( ! wp_get_post_parent_id( $parent_order_id ) ) {
653
+ if ( sizeof ( $order_item_ids ) > 0 ) {
654
  /** @var $wpdb wpdb */
655
  global $wpdb;
656
  foreach ( $order_item_ids as $order_item_id ) {
657
+ $product_id = $wpdb->get_var ( $wpdb->prepare ( "SELECT DISTINCT meta_value FROM {$wpdb->order_itemmeta} WHERE meta_key=%s AND order_item_id=%d", '_product_id', absint ( $order_item_id ) ) );
658
+ $vendor = yith_get_vendor ( $product_id, 'product' );
659
+ if ( $vendor->is_valid () ) {
660
+ $child_order_item_id = self::get_line_item_id_from_parent ( $order_item_id );
661
+ ! empty( $child_order_item_id ) && wc_delete_order_item ( absint ( $child_order_item_id ) );
662
  }
663
  }
664
  }
671
  * @author Andrea Grillo <andrea.grillo@yithemes.com>
672
  * @return void
673
  */
674
+ public static function add_order_note () {
675
 
676
+ check_ajax_referer ( 'add-order-note', 'security' );
677
 
678
+ if ( ! current_user_can ( 'edit_shop_orders' ) ) {
679
  die( - 1 );
680
  }
681
 
682
+ $post_id = absint ( $_POST[ 'post_id' ] );
683
+ $note = wp_kses_post ( trim ( stripslashes ( $_POST[ 'note' ] ) ) );
684
+ $note_type = $_POST[ 'note_type' ];
685
 
686
  $is_customer_note = $note_type == 'customer' ? 1 : 0;
687
 
688
  if ( $post_id > 0 ) {
689
+ if ( ! wp_get_post_parent_id ( $post_id ) ) {
690
  //Add the order note to parent order
691
+ $order = wc_get_order ( $post_id );
692
+ $parent_note_id = $order->add_order_note ( $note, $is_customer_note, true );
693
 
694
+ echo '<li rel="' . esc_attr ( $parent_note_id ) . '" class="note ';
695
  if ( $is_customer_note ) {
696
  echo 'customer-note';
697
  }
698
  echo '"><div class="note_content">';
699
+ echo wpautop ( wptexturize ( $note ) );
700
+ echo '</div><p class="meta"><a href="#" class="delete_note">' . __ ( 'Delete note', 'woocommerce' ) . '</a></p>';
701
  echo '</li>';
702
 
703
+ $suborder_ids = self::get_suborder ( $post_id );
704
  if ( ! empty( $suborder_ids ) ) {
705
  foreach ( $suborder_ids as $suborder_id ) {
706
+ $suborder = wc_get_order ( $suborder_id );
707
+ $note_id = $suborder->add_order_note ( _x ( 'Update by admin: ', 'Order note', 'yith_wc_product_vendors' ) . $note, $is_customer_note, true );
708
+ add_comment_meta ( $note_id, 'parent_note_id', $parent_note_id );
709
  }
710
  }
711
  /**
713
  * Updated Parent and Child orders
714
  */
715
  die();
716
+ } else {
 
 
717
  //is suborder
718
  //TODO: Suborder sub-routine
719
  }
727
  * @author Andrea Grillo <andrea.grillo@yithemes.com>
728
  * @return void
729
  */
730
+ public static function delete_order_note () {
731
+ check_ajax_referer ( 'delete-order-note', 'security' );
732
 
733
+ if ( ! current_user_can ( 'edit_shop_orders' ) ) {
734
  die( - 1 );
735
  }
736
 
737
  global $wpdb;
738
+ $parent_note_id = absint ( $_POST[ 'note_id' ] );
739
+ $note_ids = $wpdb->get_col ( $wpdb->prepare ( "SELECT comment_id FROM {$wpdb->commentmeta} WHERE meta_key=%s AND meta_value=%d", 'parent_note_id', $parent_note_id ) );
740
 
741
  if ( ! empty( $note_ids ) ) {
742
+ foreach ( $note_ids as $note_id ) {
743
+ wp_delete_comment ( $note_id );
744
  }
745
  }
746
  }
748
  /**
749
  * Reduce order item stock
750
  */
751
+ public static function reduce_order_item_stock () {
752
+ self::order_item_stock ( 'reduce' );
753
  }
754
 
755
+ /**
756
  * Increase order item stock
757
  */
758
+ public static function increase_order_item_stock () {
759
+ self::order_item_stock ( 'increase' );
760
  }
761
 
762
  /**
763
  * Reduce order item stock
764
  */
765
+ public static function order_item_stock ( $ajax_call_type ) {
766
+ check_ajax_referer ( 'order-item', 'security' );
767
 
768
+ if ( ! current_user_can ( 'edit_shop_orders' ) ) {
769
  die( - 1 );
770
  }
771
 
772
+ $order_id = absint ( $_POST[ 'order_id' ] );
773
+ if ( ! wp_get_post_parent_id ( $order_id ) ) {
774
+ $order_item_ids = isset( $_POST[ 'order_item_ids' ] ) ? $_POST[ 'order_item_ids' ] : array ();
775
+ $order_item_qty = isset( $_POST[ 'order_item_qty' ] ) ? $_POST[ 'order_item_qty' ] : array ();
776
+ $order = wc_get_order ( $order_id );
777
+ $order_items = $order->get_items ();
778
 
779
+ if ( $order && ! empty( $order_items ) && sizeof ( $order_item_ids ) > 0 ) {
780
 
781
  foreach ( $order_items as $item_id => $order_item ) {
782
  // Only reduce checked items
783
+ if ( ! in_array ( $item_id, $order_item_ids ) ) {
784
  continue;
785
  }
786
 
787
+ $_product = $order->get_product_from_item ( $order_item );
788
+ $vendor = yith_get_vendor ( $_product, 'product' );
789
+ if ( $vendor->is_valid () && $_product->exists () && $_product->managing_stock () && isset( $order_item_qty[ $item_id ] ) && $order_item_qty[ $item_id ] > 0 ) {
790
  global $wpdb;
791
 
792
+ $old_stock = $_product->get_stock_quantity ();
793
+ $child_order_item_id = self::get_line_item_id_from_parent ( $item_id );
794
+ $suborder_id = $wpdb->get_var ( $wpdb->prepare ( "SELECT DISTINCT order_id FROM {$wpdb->prefix}woocommerce_order_items WHERE order_item_id=%d", absint ( $child_order_item_id ) ) );
795
+ $suborder = wc_get_order ( $suborder_id );
796
  $note = '';
797
+ if ( 'reduce' == $ajax_call_type ) {
798
+ $stock_change = apply_filters ( 'woocommerce_reduce_order_stock_quantity', $order_item_qty[ $item_id ], $item_id );
799
+ $new_stock = $old_stock - $stock_change;
800
+ $note = sprintf ( __ ( 'Item #%s stock reduced from %s to %s.', 'woocommerce' ), $order_item[ 'product_id' ], $old_stock, $new_stock );
801
+ } elseif ( 'increase' == $ajax_call_type ) {
802
+ $stock_change = apply_filters ( 'woocommerce_restore_order_stock_quantity', $order_item_qty[ $item_id ], $item_id );
803
+ $new_stock = $old_stock + $stock_change;
804
+ $note = sprintf ( __ ( 'Item #%s stock increased from %s to %s.', 'woocommerce' ), $order_item[ 'product_id' ], $old_stock, $new_stock );
 
 
805
  }
806
 
807
+ ! empty( $note ) && $suborder->add_order_note ( $note );
808
  }
809
  }
810
  }
811
+ } else {
812
+ //is suborder
813
+ //TODO: Suborder sub-routine
814
  }
 
 
 
 
 
815
  }
816
 
817
  /**
818
  * Remove order item meta
819
  */
820
+ public static function remove_order_item_meta () {
821
  global $wpdb;
822
 
823
+ check_ajax_referer ( 'order-item', 'security' );
824
 
825
+ if ( ! current_user_can ( 'edit_shop_orders' ) ) {
826
  die( - 1 );
827
  }
828
 
829
+ $parent_meta_id = absint ( $_POST[ 'meta_id' ] );
830
+ $parent_meta_key = $wpdb->get_var ( $wpdb->prepare ( "SELECT DISTINCT meta_key FROM {$wpdb->order_itemmeta} WHERE meta_id=%d", $parent_meta_id ) );
831
+ $child_meta_id = $wpdb->get_var ( $wpdb->prepare ( "SELECT DISTINCT meta_id FROM {$wpdb->order_itemmeta} WHERE meta_value=%d AND meta_key=%s", $parent_meta_id, '_parent_' . $parent_meta_key ) );
832
+ $wpdb->query ( $wpdb->prepare ( "DELETE FROM {$wpdb->order_itemmeta} WHERE meta_key=%s AND meta_id=%d", '_parent_' . $parent_meta_key, $child_meta_id ) );
833
  }
834
 
835
  /**
836
  * Add order item via ajax
837
  */
838
+ public static function add_order_item () {
839
+ check_ajax_referer ( 'order-item', 'security' );
840
 
841
+ if ( ! current_user_can ( 'edit_shop_orders' ) ) {
842
  die( - 1 );
843
  }
844
 
845
+ $item_to_add = sanitize_text_field ( $_POST[ 'item_to_add' ] );
846
+ $order_id = absint ( $_POST[ 'order_id' ] );
847
 
848
+ if ( ! wp_get_post_parent_id ( $order_id ) ) {
849
  // Find the item
850
+ if ( ! is_numeric ( $item_to_add ) ) {
851
  die();
852
  }
853
 
854
+ $post = get_post ( $item_to_add );
855
 
856
  if ( ! $post || ( 'product' !== $post->post_type && 'product_variation' !== $post->post_type ) ) {
857
  die();
858
  }
859
 
860
+ $_product = wc_get_product ( $post->ID );
861
+ $order = wc_get_order ( $order_id );
862
+ $order_taxes = $order->get_taxes ();
863
  $class = 'new_row';
864
  $suborders_id = 0;
865
 
866
+ $vendor = yith_get_vendor ( $item_to_add, 'product' );
867
 
868
+ if ( $vendor->is_valid () ) {
869
+ $vendor_suborder_id = $vendor->get_orders ( 'suborder' );
870
+ $suborders_ids = self::get_suborder ( $order_id );
871
+ $suborder_id = array_intersect ( $vendor_suborder_id, $suborders_ids );
872
 
873
+ if ( is_array ( $suborder_id ) && count ( $suborder_id ) == 1 ) {
874
+ $suborder_id = array_shift ( $suborder_id );
875
  }
876
  }
877
 
878
+ else {
879
+ return false;
880
+ }
881
+
882
  // Set values
883
+ $item = array ();
884
+ $item_ids = array ();
885
+
886
+ $item[ 'product_id' ] = $_product->id;
887
+ $item[ 'variation_id' ] = isset( $_product->variation_id ) ? $_product->variation_id : '';
888
+ $item[ 'variation_data' ] = $item[ 'variation_id' ] ? $_product->get_variation_attributes () : '';
889
+ $item[ 'name' ] = $_product->get_title ();
890
+ $item[ 'tax_class' ] = $_product->get_tax_class ();
891
+ $item[ 'qty' ] = 1;
892
+ $item[ 'line_subtotal' ] = wc_format_decimal ( $_product->get_price_excluding_tax () );
893
+ $item[ 'line_subtotal_tax' ] = '';
894
+ $item[ 'line_total' ] = wc_format_decimal ( $_product->get_price_excluding_tax () );
895
+ $item[ 'line_tax' ] = '';
896
+ $item[ 'type' ] = 'line_item';
897
 
898
  // Add line item
899
+ foreach ( array ( 'parent_id' => $order_id, 'child_id' => $suborder_id ) as $type => $id ) {
900
+ $item_ids[ $type ] = wc_add_order_item ( $id, array (
901
+ 'order_item_name' => $item[ 'name' ],
902
+ 'order_item_type' => 'line_item',
 
903
  ) );
904
  }
905
 
906
+ wc_add_order_item_meta ( $item_ids[ 'child_id' ], '_parent_line_item_id', $item_ids[ 'parent_id' ] );
907
 
908
  foreach ( $item_ids as $key => $item_id ) {
909
  // Add line item meta
910
  if ( $item_id ) {
911
+ wc_add_order_item_meta ( $item_id, '_qty', $item[ 'qty' ] );
912
+ wc_add_order_item_meta ( $item_id, '_tax_class', $item[ 'tax_class' ] );
913
+ wc_add_order_item_meta ( $item_id, '_product_id', $item[ 'product_id' ] );
914
+ wc_add_order_item_meta ( $item_id, '_variation_id', $item[ 'variation_id' ] );
915
+ wc_add_order_item_meta ( $item_id, '_line_subtotal', $item[ 'line_subtotal' ] );
916
+ wc_add_order_item_meta ( $item_id, '_line_subtotal_tax', $item[ 'line_subtotal_tax' ] );
917
+ wc_add_order_item_meta ( $item_id, '_line_total', $item[ 'line_total' ] );
918
+ wc_add_order_item_meta ( $item_id, '_line_tax', $item[ 'line_tax' ] );
919
 
920
  // Since 2.2
921
+ wc_add_order_item_meta ( $item_id, '_line_tax_data', array ( 'total' => array (), 'subtotal' => array () ) );
922
 
923
  // Store variation data in meta
924
+ if ( $item[ 'variation_data' ] && is_array ( $item[ 'variation_data' ] ) ) {
925
+ foreach ( $item[ 'variation_data' ] as $key => $value ) {
926
+ wc_add_order_item_meta ( $item_id, str_replace ( 'attribute_', '', $key ), $value );
927
  }
928
  }
929
 
930
+ do_action ( 'woocommerce_ajax_add_order_item_meta', $item_id, $item );
931
  }
932
  }
933
 
934
+ $item[ 'item_meta' ] = $order->get_item_meta ( $item_ids[ 'parent_id' ] );
935
+ $item[ 'item_meta_array' ] = $order->get_item_meta_array ( $item_ids[ 'parent_id' ] );
936
+ $item = $order->expand_item_meta ( $item );
937
+ $item = apply_filters ( 'woocommerce_ajax_order_item', $item, $item_ids[ 'parent_id' ] );
938
 
939
  /**
940
  * WooCommerce Template Hack:
941
  * Copy the parent item id into the variable $item_id
942
  */
943
+ $item_id = $item_ids[ 'parent_id' ];
944
+ include ( WC ()->plugin_path () . '/includes/admin/meta-boxes/views/html-order-item.php' );
945
 
946
  /**
947
  * Prevent call default WooCommerce add_order_item() method
948
  */
949
  die();
950
+ } else {
 
 
951
  //is suborder
952
  //TODO: Suborder sub-routine
953
  }
956
  /**
957
  * Add commission id from parent to child order
958
  */
959
+ public function register_commission_to_parent_order ( $commission_id, $child_item_id, $key, $suborder ) {
960
  // add line item to retrieve simply the commission associated (child order)
961
+ $parent_item_id = self::get_parent_item_id ( $suborder, $child_item_id );
962
+ ! empty( $parent_item_id ) && wc_add_order_item_meta ( $parent_item_id, '_child_' . $key, $commission_id );
963
  }
964
 
965
  /**
966
  * Add order tax column via ajax
967
  */
968
+ public static function add_order_tax () {
969
+ check_ajax_referer ( 'order-item', 'security' );
970
 
971
+ if ( ! current_user_can ( 'edit_shop_orders' ) ) {
972
  die( - 1 );
973
  }
974
 
975
+ $order_id = absint ( $_POST[ 'order_id' ] );
976
 
977
+ if ( ! wp_get_post_parent_id ( $order_id ) ) {
978
+ $rate_id = absint ( $_POST[ 'rate_id' ] );
979
+ $suborder_ids = self::get_suborder ( $order_id );
980
 
981
+ foreach ( $suborder_ids as $suborder_id ) {
982
+ $suborder = ! empty( $suborder_id ) ? wc_get_order ( absint ( $suborder_id ) ) : false;
983
+ $suborder && $suborder->add_tax ( $rate_id, 0, 0 );
984
  }
985
+ } else {
 
 
986
  //is suborder
987
  //TODO: Suborder sub-routine
988
  }
991
  /**
992
  * Calc line tax
993
  */
994
+ public static function calc_line_taxes () {
995
+ check_ajax_referer ( 'calc-totals', 'security' );
996
 
997
+ if ( ! current_user_can ( 'edit_shop_orders' ) ) {
998
  die( - 1 );
999
  }
1000
 
1001
+ $order_id = absint ( $_POST[ 'order_id' ] );
1002
 
1003
+ if ( ! wp_get_post_parent_id ( $order_id ) ) {
1004
  $_post = $_POST;
1005
+ $suborder_ids = self::get_suborder ( $order_id );
1006
 
1007
  foreach ( $suborder_ids as $suborder_id ) {
1008
+ self::add_line_taxes ( $suborder_id );
1009
  }
1010
+ } else {
 
 
1011
  //is suborder
1012
  //TODO: Suborder sub-routine
1013
  }
1014
  }
1015
 
1016
+ public static function add_line_taxes ( $order_id ) {
1017
  global $wpdb;
1018
 
1019
+ check_ajax_referer ( 'calc-totals', 'security' );
1020
 
1021
+ if ( ! current_user_can ( 'edit_shop_orders' ) ) {
1022
  die( - 1 );
1023
  }
1024
 
1025
  $tax = new WC_Tax();
1026
+ $items = array ();
1027
+ $country = strtoupper ( esc_attr ( $_POST[ 'country' ] ) );
1028
+ $state = strtoupper ( esc_attr ( $_POST[ 'state' ] ) );
1029
+ $postcode = strtoupper ( esc_attr ( $_POST[ 'postcode' ] ) );
1030
+ $city = wc_clean ( esc_attr ( $_POST[ 'city' ] ) );
1031
+ $order = wc_get_order ( absint ( $order_id ) );
1032
+ $taxes = array ();
1033
+ $shipping_taxes = array ();
1034
 
1035
  // Parse the jQuery serialized items
1036
+ parse_str ( $_POST[ 'items' ], $items );
1037
 
1038
  // Prevent undefined warnings
1039
+ if ( ! isset( $items[ 'line_tax' ] ) ) {
1040
+ $items[ 'line_tax' ] = array ();
1041
  }
1042
 
1043
+ if ( ! isset( $items[ 'line_subtotal_tax' ] ) ) {
1044
+ $items[ 'line_subtotal_tax' ] = array ();
1045
  }
1046
 
1047
+ $items[ 'order_taxes' ] = array ();
1048
 
1049
  // Action
1050
+ $items = apply_filters ( 'woocommerce_ajax_calc_line_taxes', $items, $order_id, $country, $_POST );
1051
 
1052
  // Get items and fees taxes
1053
+ if ( isset( $items[ 'order_item_id' ] ) ) {
1054
+ $line_total = $line_subtotal = $order_item_tax_class = array ();
1055
+ foreach ( $items[ 'order_item_id' ] as $parent_item_id ) {
1056
+ $parent_item_id = absint ( $parent_item_id );
1057
+ $item_id = self::get_child_item_id ( $parent_item_id );
1058
 
1059
+ if ( empty( $item_id ) ) {
1060
  //no current suborder items
1061
  continue;
1062
  }
1063
 
1064
+ $line_total[ $item_id ] = isset( $items[ 'line_total' ][ $parent_item_id ] ) ? wc_format_decimal ( $items[ 'line_total' ][ $parent_item_id ] ) : 0;
1065
+ $line_subtotal[ $item_id ] = isset( $items[ 'line_subtotal' ][ $parent_item_id ] ) ? wc_format_decimal ( $items[ 'line_subtotal' ][ $parent_item_id ] ) : $line_total[ $parent_item_id ];
1066
+ $order_item_tax_class[ $item_id ] = isset( $items[ 'order_item_tax_class' ][ $parent_item_id ] ) ? sanitize_text_field ( $items[ 'order_item_tax_class' ][ $parent_item_id ] ) : '';
1067
+ $product_id = $order->get_item_meta ( $item_id, '_product_id', true );
1068
 
1069
+ $vendor = yith_get_vendor ( $product_id, 'product' );
1070
 
1071
+ if ( ! $vendor->is_valid () ) {
1072
  // no vnedor products
1073
  continue;
1074
  }
1075
 
1076
+ $vendor_order_ids = $vendor->get_orders ( 'suborder' );
1077
 
1078
+ if ( ! in_array ( $order_id, $vendor_order_ids ) ) {
1079
  // the current product isn't in the current suborder
1080
  continue;
1081
  }
1082
 
1083
  // Get product details
1084
+ if ( get_post_type ( $product_id ) == 'product' ) {
1085
+ $_product = wc_get_product ( $product_id );
1086
+ $item_tax_status = $_product->get_tax_status ();
1087
+ } else {
 
1088
  $item_tax_status = 'taxable';
1089
  }
1090
 
1091
+ if ( '0' !== $order_item_tax_class[ $item_id ] && 'taxable' === $item_tax_status ) {
1092
+ $tax_rates = WC_Tax::find_rates ( array (
1093
  'country' => $country,
1094
  'state' => $state,
1095
  'postcode' => $postcode,
1096
  'city' => $city,
1097
+ 'tax_class' => $order_item_tax_class[ $item_id ],
1098
  ) );
1099
 
1100
+ $line_taxes = WC_Tax::calc_tax ( $line_total[ $item_id ], $tax_rates, false );
1101
+ $line_subtotal_taxes = WC_Tax::calc_tax ( $line_subtotal[ $item_id ], $tax_rates, false );
1102
 
1103
  // Set the new line_tax
1104
  foreach ( $line_taxes as $_tax_id => $_tax_value ) {
1105
+ $items[ 'line_tax' ][ $item_id ][ $_tax_id ] = $_tax_value;
1106
  }
1107
 
1108
  // Set the new line_subtotal_tax
1109
  foreach ( $line_subtotal_taxes as $_tax_id => $_tax_value ) {
1110
+ $items[ 'line_subtotal_tax' ][ $item_id ][ $_tax_id ] = $_tax_value;
1111
  }
1112
 
1113
  // Sum the item taxes
1114
+ foreach ( array_keys ( $taxes + $line_taxes ) as $key ) {
1115
+ $taxes[ $key ] = ( isset( $line_taxes[ $key ] ) ? $line_taxes[ $key ] : 0 ) + ( isset( $taxes[ $key ] ) ? $taxes[ $key ] : 0 );
1116
  }
1117
  }
1118
  }
1119
  }
1120
 
1121
  // Get shipping taxes
1122
+ if ( isset( $items[ 'shipping_method_id' ] ) ) {
1123
+ $matched_tax_rates = array ();
1124
 
1125
+ $tax_rates = WC_Tax::find_rates ( array (
1126
  'country' => $country,
1127
  'state' => $state,
1128
  'postcode' => $postcode,
1129
  'city' => $city,
1130
+ 'tax_class' => '',
1131
  ) );
1132
 
1133
  if ( $tax_rates ) {
1134
  foreach ( $tax_rates as $key => $rate ) {
1135
+ if ( isset( $rate[ 'shipping' ] ) && 'yes' == $rate[ 'shipping' ] ) {
1136
+ $matched_tax_rates[ $key ] = $rate;
1137
  }
1138
  }
1139
  }
1140
 
1141
+ $shipping_cost = $shipping_taxes = array ();
1142
 
1143
+ foreach ( $items[ 'shipping_method_id' ] as $item_id ) {
1144
+ $item_id = absint ( $item_id );
1145
+ $shipping_cost[ $item_id ] = isset( $items[ 'shipping_cost' ][ $parent_item_id ] ) ? wc_format_decimal ( $items[ 'shipping_cost' ][ $parent_item_id ] ) : 0;
1146
+ $_shipping_taxes = WC_Tax::calc_shipping_tax ( $shipping_cost[ $item_id ], $matched_tax_rates );
1147
 
1148
  // Set the new shipping_taxes
1149
  foreach ( $_shipping_taxes as $_tax_id => $_tax_value ) {
1150
+ $items[ 'shipping_taxes' ][ $item_id ][ $_tax_id ] = $_tax_value;
1151
 
1152
+ $shipping_taxes[ $_tax_id ] = isset( $shipping_taxes[ $_tax_id ] ) ? $shipping_taxes[ $_tax_id ] + $_tax_value : $_tax_value;
1153
  }
1154
  }
1155
  }
1156
 
1157
  // Remove old tax rows
1158
+ $order->remove_order_items ( 'tax' );
1159
 
1160
  // Add tax rows
1161
+ foreach ( array_keys ( $taxes + $shipping_taxes ) as $tax_rate_id ) {
1162
+ $order->add_tax ( $tax_rate_id, isset( $taxes[ $tax_rate_id ] ) ? $taxes[ $tax_rate_id ] : 0, isset( $shipping_taxes[ $tax_rate_id ] ) ? $shipping_taxes[ $tax_rate_id ] : 0 );
1163
  }
1164
 
1165
  // Create the new order_taxes
1166
+ foreach ( $order->get_taxes () as $tax_id => $tax_item ) {
1167
+ $items[ 'order_taxes' ][ $tax_id ] = absint ( $tax_item[ 'rate_id' ] );
1168
  }
1169
 
1170
  foreach ( $items as $meta_key => $meta_values ) {
1171
+ if ( is_array ( $meta_values ) ) {
1172
  foreach ( $meta_values as $key => $meta_value ) {
1173
  if ( 'order_taxes' == $meta_key ) {
1174
  continue;
1175
+ } else if ( 'order_item_id' == $meta_key ) {
1176
+ $child_item_id = self::get_child_item_id ( $meta_value );
 
 
1177
  if ( $child_item_id ) {
1178
+ $items[ $meta_key ][ $key ] = $child_item_id;
1179
+ } else {
1180
+ unset( $items[ $meta_key ][ $key ] );
 
 
1181
  }
1182
+ } else if ( 'meta_key' == $meta_key || 'meta_value' == $meta_key ) {
1183
+ unset( $items[ $meta_key ][ $key ] );
1184
+ } else {
 
 
 
 
1185
  if ( 'line_tax' == $meta_key || 'line_subtotal_tax' == $meta_key || 'refund_line_tax' == $meta_key ) {
1186
+ $line_tax_ids = $items[ $meta_key ];
1187
+ $child_item_ids = array_keys ( $order->get_items () );
1188
  foreach ( $line_tax_ids as $line_tax_id => $line_tax_value ) {
1189
+ if ( ! in_array ( $line_tax_id, $child_item_ids ) ) {
1190
+ unset( $items[ $meta_key ][ $line_tax_id ] );
1191
  }
1192
  }
1193
+ } else {
1194
+ $child_item_id = self::get_child_item_id ( $meta_value );
 
 
1195
  if ( $child_item_id ) {
1196
+ $items[ $meta_key ][ $child_item_id ] = $items[ $meta_key ][ $key ];
1197
+ unset( $items[ $meta_key ][ $key ] );
1198
  }
1199
  }
1200
  }
1201
  }
1202
+ } else if ( '_order_total' == $meta_key ) {
1203
+ $items[ '_order_total' ] = $order->get_total ();
 
 
1204
  }
1205
  }
1206
 
1207
+ if ( ! empty( $items[ 'order_item_id' ] ) ) {
1208
+ wc_save_order_items ( $order_id, $items );
1209
  }
1210
  }
1211
 
1212
  /**
1213
  * Remove an order tax
1214
  */
1215
+ public static function remove_order_tax () {
1216
 
1217
+ check_ajax_referer ( 'order-item', 'security' );
1218
 
1219
+ if ( ! current_user_can ( 'edit_shop_orders' ) ) {
1220
  die( - 1 );
1221
  }
1222
 
1223
+ $order_id = absint ( $_POST[ 'order_id' ] );
1224
 
1225
+ if ( ! wp_get_post_parent_id ( $order_id ) ) {
1226
+ $rate_id = absint ( $_POST[ 'rate_id' ] );
1227
+ $parent_order = wc_get_order ( $order_id );
1228
+ $parent_taxes = $parent_order->get_taxes ();
1229
+ $suborder_ids = self::get_suborder ( $order_id );
1230
+ $parent_tax_to_remove = $parent_taxes[ $rate_id ];
1231
 
1232
+ foreach ( $suborder_ids as $suborder_id ) {
1233
+ $suborder = wc_get_order ( $suborder_id );
1234
+ $suborder_taxes = $suborder->get_taxes ();
1235
+ foreach ( $suborder_taxes as $suborder_tax_key => $suborder_tax_item ) {
1236
+ $suborder_tax_item[ 'rate_id' ] == $parent_tax_to_remove[ 'rate_id' ]
1237
  &&
1238
+ $suborder_tax_item[ 'name' ] == $parent_tax_to_remove[ 'name' ]
1239
  &&
1240
+ $suborder_tax_item[ 'label' ] == $parent_tax_to_remove[ 'label' ]
1241
  &&
1242
+ wc_delete_order_item ( $suborder_tax_key );
1243
  }
1244
  }
1245
+ } else {
 
 
1246
  //is suborder
1247
  //TODO: Suborder sub-routine
1248
  }
1251
  /**
1252
  * Check for new order email
1253
  */
1254
+ public function woocommerce_email_enabled_new_order ( $enabled, $object ) {
1255
+ return $enabled && is_a ( $object, 'WC_Order' ) && wp_get_post_parent_id ( $object->id ) != 0 ? false : $enabled;
1256
  }
1257
 
1258
  /**
1259
  * Check for email recipient
1260
  */
1261
+ public function woocommerce_email_recipient_new_order ( $recipient, $object ) {
1262
+ return ( $recipient == get_option ( 'recipient' ) || $recipient == get_option ( 'admin_email' ) ) && $object instanceof WC_Order && wp_get_post_parent_id ( $object->id ) ? false : $recipient;
1263
  }
1264
 
1265
  /**
1269
  * @use woocommerce_order_refunded action
1270
  * @see woocommerce\includes\class-wc-ajax.php:2295
1271
  */
1272
+ public function order_refunded ( $order_id, $parent_refund_id ) {
1273
+
1274
+ if ( ! wp_get_post_parent_id ( $order_id ) ) {
1275
+ $create_refund = true;
1276
+ $refund = false;
1277
+ $parent_line_item_refund = 0;
1278
+ $refund_amount = wc_format_decimal ( sanitize_text_field ( $_POST[ 'refund_amount' ] ) );
1279
+ $refund_reason = sanitize_text_field ( $_POST[ 'refund_reason' ] );
1280
+ $line_item_qtys = json_decode ( sanitize_text_field ( stripslashes ( $_POST[ 'line_item_qtys' ] ) ), true );
1281
+ $line_item_totals = json_decode ( sanitize_text_field ( stripslashes ( $_POST[ 'line_item_totals' ] ) ), true );
1282
+ $line_item_tax_totals = json_decode ( sanitize_text_field ( stripslashes ( $_POST[ 'line_item_tax_totals' ] ) ), true );
1283
+ $api_refund = $_POST[ 'api_refund' ] === 'true' ? true : false;
1284
+ $restock_refunded_items = $_POST[ 'restock_refunded_items' ] === 'true' ? true : false;
1285
+ $order = wc_get_order ( $order_id );
1286
+ $parent_order_total = wc_format_decimal ( $order->get_total () );
1287
+ $suborder_ids = self::get_suborder ( $order_id );
1288
 
1289
  //calculate line items total from parent order
1290
+ foreach ( $line_item_totals as $item_id => $total ) {
1291
+ $parent_line_item_refund += wc_format_decimal ( $total );
1292
  }
1293
 
1294
+ foreach ( $suborder_ids as $suborder_id ) {
1295
+ $suborder = wc_get_order ( $suborder_id );
1296
+ $suborder_items_ids = array_keys ( $suborder->get_items () );
1297
+ $suborder_total = wc_format_decimal ( $suborder->get_total () );
1298
+ $max_refund = wc_format_decimal ( $suborder_total - $suborder->get_total_refunded () );
1299
  $child_line_item_refund = 0;
1300
 
1301
  // Prepare line items which we are refunding
1302
+ $line_items = array ();
1303
+ $item_ids = array_unique ( array_merge ( array_keys ( $line_item_qtys, $line_item_totals ) ) );
1304
 
1305
+ foreach ( $item_ids as $item_id ) {
1306
+ $child_item_id = self::get_child_item_id ( $item_id );
1307
+ if ( $child_item_id && in_array ( $child_item_id, $suborder_items_ids ) ) {
1308
+ $line_items[ $child_item_id ] = array ( 'qty' => 0, 'refund_total' => 0, 'refund_tax' => array () );
1309
  }
1310
  }
1311
 
1312
+ foreach ( $line_item_qtys as $item_id => $qty ) {
1313
+ $child_item_id = self::get_child_item_id ( $item_id );
1314
+ if ( $child_item_id && in_array ( $child_item_id, $suborder_items_ids ) ) {
1315
+ $line_items[ $child_item_id ][ 'qty' ] = max ( $qty, 0 );
1316
  }
1317
  }
1318
 
1319
+ foreach ( $line_item_totals as $item_id => $total ) {
1320
+ $child_item_id = self::get_child_item_id ( $item_id );
1321
+ if ( $child_item_id && in_array ( $child_item_id, $suborder_items_ids ) ) {
1322
+ $total = wc_format_decimal ( $total );
1323
  $child_line_item_refund += $total;
1324
+ $line_items[ $child_item_id ][ 'refund_total' ] = $total;
1325
  }
1326
  }
1327
 
1328
+ foreach ( $line_item_tax_totals as $item_id => $tax_totals ) {
1329
+ $child_item_id = self::get_child_item_id ( $item_id );
1330
+ if ( $child_item_id && in_array ( $child_item_id, $suborder_items_ids ) ) {
1331
+ $line_items[ $child_item_id ][ 'refund_tax' ] = array_map ( 'wc_format_decimal', $tax_totals );
1332
  }
1333
  }
1334
 
1335
  //calculate refund amount percentage
1336
  $suborder_refund_amount = ( ( ( $refund_amount - $parent_line_item_refund ) * $suborder_total ) / $parent_order_total );
1337
+ $suborder_total_refund = wc_format_decimal ( $child_line_item_refund + $suborder_refund_amount );
1338
 
1339
+ if ( ! $refund_amount || $max_refund < $suborder_total_refund || 0 > $suborder_total_refund ) {
1340
  /**
1341
  * Invalid refund amount.
1342
  * Check if suborder total != 0 create a partial refund, exit otherwise
1343
  */
1344
+ $surplus = wc_format_decimal ( $suborder_total_refund - $max_refund );
1345
+ $suborder_total_refund = $suborder_total_refund - $surplus;
1346
+ $create_refund = $suborder_total_refund > 0 ? true : false;
1347
  }
1348
 
1349
+ if ( $create_refund ) {
1350
  // Create the refund object
1351
+ $refund = wc_create_refund ( array (
1352
  'amount' => $suborder_total_refund,
1353
  'reason' => $refund_reason,
1354
  'order_id' => $suborder->id,
1355
+ 'line_items' => $line_items,
1356
  )
1357
  );
1358
 
1359
+ add_post_meta ( $refund->id, '_parent_refund_id', $parent_refund_id );
1360
  }
1361
  }
1362
  }
1369
  * @use woocommerce_refund_deleted action
1370
  * @see woocommerce\includes\class-wc-ajax.php:2328
1371
  */
1372
+ public static function refund_deleted ( $refund_id, $parent_order_id ) {
1373
+ check_ajax_referer ( 'order-item', 'security' );
1374
 
1375
+ if ( ! current_user_can ( 'edit_shop_orders' ) ) {
1376
+ die( - 1 );
1377
  }
1378
 
1379
+ if ( ! wp_get_post_parent_id ( $parent_order_id ) ) {
1380
  global $wpdb;
1381
+ $child_refund_ids = $wpdb->get_col ( $wpdb->prepare ( "SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key=%s AND meta_value=%s", '_parent_refund_id', $refund_id ) );
1382
 
1383
+ foreach ( $child_refund_ids as $child_refund_id ) {
1384
+ if ( $child_refund_id && 'shop_order_refund' === get_post_type ( $child_refund_id ) ) {
1385
+ $order_id = wp_get_post_parent_id ( $child_refund_id );
1386
+ wc_delete_shop_order_transients ( $order_id );
1387
+ wp_delete_post ( $child_refund_id );
1388
  }
1389
  }
1390
  }
1399
  *
1400
  * @return string The label value
1401
  */
1402
+ public function commissions_attribute_label ( $attribute_label, $meta_key, $product = false ) {
1403
  global $pagenow;
1404
 
1405
+ if ( $product && 'post.php' == $pagenow && isset( $_GET[ 'post' ] ) && $order = wc_get_order ( $_GET[ 'post' ] ) ) {
1406
+ $line_items = $order->get_items ( 'line_item' );
1407
+ $item_meta_key = wp_get_post_parent_id ( $order->id ) ? '_commission_id' : '_child__commission_id';
1408
  foreach ( $line_items as $line_item_id => $line_item ) {
1409
+ if ( $line_item[ 'product_id' ] == $product->id ) {
1410
+ $commission_id = wc_get_order_item_meta ( $line_item_id, $item_meta_key, true );
1411
+ $admin_url = YITH_Commission ( $commission_id )->get_view_url ( 'admin' );
1412
+ $attribute_label = $item_meta_key == $meta_key ? sprintf ( "<a href='%s' class='%s'>" . __ ( 'commission_id', 'yith_wc_product_vendors' ) . '</a>', $admin_url, 'commission-id-label' ) : $attribute_label;
1413
  }
1414
  }
1415
  }
1416
+
1417
  return $attribute_label;
1418
  }
1419
 
1421
  * Filter the edit order page for vendors
1422
  *
1423
  * @author Andrea Grillo <andrea.grillo@yithemes.com>
1424
+ * @since 1.6
1425
+ *
1426
  * @param $check
1427
+ *
1428
  * @return bool
1429
  */
1430
+ public function vendor_single_order_page ( $check ) {
1431
  global $theorder;
1432
+ $vendor = yith_get_vendor ( 'current', 'user' );
1433
+ $is_ajax = defined ( 'DOING_AJAX' ) && DOING_AJAX;
1434
+ $is_order_details = is_admin () && ! $is_ajax && 'shop_order' == get_current_screen ()->id;
1435
 
1436
+ if ( ( $vendor->is_valid () || ( $vendor->is_super_user () && is_object ( $theorder ) && wp_get_post_parent_id ( $theorder->id ) ) ) && $is_order_details && 'wc_order_is_editable' == current_filter () ) {
1437
  $check = false;
1438
  }
1439
+
1440
  return $check;
1441
  }
1442
+
1443
+ /**
1444
  * Filters meta to hide, to add to the list item order meta added by author class
1445
  *
1446
  * @param $to_hidden Array of order_item_meta meta_key to hide
1449
  * @since 1.0
1450
  * @author Andrea Grillo <andrea.grillo@yithemes.com>
1451
  */
1452
+ public function hidden_order_itemmeta ( $to_hidden ) {
1453
+ if ( ! defined ( 'WP_DEBUG' ) || ( defined ( 'WP_DEBUG' ) && ! WP_DEBUG ) ) {
1454
  $to_hidden[] = '_parent_line_item_id';
1455
  }
1456
 
1457
+ if ( ! apply_filters ( 'yith_show_commissions_order_item_meta', YITH_Commissions ()->show_order_item_meta ) ) {
1458
  $to_hidden[] = '_commission_id';
1459
  $to_hidden[] = '_parent__commission_id';
1460
  }
1469
  *
1470
  * @return string The label value
1471
  */
1472
+ public function shop_order_columns ( $order_columns ) {
1473
+ $vendor = yith_get_vendor ( 'current', 'user' );
1474
+ if ( $vendor->is_super_user () ) {
1475
+ if( ( ! isset( $_GET['post_status'] ) || ( isset( $_GET['post_status'] ) && 'trash' != $_GET['post_status'] ) ) ){
1476
+ $suborder = array ( 'suborder' => _x ( 'Suborders', 'Admin: Order table column', 'yith_wc_product_vendors' ) );
1477
+ $ref_pos = array_search ( 'order_title', array_keys ( $order_columns ) );
1478
+ $order_columns = array_slice ( $order_columns, 0, $ref_pos + 1, true ) + $suborder + array_slice ( $order_columns, $ref_pos + 1, count ( $order_columns ) - 1, true );
1479
+ }
1480
+
1481
+ else {
1482
+ $vendor = array( 'vendor' => _x( 'Vendor', 'Admin: Order table column', 'yith_wc_product_vendors' ) );
1483
+ $ref_pos = array_search( 'order_title', array_keys( $order_columns ) );
1484
+ $order_columns = array_slice( $order_columns, 0, $ref_pos + 1, true ) + $vendor + array_slice( $order_columns, $ref_pos + 1, count( $order_columns ) - 1, true );
1485
+ }
1486
  }
1487
+
1488
  return $order_columns;
1489
  }
1490
 
1493
  *
1494
  * @param string $column
1495
  */
1496
+ public function render_shop_order_columns ( $column ) {
1497
  global $post, $the_order;
1498
 
1499
  if ( empty( $the_order ) || $the_order->id != $post->ID ) {
1500
+ $_the_order = wc_get_order ( $post->ID );
1501
+ } else {
 
 
1502
  $_the_order = $the_order;
1503
  }
1504
 
1505
  switch ( $column ) {
1506
  case 'suborder' :
1507
+ $suborder_ids = self::get_suborder ( $_the_order->id );
1508
 
1509
  if ( $suborder_ids ) {
1510
  foreach ( $suborder_ids as $suborder_id ) {
1511
+ $suborder = wc_get_order ( $suborder_id );
1512
+ $vendor = yith_get_vendor ( $suborder->post->post_author, 'user' );
1513
+ $order_uri = esc_url ( 'post.php?post=' . absint ( $suborder_id ) . '&action=edit' );
1514
+
1515
+ printf ( '<mark class="%s tips" data-tip="%s">%s</mark> <strong><a href="%s">#%s</a></strong> <small class="yith-wcmv-suborder-owner">(%s %s)</small>',
1516
+ sanitize_title ( $suborder->get_status () ),
1517
+ wc_get_order_status_name ( $suborder->get_status () ),
1518
+ wc_get_order_status_name ( $suborder->get_status () ),
1519
  $order_uri,
1520
  $suborder_id,
1521
+ __ ( 'in', 'Order table details', 'yith_wc_product_vendors' ),
1522
  $vendor->name
1523
  );
1524
+
1525
+ do_action ( 'yith_wcmv_after_suborder_details', $suborder );
1526
  }
1527
+ } else {
1528
+ echo '<span class="na">&ndash;</span>';
1529
+ }
1530
+
1531
+ break;
1532
+
1533
+ case 'vendor':
1534
+ $vendor = yith_get_vendor ( $_the_order->post->post_author, 'user' );
1535
+ if( $vendor->is_valid() ){
1536
+ printf( '<a href="%s">%s</a>', $vendor->get_url( 'admin' ), $vendor->name );
1537
  }
1538
 
1539
  else {
1540
  echo '<span class="na">&ndash;</span>';
1541
  }
 
1542
  break;
1543
  }
1544
  }
1545
 
1546
+ /**
1547
  * Add suborder metaboxes for Vendors order
1548
  *
1549
  * @return void
1550
+ * @author Andrea Grillo <andrea.grillo@yithemes.com>
1551
  */
1552
+ public function add_meta_boxes () {
1553
+ if ( 'shop_order' != get_current_screen ()->id ) {
1554
  return;
1555
  }
1556
 
1557
  global $post;
1558
+ $vendor = yith_get_vendor ( 'current', 'user' );
1559
+ $has_suborder = self::get_suborder ( absint ( $post->ID ) );
1560
+ $is_suborder = wp_get_post_parent_id ( absint ( $post->ID ) );
1561
+
1562
+ if ( $vendor->is_super_user () ) {
1563
+ if ( $has_suborder ) {
1564
+ $metabox_suborder_description = _x ( 'Suborders', 'Admin: Single order page. Suborder details box', 'yith_wc_product_vendors' ) . ' <span class="tips" data-tip="' . esc_attr__ ( 'Note: from this box you can monitor the status of suborders associated to individual vendors.', 'woocommerce' ) . '">[?]</span>';
1565
+ add_meta_box ( 'woocommerce-suborders', $metabox_suborder_description, array ( $this, 'output' ), 'shop_order', 'side', 'core', array ( 'metabox' => 'suborders' ) );
1566
+ } else if ( $is_suborder ) {
1567
+ $metabox_parent_order_description = _x ( 'Parent order', 'Admin: Single order page. Parent order details box', 'yith_wc_product_vendors' );
1568
+ add_meta_box ( 'woocommerce-parent-order', $metabox_parent_order_description, array ( $this, 'output' ), 'shop_order', 'side', 'high', array ( 'metabox' => 'parent-order' ) );
 
 
1569
  }
1570
  }
1571
  }
1579
  * @return void
1580
  * @author Andrea Grillo <andrea.grillo@yithemes.com>
1581
  */
1582
+ public function output ( $post, $param ) {
1583
+ switch ( $param[ 'args' ][ 'metabox' ] ) {
1584
  case 'suborders':
1585
+ $suborder_ids = self::get_suborder ( absint ( $post->ID ) );
1586
  echo '<ul class="suborders-list single-orders">';
1587
  foreach ( $suborder_ids as $suborder_id ) {
1588
+ $suborder = wc_get_order ( absint ( $suborder_id ) );
1589
+ $vendor = yith_get_vendor ( $suborder->post->post_author, 'user' );
1590
+ $suborder_uri = esc_url ( 'post.php?post=' . absint ( $suborder_id ) . '&action=edit' );
1591
  echo '<li class="suborder-info">';
1592
+ printf ( '<mark class="%s tips" data-tip="%s">%s</mark> <strong><a href="%s">#%s</a></strong> <small class="single-order yith-wcmv-suborder-owner">%s %s</small><br/>',
1593
+ sanitize_title ( $suborder->get_status () ),
1594
+ wc_get_order_status_name ( $suborder->get_status () ),
1595
+ wc_get_order_status_name ( $suborder->get_status () ),
1596
  $suborder_uri,
1597
  $suborder_id,
1598
+ __ ( 'in', 'Order table details', 'yith_wc_product_vendors' ),
1599
  $vendor->name
1600
  );
1601
  echo '<li>';
1604
  break;
1605
 
1606
  case 'parent-order':
1607
+ $parent_order_id = wp_get_post_parent_id ( absint ( $post->ID ) );
1608
+ $parent_order_uri = esc_url ( 'post.php?post=' . absint ( $parent_order_id ) . '&action=edit' );
1609
+ printf ( '<a href="%s">&#8592; %s</a>', $parent_order_uri, _x ( 'Return to main order', 'Admin: single order page. Link to parent order', 'yith_wc_product_vendors' ) );
1610
  break;
1611
  }
1612
  }
1613
+
1614
+ /**
1615
+ * Retrieve all items from an order, grouping all by vendor
1616
+ *
1617
+ * @param int $parent_order_id the parent order id
1618
+ * @param array $args additional parameters
1619
+ *
1620
+ * @return array
1621
+ * @author Lorenzo Giuffrida
1622
+ * @since 1.0.0
1623
+ */
1624
+ public static function get_order_items_by_vendor ( $parent_order_id, $args = array () ) {
1625
+
1626
+ /**
1627
+ * Define the array of defaults
1628
+ */
1629
+ $defaults = array (
1630
+ 'hide_no_vendor' => false,
1631
+ 'hide_without_shipping' => false,
1632
+ );
1633
+
1634
+ /**
1635
+ * Parse incoming $args into an array and merge it with $defaults
1636
+ */
1637
+ $args = wp_parse_args ( $args, $defaults );
1638
+
1639
+ $parent_order = wc_get_order ( $parent_order_id );
1640
+ $items = $parent_order->get_items ();
1641
+ $product_by_vendor = array ();
1642
+
1643
+ //check for vendor product
1644
+ foreach ( $items as $item_id => $item ) {
1645
+ $vendor = yith_get_vendor ( $item[ 'product_id' ], 'product' );
1646
+
1647
+ $vendor_id = 0;
1648
+ if ( $vendor->is_valid ( $vendor ) ) {
1649
+ $vendor_id = $vendor->id;
1650
+ }
1651
+
1652
+ // optionally skip product without vendor
1653
+ if ( $args[ "hide_no_vendor" ] && ! $vendor_id ) {
1654
+ continue;
1655
+ }
1656
+
1657
+ // optionally skip product without ship
1658
+ if ( $args[ "hide_without_shipping" ] ) {
1659
+ $product_id = $item[ "product_id" ];
1660
+ if ( 0 != $item[ "variation_id" ] ) {
1661
+ $product_id = $item[ "variation_id" ];
1662
+ }
1663
+
1664
+ $product = wc_get_product ( $product_id );
1665
+ if ( ! $product->needs_shipping () ) {
1666
+ continue;
1667
+ }
1668
+ }
1669
+
1670
+ $product_by_vendor[ $vendor_id ][ $item_id ] = $item;
1671
+ }
1672
+
1673
+ return $product_by_vendor;
1674
+ }
1675
  }
1676
  }
modules/yith-woocommerce-product-vendors/includes/class.yith-vendor.php CHANGED
@@ -7,11 +7,11 @@
7
  * It is also available through the world-wide-web at this URL:
8
  * http://www.gnu.org/licenses/gpl-3.0.txt
9
  */
10
- if ( ! defined( 'ABSPATH' ) ) {
11
  exit( 'Direct access forbidden.' );
12
  }
13
 
14
- if ( ! class_exists( 'YITH_Vendor' ) ) {
15
 
16
  /**
17
  * The main class for the Vendor
@@ -21,23 +21,25 @@ if ( ! class_exists( 'YITH_Vendor' ) ) {
21
  * @since 1.0.0
22
  * @author Your Inspiration Themes
23
  *
24
- * @property string $paypal_email
25
- * @property string $enable_selling
26
- * @property string $payment_type
27
- * @property string $threshold
28
- * @property string $registration_date
29
- * @property string $registration_date_gmt
30
- * @property array $admins
31
- * @property string $vacation_selling
32
- * @property string $vacation_message
33
- * @property string $vacation_start_date
34
- * @property string $vacation_end_date
35
- * @property string $show_gravatar
36
- * @property int|string $commission
37
- * @property string $vat
 
38
  */
39
  class YITH_Vendor {
40
 
 
41
  /** @public int The vendor ID. */
42
  public $id = 0;
43
 
@@ -60,7 +62,7 @@ if ( ! class_exists( 'YITH_Vendor' ) ) {
60
  * Main Instance
61
  *
62
  * @var string
63
- * @since 1.0
64
  * @access protected
65
  */
66
  protected static $_instance = null;
@@ -73,10 +75,10 @@ if ( ! class_exists( 'YITH_Vendor' ) ) {
73
  *
74
  * @return bool|YITH_Vendor
75
  */
76
- public static function retrieve( $vendor = false, $obj = 'vendor' ) {
77
- self::$_usermetaKey = YITH_Vendors()->get_user_meta_key();
78
- self::$_usermetaOwner = YITH_Vendors()->get_user_meta_owner();
79
- self::$taxonomy = YITH_Vendors()->get_taxonomy_name();
80
 
81
  // change value 'current' to false for $vendor, to make it more rock!
82
  if ( 'current' == $vendor ) {
@@ -88,93 +90,81 @@ if ( ! class_exists( 'YITH_Vendor' ) ) {
88
 
89
  // get vendor of actual user if nothind passed
90
  if ( false === $vendor ) {
91
- $vendor = get_user_meta( get_current_user_id(), self::$_usermetaKey, true );
92
- }
93
-
94
- // Get Vendor ID by user ID passed by $vendor and set the getter to 'vendor'
95
  else {
96
- $vendor = get_user_meta( $vendor, self::$_usermetaKey, true );
97
  }
98
 
99
  $obj = 'vendor';
100
- }
101
-
102
- // Get by product
103
  elseif ( 'product' == $obj ) {
104
 
105
  // get vendor of actual product if nothind passed
106
  if ( false === $vendor ) {
107
  global $post;
108
  $vendor = $post->ID;
109
- }
110
- elseif ( $vendor instanceof WP_Post ) {
111
  $vendor = $vendor->ID;
112
- }
113
- elseif ( $vendor instanceof WC_Product ) {
114
  $vendor = $vendor->id;
115
  }
116
 
117
- $terms = wp_get_post_terms( $vendor, self::$taxonomy );
118
 
119
  if ( empty( $terms ) ) {
120
- return self::_instance();
121
  }
122
 
123
- $vendor_term = array_shift( $terms );
124
- $vendor_id = $vendor_term->term_id;
125
-
126
- return self::_instance( $vendor_id, $vendor_term );
127
 
 
128
  }
129
 
130
  // exit if any object is retrieved
131
  if ( empty( $vendor ) ) {
132
- return self::_instance();
133
  }
134
 
135
  // RETRIEVE OBJECT
136
  // Get vendor by Vendor ID
137
- if ( is_numeric( $vendor ) && 'vendor' == $obj ) {
138
- $vendor_id = absint( $vendor );
139
- $vendor_term = get_term_by( 'term_id', $vendor_id, self::$taxonomy );
140
- }
141
-
142
- // get vendor by Vendor slug or name
143
- elseif ( is_string( $vendor ) ) {
144
- $vendor_term = get_term_by( 'slug', $vendor, self::$taxonomy );
145
- if ( empty( $vendor_term ) || is_wp_error( $vendor_term ) ) {
146
- $vendor_term = get_term_by( 'name', $vendor, self::$taxonomy );
147
  }
148
- if ( empty( $vendor_term ) || is_wp_error( $vendor_term ) ) {
149
- return self::_instance();
150
  }
151
  $vendor_id = $vendor_term->term_id;
152
- }
153
-
154
- // get vendor by object vendor
155
  elseif ( $vendor instanceof YITH_Vendor ) {
156
- $vendor_id = absint( $vendor->id );
157
  $vendor_term = $vendor->term;
158
- return self::_instance( $vendor_id, $vendor_term );
159
- }
160
 
161
- // get vendor by term object
162
- elseif ( isset( $vendor->slug ) && term_exists( $vendor->slug, self::$taxonomy ) ) {
163
- $vendor_id = absint( $vendor->term_id );
 
164
  $vendor_term = $vendor;
165
- }
166
-
167
- // no vendor found
168
  else {
169
- return self::_instance();
170
  }
171
 
172
  // return false is there is a term associated
173
  if ( empty( $vendor_term ) ) {
174
- return self::_instance();
175
  }
176
 
177
- return self::_instance( $vendor_id, $vendor_term );
178
  }
179
 
180
  /**
@@ -185,12 +175,12 @@ if ( ! class_exists( 'YITH_Vendor' ) ) {
185
  *
186
  * @return mixed
187
  */
188
- protected static function _instance( $vendor_id = 0, $vendor_term = null ) {
189
- if ( is_null( self::$_instance ) || ! isset( self::$_instance[$vendor_id] ) ) {
190
- self::$_instance[$vendor_id] = new self( $vendor_id, $vendor_term );
191
  }
192
 
193
- return self::$_instance[$vendor_id];
194
  }
195
 
196
  /**
@@ -202,7 +192,7 @@ if ( ! class_exists( 'YITH_Vendor' ) ) {
202
  * @return YITH_Vendor The current object
203
  *
204
  */
205
- public function __construct( $vendor_id = 0, $term = null ) {
206
  if ( empty( $vendor_id ) || empty( $term ) ) {
207
  return;
208
  }
@@ -210,7 +200,8 @@ if ( ! class_exists( 'YITH_Vendor' ) ) {
210
  $this->id = $vendor_id;
211
  $this->term = $term;
212
 
213
- $this->_populate();
 
214
  return $this;
215
  }
216
 
@@ -219,13 +210,13 @@ if ( ! class_exists( 'YITH_Vendor' ) ) {
219
  *
220
  * @since 1.0
221
  */
222
- protected function _populate() {
223
  $this->name = $this->term->name;
224
  $this->slug = $this->term->slug;
225
  $this->description = $this->term->description;
226
 
227
- $this->_changed = array();
228
- add_action( 'shutdown', array( $this, 'save_data' ), 10 );
229
  }
230
 
231
  /**
@@ -235,17 +226,17 @@ if ( ! class_exists( 'YITH_Vendor' ) ) {
235
  *
236
  * @return mixed
237
  */
238
- public function __get( $key ) {
239
- if ( isset( $this->_changed[$key] ) ) {
240
- return $this->_changed[$key];
241
  }
242
 
243
- $value = get_woocommerce_term_meta( $this->id, $key );
244
 
245
  // defaults
246
- $defaults = array(
247
  'payment_type' => 'instant',
248
- 'threshold' => 50
249
  );
250
  foreach ( $defaults as $std_key => $std_value ) {
251
  $key == $std_key && ! isset( $this->$key ) && $value = $std_value;
@@ -253,29 +244,21 @@ if ( ! class_exists( 'YITH_Vendor' ) ) {
253
 
254
  // Get values or default if not set
255
  if ( 'admins' === $key ) {
256
- $value = $this->get_admins();
257
- }
258
-
259
- else {
260
  if ( 'taxonomy' === $key ) {
261
  $value = self::$taxonomy;
262
- }
263
-
264
- else {
265
  if ( 'socials' === $key && empty( $value ) ) {
266
- $value = array();
267
- }
268
-
269
- else {
270
  if ( 'registration_date' === $key && empty( $value ) ) {
271
- $owner_id = $this->get_owner();
272
  if ( ! empty( $owner_id ) ) {
273
- $owner = get_user_by( 'id', $owner_id );
274
  $value = $owner->user_registered;
275
  }
276
- }
277
-
278
- else {
279
  if ( isset( $this->term->$key ) ) {
280
  $value = $this->term->$key;
281
  }
@@ -293,46 +276,43 @@ if ( ! class_exists( 'YITH_Vendor' ) ) {
293
  * @param mixed $property
294
  * @param mixed $value
295
  */
296
- public function __set( $property, $value ) {
297
  if ( $this->_changed === false ) {
298
  return;
299
  }
300
 
301
  if ( $value === true ) {
302
  $value = 'yes';
303
- }
304
-
305
- elseif ( $value === false ) {
306
  $value = 'no';
307
  }
308
 
309
- $this->_changed[$property] = $value;
310
  }
311
 
312
  /**
313
  * Save data function.
314
  */
315
- public function save_data() {
316
- if ( ! $this->is_valid() || empty( $this->_changed ) ) {
317
  return;
318
  }
319
 
320
  // save the property to change in the term
321
- $term_properties = array();
322
 
323
  foreach ( $this->_changed as $property => $value ) {
324
- $value = ! is_array( $value ) ? wc_clean( $value ) : $value;
325
- if ( in_array( $property, array( 'name', 'slug', 'description' ) ) ) {
326
- $term_properties[$property] = $value;
327
- }
328
- else {
329
- update_woocommerce_term_meta( $this->id, $property, $value );
330
  }
331
  }
332
 
333
  // save the term data
334
  if ( ! empty( $term_properties ) ) {
335
- wp_update_term( $this->id, self::$taxonomy, $term_properties );
336
  }
337
  }
338
 
@@ -343,8 +323,8 @@ if ( ! class_exists( 'YITH_Vendor' ) ) {
343
  *
344
  * @return bool
345
  */
346
- public function __isset( $key ) {
347
- return isset( $this->term->$key ) ? true : metadata_exists( 'woocommerce_term', $this->id, $key );
348
  }
349
 
350
  /**
@@ -352,11 +332,12 @@ if ( ! class_exists( 'YITH_Vendor' ) ) {
352
  *
353
  * @Author Andrea Grillo <andrea.grillo@yithemes.com>
354
  * @return string The vendor commission
355
- * @fire yith_vendor_commission filter
356
  */
357
- public function get_commission( $product_id = false ) {
358
- $base_commission = YITH_Vendors()->get_base_commission();
359
- return apply_filters( 'yith_vendor_commission', $base_commission, $this->id, $this );
 
360
  }
361
 
362
  /**
@@ -367,9 +348,10 @@ if ( ! class_exists( 'YITH_Vendor' ) ) {
367
  *
368
  * @return mixed
369
  */
370
- public function get_setting( $key, $default = false ) {
371
- $settings = get_option( 'yit_vendor_' . $this->id . '_options' );
372
- return isset( $settings[$key] ) ? wc_clean( $settings[$key] ) : $default;
 
373
  }
374
 
375
  /**
@@ -379,17 +361,17 @@ if ( ! class_exists( 'YITH_Vendor' ) ) {
379
  * @since 1.0
380
  * @author Andrea Grillo <andrea.grillo@yithemes.com>
381
  */
382
- public function get_owner() {
383
- $args = array(
384
  'meta_key' => self::$_usermetaOwner,
385
  'meta_value' => $this->id,
386
  'meta_compare' => '=',
387
  'fields' => 'ids',
388
- 'number' => 1
389
  );
390
- $owner = get_users( $args );
391
 
392
- return ! empty( $owner ) ? array_shift( $owner ) : 0;
393
  }
394
 
395
  /**
@@ -399,14 +381,14 @@ if ( ! class_exists( 'YITH_Vendor' ) ) {
399
  * @since 1.0
400
  * @author Andrea Grillo <andrea.grillo@yithemes.com>
401
  */
402
- public function get_admins() {
403
- $args = array(
404
  'meta_key' => self::$_usermetaKey,
405
  'meta_value' => $this->id,
406
  'meta_compare' => '=',
407
- 'fields' => 'ids'
408
  );
409
- $admins = get_users( $args );
410
 
411
  return $admins;
412
  }
@@ -419,13 +401,13 @@ if ( ! class_exists( 'YITH_Vendor' ) ) {
419
  * @return bool
420
  * @since 1.0
421
  */
422
- public function is_super_user( $user_id = false ) {
423
  if ( ! $user_id ) {
424
- $user_id = get_current_user_id();
425
  }
426
 
427
  // if the user is shop manager or administrator, return true
428
- return user_can( $user_id, 'manage_woocommerce' );
429
  }
430
 
431
  /**
@@ -436,17 +418,17 @@ if ( ! class_exists( 'YITH_Vendor' ) ) {
436
  * @return bool
437
  * @since 1.0
438
  */
439
- public function is_user_admin( $user_id = false ) {
440
  if ( ! $user_id ) {
441
- $user_id = get_current_user_id();
442
  }
443
 
444
  // if the user is shop manager or administrator, return true
445
- if ( $this->is_super_user( $user_id ) ) {
446
  return true;
447
  }
448
 
449
- foreach ( $this->get_admins() as $admin_id ) {
450
  if ( $admin_id == $user_id ) {
451
  return true;
452
  }
@@ -463,8 +445,8 @@ if ( ! class_exists( 'YITH_Vendor' ) ) {
463
  * @return bool
464
  * @since 1.0
465
  */
466
- public function has_limited_access( $user_id = false ) {
467
- return (bool) ( ! $this->is_super_user( $user_id ) && $this->is_user_admin( $user_id ) );
468
  }
469
 
470
  /**
@@ -474,16 +456,16 @@ if ( ! class_exists( 'YITH_Vendor' ) ) {
474
  *
475
  * @return array
476
  */
477
- public function get_query_products_args( $extra = array() ) {
478
- return wp_parse_args( $extra, array(
479
  'post_type' => 'product',
480
- 'tax_query' => array(
481
- array(
482
  'taxonomy' => self::$taxonomy,
483
  'field' => 'id',
484
- 'terms' => $this->id
485
- )
486
- )
487
  ) );
488
  }
489
 
@@ -494,15 +476,16 @@ if ( ! class_exists( 'YITH_Vendor' ) ) {
494
  *
495
  * @return array
496
  */
497
- public function get_products( $extra = array() ) {
498
- $args = wp_parse_args( $extra, array(
499
  'posts_per_page' => - 1,
500
- 'fields' => 'ids'
501
  )
502
  );
503
 
504
- $args = $this->get_query_products_args( $args );
505
- return get_posts( $args );
 
506
  }
507
 
508
  /**
@@ -514,7 +497,7 @@ if ( ! class_exists( 'YITH_Vendor' ) ) {
514
  * @author Antonino Scarfi <antonino.scarfi@yithemes.com>
515
  * @return bool
516
  */
517
- public function is_valid() {
518
  return ! empty( $this->id ) && ! empty( $this->term );
519
  }
520
 
@@ -527,12 +510,12 @@ if ( ! class_exists( 'YITH_Vendor' ) ) {
527
  * @author Antonino Scarfi <antonino.scarfi@yithemes.com>
528
  * @return bool
529
  */
530
- public function is_owner( $user_id = false ) {
531
  if ( ! $user_id ) {
532
- $user_id = get_current_user_id();
533
  }
534
 
535
- return get_user_meta( $user_id, self::$_usermetaOwner, true ) == $this->id;
536
  }
537
 
538
  /**
@@ -542,23 +525,22 @@ if ( ! class_exists( 'YITH_Vendor' ) ) {
542
  *
543
  * @return string
544
  */
545
- public function get_url( $context = 'frontend' ) {
546
  $url = '';
547
 
548
  if ( 'frontend' == $context ) {
549
- $url = get_term_link( $this->term, self::$taxonomy );
550
 
551
- if ( $url && is_wp_error( $url ) ) {
552
  $url = false;
553
  }
554
- }
555
- else {
556
  if ( 'admin' == $context ) {
557
- $url = get_edit_term_link( $this->id, self::$taxonomy );
558
  }
559
  }
560
 
561
- return apply_filters( 'yith_vendor_url', $url, $this, $context );
562
  }
563
 
564
  /**
@@ -566,16 +548,16 @@ if ( ! class_exists( 'YITH_Vendor' ) ) {
566
  *
567
  * @return array|null
568
  */
569
- public function get_unpaid_commissions( $extra_args = array() ) {
570
- $args = array(
571
  'vendor_id' => $this->id,
572
  'order_id' => '', // useful when is set the order as completed from orders list, because it set "order_id" in the query string
573
- 'status' => 'unpaid'
574
  );
575
 
576
- $args = wp_parse_args( $extra_args, $args );
577
 
578
- return YITH_Commissions()->get_commissions( $args );
579
  }
580
 
581
  /**
@@ -583,18 +565,18 @@ if ( ! class_exists( 'YITH_Vendor' ) ) {
583
  *
584
  * @return array|null
585
  */
586
- public function get_unpaid_commissions_if_out_threshold() {
587
- if ( $this->get_unpaid_commissions_amount() < $this->threshold ) {
588
- return array();
589
  }
590
 
591
- $args = array(
592
  'vendor_id' => $this->id,
593
  'order_id' => '', // useful when is set the order as completed from orders list, because it set "order_id" in the query string
594
- 'status' => 'unpaid'
595
  );
596
 
597
- return YITH_Commissions()->get_commissions( $args );
598
  }
599
 
600
  /**
@@ -602,10 +584,11 @@ if ( ! class_exists( 'YITH_Vendor' ) ) {
602
  *
603
  * @return float
604
  */
605
- public function get_unpaid_commissions_amount() {
606
  global $wpdb;
607
- $amount = $wpdb->get_var( $wpdb->prepare( "SELECT SUM(amount) FROM $wpdb->commissions WHERE status = %s AND vendor_id = %d", 'unpaid', $this->id ) );
608
- return floatval( $amount );
 
609
  }
610
 
611
  /**
@@ -613,19 +596,18 @@ if ( ! class_exists( 'YITH_Vendor' ) ) {
613
  *
614
  * @return array
615
  */
616
- public function commissions_to_pay() {
617
  if ( empty( $this->paypal_email ) ) {
618
- return array();
619
  }
620
 
621
- $commissions = array();
622
 
623
  if ( 'threshold' == $this->payment_type ) {
624
- $commissions = $this->get_unpaid_commissions_if_out_threshold(); // could be empty
625
- }
626
- else {
627
  if ( 'instant' == $this->payment_type ) {
628
- $commissions = $this->get_unpaid_commissions();
629
  }
630
  }
631
 
@@ -641,22 +623,19 @@ if ( ! class_exists( 'YITH_Vendor' ) ) {
641
  *
642
  * @return string The registration date
643
  */
644
- public function get_registration_date( $context = '', $format = '', $gmt = false ) {
645
  $registration_date = $gmt ? $this->registration_date_gmt : $this->registration_date;
646
 
647
  if ( 'timestamp' == $context ) {
648
- return mysql2date( 'U', $registration_date );
649
- }
650
-
651
- else {
652
  if ( 'display' == $context ) {
653
  if ( empty( $format ) ) {
654
- $format = get_option( 'date_format' );
655
  }
656
- return mysql2date( $format, $registration_date );
657
- }
658
 
659
- else {
 
660
  return $registration_date;
661
  }
662
  }
@@ -667,18 +646,19 @@ if ( ! class_exists( 'YITH_Vendor' ) ) {
667
  *
668
  * @return array The order ids
669
  */
670
- public function get_orders( $type = 'all', $status = false ) {
671
  global $wpdb;
672
- $query = $wpdb->prepare( "SELECT DISTINCT order_id FROM {$wpdb->commissions} WHERE vendor_id = %d", $this->id );
673
 
674
  if ( 'suborder' == $type ) {
675
- $query = $wpdb->prepare( "SELECT DISTINCT ID FROM {$wpdb->posts} WHERE post_parent!=%d AND post_type=%s AND post_author=%d", 0, 'shop_order', $this->get_owner() );
676
  if ( $status ) {
677
- $query .= $wpdb->prepare( " AND post_status=%s", $status );
678
  }
679
  }
680
 
681
- $order_id = $wpdb->get_col( $query );
 
682
  return $order_id;
683
  }
684
 
@@ -687,28 +667,36 @@ if ( ! class_exists( 'YITH_Vendor' ) ) {
687
  *
688
  * @return array The review average and the product with reviews count
689
  */
690
- public function get_reviews_average_and_product() {
 
 
 
 
 
 
691
  $average_rating = $reviews_product_count = $average = 0;
692
- $product_ids = $this->get_products();
693
 
694
  foreach ( $product_ids as $product_id ) {
695
- $product = wc_get_product( $product_id );
696
- $product_review_count = $product->get_review_count();
697
 
698
  if ( ! empty( $product_review_count ) ) {
699
  $reviews_product_count ++;
700
  }
701
- $average += $product->get_average_rating();
702
  }
703
 
704
  if ( ! empty( $reviews_product_count ) ) {
705
- $average_rating = number_format( $average / $reviews_product_count, 2 );
706
  }
707
 
708
- return array(
709
  'average_rating' => $average_rating,
710
- 'reviews_product_count' => $reviews_product_count
711
  );
 
 
712
  }
713
 
714
  /**
@@ -716,33 +704,33 @@ if ( ! class_exists( 'YITH_Vendor' ) ) {
716
  *
717
  * @return array The review average and the product with reviews count
718
  */
719
- public function email_order_items_table( $order, $show_download_links = false, $show_sku = false, $show_purchase_note = false, $show_image = false, $image_size = array( 32, 32 ), $plain_text = false ) {
720
 
721
- ob_start();
722
 
723
  $template = $plain_text ? 'emails/plain/vendor-email-order-items.php' : 'emails/vendor-email-order-items.php';
724
 
725
- yith_wcpv_get_template( $template, array(
726
  'order' => $order,
727
  'vendor' => $this,
728
- 'items' => $order->get_items(),
729
  'show_download_links' => $show_download_links,
730
  'show_sku' => $show_sku,
731
  'show_purchase_note' => $show_purchase_note,
732
  'show_image' => $show_image,
733
- 'image_size' => $image_size
734
  ), '' );
735
 
736
- return ob_get_clean();
737
  }
738
 
739
- public function is_on_vacation(){
740
- return function_exists( 'YITH_Vendor_Vacation' ) ? YITH_Vendor_Vacation()->vendor_is_on_vacation( $this ) : false;
741
  }
742
  }
743
  }
744
 
745
- if ( ! function_exists( 'yith_get_vendor' ) ) {
746
  /**
747
  * Main instance of plugin
748
  *
@@ -753,7 +741,7 @@ if ( ! function_exists( 'yith_get_vendor' ) ) {
753
  * @since 1.0
754
  * @author Andrea Grillo <andrea.grillo@yithemes.com>
755
  */
756
- function yith_get_vendor( $vendor = false, $obj = 'vendor' ) {
757
- return YITH_Vendor::retrieve( $vendor, $obj );
758
  }
759
  }
7
  * It is also available through the world-wide-web at this URL:
8
  * http://www.gnu.org/licenses/gpl-3.0.txt
9
  */
10
+ if ( ! defined ( 'ABSPATH' ) ) {
11
  exit( 'Direct access forbidden.' );
12
  }
13
 
14
+ if ( ! class_exists ( 'YITH_Vendor' ) ) {
15
 
16
  /**
17
  * The main class for the Vendor
21
  * @since 1.0.0
22
  * @author Your Inspiration Themes
23
  *
24
+ * @property string $paypal_email
25
+ * @property string $enable_selling
26
+ * @property string $payment_type
27
+ * @property string $threshold
28
+ * @property string $registration_date
29
+ * @property string $registration_date_gmt
30
+ * @property array $admins
31
+ * @property string $vacation_selling
32
+ * @property string $vacation_message
33
+ * @property string $vacation_start_date
34
+ * @property string $name
35
+ * @property string $vacation_end_date
36
+ * @property string $show_gravatar
37
+ * @property int|string $commission
38
+ * @property string $vat
39
  */
40
  class YITH_Vendor {
41
 
42
+
43
  /** @public int The vendor ID. */
44
  public $id = 0;
45
 
62
  * Main Instance
63
  *
64
  * @var string
65
+ * @since 1.0
66
  * @access protected
67
  */
68
  protected static $_instance = null;
75
  *
76
  * @return bool|YITH_Vendor
77
  */
78
+ public static function retrieve ( $vendor = false, $obj = 'vendor' ) {
79
+ self::$_usermetaKey = YITH_Vendors ()->get_user_meta_key ();
80
+ self::$_usermetaOwner = YITH_Vendors ()->get_user_meta_owner ();
81
+ self::$taxonomy = YITH_Vendors ()->get_taxonomy_name ();
82
 
83
  // change value 'current' to false for $vendor, to make it more rock!
84
  if ( 'current' == $vendor ) {
90
 
91
  // get vendor of actual user if nothind passed
92
  if ( false === $vendor ) {
93
+ $vendor = get_user_meta ( get_current_user_id (), self::$_usermetaKey, true );
94
+ } // Get Vendor ID by user ID passed by $vendor and set the getter to 'vendor'
 
 
95
  else {
96
+ $vendor = get_user_meta ( $vendor, self::$_usermetaKey, true );
97
  }
98
 
99
  $obj = 'vendor';
100
+ } // Get by product
 
 
101
  elseif ( 'product' == $obj ) {
102
 
103
  // get vendor of actual product if nothind passed
104
  if ( false === $vendor ) {
105
  global $post;
106
  $vendor = $post->ID;
107
+ } elseif ( $vendor instanceof WP_Post ) {
 
108
  $vendor = $vendor->ID;
109
+ } elseif ( $vendor instanceof WC_Product ) {
 
110
  $vendor = $vendor->id;
111
  }
112
 
113
+ $terms = wp_get_post_terms ( $vendor, self::$taxonomy );
114
 
115
  if ( empty( $terms ) ) {
116
+ return self::_instance ();
117
  }
118
 
119
+ /* WPML SUPPORT */
120
+ $vendor_term = array_shift ( $terms );
121
+ $default_language = function_exists( 'wpml_get_default_language' ) ? wpml_get_default_language() : null;
122
+ $vendor_id = yit_wpml_object_id( $vendor_term->term_id, YITH_Vendors()->get_taxonomy_name(), true, $default_language );
123
 
124
+ return self::_instance ( $vendor_id, $vendor_term );
125
  }
126
 
127
  // exit if any object is retrieved
128
  if ( empty( $vendor ) ) {
129
+ return self::_instance ();
130
  }
131
 
132
  // RETRIEVE OBJECT
133
  // Get vendor by Vendor ID
134
+ if ( is_numeric ( $vendor ) && 'vendor' == $obj ) {
135
+ $vendor_id = absint ( $vendor );
136
+ $vendor_term = get_term_by ( 'term_id', $vendor_id, self::$taxonomy );
137
+ } // get vendor by Vendor slug or name
138
+ elseif ( is_string ( $vendor ) ) {
139
+ $vendor_term = get_term_by ( 'slug', $vendor, self::$taxonomy );
140
+ if ( empty( $vendor_term ) || is_wp_error ( $vendor_term ) ) {
141
+ $vendor_term = get_term_by ( 'name', $vendor, self::$taxonomy );
 
 
142
  }
143
+ if ( empty( $vendor_term ) || is_wp_error ( $vendor_term ) ) {
144
+ return self::_instance ();
145
  }
146
  $vendor_id = $vendor_term->term_id;
147
+ } // get vendor by object vendor
 
 
148
  elseif ( $vendor instanceof YITH_Vendor ) {
149
+ $vendor_id = absint ( $vendor->id );
150
  $vendor_term = $vendor->term;
 
 
151
 
152
+ return self::_instance ( $vendor_id, $vendor_term );
153
+ } // get vendor by term object
154
+ elseif ( isset( $vendor->slug ) && term_exists ( $vendor->slug, self::$taxonomy ) ) {
155
+ $vendor_id = absint ( $vendor->term_id );
156
  $vendor_term = $vendor;
157
+ } // no vendor found
 
 
158
  else {
159
+ return self::_instance ();
160
  }
161
 
162
  // return false is there is a term associated
163
  if ( empty( $vendor_term ) ) {
164
+ return self::_instance ();
165
  }
166
 
167
+ return self::_instance ( $vendor_id, $vendor_term );
168
  }
169
 
170
  /**
175
  *
176
  * @return mixed
177
  */
178
+ protected static function _instance ( $vendor_id = 0, $vendor_term = null ) {
179
+ if ( is_null ( self::$_instance ) || ! isset( self::$_instance[ $vendor_id ] ) ) {
180
+ self::$_instance[ $vendor_id ] = new self( $vendor_id, $vendor_term );
181
  }
182
 
183
+ return self::$_instance[ $vendor_id ];
184
  }
185
 
186
  /**
192
  * @return YITH_Vendor The current object
193
  *
194
  */
195
+ public function __construct ( $vendor_id = 0, $term = null ) {
196
  if ( empty( $vendor_id ) || empty( $term ) ) {
197
  return;
198
  }
200
  $this->id = $vendor_id;
201
  $this->term = $term;
202
 
203
+ $this->_populate ();
204
+
205
  return $this;
206
  }
207
 
210
  *
211
  * @since 1.0
212
  */
213
+ protected function _populate () {
214
  $this->name = $this->term->name;
215
  $this->slug = $this->term->slug;
216
  $this->description = $this->term->description;
217
 
218
+ $this->_changed = array ();
219
+ add_action ( 'shutdown', array ( $this, 'save_data' ), 10 );
220
  }
221
 
222
  /**
226
  *
227
  * @return mixed
228
  */
229
+ public function __get ( $key ) {
230
+ if ( isset( $this->_changed[ $key ] ) ) {
231
+ return $this->_changed[ $key ];
232
  }
233
 
234
+ $value = get_woocommerce_term_meta ( $this->id, $key );
235
 
236
  // defaults
237
+ $defaults = array (
238
  'payment_type' => 'instant',
239
+ 'threshold' => 50,
240
  );
241
  foreach ( $defaults as $std_key => $std_value ) {
242
  $key == $std_key && ! isset( $this->$key ) && $value = $std_value;
244
 
245
  // Get values or default if not set
246
  if ( 'admins' === $key ) {
247
+ $value = $this->get_admins ();
248
+ } else {
 
 
249
  if ( 'taxonomy' === $key ) {
250
  $value = self::$taxonomy;
251
+ } else {
 
 
252
  if ( 'socials' === $key && empty( $value ) ) {
253
+ $value = array ();
254
+ } else {
 
 
255
  if ( 'registration_date' === $key && empty( $value ) ) {
256
+ $owner_id = $this->get_owner ();
257
  if ( ! empty( $owner_id ) ) {
258
+ $owner = get_user_by ( 'id', $owner_id );
259
  $value = $owner->user_registered;
260
  }
261
+ } else {
 
 
262
  if ( isset( $this->term->$key ) ) {
263
  $value = $this->term->$key;
264
  }
276
  * @param mixed $property
277
  * @param mixed $value
278
  */
279
+ public function __set ( $property, $value ) {
280
  if ( $this->_changed === false ) {
281
  return;
282
  }
283
 
284
  if ( $value === true ) {
285
  $value = 'yes';
286
+ } elseif ( $value === false ) {
 
 
287
  $value = 'no';
288
  }
289
 
290
+ $this->_changed[ $property ] = $value;
291
  }
292
 
293
  /**
294
  * Save data function.
295
  */
296
+ public function save_data () {
297
+ if ( ! $this->is_valid () || empty( $this->_changed ) ) {
298
  return;
299
  }
300
 
301
  // save the property to change in the term
302
+ $term_properties = array ();
303
 
304
  foreach ( $this->_changed as $property => $value ) {
305
+ $value = ! is_array ( $value ) ? wc_clean ( $value ) : $value;
306
+ if ( in_array ( $property, array ( 'name', 'slug', 'description' ) ) ) {
307
+ $term_properties[ $property ] = $value;
308
+ } else {
309
+ update_woocommerce_term_meta ( $this->id, $property, $value );
 
310
  }
311
  }
312
 
313
  // save the term data
314
  if ( ! empty( $term_properties ) ) {
315
+ wp_update_term ( $this->id, self::$taxonomy, $term_properties );
316
  }
317
  }
318
 
323
  *
324
  * @return bool
325
  */
326
+ public function __isset ( $key ) {
327
+ return isset( $this->term->$key ) ? true : metadata_exists ( 'woocommerce_term', $this->id, $key );
328
  }
329
 
330
  /**
332
  *
333
  * @Author Andrea Grillo <andrea.grillo@yithemes.com>
334
  * @return string The vendor commission
335
+ * @fire yith_vendor_commission filter
336
  */
337
+ public function get_commission ( $product_id = false ) {
338
+ $base_commission = YITH_Vendors ()->get_base_commission ();
339
+
340
+ return apply_filters ( 'yith_vendor_commission', $base_commission, $this->id, $this );
341
  }
342
 
343
  /**
348
  *
349
  * @return mixed
350
  */
351
+ public function get_setting ( $key, $default = false ) {
352
+ $settings = get_option ( 'yit_vendor_' . $this->id . '_options' );
353
+
354
+ return isset( $settings[ $key ] ) ? wc_clean ( $settings[ $key ] ) : $default;
355
  }
356
 
357
  /**
361
  * @since 1.0
362
  * @author Andrea Grillo <andrea.grillo@yithemes.com>
363
  */
364
+ public function get_owner () {
365
+ $args = array (
366
  'meta_key' => self::$_usermetaOwner,
367
  'meta_value' => $this->id,
368
  'meta_compare' => '=',
369
  'fields' => 'ids',
370
+ 'number' => 1,
371
  );
372
+ $owner = get_users ( $args );
373
 
374
+ return ! empty( $owner ) ? array_shift ( $owner ) : 0;
375
  }
376
 
377
  /**
381
  * @since 1.0
382
  * @author Andrea Grillo <andrea.grillo@yithemes.com>
383
  */
384
+ public function get_admins () {
385
+ $args = array (
386
  'meta_key' => self::$_usermetaKey,
387
  'meta_value' => $this->id,
388
  'meta_compare' => '=',
389
+ 'fields' => 'ids',
390
  );
391
+ $admins = get_users ( $args );
392
 
393
  return $admins;
394
  }
401
  * @return bool
402
  * @since 1.0
403
  */
404
+ public function is_super_user ( $user_id = false ) {
405
  if ( ! $user_id ) {
406
+ $user_id = get_current_user_id ();
407
  }
408
 
409
  // if the user is shop manager or administrator, return true
410
+ return user_can ( $user_id, 'manage_woocommerce' );
411
  }
412
 
413
  /**
418
  * @return bool
419
  * @since 1.0
420
  */
421
+ public function is_user_admin ( $user_id = false ) {
422
  if ( ! $user_id ) {
423
+ $user_id = get_current_user_id ();
424
  }
425
 
426
  // if the user is shop manager or administrator, return true
427
+ if ( $this->is_super_user ( $user_id ) ) {
428
  return true;
429
  }
430
 
431
+ foreach ( $this->get_admins () as $admin_id ) {
432
  if ( $admin_id == $user_id ) {
433
  return true;
434
  }
445
  * @return bool
446
  * @since 1.0
447
  */
448
+ public function has_limited_access ( $user_id = false ) {
449
+ return (bool)( ! $this->is_super_user ( $user_id ) && $this->is_user_admin ( $user_id ) );
450
  }
451
 
452
  /**
456
  *
457
  * @return array
458
  */
459
+ public function get_query_products_args ( $extra = array () ) {
460
+ return wp_parse_args ( $extra, array (
461
  'post_type' => 'product',
462
+ 'tax_query' => array (
463
+ array (
464
  'taxonomy' => self::$taxonomy,
465
  'field' => 'id',
466
+ 'terms' => $this->id,
467
+ ),
468
+ ),
469
  ) );
470
  }
471
 
476
  *
477
  * @return array
478
  */
479
+ public function get_products ( $extra = array () ) {
480
+ $args = wp_parse_args ( $extra, array (
481
  'posts_per_page' => - 1,
482
+ 'fields' => 'ids',
483
  )
484
  );
485
 
486
+ $args = $this->get_query_products_args ( $args );
487
+
488
+ return get_posts ( $args );
489
  }
490
 
491
  /**
497
  * @author Antonino Scarfi <antonino.scarfi@yithemes.com>
498
  * @return bool
499
  */
500
+ public function is_valid () {
501
  return ! empty( $this->id ) && ! empty( $this->term );
502
  }
503
 
510
  * @author Antonino Scarfi <antonino.scarfi@yithemes.com>
511
  * @return bool
512
  */
513
+ public function is_owner ( $user_id = false ) {
514
  if ( ! $user_id ) {
515
+ $user_id = get_current_user_id ();
516
  }
517
 
518
+ return get_user_meta ( $user_id, self::$_usermetaOwner, true ) == $this->id;
519
  }
520
 
521
  /**
525
  *
526
  * @return string
527
  */
528
+ public function get_url ( $context = 'frontend' ) {
529
  $url = '';
530
 
531
  if ( 'frontend' == $context ) {
532
+ $url = get_term_link ( $this->term, self::$taxonomy );
533
 
534
+ if ( $url && is_wp_error ( $url ) ) {
535
  $url = false;
536
  }
537
+ } else {
 
538
  if ( 'admin' == $context ) {
539
+ $url = get_edit_term_link ( $this->id, self::$taxonomy );
540
  }
541
  }
542
 
543
+ return apply_filters ( 'yith_vendor_url', $url, $this, $context );
544
  }
545
 
546
  /**
548
  *
549
  * @return array|null
550
  */
551
+ public function get_unpaid_commissions ( $extra_args = array () ) {
552
+ $args = array (
553
  'vendor_id' => $this->id,
554
  'order_id' => '', // useful when is set the order as completed from orders list, because it set "order_id" in the query string
555
+ 'status' => 'unpaid',
556
  );
557
 
558
+ $args = wp_parse_args ( $extra_args, $args );
559
 
560
+ return YITH_Commissions ()->get_commissions ( $args );
561
  }
562
 
563
  /**
565
  *
566
  * @return array|null
567
  */
568
+ public function get_unpaid_commissions_if_out_threshold () {
569
+ if ( $this->get_unpaid_commissions_amount () < $this->threshold ) {
570
+ return array ();
571
  }
572
 
573
+ $args = array (
574
  'vendor_id' => $this->id,
575
  'order_id' => '', // useful when is set the order as completed from orders list, because it set "order_id" in the query string
576
+ 'status' => 'unpaid',
577
  );
578
 
579
+ return YITH_Commissions ()->get_commissions ( $args );
580
  }
581
 
582
  /**
584
  *
585
  * @return float
586
  */
587
+ public function get_unpaid_commissions_amount () {
588
  global $wpdb;
589
+ $amount = $wpdb->get_var ( $wpdb->prepare ( "SELECT SUM(amount) FROM $wpdb->commissions WHERE status = %s AND vendor_id = %d", 'unpaid', $this->id ) );
590
+
591
+ return floatval ( $amount );
592
  }
593
 
594
  /**
596
  *
597
  * @return array
598
  */
599
+ public function commissions_to_pay () {
600
  if ( empty( $this->paypal_email ) ) {
601
+ return array ();
602
  }
603
 
604
+ $commissions = array ();
605
 
606
  if ( 'threshold' == $this->payment_type ) {
607
+ $commissions = $this->get_unpaid_commissions_if_out_threshold (); // could be empty
608
+ } else {
 
609
  if ( 'instant' == $this->payment_type ) {
610
+ $commissions = $this->get_unpaid_commissions ();
611
  }
612
  }
613
 
623
  *
624
  * @return string The registration date
625
  */
626
+ public function get_registration_date ( $context = '', $format = '', $gmt = false ) {
627
  $registration_date = $gmt ? $this->registration_date_gmt : $this->registration_date;
628
 
629
  if ( 'timestamp' == $context ) {
630
+ return mysql2date ( 'U', $registration_date );
631
+ } else {
 
 
632
  if ( 'display' == $context ) {
633
  if ( empty( $format ) ) {
634
+ $format = get_option ( 'date_format' );
635
  }
 
 
636
 
637
+ return mysql2date ( $format, $registration_date );
638
+ } else {
639
  return $registration_date;
640
  }
641
  }
646
  *
647
  * @return array The order ids
648
  */
649
+ public function get_orders ( $type = 'all', $status = false ) {
650
  global $wpdb;
651
+ $query = $wpdb->prepare ( "SELECT DISTINCT order_id FROM {$wpdb->commissions} WHERE vendor_id = %d", $this->id );
652
 
653
  if ( 'suborder' == $type ) {
654
+ $query = $wpdb->prepare ( "SELECT DISTINCT ID FROM {$wpdb->posts} WHERE post_parent!=%d AND post_type=%s AND post_author=%d", 0, 'shop_order', $this->get_owner () );
655
  if ( $status ) {
656
+ $query .= $wpdb->prepare ( " AND post_status=%s", $status );
657
  }
658
  }
659
 
660
+ $order_id = $wpdb->get_col ( $query );
661
+
662
  return $order_id;
663
  }
664
 
667
  *
668
  * @return array The review average and the product with reviews count
669
  */
670
+ public function get_reviews_average_and_product () {
671
+ $response = apply_filters ( 'yith_wcmv_reviews_average_and_product', array (), $this );
672
+
673
+ if ( ! empty( $response ) ) {
674
+ return $response;
675
+ }
676
+
677
  $average_rating = $reviews_product_count = $average = 0;
678
+ $product_ids = $this->get_products ();
679
 
680
  foreach ( $product_ids as $product_id ) {
681
+ $product = wc_get_product ( $product_id );
682
+ $product_review_count = $product->get_review_count ();
683
 
684
  if ( ! empty( $product_review_count ) ) {
685
  $reviews_product_count ++;
686
  }
687
+ $average += $product->get_average_rating ();
688
  }
689
 
690
  if ( ! empty( $reviews_product_count ) ) {
691
+ $average_rating = number_format ( $average / $reviews_product_count, 2 );
692
  }
693
 
694
+ $response = array (
695
  'average_rating' => $average_rating,
696
+ 'reviews_product_count' => $reviews_product_count,
697
  );
698
+
699
+ return $response;
700
  }
701
 
702
  /**
704
  *
705
  * @return array The review average and the product with reviews count
706
  */
707
+ public function email_order_items_table ( $order, $show_download_links = false, $show_sku = false, $show_purchase_note = false, $show_image = false, $image_size = array ( 32, 32 ), $plain_text = false ) {
708
 
709
+ ob_start ();
710
 
711
  $template = $plain_text ? 'emails/plain/vendor-email-order-items.php' : 'emails/vendor-email-order-items.php';
712
 
713
+ yith_wcpv_get_template ( $template, array (
714
  'order' => $order,
715
  'vendor' => $this,
716
+ 'items' => $order->get_items (),
717
  'show_download_links' => $show_download_links,
718
  'show_sku' => $show_sku,
719
  'show_purchase_note' => $show_purchase_note,
720
  'show_image' => $show_image,
721
+ 'image_size' => $image_size,
722
  ), '' );
723
 
724
+ return ob_get_clean ();
725
  }
726
 
727
+ public function is_on_vacation () {
728
+ return function_exists ( 'YITH_Vendor_Vacation' ) ? YITH_Vendor_Vacation ()->vendor_is_on_vacation ( $this ) : false;
729
  }
730
  }
731
  }
732
 
733
+ if ( ! function_exists ( 'yith_get_vendor' ) ) {
734
  /**
735
  * Main instance of plugin
736
  *
741
  * @since 1.0
742
  * @author Andrea Grillo <andrea.grillo@yithemes.com>
743
  */
744
+ function yith_get_vendor ( $vendor = false, $obj = 'vendor' ) {
745
+ return YITH_Vendor::retrieve ( $vendor, $obj );
746
  }
747
  }
modules/yith-woocommerce-product-vendors/includes/class.yith-vendors-admin.php CHANGED
@@ -7,7 +7,7 @@
7
  * It is also available through the world-wide-web at this URL:
8
  * http://www.gnu.org/licenses/gpl-3.0.txt
9
  */
10
- if ( ! defined( 'ABSPATH' ) ) {
11
  exit( 'Direct access forbidden.' );
12
  }
13
 
@@ -20,7 +20,7 @@ if ( ! defined( 'ABSPATH' ) ) {
20
  * @author Your Inspiration Themes
21
  *
22
  */
23
- if ( ! class_exists( 'YITH_Vendors_Admin' ) ) {
24
 
25
  class YITH_Vendors_Admin {
26
 
@@ -42,19 +42,19 @@ if ( ! class_exists( 'YITH_Vendors_Admin' ) ) {
42
  /**
43
  * @var string Official plugin documentation
44
  */
45
- protected $_official_documentation = 'http://yithemes.com/docs-plugins/yith-woocommerce-multi-vendor/' ;
46
 
47
  /**
48
  * @var string Official plugin landing page
49
  */
50
- protected $_premium_landing = 'http://yithemes.com/docs-plugins/yith-woocommerce-multi-vendor/' ;
51
 
52
  /**
53
  * @var string Official plugin landing page
54
  */
55
- protected $_premium_live = 'http://plugins.yithemes.com/yith-woocommerce-multi-vendor/' ;
56
 
57
- /**
58
  * @var string Vendor options role
59
  */
60
  protected $_vendor_role = 'manage_vendor_store';
@@ -64,17 +64,16 @@ if ( ! class_exists( 'YITH_Vendors_Admin' ) ) {
64
  * Construct
65
  */
66
  public function __construct() {
67
- $this->_taxonomy_name = YITH_Vendors()->get_taxonomy_name();
68
 
69
  /* Add Admin Body Class */
70
  add_filter( 'admin_body_class', array( $this, 'admin_body_class' ) );
71
 
72
- /* Panel Settings */
73
  add_action( 'admin_menu', array( $this, 'register_panel' ), 5 );
74
  add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
75
 
76
  /* Dashboard Menu */
77
- add_action( 'admin_menu', array( $this, 'remove_vendor_taxonomy_menu_item' ) );
78
  add_action( 'admin_menu', array( $this, 'add_new_link_check' ) );
79
 
80
  /* Plugin Informations */
@@ -82,47 +81,46 @@ if ( ! class_exists( 'YITH_Vendors_Admin' ) ) {
82
  add_filter( 'plugin_row_meta', array( $this, 'plugin_row_meta' ), 10, 4 );
83
  add_action( 'yith_wc_multi_vendor_premium_tab', array( $this, 'show_premium_tab' ) );
84
 
85
- /* Taxonomy management */
86
- add_action( $this->_taxonomy_name . '_add_form_fields', array( $this, 'add_taxonomy_fields' ), 1, 1 );
87
- add_action( $this->_taxonomy_name . '_edit_form_fields', array( $this, 'edit_taxonomy_fields' ), 1, 1 );
88
- add_filter( 'pre_insert_term', array( $this, 'check_duplicate_term_name' ), 10, 2 );
89
- add_filter( 'edit_terms', array( $this, 'check_duplicate_term_name' ), 10, 2 );
90
- add_action( 'edited_' . $this->_taxonomy_name, array( $this, 'save_taxonomy_fields' ), 10, 2 );
91
- add_action( 'created_' . $this->_taxonomy_name, array( $this, 'save_taxonomy_fields' ), 10, 2 );
92
- add_action( 'pre_delete_term', array( $this, 'remove_vendor_data' ), 10, 2 );
93
- add_action( 'add_meta_boxes', array( $this, 'single_value_taxonomy' ) );
94
 
95
  /* Taxonomy Table Management */
96
  add_filter( "bulk_actions-edit-{$this->_taxonomy_name}", '__return_empty_array' );
97
 
98
- /* Allow html in taxonomy descriptions */
99
- remove_filter( 'pre_term_description', 'wp_filter_kses' );
100
- remove_filter( 'term_description', 'wp_kses_data' );
101
 
102
- /* WooCommerce */
103
- add_action( 'pre_user_query', array( $this, 'json_search_customer_name' ), 15 );
104
 
105
- /* Vendor products management */
106
- add_filter( 'request', array( $this, 'filter_product_list' ) );
107
- add_filter( 'wp_count_posts', array( $this, 'vendor_count_posts' ), 10, 3 );
108
- add_action( 'save_post', array( $this, 'add_vendor_taxonomy_to_product' ), 10, 2 );
109
- add_action( 'current_screen', array( $this, 'disabled_manage_other_vendors_posts' ) );
110
 
111
- /* Grouped Products */
112
- add_action( 'pre_get_posts', array( $this, 'filter_vendor_linked_products' ), 10, 1 );
113
 
114
- /* Vendor media management */
115
- add_filter( 'ajax_query_attachments_args', array( $this, 'filter_vendor_media' ), 10, 1 );
116
 
117
- /* Vendor menu */
118
- add_action( 'admin_menu', array( $this, 'menu_items' ) );
119
- add_action( 'admin_menu', array( $this, 'remove_media_page' ) );
120
- add_action( 'admin_menu', array( $this, 'remove_dashboard_widgets' ) );
121
  add_action( 'admin_init', array( YITH_Vendors(), 'remove_wp_bar_admin_menu' ) );
122
  add_action( 'admin_bar_menu', array( $this, 'show_admin_bar_visit_store' ), 31 );
123
 
124
- /* Vendor information management */
125
- add_action( 'admin_action_yith_admin_save_fields', array( $this, 'save_taxonomy_fields' ) );
126
 
127
  /* Prevent WooCommerce Access Admin */
128
  add_filter( 'woocommerce_prevent_admin_access', array( $this, 'prevent_admin_access' ) );
@@ -140,8 +138,12 @@ if ( ! class_exists( 'YITH_Vendors_Admin' ) ) {
140
  /* Custom manage users columns. */
141
  add_filter( 'manage_users_columns', array( $this, 'manage_users_columns' ) );
142
  add_filter( 'manage_users_custom_column', array( $this, 'manage_users_custom_column' ), 10, 3 );
143
-
 
144
  add_action( 'yit_panel_wc_before_update', array( $this, 'manage_caps' ) );
 
 
 
145
  }
146
 
147
  /**
@@ -150,10 +152,11 @@ if ( ! class_exists( 'YITH_Vendors_Admin' ) ) {
150
  * @return void
151
  * @since 1.0
152
  * @author Andrea Grillo <andrea.grillo@yithemes.com>
153
- * @use /Yit_Plugin_Panel class
154
  * @see plugin-fw/lib/yit-plugin-panel.php
155
  */
156
  public function register_panel() {
 
157
 
158
  if ( ! empty( $this->_panel ) ) {
159
  return;
@@ -163,8 +166,7 @@ if ( ! class_exists( 'YITH_Vendors_Admin' ) ) {
163
  'commissions' => __( 'Commissions', 'yith_wc_product_vendors' ),
164
  'vendors' => __( 'Vendors', 'yith_wc_product_vendors' ),
165
  'premium' => __( 'Premium Version', 'yith_wc_product_vendors' ),
166
- )
167
- );
168
 
169
  $args = array(
170
  'create_menu_page' => true,
@@ -182,7 +184,7 @@ if ( ! class_exists( 'YITH_Vendors_Admin' ) ) {
182
 
183
  /* === Fixed: not updated theme/old plugin framework === */
184
  if ( ! class_exists( 'YIT_Plugin_Panel_WooCommerce' ) ) {
185
- require_once( 'plugin-fw/lib/yit-plugin-panel-wc.php' );
186
  }
187
 
188
  $this->_panel = new YIT_Plugin_Panel_WooCommerce( $args );
@@ -201,7 +203,7 @@ if ( ! class_exists( 'YITH_Vendors_Admin' ) ) {
201
  wp_enqueue_style( 'yith-wc-product-vendors-admin', YITH_WPV_ASSETS_URL . 'css/admin.css', array( 'jquery-chosen' ) );
202
 
203
  /* === Admin Menu Hack === */
204
- if( $this->is_vendor_tax_page() ){
205
  wp_enqueue_script( 'yith-wpv-tax-menu' );
206
  $css = '#adminmenu li#menu-posts-product > a:after{display:none;}
207
  #adminmenu .wp-has-current-submenu .wp-submenu{display: none;}
@@ -224,10 +226,19 @@ if ( ! class_exists( 'YITH_Vendors_Admin' ) ) {
224
  public function filter_product_list( $request ) {
225
  global $typenow;
226
 
227
- $vendor = yith_get_vendor( 'current', 'user' );
 
 
 
 
 
 
 
 
 
 
 
228
 
229
- if ( is_admin() && ! $vendor->is_super_user() && $vendor->is_user_admin() && 'product' == $typenow ) {
230
- $request[ $vendor->term->taxonomy ] = $vendor->slug;
231
  return apply_filters( "yith_wcmv_{$typenow}_request", $request );
232
  }
233
 
@@ -245,12 +256,12 @@ if ( ! class_exists( 'YITH_Vendors_Admin' ) ) {
245
  *
246
  * @return arr Modified request
247
  * @since 1.0
248
- * @use wp_post_count action
249
  */
250
  public function vendor_count_posts( $counts, $type, $perm ) {
251
- $vendor = yith_get_vendor( 'current', 'user' );
252
 
253
- if ( ! $vendor || 'product' != $type || $vendor->is_super_user() || ! $vendor->is_user_admin() ) {
254
  return $counts;
255
  }
256
 
@@ -261,8 +272,8 @@ if ( ! class_exists( 'YITH_Vendors_Admin' ) ) {
261
 
262
  // Update count object
263
  foreach ( $stati as $status ) {
264
- $posts = $vendor->get_products( "post_status=$status" );
265
- $counts->$status = count( $posts );
266
  }
267
 
268
  return $counts;
@@ -279,10 +290,25 @@ if ( ! class_exists( 'YITH_Vendors_Admin' ) ) {
279
  * @use save_post action
280
  */
281
  public function add_vendor_taxonomy_to_product( $post_id, $post ) {
282
- $vendor = yith_get_vendor( 'current', 'user' );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
283
 
284
- if ( 'product' == $post->post_type && current_user_can( 'edit_post', $post_id ) && $vendor->has_limited_access() ) {
285
- wp_set_object_terms( $post_id, $vendor->term->slug, $vendor->term->taxonomy, false );
286
  }
287
  }
288
 
@@ -295,16 +321,20 @@ if ( ! class_exists( 'YITH_Vendors_Admin' ) ) {
295
  * @use current_screen filter
296
  */
297
  public function disabled_manage_other_vendors_posts() {
298
- if ( isset( $_POST['post_ID'] ) || ! isset( $_GET['post'] ) ) {
299
- return;
300
- }
301
 
302
- $vendor = yith_get_vendor( 'current', 'user' );
303
- $product_vendor = yith_get_vendor( $_GET['post'], 'product' ); // If false, the product hasn't any vendor set
 
 
 
 
304
 
305
  if ( $vendor->has_limited_access() && false !== $product_vendor && $vendor->id != $product_vendor->id ) {
306
- wp_die( sprintf( __( 'You do not have permission to edit this product. %1$sClick here to view and edit your products%2$s.', 'yith_wc_product_vendors' ), '<a href="' . esc_url( 'edit.php?post_type=product' ) . '">', '</a>' ) );
307
- }
308
  }
309
 
310
  /**
@@ -317,16 +347,17 @@ if ( ! class_exists( 'YITH_Vendors_Admin' ) ) {
317
  * @since 1.0
318
  */
319
  public function filter_vendor_media( $query = array() ) {
320
- $vendor = yith_get_vendor( 'current', 'user' );
321
 
322
  if ( $vendor->is_valid() && $vendor->has_limited_access() ) {
323
  $vendor_admin_ids = $vendor->get_admins();
324
 
325
- if ( ! empty( $vendor_admin_ids ) ) {
326
- $query['author__in'] = $vendor_admin_ids;
327
  }
328
 
329
  }
 
330
  return $query;
331
  }
332
 
@@ -338,29 +369,27 @@ if ( ! class_exists( 'YITH_Vendors_Admin' ) ) {
338
  * @return void
339
  */
340
  public function menu_items() {
341
- $vendor = yith_get_vendor( 'current', 'user' );
342
 
343
- if ( ! $vendor->is_valid() || ! $vendor->has_limited_access() || ! $vendor->is_owner() ) {
344
- return;
345
- }
346
 
347
- $menus = apply_filters( 'yith_wc_product_vendors_details_menu_items',
348
- array(
349
  'vendor_details' => array(
350
- 'page_title' => __( 'Vendor Profile', 'yith_wc_product_vendors' ),
351
- 'menu_title' => __( 'Vendor Profile', 'yith_wc_product_vendors' ),
352
- 'capability' => 'edit_products',
353
- 'menu_slug' => 'yith_vendor_details',
354
- 'function' => array( $this, 'admin_details_page' ),
355
- 'icon' => 'dashicons-id-alt',
356
- 'position' => 56
357
- ),
358
- )
359
- );
360
-
361
- foreach( $menus as $menu_args ){
362
  extract( $menu_args );
363
- add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function, $icon, $position );
364
  }
365
  }
366
 
@@ -371,10 +400,10 @@ if ( ! class_exists( 'YITH_Vendors_Admin' ) ) {
371
  * @since 1.0.0
372
  * @return void
373
  */
374
- public function remove_media_page(){
375
  /* Remove Media Library */
376
  $vendor = yith_get_vendor( 'current', 'user' );
377
- if( $vendor->is_valid() && $vendor->has_limited_access() ){
378
  remove_menu_page( 'upload.php' );
379
  }
380
  }
@@ -391,227 +420,222 @@ if ( ! class_exists( 'YITH_Vendors_Admin' ) ) {
391
  yith_wcpv_get_template( 'vendor-admin', array( 'vendor' => $vendor ), 'admin' );
392
  }
393
 
394
- /**
395
- * Update vendor information
396
- *
397
- * @param $query object The query object
398
- *
399
- * @author Andrea Grillo <andrea.grillo@yithemes.com>
400
- * @since 1.0.0
401
- * @fire product_vendors_details_fields_save action
402
- */
403
  public function filter_vendor_linked_products( $query ) {
404
- $vendor = yith_get_vendor( 'current', 'user' );
405
- $action = isset( $_GET['action'] ) ? $_GET['action'] : false;
406
 
407
- if ( $vendor->is_valid() && $vendor->has_limited_access() && ( 'woocommerce_json_search_products' == $action || 'woocommerce_json_search_grouped_products' == $action ) ) {
408
- $query_args = $vendor->get_query_products_args();
409
- $query->set( 'tax_query', $query_args['tax_query'] );
410
  }
411
  }
412
 
413
- /**
414
- * Add fields to vendor taxonomy (add new vendor screen)
415
- *
416
- * @param str $taxonomy Current taxonomy name
417
- *
418
- * @author Andrea Grillo <andrea.grillo@yithemes.com>
419
- * @return void
420
- */
421
- public function add_taxonomy_fields( $taxonomy ) {
422
-
423
- $args = array( 'commission' => YITH_Vendors()->get_base_commission() );
424
-
425
- $this->enqueue_ajax_choosen();
426
-
427
- yith_wcpv_get_template( 'add-product-vendors-taxonomy', $args, 'admin' );
428
-
429
- $this->add_select_customer_script();
430
- }
431
-
432
- /**
433
- * Edit fields to vendor taxonomy
434
- *
435
- * @param WP_Post $vendor Current vendor information
436
- *
437
- * @author Andrea Grillo <andrea.grillo@yithemes.com>
438
- * @return void
439
- */
440
- public function edit_taxonomy_fields( $vendor ) {
441
-
442
- $vendor = yith_get_vendor( $vendor );
443
- $vendor_admins = $this->vendor_admins_chosen( $vendor );
444
-
445
- //Get Vendor Owner
446
- $owner = get_userdata( $vendor->get_owner() );
447
- $owner_display = is_a( $owner, 'WP_User' ) ? $owner->display_name . '(#' . $owner->ID . ' - ' . $owner->user_email . ')' : '';
448
- $vendor_owner = ! empty( $owner_display ) ? '<option value="' . esc_attr( $owner->ID ) . '" selected="selected">' . $owner_display . '</option>' : '<option></option>';
449
-
450
- $this->enqueue_ajax_choosen();
451
-
452
- $args = apply_filters( 'yith_edit_taxonomy_args', array(
453
- 'owner' => $vendor_owner,
454
- 'vendor_admins' => $vendor_admins,
455
- 'vendor' => $vendor
456
- )
457
- );
458
-
459
- yith_wcpv_get_template( 'edit-product-vendors-taxonomy', $args, 'admin' );
460
-
461
- $this->add_select_customer_script();
462
- }
463
-
464
- /**
465
- * Check for duplicate vendor name
466
- *
467
- * @author Andrea Grillo <andrea.grillo@yithemes.com>
468
- *
469
- * @param $term string The term name
470
- * @param $taxonomy string The taxonomy name
471
- *
472
- * @return mixed term object | WP_Error
473
- * @since 1.0
474
- */
475
- public function check_duplicate_term_name( $term, $taxonomy ) {
476
-
477
- if ( $this->_taxonomy_name != $taxonomy ) {
478
- return $term;
479
- }
480
-
481
- if ( 'edit_terms' == current_action() && isset( $_POST['name'] ) ) {
482
- $duplicate = get_term_by( 'name', $_POST['name'], $taxonomy );
483
-
484
- /**
485
- * If the vendor name exist -> check if is the edited item or not
486
- */
487
- if ( $duplicate && $duplicate->term_id == $term ) {
488
- $duplicate = false;
489
- }
490
-
491
- $message = __( 'A vendor with this name already exists.', 'yith_wc_product_vendors' );
492
- $title = __( 'Vendor name already exists', 'yith_wc_product_vendors' );
493
- $back_link = admin_url( 'edit-tag.php' );
494
 
495
  $back_link = esc_url( add_query_arg( $back_link, array(
496
  'action' => 'edit',
497
- 'taxonomy' => $_POST['taxonomy'],
498
- 'tag_ID' => $_POST['tag_ID'],
499
  'post_type' => 'product'
500
- )
501
- )
502
- );
503
 
504
- $args = array( 'back_link' => $back_link );
505
 
506
- return ! $duplicate ? $term : wp_die( $message, $title, $args );
507
 
508
- } else {
509
- $duplicate = get_term_by( 'name', $term, $taxonomy );
510
 
511
- return ! $duplicate ? $term : new WP_Error( 'term_exists', __( 'A vendor with this name already exists.', 'yith_wc_product_vendors' ), $duplicate );
512
- }
513
- }
514
 
515
- /**
516
- * Save extra taxonomy fields for product vendors taxonomy
517
- *
518
- * @author Andrea Grillo <andrea.grillo@yithemes.com>
519
  *
520
  * @param $vendor_id string The vendor id
521
  *
522
  * @return void
523
  * @since 1.0
524
- */
525
- public function save_taxonomy_fields( $vendor_id = 0 ) {
526
 
527
- if ( ! isset( $_POST['yith_vendor_data'] ) ) {
528
  return;
529
  }
530
 
531
  $is_new = strpos( current_action(), 'created_' ) !== false;
532
 
533
  // if not is set $vendor_id check if there is the update_vendor_id field inside the $_POST array
534
- if( empty( $vendor_id ) && isset( $_POST['update_vendor_id'] ) ) {
535
- $vendor_id = $_POST['update_vendor_id'];
536
  }
537
 
538
  $vendor = yith_get_vendor( $vendor_id );
539
 
540
- if( ! $vendor->is_valid() ) {
541
  return;
542
  }
543
 
544
- $post_value = $_POST['yith_vendor_data'];
545
- $usermeta_owner = YITH_Vendors()->get_user_meta_owner();
546
- $usermeta_admin = YITH_Vendors()->get_user_meta_key();
547
 
548
- if ( ! $vendor->has_limited_access() ) {
549
  foreach ( apply_filters( 'yith_wpv_save_checkboxes', array( 'enable_selling' ), $vendor->has_limited_access() ) as $key ) {
550
- ! isset( $post_value[ $key ] ) && $post_value[ $key ] = 'no';
551
  }
552
- }
553
-
554
- else {
555
  foreach ( apply_filters( 'yith_wpv_save_checkboxes', array(), $vendor->has_limited_access() ) as $key ) {
556
- ! isset( $post_value[$key] ) && $post_value[$key] = 'no';
557
  }
558
  }
559
 
560
  // set values
561
  foreach ( $post_value as $key => $value ) {
562
- $vendor->$key = ! is_array( $value ) ? wc_clean( $value ) : $value;
563
  }
564
 
565
  // add vendor registrantion date
566
- if( $is_new ) {
567
- $vendor->registration_date = current_time( 'mysql' );
568
- $vendor->registration_date_gmt = current_time( 'mysql', 1 );
569
  }
570
 
571
  // Get current vendor admins and owner
572
- $admins = $vendor->get_admins();
573
- $owner = $vendor->get_owner();
574
 
575
- // Remove all current admins (user meta)
576
- foreach ( $admins as $user_id ) {
577
  $user = get_user_by( 'id', $user_id );
578
  delete_user_meta( $user_id, $usermeta_admin );
579
  $user->remove_role( YITH_Vendors()->get_role_name() );
580
  $user->add_role( 'customer' );
581
- }
582
-
583
- // Remove current owner and update it
584
- if ( ! empty( $post_value['owner'] ) && $owner != $post_value['owner'] ) {
585
- delete_user_meta( $owner, $usermeta_owner );
586
- update_user_meta(intval( $post_value['owner'] ), $usermeta_owner, $vendor->id );
587
- $owner = intval( $post_value['owner'] );
588
- }
589
-
590
- //Add Vendor Owner
591
- if ( ! isset( $post_value['admins'] ) ) {
592
- $post_value['admins'] = array( $owner );
593
- } else {
594
- $post_value['admins'][] = $owner;
595
- }
596
-
597
- // Only add selected admins
598
- if ( isset( $post_value['admins'] ) && ! empty( $post_value['admins'] ) ) {
599
- foreach ( $post_value['admins'] as $user_id ) {
600
- update_user_meta( $user_id, $usermeta_admin, $vendor->id );
601
- $user = get_user_by( 'id', $user_id );
602
- if( $user instanceof WP_User ){
603
  $user->add_role( YITH_Vendors()->get_role_name() );
604
  $user->remove_role( 'customer' );
605
  }
606
- }
607
- }
608
 
609
  do_action( 'yith_wpv_after_save_taxonomy', $vendor, $post_value );
610
 
611
- if( 'admin_action_yith_admin_save_fields' == current_action() ){
612
- wp_redirect( esc_url_raw( add_query_arg( array( 'page' => $_POST['page'], 'tab' => $_POST['tab'] ) ) ) );
613
  }
614
- }
615
 
616
  /**
617
  * Add or Remove capabilities to vendor admins
@@ -630,58 +654,55 @@ if ( ! class_exists( 'YITH_Vendors_Admin' ) ) {
630
  if ( '' == $caps ) {
631
  $caps = array_keys( YITH_Vendors()->vendor_enabled_capabilities() );
632
 
633
- }
634
- elseif ( is_string( $caps ) ) {
635
  $caps = array( $caps );
636
  }
637
 
638
  foreach ( $caps as $cap ) {
639
  if ( 'add' == $method ) {
640
  $role->add_cap( $cap, true );
641
- }
642
-
643
- else {
644
  $role->remove_cap( $cap );
645
  }
646
  }
647
  }
648
  }
649
 
650
- /**
651
- * Remove admin usermeta info
652
- *
653
- * @param $term
654
- * @param $taxonomy
655
- *
656
- * @since 1.0.0
657
- * @return void
658
- */
659
- public function remove_admin_usermeta_info( $term, $taxonomy ) {
660
-
661
- if ( $this->_taxonomy_name != $taxonomy ) {
662
- return;
663
- }
664
-
665
- global $wpdb;
666
-
667
- $sql = $wpdb->prepare( "SELECT user_id FROM {$wpdb->usermeta} WHERE meta_value=%d AND meta_key=%s", $term, 'yith_product_vendor_owner' );
668
- $user_id = $wpdb->get_var( $sql );
669
-
670
- if ( $user_id > 0 ) {
671
- /**
672
- * Remove admin caps to user
673
- */
674
  $user = get_user_by( 'id', $user_id );
675
  $user->remove_role( YITH_Vendors()->get_role_name() );
676
  $user->add_role( 'customer' );
677
 
678
- /**
679
- * Remove vendor admin
680
- */
681
- delete_user_meta( $user_id, YITH_Vendors()->get_user_meta_key() );
682
- delete_user_meta( $user_id, YITH_Vendors()->get_user_meta_owner() );
683
- }
684
- }
685
 
686
  /**
687
  * Set vendor products to draft
@@ -693,12 +714,12 @@ if ( ! class_exists( 'YITH_Vendors_Admin' ) ) {
693
  * @since 1.0.0
694
  * @return void
695
  */
696
- public function set_product_to_orphan( $term, $post_status = 'draft' ){
697
 
698
  $vendor = yith_get_vendor( $term );
699
  $product_ids = $vendor->get_products();
700
 
701
- foreach( $product_ids as $product_id ){
702
  wp_update_post( array( 'ID' => $product_id, 'post_status' => $post_status ) );
703
  }
704
  }
@@ -726,39 +747,17 @@ if ( ! class_exists( 'YITH_Vendors_Admin' ) ) {
726
  $this->set_product_to_orphan( $term );
727
  }
728
 
729
- /**
730
- * Remove the WooCommerce taxonomy Metabox and add a new Metabox for single taxonomy management
731
- *
732
- * @author Andrea Grillo <andrea.grillo@yithemes.com>
733
- * @since 1.0.0
734
- * @return void
735
- */
736
- public function single_value_taxonomy() {
737
-
738
- $id = 'tagsdiv-' . $this->_taxonomy_name;
739
- $taxonomy = get_taxonomies( array( 'show_ui' => true ), 'object' );
740
- $product_vendors = $taxonomy[ $this->_taxonomy_name ];
741
- $page = 'product';
742
- $context = 'side';
743
- $callback = array( $this, 'single_taxonomy_meta_box' );
744
- $callback_args = array( 'taxonomy' => $this->_taxonomy_name );
745
- $priority = 'default';
746
-
747
- remove_meta_box( $id, $page, $context );
748
- add_meta_box( $id, $product_vendors->labels->name, $callback, $page, $context, $priority, $callback_args );
749
- }
750
-
751
- /**
752
- * Add select customer scripts
753
- *
754
- * @Author Andrea Grillo <andrea.grillo@yithemes.com>
755
- * @since 1.0.0
756
- * @return void
757
- * @use woocommerce_json_search_customers action
758
- */
759
- public function add_select_customer_script() {
760
-
761
- $inline_js = "
762
  jQuery('select.ajax_chosen_select_customer').ajaxChosen({
763
  method: 'GET',
764
  url: '" . admin_url( 'admin-ajax.php' ) . "',
@@ -783,43 +782,43 @@ if ( ! class_exists( 'YITH_Vendors_Admin' ) ) {
783
  });
784
  ";
785
 
786
- wc_enqueue_js( $inline_js );
787
- }
788
-
789
- /**
790
- * When searching using the WP_User_Query, search names (user meta) too
791
- *
792
- * @param object $query The current query object
793
- *
794
- * @return void
795
- * @see WP_User_Query Class wp-includes/user.php
796
- */
797
- public function json_search_customer_name( $query ) {
798
-
799
- if ( isset( $_GET['plugin'] ) && YITH_WPV_SLUG == $_GET['plugin'] ) {
800
- global $wpdb;
801
-
802
- $term = wc_clean( stripslashes( $_GET['term'] ) );
803
- $term = $wpdb->esc_like( $term );
804
-
805
- $where_old = $wpdb->prepare( ") OR user_name.meta_value LIKE %s ", '%' . $term . '%' );
806
- $where_new = $wpdb->prepare( " OR user_name.meta_value LIKE %s) ", '%' . $term . '%' );
807
- $query->query_where = str_replace( $where_old, $where_new, $query->query_where );
808
- $query->query_where .= $wpdb->prepare( "AND ID NOT IN (SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = %s)", YITH_Vendors()->get_user_meta_key() );
809
- }
810
- }
811
-
812
- /**
813
- * Print the Single Taxonomy Metabox
814
- *
815
- * @author Andrea Grillo <andrea.grillo@yithemes.com>
816
- * @since 1.0.0
817
- *
818
- * @param $taxonomy string Taxonomy Name
819
- * @param $taxonomy_box string Taxonomy Box
820
- *
821
- * @return void
822
- */
823
  public function single_taxonomy_meta_box( $taxonomy, $taxonomy_box ) {
824
  $taxonomy_label = YITH_Vendors()->get_vendors_taxonomy_label();
825
  $vendor = yith_get_vendor( 'current', 'product' );
@@ -828,8 +827,8 @@ if ( ! class_exists( 'YITH_Vendors_Admin' ) ) {
828
  'id' => 'tax-input-yith_shop_vendor',
829
  'name' => 'tax_input[yith_shop_vendor]',
830
  'taxonomy' => $this->_taxonomy_name,
831
- 'show_option_none' => ! $vendor->is_super_user() ? '' : sprintf( __( 'No %s' ), strtolower( $taxonomy_label['singular_name'] ) ),
832
- 'hide_empty' => ! $vendor->is_super_user(),
833
  'selected' => $vendor ? $vendor->id : 0,
834
  'walker' => YITH_Walker_CategoryDropdown(),
835
  'option_none_value' => '', // Avoid to save -1 as new vendor when you create a new product
@@ -839,33 +838,33 @@ if ( ! class_exists( 'YITH_Vendors_Admin' ) ) {
839
 
840
  if ( $vendor->is_valid() && $vendor->has_limited_access() && $vendor->is_user_admin() ) {
841
  echo $vendor->name;
842
- }
843
- else {
844
  wp_dropdown_categories( $args );
845
  }
846
  }
847
 
848
- /**
849
- * Add ajax-chosen javascript libr
850
- *
851
- * @Author Andrea Grillo <andrea.grillo@yithemes.com>
852
- * @since 1.0.0
853
- * @return void
854
- */
855
- public function enqueue_ajax_choosen() {
856
- wp_enqueue_script( 'ajax-chosen' );
857
- }
858
 
859
  /**
860
- * If an user is a vendor admin remove the woocommerce prevent admin access
861
- *
862
- * @Author Andrea Grillo <andrea.grillo@yithemes.com>
863
- * @since 1.0.0
864
- * @return bool
865
- * @use woocommerce_prevent_admin_access hooks
866
- */
867
- public function prevent_admin_access( $prevent_access ){
868
  $vendor = yith_get_vendor( 'current', 'user' );
 
869
  return $vendor->is_valid() && $vendor->has_limited_access() && $vendor->is_user_admin() ? false : $prevent_access;
870
  }
871
 
@@ -873,75 +872,77 @@ if ( ! class_exists( 'YITH_Vendors_Admin' ) ) {
873
  * Get vendors admin array for choosen select
874
  *
875
  * @Author Andrea Grillo <andrea.grillo@yithemes.com>
876
- * @since 1.0.0
877
  *
878
  * @param $vendor The vendor object
879
  *
880
  * @return string The admins
881
  */
882
- public function vendor_admins_chosen( $vendor = '' ){
883
- if( empty( $vendor ) ){
884
  $vendor = yith_get_vendor( 'current', 'user' );
885
  }
886
 
887
  $admins = '';
888
- foreach ( $vendor->get_admins() as $user_id ) {
889
- if( $vendor->owner != $user_id ){
890
  $user = get_userdata( $user_id );
891
  $user_display = is_object( $user ) ? $user->display_name . '(#' . $user_id . ' - ' . $user->user_email . ')' : '';
892
  $admins .= '<option value="' . esc_attr( $user_id ) . '" selected="selected">' . $user_display . '</option>';
893
  }
894
- }
 
895
  return $admins;
896
  }
897
 
898
- /**
899
- * Action Links
900
- *
901
- * add the action links to plugin admin page
902
- *
903
- * @param $links | links plugin array
904
- *
905
- * @return mixed Array
906
- * @since 1.0
907
- * @author Andrea Grillo <andrea.grillo@yithemes.com>
908
- * @return mixed
909
- * @use plugin_action_links_{$plugin_file_name}
910
- */
911
  public function action_links( $links ) {
912
- $links[] = '<a href="' . admin_url( "admin.php?page={$this->_panel_page}" ) . '">' . __( 'Settings', 'yith_wc_product_vendors' ) . '</a>';
913
- $premium_live_text = defined( 'YITH_WPV_FREE_INIT' ) ? __( 'Premium live demo', 'yith_wc_product_vendors' ) : __( 'Live demo', 'yith_wc_product_vendors' );
914
- $links[] = '<a href="' . $this->_premium_live . '" target="_blank">' . $premium_live_text . '</a>';
915
-
916
- if ( defined( 'YITH_WPV_FREE_INIT' ) ) {
917
- $links[] = '<a href="' . $this->get_premium_landing_uri() . '" target="_blank">' . __( 'Premium Version', 'yith_wc_product_vendors' ) . '</a>';
918
- }
919
-
920
- return $links;
921
- }
922
-
923
- /**
924
- * plugin_row_meta
925
- *
926
- * add the action links to plugin admin page
927
- *
928
- * @param $plugin_meta
929
- * @param $plugin_file
930
- * @param $plugin_data
931
- * @param $status
932
- *
933
- * @return Array
934
- * @since 1.0
935
- * @author Andrea Grillo <andrea.grillo@yithemes.com>
936
- * @use plugin_row_meta
937
- */
938
- public function plugin_row_meta( $plugin_meta, $plugin_file, $plugin_data, $status ) {
939
-
940
- if( ( defined( 'YITH_WPV_INIT' ) && YITH_WPV_INIT == $plugin_file ) || ( defined( 'YITH_WPV_FREE_INIT' ) && YITH_WPV_FREE_INIT == $plugin_file ) ){
941
  $plugin_meta[] = '<a href="' . $this->_official_documentation . '" target="_blank">' . __( 'Plugin Documentation', 'yith_wc_product_vendors' ) . '</a>';
942
  }
943
- return $plugin_meta;
944
- }
 
945
 
946
  /**
947
  * Get the premium landing uri
@@ -962,54 +963,54 @@ if ( ! class_exists( 'YITH_Vendors_Admin' ) ) {
962
  * @return string The premium landing link
963
  * @return void
964
  */
965
- public function remove_dashboard_widgets(){
966
  $vendor = yith_get_vendor( 'current', 'user' );
967
 
968
- if ( $vendor->is_valid() && $vendor->has_limited_access()) {
969
 
970
  $to_removes = array(
971
  array(
972
- 'id' => 'woocommerce_dashboard_status',
973
- 'screen' => 'dashboard',
974
- 'context' => 'normal'
975
  ),
976
  array(
977
- 'id' => 'dashboard_activity',
978
- 'screen' => 'dashboard',
979
- 'context' => 'normal'
980
  ),
981
  array(
982
- 'id' => 'woocommerce_dashboard_recent_reviews',
983
- 'screen' => 'dashboard',
984
- 'context' => 'normal'
985
  ),
986
  array(
987
- 'id' => 'dashboard_right_now',
988
- 'screen' => 'dashboard',
989
- 'context' => 'normal'
990
  ),
991
  array(
992
- 'id' => 'dashboard_quick_press',
993
- 'screen' => 'dashboard',
994
- 'context' => 'normal'
995
  ),
996
 
997
  );
998
 
999
- foreach( $to_removes as $widget ){
1000
- remove_meta_box( $widget['id'], $widget['screen'], $widget['context'] );
1001
  }
1002
  }
1003
  }
1004
 
1005
- /**
1006
  * Show the premium tabs
1007
  *
1008
  * @since 1.0.0
1009
  * @author Andrea Grillo <andrea.grillo@yithemes.com>
1010
  * @return string The premium landing link
1011
  */
1012
- public function show_premium_tab(){
1013
  yith_wcpv_get_template( 'premium', array(), 'admin' );
1014
  }
1015
 
@@ -1019,7 +1020,8 @@ if ( ! class_exists( 'YITH_Vendors_Admin' ) ) {
1019
  * and is are logged in
1020
  *
1021
  * @author Andrea Grillo <andrea.grillo@yithemes.com>
1022
- * @since 1.5.1
 
1023
  * @param $wp_admin_bar The WP_Admin_Bar object
1024
  */
1025
  public function show_admin_bar_visit_store( $wp_admin_bar ) {
@@ -1032,8 +1034,7 @@ if ( ! class_exists( 'YITH_Vendors_Admin' ) ) {
1032
  'id' => 'view-store',
1033
  'title' => __( 'Visit Store', 'woocommerce' ),
1034
  'href' => $vendor->get_url( 'frontend' )
1035
- )
1036
- );
1037
  }
1038
  }
1039
 
@@ -1041,29 +1042,29 @@ if ( ! class_exists( 'YITH_Vendors_Admin' ) ) {
1041
  * Add an extra body classes for vendors dashboard
1042
  *
1043
  * @author Andrea Grillo <andrea.grillo@yithemes.com>
1044
- * @since 1.5.1
 
1045
  * @param $admin_body_classes
 
1046
  * @return string
1047
  */
1048
- public function admin_body_class( $admin_body_classes ){
1049
  global $post;
1050
- $vendor = yith_get_vendor( 'current', 'user' );
1051
- $is_ajax = defined( 'DOING_AJAX' ) && DOING_AJAX;
1052
- $is_order_details = is_admin() && ! $is_ajax && 'shop_order' == get_current_screen()->id;
1053
- $refund_management = 'yes' == get_option( 'yith_wpv_vendors_option_order_refund_synchronization', 'no' ) ? true : false;
1054
 
1055
 
1056
- if( $vendor->is_valid() && $vendor->has_limited_access() ){
1057
  $admin_body_classes = $admin_body_classes . ' vendor_limited_access';
1058
- if( $is_order_details && $refund_management ){
1059
  $admin_body_classes = $admin_body_classes . ' vendor_refund_management';
1060
  }
1061
- }
1062
-
1063
- else if( $vendor->is_super_user() ){
1064
  $admin_body_classes = $admin_body_classes . ' vendor_super_user';
1065
 
1066
- if( $post && wp_get_post_parent_id( $post->ID ) && 'shop_order' == $post->post_type && $is_order_details ){
1067
  $admin_body_classes = $admin_body_classes . ' vendor_suborder_detail';
1068
  }
1069
  }
@@ -1078,29 +1079,30 @@ if ( ! class_exists( 'YITH_Vendors_Admin' ) ) {
1078
  * @since 1.5.1
1079
  * @return string
1080
  */
1081
- public function products_to_approve(){
1082
  $vendor = yith_get_vendor( 'current', 'user' );
1083
  /* Add the pending products bubble on Products -> Vendors menu */
1084
  if ( $vendor->is_super_user() ) {
1085
  global $menu, $submenu;
1086
- $products = get_posts( array( 'post_type' => 'product', 'post_status' => 'pending' ) );
1087
  $num_pending_products = count( $products );
1088
 
1089
  if ( $num_pending_products > 0 ) {
1090
- $bubble = " <span class='awaiting-mod count-{$num_pending_products}'><span class='pending-count'>{$num_pending_products}</span></span>";
1091
- $products_uri = htmlspecialchars( add_query_arg( array( 'post_type' => 'product' ), 'edit.php' ) );
1092
 
1093
  foreach ( $menu as $key => $value ) {
1094
- if ( $menu[$key][2] == $products_uri && $num_pending_products > 0 ) {
1095
- $menu[$key][0] .= $bubble;
1096
  }
1097
  }
1098
 
1099
  foreach ( $submenu as $key => $value ) {
1100
- $submenu_items = $submenu[$key];
1101
  foreach ( $submenu_items as $position => $value ) {
1102
- if ( $submenu[$key][$position][2] == $products_uri ) {
1103
- $submenu[$key][$position][0] .= $bubble;
 
1104
  return;
1105
  }
1106
  }
@@ -1123,17 +1125,19 @@ if ( ! class_exists( 'YITH_Vendors_Admin' ) ) {
1123
  public function filter_order_list( $query ) {
1124
  global $typenow;
1125
 
1126
- if ( 'shop_order' == $typenow ) {
1127
  $vendor = yith_get_vendor( 'current', 'vendor' );
1128
  if ( $vendor->is_super_user() ) {
1129
- $query['post_parent'] = 0;
1130
  }
 
1131
  return apply_filters( "yith_wcmv_{$typenow}_request", $query );
1132
  }
 
1133
  return $query;
1134
  }
1135
 
1136
- /**
1137
  * Filter the post count for vendor
1138
  *
1139
  * @author Andrea Grillo <andrea.grillo@yithemes.com>
@@ -1144,7 +1148,7 @@ if ( ! class_exists( 'YITH_Vendors_Admin' ) ) {
1144
  *
1145
  * @return arr Modified request
1146
  * @since 1.0
1147
- * @use wp_post_count action
1148
  */
1149
  public function vendor_count_shop_orders( $counts, $type, $perm ) {
1150
  if ( 'shop_order' == $type ) {
@@ -1152,28 +1156,24 @@ if ( ! class_exists( 'YITH_Vendors_Admin' ) ) {
1152
  * Get a list of post statuses.
1153
  */
1154
  $vendor = yith_get_vendor( 'current', 'user' );
1155
- $stati = get_post_stati();
1156
 
1157
  if ( $vendor->is_valid() && $vendor->has_limited_access() ) {
1158
  foreach ( $stati as $status ) {
1159
  $orders = $vendor->get_orders( 'suborder', $status );
1160
  $counts->$status = count( $orders );
1161
  }
1162
- return $counts;
1163
- }
1164
 
1165
- else {
 
1166
  global $wpdb;
1167
  foreach ( $stati as $status ) {
1168
- $orders = $wpdb->get_col(
1169
- $wpdb->prepare(
1170
- "SELECT DISTINCT ID FROM {$wpdb->posts} WHERE post_parent=%d AND post_status=%s AND post_type=%s", 0, $status, 'shop_order'
1171
- )
1172
- );
1173
  $counts->$status = count( $orders );
1174
  }
1175
  }
1176
  }
 
1177
  return $counts;
1178
  }
1179
 
@@ -1193,33 +1193,20 @@ if ( ! class_exists( 'YITH_Vendors_Admin' ) ) {
1193
  'post_type' => 'product'
1194
  );
1195
 
1196
- $menu_args = apply_filters( 'yith_wc_product_vendors_taxonomy_menu_items',
1197
- array(
1198
  'page_title' => YITH_Vendors()->get_vendors_taxonomy_label( 'menu_name' ),
1199
  'menu_title' => YITH_Vendors()->get_vendors_taxonomy_label( 'menu_name' ),
1200
- 'capability' => 'manage_options',
1201
  'menu_slug' => htmlspecialchars( add_query_arg( $url_args, 'edit-tags.php' ) ),
1202
  'function' => '',
1203
  'icon' => 'dashicons-admin-multisite',
1204
  'position' => 56
1205
- )
1206
- );
1207
  extract( $menu_args );
1208
  add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function, $icon, $position );
1209
  }
1210
  }
1211
 
1212
- /**
1213
- * Remove vendor taxonomy link under Products
1214
- *
1215
- * @author Andrea Grillo <andrea.grillo@yithemes.com>
1216
- * @since 1.6.3
1217
- * @return void
1218
- */
1219
- public function remove_vendor_taxonomy_menu_item(){
1220
- remove_submenu_page( 'edit.php?post_type=product', 'edit-tags.php?taxonomy=yith_shop_vendor&amp;post_type=product' );
1221
- }
1222
-
1223
  /**
1224
  * Check if current page is the vendor taxonomy page (in admin)
1225
  *
@@ -1227,9 +1214,10 @@ if ( ! class_exists( 'YITH_Vendors_Admin' ) ) {
1227
  * @since 1.7
1228
  * @return bool
1229
  */
1230
- public function is_vendor_tax_page(){
1231
  global $pagenow;
1232
- return 'edit-tags.php' == $pagenow && ! empty( $_GET['taxonomy'] ) && YITH_Vendors()->get_taxonomy_name() == $_GET['taxonomy'] && ! empty( $_GET['post_type'] ) && 'product' == $_GET['post_type'];
 
1233
  }
1234
 
1235
  /**
@@ -1254,11 +1242,12 @@ if ( ! class_exists( 'YITH_Vendors_Admin' ) ) {
1254
 
1255
  if ( is_array( $user->roles ) ) {
1256
  foreach ( $user->roles as $role ) {
1257
- $user_roles[] = translate_user_role( $wp_roles->role_names[$role] );
1258
  }
1259
  $output = join( ', ', $user_roles );
1260
  }
1261
  }
 
1262
  return $output;
1263
  }
1264
 
@@ -1275,16 +1264,17 @@ if ( ! class_exists( 'YITH_Vendors_Admin' ) ) {
1275
  public function manage_users_columns( $columns ) {
1276
 
1277
  // Unset the core WP `role` column.
1278
- if ( isset( $columns['role'] ) ) {
1279
- unset( $columns['role'] );
1280
  }
1281
 
1282
  // Add our new roles column.
1283
- $columns['roles'] = esc_html__( 'Roles', 'yith_wc_product_vendors' );
 
1284
  return $columns;
1285
  }
1286
-
1287
- /**
1288
  * Check for order capabilities
1289
  *
1290
  * Add or remove vendor capabilities for coupon and review management
@@ -1294,33 +1284,47 @@ if ( ! class_exists( 'YITH_Vendors_Admin' ) ) {
1294
  * @author Andrea Grillo <andrea.grillo@yithemes.com>
1295
  */
1296
  public function manage_caps() {
1297
- $is_vendor_tab = isset( $_GET['page'] ) && 'yith_wpv_panel' == $_GET['page'] && isset( $_GET['tab'] ) && 'vendors' == $_GET['tab'] ? true : false;
1298
- if ( $is_vendor_tab ) {
 
 
 
1299
  $caps = apply_filters( 'yith_wcmv_manage_role_caps', array(
1300
- 'order' => array(
1301
- 'edit_shop_orders',
1302
- 'read_shop_orders',
1303
- 'delete_shop_orders',
1304
- 'publish_shop_orders',
1305
- 'edit_published_shop_orders',
1306
- 'delete_published_shop_orders',
1307
  ),
1308
  )
1309
  );
1310
 
1311
  foreach ( array_keys( $caps ) as $option ) {
 
 
 
 
 
 
 
 
 
 
1312
  $option_id = "yith_wpv_vendors_option_{$option}_management";
1313
  $old_value = get_option( $option_id, 'no' );
1314
  $new_value = isset( $_POST[ $option_id ] ) ? 'yes' : 'no';
1315
 
1316
  if ( $old_value != $new_value ) {
1317
- foreach ( $caps[$option] as $cap ) {
 
1318
  if ( 'no' == $old_value ) {
1319
- $this->_manage_vendor_caps( 'add', $caps[$option] );
1320
- }
 
 
1321
 
1322
- else {
1323
- $this->_manage_vendor_caps( 'remove', $caps[$option] );
1324
  }
1325
  }
1326
  }
@@ -1351,7 +1355,7 @@ if ( ! class_exists( 'YITH_Vendors_Admin' ) ) {
1351
  foreach ( $menu as $position => $args ) {
1352
  $submenu_url = "post-new.php?post_type={$typenow}";
1353
  if ( in_array( $submenu_url, $args ) ) {
1354
- $submenu[$section][$position][4] = 'yith-wcpv-hide-submenu-item';
1355
  break;
1356
  }
1357
  }
@@ -1360,7 +1364,7 @@ if ( ! class_exists( 'YITH_Vendors_Admin' ) ) {
1360
  }
1361
  }
1362
 
1363
- /**
1364
  * Remove add new post types button
1365
  *
1366
  * @return void
@@ -1370,15 +1374,26 @@ if ( ! class_exists( 'YITH_Vendors_Admin' ) ) {
1370
  public function remove_add_new_button() {
1371
  global $post_type, $wp_version;
1372
  $rule = false;
1373
- if( version_compare( $wp_version, '4.3-RC1', '>=' ) ){
1374
  $rule = ".post-type-{$post_type} .wrap .page-title-action{ display: none; }";
1375
- }
1376
-
1377
- else {
1378
  $rule = ".post-type-{$post_type} .add-new-h2{ display: none; }";
1379
  }
1380
 
1381
  wp_add_inline_style( 'yith-wc-product-vendors-admin', $rule );
1382
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
1383
  }
1384
  }
7
  * It is also available through the world-wide-web at this URL:
8
  * http://www.gnu.org/licenses/gpl-3.0.txt
9
  */
10
+ if ( !defined( 'ABSPATH' ) ) {
11
  exit( 'Direct access forbidden.' );
12
  }
13
 
20
  * @author Your Inspiration Themes
21
  *
22
  */
23
+ if ( !class_exists( 'YITH_Vendors_Admin' ) ) {
24
 
25
  class YITH_Vendors_Admin {
26
 
42
  /**
43
  * @var string Official plugin documentation
44
  */
45
+ protected $_official_documentation = 'http://yithemes.com/docs-plugins/yith-woocommerce-multi-vendor/';
46
 
47
  /**
48
  * @var string Official plugin landing page
49
  */
50
+ protected $_premium_landing = 'http://yithemes.com/docs-plugins/yith-woocommerce-multi-vendor/';
51
 
52
  /**
53
  * @var string Official plugin landing page
54
  */
55
+ protected $_premium_live = 'http://plugins.yithemes.com/yith-woocommerce-multi-vendor/';
56
 
57
+ /**
58
  * @var string Vendor options role
59
  */
60
  protected $_vendor_role = 'manage_vendor_store';
64
  * Construct
65
  */
66
  public function __construct() {
67
+ $this->_taxonomy_name = YITH_Vendors()->get_taxonomy_name();
68
 
69
  /* Add Admin Body Class */
70
  add_filter( 'admin_body_class', array( $this, 'admin_body_class' ) );
71
 
72
+ /* Panel Settings */
73
  add_action( 'admin_menu', array( $this, 'register_panel' ), 5 );
74
  add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
75
 
76
  /* Dashboard Menu */
 
77
  add_action( 'admin_menu', array( $this, 'add_new_link_check' ) );
78
 
79
  /* Plugin Informations */
81
  add_filter( 'plugin_row_meta', array( $this, 'plugin_row_meta' ), 10, 4 );
82
  add_action( 'yith_wc_multi_vendor_premium_tab', array( $this, 'show_premium_tab' ) );
83
 
84
+ /* Taxonomy management */
85
+ add_action( $this->_taxonomy_name . '_add_form_fields', array( $this, 'add_taxonomy_fields' ), 1, 1 );
86
+ add_action( $this->_taxonomy_name . '_edit_form_fields', array( $this, 'edit_taxonomy_fields' ), 1, 1 );
87
+ add_filter( 'pre_insert_term', array( $this, 'check_duplicate_term_name' ), 10, 2 );
88
+ add_filter( 'edit_terms', array( $this, 'check_duplicate_term_name' ), 10, 2 );
89
+ add_action( 'edited_' . $this->_taxonomy_name, array( $this, 'save_taxonomy_fields' ), 10, 2 );
90
+ add_action( 'created_' . $this->_taxonomy_name, array( $this, 'save_taxonomy_fields' ), 10, 2 );
91
+ add_action( 'pre_delete_term', array( $this, 'remove_vendor_data' ), 10, 2 );
 
92
 
93
  /* Taxonomy Table Management */
94
  add_filter( "bulk_actions-edit-{$this->_taxonomy_name}", '__return_empty_array' );
95
 
96
+ /* Allow html in taxonomy descriptions */
97
+ remove_filter( 'pre_term_description', 'wp_filter_kses' );
98
+ remove_filter( 'term_description', 'wp_kses_data' );
99
 
100
+ /* WooCommerce */
101
+ add_action( 'pre_user_query', array( $this, 'json_search_customer_name' ), 15 );
102
 
103
+ /* Vendor products management */
104
+ add_filter( 'request', array( $this, 'filter_product_list' ) );
105
+ add_filter( 'wp_count_posts', array( $this, 'vendor_count_posts' ), 10, 3 );
106
+ add_action( 'save_post', array( $this, 'add_vendor_taxonomy_to_product' ), 10, 2 );
107
+ add_action( 'current_screen', array( $this, 'disabled_manage_other_vendors_posts' ) );
108
 
109
+ /* Grouped Products */
110
+ add_action( 'pre_get_posts', array( $this, 'filter_vendor_linked_products' ), 10, 1 );
111
 
112
+ /* Vendor media management */
113
+ add_filter( 'ajax_query_attachments_args', array( $this, 'filter_vendor_media' ), 10, 1 );
114
 
115
+ /* Vendor menu */
116
+ add_action( 'admin_menu', array( $this, 'menu_items' ) );
117
+ add_action( 'admin_menu', array( $this, 'remove_media_page' ) );
118
+ add_action( 'admin_menu', array( $this, 'remove_dashboard_widgets' ) );
119
  add_action( 'admin_init', array( YITH_Vendors(), 'remove_wp_bar_admin_menu' ) );
120
  add_action( 'admin_bar_menu', array( $this, 'show_admin_bar_visit_store' ), 31 );
121
 
122
+ /* Vendor information management */
123
+ add_action( 'admin_action_yith_admin_save_fields', array( $this, 'save_taxonomy_fields' ) );
124
 
125
  /* Prevent WooCommerce Access Admin */
126
  add_filter( 'woocommerce_prevent_admin_access', array( $this, 'prevent_admin_access' ) );
138
  /* Custom manage users columns. */
139
  add_filter( 'manage_users_columns', array( $this, 'manage_users_columns' ) );
140
  add_filter( 'manage_users_custom_column', array( $this, 'manage_users_custom_column' ), 10, 3 );
141
+
142
+ /* Manage Vendor Caps */
143
  add_action( 'yit_panel_wc_before_update', array( $this, 'manage_caps' ) );
144
+
145
+ /* Regenerate Permalink after panel update */
146
+ add_action( 'yit_panel_wc_before_update', array( $this, 'flush_rewrite_rules' ) );
147
  }
148
 
149
  /**
152
  * @return void
153
  * @since 1.0
154
  * @author Andrea Grillo <andrea.grillo@yithemes.com>
155
+ * @use /Yit_Plugin_Panel class
156
  * @see plugin-fw/lib/yit-plugin-panel.php
157
  */
158
  public function register_panel() {
159
+ $vendor = yith_get_vendor( 'current', 'user' );
160
 
161
  if ( ! empty( $this->_panel ) ) {
162
  return;
166
  'commissions' => __( 'Commissions', 'yith_wc_product_vendors' ),
167
  'vendors' => __( 'Vendors', 'yith_wc_product_vendors' ),
168
  'premium' => __( 'Premium Version', 'yith_wc_product_vendors' ),
169
+ ) );
 
170
 
171
  $args = array(
172
  'create_menu_page' => true,
184
 
185
  /* === Fixed: not updated theme/old plugin framework === */
186
  if ( ! class_exists( 'YIT_Plugin_Panel_WooCommerce' ) ) {
187
+ require_once( YITH_WPV_PATH . 'plugin-fw/lib/yit-plugin-panel-wc.php' );
188
  }
189
 
190
  $this->_panel = new YIT_Plugin_Panel_WooCommerce( $args );
203
  wp_enqueue_style( 'yith-wc-product-vendors-admin', YITH_WPV_ASSETS_URL . 'css/admin.css', array( 'jquery-chosen' ) );
204
 
205
  /* === Admin Menu Hack === */
206
+ if ( $this->is_vendor_tax_page() ) {
207
  wp_enqueue_script( 'yith-wpv-tax-menu' );
208
  $css = '#adminmenu li#menu-posts-product > a:after{display:none;}
209
  #adminmenu .wp-has-current-submenu .wp-submenu{display: none;}
226
  public function filter_product_list( $request ) {
227
  global $typenow;
228
 
229
+ $vendor = yith_get_vendor( 'current', 'user' );
230
+
231
+ if ( is_admin() && !$vendor->is_super_user() && $vendor->is_user_admin() && 'product' == $typenow ) {
232
+ $slug = $vendor->slug;
233
+ /* WPML Support */
234
+ global $sitepress;
235
+ if( isset( $sitepress ) ){
236
+ $vendor_id = yit_wpml_object_id( $vendor->id, YITH_Vendors()->get_taxonomy_name(), true );
237
+ $wpml_vendor = get_term_by( 'id', $vendor_id, $vendor->term->taxonomy, $current_language );
238
+ $slug = $wpml_vendor->slug;
239
+ }
240
+ $request[ $vendor->term->taxonomy ] = $slug;
241
 
 
 
242
  return apply_filters( "yith_wcmv_{$typenow}_request", $request );
243
  }
244
 
256
  *
257
  * @return arr Modified request
258
  * @since 1.0
259
+ * @use wp_post_count action
260
  */
261
  public function vendor_count_posts( $counts, $type, $perm ) {
262
+ $vendor = yith_get_vendor( 'current', 'user' );
263
 
264
+ if ( !$vendor || 'product' != $type || $vendor->is_super_user() || !$vendor->is_user_admin() ) {
265
  return $counts;
266
  }
267
 
272
 
273
  // Update count object
274
  foreach ( $stati as $status ) {
275
+ $posts = $vendor->get_products( "post_status=$status" );
276
+ $counts->$status = count( $posts );
277
  }
278
 
279
  return $counts;
290
  * @use save_post action
291
  */
292
  public function add_vendor_taxonomy_to_product( $post_id, $post ) {
293
+ $current_vendor = yith_get_vendor( 'current', 'user' );
294
+
295
+ if ( 'product' == $post->post_type && current_user_can( 'edit_post', $post_id ) ) {
296
+ $object_id = $terms = $taxonomy = '';
297
+ if ( $current_vendor->has_limited_access() ) {
298
+ $object_id = $post_id;
299
+ $terms = $current_vendor->term->slug;
300
+ $taxonomy = $current_vendor->term->taxonomy;
301
+ } elseif ( $current_vendor->is_super_user() && !empty( $_POST[ 'tax_input' ][ YITH_Vendors()->get_taxonomy_name() ] ) ) {
302
+ $vendor_id = array_shift( $_POST[ 'tax_input' ][ YITH_Vendors()->get_taxonomy_name() ] );
303
+ $vendor = yith_get_vendor( $vendor_id, 'vendor' );
304
+ if ( $vendor->is_valid() ) {
305
+ $object_id = $post_id;
306
+ $terms = $vendor->term->slug;
307
+ $taxonomy = $vendor->term->taxonomy;
308
+ }
309
+ }
310
 
311
+ wp_set_object_terms( $object_id, $terms, $taxonomy, false );
 
312
  }
313
  }
314
 
321
  * @use current_screen filter
322
  */
323
  public function disabled_manage_other_vendors_posts() {
324
+ if ( isset( $_POST[ 'post_ID' ] ) || !isset( $_GET[ 'post' ] ) ) {
325
+ return;
326
+ }
327
 
328
+ $vendor = yith_get_vendor( 'current', 'user' );
329
+ /* WPML Support */
330
+ $default_language = function_exists( 'wpml_get_default_language' ) ? wpml_get_default_language() : null;
331
+ $post_id = yit_wpml_object_id( $_GET['post'], 'product', true, $default_language );
332
+ $product_vendor = yith_get_vendor( $post_id, 'product' ); // If false, the product hasn't any vendor set
333
+ $post = get_post( $_GET['post'] );
334
 
335
  if ( $vendor->has_limited_access() && false !== $product_vendor && $vendor->id != $product_vendor->id ) {
336
+ wp_die( sprintf( __( 'You do not have permission to edit this product. %1$sClick here to view and edit your products%2$s.', 'yith_wc_product_vendors' ), '<a href="' . esc_url( 'edit.php?post_type=product' ) . '">', '</a>' ) );
337
+ }
338
  }
339
 
340
  /**
347
  * @since 1.0
348
  */
349
  public function filter_vendor_media( $query = array() ) {
350
+ $vendor = yith_get_vendor( 'current', 'user' );
351
 
352
  if ( $vendor->is_valid() && $vendor->has_limited_access() ) {
353
  $vendor_admin_ids = $vendor->get_admins();
354
 
355
+ if ( !empty( $vendor_admin_ids ) ) {
356
+ $query[ 'author__in' ] = $vendor_admin_ids;
357
  }
358
 
359
  }
360
+
361
  return $query;
362
  }
363
 
369
  * @return void
370
  */
371
  public function menu_items() {
372
+ $vendor = yith_get_vendor( 'current', 'user' );
373
 
374
+ if ( !$vendor->is_valid() || !$vendor->has_limited_access() || !$vendor->is_owner() ) {
375
+ return;
376
+ }
377
 
378
+ $menus = apply_filters( 'yith_wc_product_vendors_details_menu_items', array(
 
379
  'vendor_details' => array(
380
+ 'page_title' => __( 'Vendor Profile', 'yith_wc_product_vendors' ),
381
+ 'menu_title' => __( 'Vendor Profile', 'yith_wc_product_vendors' ),
382
+ 'capability' => 'edit_products',
383
+ 'menu_slug' => 'yith_vendor_details',
384
+ 'function' => array( $this, 'admin_details_page' ),
385
+ 'icon' => 'dashicons-id-alt',
386
+ 'position' => 56
387
+ ),
388
+ ) );
389
+
390
+ foreach ( $menus as $menu_args ) {
 
391
  extract( $menu_args );
392
+ add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function, $icon, $position );
393
  }
394
  }
395
 
400
  * @since 1.0.0
401
  * @return void
402
  */
403
+ public function remove_media_page() {
404
  /* Remove Media Library */
405
  $vendor = yith_get_vendor( 'current', 'user' );
406
+ if ( $vendor->is_valid() && $vendor->has_limited_access() ) {
407
  remove_menu_page( 'upload.php' );
408
  }
409
  }
420
  yith_wcpv_get_template( 'vendor-admin', array( 'vendor' => $vendor ), 'admin' );
421
  }
422
 
423
+ /**
424
+ * Update vendor information
425
+ *
426
+ * @param $query object The query object
427
+ *
428
+ * @author Andrea Grillo <andrea.grillo@yithemes.com>
429
+ * @since 1.0.0
430
+ * @fire product_vendors_details_fields_save action
431
+ */
432
  public function filter_vendor_linked_products( $query ) {
433
+ $vendor = yith_get_vendor( 'current', 'user' );
434
+ $action = isset( $_GET[ 'action' ] ) ? $_GET[ 'action' ] : false;
435
 
436
+ if ( $vendor->is_valid() && $vendor->has_limited_access() && ( 'woocommerce_json_search_products' == $action || 'woocommerce_json_search_grouped_products' == $action ) ) {
437
+ $query_args = $vendor->get_query_products_args();
438
+ $query->set( 'tax_query', $query_args[ 'tax_query' ] );
439
  }
440
  }
441
 
442
+ /**
443
+ * Add fields to vendor taxonomy (add new vendor screen)
444
+ *
445
+ * @param str $taxonomy Current taxonomy name
446
+ *
447
+ * @author Andrea Grillo <andrea.grillo@yithemes.com>
448
+ * @return void
449
+ */
450
+ public function add_taxonomy_fields( $taxonomy ) {
451
+
452
+ $args = array( 'commission' => YITH_Vendors()->get_base_commission() );
453
+
454
+ $this->enqueue_ajax_choosen();
455
+
456
+ yith_wcpv_get_template( 'add-product-vendors-taxonomy', $args, 'admin' );
457
+
458
+ $this->add_select_customer_script();
459
+ }
460
+
461
+ /**
462
+ * Edit fields to vendor taxonomy
463
+ *
464
+ * @param WP_Post $vendor Current vendor information
465
+ *
466
+ * @author Andrea Grillo <andrea.grillo@yithemes.com>
467
+ * @return void
468
+ */
469
+ public function edit_taxonomy_fields( $vendor ) {
470
+
471
+ $vendor = yith_get_vendor( $vendor );
472
+ $vendor_admins = $this->vendor_admins_chosen( $vendor );
473
+
474
+ //Get Vendor Owner
475
+ $owner = get_userdata( $vendor->get_owner() );
476
+ $owner_display = $owner instanceof WP_User ? $owner->display_name . '(#' . $owner->ID . ' - ' . $owner->user_email . ')' : '';
477
+ $vendor_owner = !empty( $owner_display ) ? '<option value="' . esc_attr( $owner->ID ) . '" selected="selected">' . $owner_display . '</option>' : '<option></option>';
478
+
479
+ $this->enqueue_ajax_choosen();
480
+
481
+ $args = apply_filters( 'yith_edit_taxonomy_args', array(
482
+ 'owner' => $vendor_owner,
483
+ 'vendor_admins' => $vendor_admins,
484
+ 'vendor' => $vendor
485
+ ) );
486
+
487
+ yith_wcpv_get_template( 'edit-product-vendors-taxonomy', $args, 'admin' );
488
+
489
+ $this->add_select_customer_script();
490
+ }
491
+
492
+ /**
493
+ * Check for duplicate vendor name
494
+ *
495
+ * @author Andrea Grillo <andrea.grillo@yithemes.com>
496
+ *
497
+ * @param $term string The term name
498
+ * @param $taxonomy string The taxonomy name
499
+ *
500
+ * @return mixed term object | WP_Error
501
+ * @since 1.0
502
+ */
503
+ public function check_duplicate_term_name( $term, $taxonomy ) {
504
+
505
+ if ( $this->_taxonomy_name != $taxonomy ) {
506
+ return $term;
507
+ }
508
+
509
+ if ( 'edit_terms' == current_action() && isset( $_POST[ 'name' ] ) ) {
510
+ $duplicate = get_term_by( 'name', $_POST[ 'name' ], $taxonomy );
511
+
512
+ /**
513
+ * If the vendor name exist -> check if is the edited item or not
514
+ */
515
+ if ( $duplicate && $duplicate->term_id == $term ) {
516
+ $duplicate = false;
517
+ }
518
+
519
+ $message = __( 'A vendor with this name already exists.', 'yith_wc_product_vendors' );
520
+ $title = __( 'Vendor name already exists', 'yith_wc_product_vendors' );
521
+ $back_link = admin_url( 'edit-tag.php' );
 
522
 
523
  $back_link = esc_url( add_query_arg( $back_link, array(
524
  'action' => 'edit',
525
+ 'taxonomy' => $_POST[ 'taxonomy' ],
526
+ 'tag_ID' => $_POST[ 'tag_ID' ],
527
  'post_type' => 'product'
528
+ ) ) );
 
 
529
 
530
+ $args = array( 'back_link' => $back_link );
531
 
532
+ return !$duplicate ? $term : wp_die( $message, $title, $args );
533
 
534
+ } else {
535
+ $duplicate = get_term_by( 'name', $term, $taxonomy );
536
 
537
+ return !$duplicate ? $term : new WP_Error( 'term_exists', __( 'A vendor with this name already exists.', 'yith_wc_product_vendors' ), $duplicate );
538
+ }
539
+ }
540
 
541
+ /**
542
+ * Save extra taxonomy fields for product vendors taxonomy
543
+ *
544
+ * @author Andrea Grillo <andrea.grillo@yithemes.com>
545
  *
546
  * @param $vendor_id string The vendor id
547
  *
548
  * @return void
549
  * @since 1.0
550
+ */
551
+ public function save_taxonomy_fields( $vendor_id = 0 ) {
552
 
553
+ if ( !isset( $_POST[ 'yith_vendor_data' ] ) ) {
554
  return;
555
  }
556
 
557
  $is_new = strpos( current_action(), 'created_' ) !== false;
558
 
559
  // if not is set $vendor_id check if there is the update_vendor_id field inside the $_POST array
560
+ if ( empty( $vendor_id ) && isset( $_POST[ 'update_vendor_id' ] ) ) {
561
+ $vendor_id = $_POST[ 'update_vendor_id' ];
562
  }
563
 
564
  $vendor = yith_get_vendor( $vendor_id );
565
 
566
+ if ( !$vendor->is_valid() ) {
567
  return;
568
  }
569
 
570
+ $post_value = $_POST[ 'yith_vendor_data' ];
571
+ $usermeta_owner = YITH_Vendors()->get_user_meta_owner();
572
+ $usermeta_admin = YITH_Vendors()->get_user_meta_key();
573
 
574
+ if ( !$vendor->has_limited_access() ) {
575
  foreach ( apply_filters( 'yith_wpv_save_checkboxes', array( 'enable_selling' ), $vendor->has_limited_access() ) as $key ) {
576
+ !isset( $post_value[ $key ] ) && $post_value[ $key ] = 'no';
577
  }
578
+ } else {
 
 
579
  foreach ( apply_filters( 'yith_wpv_save_checkboxes', array(), $vendor->has_limited_access() ) as $key ) {
580
+ !isset( $post_value[ $key ] ) && $post_value[ $key ] = 'no';
581
  }
582
  }
583
 
584
  // set values
585
  foreach ( $post_value as $key => $value ) {
586
+ $vendor->$key = !is_array( $value ) ? wc_clean( $value ) : $value;
587
  }
588
 
589
  // add vendor registrantion date
590
+ if ( $is_new ) {
591
+ $vendor->registration_date = current_time( 'mysql' );
592
+ $vendor->registration_date_gmt = current_time( 'mysql', 1 );
593
  }
594
 
595
  // Get current vendor admins and owner
596
+ $admins = $vendor->get_admins();
597
+ $owner = $vendor->get_owner();
598
 
599
+ // Remove all current admins (user meta)
600
+ foreach ( $admins as $user_id ) {
601
  $user = get_user_by( 'id', $user_id );
602
  delete_user_meta( $user_id, $usermeta_admin );
603
  $user->remove_role( YITH_Vendors()->get_role_name() );
604
  $user->add_role( 'customer' );
605
+ }
606
+
607
+ // Remove current owner and update it
608
+ if ( !empty( $post_value[ 'owner' ] ) && $owner != $post_value[ 'owner' ] ) {
609
+ delete_user_meta( $owner, $usermeta_owner );
610
+ update_user_meta( intval( $post_value[ 'owner' ] ), $usermeta_owner, $vendor->id );
611
+ $owner = intval( $post_value[ 'owner' ] );
612
+ }
613
+
614
+ //Add Vendor Owner
615
+ if ( !isset( $post_value[ 'admins' ] ) ) {
616
+ $post_value[ 'admins' ] = array( $owner );
617
+ } else {
618
+ $post_value[ 'admins' ][] = $owner;
619
+ }
620
+
621
+ // Only add selected admins
622
+ if ( isset( $post_value[ 'admins' ] ) && !empty( $post_value[ 'admins' ] ) ) {
623
+ foreach ( $post_value[ 'admins' ] as $user_id ) {
624
+ update_user_meta( $user_id, $usermeta_admin, $vendor->id );
625
+ $user = get_user_by( 'id', $user_id );
626
+ if ( $user instanceof WP_User ) {
627
  $user->add_role( YITH_Vendors()->get_role_name() );
628
  $user->remove_role( 'customer' );
629
  }
630
+ }
631
+ }
632
 
633
  do_action( 'yith_wpv_after_save_taxonomy', $vendor, $post_value );
634
 
635
+ if ( 'admin_action_yith_admin_save_fields' == current_action() ) {
636
+ wp_redirect( esc_url_raw( add_query_arg( array( 'page' => $_POST[ 'page' ], 'tab' => $_POST[ 'tab' ] ) ) ) );
637
  }
638
+ }
639
 
640
  /**
641
  * Add or Remove capabilities to vendor admins
654
  if ( '' == $caps ) {
655
  $caps = array_keys( YITH_Vendors()->vendor_enabled_capabilities() );
656
 
657
+ } elseif ( is_string( $caps ) ) {
 
658
  $caps = array( $caps );
659
  }
660
 
661
  foreach ( $caps as $cap ) {
662
  if ( 'add' == $method ) {
663
  $role->add_cap( $cap, true );
664
+ } else {
 
 
665
  $role->remove_cap( $cap );
666
  }
667
  }
668
  }
669
  }
670
 
671
+ /**
672
+ * Remove admin usermeta info
673
+ *
674
+ * @param $term
675
+ * @param $taxonomy
676
+ *
677
+ * @since 1.0.0
678
+ * @return void
679
+ */
680
+ public function remove_admin_usermeta_info( $term, $taxonomy ) {
681
+
682
+ if ( $this->_taxonomy_name != $taxonomy ) {
683
+ return;
684
+ }
685
+
686
+ global $wpdb;
687
+
688
+ $sql = $wpdb->prepare( "SELECT user_id FROM {$wpdb->usermeta} WHERE meta_value=%d AND meta_key=%s", $term, 'yith_product_vendor_owner' );
689
+ $user_id = $wpdb->get_var( $sql );
690
+
691
+ if ( $user_id > 0 ) {
692
+ /**
693
+ * Remove admin caps to user
694
+ */
695
  $user = get_user_by( 'id', $user_id );
696
  $user->remove_role( YITH_Vendors()->get_role_name() );
697
  $user->add_role( 'customer' );
698
 
699
+ /**
700
+ * Remove vendor admin
701
+ */
702
+ delete_user_meta( $user_id, YITH_Vendors()->get_user_meta_key() );
703
+ delete_user_meta( $user_id, YITH_Vendors()->get_user_meta_owner() );
704
+ }
705
+ }
706
 
707
  /**
708
  * Set vendor products to draft
714
  * @since 1.0.0
715
  * @return void
716
  */
717
+ public function set_product_to_orphan( $term, $post_status = 'draft' ) {
718
 
719
  $vendor = yith_get_vendor( $term );
720
  $product_ids = $vendor->get_products();
721
 
722
+ foreach ( $product_ids as $product_id ) {
723
  wp_update_post( array( 'ID' => $product_id, 'post_status' => $post_status ) );
724
  }
725
  }
747
  $this->set_product_to_orphan( $term );
748
  }
749
 
750
+ /**
751
+ * Add select customer scripts
752
+ *
753
+ * @Author Andrea Grillo <andrea.grillo@yithemes.com>
754
+ * @since 1.0.0
755
+ * @return void
756
+ * @use woocommerce_json_search_customers action
757
+ */
758
+ public function add_select_customer_script() {
759
+
760
+ $inline_js = "
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
761
  jQuery('select.ajax_chosen_select_customer').ajaxChosen({
762
  method: 'GET',
763
  url: '" . admin_url( 'admin-ajax.php' ) . "',
782
  });
783
  ";
784
 
785
+ wc_enqueue_js( $inline_js );
786
+ }
787
+
788
+ /**
789
+ * When searching using the WP_User_Query, search names (user meta) too
790
+ *
791
+ * @param object $query The current query object
792
+ *
793
+ * @return void
794
+ * @see WP_User_Query Class wp-includes/user.php
795
+ */
796
+ public function json_search_customer_name( $query ) {
797
+
798
+ if ( isset( $_GET[ 'plugin' ] ) && YITH_WPV_SLUG == $_GET[ 'plugin' ] ) {
799
+ global $wpdb;
800
+
801
+ $term = wc_clean( stripslashes( $_GET[ 'term' ] ) );
802
+ $term = $wpdb->esc_like( $term );
803
+
804
+ $where_old = $wpdb->prepare( ") OR user_name.meta_value LIKE %s ", '%' . $term . '%' );
805
+ $where_new = $wpdb->prepare( " OR user_name.meta_value LIKE %s) ", '%' . $term . '%' );
806
+ $query->query_where = str_replace( $where_old, $where_new, $query->query_where );
807
+ $query->query_where .= $wpdb->prepare( "AND ID NOT IN (SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = %s)", YITH_Vendors()->get_user_meta_key() );
808
+ }
809
+ }
810
+
811
+ /**
812
+ * Print the Single Taxonomy Metabox
813
+ *
814
+ * @author Andrea Grillo <andrea.grillo@yithemes.com>
815
+ * @since 1.0.0
816
+ *
817
+ * @param $taxonomy string Taxonomy Name
818
+ * @param $taxonomy_box string Taxonomy Box
819
+ *
820
+ * @return void
821
+ */
822
  public function single_taxonomy_meta_box( $taxonomy, $taxonomy_box ) {
823
  $taxonomy_label = YITH_Vendors()->get_vendors_taxonomy_label();
824
  $vendor = yith_get_vendor( 'current', 'product' );
827
  'id' => 'tax-input-yith_shop_vendor',
828
  'name' => 'tax_input[yith_shop_vendor]',
829
  'taxonomy' => $this->_taxonomy_name,
830
+ 'show_option_none' => !$vendor->is_super_user() ? '' : sprintf( __( 'No %s' ), strtolower( $taxonomy_label[ 'singular_name' ] ) ),
831
+ 'hide_empty' => !$vendor->is_super_user(),
832
  'selected' => $vendor ? $vendor->id : 0,
833
  'walker' => YITH_Walker_CategoryDropdown(),
834
  'option_none_value' => '', // Avoid to save -1 as new vendor when you create a new product
838
 
839
  if ( $vendor->is_valid() && $vendor->has_limited_access() && $vendor->is_user_admin() ) {
840
  echo $vendor->name;
841
+ } else {
 
842
  wp_dropdown_categories( $args );
843
  }
844
  }
845
 
846
+ /**
847
+ * Add ajax-chosen javascript libr
848
+ *
849
+ * @Author Andrea Grillo <andrea.grillo@yithemes.com>
850
+ * @since 1.0.0
851
+ * @return void
852
+ */
853
+ public function enqueue_ajax_choosen() {
854
+ wp_enqueue_script( 'ajax-chosen' );
855
+ }
856
 
857
  /**
858
+ * If an user is a vendor admin remove the woocommerce prevent admin access
859
+ *
860
+ * @Author Andrea Grillo <andrea.grillo@yithemes.com>
861
+ * @since 1.0.0
862
+ * @return bool
863
+ * @use woocommerce_prevent_admin_access hooks
864
+ */
865
+ public function prevent_admin_access( $prevent_access ) {
866
  $vendor = yith_get_vendor( 'current', 'user' );
867
+
868
  return $vendor->is_valid() && $vendor->has_limited_access() && $vendor->is_user_admin() ? false : $prevent_access;
869
  }
870
 
872
  * Get vendors admin array for choosen select
873
  *
874
  * @Author Andrea Grillo <andrea.grillo@yithemes.com>
875
+ * @since 1.0.0
876
  *
877
  * @param $vendor The vendor object
878
  *
879
  * @return string The admins
880
  */
881
+ public function vendor_admins_chosen( $vendor = '' ) {
882
+ if ( empty( $vendor ) ) {
883
  $vendor = yith_get_vendor( 'current', 'user' );
884
  }
885
 
886
  $admins = '';
887
+ foreach ( $vendor->get_admins() as $user_id ) {
888
+ if ( $vendor->owner != $user_id ) {
889
  $user = get_userdata( $user_id );
890
  $user_display = is_object( $user ) ? $user->display_name . '(#' . $user_id . ' - ' . $user->user_email . ')' : '';
891
  $admins .= '<option value="' . esc_attr( $user_id ) . '" selected="selected">' . $user_display . '</option>';
892
  }
893
+ }
894
+
895
  return $admins;
896
  }
897
 
898
+ /**
899
+ * Action Links
900
+ *
901
+ * add the action links to plugin admin page
902
+ *
903
+ * @param $links | links plugin array
904
+ *
905
+ * @return mixed Array
906
+ * @since 1.0
907
+ * @author Andrea Grillo <andrea.grillo@yithemes.com>
908
+ * @return mixed
909
+ * @use plugin_action_links_{$plugin_file_name}
910
+ */
911
  public function action_links( $links ) {
912
+ $links[] = '<a href="' . admin_url( "admin.php?page={$this->_panel_page}" ) . '">' . __( 'Settings', 'yith_wc_product_vendors' ) . '</a>';
913
+ $premium_live_text = defined( 'YITH_WPV_FREE_INIT' ) ? __( 'Premium live demo', 'yith_wc_product_vendors' ) : __( 'Live demo', 'yith_wc_product_vendors' );
914
+ $links[] = '<a href="' . $this->_premium_live . '" target="_blank">' . $premium_live_text . '</a>';
915
+
916
+ if ( defined( 'YITH_WPV_FREE_INIT' ) ) {
917
+ $links[] = '<a href="' . $this->get_premium_landing_uri() . '" target="_blank">' . __( 'Premium Version', 'yith_wc_product_vendors' ) . '</a>';
918
+ }
919
+
920
+ return $links;
921
+ }
922
+
923
+ /**
924
+ * plugin_row_meta
925
+ *
926
+ * add the action links to plugin admin page
927
+ *
928
+ * @param $plugin_meta
929
+ * @param $plugin_file
930
+ * @param $plugin_data
931
+ * @param $status
932
+ *
933
+ * @return Array
934
+ * @since 1.0
935
+ * @author Andrea Grillo <andrea.grillo@yithemes.com>
936
+ * @use plugin_row_meta
937
+ */
938
+ public function plugin_row_meta( $plugin_meta, $plugin_file, $plugin_data, $status ) {
939
+
940
+ if ( ( defined( 'YITH_WPV_INIT' ) && YITH_WPV_INIT == $plugin_file ) || ( defined( 'YITH_WPV_FREE_INIT' ) && YITH_WPV_FREE_INIT == $plugin_file ) ) {
941
  $plugin_meta[] = '<a href="' . $this->_official_documentation . '" target="_blank">' . __( 'Plugin Documentation', 'yith_wc_product_vendors' ) . '</a>';
942
  }
943
+
944
+ return $plugin_meta;
945
+ }
946
 
947
  /**
948
  * Get the premium landing uri
963
  * @return string The premium landing link
964
  * @return void
965
  */
966
+ public function remove_dashboard_widgets() {
967
  $vendor = yith_get_vendor( 'current', 'user' );
968
 
969
+ if ( $vendor->is_valid() && $vendor->has_limited_access() ) {
970
 
971
  $to_removes = array(
972
  array(
973
+ 'id' => 'woocommerce_dashboard_status',
974
+ 'screen' => 'dashboard',
975
+ 'context' => 'normal'
976
  ),
977
  array(
978
+ 'id' => 'dashboard_activity',
979
+ 'screen' => 'dashboard',
980
+ 'context' => 'normal'
981
  ),
982
  array(
983
+ 'id' => 'woocommerce_dashboard_recent_reviews',
984
+ 'screen' => 'dashboard',
985
+ 'context' => 'normal'
986
  ),
987
  array(
988
+ 'id' => 'dashboard_right_now',
989
+ 'screen' => 'dashboard',
990
+ 'context' => 'normal'
991
  ),
992
  array(
993
+ 'id' => 'dashboard_quick_press',
994
+ 'screen' => 'dashboard',
995
+ 'context' => 'normal'
996
  ),
997
 
998
  );
999
 
1000
+ foreach ( $to_removes as $widget ) {
1001
+ remove_meta_box( $widget[ 'id' ], $widget[ 'screen' ], $widget[ 'context' ] );
1002
  }
1003
  }
1004
  }
1005
 
1006
+ /**
1007
  * Show the premium tabs
1008
  *
1009
  * @since 1.0.0
1010
  * @author Andrea Grillo <andrea.grillo@yithemes.com>
1011
  * @return string The premium landing link
1012
  */
1013
+ public function show_premium_tab() {
1014
  yith_wcpv_get_template( 'premium', array(), 'admin' );
1015
  }
1016
 
1020
  * and is are logged in
1021
  *
1022
  * @author Andrea Grillo <andrea.grillo@yithemes.com>
1023
+ * @since 1.5.1
1024
+ *
1025
  * @param $wp_admin_bar The WP_Admin_Bar object
1026
  */
1027
  public function show_admin_bar_visit_store( $wp_admin_bar ) {
1034
  'id' => 'view-store',
1035
  'title' => __( 'Visit Store', 'woocommerce' ),
1036
  'href' => $vendor->get_url( 'frontend' )
1037
+ ) );
 
1038
  }
1039
  }
1040
 
1042
  * Add an extra body classes for vendors dashboard
1043
  *
1044
  * @author Andrea Grillo <andrea.grillo@yithemes.com>
1045
+ * @since 1.5.1
1046
+ *
1047
  * @param $admin_body_classes
1048
+ *
1049
  * @return string
1050
  */
1051
+ public function admin_body_class( $admin_body_classes ) {
1052
  global $post;
1053
+ $vendor = yith_get_vendor( 'current', 'user' );
1054
+ $is_ajax = defined( 'DOING_AJAX' ) && DOING_AJAX;
1055
+ $is_order_details = is_admin() && !$is_ajax && 'shop_order' == get_current_screen()->id;
1056
+ $refund_management = 'yes' == get_option( 'yith_wpv_vendors_option_order_refund_synchronization', 'no' ) ? true : false;
1057
 
1058
 
1059
+ if ( $vendor->is_valid() && $vendor->has_limited_access() ) {
1060
  $admin_body_classes = $admin_body_classes . ' vendor_limited_access';
1061
+ if ( $is_order_details && $refund_management ) {
1062
  $admin_body_classes = $admin_body_classes . ' vendor_refund_management';
1063
  }
1064
+ } else if ( $vendor->is_super_user() ) {
 
 
1065
  $admin_body_classes = $admin_body_classes . ' vendor_super_user';
1066
 
1067
+ if ( $post && wp_get_post_parent_id( $post->ID ) && 'shop_order' == $post->post_type && $is_order_details ) {
1068
  $admin_body_classes = $admin_body_classes . ' vendor_suborder_detail';
1069
  }
1070
  }
1079
  * @since 1.5.1
1080
  * @return string
1081
  */
1082
+ public function products_to_approve() {
1083
  $vendor = yith_get_vendor( 'current', 'user' );
1084
  /* Add the pending products bubble on Products -> Vendors menu */
1085
  if ( $vendor->is_super_user() ) {
1086
  global $menu, $submenu;
1087
+ $products = get_posts( array( 'post_type' => 'product', 'post_status' => 'pending' ) );
1088
  $num_pending_products = count( $products );
1089
 
1090
  if ( $num_pending_products > 0 ) {
1091
+ $bubble = " <span class='awaiting-mod count-{$num_pending_products}'><span class='pending-count'>{$num_pending_products}</span></span>";
1092
+ $products_uri = htmlspecialchars( add_query_arg( array( 'post_type' => 'product' ), 'edit.php' ) );
1093
 
1094
  foreach ( $menu as $key => $value ) {
1095
+ if ( $menu[ $key ][ 2 ] == $products_uri && $num_pending_products > 0 ) {
1096
+ $menu[ $key ][ 0 ] .= $bubble;
1097
  }
1098
  }
1099
 
1100
  foreach ( $submenu as $key => $value ) {
1101
+ $submenu_items = $submenu[ $key ];
1102
  foreach ( $submenu_items as $position => $value ) {
1103
+ if ( $submenu[ $key ][ $position ][ 2 ] == $products_uri ) {
1104
+ $submenu[ $key ][ $position ][ 0 ] .= $bubble;
1105
+
1106
  return;
1107
  }
1108
  }
1125
  public function filter_order_list( $query ) {
1126
  global $typenow;
1127
 
1128
+ if ( 'shop_order' == $typenow && ( ! isset( $_GET['post_status'] ) || ( isset( $_GET['post_status'] ) && 'trash' != $_GET['post_status'] ) ) ) {
1129
  $vendor = yith_get_vendor( 'current', 'vendor' );
1130
  if ( $vendor->is_super_user() ) {
1131
+ $query[ 'post_parent' ] = 0;
1132
  }
1133
+
1134
  return apply_filters( "yith_wcmv_{$typenow}_request", $query );
1135
  }
1136
+
1137
  return $query;
1138
  }
1139
 
1140
+ /**
1141
  * Filter the post count for vendor
1142
  *
1143
  * @author Andrea Grillo <andrea.grillo@yithemes.com>
1148
  *
1149
  * @return arr Modified request
1150
  * @since 1.0
1151
+ * @use wp_post_count action
1152
  */
1153
  public function vendor_count_shop_orders( $counts, $type, $perm ) {
1154
  if ( 'shop_order' == $type ) {
1156
  * Get a list of post statuses.
1157
  */
1158
  $vendor = yith_get_vendor( 'current', 'user' );
1159
+ $stati = get_post_stati();
1160
 
1161
  if ( $vendor->is_valid() && $vendor->has_limited_access() ) {
1162
  foreach ( $stati as $status ) {
1163
  $orders = $vendor->get_orders( 'suborder', $status );
1164
  $counts->$status = count( $orders );
1165
  }
 
 
1166
 
1167
+ return $counts;
1168
+ } else {
1169
  global $wpdb;
1170
  foreach ( $stati as $status ) {
1171
+ $orders = $wpdb->get_col( $wpdb->prepare( "SELECT DISTINCT ID FROM {$wpdb->posts} WHERE post_parent=%d AND post_status=%s AND post_type=%s", 0, $status, 'shop_order' ) );
 
 
 
 
1172
  $counts->$status = count( $orders );
1173
  }
1174
  }
1175
  }
1176
+
1177
  return $counts;
1178
  }
1179
 
1193
  'post_type' => 'product'
1194
  );
1195
 
1196
+ $menu_args = apply_filters( 'yith_wc_product_vendors_taxonomy_menu_items', array(
 
1197
  'page_title' => YITH_Vendors()->get_vendors_taxonomy_label( 'menu_name' ),
1198
  'menu_title' => YITH_Vendors()->get_vendors_taxonomy_label( 'menu_name' ),
1199
+ 'capability' => 'manage_woocommerce',
1200
  'menu_slug' => htmlspecialchars( add_query_arg( $url_args, 'edit-tags.php' ) ),
1201
  'function' => '',
1202
  'icon' => 'dashicons-admin-multisite',
1203
  'position' => 56
1204
+ ) );
 
1205
  extract( $menu_args );
1206
  add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function, $icon, $position );
1207
  }
1208
  }
1209
 
 
 
 
 
 
 
 
 
 
 
 
1210
  /**
1211
  * Check if current page is the vendor taxonomy page (in admin)
1212
  *
1214
  * @since 1.7
1215
  * @return bool
1216
  */
1217
+ public function is_vendor_tax_page() {
1218
  global $pagenow;
1219
+
1220
+ return 'edit-tags.php' == $pagenow && !empty( $_GET[ 'taxonomy' ] ) && YITH_Vendors()->get_taxonomy_name() == $_GET[ 'taxonomy' ] && !empty( $_GET[ 'post_type' ] ) && 'product' == $_GET[ 'post_type' ];
1221
  }
1222
 
1223
  /**
1242
 
1243
  if ( is_array( $user->roles ) ) {
1244
  foreach ( $user->roles as $role ) {
1245
+ $user_roles[] = translate_user_role( $wp_roles->role_names[ $role ] );
1246
  }
1247
  $output = join( ', ', $user_roles );
1248
  }
1249
  }
1250
+
1251
  return $output;
1252
  }
1253
 
1264
  public function manage_users_columns( $columns ) {
1265
 
1266
  // Unset the core WP `role` column.
1267
+ if ( isset( $columns[ 'role' ] ) ) {
1268
+ unset( $columns[ 'role' ] );
1269
  }
1270
 
1271
  // Add our new roles column.
1272
+ $columns[ 'roles' ] = esc_html__( 'Roles', 'yith_wc_product_vendors' );
1273
+
1274
  return $columns;
1275
  }
1276
+
1277
+ /**
1278
  * Check for order capabilities
1279
  *
1280
  * Add or remove vendor capabilities for coupon and review management
1284
  * @author Andrea Grillo <andrea.grillo@yithemes.com>
1285
  */
1286
  public function manage_caps() {
1287
+ $is_vendor_panel = isset( $_GET[ 'page' ] ) && 'yith_wpv_panel' == $_GET[ 'page' ] ? true : false;
1288
+ $is_vendor_tab = isset( $_GET[ 'tab' ] ) && 'vendors' == $_GET[ 'tab' ];
1289
+ $is_addons_tab = isset( $_GET[ 'tab' ] ) && 'add-ons' == $_GET[ 'tab' ];
1290
+ $addons_caps_key = array_keys( YITH_Vendors()->addons->plugins );
1291
+ if ( $is_vendor_panel && ( $is_vendor_tab || $is_addons_tab ) ) {
1292
  $caps = apply_filters( 'yith_wcmv_manage_role_caps', array(
1293
+ 'order' => array(
1294
+ 'edit_shop_orders' => true,
1295
+ 'read_shop_orders' => true,
1296
+ 'delete_shop_orders' => true,
1297
+ 'publish_shop_orders' => true,
1298
+ 'edit_published_shop_orders' => true,
1299
+ 'delete_published_shop_orders' => true,
1300
  ),
1301
  )
1302
  );
1303
 
1304
  foreach ( array_keys( $caps ) as $option ) {
1305
+ $option_slug = str_replace( '_' , '-' , $option );
1306
+
1307
+ if( $is_vendor_tab && in_array( $option_slug, $addons_caps_key ) ){
1308
+ continue;
1309
+ }
1310
+
1311
+ elseif( $is_addons_tab && ! in_array( $option_slug, $addons_caps_key ) ){
1312
+ continue;
1313
+ }
1314
+
1315
  $option_id = "yith_wpv_vendors_option_{$option}_management";
1316
  $old_value = get_option( $option_id, 'no' );
1317
  $new_value = isset( $_POST[ $option_id ] ) ? 'yes' : 'no';
1318
 
1319
  if ( $old_value != $new_value ) {
1320
+ foreach ( $caps[ $option ] as $key_cap => $key_val ) {
1321
+ $cap = $key_val === true ? $key_cap : $key_val;
1322
  if ( 'no' == $old_value ) {
1323
+ $this->_manage_vendor_caps( 'add', $cap );
1324
+
1325
+ } else {
1326
+ $this->_manage_vendor_caps( 'remove', $cap );
1327
 
 
 
1328
  }
1329
  }
1330
  }
1355
  foreach ( $menu as $position => $args ) {
1356
  $submenu_url = "post-new.php?post_type={$typenow}";
1357
  if ( in_array( $submenu_url, $args ) ) {
1358
+ $submenu[ $section ][ $position ][ 4 ] = 'yith-wcpv-hide-submenu-item';
1359
  break;
1360
  }
1361
  }
1364
  }
1365
  }
1366
 
1367
+ /**
1368
  * Remove add new post types button
1369
  *
1370
  * @return void
1374
  public function remove_add_new_button() {
1375
  global $post_type, $wp_version;
1376
  $rule = false;
1377
+ if ( version_compare( $wp_version, '4.3-RC1', '>=' ) ) {
1378
  $rule = ".post-type-{$post_type} .wrap .page-title-action{ display: none; }";
1379
+ } else {
 
 
1380
  $rule = ".post-type-{$post_type} .add-new-h2{ display: none; }";
1381
  }
1382
 
1383
  wp_add_inline_style( 'yith-wc-product-vendors-admin', $rule );
1384
  }
1385
+
1386
+ /**
1387
+ * Refresh rewrite rules for frontpage
1388
+ *
1389
+ * @return void
1390
+ * @since 1.6.0
1391
+ * @author Andrea Grillo <andrea.grillo@yithemes.com>
1392
+ */
1393
+ public function flush_rewrite_rules(){
1394
+ if( isset( $_GET['page'] ) && $this->_panel_page == $_GET['page'] && isset( $_GET['tab'] ) && 'frontpage' == $_GET['tab'] ) {
1395
+ flush_rewrite_rules();
1396
+ }
1397
+ }
1398
  }
1399
  }
modules/yith-woocommerce-product-vendors/includes/class.yith-vendors.php CHANGED
@@ -141,7 +141,7 @@ if ( ! class_exists( 'YITH_Vendors' ) ) {
141
  /* === START Hooks === */
142
 
143
  /* plugins loaded */
144
- add_action( 'plugins_loaded', array( $this, 'plugin_fw_loader' ) );
145
 
146
  /* init */
147
  add_action( 'init', array( $this, 'init' ) );
@@ -251,13 +251,16 @@ if ( ! class_exists( 'YITH_Vendors' ) ) {
251
  * @return void
252
  */
253
  public function register_vendors_taxonomy() {
254
- $args = array(
255
- 'public' => true,
256
- 'hierarchical' => false,
257
- 'show_admin_column' => true,
258
- 'labels' => $this->get_vendors_taxonomy_label(),
259
- 'rewrite' => array( 'slug' => 'vendor' ),
260
- "meta_box_cb" => array( YITH_Vendors()->admin, 'single_taxonomy_meta_box' )
 
 
 
261
  );
262
 
263
  register_taxonomy( $this->_taxonomy_name, apply_filters( 'yith_wcmv_register_taxonomy_object_type', array( 'product' ) ), $args );
@@ -541,7 +544,14 @@ if ( ! class_exists( 'YITH_Vendors' ) ) {
541
  }
542
  }
543
  }
544
- 'remove_role' == $method && delete_option( 'yith_wcmv_setup');
 
 
 
 
 
 
 
545
  }
546
 
547
  /**
141
  /* === START Hooks === */
142
 
143
  /* plugins loaded */
144
+ add_action( 'plugins_loaded', array( $this, 'plugin_fw_loader' ), 15 );
145
 
146
  /* init */
147
  add_action( 'init', array( $this, 'init' ) );
251
  * @return void
252
  */
253
  public function register_vendors_taxonomy() {
254
+ $slug = get_option( 'yith_wpv_vendor_taxonomy_rewrite', 'vendor' );
255
+ $args = apply_filters( 'yith_wcmv_vendor_taxonomy_args', array(
256
+ 'public' => true,
257
+ 'hierarchical' => false,
258
+ 'show_admin_column' => true,
259
+ 'show_in_menu' => false,
260
+ 'labels' => $this->get_vendors_taxonomy_label(),
261
+ 'rewrite' => array( 'slug' => ! empty( $slug ) ? $slug : 'vendor' ),
262
+ "meta_box_cb" => array( YITH_Vendors()->admin, 'single_taxonomy_meta_box' )
263
+ )
264
  );
265
 
266
  register_taxonomy( $this->_taxonomy_name, apply_filters( 'yith_wcmv_register_taxonomy_object_type', array( 'product' ) ), $args );
544
  }
545
  }
546
  }
547
+
548
+ if( 'remove_role' == $method ) {
549
+ delete_option( 'yith_wcmv_setup');
550
+ delete_option( 'yith_wcmv_version');
551
+ }
552
+
553
+ //regenerate permalink
554
+ flush_rewrite_rules();
555
  }
556
 
557
  /**
modules/yith-woocommerce-product-vendors/includes/functions.yith-update.php CHANGED
@@ -79,3 +79,19 @@ add_action( 'admin_init', 'yith_vendors_update_db_1_0_1' );
79
  add_action( 'admin_init', 'yith_vendors_update_db_1_0_2' );
80
  add_action( 'admin_init', 'yith_vendors_update_db_1_0_3' );
81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
  add_action( 'admin_init', 'yith_vendors_update_db_1_0_2' );
80
  add_action( 'admin_init', 'yith_vendors_update_db_1_0_3' );
81
 
82
+ /**
83
+ * Plugin Version Update
84
+ */
85
+ function yith_vendors_plugin_update() {
86
+ $db_plugin_version = get_option( 'yith_wcmv_version', '1.0.0' );
87
+ if ( version_compare( $db_plugin_version, YITH_Vendors()->version, '<' ) ) {
88
+ /* Check if Vendor Role Exists */
89
+ YITH_Vendors::add_vendor_role();
90
+ /* Add Vendor Role to vendor owner and admins */
91
+ YITH_Vendors::setup( 'add_role' );
92
+ update_option( 'yith_wcmv_version', YITH_Vendors()->version );
93
+ }
94
+ }
95
+
96
+ add_action( 'admin_init', 'yith_vendors_plugin_update', 20 ); //priority set to 20 after vendor register taxonomy
97
+
modules/yith-woocommerce-product-vendors/includes/functions.yith-vendors.php CHANGED
@@ -59,67 +59,49 @@ if ( !function_exists( 'yith_wcpv_check_duplicate_term_name' ) ) {
59
  }
60
  }
61
 
62
- if ( !function_exists( 'yith_has_live_chat_plugin' ) ) {
63
  /**
64
- * Check if user has YITH Live Chat Premium plugin
65
- *
66
- * @author Andrea Grillo <andrea.grillo@yithemes.com>
67
- * @since 1.0
68
- * @return bool
69
- */
70
- function yith_has_live_chat_plugin() {
71
- return defined( 'YLC_PREMIUM' ) && YLC_PREMIUM && defined( 'YLC_VERSION' ) && version_compare( YLC_VERSION, apply_filters( 'yith_wcmv_live_chat_min_version', '1.0.5' ), '>' );
72
- }
73
- }
74
-
75
- if ( !function_exists( 'yith_has_membership_plugin' ) ) {
76
- /**
77
- * Check if user has YITH WooCommerce Membership Premium plugin
78
  *
79
  * @author Leanza Francesco <leanzafrancesco@gmail.com>
80
  * @since 1.0
81
  * @return bool
82
  */
83
- function yith_has_membership_plugin() {
84
- return defined( 'YITH_WCMBS_PREMIUM' ) && YITH_WCMBS_PREMIUM;
85
  }
86
  }
87
 
88
- if ( !function_exists( 'yith_has_size_charts_plugin' ) ) {
89
  /**
90
- * Check if user has YITH Product Size Charts for WooCommerce plugin
91
  *
92
  * @author Leanza Francesco <leanzafrancesco@gmail.com>
93
  * @since 1.0
94
- * @return bool
95
  */
96
- function yith_has_size_charts_plugin() {
97
- return defined( 'YITH_WCPSC_PREMIUM' ) && YITH_WCPSC_PREMIUM && version_compare( YITH_WCPSC_VERSION, apply_filters( 'yith_wcmv_size_charts_min_version', '1.0.4' ), '>=' ) ;
98
- }
99
- }
100
 
101
- if ( !function_exists( 'yith_has_name_your_price_plugin' ) ) {
102
- /**
103
- * Check if user has YITH Product Size Charts for WooCommerce plugin
104
- *
105
- * @author Leanza Francesco <leanzafrancesco@gmail.com>
106
- * @since 1.0
107
- * @return bool
108
- */
109
- function yith_has_name_your_price_plugin() {
110
- return defined( 'YWCNP_PREMIUM' ) && YWCNP_PREMIUM && version_compare( YWCNP_VERSION, apply_filters( 'yith_wcmv_name_your_price_min_version', '1.0.0' ), '>=' ) ;
111
- }
112
- }
113
 
114
- if ( !function_exists( 'yith_has_subscription_plugin' ) ) {
115
- /**
116
- * Check if user has YITH WooCommerce Subscription plugin
117
- *
118
- * @author Leanza Francesco <leanzafrancesco@gmail.com>
119
- * @since 1.0
120
- * @return bool
121
- */
122
- function yith_has_subscription_plugin() {
123
- return defined( 'YITH_YWSBS_PREMIUM' ) && YITH_YWSBS_PREMIUM && version_compare( YITH_YWSBS_VERSION, apply_filters( 'yith_wcmv_subscription_min_version', '1.0.0' ), '>=' ) ;
 
 
 
 
 
 
 
124
  }
125
  }
59
  }
60
  }
61
 
62
+ if ( !function_exists( 'yith_wcmv_is_premium' ) ) {
63
  /**
64
+ * Check if this is the premium version
 
 
 
 
 
 
 
 
 
 
 
 
 
65
  *
66
  * @author Leanza Francesco <leanzafrancesco@gmail.com>
67
  * @since 1.0
68
  * @return bool
69
  */
70
+ function yith_wcmv_is_premium() {
71
+ return defined( 'YITH_WPV_PREMIUM' ) && YITH_WPV_PREMIUM;
72
  }
73
  }
74
 
75
+ if ( !function_exists( 'yith_wcmv_create_capabilities' ) ) {
76
  /**
77
+ * create a capability array
78
  *
79
  * @author Leanza Francesco <leanzafrancesco@gmail.com>
80
  * @since 1.0
81
+ * @return array
82
  */
83
+ function yith_wcmv_create_capabilities( $capability_type ) {
84
+ if ( !is_array( $capability_type ) )
85
+ $capability_type = array( $capability_type, $capability_type . 's' );
 
86
 
87
+ list( $singular_base, $plural_base ) = $capability_type;
 
 
 
 
 
 
 
 
 
 
 
88
 
89
+ $capabilities = array(
90
+ 'edit_' . $singular_base => true,
91
+ 'read_' . $singular_base => true,
92
+ 'delete_' . $singular_base => true,
93
+ 'edit_' . $plural_base => true,
94
+ 'edit_others_' . $plural_base => true,
95
+ 'publish_' . $plural_base => true,
96
+ 'read_private_' . $plural_base => true,
97
+ 'delete_' . $plural_base => true,
98
+ 'delete_private_' . $plural_base => true,
99
+ 'delete_published_' . $plural_base => true,
100
+ 'delete_others_' . $plural_base => true,
101
+ 'edit_private_' . $plural_base => true,
102
+ 'edit_published_' . $plural_base => true,
103
+ );
104
+
105
+ return $capabilities;
106
  }
107
  }
modules/yith-woocommerce-product-vendors/init.php CHANGED
@@ -5,7 +5,7 @@ Plugin URI: http://yithemes.com/themes/plugins/yith-woocommerce-product-vendors/
5
  Description: YITH WooCommerce Multi Vendor is a plugin explicitly developed to switch your website into a platform hosting more than one shop.
6
  Author: yithemes
7
  Text Domain: yith_wc_product_vendors
8
- Version: 1.7.1
9
  Author URI: http://yithemes.com/
10
  */
11
 
@@ -46,7 +46,7 @@ if ( ! function_exists( 'WC' ) ) {
46
  if ( defined( 'YITH_WPV_VERSION' ) ) {
47
  return;
48
  } else {
49
- define( 'YITH_WPV_VERSION', '1.7.1' );
50
  }
51
 
52
  if ( ! defined( 'YITH_WPV_DB_VERSION' ) ) {
5
  Description: YITH WooCommerce Multi Vendor is a plugin explicitly developed to switch your website into a platform hosting more than one shop.
6
  Author: yithemes
7
  Text Domain: yith_wc_product_vendors
8
+ Version: 1.8.0
9
  Author URI: http://yithemes.com/
10
  */
11
 
46
  if ( defined( 'YITH_WPV_VERSION' ) ) {
47
  return;
48
  } else {
49
+ define( 'YITH_WPV_VERSION', '1.8.0' );
50
  }
51
 
52
  if ( ! defined( 'YITH_WPV_DB_VERSION' ) ) {
templates/yith-list-plugins.php CHANGED
@@ -36,8 +36,10 @@ $count_recommended = count( $recommended_modules_list );
36
 
37
  $plugin_filter_status = ! isset( $_GET['plugin_status'] ) ? 'all' : $_GET['plugin_status'];
38
 
 
 
 
39
  //--------------------------------------------------
40
-
41
  if ( isset( $_GET['message'] ) && $_GET['message'] == 'activated' ) : ?>
42
  <div id="message" class="updated notice is-dismissible">
43
  <p><?php _e( 'Module <strong>activated</strong>.', 'yith-essential-kit-for-woocommerce-1' ) ?></p></div>
@@ -104,7 +106,7 @@ elseif ( isset( $_GET['message'] ) && $_GET['message'] == 'deactivated-all' ) :
104
  continue;
105
  }
106
 
107
- $this->print_single_plugin( $module_data, $is_active , $is_new , $is_recommended , $is_premium_installed );
108
 
109
  }
110
 
36
 
37
  $plugin_filter_status = ! isset( $_GET['plugin_status'] ) ? 'all' : $_GET['plugin_status'];
38
 
39
+ $theme = wp_get_theme();
40
+ $uri = $theme->get( 'ThemeURI' );
41
+ $is_referral_theme = strstr( $uri , 'despacho' );
42
  //--------------------------------------------------
 
43
  if ( isset( $_GET['message'] ) && $_GET['message'] == 'activated' ) : ?>
44
  <div id="message" class="updated notice is-dismissible">
45
  <p><?php _e( 'Module <strong>activated</strong>.', 'yith-essential-kit-for-woocommerce-1' ) ?></p></div>
106
  continue;
107
  }
108
 
109
+ $this->print_single_plugin( $module_data, $is_active , $is_new , $is_recommended , $is_premium_installed , $is_referral_theme );
110
 
111
  }
112
 
templates/yith-single-plugin.php CHANGED
@@ -14,6 +14,7 @@ if ( ! defined( 'ABSPATH' ) ) {
14
 
15
  $is_premium_activated = isset( $module_data['premium_constat'] ) && defined( $module_data['premium_constat'] );
16
  $is_yith_repository = ( isset( $module_data['repository'] ) && $module_data['repository'] == 'yith' );
 
17
  // yith repository
18
  if ( $is_yith_repository ) {
19
  $plugin = $module_data;
@@ -54,6 +55,10 @@ if ( ! empty( $author ) ) {
54
  $author = ' <cite>' . sprintf( __( 'By %s' ), $author ) . '</cite>';
55
  }
56
 
 
 
 
 
57
  $action_links = array();
58
  if ( $is_active ) {
59
  $action_links[] = '<a class="deactivate-now button" data-slug="' . $plugin['slug'] . '" href="' . wp_nonce_url( add_query_arg( array( 'action' => 'deactivate', 'module' => $plugin['slug'] ) ), 'deactivate-yit-plugin' ) . '" aria-label="' . sprintf( __( 'Deactivate %s now', 'yith-essential-kit-for-woocommerce-1' ), $plugin['slug'] ) . '" data-name="' . $plugin['name'] . '">' . __( 'Deactivate', 'yith-essential-kit-for-woocommerce-1' ) . '</a>';
@@ -90,7 +95,7 @@ else if ( $is_premium_installed ) {
90
  $btn_title = __( 'Activate Premium' );
91
  }
92
  else {
93
- $premium_url = 'http://yithemes.com/themes/plugins/' . ( isset( $module_data['premium-url'] ) ? $module_data['premium-url'] : $plugin['slug'] );
94
  $btn_class = 'btn-premium tobuy';
95
  $btn_title = __( 'Buy Premium Version' );
96
  }
14
 
15
  $is_premium_activated = isset( $module_data['premium_constat'] ) && defined( $module_data['premium_constat'] );
16
  $is_yith_repository = ( isset( $module_data['repository'] ) && $module_data['repository'] == 'yith' );
17
+ $referral_arg = $is_referral_theme ? '?refer_id=1036888' : '' ;
18
  // yith repository
19
  if ( $is_yith_repository ) {
20
  $plugin = $module_data;
55
  $author = ' <cite>' . sprintf( __( 'By %s' ), $author ) . '</cite>';
56
  }
57
 
58
+ if( !empty( $referral_arg ) ) {
59
+ $author = str_replace('http://yithemes.com/' , 'http://yithemes.com/'.$referral_arg , $author);
60
+ }
61
+
62
  $action_links = array();
63
  if ( $is_active ) {
64
  $action_links[] = '<a class="deactivate-now button" data-slug="' . $plugin['slug'] . '" href="' . wp_nonce_url( add_query_arg( array( 'action' => 'deactivate', 'module' => $plugin['slug'] ) ), 'deactivate-yit-plugin' ) . '" aria-label="' . sprintf( __( 'Deactivate %s now', 'yith-essential-kit-for-woocommerce-1' ), $plugin['slug'] ) . '" data-name="' . $plugin['name'] . '">' . __( 'Deactivate', 'yith-essential-kit-for-woocommerce-1' ) . '</a>';
95
  $btn_title = __( 'Activate Premium' );
96
  }
97
  else {
98
+ $premium_url = 'http://yithemes.com/themes/plugins/' . ( isset( $module_data['premium-url'] ) ? $module_data['premium-url'] : $plugin['slug'] ).$referral_arg;
99
  $btn_class = 'btn-premium tobuy';
100
  $btn_title = __( 'Buy Premium Version' );
101
  }
yith-jetpack.php CHANGED
@@ -617,7 +617,7 @@ if ( ! class_exists( 'YITH_JetPack' ) ) {
617
  *
618
  * @since 1.0.0
619
  */
620
- public function print_single_plugin( $module_data, $is_active , $is_new , $is_recommended , $is_premium_installed ) {
621
 
622
  $plugins_allowedtags = array(
623
  'a' => array( 'href' => array(), 'title' => array(), 'target' => array() ),
617
  *
618
  * @since 1.0.0
619
  */
620
+ public function print_single_plugin( $module_data, $is_active , $is_new , $is_recommended , $is_premium_installed , $is_referral_theme ) {
621
 
622
  $plugins_allowedtags = array(
623
  'a' => array( 'href' => array(), 'title' => array(), 'target' => array() ),