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 / modules / mod.additives.additives.php

mod.additives.additives.php in WPPizza – A Restaurant Plugin 3.20, at classes/modules/mod.additives.additives.php

381 lines 14.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * WPPIZZA_MODULE_ADDITIVES Class
4 *
5 * @package WPPIZZA
6 * @subpackage WPPIZZA_MODULE_ADDITIVES
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 *
19 *
20 *
21 *
22 ************************************************************************************************************************/
23 class WPPIZZA_MODULE_ADDITIVES{
24
25 private $settings_page = 'additives';/* which admin subpage (identified there by this->class_key) are we adding this to */
26
27
28 private $section_key = 'additives';/* must be unique */
29
30
31 function __construct() {
32 /**********************************************************
33 [add settings to admin]
34 ***********************************************************/
35 if(is_admin()){
36 /* add admin options settings page*/
37 add_filter('wppizza_filter_settings_sections_'.$this->settings_page.'', array($this, 'admin_options_settings'), 20, 5);
38 /* add admin options settings page fields */
39 add_action('wppizza_admin_settings_section_fields_'.$this->settings_page.'', array($this, 'admin_options_fields_settings'), 20, 5);
40 /**add default options **/
41 add_filter('wppizza_filter_setup_default_options', array( $this, 'options_default'));
42 /**validate options**/
43 add_filter('wppizza_filter_options_validate', array( $this, 'options_validate'), 10, 2 );
44 /**metaboxes sizes/prices - priority same as submenu page **/
45 add_filter('wppizza_filter_admin_metaboxes', array( $this, 'wppizza_filter_admin_add_metaboxes'), 60, 4);
46 add_filter('wppizza_filter_admin_save_metaboxes',array( $this, 'wppizza_filter_admin_save_metaboxes'), 10, 3);
47 /** admin ajax **/
48 add_action('wppizza_ajax_admin_'.$this->settings_page.'', array( $this, 'admin_ajax'));
49 }
50 }
51 /*******************************************************************************************************************************************************
52 *
53 * [admin ajax]
54 *
55 ********************************************************************************************************************************************************/
56 function admin_ajax($wppizza_options){
57
58 //need right caps for access
59 if( !current_user_can('wppizza_cap_'.$this->settings_page.'')){
60 echo '<div class="wppizza-highlight">'.__('Access Denied', 'wppizza-admin').' ['.$this->section_key.']</div>';
61 exit();
62 }
63
64 /*****************************************************
65 [adding new additive]
66 *****************************************************/
67 if($_POST['vars']['field']==$this->section_key && isset($_POST['vars']['setKeys']) ){
68
69 /**get next highest key available**/
70 $nextKey=0;
71 if(isset($_POST['vars']['setKeys']) && is_array($_POST['vars']['setKeys'])){
72 $currentKeys=array();
73 foreach($_POST['vars']['setKeys'] as $key_exists){
74 $currentKeys[$key_exists['value']]=$key_exists['value'];
75 }
76 $highestKey=max($currentKeys);
77 $nextKey=$highestKey+1;
78 }
79
80 /** cretae some (albeit empty) default values */
81 $default_values['sort'] = '';
82 $default_values['name'] = '';
83
84
85 $output = $this->wppizza_admin_section_additives($_POST['vars']['field'], $nextKey, $default_values);
86
87 print"".$output."";
88 exit();
89 }
90 }
91
92
93 /*******************************************************************************************************************************************************
94 *
95 *
96 *
97 * [frontend filters]
98 *
99 *
100 *
101 ********************************************************************************************************************************************************/
102
103
104
105 /*******************************************************************************************************************************************************
106 *
107 *
108 *
109 * [add admin page options]
110 *
111 *
112 *
113 ********************************************************************************************************************************************************/
114 /*********************************************************
115 *
116 * [add metaboxes]
117 * @since 3.0
118 *
119 *********************************************************/
120 function wppizza_filter_admin_add_metaboxes($wppizza_meta_box, $meta_values, $meal_sizes, $wppizza_options){
121
122 if(!empty($wppizza_options[$this->section_key])){
123 /*->*** which additives in item ***/
124 $wppizza_meta_box[$this->section_key]='';
125 $wppizza_meta_box[$this->section_key].="<div class='".WPPIZZA_SLUG."_option_meta ".WPPIZZA_SLUG."_option_meta_".$this->section_key."'>";
126
127 $wppizza_meta_box[$this->section_key].="<label class='".WPPIZZA_SLUG."-meta-label'>".__('Additives', 'wppizza-admin').": </label>";
128
129
130 $wppizza_meta_box[$this->section_key].="<div>";/* wrapper div needed to make css->display:flex work here */
131
132 asort($wppizza_options[$this->section_key]);//sort but keep index
133
134 /*
135 if we perhaps ever want to use chosen.js instead of the below buttons (but must be enabled in subpage.post and admin.common js too if we do)
136 Note:saving of values would need to be checked still
137 */
138 // $wppizza_meta_box[$this->section_key] .= '<select id="'.WPPIZZA_PREFIX.'-meta-'.$this->section_key.'" name="'.WPPIZZA_SLUG.'['.$this->section_key.']" class="'.WPPIZZA_PREFIX.'-meta-'.$this->section_key.'" multiple="multiple" >';
139 // foreach($wppizza_options[$this->section_key] as $key=>$value){
140 // $wppizza_meta_box[$this->section_key] .= '<option value="'.$key.'" '.selected(in_array($key, $meta_values[$this->section_key]), true ,false).' >'.$value['name'].'</option>';
141 // }
142 // $wppizza_meta_box[$this->section_key].="</select>";
143
144
145 foreach($wppizza_options[$this->section_key] as $key=>$value){
146 $wppizza_meta_box[$this->section_key].="<label class='button'>";
147 $wppizza_meta_box[$this->section_key].="<input name='".WPPIZZA_SLUG."[".$this->section_key."][".$key."]' size='5' type='checkbox' ". checked((is_array($meta_values[$this->section_key]) && in_array($key,$meta_values[$this->section_key])),true,false)." value='".$key."' /> ".$value['name']."";
148 $wppizza_meta_box[$this->section_key].="</label>";
149 }
150
151 $wppizza_meta_box[$this->section_key].="</div>";
152
153 $wppizza_meta_box[$this->section_key].="</div>";
154 }
155
156
157 return $wppizza_meta_box;
158 }
159
160 /*********************************************************
161 *
162 * [save metaboxes values]
163 * @since 3.0
164 *
165 *********************************************************/
166 function wppizza_filter_admin_save_metaboxes($itemMeta, $item_id, $wppizza_options){
167
168 //**additives**//
169 $itemMeta[$this->section_key]=array();
170 if(isset($_POST[WPPIZZA_SLUG][$this->section_key])){
171 foreach($_POST[WPPIZZA_SLUG][$this->section_key] as $key=>$val){
172 $itemMeta[$this->section_key][$key] = (int)$_POST[WPPIZZA_SLUG][$this->section_key][$key];
173 }}
174
175 return $itemMeta;
176 }
177
178
179 /*------------------------------------------------------------------------------
180 #
181 #
182 # [settings page]
183 #
184 #
185 ------------------------------------------------------------------------------*/
186
187 /*------------------------------------------------------------------------------
188 # [settings section - setting page]
189 # @since 3.0
190 # @return array()
191 ------------------------------------------------------------------------------*/
192 function admin_options_settings($settings, $sections, $fields, $inputs, $help){
193
194 /*section*/
195 if($sections){
196 $settings['sections'][$this->section_key] = __('Additives', 'wppizza-admin');
197 }
198 /*help*/
199 if($help){
200 $settings['help'][$this->section_key][] = array(
201 'label'=>__('Manage Additives', 'wppizza-admin'),
202 'description'=>array(
203 __('Some meals or beverages may contain additives.', 'wppizza-admin'),
204 __('Add any possible additives (or any other notes for that matter) here and select them at any meal / beverage that contains these additives. This in turn will add a footnote to pages denoting which item contains what additives', 'wppizza-admin'),
205 __('By default, additives will be sorted alphabetically.', 'wppizza-admin'),
206 __('However, you can use the "sort" field to customise the sortorder. If you do, your choosen sort id will be used to identify your choosen additives in the frontend so you want to make sure to have unique identifiers/sort id\'s.', 'wppizza-admin')
207 )
208 );
209 }
210 /*fields*/
211 if($fields){
212 $field = $this->section_key;
213 $settings['fields'][$this->section_key][$field] = array( '', array(
214 'value_key'=>$field,
215 'option_key'=>$this->settings_page,
216 'label'=> '',
217 'description'=>array()
218 ));
219 }
220
221
222 return $settings;
223 }
224 /*------------------------------------------------------------------------------
225 # [output option fields - setting page]
226 # @since 3.0
227 # @return array()
228 ------------------------------------------------------------------------------*/
229 function admin_options_fields_settings($wppizza_options, $options_key, $field, $label, $description){
230
231 if($field==$this->section_key){
232
233 echo"<div id='wppizza_".$field."_options' class='wppizza_admin_options'>";/*new sizes will be appended to this div's id by ajax*/
234
235 if(!empty($wppizza_options[$field])){
236 asort($wppizza_options[$field]);//sort but keep index
237 /* get additives that are in use */
238 $additives_in_use = wppizza_options_in_use($field);
239 foreach($wppizza_options[$field] as $key=>$values){
240 echo"".$this->wppizza_admin_section_additives($field, $key, $values, $additives_in_use[$field]);
241 }
242 }
243 echo"</div>";
244 /** add new button **/
245 echo"<div id='wppizza-".$field."-add' class='wppizza_admin_add'>";
246 echo "<input type='button' id='wppizza_add_".$field."' class='button' value='".__('add additive', 'wppizza-admin')."' />";
247 echo"</div>";
248 }
249 }
250
251
252 /**
253 [available sizes of meal items or add new via ajax]
254 **/
255 private function wppizza_admin_section_additives($field, $key, $values=null, $additives_in_use=null){
256
257 $str='';
258
259 $str.="<div class='wppizza_option wppizza_".$this->section_key."_option'>";
260
261 /*
262 for easy checking for existing keys when adding new
263 */
264 $str.="<input id='wppizza_".$field."_".$key."' class='wppizza-getkey' name='wppizza-getkey[".$key."]' type='hidden' value='".$key."'>";
265
266 $str.="<span class='wppizza_label_50'>";
267 $str.="ID: ".$key."";
268 $str.="</span>";
269
270 $str.="<span>";
271 $str.="".__('sort', 'wppizza-admin')."/".__('ident', 'wppizza-admin').":";
272 $str.="<input name='".WPPIZZA_SLUG."[".$field."][".$key."][sort]' size='3' type='text' value='". $values['sort'] ."' placeholder=''/>";
273 $str.="</span>";
274
275 $str.="<span>";
276 $str.="".__('name', 'wppizza-admin').":";
277 $str.="<input name='".WPPIZZA_SLUG."[".$field."][".$key."][name]' size='30' type='text' value='". $values['name'] ."' placeholder=''/>";
278 $str.="</span>";
279
280 $str.="<span class='".WPPIZZA_SLUG."-option-inuse ".WPPIZZA_SLUG."-".$field."-inuse'>";
281 if(!isset($additives_in_use[$key])){
282 $str.="<a href='javascript:void(0);' class='".WPPIZZA_SLUG."-delete ".$field." ".WPPIZZA_SLUG."-dashicons dashicons-trash' title='".__('delete', 'wppizza-admin')."'></a>";
283 }else{
284 $str.="".__('in use', 'wppizza-admin')."";
285 }
286 $str.="</span>";
287
288 $str.="</div>";
289
290 return $str;
291 }
292
293
294
295 /****************************************************************
296 *
297 * [insert default option on install]
298 * $parameter $options array() | filter passing on filtered options
299 * @since 3.0
300 * @return array()
301 *
302 ****************************************************************/
303 function options_default($options){
304 /*
305 default additives
306 */
307 $options[$this->section_key]=array(
308 0=>array('sort'=>1,'name'=>wppizza_textdomain('Food coloring', 'wppizza')),
309 1=>array('sort'=>2,'name'=>wppizza_textdomain('Flavor enhancers', 'wppizza')),
310 2=>array('sort'=>3,'name'=>wppizza_textdomain('Preservatives', 'wppizza')),
311 3=>array('sort'=>4,'name'=>wppizza_textdomain('Stabilizers', 'wppizza')),
312 4=>array('sort'=>5,'name'=>wppizza_textdomain('Sweeteners', 'wppizza'))
313 );
314
315 /* allow filtering */
316 $options[$this->section_key] = apply_filters('wppizza_filter_install_default_additives', $options[$this->section_key]);
317
318 return $options;
319 }
320
321 /*------------------------------------------------------------------------------
322 # [validate options on save/update]
323 #
324 # @since 3.0
325 # @return array()
326 ------------------------------------------------------------------------------*/
327 function options_validate($options, $input){
328 /**make sure we get the full array on install/update**/
329 if ( empty( $_POST['_wp_http_referer'] ) ) {
330 return $input;
331 }
332 /********************************
333 * [validate]
334 ********************************/
335 if(isset($_POST[''.WPPIZZA_SLUG.'_'.$this->settings_page.''])){
336
337 $options[$this->section_key] = array();//initialize as empty array
338 if(!empty($input[$this->section_key])){
339
340 foreach($input[$this->section_key] as $key=>$values){
341
342 if(trim($values['name'])!=''){
343
344 $sort = ($values['sort']!='' ) ? preg_replace("/[^a-zA-Z0-9\-_+*:]/","", $values['sort'] ) : '';
345
346 $options[$this->section_key][$key] = array( 'sort' => $sort, 'name' => wppizza_validate_string($values['name']));
347
348 }
349
350 }
351
352 }
353
354 /*
355 in case someone does something really daft (editing post pages and sizes at the same time in 2 different windows)....
356 make sure we do not delete something that really should be there.
357 */
358 global $wppizza_options;
359
360 $additives_in_use = wppizza_options_in_use($this->section_key);
361
362 if(!empty($additives_in_use[$this->section_key])){
363 foreach($additives_in_use[$this->section_key] as $additive_key){
364
365 if(!isset($options[$this->section_key][$additive_key])){
366 $options[$this->section_key][$additive_key] = $wppizza_options[$this->section_key][$additive_key];
367 }
368
369 }}
370 }
371
372 return $options;
373 }
374 }
375 /***************************************************************
376 *
377 * [ini]
378 *
379 ***************************************************************/
380 $WPPIZZA_MODULE_ADDITIVES = new WPPIZZA_MODULE_ADDITIVES();
381 ?>