| @@ -1,140 +1,110 @@ | ||
| 1 | -<?php | |
| 2 | - | |
| 3 | -/** | |
| 4 | - * Text Field Class | |
| 5 | - */ | |
| 6 | -class WeForms_Form_Field_Image extends WeForms_Field_Contract { | |
| 7 | - | |
| 8 | - public function __construct() { | |
| 9 | - $this->name = __( 'Image Upload', 'weforms' ); | |
| 10 | - $this->input_type = 'image_upload'; | |
| 11 | - $this->icon = 'file-image-o'; | |
| 12 | - } | |
| 13 | - | |
| 14 | - /** | |
| 15 | - * Render the text field | |
| 16 | - * | |
| 17 | - * @param array $field_settings | |
| 18 | - * @param int $form_id | |
| 19 | - * | |
| 20 | - * @return void | |
| 21 | - */ | |
| 22 | - public function render( $field_settings, $form_id ) { | |
| 23 | - $form_settings = weforms()->form->get( $form_id )->get_settings(); | |
| 24 | - $use_theme_css = isset( $form_settings['use_theme_css'] ) ? $form_settings['use_theme_css'] : 'wpuf-style'; | |
| 25 | - $unique_id = sprintf( '%s-%d', $field_settings['name'], $form_id ); ?> | |
| 26 | - <li <?php $this->print_list_attributes( $field_settings ); ?>> | |
| 27 | - <?php $this->print_label( $field_settings, $form_id ); ?> | |
| 28 | - <div class="wpuf-fields"> | |
| 29 | - <div id="wpuf-<?php echo esc_attr( $unique_id ); ?>-upload-container" data-style="<?php echo esc_attr( $use_theme_css ); ?>"> | |
| 30 | - <div class="wpuf-attachment-upload-filelist" data-style="<?php echo esc_attr( $use_theme_css ); ?>" data-type="file" data-required="<?php echo esc_attr( $field_settings['required'] ); ?>" data-style="<?php echo esc_attr( $use_theme_css ); ?>"> | |
| 31 | - <a id="wpuf-<?php echo esc_attr( $unique_id ); ?>-pickfiles" data-style="<?php echo esc_attr( $use_theme_css ); ?>" data-form_id="<?php echo esc_attr( $form_id ); ?>" class="button file-selector <?php echo ' wpuf_' . esc_attr( $field_settings['name'] ) . '_' . esc_attr( | |
| 32 | - $form_id); ?>" href="#"><?php echo esc_attr ( $field_settings['button_label'] ); ?></a> | |
| 33 | - <ul class="wpuf-attachment-list thumbnails"></ul> | |
| 34 | - </div> | |
| 35 | - </div><!-- .container --> | |
| 36 | - | |
| 37 | - <?php $this->help_text( $field_settings ); ?> | |
| 38 | - | |
| 39 | - </div> <!-- .wpuf-fields --> | |
| 40 | - </li> | |
| 41 | - <?php | |
| 42 | - $uid = esc_attr( $unique_id ); | |
| 43 | - $count = esc_attr( $field_settings['count'] ); | |
| 44 | - $name = esc_attr( $field_settings['name'] ); | |
| 45 | - $max_size = esc_attr($field_settings['max_size']); | |
| 46 | - | |
| 47 | - $script = ";(function($) { | |
| 48 | - $(document).ready( function() { | |
| 49 | - var uploader = new WPUF_Uploader( | |
| 50 | - 'wpuf-{$uid}-pickfiles', | |
| 51 | - 'wpuf-{$uid}-upload-container', | |
| 52 | - {$count}, | |
| 53 | - '{$name}', | |
| 54 | - 'jpg,jpeg,gif,png,bmp', | |
| 55 | - {$max_size} | |
| 56 | - ); | |
| 57 | - wpuf_plupload_items.push(uploader); | |
| 58 | - }); | |
| 59 | - })(jQuery);"; | |
| 60 | - | |
| 61 | - wp_add_inline_script( 'wpuf-form', $script ); | |
| 62 | - } | |
| 63 | - | |
| 64 | - /** | |
| 65 | - * Get field options setting | |
| 66 | - * | |
| 67 | - * @return array | |
| 68 | - */ | |
| 69 | - public function get_options_settings() { | |
| 70 | - $default_options = $this->get_default_option_settings( true, ['dynamic', 'width'] ); // exclude dynamic | |
| 71 | - | |
| 72 | - $settings = [ | |
| 73 | - [ | |
| 74 | - 'name' => 'max_size', | |
| 75 | - 'title' => __( 'Max. file size', 'weforms' ), | |
| 76 | - 'type' => 'text', | |
| 77 | - 'section' => 'advanced', | |
| 78 | - 'priority' => 20, | |
| 79 | - 'help_text' => __( 'Enter maximum upload size limit in KB', 'weforms' ), | |
| 80 | - ], | |
| 81 | - | |
| 82 | - [ | |
| 83 | - 'name' => 'count', | |
| 84 | - 'title' => __( 'Max. files', 'weforms' ), | |
| 85 | - 'type' => 'text', | |
| 86 | - 'section' => 'advanced', | |
| 87 | - 'priority' => 21, | |
| 88 | - 'help_text' => __( 'Number of images can be uploaded', 'weforms' ), | |
| 89 | - ], | |
| 90 | - [ | |
| 91 | - 'name' => 'button_label', | |
| 92 | - 'title' => __( 'Button Label', 'weforms' ), | |
| 93 | - 'type' => 'text', | |
| 94 | - 'default' => __( 'Select Image', 'weforms' ), | |
| 95 | - 'section' => 'basic', | |
| 96 | - 'priority' => 22, | |
| 97 | - 'help_text' => __( 'Enter a label for the Select button', 'weforms' ), | |
| 98 | - ], | |
| 99 | - ]; | |
| 100 | - | |
| 101 | - return array_merge( $default_options, $settings ); | |
| 102 | - } | |
| 103 | - | |
| 104 | - /** | |
| 105 | - * Get the field props | |
| 106 | - * | |
| 107 | - * @return array | |
| 108 | - */ | |
| 109 | - public function get_field_props() { | |
| 110 | - $defaults = $this->default_attributes(); | |
| 111 | - $props = [ | |
| 112 | - 'max_size' => '1024', | |
| 113 | - 'count' => '1', | |
| 114 | - 'button_label' => __( 'Select Image', 'weforms' ), | |
| 115 | - ]; | |
| 116 | - | |
| 117 | - return array_merge( $defaults, $props ); | |
| 118 | - } | |
| 119 | - | |
| 120 | - /** | |
| 121 | - * Prepare entry | |
| 122 | - * | |
| 123 | - * @param $field | |
| 124 | - * | |
| 125 | - * @return @return mixed | |
| 126 | - */ | |
| 127 | - public function prepare_entry( $field, $args = [] ) { | |
| 128 | - if( empty( $_POST[ '_wpnonce' ] ) ) { | |
| 129 | - wp_send_json_error( __( 'Unauthorized operation', 'weforms' ) ); | |
| 130 | - } | |
| 131 | - | |
| 132 | - if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['_wpnonce'] ) ), 'wpuf_form_add' ) ) { | |
| 133 | - wp_send_json_error( __( 'Unauthorized operation', 'weforms' ) ); | |
| 134 | - } | |
| 135 | - | |
| 136 | - $args = ! empty( $args ) ? $args : weforms_clean( $_POST ); | |
| 137 | - | |
| 138 | - return isset( $args['wpuf_files'][$field['name']] ) ? $args['wpuf_files'][$field['name']] : []; | |
| 139 | - } | |
| 140 | -} | |
| 1 | +<?php | |
| 2 | + | |
| 3 | +/** | |
| 4 | + * Text Field Class | |
| 5 | + */ | |
| 6 | +class WeForms_Form_Field_Image extends WeForms_Field_Contract { | |
| 7 | + | |
| 8 | + function __construct() { | |
| 9 | + $this->name = __( 'Image Upload', 'weforms' ); | |
| 10 | + $this->input_type = 'image_upload'; | |
| 11 | + $this->icon = 'file-image-o'; | |
| 12 | + } | |
| 13 | + | |
| 14 | + /** | |
| 15 | + * Render the text field | |
| 16 | + * | |
| 17 | + * @param array $field_settings | |
| 18 | + * @param integer $form_id | |
| 19 | + * | |
| 20 | + * @return void | |
| 21 | + */ | |
| 22 | + public function render( $field_settings, $form_id ) { | |
| 23 | + $unique_id = sprintf( '%s-%d', $field_settings['name'], $form_id ); | |
| 24 | + ?> | |
| 25 | + <li <?php $this->print_list_attributes( $field_settings ); ?>> | |
| 26 | + <?php $this->print_label( $field_settings, $form_id ); ?> | |
| 27 | + | |
| 28 | + <div class="wpuf-fields"> | |
| 29 | + <div id="wpuf-<?php echo $unique_id; ?>-upload-container"> | |
| 30 | + <div class="wpuf-attachment-upload-filelist" data-type="file" data-required="<?php echo $field_settings['required']; ?>"> | |
| 31 | + <a id="wpuf-<?php echo $unique_id; ?>-pickfiles" data-form_id="<?php echo $form_id; ?>" class="button file-selector <?php echo ' wpuf_' . $field_settings['name'] . '_' . $form_id; ?>" href="#"><?php _e( 'Select Image', 'weforms' ); ?></a> | |
| 32 | + | |
| 33 | + <ul class="wpuf-attachment-list thumbnails"></ul> | |
| 34 | + </div> | |
| 35 | + </div><!-- .container --> | |
| 36 | + | |
| 37 | + <?php $this->help_text( $field_settings ); ?> | |
| 38 | + | |
| 39 | + </div> <!-- .wpuf-fields --> | |
| 40 | + | |
| 41 | + <script type="text/javascript"> | |
| 42 | + ;(function($) { | |
| 43 | + $(document).ready( function(){ | |
| 44 | + var uploader = new WPUF_Uploader('wpuf-<?php echo $unique_id; ?>-pickfiles', 'wpuf-<?php echo $unique_id; ?>-upload-container', <?php echo $field_settings['count']; ?>, '<?php echo $field_settings['name']; ?>', 'jpg,jpeg,gif,png,bmp', <?php echo $field_settings['max_size'] ?>); | |
| 45 | + wpuf_plupload_items.push(uploader); | |
| 46 | + }); | |
| 47 | + })(jQuery); | |
| 48 | + </script> | |
| 49 | + | |
| 50 | + </li> | |
| 51 | + <?php | |
| 52 | + } | |
| 53 | + | |
| 54 | + /** | |
| 55 | + * Get field options setting | |
| 56 | + * | |
| 57 | + * @return array | |
| 58 | + */ | |
| 59 | + public function get_options_settings() { | |
| 60 | + $default_options = $this->get_default_option_settings(true, array('dynamic', 'width') ); // exclude dynamic | |
| 61 | + | |
| 62 | + $settings = array( | |
| 63 | + array( | |
| 64 | + 'name' => 'max_size', | |
| 65 | + 'title' => __( 'Max. file size', 'weforms' ), | |
| 66 | + 'type' => 'text', | |
| 67 | + 'section' => 'advanced', | |
| 68 | + 'priority' => 20, | |
| 69 | + 'help_text' => __( 'Enter maximum upload size limit in KB', 'weforms' ), | |
| 70 | + ), | |
| 71 | + | |
| 72 | + array( | |
| 73 | + 'name' => 'count', | |
| 74 | + 'title' => __( 'Max. files', 'weforms' ), | |
| 75 | + 'type' => 'text', | |
| 76 | + 'section' => 'advanced', | |
| 77 | + 'priority' => 21, | |
| 78 | + 'help_text' => __( 'Number of images can be uploaded', 'weforms' ), | |
| 79 | + ), | |
| 80 | + ); | |
| 81 | + | |
| 82 | + return array_merge( $default_options, $settings ); | |
| 83 | + } | |
| 84 | + | |
| 85 | + /** | |
| 86 | + * Get the field props | |
| 87 | + * | |
| 88 | + * @return array | |
| 89 | + */ | |
| 90 | + public function get_field_props() { | |
| 91 | + $defaults = $this->default_attributes(); | |
| 92 | + $props = array( | |
| 93 | + 'max_size' => '1024', | |
| 94 | + 'count' => '1', | |
| 95 | + ); | |
| 96 | + | |
| 97 | + return array_merge( $defaults, $props ); | |
| 98 | + } | |
| 99 | + | |
| 100 | + /** | |
| 101 | + * Prepare entry | |
| 102 | + * | |
| 103 | + * @param $field | |
| 104 | + * | |
| 105 | + * @return @return mixed | |
| 106 | + */ | |
| 107 | + public function prepare_entry( $field ) { | |
| 108 | + return isset( $_POST['wpuf_files'][$field['name']] ) ? $_POST['wpuf_files'][$field['name']] : array(); | |
| 109 | + } | |
| 110 | +} | |