| 1 |
<?php |
| 2 |
/** |
| 3 |
* WPPIZZA_MODULE_FOODTYPE Class |
| 4 |
* |
| 5 |
* @package WPPIZZA |
| 6 |
* @subpackage WPPIZZA_MODULE_FOODTYPE |
| 7 |
* @copyright Copyright (c) 2015, Oliver Bach |
| 8 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 9 |
* @since 3.12.14 |
| 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_FOODTYPE{ |
| 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 = 'foodtype';/* 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'), 30, 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'), 30, 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 foodtype] |
| 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 |
$default_values['icon'] = ''; |
| 84 |
|
| 85 |
|
| 86 |
$output = $this->wppizza_admin_section_options($_POST['vars']['field'], $nextKey, $default_values); |
| 87 |
|
| 88 |
print"".$output.""; |
| 89 |
exit(); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
/******************************************************************************************************************************************************* |
| 95 |
* |
| 96 |
* |
| 97 |
* |
| 98 |
* [frontend filters] |
| 99 |
* |
| 100 |
* |
| 101 |
* |
| 102 |
********************************************************************************************************************************************************/ |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
/******************************************************************************************************************************************************* |
| 107 |
* |
| 108 |
* |
| 109 |
* |
| 110 |
* [add admin page options] |
| 111 |
* |
| 112 |
* |
| 113 |
* |
| 114 |
********************************************************************************************************************************************************/ |
| 115 |
/********************************************************* |
| 116 |
* |
| 117 |
* [add metaboxes] |
| 118 |
* @since 3.0 |
| 119 |
* |
| 120 |
*********************************************************/ |
| 121 |
function wppizza_filter_admin_add_metaboxes($wppizza_meta_box, $meta_values, $meal_sizes, $wppizza_options){ |
| 122 |
|
| 123 |
if(!empty($wppizza_options[$this->section_key])){ |
| 124 |
/*->*** which foodtypes in item ***/ |
| 125 |
$wppizza_meta_box[$this->section_key]=''; |
| 126 |
$wppizza_meta_box[$this->section_key].="<div class='".WPPIZZA_SLUG."_option_meta ".WPPIZZA_SLUG."_option_meta_".$this->section_key."'>"; |
| 127 |
|
| 128 |
$wppizza_meta_box[$this->section_key].="<label class='".WPPIZZA_SLUG."-meta-label'>".__('Type', 'wppizza-admin').": </label>"; |
| 129 |
|
| 130 |
|
| 131 |
$wppizza_meta_box[$this->section_key].="<div>";/* wrapper div needed to make css->display:flex work here */ |
| 132 |
|
| 133 |
asort($wppizza_options[$this->section_key]);//sort but keep index |
| 134 |
|
| 135 |
/* |
| 136 |
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) |
| 137 |
Note:saving of values would need to be checked still |
| 138 |
*/ |
| 139 |
// $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" >'; |
| 140 |
// foreach($wppizza_options[$this->section_key] as $key=>$value){ |
| 141 |
// $wppizza_meta_box[$this->section_key] .= '<option value="'.$key.'" '.selected(in_array($key, $meta_values[$this->section_key]), true ,false).' >'.$value['name'].'</option>'; |
| 142 |
// } |
| 143 |
// $wppizza_meta_box[$this->section_key].="</select>"; |
| 144 |
|
| 145 |
|
| 146 |
foreach($wppizza_options[$this->section_key] as $key=>$value){ |
| 147 |
$wppizza_meta_box[$this->section_key].="<label class='button'>"; |
| 148 |
$wppizza_meta_box[$this->section_key].="<input name='".WPPIZZA_SLUG."[".$this->section_key."][".$key."]' size='5' type='checkbox' ". checked((isset($meta_values[$this->section_key]) && in_array($key,$meta_values[$this->section_key])),true,false)." value='".$key."' /> ".$value['name'].""; |
| 149 |
$wppizza_meta_box[$this->section_key].="</label>"; |
| 150 |
} |
| 151 |
|
| 152 |
$wppizza_meta_box[$this->section_key].="</div>"; |
| 153 |
|
| 154 |
$wppizza_meta_box[$this->section_key].="</div>"; |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
return $wppizza_meta_box; |
| 159 |
} |
| 160 |
|
| 161 |
/********************************************************* |
| 162 |
* |
| 163 |
* [save metaboxes values] |
| 164 |
* @since 3.0 |
| 165 |
* |
| 166 |
*********************************************************/ |
| 167 |
function wppizza_filter_admin_save_metaboxes($itemMeta, $item_id, $wppizza_options){ |
| 168 |
|
| 169 |
//**foodtypes**// |
| 170 |
$itemMeta[$this->section_key]=array(); |
| 171 |
if(isset($_POST[WPPIZZA_SLUG][$this->section_key])){ |
| 172 |
foreach($_POST[WPPIZZA_SLUG][$this->section_key] as $key=>$val){ |
| 173 |
$itemMeta[$this->section_key][$key] = (int)$_POST[WPPIZZA_SLUG][$this->section_key][$key]; |
| 174 |
}} |
| 175 |
|
| 176 |
return $itemMeta; |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
/*------------------------------------------------------------------------------ |
| 181 |
# |
| 182 |
# |
| 183 |
# [settings page] |
| 184 |
# |
| 185 |
# |
| 186 |
------------------------------------------------------------------------------*/ |
| 187 |
|
| 188 |
/*------------------------------------------------------------------------------ |
| 189 |
# [settings section - setting page] |
| 190 |
# @since 3.0 |
| 191 |
# @return array() |
| 192 |
------------------------------------------------------------------------------*/ |
| 193 |
function admin_options_settings($settings, $sections, $fields, $inputs, $help){ |
| 194 |
|
| 195 |
/*section*/ |
| 196 |
if($sections){ |
| 197 |
$settings['sections'][$this->section_key] = __('Food Type', 'wppizza-admin'); |
| 198 |
} |
| 199 |
/*help*/ |
| 200 |
if($help){ |
| 201 |
|
| 202 |
$settings['help'][$this->section_key][] = array( |
| 203 |
'label'=>__('Manage Type', 'wppizza-admin'), |
| 204 |
'description'=>array( |
| 205 |
__('Some meals or beverages may be of a certain type you may wish to indicate alongside the menu item title.', 'wppizza-admin'), |
| 206 |
__('Define the types you would like to have available for selection here and select these as applicable in the individual menu item pages.', 'wppizza-admin'), |
| 207 |
__('By default, selected types will be sorted alphabetically, but you can use the "sort" field to customise the sortorder.', 'wppizza-admin'), |
| 208 |
__('If a food type is associated with an icon, the icon will be displayed, otherwise the selected sortid will be used. The full name/type will be displayed on hovering over icon or identification', 'wppizza-admin'), |
| 209 |
__('Note: Food Types will only be shown next to the menu item title unless specifically using shortcodes elsewhere.', 'wppizza-admin'), |
| 210 |
) |
| 211 |
); |
| 212 |
|
| 213 |
} |
| 214 |
/*fields*/ |
| 215 |
if($fields){ |
| 216 |
$field = $this->section_key; |
| 217 |
$settings['fields'][$this->section_key][$field] = array( '', array( |
| 218 |
'value_key'=>$field, |
| 219 |
'option_key'=>$this->settings_page, |
| 220 |
'label'=> '', |
| 221 |
'description'=>array() |
| 222 |
)); |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
return $settings; |
| 227 |
} |
| 228 |
/*------------------------------------------------------------------------------ |
| 229 |
# [output option fields - setting page] |
| 230 |
# @since 3.0 |
| 231 |
# @return array() |
| 232 |
------------------------------------------------------------------------------*/ |
| 233 |
function admin_options_fields_settings($wppizza_options, $options_key, $field, $label, $description){ |
| 234 |
|
| 235 |
if($field==$this->section_key){ |
| 236 |
|
| 237 |
echo"<div id='wppizza_".$field."_options' class='wppizza_admin_options'>";/*new sizes will be appended to this div's id by ajax*/ |
| 238 |
|
| 239 |
if(!empty($wppizza_options[$field])){ |
| 240 |
asort($wppizza_options[$field]);//sort but keep index |
| 241 |
/* get allergens that are in use */ |
| 242 |
$options_in_use = wppizza_options_in_use($field); |
| 243 |
foreach($wppizza_options[$field] as $key=>$values){ |
| 244 |
echo"".$this->wppizza_admin_section_options($field, $key, $values, $options_in_use[$field]); |
| 245 |
} |
| 246 |
} |
| 247 |
echo"</div>"; |
| 248 |
/** add new button **/ |
| 249 |
echo"<div id='wppizza-".$field."-add' class='wppizza_admin_add'>"; |
| 250 |
echo "<input type='button' id='wppizza_add_".$field."' class='button' value='".__('add food type', 'wppizza-admin')."' />"; |
| 251 |
echo"</div>"; |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
/** |
| 257 |
[available sizes of meal items or add new via ajax] |
| 258 |
**/ |
| 259 |
private function wppizza_admin_section_options($field, $key, $values=null, $options_in_use=null){ |
| 260 |
|
| 261 |
$str=''; |
| 262 |
|
| 263 |
$str.="<div class='wppizza_option wppizza_".$this->section_key."_option'>"; |
| 264 |
|
| 265 |
/* |
| 266 |
for easy checking for existing keys when adding new |
| 267 |
*/ |
| 268 |
$str.="<input id='wppizza_".$field."_".$key."' class='wppizza-getkey' name='wppizza-getkey[".$key."]' type='hidden' value='".$key."'>"; |
| 269 |
|
| 270 |
$str.="<span class='wppizza_label_50'>"; |
| 271 |
$str.="ID: ".$key.""; |
| 272 |
$str.="</span>"; |
| 273 |
|
| 274 |
$str.="<span>"; |
| 275 |
$str.="".__('sort', 'wppizza-admin')."/".__('ident', 'wppizza-admin').":"; |
| 276 |
$str.="<input name='".WPPIZZA_SLUG."[".$field."][".$key."][sort]' size='3' type='text' value='". $values['sort'] ."' placeholder=''/>"; |
| 277 |
$str.="</span>"; |
| 278 |
|
| 279 |
$str.="<span>"; |
| 280 |
$str.="".__('name', 'wppizza-admin').":"; |
| 281 |
$str.="<input name='".WPPIZZA_SLUG."[".$field."][".$key."][name]' size='30' type='text' value='". $values['name'] ."' placeholder=''/>"; |
| 282 |
$str.="</span>"; |
| 283 |
|
| 284 |
$str.="<span>"; |
| 285 |
$str.="".__('icon', 'wppizza-admin').":"; |
| 286 |
|
| 287 |
|
| 288 |
$str.="<select name='".WPPIZZA_SLUG."[".$field."][".$key."][icon]'>"; |
| 289 |
/* default option */ |
| 290 |
$str.="<option value=''> === ".__('No Icon', 'wppizza-admin')." === </option>"; |
| 291 |
|
| 292 |
/* icons etc */ |
| 293 |
$icon_options = array(); |
| 294 |
|
| 295 |
$icon_options['spice'] = array( |
| 296 |
'label' => __('Spice Level', 'wppizza-admin') , |
| 297 |
'items' => array( |
| 298 |
'mild' => __('Mild', 'wppizza-admin'), |
| 299 |
'medium' => __('Medium', 'wppizza-admin'), |
| 300 |
'hot' => __('Hot', 'wppizza-admin'), |
| 301 |
'very_hot' => __('Very Hot', 'wppizza-admin'), |
| 302 |
) |
| 303 |
); |
| 304 |
|
| 305 |
$icon_options['other'] = array( |
| 306 |
'label' => __('Other', 'wppizza-admin') , |
| 307 |
'items' => array( |
| 308 |
'kosher' => __('Kosher', 'wppizza-admin'), |
| 309 |
'halal' => __('Halal', 'wppizza-admin'), |
| 310 |
'vegetarian' => __('Vegetarian', 'wppizza-admin'), |
| 311 |
'vegan' => __('Vegan', 'wppizza-admin'), |
| 312 |
'frozen' => __('Frozen', 'wppizza-admin'), |
| 313 |
) |
| 314 |
); |
| 315 |
|
| 316 |
|
| 317 |
foreach($icon_options as $ident => $aGroup){ |
| 318 |
$str.="<optgroup label=' -- ".$aGroup['label']." -- '>"; |
| 319 |
|
| 320 |
//sort if it's not a "spice" group |
| 321 |
if(!in_array($ident , array('spice') )){ |
| 322 |
asort($aGroup['items']); |
| 323 |
} |
| 324 |
|
| 325 |
foreach($aGroup['items'] as $k => $v ) { |
| 326 |
$str.="<option value='".$k."' ".selected($values['icon'], $k ,false)." > ".$v." </option>"; |
| 327 |
} |
| 328 |
|
| 329 |
} |
| 330 |
|
| 331 |
$str.="</select>"; |
| 332 |
|
| 333 |
$str.="</span>"; |
| 334 |
|
| 335 |
$str.="<span class='".WPPIZZA_SLUG."-option-inuse ".WPPIZZA_SLUG."-".$field."-inuse'>"; |
| 336 |
if(!isset($options_in_use[$key])){ |
| 337 |
$str.="<a href='javascript:void(0);' class='".WPPIZZA_SLUG."-delete ".$field." ".WPPIZZA_SLUG."-dashicons dashicons-trash' title='".__('delete', 'wppizza-admin')."'></a>"; |
| 338 |
}else{ |
| 339 |
$str.="".__('in use', 'wppizza-admin').""; |
| 340 |
} |
| 341 |
$str.="</span>"; |
| 342 |
|
| 343 |
$str.="</div>"; |
| 344 |
|
| 345 |
return $str; |
| 346 |
} |
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
/**************************************************************** |
| 351 |
* |
| 352 |
* [insert default option on install] |
| 353 |
* $parameter $options array() | filter passing on filtered options |
| 354 |
* @since 3.0 |
| 355 |
* @return array() |
| 356 |
* |
| 357 |
****************************************************************/ |
| 358 |
function options_default($options){ |
| 359 |
/* |
| 360 |
default food types |
| 361 |
*/ |
| 362 |
$options[$this->section_key] = array( |
| 363 |
|
| 364 |
0=>array( 'sort'=>'+', 'name'=>wppizza_textdomain('Mild', 'wppizza'), 'icon' => 'mild' ), |
| 365 |
1=>array( 'sort'=>'++', 'name'=>wppizza_textdomain('Spicy', 'wppizza'), 'icon' => 'medium'), |
| 366 |
2=>array( 'sort'=>'+++', 'name'=>wppizza_textdomain('Hot', 'wppizza'), 'icon' => 'hot'), |
| 367 |
3=>array( 'sort'=>'++++', 'name'=>wppizza_textdomain('Very Hot', 'wppizza'), 'icon' => 'very_hot'), |
| 368 |
4=>array( 'sort'=>'Fz', 'name'=>wppizza_textdomain('Frozen', 'wppizza'), 'icon' => 'frozen'), |
| 369 |
5=>array( 'sort'=>'Hl', 'name'=>wppizza_textdomain('Halal', 'wppizza'), 'icon' => 'halal'), |
| 370 |
6=>array( 'sort'=>'Ks', 'name'=>wppizza_textdomain('Kosher', 'wppizza'), 'icon' => 'kosher'), |
| 371 |
7=>array( 'sort'=>'V', 'name'=>wppizza_textdomain('Vegetarian', 'wppizza'), 'icon' => 'vegetarian'), |
| 372 |
8=>array( 'sort'=>'Vg', 'name'=>wppizza_textdomain('Vegan', 'wppizza'), 'icon' => 'vegan'), |
| 373 |
|
| 374 |
); |
| 375 |
/* allow filtering */ |
| 376 |
$options[$this->section_key] = apply_filters('wppizza_filter_install_default_foodtype', $options[$this->section_key]); |
| 377 |
|
| 378 |
return $options; |
| 379 |
} |
| 380 |
|
| 381 |
/*------------------------------------------------------------------------------ |
| 382 |
# [validate options on save/update] |
| 383 |
# |
| 384 |
# @since 3.0 |
| 385 |
# @return array() |
| 386 |
------------------------------------------------------------------------------*/ |
| 387 |
function options_validate($options, $input){ |
| 388 |
/**make sure we get the full array on install/update**/ |
| 389 |
if ( empty( $_POST['_wp_http_referer'] ) ) { |
| 390 |
return $input; |
| 391 |
} |
| 392 |
/******************************** |
| 393 |
* [validate] |
| 394 |
********************************/ |
| 395 |
if(isset($_POST[''.WPPIZZA_SLUG.'_'.$this->settings_page.''])){ |
| 396 |
|
| 397 |
$options[$this->section_key] = array();//initialize as empty array |
| 398 |
if(!empty($input[$this->section_key])){ |
| 399 |
|
| 400 |
foreach($input[$this->section_key] as $key=>$values){ |
| 401 |
|
| 402 |
if(trim($values['name'])!=''){ |
| 403 |
|
| 404 |
$sort = ($values['sort']!='' ) ? preg_replace("/[^a-zA-Z0-9\-_+*:]/","", $values['sort'] ) : ''; |
| 405 |
|
| 406 |
$options[$this->section_key][$key] = array( 'sort' => $sort, 'name' => wppizza_validate_string($values['name']), 'icon' => wppizza_validate_string($values['icon']) ); |
| 407 |
|
| 408 |
} |
| 409 |
|
| 410 |
} |
| 411 |
|
| 412 |
} |
| 413 |
|
| 414 |
/* |
| 415 |
in case someone does something really daft (editing post pages and sizes at the same time in 2 different windows).... |
| 416 |
make sure we do not delete something that really should be there. |
| 417 |
*/ |
| 418 |
global $wppizza_options; |
| 419 |
|
| 420 |
$options_in_use = wppizza_options_in_use($this->section_key); |
| 421 |
|
| 422 |
if(!empty($options_in_use[$this->section_key])){ |
| 423 |
foreach($options_in_use[$this->section_key] as $foodtype_key){ |
| 424 |
|
| 425 |
if(!isset($options[$this->section_key][$foodtype_key])){ |
| 426 |
$options[$this->section_key][$foodtype_key] = $wppizza_options[$this->section_key][$foodtype_key]; |
| 427 |
} |
| 428 |
|
| 429 |
}} |
| 430 |
} |
| 431 |
|
| 432 |
return $options; |
| 433 |
} |
| 434 |
} |
| 435 |
/*************************************************************** |
| 436 |
* |
| 437 |
* [ini] |
| 438 |
* |
| 439 |
***************************************************************/ |
| 440 |
$WPPIZZA_MODULE_FOODTYPE = new WPPIZZA_MODULE_FOODTYPE(); |
| 441 |
?> |