EditProfileBuilder.php
5 years ago
FieldsShortcodeCallback.php
4 years ago
FrontendProfileBuilder.php
4 years ago
GlobalShortcodes.php
4 years ago
LoginFormBuilder.php
5 years ago
PasswordResetBuilder.php
5 years ago
RegistrationFormBuilder.php
5 years ago
index.php
5 years ago
GlobalShortcodes.php
548 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 = $GLOBALS['pp_melange_form_id']; |
| 59 | $redirect = $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 | return do_shortcode(stripslashes(wp_kses_post($atts['custom_html']))); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Registration url |
| 160 | */ |
| 161 | public static function link_registration($atts) |
| 162 | { |
| 163 | $atts = ppress_normalize_attributes($atts); |
| 164 | |
| 165 | if (( ! empty($atts['raw']) && ($atts['raw'] == true))) { |
| 166 | return wp_registration_url(); |
| 167 | } |
| 168 | |
| 169 | $atts = shortcode_atts( |
| 170 | array( |
| 171 | 'class' => '', |
| 172 | 'id' => '', |
| 173 | 'title' => '', |
| 174 | 'label' => esc_html__('Sign Up', 'wp-user-avatar'), |
| 175 | 'raw' => '', |
| 176 | ), |
| 177 | $atts |
| 178 | ); |
| 179 | |
| 180 | $class = 'class="' . $atts['class'] . '"'; |
| 181 | $id = 'id="' . $atts['id'] . '"'; |
| 182 | $label = $atts['label']; |
| 183 | $title = 'title="' . $atts['title'] . '"'; |
| 184 | |
| 185 | |
| 186 | $html = '<a href="' . wp_registration_url() . "\" {$title} {$class} {$id}>$label</a>"; |
| 187 | |
| 188 | return $html; |
| 189 | } |
| 190 | |
| 191 | /** Lost password url */ |
| 192 | public static function link_lost_password($atts) |
| 193 | { |
| 194 | $atts = ppress_normalize_attributes($atts); |
| 195 | |
| 196 | |
| 197 | if (( ! empty($atts['raw']) && ($atts['raw'] == true))) { |
| 198 | return wp_lostpassword_url(); |
| 199 | } |
| 200 | |
| 201 | $atts = shortcode_atts( |
| 202 | array( |
| 203 | 'class' => '', |
| 204 | 'id' => '', |
| 205 | 'title' => '', |
| 206 | 'label' => esc_html__('Reset Password', 'wp-user-avatar'), |
| 207 | 'raw' => '', |
| 208 | ), |
| 209 | $atts |
| 210 | ); |
| 211 | |
| 212 | $class = 'class="' . $atts['class'] . '"'; |
| 213 | $id = 'id="' . $atts['id'] . '"'; |
| 214 | $label = $atts['label']; |
| 215 | $title = 'title="' . $atts['title'] . '"'; |
| 216 | |
| 217 | $html = "<a href=\"" . wp_lostpassword_url() . "\" {$title} {$class} {$id}>$label</a>"; |
| 218 | |
| 219 | return $html; |
| 220 | } |
| 221 | |
| 222 | |
| 223 | /** Login url */ |
| 224 | public static function link_login($atts) |
| 225 | { |
| 226 | $atts = ppress_normalize_attributes($atts); |
| 227 | |
| 228 | if (( ! empty($atts['raw']) && ($atts['raw'] == true))) { |
| 229 | return wp_login_url(); |
| 230 | } |
| 231 | |
| 232 | $atts = shortcode_atts( |
| 233 | array( |
| 234 | 'class' => '', |
| 235 | 'id' => '', |
| 236 | 'title' => '', |
| 237 | 'label' => esc_html__('Login', 'wp-user-avatar'), |
| 238 | 'raw' => '', |
| 239 | ), |
| 240 | $atts |
| 241 | ); |
| 242 | |
| 243 | $class = 'class="' . $atts['class'] . '"'; |
| 244 | $id = 'id="' . $atts['id'] . '"'; |
| 245 | $label = $atts['label']; |
| 246 | $title = 'title="' . $atts['title'] . '"'; |
| 247 | |
| 248 | $html = '<a href="' . wp_login_url() . '" ' . "$title $class $id" . '>' . $label . '</a>'; |
| 249 | |
| 250 | return $html; |
| 251 | } |
| 252 | |
| 253 | /** Logout URL */ |
| 254 | public static function link_logout($atts) |
| 255 | { |
| 256 | if ( ! is_user_logged_in()) { |
| 257 | return; |
| 258 | } |
| 259 | |
| 260 | $atts = ppress_normalize_attributes($atts); |
| 261 | |
| 262 | if (( ! empty($atts['raw']) && ($atts['raw'] == true))) { |
| 263 | return wp_logout_url(); |
| 264 | } |
| 265 | |
| 266 | $atts = shortcode_atts( |
| 267 | array( |
| 268 | 'class' => '', |
| 269 | 'id' => '', |
| 270 | 'title' => '', |
| 271 | 'label' => esc_html__('Log Out', 'wp-user-avatar'), |
| 272 | 'raw' => '', |
| 273 | ), |
| 274 | $atts |
| 275 | ); |
| 276 | |
| 277 | $class = 'class="' . $atts['class'] . '"'; |
| 278 | $id = 'id="' . $atts['id'] . '"'; |
| 279 | $label = $atts['label']; |
| 280 | $title = 'title="' . $atts['title'] . '"'; |
| 281 | |
| 282 | $html = '<a href="' . wp_logout_url() . '" ' . "$title $class $id" . '>' . $label . '</a>'; |
| 283 | |
| 284 | return $html; |
| 285 | |
| 286 | } |
| 287 | |
| 288 | /** |
| 289 | * URL to user edit page |
| 290 | */ |
| 291 | public static function link_edit_profile($atts) |
| 292 | { |
| 293 | if ( ! is_user_logged_in()) return; |
| 294 | |
| 295 | $atts = ppress_normalize_attributes($atts); |
| 296 | |
| 297 | $atts = shortcode_atts( |
| 298 | array( |
| 299 | 'class' => '', |
| 300 | 'id' => '', |
| 301 | 'title' => '', |
| 302 | 'label' => esc_html__('Edit Profile', 'wp-user-avatar'), |
| 303 | 'raw' => '', |
| 304 | ), |
| 305 | $atts |
| 306 | ); |
| 307 | |
| 308 | $class = 'class="' . $atts['class'] . '"'; |
| 309 | $id = 'id="' . $atts['id'] . '"'; |
| 310 | $label = $atts['label']; |
| 311 | $title = 'title="' . $atts['title'] . '"'; |
| 312 | |
| 313 | |
| 314 | $edit_profile_page_url = admin_url('profile.php'); |
| 315 | |
| 316 | $edit_profile_page_id = ppress_get_setting('edit_user_profile_url'); |
| 317 | |
| 318 | if ( ! empty($edit_profile_page_id)) { |
| 319 | $edit_profile_page_url = get_permalink($edit_profile_page_id); |
| 320 | } |
| 321 | |
| 322 | if ( ! empty($atts['raw']) && ($atts['raw'] == true)) { |
| 323 | return $edit_profile_page_url; |
| 324 | } |
| 325 | |
| 326 | $html = '<a href="' . $edit_profile_page_url . '" ' . "$title $class $id" . '>' . $label . '</a>'; |
| 327 | |
| 328 | return $html; |
| 329 | } |
| 330 | |
| 331 | /** |
| 332 | * Display avatar of currently logged in user |
| 333 | * |
| 334 | * @param $atts |
| 335 | * |
| 336 | * @return string |
| 337 | */ |
| 338 | public static function user_avatar($atts) |
| 339 | { |
| 340 | $atts = shortcode_atts( |
| 341 | array( |
| 342 | 'user' => '', |
| 343 | 'class' => '', |
| 344 | 'id' => '', |
| 345 | 'size' => 300, |
| 346 | 'alt' => '', |
| 347 | 'original' => false, |
| 348 | ), |
| 349 | $atts |
| 350 | ); |
| 351 | |
| 352 | $class = $atts['class']; |
| 353 | $id = $atts['id']; |
| 354 | $size = absint($atts['size']); |
| 355 | $alt = $atts['alt']; |
| 356 | $original = in_array($atts['original'], ['true', true], true); |
| 357 | |
| 358 | $user_id = self::$current_user->ID; |
| 359 | |
| 360 | if ( ! empty($atts['user'])) { |
| 361 | |
| 362 | $user_id = is_numeric($atts['user']) ? absint($atts['user']) : get_user_by('login', $atts['user']); |
| 363 | |
| 364 | if ($user_id instanceof \WP_User) { |
| 365 | $user_id = $user_id->ID; |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | return UserAvatar::get_avatar_img($user_id, $size, $alt, $class, $id, $original); |
| 370 | } |
| 371 | |
| 372 | public static function user_cover_image($atts) |
| 373 | { |
| 374 | $atts = shortcode_atts([ |
| 375 | 'user' => '', |
| 376 | 'class' => '', |
| 377 | 'id' => '', |
| 378 | 'alt' => '', |
| 379 | ], $atts); |
| 380 | |
| 381 | $class = $atts['class']; |
| 382 | $id = $atts['id']; |
| 383 | $alt = sanitize_text_field($atts['alt']); |
| 384 | |
| 385 | if ( ! empty($id)) { |
| 386 | $id = " id='$id'"; |
| 387 | } |
| 388 | |
| 389 | $user_id = self::$current_user->ID; |
| 390 | |
| 391 | if ( ! empty($atts['user'])) { |
| 392 | |
| 393 | $user_id = is_numeric($atts['user']) ? absint($atts['user']) : get_user_by('login', $atts['user']); |
| 394 | |
| 395 | if ($user_id instanceof \WP_User) { |
| 396 | $user_id = $user_id->ID; |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | $url = ppress_get_cover_image_url($user_id); |
| 401 | |
| 402 | $avatar = "<img data-del=\"cover-image\" alt='{$alt}' src='{$url}' class='pp-user-cover-image {$class}'{$id}>"; |
| 403 | |
| 404 | return $avatar; |
| 405 | } |
| 406 | |
| 407 | /** |
| 408 | * Redirect non logged users to login page. |
| 409 | * |
| 410 | * @param array $atts |
| 411 | * |
| 412 | * @return false|string|void |
| 413 | */ |
| 414 | public static function redirect_non_logged_in_users($atts) |
| 415 | { |
| 416 | if (is_user_logged_in()) { |
| 417 | return; |
| 418 | } |
| 419 | |
| 420 | $atts = shortcode_atts( |
| 421 | array( |
| 422 | 'url' => '', |
| 423 | ), |
| 424 | $atts |
| 425 | ); |
| 426 | |
| 427 | $url = empty($atts['url']) ? ppress_login_url() : esc_url_raw($atts['url']); |
| 428 | |
| 429 | ob_start(); |
| 430 | ppress_content_http_redirect($url); |
| 431 | |
| 432 | return ob_get_clean(); |
| 433 | } |
| 434 | |
| 435 | |
| 436 | /** |
| 437 | * Redirect logged users to login page. |
| 438 | * |
| 439 | * @param array $atts |
| 440 | * |
| 441 | * @return false|string|void |
| 442 | */ |
| 443 | public static function redirect_logged_in_users($atts) |
| 444 | { |
| 445 | if ( ! is_user_logged_in()) { |
| 446 | return; |
| 447 | } |
| 448 | |
| 449 | $atts = shortcode_atts( |
| 450 | array( |
| 451 | 'url' => '', |
| 452 | ), |
| 453 | $atts |
| 454 | ); |
| 455 | |
| 456 | $url = empty($atts['url']) ? ppress_login_url() : esc_url_raw($atts['url']); |
| 457 | |
| 458 | ob_start(); |
| 459 | ppress_content_http_redirect($url); |
| 460 | |
| 461 | return ob_get_clean(); |
| 462 | } |
| 463 | |
| 464 | |
| 465 | /** |
| 466 | * Only logged user can view content. |
| 467 | * |
| 468 | * @param array $atts |
| 469 | * @param mixed $content |
| 470 | * |
| 471 | * @return mixed |
| 472 | */ |
| 473 | public static function pp_log_in_users($atts, $content) |
| 474 | { |
| 475 | if (is_user_logged_in()) { |
| 476 | return do_shortcode($content); |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | |
| 481 | /** |
| 482 | * Only non-logged user can view content. |
| 483 | * |
| 484 | * @param array $atts |
| 485 | * @param mixed $content |
| 486 | * |
| 487 | * @return mixed |
| 488 | */ |
| 489 | public static function pp_non_log_in_users($atts, $content) |
| 490 | { |
| 491 | if ( ! is_user_logged_in()) { |
| 492 | return do_shortcode($content); |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | |
| 497 | /** |
| 498 | * URL to topics started by users. |
| 499 | * |
| 500 | * @return string |
| 501 | */ |
| 502 | public static function bbp_topic_started_url() |
| 503 | { |
| 504 | if (function_exists('bbp_get_user_topics_created_url')) { |
| 505 | return esc_url_raw(bbp_get_user_topics_created_url(self::$current_user->ID)); |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | |
| 510 | /** |
| 511 | * URL to topics started by users. |
| 512 | * |
| 513 | * @return string |
| 514 | */ |
| 515 | public static function bbp_replies_created_url() |
| 516 | { |
| 517 | if (function_exists('bbp_user_replies_created_url')) { |
| 518 | return esc_url_raw(bbp_get_user_replies_created_url(self::$current_user->ID)); |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | |
| 523 | /** |
| 524 | * URL to topics started by users. |
| 525 | * |
| 526 | * @return string |
| 527 | */ |
| 528 | public static function bbp_favorites_url() |
| 529 | { |
| 530 | if (function_exists('bbp_get_favorites_permalink')) { |
| 531 | return esc_url_raw(bbp_get_favorites_permalink(self::$current_user->ID)); |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | |
| 536 | /** |
| 537 | * URL to topics started by users. |
| 538 | * |
| 539 | * @return string |
| 540 | */ |
| 541 | public static function bbp_subscriptions_url() |
| 542 | { |
| 543 | if (function_exists('bbp_get_subscriptions_permalink')) { |
| 544 | return esc_url(bbp_get_subscriptions_permalink(self::$current_user->ID)); |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | } |