| 1 |
<?php |
| 2 |
/** |
| 3 |
* WPPIZZA_USER Class |
| 4 |
* |
| 5 |
* @package WPPIZZA |
| 6 |
* @subpackage WPPIZZA_USER |
| 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 |
class WPPIZZA_USER{ |
| 19 |
function __construct() { |
| 20 |
|
| 21 |
/*************** |
| 22 |
[add selected fields to the registration process - frontend] |
| 23 |
***************/ |
| 24 |
add_action( 'register_form', array( $this, 'user_register_formfields') ); |
| 25 |
add_action( 'user_register', array( $this, 'register_user_profile'), 100 ); |
| 26 |
/** |
| 27 |
registration fields - also invoked outside is_admin for themed profiles |
| 28 |
**/ |
| 29 |
add_action( 'show_user_profile', array( $this, 'print_user_profile') ); |
| 30 |
add_action( 'personal_options_update', array( $this, 'update_user_profile' )); |
| 31 |
|
| 32 |
/** |
| 33 |
multisite |
| 34 |
**/ |
| 35 |
if(is_multisite()){ |
| 36 |
add_action( 'signup_extra_fields', array( $this, 'user_register_formfields') ); |
| 37 |
add_filter( 'add_signup_meta',array($this, 'wppizza_ms_user_register_add_signup_meta'));//capture the data |
| 38 |
add_action( 'wpmu_activate_user', array($this, 'register_user_profile'), 10, 3 );//get the meta data out of signups and push it into wp_usermeta during activation |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
admin only |
| 43 |
**/ |
| 44 |
if(is_admin()){ |
| 45 |
/** |
| 46 |
registration fields - admin |
| 47 |
**/ |
| 48 |
add_action( 'edit_user_profile', array( $this, 'print_user_profile') ); |
| 49 |
add_action( 'edit_user_profile_update', array( $this, 'update_user_profile' )); |
| 50 |
} |
| 51 |
|
| 52 |
} |
| 53 |
|
| 54 |
/****************************************************** |
| 55 |
* |
| 56 |
* [save wppizza formfields that were added to WP registration form] |
| 57 |
* @return void |
| 58 |
******************************************************/ |
| 59 |
function register_user_profile( $user_id, $password = '', $meta = array()){ |
| 60 |
|
| 61 |
/** |
| 62 |
enabled form fields |
| 63 |
**/ |
| 64 |
$formfields = WPPIZZA() -> helpers -> enabled_formfields(); |
| 65 |
/** |
| 66 |
loop trough form fields enabled for registration |
| 67 |
**/ |
| 68 |
foreach( $formfields as $field ) { |
| 69 |
|
| 70 |
/** |
| 71 |
only listen deal with fields that were enabled to be used for registration |
| 72 |
**/ |
| 73 |
if(!empty($field['onregister'])) { |
| 74 |
|
| 75 |
|
| 76 |
$metaValue = ''; |
| 77 |
|
| 78 |
/* |
| 79 |
since v3.16, |
| 80 |
selects, radios, multicheckboxes are now post'ed using their indexes , |
| 81 |
so we map these back to the values here to keep this consistent with pre v3.16 registrations |
| 82 |
*/ |
| 83 |
if(in_array($field['type'], array('select', 'radio', 'multicheckbox'))){ |
| 84 |
|
| 85 |
/* |
| 86 |
select / radio |
| 87 |
convert posted numeric index to associated string |
| 88 |
*/ |
| 89 |
if(in_array($field['type'], array('select', 'radio'))){ |
| 90 |
|
| 91 |
//sanitise posted value (technically this should actually always be numeric) |
| 92 |
$postedIndex = wppizza_validate_string($_POST[''.WPPIZZA_SLUG.'_'.$field['key']]); |
| 93 |
//set meta value - if it exists |
| 94 |
$metaValue = !empty($field['value'][$postedIndex]) ? $field['value'][$postedIndex] : '' ; |
| 95 |
} |
| 96 |
|
| 97 |
/* |
| 98 |
multicheckbox |
| 99 |
convert posted array index |
| 100 |
*/ |
| 101 |
if(in_array($field['type'], array('multicheckbox'))){ |
| 102 |
|
| 103 |
//must be an array now |
| 104 |
$metaValue = array(); |
| 105 |
|
| 106 |
//sanitise posted value (technically this should actually always be numeric) |
| 107 |
$postedIndex = wppizza_validate_array($_POST[''.WPPIZZA_SLUG.'_'.$field['key']]); |
| 108 |
//set meta value - if it exists |
| 109 |
foreach($postedIndex as $idxKey){ |
| 110 |
if(!empty($field['value'][$idxKey])){ |
| 111 |
$metaValue[$idxKey] = $field['value'][$idxKey]; |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
} |
| 116 |
|
| 117 |
} |
| 118 |
/* |
| 119 |
all others (i.e non selects, non-radios, non-multicheckboxes) |
| 120 |
*/ |
| 121 |
else{ |
| 122 |
|
| 123 |
$metaValue = !empty($_POST[''.WPPIZZA_SLUG.'_'.$field['key']]) ? wppizza_validate_string($_POST[''.WPPIZZA_SLUG.'_'.$field['key']]) : false ; |
| 124 |
|
| 125 |
} |
| 126 |
|
| 127 |
/* |
| 128 |
set meta data |
| 129 |
*/ |
| 130 |
update_user_meta( $user_id, ''.WPPIZZA_SLUG.'_'.$field['key'], $metaValue ); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
distinctly add email from wp email field for cemail |
| 136 |
**/ |
| 137 |
if( isset($_POST['user_email']) ){ |
| 138 |
$sanitizedEmail = wppizza_validate_string($_POST['user_email']); |
| 139 |
update_user_meta( $user_id, ''.WPPIZZA_SLUG.'_cemail', $sanitizedEmail ); |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
/* update user */ |
| 144 |
$userdata = array( |
| 145 |
'ID' => $user_id, |
| 146 |
); |
| 147 |
$new_user_id = wp_update_user( $userdata ); |
| 148 |
|
| 149 |
|
| 150 |
return; |
| 151 |
} |
| 152 |
|
| 153 |
/******************************************************************************************** |
| 154 |
update profile |
| 155 |
@return void; |
| 156 |
@since unknown |
| 157 |
********************************************************************************************/ |
| 158 |
function update_user_profile($user_id){ |
| 159 |
/* |
| 160 |
bail if user does not have the required permissions |
| 161 |
*/ |
| 162 |
if ( !current_user_can( 'edit_user', $user_id ) ) { |
| 163 |
return false; |
| 164 |
} |
| 165 |
|
| 166 |
/* |
| 167 |
get and loop through enabled formfields |
| 168 |
*/ |
| 169 |
$formfields = WPPIZZA()->helpers->enabled_formfields(); |
| 170 |
|
| 171 |
foreach( $formfields as $field ) { |
| 172 |
|
| 173 |
/* |
| 174 |
only worry about fields that are set to be used when registering |
| 175 |
but also specifically excluding cemail here |
| 176 |
*/ |
| 177 |
if($field['key']!='cemail' && !empty($field['onregister'])) { |
| 178 |
|
| 179 |
/* |
| 180 |
sanitise arrays separately |
| 181 |
*/ |
| 182 |
if(!empty($_POST[''.WPPIZZA_SLUG.'_'.$field['key']]) && is_array($_POST[''.WPPIZZA_SLUG.'_'.$field['key']])){ |
| 183 |
|
| 184 |
$metaValue = array(); |
| 185 |
|
| 186 |
foreach($_POST[''.WPPIZZA_SLUG.'_'.$field['key']] as $arrKey => $val){ |
| 187 |
|
| 188 |
$metaValue[$arrKey] = wppizza_validate_string($val); |
| 189 |
|
| 190 |
} |
| 191 |
|
| 192 |
}else{ |
| 193 |
|
| 194 |
$metaValue = !empty($_POST[''.WPPIZZA_SLUG.'_'.$field['key']]) ? wppizza_validate_string($_POST[''.WPPIZZA_SLUG.'_'.$field['key']]) : false ; |
| 195 |
} |
| 196 |
|
| 197 |
update_user_meta( $user_id, ''.WPPIZZA_SLUG.'_'.$field['key'], $metaValue ); |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
distinctly add email from wp email field |
| 203 |
**/ |
| 204 |
$sanitizedEmail = wppizza_validate_string($_POST['email']); |
| 205 |
update_user_meta( $user_id, ''.WPPIZZA_SLUG.'_cemail', $sanitizedEmail ); |
| 206 |
|
| 207 |
return; |
| 208 |
} |
| 209 |
|
| 210 |
/* |
| 211 |
multisite signup add meta |
| 212 |
*/ |
| 213 |
function wppizza_ms_user_register_add_signup_meta($meta){ |
| 214 |
$formfields=WPPIZZA()->helpers->enabled_formfields(); |
| 215 |
foreach( $formfields as $field ) { |
| 216 |
if(!empty($field['onregister']) && isset($_POST[''.WPPIZZA_SLUG.'_'.$field['key']])) { |
| 217 |
/**selects/radios should be stored by index**/ |
| 218 |
if($field['type'] == 'select' || $field['type'] == 'radio'){ |
| 219 |
$posted=$_POST[''.WPPIZZA_SLUG.'_'.$field['key']]; |
| 220 |
$sanitizedInput = isset($field['value'][$posted]) ? $posted : null; |
| 221 |
}else{ |
| 222 |
$sanitizedInput = wppizza_validate_string($_POST[''.WPPIZZA_SLUG.'_'.$field['key']]); |
| 223 |
} |
| 224 |
|
| 225 |
$meta[''.WPPIZZA_SLUG.'_'.$field['key']] = $sanitizedInput; |
| 226 |
}} |
| 227 |
|
| 228 |
return $meta; |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
/******************************************************************************************** |
| 234 |
profile page - admin or themed profiles |
| 235 |
********************************************************************************************/ |
| 236 |
function print_user_profile($user){ |
| 237 |
|
| 238 |
global $wppizza_options; |
| 239 |
|
| 240 |
/* get user meta data */ |
| 241 |
$userMetaData=$this->user_meta($user->ID); |
| 242 |
|
| 243 |
/* get enabled formfields*/ |
| 244 |
$formfields=WPPIZZA()->helpers->enabled_formfields(); |
| 245 |
|
| 246 |
/* h3 header */ |
| 247 |
if($wppizza_options['localization']['user_profile_label_additional_info']!=''){ |
| 248 |
print'<h3>'.esc_html($wppizza_options['localization']['user_profile_label_additional_info']).'</h3>'; |
| 249 |
} |
| 250 |
|
| 251 |
print'<table class="form-table">'; |
| 252 |
foreach( $formfields as $field ) { |
| 253 |
|
| 254 |
/**lets exclude disabled and "email" as wp already has this of course, as well as gratuities**/ |
| 255 |
if($field['type']!='email' && $field['type']!='tips' && !empty($field['onregister'])){ |
| 256 |
|
| 257 |
$selectedValue = !empty($userMetaData[''.WPPIZZA_SLUG.'_'.$field['key'].'']) ? (maybe_unserialize($userMetaData['wppizza_'.$field['key'].''])) : ''; |
| 258 |
|
| 259 |
print'<tr><th><label for="'.esc_attr(WPPIZZA_SLUG.'_'.$field['key']).'">' . esc_html($field['lbl']) . '</label></th><td>'; |
| 260 |
|
| 261 |
/**normal text input**/ |
| 262 |
if ( $field['type']=='text'){ |
| 263 |
print'<input type="text" name="'.esc_attr(WPPIZZA_SLUG.'_'.$field['key']).'" id="'.esc_attr(WPPIZZA_SLUG.'_'.$field['key']).'" value="'.esc_attr($selectedValue).'" class="regular-text" />'; |
| 264 |
} |
| 265 |
/**textareas**/ |
| 266 |
if ( $field['type']=='textarea'){ |
| 267 |
print'<textarea name="'.esc_attr(WPPIZZA_SLUG.'_'.$field['key']).'" id="'.esc_attr(WPPIZZA_SLUG.'_'.$field['key']).'" rows="5" cols="30">'.esc_textarea($selectedValue).'</textarea>'; |
| 268 |
} |
| 269 |
/**select**/ |
| 270 |
if ( $field['type']=='select'){ |
| 271 |
|
| 272 |
$setVal = wppizza_decode_entities_trim($selectedValue); |
| 273 |
|
| 274 |
print'<select name="'.esc_attr(WPPIZZA_SLUG.'_'.$field['key']).'" id="'.esc_attr(WPPIZZA_SLUG.'_'.$field['key']).'">'; |
| 275 |
|
| 276 |
print'<option value="">-----------</option>'; |
| 277 |
|
| 278 |
foreach($field['value'] as $sKey => $value){ |
| 279 |
|
| 280 |
$optVal = wppizza_decode_entities_trim($value); |
| 281 |
|
| 282 |
print'<option value="'.esc_attr($value).'" '.selected($optVal, $setVal, false).'>'.esc_html($value).'</option>'; |
| 283 |
|
| 284 |
} |
| 285 |
print'</select>'; |
| 286 |
} |
| 287 |
/**checkbox**/ |
| 288 |
if ($field['type']=='checkbox'){ |
| 289 |
print'<input type="checkbox" name="'.esc_attr(WPPIZZA_SLUG.'_'.$field['key']).'" id="'.esc_attr(WPPIZZA_SLUG.'_'.$field['key']).'" value="1" '.checked(!empty($selectedValue),true,false).' />'; |
| 290 |
} |
| 291 |
/**multicheckbox**/ |
| 292 |
if ($field['type']=='multicheckbox'){ |
| 293 |
foreach($field['value'] as $mKey => $multicheckbox_value){ |
| 294 |
echo'<span><input type="checkbox" name="'.esc_attr(WPPIZZA_SLUG.'_'.$field['key']).'['.esc_attr($mKey).']" id="'.esc_attr(WPPIZZA_SLUG.'_'.$field['key'].'_'.$mKey).'" '.checked(!empty($selectedValue[$mKey]),true,false).' value="'.esc_attr($multicheckbox_value).'"/>'.esc_html($multicheckbox_value).' </span>'; |
| 295 |
} |
| 296 |
} |
| 297 |
/**radio**/ |
| 298 |
if ($field['type']=='radio'){ |
| 299 |
|
| 300 |
$setVal = wppizza_decode_entities_trim($selectedValue); |
| 301 |
|
| 302 |
foreach($field['value'] as $rKey=>$radio_value){ |
| 303 |
|
| 304 |
$optVal = wppizza_decode_entities_trim($radio_value); |
| 305 |
|
| 306 |
echo'<span><input type="radio" name="'.esc_attr(WPPIZZA_SLUG.'_'.$field['key']).'" id="'.esc_attr(WPPIZZA_SLUG.'_'.$field['key']).'" '.checked($optVal, $setVal, false).' value="'.esc_attr($radio_value).'"/>'.esc_html($radio_value).' </span>'; |
| 307 |
} |
| 308 |
} |
| 309 |
print"</td></tr>"; |
| 310 |
}} |
| 311 |
print"</table>"; |
| 312 |
} |
| 313 |
|
| 314 |
|
| 315 |
/****************************************************** |
| 316 |
* |
| 317 |
* [show selected fields in WP registration form] |
| 318 |
* |
| 319 |
******************************************************/ |
| 320 |
function user_register_formfields(){ |
| 321 |
|
| 322 |
$formfields = WPPIZZA()->helpers->enabled_formfields(); |
| 323 |
|
| 324 |
foreach($formfields as $field){ |
| 325 |
if(!empty($field['onregister'])) { |
| 326 |
|
| 327 |
|
| 328 |
/* name / id / label for*/ |
| 329 |
$name_id = ''.WPPIZZA_SLUG.'_'.$field['key'].''; |
| 330 |
/** class **/ |
| 331 |
$class= 'input'; |
| 332 |
|
| 333 |
/* |
| 334 |
entered value - strings / text(areas) only |
| 335 |
- i.e no arrays to eliminate potential fatal errors in case the page gets reloaded with "This username is already registered" or some such |
| 336 |
*/ |
| 337 |
if(in_array( $field['type'], array('text', 'textarea'))){ |
| 338 |
$input_value = !empty($_POST[ ''.WPPIZZA_SLUG.'_'.$field['key']]) && !is_array($_POST[ ''.WPPIZZA_SLUG.'_'.$field['key']]) ? stripslashes(wppizza_validate_string($_POST[ ''.WPPIZZA_SLUG.'_'.$field['key']])) : '' ; |
| 339 |
} |
| 340 |
|
| 341 |
/* |
| 342 |
output |
| 343 |
*/ |
| 344 |
echo'<p>'; |
| 345 |
/* label */ |
| 346 |
echo'<label for="' . esc_attr($name_id) . '">'; |
| 347 |
/* text input */ |
| 348 |
if ( $field['type']=='text'){ |
| 349 |
echo''.esc_html($field['lbl']).'<br />'; |
| 350 |
echo'<input type="text" name="' . esc_attr($name_id) . '" id="' . esc_attr($name_id) . '" class="' . esc_attr($class) . '" value="'. esc_attr($input_value) . '" size="20" />'; |
| 351 |
} |
| 352 |
/**textareas**/ |
| 353 |
if ( $field['type']=='textarea'){ |
| 354 |
echo''.esc_html($field['lbl']).'<br />'; |
| 355 |
print'<textarea name="' . esc_attr($name_id) . '" id="' . esc_attr($name_id) . '" class="' . esc_attr($class) . '" rows="5" cols="30">' . esc_textarea($input_value) . '</textarea>'; |
| 356 |
} |
| 357 |
/**select**/ |
| 358 |
if ( $field['type']=='select'){ |
| 359 |
echo''.esc_html($field['lbl']).'<br />'; |
| 360 |
print'<select name="' . esc_attr($name_id) . '" id="' . esc_attr($name_id) . '" class="' . esc_attr($class) . '">'; |
| 361 |
print'<option value="">--------</option>'; |
| 362 |
foreach($field['value'] as $key => $select_value){ |
| 363 |
print'<option value="' . esc_attr($key) . '" '.selected($key,$select_value,false).'>' . esc_html($select_value) . '</option>'; |
| 364 |
} |
| 365 |
print'</select>'; |
| 366 |
} |
| 367 |
/**checkbox**/ |
| 368 |
if ( $field['type']=='checkbox'){ |
| 369 |
echo''.esc_html($field['lbl']).' '; |
| 370 |
echo'<input type="checkbox" name="' . esc_attr($name_id) . '" id="' . esc_attr($name_id) . '" class="" value="1" />'; |
| 371 |
} |
| 372 |
/**multicheckbox**/ |
| 373 |
if ( $field['type'] == 'multicheckbox'){ |
| 374 |
echo''.esc_html($field['lbl']).'<br />'; |
| 375 |
foreach($field['value'] as $key => $select_value){ |
| 376 |
/* show multi checkbox options */ |
| 377 |
echo'<span><input type="checkbox" name="' . esc_attr($name_id) . '[]" id="' . esc_attr($name_id . '_'.$key).'" value="'. esc_attr($key) . '" />'.esc_html($select_value).' </span>'; |
| 378 |
} |
| 379 |
} |
| 380 |
/**radio**/ |
| 381 |
if ( $field['type']=='radio'){ |
| 382 |
echo''.esc_html($field['lbl']).'<br />'; |
| 383 |
$i=0; |
| 384 |
foreach($field['value'] as $key => $select_value){ |
| 385 |
/* show radio options, preselecting first one */ |
| 386 |
echo'<span><input type="radio" name="' . esc_attr($name_id) . '" id="' . esc_attr($name_id) . '" value="'. esc_attr($key) . '" '.checked($i,0,false).'/>'.esc_html($select_value).' </span>'; |
| 387 |
$i++; |
| 388 |
} |
| 389 |
} |
| 390 |
echo'</label>'; |
| 391 |
echo'</p>'; |
| 392 |
} |
| 393 |
} |
| 394 |
} |
| 395 |
|
| 396 |
/****************************************************** |
| 397 |
* |
| 398 |
* [update meta from order page ] |
| 399 |
* set user_id to force update for specific user (when creating account) |
| 400 |
******************************************************/ |
| 401 |
function update_profile($user_id = false, $userdata = false, $force_update = false){ |
| 402 |
|
| 403 |
/* |
| 404 |
can users register and is there actually any data to update ? |
| 405 |
*/ |
| 406 |
$users_can_register = is_multisite() ? apply_filters('option_users_can_register', false) : get_option('users_can_register'); |
| 407 |
/* users cannot register anyway or userdata empty, bail */ |
| 408 |
if(empty($users_can_register) || empty($userdata)){ |
| 409 |
return; |
| 410 |
} |
| 411 |
|
| 412 |
/********* |
| 413 |
get formfield keys native to this plugin to distinguish between |
| 414 |
own wppizza fields or fields that have been added by filters |
| 415 |
*********/ |
| 416 |
$native_formfields = WPPIZZA() -> helpers -> native_formfields(); |
| 417 |
|
| 418 |
/* |
| 419 |
user id 0 / not logged in / no userdata and not forced update, bail |
| 420 |
*/ |
| 421 |
if((empty($user_id) || !is_user_logged_in()) && !$force_update ){return;} |
| 422 |
|
| 423 |
|
| 424 |
/* |
| 425 |
get all enabled formfields |
| 426 |
*/ |
| 427 |
$formfields = WPPIZZA() -> helpers -> enabled_formfields(); |
| 428 |
|
| 429 |
|
| 430 |
/** |
| 431 |
loop trough enabled form fields checking they are enabled for registration |
| 432 |
and update what we can |
| 433 |
**/ |
| 434 |
foreach( $formfields as $ffKey => $field ) { |
| 435 |
|
| 436 |
/** |
| 437 |
only capture fields that were enabled to be used for registration |
| 438 |
**/ |
| 439 |
if(!empty($field['onregister']) && isset($userdata[$ffKey]) && isset($field['value']) && is_array($field['value']) ) { |
| 440 |
|
| 441 |
$metaValue = $userdata[$ffKey]['value']; |
| 442 |
|
| 443 |
|
| 444 |
/* |
| 445 |
map if necessary |
| 446 |
*/ |
| 447 |
if(in_array($field['type'], array('select', 'radio', 'checkbox', 'multicheckbox'))){ |
| 448 |
|
| 449 |
/* |
| 450 |
select |
| 451 |
convert numeric index back to string |
| 452 |
*/ |
| 453 |
if(in_array($field['type'], array('select'))){ |
| 454 |
|
| 455 |
//for backwards compatibility with other plugins |
| 456 |
if(!isset($native_formfields[$ffKey])){ |
| 457 |
$metaValue = wppizza_sanitize_post_vars(wppizza_decode_entities_trim($metaValue)); |
| 458 |
} |
| 459 |
$metaValue = isset($field['value'][$metaValue]) ? $field['value'][$metaValue] : '' ; |
| 460 |
|
| 461 |
} |
| 462 |
|
| 463 |
/* |
| 464 |
radio |
| 465 |
convert numeric index back to string |
| 466 |
*/ |
| 467 |
if(in_array($field['type'], array('radio'))){ |
| 468 |
$metaValue = isset($field['value'][$metaValue]) ? $field['value'][$metaValue] : '' ; |
| 469 |
} |
| 470 |
|
| 471 |
/* |
| 472 |
checkbox - dont use the value (as it might be a string saying "No" or some such) |
| 473 |
but the boolean valueSet here |
| 474 |
*/ |
| 475 |
if(in_array($field['type'], array('checkbox'))){ |
| 476 |
$metaValue = !empty($userdata[$ffKey]['ischecked']) ? true : false ;//bool |
| 477 |
} |
| 478 |
|
| 479 |
/* |
| 480 |
multicheckbox |
| 481 |
convert numeric indexes back to string |
| 482 |
*/ |
| 483 |
if(in_array($field['type'], array('multicheckbox'))){ |
| 484 |
|
| 485 |
|
| 486 |
$selected = array_flip(wppizza_strtoarray($metaValue)); |
| 487 |
$intersected = array_intersect_key($field['value'], $selected); |
| 488 |
$metaValue = $intersected; |
| 489 |
} |
| 490 |
|
| 491 |
} |
| 492 |
|
| 493 |
/* |
| 494 |
update the meta |
| 495 |
*/ |
| 496 |
update_user_meta( $user_id, ''.WPPIZZA_SLUG.'_'.$ffKey, $metaValue ); |
| 497 |
} |
| 498 |
} |
| 499 |
|
| 500 |
return; |
| 501 |
} |
| 502 |
|
| 503 |
/****************************************************** |
| 504 |
* |
| 505 |
* [create account from order page when order has been successfully executed] |
| 506 |
* |
| 507 |
******************************************************/ |
| 508 |
function create_account($order_id = false, $userdata = false){ |
| 509 |
global $wp_version; |
| 510 |
/* |
| 511 |
already logged in, users cannot register, no userdata or no email, bail |
| 512 |
*/ |
| 513 |
$set_email = !empty($userdata['cemail']['value']) ? $userdata['cemail']['value'] : false ; |
| 514 |
$can_register = is_multisite() ? apply_filters('option_users_can_register', false) : get_option('users_can_register'); |
| 515 |
if( is_user_logged_in() || empty($can_register) || empty($set_email) ){ |
| 516 |
return; |
| 517 |
} |
| 518 |
|
| 519 |
/************************************************ |
| 520 |
check if email exists already |
| 521 |
if it does not carry on adding account |
| 522 |
************************************************/ |
| 523 |
$user_id = username_exists( $set_email ); |
| 524 |
$email_id = email_exists( $set_email ); |
| 525 |
|
| 526 |
|
| 527 |
/** |
| 528 |
user already exists, just login and update meta |
| 529 |
i dont think one should be doing this |
| 530 |
as anyone could use any others email to get logged in ...!? |
| 531 |
**/ |
| 532 |
//if($user_id && $email_id){ |
| 533 |
// /* just login */ |
| 534 |
// wp_set_auth_cookie( $user_id ); |
| 535 |
// /** update user profile */ |
| 536 |
// $this->update_profile($user_id); |
| 537 |
//} |
| 538 |
|
| 539 |
|
| 540 |
/** |
| 541 |
new user |
| 542 |
**/ |
| 543 |
if(!$user_id && !$email_id){ |
| 544 |
|
| 545 |
/* |
| 546 |
change name and email address "From" for registration emals (to avoid it simply sayin "Wordpress") |
| 547 |
@since 3.7.1 |
| 548 |
*/ |
| 549 |
add_filter( 'wp_mail_from', array($this, 'registrations_sender_email' )); |
| 550 |
add_filter( 'wp_mail_from_name', array($this, 'registrations_sender_name' )); |
| 551 |
|
| 552 |
/*generate a pw**/ |
| 553 |
$user_password = wp_generate_password( 10, true ); |
| 554 |
/*create the user**/ |
| 555 |
$user_id_new = wp_create_user( $set_email, $user_password, $set_email ); |
| 556 |
/*send un/pw to user*/ |
| 557 |
if($user_id_new && $user_password!=''){/*bit of overkill*/ |
| 558 |
|
| 559 |
$new_user_notification = apply_filters('wppizza_new_user_notification', 'both');/* should return 'user', 'admin' or 'both'. default 'both' */ |
| 560 |
|
| 561 |
/*old wp versions <4.3**/ |
| 562 |
if ( version_compare( $wp_version, '4.3', '<' ) ) { |
| 563 |
wp_new_user_notification( $user_id_new, $user_password ); |
| 564 |
} |
| 565 |
if ( version_compare( $wp_version, '4.3', '==' ) ) { |
| 566 |
wp_new_user_notification( $user_id_new, $new_user_notification ); |
| 567 |
} |
| 568 |
if ( version_compare( $wp_version, '4.3.1', '>=' ) ) { |
| 569 |
wp_new_user_notification( $user_id_new, null, $new_user_notification ); |
| 570 |
} |
| 571 |
/**login too*/ |
| 572 |
wp_set_auth_cookie( $user_id_new ); |
| 573 |
/**turn off admin bar front by default**/ |
| 574 |
update_user_meta( $user_id_new, 'show_admin_bar_front', 'false' ); |
| 575 |
/** force update user profile */ |
| 576 |
$this->update_profile($user_id_new, $userdata, true); |
| 577 |
|
| 578 |
/*************************************************************** |
| 579 |
associate order with this userid now |
| 580 |
****************************************************************/ |
| 581 |
$update_db_values = array(); |
| 582 |
/** amend wp_user_id */ |
| 583 |
$update_db_values['wp_user_id'] = array('type'=> '%d', 'data' =>(int)$user_id_new); |
| 584 |
/* update db */ |
| 585 |
$order_update_user_id = WPPIZZA()->db->update_order(false, $order_id, false , $update_db_values); |
| 586 |
|
| 587 |
} |
| 588 |
} |
| 589 |
return; |
| 590 |
} |
| 591 |
|
| 592 |
/****************************************************** |
| 593 |
* |
| 594 |
* [set user registration from email and name |
| 595 |
* instead of default "Wordpress" , "wordpress@...."] |
| 596 |
* @since 3.7.1 |
| 597 |
* |
| 598 |
******************************************************/ |
| 599 |
function registrations_sender_email( $email_address ) { |
| 600 |
/* |
| 601 |
set to do not reply if part before @ is still "wordpress" |
| 602 |
*/ |
| 603 |
$x_email_address = explode('@', $email_address); |
| 604 |
if(!empty($x_email_address[0]) && strtolower(trim($x_email_address[0])) == 'wordpress' && !empty($x_email_address[1])){ |
| 605 |
/* simply replace "wordpress" as the domain has already been dealt with by wp_mail function */ |
| 606 |
$email_address = 'do-not-reply@'.$x_email_address[1]; |
| 607 |
} |
| 608 |
return $email_address; |
| 609 |
} |
| 610 |
|
| 611 |
/******************************************************************************************** |
| 612 |
# @since unknown |
| 613 |
********************************************************************************************/ |
| 614 |
function registrations_sender_name( $email_from ) { |
| 615 |
/* |
| 616 |
replace email from, if it is still set to be wordpress here |
| 617 |
*/ |
| 618 |
if(empty($email_from) || strtolower(trim($email_from)) == 'wordpress' ){ |
| 619 |
global $blog_id; |
| 620 |
$blog_info = WPPIZZA() -> helpers -> wppizza_blog_details($blog_id); |
| 621 |
|
| 622 |
$email_from = wp_specialchars_decode($blog_info['blogname'], ENT_QUOTES ); |
| 623 |
} |
| 624 |
return $email_from; |
| 625 |
} |
| 626 |
|
| 627 |
/****************************************************** |
| 628 |
* |
| 629 |
* [login form markup on order page] |
| 630 |
* |
| 631 |
******************************************************/ |
| 632 |
/************************************************************************ |
| 633 |
[output login form or logout link on order page or user history or admin order history by shortcode] |
| 634 |
************************************************************************/ |
| 635 |
function login_form($show_registration_disabled = false, $do_login = true, $force_login = false){ |
| 636 |
global $wppizza_options; |
| 637 |
$txt=$wppizza_options['localization']; |
| 638 |
$users_can_register = is_multisite() ? apply_filters('option_users_can_register', false) : get_option('users_can_register'); |
| 639 |
$login_form_args = array('echo'=>false, 'remember' => false); |
| 640 |
|
| 641 |
/* |
| 642 |
if: not forcing to show login, |
| 643 |
or: registration disabled sitewide, |
| 644 |
or: already logged in, |
| 645 |
or nothing in cart (if on orderpage) |
| 646 |
do nothing |
| 647 |
*/ |
| 648 |
if(empty($force_login)){ |
| 649 |
|
| 650 |
if(empty($users_can_register) && $show_registration_disabled && !is_user_logged_in()){ |
| 651 |
return __('Sorry, user registration is disabled on this system', 'wppizza-admin'); |
| 652 |
} |
| 653 |
|
| 654 |
if(empty($users_can_register) || is_user_logged_in() || empty($do_login)){ |
| 655 |
return; |
| 656 |
} |
| 657 |
} |
| 658 |
|
| 659 |
/** |
| 660 |
login enabled |
| 661 |
**/ |
| 662 |
if($do_login) { |
| 663 |
|
| 664 |
/* set classes */ |
| 665 |
$anchor_name = '' . WPPIZZA_PREFIX . '-login'; |
| 666 |
$class = '' . WPPIZZA_PREFIX . '-login'; |
| 667 |
$class_toggle = '' . WPPIZZA_PREFIX . '-login-option'; |
| 668 |
$class_show = '' . WPPIZZA_PREFIX . '-login-show'; |
| 669 |
$class_cancel = '' . WPPIZZA_PREFIX . '-login-cancel'; |
| 670 |
$class_fieldset = '' . WPPIZZA_PREFIX . '-fieldset ' . WPPIZZA_PREFIX . '-login-fieldset'; |
| 671 |
$class_form = '' . WPPIZZA_PREFIX . '-login-form'; |
| 672 |
$class_info = '' . WPPIZZA_PREFIX . '-login-info'; |
| 673 |
$class_password = '' . WPPIZZA_PREFIX . '-login-lostpw'; |
| 674 |
|
| 675 |
|
| 676 |
$login_form = apply_filters('wppizza_filter_login_markup', wp_login_form(apply_filters('wppizza_filter_loginform_arguments', $login_form_args))); |
| 677 |
/* let's add a nonce, just for the hell of it. cannot do any harm */ |
| 678 |
$nonce = wp_nonce_field( ''.WPPIZZA_PREFIX.'_nonce_login',''.WPPIZZA_PREFIX.'_nonce_login', false, false); |
| 679 |
$login_form = str_ireplace('</form', $nonce.'</form', $login_form); |
| 680 |
/* |
| 681 |
ini markup array |
| 682 |
*/ |
| 683 |
$markup = array(); |
| 684 |
/* |
| 685 |
get markup |
| 686 |
*/ |
| 687 |
if(file_exists( WPPIZZA_TEMPLATE_DIR . '/markup/global/login.php')){ |
| 688 |
require(WPPIZZA_TEMPLATE_DIR.'/markup/global/login.php'); |
| 689 |
}else{ |
| 690 |
require(WPPIZZA_PATH.'templates/markup/global/login.php'); |
| 691 |
} |
| 692 |
/* |
| 693 |
apply filter if required and implode for output |
| 694 |
*/ |
| 695 |
$markup = apply_filters('wppizza_filter_login_widget_markup', $markup); |
| 696 |
$markup = implode('', $markup); |
| 697 |
|
| 698 |
|
| 699 |
return $markup; |
| 700 |
} |
| 701 |
} |
| 702 |
|
| 703 |
/******************************************************************************** |
| 704 |
[output |
| 705 |
div with radio options to continue as guest or simultaneous registration] |
| 706 |
will not output anything if registration is disabled or emails are not on |
| 707 |
order page |
| 708 |
********************************************************************************/ |
| 709 |
function profile_options(){ |
| 710 |
global $wppizza_options; |
| 711 |
$txt = $wppizza_options['localization']; |
| 712 |
$enabled_formfields = WPPIZZA()->helpers->enabled_formfields(); |
| 713 |
$users_can_register = is_multisite() ? apply_filters('option_users_can_register', false) : get_option('users_can_register'); |
| 714 |
$user_session = WPPIZZA()->session-> get_userdata(); |
| 715 |
/*********************************************************** |
| 716 |
check if we have the email set to enabled and required |
| 717 |
as otherwise new registration on order will not work |
| 718 |
as there's noweher to send the password to |
| 719 |
*************************************************************/ |
| 720 |
$can_register = (isset($enabled_formfields['cemail']) && !empty($enabled_formfields['cemail']['required'])) ? true : false ; |
| 721 |
/** profle update ? */ |
| 722 |
$profile_update = (is_user_logged_in() && !empty($users_can_register)) ? true : false; |
| 723 |
/** create account */ |
| 724 |
$create_account = (!is_user_logged_in() && !empty($users_can_register) && !empty($can_register)) ? true : false; |
| 725 |
|
| 726 |
/* |
| 727 |
user is logged in and |
| 728 |
registration enabled |
| 729 |
add profile update checkbox |
| 730 |
*/ |
| 731 |
if($profile_update) { |
| 732 |
|
| 733 |
$id = WPPIZZA_PREFIX . '_profile_update'; |
| 734 |
$class = WPPIZZA_PREFIX . '_profile_update'; |
| 735 |
$name = WPPIZZA_PREFIX . '_profile_update'; |
| 736 |
$checked = checked(!empty($user_session[''.WPPIZZA_SLUG.'_profile_update']),true,false); |
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
/* |
| 741 |
ini markup array |
| 742 |
*/ |
| 743 |
$markup = array(); |
| 744 |
/* |
| 745 |
get markup |
| 746 |
*/ |
| 747 |
if(file_exists( WPPIZZA_TEMPLATE_DIR . '/markup/global/profile.update.php')){ |
| 748 |
require(WPPIZZA_TEMPLATE_DIR.'/markup/global/profile.update.php'); |
| 749 |
}else{ |
| 750 |
require(WPPIZZA_PATH.'templates/markup/global/profile.update.php'); |
| 751 |
} |
| 752 |
|
| 753 |
/* |
| 754 |
filter and |
| 755 |
implode for output |
| 756 |
*/ |
| 757 |
$markup = apply_filters('wppizza_filter_profile_update_markup', $markup); |
| 758 |
$markup = implode('',$markup); |
| 759 |
|
| 760 |
return $markup; |
| 761 |
} |
| 762 |
|
| 763 |
|
| 764 |
/* |
| 765 |
user is NOT logged in and |
| 766 |
registration enabled (with email set as form field) |
| 767 |
add register user / continue as guest |
| 768 |
radio options, provided email field exists and is required |
| 769 |
*/ |
| 770 |
if($create_account) { |
| 771 |
$id_create_account = WPPIZZA_PREFIX . '-create-account'; |
| 772 |
$class_create_account = WPPIZZA_PREFIX . '-create-account'; |
| 773 |
$id_account_guest = WPPIZZA_PREFIX . '_account_guest'; |
| 774 |
$id_account_register = WPPIZZA_PREFIX . '_account_register'; |
| 775 |
$class_guest = ''.WPPIZZA_PREFIX . '_account '.WPPIZZA_PREFIX . '_account_guest'; |
| 776 |
$class_register = ''.WPPIZZA_PREFIX . '_account '.WPPIZZA_PREFIX . '_account_register'; |
| 777 |
$name = WPPIZZA_PREFIX . '_account'; |
| 778 |
$checked_guest = checked(empty($user_session[''.WPPIZZA_SLUG.'_account']),true,false); |
| 779 |
$checked_register = checked(!empty($user_session[''.WPPIZZA_SLUG.'_account']),true,false); |
| 780 |
$id_register_info = WPPIZZA_PREFIX . '-user-register-info'; |
| 781 |
$register_info_css_visibility = empty($user_session[''.WPPIZZA_SLUG.'_account']) ? 'display:none' : 'display:block' ;//show/hide register info depending on selection |
| 782 |
|
| 783 |
/* |
| 784 |
ini markup array |
| 785 |
*/ |
| 786 |
$markup = array(); |
| 787 |
/* |
| 788 |
get markup |
| 789 |
*/ |
| 790 |
if(file_exists( WPPIZZA_TEMPLATE_DIR . '/markup/global/profile.register.php')){ |
| 791 |
require(WPPIZZA_TEMPLATE_DIR.'/markup/global/profile.register.php'); |
| 792 |
}else{ |
| 793 |
require(WPPIZZA_PATH.'templates/markup/global/profile.register.php'); |
| 794 |
} |
| 795 |
/* |
| 796 |
filter and |
| 797 |
implode for output |
| 798 |
*/ |
| 799 |
$markup = apply_filters('wppizza_filter_profile_register_markup', $markup); |
| 800 |
$markup = implode('',$markup); |
| 801 |
|
| 802 |
return $markup; |
| 803 |
} |
| 804 |
|
| 805 |
/* if it gets here, markup will be an empty string */ |
| 806 |
return ''; |
| 807 |
} |
| 808 |
|
| 809 |
|
| 810 |
/************************************************************************************************************************** |
| 811 |
fieldset markup userdata |
| 812 |
labels and inputs |
| 813 |
or selected only |
| 814 |
**************************************************************************************************************************/ |
| 815 |
function formfields_inputs($ffs, $order_formatted = array(), $type = 'orderpage'){ |
| 816 |
|
| 817 |
$formfields =array(); |
| 818 |
|
| 819 |
foreach($ffs as $key=>$ff){ |
| 820 |
|
| 821 |
/* |
| 822 |
if omit_if_optional is set, |
| 823 |
omit formfield if field is not required to be filled in |
| 824 |
the 'required_attribute' willalready be set (or not as the case may be) |
| 825 |
depending on if it's pickup or delivery and required or not for those |
| 826 |
|
| 827 |
Note: Perhaps change to 'hidden' type here - instead of omitting entirely - to be able to switch |
| 828 |
between pickup and delivery and keep any session values. |
| 829 |
However, this needs thorough checking with selects , radios etc, so for now let's leave this as is |
| 830 |
*/ |
| 831 |
if(!empty($ff['omit_if_optional']) && empty($ff['required_attribute'])){ |
| 832 |
continue; |
| 833 |
} |
| 834 |
|
| 835 |
/* |
| 836 |
omit tips as they are displayed in the order summary |
| 837 |
*/ |
| 838 |
if($ff['type'] != 'tips'){ |
| 839 |
|
| 840 |
|
| 841 |
/* |
| 842 |
allow label etc filtering per formfield inputs |
| 843 |
*/ |
| 844 |
$ff = apply_filters('wppizza_filter_formfields_inputs_'.$key.'', $ff, $type ); |
| 845 |
|
| 846 |
/* |
| 847 |
alternative to above but key and type passed on as arguments instead |
| 848 |
*/ |
| 849 |
$ff = apply_filters('wppizza_filter_formfields_input', $ff, $key, $type ); |
| 850 |
|
| 851 |
/* |
| 852 |
ini array |
| 853 |
*/ |
| 854 |
$formfields[$key] = array(); |
| 855 |
|
| 856 |
/* set class adding type */ |
| 857 |
$formfields[$key]['class'] = ''.WPPIZZA_PREFIX.'-'.$key.''; |
| 858 |
if(!empty($ff['type'])){ |
| 859 |
$formfields[$key]['class'] .= ' '.WPPIZZA_PREFIX.'-'.$ff['type'].''; |
| 860 |
} |
| 861 |
|
| 862 |
/* set style if set (currenty only used on *orderpage* formfields) */ |
| 863 |
$formfields[$key]['style'] = !empty($ff['style']) ? 'style="'.$ff['style'].'"' : '' ; |
| 864 |
|
| 865 |
/* set field */ |
| 866 |
$formfields[$key]['field'] = ''; |
| 867 |
|
| 868 |
/* normal links - no inputs */ |
| 869 |
if($ff['type']=='link'){ |
| 870 |
$formfields[$key]['field'] .= '<label for="'. esc_html($key) .'"' . $ff['required_class'] . '>'; |
| 871 |
$formfields[$key]['field'] .= '' . $ff['label'] . ''; |
| 872 |
$formfields[$key]['field'] .= '</label>'; |
| 873 |
} |
| 874 |
|
| 875 |
/* text / emails / (tips are displayed in subtotals) */ |
| 876 |
if(in_array($ff['type'],array('text', 'email'))){ |
| 877 |
$formfields[$key]['field'] .= '<label for="'. esc_html($key) .'"' . $ff['required_class'] . '>'; |
| 878 |
$formfields[$key]['field'] .= '' . $ff['label'] . ''; |
| 879 |
$formfields[$key]['field'] .= '</label>'; |
| 880 |
$formfields[$key]['field'] .= '<input id="'. esc_html($key) .'" name="'. $key.'" type="text" value="' . esc_html($ff['value']) . '" placeholder="' .$ff['placeholder'] . '" ' . $ff['required_attribute'] . ' ' . ( !empty($ff['autocomplete']) ? $ff['autocomplete'] : '' ) . ' />'; |
| 881 |
} |
| 882 |
|
| 883 |
/* textarea */ |
| 884 |
if($ff['type']=='textarea'){ |
| 885 |
$formfields[$key]['field'] .= '<label for="'. esc_html($key) .'"' . $ff['required_class'] . '>'; |
| 886 |
$formfields[$key]['field'] .= '' . $ff['label'] . ''; |
| 887 |
$formfields[$key]['field'] .= '</label>'; |
| 888 |
$formfields[$key]['field'] .= '<textarea id="'. esc_html($key) .'" name="'. $key.'" placeholder="' .$ff['placeholder'] . '" ' . $ff['required_attribute'] . ' ' . (!empty($ff['autocomplete']) ? $ff['autocomplete'] : '') . '>' . $ff['value'] . '</textarea>'; |
| 889 |
} |
| 890 |
|
| 891 |
/* checkbox - with label _after_ input*/ |
| 892 |
if($ff['type']=='checkbox'){ |
| 893 |
$formfields[$key]['field'] .= '<label for="'. esc_html($key) .'"' . $ff['required_class'] . ' title="'.esc_attr($ff['placeholder']).'">'; |
| 894 |
$formfields[$key]['field'] .= '<input id="'. esc_html($key) .'" name="'. $key.'" type="checkbox" value="1" ' . $ff['required_attribute'] . ' '.checked($ff['value'], true, false).'/>'; |
| 895 |
$formfields[$key]['field'] .= '' . $ff['label'] . ''; |
| 896 |
$formfields[$key]['field'] .= '</label>'; |
| 897 |
} |
| 898 |
|
| 899 |
/* multicheckbox */ |
| 900 |
if($ff['type']=='multicheckbox'){ |
| 901 |
// convert comma separated value back to (trimmed) array |
| 902 |
$val_as_array = array_map('trim', explode(',' , $ff['value']) ); |
| 903 |
|
| 904 |
$formfields[$key]['field'] .= '<label for="'. esc_html($key) .'"' . $ff['required_class'] . '>'; |
| 905 |
$formfields[$key]['field'] .= '' . $ff['label'] . ''; |
| 906 |
$formfields[$key]['field'] .= '</label>'; |
| 907 |
$formfields[$key]['field'] .= '<div class="'.WPPIZZA_PREFIX.'-multicheckbox" title="'.esc_attr($ff['placeholder']).'">'; |
| 908 |
foreach($ff['options'] as $k => $option){ |
| 909 |
//Note: Do not add the $k to the [] in the name or validation will not work when set to required |
| 910 |
$formfields[$key]['field'] .= '<label><input id="'. esc_html($key .'_'.$k).'" value="'. $k .'" name="'. $key.'[]" type="checkbox" ' . $ff['required_attribute'] . ' '.checked( ( !empty($val_as_array) && in_array($k, $val_as_array)) ,true, false).'/>'.$option.' </label>'; |
| 911 |
} |
| 912 |
$formfields[$key]['field'] .= '</div>'; |
| 913 |
} |
| 914 |
|
| 915 |
/* radio */ |
| 916 |
if($ff['type']=='radio'){ |
| 917 |
$formfields[$key]['field'] .= '<label for="'. esc_html($key) .'"' . $ff['required_class'] . '>'; |
| 918 |
$formfields[$key]['field'] .= '' . $ff['label'] . ''; |
| 919 |
$formfields[$key]['field'] .= '</label>'; |
| 920 |
$formfields[$key]['field'] .= '<div class="'.WPPIZZA_PREFIX.'-radio" title="'.esc_attr($ff['placeholder']).'">'; |
| 921 |
foreach($ff['options'] as $k => $option){ |
| 922 |
$formfields[$key]['field'] .= '<label><input id="'. esc_html($key .'_'.$k).'" value="'. $k .'" name="'. $key.'" type="radio" ' . $ff['required_attribute'] . ' '.checked($ff['value'], $k, false).'/>'.$option.' </label>'; |
| 923 |
} |
| 924 |
$formfields[$key]['field'] .= '</div>'; |
| 925 |
} |
| 926 |
|
| 927 |
/* select */ |
| 928 |
if($ff['type']=='select'){ |
| 929 |
$formfields[$key]['field'] .= '<label for="'. esc_html($key) .'"' . $ff['required_class'] . '>'; |
| 930 |
$formfields[$key]['field'] .= '' . $ff['label'] . ''; |
| 931 |
$formfields[$key]['field'] .= '</label>'; |
| 932 |
$formfields[$key]['field'] .= '<select id="'. esc_html($key) .'" name="'. $key.'" title="'.esc_attr($ff['placeholder']).'" ' . $ff['required_attribute'] . ' >'; |
| 933 |
foreach($ff['options'] as $oKey => $option){ |
| 934 |
/* account for placeholder separately*/ |
| 935 |
if($oKey == -1){ |
| 936 |
$formfields[$key]['field'] .= '<option value="">'. $option['label'] .'</option>'; |
| 937 |
}else{ |
| 938 |
$set = wppizza_decode_entities_trim($ff['value']); |
| 939 |
$match = wppizza_decode_entities_trim($oKey); |
| 940 |
|
| 941 |
$formfields[$key]['field'] .= '<option value="'. $oKey .'" '.selected($set, $match, false).'>'. $option['label'] .'</option>'; |
| 942 |
} |
| 943 |
} |
| 944 |
$formfields[$key]['field'] .= '</select>'; |
| 945 |
} |
| 946 |
|
| 947 |
/* hidden, just add hidden field */ |
| 948 |
if($ff['type']=='hidden'){ |
| 949 |
$formfields[$key]['field'] .= '<input id="'. esc_html($key) .'" name="'. $key.'" type="hidden" value="' . $ff['value'] . '" />'; |
| 950 |
} |
| 951 |
|
| 952 |
/* html, adding some custom html after it all - unused in plugin itself , but might come in handy for 3rd party plugins*/ |
| 953 |
/* to additionally bypass the above too and just have the html, set 'type' to something not used above (like html for example) */ |
| 954 |
if(!empty($ff['html'])){ |
| 955 |
$formfields[$key]['field'] .= $ff['html']; |
| 956 |
} |
| 957 |
} |
| 958 |
} |
| 959 |
|
| 960 |
/* |
| 961 |
filter formfields if required (type will be orderpage or confirmation page) |
| 962 |
*/ |
| 963 |
$formfields = apply_filters('wppizza_filter_'.$type.'_formfields', $formfields, $order_formatted); |
| 964 |
|
| 965 |
|
| 966 |
/************************************* |
| 967 |
|
| 968 |
markup |
| 969 |
|
| 970 |
*************************************/ |
| 971 |
/* |
| 972 |
ini array |
| 973 |
*/ |
| 974 |
$markup = array(); |
| 975 |
/* |
| 976 |
get markup |
| 977 |
*/ |
| 978 |
if(file_exists( WPPIZZA_TEMPLATE_DIR . '/markup/global/formfields.inputs.php')){ |
| 979 |
require(WPPIZZA_TEMPLATE_DIR.'/markup/global/formfields.inputs.php'); |
| 980 |
}else{ |
| 981 |
require(WPPIZZA_PATH.'templates/markup/global/formfields.inputs.php'); |
| 982 |
} |
| 983 |
/* |
| 984 |
apply filter if required and implode for output |
| 985 |
*/ |
| 986 |
$markup = apply_filters('wppizza_filter_formfields_inputs', $markup, $formfields); |
| 987 |
$markup = implode('', $markup); |
| 988 |
|
| 989 |
|
| 990 |
return $markup; |
| 991 |
} |
| 992 |
|
| 993 |
/****************************** |
| 994 |
fieldset markup userdata |
| 995 |
labels and values only , no inputs |
| 996 |
******************************/ |
| 997 |
function formfields_values($ffs, $page){ |
| 998 |
|
| 999 |
/********* |
| 1000 |
get formfield keys native to this plugin to distinguish between |
| 1001 |
own wppizza fields or fields that have been added by filters |
| 1002 |
(backwards compatibility) |
| 1003 |
*********/ |
| 1004 |
$native_formfields = WPPIZZA() -> helpers -> native_formfields(); |
| 1005 |
|
| 1006 |
|
| 1007 |
$formfields =array(); |
| 1008 |
if(!empty($ffs)){ |
| 1009 |
foreach($ffs as $key=>$ff){ |
| 1010 |
|
| 1011 |
/* |
| 1012 |
if omit_if_optional is set, |
| 1013 |
omit formfield if field is not required to be filled in |
| 1014 |
the 'required_attribute' willalready be set (or not as the case may be) |
| 1015 |
depending on if it's pickup or delivery and required or not for those |
| 1016 |
*/ |
| 1017 |
if(!empty($ff['omit_if_optional']) && empty($ff['required_attribute'])){ |
| 1018 |
continue; |
| 1019 |
} |
| 1020 |
|
| 1021 |
|
| 1022 |
|
| 1023 |
/* |
| 1024 |
allow filtering of formfield values (label for example) - passing on page parameters too |
| 1025 |
*/ |
| 1026 |
$ff = apply_filters('wppizza_filter_formfields_values_'.$key.'', $ff, $page); |
| 1027 |
|
| 1028 |
/* |
| 1029 |
ini array |
| 1030 |
*/ |
| 1031 |
$formfields[$key] = array(); |
| 1032 |
|
| 1033 |
/* |
| 1034 |
set class |
| 1035 |
*/ |
| 1036 |
$formfields[$key]['class'] = ''.WPPIZZA_PREFIX.'-'.$key.''; |
| 1037 |
|
| 1038 |
/* |
| 1039 |
set label |
| 1040 |
*/ |
| 1041 |
$formfields[$key]['label'] = $ff['label']; |
| 1042 |
|
| 1043 |
/* |
| 1044 |
set value - if its coming from a select/dropdown/radio, map the id back to the set label/value |
| 1045 |
*/ |
| 1046 |
|
| 1047 |
//selects |
| 1048 |
if($ff['type'] == 'select'){ |
| 1049 |
/* |
| 1050 |
for backwards compatibility with other/older plugins |
| 1051 |
|
| 1052 |
adding 'indexed' = true to the ff as key denotes that we are using numeric indexes |
| 1053 |
as dropdown values associated to the displayed text string - effectively making |
| 1054 |
it behave like any select formfield native to the wppizza plugin before adding additional |
| 1055 |
fields via filters |
| 1056 |
*/ |
| 1057 |
if(!isset($native_formfields[$key]) && empty($ff['indexed'])){ |
| 1058 |
$formfields[$key]['value'] = $ff['value'] ; |
| 1059 |
}else{ |
| 1060 |
$formfields[$key]['value'] = isset($ff['options'][$ff['value']]['label']) ? $ff['options'][$ff['value']]['label'] : '' ; |
| 1061 |
} |
| 1062 |
} |
| 1063 |
|
| 1064 |
//radios |
| 1065 |
elseif($ff['type'] == 'radio' ){ |
| 1066 |
$formfields[$key]['value'] = isset($ff['options'][$ff['value']]) ? $ff['options'][$ff['value']] : '' ; |
| 1067 |
} |
| 1068 |
|
| 1069 |
//multicheckboxes |
| 1070 |
elseif($ff['type'] == 'multicheckbox' ){ |
| 1071 |
|
| 1072 |
|
| 1073 |
$output = '';//init empty |
| 1074 |
|
| 1075 |
//intersect and implode if something was selected |
| 1076 |
if($ff['value'] != ''){ |
| 1077 |
$selected = array_flip(wppizza_strtoarray($ff['value'])); |
| 1078 |
$intersect = array_intersect_key($ff['options'], $selected); |
| 1079 |
$output = implode(', ',$intersect); |
| 1080 |
} |
| 1081 |
$formfields[$key]['value'] = $output ; |
| 1082 |
} |
| 1083 |
|
| 1084 |
//all other non dropdown / non radio / non multicheckboxes |
| 1085 |
else{ |
| 1086 |
$formfields[$key]['value'] = is_array($ff['value']) ? implode(', ',$ff['value']) : $ff['value'] ; |
| 1087 |
} |
| 1088 |
|
| 1089 |
|
| 1090 |
}} |
| 1091 |
|
| 1092 |
/************************************* |
| 1093 |
|
| 1094 |
markup |
| 1095 |
|
| 1096 |
*************************************/ |
| 1097 |
/* |
| 1098 |
ini array |
| 1099 |
*/ |
| 1100 |
$markup = array(); |
| 1101 |
/* |
| 1102 |
get markup |
| 1103 |
*/ |
| 1104 |
if(file_exists( WPPIZZA_TEMPLATE_DIR . '/markup/global/formfields.values.php')){ |
| 1105 |
require(WPPIZZA_TEMPLATE_DIR.'/markup/global/formfields.values.php'); |
| 1106 |
}else{ |
| 1107 |
require(WPPIZZA_PATH.'templates/markup/global/formfields.values.php'); |
| 1108 |
} |
| 1109 |
/* |
| 1110 |
apply filter if required and implode for output |
| 1111 |
*/ |
| 1112 |
$markup = apply_filters('wppizza_filter_formfields_values', $markup, $formfields, $page); |
| 1113 |
$markup = implode('', $markup); |
| 1114 |
|
| 1115 |
|
| 1116 |
|
| 1117 |
return $markup; |
| 1118 |
|
| 1119 |
} |
| 1120 |
|
| 1121 |
/******************************************************************************************** |
| 1122 |
get user meta data (if logged in) |
| 1123 |
used in self and markup.pages |
| 1124 |
return array or false |
| 1125 |
********************************************************************************************/ |
| 1126 |
function user_meta($user_id = false, $single_key = ''){ |
| 1127 |
|
| 1128 |
/** userid set */ |
| 1129 |
if($user_id){ |
| 1130 |
|
| 1131 |
$user_metadata=get_user_meta( $user_id, $single_key, true); |
| 1132 |
|
| 1133 |
/* returning single value only */ |
| 1134 |
if(!empty($single_key)){ |
| 1135 |
return $user_metadata; |
| 1136 |
} |
| 1137 |
|
| 1138 |
/* return as single dimension array */ |
| 1139 |
$metadata = array(); |
| 1140 |
foreach($user_metadata as $meta_key=>$meta_value){ |
| 1141 |
$metadata[$meta_key] = $meta_value[0]; |
| 1142 |
} |
| 1143 |
|
| 1144 |
/* |
| 1145 |
selectively override cemail with registered email address |
| 1146 |
*/ |
| 1147 |
$registered_user_data = get_userdata($user_id); |
| 1148 |
$registered_user_email = $registered_user_data -> user_email; |
| 1149 |
//add/override cemail with user account registered email unless it exists as distinct meta data |
| 1150 |
$metadata[WPPIZZA_SLUG.'_cemail'] = !empty($metadata[WPPIZZA_SLUG.'_cemail']) ? $metadata[WPPIZZA_SLUG.'_cemail'] : $registered_user_email ; |
| 1151 |
|
| 1152 |
|
| 1153 |
return $metadata; |
| 1154 |
} |
| 1155 |
|
| 1156 |
/** user logged in, get id and metadata from there */ |
| 1157 |
if(is_user_logged_in()){ |
| 1158 |
$user_id = get_current_user_id(); |
| 1159 |
$user_metadata = get_user_meta( $user_id, $single_key, true); |
| 1160 |
|
| 1161 |
/* returning single value only */ |
| 1162 |
if(!empty($single_key)){ |
| 1163 |
return $user_metadata; |
| 1164 |
} |
| 1165 |
|
| 1166 |
/* return as single dimension array */ |
| 1167 |
$metadata = array(); |
| 1168 |
foreach($user_metadata as $meta_key=>$meta_value){ |
| 1169 |
$metadata[$meta_key] = $meta_value[0]; |
| 1170 |
} |
| 1171 |
|
| 1172 |
/* |
| 1173 |
selectively override cemail with registered email address |
| 1174 |
*/ |
| 1175 |
$registered_user_data = get_userdata($user_id); |
| 1176 |
$registered_user_email = $registered_user_data -> user_email; |
| 1177 |
//add/override cemail with user account registered email unless it exists as distinct meta data |
| 1178 |
$metadata[WPPIZZA_SLUG.'_cemail'] = !empty($metadata[WPPIZZA_SLUG.'_cemail']) ? $metadata[WPPIZZA_SLUG.'_cemail'] : $registered_user_email ; |
| 1179 |
|
| 1180 |
return $metadata; |
| 1181 |
} |
| 1182 |
|
| 1183 |
|
| 1184 |
/** no user id and not logged in */ |
| 1185 |
if(!$user_id && !is_user_logged_in()){ |
| 1186 |
$user_metadata = false; |
| 1187 |
return $user_metadata; |
| 1188 |
} |
| 1189 |
|
| 1190 |
} |
| 1191 |
|
| 1192 |
/******************************************************************************************** |
| 1193 |
Due to legacy reasons, added meta data by the plugin to the user profiles are stored |
| 1194 |
as the actual values (instead of indexes) for selects, radios, checkboxes |
| 1195 |
since v3.16 however the form data on the checkout now uses indexes (i.e numbers) as values |
| 1196 |
that are being submitted for thios input types. To prefill the checkout form - if and when required - |
| 1197 |
we need to map these values stored in th eprofile back to their indexes |
| 1198 |
|
| 1199 |
@param array |
| 1200 |
@param bool (to revert to values when choosing to update profile from orderpage) |
| 1201 |
@return array |
| 1202 |
@since 3.16 |
| 1203 |
********************************************************************************************/ |
| 1204 |
function map_user_meta($values = array()){ |
| 1205 |
|
| 1206 |
/* |
| 1207 |
keep the original only overwriting as required below |
| 1208 |
*/ |
| 1209 |
$mapped = $values; |
| 1210 |
|
| 1211 |
/** |
| 1212 |
enabled form fields |
| 1213 |
**/ |
| 1214 |
$formfields = WPPIZZA() -> helpers -> enabled_formfields(); |
| 1215 |
|
| 1216 |
|
| 1217 |
/** |
| 1218 |
loop trough form fields enabled for registration |
| 1219 |
**/ |
| 1220 |
foreach( $formfields as $ffKey => $field ) { |
| 1221 |
|
| 1222 |
|
| 1223 |
/************* |
| 1224 |
key ident |
| 1225 |
*************/ |
| 1226 |
$keyId = WPPIZZA_SLUG.'_'.$ffKey; |
| 1227 |
|
| 1228 |
/** |
| 1229 |
only listen deal with fields that were enabled to be used for registration |
| 1230 |
the value for this key is not empty |
| 1231 |
and there is some index in the values of the formfield to start off with |
| 1232 |
**/ |
| 1233 |
if(!empty($field['onregister']) && isset($values[$keyId]) && !empty( array_filter($field['value']))) { |
| 1234 |
|
| 1235 |
/* |
| 1236 |
current value as stored in meta data |
| 1237 |
skip if empty (but allow for 0 !) |
| 1238 |
*/ |
| 1239 |
$metaValue = $values[$keyId]; |
| 1240 |
if($metaValue === null || $metaValue == '' || $metaValue === false ){ |
| 1241 |
break; |
| 1242 |
} |
| 1243 |
|
| 1244 |
|
| 1245 |
/* |
| 1246 |
map if necessary |
| 1247 |
*/ |
| 1248 |
if(in_array($field['type'], array('select', 'radio', 'multicheckbox'))){ |
| 1249 |
|
| 1250 |
/* |
| 1251 |
select / radio |
| 1252 |
convert saved string to associated numeric index |
| 1253 |
*/ |
| 1254 |
if(in_array($field['type'], array('select', 'radio'))){ |
| 1255 |
|
| 1256 |
//decode all entities and trim for more reliable comparison |
| 1257 |
$savedValue = wppizza_decode_entities_trim($metaValue); |
| 1258 |
|
| 1259 |
foreach($field['value'] as $idx => $val){ |
| 1260 |
|
| 1261 |
if($val === null || $val == '' || $val === false ){ |
| 1262 |
break ; |
| 1263 |
} |
| 1264 |
|
| 1265 |
|
| 1266 |
//decode all entities and trim for more reliable comparison |
| 1267 |
$setValue = wppizza_decode_entities_trim($val); |
| 1268 |
|
| 1269 |
//found the index |
| 1270 |
if($savedValue == $setValue){ |
| 1271 |
|
| 1272 |
//map the index |
| 1273 |
$mapped[$keyId] = $idx; |
| 1274 |
|
| 1275 |
break ; |
| 1276 |
} |
| 1277 |
|
| 1278 |
} |
| 1279 |
|
| 1280 |
} |
| 1281 |
|
| 1282 |
/* |
| 1283 |
select / radio |
| 1284 |
convert saved string to associated numeric index |
| 1285 |
*/ |
| 1286 |
if(in_array($field['type'], array('multicheckbox'))){ |
| 1287 |
|
| 1288 |
//should be saved as a serialised array |
| 1289 |
$savedValue = maybe_unserialize($metaValue); |
| 1290 |
if(!is_array($savedValue)){ |
| 1291 |
break ; |
| 1292 |
} |
| 1293 |
//comma separated indexes |
| 1294 |
$mapped[$keyId] = implode(',',array_keys($savedValue)); |
| 1295 |
|
| 1296 |
} |
| 1297 |
} |
| 1298 |
} |
| 1299 |
} |
| 1300 |
|
| 1301 |
return $mapped; |
| 1302 |
} |
| 1303 |
|
| 1304 |
} |
| 1305 |
?> |