Plugin Directory

Changeset 2582147


Ignore:
Timestamp:
08/12/2021 04:01:46 PM (5 years ago)
Author:
digitaldyna
Message:

Added file upload field support for custom validation message

Location:
cf7-custom-validation-message
Files:
20 added
5 edited

Legend:

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

    r2556056 r2582147  
    55Requires at least: 3.0.1
    66Tested up to: 5.7
    7 Stable tag: 1.2.2
     7Stable tag: 1.3.1
    88License: GPLv2 or later
    99License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    8484== Changelog ==
    8585
     86= 1.3.1 =
     87* Added custom validation message support for file upload field.
     88* Compatiblity check with WordPress 5.8.
     89
    8690= 1.2.2 =
    8791* Bug fix: Email validation checked by Inbuilt PHP function.
  • cf7-custom-validation-message/trunk/admin/class-cf7cvm-admin.php

    r2510433 r2582147  
    103103                    <tr>
    104104                        <th scope="row">
    105                             <label for="field-<?php echo $field->name; ?>"><?php echo $field->name; ?></label>
     105                            <?php //override field name for file upload
     106                            $fieldname = $field->name;
     107                            if( $field->type == 'file*' ){
     108                                $fieldname = $field->name . ' ' . __( '(Required)', 'cf7-custom-validation-message' );
     109                            }
     110                            ?>
     111                            <label for="field-<?php echo $field->name; ?>"><?php echo $fieldname; ?></label>
    106112                        </th>
    107113                        <td>
     
    157163                    }
    158164                    //textarea max length end
     165
     166                    //file field options start
     167                    if( $field->type === 'file*' && isset($field->options) && !empty($field->options) ){
     168                        //get limit from tag
     169                        $limit = $field->get_option( 'limit' );
     170                        //get custom message if already set
     171                        $custom_message_limit = isset($arr_values[$field->name.'_limit']) ? sanitize_text_field($arr_values[$field->name.'_limit']) : '';
     172                        if( isset($limit) ){ ?>
     173                        <tr>
     174                            <th scope="row">
     175                                <label for="field-<?php echo $field->name.'_limit'; ?>"><?php echo $field->name.' (Limit)'; ?></label>
     176                            </th>
     177                            <td>
     178                                <input type="text" id="field-<?php echo $field->name.'_limit'; ?>" name="wpcf7-cv[<?php echo $field->name.'_limit'; ?>]" class="regular-text" size="70" value="<?php echo $custom_message_limit; ?>">
     179                            </td>
     180                        </tr>   
     181                        <?php }
     182                        //get file types allowed in tag
     183                        $file_types_a = $field->get_option( 'filetypes' );
     184                        //get custom message if already set
     185                        $custom_message_filetype = isset($arr_values[$field->name.'_filetype']) ? sanitize_text_field($arr_values[$field->name.'_filetype']) : '';                     
     186                        if( isset($file_types_a) && !empty($file_types_a) ){ ?>
     187                        <tr>
     188                            <th scope="row">
     189                                <label for="field-<?php echo $field->name.'_filetype'; ?>"><?php echo $field->name.' (File types)'; ?></label>
     190                            </th>
     191                            <td>
     192                                <input type="text" id="field-<?php echo $field->name.'_filetype'; ?>" name="wpcf7-cv[<?php echo $field->name.'_filetype'; ?>]" class="regular-text" size="70" value="<?php echo $custom_message_filetype; ?>">
     193                            </td>
     194                        </tr>
     195                        <?php }
     196                    ?>
     197                    <?php
     198                    }
     199                    //file field options end
    159200
    160201                }
  • cf7-custom-validation-message/trunk/cf7-custom-validation-message.php

    r2556056 r2582147  
    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.2.2
     18 * Version:           1.3.1
    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.2.2' );
     37define( 'CF7_CVM_VERSION', '1.3.1' );
    3838define( 'CF7_CVM_BASE_URL', plugin_basename( __FILE__ ) );
    3939
  • cf7-custom-validation-message/trunk/includes/class-cf7cvm.php

    r2429751 r2582147  
    8080            $this->version = CF7_CVM_VERSION;
    8181        } else {
    82             $this->version = '1.0.0';
     82            $this->version = '1.3.1';
    8383        }
    8484        $this->cf7_validation_priority = 9;
     
    191191        $this->loader->add_action( 'wpcf7_validate_select*', $plugin_public, 'cf7cv_custom_form_validation', $this->get_cf7_validation_priority(), 2 );
    192192        $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', $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 );
    194194        //$this->loader->add_action( 'wpcf7_validate_acceptance*', $plugin_public, 'cf7cv_custom_form_validation', $this->get_cf7_validation_priority(), 2 );
    195195        $this->loader->add_action( 'wpcf7_validate_quiz', $plugin_public, 'cf7cv_custom_form_validation', $this->get_cf7_validation_priority(), 2 );
  • cf7-custom-validation-message/trunk/public/class-cf7cvm-public.php

    r2556056 r2582147  
    162162        return $result;
    163163    }
     164   
     165    /**
     166     * Validate the file field.
     167     *
     168     * Used different function as file field has 3 argument
     169     *
     170     * @since    1.3.1
     171     * @access   public
     172     */
     173    public function cf7cv_custom_form_validation_file( $result, $tag, $args ) {
     174        $type = $tag['type'];
     175        $name = $tag['name'];
     176
     177        $is_required = $tag->is_required();
     178        $allowed_tags = array('file*');
     179
     180        $cf7_form_id = (int)sanitize_text_field($_POST['_wpcf7']);
     181        $arr_values = get_post_meta( $cf7_form_id, '_wpcf7_cv_validation_messages', true );
     182        $validatation_msg = isset($arr_values[$name]) ? esc_attr(sanitize_text_field($arr_values[$name])) : wpcf7_get_message( $name );
     183       
     184        //check if activation
     185        $is_active = isset($arr_values['activate']) && $arr_values['activate'] === 1 ? 1 : 0;
     186        if( $is_active === 0 || $is_required == false || !in_array($type,$allowed_tags)){
     187            return $result;
     188        }
     189       
     190        //get file
     191        $file = isset( $_FILES[$name] ) ? $_FILES[$name] : null;
     192       
     193        /* File required validation */
     194        if( $type == 'file*' && empty( $file['name'] ) && $is_required ){   
     195            $result->invalidate( $name, $validatation_msg );
     196            return $result;
     197        }
     198
     199        /* File type validation */
     200        $allowed_file_types = array();
     201
     202        if ( $file_types_a = $tag->get_option( 'filetypes' ) ) {
     203            foreach ( $file_types_a as $file_types ) {
     204                $file_types = explode( '|', $file_types );
     205
     206                foreach ( $file_types as $file_type ) {
     207                    $file_type = trim( $file_type, '.' );
     208                    $file_type = str_replace( array( '.', '+', '*', '?' ),
     209                        array( '\.', '\+', '\*', '\?' ), $file_type );
     210                    $allowed_file_types[] = $file_type;
     211                }
     212            }
     213        }
     214
     215        $allowed_file_types = array_unique( $allowed_file_types );
     216        $file_type_pattern = implode( '|', $allowed_file_types );
     217       
     218        //default file type if not provided then
     219        if ( '' == $file_type_pattern ){
     220            $file_type_pattern = 'jpg|jpeg|png|gif|pdf|doc|docx|ppt|pptx|odt|avi|ogg|m4a|mov|mp3|mp4|mpg|wav|wmv';
     221            $custom_message_filetype = '';
     222        }
     223       
     224        //convert to regex
     225        $file_type_pattern = trim( $file_type_pattern, '|' );
     226        $file_type_pattern = '(' . $file_type_pattern . ')';
     227        $file_type_pattern = '/\.' . $file_type_pattern . '$/i';
     228       
     229        //check filetype with validation with regex
     230        if ( ! preg_match( $file_type_pattern, $file['name'] ) ) {
     231            $file_type = $name.'_filetype';
     232            $validatation_msg = isset($arr_values[$file_type]) ? esc_attr(sanitize_text_field($arr_values[$file_type])) : wpcf7_get_message( $name );
     233            $result->invalidate( $tag, $validatation_msg );
     234            return $result;
     235        }
     236       
     237        /* File size validation */
     238        $allowed_size = 1048576; // default size 1 MB
     239
     240        if ( $file_size_a = $tag->get_option( 'limit' ) ) {
     241            $limit_pattern = '/^([1-9][0-9]*)([kKmM]?[bB])?$/';
     242
     243            foreach ( $file_size_a as $file_size ) {
     244                if ( preg_match( $limit_pattern, $file_size, $matches ) ) {
     245                    $allowed_size = (int) $matches[1];
     246
     247                    if ( ! empty( $matches[2] ) ) {
     248                        $kbmb = strtolower( $matches[2] );
     249
     250                        if ( 'kb' == $kbmb )
     251                            $allowed_size *= 1024;
     252                        elseif ( 'mb' == $kbmb )
     253                            $allowed_size *= 1024 * 1024;
     254                    }
     255
     256                    break;
     257                }
     258            }
     259        }
     260        if ( $file['size'] > $allowed_size ) {
     261            $size = size_format($allowed_size);
     262            $file_limit = $name.'_limit';
     263            $validatation_msg = isset($arr_values[$file_limit]) ? esc_attr(sanitize_text_field($arr_values[$file_limit])) : wpcf7_get_message( $name );
     264            $result->invalidate( $tag, $validatation_msg );
     265            return $result;
     266        }
     267
     268        return $result;
     269    }
    164270
    165271    /**
Note: See TracChangeset for help on using the changeset viewer.