class-ecs-drb-source-acf.php
2 months ago
class-ecs-drb-source-base.php
2 months ago
class-ecs-drb-source-json.php
2 months ago
class-ecs-drb-source-posts.php
2 months ago
class-ecs-drb-source-terms.php
2 months ago
class-ecs-drb-source-terms.php
139 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Dynamic Repeater Builder — Taxonomy Terms Source |
| 4 | */ |
| 5 | |
| 6 | if ( ! defined( 'ABSPATH' ) ) { |
| 7 | exit; |
| 8 | } |
| 9 | |
| 10 | class ECS_DRB_Source_Terms extends ECS_DRB_Source_Base { |
| 11 | |
| 12 | public function get_id(): string { |
| 13 | return 'terms'; |
| 14 | } |
| 15 | |
| 16 | public function get_label(): string { |
| 17 | return __( 'Taxonomy Terms', 'ele-custom-skin' ); |
| 18 | } |
| 19 | |
| 20 | public function get_config_schema(): array { |
| 21 | $taxonomies = get_taxonomies( [ 'public' => true ], 'objects' ); |
| 22 | $tax_options = []; |
| 23 | foreach ( $taxonomies as $tax ) { |
| 24 | $tax_options[] = [ 'value' => $tax->name, 'label' => $tax->label ]; |
| 25 | } |
| 26 | |
| 27 | return [ |
| 28 | [ |
| 29 | 'key' => 'taxonomy', |
| 30 | 'label' => __( 'Taxonomy', 'ele-custom-skin' ), |
| 31 | 'type' => 'select', |
| 32 | 'options' => $tax_options, |
| 33 | 'default' => 'category', |
| 34 | ], |
| 35 | [ |
| 36 | 'key' => 'number', |
| 37 | 'label' => __( 'Number of Terms', 'ele-custom-skin' ), |
| 38 | 'type' => 'number', |
| 39 | 'default' => 10, |
| 40 | 'min' => 1, |
| 41 | 'max' => 500, |
| 42 | ], |
| 43 | [ |
| 44 | 'key' => 'orderby', |
| 45 | 'label' => __( 'Order By', 'ele-custom-skin' ), |
| 46 | 'type' => 'select', |
| 47 | 'options' => [ |
| 48 | [ 'value' => 'name', 'label' => 'Name' ], |
| 49 | [ 'value' => 'slug', 'label' => 'Slug' ], |
| 50 | [ 'value' => 'count', 'label' => 'Post Count' ], |
| 51 | [ 'value' => 'term_id', 'label' => 'Term ID' ], |
| 52 | ], |
| 53 | 'default' => 'name', |
| 54 | ], |
| 55 | [ |
| 56 | 'key' => 'order', |
| 57 | 'label' => __( 'Order', 'ele-custom-skin' ), |
| 58 | 'type' => 'select', |
| 59 | 'options' => [ |
| 60 | [ 'value' => 'ASC', 'label' => 'Ascending' ], |
| 61 | [ 'value' => 'DESC', 'label' => 'Descending' ], |
| 62 | ], |
| 63 | 'default' => 'ASC', |
| 64 | ], |
| 65 | [ |
| 66 | 'key' => 'hide_empty', |
| 67 | 'label' => __( 'Hide Empty Terms', 'ele-custom-skin' ), |
| 68 | 'type' => 'toggle', |
| 69 | 'default' => true, |
| 70 | ], |
| 71 | [ |
| 72 | 'key' => 'parent', |
| 73 | 'label' => __( 'Parent Term ID (0 = top level only)', 'ele-custom-skin' ), |
| 74 | 'type' => 'number', |
| 75 | 'default' => '', |
| 76 | 'placeholder' => __( 'Leave empty for all levels', 'ele-custom-skin' ), |
| 77 | ], |
| 78 | [ |
| 79 | 'key' => 'search', |
| 80 | 'label' => __( 'Search', 'ele-custom-skin' ), |
| 81 | 'type' => 'text', |
| 82 | 'default' => '', |
| 83 | 'placeholder' => '', |
| 84 | ], |
| 85 | ]; |
| 86 | } |
| 87 | |
| 88 | public function get_available_fields( array $config ): array { |
| 89 | return [ |
| 90 | [ 'key' => 'term_id', 'label' => __( 'Term ID', 'ele-custom-skin' ) ], |
| 91 | [ 'key' => 'name', 'label' => __( 'Term Name', 'ele-custom-skin' ) ], |
| 92 | [ 'key' => 'slug', 'label' => __( 'Term Slug', 'ele-custom-skin' ) ], |
| 93 | [ 'key' => 'description', 'label' => __( 'Description', 'ele-custom-skin' ) ], |
| 94 | [ 'key' => 'count', 'label' => __( 'Post Count', 'ele-custom-skin' ) ], |
| 95 | [ 'key' => 'link', 'label' => __( 'Term URL', 'ele-custom-skin' ) ], |
| 96 | [ 'key' => 'parent_id', 'label' => __( 'Parent Term ID', 'ele-custom-skin' ) ], |
| 97 | [ 'key' => 'taxonomy', 'label' => __( 'Taxonomy', 'ele-custom-skin' ) ], |
| 98 | ]; |
| 99 | } |
| 100 | |
| 101 | public function get_rows( array $config ): array { |
| 102 | $args = [ |
| 103 | 'taxonomy' => sanitize_key( $config['taxonomy'] ?? 'category' ), |
| 104 | 'number' => intval( $config['number'] ?? 10 ), |
| 105 | 'orderby' => sanitize_key( $config['orderby'] ?? 'name' ), |
| 106 | 'order' => in_array( $config['order'] ?? 'ASC', [ 'ASC', 'DESC' ], true ) ? $config['order'] : 'ASC', |
| 107 | 'hide_empty' => (bool) ( $config['hide_empty'] ?? true ), |
| 108 | ]; |
| 109 | |
| 110 | if ( isset( $config['parent'] ) && $config['parent'] !== '' ) { |
| 111 | $args['parent'] = intval( $config['parent'] ); |
| 112 | } |
| 113 | if ( ! empty( $config['search'] ) ) { |
| 114 | $args['search'] = sanitize_text_field( $config['search'] ); |
| 115 | } |
| 116 | |
| 117 | $terms = get_terms( $args ); |
| 118 | if ( is_wp_error( $terms ) ) { |
| 119 | return []; |
| 120 | } |
| 121 | |
| 122 | $rows = []; |
| 123 | foreach ( $terms as $term ) { |
| 124 | $rows[] = [ |
| 125 | 'term_id' => (string) $term->term_id, |
| 126 | 'name' => $term->name, |
| 127 | 'slug' => $term->slug, |
| 128 | 'description' => $term->description, |
| 129 | 'count' => (string) $term->count, |
| 130 | 'link' => get_term_link( $term ), |
| 131 | 'parent_id' => (string) $term->parent, |
| 132 | 'taxonomy' => $term->taxonomy, |
| 133 | ]; |
| 134 | } |
| 135 | |
| 136 | return $rows; |
| 137 | } |
| 138 | } |
| 139 |