WJECF_WC

Interface to WooCommerce.

Defined (1)

The class is defined in the following location(s).

/includes/wjecf-wc.php  
  1. class WJECF_WC { 
  2.  
  3. protected $wrappers = array(); 
  4.  
  5. /** 
  6. * Wrap a data object (WC 2.7 introduced WC_Data) 
  7. * @param type $object  
  8. * @return type 
  9. */ 
  10. public function wrap( $object, $use_pool = true ) { 
  11. if ( $use_pool ) { 
  12. //Prevent a huge amount of wrappers to be initiated; one wrapper per object instance should do the trick 
  13. foreach( $this->wrappers as $wrapper ) { 
  14. if ($wrapper->holds( $object ) ) { 
  15. //error_log('Reusing wrapper ' . get_class( $object ) ); 
  16. return $wrapper; 
  17.  
  18. if ( is_numeric( $object ) ) { 
  19. $post_type = get_post_type( $object ); 
  20. if ( $post_type == 'shop_coupon' ) { 
  21. $object = WJECF_WC()->get_coupon( $object ); 
  22. } elseif ( $post_type == 'product' ) { 
  23. $object = new WC_Product( $object ); 
  24. }  
  25.  
  26.  
  27. if ( $object instanceof WC_Coupon ) { 
  28. return $this->wrappers[] = new WJECF_Wrap_Coupon( $object ); 
  29.  
  30. if ( $object instanceof WC_Product ) { 
  31. return $this->wrappers[] = new WJECF_Wrap_Product( $object ); 
  32.  
  33. throw new Exception( 'Cannot wrap ' . get_class( $object ) ); 
  34.  
  35. /** 
  36. * Returns a specific item in the cart. 
  37. * @param string $cart_item_key Cart item key. 
  38. * @return array Item data 
  39. */ 
  40. public function get_cart_item( $cart_item_key ) {  
  41. if ( $this->check_woocommerce_version('2.2.9') ) { 
  42. return WC()->cart->get_cart_item( $cart_item_key ); 
  43.  
  44. return isset( WC()->cart->cart_contents[ $cart_item_key ] ) ? WC()->cart->cart_contents[ $cart_item_key ] : array(); 
  45.  
  46. /** 
  47. * Get categories of a product (and anchestors) 
  48. * @param int $product_id  
  49. * @return array product_cat_ids 
  50. */ 
  51. public function wc_get_product_cat_ids( $product_id ) { 
  52. //Since WC 2.5.0 
  53. if ( function_exists( 'wc_get_product_cat_ids' ) ) { 
  54. return wc_get_product_cat_ids( $product_id ); 
  55.  
  56. $product_cats = wp_get_post_terms( $product_id, 'product_cat', array( "fields" => "ids" ) ); 
  57.  
  58. foreach ( $product_cats as $product_cat ) { 
  59. $product_cats = array_merge( $product_cats, get_ancestors( $product_cat, 'product_cat' ) ); 
  60. return $product_cats; 
  61.  
  62. /** 
  63. * Coupon types that apply to individual products. Controls which validation rules will apply. 
  64. * @since 2.5.0 
  65. * @return array 
  66. */ 
  67. public function wc_get_product_coupon_types() { 
  68. //Since WC 2.5.0 
  69. if ( function_exists( 'wc_get_product_coupon_types' ) ) { 
  70. return wc_get_product_coupon_types(); 
  71. return array( 'fixed_product', 'percent_product' ); 
  72.  
  73. public function wc_get_cart_coupon_types() { 
  74. //Since WC 2.5.0 
  75. if ( function_exists( 'wc_get_cart_coupon_types' ) ) { 
  76. return wc_get_cart_coupon_types(); 
  77. return array( 'fixed_cart', 'percent' ); 
  78. }  
  79.  
  80. /** 
  81. * @since 2.4.0 for WC 2.7 compatibility 
  82. *  
  83. * Get a WC_Coupon object 
  84. * @param WC_Coupon|string $coupon The coupon code or a WC_Coupon object 
  85. * @return WC_Coupon The coupon object 
  86. */ 
  87. public function get_coupon( $coupon ) { 
  88. if ( is_numeric( $coupon ) ) { 
  89. //By id 
  90. global $wpdb; 
  91. $coupon = $wpdb->get_var( $wpdb->prepare( "SELECT post_title FROM $wpdb->posts WHERE id = %s AND post_type = 'shop_coupon' AND post_status = 'publish'", $coupon ) ); 
  92. if ( ! ( $coupon instanceof WC_Coupon ) ) { 
  93. //By code 
  94. $coupon = new WC_Coupon( $coupon ); 
  95. return $coupon; 
  96. }  
  97.  
  98. //ADMIN 
  99.  
  100. /** 
  101. * Display a WooCommerce help tip 
  102. * @param string $tip The tip to display 
  103. * @return string 
  104. */ 
  105. public function wc_help_tip( $tip ) { 
  106. //Since WC 2.5.0 
  107. if ( function_exists( 'wc_help_tip' ) ) { 
  108. return wc_help_tip( $tip ); 
  109.  
  110. return '<img class="help_tip" style="margin-top: 21px;" data-tip="' . esc_attr( $tip ) . '" src="' . esc_url( WC()->plugin_url() ) . '/assets/images/help.png" height="16" width="16" />'; 
  111.  
  112. /** 
  113. * Renders a product selection <input>. Will use either select2 v4 (WC3.0+) select2 v3 (WC2.3+) or chosen (< WC2.3) 
  114. * @param string $dom_id  
  115. * @param string $field_name  
  116. * @param array $selected_ids Array of integers 
  117. * @param string|null $placeholder  
  118. * @return void 
  119. */ 
  120. public function render_admin_product_selector( $dom_id, $field_name, $selected_ids, $placeholder = null ) { 
  121. $product_key_values = array(); 
  122. foreach ( $selected_ids as $product_id ) { 
  123. $product = wc_get_product( $product_id ); 
  124. if ( is_object( $product ) ) { 
  125. $product_key_values[ esc_attr( $product_id ) ] = wp_kses_post( $product->get_formatted_name() ); 
  126.  
  127. if ( $placeholder === null ) $placeholder = __( 'Search for a product*', 'woocommerce' ); 
  128.  
  129. //In WooCommerce version 2.3.0 chosen was replaced by select2 
  130. //In WooCommerce version 3.0 select2 v3 was replaced by select2 v4 
  131. if ( $this->check_woocommerce_version('3.0') ) { 
  132. $this->render_admin_select2_v4_product_selector( $dom_id, $field_name, $product_key_values, $placeholder ); 
  133. } elseif ( $this->check_woocommerce_version('2.3.0') ) { 
  134. $this->render_admin_select2_product_selector( $dom_id, $field_name, $product_key_values, $placeholder ); 
  135. } else { 
  136. $this->render_admin_chosen_product_selector( $dom_id, $field_name, $product_key_values, $placeholder ); 
  137.  
  138.  
  139. /** 
  140. * Renders a product selection <input>.  
  141. * Chosen (Legacy) 
  142. * @param string $dom_id  
  143. * @param string $field_name  
  144. * @param array $selected_ids Array of integers 
  145. * @param string|null $placeholder  
  146. */  
  147. private function render_admin_chosen_product_selector( $dom_id, $field_name, $selected_keys_and_values, $placeholder ) { 
  148. // $selected_keys_and_values must be an array of [ id => name ] 
  149.  
  150. echo '<select id="' . esc_attr( $dom_id ) . '" name="' . esc_attr( $field_name ) . '[]" class="ajax_chosen_select_products_and_variations" multiple="multiple" data-placeholder="' . esc_attr( $placeholder ) . '">'; 
  151. foreach ( $selected_keys_and_values as $product_id => $product_name ) { 
  152. echo '<option value="' . $product_id . '" selected="selected">' . $product_name . '</option>'; 
  153. echo '</select>'; 
  154.  
  155. /** 
  156. * @since 2.4.1 for WC 3.0 compatibility 
  157. *  
  158. * Renders a product selection <input>.  
  159. * Select2 version 3 (Since WC 2.3.0) 
  160. * @param string $dom_id  
  161. * @param string $field_name  
  162. * @param array $selected_ids Array of integers 
  163. * @param string|null $placeholder  
  164. */  
  165. private function render_admin_select2_product_selector( $dom_id, $field_name, $selected_keys_and_values, $placeholder ) { 
  166. // $selected_keys_and_values must be an array of [ id => name ] 
  167.  
  168. $json_encoded = esc_attr( json_encode( $selected_keys_and_values ) ); 
  169. echo '<input type="hidden" class="wc-product-search" data-multiple="true" style="width: 50%;" name="'  
  170. . esc_attr( $field_name ) . '" data-placeholder="'  
  171. . esc_attr( $placeholder ) . '" data-action="woocommerce_json_search_products_and_variations" data-selected="'  
  172. . $json_encoded . '" value="' . implode( ', ', array_keys( $selected_keys_and_values ) ) . '" />'; 
  173.  
  174. }  
  175.  
  176. /** 
  177. * Renders a product selection <input>.  
  178. * Select2 version 4 (Since WC 3.0) 
  179. * @param string $dom_id  
  180. * @param string $field_name  
  181. * @param string $selected_keys_and_values  
  182. * @param string $placeholder  
  183. */ 
  184. private function render_admin_select2_v4_product_selector( $dom_id, $field_name, $selected_keys_and_values, $placeholder ) { 
  185. // $selected_keys_and_values must be an array of [ id => name ] 
  186.  
  187. $json_encoded = esc_attr( json_encode( $selected_keys_and_values ) ); 
  188.  
  189. echo '<select id="'. esc_attr( $dom_id ) .'" class="wc-product-search" name="' 
  190. . esc_attr( $field_name ) . '[]" multiple="multiple" style="width: 50%;" data-placeholder="' 
  191. . esc_attr( $placeholder ) . '" data-action="woocommerce_json_search_products_and_variations">'; 
  192.  
  193. foreach ( $selected_keys_and_values as $product_id => $product_name ) { 
  194. echo '<option value="' . esc_attr( $product_id ) . '"' . selected( true, true, false ) . '>' . wp_kses_post( $product_name ) . '</option>'; 
  195.  
  196. echo '</select>'; 
  197.  
  198.  
  199. /** 
  200. * Renders a customer selection <input>. Will use either select2 v4 (WC3.0+) or select2 v3 (WC2.3+) 
  201. * @param string $dom_id  
  202. * @param string $field_name  
  203. * @param array $selected_customer_ids Array of integers 
  204. * @param string|null $placeholder  
  205. * @return void 
  206. */ 
  207. public function render_admin_customer_selector( $dom_id, $field_name, $selected_customer_ids, $placeholder = null ) { 
  208. $selected_keys_and_values = array();  
  209. foreach ( $selected_customer_ids as $customer_id ) { 
  210. $customer = get_userdata( $customer_id ); 
  211. if ( is_object( $customer ) ) { 
  212. $selected_keys_and_values[ $customer_id ] = $customer->display_name . ' (#' . $customer->ID . ' – ' . sanitize_email( $customer->user_email ) . ')'; 
  213. if ( $placeholder === null ) $placeholder = __( 'Any customer', 'woocommerce-jos-autocoupon' ); 
  214.  
  215. //In WooCommerce version 2.3.0 chosen was replaced by select2 
  216. //In WooCommerce version 3.0 select2 v3 was replaced by select2 v4 
  217. if ( $this->check_woocommerce_version('3.0') ) { 
  218. $this->render_admin_select2_v4_customer_selector( $dom_id, $field_name, $selected_keys_and_values, $placeholder ); 
  219. } else { 
  220. $this->render_admin_select2_customer_selector( $dom_id, $field_name, $selected_keys_and_values, $placeholder ); 
  221.  
  222. private function render_admin_select2_customer_selector( $dom_id, $field_name, $selected_keys_and_values, $placeholder ) { 
  223. // $selected_keys_and_values must be an array of [ id => name ] .e.g. [ 12 => 'John Smith (#12 john.smith@example.com)' ] 
  224.  
  225. $json_encoded = esc_attr( json_encode( $selected_keys_and_values ) ); 
  226. echo '<input type="hidden" class="wc-customer-search" data-multiple="true" style="width: 50%;" name="' 
  227. . esc_attr( $field_name ) . '" data-placeholder="' 
  228. . esc_attr( $placeholder ) . '" data-action="woocommerce_json_search_customers" data-selected="' 
  229. . $json_encoded . '" value="' . implode( ', ', array_keys( $selected_keys_and_values ) ) . '" />'; 
  230.  
  231. private function render_admin_select2_v4_customer_selector( $dom_id, $field_name, $selected_keys_and_values, $placeholder ) { 
  232. // $selected_keys_and_values must be an array of [ id => name ] 
  233.  
  234. $json_encoded = esc_attr( json_encode( $selected_keys_and_values ) ); 
  235.  
  236. echo '<select id="'. esc_attr( $dom_id ) .'" class="wc-customer-search" name="' 
  237. . esc_attr( $field_name ) . '[]" multiple="multiple" style="width: 50%;" data-placeholder="' 
  238. . esc_attr( $placeholder ) . '" data-action="woocommerce_json_search_customers">'; 
  239.  
  240. foreach ( $selected_keys_and_values as $key => $value ) { 
  241. echo '<option value="' . esc_attr( $key ) . '"' . selected( true, true, false ) . '>' . wp_kses_post( $value ) . '</option>'; 
  242.  
  243. echo '</select>'; 
  244. }  
  245.  
  246.  
  247. //VERSION 
  248.  
  249. /** 
  250. * Check whether WooCommerce version is greater or equal than $req_version 
  251. * @param string @req_version The version to compare to 
  252. * @return bool true if WooCommerce is at least the given version 
  253. */ 
  254. public function check_woocommerce_version( $req_version ) { 
  255. return version_compare( $this->get_woocommerce_version(), $req_version, '>=' ); 
  256. }  
  257.  
  258. private $wc_version = null; 
  259.  
  260. /** 
  261. * Get the WooCommerce version number 
  262. * @return string|bool WC Version number or false if WC not detected 
  263. */ 
  264. public function get_woocommerce_version() { 
  265. if ( isset( $this->wc_version ) ) { 
  266. return $this->wc_version; 
  267.  
  268. if ( defined( 'WC_VERSION' ) ) { 
  269. return $this->wc_version = WC_VERSION; 
  270.  
  271. // If get_plugins() isn't available, require it 
  272. if ( ! function_exists( 'get_plugins' ) ) { 
  273. require_once( ABSPATH . 'wp-admin/includes/plugin.php' ); 
  274. }  
  275. // Create the plugins folder and file variables 
  276. $plugin_folder = get_plugins( '/woocommerce' ); 
  277. $plugin_file = 'woocommerce.php'; 
  278.  
  279. // If the plugin version number is set, return it  
  280. if ( isset( $plugin_folder[$plugin_file]['Version'] ) ) { 
  281. return $this->wc_version = $plugin_folder[$plugin_file]['Version']; 
  282.  
  283. return $this->wc_version = false; // Not found 
  284.  
  285. //INSTANCE 
  286.  
  287. /** 
  288. * Singleton Instance 
  289. * @static 
  290. * @return Singleton Instance 
  291. */ 
  292. public static function instance() { 
  293. if ( is_null( self::$_instance ) ) { 
  294. self::$_instance = new self(); 
  295. return self::$_instance; 
  296. protected static $_instance = null;