| 1 |
<?php |
| 2 |
/** |
| 3 |
* WPPIZZA_MODULE_ORDERSETTINGS_TIPS Class |
| 4 |
* |
| 5 |
* @package WPPIZZA |
| 6 |
* @subpackage WPPIZZA_MODULE_ORDERSETTINGS_TIPS |
| 7 |
* @copyright Copyright (c) 2015, Oliver Bach |
| 8 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 9 |
* @since 3.12.13 |
| 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_ORDERSETTINGS_TIPS{ |
| 24 |
|
| 25 |
private $settings_page = 'order_settings';/* which admin subpage (identified there by this->class_key) are we adding this to */ |
| 26 |
|
| 27 |
private $section_key = 'tips';/* must be unique */ |
| 28 |
|
| 29 |
|
| 30 |
function __construct() { |
| 31 |
/********************************************************** |
| 32 |
[add settings to admin] |
| 33 |
***********************************************************/ |
| 34 |
if(is_admin()){ |
| 35 |
/* add admin options settings page*/ |
| 36 |
add_filter('wppizza_filter_settings_sections_'.$this->settings_page.'', array($this, 'admin_options_settings'), 45, 5); |
| 37 |
/* add admin options settings page fields */ |
| 38 |
add_action('wppizza_admin_settings_section_fields_'.$this->settings_page.'', array($this, 'admin_options_fields_settings'), 10, 5); |
| 39 |
/**add default options **/ |
| 40 |
add_filter('wppizza_filter_setup_default_options', array( $this, 'options_default')); |
| 41 |
/**validate options**/ |
| 42 |
add_filter('wppizza_filter_options_validate', array( $this, 'options_validate'), 10, 2 ); |
| 43 |
} |
| 44 |
|
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
/******************************************************************************************************************************************************* |
| 49 |
* |
| 50 |
* |
| 51 |
* |
| 52 |
* [add admin page options] |
| 53 |
* |
| 54 |
* |
| 55 |
* |
| 56 |
********************************************************************************************************************************************************/ |
| 57 |
|
| 58 |
/*------------------------------------------------------------------------------ |
| 59 |
# |
| 60 |
# |
| 61 |
# [settings page] |
| 62 |
# |
| 63 |
# |
| 64 |
------------------------------------------------------------------------------*/ |
| 65 |
|
| 66 |
/*------------------------------------------------------------------------------ |
| 67 |
# [settings section - setting page] |
| 68 |
# @since 3.12.13 |
| 69 |
# @return array() |
| 70 |
------------------------------------------------------------------------------*/ |
| 71 |
function admin_options_settings($settings, $sections, $fields, $inputs, $help){ |
| 72 |
|
| 73 |
/*section*/ |
| 74 |
if($sections){ |
| 75 |
$settings['sections'][$this->section_key] = __('Tips', 'wppizza-admin'); |
| 76 |
} |
| 77 |
/*help*/ |
| 78 |
if($help){ |
| 79 |
$settings['help'][$this->section_key][] = array( |
| 80 |
'label'=>__('Tips', 'wppizza-admin'), |
| 81 |
'description'=>array( |
| 82 |
/* Translators: 1: WPPizza Name as defined by constant */ |
| 83 |
sprintf(__('Please note that tips must be enabled in "%s -> Order Form" first for the settings here to have any effect.', 'wppizza-admin'), WPPIZZA_NAME), |
| 84 |
__('Adjust settings as appropriate according to the information provided next to each individual option.', 'wppizza-admin'), |
| 85 |
) |
| 86 |
); |
| 87 |
} |
| 88 |
/*fields*/ |
| 89 |
if($fields){ |
| 90 |
|
| 91 |
$field = 'tips_display'; |
| 92 |
$settings['fields'][$this->section_key][$field] = array( __('Tips Display', 'wppizza-admin'), array( |
| 93 |
'value_key'=>$field, |
| 94 |
'option_key'=>$this->settings_page, |
| 95 |
'label'=>'', |
| 96 |
'description'=>array() |
| 97 |
)); |
| 98 |
|
| 99 |
$field = 'tips_percentage_options'; |
| 100 |
$settings['fields'][$this->section_key][$field] = array( __('Percentages selectable', 'wppizza-admin'), array( |
| 101 |
'value_key'=>$field, |
| 102 |
'option_key'=>$this->settings_page, |
| 103 |
'label'=>__('Enter comma separated percentages you want to make available for easy selection to the customer (use "." if you need decimal fractions)', 'wppizza-admin'), |
| 104 |
'description'=>array() |
| 105 |
)); |
| 106 |
|
| 107 |
$field = 'tips_percentage_default'; |
| 108 |
$settings['fields'][$this->section_key][$field] = array( __('Default percentage', 'wppizza-admin'), array( |
| 109 |
'value_key'=>$field, |
| 110 |
'option_key'=>$this->settings_page, |
| 111 |
'label'=>__('Enter a preselected tips percentage value.', 'wppizza-admin').' <span class="wppizza-highlight">'.__('Must be one of the selectable percentages values above (or 0 to ignore).', 'wppizza-admin').'</span>', |
| 112 |
'description'=>array() |
| 113 |
)); |
| 114 |
|
| 115 |
|
| 116 |
$field = 'tips_value_options'; |
| 117 |
$settings['fields'][$this->section_key][$field] = array( __('Fixed values selectable', 'wppizza-admin'), array( |
| 118 |
'value_key'=>$field, |
| 119 |
'option_key'=>$this->settings_page, |
| 120 |
'label'=>__('Enter comma separated fixed (decimal) values you want to make available for easy selection to the customer (use "." if you need decimal fractions)', 'wppizza-admin'), |
| 121 |
'description'=>array() |
| 122 |
)); |
| 123 |
|
| 124 |
$field = 'tips_value_default'; |
| 125 |
$settings['fields'][$this->section_key][$field] = array( __('Default fixed value', 'wppizza-admin'), array( |
| 126 |
'value_key'=>$field, |
| 127 |
'option_key'=>$this->settings_page, |
| 128 |
'label'=>__('Enter a preselected tips fixed value.', 'wppizza-admin').' <span class="wppizza-highlight">'.__('Must be one of the selectable (decimal) values above (or 0 to ignore).', 'wppizza-admin').'</span> <span>'.__('Takes precedence over percentage defaults.', 'wppizza-admin').'</span>', |
| 129 |
'description'=>array() |
| 130 |
)); |
| 131 |
|
| 132 |
} |
| 133 |
|
| 134 |
return $settings; |
| 135 |
} |
| 136 |
/*------------------------------------------------------------------------------ |
| 137 |
# [output option fields - setting page] |
| 138 |
# @since 3.12.13 |
| 139 |
# @return array() |
| 140 |
------------------------------------------------------------------------------*/ |
| 141 |
function admin_options_fields_settings($wppizza_options, $options_key, $field, $label, $description){ |
| 142 |
|
| 143 |
if($field=='tips_display'){ |
| 144 |
echo "<label>"; |
| 145 |
// tips input value shown - default, as normal |
| 146 |
echo "".__('Default', 'wppizza-admin')."<input id='".$field."' name='".WPPIZZA_SLUG."[".$options_key."][".$field."]' type='radio' ".checked($wppizza_options[$options_key][$field], 1, false)." value='1' /> "; |
| 147 |
// adding percentage dropdown to choose from next to Tips label |
| 148 |
echo "".__('Add percentage selection', 'wppizza-admin')."<input id='".$field."' name='".WPPIZZA_SLUG."[".$options_key."][".$field."]' type='radio' ".checked($wppizza_options[$options_key][$field],2,false)." value='2' /> "; |
| 149 |
// adding fixed value dropdown to choose from next to Tips label |
| 150 |
echo "".__('Add fixed value selection', 'wppizza-admin')."<input id='".$field."' name='".WPPIZZA_SLUG."[".$options_key."][".$field."]' type='radio' ".checked($wppizza_options[$options_key][$field],3,false)." value='3' /> "; |
| 151 |
// adding fixed value and percentage dropdown to choose from next to Tips label |
| 152 |
echo "".__('Add percentage and fixed value selection', 'wppizza-admin')."<input id='".$field."' name='".WPPIZZA_SLUG."[".$options_key."][".$field."]' type='radio' ".checked($wppizza_options[$options_key][$field],4,false)." value='4' /> "; |
| 153 |
|
| 154 |
// tips input value hidden, only percentage dropdown shown - currently not implemented (might not ever get implemented in fact) |
| 155 |
//echo "".__('Percentages Only', 'wppizza-admin')."<input id='".$field."' name='".WPPIZZA_SLUG."[".$options_key."][".$field."]' type='radio' ".checked($wppizza_options[$options_key][$field],3,false)." value='3' /> "; |
| 156 |
|
| 157 |
echo "".$label.""; |
| 158 |
echo "</label>"; |
| 159 |
echo"".$description.""; |
| 160 |
} |
| 161 |
|
| 162 |
if($field=='tips_percentage_options'){ |
| 163 |
echo "<label>"; |
| 164 |
echo "<input id='".$field."' name='".WPPIZZA_SLUG."[".$options_key."][".$field."]' size='15' type='text' value='".implode(',',$wppizza_options[$options_key][$field])."' />"; |
| 165 |
echo "".$label.""; |
| 166 |
echo "</label>"; |
| 167 |
echo"".$description.""; |
| 168 |
} |
| 169 |
|
| 170 |
if($field=='tips_percentage_default'){ |
| 171 |
echo "<label>"; |
| 172 |
echo "<input id='".$field."' name='".WPPIZZA_SLUG."[".$options_key."][".$field."]' size='1' type='text' value='".$wppizza_options[$options_key][$field]."' />"; |
| 173 |
echo "".$label.""; |
| 174 |
echo "</label>"; |
| 175 |
echo"".$description.""; |
| 176 |
} |
| 177 |
|
| 178 |
if($field=='tips_value_options'){ |
| 179 |
echo "<label>"; |
| 180 |
echo "<input id='".$field."' name='".WPPIZZA_SLUG."[".$options_key."][".$field."]' size='15' type='text' value='".implode(',',(array)$wppizza_options[$options_key][$field])."' />"; |
| 181 |
echo "".$label.""; |
| 182 |
echo "</label>"; |
| 183 |
echo"".$description.""; |
| 184 |
} |
| 185 |
|
| 186 |
if($field=='tips_value_default'){ |
| 187 |
echo "<label>"; |
| 188 |
echo "<input id='".$field."' name='".WPPIZZA_SLUG."[".$options_key."][".$field."]' size='1' type='text' value='".$wppizza_options[$options_key][$field]."' />"; |
| 189 |
echo "".$label.""; |
| 190 |
echo "</label>"; |
| 191 |
echo"".$description.""; |
| 192 |
} |
| 193 |
|
| 194 |
} |
| 195 |
|
| 196 |
/*------------------------------------------------------------------------------ |
| 197 |
# [insert default option on install] |
| 198 |
# $parameter $options array() | filter passing on filtered options |
| 199 |
# @since 3.12.13 |
| 200 |
# @return array() |
| 201 |
------------------------------------------------------------------------------*/ |
| 202 |
function options_default($options){ |
| 203 |
|
| 204 |
$options[$this->settings_page]['tips_display'] = 1; |
| 205 |
$options[$this->settings_page]['tips_percentage_options'] = array(5,10,15,20); |
| 206 |
$options[$this->settings_page]['tips_percentage_default'] = 0; |
| 207 |
$options[$this->settings_page]['tips_value_options'] = array(1,2.50,5); |
| 208 |
$options[$this->settings_page]['tips_value_default'] = 0; |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
return $options; |
| 213 |
} |
| 214 |
|
| 215 |
/*------------------------------------------------------------------------------ |
| 216 |
# [validate options on save/update] |
| 217 |
# |
| 218 |
# @since 3.12.13 |
| 219 |
# @return array() |
| 220 |
------------------------------------------------------------------------------*/ |
| 221 |
function options_validate($options, $input){ |
| 222 |
/**make sure we get the full array on install/update**/ |
| 223 |
if ( empty( $_POST['_wp_http_referer'] ) ) { |
| 224 |
return $input; |
| 225 |
} |
| 226 |
/* |
| 227 |
settings |
| 228 |
*/ |
| 229 |
if(isset($_POST[''.WPPIZZA_SLUG.'_'.$this->settings_page.''])){ |
| 230 |
$options[$this->settings_page]['tips_display'] = in_array($input[$this->settings_page]['tips_display'], array(1,2,3,4)) ? absint($input[$this->settings_page]['tips_display']) : 1; |
| 231 |
$options[$this->settings_page]['tips_percentage_options']= array_unique(wppizza_validate_array($input[$this->settings_page]['tips_percentage_options'],'wppizza_validate_float_pc')); |
| 232 |
$options[$this->settings_page]['tips_percentage_default']= ( in_array(wppizza_validate_float_pc($input[$this->settings_page]['tips_percentage_default']), $options[$this->settings_page]['tips_percentage_options'])) ? wppizza_validate_float_pc($input[$this->settings_page]['tips_percentage_default']) : 0 ; |
| 233 |
|
| 234 |
$options[$this->settings_page]['tips_value_options']= array_unique(wppizza_validate_array($input[$this->settings_page]['tips_value_options'],'wppizza_format_price_float')); |
| 235 |
$options[$this->settings_page]['tips_value_default']= ( in_array(wppizza_validate_float_pc($input[$this->settings_page]['tips_value_default']), $options[$this->settings_page]['tips_value_options'])) ? wppizza_validate_float_pc($input[$this->settings_page]['tips_value_default']) : 0 ; |
| 236 |
|
| 237 |
} |
| 238 |
|
| 239 |
return $options; |
| 240 |
} |
| 241 |
} |
| 242 |
/*************************************************************** |
| 243 |
* |
| 244 |
* [ini] |
| 245 |
* |
| 246 |
***************************************************************/ |
| 247 |
$WPPIZZA_MODULE_ORDERSETTINGS_TIPS = new WPPIZZA_MODULE_ORDERSETTINGS_TIPS(); |
| 248 |
?> |