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