| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* The Integration abstract class |
| 5 |
* |
| 6 |
* @since 1.1.0 |
| 7 |
*/ |
| 8 |
abstract class WeForms_Abstract_Integration { |
| 9 |
|
| 10 |
/** |
| 11 |
* The integration id |
| 12 |
* |
| 13 |
* @var bool |
| 14 |
*/ |
| 15 |
public $id; |
| 16 |
|
| 17 |
/** |
| 18 |
* If the integration is enabled |
| 19 |
* |
| 20 |
* @var bool |
| 21 |
*/ |
| 22 |
public $enabled; |
| 23 |
|
| 24 |
/** |
| 25 |
* Integration title |
| 26 |
* |
| 27 |
* @var string |
| 28 |
*/ |
| 29 |
public $title; |
| 30 |
|
| 31 |
/** |
| 32 |
* URL to the integration icon |
| 33 |
* |
| 34 |
* @var string |
| 35 |
*/ |
| 36 |
public $icon; |
| 37 |
|
| 38 |
/** |
| 39 |
* The settings fields for this integrations |
| 40 |
* |
| 41 |
* @var array |
| 42 |
*/ |
| 43 |
public $settings_fields = []; |
| 44 |
|
| 45 |
/** |
| 46 |
* The settings fields for this integrations |
| 47 |
* |
| 48 |
* @var array |
| 49 |
*/ |
| 50 |
protected $template = null; |
| 51 |
|
| 52 |
/** |
| 53 |
* The settings settings_template |
| 54 |
* |
| 55 |
* @var array |
| 56 |
*/ |
| 57 |
protected $settings_template = null; |
| 58 |
|
| 59 |
/** |
| 60 |
* Get the integration title |
| 61 |
* |
| 62 |
* @return string |
| 63 |
*/ |
| 64 |
public function get_title() { |
| 65 |
return apply_filters( 'weforms_integration_title', $this->title, $this ); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Get the integration id |
| 70 |
* |
| 71 |
* @return string |
| 72 |
*/ |
| 73 |
public function get_id() { |
| 74 |
return apply_filters( 'weforms_integration_title', $this->id, $this ); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Get intgration icon |
| 79 |
* |
| 80 |
* @return string |
| 81 |
*/ |
| 82 |
public function get_icon() { |
| 83 |
return apply_filters( 'weforms_integration_icon', $this->icon, $this ); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Check if the integration is enabled |
| 88 |
* |
| 89 |
* @return bool |
| 90 |
*/ |
| 91 |
public function is_enabled() { |
| 92 |
return $this->enabled == true; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Check if it's a pro field |
| 97 |
* |
| 98 |
* @return bool |
| 99 |
*/ |
| 100 |
public function is_pro() { |
| 101 |
return false; |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Get the settings fields |
| 106 |
* |
| 107 |
* @return array |
| 108 |
*/ |
| 109 |
public function get_settings_fields() { |
| 110 |
return $this->settings_fields; |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Get the integration settings for the component |
| 115 |
* |
| 116 |
* @return array |
| 117 |
*/ |
| 118 |
public function get_js_settings() { |
| 119 |
return [ |
| 120 |
'id' => $this->get_id(), |
| 121 |
'title' => $this->get_title(), |
| 122 |
'icon' => $this->get_icon(), |
| 123 |
'settings' => $this->get_settings_fields(), |
| 124 |
'pro' => $this->is_pro(), |
| 125 |
]; |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Check if it's the forms page |
| 130 |
* |
| 131 |
* @return bool |
| 132 |
*/ |
| 133 |
public function is_weforms_page() { |
| 134 |
if ( get_current_screen()->base != 'toplevel_page_weforms' ) { |
| 135 |
return false; |
| 136 |
} |
| 137 |
|
| 138 |
return true; |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Load the individual template file if exists |
| 143 |
* |
| 144 |
* @return void |
| 145 |
*/ |
| 146 |
public function load_template() { |
| 147 |
if ( !$this->is_weforms_page() ) { |
| 148 |
return; |
| 149 |
} |
| 150 |
|
| 151 |
if ( !$this->template ) { |
| 152 |
return; |
| 153 |
} |
| 154 |
|
| 155 |
echo '<script type="text/x-template" id="tmpl-wpuf-integration-' . esc_attr( $this->id ). '">'; |
| 156 |
include $this->template; |
| 157 |
echo '</script>'; |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Load settings |
| 162 |
* |
| 163 |
* @return void |
| 164 |
*/ |
| 165 |
public function load_settings( $priority = 10 ) { |
| 166 |
if ( !$this->settings_template ) { |
| 167 |
return; |
| 168 |
} |
| 169 |
|
| 170 |
add_action( 'weforms_settings_tabs', [ $this, 'settings_tabs' ], $priority ); |
| 171 |
add_action( 'weforms_settings_tab_content_' . $this->id, [ $this, 'settings_panel' ], $priority ); |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* Render the settings panel |
| 176 |
* |
| 177 |
* @return void |
| 178 |
*/ |
| 179 |
public function settings_panel() { |
| 180 |
if ( file_exists( $this->settings_template ) ) { |
| 181 |
include $this->settings_template; |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Add new tab at settings |
| 187 |
* |
| 188 |
* @return void |
| 189 |
*/ |
| 190 |
public function settings_tabs( $tabs ) { |
| 191 |
$tabs[ $this->id ] = [ |
| 192 |
'label' => $this->title, |
| 193 |
'icon' => $this->icon, |
| 194 |
]; |
| 195 |
|
| 196 |
return $tabs; |
| 197 |
} |
| 198 |
|
| 199 |
/** |
| 200 |
* Get file prefix |
| 201 |
* |
| 202 |
* @return string |
| 203 |
*/ |
| 204 |
public function get_prefix() { |
| 205 |
$prefix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min'; |
| 206 |
|
| 207 |
return $prefix; |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* Get default componenet url |
| 212 |
* |
| 213 |
* @return string |
| 214 |
*/ |
| 215 |
public function module_component_file( $plugin_file ) { |
| 216 |
$prefix = $this->get_prefix(); |
| 217 |
|
| 218 |
return plugins_url( 'component/index' . $prefix . '.js', $plugin_file ); |
| 219 |
} |
| 220 |
} |
| 221 |
|