/plugin/fituet-event/metaboxio/inc/fields/taxonomy.php

https://gitlab.com/thongta/fituet · PHP · 334 lines · 189 code · 37 blank · 108 comment · 22 complexity · 6663279db8656043592b20d097ac4945 MD5 · raw file

  1. <?php
  2. // Prevent loading this file directly
  3. defined( 'ABSPATH' ) || exit;
  4. require_once RWMB_FIELDS_DIR . 'select-advanced.php';
  5. require_once RWMB_FIELDS_DIR . 'checkbox-list.php';
  6. if ( ! class_exists( 'RWMB_Taxonomy_Field' ) ) {
  7. class RWMB_Taxonomy_Field extends RWMB_Field {
  8. /**
  9. * Enqueue scripts and styles
  10. *
  11. * @return void
  12. */
  13. static function admin_enqueue_scripts() {
  14. RWMB_Select_Advanced_Field::admin_enqueue_scripts();
  15. wp_enqueue_style( 'rwmb-taxonomy', RWMB_CSS_URL . 'taxonomy.css', array(), RWMB_VER );
  16. wp_enqueue_script( 'rwmb-taxonomy', RWMB_JS_URL . 'taxonomy.js', array( 'rwmb-select-advanced' ), RWMB_VER, true );
  17. }
  18. /**
  19. * Add default value for 'taxonomy' field
  20. *
  21. * @param $field
  22. *
  23. * @return array
  24. */
  25. static function normalize_field( $field ) {
  26. $default_args = array(
  27. 'hide_empty' => false,
  28. );
  29. // Set default args
  30. $field['options']['args'] = ! isset( $field['options']['args'] ) ? $default_args : wp_parse_args( $field['options']['args'], $default_args );
  31. $tax = get_taxonomy( $field['options']['taxonomy'] );
  32. $field['placeholder'] = empty( $field['placeholder'] ) ? sprintf( __( 'Select a %s', 'meta-box' ), $tax->labels->singular_name ) : $field['placeholder'];
  33. switch ( $field['options']['type'] ) {
  34. case 'select_advanced':
  35. $field = RWMB_Select_Advanced_Field::normalize_field( $field );
  36. break;
  37. case 'checkbox_list':
  38. case 'checkbox_tree':
  39. $field = RWMB_Checkbox_List_Field::normalize_field( $field );
  40. break;
  41. case 'select':
  42. case 'select_tree':
  43. $field = RWMB_Select_Field::normalize_field( $field );
  44. break;
  45. default:
  46. $field['options']['type'] = 'select';
  47. $field = RWMB_Select_Field::normalize_field( $field );
  48. }
  49. if ( in_array( $field['options']['type'], array( 'checkbox_tree', 'select_tree' ) ) ) {
  50. if ( isset( $field['options']['args']['parent'] ) ) {
  51. $field['options']['parent'] = $field['options']['args']['parent'];
  52. unset( $field['options']['args']['parent'] );
  53. } else {
  54. $field['options']['parent'] = 0;
  55. }
  56. }
  57. $field['field_name'] = "{$field['id']}[]";
  58. return $field;
  59. }
  60. /**
  61. * Get field HTML
  62. *
  63. * @param $field
  64. * @param $meta
  65. *
  66. * @return string
  67. */
  68. static function html( $meta, $field ) {
  69. $options = $field['options'];
  70. $terms = get_terms( $options['taxonomy'], $options['args'] );
  71. $field['options'] = self::get_options( $terms );
  72. $field['display_type'] = $options['type'];
  73. $html = '';
  74. switch ( $options['type'] ) {
  75. case 'checkbox_list':
  76. $html = RWMB_Checkbox_List_Field::html( $meta, $field );
  77. break;
  78. case 'checkbox_tree':
  79. $elements = self::process_terms( $terms );
  80. $html .= self::walk_checkbox_tree( $meta, $field, $elements, $options['parent'], true );
  81. break;
  82. case 'select_tree':
  83. $elements = self::process_terms( $terms );
  84. $html .= self::walk_select_tree( $meta, $field, $elements, $options['parent'], true );
  85. break;
  86. case 'select_advanced':
  87. $html = RWMB_Select_Advanced_Field::html( $meta, $field );
  88. break;
  89. case 'select':
  90. default:
  91. $html = RWMB_Select_Field::html( $meta, $field );
  92. }
  93. return $html;
  94. }
  95. /**
  96. * Walker for displaying checkboxes in tree format
  97. *
  98. * @param $meta
  99. * @param $field
  100. * @param $elements
  101. * @param int $parent
  102. * @param bool $active
  103. *
  104. * @return string
  105. */
  106. static function walk_checkbox_tree( $meta, $field, $elements, $parent = 0, $active = false ) {
  107. if ( ! isset( $elements[$parent] ) ) {
  108. return '';
  109. }
  110. $terms = $elements[$parent];
  111. $field['options'] = self::get_options( $terms );
  112. $hidden = $active ? '' : 'hidden';
  113. $html = "<ul class = 'rw-taxonomy-tree {$hidden}'>";
  114. $li = '<li><label><input type="checkbox" name="%s" value="%s"%s> %s</label>';
  115. foreach ( $terms as $term ) {
  116. $html .= sprintf(
  117. $li,
  118. $field['field_name'],
  119. $term->term_id,
  120. checked( in_array( $term->term_id, $meta ), true, false ),
  121. $term->name
  122. );
  123. $html .= self::walk_checkbox_tree( $meta, $field, $elements, $term->term_id, $active && in_array( $term->term_id, $meta ) ) . '</li>';
  124. }
  125. $html .= '</ul>';
  126. return $html;
  127. }
  128. /**
  129. * Walker for displaying select in tree format
  130. *
  131. * @param $meta
  132. * @param $field
  133. * @param $elements
  134. * @param int $parent
  135. * @param bool $active
  136. *
  137. * @return string
  138. */
  139. static function walk_select_tree( $meta, $field, $elements, $parent = 0, $active = false ) {
  140. if ( ! isset( $elements[$parent] ) ) {
  141. return '';
  142. }
  143. $terms = $elements[$parent];
  144. $field['options'] = self::get_options( $terms );
  145. $classes = array( 'rw-taxonomy-tree' );
  146. $classes[] = $active ? 'active' : 'disabled';
  147. $classes[] = "rwmb-taxonomy-{$parent}";
  148. $html = '<div class="' . implode( ' ', $classes ) . '">';
  149. $html .= RWMB_Select_Field::html( $meta, $field );
  150. foreach ( $terms as $term ) {
  151. $html .= self::walk_select_tree( $meta, $field, $elements, $term->term_id, $active && in_array( $term->term_id, $meta ) );
  152. }
  153. $html .= '</div>';
  154. return $html;
  155. }
  156. /**
  157. * Processes terms into indexed array for walker functions
  158. *
  159. * @param $terms
  160. *
  161. * @internal param $field
  162. * @return array
  163. */
  164. static function process_terms( $terms ) {
  165. $elements = array();
  166. foreach ( $terms as $term ) {
  167. $elements[$term->parent][] = $term;
  168. }
  169. return $elements;
  170. }
  171. /**
  172. * Get options for selects, checkbox list, etc via the terms
  173. *
  174. * @param array $terms Array of term objects
  175. *
  176. * @return array
  177. */
  178. static function get_options( $terms = array() ) {
  179. $options = array();
  180. foreach ( $terms as $term ) {
  181. $options[$term->term_id] = $term->name;
  182. }
  183. return $options;
  184. }
  185. /**
  186. * Save meta value
  187. *
  188. * @param mixed $new
  189. * @param mixed $old
  190. * @param int $post_id
  191. * @param array $field
  192. *
  193. * @return string
  194. */
  195. static function save( $new, $old, $post_id, $field ) {
  196. $new = array_unique( array_map( 'intval', (array) $new ) );
  197. $new = empty( $new ) ? null : $new;
  198. wp_set_object_terms( $post_id, $new, $field['options']['taxonomy'] );
  199. }
  200. /**
  201. * Standard meta retrieval
  202. *
  203. * @param int $post_id
  204. * @param bool $saved
  205. * @param array $field
  206. *
  207. * @return array
  208. */
  209. static function meta( $post_id, $saved, $field ) {
  210. $options = $field['options'];
  211. $meta = wp_get_post_terms( $post_id, $options['taxonomy'] );
  212. $meta = is_array( $meta ) ? $meta : (array) $meta;
  213. $meta = wp_list_pluck( $meta, 'term_id' );
  214. return $meta;
  215. }
  216. /**
  217. * Get the field value
  218. * Return list of post term objects
  219. *
  220. * @param array $field Field parameters
  221. * @param array $args Additional arguments. Rarely used. See specific fields for details
  222. * @param int|null $post_id Post ID. null for current post. Optional.
  223. *
  224. * @return array List of post term objects
  225. */
  226. static function get_value( $field, $args = array(), $post_id = null ) {
  227. if ( ! $post_id ) {
  228. $post_id = get_the_ID();
  229. }
  230. $value = wp_get_post_terms( $post_id, $field['options']['taxonomy'] );
  231. // Get single value if necessary
  232. if ( ! $field['clone'] && ! $field['multiple'] ) {
  233. $value = reset( $value );
  234. }
  235. return $value;
  236. }
  237. /**
  238. * Output the field value
  239. * Display unordered list of option labels, not option values
  240. *
  241. * @param array $field Field parameters
  242. * @param array $args Additional arguments. Not used for these fields.
  243. * @param int|null $post_id Post ID. null for current post. Optional.
  244. *
  245. * @return string Link(s) to post
  246. */
  247. static function the_value( $field, $args = array(), $post_id = null ) {
  248. $class = RW_Meta_Box::get_class_name( $field );
  249. $value = call_user_func( array( $class, 'get_value' ), $field, $args, $post_id );
  250. if ( ! $value || is_wp_error( $value ) ) {
  251. return '';
  252. }
  253. $function = array( $class, 'get_option_label' );
  254. if ( $field['clone'] ) {
  255. $output = '<ul>';
  256. if ( $field['multiple'] ) {
  257. foreach ( $value as $subvalue ) {
  258. $output .= '<li>';
  259. array_walk_recursive( $subvalue, $function, $field );
  260. $output .= '<ul><li>' . implode( '</li><li>', $subvalue ) . '</li></ul>';
  261. $output .= '</li>';
  262. }
  263. } else {
  264. array_walk_recursive( $value, $function, $field );
  265. $output = '<li>' . implode( '</li><li>', $value ) . '</li>';
  266. }
  267. $output .= '</ul>';
  268. } else {
  269. if ( $field['multiple'] ) {
  270. array_walk_recursive( $value, $function, $field );
  271. $output = '<ul><li>' . implode( '</li><li>', $value ) . '</li></ul>';
  272. } else {
  273. call_user_func_array( $function, array( &$value, 0, $field ) );
  274. $output = $value;
  275. }
  276. }
  277. return $output;
  278. }
  279. /**
  280. * Get post link to display in the frontend
  281. *
  282. * @param object $value Option value, e.g. term object
  283. * @param int $index Array index
  284. * @param array $field Field parameter
  285. *
  286. * @return string
  287. */
  288. static function get_option_label( &$value, $index, $field ) {
  289. $value = sprintf(
  290. '<a href="%s" title="%s">%s</a>',
  291. esc_url( get_term_link( $value ) ),
  292. esc_attr( $value->name ),
  293. $value->name
  294. );
  295. }
  296. }
  297. }