public function test_image_id_from_url() { global $wp_version; $_id_value = cmb2_utils()->image_id_from_url(esc_url_raw(get_permalink($this->attachment_id))); if ($wp_version > 3.9) { $this->assertEquals($_id_value, $this->attachment_id); } else { $this->assertGreaterThan(0, $this->attachment_id); } }
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); }
/** * Parse the picker attributes. * @since 2.2.0 * @param string $arg 'date' or 'time' * @param array $args Optional arguments to modify (else use $this->field->args['attributes']) * @return array Array of field attributes */ public function parse_picker_options($arg = 'date', $args = array()) { $att = 'data-' . $arg . 'picker'; $update = empty($args); $atts = array(); $format = $this->field->args($arg . '_format'); if ($js_format = cmb2_utils()->php_to_js_dateformat($format)) { if ($update) { $atts = $this->field->args('attributes'); } else { $atts = isset($args['attributes']) ? $args['attributes'] : $atts; } // Don't override user-provided datepicker values $data = isset($atts[$att]) ? json_decode($atts[$att], true) : array(); $data[$arg . 'Format'] = $js_format; $atts[$att] = function_exists('wp_json_encode') ? wp_json_encode($data) : json_encode($data); } if ($update) { $this->field->args['attributes'] = $atts; } return array_merge($args, $atts); }
/** * Enqueue the CMB2 JS * @since 2.0.7 */ public static function enqueue() { // Filter required script dependencies $dependencies = apply_filters('cmb2_script_dependencies', self::$dependencies); // if colorpicker if (!is_admin() && isset($dependencies['wp-color-picker'])) { self::colorpicker_frontend(); } // if file/file_list if (isset($dependencies['media-editor'])) { wp_enqueue_media(); } // if timepicker if (isset($dependencies['jquery-ui-datetimepicker'])) { wp_register_script('jquery-ui-datetimepicker', cmb2_utils()->url('js/jquery-ui-timepicker-addon.min.js'), array('jquery-ui-slider'), CMB2_VERSION); } // Only use minified files if SCRIPT_DEBUG is off $debug = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG; $min = $debug ? '' : '.min'; // Register cmb JS wp_enqueue_script(self::$handle, cmb2_utils()->url("js/cmb2{$min}.js"), $dependencies, CMB2_VERSION, true); self::localize($debug); }
/** * Offset a time value based on timezone * @since 1.0.0 * @return string Offset time string */ public function field_timezone_offset() { return cmb2_utils()->timezone_offset($this->field_timezone()); }
public function render() { $this->field->args['default'] = $this->field->get_default() ? $this->field->get_default() : cmb2_utils()->timezone_string(); $this->args = wp_parse_args($this->args, array('class' => 'cmb2_select cmb2-select-timezone', 'options' => wp_timezone_choice($this->field->escaped_value()), 'desc' => $this->_desc())); return parent::render(); }
public function file() { $meta_value = $this->field->escaped_value(); $options = (array) $this->field->options(); $img_size = $this->field->args('preview_size'); $query_args = $this->field->args('query_args'); // if options array and 'url' => false, then hide the url field $input_type = array_key_exists('url', $options) && false === $options['url'] ? 'hidden' : 'text'; echo $this->input(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) : '')); printf('<input class="cmb2-upload-button button" type="button" value="%s" />', esc_attr($this->_text('add_upload_file_text', __('Add or Upload File', 'cmb2')))); $this->_desc(true, true); // If we're looking at a file in a group, we need to get the non-prefixed id $cached_id = $this->field->group ? $this->field->args('_id') : $this->_id(); // Reset field args for attachment ID $args = $this->field->args(); $args['id'] = $cached_id . '_id'; unset($args['_id'], $args['_name']); // And get new field object $this->field = new CMB2_Field(array('field_args' => $args, 'group_field' => $this->field->group, 'object_type' => $this->field->object_type, 'object_id' => $this->field->object_id)); // 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 = cmb2_utils()->image_id_from_url(esc_url_raw($meta_value)); } echo $this->input(array('type' => 'hidden', 'class' => 'cmb2-upload-file-id', 'value' => $_id_value, 'desc' => '')), '<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="" />'; } $this->img_status_output(array('image' => $image, 'tag' => 'div', 'cached_id' => $cached_id)); } else { $this->file_status_output(array('value' => $meta_value, 'tag' => 'div', 'cached_id' => $cached_id)); } } echo '</div>'; CMB2_JS::add_dependencies('media-editor'); }
public function test_class_getters() { $this->assertInstanceOf('CMB2_Ajax', cmb2_ajax()); $this->assertInstanceOf('CMB2_Utils', cmb2_utils()); $this->assertInstanceOf('CMB2_Option', cmb2_options('test')); }
public function file() { $meta_value = $this->field->escaped_value(); $options = (array) $this->field->args('options'); $img_size = $this->field->args('preview_size'); // if options array and 'url' => false, then hide the url field $input_type = array_key_exists('url', $options) && false === $options['url'] ? 'hidden' : 'text'; echo $this->input(array('type' => $input_type, 'class' => 'cmb2-upload-file', 'size' => 45, 'desc' => '', 'data-previewsize' => is_array($img_size) ? '[' . implode(',', $img_size) . ']' : 350)), '<input class="cmb2-upload-button button" type="button" value="' . esc_attr($this->_text('add_upload_file_text', __('Add or Upload File', 'cmb2'))) . '" />', $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 CMB2_Field(array('field_args' => $args, 'group_field' => $this->field->group, 'object_type' => $this->field->object_type(), 'object_id' => $this->field->object_id())); // 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 = cmb2_utils()->image_id_from_url(esc_url_raw($meta_value)); } echo $this->input(array('type' => 'hidden', 'class' => 'cmb2-upload-file-id', 'value' => $_id_value, 'desc' => '')), '<div id="', $this->_id('-status'), '" class="cmb2-media-status">'; if (!empty($meta_value)) { if ($this->is_valid_img_ext($meta_value)) { echo '<div class="img-status">'; 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="" />'; } echo $image; echo '<p class="cmb2-remove-wrapper"><a href="#" class="cmb2-remove-file-button" rel="', $cached_id, '">' . esc_html($this->_text('remove_image_text', __('Remove Image', 'cmb2'))) . '</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 esc_html($this->_text('file_text', __('File:', 'cmb2'))), ' <strong>', $title, '</strong> (<a href="', $meta_value, '" target="_blank" rel="external">', esc_html($this->_text('file-download-text', __('Download', 'cmb2'))), '</a> / <a href="#" class="cmb2-remove-file-button" rel="', $cached_id, '">', esc_html($this->_text('remove_text', __('Remove', 'cmb2'))), '</a>)'; } } echo '</div>'; }
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)); } }
/** * Add a field array to a fields array in desired position * @since 2.0.2 * @param array $field Metabox field config array * @param array &$fields Array (passed by reference) to append the field (array) to * @param integer $position Optionally specify a position in the array to be inserted */ protected function _add_field_to_array($field, &$fields, $position = 0) { if ($position) { cmb2_utils()->array_insert($fields, array($field['id'] => $field), $position); } else { $fields[$field['id']] = $field; } }
/** * file/file_list file wrap * @since 2.0.2 * @param array $args Array of arguments for output * @return string File wrap output */ public function file_status_output($args) { return sprintf('<%1$s class="file-status"><span>%2$s <strong>%3$s</strong></span> (<a href="%4$s" target="_blank" rel="external">%5$s</a> / <a href="#" class="cmb2-remove-file-button"%6$s>%7$s</a>)%8$s</%1$s>', $args['tag'], esc_html($this->_text('file_text', __('File:', 'cmb2'))), cmb2_utils()->get_file_name_from_path($args['value']), $args['value'], esc_html($this->_text('file_download_text', __('Download', 'cmb2'))), isset($args['cached_id']) ? ' rel="' . $args['cached_id'] . '"' : '', esc_html($this->_text('remove_text', __('Remove', 'cmb2'))), isset($args['id_input']) ? $args['id_input'] : ''); }
/** * Get timestamp from text date * @since 2.2.0 * @param string $value Date value * @return mixed Unix timestamp representing the date. */ public function get_timestamp_from_value($value) { return cmb2_utils()->get_timestamp_from_value($value, $this->args('date_format')); }
/** * Wrapper for `get_terms` to account for changes in WP 4.6 where taxonomy is expected * as part of the arguments. * @since 2.2.2 * @return mixed Array of terms on success */ public function get_terms() { return cmb2_utils()->wp_at_least('4.5.0') ? get_terms(array('taxonomy' => $this->field->args('taxonomy'), 'hide_empty' => false)) : get_terms($this->field->args('taxonomy'), 'hide_empty=0'); }
/** * Peforms saving of `file` attachement's ID * @since 1.1.0 */ public function _save_file_id_value($file_id_key) { $id_field = $this->_new_supporting_field($file_id_key); // Check standard data_to_save data $id_val = isset($this->field->data_to_save[$file_id_key]) ? $this->field->data_to_save[$file_id_key] : null; // If there is no ID saved yet, try to get it from the url if ($this->value && !$id_val) { $id_val = cmb2_utils()->image_id_from_url($this->value); } return $id_field->save_field($id_val); }
/** * Return non-empty value or field default if value IS empty * @since 2.0.0 * * @param mixed $meta_value Field value * * @return mixed Field value, or default value */ public function val_or_default($meta_value) { return !cmb2_utils()->isempty($meta_value) ? $meta_value : $this->get_param_callback_result('default', false); }
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' => 'cmb2_upload_file', 'size' => 45, 'desc' => '')), '<input class="cmb2_upload_button button" type="button" value="' . __('Add or Upload File', 'cmb2') . '" />', $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 CMB2_Field(array('field_args' => $args, 'group_field' => $this->field->group, 'object_type' => $this->field->object_type(), 'object_id' => $this->field->object_id())); // 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 = cmb2_utils()->image_id_from_url(esc_url_raw($meta_value)); } echo $this->input(array('type' => 'hidden', 'class' => 'cmb2_upload_file_id', 'value' => $_id_value, 'desc' => '')), '<div id="', $this->_id('_status'), '" class="cmb2_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="cmb2_remove_wrapper"><a href="#" class="cmb2_remove_file_button" rel="', $cached_id, '">' . __('Remove Image', 'cmb2') . '</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:', 'cmb2'), ' <strong>', $title, '</strong> (<a href="', $meta_value, '" target="_blank" rel="external">' . __('Download', 'cmb2') . '</a> / <a href="#" class="cmb2_remove_file_button" rel="', $cached_id, '">' . __('Remove', 'cmb2') . '</a>)'; } } echo '</div>'; }
/** * Return a formatted timestamp for a field * @since 2.0.0 * @param string $format Either date_format or time_format * @param string $meta_value Optional meta value to check * @return string Formatted date */ public function get_timestamp_format($format = 'date_format', $meta_value = 0) { $meta_value = $meta_value ? $meta_value : $this->escaped_value(); $meta_value = cmb2_utils()->make_valid_time_stamp($meta_value); if (empty($meta_value)) { return ''; } return is_array($meta_value) ? array_map(array($this, 'format_timestamp'), $meta_value, $format) : $this->format_timestamp($meta_value, $format); }
public function test_select_timezone_field_after_value_update() { $value_to_save = cmb2_utils()->timezone_string(); update_post_meta($this->post_id, $this->text_type_field['id'], $value_to_save); $zones = wp_timezone_choice($value_to_save); $this->assertHTMLstringsAreEqual(sprintf('<select class="cmb2_select cmb2-select-timezone" name="field_test_field" id="field_test_field">%s</select><span class="cmb2-metabox-description">This is a description</span>', $zones), $this->capture_render(array($this->get_field_type_object('select_timezone'), 'render'))); delete_post_meta($this->post_id, $this->text_type_field['id']); }
/** * Peforms saving of `file` attachement's ID * @since 1.1.0 */ public function _save_file_id() { $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 CMB2_Field(array('field_args' => $args, 'group_field' => $group, 'object_id' => $this->field->object_id, 'object_type' => $this->field->object_type)); $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 ($this->value && !$id_val) { $id_val = cmb2_utils()->image_id_from_url($this->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($id_val_old); } }
/** * Registers styles for CMB2 * @since 2.0.7 */ protected static function register_styles() { if (self::$css_registration_done) { return; } // Only use minified files if SCRIPT_DEBUG is off $min = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min'; $front = is_admin() ? '' : '-front'; // Filter required styles and register stylesheet $styles = apply_filters('cmb2_style_dependencies', array()); wp_register_style('cmb2-styles', cmb2_utils()->url("css/cmb2{$front}{$min}.css"), $styles); self::$css_registration_done = true; }
/** * Registers scripts and styles for CMB * @since 1.0.0 */ public static function register_scripts() { if (self::$registration_done) { return; } // Only use minified files if SCRIPT_DEBUG is off $min = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min'; if (!is_admin()) { // we need to register colorpicker on the front-end wp_register_script('iris', admin_url('js/iris.min.js'), array('jquery-ui-draggable', 'jquery-ui-slider', 'jquery-touch-punch'), CMB2_VERSION); wp_register_script('wp-color-picker', admin_url('js/color-picker.min.js'), array('iris'), CMB2_VERSION); wp_localize_script('wp-color-picker', 'wpColorPickerL10n', array('clear' => __('Clear', 'cmb2'), 'defaultString' => __('Default', 'cmb2'), 'pick' => __('Select Color', 'cmb2'), 'current' => __('Current Color', 'cmb2'))); } wp_register_script('cmb-timepicker', cmb2_utils()->url('js/jquery.timePicker.min.js')); // scripts required for cmb $scripts = array('jquery', 'jquery-ui-core', 'jquery-ui-datepicker', 'cmb-timepicker', 'wp-color-picker'); // styles required for cmb $styles = array('wp-color-picker'); wp_register_script('cmb2-scripts', cmb2_utils()->url("js/cmb2{$min}.js"), $scripts, CMB2_VERSION); wp_localize_script('cmb2-scripts', 'cmb2_l10', apply_filters('cmb2_localized_data', array('ajax_nonce' => wp_create_nonce('ajax_nonce'), 'ajaxurl' => admin_url('/admin-ajax.php'), 'script_debug' => defined('SCRIPT_DEBUG') && SCRIPT_DEBUG, 'up_arrow_class' => 'dashicons dashicons-arrow-up-alt2', 'down_arrow_class' => 'dashicons dashicons-arrow-down-alt2', 'defaults' => array('color_picker' => false, 'date_picker' => array('changeMonth' => true, 'changeYear' => true, 'dateFormat' => __('mm/dd/yy', 'cmb2'), 'dayNames' => explode(',', __('Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday', 'cmb2')), 'dayNamesMin' => explode(',', __('Su, Mo, Tu, We, Th, Fr, Sa', 'cmb2')), 'dayNamesShort' => explode(',', __('Sun, Mon, Tue, Wed, Thu, Fri, Sat', 'cmb2')), 'monthNames' => explode(',', __('January, February, March, April, May, June, July, August, September, October, November, December', 'cmb2')), 'monthNamesShort' => explode(',', __('Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec', 'cmb2')), 'nextText' => __('Next', 'cmb2'), 'prevText' => __('Prev', 'cmb2'), 'currentText' => __('Today', 'cmb2'), 'closeText' => __('Done', 'cmb2'), 'clearText' => __('Clear', 'cmb2')), 'time_picker' => array('startTime' => '00:00', 'endTime' => '23:59', 'show24Hours' => false, 'separator' => ':', 'step' => 30)), 'strings' => array('upload_file' => __('Use this file', 'cmb2'), 'remove_image' => __('Remove Image', 'cmb2'), 'remove_file' => __('Remove', 'cmb2'), 'file' => __('File:', 'cmb2'), 'download' => __('Download', 'cmb2'), 'check_toggle' => __('Select / Deselect All', 'cmb2'))))); wp_register_style('cmb2-styles', cmb2_utils()->url("css/cmb2.css"), $styles); self::$registration_done = true; }
/** * Get the file name from a url * @since 2.0.0 * @param string $value File url or path * @return string File name */ public function get_file_name_from_path($value) { return cmb2_utils()->get_file_name_from_path($value); }