GlobalFunctions.php
1361 lines
| 1 | <?php |
| 2 | /** List of ProfilePress global helper functions */ |
| 3 | |
| 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\PPRESS_Session; |
| 8 | use ProfilePress\Core\Classes\PROFILEPRESS_sql as PROFILEPRESS_sql; |
| 9 | use ProfilePress\Core\Classes\SendEmail; |
| 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 | |
| 223 | if (isset($custom_url_login_redirect) && ! empty($custom_url_login_redirect)) { |
| 224 | $redirect = $custom_url_login_redirect; |
| 225 | } elseif ($login_redirect == 'dashboard') { |
| 226 | $redirect = network_site_url('/wp-admin'); |
| 227 | } elseif ('current_page' == $login_redirect) { |
| 228 | // in ajax mode, pp_current_url is set so we can do client-side redirection to current page after login. |
| 229 | // no way to get current url in social login hence, look it up from $_GET['pp_current_url'] |
| 230 | if ( ! empty($_GET['pp_current_url'])) { |
| 231 | $redirect = rawurldecode($_GET['pp_current_url']); |
| 232 | } elseif (isset($_POST['pp_current_url'])) { |
| 233 | $redirect = $_POST['pp_current_url']; |
| 234 | } else { |
| 235 | $redirect = ppress_get_current_url_raw(); |
| 236 | } |
| 237 | } elseif (isset($login_redirect) && ! empty($login_redirect)) { |
| 238 | $redirect = get_permalink($login_redirect); |
| 239 | } else { |
| 240 | $redirect = network_site_url('/wp-admin'); |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | return apply_filters('ppress_login_redirect', esc_url_raw($redirect)); |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Return the url to redirect to after successful reset / change of password. |
| 249 | * |
| 250 | * @return bool|string |
| 251 | */ |
| 252 | function ppress_password_reset_redirect() |
| 253 | { |
| 254 | $password_reset_redirect = ppress_get_setting('set_password_reset_redirect'); |
| 255 | $custom_url_password_reset_redirect = ppress_get_setting('custom_url_password_reset_redirect'); |
| 256 | |
| 257 | if ( ! empty($custom_url_password_reset_redirect)) { |
| 258 | $redirect = $custom_url_password_reset_redirect; |
| 259 | } elseif ( ! empty($password_reset_redirect)) { |
| 260 | $redirect = get_permalink($password_reset_redirect); |
| 261 | if ($password_reset_redirect == 'no_redirect') { |
| 262 | $redirect = ppress_password_reset_url() . '?password=changed'; |
| 263 | } |
| 264 | } else { |
| 265 | $redirect = ppress_password_reset_url() . '?password=changed'; |
| 266 | } |
| 267 | |
| 268 | return apply_filters('ppress_do_password_reset_redirect', esc_url($redirect)); |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * Return the url to frontend myprofile page. |
| 273 | * |
| 274 | * @return bool|string |
| 275 | */ |
| 276 | function ppress_profile_url() |
| 277 | { |
| 278 | $url = admin_url('profile.php'); |
| 279 | |
| 280 | $page_id = ppress_get_setting('set_user_profile_shortcode'); |
| 281 | |
| 282 | if ( ! empty($page_id)) { |
| 283 | $url = get_permalink($page_id); |
| 284 | } |
| 285 | |
| 286 | return apply_filters('ppress_profile_url', $url); |
| 287 | } |
| 288 | |
| 289 | function ppress_get_frontend_profile_url($username_or_id) |
| 290 | { |
| 291 | if (is_numeric($username_or_id)) { |
| 292 | $username_or_id = ppress_get_username_by_id($username_or_id); |
| 293 | } |
| 294 | |
| 295 | return home_url(ppress_get_profile_slug() . '/' . rawurlencode($username_or_id)); |
| 296 | } |
| 297 | |
| 298 | /** |
| 299 | * Return ProfilePress edit profile page URL or WP default profile URL as fallback |
| 300 | * |
| 301 | * @return bool|string |
| 302 | */ |
| 303 | function ppress_edit_profile_url() |
| 304 | { |
| 305 | return apply_filters('ppress_edit_profile_url', ppress_my_account_url()); |
| 306 | } |
| 307 | |
| 308 | function ppress_my_account_url() |
| 309 | { |
| 310 | $url = get_edit_profile_url(); |
| 311 | |
| 312 | $page_id = ppress_settings_by_key('edit_user_profile_url'); |
| 313 | |
| 314 | if ( ! empty($page_id)) { |
| 315 | $url = get_permalink($page_id); |
| 316 | } |
| 317 | |
| 318 | return apply_filters('ppress_my_account_url', $url); |
| 319 | } |
| 320 | |
| 321 | /** |
| 322 | * Return ProfilePress password reset url. |
| 323 | * |
| 324 | * @return string |
| 325 | */ |
| 326 | function ppress_password_reset_url() |
| 327 | { |
| 328 | $url = wp_lostpassword_url(); |
| 329 | |
| 330 | $page_id = ppress_get_setting('set_lost_password_url'); |
| 331 | |
| 332 | if ( ! empty($page_id)) { |
| 333 | $url = get_permalink($page_id); |
| 334 | } |
| 335 | |
| 336 | return apply_filters('ppress_password_reset_url', $url); |
| 337 | } |
| 338 | |
| 339 | |
| 340 | /** |
| 341 | * Get ProfilePress login page URL or WP default login url if it isn't set. |
| 342 | * |
| 343 | * @param $redirect |
| 344 | * |
| 345 | * @return string |
| 346 | */ |
| 347 | function ppress_login_url($redirect = '') |
| 348 | { |
| 349 | $login_url = wp_login_url(); |
| 350 | |
| 351 | $login_page_id = ppress_get_setting('set_login_url'); |
| 352 | |
| 353 | if ( ! empty($login_page_id)) { |
| 354 | $login_url = get_permalink($login_page_id); |
| 355 | } |
| 356 | |
| 357 | if ( ! empty($redirect)) { |
| 358 | $login_url = add_query_arg('redirect_to', rawurlencode($redirect), $login_url); |
| 359 | } |
| 360 | |
| 361 | return apply_filters('ppress_login_url', $login_url); |
| 362 | } |
| 363 | |
| 364 | /** |
| 365 | * Get ProfilePress login page URL or WP default login url if it isn't set. |
| 366 | */ |
| 367 | function ppress_registration_url() |
| 368 | { |
| 369 | $reg_url = wp_registration_url(); |
| 370 | $page_id = ppress_get_setting('set_registration_url'); |
| 371 | |
| 372 | if ( ! empty($page_id)) { |
| 373 | $reg_url = get_permalink($page_id); |
| 374 | } |
| 375 | |
| 376 | return apply_filters('ppress_registration_url', $reg_url); |
| 377 | } |
| 378 | |
| 379 | /** |
| 380 | * Return the URL of the currently view page. |
| 381 | * |
| 382 | * @return string |
| 383 | */ |
| 384 | function ppress_get_current_url() |
| 385 | { |
| 386 | global $wp; |
| 387 | |
| 388 | return home_url(add_query_arg(array(), $wp->request)); |
| 389 | } |
| 390 | |
| 391 | |
| 392 | /** |
| 393 | * Return currently viewed page url without query string. |
| 394 | * |
| 395 | * @return string |
| 396 | */ |
| 397 | function ppress_get_current_url_raw() |
| 398 | { |
| 399 | $protocol = 'http://'; |
| 400 | |
| 401 | if ((isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] == 1)) |
| 402 | || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') |
| 403 | ) { |
| 404 | $protocol = 'https://'; |
| 405 | } |
| 406 | |
| 407 | return esc_url_raw($protocol . $_SERVER['HTTP_HOST'] . parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)); |
| 408 | } |
| 409 | |
| 410 | /** |
| 411 | * Return currently viewed page url with query string. |
| 412 | * |
| 413 | * @return string |
| 414 | */ |
| 415 | function ppress_get_current_url_query_string() |
| 416 | { |
| 417 | $protocol = 'http://'; |
| 418 | |
| 419 | if ((isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] == 1)) |
| 420 | || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') |
| 421 | ) { |
| 422 | $protocol = 'https://'; |
| 423 | } |
| 424 | |
| 425 | $url = $protocol . $_SERVER['HTTP_HOST']; |
| 426 | |
| 427 | $url .= $_SERVER['REQUEST_URI']; |
| 428 | |
| 429 | return esc_url_raw($url); |
| 430 | } |
| 431 | |
| 432 | |
| 433 | /** |
| 434 | * @return string blog URL without scheme |
| 435 | */ |
| 436 | function ppress_site_url_without_scheme() |
| 437 | { |
| 438 | $parsed_url = parse_url(home_url()); |
| 439 | |
| 440 | return $parsed_url['host']; |
| 441 | } |
| 442 | |
| 443 | /** |
| 444 | * Append an option to a select dropdown |
| 445 | * |
| 446 | * @param string $option option to add |
| 447 | * @param string $select select dropdown |
| 448 | * |
| 449 | * @return string |
| 450 | */ |
| 451 | function ppress_append_option_to_select($option, $select) |
| 452 | { |
| 453 | $regex = "/<select ([^<]*)>/"; |
| 454 | |
| 455 | preg_match($regex, $select, $matches); |
| 456 | $select_attr = ppress_var($matches, 1); |
| 457 | |
| 458 | $a = preg_split($regex, $select); |
| 459 | |
| 460 | $join = '<select ' . $select_attr . '>' . "\r\n"; |
| 461 | $join .= $option . ppress_var($a, 1, ''); |
| 462 | |
| 463 | return $join; |
| 464 | } |
| 465 | |
| 466 | /** |
| 467 | * Blog name or domain name if name doesn't exist |
| 468 | * |
| 469 | * @return string |
| 470 | */ |
| 471 | function ppress_site_title() |
| 472 | { |
| 473 | $blog_name = get_option('blogname'); |
| 474 | |
| 475 | return ! empty($blog_name) ? wp_specialchars_decode($blog_name, ENT_QUOTES) : str_replace( |
| 476 | array( |
| 477 | 'http://', |
| 478 | 'https://', |
| 479 | ), |
| 480 | '', |
| 481 | site_url() |
| 482 | ); |
| 483 | } |
| 484 | |
| 485 | |
| 486 | /** |
| 487 | * Check if an admin settings page is ProfilePress' |
| 488 | * |
| 489 | * @return bool |
| 490 | */ |
| 491 | function ppress_is_admin_page() |
| 492 | { |
| 493 | $pp_builder_pages = [ |
| 494 | PPRESS_SETTINGS_SLUG, |
| 495 | PPRESS_FORMS_SETTINGS_SLUG, |
| 496 | PPRESS_MEMBER_DIRECTORIES_SLUG, |
| 497 | PPRESS_CONTENT_PROTECTION_SETTINGS_SLUG, |
| 498 | PPRESS_EXTENSIONS_SETTINGS_SLUG |
| 499 | ]; |
| 500 | |
| 501 | return (isset($_GET['page']) && in_array($_GET['page'], $pp_builder_pages)) || |
| 502 | isset($_GET['tab']) && $_GET['tab'] == 'ppress_extensions'; |
| 503 | } |
| 504 | |
| 505 | |
| 506 | /** |
| 507 | * Return admin email |
| 508 | * |
| 509 | * @return string |
| 510 | */ |
| 511 | function ppress_admin_email() |
| 512 | { |
| 513 | return get_option('admin_email'); |
| 514 | } |
| 515 | |
| 516 | /** |
| 517 | * Checks whether the given user ID exists. |
| 518 | * |
| 519 | * @param string $user_id ID of user |
| 520 | * |
| 521 | * @return null|int The user's ID on success, and null on failure. |
| 522 | */ |
| 523 | function ppress_user_id_exist($user_id) |
| 524 | { |
| 525 | if ($user = get_user_by('id', $user_id)) { |
| 526 | return $user->ID; |
| 527 | } |
| 528 | |
| 529 | return null; |
| 530 | } |
| 531 | |
| 532 | /** |
| 533 | * Get a user's username by their ID |
| 534 | * |
| 535 | * @param int $user_id |
| 536 | * |
| 537 | * @return bool|string |
| 538 | */ |
| 539 | function ppress_get_username_by_id($user_id) |
| 540 | { |
| 541 | return ppress_var_obj(get_user_by('id', $user_id), 'user_login'); |
| 542 | } |
| 543 | |
| 544 | /** |
| 545 | * front-end profile slug. |
| 546 | * |
| 547 | * @return string |
| 548 | */ |
| 549 | function ppress_get_profile_slug() |
| 550 | { |
| 551 | return apply_filters('ppress_profile_slug', ppress_get_setting('set_user_profile_slug', 'profile', true)); |
| 552 | } |
| 553 | |
| 554 | /** |
| 555 | * Filter form field attributes for unofficial attributes. |
| 556 | * |
| 557 | * @param array $atts supplied shortcode attributes |
| 558 | * |
| 559 | * @return mixed |
| 560 | * |
| 561 | */ |
| 562 | function ppress_other_field_atts($atts) |
| 563 | { |
| 564 | if ( ! is_array($atts)) return $atts; |
| 565 | |
| 566 | $official_atts = array('name', 'class', 'id', 'value', 'title', 'required', 'placeholder', 'key', 'field_key', 'limit', 'options', 'checkbox_text', 'processing_label'); |
| 567 | |
| 568 | $other_atts = array(); |
| 569 | |
| 570 | foreach ($atts as $key => $value) { |
| 571 | if ( ! in_array($key, $official_atts)) { |
| 572 | $other_atts[$key] = $value; |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | $other_atts_html = ''; |
| 577 | |
| 578 | foreach ($other_atts as $key => $value) { |
| 579 | if ( ! empty($value)) { |
| 580 | $other_atts_html .= "$key=\"$value\" "; |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | return $other_atts_html; |
| 585 | } |
| 586 | |
| 587 | |
| 588 | /** |
| 589 | * Create an index.php file to prevent directory browsing. |
| 590 | * |
| 591 | * @param string $location folder path to create the file in. |
| 592 | */ |
| 593 | function ppress_create_index_file($location) |
| 594 | { |
| 595 | $content = "You are not allowed here!"; |
| 596 | $fp = fopen($location . "/index.php", "wb"); |
| 597 | fwrite($fp, $content); |
| 598 | fclose($fp); |
| 599 | } |
| 600 | |
| 601 | /** |
| 602 | * Get front-end do password reset form url. |
| 603 | * |
| 604 | * @param string $user_login |
| 605 | * @param string $key |
| 606 | * |
| 607 | * @return string |
| 608 | */ |
| 609 | function ppress_get_do_password_reset_url($user_login, $key) |
| 610 | { |
| 611 | $url = network_site_url("wp-login.php?action=rp&key=$key&login=" . rawurlencode($user_login), 'login'); |
| 612 | |
| 613 | $page_id = ppress_get_setting('set_lost_password_url'); |
| 614 | |
| 615 | if (apply_filters('ppress_front_end_do_password_reset', true) && ! empty($page_id)) { |
| 616 | |
| 617 | $url = add_query_arg( |
| 618 | array( |
| 619 | 'key' => $key, |
| 620 | 'login' => rawurlencode($user_login) |
| 621 | ), |
| 622 | ppress_password_reset_url() |
| 623 | ); |
| 624 | } |
| 625 | |
| 626 | return $url; |
| 627 | } |
| 628 | |
| 629 | /** |
| 630 | * Return true if a field key exist/is multi selectable dropdown. |
| 631 | * |
| 632 | * @param $field_key |
| 633 | * |
| 634 | * @return bool |
| 635 | */ |
| 636 | function ppress_is_select_field_multi_selectable($field_key) |
| 637 | { |
| 638 | $data = get_option('ppress_cpf_select_multi_selectable', array()); |
| 639 | |
| 640 | return array_key_exists($field_key, $data); |
| 641 | } |
| 642 | |
| 643 | |
| 644 | /** |
| 645 | * Return username/username of a user using the user's nicename to do the DB search. |
| 646 | * |
| 647 | * @param string $slug |
| 648 | * |
| 649 | * @return bool|null|string |
| 650 | */ |
| 651 | function ppress_is_slug_nice_name($slug) |
| 652 | { |
| 653 | global $wpdb; |
| 654 | |
| 655 | $response = $wpdb->get_var( |
| 656 | $wpdb->prepare( |
| 657 | "SELECT user_login FROM {$wpdb->prefix}users WHERE user_nicename = '%s'", |
| 658 | array($slug) |
| 659 | ) |
| 660 | ); |
| 661 | |
| 662 | // if response isn't null, the username/user_login is returned. |
| 663 | return is_null($response) ? false : $response; |
| 664 | } |
| 665 | |
| 666 | /** |
| 667 | * Return array of editable roles. |
| 668 | * |
| 669 | * @param $remove_admin |
| 670 | * |
| 671 | * @return mixed |
| 672 | */ |
| 673 | function ppress_get_editable_roles($remove_admin = true) |
| 674 | { |
| 675 | $all_roles = wp_roles()->roles; |
| 676 | |
| 677 | if (true == $remove_admin) { |
| 678 | unset($all_roles['administrator']); |
| 679 | } |
| 680 | |
| 681 | return $all_roles; |
| 682 | } |
| 683 | |
| 684 | function ppress_wp_roles_key_value($remove_admin = true) |
| 685 | { |
| 686 | $wp_roles = ppress_get_editable_roles($remove_admin); |
| 687 | |
| 688 | return array_reduce(array_keys($wp_roles), function ($carry, $item) use ($wp_roles) { |
| 689 | $carry[$item] = $wp_roles[$item]['name']; |
| 690 | |
| 691 | return $carry; |
| 692 | }, []); |
| 693 | } |
| 694 | |
| 695 | function ppress_wp_new_user_notification($user_id, $deprecated = null, $notify = '') |
| 696 | { |
| 697 | if (null !== $deprecated) { |
| 698 | _deprecated_argument(__FUNCTION__, '4.3.1'); |
| 699 | } |
| 700 | |
| 701 | // Accepts only 'user', 'admin' , 'both' or default '' as $notify. |
| 702 | if ( ! in_array($notify, array('user', 'admin', 'both', ''), true)) { |
| 703 | return; |
| 704 | } |
| 705 | |
| 706 | $new_user_notification = apply_filters('ppress_new_user_notification', 'enable'); |
| 707 | |
| 708 | if ('enable' != $new_user_notification) return; |
| 709 | |
| 710 | $user = get_userdata($user_id); |
| 711 | |
| 712 | // The blogname option is escaped with esc_html() on the way into the database in sanitize_option(). |
| 713 | // We want to reverse this for the plain text arena of emails. |
| 714 | $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES); |
| 715 | |
| 716 | if ('user' !== $notify) { |
| 717 | |
| 718 | if (ppress_get_setting('new_user_admin_email_email_enabled', 'on') !== 'on') return; |
| 719 | |
| 720 | $message = ppress_get_setting('new_user_admin_email_email_content', ppress_new_user_admin_notification_message_default(), true); |
| 721 | |
| 722 | $title = ppress_get_setting('new_user_admin_email_email_subject', sprintf(__('[%s] New User Registration'), $blogname), true); |
| 723 | |
| 724 | // handle support for custom fields placeholder. |
| 725 | preg_match_all('#({{[a-z_-]+}})#', $message, $matches); |
| 726 | |
| 727 | if (isset($matches[1]) && ! empty($matches[1])) { |
| 728 | |
| 729 | foreach ($matches[1] as $match) { |
| 730 | $key = str_replace(['{', '}'], '', $match); |
| 731 | |
| 732 | if (isset($user->{$key})) { |
| 733 | $value = $user->{$key}; |
| 734 | |
| 735 | if (is_array($value)) { |
| 736 | $value = implode(', ', $value); |
| 737 | } |
| 738 | |
| 739 | $message = str_replace($match, $value, $message); |
| 740 | } |
| 741 | } |
| 742 | } |
| 743 | |
| 744 | $search = array( |
| 745 | '{{username}}', |
| 746 | '{{user_email}}', |
| 747 | '{{site_title}}', |
| 748 | '{{first_name}}', |
| 749 | '{{last_name}}' |
| 750 | ); |
| 751 | |
| 752 | $replace = array( |
| 753 | $user->user_login, |
| 754 | $user->user_email, |
| 755 | $blogname, |
| 756 | $user->first_name, |
| 757 | $user->last_name |
| 758 | ); |
| 759 | |
| 760 | $message = htmlspecialchars_decode( |
| 761 | apply_filters( |
| 762 | 'ppress_signup_admin_email_message', |
| 763 | str_replace($search, $replace, $message), |
| 764 | $user |
| 765 | ) |
| 766 | ); |
| 767 | |
| 768 | $title = apply_filters( |
| 769 | 'ppress_signup_admin_email_subject', |
| 770 | str_replace($search, $replace, $title), |
| 771 | $user |
| 772 | ); |
| 773 | |
| 774 | $admin_email = apply_filters('ppress_signup_notification_admin_email', ppress_get_admin_notification_emails()); |
| 775 | |
| 776 | ppress_send_email($admin_email, $title, $message); |
| 777 | } |
| 778 | |
| 779 | // `$deprecated` was pre-4.3 `$plaintext_pass`. An empty `$plaintext_pass` didn't sent a user notification. |
| 780 | if ('admin' === $notify || (empty($deprecated) && empty($notify))) { |
| 781 | return; |
| 782 | } |
| 783 | |
| 784 | $key = get_password_reset_key($user); |
| 785 | if (is_wp_error($key)) { |
| 786 | return; |
| 787 | } |
| 788 | |
| 789 | $switched_locale = switch_to_locale(get_user_locale($user)); |
| 790 | |
| 791 | /* translators: %s: User login. */ |
| 792 | $message = sprintf(__('Username: %s'), $user->user_login) . "\r\n\r\n"; |
| 793 | $message .= __('To set your password, visit the following address:') . "\r\n\r\n"; |
| 794 | $message .= network_site_url("wp-login.php?action=rp&key=$key&login=" . rawurlencode($user->user_login), 'login') . "\r\n\r\n"; |
| 795 | |
| 796 | $message .= wp_login_url() . "\r\n"; |
| 797 | |
| 798 | $wp_new_user_notification_email = array( |
| 799 | 'to' => $user->user_email, |
| 800 | /* translators: Login details notification email subject. %s: Site title. */ |
| 801 | 'subject' => __('[%s] Login Details'), |
| 802 | 'message' => $message, |
| 803 | 'headers' => '', |
| 804 | ); |
| 805 | |
| 806 | /** |
| 807 | * Filters the contents of the new user notification email sent to the new user. |
| 808 | * |
| 809 | * @param array $wp_new_user_notification_email { |
| 810 | * Used to build wp_mail(). |
| 811 | * |
| 812 | * @type string $to The intended recipient - New user email address. |
| 813 | * @type string $subject The subject of the email. |
| 814 | * @type string $message The body of the email. |
| 815 | * @type string $headers The headers of the email. |
| 816 | * } |
| 817 | * |
| 818 | * @param WP_User $user User object for new user. |
| 819 | * @param string $blogname The site title. |
| 820 | * |
| 821 | * @since 4.9.0 |
| 822 | * |
| 823 | */ |
| 824 | $wp_new_user_notification_email = apply_filters('wp_new_user_notification_email', $wp_new_user_notification_email, $user, $blogname); |
| 825 | |
| 826 | wp_mail( |
| 827 | $wp_new_user_notification_email['to'], |
| 828 | wp_specialchars_decode(sprintf($wp_new_user_notification_email['subject'], $blogname)), |
| 829 | $wp_new_user_notification_email['message'], |
| 830 | $wp_new_user_notification_email['headers'] |
| 831 | ); |
| 832 | |
| 833 | if ($switched_locale) { |
| 834 | restore_previous_locale(); |
| 835 | } |
| 836 | } |
| 837 | |
| 838 | if ( ! function_exists('wp_new_user_notification')) : |
| 839 | /** |
| 840 | * Email login credentials to a newly-registered user. |
| 841 | * |
| 842 | * A new user registration notification is also sent to admin email. |
| 843 | * |
| 844 | * @param int $user_id User ID. |
| 845 | * @param null $deprecated Not used (argument deprecated). |
| 846 | * @param string $notify Optional. Type of notification that should happen. Accepts 'admin' or an empty |
| 847 | * string (admin only), 'user', or 'both' (admin and user). Default empty. |
| 848 | * |
| 849 | * @since 4.6.0 The `$notify` parameter accepts 'user' for sending notification only to the user created. |
| 850 | * |
| 851 | * @global wpdb $wpdb WordPress database object for queries. |
| 852 | * @global PasswordHash $wp_hasher Portable PHP password hashing framework instance. |
| 853 | * |
| 854 | * @since 2.0.0 |
| 855 | * @since 4.3.0 The `$plaintext_pass` parameter was changed to `$notify`. |
| 856 | * @since 4.3.1 The `$plaintext_pass` parameter was deprecated. `$notify` added as a third parameter. |
| 857 | */ |
| 858 | function wp_new_user_notification($user_id, $deprecated = null, $notify = '') |
| 859 | { |
| 860 | return ppress_wp_new_user_notification($user_id, $deprecated, $notify); |
| 861 | } |
| 862 | endif; |
| 863 | |
| 864 | /** |
| 865 | * Does registration form has username requirement disabled? |
| 866 | * |
| 867 | * @param int $form_id |
| 868 | * @param bool $is_melange |
| 869 | * |
| 870 | * @return bool |
| 871 | */ |
| 872 | function ppress_is_signup_form_username_disabled($form_id, $is_melange = false) |
| 873 | { |
| 874 | $result = FR::get_form_meta($form_id, FR::REGISTRATION_TYPE, FR::DISABLE_USERNAME_REQUIREMENT); |
| 875 | |
| 876 | if ($is_melange === true) { |
| 877 | $result = FR::get_form_meta($form_id, FR::MELANGE_TYPE, FR::DISABLE_USERNAME_REQUIREMENT); |
| 878 | } |
| 879 | |
| 880 | if (is_string($result)) { |
| 881 | $result = $result == 'true' ? true : false; |
| 882 | } |
| 883 | |
| 884 | return $result; |
| 885 | } |
| 886 | |
| 887 | /** |
| 888 | * Generate url to reset user's password. |
| 889 | * |
| 890 | * @param string $user_login |
| 891 | * |
| 892 | * @return string |
| 893 | */ |
| 894 | function ppress_generate_password_reset_url($user_login) |
| 895 | { |
| 896 | $user = get_user_by('login', $user_login); |
| 897 | |
| 898 | $key = get_password_reset_key($user); |
| 899 | |
| 900 | return ppress_get_do_password_reset_url($user_login, $key); |
| 901 | } |
| 902 | |
| 903 | function ppress_nonce_action_string() |
| 904 | { |
| 905 | return 'pp_plugin_nonce'; |
| 906 | } |
| 907 | |
| 908 | /** |
| 909 | * Return array of countries. |
| 910 | * |
| 911 | * @return mixed |
| 912 | */ |
| 913 | function ppress_array_of_world_countries() |
| 914 | { |
| 915 | return apply_filters('ppress_countries_custom_field_data', include(PROFILEPRESS_SRC . 'Functions/data/countries.php')); |
| 916 | } |
| 917 | |
| 918 | function ppress_create_nonce() |
| 919 | { |
| 920 | return wp_create_nonce(ppress_nonce_action_string()); |
| 921 | } |
| 922 | |
| 923 | function ppress_nonce_field() |
| 924 | { |
| 925 | return wp_nonce_field(ppress_nonce_action_string(), '_wpnonce', true, false); |
| 926 | } |
| 927 | |
| 928 | function ppress_verify_nonce() |
| 929 | { |
| 930 | return check_admin_referer(ppress_nonce_action_string()); |
| 931 | } |
| 932 | |
| 933 | function ppress_verify_ajax_nonce() |
| 934 | { |
| 935 | return check_ajax_referer(ppress_nonce_action_string()); |
| 936 | } |
| 937 | |
| 938 | /** |
| 939 | * Returns a more compact md5 hashing. |
| 940 | * |
| 941 | * @param $string |
| 942 | * |
| 943 | * @return false|string |
| 944 | */ |
| 945 | function ppress_md5($string) |
| 946 | { |
| 947 | return substr(base_convert(md5($string), 16, 32), 0, 12); |
| 948 | } |
| 949 | |
| 950 | function ppress_minify_css($buffer) |
| 951 | { |
| 952 | $buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer); |
| 953 | $buffer = str_replace(': ', ':', $buffer); |
| 954 | $buffer = str_replace(array("\r\n", "\r", "\n", "\t", ' ', ' ', ' '), '', $buffer); |
| 955 | |
| 956 | return $buffer; |
| 957 | } |
| 958 | |
| 959 | function ppress_get_ip_address() |
| 960 | { |
| 961 | $ip = '127.0.0.1'; |
| 962 | |
| 963 | if ( ! empty($_SERVER['HTTP_CLIENT_IP'])) { |
| 964 | $ip = $_SERVER['HTTP_CLIENT_IP']; |
| 965 | } elseif ( ! empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { |
| 966 | $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; |
| 967 | } elseif ( ! empty($_SERVER['REMOTE_ADDR'])) { |
| 968 | $ip = $_SERVER['REMOTE_ADDR']; |
| 969 | } |
| 970 | |
| 971 | // Fix potential CSV returned from $_SERVER variables |
| 972 | $ip_array = array_map('trim', explode(',', $ip)); |
| 973 | |
| 974 | return $ip_array[0] != '::1' ? $ip_array[0] : ''; |
| 975 | } |
| 976 | |
| 977 | /** |
| 978 | * Admin email address to receive admin notification. |
| 979 | * |
| 980 | * @return mixed |
| 981 | */ |
| 982 | function ppress_get_admin_notification_emails() |
| 983 | { |
| 984 | return ppress_get_setting('admin_email_addresses', ppress_admin_email(), true); |
| 985 | } |
| 986 | |
| 987 | function ppress_get_error_log($type = 'debug') |
| 988 | { |
| 989 | $log_file = PPRESS_ERROR_LOG_FOLDER . $type . '.log'; |
| 990 | |
| 991 | $file_contents = ''; |
| 992 | |
| 993 | if (file_exists($log_file)) { |
| 994 | $file_contents = @file_get_contents($log_file); |
| 995 | } |
| 996 | |
| 997 | return $file_contents; |
| 998 | } |
| 999 | |
| 1000 | function ppress_log_error($message, $type = 'debug') |
| 1001 | { |
| 1002 | $log_folder = PPRESS_ERROR_LOG_FOLDER; |
| 1003 | |
| 1004 | // does bugs folder exist? if NO, create it. |
| 1005 | if ( ! file_exists($log_folder)) { |
| 1006 | mkdir($log_folder, 0755, true); |
| 1007 | } |
| 1008 | |
| 1009 | if ( ! file_exists(PPRESS_ERROR_LOG_FOLDER . '/index.php')) { |
| 1010 | ppress_create_index_file(PPRESS_ERROR_LOG_FOLDER); |
| 1011 | } |
| 1012 | |
| 1013 | $message = current_time('mysql') . ' - ' . $message . "\r\n\r\n"; |
| 1014 | |
| 1015 | error_log($message, 3, "{$log_folder}{$type}.log"); |
| 1016 | } |
| 1017 | |
| 1018 | function ppress_clear_error_log($type = 'debug') |
| 1019 | { |
| 1020 | return @unlink(PPRESS_ERROR_LOG_FOLDER . $type . '.log'); |
| 1021 | } |
| 1022 | |
| 1023 | function ppressPOST_var($key, $default = false, $empty = false, $bucket = false) |
| 1024 | { |
| 1025 | $bucket = ! $bucket ? $_POST : $bucket; |
| 1026 | |
| 1027 | if ($empty) { |
| 1028 | return ! empty($bucket[$key]) ? $bucket[$key] : $default; |
| 1029 | } |
| 1030 | |
| 1031 | return isset($bucket[$key]) ? $bucket[$key] : $default; |
| 1032 | } |
| 1033 | |
| 1034 | function ppressGET_var($key, $default = false, $empty = false) |
| 1035 | { |
| 1036 | $bucket = $_GET; |
| 1037 | |
| 1038 | if ($empty) { |
| 1039 | return ! empty($bucket[$key]) ? $bucket[$key] : $default; |
| 1040 | } |
| 1041 | |
| 1042 | return isset($bucket[$key]) ? $bucket[$key] : $default; |
| 1043 | } |
| 1044 | |
| 1045 | function ppress_var($bucket, $key, $default = false, $empty = false) |
| 1046 | { |
| 1047 | if ($empty) { |
| 1048 | return ! empty($bucket[$key]) ? $bucket[$key] : $default; |
| 1049 | } |
| 1050 | |
| 1051 | return isset($bucket[$key]) ? $bucket[$key] : $default; |
| 1052 | } |
| 1053 | |
| 1054 | function ppress_var_obj($bucket, $key, $default = false, $empty = false) |
| 1055 | { |
| 1056 | if ($empty) { |
| 1057 | return ! empty($bucket->$key) ? $bucket->$key : $default; |
| 1058 | } |
| 1059 | |
| 1060 | return isset($bucket->$key) ? $bucket->$key : $default; |
| 1061 | } |
| 1062 | |
| 1063 | /** |
| 1064 | * Normalize unamed shortcode |
| 1065 | * |
| 1066 | * @param array $atts |
| 1067 | * |
| 1068 | * @return mixed |
| 1069 | */ |
| 1070 | function ppress_normalize_attributes($atts) |
| 1071 | { |
| 1072 | if (is_array($atts)) { |
| 1073 | foreach ($atts as $key => $value) { |
| 1074 | if (is_int($key)) { |
| 1075 | $atts[$value] = true; |
| 1076 | unset($atts[$key]); |
| 1077 | } |
| 1078 | } |
| 1079 | } |
| 1080 | |
| 1081 | return $atts; |
| 1082 | } |
| 1083 | |
| 1084 | /** @todo link to doc on reserve words/text here */ |
| 1085 | function ppress_dnd_field_key_description() |
| 1086 | { |
| 1087 | 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'); |
| 1088 | } |
| 1089 | |
| 1090 | function ppress_reserved_field_keys() |
| 1091 | { |
| 1092 | return [ |
| 1093 | 'ID', 'id', 'user_pass', 'user_login', 'user_nicename', 'user_url', 'user_email', 'display_name', 'nickname', |
| 1094 | 'first_name', 'last_name', 'description', 'rich_editing', 'syntax_highlighting', 'comment_shortcuts', 'admin_color', |
| 1095 | 'use_ssl', 'user_registered', 'user_activation_key', 'spam', 'show_admin_bar_front', 'role', 'locale', 'deleted', 'user_level', |
| 1096 | 'user_status', 'user_description' |
| 1097 | ]; |
| 1098 | } |
| 1099 | |
| 1100 | function ppress_is_boolean($maybe_bool) |
| 1101 | { |
| 1102 | if (is_bool($maybe_bool)) { |
| 1103 | return true; |
| 1104 | } |
| 1105 | |
| 1106 | if (is_string($maybe_bool)) { |
| 1107 | $maybe_bool = strtolower($maybe_bool); |
| 1108 | |
| 1109 | $valid_boolean_values = array( |
| 1110 | 'false', |
| 1111 | 'true', |
| 1112 | '0', |
| 1113 | '1', |
| 1114 | ); |
| 1115 | |
| 1116 | return in_array($maybe_bool, $valid_boolean_values, true); |
| 1117 | } |
| 1118 | |
| 1119 | if (is_int($maybe_bool)) { |
| 1120 | return in_array($maybe_bool, array(0, 1), true); |
| 1121 | } |
| 1122 | |
| 1123 | return false; |
| 1124 | } |
| 1125 | |
| 1126 | function ppress_filter_empty_array($values) |
| 1127 | { |
| 1128 | return array_filter($values, function ($value) { |
| 1129 | return ppress_is_boolean($value) || is_int($value) || ! empty($value); |
| 1130 | }); |
| 1131 | } |
| 1132 | |
| 1133 | /** |
| 1134 | * Check if HTTP status code is successful. |
| 1135 | * |
| 1136 | * @param int $code |
| 1137 | * |
| 1138 | * @return bool |
| 1139 | */ |
| 1140 | function ppress_is_http_code_success($code) |
| 1141 | { |
| 1142 | $code = absint($code); |
| 1143 | |
| 1144 | return $code >= 200 && $code <= 299; |
| 1145 | } |
| 1146 | |
| 1147 | /** |
| 1148 | * strtotime uses the default timezone set in PHP which may or may not be UTC. |
| 1149 | * |
| 1150 | * @param $time |
| 1151 | * |
| 1152 | * @param null|int $now |
| 1153 | * |
| 1154 | * @return false|int |
| 1155 | */ |
| 1156 | function ppress_strtotime_utc($time, $now = null) |
| 1157 | { |
| 1158 | if (is_null($now)) $now = time(); |
| 1159 | |
| 1160 | $old = date_default_timezone_get(); |
| 1161 | |
| 1162 | date_default_timezone_set('UTC'); |
| 1163 | $val = strtotime($time, $now); |
| 1164 | |
| 1165 | date_default_timezone_set($old); |
| 1166 | |
| 1167 | return $val; |
| 1168 | } |
| 1169 | |
| 1170 | function ppress_array_flatten($array) |
| 1171 | { |
| 1172 | if ( ! is_array($array)) { |
| 1173 | return false; |
| 1174 | } |
| 1175 | $result = array(); |
| 1176 | foreach ($array as $key => $value) { |
| 1177 | if (is_array($value)) { |
| 1178 | // we are not doing array_merge here because we wanna keep array keys. |
| 1179 | // PS: The + operator is not an addition, it's a union. If the keys don't overlap then all is good. |
| 1180 | $result = $result + ppress_array_flatten($value); |
| 1181 | } else { |
| 1182 | $result[$key] = $value; |
| 1183 | } |
| 1184 | } |
| 1185 | |
| 1186 | return $result; |
| 1187 | } |
| 1188 | |
| 1189 | /** |
| 1190 | * Sanitizes a string key. |
| 1191 | * |
| 1192 | * Keys are used as internal identifiers. Lowercase alphanumeric characters and underscores are allowed. |
| 1193 | * |
| 1194 | * @param string $key String key |
| 1195 | * |
| 1196 | * @return string Sanitized key |
| 1197 | */ |
| 1198 | function ppress_sanitize_key($key) |
| 1199 | { |
| 1200 | return str_replace('-', '_', sanitize_key($key)); |
| 1201 | } |
| 1202 | |
| 1203 | function ppress_woocommerce_field_transform($cf_key, $cf_value) |
| 1204 | { |
| 1205 | if (class_exists('WooCommerce')) { |
| 1206 | |
| 1207 | if (in_array($cf_key, ppress_woocommerce_billing_fields())) { |
| 1208 | $cf_value = sprintf(esc_html__('%s (WooCommerce Billing Address)', 'wp-user-avatar'), $cf_value); |
| 1209 | } |
| 1210 | |
| 1211 | if (in_array($cf_key, ppress_woocommerce_shipping_fields())) { |
| 1212 | $cf_value = sprintf(esc_html__('%s (WooCommerce Shipping Address)', 'wp-user-avatar'), $cf_value); |
| 1213 | } |
| 1214 | } |
| 1215 | |
| 1216 | return $cf_value; |
| 1217 | } |
| 1218 | |
| 1219 | function ppress_custom_fields_key_value_pair($remove_default = false) |
| 1220 | { |
| 1221 | $defined_custom_fields = []; |
| 1222 | |
| 1223 | if ($remove_default === false) { |
| 1224 | $defined_custom_fields[''] = esc_html__('Select...', 'wp-user-avatar'); |
| 1225 | } |
| 1226 | |
| 1227 | $db_custom_fields = PROFILEPRESS_sql::get_profile_custom_fields(); |
| 1228 | $db_contact_infos = PROFILEPRESS_sql::get_contact_info_fields(); |
| 1229 | |
| 1230 | if ( ! empty($db_contact_infos)) { |
| 1231 | foreach ($db_contact_infos as $key => $value) { |
| 1232 | $defined_custom_fields[$key] = $value; |
| 1233 | } |
| 1234 | } |
| 1235 | |
| 1236 | if ( ! empty($db_custom_fields)) { |
| 1237 | foreach ($db_custom_fields as $db_custom_field) { |
| 1238 | $field_key = $db_custom_field['field_key']; |
| 1239 | $defined_custom_fields[$field_key] = ppress_woocommerce_field_transform($field_key, $db_custom_field['label_name']); |
| 1240 | } |
| 1241 | } |
| 1242 | |
| 1243 | return $defined_custom_fields; |
| 1244 | } |
| 1245 | |
| 1246 | function ppress_standard_fields_key_value_pair($remove_default = false) |
| 1247 | { |
| 1248 | $fields = []; |
| 1249 | if ($remove_default === false) { |
| 1250 | $fields[''] = esc_html__('Select...', 'wp-user-avatar'); |
| 1251 | } |
| 1252 | |
| 1253 | return array_merge($fields, [ |
| 1254 | 'first_last_names' => esc_html__('First and Last Names', 'wp-user-avatar'), |
| 1255 | 'last_first_names' => esc_html__('Last and First Names', 'wp-user-avatar'), |
| 1256 | 'username' => esc_html__('Username', 'wp-user-avatar'), |
| 1257 | 'first-name' => esc_html__('First Name', 'wp-user-avatar'), |
| 1258 | 'last-name' => esc_html__('Last Name', 'wp-user-avatar'), |
| 1259 | 'nickname' => esc_html__('Nickname', 'wp-user-avatar'), |
| 1260 | 'display-name' => esc_html__('Display Name', 'wp-user-avatar'), |
| 1261 | 'email' => esc_html__('Email Address', 'wp-user-avatar'), |
| 1262 | 'bio' => esc_html__('Biography', 'wp-user-avatar'), |
| 1263 | 'registration_date' => esc_html__('Registration Date', 'wp-user-avatar'), |
| 1264 | ]); |
| 1265 | } |
| 1266 | |
| 1267 | function ppress_standard_custom_fields_key_value_pair($remove_default = false) |
| 1268 | { |
| 1269 | $fields = []; |
| 1270 | |
| 1271 | if ($remove_default === false) { |
| 1272 | $fields[''] = esc_html__('Select...', 'wp-user-avatar'); |
| 1273 | } |
| 1274 | |
| 1275 | $fields[esc_html__('Standard Fields', 'wp-user-avatar')] = ppress_standard_fields_key_value_pair(true); |
| 1276 | |
| 1277 | if (EM::is_enabled(EM::CUSTOM_FIELDS)) { |
| 1278 | $fields[esc_html__('Custom Fields', 'wp-user-avatar')] = ppress_custom_fields_key_value_pair(true); |
| 1279 | } |
| 1280 | |
| 1281 | return $fields; |
| 1282 | } |
| 1283 | |
| 1284 | /** |
| 1285 | * @param int|bool $user_id |
| 1286 | * |
| 1287 | * @return bool |
| 1288 | */ |
| 1289 | function ppress_user_has_cover_image($user_id = false) |
| 1290 | { |
| 1291 | $user_id = ! $user_id ? get_current_user_id() : $user_id; |
| 1292 | |
| 1293 | $cover = get_user_meta($user_id, 'pp_profile_cover_image', true); |
| 1294 | |
| 1295 | return ! empty($cover); |
| 1296 | } |
| 1297 | |
| 1298 | |
| 1299 | /** |
| 1300 | * @param int|bool $user_id |
| 1301 | * |
| 1302 | * @return string|bool |
| 1303 | */ |
| 1304 | function ppress_get_cover_image_url($user_id = false) |
| 1305 | { |
| 1306 | $user_id = ! $user_id ? get_current_user_id() : $user_id; |
| 1307 | |
| 1308 | $slug = get_user_meta($user_id, 'pp_profile_cover_image', true); |
| 1309 | |
| 1310 | if ( ! empty($slug)) { |
| 1311 | return PPRESS_COVER_IMAGE_UPLOAD_URL . "$slug"; |
| 1312 | } |
| 1313 | |
| 1314 | return get_option('wp_user_cover_default_image_url'); |
| 1315 | } |
| 1316 | |
| 1317 | function ppress_is_my_own_profile() |
| 1318 | { |
| 1319 | global $ppress_frontend_profile_user_obj; |
| 1320 | |
| 1321 | return ppress_var_obj($ppress_frontend_profile_user_obj, 'ID') == get_current_user_id(); |
| 1322 | } |
| 1323 | |
| 1324 | function ppress_social_network_fields() |
| 1325 | { |
| 1326 | return apply_filters('ppress_core_contact_info_fields', [ |
| 1327 | Base::cif_facebook => 'Facebook', |
| 1328 | Base::cif_twitter => 'Twitter', |
| 1329 | Base::cif_linkedin => 'LinkedIn', |
| 1330 | Base::cif_vk => 'VK', |
| 1331 | Base::cif_youtube => 'YouTube', |
| 1332 | Base::cif_instagram => 'Instagram', |
| 1333 | Base::cif_github => 'GitHub', |
| 1334 | ]); |
| 1335 | } |
| 1336 | |
| 1337 | function ppress_mb_function($function_names, $args) |
| 1338 | { |
| 1339 | $mb_function_name = $function_names[0]; |
| 1340 | $function_name = $function_names[1]; |
| 1341 | if (function_exists($mb_function_name)) { |
| 1342 | $function_name = $mb_function_name; |
| 1343 | } |
| 1344 | |
| 1345 | return call_user_func_array($function_name, $args); |
| 1346 | } |
| 1347 | |
| 1348 | function ppress_recursive_trim($item) |
| 1349 | { |
| 1350 | if (is_array($item)) { |
| 1351 | |
| 1352 | $sanitized_data = []; |
| 1353 | foreach ($item as $key => $value) { |
| 1354 | $sanitized_data[$key] = ppress_recursive_trim($value); |
| 1355 | } |
| 1356 | |
| 1357 | return $sanitized_data; |
| 1358 | } |
| 1359 | |
| 1360 | return trim($item); |
| 1361 | } |