update-options.php
97 lines
| 1 | <?php |
| 2 | |
| 3 | namespace JFB_Compatibility\Jet_Engine\Actions; |
| 4 | |
| 5 | use Jet_Form_Builder\Actions\Action_Handler; |
| 6 | use Jet_Form_Builder\Actions\Types\Base; |
| 7 | use Jet_Form_Builder\Classes\Tools; |
| 8 | use Jet_Form_Builder\Exceptions\Action_Exception; |
| 9 | |
| 10 | // If this file is called directly, abort. |
| 11 | if ( ! defined( 'WPINC' ) ) { |
| 12 | die; |
| 13 | } |
| 14 | |
| 15 | /** |
| 16 | * Define Base_Type class |
| 17 | */ |
| 18 | class Update_Options extends Base { |
| 19 | |
| 20 | public function get_name() { |
| 21 | return __( 'Update Options', 'jet-form-builder' ); |
| 22 | } |
| 23 | |
| 24 | public function get_id() { |
| 25 | return 'update_options'; |
| 26 | } |
| 27 | |
| 28 | public function action_attributes() { |
| 29 | return array( |
| 30 | 'meta_fields_map' => array( |
| 31 | 'default' => array(), |
| 32 | ), |
| 33 | 'options_page' => array( |
| 34 | 'default' => '', |
| 35 | ), |
| 36 | ); |
| 37 | } |
| 38 | |
| 39 | public function self_script_name() { |
| 40 | return 'jetFormUpdateOptionsData'; |
| 41 | } |
| 42 | |
| 43 | public function editor_labels() { |
| 44 | return array( |
| 45 | 'options_page' => __( 'Options Page:', 'jet-form-builder' ), |
| 46 | 'options_map' => __( 'Options Map:', 'jet-form-builder' ), |
| 47 | ); |
| 48 | } |
| 49 | |
| 50 | public function do_action( array $request, Action_Handler $handler ) { |
| 51 | $fields_map = ! empty( $this->settings['meta_fields_map'] ) ? $this->settings['meta_fields_map'] : array(); |
| 52 | $options_data = array(); |
| 53 | |
| 54 | if ( empty( $this->settings['options_page'] ) ) { |
| 55 | return; |
| 56 | } |
| 57 | |
| 58 | if ( ! empty( $fields_map ) ) { |
| 59 | foreach ( $fields_map as $form_field => $option_field ) { |
| 60 | if ( ! empty( $option_field ) && isset( $request[ $form_field ] ) ) { |
| 61 | $options_data[ $option_field ] = $request[ $form_field ]; |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | if ( empty( $options_data ) ) { |
| 67 | throw new Action_Exception( 'failed' ); |
| 68 | } |
| 69 | |
| 70 | $option_name = $this->settings['options_page']; |
| 71 | |
| 72 | $page = isset( jet_engine()->options_pages->registered_pages[ $option_name ] ) ? jet_engine()->options_pages->registered_pages[ $option_name ] : false; |
| 73 | |
| 74 | if ( $page && method_exists( $page, 'update_options' ) ) { |
| 75 | $page->update_options( $options_data, false, false ); |
| 76 | return; |
| 77 | } |
| 78 | |
| 79 | $current_value = get_option( $option_name, array() ); |
| 80 | $new_value = array_merge( $current_value, $options_data ); |
| 81 | |
| 82 | update_option( $option_name, $new_value ); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Regsiter custom action data for the editor |
| 87 | * |
| 88 | * @return [type] [description] |
| 89 | */ |
| 90 | public function action_data() { |
| 91 | return array( |
| 92 | 'optionsPages' => Tools::with_placeholder( Tools::get_options_pages_for_js() ), |
| 93 | ); |
| 94 | } |
| 95 | |
| 96 | } |
| 97 |