| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
namespace JFB_Modules\Option_Query; |
| 5 |
|
| 6 |
use JFB_Modules\Option_Query\Interfaces\Option_Query_It; |
| 7 |
use JFB_Modules\Option_Query\Traits\Option_Query_Trait; |
| 8 |
|
| 9 |
// If this file is called directly, abort. |
| 10 |
if ( ! defined( 'WPINC' ) ) { |
| 11 |
die; |
| 12 |
} |
| 13 |
|
| 14 |
class Terms_Query implements Option_Query_It { |
| 15 |
|
| 16 |
use Option_Query_Trait; |
| 17 |
|
| 18 |
public function rep_item_id() { |
| 19 |
return 'terms'; |
| 20 |
} |
| 21 |
|
| 22 |
public function fetch(): \Generator { |
| 23 |
$taxonomy = $this->get_query( 'taxonomy' ); |
| 24 |
|
| 25 |
if ( ! $taxonomy ) { |
| 26 |
return; |
| 27 |
} |
| 28 |
|
| 29 |
if ( ! $this->has_query( 'hide_empty' ) ) { |
| 30 |
$this->set_query( 'hide_empty', false ); |
| 31 |
} |
| 32 |
|
| 33 |
$terms = get_terms( |
| 34 |
apply_filters_deprecated( |
| 35 |
'jet-form-builder/render-choice/query-options/terms', |
| 36 |
array( $this->get_query_params(), $this->get_settings() ), |
| 37 |
'3.3.1', |
| 38 |
'jet-form-builder/option-query/set-in-block' |
| 39 |
) |
| 40 |
); |
| 41 |
|
| 42 |
if ( empty( $terms ) || is_wp_error( $terms ) ) { |
| 43 |
return; |
| 44 |
} |
| 45 |
|
| 46 |
foreach ( $terms as $term ) { |
| 47 |
|
| 48 |
$item = array( |
| 49 |
'object_id' => $term->term_id, |
| 50 |
'value' => $term->term_id, |
| 51 |
'label' => apply_filters( 'jet-form-builder/render-choice/label/terms', $term->name, $term ), |
| 52 |
); |
| 53 |
|
| 54 |
$value_from = $this->get_setting( 'value_from' ); |
| 55 |
|
| 56 |
if ( ! empty( $value_from ) ) { |
| 57 |
if ( isset( $term->$value_from ) ) { |
| 58 |
$item['value'] = $term->$value_from; |
| 59 |
} else { |
| 60 |
$item['value'] = get_term_meta( $term->term_id, $value_from, true ); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
$calc_from = $this->get_setting( 'calc_from' ); |
| 65 |
|
| 66 |
if ( ! empty( $calc_from ) ) { |
| 67 |
$item['calculate'] = get_term_meta( $term->term_id, $calc_from, true ); |
| 68 |
} |
| 69 |
|
| 70 |
yield $item; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
} |
| 75 |
|