Plugin Directory

Changeset 2784454


Ignore:
Timestamp:
09/14/2022 08:24:29 AM (3 years ago)
Author:
digitaldyna
Message:

Added Rest API support for validation rules and minor but fixes

Location:
cf7-custom-validation-message/trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • cf7-custom-validation-message/trunk/README.txt

    r2582192 r2784454  
    44Tags: cf7 custom validation message, contact form 7, contact form 7 validation, cf7 validation, Contact form 7 validation, custom validation, form validation, validation, custom validation message for cf7
    55Requires at least: 3.0.1
    6 Tested up to: 5.8
    7 Stable tag: 1.3.1
     6Tested up to: 6.0.2
     7Stable tag: 2.0.0
    88License: GPLv2 or later
    99License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    8484== Changelog ==
    8585
     86= 2.0.0 =
     87* Fixed validation. Added Rest API compatiblity as it's changed by Contact Form 7.
     88* Used object notation to access the ID from contact form 7 object at admin side.
     89* While activating plugin, showing 304 error for multisite. It's fixed.
     90* Compatiblity check with WordPress 6.0.2.
     91
    8692= 1.3.1 =
    8793* Added custom validation message support for file upload field.
  • cf7-custom-validation-message/trunk/admin/class-cf7cvm-admin.php

    r2582147 r2784454  
    7272            <?php
    7373            $form_fields = array();
    74             $form_ID     = $post->id; # get CF7 form ID
     74            $form_ID     = $post->id(); # get CF7 form ID
    7575            if( $form_ID != null){
    7676                $ContactForm = WPCF7_ContactForm::get_instance( $form_ID );
  • cf7-custom-validation-message/trunk/cf7-custom-validation-message.php

    r2582147 r2784454  
    1616 * Plugin Name:       Custom Validation Message for Contact Form 7
    1717 * Description:       Plugin provides custom validation message for each field of contact form 7.
    18  * Version:           1.3.1
     18 * Version:           2.0.0
    1919 * Author:            DigitalDyna
    2020 * Author URI:        http://digitaldyna.com
     
    3535 * Rename this for your plugin and update it as you release new versions.
    3636 */
    37 define( 'CF7_CVM_VERSION', '1.3.1' );
     37define( 'CF7_CVM_VERSION', '2.0.0' );
    3838define( 'CF7_CVM_BASE_URL', plugin_basename( __FILE__ ) );
    3939
     
    4444function activate_cf7cvm() {
    4545    require_once plugin_dir_path( __FILE__ ) . 'includes/class-cf7cvm-activator.php';
    46     CF7_CVM_Activator::activate($network_wide);
     46    CF7_CVM_Activator::activate();
    4747}
    4848
  • cf7-custom-validation-message/trunk/includes/class-cf7cvm-activator.php

    r2481870 r2784454  
    3030     * @since    1.0.0
    3131     */
    32     public static function activate($network_wide) {
    33         if ( is_multisite() && $network_wide ) {
     32    public static function activate() {
     33        if ( is_multisite() ) {
    3434            if ( !is_plugin_active_for_network('contact-form-7/wp-contact-form-7.php') ){
    3535                require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
  • cf7-custom-validation-message/trunk/includes/class-cf7cvm.php

    r2582147 r2784454  
    180180        $plugin_public = new CF7_CVM_Public( $this->get_plugin_name(), $this->get_version() );
    181181
    182         $this->loader->add_action( 'wpcf7_validate_text*', $plugin_public, 'cf7cv_custom_form_validation', $this->get_cf7_validation_priority(), 2 );
    183         $this->loader->add_action( 'wpcf7_validate_email*', $plugin_public, 'cf7cv_custom_form_validation', $this->get_cf7_validation_priority(), 2 );
    184         $this->loader->add_action( 'wpcf7_validate_textarea*', $plugin_public, 'cf7cv_custom_form_validation', $this->get_cf7_validation_priority(), 2 );
    185         $this->loader->add_action( 'wpcf7_validate_tel*', $plugin_public, 'cf7cv_custom_form_validation', $this->get_cf7_validation_priority(), 2 );
    186         $this->loader->add_action( 'wpcf7_validate_url*', $plugin_public, 'cf7cv_custom_form_validation', $this->get_cf7_validation_priority(), 2 );
    187         $this->loader->add_action( 'wpcf7_validate_checkbox*', $plugin_public, 'cf7cv_custom_form_validation', $this->get_cf7_validation_priority(), 2 );
    188         $this->loader->add_action( 'wpcf7_validate_number*', $plugin_public, 'cf7cv_custom_form_validation', $this->get_cf7_validation_priority(), 2 );
    189         //$this->loader->add_action( 'wpcf7_validate_range*', $plugin_public, 'cf7cv_custom_form_validation', $this->get_cf7_validation_priority(), 2 );
    190         $this->loader->add_action( 'wpcf7_validate_date*', $plugin_public, 'cf7cv_custom_form_validation', $this->get_cf7_validation_priority(), 2 );
    191         $this->loader->add_action( 'wpcf7_validate_select*', $plugin_public, 'cf7cv_custom_form_validation', $this->get_cf7_validation_priority(), 2 );
    192         $this->loader->add_action( 'wpcf7_validate_radio', $plugin_public, 'cf7cv_custom_form_validation', $this->get_cf7_validation_priority(), 2 );
    193         $this->loader->add_action( 'wpcf7_validate_file*', $plugin_public, 'cf7cv_custom_form_validation_file', $this->get_cf7_validation_priority(), 3 );
    194         //$this->loader->add_action( 'wpcf7_validate_acceptance*', $plugin_public, 'cf7cv_custom_form_validation', $this->get_cf7_validation_priority(), 2 );
    195         $this->loader->add_action( 'wpcf7_validate_quiz', $plugin_public, 'cf7cv_custom_form_validation', $this->get_cf7_validation_priority(), 2 );
     182        $cur_ver = '';
     183        //get contact form 7 version
     184        $cur_ver = get_option( 'wpcf7' );
     185        if(!empty($cur_ver)){
     186            $cur_ver = $cur_ver['version'];
     187        }
     188       
     189        //if support Rest API then swv validation method
     190        if ($cur_ver != '' && version_compare($cur_ver, '5.6') >= 0) {
     191            //text rule
     192            remove_action( 'wpcf7_swv_create_schema', 'wpcf7_swv_add_text_rules' );
     193            $this->loader->add_action( 'wpcf7_swv_create_schema', $plugin_public, 'wpcf7_swv_add_text_rules_digitaldyna', $this->get_cf7_validation_priority(), 2 );
     194           
     195            //textarea rule
     196            remove_action( 'wpcf7_swv_create_schema', 'wpcf7_swv_add_textarea_rules' );
     197            $this->loader->add_action( 'wpcf7_swv_create_schema', $plugin_public, 'wpcf7_swv_add_textarea_rules_digitaldyna', $this->get_cf7_validation_priority(), 2 );
     198           
     199            //radio and checkbox rule
     200            remove_action( 'wpcf7_swv_create_schema', 'wpcf7_swv_add_checkbox_rules' );
     201            $this->loader->add_action( 'wpcf7_swv_create_schema', $plugin_public, 'wpcf7_swv_add_checkbox_rules_digitaldyna', $this->get_cf7_validation_priority(), 2 );
     202           
     203            //date rule
     204            remove_action( 'wpcf7_swv_create_schema', 'wpcf7_swv_add_date_rules' );
     205            $this->loader->add_action( 'wpcf7_swv_create_schema', $plugin_public, 'wpcf7_swv_add_date_rules_digitaldyna', $this->get_cf7_validation_priority(), 2 );
     206
     207            //select rule
     208            remove_action( 'wpcf7_swv_create_schema', 'wpcf7_swv_add_select_rules' );
     209            $this->loader->add_action( 'wpcf7_swv_create_schema', $plugin_public, 'wpcf7_swv_add_select_rules_digitaldyna', $this->get_cf7_validation_priority(), 2 );
     210
     211        } else {
     212
     213            //old version validation methods
     214            $this->loader->add_action( 'wpcf7_validate_text*', $plugin_public, 'cf7cv_custom_form_validation', $this->get_cf7_validation_priority(), 2 );
     215            $this->loader->add_action( 'wpcf7_validate_email*', $plugin_public, 'cf7cv_custom_form_validation', $this->get_cf7_validation_priority(), 2 );
     216            $this->loader->add_action( 'wpcf7_validate_textarea*', $plugin_public, 'cf7cv_custom_form_validation', $this->get_cf7_validation_priority(), 2 );
     217            $this->loader->add_action( 'wpcf7_validate_tel*', $plugin_public, 'cf7cv_custom_form_validation', $this->get_cf7_validation_priority(), 2 );
     218            $this->loader->add_action( 'wpcf7_validate_url*', $plugin_public, 'cf7cv_custom_form_validation', $this->get_cf7_validation_priority(), 2 );
     219            $this->loader->add_action( 'wpcf7_validate_checkbox*', $plugin_public, 'cf7cv_custom_form_validation', $this->get_cf7_validation_priority(), 2 );
     220            $this->loader->add_action( 'wpcf7_validate_number*', $plugin_public, 'cf7cv_custom_form_validation', $this->get_cf7_validation_priority(), 2 );
     221            //$this->loader->add_action( 'wpcf7_validate_range*', $plugin_public, 'cf7cv_custom_form_validation', $this->get_cf7_validation_priority(), 2 );
     222            $this->loader->add_action( 'wpcf7_validate_date*', $plugin_public, 'cf7cv_custom_form_validation', $this->get_cf7_validation_priority(), 2 );
     223            $this->loader->add_action( 'wpcf7_validate_select*', $plugin_public, 'cf7cv_custom_form_validation', $this->get_cf7_validation_priority(), 2 );
     224            $this->loader->add_action( 'wpcf7_validate_radio', $plugin_public, 'cf7cv_custom_form_validation', $this->get_cf7_validation_priority(), 2 );
     225            $this->loader->add_action( 'wpcf7_validate_file*', $plugin_public, 'cf7cv_custom_form_validation_file', $this->get_cf7_validation_priority(), 3 );
     226            //$this->loader->add_action( 'wpcf7_validate_acceptance*', $plugin_public, 'cf7cv_custom_form_validation', $this->get_cf7_validation_priority(), 2 );
     227            $this->loader->add_action( 'wpcf7_validate_quiz', $plugin_public, 'cf7cv_custom_form_validation', $this->get_cf7_validation_priority(), 2 );
     228        }
    196229
    197230    }
  • cf7-custom-validation-message/trunk/public/class-cf7cvm-public.php

    r2582147 r2784454  
    275275     * @return mixed
    276276     */
    277     function recursive_sanitize_text_field($array) {
     277    public function recursive_sanitize_text_field($array) {
    278278        foreach ( $array as $key => &$value ) {
    279279            if ( is_array( $value ) ) {
     
    287287        return $array;
    288288    }
     289   
     290    /**
     291     * Get validation message for received contact form 7 id and tag name based on rule (new Rest API)
     292     * @since   2.0.0
     293     * @param  [type] $cf7_id        [Contact Form 7 ID]
     294     * @param  [type] $tag           [Tag object]
     295     * @param  [type] $error_message [default error message]
     296     * @param  string $rule_name     [rule name like required,email,date,url]
     297     * @return [type]                [It will return string a custom error message if exists]
     298     */
     299    public function getValidationMessage($cf7_id, $tag, $error_message, $rule_name='required'){
     300        $tag_name = $tag->name;
     301        $tag_type = $tag->type;
     302        $arr_values = get_post_meta( $cf7_id, '_wpcf7_cv_validation_messages', true );
     303        $is_active = isset($arr_values['activate']) && $arr_values['activate'] === 1 ? 1 : 0;
     304        if(isset($arr_values[$tag_name]) && $is_active === 1 && $rule_name == 'required'){
     305            $error_message = isset($arr_values[$tag_name]) ? esc_attr(sanitize_text_field($arr_values[$tag_name])) : $error_message;   
     306        }else{
     307            switch ($tag_type) {
     308                case 'email*':
     309                    $confirm_field = $tag_name.'_invalid';
     310                    $error_message = isset($arr_values[$confirm_field]) ? esc_attr(sanitize_text_field($arr_values[$confirm_field])) : $error_message; 
     311                    break;
     312                case 'textarea*':
     313                    if($rule_name == 'invalid_too_short'){
     314                        $textarea_maxlength = $tag_name.'_minlength';
     315                        $error_message = isset($arr_values[$textarea_maxlength]) ? esc_attr(sanitize_text_field($arr_values[$textarea_maxlength])) : $error_message;
     316                    }
     317
     318                    if($rule_name == 'invalid_too_long'){
     319                        $textarea_minlength = $tag_name.'_maxlength';
     320                        $error_message = isset($arr_values[$textarea_minlength]) ? esc_attr(sanitize_text_field($arr_values[$textarea_minlength])) : $error_message;
     321                    }
     322                    break;
     323               
     324                default:
     325                    # code...
     326                    break;
     327            }
     328        }
     329        return $error_message;
     330    }
     331
     332    /**
     333     * Textbox and other common elements rules
     334     * @since   2.0.0
     335     * @param  [type] $schema       [Schema generated by Contact Form 7]
     336     * @param  [type] $contact_form [Contact Form 7 object]
     337     * @return [type]               [Create rule with custom error message or default message]
     338     */
     339    public function wpcf7_swv_add_text_rules_digitaldyna( $schema, $contact_form ) {
     340        $tags = $contact_form->scan_form_tags( array(
     341            'basetype' => array( 'text', 'email', 'url', 'tel' ),
     342        ) );
     343
     344        foreach ( $tags as $tag ) {
     345            if ( $tag->is_required() ) {
     346                $error_message = wpcf7_get_message( 'invalid_required' );
     347                $schema->add_rule(
     348                    wpcf7_swv_create_rule( 'required', array(
     349                        'field' => $tag->name,
     350                        'error' => $this->getValidationMessage($contact_form->id(), $tag, $error_message),
     351                    ) )
     352                );
     353            }
     354
     355            if ( 'email' === $tag->basetype ) {
     356                $error_message = wpcf7_get_message( 'invalid_email' );
     357                $schema->add_rule(
     358                    wpcf7_swv_create_rule( 'email', array(
     359                        'field' => $tag->name,
     360                        'error' => $this->getValidationMessage($contact_form->id(), $tag, $error_message, 'email'),
     361                    ) )
     362                );
     363            }
     364
     365            if ( 'url' === $tag->basetype ) {
     366                $error_message = wpcf7_get_message( 'invalid_url' );
     367                $schema->add_rule(
     368                    wpcf7_swv_create_rule( 'url', array(
     369                        'field' => $tag->name,
     370                        'error' => $this->getValidationMessage($contact_form->id(), $tag, $error_message, 'url'), //TODO:: add here invlid_url and then admin side
     371                    ) )
     372                );
     373            }
     374
     375            if ( 'tel' === $tag->basetype ) {
     376                $error_message = wpcf7_get_message( 'invalid_tel' );
     377                $schema->add_rule(
     378                    wpcf7_swv_create_rule( 'tel', array(
     379                        'field' => $tag->name,
     380                        'error' => $this->getValidationMessage($contact_form->id(), $tag, $error_message, 'tel'), //TODO:: add here invalid_tel and then admin side
     381                    ) )
     382                );
     383            }
     384
     385            if ( $minlength = $tag->get_minlength_option() ) {
     386                $error_message = wpcf7_get_message( 'invalid_too_short' );
     387                $schema->add_rule(
     388                    wpcf7_swv_create_rule( 'minlength', array(
     389                        'field' => $tag->name,
     390                        'threshold' => absint( $minlength ),
     391                        'error' => $this->getValidationMessage($contact_form->id(), $tag, $error_message, 'invalid_too_short'), //TODO:: add here invalid_too_short and then admin side
     392                    ) )
     393                );
     394            }
     395
     396            if ( $maxlength = $tag->get_maxlength_option() ) {
     397                $error_message = wpcf7_get_message( 'invalid_too_long' );
     398                $schema->add_rule(
     399                    wpcf7_swv_create_rule( 'maxlength', array(
     400                        'field' => $tag->name,
     401                        'threshold' => absint( $maxlength ),
     402                        'error' => $this->getValidationMessage($contact_form->id(), $tag, $error_message, 'invalid_too_long'), //TODO:: add here invalid_too_long and then admin side
     403                    ) )
     404                );
     405            }
     406        }
     407    }
     408
     409    /**
     410     * Textarea element rules
     411     * @since   2.0.0
     412     * @param  [type] $schema       [Schema generated by Contact Form 7]
     413     * @param  [type] $contact_form [Contact Form 7 object]
     414     * @return [type]               [Create rule with custom error message or default message]
     415     */
     416    public function wpcf7_swv_add_textarea_rules_digitaldyna( $schema, $contact_form ) {
     417        $tags = $contact_form->scan_form_tags( array(
     418            'basetype' => array( 'textarea' ),
     419        ) );
     420
     421        foreach ( $tags as $tag ) {
     422            if ( $tag->is_required() ) {
     423                $error_message = wpcf7_get_message( 'invalid_required' );
     424                $schema->add_rule(
     425                    wpcf7_swv_create_rule( 'required', array(
     426                        'field' => $tag->name,
     427                        'error' => $this->getValidationMessage($contact_form->id(), $tag, $error_message),
     428                    ) )
     429                );
     430            }
     431
     432            if ( $minlength = $tag->get_minlength_option() ) {
     433                $error_message = wpcf7_get_message( 'invalid_too_short' );
     434                $schema->add_rule(
     435                    wpcf7_swv_create_rule( 'minlength', array(
     436                        'field' => $tag->name,
     437                        'threshold' => absint( $minlength ),
     438                        'error' => $this->getValidationMessage($contact_form->id(), $tag, $error_message, 'invalid_too_short'),
     439                    ) )
     440                );
     441            }
     442
     443            if ( $maxlength = $tag->get_maxlength_option() ) {
     444                $error_message = wpcf7_get_message( 'invalid_too_long' );
     445                $schema->add_rule(
     446                    wpcf7_swv_create_rule( 'maxlength', array(
     447                        'field' => $tag->name,
     448                        'threshold' => absint( $maxlength ),
     449                        'error' => $this->getValidationMessage($contact_form->id(), $tag, $error_message, 'invalid_too_long'),
     450                    ) )
     451                );
     452            }
     453        }
     454    }
     455
     456    /**
     457     * Radio and Checkbox elements rules
     458     * @since   2.0.0
     459     * @param  [type] $schema       [Schema generated by Contact Form 7]
     460     * @param  [type] $contact_form [Contact Form 7 object]
     461     * @return [type]               [Create rule with custom error message or default message]
     462     */
     463    public function wpcf7_swv_add_checkbox_rules_digitaldyna( $schema, $contact_form ) {
     464        $error_message = wpcf7_get_message( 'invalid_required' );
     465       
     466        $tags = $contact_form->scan_form_tags( array(
     467            'type' => array( 'checkbox*', 'radio' ),
     468        ) );
     469
     470        foreach ( $tags as $tag ) {
     471            $schema->add_rule(
     472                wpcf7_swv_create_rule( 'required', array(
     473                    'field' => $tag->name,
     474                    'error' => $this->getValidationMessage($contact_form->id(), $tag, $error_message),
     475                ) )
     476            );
     477        }
     478       
     479    }
     480
     481    /**
     482     * Date element rules
     483     * @since   2.0.0
     484     * @param  [type] $schema       [Schema generated by Contact Form 7]
     485     * @param  [type] $contact_form [Contact Form 7 object]
     486     * @return [type]               [Create rule with custom error message or default message]
     487     */
     488    public function wpcf7_swv_add_date_rules_digitaldyna( $schema, $contact_form ) {
     489        $tags = $contact_form->scan_form_tags( array(
     490            'basetype' => array( 'date' ),
     491        ) );
     492
     493        foreach ( $tags as $tag ) {
     494            if ( $tag->is_required() ) {
     495                $error_message = wpcf7_get_message( 'invalid_required' );
     496                $schema->add_rule(
     497                    wpcf7_swv_create_rule( 'required', array(
     498                        'field' => $tag->name,
     499                        'error' => $this->getValidationMessage($contact_form->id(), $tag, $error_message),
     500                    ) )
     501                );
     502            }
     503           
     504            $error_message = wpcf7_get_message( 'invalid_date' );
     505            $schema->add_rule(
     506                wpcf7_swv_create_rule( 'date', array(
     507                    'field' => $tag->name,
     508                    'error' => $this->getValidationMessage($contact_form->id(), $tag, $error_message, 'date'),
     509                ) )
     510            );
     511
     512            $min = $tag->get_date_option( 'min' );
     513            $max = $tag->get_date_option( 'max' );
     514
     515            if ( false !== $min ) {
     516                $error_message = wpcf7_get_message( 'date_too_early' );
     517                $schema->add_rule(
     518                    wpcf7_swv_create_rule( 'mindate', array(
     519                        'field' => $tag->name,
     520                        'threshold' => $min,
     521                        'error' => $this->getValidationMessage($contact_form->id(), $tag, $error_message),
     522                    ) )
     523                );
     524            }
     525
     526            if ( false !== $max ) {
     527                $error_message = wpcf7_get_message( 'date_too_late' );
     528                $schema->add_rule(
     529                    wpcf7_swv_create_rule( 'maxdate', array(
     530                        'field' => $tag->name,
     531                        'threshold' => $max,
     532                        'error' => $this->getValidationMessage($contact_form->id(), $tag, $error_message),
     533                    ) )
     534                );
     535            }
     536        }
     537    }
     538
     539    /**
     540     * Select element rules
     541     * @since   2.0.0
     542     * @param  [type] $schema       [Schema generated by Contact Form 7]
     543     * @param  [type] $contact_form [Contact Form 7 object]
     544     * @return [type]               [Create rule with custom error message or default message]
     545     */
     546    public function wpcf7_swv_add_select_rules_digitaldyna( $schema, $contact_form ) {
     547        $error_message = wpcf7_get_message( 'invalid_required' );
     548        $tags = $contact_form->scan_form_tags( array(
     549            'type' => array( 'select*' ),
     550        ) );
     551
     552        foreach ( $tags as $tag ) {
     553            $schema->add_rule(
     554                wpcf7_swv_create_rule( 'required', array(
     555                    'field' => $tag->name,
     556                    'error' => $this->getValidationMessage($contact_form->id(), $tag, $error_message),
     557                ) )
     558            );
     559        }
     560    }
    289561
    290562
Note: See TracChangeset for help on using the changeset viewer.