/** * Normalize parameters for field * * @param array $field * * @return array */ static function normalize_field($field) { $field = parent::normalize_field($field); $field = wp_parse_args($field, array('js_options' => array())); $field['js_options'] = wp_parse_args($field['js_options'], array('allowClear' => true, 'width' => 'resolve', 'placeholder' => $field['placeholder'])); return $field; }
/** * Normalize parameters for field * * @param array $field * * @return array */ static function normalize_field($field) { $field = wp_parse_args($field, array('post_type' => 'post', 'field_type' => 'select_advanced', 'parent' => false, 'query_args' => array())); /** * Set default placeholder * - If multiple post types: show 'Select a post' * - If single post type: show 'Select a %post_type_name%' */ if (empty($field['placeholder'])) { $label = __('Select a post', 'meta-box'); if (is_string($field['post_type']) && post_type_exists($field['post_type'])) { $post_type_object = get_post_type_object($field['post_type']); $label = sprintf(__('Select a %s', 'meta-box'), $post_type_object->labels->singular_name); } $field['placeholder'] = $label; } if ($field['parent']) { $field['multiple'] = false; $field['field_name'] = 'parent_id'; } $field['query_args'] = wp_parse_args($field['query_args'], array('post_type' => $field['post_type'], 'post_status' => 'publish', 'posts_per_page' => -1)); switch ($field['field_type']) { case 'select': return RWMB_Select_Field::normalize_field($field); break; case 'select_advanced': default: return RWMB_Select_Advanced_Field::normalize_field($field); } }
/** * Get the attributes for a field * * @param array $field * @param mixed $value * * @return array */ public static function get_attributes($field, $value = null) { $attributes = parent::get_attributes($field, $value); $attributes['multiple'] = false; $attributes['id'] = false; return $attributes; }
/** * Enqueue scripts and styles */ public static function admin_enqueue_scripts() { RWMB_Input_List_Field::admin_enqueue_scripts(); RWMB_Select_Field::admin_enqueue_scripts(); RWMB_Select_Tree_Field::admin_enqueue_scripts(); RWMB_Select_Advanced_Field::admin_enqueue_scripts(); }
/** * Normalize parameters for field * * @param array $field * * @return array */ static function normalize_field($field) { $field = parent::normalize_field($field); $field = wp_parse_args($field, array('js_options' => array())); $field['js_options'] = wp_parse_args($field['js_options'], array('allowClear' => true, 'width' => 'resolve', 'placeholder' => empty($field['std']) ? __('Select a value', 'rwmb') : $field['std'])); $field['std'] = ''; return $field; }
/** * Normalize parameters for field * * @param array $field * * @return array */ static function normalize_field($field) { $field = wp_parse_args($field, array('js_options' => array())); $field['js_options'] = wp_parse_args($field['js_options'], array('allowClear' => true, 'width' => 'resolve', 'placeholder' => __('Select a value', 'rwmb'))); $field['default'] = isset($field['default']) ? $field['default'] : $field['js_options']['placeholder']; $field = parent::normalize_field($field); return $field; }
/** * Normalize parameters for field * * @param array $field * * @return array */ static function normalize_field($field) { $field = wp_parse_args($field, array('field_type' => 'select_advanced', 'parent' => false, 'query_args' => array())); $field['std'] = empty($field['std']) ? __('Select an user', 'meta-box') : $field['std']; $field['query_args'] = wp_parse_args($field['query_args'], array('orderby' => 'display_name', 'order' => 'asc', 'role' => '', 'fields' => 'all')); switch ($field['field_type']) { case 'select': return RWMB_Select_Field::normalize_field($field); break; case 'select_advanced': default: return RWMB_Select_Advanced_Field::normalize_field($field); } }
/** * Walker for displaying select in tree format * * @param $meta * @param $field * @param $elements * @param int $parent * @param bool $active * * @return string */ static function walk_select_tree($meta, $field, $elements, $parent = 0, $active = false) { if (!isset($elements[$parent])) { return; } $terms = $elements[$parent]; $field['options'] = self::get_options($terms); $classes = array('rw-taxonomy-tree'); $classes[] = $active ? 'active' : 'disabled'; $classes[] = "rwmb-taxonomy-{$parent}"; $html = '<div class="' . implode(' ', $classes) . '">'; $html .= RWMB_Select_Field::html($meta, $field); foreach ($terms as $term) { $html .= self::walk_select_tree($meta, $field, $elements, $term->term_id, $active && in_array($term->term_id, $meta)); } $html .= '</div>'; return $html; }
/** * Save meta value * If field is cloneable, value is saved as a single entry in DB * Otherwise value is saved as multiple entries (for backward compatibility) * * TODO: A good way to ALWAYS save values in single entry in DB, while maintaining backward compatibility * * @param $new * @param $old * @param $post_id * @param $field */ static function save($new, $old, $post_id, $field) { return RWMB_Select_Field::save($new, $old, $post_id, $field); }
/** * Enqueue scripts and styles * * @return void */ static function admin_enqueue_scripts() { wp_enqueue_style('rwmb-object-choice', RWMB_CSS_URL . 'object-choice.css', array(), RWMB_VER); wp_enqueue_script('rwmb-object-choice', RWMB_JS_URL . 'object-choice.js', array(), RWMB_VER, true); RWMB_Select_Field::admin_enqueue_scripts(); RWMB_Select_Advanced_Field::admin_enqueue_scripts(); }
/** * Get the attributes for a field * * @param array $field * @param mixed value * * @return array */ static function get_attributes($field, $value = null) { $attributes = parent::get_attributes($field, $value); $attributes = wp_parse_args($attributes, array('data-options' => wp_json_encode($field['js_options']))); return $attributes; }
/** * Output the field value * Display unordered list of option labels, not option values * * @param array $field Field parameters * @param array $args Additional arguments. Not used for these fields. * @param int|null $post_id Post ID. null for current post. Optional. * * @return string Link(s) to post */ static function the_value($field, $args = array(), $post_id = null) { return RWMB_Select_Field::the_value($field, $args, $post_id); }
/** * Walker for displaying select in treeformat * * @param $meta * @param $field * @param $elements * @param int $parent * @param string $parent_slug * @param bool $active * * @return string */ static function walk_select_tree($meta, $field, $elements, $parent = 0, $parent_slug = '', $active = false) { if (!isset($elements[$parent])) { return; } $terms = $elements[$parent]; $field['options'] = self::get_options($terms); $hidden = $active ? 'active' : 'disabled'; $disabled = disabled($active, false, false); $id = empty($parent_slug) ? '' : " id='rwmb-taxonomy-{$parent_slug}'"; $html = "<div{$id} class='rw-taxonomy-tree {$hidden}'>"; $html .= RWMB_Select_Field::html($html, $meta, $field); foreach ($terms as $term) { $html .= self::walk_select_tree($meta, $field, $elements, $term->term_id, $term->slug, in_array($term->slug, $meta) && $active) . '</li>'; } $html .= '</div>'; return $html; }
/** * Get field HTML * * @param mixed $meta * @param array $field * * @return string */ static function html($meta, $field) { $field['options'] = self::get_options($field); return RWMB_Select_Field::html($meta, $field); }