PluginProbe
WPPizza – A Restaurant Plugin / 3.20
WPPizza – A Restaurant Plugin v3.20
trunk 2.16.11.28 3.19 3.19.1 3.19.2 3.19.3 3.19.4 3.19.5 3.19.6 3.19.7 3.19.7.1 3.19.7.2 3.19.7.3 3.19.7.4 3.19.8 3.19.8.1 3.19.8.2 3.19.8.3 3.19.9 3.20 3.20.1
wppizza / classes / subpages / subpage.categories.php

subpage.categories.php in WPPizza – A Restaurant Plugin 3.20, at classes/subpages/subpage.categories.php

491 lines 17.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * WPPIZZA_CATEGORIES Class
4 *
5 * @package WPPIZZA
6 * @subpackage WPPIZZA_CATEGORIES
7 * @copyright Copyright (c) 2015, Oliver Bach
8 * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
9 * @since 3.0
10 *
11 */
12 if ( ! defined( 'ABSPATH' ) ) exit;/*Exit if accessed directly*/
13
14
15 /************************************************************************************************************************
16 *
17 *
18 * WPPIZZA_CATEGORIES filters
19 *
20 *
21 ************************************************************************************************************************/
22 class WPPIZZA_CATEGORIES{
23 /*
24 * class ident
25 * @var str
26 * @since 3.0
27 */
28 private $class_key = 'categories';/*to help consistency throughout class in various places*/
29 private $submenu_caps_title ;
30 private $submenu_priority = 0;
31 /******************************************************************************************************************
32 *
33 * [CONSTRUCTOR]
34 *
35 *
36 * @since 3.0
37 *
38 ******************************************************************************************************************/
39 function __construct() {
40
41 /*titles/labels throughout class*/
42 add_action('init', array( $this, 'init_admin_lables') );
43
44 /*register capabilities for this page*/
45 add_filter('wppizza_filter_define_caps', array( $this, 'wppizza_filter_define_caps'), $this->submenu_priority);
46
47 if(is_admin()){
48
49 /*when deleting or creating categories do NOT move into "wppizza_current_screen*/
50 add_filter('delete_'.WPPIZZA_TAXONOMY.'', array($this,'wppizza_save_sorted_custom_category'),10,3);
51 add_action('create_'.WPPIZZA_TAXONOMY.'', array($this,'wppizza_save_sorted_custom_category'),10,2);//runs as ajax call
52 add_action('edit_'.WPPIZZA_TAXONOMY.'', array($this,'wppizza_save_sorted_custom_category'),10,2);
53
54 /*add some helper functions once to use their return multiple times */
55 add_action('current_screen', array( $this, 'wppizza_current_screen') );
56
57 /** admin ajax **/
58 add_action('wppizza_ajax_admin_'.$this->class_key.'', array( $this, 'admin_ajax'));
59
60 }
61 /**load admin ajax file**/
62 add_action('wp_ajax_wppizza_admin_'.$this->class_key.'_ajax', array($this, 'set_admin_ajax') );
63 }
64
65 /******************
66 * @since 3.19.1
67 * [admin ajax include file]
68 *******************/
69 public function init_admin_lables(){
70 /*titles/labels throughout class*/
71 $this->submenu_caps_title = apply_filters('wppizza_filter_admin_label_caps_title_'.$this->class_key.'', __('Categories','wppizza-admin'));
72 }
73
74 /******************
75 * @since 3.0
76 * [admin ajax include file]
77 *******************/
78 public function set_admin_ajax(){
79 require(WPPIZZA_PATH.'ajax/admin.ajax.wppizza.php');
80 die();
81 }
82 /*******************************************************************************************************************************************************
83 *
84 * [admin ajax]
85 *
86 ********************************************************************************************************************************************************/
87 function admin_ajax($wppizza_options){
88 /************************************************************************************************************
89 *
90 *
91 * [save sorted categories]
92 *
93 *
94 ************************************************************************************************************/
95 if($_POST['vars']['field']=='save_categories_sort'){
96
97 /*
98 WP might show more categories on a page than set in "Screen options: Number of items per page"
99 when there are nested subcategories. So, when paging, we need to chunk it into 3 chunks: before,
100 current and after taking into account categories visible, to set sortorder appropriately
101 */
102 /*current category sort order*/
103 $currrent_sort_hierarchy = $wppizza_options['layout']['category_sort_hierarchy'];
104
105
106 /*if we do not have one yet, use default sort order*/
107 if(count($currrent_sort_hierarchy)<=0){
108 $currrent_sort_hierarchy = WPPIZZA()-> categories -> wppizza_get_cats_hierarchy();
109 }
110
111
112 /**currently displayed category id's on that particular page (this might only be a part of all cats if paged)*/
113 $order = explode(',', $_POST['vars']['order']);
114 $order_chunk=array();
115 $last_element_in_chunk_id=-1;/*we need to account for new categories being added to a paged page and then drag and drop resorted*/
116 foreach ($order as $sort=>$id) {
117 $catid=(int)str_replace("tag-","",$id);
118 $order_chunk[$catid]=$sort;
119 $last_element_in_chunk_sort_id=$currrent_sort_hierarchy[$catid];
120 }
121
122 /**chunk into before, to sort and after */
123 $category_chunks=array();
124 foreach($currrent_sort_hierarchy as $key=>$sort){
125 if(isset($order_chunk[$key])){
126 $category_chunks['sort'][$key]=$order_chunk[$key];
127 }
128 if(!isset($order_chunk[$key])){
129 /*
130 accounting for newly added categories on a paged page
131 where the actual sortorder would be in a paged page
132 BEFORE the current one
133 */
134 if($sort<$last_element_in_chunk_sort_id){
135 $category_chunks['before'][$key]=$sort;
136 }
137 /*
138 accounting for newly added categories on a paged page
139 where the actual sortorder would be in a paged page
140 AFTER the current one
141 */
142 if($sort>$last_element_in_chunk_sort_id){
143 $category_chunks['after'][$key]=$sort;
144 }
145 }
146 }
147
148 /***************loop through chunks, only re-sorting cats on current page****************************/
149 $new_cat_sort=array();
150 $sorter=0;
151 /*before current*/
152 if(isset($category_chunks['before']) && is_array($category_chunks['before'])){
153 //$new_cat_sort+=$category_chunks['before'];
154 //$sorter+=count($category_chunks['before']);
155 /*safer to loop to make sure we do not have non identical sort ids*/
156 foreach($category_chunks['before'] as $catId=>$true){
157 $new_cat_sort[$catId]=$sorter;
158 $sorter++;
159 }
160 }
161 /*current, to sort*/
162 if(isset($category_chunks['sort']) && is_array($category_chunks['sort'])){
163 asort($category_chunks['sort']);
164 foreach($category_chunks['sort'] as $catId=>$true){
165 $new_cat_sort[$catId]=$sorter;
166 $sorter++;
167 }
168 }
169
170 /*after current*/
171 if(isset($category_chunks['after']) && is_array($category_chunks['after'])){
172 //$new_cat_sort+=$category_chunks['after'];
173 //$sorter+=count($category_chunks['after']);
174 /*safer to loop to make sure we do not have non identical sort ids*/
175 foreach($category_chunks['after'] as $catId=>$true){
176 $new_cat_sort[$catId]=$sorter;
177 $sorter++;
178 }
179 }
180
181 $updateOptions = $wppizza_options;
182 $updateOptions['layout']['category_sort_hierarchy']=$new_cat_sort;
183
184 update_option( WPPIZZA_SLUG, $updateOptions );
185 die(1);
186 }
187 }
188 /*********************************************************
189 *
190 * [add global helpers and enque js]
191 * @since 3.0
192 *
193 *********************************************************/
194 public function wppizza_current_screen($current_screen){
195 if($current_screen->id == 'edit-'.WPPIZZA_TAXONOMY.'' && $current_screen->post_type == WPPIZZA_POST_TYPE && $current_screen->taxonomy == WPPIZZA_TAXONOMY){
196 /***enqueue scripts and styles***/
197 add_action('admin_enqueue_scripts', array( $this, 'wppizza_enqueue_admin_scripts_and_styles'));
198 }
199 }
200 /*********************************************************
201 *
202 * [js]
203 * @since 3.0
204 *
205 *********************************************************/
206 public function wppizza_enqueue_admin_scripts_and_styles($hook) {
207 /**add sortable js*/
208 wp_enqueue_script('jquery-ui-sortable');
209
210 wp_register_script(WPPIZZA_SLUG.'_'.$this->class_key.'', plugins_url( 'js/scripts.admin.'.$this->class_key.'.js', WPPIZZA_PLUGIN_PATH ), array('jquery'), WPPIZZA_VERSION ,true);
211 wp_enqueue_script(WPPIZZA_SLUG.'_'.$this->class_key.'');
212 }
213
214
215 /*****************************************************
216 [save sortorder when creating, editing, deleting categories]
217 *****************************************************/
218 function wppizza_save_sorted_custom_category( $column, $term_id, $term_obj=null ) {
219 global $wppizza_options;
220 static $run_once=0;
221
222 if($run_once>0){return;}
223
224 /******************************************
225 bypass when activating plugin as we are installing
226 already sorted default items via wp_insert_post()
227 ********************************************/
228 if(!isset($_GET['activate'])){
229
230 /************************************************************************
231 *
232 * we are adding - category page,
233 * reorder as required
234 *
235 **************************************************************************/
236 if(!isset($wppizza_options['layout']['category_sort_hierarchy'][$column]) && !empty($_POST['action']) && $_POST['action']=='add-tag' ){
237
238 /********************************************************
239 * adding new category or sub category
240 * set order on save
241 * as this runs after the term have been updated in the db by wordpress
242 * a simple call to wppizza_get_cats_hierarchy will do
243 *********************************************************/
244
245 /**
246 if we are adding a parent cat, set to topmost
247 as by default it would be sorted by name
248 **/
249 if(isset($_POST['parent']) && $_POST['parent']==-1){
250
251 /**get current categories sorted */
252 $update_cat_sort = WPPIZZA()->categories->wppizza_get_cats_hierarchy($wppizza_options['layout']['category_sort_hierarchy']);
253
254 unset($update_cat_sort[$column]);
255
256 /**resort, with new cat id set to being the first*/
257 $new_sorter=0;
258 $update_cat_re_sort=array();
259 $update_cat_re_sort[$column]=$new_sorter;
260 foreach($update_cat_sort as $catId=>$sort){
261 $new_sorter++;
262 $update_cat_re_sort[$catId]=$new_sorter;
263 }
264 $update_cat_sort = $update_cat_re_sort;
265 }
266
267 /**
268 if we are adding a child cat,
269 insert after parent
270 **/
271 if(isset($_POST['parent']) && $_POST['parent']>=0){
272
273 /**resort, with new cat id inserted after it's parent*/
274 $new_sorter=0;
275 $new_child_cat = $column;
276 $child_parent_cat = (int)$_POST['parent'];
277 $update_cat_re_sort=array();
278 foreach($wppizza_options['layout']['category_sort_hierarchy'] as $catId=>$sort){
279 $update_cat_re_sort[$catId]=$new_sorter;
280 $new_sorter++;
281
282 if($child_parent_cat == $catId){
283 $update_cat_re_sort[$new_child_cat]=$new_sorter;
284 $new_sorter++;
285 }
286 }
287 $update_cat_sort = $update_cat_re_sort;
288 }
289
290 $wppizza_options['layout']['category_sort_hierarchy'] = $update_cat_sort;
291
292 /*update option*/
293 update_option( WPPIZZA_SLUG, $wppizza_options );
294 }
295
296 /************************************************************************
297 *
298 * we are adding - post edit screen
299 * (largely the same as 'add-tag' action, but using the relevant _POST parameters
300 * reorder as required
301 *
302 **************************************************************************/
303 if(!isset($wppizza_options['layout']['category_sort_hierarchy'][$column]) && !empty($_POST['action']) && $_POST['action']=='add-wppizza_menu' ){
304 /**
305 if we are adding a parent cat, set to topmost
306 as by default it would be sorted by name
307 **/
308 if(isset($_POST['new'.WPPIZZA_TAXONOMY.'_parent']) && $_POST['new'.WPPIZZA_TAXONOMY.'_parent']==-1){
309
310 /**get current categories sorted */
311 $update_cat_sort = WPPIZZA()->categories->wppizza_get_cats_hierarchy($wppizza_options['layout']['category_sort_hierarchy']);
312
313 unset($update_cat_sort[$column]);
314
315 /**resort, with new cat id set to being the first*/
316 $new_sorter=0;
317 $update_cat_re_sort=array();
318 $update_cat_re_sort[$column]=$new_sorter;
319 foreach($update_cat_sort as $catId=>$sort){
320 $new_sorter++;
321 $update_cat_re_sort[$catId]=$new_sorter;
322 }
323 $update_cat_sort = $update_cat_re_sort;
324
325 }
326 /**
327 if we are adding a child cat,
328 insert after parent
329 **/
330 if(isset($_POST['new'.WPPIZZA_TAXONOMY.'_parent']) && $_POST['new'.WPPIZZA_TAXONOMY.'_parent']>=0){
331
332 /**resort, with new cat id inserted after it's parent*/
333 $new_sorter=0;
334 $new_child_cat = $column;
335 $child_parent_cat = (int)$_POST['new'.WPPIZZA_TAXONOMY.'_parent'];
336 $update_cat_re_sort=array();
337
338 foreach($wppizza_options['layout']['category_sort_hierarchy'] as $catId=>$sort){
339 $update_cat_re_sort[$catId]=$new_sorter;
340 $new_sorter++;
341 if($child_parent_cat == $catId){
342 $update_cat_re_sort[$new_child_cat]=$new_sorter;
343 $new_sorter++;
344 }
345 }
346 $update_cat_sort = $update_cat_re_sort;
347
348 }
349
350 $wppizza_options['layout']['category_sort_hierarchy'] = $update_cat_sort;
351 /*update option*/
352 update_option( WPPIZZA_SLUG, $wppizza_options );
353 }
354
355 /************************************************************************
356 * we are editing in single cat edit screen (where we can change parent)
357 * save new order as parent might have changed
358 * this runs after the term have been updated in the db by wordpress
359 **************************************************************************/
360 if(isset($wppizza_options['layout']['category_sort_hierarchy'][$column]) && !empty($_POST['action']) && $_POST['action']=='editedtag'){
361
362 /*
363 skip update, if parent id has not changed
364 also, post parent equals -1 if topmost, but current_term->parent will equal 0 if topmost
365 so let's account for that too
366 */
367 $current_term = get_term($column, WPPIZZA_TAXONOMY);
368
369 if($_POST['parent'] != $current_term->parent){
370 /* parent has changed, unset first before reordering */
371 unset($wppizza_options['layout']['category_sort_hierarchy'][$column]);
372
373 /**
374 changed to be parent category (post == -1)
375 provided current_term->parent !=0 as current_term->parent equals zero when topmost
376 instead of -1
377 **/
378 if($_POST['parent'] == -1 && $current_term->parent!=0){
379 /**resort, with new cat id set to being the first*/
380 $new_sorter=0;
381 $update_cat_sort=array();
382 $update_cat_sort[$column]=$new_sorter;
383 foreach($wppizza_options['layout']['category_sort_hierarchy'] as $catId=>$sort){
384 $new_sorter++;
385 $update_cat_sort[$catId]=$new_sorter;
386 }
387
388 $wppizza_options['layout']['category_sort_hierarchy'] = $update_cat_sort;
389
390 /*update option*/
391 update_option( WPPIZZA_SLUG, $wppizza_options );
392 }
393
394 /** changed to be child category (or child category of another parent)**/
395 if($_POST['parent'] >= 0){
396
397 /**resort, with new cat id inserted after it's parent*/
398 $new_sorter = 0;
399 $new_child_cat = $column;
400 $child_parent_cat = (int)$_POST['parent'];
401 $update_cat_sort = array();
402 foreach($wppizza_options['layout']['category_sort_hierarchy'] as $catId=>$sort){
403 $update_cat_sort[$catId]=$new_sorter;
404 $new_sorter++;
405
406 if($child_parent_cat == $catId){
407 //$new_sorter++;
408 $update_cat_sort[$new_child_cat]=$new_sorter;
409 $new_sorter++;
410 }
411 }
412 $wppizza_options['layout']['category_sort_hierarchy'] = $update_cat_sort;
413
414 /*update option*/
415 update_option( WPPIZZA_SLUG, $wppizza_options );
416
417 }
418 }
419 }
420 /************************************************************************
421 * we are deleting - ajax or bulk delete,
422 * save new order
423 * as this runs after the term have been updated in the db by wordpress
424 * a simple call to wppizza_get_cats_hierarchy will do
425 **************************************************************************/
426 if(isset($wppizza_options['layout']['category_sort_hierarchy'][$column]) && !empty($_POST['action']) && ( $_POST['action']=='delete-tag' || (isset($_POST['delete_tags']) && is_array($_POST['delete_tags']))) ){
427
428 /*
429 bulk delete
430 */
431 if(!empty($_POST['delete_tags'])){
432
433 foreach($_POST['delete_tags'] as $cat_id_delete){
434 unset($wppizza_options['layout']['category_sort_hierarchy'][$cat_id_delete]);//unset as deleted
435 }
436 /**resort, now we have unset deleted*/
437 $new_sorter=0;
438 $update_cat_sort=array();
439 foreach($wppizza_options['layout']['category_sort_hierarchy'] as $catId=>$sort){
440 $update_cat_sort[$catId] = $new_sorter;
441 $new_sorter++;
442 }
443
444 $wppizza_options['layout']['category_sort_hierarchy'] = $update_cat_sort;
445
446 /*update option*/
447 update_option( WPPIZZA_SLUG, $wppizza_options );
448
449 }else{
450 /* simple delete, just remove from array */
451 $cat_id_delete = $_POST['tag_ID'];
452
453 /*unset as deleted*/
454 unset($wppizza_options['layout']['category_sort_hierarchy'][$cat_id_delete]);
455
456
457 /**
458 just get categories sorted on ajax delete,
459 as this runs after the term have been updated in the db by wordpress
460 **/
461 /*update option*/
462 update_option( WPPIZZA_SLUG, $wppizza_options );
463 }
464
465 }
466
467 $run_once++;
468 }
469 }
470
471 /*********************************************************
472 *
473 * [define caps]
474 * @since 3.0
475 *
476 *********************************************************/
477 function wppizza_filter_define_caps($caps){
478 /**add editing capability for this page**/
479 $caps[$this->class_key]=array('name'=>$this->submenu_caps_title ,'cap'=>''.WPPIZZA_SLUG.'_cap_'.$this->class_key.'');
480 // let's not enable/list this option for now....probably not required anyway as one should also delete/reassign orders to someone else ...
481 //$caps[$this->class_key.'-delete-customers']=array('name'=>__('Delete Customers', 'wppizza-admin') ,'cap'=>'wppizza_cap_delete_customers');
482 return $caps;
483 }
484 }
485 /***************************************************************
486 *
487 * [ini]
488 *
489 ***************************************************************/
490 $WPPIZZA_CATEGORIES = new WPPIZZA_CATEGORIES();
491 ?>