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
RegistrationFormBuilder.php
265 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ProfilePress\Core\ShortcodeParser\Builder; |
| 4 | |
| 5 | use ProfilePress\Core\Classes\FormRepository; |
| 6 | |
| 7 | class RegistrationFormBuilder |
| 8 | { |
| 9 | public static function initialize() |
| 10 | { |
| 11 | $instance = new FieldsShortcodeCallback(FormRepository::REGISTRATION_TYPE); |
| 12 | |
| 13 | add_shortcode('reg-username', array($instance, 'username')); |
| 14 | add_shortcode('reg-password', array($instance, 'password')); |
| 15 | add_shortcode('reg-confirm-password', array($instance, 'confirm_password')); |
| 16 | add_shortcode('reg-email', array($instance, 'email')); |
| 17 | add_shortcode('reg-confirm-email', array($instance, 'confirm_email')); |
| 18 | add_shortcode('reg-website', array($instance, 'website')); |
| 19 | add_shortcode('reg-nickname', array($instance, 'nickname')); |
| 20 | add_shortcode('reg-display-name', array($instance, 'display_name')); |
| 21 | add_shortcode('reg-first-name', array($instance, 'first_name')); |
| 22 | add_shortcode('reg-last-name', array($instance, 'last_name')); |
| 23 | add_shortcode('reg-bio', array($instance, 'bio')); |
| 24 | add_shortcode('reg-avatar', array($instance, 'avatar')); |
| 25 | add_shortcode('reg-cover-image', array($instance, 'cover_image')); |
| 26 | add_shortcode('reg-cpf', array($instance, 'custom_profile_field')); |
| 27 | add_shortcode('reg-submit', array($instance, 'submit')); |
| 28 | add_shortcode('reg-password-meter', array(__CLASS__, 'password_meter')); |
| 29 | add_shortcode('reg-select-role', array(__CLASS__, 'select_role')); |
| 30 | |
| 31 | // custom fields shortcodes |
| 32 | add_shortcode('reg-text-box', array($instance, 'textbox_field')); |
| 33 | add_shortcode('reg-textarea', array($instance, 'textarea_field')); |
| 34 | add_shortcode('reg-select-dropdown', array($instance, 'select_dropdown_field')); |
| 35 | add_shortcode('reg-checkbox-list', array($instance, 'checkbox_list_field')); |
| 36 | add_shortcode('reg-single-checkbox', array($instance, 'single_checkbox_field')); |
| 37 | add_shortcode('reg-cf-password', array($instance, 'cf_password_field')); |
| 38 | add_shortcode('reg-country', array($instance, 'country_field')); |
| 39 | add_shortcode('reg-date-field', array($instance, 'date_field')); |
| 40 | add_shortcode('reg-number-field', array($instance, 'number_field')); |
| 41 | add_shortcode('reg-radio-buttons', array($instance, 'radio_buttons_field')); |
| 42 | |
| 43 | do_action('ppress_register_registration_form_shortcode'); |
| 44 | } |
| 45 | |
| 46 | public static function GET_POST() |
| 47 | { |
| 48 | return array_merge($_GET, $_POST); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Is field a required field? |
| 53 | * |
| 54 | * @param array $atts |
| 55 | * |
| 56 | * @return bool |
| 57 | */ |
| 58 | public static function is_field_required($atts) |
| 59 | { |
| 60 | $atts = ppress_normalize_attributes($atts); |
| 61 | |
| 62 | return isset($atts['required']) && ($atts['required'] === true || $atts['required'] == 'true' || $atts['required'] == '1'); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Rewrite custom field key to something more human readable. |
| 67 | * |
| 68 | * @param string $key field key |
| 69 | * |
| 70 | * @return string |
| 71 | */ |
| 72 | public static function human_readable_field_key($key) |
| 73 | { |
| 74 | return ucfirst(str_replace('_', ' ', $key)); |
| 75 | } |
| 76 | |
| 77 | public static function valid_field_atts($atts) |
| 78 | { |
| 79 | if ( ! is_array($atts)) { |
| 80 | return $atts; |
| 81 | } |
| 82 | |
| 83 | $invalid_atts = array('enforce', 'key', 'field_key', 'limit', 'options', 'checkbox_text'); |
| 84 | |
| 85 | $valid_atts = array(); |
| 86 | |
| 87 | foreach ($atts as $key => $value) { |
| 88 | if ( ! in_array($key, $invalid_atts)) { |
| 89 | $valid_atts[$key] = $value; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | return $valid_atts; |
| 94 | } |
| 95 | |
| 96 | public static function field_attributes($field_name, $atts, $required = 'false') |
| 97 | { |
| 98 | $_POST = self::GET_POST(); |
| 99 | |
| 100 | if ($field_name !== 'reg_submit') { |
| 101 | $atts['required'] = isset($atts['required']) ? $atts['required'] : $required; |
| 102 | } |
| 103 | |
| 104 | if ( ! in_array($field_name, ['ignore_value'])) { |
| 105 | $atts['value'] = isset($_POST[$field_name]) ? esc_attr($_POST[$field_name]) : @esc_attr($atts['value']); |
| 106 | } |
| 107 | |
| 108 | $output = []; |
| 109 | |
| 110 | foreach ($atts as $key => $value) { |
| 111 | if ($key != 'required' && ! empty($value)) { |
| 112 | $value = esc_attr($value); |
| 113 | $output[] = "$key=\"$value\""; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | $output = implode(' ', $output); |
| 118 | |
| 119 | if (self::is_field_required($atts)) { |
| 120 | $output .= ' required="required"'; |
| 121 | } |
| 122 | |
| 123 | return $output; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Password strength meter field. |
| 128 | * @see http://code.tutsplus.com/articles/using-the-included-password-strength-meter-script-in-wordpress--wp-34736 |
| 129 | */ |
| 130 | public static function password_meter($atts) |
| 131 | { |
| 132 | $atts['enforce'] = isset($atts['enforce']) ? $atts['enforce'] : 'true'; |
| 133 | |
| 134 | $attributes = self::field_attributes('ignore_value', self::valid_field_atts(ppress_normalize_attributes($atts))); |
| 135 | |
| 136 | wp_localize_script('password-strength-meter', 'pwsL10n', array( |
| 137 | 'empty' => esc_html__('Strength indicator', 'wp-user-avatar'), |
| 138 | 'short' => esc_html__('Very weak', 'wp-user-avatar'), |
| 139 | 'bad' => esc_html__('Weak', 'wp-user-avatar'), |
| 140 | 'good' => _x('Medium', 'password strength', 'wp-user-avatar'), |
| 141 | 'strong' => esc_html__('Strong', 'wp-user-avatar'), |
| 142 | 'mismatch' => esc_html__('Mismatch', 'wp-user-avatar'), |
| 143 | )); |
| 144 | |
| 145 | ob_start(); ?> |
| 146 | |
| 147 | <?php if ('true' == $atts['enforce']) : ?> |
| 148 | <input type="hidden" name="pp_enforce_password_meter" value="true"> |
| 149 | <?php endif; ?> |
| 150 | <div id="pp-pass-strength-result" <?php echo $attributes; ?>><?php _e('Strength indicator'); ?></div> |
| 151 | <script type="text/javascript"> |
| 152 | var pass_strength = 0; |
| 153 | jQuery(document).ready(function ($) { |
| 154 | var password1 = $('input[name=reg_password]'); |
| 155 | var password2 = $('input[name=reg_password2]'); |
| 156 | var submitButton = $('input[name=reg_submit]'); |
| 157 | var strengthMeterId = $('#pp-pass-strength-result'); |
| 158 | |
| 159 | $('body').on('keyup', 'input[name=reg_password], input[name=reg_password2]', function () { |
| 160 | pp_checkPasswordStrength(password1, password2, strengthMeterId, submitButton, []); |
| 161 | } |
| 162 | ); |
| 163 | |
| 164 | // set a time delay to enable the checker function to initialize |
| 165 | setTimeout(function () { |
| 166 | pp_checkPasswordStrength(password1, password2, strengthMeterId, submitButton, []); |
| 167 | }, 500); |
| 168 | |
| 169 | submitButton.click(function () { |
| 170 | $('input[name=pp_enforce_password_meter]').val(pass_strength); |
| 171 | }); |
| 172 | }); |
| 173 | |
| 174 | function pp_checkPasswordStrength($pass1, $pass2, $strengthResult, $submitButton, blacklistArray) { |
| 175 | var min_password_strength = <?php echo apply_filters('ppress_min_password_strength', 4); ?>; |
| 176 | var pass1 = $pass1.val(); |
| 177 | var pass2 = $pass2.val(); |
| 178 | |
| 179 | <?php if('true' == $atts['enforce']) : ?> |
| 180 | // Reset the form & meter |
| 181 | $submitButton.attr('disabled', 'disabled').css("opacity", ".4"); |
| 182 | <?php endif; ?> |
| 183 | $strengthResult.removeClass('short bad good strong'); |
| 184 | |
| 185 | // Extend our blacklist array with those from the inputs & site data |
| 186 | blacklistArray = blacklistArray.concat(wp.passwordStrength.userInputBlacklist()); |
| 187 | |
| 188 | // Get the password strength |
| 189 | var strength = wp.passwordStrength.meter(pass1, blacklistArray, pass2); |
| 190 | |
| 191 | // Add the strength meter results |
| 192 | switch (strength) { |
| 193 | case 1: |
| 194 | case 2: |
| 195 | $strengthResult.addClass('bad').html(pwsL10n.bad); |
| 196 | break; |
| 197 | case 3: |
| 198 | $strengthResult.addClass('good').html(pwsL10n.good); |
| 199 | if (min_password_strength === 3) { |
| 200 | pass_strength = 1; |
| 201 | } |
| 202 | break; |
| 203 | case 4: |
| 204 | $strengthResult.addClass('strong').html(pwsL10n.strong); |
| 205 | if (min_password_strength === 4) { |
| 206 | pass_strength = 1; |
| 207 | } |
| 208 | break; |
| 209 | case 5: |
| 210 | $strengthResult.addClass('short').html(pwsL10n.mismatch); |
| 211 | break; |
| 212 | default: |
| 213 | $strengthResult.addClass('short').html(pwsL10n.short); |
| 214 | } |
| 215 | |
| 216 | // The meter function returns a result even if pass2 is empty, |
| 217 | // enable only the submit button if the password is strong |
| 218 | <?php if('true' == $atts['enforce']) : ?> |
| 219 | if (min_password_strength <= strength) { |
| 220 | $submitButton.removeAttr('disabled').css("opacity", ""); |
| 221 | } |
| 222 | <?php endif; ?> |
| 223 | |
| 224 | return strength; |
| 225 | } |
| 226 | </script> |
| 227 | <?php |
| 228 | return apply_filters('ppress_registration_password_meter_field', ob_get_clean(), $atts); |
| 229 | } |
| 230 | |
| 231 | public static function select_role($atts) |
| 232 | { |
| 233 | $attributes = self::field_attributes('ignore_value', self::valid_field_atts(ppress_normalize_attributes($atts))); |
| 234 | |
| 235 | if ( ! empty($atts['options'])) { |
| 236 | $selectible_roles = array_map('trim', explode(',', $atts['options'])); |
| 237 | } |
| 238 | |
| 239 | if (isset($selectible_roles)) { |
| 240 | $wp_roles = array_filter(ppress_get_editable_roles(), function ($value) use ($selectible_roles) { |
| 241 | // get the array key of the $value value. |
| 242 | $key = array_search($value, ppress_get_editable_roles()); |
| 243 | |
| 244 | return in_array($key, $selectible_roles); |
| 245 | }); |
| 246 | } else { |
| 247 | $wp_roles = ppress_get_editable_roles(); |
| 248 | } |
| 249 | |
| 250 | $html = "<select name=\"reg_select_role\" $attributes>"; |
| 251 | |
| 252 | if (is_array($wp_roles)) { |
| 253 | foreach ($wp_roles as $key => $value) { |
| 254 | $_POST = self::GET_POST(); |
| 255 | $selected = selected(@$_POST['reg_select_role'], $key, false); |
| 256 | $label = $value['name']; |
| 257 | $html .= "<option value='$key' id='select_role_$key' class='select_role_option' $selected>$label</option>"; |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | $html .= '</select>'; |
| 262 | |
| 263 | return apply_filters('ppress_registration_select_role_field', $html, $atts); |
| 264 | } |
| 265 | } |