Changeset 785508
- Timestamp:
- 10/09/2013 07:28:14 PM (12 years ago)
- Location:
- gravity-forms-upload-rules/trunk
- Files:
-
- 2 edited
-
gforms-uploadrules.php (modified) (2 diffs)
-
readme.txt (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
gravity-forms-upload-rules/trunk/gforms-uploadrules.php
r768183 r785508 1 <?php 2 3 class GFUploadRules { 4 5 protected static $version = '1.0'; 6 7 public function localize() { 8 9 $locale = apply_filters( 'plugin_locale', get_locale(), 'gforms_uprules' ); 10 load_textdomain( 'gforms_uprules', WP_LANG_DIR . "/gforms_uprules/gforms_uprules-$locale.mo" ); 11 load_plugin_textdomain( 'gforms_uprules', null, basename( plugin_dir_path( __FILE__ ) ) . 'lang' ); 12 } 13 14 public function register_scripts() { 15 wp_register_script('gform_uprules_plugin_form_editor', plugins_url('/js/form_editor.js', __FILE__), array('jquery'), self::$version, true ); 16 } 17 18 public function editor_js() { 19 20 if ( ! in_array( 'gform_uprules_plugin_form_editor', wp_print_scripts(array('gform_uprules_plugin_form_editor')) ) ) : 21 ?> 22 <script type="text/javascript" id="gform_uprules_plugin_form_editor"> 23 <?php include plugin_dir_path( __FILE__ ) . 'js/form_editor.js'; ?> 24 </script> 25 <?php 26 endif; 27 } 28 29 public function dimension_field_label_minwidth() { 30 31 $locale = apply_filters( 'plugin_locale', get_locale(), 'gforms_uprules' ); 32 $label_minwidth_by_locale = apply_filters( 'gforms_uprules_dimension_field_label_minwidth', array( 'en_US' => 50 ) ); 33 34 if ( isset( $label_minwidth_by_locale[ $locale ] ) ) 35 return $label_minwidth_by_locale[ $locale ]; 36 37 //in case somebody has messed things up 38 return 50; 39 } 40 41 public function field_settings( $position ) { 42 if ( 200 != $position ) 43 return; 44 45 $label_minwidth = self::dimension_field_label_minwidth(); 46 ?> 47 <li class="uprules_filesize_setting field_setting"> 48 <label for="field_uprules_filesize"> 49 <?php _e("Filesize Limit", "gforms_uprules"); ?> 50 <?php gform_tooltip("form_field_uprules_filesize"); ?> 51 </label> 52 <input type="text" id="field_uprules_filesize" style="text-align: right;" onkeyup="SetFieldProperty('uprules_filesize_limit', this.value);" size="10" /> 53 <select id="field_uprules_filesize_dim" onchange="SetFieldProperty('uprules_filesize_dim', jQuery(this).val() );"><option value="kb">KB</option><option value="mb">MB</option></select> 54 </li> 55 <li class="uprules_dimensions_setting field_setting"> 56 <label> 57 <?php _e("Image dimensions", "gforms_uprules"); ?> 58 59 <select id="field_uprules_dims_ruletype" onchange="SetFieldProperty('uprules_dims_ruletype', jQuery(this).val() );"> 60 <option value="" style="text-align: center"><?php _e("– Select –", "gforms_uprules"); ?></option> 61 <option value="exact"><?php _e("Exact", "gforms_uprules"); ?></option> 62 <option value="conditional"><?php _e("Conditional", "gforms_uprules"); ?></option> 63 </select> 64 65 <?php gform_tooltip("form_field_uprules_dimensions"); ?> 66 67 </label> 68 69 <div class="uprules_dims_fields_exact" style="display: none;"> 70 <div> 71 <label for="uprules_dims_exact_width" class="inline" style="min-width: <?php echo esc_attr( $label_minwidth ); ?>px; margin-right: 10px;"> 72 <?php _e("Width", "gforms_uprules"); ?> 73 </label> 74 <input type="text" id="field_uprules_dims_exact_width" style="text-align: center;" onkeyup="SetFieldProperty('uprules_dims_exact_width', this.value);" size="6" /> 75 </div> 76 <div> 77 <label for="uprules_dims_exact_height" class="inline" style="min-width: <?php echo esc_attr( $label_minwidth ); ?>px; margin-right: 10px;"> 78 <?php _e("Height", "gforms_uprules"); ?> 79 </label> 80 <input type="text" id="field_uprules_dims_exact_height" style="text-align: center;" onkeyup="SetFieldProperty('uprules_dims_exact_height', this.value);" size="6" /> 81 </div> 82 </div> <!--//-- .uprules_dims_fields_exact --> 83 84 <div class="uprules_dims_fields_conditional" style="display: none;"> 85 <div class="uprules_dims_fields_conditional_min"> 86 <label for="uprules_dims_minwidth" class="inline" style="min-width: <?php echo esc_attr( $label_minwidth ); ?>px; margin-right: 10px;"> 87 <?php _e("Min", "gforms_uprules"); ?> 88 </label> 89 <input type="text" id="field_uprules_dims_minwidth" style="text-align: center;" onkeyup="SetFieldProperty('uprules_dims_minwidth', this.value);" size="6" placeholder="<?php esc_attr_e("Width", "gforms_uprules"); ?>" /> 90 × 91 <input type="text" id="field_uprules_dims_minheight" style="text-align: center;" onkeyup="SetFieldProperty('uprules_dims_minheight', this.value);" size="6" placeholder="<?php esc_attr_e("Height", "gforms_uprules"); ?>" /> 92 </div> 93 <div class="uprules_dims_fields_conditional_max"> 94 <label for="uprules_dims_maxwidth" class="inline" style="min-width: <?php echo esc_attr( $label_minwidth ); ?>px; margin-right: 10px;"> 95 <?php _e("Max", "gforms_uprules"); ?> 96 </label> 97 <input type="text" id="field_uprules_dims_maxwidth" style="text-align: center;" onkeyup="SetFieldProperty('uprules_dims_maxwidth', this.value);" size="6" placeholder="<?php esc_attr_e("Width", "gforms_uprules"); ?>" /> 98 × 99 <input type="text" id="field_uprules_dims_maxheight" style="text-align: center;" onkeyup="SetFieldProperty('uprules_dims_maxheight', this.value);" size="6" placeholder="<?php esc_attr_e("Height", "gforms_uprules"); ?>" /> 100 </div> 101 </div> <!--//-- .uprules_dims_fields_conditional --> 102 103 </li> 104 <?php 105 } 106 107 public function field_validation( $valid, $value, $form, $field ) { 108 if ( ! empty( $_FILES ) && $valid['is_valid'] && in_array( RGFormsModel::get_input_type($field), array( 'fileupload', 'post_image' ) ) ) { 109 110 $form_id = $form['id']; 111 $input_name = 'input_' . $field['id']; 112 $fileinfo = RGFormsModel::get_temp_filename( $form_id, $input_name ); 113 $temp_filepath = RGFormsModel::get_upload_path($form_id) . "/tmp/" . $fileinfo["temp_filename"]; 114 115 if ( isset( $_FILES[$input_name] ) && !empty( $_FILES[$input_name] ) ) { 116 $bytes = $_FILES[$input_name]['size']; 117 $dims = @getimagesize( $_FILES[$input_name]['tmp_name'] ); 118 } 119 elseif ( file_exists( $temp_filepath ) ) { 120 $bytes = filesize( $temp_filepath ); 121 $dims = @getimagesize( $temp_filepath ); 122 } 123 else return $valid; 124 125 126 //validate filesize 127 if ( isset( $field['uprules_filesize_limit'] ) ) { 128 $multipliers = array( 129 'kb' => 1024, 130 'mb' => 1024 * 1024 131 ); 132 $max_filesize_user = intval( $field['uprules_filesize_limit'] ); 133 $bytes_multiplier = $multipliers[$field['uprules_filesize_dim']]; 134 $max_filesize_bytes = $max_filesize_user * $bytes_multiplier; 135 } 136 137 if ( isset( $bytes ) && $max_filesize_user > 0 && $max_filesize_bytes < $bytes ) { 138 $valid['is_valid'] = false; 139 $valid['message'] = sprintf( __( 'Max file upload size (%s) exceeded.', 'gravityforms' ), size_format( $max_filesize_bytes, 2 ) ); 140 } 141 //validate image dimensions 142 if ( $valid['is_valid'] && is_array( $dims ) && isset( $field['uprules_dims_ruletype'] ) && in_array( $field['uprules_dims_ruletype'], array( 'exact', 'conditional' ) ) ) { 143 list( $up_width, $up_height ) = $dims; 144 $valid = self::validate_image_dimensions( $field, $up_width, $up_height ); 145 } 146 147 if ( ! $valid['is_valid'] ) 148 unset( RGFormsModel::$uploaded_files[$form_id][$input_name], $_FILES[$input_name] ); 149 150 } 151 return $valid; 152 } 153 154 public function is_valid_dim( $val ) { 155 return (bool)( trim($val) == absint( $val ) && $val > 0 ); 156 } 157 158 public function validate_image_dimensions( $field, $width, $height ) { 159 160 $valid = array( 'is_valid' => true, 'message' => '' ); 161 switch ( $field['uprules_dims_ruletype'] ) { 162 case 'exact': 163 if ( 164 isset( $field['uprules_dims_exact_width'] ) && self::is_valid_dim( $field['uprules_dims_exact_width'] ) && trim( $field['uprules_dims_exact_width'] ) != $width 165 || isset( $field['uprules_dims_exact_height'] ) && self::is_valid_dim( $field['uprules_dims_exact_height'] ) && trim( $field['uprules_dims_exact_height'] ) != $height 166 ) 167 $valid['is_valid'] = false; 168 $valid['message'] = sprintf( __("Only images with dimensions %d×%d allowed.", "gforms_uprules"), $field['uprules_dims_exact_width'], $field['uprules_dims_exact_height'] ); 169 break; 170 case 'conditional': 171 $errors = ""; 172 173 if ( isset( $field['uprules_dims_minwidth'] ) && self::is_valid_dim( $field['uprules_dims_minwidth'] ) && trim( $field['uprules_dims_minwidth'] ) > $width ) { 174 $valid['is_valid'] = false; 175 $errors .= '<br/>' . sprintf( __("Min width: <b>%dpx</b>", "gforms_uprules"), $field['uprules_dims_minwidth'] ); 176 } 177 if ( isset( $field['uprules_dims_minheight'] ) && self::is_valid_dim( $field['uprules_dims_minheight'] ) && trim( $field['uprules_dims_minheight'] ) > $height ) { 178 $valid['is_valid'] = false; 179 $errors .= '<br/>' . sprintf( __("Min height: <b>%dpx</b>", "gforms_uprules"), $field['uprules_dims_minheight'] ); 180 } 181 if ( isset( $field['uprules_dims_maxwidth'] ) && self::is_valid_dim( $field['uprules_dims_maxwidth'] ) && trim( $field['uprules_dims_maxwidth'] ) < $width ) { 182 $valid['is_valid'] = false; 183 $errors .= '<br/>' . sprintf( __("Max width: <b>%dpx</b>", "gforms_uprules"), $field['uprules_dims_maxwidth'] ); 184 } 185 if ( isset( $field['uprules_dims_maxheight'] ) && self::is_valid_dim( $field['uprules_dims_maxheight'] ) && trim( $field['uprules_dims_maxheight'] ) < $height ) { 186 $valid['is_valid'] = false; 187 $errors .= '<br/>' . sprintf( __("Max height: <b>%dpx</b>", "gforms_uprules"), $field['uprules_dims_maxheight'] ); 188 } 189 190 if ( ! $valid['is_valid'] && ! empty( $errors ) ) 191 $valid['message'] = __("Image dimension didn't meet following size conditions:", "gforms_uprules") . $errors; 192 193 break; 194 } 195 return $valid; 196 } 197 198 public function tooltips( $gf_tooltips ) { 199 $gf_uprules_tooltips = array( 200 'form_field_uprules_filesize' => "<h6>" . __("Filesize Limit", "gforms_uprules") . "</h6>" . __("Enter filesize limit for uploaded file. Exceeding uploads will be rejected with an error.", "gforms_uprules"), 201 'form_field_uprules_dimensions' => "<h6>" . __("Image dimensions", "gforms_uprules") . "</h6>" . __("Set validation conditions for uploaded image. Choose between <i>Exact</i> or <i>Conditional</i> validation methods. Empty fields will be not checked against. All values are in <b>pixels</b>. Non-matching images will be rejected with an error.", "gforms_uprules") 202 ); 203 204 return array_merge( $gf_uprules_tooltips, $gf_tooltips ); 205 } 206 207 public function actions() { 208 add_filter( 'gform_field_validation', array( __CLASS__, 'field_validation' ), 10, 4 ); 209 add_action( 'gform_field_advanced_settings', array( __CLASS__, 'field_settings' ), 5 ); 210 add_action( 'gform_editor_js', array( __CLASS__, 'editor_js' ), 20 ); 211 add_filter( 'gform_tooltips', array( __CLASS__, 'tooltips' ) ); 212 add_action( 'admin_init', array( __CLASS__, 'register_scripts' ) ); 213 add_action( 'init', array( __CLASS__, 'localize' ) ); 214 } 215 216 } 217 218 GFUploadRules::actions(); 219 ======= 1 220 <?php 2 221 … … 217 436 218 437 GFUploadRules::actions(); 438 -
gravity-forms-upload-rules/trunk/readme.txt
r768183 r785508 1 2 === Gravity Forms Upload Rules === 3 Contributors: entr 4 Donate link: http://zzlatev.com/gravity-forms-upload-limits/#donate 5 Tags: gravity-forms upload-rules upload 6 Requires at least: 3.0 7 Tested up to: 3.6 8 Stable tag: 1.0 9 License: GPLv2 or later 10 License URI: http://www.gnu.org/licenses/gpl-2.0.html 11 12 Adds extra upload rules to file/image Gravity Froms upload fields. You will be able to limit upload file size and dimensions of uploaded images. 13 14 == Description == 15 16 Gravity Forms is probably the most popular and mature plugin for building and managing custom forms in WordPress. 17 18 = Upload Rules = 19 This plugin attempts to fill gaps in Gravity Forms upload fields. It enables you to set file size and image size dimensions limitations to your upload fields - both file upload and image upload. 20 21 == Installation == 22 23 1. Upload the `gravity-forms-upload-rules` folder to the `/wp-content/plugins/` directory 24 2. Make sure you have Gravity Forms plugin installed and activated. 25 3. Activate the Gravity Forms Upload Rules plugin through the 'Plugins' menu in WordPress. 26 3. You should be able to use the new field settings under Advanced tab of your upload fields in Gravity Forms - Form Edit screens. 27 28 == Frequently asked questions == 29 30 Not yet. 31 32 == Screenshots == 33 34 1. Plugin settings in Post Image fields under Advanced tab. Filesize limit and Conditional image dimensions. 35 2. Plugin settings in Post Image fields under Advanced tab. Filesize limit and Exact image dimensions. 36 37 == Changelog == 38 39 = 1.0 = 40 41 * Possible fix for those who don't get upload rules fields in their admin form editor 42 43 = 0.91 = 44 45 * Fixed JS issue causing "Uncaught TypeError" error 46 47 = 0.9 = 48 * Initial release 49 50 == Upgrade notice ========= 1 51 === Gravity Forms Upload Rules === 2 52 Contributors: entr … … 48 98 49 99 == Upgrade notice == 100
Note: See TracChangeset
for help on using the changeset viewer.