EditProfileBuilder.php
3 years ago
FieldsShortcodeCallback.php
4 months ago
FrontendProfileBuilder.php
10 months ago
GlobalShortcodes.php
3 years ago
LoginFormBuilder.php
2 years ago
PasswordResetBuilder.php
4 weeks ago
RegistrationFormBuilder.php
1 year ago
index.php
5 years ago
GlobalShortcodes.php
562 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ProfilePress\Core\ShortcodeParser\Builder; |
| 4 | |
| 5 | use ProfilePress\Core\Classes\UserAvatar; |
| 6 | |
| 7 | class GlobalShortcodes |
| 8 | { |
| 9 | /** @var \WP_User */ |
| 10 | static private $current_user; |
| 11 | |
| 12 | public static function initialize() |
| 13 | { |
| 14 | add_action('init', array(__CLASS__, 'get_current_user')); |
| 15 | add_shortcode('pp-user-avatar', array(__CLASS__, 'user_avatar')); |
| 16 | add_shortcode('user-avatar', array(__CLASS__, 'user_avatar')); // backward compat |
| 17 | add_shortcode('pp-user-cover-image', array(__CLASS__, 'user_cover_image')); |
| 18 | add_shortcode('pp-custom-html', array(__CLASS__, 'custom_html_block')); |
| 19 | add_shortcode('link-registration', array(__CLASS__, 'link_registration')); |
| 20 | add_shortcode('link-lost-password', array(__CLASS__, 'link_lost_password')); |
| 21 | add_shortcode('link-login', array(__CLASS__, 'link_login')); |
| 22 | add_shortcode('link-logout', array(__CLASS__, 'link_logout')); |
| 23 | add_shortcode('link-edit-user-profile', array(__CLASS__, 'link_edit_profile')); // backward compat |
| 24 | add_shortcode('link-my-account', array(__CLASS__, 'link_edit_profile')); |
| 25 | add_shortcode('pp-login-form', array(__CLASS__, 'login_form_tag')); |
| 26 | add_shortcode('pp-registration-form', array(__CLASS__, 'registration_form_tag')); |
| 27 | add_shortcode('pp-password-reset-form', array(__CLASS__, 'password_reset_form_tag')); |
| 28 | add_shortcode('pp-edit-profile-form', array(__CLASS__, 'edit_profile_form_tag')); |
| 29 | add_shortcode('pp-redirect-non-logged-in-users', array(__CLASS__, 'redirect_non_logged_in_users')); |
| 30 | add_shortcode('pp-redirect-logged-in-users', array(__CLASS__, 'redirect_logged_in_users')); |
| 31 | add_shortcode('pp-logged-users', array(__CLASS__, 'pp_log_in_users')); |
| 32 | add_shortcode('pp-non-logged-users', array(__CLASS__, 'pp_non_log_in_users')); |
| 33 | |
| 34 | add_shortcode('password-hint', 'wp_get_password_hint'); |
| 35 | add_shortcode('pp-password-hint', 'wp_get_password_hint'); |
| 36 | |
| 37 | // BbPress |
| 38 | add_shortcode('bbp-topic-started-url', array(__CLASS__, 'bbp_topic_started_url')); |
| 39 | add_shortcode('bbp-replies-created-url', array(__CLASS__, 'bbp_replies_created_url')); |
| 40 | add_shortcode('bbp-favorites-url', array(__CLASS__, 'bbp_favorites_url')); |
| 41 | add_shortcode('bbp-subscriptions-url', array(__CLASS__, 'bbp_subscriptions_url')); |
| 42 | } |
| 43 | |
| 44 | /** Get the currently logged user */ |
| 45 | public static function get_current_user() |
| 46 | { |
| 47 | $current_user = wp_get_current_user(); |
| 48 | if ($current_user instanceof \WP_User) { |
| 49 | self::$current_user = $current_user; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | private static function melange_hidden_fields() |
| 54 | { |
| 55 | $tag = '<input type="hidden" name="is_melange" value="true">'; |
| 56 | |
| 57 | if (isset($GLOBALS['pp_melange_form_id'], $GLOBALS['pp_melange_form_redirect'])) { |
| 58 | $form_id = esc_attr($GLOBALS['pp_melange_form_id']); |
| 59 | $redirect = esc_url($GLOBALS['pp_melange_form_redirect']); |
| 60 | |
| 61 | $tag .= "<input type='hidden' name='pp_melange_id' class='pp_melange_id' value='$form_id'>"; |
| 62 | if ( ! empty($GLOBALS['pp_melange_form_redirect'])) { |
| 63 | $tag .= "<input type='hidden' name='melange_redirect' value='$redirect'>"; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | return $tag; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Login form tag |
| 72 | * |
| 73 | * @param array $atts |
| 74 | * @param string $content |
| 75 | * |
| 76 | * @return string |
| 77 | */ |
| 78 | public static function login_form_tag($atts, $content) |
| 79 | { |
| 80 | $tag = '<form method="post" data-pp-form-submit="login">'; |
| 81 | $tag .= self::melange_hidden_fields(); |
| 82 | $tag .= '<input type="hidden" name="pp_current_url" value="' . esc_attr(ppress_get_current_url_raw()) . '">'; |
| 83 | $tag .= do_shortcode($content); |
| 84 | $tag .= '</form>'; |
| 85 | |
| 86 | return $tag; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Registration form tag |
| 91 | * |
| 92 | * @param array $atts |
| 93 | * @param string $content |
| 94 | * |
| 95 | * @return string |
| 96 | */ |
| 97 | public static function registration_form_tag($atts, $content) |
| 98 | { |
| 99 | $atts = ppress_normalize_attributes($atts); |
| 100 | $novalidate = isset($atts['novalidate']) ? ' novalidate' : ''; |
| 101 | |
| 102 | $tag = sprintf('<form method="post" enctype="multipart/form-data" data-pp-form-submit="signup"%s>', $novalidate); |
| 103 | $tag .= self::melange_hidden_fields(); |
| 104 | $tag .= do_shortcode($content); |
| 105 | $tag .= '</form>'; |
| 106 | |
| 107 | return $tag; |
| 108 | |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Password reset form tag |
| 113 | * |
| 114 | * @param array $atts |
| 115 | * @param string $content |
| 116 | * |
| 117 | * @return string |
| 118 | */ |
| 119 | public static function password_reset_form_tag($atts, $content) |
| 120 | { |
| 121 | $tag = '<form method="post" data-pp-form-submit="passwordreset">'; |
| 122 | $tag .= self::melange_hidden_fields(); |
| 123 | $tag .= do_shortcode($content); |
| 124 | $tag .= '</form>'; |
| 125 | |
| 126 | return $tag; |
| 127 | |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Edit profile form tag |
| 132 | * |
| 133 | * @param array $atts |
| 134 | * @param string $content |
| 135 | * |
| 136 | * @return string |
| 137 | */ |
| 138 | public static function edit_profile_form_tag($atts, $content) |
| 139 | { |
| 140 | $tag = '<form method="post" enctype="multipart/form-data" data-pp-form-submit="editprofile">'; |
| 141 | $tag .= self::melange_hidden_fields(); |
| 142 | $tag .= do_shortcode($content); |
| 143 | $tag .= '</form>'; |
| 144 | |
| 145 | return $tag; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * @return string |
| 150 | */ |
| 151 | public static function custom_html_block($atts) |
| 152 | { |
| 153 | $atts = shortcode_atts(['custom_html' => ''], $atts); |
| 154 | |
| 155 | $val = $atts['custom_html']; |
| 156 | |
| 157 | if (ppress_is_base64($val)) { |
| 158 | $val = base64_decode($atts['custom_html']); |
| 159 | } |
| 160 | |
| 161 | if (apply_filters('ppress_sanitize_custom_html_block', true)) { |
| 162 | $val = wp_kses_post($val); |
| 163 | } |
| 164 | |
| 165 | return do_shortcode(stripslashes($val)); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Registration url |
| 170 | */ |
| 171 | public static function link_registration($atts) |
| 172 | { |
| 173 | $atts = ppress_normalize_attributes($atts); |
| 174 | |
| 175 | if (( ! empty($atts['raw']) && ($atts['raw'] == true))) { |
| 176 | return wp_registration_url(); |
| 177 | } |
| 178 | |
| 179 | $atts = shortcode_atts( |
| 180 | array( |
| 181 | 'class' => '', |
| 182 | 'id' => '', |
| 183 | 'title' => '', |
| 184 | 'label' => esc_html__('Sign Up', 'wp-user-avatar'), |
| 185 | 'raw' => '', |
| 186 | ), |
| 187 | $atts |
| 188 | ); |
| 189 | |
| 190 | $class = 'class="' . esc_attr($atts['class']) . '"'; |
| 191 | $id = 'id="' . esc_attr($atts['id']) . '"'; |
| 192 | $label = esc_attr($atts['label']); |
| 193 | $title = 'title="' . esc_attr($atts['title']) . '"'; |
| 194 | |
| 195 | |
| 196 | $html = '<a href="' . wp_registration_url() . "\" {$title} {$class} {$id}>$label</a>"; |
| 197 | |
| 198 | return $html; |
| 199 | } |
| 200 | |
| 201 | /** Lost password url */ |
| 202 | public static function link_lost_password($atts) |
| 203 | { |
| 204 | $atts = ppress_normalize_attributes($atts); |
| 205 | |
| 206 | |
| 207 | if (( ! empty($atts['raw']) && ($atts['raw'] == true))) { |
| 208 | return wp_lostpassword_url(); |
| 209 | } |
| 210 | |
| 211 | $atts = shortcode_atts( |
| 212 | array( |
| 213 | 'class' => '', |
| 214 | 'id' => '', |
| 215 | 'title' => '', |
| 216 | 'label' => esc_html__('Reset Password', 'wp-user-avatar'), |
| 217 | 'raw' => '', |
| 218 | ), |
| 219 | $atts |
| 220 | ); |
| 221 | |
| 222 | $class = 'class="' . esc_attr($atts['class']) . '"'; |
| 223 | $id = 'id="' . esc_attr($atts['id']) . '"'; |
| 224 | $label = esc_attr($atts['label']); |
| 225 | $title = 'title="' . esc_attr($atts['title']) . '"'; |
| 226 | |
| 227 | $html = "<a href=\"" . wp_lostpassword_url() . "\" {$title} {$class} {$id}>$label</a>"; |
| 228 | |
| 229 | return $html; |
| 230 | } |
| 231 | |
| 232 | |
| 233 | /** Login url */ |
| 234 | public static function link_login($atts) |
| 235 | { |
| 236 | $atts = ppress_normalize_attributes($atts); |
| 237 | |
| 238 | if (( ! empty($atts['raw']) && ($atts['raw'] == true))) { |
| 239 | return wp_login_url(); |
| 240 | } |
| 241 | |
| 242 | $atts = shortcode_atts( |
| 243 | array( |
| 244 | 'class' => '', |
| 245 | 'id' => '', |
| 246 | 'title' => '', |
| 247 | 'label' => esc_html__('Login', 'wp-user-avatar'), |
| 248 | 'raw' => '', |
| 249 | ), |
| 250 | $atts |
| 251 | ); |
| 252 | |
| 253 | $class = 'class="' . esc_attr($atts['class']) . '"'; |
| 254 | $id = 'id="' . esc_attr($atts['id']) . '"'; |
| 255 | $label = esc_attr($atts['label']); |
| 256 | $title = 'title="' . esc_attr($atts['title']) . '"'; |
| 257 | |
| 258 | $html = '<a href="' . wp_login_url() . '" ' . "$title $class $id" . '>' . $label . '</a>'; |
| 259 | |
| 260 | return $html; |
| 261 | } |
| 262 | |
| 263 | /** Logout URL */ |
| 264 | public static function link_logout($atts) |
| 265 | { |
| 266 | if ( ! is_user_logged_in()) { |
| 267 | return; |
| 268 | } |
| 269 | |
| 270 | $atts = ppress_normalize_attributes($atts); |
| 271 | |
| 272 | if (( ! empty($atts['raw']) && ($atts['raw'] == true))) { |
| 273 | return wp_logout_url(); |
| 274 | } |
| 275 | |
| 276 | $atts = shortcode_atts( |
| 277 | array( |
| 278 | 'class' => '', |
| 279 | 'id' => '', |
| 280 | 'title' => '', |
| 281 | 'label' => esc_html__('Log Out', 'wp-user-avatar'), |
| 282 | 'raw' => '', |
| 283 | ), |
| 284 | $atts |
| 285 | ); |
| 286 | |
| 287 | $class = 'class="' . esc_attr($atts['class']) . '"'; |
| 288 | $id = 'id="' . esc_attr($atts['id']) . '"'; |
| 289 | $label = esc_attr($atts['label']); |
| 290 | $title = 'title="' . esc_attr($atts['title']) . '"'; |
| 291 | |
| 292 | $html = '<a href="' . wp_logout_url() . '" ' . "$title $class $id" . '>' . $label . '</a>'; |
| 293 | |
| 294 | return $html; |
| 295 | |
| 296 | } |
| 297 | |
| 298 | /** |
| 299 | * URL to user edit page |
| 300 | */ |
| 301 | public static function link_edit_profile($atts) |
| 302 | { |
| 303 | if ( ! is_user_logged_in()) return; |
| 304 | |
| 305 | $atts = ppress_normalize_attributes($atts); |
| 306 | |
| 307 | $atts = shortcode_atts( |
| 308 | array( |
| 309 | 'class' => '', |
| 310 | 'id' => '', |
| 311 | 'title' => '', |
| 312 | 'label' => esc_html__('Edit Profile', 'wp-user-avatar'), |
| 313 | 'raw' => '', |
| 314 | ), |
| 315 | $atts |
| 316 | ); |
| 317 | |
| 318 | $class = 'class="' . esc_attr($atts['class']) . '"'; |
| 319 | $id = 'id="' . esc_attr($atts['id']) . '"'; |
| 320 | $label = esc_attr($atts['label']); |
| 321 | $title = 'title="' . esc_attr($atts['title']) . '"'; |
| 322 | |
| 323 | |
| 324 | $edit_profile_page_url = admin_url('profile.php'); |
| 325 | |
| 326 | $edit_profile_page_id = ppress_get_setting('edit_user_profile_url'); |
| 327 | |
| 328 | if ( ! empty($edit_profile_page_id)) { |
| 329 | $edit_profile_page_url = get_permalink($edit_profile_page_id); |
| 330 | } |
| 331 | |
| 332 | if ( ! empty($atts['raw']) && ($atts['raw'] == true)) { |
| 333 | return $edit_profile_page_url; |
| 334 | } |
| 335 | |
| 336 | $html = '<a href="' . $edit_profile_page_url . '" ' . "$title $class $id" . '>' . $label . '</a>'; |
| 337 | |
| 338 | return $html; |
| 339 | } |
| 340 | |
| 341 | /** |
| 342 | * Display avatar of currently logged in user |
| 343 | * |
| 344 | * @param $atts |
| 345 | * |
| 346 | * @return string |
| 347 | */ |
| 348 | public static function user_avatar($atts) |
| 349 | { |
| 350 | $atts = shortcode_atts( |
| 351 | array( |
| 352 | 'user' => '', |
| 353 | 'class' => '', |
| 354 | 'id' => '', |
| 355 | 'size' => 300, |
| 356 | 'alt' => '', |
| 357 | 'original' => false, |
| 358 | ), |
| 359 | $atts |
| 360 | ); |
| 361 | |
| 362 | $class = esc_attr($atts['class']); |
| 363 | $id = esc_attr($atts['id']); |
| 364 | $size = esc_attr(absint($atts['size'])); |
| 365 | $alt = esc_attr($atts['alt']); |
| 366 | $original = in_array($atts['original'], ['true', true], true); |
| 367 | |
| 368 | $user_id = self::$current_user->ID; |
| 369 | |
| 370 | if ( ! empty($atts['user'])) { |
| 371 | |
| 372 | $user_id = is_numeric($atts['user']) ? absint($atts['user']) : get_user_by('login', $atts['user']); |
| 373 | |
| 374 | if ($user_id instanceof \WP_User) { |
| 375 | $user_id = $user_id->ID; |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | return UserAvatar::get_avatar_img($user_id, $size, $alt, $class, $id, $original); |
| 380 | } |
| 381 | |
| 382 | public static function user_cover_image($atts) |
| 383 | { |
| 384 | $atts = shortcode_atts([ |
| 385 | 'user' => '', |
| 386 | 'class' => '', |
| 387 | 'id' => '', |
| 388 | 'alt' => '', |
| 389 | ], $atts); |
| 390 | |
| 391 | $class = esc_attr($atts['class']); |
| 392 | $id = esc_attr($atts['id']); |
| 393 | $alt = esc_attr($atts['alt']); |
| 394 | |
| 395 | if ( ! empty($id)) { |
| 396 | $id = " id='$id'"; |
| 397 | } |
| 398 | |
| 399 | $user_id = self::$current_user->ID; |
| 400 | |
| 401 | if ( ! empty($atts['user'])) { |
| 402 | |
| 403 | $user_id = is_numeric($atts['user']) ? absint($atts['user']) : get_user_by('login', $atts['user']); |
| 404 | |
| 405 | if ($user_id instanceof \WP_User) { |
| 406 | $user_id = $user_id->ID; |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | $url = ppress_get_cover_image_url($user_id); |
| 411 | |
| 412 | $avatar = "<img data-del=\"cover-image\" alt='{$alt}' src='{$url}' class='pp-user-cover-image {$class}'{$id}>"; |
| 413 | |
| 414 | return $avatar; |
| 415 | } |
| 416 | |
| 417 | /** |
| 418 | * Redirect non logged users to login page. |
| 419 | * |
| 420 | * @param array $atts |
| 421 | * |
| 422 | * @return false|string|void |
| 423 | */ |
| 424 | public static function redirect_non_logged_in_users($atts) |
| 425 | { |
| 426 | if (is_user_logged_in()) { |
| 427 | return; |
| 428 | } |
| 429 | |
| 430 | $atts = shortcode_atts( |
| 431 | array( |
| 432 | 'url' => '', |
| 433 | ), |
| 434 | $atts |
| 435 | ); |
| 436 | |
| 437 | $url = empty($atts['url']) ? ppress_login_url() : esc_url_raw($atts['url']); |
| 438 | |
| 439 | ob_start(); |
| 440 | ppress_content_http_redirect($url); |
| 441 | |
| 442 | return ob_get_clean(); |
| 443 | } |
| 444 | |
| 445 | |
| 446 | /** |
| 447 | * Redirect logged users to login page. |
| 448 | * |
| 449 | * @param array $atts |
| 450 | * |
| 451 | * @return false|string|void |
| 452 | */ |
| 453 | public static function redirect_logged_in_users($atts) |
| 454 | { |
| 455 | if ( ! is_user_logged_in()) { |
| 456 | return; |
| 457 | } |
| 458 | |
| 459 | $atts = shortcode_atts( |
| 460 | array( |
| 461 | 'url' => '', |
| 462 | ), |
| 463 | $atts |
| 464 | ); |
| 465 | |
| 466 | $url = empty($atts['url']) ? ppress_login_url() : esc_url_raw($atts['url']); |
| 467 | |
| 468 | ob_start(); |
| 469 | ppress_content_http_redirect($url); |
| 470 | |
| 471 | return ob_get_clean(); |
| 472 | } |
| 473 | |
| 474 | |
| 475 | /** |
| 476 | * Only logged user can view content. |
| 477 | * |
| 478 | * @param array $atts |
| 479 | * @param mixed $content |
| 480 | * |
| 481 | * @return mixed |
| 482 | */ |
| 483 | public static function pp_log_in_users($atts, $content) |
| 484 | { |
| 485 | if (is_user_logged_in()) { |
| 486 | return do_shortcode($content); |
| 487 | } |
| 488 | |
| 489 | return ''; |
| 490 | } |
| 491 | |
| 492 | |
| 493 | /** |
| 494 | * Only non-logged user can view content. |
| 495 | * |
| 496 | * @param array $atts |
| 497 | * @param mixed $content |
| 498 | * |
| 499 | * @return mixed |
| 500 | */ |
| 501 | public static function pp_non_log_in_users($atts, $content) |
| 502 | { |
| 503 | if ( ! is_user_logged_in()) { |
| 504 | return do_shortcode($content); |
| 505 | } |
| 506 | |
| 507 | return ''; |
| 508 | } |
| 509 | |
| 510 | |
| 511 | /** |
| 512 | * URL to topics started by users. |
| 513 | * |
| 514 | * @return string |
| 515 | */ |
| 516 | public static function bbp_topic_started_url() |
| 517 | { |
| 518 | if (function_exists('bbp_get_user_topics_created_url')) { |
| 519 | return esc_url_raw(bbp_get_user_topics_created_url(self::$current_user->ID)); |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | |
| 524 | /** |
| 525 | * URL to topics started by users. |
| 526 | * |
| 527 | * @return string |
| 528 | */ |
| 529 | public static function bbp_replies_created_url() |
| 530 | { |
| 531 | if (function_exists('bbp_user_replies_created_url')) { |
| 532 | return esc_url_raw(bbp_get_user_replies_created_url(self::$current_user->ID)); |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | |
| 537 | /** |
| 538 | * URL to topics started by users. |
| 539 | * |
| 540 | * @return string |
| 541 | */ |
| 542 | public static function bbp_favorites_url() |
| 543 | { |
| 544 | if (function_exists('bbp_get_favorites_permalink')) { |
| 545 | return esc_url_raw(bbp_get_favorites_permalink(self::$current_user->ID)); |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | |
| 550 | /** |
| 551 | * URL to topics started by users. |
| 552 | * |
| 553 | * @return string |
| 554 | */ |
| 555 | public static function bbp_subscriptions_url() |
| 556 | { |
| 557 | if (function_exists('bbp_get_subscriptions_permalink')) { |
| 558 | return esc_url(bbp_get_subscriptions_permalink(self::$current_user->ID)); |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | } |