public function file() { $meta_value = $this->field->escaped_value(); $allow = $this->field->args('allow'); $input_type = 'url' == $allow || is_array($allow) && in_array('url', $allow) ? 'text' : 'hidden'; echo $this->input(array('type' => $input_type, 'class' => 'cmb_upload_file', 'size' => 45, 'desc' => '')), '<input class="cmb_upload_button button" type="button" value="' . __('Add or Upload File', 'cmb') . '" />', $this->_desc(true); $cached_id = $this->_id(); // Reset field args for attachment ID $args = $this->field->args(); $args['id'] = $args['_id'] . '_id'; unset($args['_id'], $args['_name']); // And get new field object $this->field = new cmb_Meta_Box_field($args, $this->field->group); // Get ID value $_id_value = $this->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 = cmb_Meta_Box::image_id_from_url(esc_url_raw($meta_value)); } echo $this->input(array('type' => 'hidden', 'class' => 'cmb_upload_file_id', 'value' => $_id_value, 'desc' => '')), '<div id="', $this->_id('_status'), '" class="cmb_media_status">'; if (!empty($meta_value)) { if ($this->is_valid_img_ext($meta_value)) { echo '<div class="img_status">'; echo '<img style="max-width: 350px; width: 100%; height: auto;" src="', $meta_value, '" alt="" />'; echo '<p class="cmb_remove_wrapper"><a href="#" class="cmb_remove_file_button" rel="', $cached_id, '">' . __('Remove Image', 'cmb') . '</a></p>'; echo '</div>'; } else { // $file_ext = $this->get_file_ext( $meta_value ); $parts = explode('/', $meta_value); for ($i = 0; $i < count($parts); ++$i) { $title = $parts[$i]; } echo __('File:', 'cmb'), ' <strong>', $title, '</strong> (<a href="', $meta_value, '" target="_blank" rel="external">' . __('Download', 'cmb') . '</a> / <a href="#" class="cmb_remove_file_button" rel="', $cached_id, '">' . __('Remove', 'cmb') . '</a>)'; } } echo '</div>'; }
/** * Peforms saving of `file` attachement's ID * @since 1.1.0 * @param string $value File url */ public function _save_file_id($value) { $group = $this->field->group; $args = $this->field->args(); $args['id'] = $args['_id'] . '_id'; unset($args['_id'], $args['_name']); // And get new field object $field = new cmb_Meta_Box_field($args, $group); $id_key = $field->_id(); $id_val_old = $field->escaped_value('absint'); if ($group) { // Check group $_POST data $i = $group->index; $base_id = $group->_id(); $id_val = isset($_POST[$base_id][$i][$id_key]) ? absint($_POST[$base_id][$i][$id_key]) : 0; } else { // Check standard $_POST data $id_val = isset($_POST[$field->id()]) ? $_POST[$field->id()] : null; } // If there is no ID saved yet, try to get it from the url if ($value && !$id_val) { $id_val = cmb_Meta_Box::image_id_from_url($value); } if ($group) { return array('attach_id' => $id_val, 'field_id' => $id_key); } if ($id_val && $id_val != $id_val_old) { return $field->update_data($id_val); } elseif (empty($id_val) && $id_val_old) { return $field->remove_data($old); } }
public static function file($field, $meta, $object_id, $object_type) { $input_type_url = 'hidden'; if ('url' == $field['allow'] || is_array($field['allow']) && in_array('url', $field['allow'])) { $input_type_url = 'text'; } echo '<input class="cmb_upload_file" type="' . $input_type_url . '" size="45" id="', $field['id'], '" name="', $field['id'], '" value="', $meta, '" />'; echo '<input class="cmb_upload_button button" type="button" value="' . __('Add or Upload File', 'cmb') . '" />'; $_id_name = $field['id'] . '_id'; $_id_meta = get_metadata($object_type, $object_id, $_id_name, !isset($field['multiple']) || !$field['multiple']); // If there is no ID saved yet, try to get it from the url if ($meta && !$_id_meta) { $_id_meta = cmb_Meta_Box::image_id_from_url(esc_url_raw($meta)); } echo '<input class="cmb_upload_file_id" type="hidden" id="', $_id_name, '" name="', $_id_name, '" value="', $_id_meta, '" />', self::desc($field['desc'], true), '<div id="', $field['id'], '_status" class="cmb_media_status">'; if (!empty($meta)) { if (self::is_valid_img_ext($meta)) { echo '<div class="img_status">'; echo '<img style="max-width: 350px; width: 100%; height: auto;" src="', $meta, '" alt="" />'; echo '<p><a href="#" class="cmb_remove_file_button" rel="', $field['id'], '">' . __('Remove Image', 'cmb') . '</a></p>'; echo '</div>'; } else { // $file_ext = self::get_file_ext( $meta ); $parts = explode('/', $meta); for ($i = 0; $i < count($parts); ++$i) { $title = $parts[$i]; } echo __('File:', 'cmb'), ' <strong>', $title, '</strong> (<a href="', $meta, '" target="_blank" rel="external">' . __('Download', 'cmb') . '</a> / <a href="#" class="cmb_remove_file_button" rel="', $field['id'], '">' . __('Remove', 'cmb') . '</a>)'; } } echo '</div>'; }
/** * Sanitize code textareas * @since 1.0.2 * @param string $meta Meta value * @param array $field Field config array * @return string Sanitized data */ public static function file($meta, $field) { $_id_name = $field['id'] . '_id'; // get _id old value $_id_old = cmb_Meta_Box::get_data($_id_name); // If specified NOT to save the file ID if (isset($field['save_id']) && !$field['save_id']) { $_new_id = ''; } else { // otherwise get the file ID $_new_id = isset($_POST[$_id_name]) ? $_POST[$_id_name] : null; // If there is no ID saved yet, try to get it from the url if (isset($_POST[$field['id']]) && $_POST[$field['id']] && !$_new_id) { $_new_id = cmb_Meta_Box::image_id_from_url(esc_url_raw($_POST[$field['id']])); } } if ($_new_id && $_new_id != $_id_old) { $updated[] = $_id_name; cmb_Meta_Box::update_data($_new_id, $_id_name); } elseif ('' == $_new_id && $_id_old) { $updated[] = $_id_name; cmb_Meta_Box::remove_data($_id_name, $old); } return self::default_sanitization($meta, $field); }