| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
namespace JFB_Modules\Option_Query; |
| 5 |
|
| 6 |
// If this file is called directly, abort. |
| 7 |
if ( ! defined( 'WPINC' ) ) { |
| 8 |
die; |
| 9 |
} |
| 10 |
|
| 11 |
use Jet_Form_Builder\Generators; |
| 12 |
use Jet_Form_Builder\Blocks\Types; |
| 13 |
use Jet_Form_Builder\Plugin; |
| 14 |
use JFB_Modules\Option_Query\Interfaces\Option_Query_It; |
| 15 |
use JFB_Modules\Option_Query\Traits\Option_Query_Trait; |
| 16 |
|
| 17 |
class Legacy_Generator_Query implements Option_Query_It { |
| 18 |
|
| 19 |
use Option_Query_Trait; |
| 20 |
|
| 21 |
/** |
| 22 |
* @var Types\Base |
| 23 |
*/ |
| 24 |
private $block; |
| 25 |
|
| 26 |
public function __construct() { |
| 27 |
$this->disable_feature( 'search' ); |
| 28 |
} |
| 29 |
|
| 30 |
public function rep_item_id() { |
| 31 |
return 'generate'; |
| 32 |
} |
| 33 |
|
| 34 |
public function fetch(): \Generator { |
| 35 |
/** @var Generators\Base $generator_instance */ |
| 36 |
|
| 37 |
$generator = $this->get_setting( 'generator_function' ); |
| 38 |
$generators = Plugin::instance()->form->get_options_generators(); |
| 39 |
$generator_instance = $generators[ $generator ] ?? false; |
| 40 |
|
| 41 |
if ( ! $generator_instance ) { |
| 42 |
return; |
| 43 |
} |
| 44 |
|
| 45 |
$generator_instance->set_block( $this->get_block() ); |
| 46 |
$generated = $generator_instance->get_values( $this->get_settings() ); |
| 47 |
$generator_instance->clear_block(); |
| 48 |
|
| 49 |
$value_from = $this->get_setting( 'value_from' ); |
| 50 |
$calc_from = $this->get_setting( 'calc_from' ); |
| 51 |
|
| 52 |
foreach ( $generated as $key => $item ) { |
| 53 |
|
| 54 |
if ( ! $value_from && ! $calc_from ) { |
| 55 |
yield $item; |
| 56 |
continue; |
| 57 |
} |
| 58 |
|
| 59 |
if ( ! is_array( $item ) ) { |
| 60 |
$item = array( |
| 61 |
'value' => $key, |
| 62 |
'label' => $item, |
| 63 |
); |
| 64 |
} |
| 65 |
|
| 66 |
$item['object_id'] = $item['value']; |
| 67 |
|
| 68 |
if ( ! empty( $value_from ) ) { |
| 69 |
$item['value'] = get_post_meta( $item['object_id'], $value_from, true ); |
| 70 |
} |
| 71 |
|
| 72 |
if ( ! empty( $calc_from ) ) { |
| 73 |
$item['calculate'] = get_post_meta( $item['object_id'], $calc_from, true ); |
| 74 |
} |
| 75 |
|
| 76 |
yield $item; |
| 77 |
|
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* @return Types\Base |
| 83 |
*/ |
| 84 |
public function get_block(): Types\Base { |
| 85 |
return $this->block; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* @param Types\Base $block |
| 90 |
*/ |
| 91 |
public function set_block( Types\Base $block ) { |
| 92 |
$this->block = $block; |
| 93 |
} |
| 94 |
|
| 95 |
} |
| 96 |
|