PluginProbe ʕ •ᴥ•ʔ
Paid Membership Plugin, Ecommerce, User Registration Form, Login Form, User Profile & Restrict Content – ProfilePress / 3.2.2
Paid Membership Plugin, Ecommerce, User Registration Form, Login Form, User Profile & Restrict Content – ProfilePress v3.2.2
4.16.19 4.16.18 4.16.17 4.16.16 trunk 1.0 1.0.1 1.0.2 1.1 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.5a 1.1.6 1.1.7 1.2 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.4 1.4.1 1.4.2 1.5 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 1.7 1.7.1 1.7.2 1.8 1.8.1 1.8.10 1.8.2 1.8.3 1.8.4 1.8.5 1.8.6 1.8.7 1.8.8 1.8.9 1.9 1.9.1 1.9.10 1.9.11 1.9.12 1.9.13 1.9.2 1.9.3 1.9.4 1.9.5 1.9.6 1.9.7 1.9.8 1.9.9 2.1.9 2.2.10 2.2.11 2.2.12 2.2.13 2.2.14 2.2.15 2.2.16 2.2.2 2.2.5 2.2.6 2.2.7 2.2.8 2.2.9 3.0 3.1 3.1.1 3.1.10 3.1.11 3.1.12 3.1.13 3.1.14 3.1.15 3.1.16 3.1.17 3.1.18 3.1.19 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 3.2.0 3.2.1 3.2.10 3.2.11 3.2.12 3.2.13 3.2.14 3.2.15 3.2.16 3.2.2 3.2.3 3.2.4 3.2.5 3.2.6 3.2.7 3.2.8 3.2.9 4.0.0 4.0.1 4.0.2 4.0.3 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.10.0 4.10.1 4.10.2 4.10.3 4.11.0 4.12.0 4.13.0 4.13.1 4.13.2 4.13.3 4.13.4 4.14.0 4.14.1 4.14.2 4.14.3 4.14.4 4.15.0 4.15.1 4.15.10 4.15.11 4.15.12 4.15.13 4.15.14 4.15.15 4.15.16 4.15.17 4.15.18 4.15.19 4.15.2 4.15.20 4.15.20.1 4.15.21 4.15.22 4.15.23 4.15.24 4.15.25 4.15.3 4.15.4 4.15.5 4.15.6 4.15.7 4.15.8 4.15.9 4.16.0 4.16.1 4.16.10 4.16.11 4.16.12 4.16.13 4.16.14 4.16.15 4.16.2 4.16.3 4.16.4 4.16.5 4.16.6 4.16.7 4.16.8 4.16.9 4.2.0 4.3.0 4.3.1 4.3.2 4.4.0 4.4.1 4.5.0 4.5.1 4.5.2 4.5.3 4.5.4 4.5.5 4.6.0 4.7.0 4.8.0 4.9.0
wp-user-avatar / src / Functions / GlobalFunctions.php
wp-user-avatar / src / Functions Last commit date
data 5 years ago GlobalFunctions.php 4 years ago PPressBFnote.php 4 years ago custom-settings-api.php 4 years ago
GlobalFunctions.php
1447 lines
1 <?php
2
3 use ProfilePress\Core\Admin\SettingsPages\MailOptin;
4 use ProfilePress\Core\Base;
5 use ProfilePress\Core\Classes\ExtensionManager as EM;
6 use ProfilePress\Core\Classes\FormRepository as FR;
7 use ProfilePress\Core\Classes\PROFILEPRESS_sql as PROFILEPRESS_sql;
8 use ProfilePress\Core\Classes\SendEmail;
9
10 /** Plugin DB settings data */
11 function ppress_db_data()
12 {
13 return get_option(PPRESS_SETTINGS_DB_OPTION_NAME, []);
14 }
15
16 function ppress_update_settings($key, $value)
17 {
18 $data = ppress_db_data();
19 $data[$key] = $value;
20 update_option(PPRESS_SETTINGS_DB_OPTION_NAME, $data);
21 }
22
23 /**
24 * Array of WooCommerce billing fields.
25 *
26 * @return array
27 */
28 function ppress_woocommerce_billing_fields()
29 {
30 return array(
31 'billing_first_name',
32 'billing_last_name',
33 'billing_company',
34 'billing_address_1',
35 'billing_address_2',
36 'billing_city',
37 'billing_postcode',
38 'billing_country',
39 'billing_state',
40 'billing_phone',
41 'billing_email'
42 );
43 }
44
45 /**
46 * Array of WooCommerce billing fields.
47 *
48 * @return array
49 */
50 function ppress_woocommerce_shipping_fields()
51 {
52 return array(
53 'shipping_first_name',
54 'shipping_last_name',
55 'shipping_company',
56 'shipping_address_1',
57 'shipping_address_2',
58 'shipping_city',
59 'shipping_postcode',
60 'shipping_country',
61 'shipping_state'
62 );
63 }
64
65 /**
66 * Array of WooCommerce billing and shipping fields.
67 *
68 * @return array
69 */
70 function ppress_woocommerce_billing_shipping_fields()
71 {
72 return array_merge(ppress_woocommerce_billing_fields(), ppress_woocommerce_shipping_fields());
73 }
74
75 /**
76 * @param string $key
77 * @param bool $default
78 * @param bool $is_empty set to true to return the default if value is empty
79 *
80 * @return mixed
81 */
82 function ppress_settings_by_key($key = '', $default = false, $is_empty = false)
83 {
84 $cache_key = 'ppress_settings_db_data';
85
86 $data = wp_cache_get($cache_key);
87
88 if (false === $data) {
89 $data = ppress_db_data();
90 wp_cache_set($key, $data);
91 }
92
93 if ($is_empty === true) {
94 return ! empty($data[$key]) ? $data[$key] : $default;
95 }
96
97 return isset($data[$key]) ? $data[$key] : $default;
98 }
99
100 function ppress_get_setting($key = '', $default = false, $is_empty = false)
101 {
102 return ppress_settings_by_key($key, $default, $is_empty);
103 }
104
105 /**
106 * Send email.
107 *
108 * @param string|array $to
109 * @param $subject
110 * @param $message
111 *
112 * @return bool|WP_Error
113 */
114 function ppress_send_email($to, $subject, $message)
115 {
116 return (new SendEmail($to, $subject, $message))->send();
117 }
118
119 function ppress_welcome_msg_content_default()
120 {
121 return <<<HTML
122 <h1>Welcome {{first_name}}!</h1>
123 <p>We are so happy to have you. Below is your login credential:</p>
124 <p>Username: {{username}}</p>
125 <p>Password: the password you registered with.</p>
126
127 <div style="padding: 10px 0 50px 0; text-align: center;">
128 <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>
129 </div>
130 <p>If you have any problems, do not hesitate to contact us.</p>
131 HTML;
132 }
133
134 function ppress_new_user_admin_notification_message_default()
135 {
136 return <<<HTML
137 <p>New user registration on your site {{site_title}}.</p>
138 <p>Username: {{username}}</p>
139 <p>Email address: {{user_email}}</p>
140 HTML;
141 }
142
143 function ppress_passwordless_login_message_default()
144 {
145 return <<<MESSAGE
146 <p>Hi {{username}}, we have generated a one-time login link for you.</p>
147 <div style="padding: 10px 0 50px 0; text-align: center;">
148 <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>
149 </div>
150 MESSAGE;
151 }
152
153 function ppress_user_moderation_msg_default($type)
154 {
155 $pending = <<<MESSAGE
156 <p>Hi {{first_name}} {{last_name}}, your account is pending approval.</p>
157 <p>You will receive an email once your account is approved.</p>
158 <p>Regards.</p>
159 MESSAGE;
160
161 $approved = <<<MESSAGE
162 <p>Hi {{first_name}} {{last_name}}, your account has been approved.</p>
163
164 <p>Regards.</p>
165 MESSAGE;
166
167 $rejected = <<<MESSAGE
168 <p>Hi {{first_name}} {{last_name}}, your account has been rejected.</p>
169
170 <p>Regards.</p>
171 MESSAGE;
172
173 $blocked = <<<MESSAGE
174 <p>Hi {{first_name}} {{last_name}}, your account has been blocked.</p>
175
176 <p>Regards.</p>
177 MESSAGE;
178
179 $unblocked = <<<MESSAGE
180 <p>Hi {{first_name}} {{last_name}}, your account with username "{{username}}" has been unblocked.</p>
181
182 <p>Regards.</p>
183 MESSAGE;
184
185 $admin_notification = <<<MESSAGE
186 <p>A new user is awaiting your approval on your site.</p>
187 <p>Username: {{username}}</p>
188 <p>E-mail: {{email}}</p>
189 <p>Click to approve: {{approval_url}}</p>
190 <p>Click to reject: {{rejection_url}}</p>
191 MESSAGE;
192
193 return ${$type};
194 }
195
196 function ppress_password_reset_content_default()
197 {
198 return <<<HTML
199 <p>Someone requested to reset the password for the following account:</p>
200 <p>Username: {{username}}</p>
201 <p>If this was a mistake, just ignore this email and nothing will happen.</p>
202 <p>To reset your password, click the button below.</p>
203 <div style="padding: 10px 0 50px 0; text-align: center;">
204 <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>
205 </div>
206 HTML;
207 }
208
209 /**
210 * Return the url to redirect to after login authentication
211 *
212 * @return bool|string
213 */
214 function ppress_login_redirect()
215 {
216 if ( ! empty($_REQUEST['redirect_to'])) {
217 $redirect = rawurldecode($_REQUEST['redirect_to']);
218 } else {
219 $login_redirect = ppress_get_setting('set_login_redirect');
220 $custom_url_login_redirect = ppress_get_setting('custom_url_login_redirect');
221 $referrer_url = ppress_var($_POST, 'login_referrer_page', ppress_var($_POST, 'signup_referrer_page'));
222
223 if ( ! empty($custom_url_login_redirect)) {
224 $redirect = $custom_url_login_redirect;
225 } elseif ($login_redirect == 'dashboard') {
226 $redirect = network_site_url('/wp-admin');
227 } elseif ($login_redirect == 'previous_page' && ! empty($referrer_url)) {
228 $redirect = $referrer_url;
229 } elseif ('current_page' == $login_redirect) {
230 // in ajax mode, pp_current_url is set so we can do client-side redirection to current page after login.
231 // no way to get current url in social login hence, look it up from $_GET['pp_current_url']
232 if ( ! empty($_GET['pp_current_url'])) {
233 $redirect = rawurldecode($_GET['pp_current_url']);
234 } elseif ( ! empty($_POST['pp_current_url'])) {
235 $redirect = rawurldecode($_POST['pp_current_url']);
236 } else {
237 $redirect = ppress_get_current_url_raw();
238 }
239 } elseif ( ! empty($login_redirect)) {
240 $redirect = get_permalink($login_redirect);
241 } else {
242 $redirect = network_site_url('/wp-admin');
243 }
244 }
245
246 return apply_filters('ppress_login_redirect', wp_validate_redirect($redirect));
247 }
248
249 /**
250 * Return the url to redirect to after successful reset / change of password.
251 *
252 * @return bool|string
253 */
254 function ppress_password_reset_redirect()
255 {
256 $password_reset_redirect = ppress_get_setting('set_password_reset_redirect');
257 $custom_url_password_reset_redirect = ppress_get_setting('custom_url_password_reset_redirect');
258
259 if ( ! empty($custom_url_password_reset_redirect)) {
260 $redirect = $custom_url_password_reset_redirect;
261 } elseif ( ! empty($password_reset_redirect)) {
262 $redirect = get_permalink($password_reset_redirect);
263 if ($password_reset_redirect == 'no_redirect') {
264 $redirect = ppress_password_reset_url() . '?password=changed';
265 }
266 } else {
267 $redirect = ppress_password_reset_url() . '?password=changed';
268 }
269
270 return apply_filters('ppress_do_password_reset_redirect', esc_url_raw($redirect));
271 }
272
273 /**
274 * Return the url to frontend myprofile page.
275 *
276 * @return bool|string
277 */
278 function ppress_profile_url()
279 {
280 $url = admin_url('profile.php');
281
282 $page_id = ppress_get_setting('set_user_profile_shortcode');
283
284 if ( ! empty($page_id)) {
285 $url = get_permalink($page_id);
286 }
287
288 return apply_filters('ppress_profile_url', $url);
289 }
290
291 function ppress_get_frontend_profile_url($username_or_id)
292 {
293 if (is_numeric($username_or_id)) {
294 $username_or_id = ppress_get_username_by_id($username_or_id);
295 }
296
297 return home_url(ppress_get_profile_slug() . '/' . rawurlencode($username_or_id));
298 }
299
300 /**
301 * Return ProfilePress edit profile page URL or WP default profile URL as fallback
302 *
303 * @return bool|string
304 */
305 function ppress_edit_profile_url()
306 {
307 return apply_filters('ppress_edit_profile_url', ppress_my_account_url());
308 }
309
310 function ppress_my_account_url()
311 {
312 $url = get_edit_profile_url();
313
314 $page_id = ppress_settings_by_key('edit_user_profile_url');
315
316 if ( ! empty($page_id) && get_post_status($page_id)) {
317 $url = get_permalink($page_id);
318 }
319
320 return apply_filters('ppress_my_account_url', $url);
321 }
322
323 /**
324 * Return ProfilePress password reset url.
325 *
326 * @return string
327 */
328 function ppress_password_reset_url()
329 {
330 $url = wp_lostpassword_url();
331
332 $page_id = ppress_get_setting('set_lost_password_url');
333
334 if ( ! empty($page_id) && get_post_status($page_id)) {
335 $url = get_permalink($page_id);
336 }
337
338 return apply_filters('ppress_password_reset_url', $url);
339 }
340
341
342 /**
343 * Get ProfilePress login page URL or WP default login url if it isn't set.
344 *
345 * @param $redirect
346 *
347 * @return string
348 */
349 function ppress_login_url($redirect = '')
350 {
351 $login_url = wp_login_url();
352
353 $login_page_id = ppress_get_setting('set_login_url');
354
355 if ( ! empty($login_page_id) && get_post_status($login_page_id)) {
356 $login_url = get_permalink($login_page_id);
357 }
358
359 if ( ! empty($redirect)) {
360 $login_url = add_query_arg('redirect_to', rawurlencode(wp_validate_redirect($redirect)), $login_url);
361 }
362
363 return apply_filters('ppress_login_url', $login_url);
364 }
365
366 /**
367 * Get ProfilePress login page URL or WP default login url if it isn't set.
368 */
369 function ppress_registration_url()
370 {
371 $reg_url = wp_registration_url();
372
373 $page_id = ppress_get_setting('set_registration_url');
374
375 if ( ! empty($page_id) && get_post_status($page_id)) {
376 $reg_url = get_permalink($page_id);
377 }
378
379 return apply_filters('ppress_registration_url', $reg_url);
380 }
381
382 /**
383 * Return the URL of the currently view page.
384 *
385 * @return string
386 */
387 function ppress_get_current_url()
388 {
389 global $wp;
390
391 return home_url(add_query_arg(array(), $wp->request));
392 }
393
394
395 /**
396 * Return currently viewed page url without query string.
397 *
398 * @return string
399 */
400 function ppress_get_current_url_raw()
401 {
402 $protocol = 'http://';
403
404 if ((isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] == 1))
405 || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')
406 ) {
407 $protocol = 'https://';
408 }
409
410 return esc_url_raw($protocol . $_SERVER['HTTP_HOST'] . parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
411 }
412
413 /**
414 * Return currently viewed page url with query string.
415 *
416 * @return string
417 */
418 function ppress_get_current_url_query_string()
419 {
420 $protocol = 'http://';
421
422 if ((isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] == 1))
423 || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')
424 ) {
425 $protocol = 'https://';
426 }
427
428 $url = $protocol . $_SERVER['HTTP_HOST'];
429
430 $url .= $_SERVER['REQUEST_URI'];
431
432 return esc_url_raw($url);
433 }
434
435
436 /**
437 * @return string blog URL without scheme
438 */
439 function ppress_site_url_without_scheme()
440 {
441 $parsed_url = parse_url(home_url());
442
443 return $parsed_url['host'];
444 }
445
446 /**
447 * Append an option to a select dropdown
448 *
449 * @param string $option option to add
450 * @param string $select select dropdown
451 *
452 * @return string
453 */
454 function ppress_append_option_to_select($option, $select)
455 {
456 $regex = "/<select ([^<]*)>/";
457
458 preg_match($regex, $select, $matches);
459 $select_attr = ppress_var($matches, 1);
460
461 $a = preg_split($regex, $select);
462
463 $join = '<select ' . $select_attr . '>' . "\r\n";
464 $join .= $option . ppress_var($a, 1, '');
465
466 return $join;
467 }
468
469 /**
470 * Blog name or domain name if name doesn't exist
471 *
472 * @return string
473 */
474 function ppress_site_title()
475 {
476 $blog_name = get_option('blogname');
477
478 return ! empty($blog_name) ? wp_specialchars_decode($blog_name, ENT_QUOTES) : str_replace(
479 array(
480 'http://',
481 'https://',
482 ),
483 '',
484 site_url()
485 );
486 }
487
488
489 /**
490 * Check if an admin settings page is ProfilePress'
491 *
492 * @return bool
493 */
494 function ppress_is_admin_page()
495 {
496 $pp_builder_pages = [
497 PPRESS_SETTINGS_SLUG,
498 PPRESS_FORMS_SETTINGS_SLUG,
499 PPRESS_MEMBER_DIRECTORIES_SLUG,
500 PPRESS_CONTENT_PROTECTION_SETTINGS_SLUG,
501 PPRESS_EXTENSIONS_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 * @return mixed
916 */
917 function ppress_array_of_world_countries()
918 {
919 return apply_filters('ppress_countries_custom_field_data', include(PROFILEPRESS_SRC . 'Functions/data/countries.php'));
920 }
921
922 function ppress_create_nonce()
923 {
924 return wp_create_nonce(ppress_nonce_action_string());
925 }
926
927 function ppress_nonce_field()
928 {
929 return wp_nonce_field(ppress_nonce_action_string(), '_wpnonce', true, false);
930 }
931
932 function ppress_verify_nonce()
933 {
934 return check_admin_referer(ppress_nonce_action_string());
935 }
936
937 function ppress_verify_ajax_nonce()
938 {
939 return check_ajax_referer(ppress_nonce_action_string());
940 }
941
942 /**
943 * Returns a more compact md5 hashing.
944 *
945 * @param $string
946 *
947 * @return false|string
948 */
949 function ppress_md5($string)
950 {
951 return substr(base_convert(md5($string), 16, 32), 0, 12);
952 }
953
954 function ppress_minify_css($buffer)
955 {
956 $buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);
957 $buffer = str_replace(': ', ':', $buffer);
958 $buffer = str_replace(array("\r\n", "\r", "\n", "\t", ' ', ' ', ' '), '', $buffer);
959
960 return $buffer;
961 }
962
963 function ppress_minify_js($code)
964 {
965 // make it into one long line
966 $code = str_replace(array("\n", "\r"), '', $code);
967 // replace all multiple spaces by one space
968 $code = preg_replace('!\s+!', ' ', $code);
969 // replace some unneeded spaces, modify as needed
970 $code = str_replace(array(' {', ' }', '{ ', '; '), array('{', '}', '{', ';'), $code);
971
972 return $code;
973 }
974
975 function ppress_get_ip_address()
976 {
977 $ip = '127.0.0.1';
978
979 if ( ! empty($_SERVER['HTTP_CLIENT_IP'])) {
980 $ip = $_SERVER['HTTP_CLIENT_IP'];
981 } elseif ( ! empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
982 $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
983 } elseif ( ! empty($_SERVER['REMOTE_ADDR'])) {
984 $ip = $_SERVER['REMOTE_ADDR'];
985 }
986
987 // Fix potential CSV returned from $_SERVER variables
988 $ip_array = array_map('trim', explode(',', $ip));
989
990 return $ip_array[0] != '::1' ? $ip_array[0] : '';
991 }
992
993 /**
994 * Admin email address to receive admin notification.
995 *
996 * @return mixed
997 */
998 function ppress_get_admin_notification_emails()
999 {
1000 return ppress_get_setting('admin_email_addresses', ppress_admin_email(), true);
1001 }
1002
1003 function ppress_get_error_log($type = 'debug')
1004 {
1005 $log_file = PPRESS_ERROR_LOG_FOLDER . $type . '.log';
1006
1007 $file_contents = '';
1008
1009 if (file_exists($log_file)) {
1010 $file_contents = @file_get_contents($log_file);
1011 }
1012
1013 return $file_contents;
1014 }
1015
1016 function ppress_log_error($message, $type = 'debug')
1017 {
1018 $log_folder = PPRESS_ERROR_LOG_FOLDER;
1019
1020 // does bugs folder exist? if NO, create it.
1021 if ( ! file_exists($log_folder)) {
1022 mkdir($log_folder, 0755, true);
1023 }
1024
1025 if ( ! file_exists(PPRESS_ERROR_LOG_FOLDER . '/index.php')) {
1026 ppress_create_index_file(PPRESS_ERROR_LOG_FOLDER);
1027 }
1028
1029 $message = current_time('mysql') . ' - ' . $message . "\r\n\r\n";
1030
1031 error_log($message, 3, "{$log_folder}{$type}.log");
1032 }
1033
1034 function ppress_clear_error_log($type = 'debug')
1035 {
1036 return @unlink(PPRESS_ERROR_LOG_FOLDER . $type . '.log');
1037 }
1038
1039 function ppressPOST_var($key, $default = false, $empty = false, $bucket = false)
1040 {
1041 $bucket = ! $bucket ? $_POST : $bucket;
1042
1043 if ($empty) {
1044 return ! empty($bucket[$key]) ? $bucket[$key] : $default;
1045 }
1046
1047 return isset($bucket[$key]) ? $bucket[$key] : $default;
1048 }
1049
1050 function ppressGET_var($key, $default = false, $empty = false)
1051 {
1052 $bucket = $_GET;
1053
1054 if ($empty) {
1055 return ! empty($bucket[$key]) ? $bucket[$key] : $default;
1056 }
1057
1058 return isset($bucket[$key]) ? $bucket[$key] : $default;
1059 }
1060
1061 function ppress_var($bucket, $key, $default = false, $empty = false)
1062 {
1063 if ($empty) {
1064 return ! empty($bucket[$key]) ? $bucket[$key] : $default;
1065 }
1066
1067 return isset($bucket[$key]) ? $bucket[$key] : $default;
1068 }
1069
1070 function ppress_var_obj($bucket, $key, $default = false, $empty = false)
1071 {
1072 if ($empty) {
1073 return ! empty($bucket->$key) ? $bucket->$key : $default;
1074 }
1075
1076 return isset($bucket->$key) ? $bucket->$key : $default;
1077 }
1078
1079 /**
1080 * Normalize unamed shortcode
1081 *
1082 * @param array $atts
1083 *
1084 * @return mixed
1085 */
1086 function ppress_normalize_attributes($atts)
1087 {
1088 if (is_array($atts)) {
1089 foreach ($atts as $key => $value) {
1090 if (is_int($key)) {
1091 $atts[$value] = true;
1092 unset($atts[$key]);
1093 }
1094 }
1095 }
1096
1097 return $atts;
1098 }
1099
1100 /** @todo link to doc on reserve words/text here */
1101 function ppress_dnd_field_key_description()
1102 {
1103 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');
1104 }
1105
1106 function ppress_reserved_field_keys()
1107 {
1108 return [
1109 'ID', 'id', 'user_pass', 'user_login', 'user_nicename', 'user_url', 'user_email', 'display_name', 'nickname',
1110 'first_name', 'last_name', 'description', 'rich_editing', 'syntax_highlighting', 'comment_shortcuts', 'admin_color',
1111 'use_ssl', 'user_registered', 'user_activation_key', 'spam', 'show_admin_bar_front', 'role', 'locale', 'deleted', 'user_level',
1112 'user_status', 'user_description'
1113 ];
1114 }
1115
1116 function ppress_is_boolean($maybe_bool)
1117 {
1118 if (is_bool($maybe_bool)) {
1119 return true;
1120 }
1121
1122 if (is_string($maybe_bool)) {
1123 $maybe_bool = strtolower($maybe_bool);
1124
1125 $valid_boolean_values = array(
1126 'false',
1127 'true',
1128 '0',
1129 '1',
1130 );
1131
1132 return in_array($maybe_bool, $valid_boolean_values, true);
1133 }
1134
1135 if (is_int($maybe_bool)) {
1136 return in_array($maybe_bool, array(0, 1), true);
1137 }
1138
1139 return false;
1140 }
1141
1142 function ppress_filter_empty_array($values)
1143 {
1144 return array_filter($values, function ($value) {
1145 return ppress_is_boolean($value) || is_int($value) || ! empty($value);
1146 });
1147 }
1148
1149 /**
1150 * Check if HTTP status code is successful.
1151 *
1152 * @param int $code
1153 *
1154 * @return bool
1155 */
1156 function ppress_is_http_code_success($code)
1157 {
1158 $code = absint($code);
1159
1160 return $code >= 200 && $code <= 299;
1161 }
1162
1163 /**
1164 * strtotime uses the default timezone set in PHP which may or may not be UTC.
1165 *
1166 * @param $time
1167 *
1168 * @param null|int $now
1169 *
1170 * @return false|int
1171 */
1172 function ppress_strtotime_utc($time, $now = null)
1173 {
1174 if (is_null($now)) $now = time();
1175
1176 $old = date_default_timezone_get();
1177
1178 date_default_timezone_set('UTC');
1179 $val = strtotime($time, $now);
1180
1181 date_default_timezone_set($old);
1182
1183 return $val;
1184 }
1185
1186 function ppress_array_flatten($array)
1187 {
1188 if ( ! is_array($array)) {
1189 return false;
1190 }
1191 $result = array();
1192 foreach ($array as $key => $value) {
1193 if (is_array($value)) {
1194 // we are not doing array_merge here because we wanna keep array keys.
1195 // PS: The + operator is not an addition, it's a union. If the keys don't overlap then all is good.
1196 $result = $result + ppress_array_flatten($value);
1197 } else {
1198 $result[$key] = $value;
1199 }
1200 }
1201
1202 return $result;
1203 }
1204
1205 /**
1206 * Sanitizes a string key.
1207 *
1208 * Keys are used as internal identifiers. Lowercase alphanumeric characters and underscores are allowed.
1209 *
1210 * @param string $key String key
1211 *
1212 * @return string Sanitized key
1213 */
1214 function ppress_sanitize_key($key)
1215 {
1216 return str_replace('-', '_', sanitize_key($key));
1217 }
1218
1219 function ppress_woocommerce_field_transform($cf_key, $cf_value)
1220 {
1221 if (class_exists('WooCommerce')) {
1222
1223 if (in_array($cf_key, ppress_woocommerce_billing_fields())) {
1224 $cf_value = sprintf(esc_html__('%s (WooCommerce Billing Address)', 'wp-user-avatar'), $cf_value);
1225 }
1226
1227 if (in_array($cf_key, ppress_woocommerce_shipping_fields())) {
1228 $cf_value = sprintf(esc_html__('%s (WooCommerce Shipping Address)', 'wp-user-avatar'), $cf_value);
1229 }
1230 }
1231
1232 return $cf_value;
1233 }
1234
1235 function ppress_custom_fields_key_value_pair($remove_default = false)
1236 {
1237 $defined_custom_fields = [];
1238
1239 if ($remove_default === false) {
1240 $defined_custom_fields[''] = esc_html__('Select...', 'wp-user-avatar');
1241 }
1242
1243 if (EM::is_premium()) {
1244 $db_custom_fields = PROFILEPRESS_sql::get_profile_custom_fields();
1245 $db_contact_infos = PROFILEPRESS_sql::get_contact_info_fields();
1246
1247 if ( ! empty($db_contact_infos)) {
1248 foreach ($db_contact_infos as $key => $value) {
1249 $defined_custom_fields[$key] = $value;
1250 }
1251 }
1252
1253 if ( ! empty($db_custom_fields)) {
1254 foreach ($db_custom_fields as $db_custom_field) {
1255 $field_key = $db_custom_field['field_key'];
1256 $defined_custom_fields[$field_key] = ppress_woocommerce_field_transform($field_key, $db_custom_field['label_name']);
1257 }
1258 }
1259 }
1260
1261 return $defined_custom_fields;
1262 }
1263
1264 function ppress_standard_fields_key_value_pair($remove_default = false)
1265 {
1266 $fields = [];
1267 if ($remove_default === false) {
1268 $fields[''] = esc_html__('Select...', 'wp-user-avatar');
1269 }
1270
1271 return array_merge($fields, [
1272 'first_last_names' => esc_html__('First and Last Names', 'wp-user-avatar'),
1273 'last_first_names' => esc_html__('Last and First Names', 'wp-user-avatar'),
1274 'username' => esc_html__('Username', 'wp-user-avatar'),
1275 'first-name' => esc_html__('First Name', 'wp-user-avatar'),
1276 'last-name' => esc_html__('Last Name', 'wp-user-avatar'),
1277 'nickname' => esc_html__('Nickname', 'wp-user-avatar'),
1278 'display-name' => esc_html__('Display Name', 'wp-user-avatar'),
1279 'email' => esc_html__('Email Address', 'wp-user-avatar'),
1280 'bio' => esc_html__('Biography', 'wp-user-avatar'),
1281 'registration_date' => esc_html__('Registration Date', 'wp-user-avatar'),
1282 ]);
1283 }
1284
1285 function ppress_standard_custom_fields_key_value_pair($remove_default = false)
1286 {
1287 $fields = [];
1288
1289 if ($remove_default === false) {
1290 $fields[''] = esc_html__('Select...', 'wp-user-avatar');
1291 }
1292
1293 $fields[esc_html__('Standard Fields', 'wp-user-avatar')] = ppress_standard_fields_key_value_pair(true);
1294
1295 if (EM::is_enabled(EM::CUSTOM_FIELDS)) {
1296 $fields[esc_html__('Custom Fields', 'wp-user-avatar')] = ppress_custom_fields_key_value_pair(true);
1297 }
1298
1299 return $fields;
1300 }
1301
1302 /**
1303 * @param int|bool $user_id
1304 *
1305 * @return bool
1306 */
1307 function ppress_user_has_cover_image($user_id = false)
1308 {
1309 $user_id = ! $user_id ? get_current_user_id() : $user_id;
1310
1311 $cover = get_user_meta($user_id, 'pp_profile_cover_image', true);
1312
1313 return ! empty($cover);
1314 }
1315
1316
1317 /**
1318 * @param int|bool $user_id
1319 *
1320 * @return string|bool
1321 */
1322 function ppress_get_cover_image_url($user_id = false)
1323 {
1324 $user_id = ! $user_id ? get_current_user_id() : $user_id;
1325
1326 $slug = get_user_meta($user_id, 'pp_profile_cover_image', true);
1327
1328 if ( ! empty($slug)) {
1329 return PPRESS_COVER_IMAGE_UPLOAD_URL . "$slug";
1330 }
1331
1332 return get_option('wp_user_cover_default_image_url');
1333 }
1334
1335 function ppress_is_my_own_profile()
1336 {
1337 global $ppress_frontend_profile_user_obj;
1338
1339 return ppress_var_obj($ppress_frontend_profile_user_obj, 'ID') == get_current_user_id();
1340 }
1341
1342 function ppress_social_network_fields()
1343 {
1344 return apply_filters('ppress_core_contact_info_fields', [
1345 Base::cif_facebook => 'Facebook',
1346 Base::cif_twitter => 'Twitter',
1347 Base::cif_linkedin => 'LinkedIn',
1348 Base::cif_vk => 'VK',
1349 Base::cif_youtube => 'YouTube',
1350 Base::cif_instagram => 'Instagram',
1351 Base::cif_github => 'GitHub',
1352 ]);
1353 }
1354
1355 function ppress_mb_function($function_names, $args)
1356 {
1357 $mb_function_name = $function_names[0];
1358 $function_name = $function_names[1];
1359 if (function_exists($mb_function_name)) {
1360 $function_name = $mb_function_name;
1361 }
1362
1363 return call_user_func_array($function_name, $args);
1364 }
1365
1366 function ppress_recursive_trim($item)
1367 {
1368 if (is_array($item)) {
1369
1370 $sanitized_data = [];
1371 foreach ($item as $key => $value) {
1372 $sanitized_data[$key] = ppress_recursive_trim($value);
1373 }
1374
1375 return $sanitized_data;
1376 }
1377
1378 return trim($item);
1379 }
1380
1381 function ppress_check_type_and_ext($file, $accepted_mime_types = [], $accepted_file_ext = [])
1382 {
1383
1384 if (empty($file_name)) {
1385 $file_name = $file['name'];
1386 }
1387
1388 $tmp_name = $file['tmp_name'];
1389
1390 $wp_filetype = wp_check_filetype_and_ext($tmp_name, $file_name);
1391
1392 $ext = $wp_filetype['ext'];
1393 $type = $wp_filetype['type'];
1394 $proper_filename = $wp_filetype['proper_filename'];
1395
1396 // When a proper_filename value exists, it could be a security issue if it's different than the original file name.
1397 if ($proper_filename && strtolower($proper_filename) !== strtolower($file_name)) {
1398 return new WP_Error('invalid_file', esc_html__('There was an problem while verifying your file.', 'wp-user-avatar'));
1399 }
1400
1401 // If either $ext or $type are empty, WordPress doesn't like this file and we should bail.
1402 if ( ! $ext) {
1403 return new WP_Error('illegal_extension', esc_html__('Sorry, this file extension is not permitted for security reasons.', 'wp-user-avatar'));
1404 }
1405
1406 if ( ! $type) {
1407 return new WP_Error('illegal_type', esc_html__('Sorry, this file type is not permitted for security reasons.', 'wp-user-avatar'));
1408 }
1409
1410 if ( ! empty($accepted_mime_types) && ! in_array($type, $accepted_mime_types)) {
1411 return new WP_Error('illegal_type', esc_html__('Error: The file you uploaded is not accepted on our website.', 'wp-user-avatar'));
1412 }
1413
1414 if ( ! empty($accepted_file_ext) && ! in_array($ext, $accepted_file_ext)) {
1415 return new WP_Error('illegal_type', esc_html__('Error: The file you uploaded is not accepted on our website.', 'wp-user-avatar'));
1416 }
1417
1418 return true;
1419 }
1420
1421 function ppress_decode_html_strip_tags($val)
1422 {
1423 return strip_tags(html_entity_decode($val));
1424 }
1425
1426 function ppress_content_http_redirect($myURL)
1427 {
1428 ?>
1429 <script type="text/javascript">
1430 window.location.href = "<?php echo $myURL;?>"
1431 </script>
1432 <meta http-equiv="refresh" content="0; url=<?php echo $myURL; ?>">
1433 Please wait while you are redirected...or
1434 <a href="<?php echo $myURL; ?>">Click Here</a> if you do not want to wait.
1435 <?php
1436 }
1437
1438 function ppress_shortcode_exist_in_post($shortcode)
1439 {
1440 global $post;
1441
1442 if (isset($post->post_content) && has_shortcode($post->post_content, $shortcode)) {
1443 return true;
1444 }
1445
1446 return false;
1447 }