Overview
  • Namespace
  • Class

Namespaces

  • None
  • WC_POS
    • Admin
      • Settings
        • Receipt
    • API
    • Gateways
    • Integrations
    • Products

Classes

  • WC_POS\Activator
  • WC_POS\Admin
  • WC_POS\Admin\Gateways
  • WC_POS\Admin\Menu
  • WC_POS\Admin\Notices
  • WC_POS\Admin\Orders
  • WC_POS\Admin\Page
  • WC_POS\Admin\Permalink
  • WC_POS\Admin\Plugins
  • WC_POS\Admin\Products
  • WC_POS\Admin\Settings
  • WC_POS\Admin\Settings\Access
  • WC_POS\Admin\Settings\Checkout
  • WC_POS\Admin\Settings\Customers
  • WC_POS\Admin\Settings\Gateways
  • WC_POS\Admin\Settings\General
  • WC_POS\Admin\Settings\HotKeys
  • WC_POS\Admin\Settings\Page
  • WC_POS\Admin\Settings\Receipt\Options
  • WC_POS\Admin\Settings\Receipt\Template
  • WC_POS\Admin\Settings\Receipts
  • WC_POS\Admin\Settings\Status
  • WC_POS\Admin\Settings\Tools
  • WC_POS\Admin\Status
  • WC_POS\Ajax
  • WC_POS\API
  • WC_POS\API\Coupons
  • WC_POS\API\Customers
  • WC_POS\API\Gateways
  • WC_POS\API\i18n
  • WC_POS\API\Orders
  • WC_POS\API\Params
  • WC_POS\API\Payload
  • WC_POS\API\Products
  • WC_POS\API\Settings
  • WC_POS\API\Support
  • WC_POS\API\Templates
  • WC_POS\Customers
  • WC_POS\Deactivator
  • WC_POS\Gateways
  • WC_POS\Gateways\Card
  • WC_POS\Gateways\Cash
  • WC_POS\i18n
  • WC_POS\Integrations\Bookings
  • WC_POS\Products
  • WC_POS\Products\Visibility
  • WC_POS\Setup
  • WC_POS\Status
  • WC_POS\Tax
  • WC_POS\Template

Functions

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