PageRenderTime 62ms CodeModel.GetById 35ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/fields/class-gf-field-password.php

https://gitlab.com/level-level/gravityforms
PHP | 178 lines | 133 code | 39 blank | 6 comment | 22 complexity | 7642e8e0bcbbc52303cdeff106d5cdac MD5 | raw file
  1. <?php
  2. if ( ! class_exists( 'GFForms' ) ) {
  3. die();
  4. }
  5. add_action( 'gform_after_submission', array( 'GF_Field_Password', 'delete_passwords' ), 100, 2 );
  6. class GF_Field_Password extends GF_Field {
  7. public $type = 'password';
  8. public function get_form_editor_field_title() {
  9. return esc_attr__( 'Password', 'gravityforms' );
  10. }
  11. function get_form_editor_field_settings() {
  12. return array(
  13. 'conditional_logic_field_setting',
  14. 'error_message_setting',
  15. 'label_setting',
  16. 'label_placement_setting',
  17. 'admin_label_setting',
  18. 'rules_setting',
  19. 'input_placeholders_setting',
  20. 'sub_labels_setting',
  21. 'sub_label_placement_setting',
  22. 'description_setting',
  23. 'css_class_setting',
  24. 'password_strength_setting',
  25. );
  26. }
  27. public function get_form_editor_button() {
  28. return array(); // this button is conditionally added in the form detail page
  29. }
  30. public function get_entry_inputs() {
  31. return null;
  32. }
  33. public function validate( $value, $form ) {
  34. $password = rgpost( 'input_' . $this->id );
  35. $confirm = rgpost( 'input_' . $this->id . '_2' );
  36. if ( $password != $confirm ) {
  37. $this->failed_validation = true;
  38. $this->validation_message = esc_html__( 'Your passwords do not match.', 'gravityforms' );
  39. } elseif ( $this->passwordStrengthEnabled && ! empty( $this->minPasswordStrength ) && ! empty( $password ) ) {
  40. $strength = $_POST[ 'input_' . $this->id . '_strength' ];
  41. $levels = array( 'short' => 1, 'bad' => 2, 'good' => 3, 'strong' => 4 );
  42. if ( $levels[ $strength ] < $levels[ $this->minPasswordStrength ] ) {
  43. $this->failed_validation = true;
  44. $this->validation_message = empty( $this->errorMessage ) ? sprintf( esc_html__( 'Your password does not meet the required strength. %sHint: To make it stronger, use upper and lower case letters, numbers and symbols like ! " ? $ %% ^ & ).', 'gravityforms' ), '<br />' ) : $this->errorMessage;
  45. }
  46. }
  47. }
  48. public function get_field_input( $form, $value = '', $entry = null ) {
  49. if ( is_array( $value ) ) {
  50. $value = array_values( $value );
  51. }
  52. $form_id = $form['id'];
  53. $is_entry_detail = $this->is_entry_detail();
  54. $is_form_editor = $this->is_form_editor();
  55. $is_admin = $is_entry_detail || $is_form_editor;
  56. $id = (int) $this->id;
  57. $field_id = $is_entry_detail || $is_form_editor || $form_id == 0 ? "input_$id" : 'input_' . $form_id . "_$id";
  58. $class_suffix = $is_entry_detail ? '_admin' : '';
  59. $form_sub_label_placement = rgar( $form, 'subLabelPlacement' );
  60. $field_sub_label_placement = $this->subLabelPlacement;
  61. $is_sub_label_above = $field_sub_label_placement == 'above' || ( empty( $field_sub_label_placement ) && $form_sub_label_placement == 'above' );
  62. $sub_label_class_attribute = $field_sub_label_placement == 'hidden_label' ? "class='hidden_sub_label screen-reader-text'" : '';
  63. $disabled_text = $is_form_editor ? 'disabled="disabled"' : '';
  64. $first_tabindex = $this->get_tabindex();
  65. $last_tabindex = $this->get_tabindex();
  66. $strength_style = ! $this->passwordStrengthEnabled ? "style='display:none;'" : '';
  67. $strength_indicator_label = esc_html__( 'Strength indicator', 'gravityforms' );
  68. $strength = $this->passwordStrengthEnabled || $is_admin ? "<div id='{$field_id}_strength_indicator' class='gfield_password_strength' {$strength_style}>
  69. {$strength_indicator_label}
  70. </div>
  71. <input type='hidden' class='gform_hidden' id='{$field_id}_strength' name='input_{$id}_strength' />" : '';
  72. $action = ! $is_admin ? "gformShowPasswordStrength(\"$field_id\");" : '';
  73. $onchange = $this->passwordStrengthEnabled ? "onchange='{$action}'" : '';
  74. $onkeyup = $this->passwordStrengthEnabled ? "onkeyup='{$action}'" : '';
  75. $confirmation_value = rgpost( 'input_' . $id . '_2' );
  76. $password_value = is_array( $value ) ? $value[0] : $value;
  77. $password_value = esc_attr( $password_value );
  78. $confirmation_value = esc_attr( $confirmation_value );
  79. $enter_password_field_input = GFFormsModel::get_input( $this, $this->id . '' );
  80. $confirm_password_field_input = GFFormsModel::get_input( $this, $this->id . '.2' );
  81. $enter_password_label = rgar( $enter_password_field_input, 'customLabel' ) != '' ? $enter_password_field_input['customLabel'] : esc_html__( 'Enter Password', 'gravityforms' );
  82. $enter_password_label = gf_apply_filters( array( 'gform_password', $form_id ), $enter_password_label, $form_id );
  83. $confirm_password_label = rgar( $confirm_password_field_input, 'customLabel' ) != '' ? $confirm_password_field_input['customLabel'] : esc_html__( 'Confirm Password', 'gravityforms' );
  84. $confirm_password_label = gf_apply_filters( array( 'gform_password_confirm', $form_id ), $confirm_password_label, $form_id );
  85. $required_attribute = $this->isRequired ? 'aria-required="true"' : '';
  86. $invalid_attribute = $this->failed_validation ? 'aria-invalid="true"' : 'aria-invalid="false"';
  87. $enter_password_placeholder_attribute = GFCommon::get_input_placeholder_attribute( $enter_password_field_input );
  88. $confirm_password_placeholder_attribute = GFCommon::get_input_placeholder_attribute( $confirm_password_field_input );
  89. if ( $is_sub_label_above ) {
  90. return "<div class='ginput_complex$class_suffix ginput_container ginput_container_password' id='{$field_id}_container'>
  91. <span id='{$field_id}_1_container' class='ginput_left'>
  92. <label for='{$field_id}' {$sub_label_class_attribute}>{$enter_password_label}</label>
  93. <input type='password' name='input_{$id}' id='{$field_id}' {$onkeyup} {$onchange} value='{$password_value}' {$first_tabindex} {$enter_password_placeholder_attribute} {$required_attribute} {$invalid_attribute} {$disabled_text}/>
  94. </span>
  95. <span id='{$field_id}_2_container' class='ginput_right'>
  96. <label for='{$field_id}_2' {$sub_label_class_attribute}>{$confirm_password_label}</label>
  97. <input type='password' name='input_{$id}_2' id='{$field_id}_2' {$onkeyup} {$onchange} value='{$confirmation_value}' {$last_tabindex} {$confirm_password_placeholder_attribute} {$required_attribute} {$invalid_attribute} {$disabled_text}/>
  98. </span>
  99. <div class='gf_clear gf_clear_complex'></div>
  100. </div>{$strength}";
  101. } else {
  102. return "<div class='ginput_complex$class_suffix ginput_container ginput_container_password' id='{$field_id}_container'>
  103. <span id='{$field_id}_1_container' class='ginput_left'>
  104. <input type='password' name='input_{$id}' id='{$field_id}' {$onkeyup} {$onchange} value='{$password_value}' {$first_tabindex} {$enter_password_placeholder_attribute} {$required_attribute} {$invalid_attribute} {$disabled_text}/>
  105. <label for='{$field_id}' {$sub_label_class_attribute}>{$enter_password_label}</label>
  106. </span>
  107. <span id='{$field_id}_2_container' class='ginput_right'>
  108. <input type='password' name='input_{$id}_2' id='{$field_id}_2' {$onkeyup} {$onchange} value='{$confirmation_value}' {$last_tabindex} {$confirm_password_placeholder_attribute} {$required_attribute} {$invalid_attribute} {$disabled_text}/>
  109. <label for='{$field_id}_2' {$sub_label_class_attribute}>{$confirm_password_label}</label>
  110. </span>
  111. <div class='gf_clear gf_clear_complex'></div>
  112. </div>{$strength}";
  113. }
  114. }
  115. public function get_field_label_class(){
  116. return 'gfield_label gfield_label_before_complex';
  117. }
  118. public function get_value_save_entry( $value, $form, $input_name, $lead_id, $lead ) {
  119. /**
  120. * A filter to allow the password to be encrypted (default set to false)
  121. *
  122. * @param bool Whether to encrypt the Password field with true or false
  123. * @param array $form The Current Form Object
  124. */
  125. $encrypt_password = apply_filters( 'gform_encrypt_password', false, $this, $form );
  126. if ( $encrypt_password ) {
  127. $value = GFCommon::encrypt( $value );
  128. GFFormsModel::set_encrypted_fields( $lead_id, $this->id );
  129. }
  130. return $value;
  131. }
  132. public static function delete_passwords( $entry, $form ) {
  133. $password_fields = GFAPI::get_fields_by_type( $form, array( 'password' ) );
  134. foreach ( $password_fields as $password_field ) {
  135. GFAPI::update_entry_field( $entry['id'], $password_field->id, '' );
  136. }
  137. }
  138. }
  139. GF_Fields::register( new GF_Field_Password() );