base-handler.php
2 years ago
get-response-handler.php
2 years ago
mailchimp-handler.php
2 years ago
options-handler.php
2 years ago
tab-handler-manager.php
2 years ago
base-handler.php
121 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace Jet_Form_Builder\Admin\Tabs_Handlers; |
| 5 | |
| 6 | // If this file is called directly, abort. |
| 7 | use JFB_Components\Repository\Repository_Item_Instance_Trait; |
| 8 | |
| 9 | if ( ! defined( 'WPINC' ) ) { |
| 10 | die; |
| 11 | } |
| 12 | |
| 13 | abstract class Base_Handler implements Repository_Item_Instance_Trait { |
| 14 | |
| 15 | const PREFIX = 'jet_form_builder_settings__'; |
| 16 | |
| 17 | abstract public function slug(); |
| 18 | |
| 19 | abstract public function on_get_request(); |
| 20 | |
| 21 | abstract public function on_load(); |
| 22 | |
| 23 | public function after_install() { |
| 24 | add_action( "wp_ajax_jet_fb_save_tab__{$this->slug()}", array( $this, 'on_raw_request' ) ); |
| 25 | add_action( 'jet-fb/admin-pages/before-assets/jfb-settings', array( $this, 'before_assets' ) ); |
| 26 | } |
| 27 | |
| 28 | public function on_raw_request() { |
| 29 | if ( |
| 30 | empty( $_POST['_nonce'] ) || |
| 31 | ! wp_verify_nonce( sanitize_key( $_POST['_nonce'] ?? '' ), 'jfb-settings' ) || |
| 32 | ! current_user_can( 'manage_options' ) |
| 33 | ) { |
| 34 | $this->send_response( false ); |
| 35 | |
| 36 | return; |
| 37 | } |
| 38 | |
| 39 | $this->on_get_request(); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * @since 3.1.0 |
| 44 | * |
| 45 | * @return array |
| 46 | */ |
| 47 | public function on_editor_load(): array { |
| 48 | return $this->get_options(); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Define this, if you want to save options |
| 53 | * in Tab_Handler_Manager::$_tabs_options. |
| 54 | * |
| 55 | * Then you can get this data via |
| 56 | * $this->get_global_options() |
| 57 | * |
| 58 | * @return array |
| 59 | */ |
| 60 | public function save_global_default() { |
| 61 | return array(); |
| 62 | } |
| 63 | |
| 64 | public function is_visible( $advanced_options ) { |
| 65 | return true; |
| 66 | } |
| 67 | |
| 68 | public function get_options( $if_empty = array() ) { |
| 69 | $response = get_option( $this->option_name(), false ); |
| 70 | |
| 71 | $response = $response |
| 72 | ? json_decode( $response, true ) |
| 73 | : array(); |
| 74 | |
| 75 | return array_merge( $if_empty, $response ); |
| 76 | } |
| 77 | |
| 78 | public function update_options( $options ) { |
| 79 | $options = wp_json_encode( $options ); |
| 80 | $prev_value = get_option( $this->option_name(), false ); |
| 81 | |
| 82 | if ( $prev_value === $options ) { |
| 83 | return true; |
| 84 | } |
| 85 | |
| 86 | return update_option( $this->option_name(), $options ); |
| 87 | } |
| 88 | |
| 89 | public function before_assets() { |
| 90 | } |
| 91 | |
| 92 | public function option_name() { |
| 93 | return self::PREFIX . $this->slug(); |
| 94 | } |
| 95 | |
| 96 | public function get_success_response_data() { |
| 97 | return array( |
| 98 | 'message' => __( 'Saved successfully!', 'jet-form-builder' ), |
| 99 | ); |
| 100 | } |
| 101 | |
| 102 | public function get_failed_response_data() { |
| 103 | return array( |
| 104 | 'message' => __( 'Unsuccessful save.', 'jet-form-builder' ), |
| 105 | ); |
| 106 | } |
| 107 | |
| 108 | public function send_response( $result ) { |
| 109 | if ( $result ) { |
| 110 | wp_send_json_success( $this->get_success_response_data() ); |
| 111 | } |
| 112 | |
| 113 | wp_send_json_error( $this->get_failed_response_data() ); |
| 114 | } |
| 115 | |
| 116 | public function rep_item_id() { |
| 117 | return $this->slug(); |
| 118 | } |
| 119 | |
| 120 | } |
| 121 |