data
5 years ago
GlobalFunctions.php
4 years ago
PPressBFnote.php
4 years ago
custom-settings-api.php
4 years ago
GlobalFunctions.php
1457 lines
| 1 | <?php |
| 2 | |
| 3 | use ProfilePress\Core\Admin\SettingsPages\MailOptin; |
| 4 | use ProfilePress\Core\Base; |
| 5 | use ProfilePress\Core\Classes\ExtensionManager as EM; |
| 6 | use ProfilePress\Core\Classes\FormRepository as FR; |
| 7 | use ProfilePress\Core\Classes\PROFILEPRESS_sql as PROFILEPRESS_sql; |
| 8 | use ProfilePress\Core\Classes\SendEmail; |
| 9 | use ProfilePress\Core\ShortcodeParser\Builder\FieldsShortcodeCallback; |
| 10 | |
| 11 | /** Plugin DB settings data */ |
| 12 | function ppress_db_data() |
| 13 | { |
| 14 | return get_option(PPRESS_SETTINGS_DB_OPTION_NAME, []); |
| 15 | } |
| 16 | |
| 17 | function ppress_update_settings($key, $value) |
| 18 | { |
| 19 | $data = ppress_db_data(); |
| 20 | $data[$key] = $value; |
| 21 | update_option(PPRESS_SETTINGS_DB_OPTION_NAME, $data); |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * Array of WooCommerce billing fields. |
| 26 | * |
| 27 | * @return array |
| 28 | */ |
| 29 | function ppress_woocommerce_billing_fields() |
| 30 | { |
| 31 | return array( |
| 32 | 'billing_first_name', |
| 33 | 'billing_last_name', |
| 34 | 'billing_company', |
| 35 | 'billing_address_1', |
| 36 | 'billing_address_2', |
| 37 | 'billing_city', |
| 38 | 'billing_postcode', |
| 39 | 'billing_country', |
| 40 | 'billing_state', |
| 41 | 'billing_phone', |
| 42 | 'billing_email' |
| 43 | ); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Array of WooCommerce billing fields. |
| 48 | * |
| 49 | * @return array |
| 50 | */ |
| 51 | function ppress_woocommerce_shipping_fields() |
| 52 | { |
| 53 | return array( |
| 54 | 'shipping_first_name', |
| 55 | 'shipping_last_name', |
| 56 | 'shipping_company', |
| 57 | 'shipping_address_1', |
| 58 | 'shipping_address_2', |
| 59 | 'shipping_city', |
| 60 | 'shipping_postcode', |
| 61 | 'shipping_country', |
| 62 | 'shipping_state' |
| 63 | ); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Array of WooCommerce billing and shipping fields. |
| 68 | * |
| 69 | * @return array |
| 70 | */ |
| 71 | function ppress_woocommerce_billing_shipping_fields() |
| 72 | { |
| 73 | return array_merge(ppress_woocommerce_billing_fields(), ppress_woocommerce_shipping_fields()); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * @param string $key |
| 78 | * @param bool $default |
| 79 | * @param bool $is_empty set to true to return the default if value is empty |
| 80 | * |
| 81 | * @return mixed |
| 82 | */ |
| 83 | function ppress_settings_by_key($key = '', $default = false, $is_empty = false) |
| 84 | { |
| 85 | $cache_key = 'ppress_settings_db_data'; |
| 86 | |
| 87 | $data = wp_cache_get($cache_key); |
| 88 | |
| 89 | if (false === $data) { |
| 90 | $data = ppress_db_data(); |
| 91 | wp_cache_set($key, $data); |
| 92 | } |
| 93 | |
| 94 | if ($is_empty === true) { |
| 95 | return ! empty($data[$key]) ? $data[$key] : $default; |
| 96 | } |
| 97 | |
| 98 | return isset($data[$key]) ? $data[$key] : $default; |
| 99 | } |
| 100 | |
| 101 | function ppress_get_setting($key = '', $default = false, $is_empty = false) |
| 102 | { |
| 103 | return ppress_settings_by_key($key, $default, $is_empty); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Send email. |
| 108 | * |
| 109 | * @param string|array $to |
| 110 | * @param $subject |
| 111 | * @param $message |
| 112 | * |
| 113 | * @return bool|WP_Error |
| 114 | */ |
| 115 | function ppress_send_email($to, $subject, $message) |
| 116 | { |
| 117 | return (new SendEmail($to, $subject, $message))->send(); |
| 118 | } |
| 119 | |
| 120 | function ppress_welcome_msg_content_default() |
| 121 | { |
| 122 | return <<<HTML |
| 123 | <h1>Welcome {{first_name}}!</h1> |
| 124 | <p>We are so happy to have you. Below is your login credential:</p> |
| 125 | <p>Username: {{username}}</p> |
| 126 | <p>Password: the password you registered with.</p> |
| 127 | |
| 128 | <div style="padding: 10px 0 50px 0; text-align: center;"> |
| 129 | <a style="background: #555555; color: #fff; padding: 12px 30px; text-decoration: none; border-radius: 3px; letter-spacing: 0.3px;" href="{{login_link}}">Click here to login</a> |
| 130 | </div> |
| 131 | <p>If you have any problems, do not hesitate to contact us.</p> |
| 132 | HTML; |
| 133 | } |
| 134 | |
| 135 | function ppress_new_user_admin_notification_message_default() |
| 136 | { |
| 137 | return <<<HTML |
| 138 | <p>New user registration on your site {{site_title}}.</p> |
| 139 | <p>Username: {{username}}</p> |
| 140 | <p>Email address: {{user_email}}</p> |
| 141 | HTML; |
| 142 | } |
| 143 | |
| 144 | function ppress_passwordless_login_message_default() |
| 145 | { |
| 146 | return <<<MESSAGE |
| 147 | <p>Hi {{username}}, we have generated a one-time login link for you.</p> |
| 148 | <div style="padding: 10px 0 50px 0; text-align: center;"> |
| 149 | <a style="background: #555555; color: #fff; padding: 12px 30px; text-decoration: none; border-radius: 3px; letter-spacing: 0.3px;" href="{{passwordless_link}}">Click here to login</a> |
| 150 | </div> |
| 151 | MESSAGE; |
| 152 | } |
| 153 | |
| 154 | function ppress_user_moderation_msg_default($type) |
| 155 | { |
| 156 | $pending = <<<MESSAGE |
| 157 | <p>Hi {{first_name}} {{last_name}}, your account is pending approval.</p> |
| 158 | <p>You will receive an email once your account is approved.</p> |
| 159 | <p>Regards.</p> |
| 160 | MESSAGE; |
| 161 | |
| 162 | $approved = <<<MESSAGE |
| 163 | <p>Hi {{first_name}} {{last_name}}, your account has been approved.</p> |
| 164 | |
| 165 | <p>Regards.</p> |
| 166 | MESSAGE; |
| 167 | |
| 168 | $rejected = <<<MESSAGE |
| 169 | <p>Hi {{first_name}} {{last_name}}, your account has been rejected.</p> |
| 170 | |
| 171 | <p>Regards.</p> |
| 172 | MESSAGE; |
| 173 | |
| 174 | $blocked = <<<MESSAGE |
| 175 | <p>Hi {{first_name}} {{last_name}}, your account has been blocked.</p> |
| 176 | |
| 177 | <p>Regards.</p> |
| 178 | MESSAGE; |
| 179 | |
| 180 | $unblocked = <<<MESSAGE |
| 181 | <p>Hi {{first_name}} {{last_name}}, your account with username "{{username}}" has been unblocked.</p> |
| 182 | |
| 183 | <p>Regards.</p> |
| 184 | MESSAGE; |
| 185 | |
| 186 | $admin_notification = <<<MESSAGE |
| 187 | <p>A new user is awaiting your approval on your site.</p> |
| 188 | <p>Username: {{username}}</p> |
| 189 | <p>E-mail: {{email}}</p> |
| 190 | <p>Click to approve: {{approval_url}}</p> |
| 191 | <p>Click to reject: {{rejection_url}}</p> |
| 192 | MESSAGE; |
| 193 | |
| 194 | return ${$type}; |
| 195 | } |
| 196 | |
| 197 | function ppress_password_reset_content_default() |
| 198 | { |
| 199 | return <<<HTML |
| 200 | <p>Someone requested to reset the password for the following account:</p> |
| 201 | <p>Username: {{username}}</p> |
| 202 | <p>If this was a mistake, just ignore this email and nothing will happen.</p> |
| 203 | <p>To reset your password, click the button below.</p> |
| 204 | <div style="padding: 10px 0 50px 0; text-align: center;"> |
| 205 | <a style="background: #555555; color: #fff; padding: 12px 30px; text-decoration: none; border-radius: 3px; letter-spacing: 0.3px;" href="{{password_reset_link}}"> Reset Password</a> |
| 206 | </div> |
| 207 | HTML; |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Return the url to redirect to after login authentication |
| 212 | * |
| 213 | * @return bool|string |
| 214 | */ |
| 215 | function ppress_login_redirect() |
| 216 | { |
| 217 | if ( ! empty($_REQUEST['redirect_to'])) { |
| 218 | $redirect = rawurldecode($_REQUEST['redirect_to']); |
| 219 | } else { |
| 220 | $login_redirect = ppress_get_setting('set_login_redirect'); |
| 221 | $custom_url_login_redirect = ppress_get_setting('custom_url_login_redirect'); |
| 222 | $referrer_url = ppress_var($_POST, 'login_referrer_page', ppress_var($_POST, 'signup_referrer_page')); |
| 223 | |
| 224 | if ( ! empty($custom_url_login_redirect)) { |
| 225 | $redirect = $custom_url_login_redirect; |
| 226 | } elseif ($login_redirect == 'dashboard') { |
| 227 | $redirect = network_site_url('/wp-admin'); |
| 228 | } elseif ($login_redirect == 'previous_page' && ! empty($referrer_url)) { |
| 229 | $redirect = $referrer_url; |
| 230 | } elseif ('current_page' == $login_redirect) { |
| 231 | // in ajax mode, pp_current_url is set so we can do client-side redirection to current page after login. |
| 232 | // no way to get current url in social login hence, look it up from $_GET['pp_current_url'] |
| 233 | if ( ! empty($_GET['pp_current_url'])) { |
| 234 | $redirect = rawurldecode($_GET['pp_current_url']); |
| 235 | } elseif ( ! empty($_POST['pp_current_url'])) { |
| 236 | $redirect = rawurldecode($_POST['pp_current_url']); |
| 237 | } else { |
| 238 | $redirect = ppress_get_current_url_raw(); |
| 239 | } |
| 240 | } elseif ( ! empty($login_redirect) && is_numeric($login_redirect)) { |
| 241 | $redirect = get_permalink($login_redirect); |
| 242 | } else { |
| 243 | $redirect = network_site_url('/wp-admin'); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | return wp_validate_redirect($redirect); |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Return the url to redirect to after successful reset / change of password. |
| 252 | * |
| 253 | * @return bool|string |
| 254 | */ |
| 255 | function ppress_password_reset_redirect() |
| 256 | { |
| 257 | $password_reset_redirect = ppress_get_setting('set_password_reset_redirect'); |
| 258 | $custom_url_password_reset_redirect = ppress_get_setting('custom_url_password_reset_redirect'); |
| 259 | |
| 260 | if ( ! empty($custom_url_password_reset_redirect)) { |
| 261 | $redirect = $custom_url_password_reset_redirect; |
| 262 | } elseif ( ! empty($password_reset_redirect)) { |
| 263 | $redirect = get_permalink($password_reset_redirect); |
| 264 | if ($password_reset_redirect == 'no_redirect') { |
| 265 | $redirect = ppress_password_reset_url() . '?password=changed'; |
| 266 | } |
| 267 | } else { |
| 268 | $redirect = ppress_password_reset_url() . '?password=changed'; |
| 269 | } |
| 270 | |
| 271 | return apply_filters('ppress_do_password_reset_redirect', esc_url_raw($redirect)); |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * Return the url to frontend myprofile page. |
| 276 | * |
| 277 | * @return bool|string |
| 278 | */ |
| 279 | function ppress_profile_url() |
| 280 | { |
| 281 | $url = admin_url('profile.php'); |
| 282 | |
| 283 | $page_id = ppress_get_setting('set_user_profile_shortcode'); |
| 284 | |
| 285 | if ( ! empty($page_id)) { |
| 286 | $url = get_permalink($page_id); |
| 287 | } |
| 288 | |
| 289 | return apply_filters('ppress_profile_url', $url); |
| 290 | } |
| 291 | |
| 292 | function ppress_get_frontend_profile_url($username_or_id) |
| 293 | { |
| 294 | if (is_numeric($username_or_id)) { |
| 295 | $username_or_id = ppress_get_username_by_id($username_or_id); |
| 296 | } |
| 297 | |
| 298 | return home_url(ppress_get_profile_slug() . '/' . rawurlencode($username_or_id)); |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * Return ProfilePress edit profile page URL or WP default profile URL as fallback |
| 303 | * |
| 304 | * @return bool|string |
| 305 | */ |
| 306 | function ppress_edit_profile_url() |
| 307 | { |
| 308 | return apply_filters('ppress_edit_profile_url', ppress_my_account_url()); |
| 309 | } |
| 310 | |
| 311 | function ppress_my_account_url() |
| 312 | { |
| 313 | $url = get_edit_profile_url(); |
| 314 | |
| 315 | $page_id = ppress_settings_by_key('edit_user_profile_url'); |
| 316 | |
| 317 | if ( ! empty($page_id) && get_post_status($page_id)) { |
| 318 | $url = get_permalink($page_id); |
| 319 | } |
| 320 | |
| 321 | return apply_filters('ppress_my_account_url', $url); |
| 322 | } |
| 323 | |
| 324 | /** |
| 325 | * Return ProfilePress password reset url. |
| 326 | * |
| 327 | * @return string |
| 328 | */ |
| 329 | function ppress_password_reset_url() |
| 330 | { |
| 331 | $url = wp_lostpassword_url(); |
| 332 | |
| 333 | $page_id = ppress_get_setting('set_lost_password_url'); |
| 334 | |
| 335 | if ( ! empty($page_id) && get_post_status($page_id)) { |
| 336 | $url = get_permalink($page_id); |
| 337 | } |
| 338 | |
| 339 | return apply_filters('ppress_password_reset_url', $url); |
| 340 | } |
| 341 | |
| 342 | |
| 343 | /** |
| 344 | * Get ProfilePress login page URL or WP default login url if it isn't set. |
| 345 | * |
| 346 | * @param $redirect |
| 347 | * |
| 348 | * @return string |
| 349 | */ |
| 350 | function ppress_login_url($redirect = '') |
| 351 | { |
| 352 | $login_url = wp_login_url(); |
| 353 | |
| 354 | $login_page_id = ppress_get_setting('set_login_url'); |
| 355 | |
| 356 | if ( ! empty($login_page_id) && get_post_status($login_page_id)) { |
| 357 | $login_url = get_permalink($login_page_id); |
| 358 | } |
| 359 | |
| 360 | if ( ! empty($redirect)) { |
| 361 | $login_url = add_query_arg('redirect_to', rawurlencode(wp_validate_redirect($redirect)), $login_url); |
| 362 | } |
| 363 | |
| 364 | return apply_filters('ppress_login_url', $login_url); |
| 365 | } |
| 366 | |
| 367 | /** |
| 368 | * Get ProfilePress login page URL or WP default login url if it isn't set. |
| 369 | */ |
| 370 | function ppress_registration_url() |
| 371 | { |
| 372 | $reg_url = wp_registration_url(); |
| 373 | |
| 374 | $page_id = ppress_get_setting('set_registration_url'); |
| 375 | |
| 376 | if ( ! empty($page_id) && get_post_status($page_id)) { |
| 377 | $reg_url = get_permalink($page_id); |
| 378 | } |
| 379 | |
| 380 | return apply_filters('ppress_registration_url', $reg_url); |
| 381 | } |
| 382 | |
| 383 | /** |
| 384 | * Return the URL of the currently view page. |
| 385 | * |
| 386 | * @return string |
| 387 | */ |
| 388 | function ppress_get_current_url() |
| 389 | { |
| 390 | global $wp; |
| 391 | |
| 392 | return home_url(add_query_arg(array(), $wp->request)); |
| 393 | } |
| 394 | |
| 395 | |
| 396 | /** |
| 397 | * Return currently viewed page url without query string. |
| 398 | * |
| 399 | * @return string |
| 400 | */ |
| 401 | function ppress_get_current_url_raw() |
| 402 | { |
| 403 | $protocol = 'http://'; |
| 404 | |
| 405 | if ((isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] == 1)) |
| 406 | || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') |
| 407 | ) { |
| 408 | $protocol = 'https://'; |
| 409 | } |
| 410 | |
| 411 | return esc_url_raw($protocol . $_SERVER['HTTP_HOST'] . parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)); |
| 412 | } |
| 413 | |
| 414 | /** |
| 415 | * Return currently viewed page url with query string. |
| 416 | * |
| 417 | * @return string |
| 418 | */ |
| 419 | function ppress_get_current_url_query_string() |
| 420 | { |
| 421 | $protocol = 'http://'; |
| 422 | |
| 423 | if ((isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] == 1)) |
| 424 | || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') |
| 425 | ) { |
| 426 | $protocol = 'https://'; |
| 427 | } |
| 428 | |
| 429 | $url = $protocol . $_SERVER['HTTP_HOST']; |
| 430 | |
| 431 | $url .= $_SERVER['REQUEST_URI']; |
| 432 | |
| 433 | return esc_url_raw($url); |
| 434 | } |
| 435 | |
| 436 | |
| 437 | /** |
| 438 | * @return string blog URL without scheme |
| 439 | */ |
| 440 | function ppress_site_url_without_scheme() |
| 441 | { |
| 442 | $parsed_url = parse_url(home_url()); |
| 443 | |
| 444 | return $parsed_url['host']; |
| 445 | } |
| 446 | |
| 447 | /** |
| 448 | * Append an option to a select dropdown |
| 449 | * |
| 450 | * @param string $option option to add |
| 451 | * @param string $select select dropdown |
| 452 | * |
| 453 | * @return string |
| 454 | */ |
| 455 | function ppress_append_option_to_select($option, $select) |
| 456 | { |
| 457 | $regex = "/<select ([^<]*)>/"; |
| 458 | |
| 459 | preg_match($regex, $select, $matches); |
| 460 | $select_attr = ppress_var($matches, 1); |
| 461 | |
| 462 | $a = preg_split($regex, $select); |
| 463 | |
| 464 | $join = '<select ' . $select_attr . '>' . "\r\n"; |
| 465 | $join .= $option . ppress_var($a, 1, ''); |
| 466 | |
| 467 | return $join; |
| 468 | } |
| 469 | |
| 470 | /** |
| 471 | * Blog name or domain name if name doesn't exist |
| 472 | * |
| 473 | * @return string |
| 474 | */ |
| 475 | function ppress_site_title() |
| 476 | { |
| 477 | $blog_name = get_option('blogname'); |
| 478 | |
| 479 | return ! empty($blog_name) ? wp_specialchars_decode($blog_name, ENT_QUOTES) : str_replace( |
| 480 | array( |
| 481 | 'http://', |
| 482 | 'https://', |
| 483 | ), |
| 484 | '', |
| 485 | site_url() |
| 486 | ); |
| 487 | } |
| 488 | |
| 489 | |
| 490 | /** |
| 491 | * Check if an admin settings page is ProfilePress' |
| 492 | * |
| 493 | * @return bool |
| 494 | */ |
| 495 | function ppress_is_admin_page() |
| 496 | { |
| 497 | $pp_builder_pages = [ |
| 498 | PPRESS_SETTINGS_SLUG, |
| 499 | PPRESS_FORMS_SETTINGS_SLUG, |
| 500 | PPRESS_MEMBER_DIRECTORIES_SLUG, |
| 501 | PPRESS_CONTENT_PROTECTION_SETTINGS_SLUG, |
| 502 | PPRESS_EXTENSIONS_SETTINGS_SLUG, |
| 503 | MailOptin::SLUG |
| 504 | ]; |
| 505 | |
| 506 | return (isset($_GET['page']) && in_array($_GET['page'], $pp_builder_pages)) || |
| 507 | isset($_GET['tab']) && $_GET['tab'] == 'ppress_extensions'; |
| 508 | } |
| 509 | |
| 510 | |
| 511 | /** |
| 512 | * Return admin email |
| 513 | * |
| 514 | * @return string |
| 515 | */ |
| 516 | function ppress_admin_email() |
| 517 | { |
| 518 | return get_option('admin_email'); |
| 519 | } |
| 520 | |
| 521 | /** |
| 522 | * Checks whether the given user ID exists. |
| 523 | * |
| 524 | * @param string $user_id ID of user |
| 525 | * |
| 526 | * @return null|int The user's ID on success, and null on failure. |
| 527 | */ |
| 528 | function ppress_user_id_exist($user_id) |
| 529 | { |
| 530 | if ($user = get_user_by('id', $user_id)) { |
| 531 | return $user->ID; |
| 532 | } |
| 533 | |
| 534 | return null; |
| 535 | } |
| 536 | |
| 537 | /** |
| 538 | * Get a user's username by their ID |
| 539 | * |
| 540 | * @param int $user_id |
| 541 | * |
| 542 | * @return bool|string |
| 543 | */ |
| 544 | function ppress_get_username_by_id($user_id) |
| 545 | { |
| 546 | return ppress_var_obj(get_user_by('id', $user_id), 'user_login'); |
| 547 | } |
| 548 | |
| 549 | /** |
| 550 | * front-end profile slug. |
| 551 | * |
| 552 | * @return string |
| 553 | */ |
| 554 | function ppress_get_profile_slug() |
| 555 | { |
| 556 | return apply_filters('ppress_profile_slug', ppress_get_setting('set_user_profile_slug', 'profile', true)); |
| 557 | } |
| 558 | |
| 559 | /** |
| 560 | * Filter form field attributes for unofficial attributes. |
| 561 | * |
| 562 | * @param array $atts supplied shortcode attributes |
| 563 | * |
| 564 | * @return mixed |
| 565 | * |
| 566 | */ |
| 567 | function ppress_other_field_atts($atts) |
| 568 | { |
| 569 | if ( ! is_array($atts)) return $atts; |
| 570 | |
| 571 | $official_atts = array('name', 'class', 'id', 'value', 'title', 'required', 'placeholder', 'key', 'field_key', 'limit', 'options', 'checkbox_text', 'processing_label'); |
| 572 | |
| 573 | $other_atts = array(); |
| 574 | |
| 575 | foreach ($atts as $key => $value) { |
| 576 | if ( ! in_array($key, $official_atts)) { |
| 577 | $other_atts[$key] = $value; |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | $other_atts_html = ''; |
| 582 | |
| 583 | foreach ($other_atts as $key => $value) { |
| 584 | if ( ! empty($value)) { |
| 585 | $other_atts_html .= "$key=\"$value\" "; |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | return $other_atts_html; |
| 590 | } |
| 591 | |
| 592 | |
| 593 | /** |
| 594 | * Create an index.php file to prevent directory browsing. |
| 595 | * |
| 596 | * @param string $location folder path to create the file in. |
| 597 | */ |
| 598 | function ppress_create_index_file($location) |
| 599 | { |
| 600 | $content = "You are not allowed here!"; |
| 601 | $fp = fopen($location . "/index.php", "wb"); |
| 602 | fwrite($fp, $content); |
| 603 | fclose($fp); |
| 604 | } |
| 605 | |
| 606 | /** |
| 607 | * Get front-end do password reset form url. |
| 608 | * |
| 609 | * @param string $user_login |
| 610 | * @param string $key |
| 611 | * |
| 612 | * @return string |
| 613 | */ |
| 614 | function ppress_get_do_password_reset_url($user_login, $key) |
| 615 | { |
| 616 | $url = network_site_url("wp-login.php?action=rp&key=$key&login=" . rawurlencode($user_login), 'login'); |
| 617 | |
| 618 | $page_id = ppress_get_setting('set_lost_password_url'); |
| 619 | |
| 620 | if (apply_filters('ppress_front_end_do_password_reset', true) && ! empty($page_id)) { |
| 621 | |
| 622 | $url = add_query_arg( |
| 623 | array( |
| 624 | 'key' => $key, |
| 625 | 'login' => rawurlencode($user_login) |
| 626 | ), |
| 627 | ppress_password_reset_url() |
| 628 | ); |
| 629 | } |
| 630 | |
| 631 | return $url; |
| 632 | } |
| 633 | |
| 634 | /** |
| 635 | * Return true if a field key exist/is multi selectable dropdown. |
| 636 | * |
| 637 | * @param $field_key |
| 638 | * |
| 639 | * @return bool |
| 640 | */ |
| 641 | function ppress_is_select_field_multi_selectable($field_key) |
| 642 | { |
| 643 | $data = get_option('ppress_cpf_select_multi_selectable', array()); |
| 644 | |
| 645 | return array_key_exists($field_key, $data); |
| 646 | } |
| 647 | |
| 648 | |
| 649 | /** |
| 650 | * Return username/username of a user using the user's nicename to do the DB search. |
| 651 | * |
| 652 | * @param string $slug |
| 653 | * |
| 654 | * @return bool|null|string |
| 655 | */ |
| 656 | function ppress_is_slug_nice_name($slug) |
| 657 | { |
| 658 | global $wpdb; |
| 659 | |
| 660 | $response = $wpdb->get_var( |
| 661 | $wpdb->prepare( |
| 662 | "SELECT user_login FROM {$wpdb->prefix}users WHERE user_nicename = '%s'", |
| 663 | array($slug) |
| 664 | ) |
| 665 | ); |
| 666 | |
| 667 | // if response isn't null, the username/user_login is returned. |
| 668 | return is_null($response) ? false : $response; |
| 669 | } |
| 670 | |
| 671 | /** |
| 672 | * Return array of editable roles. |
| 673 | * |
| 674 | * @param $remove_admin |
| 675 | * |
| 676 | * @return mixed |
| 677 | */ |
| 678 | function ppress_get_editable_roles($remove_admin = true) |
| 679 | { |
| 680 | $all_roles = wp_roles()->roles; |
| 681 | |
| 682 | if (true == $remove_admin) { |
| 683 | unset($all_roles['administrator']); |
| 684 | } |
| 685 | |
| 686 | return $all_roles; |
| 687 | } |
| 688 | |
| 689 | function ppress_wp_roles_key_value($remove_admin = true) |
| 690 | { |
| 691 | $wp_roles = ppress_get_editable_roles($remove_admin); |
| 692 | |
| 693 | return array_reduce(array_keys($wp_roles), function ($carry, $item) use ($wp_roles) { |
| 694 | $carry[$item] = $wp_roles[$item]['name']; |
| 695 | |
| 696 | return $carry; |
| 697 | }, []); |
| 698 | } |
| 699 | |
| 700 | function ppress_wp_new_user_notification($user_id, $deprecated = null, $notify = '') |
| 701 | { |
| 702 | if (null !== $deprecated) { |
| 703 | _deprecated_argument(__FUNCTION__, '4.3.1'); |
| 704 | } |
| 705 | |
| 706 | // Accepts only 'user', 'admin' , 'both' or default '' as $notify. |
| 707 | if ( ! in_array($notify, array('user', 'admin', 'both', ''), true)) { |
| 708 | return; |
| 709 | } |
| 710 | |
| 711 | $new_user_notification = apply_filters('ppress_new_user_notification', 'enable'); |
| 712 | |
| 713 | if ('enable' != $new_user_notification) return; |
| 714 | |
| 715 | $user = get_userdata($user_id); |
| 716 | |
| 717 | // The blogname option is escaped with esc_html() on the way into the database in sanitize_option(). |
| 718 | // We want to reverse this for the plain text arena of emails. |
| 719 | $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES); |
| 720 | |
| 721 | if ('user' !== $notify) { |
| 722 | |
| 723 | if (ppress_get_setting('new_user_admin_email_email_enabled', 'on') !== 'on') return; |
| 724 | |
| 725 | $message = ppress_get_setting('new_user_admin_email_email_content', ppress_new_user_admin_notification_message_default(), true); |
| 726 | |
| 727 | $title = ppress_get_setting('new_user_admin_email_email_subject', sprintf(__('[%s] New User Registration'), $blogname), true); |
| 728 | |
| 729 | // handle support for custom fields placeholder. |
| 730 | preg_match_all('#({{[a-z_-]+}})#', $message, $matches); |
| 731 | |
| 732 | if (isset($matches[1]) && ! empty($matches[1])) { |
| 733 | |
| 734 | foreach ($matches[1] as $match) { |
| 735 | $key = str_replace(['{', '}'], '', $match); |
| 736 | |
| 737 | if (isset($user->{$key})) { |
| 738 | $value = $user->{$key}; |
| 739 | |
| 740 | if (is_array($value)) { |
| 741 | $value = implode(', ', $value); |
| 742 | } |
| 743 | |
| 744 | $message = str_replace($match, $value, $message); |
| 745 | } |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | $search = array( |
| 750 | '{{username}}', |
| 751 | '{{user_email}}', |
| 752 | '{{site_title}}', |
| 753 | '{{first_name}}', |
| 754 | '{{last_name}}' |
| 755 | ); |
| 756 | |
| 757 | $replace = array( |
| 758 | $user->user_login, |
| 759 | $user->user_email, |
| 760 | $blogname, |
| 761 | $user->first_name, |
| 762 | $user->last_name |
| 763 | ); |
| 764 | |
| 765 | $message = htmlspecialchars_decode( |
| 766 | apply_filters( |
| 767 | 'ppress_signup_admin_email_message', |
| 768 | str_replace($search, $replace, $message), |
| 769 | $user |
| 770 | ) |
| 771 | ); |
| 772 | |
| 773 | $title = apply_filters( |
| 774 | 'ppress_signup_admin_email_subject', |
| 775 | str_replace($search, $replace, $title), |
| 776 | $user |
| 777 | ); |
| 778 | |
| 779 | $admin_email = apply_filters('ppress_signup_notification_admin_email', ppress_get_admin_notification_emails()); |
| 780 | |
| 781 | ppress_send_email($admin_email, $title, $message); |
| 782 | } |
| 783 | |
| 784 | // `$deprecated` was pre-4.3 `$plaintext_pass`. An empty `$plaintext_pass` didn't sent a user notification. |
| 785 | if ('admin' === $notify || (empty($deprecated) && empty($notify))) { |
| 786 | return; |
| 787 | } |
| 788 | |
| 789 | $key = get_password_reset_key($user); |
| 790 | if (is_wp_error($key)) { |
| 791 | return; |
| 792 | } |
| 793 | |
| 794 | $switched_locale = switch_to_locale(get_user_locale($user)); |
| 795 | |
| 796 | /* translators: %s: User login. */ |
| 797 | $message = sprintf(__('Username: %s'), $user->user_login) . "\r\n\r\n"; |
| 798 | $message .= __('To set your password, visit the following address:') . "\r\n\r\n"; |
| 799 | $message .= network_site_url("wp-login.php?action=rp&key=$key&login=" . rawurlencode($user->user_login), 'login') . "\r\n\r\n"; |
| 800 | |
| 801 | $message .= wp_login_url() . "\r\n"; |
| 802 | |
| 803 | $wp_new_user_notification_email = array( |
| 804 | 'to' => $user->user_email, |
| 805 | /* translators: Login details notification email subject. %s: Site title. */ |
| 806 | 'subject' => __('[%s] Login Details'), |
| 807 | 'message' => $message, |
| 808 | 'headers' => '', |
| 809 | ); |
| 810 | |
| 811 | /** |
| 812 | * Filters the contents of the new user notification email sent to the new user. |
| 813 | * |
| 814 | * @param array $wp_new_user_notification_email { |
| 815 | * Used to build wp_mail(). |
| 816 | * |
| 817 | * @type string $to The intended recipient - New user email address. |
| 818 | * @type string $subject The subject of the email. |
| 819 | * @type string $message The body of the email. |
| 820 | * @type string $headers The headers of the email. |
| 821 | * } |
| 822 | * |
| 823 | * @param WP_User $user User object for new user. |
| 824 | * @param string $blogname The site title. |
| 825 | * |
| 826 | * @since 4.9.0 |
| 827 | * |
| 828 | */ |
| 829 | $wp_new_user_notification_email = apply_filters('wp_new_user_notification_email', $wp_new_user_notification_email, $user, $blogname); |
| 830 | |
| 831 | wp_mail( |
| 832 | $wp_new_user_notification_email['to'], |
| 833 | wp_specialchars_decode(sprintf($wp_new_user_notification_email['subject'], $blogname)), |
| 834 | $wp_new_user_notification_email['message'], |
| 835 | $wp_new_user_notification_email['headers'] |
| 836 | ); |
| 837 | |
| 838 | if ($switched_locale) { |
| 839 | restore_previous_locale(); |
| 840 | } |
| 841 | } |
| 842 | |
| 843 | if ( ! function_exists('wp_new_user_notification')) : |
| 844 | /** |
| 845 | * Email login credentials to a newly-registered user. |
| 846 | * |
| 847 | * A new user registration notification is also sent to admin email. |
| 848 | * |
| 849 | * @param int $user_id User ID. |
| 850 | * @param null $deprecated Not used (argument deprecated). |
| 851 | * @param string $notify Optional. Type of notification that should happen. Accepts 'admin' or an empty |
| 852 | * string (admin only), 'user', or 'both' (admin and user). Default empty. |
| 853 | * |
| 854 | * @since 4.6.0 The `$notify` parameter accepts 'user' for sending notification only to the user created. |
| 855 | * |
| 856 | * @global wpdb $wpdb WordPress database object for queries. |
| 857 | * @global PasswordHash $wp_hasher Portable PHP password hashing framework instance. |
| 858 | * |
| 859 | * @since 2.0.0 |
| 860 | * @since 4.3.0 The `$plaintext_pass` parameter was changed to `$notify`. |
| 861 | * @since 4.3.1 The `$plaintext_pass` parameter was deprecated. `$notify` added as a third parameter. |
| 862 | */ |
| 863 | function wp_new_user_notification($user_id, $deprecated = null, $notify = '') |
| 864 | { |
| 865 | return ppress_wp_new_user_notification($user_id, $deprecated, $notify); |
| 866 | } |
| 867 | endif; |
| 868 | |
| 869 | /** |
| 870 | * Does registration form has username requirement disabled? |
| 871 | * |
| 872 | * @param int $form_id |
| 873 | * @param bool $is_melange |
| 874 | * |
| 875 | * @return bool |
| 876 | */ |
| 877 | function ppress_is_signup_form_username_disabled($form_id, $is_melange = false) |
| 878 | { |
| 879 | $result = FR::get_form_meta($form_id, FR::REGISTRATION_TYPE, FR::DISABLE_USERNAME_REQUIREMENT); |
| 880 | |
| 881 | if ($is_melange === true) { |
| 882 | $result = FR::get_form_meta($form_id, FR::MELANGE_TYPE, FR::DISABLE_USERNAME_REQUIREMENT); |
| 883 | } |
| 884 | |
| 885 | if (is_string($result)) { |
| 886 | $result = $result == 'true' ? true : false; |
| 887 | } |
| 888 | |
| 889 | return $result; |
| 890 | } |
| 891 | |
| 892 | /** |
| 893 | * Generate url to reset user's password. |
| 894 | * |
| 895 | * @param string $user_login |
| 896 | * |
| 897 | * @return string |
| 898 | */ |
| 899 | function ppress_generate_password_reset_url($user_login) |
| 900 | { |
| 901 | $user = get_user_by('login', $user_login); |
| 902 | |
| 903 | $key = get_password_reset_key($user); |
| 904 | |
| 905 | return ppress_get_do_password_reset_url($user_login, $key); |
| 906 | } |
| 907 | |
| 908 | function ppress_nonce_action_string() |
| 909 | { |
| 910 | return 'pp_plugin_nonce'; |
| 911 | } |
| 912 | |
| 913 | /** |
| 914 | * Return array of countries. |
| 915 | * |
| 916 | * @return mixed |
| 917 | */ |
| 918 | function ppress_array_of_world_countries() |
| 919 | { |
| 920 | return apply_filters('ppress_countries_custom_field_data', include(PROFILEPRESS_SRC . 'Functions/data/countries.php')); |
| 921 | } |
| 922 | |
| 923 | function ppress_create_nonce() |
| 924 | { |
| 925 | return wp_create_nonce(ppress_nonce_action_string()); |
| 926 | } |
| 927 | |
| 928 | function ppress_nonce_field() |
| 929 | { |
| 930 | return wp_nonce_field(ppress_nonce_action_string(), '_wpnonce', true, false); |
| 931 | } |
| 932 | |
| 933 | function ppress_verify_nonce() |
| 934 | { |
| 935 | return check_admin_referer(ppress_nonce_action_string()); |
| 936 | } |
| 937 | |
| 938 | function ppress_verify_ajax_nonce() |
| 939 | { |
| 940 | return check_ajax_referer(ppress_nonce_action_string()); |
| 941 | } |
| 942 | |
| 943 | /** |
| 944 | * Returns a more compact md5 hashing. |
| 945 | * |
| 946 | * @param $string |
| 947 | * |
| 948 | * @return false|string |
| 949 | */ |
| 950 | function ppress_md5($string) |
| 951 | { |
| 952 | return substr(base_convert(md5($string), 16, 32), 0, 12); |
| 953 | } |
| 954 | |
| 955 | function ppress_minify_css($buffer) |
| 956 | { |
| 957 | $buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer); |
| 958 | $buffer = str_replace(': ', ':', $buffer); |
| 959 | $buffer = str_replace(array("\r\n", "\r", "\n", "\t", ' ', ' ', ' '), '', $buffer); |
| 960 | |
| 961 | return $buffer; |
| 962 | } |
| 963 | |
| 964 | function ppress_minify_js($code) |
| 965 | { |
| 966 | // make it into one long line |
| 967 | $code = str_replace(array("\n", "\r"), '', $code); |
| 968 | // replace all multiple spaces by one space |
| 969 | $code = preg_replace('!\s+!', ' ', $code); |
| 970 | // replace some unneeded spaces, modify as needed |
| 971 | $code = str_replace(array(' {', ' }', '{ ', '; '), array('{', '}', '{', ';'), $code); |
| 972 | |
| 973 | return $code; |
| 974 | } |
| 975 | |
| 976 | function ppress_get_ip_address() |
| 977 | { |
| 978 | $ip = '127.0.0.1'; |
| 979 | |
| 980 | if ( ! empty($_SERVER['HTTP_CLIENT_IP'])) { |
| 981 | $ip = $_SERVER['HTTP_CLIENT_IP']; |
| 982 | } elseif ( ! empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { |
| 983 | $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; |
| 984 | } elseif ( ! empty($_SERVER['REMOTE_ADDR'])) { |
| 985 | $ip = $_SERVER['REMOTE_ADDR']; |
| 986 | } |
| 987 | |
| 988 | // Fix potential CSV returned from $_SERVER variables |
| 989 | $ip_array = array_map('trim', explode(',', $ip)); |
| 990 | |
| 991 | return $ip_array[0] != '::1' ? $ip_array[0] : ''; |
| 992 | } |
| 993 | |
| 994 | /** |
| 995 | * Admin email address to receive admin notification. |
| 996 | * |
| 997 | * @return mixed |
| 998 | */ |
| 999 | function ppress_get_admin_notification_emails() |
| 1000 | { |
| 1001 | return ppress_get_setting('admin_email_addresses', ppress_admin_email(), true); |
| 1002 | } |
| 1003 | |
| 1004 | function ppress_get_error_log($type = 'debug') |
| 1005 | { |
| 1006 | $log_file = PPRESS_ERROR_LOG_FOLDER . $type . '.log'; |
| 1007 | |
| 1008 | $file_contents = ''; |
| 1009 | |
| 1010 | if (file_exists($log_file)) { |
| 1011 | $file_contents = @file_get_contents($log_file); |
| 1012 | } |
| 1013 | |
| 1014 | return $file_contents; |
| 1015 | } |
| 1016 | |
| 1017 | function ppress_log_error($message, $type = 'debug') |
| 1018 | { |
| 1019 | $log_folder = PPRESS_ERROR_LOG_FOLDER; |
| 1020 | |
| 1021 | // does bugs folder exist? if NO, create it. |
| 1022 | if ( ! file_exists($log_folder)) { |
| 1023 | mkdir($log_folder, 0755, true); |
| 1024 | } |
| 1025 | |
| 1026 | if ( ! file_exists(PPRESS_ERROR_LOG_FOLDER . '/index.php')) { |
| 1027 | ppress_create_index_file(PPRESS_ERROR_LOG_FOLDER); |
| 1028 | } |
| 1029 | |
| 1030 | $message = current_time('mysql') . ' - ' . $message . "\r\n\r\n"; |
| 1031 | |
| 1032 | error_log($message, 3, "{$log_folder}{$type}.log"); |
| 1033 | } |
| 1034 | |
| 1035 | function ppress_clear_error_log($type = 'debug') |
| 1036 | { |
| 1037 | return @unlink(PPRESS_ERROR_LOG_FOLDER . $type . '.log'); |
| 1038 | } |
| 1039 | |
| 1040 | function ppressPOST_var($key, $default = false, $empty = false, $bucket = false) |
| 1041 | { |
| 1042 | $bucket = ! $bucket ? $_POST : $bucket; |
| 1043 | |
| 1044 | if ($empty) { |
| 1045 | return ! empty($bucket[$key]) ? $bucket[$key] : $default; |
| 1046 | } |
| 1047 | |
| 1048 | return isset($bucket[$key]) ? $bucket[$key] : $default; |
| 1049 | } |
| 1050 | |
| 1051 | function ppressGET_var($key, $default = false, $empty = false) |
| 1052 | { |
| 1053 | $bucket = $_GET; |
| 1054 | |
| 1055 | if ($empty) { |
| 1056 | return ! empty($bucket[$key]) ? $bucket[$key] : $default; |
| 1057 | } |
| 1058 | |
| 1059 | return isset($bucket[$key]) ? $bucket[$key] : $default; |
| 1060 | } |
| 1061 | |
| 1062 | function ppress_var($bucket, $key, $default = false, $empty = false) |
| 1063 | { |
| 1064 | if ($empty) { |
| 1065 | return ! empty($bucket[$key]) ? $bucket[$key] : $default; |
| 1066 | } |
| 1067 | |
| 1068 | return isset($bucket[$key]) ? $bucket[$key] : $default; |
| 1069 | } |
| 1070 | |
| 1071 | function ppress_var_obj($bucket, $key, $default = false, $empty = false) |
| 1072 | { |
| 1073 | if ($empty) { |
| 1074 | return ! empty($bucket->$key) ? $bucket->$key : $default; |
| 1075 | } |
| 1076 | |
| 1077 | return isset($bucket->$key) ? $bucket->$key : $default; |
| 1078 | } |
| 1079 | |
| 1080 | /** |
| 1081 | * Normalize unamed shortcode |
| 1082 | * |
| 1083 | * @param array $atts |
| 1084 | * |
| 1085 | * @return mixed |
| 1086 | */ |
| 1087 | function ppress_normalize_attributes($atts) |
| 1088 | { |
| 1089 | if (is_array($atts)) { |
| 1090 | foreach ($atts as $key => $value) { |
| 1091 | if (is_int($key)) { |
| 1092 | $atts[$value] = true; |
| 1093 | unset($atts[$key]); |
| 1094 | } |
| 1095 | } |
| 1096 | } |
| 1097 | |
| 1098 | return $atts; |
| 1099 | } |
| 1100 | |
| 1101 | /** @todo link to doc on reserve words/text here */ |
| 1102 | function ppress_dnd_field_key_description() |
| 1103 | { |
| 1104 | return esc_html__('It must be unique for each field, not a reserve text, in lowercase letters only with an underscore ( _ ) separating words e.g job_title', 'wp-user-avatar'); |
| 1105 | } |
| 1106 | |
| 1107 | function ppress_reserved_field_keys() |
| 1108 | { |
| 1109 | return [ |
| 1110 | 'ID', 'id', 'user_pass', 'user_login', 'user_nicename', 'user_url', 'user_email', 'display_name', 'nickname', |
| 1111 | 'first_name', 'last_name', 'description', 'rich_editing', 'syntax_highlighting', 'comment_shortcuts', 'admin_color', |
| 1112 | 'use_ssl', 'user_registered', 'user_activation_key', 'spam', 'show_admin_bar_front', 'role', 'locale', 'deleted', 'user_level', |
| 1113 | 'user_status', 'user_description' |
| 1114 | ]; |
| 1115 | } |
| 1116 | |
| 1117 | function ppress_is_boolean($maybe_bool) |
| 1118 | { |
| 1119 | if (is_bool($maybe_bool)) { |
| 1120 | return true; |
| 1121 | } |
| 1122 | |
| 1123 | if (is_string($maybe_bool)) { |
| 1124 | $maybe_bool = strtolower($maybe_bool); |
| 1125 | |
| 1126 | $valid_boolean_values = array( |
| 1127 | 'false', |
| 1128 | 'true', |
| 1129 | '0', |
| 1130 | '1', |
| 1131 | ); |
| 1132 | |
| 1133 | return in_array($maybe_bool, $valid_boolean_values, true); |
| 1134 | } |
| 1135 | |
| 1136 | if (is_int($maybe_bool)) { |
| 1137 | return in_array($maybe_bool, array(0, 1), true); |
| 1138 | } |
| 1139 | |
| 1140 | return false; |
| 1141 | } |
| 1142 | |
| 1143 | function ppress_filter_empty_array($values) |
| 1144 | { |
| 1145 | return array_filter($values, function ($value) { |
| 1146 | return ppress_is_boolean($value) || is_int($value) || ! empty($value); |
| 1147 | }); |
| 1148 | } |
| 1149 | |
| 1150 | /** |
| 1151 | * Check if HTTP status code is successful. |
| 1152 | * |
| 1153 | * @param int $code |
| 1154 | * |
| 1155 | * @return bool |
| 1156 | */ |
| 1157 | function ppress_is_http_code_success($code) |
| 1158 | { |
| 1159 | $code = absint($code); |
| 1160 | |
| 1161 | return $code >= 200 && $code <= 299; |
| 1162 | } |
| 1163 | |
| 1164 | /** |
| 1165 | * strtotime uses the default timezone set in PHP which may or may not be UTC. |
| 1166 | * |
| 1167 | * @param $time |
| 1168 | * |
| 1169 | * @param null|int $now |
| 1170 | * |
| 1171 | * @return false|int |
| 1172 | */ |
| 1173 | function ppress_strtotime_utc($time, $now = null) |
| 1174 | { |
| 1175 | if (is_null($now)) $now = time(); |
| 1176 | |
| 1177 | $old = date_default_timezone_get(); |
| 1178 | |
| 1179 | date_default_timezone_set('UTC'); |
| 1180 | $val = strtotime($time, $now); |
| 1181 | |
| 1182 | date_default_timezone_set($old); |
| 1183 | |
| 1184 | return $val; |
| 1185 | } |
| 1186 | |
| 1187 | function ppress_array_flatten($array) |
| 1188 | { |
| 1189 | if ( ! is_array($array)) { |
| 1190 | return false; |
| 1191 | } |
| 1192 | $result = array(); |
| 1193 | foreach ($array as $key => $value) { |
| 1194 | if (is_array($value)) { |
| 1195 | // we are not doing array_merge here because we wanna keep array keys. |
| 1196 | // PS: The + operator is not an addition, it's a union. If the keys don't overlap then all is good. |
| 1197 | $result = $result + ppress_array_flatten($value); |
| 1198 | } else { |
| 1199 | $result[$key] = $value; |
| 1200 | } |
| 1201 | } |
| 1202 | |
| 1203 | return $result; |
| 1204 | } |
| 1205 | |
| 1206 | /** |
| 1207 | * Sanitizes a string key. |
| 1208 | * |
| 1209 | * Keys are used as internal identifiers. Lowercase alphanumeric characters and underscores are allowed. |
| 1210 | * |
| 1211 | * @param string $key String key |
| 1212 | * |
| 1213 | * @return string Sanitized key |
| 1214 | */ |
| 1215 | function ppress_sanitize_key($key) |
| 1216 | { |
| 1217 | return str_replace('-', '_', sanitize_key($key)); |
| 1218 | } |
| 1219 | |
| 1220 | function ppress_woocommerce_field_transform($cf_key, $cf_value) |
| 1221 | { |
| 1222 | if (class_exists('WooCommerce')) { |
| 1223 | |
| 1224 | if (in_array($cf_key, ppress_woocommerce_billing_fields())) { |
| 1225 | $cf_value = sprintf(esc_html__('%s (WooCommerce Billing Address)', 'wp-user-avatar'), $cf_value); |
| 1226 | } |
| 1227 | |
| 1228 | if (in_array($cf_key, ppress_woocommerce_shipping_fields())) { |
| 1229 | $cf_value = sprintf(esc_html__('%s (WooCommerce Shipping Address)', 'wp-user-avatar'), $cf_value); |
| 1230 | } |
| 1231 | } |
| 1232 | |
| 1233 | return $cf_value; |
| 1234 | } |
| 1235 | |
| 1236 | function ppress_custom_fields_key_value_pair($remove_default = false) |
| 1237 | { |
| 1238 | $defined_custom_fields = []; |
| 1239 | |
| 1240 | if ($remove_default === false) { |
| 1241 | $defined_custom_fields[''] = esc_html__('Select...', 'wp-user-avatar'); |
| 1242 | } |
| 1243 | |
| 1244 | if (EM::is_premium()) { |
| 1245 | $db_custom_fields = PROFILEPRESS_sql::get_profile_custom_fields(); |
| 1246 | $db_contact_infos = PROFILEPRESS_sql::get_contact_info_fields(); |
| 1247 | |
| 1248 | if ( ! empty($db_contact_infos)) { |
| 1249 | foreach ($db_contact_infos as $key => $value) { |
| 1250 | $defined_custom_fields[$key] = $value; |
| 1251 | } |
| 1252 | } |
| 1253 | |
| 1254 | if ( ! empty($db_custom_fields)) { |
| 1255 | foreach ($db_custom_fields as $db_custom_field) { |
| 1256 | $field_key = $db_custom_field['field_key']; |
| 1257 | $defined_custom_fields[$field_key] = ppress_woocommerce_field_transform($field_key, $db_custom_field['label_name']); |
| 1258 | } |
| 1259 | } |
| 1260 | } |
| 1261 | |
| 1262 | return $defined_custom_fields; |
| 1263 | } |
| 1264 | |
| 1265 | function ppress_standard_fields_key_value_pair($remove_default = false) |
| 1266 | { |
| 1267 | $fields = []; |
| 1268 | if ($remove_default === false) { |
| 1269 | $fields[''] = esc_html__('Select...', 'wp-user-avatar'); |
| 1270 | } |
| 1271 | |
| 1272 | return array_merge($fields, [ |
| 1273 | 'first_last_names' => esc_html__('First and Last Names', 'wp-user-avatar'), |
| 1274 | 'last_first_names' => esc_html__('Last and First Names', 'wp-user-avatar'), |
| 1275 | 'username' => esc_html__('Username', 'wp-user-avatar'), |
| 1276 | 'first-name' => esc_html__('First Name', 'wp-user-avatar'), |
| 1277 | 'last-name' => esc_html__('Last Name', 'wp-user-avatar'), |
| 1278 | 'nickname' => esc_html__('Nickname', 'wp-user-avatar'), |
| 1279 | 'display-name' => esc_html__('Display Name', 'wp-user-avatar'), |
| 1280 | 'email' => esc_html__('Email Address', 'wp-user-avatar'), |
| 1281 | 'bio' => esc_html__('Biography', 'wp-user-avatar'), |
| 1282 | 'registration_date' => esc_html__('Registration Date', 'wp-user-avatar'), |
| 1283 | ]); |
| 1284 | } |
| 1285 | |
| 1286 | function ppress_standard_custom_fields_key_value_pair($remove_default = false) |
| 1287 | { |
| 1288 | $fields = []; |
| 1289 | |
| 1290 | if ($remove_default === false) { |
| 1291 | $fields[''] = esc_html__('Select...', 'wp-user-avatar'); |
| 1292 | } |
| 1293 | |
| 1294 | $fields[esc_html__('Standard Fields', 'wp-user-avatar')] = ppress_standard_fields_key_value_pair(true); |
| 1295 | |
| 1296 | if (EM::is_enabled(EM::CUSTOM_FIELDS)) { |
| 1297 | $fields[esc_html__('Custom Fields', 'wp-user-avatar')] = ppress_custom_fields_key_value_pair(true); |
| 1298 | } |
| 1299 | |
| 1300 | return $fields; |
| 1301 | } |
| 1302 | |
| 1303 | /** |
| 1304 | * @param int|bool $user_id |
| 1305 | * |
| 1306 | * @return bool |
| 1307 | */ |
| 1308 | function ppress_user_has_cover_image($user_id = false) |
| 1309 | { |
| 1310 | $user_id = ! $user_id ? get_current_user_id() : $user_id; |
| 1311 | |
| 1312 | $cover = get_user_meta($user_id, 'pp_profile_cover_image', true); |
| 1313 | |
| 1314 | return ! empty($cover); |
| 1315 | } |
| 1316 | |
| 1317 | |
| 1318 | /** |
| 1319 | * @param int|bool $user_id |
| 1320 | * |
| 1321 | * @return string|bool |
| 1322 | */ |
| 1323 | function ppress_get_cover_image_url($user_id = false) |
| 1324 | { |
| 1325 | $user_id = ! $user_id ? get_current_user_id() : $user_id; |
| 1326 | |
| 1327 | $slug = get_user_meta($user_id, 'pp_profile_cover_image', true); |
| 1328 | |
| 1329 | if ( ! empty($slug)) { |
| 1330 | return PPRESS_COVER_IMAGE_UPLOAD_URL . "$slug"; |
| 1331 | } |
| 1332 | |
| 1333 | return get_option('wp_user_cover_default_image_url'); |
| 1334 | } |
| 1335 | |
| 1336 | function ppress_is_my_own_profile() |
| 1337 | { |
| 1338 | global $ppress_frontend_profile_user_obj; |
| 1339 | |
| 1340 | return ppress_var_obj($ppress_frontend_profile_user_obj, 'ID') == get_current_user_id(); |
| 1341 | } |
| 1342 | |
| 1343 | function ppress_social_network_fields() |
| 1344 | { |
| 1345 | return apply_filters('ppress_core_contact_info_fields', [ |
| 1346 | Base::cif_facebook => 'Facebook', |
| 1347 | Base::cif_twitter => 'Twitter', |
| 1348 | Base::cif_linkedin => 'LinkedIn', |
| 1349 | Base::cif_vk => 'VK', |
| 1350 | Base::cif_youtube => 'YouTube', |
| 1351 | Base::cif_instagram => 'Instagram', |
| 1352 | Base::cif_github => 'GitHub', |
| 1353 | ]); |
| 1354 | } |
| 1355 | |
| 1356 | function ppress_mb_function($function_names, $args) |
| 1357 | { |
| 1358 | $mb_function_name = $function_names[0]; |
| 1359 | $function_name = $function_names[1]; |
| 1360 | if (function_exists($mb_function_name)) { |
| 1361 | $function_name = $mb_function_name; |
| 1362 | } |
| 1363 | |
| 1364 | return call_user_func_array($function_name, $args); |
| 1365 | } |
| 1366 | |
| 1367 | function ppress_recursive_trim($item) |
| 1368 | { |
| 1369 | if (is_array($item)) { |
| 1370 | |
| 1371 | $sanitized_data = []; |
| 1372 | foreach ($item as $key => $value) { |
| 1373 | $sanitized_data[$key] = ppress_recursive_trim($value); |
| 1374 | } |
| 1375 | |
| 1376 | return $sanitized_data; |
| 1377 | } |
| 1378 | |
| 1379 | return trim($item); |
| 1380 | } |
| 1381 | |
| 1382 | function ppress_check_type_and_ext($file, $accepted_mime_types = [], $accepted_file_ext = []) |
| 1383 | { |
| 1384 | |
| 1385 | if (empty($file_name)) { |
| 1386 | $file_name = $file['name']; |
| 1387 | } |
| 1388 | |
| 1389 | $tmp_name = $file['tmp_name']; |
| 1390 | |
| 1391 | $wp_filetype = wp_check_filetype_and_ext($tmp_name, $file_name); |
| 1392 | |
| 1393 | $ext = $wp_filetype['ext']; |
| 1394 | $type = $wp_filetype['type']; |
| 1395 | $proper_filename = $wp_filetype['proper_filename']; |
| 1396 | |
| 1397 | // When a proper_filename value exists, it could be a security issue if it's different than the original file name. |
| 1398 | if ($proper_filename && strtolower($proper_filename) !== strtolower($file_name)) { |
| 1399 | return new WP_Error('invalid_file', esc_html__('There was an problem while verifying your file.', 'wp-user-avatar')); |
| 1400 | } |
| 1401 | |
| 1402 | // If either $ext or $type are empty, WordPress doesn't like this file and we should bail. |
| 1403 | if ( ! $ext) { |
| 1404 | return new WP_Error('illegal_extension', esc_html__('Sorry, this file extension is not permitted for security reasons.', 'wp-user-avatar')); |
| 1405 | } |
| 1406 | |
| 1407 | if ( ! $type) { |
| 1408 | return new WP_Error('illegal_type', esc_html__('Sorry, this file type is not permitted for security reasons.', 'wp-user-avatar')); |
| 1409 | } |
| 1410 | |
| 1411 | if ( ! empty($accepted_mime_types) && ! in_array($type, $accepted_mime_types)) { |
| 1412 | return new WP_Error('illegal_type', esc_html__('Error: The file you uploaded is not accepted on our website.', 'wp-user-avatar')); |
| 1413 | } |
| 1414 | |
| 1415 | if ( ! empty($accepted_file_ext) && ! in_array($ext, $accepted_file_ext)) { |
| 1416 | return new WP_Error('illegal_type', esc_html__('Error: The file you uploaded is not accepted on our website.', 'wp-user-avatar')); |
| 1417 | } |
| 1418 | |
| 1419 | return true; |
| 1420 | } |
| 1421 | |
| 1422 | function ppress_decode_html_strip_tags($val) |
| 1423 | { |
| 1424 | return strip_tags(html_entity_decode($val)); |
| 1425 | } |
| 1426 | |
| 1427 | function ppress_content_http_redirect($myURL) |
| 1428 | { |
| 1429 | ?> |
| 1430 | <script type="text/javascript"> |
| 1431 | window.location.href = "<?php echo $myURL;?>" |
| 1432 | </script> |
| 1433 | <meta http-equiv="refresh" content="0; url=<?php echo $myURL; ?>"> |
| 1434 | Please wait while you are redirected...or |
| 1435 | <a href="<?php echo $myURL; ?>">Click Here</a> if you do not want to wait. |
| 1436 | <?php |
| 1437 | } |
| 1438 | |
| 1439 | function ppress_shortcode_exist_in_post($shortcode) |
| 1440 | { |
| 1441 | global $post; |
| 1442 | |
| 1443 | if (isset($post->post_content) && has_shortcode($post->post_content, $shortcode)) { |
| 1444 | return true; |
| 1445 | } |
| 1446 | |
| 1447 | return false; |
| 1448 | } |
| 1449 | |
| 1450 | function ppress_clean($var) |
| 1451 | { |
| 1452 | if (is_array($var)) { |
| 1453 | return array_map('ppress_clean', $var); |
| 1454 | } else { |
| 1455 | return is_scalar($var) ? sanitize_textarea_field($var) : $var; |
| 1456 | } |
| 1457 | } |