GlobalFunctions.php
| 1 | <?php |
| 2 | |
| 3 | use ProfilePress\Core\Admin\SettingsPages\MailOptin; |
| 4 | use ProfilePress\Core\Base; |
| 5 | use ProfilePress\Core\Classes\ExtensionManager as EM; |
| 6 | use ProfilePress\Core\Classes\FormRepository as FR; |
| 7 | use ProfilePress\Core\Classes\PROFILEPRESS_sql; |
| 8 | use ProfilePress\Core\Classes\SendEmail; |
| 9 | use ProfilePress\Core\Membership\CheckoutFields; |
| 10 | use ProfilePress\Core\ShortcodeParser\Builder\FrontendProfileBuilder; |
| 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 $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 | $reset_url = untrailingslashit(ppress_password_reset_url()); |
| 224 | $redirect = strstr($referrer_url, $reset_url) ? ppress_my_account_url() : $referrer_url; |
| 225 | } elseif ('current_page' == $login_redirect) { |
| 226 | // in ajax mode, pp_current_url is set so we can do client-side redirection to current page after login. |
| 227 | // no way to get current url in social login hence, look it up from $_GET['pp_current_url'] |
| 228 | if ( ! empty($_GET['pp_current_url'])) { |
| 229 | $redirect = rawurldecode($_GET['pp_current_url']); |
| 230 | } elseif ( ! empty($_POST['pp_current_url'])) { |
| 231 | $redirect = rawurldecode($_POST['pp_current_url']); |
| 232 | } else { |
| 233 | $redirect = ppress_get_current_url_raw(); |
| 234 | } |
| 235 | } elseif ( ! empty($login_redirect) && is_numeric($login_redirect)) { |
| 236 | $redirect = get_permalink($login_redirect); |
| 237 | } else { |
| 238 | $redirect = admin_url(); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | return wp_validate_redirect(apply_filters('ppress_core_login_redirect', $redirect)); |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * Return the url to redirect to after successful reset / change of password. |
| 247 | * |
| 248 | * @return bool|string |
| 249 | */ |
| 250 | function ppress_password_reset_redirect() |
| 251 | { |
| 252 | $password_reset_redirect = ppress_get_setting('set_password_reset_redirect'); |
| 253 | $custom_url_password_reset_redirect = ppress_get_setting('custom_url_password_reset_redirect'); |
| 254 | |
| 255 | if ( ! empty($custom_url_password_reset_redirect)) { |
| 256 | $redirect = $custom_url_password_reset_redirect; |
| 257 | } elseif ( ! empty($password_reset_redirect)) { |
| 258 | $redirect = get_permalink($password_reset_redirect); |
| 259 | if ($password_reset_redirect == 'no_redirect') { |
| 260 | $redirect = ppress_password_reset_url() . '?password=changed'; |
| 261 | } |
| 262 | } else { |
| 263 | $redirect = ppress_password_reset_url() . '?password=changed'; |
| 264 | } |
| 265 | |
| 266 | return apply_filters('ppress_do_password_reset_redirect', esc_url_raw($redirect)); |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * Return the url to frontend myprofile page. |
| 271 | * |
| 272 | * @return bool|string |
| 273 | */ |
| 274 | function ppress_profile_url() |
| 275 | { |
| 276 | $url = admin_url('profile.php'); |
| 277 | |
| 278 | $page_id = ppress_get_setting('set_user_profile_shortcode'); |
| 279 | |
| 280 | if ( ! empty($page_id)) { |
| 281 | $url = get_permalink($page_id); |
| 282 | } |
| 283 | |
| 284 | return apply_filters('ppress_profile_url', $url); |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * @param $username_or_id |
| 289 | * |
| 290 | * @return string |
| 291 | */ |
| 292 | function ppress_get_frontend_profile_url($username_or_id) |
| 293 | { |
| 294 | if (is_numeric($username_or_id)) { |
| 295 | $username_or_id = ppress_get_username_by_id($username_or_id); |
| 296 | } |
| 297 | |
| 298 | return apply_filters( |
| 299 | 'ppress_frontend_profile_url', |
| 300 | home_url(ppress_get_profile_slug() . '/' . rawurlencode($username_or_id)), |
| 301 | $username_or_id |
| 302 | ); |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * Return ProfilePress edit profile page URL or WP default profile URL as fallback |
| 307 | * |
| 308 | * @return bool|string |
| 309 | */ |
| 310 | function ppress_edit_profile_url() |
| 311 | { |
| 312 | return apply_filters('ppress_edit_profile_url', ppress_my_account_url()); |
| 313 | } |
| 314 | |
| 315 | function ppress_my_account_url() |
| 316 | { |
| 317 | $url = get_edit_profile_url(); |
| 318 | |
| 319 | $page_id = ppress_settings_by_key('edit_user_profile_url'); |
| 320 | |
| 321 | if ( ! empty($page_id) && get_post_status($page_id)) { |
| 322 | $url = get_permalink($page_id); |
| 323 | } |
| 324 | |
| 325 | return apply_filters('ppress_my_account_url', $url); |
| 326 | } |
| 327 | |
| 328 | /** |
| 329 | * Return ProfilePress password reset url. |
| 330 | * |
| 331 | * @return string |
| 332 | */ |
| 333 | function ppress_password_reset_url() |
| 334 | { |
| 335 | $url = wp_lostpassword_url(); |
| 336 | |
| 337 | $page_id = ppress_get_setting('set_lost_password_url'); |
| 338 | |
| 339 | if ( ! empty($page_id) && get_post_status($page_id)) { |
| 340 | $url = get_permalink($page_id); |
| 341 | } |
| 342 | |
| 343 | return apply_filters('ppress_password_reset_url', $url); |
| 344 | } |
| 345 | |
| 346 | |
| 347 | /** |
| 348 | * Get ProfilePress login page URL or WP default login url if it isn't set. |
| 349 | * |
| 350 | * @param $redirect |
| 351 | * |
| 352 | * @return string |
| 353 | */ |
| 354 | function ppress_login_url($redirect = '') |
| 355 | { |
| 356 | $login_url = wp_login_url(); |
| 357 | |
| 358 | $login_page_id = ppress_get_setting('set_login_url'); |
| 359 | |
| 360 | if ( ! empty($login_page_id) && get_post_status($login_page_id)) { |
| 361 | $login_url = get_permalink($login_page_id); |
| 362 | } |
| 363 | |
| 364 | if ( ! empty($redirect)) { |
| 365 | $login_url = add_query_arg('redirect_to', rawurlencode(wp_validate_redirect($redirect)), $login_url); |
| 366 | } |
| 367 | |
| 368 | return apply_filters('ppress_login_url', $login_url); |
| 369 | } |
| 370 | |
| 371 | /** |
| 372 | * Get ProfilePress login page URL or WP default login url if it isn't set. |
| 373 | */ |
| 374 | function ppress_registration_url() |
| 375 | { |
| 376 | $page_id = ppress_get_setting('set_registration_url'); |
| 377 | |
| 378 | if ( ! empty($page_id) && get_post_status($page_id)) { |
| 379 | $reg_url = get_permalink($page_id); |
| 380 | } else { |
| 381 | $reg_url = wp_registration_url(); |
| 382 | } |
| 383 | |
| 384 | return apply_filters('ppress_registration_url', $reg_url); |
| 385 | } |
| 386 | |
| 387 | /** |
| 388 | * Return the URL of the currently view page. |
| 389 | * |
| 390 | * @return string |
| 391 | */ |
| 392 | function ppress_get_current_url() |
| 393 | { |
| 394 | global $wp; |
| 395 | |
| 396 | return home_url(add_query_arg(array(), $wp->request)); |
| 397 | } |
| 398 | |
| 399 | |
| 400 | /** |
| 401 | * Return currently viewed page url without query string. |
| 402 | * |
| 403 | * @return string |
| 404 | */ |
| 405 | function ppress_get_current_url_raw() |
| 406 | { |
| 407 | $protocol = 'http://'; |
| 408 | |
| 409 | if ((isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] == 1)) |
| 410 | || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') |
| 411 | ) { |
| 412 | $protocol = 'https://'; |
| 413 | } |
| 414 | |
| 415 | return esc_url_raw($protocol . $_SERVER['HTTP_HOST'] . parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)); |
| 416 | } |
| 417 | |
| 418 | /** |
| 419 | * Return currently viewed page url with query string. |
| 420 | * |
| 421 | * @return string |
| 422 | */ |
| 423 | function ppress_get_current_url_query_string() |
| 424 | { |
| 425 | $protocol = 'http://'; |
| 426 | |
| 427 | if ((isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] == 1)) |
| 428 | || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') |
| 429 | ) { |
| 430 | $protocol = 'https://'; |
| 431 | } |
| 432 | |
| 433 | $url = $protocol . $_SERVER['HTTP_HOST']; |
| 434 | |
| 435 | $url .= $_SERVER['REQUEST_URI']; |
| 436 | |
| 437 | return esc_url_raw($url); |
| 438 | } |
| 439 | |
| 440 | /** |
| 441 | * @return string blog URL without scheme |
| 442 | */ |
| 443 | function ppress_site_url_without_scheme() |
| 444 | { |
| 445 | $parsed_url = parse_url(home_url()); |
| 446 | |
| 447 | return $parsed_url['host']; |
| 448 | } |
| 449 | |
| 450 | /** |
| 451 | * Append an option to a select dropdown |
| 452 | * |
| 453 | * @param string $option option to add |
| 454 | * @param string $select select dropdown |
| 455 | * |
| 456 | * @return string |
| 457 | */ |
| 458 | function ppress_append_option_to_select($option, $select) |
| 459 | { |
| 460 | $regex = "/<select ([^<]*)>/"; |
| 461 | |
| 462 | preg_match($regex, $select, $matches); |
| 463 | $select_attr = ppress_var($matches, 1); |
| 464 | |
| 465 | $a = preg_split($regex, $select); |
| 466 | |
| 467 | $join = '<select ' . $select_attr . '>' . "\r\n"; |
| 468 | $join .= $option . ppress_var($a, 1, ''); |
| 469 | |
| 470 | return $join; |
| 471 | } |
| 472 | |
| 473 | /** |
| 474 | * Blog name or domain name if name doesn't exist |
| 475 | * |
| 476 | * @return string |
| 477 | */ |
| 478 | function ppress_site_title() |
| 479 | { |
| 480 | $blog_name = get_option('blogname'); |
| 481 | |
| 482 | return ! empty($blog_name) ? wp_specialchars_decode($blog_name, ENT_QUOTES) : str_replace( |
| 483 | array( |
| 484 | 'http://', |
| 485 | 'https://', |
| 486 | ), |
| 487 | '', |
| 488 | site_url() |
| 489 | ); |
| 490 | } |
| 491 | |
| 492 | |
| 493 | /** |
| 494 | * Check if an admin settings page is ProfilePress' |
| 495 | * |
| 496 | * @return bool |
| 497 | */ |
| 498 | function ppress_is_admin_page() |
| 499 | { |
| 500 | $pp_builder_pages = [ |
| 501 | PPRESS_SETTINGS_SLUG, |
| 502 | PPRESS_MEMBERSHIP_ORDERS_SETTINGS_SLUG, |
| 503 | PPRESS_MEMBERSHIP_SUBSCRIPTIONS_SETTINGS_SLUG, |
| 504 | PPRESS_MEMBERSHIP_PLANS_SETTINGS_SLUG, |
| 505 | PPRESS_MEMBERSHIP_CUSTOMERS_SETTINGS_SLUG, |
| 506 | PPRESS_FORMS_SETTINGS_SLUG, |
| 507 | PPRESS_MEMBER_DIRECTORIES_SLUG, |
| 508 | PPRESS_CONTENT_PROTECTION_SETTINGS_SLUG, |
| 509 | PPRESS_EXTENSIONS_SETTINGS_SLUG, |
| 510 | PPRESS_DASHBOARD_SETTINGS_SLUG, |
| 511 | MailOptin::SLUG |
| 512 | ]; |
| 513 | |
| 514 | return (isset($_GET['page']) && in_array($_GET['page'], $pp_builder_pages)) || |
| 515 | isset($_GET['tab']) && $_GET['tab'] == 'ppress_extensions'; |
| 516 | } |
| 517 | |
| 518 | |
| 519 | /** |
| 520 | * Return admin email |
| 521 | * |
| 522 | * @return string |
| 523 | */ |
| 524 | function ppress_admin_email() |
| 525 | { |
| 526 | return get_option('admin_email'); |
| 527 | } |
| 528 | |
| 529 | /** |
| 530 | * Checks whether the given user ID exists. |
| 531 | * |
| 532 | * @param string $user_id ID of user |
| 533 | * |
| 534 | * @return null|int The user's ID on success, and null on failure. |
| 535 | */ |
| 536 | function ppress_user_id_exist($user_id) |
| 537 | { |
| 538 | if ($user = get_user_by('id', $user_id)) { |
| 539 | return $user->ID; |
| 540 | } |
| 541 | |
| 542 | return null; |
| 543 | } |
| 544 | |
| 545 | /** |
| 546 | * Get a user's username by their ID |
| 547 | * |
| 548 | * @param int $user_id |
| 549 | * |
| 550 | * @return bool|string |
| 551 | */ |
| 552 | function ppress_get_username_by_id($user_id) |
| 553 | { |
| 554 | return ppress_var_obj(get_user_by('id', $user_id), 'user_login'); |
| 555 | } |
| 556 | |
| 557 | /** |
| 558 | * front-end profile slug. |
| 559 | * |
| 560 | * @return string |
| 561 | */ |
| 562 | function ppress_get_profile_slug() |
| 563 | { |
| 564 | return apply_filters('ppress_profile_slug', ppress_get_setting('set_user_profile_slug', 'profile', true)); |
| 565 | } |
| 566 | |
| 567 | /** |
| 568 | * Filter form field attributes for unofficial attributes. |
| 569 | * |
| 570 | * @param array $atts supplied shortcode attributes |
| 571 | * |
| 572 | * @return mixed |
| 573 | * |
| 574 | */ |
| 575 | function ppress_other_field_atts($atts) |
| 576 | { |
| 577 | if ( ! is_array($atts)) return $atts; |
| 578 | |
| 579 | $official_atts = array( |
| 580 | 'name', |
| 581 | 'class', |
| 582 | 'id', |
| 583 | 'value', |
| 584 | 'title', |
| 585 | 'required', |
| 586 | 'placeholder', |
| 587 | 'key', |
| 588 | 'field_key', |
| 589 | 'limit', |
| 590 | 'options', |
| 591 | 'checkbox_text', |
| 592 | 'processing_label' |
| 593 | ); |
| 594 | |
| 595 | $other_atts = array(); |
| 596 | |
| 597 | foreach ($atts as $key => $value) { |
| 598 | if ( ! in_array($key, $official_atts) && strpos($key, 'on') !== 0) { |
| 599 | $other_atts[esc_attr($key)] = esc_attr($value); |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | $other_atts_html = ''; |
| 604 | |
| 605 | if (is_array($other_atts) && ! empty($other_atts)) { |
| 606 | |
| 607 | foreach ($other_atts as $key => $value) { |
| 608 | |
| 609 | if ( ! empty($value)) { |
| 610 | $other_atts_html = sprintf('%s="%s" ', esc_attr($key), esc_attr($value)); |
| 611 | } |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | return $other_atts_html; |
| 616 | } |
| 617 | |
| 618 | |
| 619 | /** |
| 620 | * Create an index.php file to prevent directory browsing. |
| 621 | * |
| 622 | * @param string $location folder path to create the file in. |
| 623 | */ |
| 624 | function ppress_create_index_file($location) |
| 625 | { |
| 626 | $content = "You are not allowed here!"; |
| 627 | $fp = fopen($location . "/index.php", "wb"); |
| 628 | if ($fp) { |
| 629 | fwrite($fp, $content); |
| 630 | fclose($fp); |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | /** |
| 635 | * Get front-end do password reset form url. |
| 636 | * |
| 637 | * @param string $user_login |
| 638 | * @param string $key |
| 639 | * |
| 640 | * @return string |
| 641 | */ |
| 642 | function ppress_get_do_password_reset_url($user_login, $key) |
| 643 | { |
| 644 | $url = network_site_url("wp-login.php?action=rp&key=$key&login=" . rawurlencode($user_login), 'login'); |
| 645 | |
| 646 | $page_id = ppress_get_setting('set_lost_password_url'); |
| 647 | |
| 648 | if (apply_filters('ppress_front_end_do_password_reset', true) && ! empty($page_id)) { |
| 649 | |
| 650 | $url = add_query_arg( |
| 651 | array( |
| 652 | 'key' => $key, |
| 653 | 'login' => rawurlencode($user_login) |
| 654 | ), |
| 655 | ppress_password_reset_url() |
| 656 | ); |
| 657 | } |
| 658 | |
| 659 | return $url; |
| 660 | } |
| 661 | |
| 662 | /** |
| 663 | * Return true if a field key exist/is multi selectable dropdown. |
| 664 | * |
| 665 | * @param $field_key |
| 666 | * |
| 667 | * @return bool |
| 668 | */ |
| 669 | function ppress_is_select_field_multi_selectable($field_key) |
| 670 | { |
| 671 | $data = get_option('ppress_cpf_select_multi_selectable', array()); |
| 672 | |
| 673 | return array_key_exists($field_key, $data); |
| 674 | } |
| 675 | |
| 676 | |
| 677 | /** |
| 678 | * Return username/username of a user using the user's nicename to do the DB search. |
| 679 | * |
| 680 | * @param string $slug |
| 681 | * |
| 682 | * @return bool|null|string |
| 683 | */ |
| 684 | function ppress_is_slug_nice_name($slug) |
| 685 | { |
| 686 | global $wpdb; |
| 687 | |
| 688 | $response = $wpdb->get_var( |
| 689 | $wpdb->prepare( |
| 690 | "SELECT user_login FROM {$wpdb->prefix}users WHERE user_nicename = '%s'", |
| 691 | array($slug) |
| 692 | ) |
| 693 | ); |
| 694 | |
| 695 | // if response isn't null, the username/user_login is returned. |
| 696 | return is_null($response) ? false : $response; |
| 697 | } |
| 698 | |
| 699 | /** |
| 700 | * Return array of editable roles. |
| 701 | * |
| 702 | * @param $remove_admin |
| 703 | * |
| 704 | * @return mixed |
| 705 | */ |
| 706 | function ppress_get_editable_roles($remove_admin = true) |
| 707 | { |
| 708 | $all_roles = wp_roles()->roles; |
| 709 | |
| 710 | if (true == $remove_admin) { |
| 711 | unset($all_roles['administrator']); |
| 712 | } |
| 713 | |
| 714 | return $all_roles; |
| 715 | } |
| 716 | |
| 717 | function ppress_wp_roles_key_value($remove_admin = true) |
| 718 | { |
| 719 | $wp_roles = ppress_get_editable_roles($remove_admin); |
| 720 | |
| 721 | return array_reduce(array_keys($wp_roles), function ($carry, $item) use ($wp_roles) { |
| 722 | $carry[$item] = $wp_roles[$item]['name']; |
| 723 | |
| 724 | return $carry; |
| 725 | }, []); |
| 726 | } |
| 727 | |
| 728 | function ppress_wp_new_user_notification($user_id, $deprecated = null, $notify = '') |
| 729 | { |
| 730 | if (null !== $deprecated) { |
| 731 | _deprecated_argument(__FUNCTION__, '4.3.1'); |
| 732 | } |
| 733 | |
| 734 | // Accepts only 'user', 'admin' , 'both' or default '' as $notify. |
| 735 | if ( ! in_array($notify, array('user', 'admin', 'both', ''), true)) { |
| 736 | return; |
| 737 | } |
| 738 | |
| 739 | $new_user_notification = apply_filters('ppress_new_user_notification', 'enable'); |
| 740 | |
| 741 | if ('enable' != $new_user_notification) return; |
| 742 | |
| 743 | $user = get_userdata($user_id); |
| 744 | |
| 745 | // The blogname option is escaped with esc_html() on the way into the database in sanitize_option(). |
| 746 | // We want to reverse this for the plain text arena of emails. |
| 747 | $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES); |
| 748 | |
| 749 | /** |
| 750 | * Filters whether the admin is notified of a new user registration. |
| 751 | * |
| 752 | * @param bool $send Whether to send the email. Default true. |
| 753 | * @param WP_User $user User object for new user. |
| 754 | * |
| 755 | * @since 6.1.0 |
| 756 | * |
| 757 | */ |
| 758 | $send_notification_to_admin = apply_filters('wp_send_new_user_notification_to_admin', true, $user); |
| 759 | |
| 760 | if ('user' !== $notify && true === $send_notification_to_admin) { |
| 761 | |
| 762 | if (ppress_get_setting('new_user_admin_email_email_enabled', 'on') == 'on') { |
| 763 | |
| 764 | $message = ppress_get_setting('new_user_admin_email_email_content', ppress_new_user_admin_notification_message_default(), true); |
| 765 | |
| 766 | $title = ppress_get_setting('new_user_admin_email_email_subject', sprintf(__('[%s] New User Registration'), $blogname), true); |
| 767 | |
| 768 | $search = array( |
| 769 | '{{username}}', |
| 770 | '{{user_email}}', |
| 771 | '{{site_title}}', |
| 772 | '{{first_name}}', |
| 773 | '{{last_name}}' |
| 774 | ); |
| 775 | |
| 776 | $replace = array( |
| 777 | $user->user_login, |
| 778 | $user->user_email, |
| 779 | $blogname, |
| 780 | $user->first_name, |
| 781 | $user->last_name |
| 782 | ); |
| 783 | |
| 784 | $message = htmlspecialchars_decode( |
| 785 | apply_filters( |
| 786 | 'ppress_signup_admin_email_message', |
| 787 | str_replace($search, $replace, $message), |
| 788 | $user |
| 789 | ) |
| 790 | ); |
| 791 | |
| 792 | $title = apply_filters( |
| 793 | 'ppress_signup_admin_email_subject', |
| 794 | str_replace($search, $replace, $title), |
| 795 | $user |
| 796 | ); |
| 797 | |
| 798 | $title = ppress_custom_profile_field_search_replace($title, $user); |
| 799 | $message = ppress_custom_profile_field_search_replace($message, $user); |
| 800 | |
| 801 | $admin_email = apply_filters('ppress_signup_notification_admin_email', ppress_get_admin_notification_emails()); |
| 802 | |
| 803 | ppress_send_email($admin_email, $title, $message); |
| 804 | } |
| 805 | } |
| 806 | |
| 807 | /** |
| 808 | * Filters whether the user is notified of their new user registration. |
| 809 | * |
| 810 | * @param bool $send Whether to send the email. Default true. |
| 811 | * @param WP_User $user User object for new user. |
| 812 | * |
| 813 | * @since 6.1.0 |
| 814 | * |
| 815 | */ |
| 816 | $send_notification_to_user = apply_filters('wp_send_new_user_notification_to_user', true, $user); |
| 817 | |
| 818 | // `$deprecated` was pre-4.3 `$plaintext_pass`. An empty `$plaintext_pass` didn't sent a user notification. |
| 819 | if ('admin' === $notify || true !== $send_notification_to_user || (empty($deprecated) && empty($notify))) { |
| 820 | return; |
| 821 | } |
| 822 | |
| 823 | $key = get_password_reset_key($user); |
| 824 | if (is_wp_error($key)) { |
| 825 | return; |
| 826 | } |
| 827 | |
| 828 | $switched_locale = switch_to_locale(get_user_locale($user)); |
| 829 | |
| 830 | /* translators: %s: User login. */ |
| 831 | $message = sprintf(__('Username: %s'), $user->user_login) . "\r\n\r\n"; |
| 832 | $message .= __('To set your password, visit the following address:') . "\r\n\r\n"; |
| 833 | $message .= network_site_url("wp-login.php?action=rp&key=$key&login=" . rawurlencode($user->user_login), 'login') . "\r\n\r\n"; |
| 834 | |
| 835 | $message .= wp_login_url() . "\r\n"; |
| 836 | |
| 837 | $wp_new_user_notification_email = array( |
| 838 | 'to' => $user->user_email, |
| 839 | /* translators: Login details notification email subject. %s: Site title. */ |
| 840 | 'subject' => __('[%s] Login Details'), |
| 841 | 'message' => $message, |
| 842 | 'headers' => '', |
| 843 | ); |
| 844 | |
| 845 | /** |
| 846 | * Filters the contents of the new user notification email sent to the new user. |
| 847 | * |
| 848 | * @param array $wp_new_user_notification_email { |
| 849 | * Used to build wp_mail(). |
| 850 | * |
| 851 | * @type string $to The intended recipient - New user email address. |
| 852 | * @type string $subject The subject of the email. |
| 853 | * @type string $message The body of the email. |
| 854 | * @type string $headers The headers of the email. |
| 855 | * } |
| 856 | * |
| 857 | * @param WP_User $user User object for new user. |
| 858 | * @param string $blogname The site title. |
| 859 | * |
| 860 | * @since 4.9.0 |
| 861 | * |
| 862 | */ |
| 863 | $wp_new_user_notification_email = apply_filters('wp_new_user_notification_email', $wp_new_user_notification_email, $user, $blogname); |
| 864 | |
| 865 | wp_mail( |
| 866 | $wp_new_user_notification_email['to'], |
| 867 | wp_specialchars_decode(sprintf($wp_new_user_notification_email['subject'], $blogname)), |
| 868 | $wp_new_user_notification_email['message'], |
| 869 | $wp_new_user_notification_email['headers'] |
| 870 | ); |
| 871 | |
| 872 | if ($switched_locale) { |
| 873 | restore_previous_locale(); |
| 874 | } |
| 875 | } |
| 876 | |
| 877 | if ( ! function_exists('wp_new_user_notification')) : |
| 878 | /** |
| 879 | * Email login credentials to a newly-registered user. |
| 880 | * |
| 881 | * A new user registration notification is also sent to admin email. |
| 882 | * |
| 883 | * @param int $user_id User ID. |
| 884 | * @param null $deprecated Not used (argument deprecated). |
| 885 | * @param string $notify Optional. Type of notification that should happen. Accepts 'admin' or an empty |
| 886 | * string (admin only), 'user', or 'both' (admin and user). Default empty. |
| 887 | * |
| 888 | * @since 4.6.0 The `$notify` parameter accepts 'user' for sending notification only to the user created. |
| 889 | * |
| 890 | * @global wpdb $wpdb WordPress database object for queries. |
| 891 | * @global PasswordHash $wp_hasher Portable PHP password hashing framework instance. |
| 892 | * |
| 893 | * @since 2.0.0 |
| 894 | * @since 4.3.0 The `$plaintext_pass` parameter was changed to `$notify`. |
| 895 | * @since 4.3.1 The `$plaintext_pass` parameter was deprecated. `$notify` added as a third parameter. |
| 896 | */ |
| 897 | function wp_new_user_notification($user_id, $deprecated = null, $notify = '') |
| 898 | { |
| 899 | return ppress_wp_new_user_notification($user_id, $deprecated, $notify); |
| 900 | } |
| 901 | endif; |
| 902 | |
| 903 | /** |
| 904 | * Does registration form has username requirement disabled? |
| 905 | * |
| 906 | * @param int $form_id |
| 907 | * @param bool $is_melange |
| 908 | * |
| 909 | * @return bool |
| 910 | */ |
| 911 | function ppress_is_signup_form_username_disabled($form_id, $is_melange = false) |
| 912 | { |
| 913 | $result = FR::get_form_meta($form_id, FR::REGISTRATION_TYPE, FR::DISABLE_USERNAME_REQUIREMENT); |
| 914 | |
| 915 | if ($is_melange === true) { |
| 916 | $result = FR::get_form_meta($form_id, FR::MELANGE_TYPE, FR::DISABLE_USERNAME_REQUIREMENT); |
| 917 | } |
| 918 | |
| 919 | if (is_string($result)) { |
| 920 | $result = $result == 'true' ? true : false; |
| 921 | } |
| 922 | |
| 923 | return $result; |
| 924 | } |
| 925 | |
| 926 | /** |
| 927 | * Generate url to reset user's password. |
| 928 | * |
| 929 | * @param string $user_login |
| 930 | * |
| 931 | * @return string |
| 932 | */ |
| 933 | function ppress_generate_password_reset_url($user_login) |
| 934 | { |
| 935 | $user = get_user_by('login', $user_login); |
| 936 | |
| 937 | $key = get_password_reset_key($user); |
| 938 | |
| 939 | if (is_wp_error($key)) { |
| 940 | ppress_log_error($key->get_error_message()); |
| 941 | |
| 942 | return ''; |
| 943 | } |
| 944 | |
| 945 | return ppress_get_do_password_reset_url($user_login, $key); |
| 946 | } |
| 947 | |
| 948 | function ppress_nonce_action_string() |
| 949 | { |
| 950 | return 'pp_plugin_nonce'; |
| 951 | } |
| 952 | |
| 953 | /** |
| 954 | * Return array of countries. |
| 955 | * |
| 956 | * @param string $country_code |
| 957 | * |
| 958 | * @return mixed|string |
| 959 | */ |
| 960 | function ppress_array_of_world_countries($country_code = '') |
| 961 | { |
| 962 | $list = apply_filters('ppress_countries_list', include(PROFILEPRESS_SRC . 'Functions/data/countries.php')); |
| 963 | |
| 964 | if ( ! empty($country_code)) { |
| 965 | return ppress_var($list, $country_code); |
| 966 | } |
| 967 | |
| 968 | return $list; |
| 969 | } |
| 970 | |
| 971 | /** |
| 972 | * @param $country |
| 973 | * |
| 974 | * @return mixed |
| 975 | */ |
| 976 | function ppress_array_of_world_states($country = '') |
| 977 | { |
| 978 | $states = apply_filters('ppress_countries_states_list', include(PROFILEPRESS_SRC . 'Functions/data/states.php')); |
| 979 | |
| 980 | if ( ! empty($country)) { |
| 981 | return ppress_var($states, $country, []); |
| 982 | } |
| 983 | |
| 984 | return $states; |
| 985 | } |
| 986 | |
| 987 | function ppress_get_country_title($country) |
| 988 | { |
| 989 | if ( ! empty($country)) { |
| 990 | |
| 991 | $val = ppress_array_of_world_countries($country); |
| 992 | |
| 993 | if ( ! empty($val)) return $val; |
| 994 | } |
| 995 | |
| 996 | return $country; |
| 997 | } |
| 998 | |
| 999 | function ppress_get_country_state_title($state, $country) |
| 1000 | { |
| 1001 | return ppress_var(ppress_array_of_world_states($country), $state, $state, true); |
| 1002 | } |
| 1003 | |
| 1004 | function ppress_create_nonce() |
| 1005 | { |
| 1006 | return wp_create_nonce(ppress_nonce_action_string()); |
| 1007 | } |
| 1008 | |
| 1009 | function ppress_nonce_field() |
| 1010 | { |
| 1011 | return wp_nonce_field(ppress_nonce_action_string(), '_wpnonce', true, false); |
| 1012 | } |
| 1013 | |
| 1014 | function ppress_verify_nonce() |
| 1015 | { |
| 1016 | return check_admin_referer(ppress_nonce_action_string()); |
| 1017 | } |
| 1018 | |
| 1019 | function ppress_verify_ajax_nonce() |
| 1020 | { |
| 1021 | return check_ajax_referer(ppress_nonce_action_string()); |
| 1022 | } |
| 1023 | |
| 1024 | /** |
| 1025 | * Returns a more compact md5 hashing. |
| 1026 | * |
| 1027 | * @param $string |
| 1028 | * |
| 1029 | * @return false|string |
| 1030 | */ |
| 1031 | function ppress_md5($string) |
| 1032 | { |
| 1033 | return substr(base_convert(md5($string), 16, 32), 0, 12); |
| 1034 | } |
| 1035 | |
| 1036 | /** |
| 1037 | * Generate unique ID |
| 1038 | * |
| 1039 | * @param int $length |
| 1040 | * |
| 1041 | * @return string |
| 1042 | */ |
| 1043 | function ppress_generate_unique_id($length = 10) |
| 1044 | { |
| 1045 | $characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
| 1046 | $charactersLength = strlen($characters); |
| 1047 | $randomString = ''; |
| 1048 | for ($i = 0; $i < $length; $i++) { |
| 1049 | $randomString .= $characters[mt_rand(0, $charactersLength - 1)]; |
| 1050 | } |
| 1051 | |
| 1052 | return ppress_md5(time() . $randomString); |
| 1053 | } |
| 1054 | |
| 1055 | function ppress_minify_css($buffer) |
| 1056 | { |
| 1057 | $buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer); |
| 1058 | $buffer = str_replace(': ', ':', $buffer); |
| 1059 | |
| 1060 | return str_replace(array("\r\n", "\r", "\n", "\t", ' ', ' ', ' '), '', $buffer); |
| 1061 | } |
| 1062 | |
| 1063 | function ppress_minify_js($code) |
| 1064 | { |
| 1065 | // make it into one long line |
| 1066 | $code = str_replace(array("\n", "\r"), '', $code); |
| 1067 | // replace all multiple spaces by one space |
| 1068 | $code = preg_replace('!\s+!', ' ', $code); |
| 1069 | |
| 1070 | // replace some unneeded spaces, modify as needed |
| 1071 | return str_replace(array(' {', ' }', '{ ', '; '), array('{', '}', '{', ';'), $code); |
| 1072 | } |
| 1073 | |
| 1074 | function ppress_minify_html($html) |
| 1075 | { |
| 1076 | $lines = explode(PHP_EOL, $html); |
| 1077 | array_walk($lines, function (&$line) { |
| 1078 | $line = trim($line); |
| 1079 | }); |
| 1080 | |
| 1081 | $lines = array_filter($lines, function ($line) { |
| 1082 | return $line !== ''; |
| 1083 | }); |
| 1084 | |
| 1085 | return implode(PHP_EOL, $lines); |
| 1086 | } |
| 1087 | |
| 1088 | function ppress_get_ip_address() |
| 1089 | { |
| 1090 | $user_ip = '127.0.0.1'; |
| 1091 | |
| 1092 | $keys = array( |
| 1093 | 'HTTP_CLIENT_IP', |
| 1094 | 'HTTP_X_FORWARDED_FOR', |
| 1095 | 'HTTP_X_FORWARDED', |
| 1096 | 'HTTP_X_CLUSTER_CLIENT_IP', |
| 1097 | 'HTTP_FORWARDED_FOR', |
| 1098 | 'HTTP_FORWARDED', |
| 1099 | 'REMOTE_ADDR', |
| 1100 | ); |
| 1101 | |
| 1102 | foreach ($keys as $key) { |
| 1103 | // Bail if the key doesn't exists. |
| 1104 | if ( ! isset($_SERVER[$key])) { |
| 1105 | continue; |
| 1106 | } |
| 1107 | |
| 1108 | if ($key == 'HTTP_X_FORWARDED_FOR' && ! empty($_SERVER[$key])) { |
| 1109 | //to check ip is pass from proxy |
| 1110 | // can include more than 1 ip, first is the public one |
| 1111 | $_SERVER[$key] = explode(',', $_SERVER[$key]); |
| 1112 | $_SERVER[$key] = $_SERVER[$key][0]; |
| 1113 | } |
| 1114 | |
| 1115 | // Bail if the IP is not valid. |
| 1116 | if ( ! filter_var(wp_unslash(trim($_SERVER[$key])), FILTER_VALIDATE_IP)) { |
| 1117 | continue; |
| 1118 | } |
| 1119 | |
| 1120 | $user_ip = str_replace('::1', '127.0.0.1', $_SERVER[$key]); |
| 1121 | } |
| 1122 | |
| 1123 | return apply_filters('ppress_get_ip', $user_ip); |
| 1124 | } |
| 1125 | |
| 1126 | /** |
| 1127 | * Admin email address to receive admin notification. |
| 1128 | * |
| 1129 | * @return mixed |
| 1130 | */ |
| 1131 | function ppress_get_admin_notification_emails() |
| 1132 | { |
| 1133 | return ppress_get_setting('admin_email_addresses', ppress_admin_email(), true); |
| 1134 | } |
| 1135 | |
| 1136 | /** |
| 1137 | * @return WP_Filesystem_Base|false |
| 1138 | */ |
| 1139 | function ppress_file_system() |
| 1140 | { |
| 1141 | global $wp_filesystem; |
| 1142 | |
| 1143 | require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 1144 | |
| 1145 | // If for some reason the include doesn't work as expected just return false. |
| 1146 | if ( ! function_exists('\WP_Filesystem')) { |
| 1147 | return false; |
| 1148 | } |
| 1149 | |
| 1150 | $writable = WP_Filesystem(false, '', true); |
| 1151 | |
| 1152 | // We consider the directory as writable if it uses the direct transport, |
| 1153 | // otherwise credentials would be needed. |
| 1154 | return ($writable && 'direct' === $wp_filesystem->method) ? $wp_filesystem : false; |
| 1155 | } |
| 1156 | |
| 1157 | function ppress_get_file($file) |
| 1158 | { |
| 1159 | $content = ''; |
| 1160 | |
| 1161 | $fs = ppress_file_system(); |
| 1162 | |
| 1163 | if ($fs && $fs->exists($file)) { |
| 1164 | $content = $fs->get_contents($file); |
| 1165 | } |
| 1166 | |
| 1167 | return $content; |
| 1168 | } |
| 1169 | |
| 1170 | function ppress_get_error_log($type = 'debug') |
| 1171 | { |
| 1172 | $file_token = get_option('ppress_debug_log_token'); |
| 1173 | |
| 1174 | $log_file = PPRESS_ERROR_LOG_FOLDER . "{$type}-{$file_token}.log"; |
| 1175 | |
| 1176 | $file_contents = ''; |
| 1177 | |
| 1178 | if (file_exists($log_file)) { |
| 1179 | $file_contents = @file_get_contents($log_file); |
| 1180 | } |
| 1181 | |
| 1182 | return $file_contents; |
| 1183 | } |
| 1184 | |
| 1185 | function ppress_log_error($message, $type = 'debug') |
| 1186 | { |
| 1187 | if (is_array($message)) wp_json_encode($message); |
| 1188 | |
| 1189 | $log_folder = PPRESS_ERROR_LOG_FOLDER; |
| 1190 | |
| 1191 | // does bugs folder exist? if NO, create it. |
| 1192 | if ( ! file_exists($log_folder)) { |
| 1193 | mkdir($log_folder, 0755, true); |
| 1194 | } |
| 1195 | |
| 1196 | if ( ! file_exists(PPRESS_ERROR_LOG_FOLDER . '/index.php')) { |
| 1197 | ppress_create_index_file(PPRESS_ERROR_LOG_FOLDER); |
| 1198 | } |
| 1199 | |
| 1200 | // |
| 1201 | $fs = ppress_file_system(); |
| 1202 | |
| 1203 | $file_token = get_option('ppress_debug_log_token'); |
| 1204 | if (false === $file_token) { |
| 1205 | $file_token = uniqid(wp_rand(), true); |
| 1206 | update_option('ppress_debug_log_token', $file_token); |
| 1207 | } |
| 1208 | |
| 1209 | $filename = "{$type}-{$file_token}.log"; // ex. debug-5c2f6a9b9b5a3.log. |
| 1210 | $file = $log_folder . $filename; |
| 1211 | $old_file = $log_folder . "{$type}.log"; |
| 1212 | |
| 1213 | // If old file exists, move it. |
| 1214 | if ($fs && $fs->exists($old_file)) { |
| 1215 | $old_content = ppress_get_file($old_file); |
| 1216 | $fs->put_contents($file, $old_content, FS_CHMOD_FILE); |
| 1217 | |
| 1218 | // Move old file to new location. |
| 1219 | $fs->move($old_file, $file); |
| 1220 | |
| 1221 | if ($fs->exists($old_file)) { |
| 1222 | $fs->delete($old_file); |
| 1223 | } |
| 1224 | } |
| 1225 | // |
| 1226 | |
| 1227 | $message = current_time('mysql') . ' - ' . $message . "\r\n\r\n"; |
| 1228 | |
| 1229 | error_log($message, 3, "{$log_folder}{$filename}"); |
| 1230 | } |
| 1231 | |
| 1232 | function ppress_clear_error_log($type = 'debug') |
| 1233 | { |
| 1234 | $file_token = get_option('ppress_debug_log_token'); |
| 1235 | @unlink(PPRESS_ERROR_LOG_FOLDER . "{$type}-{$file_token}.log"); |
| 1236 | @unlink(PPRESS_ERROR_LOG_FOLDER . $type . '.log'); |
| 1237 | } |
| 1238 | |
| 1239 | function ppressPOST_var($key, $default = false, $empty = false, $bucket = false) |
| 1240 | { |
| 1241 | $bucket = ! $bucket ? $_POST : $bucket; |
| 1242 | |
| 1243 | if ($empty) { |
| 1244 | return ! empty($bucket[$key]) ? $bucket[$key] : $default; |
| 1245 | } |
| 1246 | |
| 1247 | return isset($bucket[$key]) ? $bucket[$key] : $default; |
| 1248 | } |
| 1249 | |
| 1250 | function ppressGET_var($key, $default = false, $empty = false) |
| 1251 | { |
| 1252 | $bucket = $_GET; |
| 1253 | |
| 1254 | if ($empty) { |
| 1255 | return ! empty($bucket[$key]) ? $bucket[$key] : $default; |
| 1256 | } |
| 1257 | |
| 1258 | return $bucket[$key] ?? $default; |
| 1259 | } |
| 1260 | |
| 1261 | function ppress_var($bucket, $key, $default = false, $empty = false) |
| 1262 | { |
| 1263 | if ($empty) { |
| 1264 | return isset($bucket[$key]) && ( ! empty($bucket[$key]) || ppress_is_boolean($bucket[$key]) || is_numeric($bucket[$key])) ? $bucket[$key] : $default; |
| 1265 | } |
| 1266 | |
| 1267 | return $bucket[$key] ?? $default; |
| 1268 | } |
| 1269 | |
| 1270 | function ppress_var_obj($bucket, $key, $default = false, $empty = false) |
| 1271 | { |
| 1272 | if ($empty) { |
| 1273 | return isset($bucket->$key) && ( ! empty($bucket->$key) || ppress_is_boolean($bucket->$key)) ? $bucket->$key : $default; |
| 1274 | } |
| 1275 | |
| 1276 | return $bucket->$key ?? $default; |
| 1277 | } |
| 1278 | |
| 1279 | /** |
| 1280 | * Normalize unamed shortcode |
| 1281 | * |
| 1282 | * @param array $atts |
| 1283 | * |
| 1284 | * @return mixed |
| 1285 | */ |
| 1286 | function ppress_normalize_attributes($atts) |
| 1287 | { |
| 1288 | if (is_array($atts)) { |
| 1289 | foreach ($atts as $key => $value) { |
| 1290 | if (is_int($key)) { |
| 1291 | $atts[$value] = true; |
| 1292 | unset($atts[$key]); |
| 1293 | } |
| 1294 | } |
| 1295 | } |
| 1296 | |
| 1297 | return $atts; |
| 1298 | } |
| 1299 | |
| 1300 | function ppress_dnd_field_key_description() |
| 1301 | { |
| 1302 | 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'); |
| 1303 | } |
| 1304 | |
| 1305 | function ppress_reserved_field_keys() |
| 1306 | { |
| 1307 | return [ |
| 1308 | 'ID', |
| 1309 | 'id', |
| 1310 | 'user_pass', |
| 1311 | 'user_login', |
| 1312 | 'user_nicename', |
| 1313 | 'user_url', |
| 1314 | 'user_email', |
| 1315 | 'display_name', |
| 1316 | 'nickname', |
| 1317 | 'first_name', |
| 1318 | 'last_name', |
| 1319 | 'description', |
| 1320 | 'rich_editing', |
| 1321 | 'syntax_highlighting', |
| 1322 | 'comment_shortcuts', |
| 1323 | 'admin_color', |
| 1324 | 'use_ssl', |
| 1325 | 'user_registered', |
| 1326 | 'user_activation_key', |
| 1327 | 'spam', |
| 1328 | 'show_admin_bar_front', |
| 1329 | 'role', |
| 1330 | 'locale', |
| 1331 | 'deleted', |
| 1332 | 'user_level', |
| 1333 | 'user_status', |
| 1334 | 'user_description' |
| 1335 | ]; |
| 1336 | } |
| 1337 | |
| 1338 | function ppress_is_boolean($maybe_bool) |
| 1339 | { |
| 1340 | if (is_bool($maybe_bool)) { |
| 1341 | return true; |
| 1342 | } |
| 1343 | |
| 1344 | if (is_string($maybe_bool)) { |
| 1345 | $maybe_bool = strtolower($maybe_bool); |
| 1346 | |
| 1347 | $valid_boolean_values = array( |
| 1348 | 'false', |
| 1349 | 'true', |
| 1350 | '0', |
| 1351 | '1', |
| 1352 | ); |
| 1353 | |
| 1354 | return in_array($maybe_bool, $valid_boolean_values, true); |
| 1355 | } |
| 1356 | |
| 1357 | if (is_int($maybe_bool)) { |
| 1358 | return in_array($maybe_bool, array(0, 1), true); |
| 1359 | } |
| 1360 | |
| 1361 | return false; |
| 1362 | } |
| 1363 | |
| 1364 | function ppress_filter_empty_array($values) |
| 1365 | { |
| 1366 | if ( ! is_array($values)) return $values; |
| 1367 | |
| 1368 | return array_filter($values, function ($value) { |
| 1369 | return ppress_is_boolean($value) || is_int($value) || ! empty($value); |
| 1370 | }); |
| 1371 | } |
| 1372 | |
| 1373 | /** |
| 1374 | * Check if HTTP status code is successful. |
| 1375 | * |
| 1376 | * @param int $code |
| 1377 | * |
| 1378 | * @return bool |
| 1379 | */ |
| 1380 | function ppress_is_http_code_success($code) |
| 1381 | { |
| 1382 | $code = absint($code); |
| 1383 | |
| 1384 | return $code >= 200 && $code <= 299; |
| 1385 | } |
| 1386 | |
| 1387 | /** |
| 1388 | * Converts date/time which should be in UTC to timestamp. |
| 1389 | * |
| 1390 | * strtotime uses the default timezone set in PHP which may or may not be UTC. |
| 1391 | * |
| 1392 | * @param $time |
| 1393 | * |
| 1394 | * @return false|int |
| 1395 | */ |
| 1396 | function ppress_strtotime_utc($time) |
| 1397 | { |
| 1398 | return strtotime($time . ' UTC'); |
| 1399 | } |
| 1400 | |
| 1401 | function ppress_array_flatten($array) |
| 1402 | { |
| 1403 | if ( ! is_array($array)) { |
| 1404 | return false; |
| 1405 | } |
| 1406 | $result = array(); |
| 1407 | foreach ($array as $key => $value) { |
| 1408 | if (is_array($value)) { |
| 1409 | // we are not doing array_merge here because we wanna keep array keys. |
| 1410 | // PS: The + operator is not an addition, it's a union. If the keys don't overlap then all is good. |
| 1411 | $result = $result + ppress_array_flatten($value); |
| 1412 | } else { |
| 1413 | $result[$key] = $value; |
| 1414 | } |
| 1415 | } |
| 1416 | |
| 1417 | return $result; |
| 1418 | } |
| 1419 | |
| 1420 | /** |
| 1421 | * Sanitizes a string key. |
| 1422 | * |
| 1423 | * Keys are used as internal identifiers. Lowercase alphanumeric characters and underscores are allowed. |
| 1424 | * |
| 1425 | * @param string $key String key |
| 1426 | * |
| 1427 | * @return string Sanitized key |
| 1428 | */ |
| 1429 | function ppress_sanitize_key($key) |
| 1430 | { |
| 1431 | return str_replace('-', '_', sanitize_key($key)); |
| 1432 | } |
| 1433 | |
| 1434 | function ppress_woocommerce_field_transform($cf_key, $cf_value) |
| 1435 | { |
| 1436 | if (class_exists('WooCommerce')) { |
| 1437 | |
| 1438 | if (in_array($cf_key, ppress_woocommerce_billing_fields())) { |
| 1439 | $cf_value = sprintf(esc_html__('%s (WooCommerce Billing Address)', 'wp-user-avatar'), $cf_value); |
| 1440 | } |
| 1441 | |
| 1442 | if (in_array($cf_key, ppress_woocommerce_shipping_fields())) { |
| 1443 | $cf_value = sprintf(esc_html__('%s (WooCommerce Shipping Address)', 'wp-user-avatar'), $cf_value); |
| 1444 | } |
| 1445 | } |
| 1446 | |
| 1447 | return $cf_value; |
| 1448 | } |
| 1449 | |
| 1450 | function ppress_custom_fields_key_value_pair($remove_default = false) |
| 1451 | { |
| 1452 | $defined_custom_fields = []; |
| 1453 | |
| 1454 | if ($remove_default === false) { |
| 1455 | $defined_custom_fields[''] = esc_html__('Select...', 'wp-user-avatar'); |
| 1456 | } |
| 1457 | |
| 1458 | foreach (CheckoutFields::standard_billing_fields() as $key => $field) { |
| 1459 | $defined_custom_fields[$key] = $field['label']; |
| 1460 | } |
| 1461 | |
| 1462 | if (EM::is_premium()) { |
| 1463 | $db_custom_fields = PROFILEPRESS_sql::get_profile_custom_fields(); |
| 1464 | $db_contact_infos = PROFILEPRESS_sql::get_contact_info_fields(); |
| 1465 | |
| 1466 | if ( ! empty($db_contact_infos)) { |
| 1467 | foreach ($db_contact_infos as $key => $value) { |
| 1468 | $defined_custom_fields[$key] = $value; |
| 1469 | } |
| 1470 | } |
| 1471 | |
| 1472 | if ( ! empty($db_custom_fields)) { |
| 1473 | foreach ($db_custom_fields as $db_custom_field) { |
| 1474 | $field_key = $db_custom_field['field_key']; |
| 1475 | $defined_custom_fields[$field_key] = ppress_woocommerce_field_transform($field_key, $db_custom_field['label_name']); |
| 1476 | } |
| 1477 | } |
| 1478 | } |
| 1479 | |
| 1480 | return $defined_custom_fields; |
| 1481 | } |
| 1482 | |
| 1483 | function ppress_standard_fields_key_value_pair($remove_default = false) |
| 1484 | { |
| 1485 | $fields = []; |
| 1486 | if ($remove_default === false) { |
| 1487 | $fields[''] = esc_html__('Select...', 'wp-user-avatar'); |
| 1488 | } |
| 1489 | |
| 1490 | return array_merge($fields, [ |
| 1491 | 'first_last_names' => esc_html__('First and Last Names', 'wp-user-avatar'), |
| 1492 | 'last_first_names' => esc_html__('Last and First Names', 'wp-user-avatar'), |
| 1493 | 'username' => esc_html__('Username', 'wp-user-avatar'), |
| 1494 | 'first-name' => esc_html__('First Name', 'wp-user-avatar'), |
| 1495 | 'last-name' => esc_html__('Last Name', 'wp-user-avatar'), |
| 1496 | 'nickname' => esc_html__('Nickname', 'wp-user-avatar'), |
| 1497 | 'display-name' => esc_html__('Display Name', 'wp-user-avatar'), |
| 1498 | 'email' => esc_html__('Email Address', 'wp-user-avatar'), |
| 1499 | 'bio' => esc_html__('Biography', 'wp-user-avatar'), |
| 1500 | 'registration_date' => esc_html__('Registration Date', 'wp-user-avatar'), |
| 1501 | ]); |
| 1502 | } |
| 1503 | |
| 1504 | function ppress_standard_custom_fields_key_value_pair($remove_default = false) |
| 1505 | { |
| 1506 | $fields = []; |
| 1507 | |
| 1508 | if ($remove_default === false) { |
| 1509 | $fields[''] = esc_html__('Select...', 'wp-user-avatar'); |
| 1510 | } |
| 1511 | |
| 1512 | $fields[esc_html__('Standard Fields', 'wp-user-avatar')] = ppress_standard_fields_key_value_pair(true); |
| 1513 | |
| 1514 | if (EM::is_enabled(EM::CUSTOM_FIELDS)) { |
| 1515 | $fields[esc_html__('Custom Fields', 'wp-user-avatar')] = ppress_custom_fields_key_value_pair(true); |
| 1516 | } |
| 1517 | |
| 1518 | return $fields; |
| 1519 | } |
| 1520 | |
| 1521 | /** |
| 1522 | * @param int|bool $user_id |
| 1523 | * |
| 1524 | * @return bool |
| 1525 | */ |
| 1526 | function ppress_user_has_cover_image($user_id = false) |
| 1527 | { |
| 1528 | $user_id = ! $user_id ? get_current_user_id() : $user_id; |
| 1529 | |
| 1530 | $cover = get_user_meta($user_id, 'pp_profile_cover_image', true); |
| 1531 | |
| 1532 | return ! empty($cover); |
| 1533 | } |
| 1534 | |
| 1535 | |
| 1536 | /** |
| 1537 | * @param int|bool $user_id |
| 1538 | * |
| 1539 | * @return string|bool |
| 1540 | */ |
| 1541 | function ppress_get_cover_image_url($user_id = false) |
| 1542 | { |
| 1543 | $user_id = ! $user_id ? get_current_user_id() : $user_id; |
| 1544 | |
| 1545 | $slug = get_user_meta($user_id, 'pp_profile_cover_image', true); |
| 1546 | |
| 1547 | $url = ! empty($slug) ? PPRESS_COVER_IMAGE_UPLOAD_URL . "$slug" : get_option('wp_user_cover_default_image_url'); |
| 1548 | |
| 1549 | return esc_url_raw($url); |
| 1550 | } |
| 1551 | |
| 1552 | function ppress_is_my_own_profile() |
| 1553 | { |
| 1554 | global $ppress_frontend_profile_user_obj; |
| 1555 | |
| 1556 | return ppress_var_obj($ppress_frontend_profile_user_obj, 'ID') == get_current_user_id(); |
| 1557 | } |
| 1558 | |
| 1559 | function ppress_is_my_account_page() |
| 1560 | { |
| 1561 | return ppress_post_content_has_shortcode('profilepress-my-account'); |
| 1562 | } |
| 1563 | |
| 1564 | function ppress_social_network_fields() |
| 1565 | { |
| 1566 | return apply_filters('ppress_core_contact_info_fields', [ |
| 1567 | Base::cif_facebook => 'Facebook', |
| 1568 | Base::cif_twitter => 'X/Twitter', |
| 1569 | Base::cif_linkedin => 'LinkedIn', |
| 1570 | Base::cif_vk => 'VK', |
| 1571 | Base::cif_youtube => 'YouTube', |
| 1572 | Base::cif_instagram => 'Instagram', |
| 1573 | Base::cif_github => 'GitHub', |
| 1574 | Base::cif_pinterest => 'Pinterest', |
| 1575 | Base::cif_bluesky => 'Bluesky', |
| 1576 | Base::cif_threads => 'Threads', |
| 1577 | ]); |
| 1578 | } |
| 1579 | |
| 1580 | function ppress_social_login_networks() |
| 1581 | { |
| 1582 | return apply_filters('ppress_social_login_networks', [ |
| 1583 | 'facebook' => 'Facebook', |
| 1584 | 'twitter' => 'X/Twitter', |
| 1585 | 'google' => 'Google', |
| 1586 | 'linkedin' => 'LinkedIn', |
| 1587 | 'microsoft' => 'Microsoft', |
| 1588 | 'yahoo' => 'Yahoo', |
| 1589 | 'amazon' => 'Amazon', |
| 1590 | 'github' => 'GitHub', |
| 1591 | 'wordpresscom' => 'WordPress.com', |
| 1592 | 'vk' => 'VK.com' |
| 1593 | ]); |
| 1594 | } |
| 1595 | |
| 1596 | function ppress_mb_function($function_names, $args) |
| 1597 | { |
| 1598 | $mb_function_name = $function_names[0]; |
| 1599 | $function_name = $function_names[1]; |
| 1600 | if (function_exists($mb_function_name)) { |
| 1601 | $function_name = $mb_function_name; |
| 1602 | } |
| 1603 | |
| 1604 | return call_user_func_array($function_name, $args); |
| 1605 | } |
| 1606 | |
| 1607 | function ppress_recursive_trim($item) |
| 1608 | { |
| 1609 | if (is_array($item)) { |
| 1610 | |
| 1611 | $sanitized_data = []; |
| 1612 | foreach ($item as $key => $value) { |
| 1613 | $sanitized_data[$key] = ppress_recursive_trim($value); |
| 1614 | } |
| 1615 | |
| 1616 | return $sanitized_data; |
| 1617 | } |
| 1618 | |
| 1619 | return trim($item); |
| 1620 | } |
| 1621 | |
| 1622 | function ppress_check_type_and_ext($file, $accepted_mime_types = [], $accepted_file_ext = []) |
| 1623 | { |
| 1624 | |
| 1625 | if (empty($file_name)) { |
| 1626 | $file_name = $file['name']; |
| 1627 | } |
| 1628 | |
| 1629 | $tmp_name = $file['tmp_name']; |
| 1630 | |
| 1631 | $wp_filetype = wp_check_filetype_and_ext($tmp_name, $file_name); |
| 1632 | |
| 1633 | $ext = $wp_filetype['ext']; |
| 1634 | $type = $wp_filetype['type']; |
| 1635 | $proper_filename = $wp_filetype['proper_filename']; |
| 1636 | |
| 1637 | // When a proper_filename value exists, it could be a security issue if it's different than the original file name. |
| 1638 | if ($proper_filename && strtolower($proper_filename) !== strtolower($file_name)) { |
| 1639 | return new WP_Error('invalid_file', esc_html__('There was an problem while verifying your file.', 'wp-user-avatar')); |
| 1640 | } |
| 1641 | |
| 1642 | // If either $ext or $type are empty, WordPress doesn't like this file and we should bail. |
| 1643 | if ( ! $ext) { |
| 1644 | return new WP_Error('illegal_extension', esc_html__('Sorry, this file extension is not permitted for security reasons.', 'wp-user-avatar')); |
| 1645 | } |
| 1646 | |
| 1647 | if ( ! $type) { |
| 1648 | return new WP_Error('illegal_type', esc_html__('Sorry, this file type is not permitted for security reasons.', 'wp-user-avatar')); |
| 1649 | } |
| 1650 | |
| 1651 | if ( ! empty($accepted_mime_types) && ! in_array($type, $accepted_mime_types)) { |
| 1652 | return new WP_Error('illegal_type', esc_html__('Error: The file you uploaded is not accepted on our website.', 'wp-user-avatar')); |
| 1653 | } |
| 1654 | |
| 1655 | if ( ! empty($accepted_file_ext) && ! in_array($ext, $accepted_file_ext)) { |
| 1656 | return new WP_Error('illegal_type', esc_html__('Error: The file you uploaded is not accepted on our website.', 'wp-user-avatar')); |
| 1657 | } |
| 1658 | |
| 1659 | return true; |
| 1660 | } |
| 1661 | |
| 1662 | function ppress_decode_html_strip_tags($val) |
| 1663 | { |
| 1664 | return strip_tags(html_entity_decode($val)); |
| 1665 | } |
| 1666 | |
| 1667 | function ppress_content_http_redirect($myURL) |
| 1668 | { |
| 1669 | ?> |
| 1670 | <script type="text/javascript"> |
| 1671 | window.location.href = "<?php echo $myURL;?>" |
| 1672 | </script> |
| 1673 | <meta http-equiv="refresh" content="0; url=<?php echo $myURL; ?>"> |
| 1674 | Please wait while you are redirected...or |
| 1675 | <a href="<?php echo $myURL; ?>">Click Here</a> if you do not want to wait. |
| 1676 | <?php |
| 1677 | } |
| 1678 | |
| 1679 | function ppress_do_admin_redirect($url) |
| 1680 | { |
| 1681 | if ( ! headers_sent()) { |
| 1682 | wp_safe_redirect($url); |
| 1683 | exit; |
| 1684 | } |
| 1685 | |
| 1686 | ppress_content_http_redirect($url); |
| 1687 | } |
| 1688 | |
| 1689 | function ppress_is_json($str) |
| 1690 | { |
| 1691 | $json = json_decode($str); |
| 1692 | |
| 1693 | return $json && $str != $json; |
| 1694 | } |
| 1695 | |
| 1696 | function ppress_clean($var, $callback = 'sanitize_textarea_field') |
| 1697 | { |
| 1698 | if (is_array($var)) { |
| 1699 | return array_map('ppress_clean', $var); |
| 1700 | } else { |
| 1701 | return is_scalar($var) ? call_user_func($callback, $var) : $var; |
| 1702 | } |
| 1703 | } |
| 1704 | |
| 1705 | /** |
| 1706 | * Strip shortcode tags until the value stops changing. |
| 1707 | * |
| 1708 | * strip_shortcodes() makes a single non-recursive pass, so a nested tag such as |
| 1709 | * "[profile-[profile-email]email]" comes out of it as a working shortcode. |
| 1710 | * |
| 1711 | * @param $var |
| 1712 | * |
| 1713 | * @return mixed |
| 1714 | */ |
| 1715 | function ppress_strip_shortcodes($var) |
| 1716 | { |
| 1717 | return ppress_clean($var, function ($value) { |
| 1718 | while ($value !== ($stripped = strip_shortcodes($value))) { |
| 1719 | $value = $stripped; |
| 1720 | } |
| 1721 | |
| 1722 | return $value; |
| 1723 | }); |
| 1724 | } |
| 1725 | |
| 1726 | /** |
| 1727 | * Strip shortcode tag and sanitize data |
| 1728 | * |
| 1729 | * @param $var |
| 1730 | * |
| 1731 | * @return mixed |
| 1732 | */ |
| 1733 | function ppress_strip_shortcodes_clean($var) |
| 1734 | { |
| 1735 | return ppress_strip_shortcodes(ppress_clean($var)); |
| 1736 | } |
| 1737 | |
| 1738 | /** |
| 1739 | * @param $s |
| 1740 | * |
| 1741 | * @return bool |
| 1742 | * @see https://stackoverflow.com/a/23810738/2648410 |
| 1743 | */ |
| 1744 | function ppress_is_base64($s) |
| 1745 | { |
| 1746 | // Check if there are valid base64 characters |
| 1747 | if ( ! preg_match('/^[a-zA-Z0-9\/\r\n+]*={0,2}$/', $s)) return false; |
| 1748 | |
| 1749 | // Decode the string in strict mode and check the results |
| 1750 | $decoded = base64_decode($s, true); |
| 1751 | if (false === $decoded) return false; |
| 1752 | |
| 1753 | // Encode the string again |
| 1754 | if (base64_encode($decoded) != $s) return false; |
| 1755 | |
| 1756 | return true; |
| 1757 | } |
| 1758 | |
| 1759 | /** |
| 1760 | * @param int $plan_id Plan ID or Subscription ID if change plan URL |
| 1761 | * @param bool $is_change_plan set to true to return checkout url to change plan |
| 1762 | * |
| 1763 | * @return false|string |
| 1764 | */ |
| 1765 | function ppress_plan_checkout_url($plan_id, $is_change_plan = false) |
| 1766 | { |
| 1767 | $page_id = ppress_settings_by_key('checkout_page_id'); |
| 1768 | |
| 1769 | if ( ! empty($page_id)) { |
| 1770 | |
| 1771 | $cid = $is_change_plan ? 'change_plan' : 'plan'; |
| 1772 | |
| 1773 | return add_query_arg($cid, absint($plan_id), get_permalink($page_id)); |
| 1774 | } |
| 1775 | |
| 1776 | return false; |
| 1777 | } |
| 1778 | |
| 1779 | /** |
| 1780 | * Generate unique ID for each optin form. |
| 1781 | * |
| 1782 | * @param int $length |
| 1783 | * |
| 1784 | * @return string |
| 1785 | */ |
| 1786 | function ppress_generateUniqueId($length = 10) |
| 1787 | { |
| 1788 | $characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
| 1789 | $charactersLength = strlen($characters); |
| 1790 | $randomString = ''; |
| 1791 | for ($i = 0; $i < $length; $i++) { |
| 1792 | $randomString .= $characters[mt_rand(0, $charactersLength - 1)]; |
| 1793 | } |
| 1794 | |
| 1795 | return ppress_md5(time() . $randomString); |
| 1796 | } |
| 1797 | |
| 1798 | function ppress_render_view($template, $vars = [], $parentDir = '') |
| 1799 | { |
| 1800 | if (empty($parentDir)) $parentDir = dirname(__FILE__, 2) . '/templates/'; |
| 1801 | |
| 1802 | $path = $parentDir . $template . '.php'; |
| 1803 | |
| 1804 | extract($vars); |
| 1805 | ob_start(); |
| 1806 | require apply_filters('ppress_render_view', $path, $vars, $template, $parentDir); |
| 1807 | echo apply_filters('ppress_render_view_output', ob_get_clean(), $template, $vars, $parentDir); |
| 1808 | } |
| 1809 | |
| 1810 | function ppress_post_content_has_shortcode($tag = '', $post = null) |
| 1811 | { |
| 1812 | if (is_null($post)) { |
| 1813 | global $post; |
| 1814 | } |
| 1815 | |
| 1816 | return is_singular() && is_a($post, 'WP_Post') && has_shortcode($post->post_content, $tag); |
| 1817 | } |
| 1818 | |
| 1819 | function ppress_maybe_define_constant($name, $value) |
| 1820 | { |
| 1821 | if ( ! defined($name)) { |
| 1822 | define($name, $value); |
| 1823 | } |
| 1824 | } |
| 1825 | |
| 1826 | function ppress_upgrade_urls_affilify($url) |
| 1827 | { |
| 1828 | return apply_filters('ppress_pro_upgrade_url', $url); |
| 1829 | } |
| 1830 | |
| 1831 | function ppress_cache_transform($cache_key, $callback) |
| 1832 | { |
| 1833 | static $cache = []; |
| 1834 | |
| 1835 | // If the cache key does not exist, compute the value and cache it. |
| 1836 | if ( ! isset($cache[$cache_key])) { |
| 1837 | $cache[$cache_key] = $callback(); |
| 1838 | } |
| 1839 | |
| 1840 | // Return the cached result. |
| 1841 | return $cache[$cache_key]; |
| 1842 | } |
| 1843 | |
| 1844 | function ppress_form_has_field($form_id, $form_type, $field_shortcode_tag) |
| 1845 | { |
| 1846 | if (FR::is_drag_drop($form_id, $form_type)) { |
| 1847 | |
| 1848 | $settings = FR::form_builder_fields_settings($form_id, $form_type); |
| 1849 | |
| 1850 | $found_field = wp_list_filter($settings, ['fieldType' => $field_shortcode_tag]); |
| 1851 | |
| 1852 | if ( ! empty($found_field)) return true; |
| 1853 | |
| 1854 | } else { |
| 1855 | |
| 1856 | $registration_structure = FR::get_form_meta($form_id, $form_type, FR::FORM_STRUCTURE); |
| 1857 | |
| 1858 | // find the first occurrence of reg-select-role shortcode. |
| 1859 | preg_match('/\[' . $field_shortcode_tag . '.*\]/', $registration_structure, $matches); |
| 1860 | |
| 1861 | if ( ! empty($matches[0])) return true; |
| 1862 | } |
| 1863 | |
| 1864 | return false; |
| 1865 | } |
| 1866 | |
| 1867 | /** |
| 1868 | * @param $message |
| 1869 | * @param WP_User $user |
| 1870 | * |
| 1871 | * @return array|mixed|string|string[] |
| 1872 | */ |
| 1873 | function ppress_custom_profile_field_search_replace($message, $user) |
| 1874 | { |
| 1875 | // handle support for custom fields placeholder. |
| 1876 | preg_match_all('#({{[a-z_-]+}})#', $message, $matches); |
| 1877 | |
| 1878 | if ( ! empty($matches[1])) { |
| 1879 | |
| 1880 | foreach ($matches[1] as $match) { |
| 1881 | |
| 1882 | $key = str_replace(['{', '}'], '', $match); |
| 1883 | |
| 1884 | $value = ''; |
| 1885 | |
| 1886 | $field_type = PROFILEPRESS_sql::get_field_type($key); |
| 1887 | |
| 1888 | if ($field_type == 'file') { |
| 1889 | $value = FrontendProfileBuilder::get_user_uploaded_file($user->ID, $key); |
| 1890 | } elseif (isset($user->{$key})) { |
| 1891 | |
| 1892 | $value = $user->{$key}; |
| 1893 | |
| 1894 | if (is_array($value)) $value = implode(', ', $value); |
| 1895 | } |
| 1896 | |
| 1897 | $message = str_replace($match, $value, $message); |
| 1898 | } |
| 1899 | } |
| 1900 | |
| 1901 | return $message; |
| 1902 | } |
| 1903 |