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