1 <?php
2
3 4 5 6 7 8 9 10
11
12 namespace WC_POS\Products;
13
14 class Visibility {
15
16 protected $options;
17
18 19 20
21 public function __construct() {
22
23
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 44 45 46 47 48 49
50 public function posts_where( $where, $query ) {
51 global $wpdb;
52
53
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
61 if( is_admin() && !is_pos() )
62 return $where;
63
64
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 79 80 81 82 83
84 public function pos_visibility_filters( $views ) {
85 global $wpdb;
86
87 $visibility_filters = array(
88
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 121 122 123
124 public function pre_get_posts( $query ) {
125
126
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 146 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' => '— No Change —'), $this->options
154 );
155 include 'views/quick-edit-visibility-select.php';
156 }
157
158 159 160 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 172 173
174 public function save_post( $post_id, $post ) {
175
176
177 if ( defined( '\DOING_AUTOSAVE' ) && \DOING_AUTOSAVE ) {
178 return;
179 }
180
181
182 if ( wp_is_post_revision( $post_id ) || wp_is_post_autosave( $post_id ) ) {
183 return;
184 }
185
186
187 if ( 'product' != $post->post_type ) {
188 return;
189 }
190
191
192 if ( ! current_user_can( 'edit_post', $post_id ) ) {
193 return;
194 }
195
196
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
216 if ( isset( $_REQUEST['_pos_visibility'] ) ) {
217 update_post_meta( $post_id, '_pos_visibility', $_REQUEST['_pos_visibility'] );
218 }
219
220 }
221
222 223 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 249 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 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 }