RWMB_Taxonomy_Field

The Evolved RWMB Taxonomy Field class.

Defined (1)

The class is defined in the following location(s).

/meta-box/demo/field-taxonomy.php  
  1. class RWMB_Taxonomy_Field 
  2. /** 
  3. * Enqueue scripts and styles 
  4. * @return void 
  5. */ 
  6. static function admin_enqueue_scripts() 
  7. wp_enqueue_style( 
  8. 'rwmb-taxonomy',  
  9. RWMB_CSS_URL . 'taxonomy.css',  
  10. RWMB_VER 
  11. ); 
  12. wp_enqueue_script( 
  13. 'rwmb-taxonomy',  
  14. RWMB_JS_URL . 'taxonomy.js',  
  15. array( 'jquery', 'wp-ajax-response' ),  
  16. RWMB_VER,  
  17. true 
  18. ); 
  19.  
  20. /** 
  21. * Add default value for 'taxonomy' field 
  22. * @param $field 
  23. * @return array 
  24. */ 
  25. static function normalize_field( $field ) 
  26. // Default query arguments for get_terms() function 
  27. $default_args = array( 
  28. 'hide_empty' => false 
  29. ); 
  30.  
  31. if ( ! isset( $field['options']['args'] ) ) 
  32. $field['options']['args'] = $default_args; 
  33. else 
  34. $field['options']['args'] = wp_parse_args( $field['options']['args'], $default_args ); 
  35.  
  36. // Show field as checkbox list by default 
  37. if ( ! isset( $field['options']['type'] ) ) 
  38. $field['options']['type'] = 'checkbox_list'; 
  39.  
  40. // If field is shown as checkbox list, add multiple value 
  41. if ( in_array( $field['options']['type'], array( 'checkbox_list', 'checkbox_tree', 'select_tree' ) ) ) 
  42. $field['multiple'] = true; 
  43. $field['field_name'] = "{$field['id']}[]"; 
  44.  
  45. if ( in_array( $field['options']['type'], array( 'checkbox_tree', 'select_tree' ) ) ) 
  46. if ( isset( $field['options']['args']['parent'] ) ) 
  47. $field['options']['parent'] = $field['options']['args']['parent']; 
  48. unset( $field['options']['args']['parent'] ); 
  49. else 
  50. $field['options']['parent'] = 0; 
  51.  
  52. return $field; 
  53.  
  54. /** 
  55. * Get field HTML 
  56. * @param $html 
  57. * @param $field 
  58. * @param $meta 
  59. * @return string 
  60. */ 
  61. static function html( $html, $meta, $field ) 
  62.  
  63. $options = $field['options']; 
  64. $terms = get_terms( $options['taxonomy'], $options['args'] ); 
  65.  
  66. $html = ''; 
  67. // Checkbox LIST 
  68. if ( 'checkbox_list' === $options['type'] ) 
  69. $html = array(); 
  70. foreach ( $terms as $term ) 
  71. $checked = checked( in_array( $term->slug, $meta ), true, false ); 
  72. $html[] = "<label><input type='checkbox' name='{$field['field_name']}' value='{$term->slug}'{$checked} /> {$term->name}</label>"; 
  73. $html = implode( '<br />', $html ); 
  74. // Checkbox TREE 
  75. elseif ( 'checkbox_tree' === $options['type'] ) 
  76. $elements = self::process_terms( $terms ); 
  77. $html .= self::walk_checkbox_tree( $meta, $field, $elements, $field['options']['parent'], true ); 
  78. // Select Tree 
  79. elseif ( 'select_tree' == $options['type'] ) 
  80. $elements = self::process_terms( $terms ); 
  81. $html .= self::walk_select_tree( $meta, $field, $elements, $field['options']['parent'], '', true ); 
  82. // Select 
  83. else 
  84. $multiple = $field['multiple'] ? " multiple='multiple' style='height: auto;'" : "'"; 
  85. $html .= "<select name='{$field['field_name']}'{$multiple}>"; 
  86. foreach ( $terms as $term ) 
  87. $selected = selected( in_array( $term->slug, $meta ), true, false ); 
  88. $html .= "<option value='{$term->slug}'{$selected}>{$term->name}</option>"; 
  89. $html .= "</select>"; 
  90.  
  91. return $html; 
  92.  
  93. /** 
  94. * Walker for displaying checkboxes in treeformat 
  95. * @param $meta 
  96. * @param $field 
  97. * @param $elements 
  98. * @param int $parent 
  99. * @param bool $active 
  100. * @return string 
  101. */ 
  102. static function walk_checkbox_tree( $meta, $field, $elements, $parent = 0, $active = false ) 
  103. if(!isset($elements[$parent])) 
  104. return; 
  105. $terms = $elements[$parent]; 
  106. $hidden = ( !$active ? 'hidden' : '' ); 
  107.  
  108. $html = "<ul class = 'rw-taxonomy-tree {$hidden}'>"; 
  109. foreach ( $terms as $term ) 
  110. $checked = checked( in_array( $term->slug, $meta ), true, false ); 
  111. $html .= "<li><label><input type='checkbox' name='{$field['field_name']}' value='{$term->slug}'{$checked} /> {$term->name}</label>"; 
  112. $html .= self::walk_checkbox_tree( $meta, $field, $elements, $term->term_id, ( in_array( $term->slug, $meta ) ) && $active ) . "</li>"; 
  113. $html .= "</ul>"; 
  114.  
  115. return $html; 
  116.  
  117. /** 
  118. * Walker for displaying select in treeformat 
  119. * @param $meta 
  120. * @param $field 
  121. * @param $elements 
  122. * @param int $parent 
  123. * @param string $parent_slug 
  124. * @param bool $active 
  125. * @return string 
  126. */ 
  127. static function walk_select_tree( $meta, $field, $elements, $parent = 0, $parent_slug='', $active = false ) 
  128. if ( ! isset( $elements[$parent] ) ) 
  129. return; 
  130. $terms = $elements[$parent]; 
  131. $hidden = ( ! $active ? 'disabled' : 'active' ); 
  132. $disabled = disabled( $active, false, false ); 
  133. $multiple = $field['multiple'] ? " multiple='multiple' style='height: auto;'" : "'"; 
  134. $id = ''; 
  135. if ( ! empty( $parent_slug ) ) 
  136. $id = "id='rwmb-taxonomy-{$parent_slug}'"; 
  137. $html = "<div {$id} class = 'rw-taxonomy-tree {$hidden}'>"; 
  138. $html .= "<select name='{$field['field_name']}'{$disabled} {$multiple}>"; 
  139. $html .= "<option value=''>None</option>"; 
  140. foreach ( $terms as $term ) 
  141. $selected = selected( in_array( $term->slug, $meta ), true, false ); 
  142. $html .= "<option value='{$term->slug}'{$selected}>{$term->name}</option>"; 
  143. $html .= "</select>"; 
  144. foreach ( $terms as $term ) 
  145. $html .= self::walk_select_tree( $meta, $field, $elements, $term->term_id, $term->slug, ( in_array( $term->slug, $meta ) ) && $active ) . "</li>"; 
  146. $html .= "</div>"; 
  147.  
  148. return $html; 
  149.  
  150. /** 
  151. * Processes terms into indexed array for walker functions 
  152. * @param $terms 
  153. * @internal param $field 
  154. * @return array 
  155. */ 
  156. static function process_terms( $terms ) 
  157. $elements = array(); 
  158. foreach ( $terms as $term ) 
  159. $elements[$term->parent][] = $term; 
  160. return $elements; 
  161.  
  162. /** 
  163. * Save post taxonomy 
  164. * @param $post_id 
  165. * @param $field 
  166. * @param $old 
  167. * @param $new 
  168. */ 
  169. static function save( $new, $old, $post_id, $field ) 
  170. wp_set_object_terms( $post_id, $new, $field['options']['taxonomy'] ); 
  171.  
  172.  
  173. /** 
  174. * Standard meta retrieval 
  175. * @param mixed $meta 
  176. * @param int $post_id 
  177. * @param array $field 
  178. * @param bool $saved 
  179. * @return mixed 
  180. */ 
  181. static function meta( $meta, $post_id, $saved, $field ) 
  182. $options = $field['options']; 
  183.  
  184. $meta = wp_get_post_terms( $post_id, $options['taxonomy'] ); 
  185. $meta = is_array( $meta ) ? $meta : (array) $meta; 
  186. $meta = wp_list_pluck( $meta, 'slug' ); 
  187.  
  188. return $meta; 
  189. } // END Class RWMB_Taxonomy_Field