| 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 |
* @return array |
| 44 |
* @since 3.1.0 |
| 45 |
*/ |
| 46 |
public function on_editor_load(): array { |
| 47 |
return $this->get_options(); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Define this, if you want to save options |
| 52 |
* in Tab_Handler_Manager::$_tabs_options. |
| 53 |
* |
| 54 |
* Then you can get this data via |
| 55 |
* $this->get_global_options() |
| 56 |
* |
| 57 |
* @return array |
| 58 |
*/ |
| 59 |
public function save_global_default() { |
| 60 |
return array(); |
| 61 |
} |
| 62 |
|
| 63 |
public function is_visible( $advanced_options ) { |
| 64 |
return true; |
| 65 |
} |
| 66 |
|
| 67 |
public function get_options( $if_empty = array() ) { |
| 68 |
$response = get_option( $this->option_name(), false ); |
| 69 |
|
| 70 |
$response = $response |
| 71 |
? json_decode( $response, true ) |
| 72 |
: array(); |
| 73 |
|
| 74 |
return array_merge( $if_empty, $response ); |
| 75 |
} |
| 76 |
|
| 77 |
public function update_options( $options ) { |
| 78 |
$json_options = wp_json_encode( $options ); |
| 79 |
$prev_value = get_option( $this->option_name(), false ); |
| 80 |
|
| 81 |
if ( $prev_value === $json_options ) { |
| 82 |
return true; |
| 83 |
} |
| 84 |
|
| 85 |
$prev_options = json_decode( $prev_value, true ) ?: array(); |
| 86 |
|
| 87 |
return update_option( |
| 88 |
$this->option_name(), |
| 89 |
wp_json_encode( array_merge( $prev_options, $options ) ), |
| 90 |
false |
| 91 |
); |
| 92 |
} |
| 93 |
|
| 94 |
public function before_assets() { |
| 95 |
} |
| 96 |
|
| 97 |
public function option_name() { |
| 98 |
return self::PREFIX . $this->slug(); |
| 99 |
} |
| 100 |
|
| 101 |
public function get_success_response_data() { |
| 102 |
return array( |
| 103 |
'message' => __( 'Saved successfully!', 'jet-form-builder' ), |
| 104 |
); |
| 105 |
} |
| 106 |
|
| 107 |
public function get_failed_response_data() { |
| 108 |
return array( |
| 109 |
'message' => __( 'Unsuccessful save.', 'jet-form-builder' ), |
| 110 |
); |
| 111 |
} |
| 112 |
|
| 113 |
public function send_response( $result ) { |
| 114 |
if ( $result ) { |
| 115 |
wp_send_json_success( $this->get_success_response_data() ); |
| 116 |
} |
| 117 |
|
| 118 |
wp_send_json_error( $this->get_failed_response_data() ); |
| 119 |
} |
| 120 |
|
| 121 |
public function rep_item_id() { |
| 122 |
return $this->slug(); |
| 123 |
} |
| 124 |
|
| 125 |
} |
| 126 |
|