class-acf-walker-taxonomy-field.php
134 lines
| 1 | <?php |
| 2 | |
| 3 | if ( ! defined( 'ABSPATH' ) ) { |
| 4 | exit; // Exit if accessed directly |
| 5 | } |
| 6 | |
| 7 | if ( ! class_exists( 'ACF_Taxonomy_Field_Walker' ) ) : |
| 8 | |
| 9 | class ACF_Taxonomy_Field_Walker extends Walker { |
| 10 | |
| 11 | /** |
| 12 | * What the class handles. |
| 13 | * |
| 14 | * @since ACF 2.1.0 |
| 15 | * @var string |
| 16 | */ |
| 17 | public $tree_type = 'category'; |
| 18 | |
| 19 | /** |
| 20 | * DB fields to use. |
| 21 | * |
| 22 | * @since ACF 2.1.0 |
| 23 | * @var array |
| 24 | */ |
| 25 | public $db_fields = array( |
| 26 | 'parent' => 'parent', |
| 27 | 'id' => 'term_id', |
| 28 | ); |
| 29 | |
| 30 | /** |
| 31 | * The field being rendered. |
| 32 | * |
| 33 | * @since ACF 1.0.0 |
| 34 | * @var array |
| 35 | */ |
| 36 | public $field; |
| 37 | |
| 38 | /** |
| 39 | * Constructor |
| 40 | * |
| 41 | * @date 20/4/21 |
| 42 | * @since ACF 1.0.0 |
| 43 | * |
| 44 | * @param array $field The field being rendered. |
| 45 | * @return void |
| 46 | */ |
| 47 | function __construct( $field ) { |
| 48 | $this->field = $field; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Starts the list before the elements are added. |
| 53 | * |
| 54 | * @see Walker:start_lvl() |
| 55 | * |
| 56 | * @since ACF 1.0.0 |
| 57 | * |
| 58 | * @param string $output Used to append additional content (passed by reference). |
| 59 | * @param integer $depth Depth of category. Used for tab indentation. |
| 60 | * @param array $args An array of arguments. @see wp_terms_checklist() |
| 61 | */ |
| 62 | public function start_lvl( &$output, $depth = 0, $args = array() ) { |
| 63 | $indent = str_repeat( "\t", $depth ); |
| 64 | $output .= "$indent<ul class='children acf-bl'>\n"; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Ends the list of after the elements are added. |
| 69 | * |
| 70 | * @see Walker::end_lvl() |
| 71 | * |
| 72 | * @since ACF 1.0.0 |
| 73 | * |
| 74 | * @param string $output Used to append additional content (passed by reference). |
| 75 | * @param integer $depth Depth of category. Used for tab indentation. |
| 76 | * @param array $args An array of arguments. @see wp_terms_checklist() |
| 77 | */ |
| 78 | public function end_lvl( &$output, $depth = 0, $args = array() ) { |
| 79 | $indent = str_repeat( "\t", $depth ); |
| 80 | $output .= "$indent</ul>\n"; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Start the element output. |
| 85 | * |
| 86 | * @see Walker::start_el() |
| 87 | * |
| 88 | * @since ACF 1.0.0 |
| 89 | * |
| 90 | * @param string $output Used to append additional content (passed by reference). |
| 91 | * @param WP_Term $term The current term object. |
| 92 | * @param integer $depth Depth of the term in reference to parents. Default 0. |
| 93 | * @param array $args An array of arguments. @see wp_terms_checklist() |
| 94 | * @param integer $id ID of the current term. |
| 95 | */ |
| 96 | public function start_el( &$output, $term, $depth = 0, $args = array(), $id = 0 ) { |
| 97 | $is_selected = in_array( $term->term_id, $this->field['value'] ); |
| 98 | |
| 99 | // Generate array of checkbox input attributes. |
| 100 | $input_attrs = array( |
| 101 | 'type' => $this->field['field_type'], |
| 102 | 'name' => $this->field['name'], |
| 103 | 'value' => $term->term_id, |
| 104 | ); |
| 105 | if ( $is_selected ) { |
| 106 | $input_attrs['checked'] = true; |
| 107 | } |
| 108 | |
| 109 | $output .= "\n" . '<li data-id="' . esc_attr( $term->term_id ) . '">' . |
| 110 | '<label' . ( $is_selected ? ' class="selected"' : '' ) . '>' . |
| 111 | '<input ' . acf_esc_attrs( $input_attrs ) . '/> ' . |
| 112 | '<span>' . acf_esc_html( $term->name ) . '</span>' . |
| 113 | '</label>'; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Ends the element output, if needed. |
| 118 | * |
| 119 | * @see Walker::end_el() |
| 120 | * |
| 121 | * @since ACF 1.0.0 |
| 122 | * |
| 123 | * @param string $output Used to append additional content (passed by reference). |
| 124 | * @param WP_Term $category The current term object. |
| 125 | * @param integer $depth Depth of the term in reference to parents. Default 0. |
| 126 | * @param array $args An array of arguments. @see wp_terms_checklist() |
| 127 | */ |
| 128 | public function end_el( &$output, $category, $depth = 0, $args = array() ) { |
| 129 | $output .= "</li>\n"; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | endif; |
| 134 |