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