WC_POS_Products_Visibility

Handles the POS Only functionality (optional).

Defined (1)

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

/includes/products/class-wc-pos-visibility.php  
  1. class WC_POS_Products_Visibility { 
  2.  
  3. protected $options; 
  4.  
  5. /** 
  6. * Constructor 
  7. */ 
  8. public function __construct() { 
  9.  
  10. // visibility options 
  11. $this->options = array( 
  12. '' => __( 'POS & Online', 'woocommerce-pos' ),  
  13. 'pos_only' => __( 'POS Only', 'woocommerce-pos' ),  
  14. 'online_only' => __( 'Online Only', 'woocommerce-pos' ) 
  15. ); 
  16.  
  17. add_filter( 'posts_where', array( $this, 'posts_where' ), 10 , 2 ); 
  18. add_filter( 'views_edit-product', array( $this, 'pos_visibility_filters' ), 10, 1 ); 
  19. add_action( 'pre_get_posts', array( $this, 'pre_get_posts' ), 10, 1 ); 
  20. add_action( 'bulk_edit_custom_box', array( $this, 'bulk_edit' ), 10, 2 ); 
  21. add_action( 'quick_edit_custom_box', array( $this, 'quick_edit'), 10, 2 ); 
  22. add_action( 'save_post', array( $this, 'save_post' ), 10, 2 ); 
  23. add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) ); 
  24. add_action( 'manage_product_posts_custom_column' , array( $this, 'custom_product_column' ), 10, 2 ); 
  25. add_action( 'post_submitbox_misc_actions', array( $this, 'post_submitbox_misc_actions' ), 99 ); 
  26.  
  27.  
  28. /** 
  29. * Show/hide POS products 
  30. * @param $where 
  31. * @param $query 
  32. * @return string 
  33. */ 
  34. public function posts_where( $where, $query ) { 
  35. global $wpdb; 
  36.  
  37. // only alter product queries 
  38. if( is_array( $query->get('post_type') ) && !in_array( 'product', $query->get('post_type') ) ) 
  39. return $where; 
  40.  
  41. if( !is_array( $query->get('post_type') ) && $query->get('post_type') !== 'product' ) 
  42. return $where; 
  43.  
  44. // don't alter product queries in the admin 
  45. if( is_admin() && !is_pos() ) 
  46. return $where; 
  47.  
  48. // hide products 
  49. if( is_pos() ) { 
  50. $hide = 'online_only'; 
  51. } else { 
  52. $hide = 'pos_only'; 
  53.  
  54. $where .= " AND ID NOT IN (SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = '_pos_visibility' AND meta_value = '$hide')"; 
  55.  
  56. return $where; 
  57.  
  58.  
  59. /** 
  60. * Admin filters for POS / Online visibility 
  61. * @param array $views 
  62. * @return array 
  63. */ 
  64. public function pos_visibility_filters( $views ) { 
  65. global $wpdb; 
  66.  
  67. $visibility_filters = array( 
  68. // 'pos_and_online' => __( 'POS & Online', 'woocommerce-pos' ),  
  69. 'pos_only' => __( 'POS Only', 'woocommerce-pos' ),  
  70. 'online_only' => __( 'Online Only', 'woocommerce-pos' ) 
  71. ); 
  72.  
  73. if ( isset( $_GET['pos_visibility'] ) && !empty( $_GET['pos_visibility'] ) ) { 
  74. $views['all'] = str_replace( 'class="current"', '', $views['all'] ); 
  75.  
  76. foreach( $visibility_filters as $key => $label ) { 
  77.  
  78. $sql = "SELECT count(DISTINCT pm.post_id) 
  79. FROM $wpdb->postmeta pm 
  80. JOIN $wpdb->posts p ON (p.ID = pm.post_id) 
  81. WHERE pm.meta_key = '_pos_visibility' 
  82. AND pm.meta_value = '$key' 
  83. AND p.post_type = 'product' 
  84. AND p.post_status = 'publish' 
  85. "; 
  86. $count = $wpdb->get_var($sql); 
  87.  
  88. $class = ( isset( $_GET['pos_visibility'] ) && $_GET['pos_visibility'] == $key ) ? 'current' : ''; 
  89. if( $class == '' ) $query_string = remove_query_arg(array( 'paged' )); 
  90. $query_string = remove_query_arg(array( 'pos_visibility', 'post_status' )); 
  91. $query_string = add_query_arg( 'pos_visibility', urlencode($key), $query_string ); 
  92. $views[$key] = '<a href="'. $query_string . '" class="' . esc_attr( $class ) . '">' . $label . ' <span class="count">(' . number_format_i18n( $count ) . ')</a></a>'; 
  93.  
  94. return $views; 
  95.  
  96. /** 
  97. * Filter the results on the products admin page 
  98. * @param $query 
  99. */ 
  100. public function pre_get_posts( $query ) { 
  101.  
  102. // product page 
  103. if ( is_admin() && get_query_var( 'post_type' ) == 'product' ) { 
  104.  
  105. if ( isset( $_GET['pos_visibility'] ) && ! empty( $_GET['pos_visibility'] ) ) { 
  106. $meta_query = array( 
  107. array( 
  108. 'key' => '_pos_visibility',  
  109. 'value' => $_GET['pos_visibility'],  
  110. 'compare' => '==' 
  111. ); 
  112.  
  113. $query->set( 'meta_query', $meta_query ); 
  114.  
  115.  
  116. /** 
  117. * @param $column_name 
  118. * @param $post_type 
  119. */ 
  120. public function bulk_edit($column_name, $post_type) { 
  121. if ( 'name' != $column_name || 'product' != $post_type ) { 
  122. return; 
  123. $options = array_merge( 
  124. array('-1' => '— No Change —'), $this->options 
  125. ); 
  126. include 'views/quick-edit-visibility-select.php'; 
  127.  
  128. /** 
  129. * @param $column_name 
  130. * @param $post_type 
  131. */ 
  132. public function quick_edit($column_name, $post_type) { 
  133. if ( 'product_cat' != $column_name || 'product' != $post_type ) { 
  134. return; 
  135. $options = $this->options; 
  136. include 'views/quick-edit-visibility-select.php'; 
  137.  
  138. /** 
  139. * @param $post_id 
  140. * @param $post 
  141. */ 
  142. public function save_post( $post_id, $post ) { 
  143.  
  144. // If this is an autosave, our form has not been submitted, so we don't want to do anything. 
  145. if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { 
  146. return; 
  147.  
  148. // Don't save revisions and autosaves 
  149. if ( wp_is_post_revision( $post_id ) || wp_is_post_autosave( $post_id ) ) { 
  150. return; 
  151.  
  152. // Check post type is product 
  153. if ( 'product' != $post->post_type ) { 
  154. return; 
  155.  
  156. // Check user permission 
  157. if ( ! current_user_can( 'edit_post', $post_id ) ) { 
  158. return; 
  159.  
  160. // Check nonces 
  161. if ( ! isset( $_REQUEST['woocommerce_quick_edit_nonce'] ) && 
  162. !isset( $_REQUEST['woocommerce_bulk_edit_nonce'] ) && 
  163. !isset( $_REQUEST['woocommerce_meta_nonce'] ) ) { 
  164. return; 
  165. if ( isset( $_REQUEST['woocommerce_quick_edit_nonce'] ) && 
  166. !wp_verify_nonce( $_REQUEST['woocommerce_quick_edit_nonce'], 'woocommerce_quick_edit_nonce' ) ) { 
  167. return; 
  168. if ( isset( $_REQUEST['woocommerce_bulk_edit_nonce'] ) && 
  169. !wp_verify_nonce( $_REQUEST['woocommerce_bulk_edit_nonce'], 'woocommerce_bulk_edit_nonce' ) ) { 
  170. return; 
  171. if ( isset( $_REQUEST['woocommerce_meta_nonce'] ) && 
  172. !wp_verify_nonce( $_REQUEST['woocommerce_meta_nonce'], 'woocommerce_save_data' ) ) { 
  173. return; 
  174.  
  175. // Get the product and save 
  176. if ( isset( $_REQUEST['_pos_visibility'] ) ) { 
  177. update_post_meta( $post_id, '_pos_visibility', $_REQUEST['_pos_visibility'] ); 
  178.  
  179.  
  180. /** 
  181. * @param $hook 
  182. */ 
  183. public function admin_enqueue_scripts( $hook ) { 
  184. $pages = array('edit.php', 'post.php', 'post-new.php'); 
  185. $screen = get_current_screen(); 
  186.  
  187. if( !in_array( $hook, $pages ) || $screen->post_type != 'product' ) 
  188. return; 
  189.  
  190. if(defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG) { 
  191. $script = WC_POS_PLUGIN_URL . 'assets/js/src/products.js'; 
  192. } else { 
  193. $script = WC_POS_PLUGIN_URL . 'assets/js/products.min.js'; 
  194.  
  195. wp_enqueue_script( 
  196. WC_POS_PLUGIN_NAME . '-admin-edit',  
  197. $script,  
  198. false,  
  199. WC_POS_VERSION,  
  200. true 
  201. ); 
  202.  
  203. /** 
  204. * @param $column 
  205. * @param $post_id 
  206. */ 
  207. public function custom_product_column($column, $post_id) { 
  208. if( $column == 'name') { 
  209. $selected = get_post_meta( $post_id , '_pos_visibility' , true ); 
  210. echo '<div class="hidden" id="woocommerce_pos_inline_'. $post_id .'" data-visibility="'. $selected .'"></div>'; 
  211.  
  212. /** 
  213. * Add visibility option to the Product edit page 
  214. */ 
  215. public function post_submitbox_misc_actions() { 
  216. global $post; 
  217.  
  218. if ( 'product' != $post->post_type ) { 
  219. return; 
  220.  
  221. $selected = get_post_meta( $post->ID , '_pos_visibility' , true ); 
  222. if( !$selected ) { $selected = ''; } 
  223. include 'views/post-metabox-visibility-select.php'; 
  224.