Builder
4 weeks ago
MyAccount
4 weeks ago
EditProfileTag.php
1 year ago
FormProcessor.php
1 year ago
FrontendProfileTag.php
1 year ago
Init.php
3 years ago
LoginFormTag.php
2 years ago
MelangeTag.php
1 year ago
MemberDirectoryTag.php
1 year ago
MembershipShortcodes.php
1 year ago
PasswordResetTag.php
4 weeks ago
RegistrationFormTag.php
1 year ago
index.php
5 years ago
FrontendProfileTag.php
213 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ProfilePress\Core\ShortcodeParser; |
| 4 | |
| 5 | use ProfilePress\Core\Classes\FormRepository as FR; |
| 6 | use ProfilePress\Core\ShortcodeParser\Builder\FrontendProfileBuilder; |
| 7 | |
| 8 | /** |
| 9 | * Parse the individual profile shortcode of "Edit profile" builder |
| 10 | */ |
| 11 | class FrontendProfileTag |
| 12 | { |
| 13 | public function __construct() |
| 14 | { |
| 15 | add_shortcode('profilepress-user-profile', array($this, 'user_profile_parser')); |
| 16 | |
| 17 | add_action('wp', array($this, 'set_up_detected_profile')); |
| 18 | |
| 19 | add_filter('pre_get_document_title', array($this, 'rewrite_profile_title'), 999999999, 1); |
| 20 | add_filter('wp_title', array($this, 'rewrite_profile_title'), 999999999, 1); |
| 21 | } |
| 22 | |
| 23 | public function set_up_detected_profile() |
| 24 | { |
| 25 | global $ppress_frontend_profile_user_obj; |
| 26 | |
| 27 | $who = get_query_var('who'); |
| 28 | |
| 29 | $user = ''; |
| 30 | |
| 31 | if (empty($who)) { |
| 32 | if (is_user_logged_in()) { |
| 33 | $user = wp_get_current_user(); |
| 34 | } else { |
| 35 | |
| 36 | if (strpos($_SERVER['REQUEST_URI'], '/' . ppress_get_profile_slug() . '/') !== false) { |
| 37 | nocache_headers(); |
| 38 | wp_safe_redirect(home_url()); |
| 39 | exit; |
| 40 | } |
| 41 | } |
| 42 | } else { |
| 43 | |
| 44 | $username_or_nicename = apply_filters('ppress_frontend_user_profile_username', rawurldecode($who)); |
| 45 | |
| 46 | // attempt to check if the slug is a nice-name and then retrieve the username of the user. |
| 47 | $check = ppress_is_slug_nice_name($username_or_nicename); |
| 48 | |
| 49 | if (is_string($check)) $username_or_nicename = $check; |
| 50 | |
| 51 | $user = get_user_by('login', $username_or_nicename); |
| 52 | |
| 53 | if ( ! $user) { |
| 54 | nocache_headers(); |
| 55 | wp_safe_redirect(home_url()); |
| 56 | exit; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | $user = apply_filters('ppress_frontend_profile_wp_user_object', $user); |
| 61 | |
| 62 | $ppress_frontend_profile_user_obj = $user; |
| 63 | |
| 64 | FrontendProfileBuilder::get_instance($user); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Shortcode callback function to parse the shortcode. |
| 69 | * |
| 70 | * @param $atts |
| 71 | * |
| 72 | * @return string |
| 73 | */ |
| 74 | public function user_profile_parser($atts) |
| 75 | { |
| 76 | global $ppress_frontend_profile_user_obj; |
| 77 | |
| 78 | if ( ! is_user_logged_in() && ppress_get_setting('disable_guests_can_view_profiles') == 'on') { |
| 79 | return wpautop(sprintf( |
| 80 | __('This content is available to members only. Please <a href="%1$s">login</a> or <a href="%2$s">register</a> to view this area.', 'wp-user-avatar'), |
| 81 | ppress_login_url(), |
| 82 | ppress_registration_url() |
| 83 | )); |
| 84 | } |
| 85 | |
| 86 | if (is_user_logged_in() && ppress_get_setting('disable_members_can_view_profiles') == 'on' && ! ppress_is_my_own_profile()) { |
| 87 | return wpautop(esc_html__('You are not authorized to access this area.', 'wp-user-avatar')); |
| 88 | } |
| 89 | |
| 90 | $id = absint($atts['id']); |
| 91 | |
| 92 | $user = ''; |
| 93 | |
| 94 | if ( ! empty($atts['user-id'])) { |
| 95 | |
| 96 | $user = apply_filters('ppress_frontend_profile_wp_user_object', get_user_by('ID', absint($atts['user-id']))); |
| 97 | |
| 98 | $ppress_frontend_profile_user_obj = $user; |
| 99 | |
| 100 | // we are instantiating the class directly because it has already been called by set_up_detected_profile hooked into wp |
| 101 | // action. If we had used the singleton instance method, it would have returned the previous instance. |
| 102 | new FrontendProfileBuilder($user); |
| 103 | } |
| 104 | |
| 105 | do_action('ppress_frond_end_profile_id', $id); |
| 106 | |
| 107 | $attribution_start = apply_filters('ppress_hide_attribution', '<!-- This WordPress front-end profile is built and powered by ProfilePress WordPress plugin - https://profilepress.com -->' . "\r\n"); |
| 108 | $attribution_end = apply_filters('ppress_hide_attribution', "\r\n" . '<!-- / ProfilePress WordPress plugin. -->' . "\r\n"); |
| 109 | $css = self::get_user_profile_css($id); |
| 110 | |
| 111 | // call the registration structure/design |
| 112 | return apply_filters('ppress_front_end_profile', $attribution_start . $css . $this->get_user_profile_structure($id) . $attribution_end, $user, $id); |
| 113 | } |
| 114 | |
| 115 | |
| 116 | /** |
| 117 | * Get the registration structure from the database |
| 118 | * |
| 119 | * @param int $id |
| 120 | * |
| 121 | * @return string |
| 122 | */ |
| 123 | public static function get_user_profile_structure($id) |
| 124 | { |
| 125 | if (FR::is_drag_drop($id, FR::USER_PROFILE_TYPE)) { |
| 126 | $form_instance = FR::dnd_class_instance($id, FR::USER_PROFILE_TYPE); |
| 127 | if ( ! $form_instance) return esc_html__('Form class not found. Please check if this user profile actually exist in ProfilePress.', 'wp-user-avatar'); |
| 128 | $user_profile_structure = $form_instance->form_structure(); |
| 129 | } else { |
| 130 | $user_profile_structure = FR::get_form_meta($id, FR::USER_PROFILE_TYPE, FR::FORM_STRUCTURE); |
| 131 | } |
| 132 | |
| 133 | return do_shortcode($user_profile_structure); |
| 134 | } |
| 135 | |
| 136 | |
| 137 | /** |
| 138 | * Get the CSS stylesheet for the ID registration |
| 139 | * |
| 140 | * @return mixed |
| 141 | */ |
| 142 | public static function get_user_profile_css($id) |
| 143 | { |
| 144 | if (FR::is_drag_drop($id, FR::USER_PROFILE_TYPE)) { |
| 145 | $form_instance = FR::dnd_class_instance($id, FR::USER_PROFILE_TYPE); |
| 146 | if ( ! $form_instance) return ''; |
| 147 | $user_profile_css = $form_instance->minified_form_css(); |
| 148 | } else { |
| 149 | $user_profile_css = FR::get_form_meta($id, FR::USER_PROFILE_TYPE, FR::FORM_CSS); |
| 150 | } |
| 151 | |
| 152 | return "<style type=\"text/css\">\r\n $user_profile_css \r\n</style>"; |
| 153 | } |
| 154 | |
| 155 | /** Rewrite the title of the profile */ |
| 156 | public function rewrite_profile_title($title) |
| 157 | { |
| 158 | global $post, $ppress_frontend_profile_user_obj; |
| 159 | |
| 160 | // if currently viewed page is the page with the front-end profile, rewrite the title accordingly. |
| 161 | if ((isset($post->ID) && !empty(ppress_get_setting('set_user_profile_shortcode')) && $post->ID == ppress_get_setting('set_user_profile_shortcode')) |
| 162 | || ppress_post_content_has_shortcode('profilepress-user-profile') |
| 163 | ) { |
| 164 | |
| 165 | $user_object = $ppress_frontend_profile_user_obj; |
| 166 | |
| 167 | if (isset($user_object) && is_object($user_object)) { |
| 168 | |
| 169 | // if first and last name is set, use the combo as title |
| 170 | if ( ! empty($user_object->first_name) && ! empty($user_object->last_name)) { |
| 171 | $title = "$user_object->first_name {$user_object->last_name}"; |
| 172 | } // if either first or last name is set, use either as title |
| 173 | elseif ( ! empty($user_object->first_name) || ! empty($user_object->last_name)) { |
| 174 | $title = "$user_object->first_name {$user_object->last_name}"; |
| 175 | } // else use their username |
| 176 | else { |
| 177 | $title = $user_object->user_login; |
| 178 | } |
| 179 | |
| 180 | $title = apply_filters('ppress_profile_username_title', self::title_possessiveness($title), $title); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | return $title; |
| 185 | } |
| 186 | |
| 187 | public static function title_possessiveness($string) |
| 188 | { |
| 189 | $string = trim($string); |
| 190 | $lastchar = substr($string, -1); |
| 191 | |
| 192 | $profile_string = esc_html__('Profile', 'wp-user-avatar'); |
| 193 | |
| 194 | if ('s' == $lastchar) { |
| 195 | $title = ucwords($string) . "' $profile_string"; |
| 196 | } else { |
| 197 | $title = ucwords($string) . "'s $profile_string"; |
| 198 | } |
| 199 | |
| 200 | return $title; |
| 201 | } |
| 202 | |
| 203 | public static function get_instance() |
| 204 | { |
| 205 | static $instance = false; |
| 206 | |
| 207 | if ( ! $instance) { |
| 208 | $instance = new self; |
| 209 | } |
| 210 | |
| 211 | return $instance; |
| 212 | } |
| 213 | } |