-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiwp-waas-integration.php
270 lines (233 loc) · 8.66 KB
/
iwp-waas-integration.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
<?php // @codingStandardsIgnoreLine
/**
* Plugin Name: InstaWP WaaS Integration
* Description: Integration with WooCommerce & SureCart for InstaWP WaaS Feature
* Version: 1.0.4
* Author: InstaWP
* Author URI: https://instawp.com
* License: GPL-2.0-or-later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: iwp-waas-integration
*
* @package iwp-waas-integration
* @author Sayan Datta
*/
defined( 'ABSPATH' ) || exit;
/**
* Abort if the class is already exists.
*/
if ( ! class_exists( 'InstaWP_WaaS_Integration' ) ) {
/**
* InstaWP WaaS class.
*
* @class Main class of the plugin.
*/
class InstaWP_WaaS_Integration {
/**
* Plugin version.
*
* @var string
*/
public $version = '1.0.4';
/**
* Settings Group.
*
* @var string
*/
public $settings_group = 'iwp_waas_integration';
/**
* @var WordPressSettingsFramework
*/
private $wpsf;
/**
* Instance of this class.
*
* @since 1.0.0
* @var object
*/
protected static $instance;
/**
* Get the singleton instance of this class.
*
* @since 1.0.0
* @return InstaWP_WaaS_Integration
*/
public static function get() {
if ( ! ( self::$instance instanceof self ) ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Constructor
*
* @since 1.0.0
*/
public function __construct() {
$this->define_constants();
require_once INSTAWP_WAAS_WC_PATH . 'plugin-update-checker/plugin-update-checker.php';
require_once INSTAWP_WAAS_WC_PATH . 'settings/wp-settings-framework.php';
add_filter( 'wpsf_register_settings_' . $this->settings_group, [ $this, 'settings' ] );
$this->wpsf = new WordPressSettingsFramework( null, $this->settings_group );
$updateChecker = \YahnisElsts\PluginUpdateChecker\v5\PucFactory::buildUpdateChecker( 'https://github.com/InstaWP/iwp-waas-integration', INSTAWP_WAAS_WC_FILE, 'iwp-waas-integration' );
$updateChecker->setBranch( 'main' );
add_action( 'admin_menu', [ $this, 'register_menu' ], 20 );
add_action( 'plugins_loaded', [ $this, 'register_classes' ], 11 );
add_action( 'woocommerce_integrations_init', [ $this, 'wc_integration_class' ] );
add_filter( 'woocommerce_integrations', [ $this, 'wc_integrations' ], 999 );
add_filter( 'woocommerce_email_classes', [ $this, 'wc_email_classes' ], 999 );
}
/**
* Define the plugin constants.
*/
private function define_constants() {
define( 'INSTAWP_WAAS_WC_VERSION', $this->version );
define( 'INSTAWP_WAAS_WC_FILE', __FILE__ );
define( 'INSTAWP_WAAS_WC_PATH', dirname( INSTAWP_WAAS_WC_FILE ) . '/' );
define( 'INSTAWP_WAAS_WC_URL', plugins_url( '', INSTAWP_WAAS_WC_FILE ) . '/' );
define( 'INSTAWP_WAAS_API_DOMAIN', 'https://app.instawp.io' );
}
public function register_classes() {
if ( class_exists( 'SureCart' ) ) {
require_once INSTAWP_WAAS_WC_PATH . 'classes/surecart/class-instawp-waas-surecart-integration.php';
}
if ( function_exists( 'EDD' ) ) {
require_once INSTAWP_WAAS_WC_PATH . 'classes/easy-digital-downloads/class-instawp-edd-integration.php';
new InstaWP_WaaS_EDD_Integration();
}
}
/**
* WooCommerce email loader
*
* @param array $emails emails.
* @return array
*/
public function wc_integrations( $integrations ) {
$integrations[] = 'InstaWP_WaaS_WC_Integration';
return $integrations;
}
public function wc_integration_class() {
require_once INSTAWP_WAAS_WC_PATH . 'classes/woocommerce/class-instawp-waas-wc-integration.php';
}
/**
* WooCommerce email loader
*
* @param array $emails emails.
* @return array
*/
public function wc_email_classes( $emails ) {
$emails['InstaWP_WaaS_WC_Email'] = include_once INSTAWP_WAAS_WC_PATH . 'classes/woocommerce/class-instawp-waas-wc-email.php';
return $emails;
}
public function register_menu() {
if ( class_exists( 'SureCart' ) ) {
$this->wpsf->add_settings_page( [
'parent_slug' => 'sc-dashboard',
'page_title' => __( 'InstaWP WaaS SureCart Integration', 'iwp-waas-integration' ),
'menu_title' => __( 'InstaWP WaaS', 'iwp-waas-integration' ),
'capability' => 'manage_sc_shop_settings',
] );
}
}
public function settings( $settings ) {
$settings['tabs'] = [
[
'id' => 'settings_tab',
'title' => __( 'Settings', 'iwp-waas-integration' ),
],
[
'id' => 'email_tab',
'title' => __( 'Email', 'iwp-waas-integration' ),
],
];
$settings['sections'] = [
[
'tab_id' => 'settings_tab',
'section_id' => 'settings',
'section_title' => 'Configure',
'section_order' => 10,
'fields' => [
[
'id' => 'api_key',
'title' => __( 'API Key', 'iwp-waas-integration' ),
'desc' => __( 'Enter InstaWP API Key here.', 'iwp-waas-integration' ),
'type' => 'text',
],
[
'id' => 'app_email',
'title' => __( 'Send email through App', 'iwp-waas-integration' ),
'type' => 'toggle',
'default' => false,
],
]
],
];
if ( ! wpsf_get_setting( $this->settings_group, 'settings_tab_settings', 'app_email' ) ) {
$settings['sections'][] = [
'tab_id' => 'email_tab',
'section_id' => 'email',
'section_title' => 'Email Settings',
'section_order' => 10,
'fields' => [
[
'id' => 'subject',
'title' => __( 'Email Subject', 'iwp-waas-integration' ),
'type' => 'text',
'default' => 'Your InstaWP WaaS link',
],
[
'id' => 'body',
'title' => __( 'Email Body', 'iwp-waas-integration' ),
'type' => 'editor',
'default' => 'Link to build your website is {{link}}',
],
],
];
}
return $settings;
}
public function get_field_value( $id, $field, $api_key ) {
$items = $this->fetch_waas_list( $api_key );
return $items[ $id ][ $field ] ?? '';
}
public function fetch_waas_list( $api_key ) {
$response = wp_remote_get( INSTAWP_WAAS_API_DOMAIN . '/api/v2/waas', [
'sslverify' => false,
'headers' => [
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' . $api_key
]
] );
if ( is_wp_error( $response ) ) {
$error_message = $response->get_error_message();
error_log("error is: ". $error_message);
return [];
}
$body = wp_remote_retrieve_body( $response );
$data = json_decode( $body, true );
if ( ! $data['status'] ) {
return [];
}
$options = [];
foreach ( $data['data'] as $item ) {
$options[ $item['id'] ] = $item;
}
return $options;
}
}
}
/**
* Check the existance of the function.
*/
if ( ! function_exists( 'instawp_waas' ) ) {
/**
* Returns the main instance of InstaWP_WaaS_Integration to prevent the need to use globals.
*
* @return InstaWP_WaaS_Integration
*/
function instawp_waas() {
return InstaWP_WaaS_Integration::get();
}
// Start it.
instawp_waas();
}