Example #1
0
 public function render()
 {
     $field = $this->field;
     $meta_value = $field->escaped_value();
     $options = (array) $field->options();
     $img_size = $field->args('preview_size');
     $query_args = $field->args('query_args');
     $output = '';
     // if options array and 'url' => false, then hide the url field
     $input_type = array_key_exists('url', $options) && false === $options['url'] ? 'hidden' : 'text';
     $output .= parent::render(array('type' => $input_type, 'class' => 'cmb2-upload-file regular-text', 'size' => 45, 'desc' => '', 'data-previewsize' => is_array($img_size) ? '[' . implode(',', $img_size) . ']' : 350, 'data-queryargs' => !empty($query_args) ? json_encode($query_args) : '', 'js_dependencies' => 'media-editor'));
     $output .= sprintf('<input class="cmb2-upload-button button" type="button" value="%s" />', esc_attr($this->_text('add_upload_file_text', __('Add or Upload File', 'cmb2'))));
     $output .= $this->_desc(true);
     $cached_id = $this->_id();
     // Reset field args for attachment ID
     $args = array('id' => ($field->group ? $field->args('_id') : $cached_id) . '_id');
     // And get new field object
     // (Need to set it to the types field property)
     $this->types->field = $field = $field->get_field_clone($args);
     // Get ID value
     $_id_value = $field->escaped_value('absint');
     // If there is no ID saved yet, try to get it from the url
     if ($meta_value && !$_id_value) {
         $_id_value = cmb2_utils()->image_id_from_url(esc_url_raw($meta_value));
     }
     $output .= parent::render(array('type' => 'hidden', 'class' => 'cmb2-upload-file-id', 'value' => $_id_value, 'desc' => ''));
     $output .= '<div id="' . $this->_id('-status') . '" class="cmb2-media-status">';
     if (!empty($meta_value)) {
         if ($this->is_valid_img_ext($meta_value)) {
             if ($_id_value) {
                 $image = wp_get_attachment_image($_id_value, $img_size, null, array('class' => 'cmb-file-field-image'));
             } else {
                 $size = is_array($img_size) ? $img_size[0] : 350;
                 $image = '<img style="max-width: ' . absint($size) . 'px; width: 100%; height: auto;" src="' . $meta_value . '" alt="" />';
             }
             $output .= $this->img_status_output(array('image' => $image, 'tag' => 'div', 'cached_id' => $cached_id));
         } else {
             $output .= $this->file_status_output(array('value' => $meta_value, 'tag' => 'div', 'cached_id' => $cached_id));
         }
     }
     $output .= '</div>';
     return $this->rendered($output);
 }
Example #2
0
 public function render()
 {
     $field = $this->field;
     $meta_value = $field->escaped_value();
     $name = $this->_name();
     $img_size = $field->args('preview_size');
     $query_args = $field->args('query_args');
     $output = '';
     $output .= parent::render(array('type' => 'hidden', 'class' => 'cmb2-upload-file cmb2-upload-list', 'size' => 45, 'desc' => '', 'value' => '', 'data-previewsize' => is_array($img_size) ? sprintf('[%s]', implode(',', $img_size)) : 50, 'data-queryargs' => !empty($query_args) ? json_encode($query_args) : '', 'js_dependencies' => 'media-editor'));
     $output .= parent::render(array('type' => 'button', 'class' => 'cmb2-upload-button button cmb2-upload-list', 'value' => esc_html($this->_text('add_upload_files_text', __('Add or Upload Files', 'cmb2'))), 'name' => '', 'id' => ''));
     $output .= '<ul id="' . $this->_id('-status') . '" class="cmb2-media-status cmb-attach-list">';
     if ($meta_value && is_array($meta_value)) {
         foreach ($meta_value as $id => $fullurl) {
             $id_input = parent::render(array('type' => 'hidden', 'value' => $fullurl, 'name' => $name . '[' . $id . ']', 'id' => 'filelist-' . $id, 'data-id' => $id, 'desc' => '', 'class' => false));
             if ($this->is_valid_img_ext($fullurl)) {
                 $output .= $this->img_status_output(array('image' => wp_get_attachment_image($id, $img_size), 'tag' => 'li', 'id_input' => $id_input));
             } else {
                 $output .= $this->file_status_output(array('value' => $fullurl, 'tag' => 'li', 'id_input' => $id_input));
             }
         }
     }
     $output .= '</ul>';
     return $this->rendered($output);
 }
Example #3
0
 protected function file_output($url_value, $id, CMB2_Type_File_Base $field_type)
 {
     // If there is no ID saved yet, try to get it from the url
     if ($url_value && !$id) {
         $id = cmb2_utils()->image_id_from_url(esc_url_raw($url_value));
     }
     if ($field_type->is_valid_img_ext($url_value)) {
         $img_size = $this->field->args('preview_size');
         if ($id) {
             $image = wp_get_attachment_image($id, $img_size, null, array('class' => 'cmb-image-display'));
         } else {
             $size = is_array($img_size) ? $img_size[0] : 200;
             $image = '<img class="cmb-image-display" style="max-width: ' . absint($size) . 'px; width: 100%; height: auto;" src="' . $url_value . '" alt="" />';
         }
         echo $image;
     } else {
         printf('<div class="file-status"><span>%1$s <strong><a href="%2$s">%3$s</a></strong></span></div>', esc_html($field_type->_text('file_text', __('File:', 'cmb2'))), $url_value, cmb2_utils()->get_file_name_from_path($url_value));
     }
 }