active-campaign-handler.php
4 years ago
base-handler.php
4 years ago
captcha-handler.php
4 years ago
get-response-handler.php
4 years ago
mailchimp-handler.php
4 years ago
payments-gateways-handler.php
4 years ago
paypal-handler.php
4 years ago
tab-handler-manager.php
4 years ago
tab-handler-manager.php
133 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace Jet_Form_Builder\Admin\Tabs_Handlers; |
| 5 | |
| 6 | use Jet_Form_Builder\Plugin; |
| 7 | |
| 8 | class Tab_Handler_Manager { |
| 9 | |
| 10 | public static $instance; |
| 11 | private $_tabs = array(); |
| 12 | private $_tabs_options = array(); |
| 13 | |
| 14 | public static function instance() { |
| 15 | if ( is_null( self::$instance ) ) { |
| 16 | self::$instance = new self(); |
| 17 | } |
| 18 | |
| 19 | return self::$instance; |
| 20 | } |
| 21 | |
| 22 | public function __construct() { |
| 23 | $this->register_tabs(); |
| 24 | } |
| 25 | |
| 26 | public function register_tabs() { |
| 27 | $tabs = apply_filters( |
| 28 | 'jet-form-builder/register-tabs-handlers', |
| 29 | array( |
| 30 | new Captcha_Handler(), |
| 31 | new Mailchimp_Handler(), |
| 32 | new Active_Campaign_Handler(), |
| 33 | new Get_Response_Handler(), |
| 34 | new Paypal_Handler(), |
| 35 | new Payments_Gateways_Handler(), |
| 36 | ) |
| 37 | ); |
| 38 | |
| 39 | foreach ( $tabs as $tab ) { |
| 40 | if ( $tab instanceof Base_Handler ) { |
| 41 | $this->register_tab( $tab ); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | foreach ( $this->_tabs as $tab ) { |
| 46 | $this->register_hooks_for_tab( $tab ); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * @param Base_Handler $tab |
| 52 | */ |
| 53 | public function register_tab( Base_Handler $tab ) { |
| 54 | $default_options = $tab->save_global_default(); |
| 55 | |
| 56 | if ( ! empty( $default_options ) && is_array( $default_options ) ) { |
| 57 | $this->save_options_tab( $tab->slug(), $tab->get_options( $default_options ) ); |
| 58 | } |
| 59 | $this->_tabs[ $tab->slug() ] = $tab; |
| 60 | } |
| 61 | |
| 62 | private function register_hooks_for_tab( Base_Handler $tab ) { |
| 63 | add_action( "wp_ajax_jet_fb_save_tab__{$tab->slug()}", array( $tab, 'on_get_request' ) ); |
| 64 | |
| 65 | add_filter( |
| 66 | 'jet-form-builder/page-config/jfb-settings', |
| 67 | function ( $page_config ) use ( $tab ) { |
| 68 | $page_config[ $tab->slug() ] = apply_filters( |
| 69 | "jet-form-builder/tab-config/{$tab->slug()}", |
| 70 | $tab->on_load() |
| 71 | ); |
| 72 | |
| 73 | return $page_config; |
| 74 | } |
| 75 | ); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * @param $slug |
| 80 | * |
| 81 | * @return Base_Handler |
| 82 | */ |
| 83 | public function tab( $slug ) { |
| 84 | $this->isset_tab( $slug ); |
| 85 | |
| 86 | return $this->_tabs[ $slug ]; |
| 87 | } |
| 88 | |
| 89 | public function options( $slug, $default = array() ) { |
| 90 | $this->isset_tab( $slug ); |
| 91 | |
| 92 | return $this->_tabs[ $slug ]->get_options( $default ); |
| 93 | } |
| 94 | |
| 95 | public function isset_tab( $slug ) { |
| 96 | if ( ! isset( $this->_tabs[ $slug ] ) ) { |
| 97 | _doing_it_wrong( |
| 98 | __METHOD__, |
| 99 | esc_html( "Undefined tab: $slug" ), |
| 100 | esc_html( Plugin::instance()->get_version() ) |
| 101 | ); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | public function all( $default_tabs = array() ) { |
| 106 | $response = array(); |
| 107 | |
| 108 | foreach ( $this->_tabs as $slug => $tab ) { |
| 109 | $default = array(); |
| 110 | if ( isset( $default_tabs[ $slug ] ) ) { |
| 111 | $default = $default_tabs[ $slug ]; |
| 112 | } |
| 113 | |
| 114 | $response[ $slug ] = $tab->get_options( $default ); |
| 115 | } |
| 116 | |
| 117 | return $response; |
| 118 | } |
| 119 | |
| 120 | public function save_options_tab( $slug, $options ) { |
| 121 | if ( isset( $this->_tabs_options[ $slug ] ) && $this->_tabs_options[ $slug ] ) { |
| 122 | return; |
| 123 | } |
| 124 | $this->_tabs_options[ $slug ] = $options; |
| 125 | } |
| 126 | |
| 127 | public function get_options_tab( $slug ) { |
| 128 | return $this->_tabs_options[ $slug ]; |
| 129 | } |
| 130 | |
| 131 | |
| 132 | } |
| 133 |