base-source.php
4 years ago
preset-source-options-page.php
4 years ago
preset-source-post.php
4 years ago
preset-source-query-var.php
4 years ago
preset-source-user.php
4 years ago
preset-source-options-page.php
120 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace Jet_Form_Builder\Presets\Sources; |
| 5 | |
| 6 | use Jet_Form_Builder\Classes\Tools; |
| 7 | use Jet_Form_Builder\Exceptions\Preset_Exception; |
| 8 | |
| 9 | class Preset_Source_Options_Page extends Base_Source { |
| 10 | |
| 11 | public function get_id() { |
| 12 | return 'option_page'; |
| 13 | } |
| 14 | |
| 15 | public function condition(): bool { |
| 16 | return function_exists( 'jet_engine' ); |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * @return object |
| 21 | * @throws Preset_Exception |
| 22 | */ |
| 23 | public function query_source() { |
| 24 | $key = explode( '::', $this->prop ); |
| 25 | |
| 26 | if ( 2 !== count( $key ) || empty( $key[0] ) ) { |
| 27 | throw new Preset_Exception( "Invalid prop: {$this->prop}" ); |
| 28 | } |
| 29 | |
| 30 | $item = jet_engine()->options_pages->registered_pages[ $key[0] ] ?? false; |
| 31 | |
| 32 | if ( ! $item ) { |
| 33 | throw new Preset_Exception( "Undefined option page: {$key[0]}" ); |
| 34 | } |
| 35 | |
| 36 | return $item; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * @param string $prop |
| 41 | * |
| 42 | * @return string |
| 43 | * |
| 44 | * @throws Preset_Exception |
| 45 | * @see https://gist.github.com/MjHead/49ebe7ecc20bff9aaf8516417ed27c38 |
| 46 | */ |
| 47 | public function default_prop( string $prop ) { |
| 48 | $key = explode( '::', $prop ); |
| 49 | |
| 50 | if ( empty( $key[1] ) ) { |
| 51 | throw new Preset_Exception( "Undefined {$key[0]} page value: {$key[1]}" ); |
| 52 | } |
| 53 | |
| 54 | if ( ! is_a( $this->src(), '\Jet_Engine_Options_Page_Factory' ) ) { |
| 55 | throw new Preset_Exception( 'Source is not instance of ' . \Jet_Engine_Options_Page_Factory::class ); |
| 56 | } |
| 57 | |
| 58 | return $this->src()->get( $prop ); |
| 59 | } |
| 60 | |
| 61 | public function after_register() { |
| 62 | add_filter( 'jet-form-builder/editor/preset-config', array( $this, 'manage_config' ) ); |
| 63 | } |
| 64 | |
| 65 | public function manage_config( $config ) { |
| 66 | $config['global_fields'][0]['options'][] = array( |
| 67 | 'value' => $this->get_id(), |
| 68 | 'label' => __( 'Option Page', 'jet-form-builder' ), |
| 69 | ); |
| 70 | |
| 71 | $config['map_fields'][] = array( |
| 72 | 'name' => 'prop', |
| 73 | 'label' => __( 'Option Value', 'jet-form-builder' ), |
| 74 | 'type' => 'grouped_select', |
| 75 | 'options' => Tools::with_placeholder( |
| 76 | $this->get_options_fields_for_select() |
| 77 | ), |
| 78 | 'parent_condition' => array( |
| 79 | 'field' => 'from', |
| 80 | 'value' => $this->get_id(), |
| 81 | ), |
| 82 | ); |
| 83 | |
| 84 | return $config; |
| 85 | } |
| 86 | |
| 87 | public function get_options_fields_for_select(): array { |
| 88 | $result = array(); |
| 89 | |
| 90 | foreach ( jet_engine()->options_pages->options_list as $slug => $data ) { |
| 91 | $blocks_group = array(); |
| 92 | |
| 93 | foreach ( $data['options'] as $name => $field_data ) { |
| 94 | $black_list = array( 'html', 'tab', 'accordion', 'endpoint' ); |
| 95 | |
| 96 | if ( ! in_array( $field_data['type'], $black_list, true ) ) { |
| 97 | $group[ $name ] = $field_data['title']; |
| 98 | |
| 99 | $blocks_group[] = array( |
| 100 | 'value' => $name, |
| 101 | 'label' => $field_data['title'], |
| 102 | ); |
| 103 | } |
| 104 | } |
| 105 | if ( ! empty( $blocks_group ) ) { |
| 106 | $result[] = array( |
| 107 | 'label' => $data['label'], |
| 108 | 'values' => $blocks_group, |
| 109 | ); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | |
| 114 | |
| 115 | return $result; |
| 116 | |
| 117 | } |
| 118 | |
| 119 | } |
| 120 |