Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Newly reconfigured taxonomy field #175

Merged
merged 5 commits into from Nov 9, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion inc/fields/select-advanced.php
Expand Up @@ -33,7 +33,7 @@ static function admin_enqueue_scripts()
* @return string * @return string
*/ */
static function html( $html, $meta, $field ) static function html( $html, $meta, $field )
{ {
$html = sprintf( $html = sprintf(
'<select class="rwmb-select-advanced" name="%s" id="%s"%s data-options="%s">', '<select class="rwmb-select-advanced" name="%s" id="%s"%s data-options="%s">',
$field['field_name'], $field['field_name'],
Expand Down
176 changes: 64 additions & 112 deletions inc/fields/taxonomy.php
@@ -1,6 +1,8 @@
<?php <?php
// Prevent loading this file directly // Prevent loading this file directly
defined( 'ABSPATH' ) || exit; defined( 'ABSPATH' ) || exit;
require_once RWMB_FIELDS_DIR . 'select-advanced.php';
require_once RWMB_FIELDS_DIR . 'checkbox-list.php';


if ( ! class_exists( 'RWMB_Taxonomy_Field' ) ) if ( ! class_exists( 'RWMB_Taxonomy_Field' ) )
{ {
Expand All @@ -13,10 +15,7 @@ class RWMB_Taxonomy_Field
*/ */
static function admin_enqueue_scripts() static function admin_enqueue_scripts()
{ {
wp_enqueue_style( 'select2', RWMB_CSS_URL . 'select2-css/select2.css', array(), '3.2' ); RWMB_Select_Advanced_Field::admin_enqueue_scripts();
wp_enqueue_style( 'rwmb-select-advanced', RWMB_CSS_URL . 'select-advanced.css', array(), RWMB_VER );
wp_register_script( 'select2', RWMB_JS_URL . 'select2-js/select2.js', array(), '3.2', true );
wp_register_script( 'select_advanced', RWMB_JS_URL . 'select-advanced.js', array('select2'), RWMB_VER, true );
wp_enqueue_style( 'rwmb-taxonomy', RWMB_CSS_URL . 'taxonomy.css', array(), RWMB_VER ); wp_enqueue_style( 'rwmb-taxonomy', RWMB_CSS_URL . 'taxonomy.css', array(), RWMB_VER );
wp_enqueue_script( 'rwmb-taxonomy', RWMB_JS_URL . 'taxonomy.js', array( 'jquery', 'select_advanced', 'wp-ajax-response' ), RWMB_VER, true ); wp_enqueue_script( 'rwmb-taxonomy', RWMB_JS_URL . 'taxonomy.js', array( 'jquery', 'select_advanced', 'wp-ajax-response' ), RWMB_VER, true );
} }
Expand All @@ -30,40 +29,33 @@ static function admin_enqueue_scripts()
*/ */
static function normalize_field( $field ) 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"
) );
// Default query arguments for get_terms() function
$default_args = array( $default_args = array(
'hide_empty' => false, 'hide_empty' => false,
); );


if ( ! isset( $field['options']['args'] ) ) //Set default args
$field['options']['args'] = $default_args; $field['options']['args'] = ( ! isset( $field['options']['args'] ) ) ? $default_args : wp_parse_args( $field['options']['args'], $default_args );
else
$field['options']['args'] = wp_parse_args( $field['options']['args'], $default_args ); //Field name be an array by default

$field['field_name'] = "{$field['id']}[]";
// Show field as checkbox list by default
if ( ! isset( $field['options']['type'] ) ) switch( $field['options']['type'] )
$field['options']['type'] = 'checkbox_list';

// If field is shown as checkbox list, add multiple value
if ( in_array( $field['options']['type'], array( 'checkbox_list', 'checkbox_tree' ) ) )
{ {
$field['multiple'] = true; case 'select_advanced':
$field['field_name'] = "{$field['id']}[]"; $field = RWMB_Select_Advanced_Field::normalize_field( $field );
break;
case 'checkbox_list':
case 'checkbox_tree':
$field = RWMB_Checkbox_List_Field::normalize_field( $field );
break;
case 'select':
case 'select_tree':
$field = RWMB_Select_Field::normalize_field( $field );
break;
default:
$field['options']['type'] = 'select';
} }


// For select tree: display it as a normal select box (no multiple attribute), but allows to save multiple values
if ( 'select_tree' == $field['options']['type'] || ('select_advanced' == $field['options']['type'] && $field['multiple'] == true) )
$field['field_name'] = "{$field['id']}[]";

if ( in_array( $field['options']['type'], array( 'checkbox_tree', 'select_tree' ) ) ) if ( in_array( $field['options']['type'], array( 'checkbox_tree', 'select_tree' ) ) )
{ {
if ( isset( $field['options']['args']['parent'] ) ) if ( isset( $field['options']['args']['parent'] ) )
Expand Down Expand Up @@ -94,74 +86,29 @@ static function html( $html, $meta, $field )


$options = $field['options']; $options = $field['options'];
$terms = get_terms( $options['taxonomy'], $options['args'] ); $terms = get_terms( $options['taxonomy'], $options['args'] );
$field['options'] = self::get_options( $terms );


$html = ''; $html = '';
// Checkbox LIST
if ( 'checkbox_list' === $options['type'] ) switch( $options['type'] )
{
$html = array();
$tpl = '<label><input type="checkbox" name="%s" value="%s" %s /> %s</label>';
foreach ( $terms as $term )
{
$html[] = sprintf(
$tpl,
$field['field_name'],
$term->slug,
checked( in_array( $term->slug, $meta ), true, false ),
$term->name
);
}
$html = implode( '<br />', $html );
}
// Checkbox TREE
elseif ( 'checkbox_tree' === $options['type'] )
{
$elements = self::process_terms( $terms );
$html .= self::walk_checkbox_tree( $meta, $field, $elements, $field['options']['parent'], true );
}
// Select TREE
elseif ( 'select_tree' === $options['type'] )
{
$elements = self::process_terms( $terms );
$html .= self::walk_select_tree( $meta, $field, $elements, $field['options']['parent'], '', true );
}
// Select
elseif ( 'select_advanced' === $options['type'] ) {
$html = sprintf(
'<select class="rwmb-select-advanced" name="%s" id="%s"%s data-options ="%s">',
$field['field_name'],
$field['id'],
$field['multiple'] ? ' multiple="multiple"' : '',
esc_attr( json_encode( $field['js_options'] ))
);
$option = '<option value="%s" %s>%s</option>';

foreach ( $terms as $term )
{
$html .= sprintf(
$option,
$term->slug,
selected( in_array( $term->slug, $meta ), true, false ),
$term->name
);
}
$html .= '</select>';
}
else
{ {
$multiple = $field['multiple'] ? " multiple='multiple' style='height: auto;'" : ''; case 'checkbox_list':
$html .= "<select name='{$field['field_name']}'{$multiple}>"; $html = RWMB_Checkbox_List_Field::html( $html, $meta, $field );
$option = '<option value="%s" %s>%s</option>'; break;
foreach ( $terms as $term ) case 'checkbox_tree':
{ $elements = self::process_terms( $terms );
$html .= sprintf( $html .= self::walk_checkbox_tree( $meta, $field, $elements, $options['parent'], true );
$option, break;
$term->slug, case 'select_tree':
selected( in_array( $term->slug, $meta ), true, false ), $elements = self::process_terms( $terms );
$term->name $html .= self::walk_select_tree( $meta, $field, $elements, $options['parent'], '', true );
); break;
} case 'select_advanced':
$html .= '</select>'; $html = RWMB_Select_Advanced_Field::html( $html, $meta, $field );
break;
case 'select':
default:
$html = RWMB_Select_Field::html( $html, $meta, $field );
} }


return $html; return $html;
Expand All @@ -183,6 +130,7 @@ static function walk_checkbox_tree( $meta, $field, $elements, $parent = 0, $acti
if ( ! isset( $elements[$parent] ) ) if ( ! isset( $elements[$parent] ) )
return; return;
$terms = $elements[$parent]; $terms = $elements[$parent];
$field['options'] = self::get_options( $terms );
$hidden = ( !$active ? 'hidden' : '' ); $hidden = ( !$active ? 'hidden' : '' );


$html = "<ul class = 'rw-taxonomy-tree {$hidden}'>"; $html = "<ul class = 'rw-taxonomy-tree {$hidden}'>";
Expand Down Expand Up @@ -220,26 +168,13 @@ static function walk_select_tree( $meta, $field, $elements, $parent = 0, $parent
if ( ! isset( $elements[$parent] ) ) if ( ! isset( $elements[$parent] ) )
return; return;
$terms = $elements[$parent]; $terms = $elements[$parent];
$field['options'] = self::get_options( $terms );
$hidden = $active ? 'active' : 'disabled'; $hidden = $active ? 'active' : 'disabled';
$disabled = disabled( $active, false, false ); $disabled = disabled( $active, false, false );
$multiple = $field['multiple'] ? " multiple='multiple' style='height: auto;'" : '';
$id = empty( $parent_slug ) ? '' : " id='rwmb-taxonomy-{$parent_slug}'"; $id = empty( $parent_slug ) ? '' : " id='rwmb-taxonomy-{$parent_slug}'";


$html = "<div{$id} class='rw-taxonomy-tree {$hidden}'>"; $html = "<div{$id} class='rw-taxonomy-tree {$hidden}'>";
$html .= "<select name='{$field['field_name']}'{$disabled}{$multiple}>"; $html .= RWMB_Select_Field::html( $html, $meta, $field );
$html .= "<option value=''>None</option>";

$option = '<option value="%s" %s>%s</option>';
foreach ( $terms as $term )
{
$html .= sprintf(
$option,
$term->slug,
selected( in_array( $term->slug, $meta ), true, false ),
$term->name
);
}
$html .= '</select>';
foreach ( $terms as $term ) 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 .= self::walk_select_tree( $meta, $field, $elements, $term->term_id, $term->slug, in_array( $term->slug, $meta ) && $active ) . '</li>';
Expand All @@ -266,6 +201,23 @@ static function process_terms( $terms )
} }
return $elements; return $elements;
} }

/**
* Get options for selects, checkbox list, etc via the terms
*
* @param $terms array of term objects
*
* @param $options array
*/
static function get_options( $terms = array() )
{
$options = array();
foreach( $terms as $term )
{
$options[$term->slug] = $term->name;
}
return $options;
}


/** /**
* Save post taxonomy * Save post taxonomy
Expand Down
2 changes: 1 addition & 1 deletion inc/fields/url.php
Expand Up @@ -7,7 +7,7 @@


if ( ! class_exists( 'RWMB_URL_Field' ) ) if ( ! class_exists( 'RWMB_URL_Field' ) )
{ {
class RWMB_URL_Field class RWMB_URL_Field extends RWMB_Text_Field
{ {
/** /**
* Get field HTML * Get field HTML
Expand Down