Changeset 1859796
- Timestamp:
- 04/17/2018 02:07:12 PM (8 years ago)
- Location:
- cf7-field-validation/trunk
- Files:
-
- 2 edited
-
cf7-custom-validation.php (modified) (2 diffs)
-
readme.txt (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
cf7-field-validation/trunk/cf7-custom-validation.php
r1859710 r1859796 4 4 Plugin URI: https://wordpress.org/plugins/cf7-field-validation/ 5 5 Description: Contact Form 7 validation messages provide custom error messages for each field. 6 Version: 1.1. 26 Version: 1.1.3 7 7 Author: Aiyaz, maheshpatel 8 8 Author URI: https://profiles.wordpress.org/aiyaz … … 10 10 */ 11 11 12 add_action( 'wpcf7_save_contact_form', 'action_cf7cv_save_contact_form', 9, 1);13 function action_cf7cv_save_contact_form( $contact_form)12 add_action( 'wpcf7_save_contact_form', 'action_cf7cv_save_contact_form', 9, 1 ); 13 function action_cf7cv_save_contact_form( $contact_form ) 14 14 { 15 $tags = $contact_form->scan_form_tags(); 16 $post_id = $contact_form->id(); 17 18 foreach ($tags as $value) { 19 if ($value['type'] == 'text*' || $value['type'] == 'email*' || $value['type'] == 'textarea*' || $value['type'] == 'tel*' || $value['type'] == 'url*' || $value['type'] == 'checkbox*' || $value['type'] == 'file*' || $value['type'] == 'date*' || $value['type'] == 'radio*' || $value['type'] == 'number*') { 20 $key = "_cf7cm_" . $value['name'] . "-valid"; 21 update_post_meta($post_id, $key, $value['name']); 22 } 23 } 24 } 25 26 add_action('wpcf7_after_create', 'action_wpcf7_after_create', 9, 1); 27 function action_wpcf7_after_create($instance) 15 $tags = $contact_form->scan_form_tags(); 16 $post_id = $contact_form->id(); 17 18 foreach ($tags as $value) 19 { 20 if($value['type'] == 'text*' || $value['type'] == 'email*' || $value['type'] == 'textarea*' || $value['type'] == 'tel*' 21 || $value['type'] == 'url*' || $value['type'] == 'checkbox*' || $value['type'] == 'file*' || $value['type'] == 'date*' || $value['type'] == 'radio' 22 || $value['type'] == 'number*'){ 23 $key = "_cf7cm_".$value['name']."-valid"; 24 update_post_meta($post_id,$key, $value['name']); 25 } 26 } 27 } 28 29 add_action( 'wpcf7_after_create', 'action_wpcf7_after_create', 9, 1 ); 30 function action_wpcf7_after_create( $instance ) 28 31 { 29 $tags = $instance->form_scan_shortcode(); 30 $post_id = $instance->id(); 31 32 foreach ($tags as $value) { 33 34 if ($value['type'] == 'text*' || $value['type'] == 'email*' || $value['type'] == 'textarea*' || $value['type'] == 'tel*' || $value['type'] == 'url*' || $value['type'] == 'checkbox*' || $value['type'] == 'file*' || $value['type'] == 'date*' || $value['type'] == 'radio*' || $value['type'] == 'number*') { 35 $key = "_cf7cm_" . $value['name'] . "-valid"; 36 update_post_meta($post_id, $key, $value['name']); 37 } 38 } 39 } 40 41 function get_meta_values($p_id = '', $key = '') 42 { 43 32 $tags = $instance->form_scan_shortcode(); 33 $post_id = $instance->id(); 34 35 foreach ($tags as $value) { 36 37 if($value['type'] == 'text*' || $value['type'] == 'email*' || $value['type'] == 'textarea*' || $value['type'] == 'tel*' 38 || $value['type'] == 'url*' || $value['type'] == 'checkbox*' || $value['type'] == 'file*' || $value['type'] == 'date*' || $value['type'] == 'radio' 39 || $value['type'] == 'number*'){ 40 $key = "_cf7cm_".$value['name']."-valid"; 41 update_post_meta($post_id,$key, $value['name']); 42 } 43 } 44 } 45 46 function get_meta_values($p_id ='', $key = '') { 47 44 48 global $wpdb; 45 if (empty($key))49 if( empty( $key ) ) 46 50 return; 47 48 $r = $wpdb->get_results( "SELECT pm.meta_value FROM {$wpdb->postmeta} pm WHERE pm.meta_key LIKE '%$key%' AND pm.post_id = $p_id ");49 51 52 $r = $wpdb->get_results( "SELECT pm.meta_value FROM {$wpdb->postmeta} pm WHERE pm.meta_key LIKE '%$key%' AND pm.post_id = $p_id "); 53 50 54 return $r; 51 55 } 52 56 53 57 54 function cf7cv_custom_validation_messages($messages) 55 { 56 57 if (isset($_GET['post']) && !empty($_GET['post'])) { 58 $p_id = $_GET['post']; 59 $p_val = get_meta_values($p_id, '_cf7cm_'); 60 61 foreach ($p_val as $value) { 62 $key = $value->meta_value; 63 $newmsg = array( 64 'description' => __("Error message for $value->meta_value field", 'contact-form-7'), 65 'default' => __("Please fill in the required field.", 'contact-form-7') 66 ); 67 68 $messages[$key] = $newmsg; 69 } 70 } 71 return $messages; 72 } 73 74 add_filter('wpcf7_messages', 'cf7cv_custom_validation_messages', 10, 1); 58 function cf7cv_custom_validation_messages( $messages ) { 59 60 if(isset($_GET['post']) && !empty($_GET['post']) ) 61 { 62 $p_id = $_GET['post']; 63 $p_val = get_meta_values($p_id, '_cf7cm_'); 64 65 foreach ($p_val as $value) { 66 $key = $value->meta_value; 67 $newmsg = array( 68 'description' => __( "Error message for $value->meta_value field", 'contact-form-7' ), 69 'default' => __( "Please fill in the required field.", 'contact-form-7' )); 70 71 $messages[$key] = $newmsg ; 72 } 73 } 74 return $messages; 75 } 76 77 add_filter( 'wpcf7_messages', 'cf7cv_custom_validation_messages', 10, 1 ); 75 78 76 79 /* Validation filter */ 77 80 78 add_filter('wpcf7_validate_text', 'cf7cv_custom_form_validation', 10, 2); 79 add_filter('wpcf7_validate_text*', 'cf7cv_custom_form_validation', 10, 2); 80 add_filter('wpcf7_validate_email', 'cf7cv_custom_form_validation', 10, 2); 81 add_filter('wpcf7_validate_email*', 'cf7cv_custom_form_validation', 10, 2); 82 add_filter('wpcf7_validate_url', 'cf7cv_custom_form_validation', 10, 2); 83 add_filter('wpcf7_validate_url*', 'cf7cv_custom_form_validation', 10, 2); 84 add_filter('wpcf7_validate_tel', 'cf7cv_custom_form_validation', 10, 2); 85 add_filter('wpcf7_validate_tel*', 'cf7cv_custom_form_validation', 10, 2); 86 add_filter('wpcf7_validate_textarea', 'cf7cv_custom_form_validation', 10, 2); 87 add_filter('wpcf7_validate_textarea*', 'cf7cv_custom_form_validation', 10, 2); 88 add_filter('wpcf7_validate_number', 'cf7cv_custom_form_validation', 10, 2); 89 add_filter('wpcf7_validate_number*', 'cf7cv_custom_form_validation', 10, 2); 90 add_filter('wpcf7_validate_range', 'cf7cv_custom_form_validation', 10, 2); 91 add_filter('wpcf7_validate_range*', 'cf7cv_custom_form_validation', 10, 2); 92 add_filter('wpcf7_validate_date', 'cf7cv_custom_form_validation', 10, 2); 93 add_filter('wpcf7_validate_date*', 'cf7cv_custom_form_validation', 10, 2); 94 add_filter('wpcf7_validate_checkbox', 'cf7cv_custom_form_validation', 10, 2); 95 add_filter('wpcf7_validate_checkbox*', 'cf7cv_custom_form_validation', 10, 2); 96 add_filter('wpcf7_validate_radio', 'cf7cv_custom_form_validation', 10, 2); 97 add_filter('wpcf7_validate_file', 'cf7cv_custom_form_validation', 10, 2); 98 add_filter('wpcf7_validate_file*', 'cf7cv_custom_form_validation', 10, 2); 99 100 function cf7cv_custom_form_validation($result, $tag) 101 { 102 103 $name = $tag->name; 104 $check_empty = wpcf7_get_message($name); 105 106 if (empty($check_empty)) { 107 $name = "invalid_required"; 108 } 109 110 if ('text' == $tag->basetype) { 111 112 $value = isset($_POST[$name]) ? trim(wp_unslash(strtr((string) $_POST[$name], "\n", " "))) : ''; 113 if ($tag->is_required() && '' == $value) { 114 $result->invalidate($tag, wpcf7_get_message($name)); 115 } 116 } 117 118 if ('email' == $tag->basetype) { 119 120 $value = isset($_POST[$name]) ? trim(wp_unslash(strtr((string) $_POST[$name], "\n", " "))) : ''; 121 if ($tag->is_required() && '' == $value) { 122 $result->invalidate($tag, wpcf7_get_message($name)); 123 } 124 if ('' != $value && !wpcf7_is_email($value)) { 125 $result->invalidate($tag, wpcf7_get_message('invalid_email')); 126 } 127 } 128 129 if ('url' == $tag->basetype) { 130 131 $value = isset($_POST[$name]) ? trim(wp_unslash(strtr((string) $_POST[$name], "\n", " "))) : ''; 132 if ($tag->is_required() && '' == $value) { 133 $result->invalidate($tag, wpcf7_get_message($name)); 134 } elseif ('' != $value && !wpcf7_is_url($value)) { 135 $result->invalidate($tag, wpcf7_get_message('invalid_url')); 136 } 137 } 138 139 if ('tel' == $tag->basetype) { 140 141 $value = isset($_POST[$name]) ? trim(wp_unslash(strtr((string) $_POST[$name], "\n", " "))) : ''; 142 if ($tag->is_required() && '' == $value) { 143 $result->invalidate($tag, wpcf7_get_message($name)); 144 } elseif ('' != $value && !wpcf7_is_tel($value)) { 145 $result->invalidate($tag, wpcf7_get_message('invalid_tel')); 146 } 147 } 148 149 if ('number' == $tag->basetype) { 150 151 $value = isset($_POST[$name]) ? trim(wp_unslash(strtr((string) $_POST[$name], "\n", " "))) : ''; 152 $min = $tag->get_option('min', 'signed_int', true); 153 $max = $tag->get_option('max', 'signed_int', true); 154 155 if ($tag->is_required() && '' == $value) { 156 $result->invalidate($tag, wpcf7_get_message($name)); 157 } elseif ('' != $value && !wpcf7_is_number($value)) { 158 $result->invalidate($tag, wpcf7_get_message($name)); 159 $result->invalidate($tag, wpcf7_get_message($name)); 160 } elseif ('' != $value && '' != $min && (float) $value < (float) $min) { 161 $result->invalidate($tag, wpcf7_get_message('number_too_small')); 162 } elseif ('' != $value && '' != $max && (float) $max < (float) $value) { 163 $result->invalidate($tag, wpcf7_get_message('number_too_large')); 164 } 165 } 166 if ('date' == $tag->basetype) { 167 168 $min = $tag->get_date_option('min'); 169 $max = $tag->get_date_option('max'); 170 171 $value = isset($_POST[$name]) ? trim(strtr((string) $_POST[$name], "\n", " ")) : ''; 172 173 if ($tag->is_required() && '' == $value) { 174 $result->invalidate($tag, wpcf7_get_message($name)); 175 } elseif ('' != $value && !wpcf7_is_date($value)) { 176 $result->invalidate($tag, wpcf7_get_message('invalid_date')); 177 } elseif ('' != $value && !empty($min) && $value < $min) { 178 $result->invalidate($tag, wpcf7_get_message('date_too_early')); 179 } elseif ('' != $value && !empty($max) && $max < $value) { 180 $result->invalidate($tag, wpcf7_get_message('date_too_late')); 181 } 182 } 183 184 if ('textarea' == $tag->basetype) { 185 $value = isset($_POST[$name]) ? (string) $_POST[$name] : ''; 186 187 if ($tag->is_required() && '' == $value) { 188 $result->invalidate($tag, wpcf7_get_message($name)); 189 } 190 191 if ('' !== $value) { 192 $maxlength = $tag->get_maxlength_option(); 193 $minlength = $tag->get_minlength_option(); 194 195 if ($maxlength && $minlength && $maxlength < $minlength) { 196 $maxlength = $minlength = null; 197 } 198 199 $code_units = wpcf7_count_code_units(stripslashes($value)); 200 201 if (false !== $code_units) { 202 if ($maxlength && $maxlength < $code_units) { 203 $result->invalidate($tag, wpcf7_get_message('invalid_too_long')); 204 } elseif ($minlength && $code_units < $minlength) { 205 $result->invalidate($tag, wpcf7_get_message('invalid_too_short')); 206 } 207 } 208 } 209 } 210 211 if ('checkbox' == $tag->basetype || 'radio' == $tag->basetype) { 212 213 $is_required = $tag->is_required() || 'radio' == $tag->type; 214 $value = isset($_POST[$name]) ? (array) $_POST[$name] : array(); 215 216 if ($is_required && empty($value)) { 217 $result->invalidate($tag, wpcf7_get_message($name)); 218 } 219 } 220 221 if ('file' == $tag->basetype) { 222 223 if ($tag->is_required() && empty($_FILES[$name])) { 224 $result->invalidate($name, wpcf7_get_message($name)); 225 } 226 } 227 228 return $result; 229 } 230 ?> 81 add_filter( 'wpcf7_validate_text', 'cf7cv_custom_form_validation', 10, 2 ); 82 add_filter( 'wpcf7_validate_text*', 'cf7cv_custom_form_validation', 10, 2 ); 83 add_filter( 'wpcf7_validate_email', 'cf7cv_custom_form_validation', 10, 2 ); 84 add_filter( 'wpcf7_validate_email*', 'cf7cv_custom_form_validation', 10, 2 ); 85 add_filter( 'wpcf7_validate_url', 'cf7cv_custom_form_validation', 10, 2 ); 86 add_filter( 'wpcf7_validate_url*', 'cf7cv_custom_form_validation', 10, 2 ); 87 add_filter( 'wpcf7_validate_tel', 'cf7cv_custom_form_validation', 10, 2 ); 88 add_filter( 'wpcf7_validate_tel*', 'cf7cv_custom_form_validation', 10, 2 ); 89 add_filter( 'wpcf7_validate_textarea', 'cf7cv_custom_form_validation', 10, 2 ); 90 add_filter( 'wpcf7_validate_textarea*', 'cf7cv_custom_form_validation', 10, 2 ); 91 add_filter( 'wpcf7_validate_number', 'cf7cv_custom_form_validation', 10, 2 ); 92 add_filter( 'wpcf7_validate_number*', 'cf7cv_custom_form_validation', 10, 2 ); 93 add_filter( 'wpcf7_validate_range', 'cf7cv_custom_form_validation', 10, 2 ); 94 add_filter( 'wpcf7_validate_range*', 'cf7cv_custom_form_validation', 10, 2 ); 95 add_filter( 'wpcf7_validate_date', 'cf7cv_custom_form_validation', 10, 2 ); 96 add_filter( 'wpcf7_validate_date*', 'cf7cv_custom_form_validation', 10, 2 ); 97 add_filter( 'wpcf7_validate_checkbox', 'cf7cv_custom_form_validation', 10, 2 ); 98 add_filter( 'wpcf7_validate_checkbox*', 'cf7cv_custom_form_validation', 10, 2 ); 99 add_filter( 'wpcf7_validate_radio', 'cf7cv_custom_form_validation', 10, 2 ); 100 add_filter( 'wpcf7_validate_file', 'cf7cv_custom_form_validation', 10, 2 ); 101 add_filter( 'wpcf7_validate_file*', 'cf7cv_custom_form_validation', 10, 2 ); 102 103 function cf7cv_custom_form_validation( $result, $tag ) { 104 105 $tag_name = $tag->name; 106 $name = $tag_name; 107 108 if( empty( $name ) ) { 109 $name = "invalid_required"; 110 } 111 112 if ( 'text' == $tag->basetype ) { 113 $value = isset( $_POST[$tag_name] ) 114 ? trim( wp_unslash( strtr( (string) $_POST[$tag_name], "\n", " " ) ) ) 115 : ''; 116 if ( $tag->is_required() && '' == $value ) { 117 $result->invalidate( $tag, wpcf7_get_message( $name ) ); 118 } 119 } 120 121 if ( 'email' == $tag->basetype ) { 122 123 $value = isset( $_POST[$tag_name] ) 124 ? trim( wp_unslash( strtr( (string) $_POST[$tag_name], "\n", " " ) ) ) 125 : ''; 126 if ( $tag->is_required() && '' == $value ) { 127 $result->invalidate( $tag, wpcf7_get_message( $name ) ); 128 } elseif ( '' != $value && ! wpcf7_is_email( $value ) ) { 129 $result->invalidate( $tag, wpcf7_get_message( $name ) ); 130 } 131 } 132 133 if ( 'url' == $tag->basetype ) { 134 135 $value = isset( $_POST[$tag_name] ) 136 ? trim( wp_unslash( strtr( (string) $_POST[$tag_name], "\n", " " ) ) ) 137 : ''; 138 if ( $tag->is_required() && '' == $value ) { 139 $result->invalidate( $tag, wpcf7_get_message( $name ) ); 140 } elseif ( '' != $value && ! wpcf7_is_url( $value ) ) { 141 $result->invalidate( $tag, wpcf7_get_message( $name ) ); 142 } 143 } 144 145 if ( 'tel' == $tag->basetype ) { 146 147 $value = isset( $_POST[$tag_name] ) 148 ? trim( wp_unslash( strtr( (string) $_POST[$tag_name], "\n", " " ) ) ) 149 : ''; 150 if ( $tag->is_required() && '' == $value ) { 151 $result->invalidate( $tag, wpcf7_get_message( $name ) ); 152 } elseif ( '' != $value && ! wpcf7_is_tel( $value ) ) { 153 $result->invalidate( $tag, wpcf7_get_message( $name ) ); 154 } 155 } 156 157 if ( 'number' == $tag->basetype ) { 158 159 $value = isset( $_POST[$tag_name] ) 160 ? trim( wp_unslash( strtr( (string) $_POST[$tag_name], "\n", " " ) ) ) 161 : ''; 162 $min = $tag->get_option( 'min', 'signed_int', true ); 163 $max = $tag->get_option( 'max', 'signed_int', true ); 164 165 if ( $tag->is_required() && '' == $value ) { 166 $result->invalidate( $tag, wpcf7_get_message( $name ) ); 167 } elseif ( '' != $value && ! wpcf7_is_number( $value ) ) { 168 $result->invalidate( $tag, wpcf7_get_message( $name ) ); 169 $result->invalidate( $tag, wpcf7_get_message( $name ) ); 170 } elseif ( '' != $value && '' != $min && (float) $value < (float) $min ) { 171 $result->invalidate( $tag, wpcf7_get_message( 'number_too_small' ) ); 172 } elseif ( '' != $value && '' != $max && (float) $max < (float) $value ) { 173 $result->invalidate( $tag, wpcf7_get_message( 'number_too_large' ) ); 174 } 175 } 176 if ( 'date' == $tag->basetype ) { 177 178 $min = $tag->get_date_option( 'min' ); 179 $max = $tag->get_date_option( 'max' ); 180 181 $value = isset( $_POST[$tag_name] ) 182 ? trim( strtr( (string) $_POST[$tag_name], "\n", " " ) ) 183 : ''; 184 185 if ( $tag->is_required() && '' == $value ) { 186 $result->invalidate( $tag, wpcf7_get_message( $name ) ); 187 } elseif ( '' != $value && ! wpcf7_is_date( $value ) ) { 188 $result->invalidate( $tag, wpcf7_get_message( 'invalid_date' ) ); 189 } elseif ( '' != $value && ! empty( $min ) && $value < $min ) { 190 $result->invalidate( $tag, wpcf7_get_message( 'date_too_early' ) ); 191 } elseif ( '' != $value && ! empty( $max ) && $max < $value ) { 192 $result->invalidate( $tag, wpcf7_get_message( 'date_too_late' ) ); 193 } 194 } 195 196 if ( 'textarea' == $tag->basetype ) { 197 $value = isset( $_POST[$tag_name] ) ? (string) $_POST[$tag_name] : ''; 198 199 if ( $tag->is_required() && '' == $value ) { 200 $result->invalidate( $tag, wpcf7_get_message( $name ) ); 201 } 202 203 if ( '' !== $value ) { 204 $maxlength = $tag->get_maxlength_option(); 205 $minlength = $tag->get_minlength_option(); 206 207 if ( $maxlength && $minlength && $maxlength < $minlength ) { 208 $maxlength = $minlength = null; 209 } 210 211 $code_units = wpcf7_count_code_units( stripslashes( $value ) ); 212 213 if ( false !== $code_units ) { 214 if ( $maxlength && $maxlength < $code_units ) { 215 $result->invalidate( $tag, wpcf7_get_message( 'invalid_too_long' ) ); 216 } elseif ( $minlength && $code_units < $minlength ) { 217 $result->invalidate( $tag, wpcf7_get_message( 'invalid_too_short' ) ); 218 } 219 } 220 } 221 } 222 223 if ( 'checkbox' == $tag->basetype || 'radio' == $tag->basetype ) { 224 225 $is_required = $tag->is_required() || 'radio' == $tag->type; 226 $value = isset( $_POST[$tag_name] ) ? (array) $_POST[$tag_name] : array(); 227 if ( $is_required && empty( $value ) ) { 228 $result->invalidate( $tag, wpcf7_get_message( $name ) ); 229 } 230 } 231 232 if( 'file' == $tag->basetype ){ 233 if ( $tag->is_required() && empty($_FILES[$tag_name]['name']) ){ 234 $result->invalidate( $name, wpcf7_get_message( $name ) ); 235 } 236 } 237 return $result; 238 } ?> -
cf7-field-validation/trunk/readme.txt
r1799567 r1859796 5 5 Requires at least: 4.3.1 6 6 Tested up to: 4.9.1 7 Stable tag: 1.1. 27 Stable tag: 1.1.3 8 8 License: GPLv2 or later 9 9 License URI: http://www.gnu.org/licenses/gpl-2.0.html … … 38 38 == Changelog == 39 39 40 == 1.1.3 == 41 * Fixed bug of radio field validation 42 * Check for contact form plugin compatibility. 43 40 44 == 1.1 == 41 45
Note: See TracChangeset
for help on using the changeset viewer.