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