| 1 |
<?php |
| 2 |
|
| 3 |
namespace WeDevs\ERP; |
| 4 |
|
| 5 |
use WeDevs\ERP\Settings\Template as Settings_Page; |
| 6 |
|
| 7 |
/** |
| 8 |
* Integration Class |
| 9 |
*/ |
| 10 |
class Integration extends Settings_Page { |
| 11 |
|
| 12 |
/** |
| 13 |
* Integration instances. |
| 14 |
*/ |
| 15 |
public $integrations; |
| 16 |
|
| 17 |
/** |
| 18 |
* Integration ID. |
| 19 |
* |
| 20 |
* @var string |
| 21 |
*/ |
| 22 |
public $id; |
| 23 |
|
| 24 |
/** |
| 25 |
* Integration title. |
| 26 |
* |
| 27 |
* @var string |
| 28 |
*/ |
| 29 |
public $title; |
| 30 |
/** |
| 31 |
* Integration description. |
| 32 |
* |
| 33 |
* @var string |
| 34 |
*/ |
| 35 |
public $description; |
| 36 |
|
| 37 |
/** |
| 38 |
* Form option fields. |
| 39 |
* |
| 40 |
* @var array |
| 41 |
*/ |
| 42 |
public $form_fields = []; |
| 43 |
|
| 44 |
/** |
| 45 |
* Extra settings. |
| 46 |
* |
| 47 |
* @var array |
| 48 |
*/ |
| 49 |
public $extra = []; |
| 50 |
|
| 51 |
/** |
| 52 |
* sub tab |
| 53 |
*/ |
| 54 |
public $sub_tab = []; |
| 55 |
/** |
| 56 |
* Initializes the WeDevs_ERP() class |
| 57 |
* |
| 58 |
* Checks for an existing WeDevs_ERP() instance |
| 59 |
* and if it doesn't find one, creates it. |
| 60 |
*/ |
| 61 |
public static function init() { |
| 62 |
static $instance = false; |
| 63 |
|
| 64 |
if ( ! $instance ) { |
| 65 |
$instance = new self(); |
| 66 |
} |
| 67 |
|
| 68 |
return $instance; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Class constructor. |
| 73 |
*/ |
| 74 |
public function __construct() { |
| 75 |
// Let 3rd parties unhook the above via this hook |
| 76 |
do_action( 'erp_integration', $this ); |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Initialize integrations. |
| 81 |
* |
| 82 |
* @return array |
| 83 |
*/ |
| 84 |
public function init_integrations() { |
| 85 |
$this->integrations = apply_filters( 'erp_integration_classes', $this->integrations ); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Return the integration classes - used in admin to load settings. |
| 90 |
* |
| 91 |
* @return array |
| 92 |
*/ |
| 93 |
public function get_integrations() { |
| 94 |
return $this->integrations; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Get an registered integration instance |
| 99 |
* |
| 100 |
* @param string $class_name |
| 101 |
* |
| 102 |
* @return \Integration|false |
| 103 |
*/ |
| 104 |
public function get_integration( $class_name ) { |
| 105 |
if ( $this->integrations && array_key_exists( $class_name, $this->integrations ) ) { |
| 106 |
return $this->integrations[ $class_name ]; |
| 107 |
} |
| 108 |
|
| 109 |
return false; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Get saved option id |
| 114 |
* |
| 115 |
* @return string |
| 116 |
*/ |
| 117 |
public function get_option_id() { |
| 118 |
return 'erp_integration_settings_' . $this->id; |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Get the form fields after they are initialized. |
| 123 |
* |
| 124 |
* @return array of options |
| 125 |
*/ |
| 126 |
public function get_form_fields() { |
| 127 |
return apply_filters( 'erp_settings_integration_form_fields_' . $this->id, $this->form_fields ); |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Generate settings html. |
| 132 |
* |
| 133 |
* @return void |
| 134 |
*/ |
| 135 |
public function generate_settings_html() { |
| 136 |
$settings = $this->get_form_fields(); |
| 137 |
$this->output_fields( $settings ); |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Get integration setting by key |
| 142 |
* |
| 143 |
* @param string $option |
| 144 |
* @param string $default |
| 145 |
* |
| 146 |
* @return string |
| 147 |
*/ |
| 148 |
public function get_setting( $option, $default = '' ) { |
| 149 |
$settings = get_option( 'erp_settings_erp-integration', [] ); |
| 150 |
|
| 151 |
if ( array_key_exists( $option, $settings ) ) { |
| 152 |
return $settings[ $option ]; |
| 153 |
} |
| 154 |
|
| 155 |
return $default; |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Get the admin options of this integration. |
| 160 |
* |
| 161 |
* @return void |
| 162 |
*/ |
| 163 |
public function admin_options() { |
| 164 |
?> |
| 165 |
<h3 style="padding-top: 10px;"><?php echo esc_html( $this->get_title() ); ?></h3> |
| 166 |
<?php |
| 167 |
echo wp_kses_post( wpautop( $this->get_description() ) ); |
| 168 |
|
| 169 |
/** |
| 170 |
* erp_email_settings_before action hook. |
| 171 |
* |
| 172 |
* @param object WeDevs\ERP\Integration |
| 173 |
*/ |
| 174 |
do_action( 'erp_integration_settings_before', $this ); |
| 175 |
?> |
| 176 |
|
| 177 |
<table class="form-table"> |
| 178 |
<?php $this->generate_settings_html(); ?> |
| 179 |
</table> |
| 180 |
|
| 181 |
<?php |
| 182 |
/** |
| 183 |
* erp_integration_settings_after action hook. |
| 184 |
* |
| 185 |
* @param object WeDevs\ERP\Integration |
| 186 |
*/ |
| 187 |
do_action( 'erp_integration_settings_after', $this ); ?> |
| 188 |
<?php |
| 189 |
} |
| 190 |
} |
| 191 |
|