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