/
llms-abstract-email-provider.php
394 lines (331 loc) · 8.79 KB
/
llms-abstract-email-provider.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
<?php
/**
* Base class used by email delivery provider "connector" classes
*
* @package LifterLMS/Abstracts/Classes
*
* @since 3.40.0
* @version 5.9.0
*/
defined( 'ABSPATH' ) || exit;
/**
* LLMS_Abstract_Email_Provider
*
* @since 3.40.0
* @since 6.0.0 Removed the deprecated `LLMS_Abstract_Email_Provider::output_css()` method.
*/
abstract class LLMS_Abstract_Email_Provider {
/**
* Connector's ID.
*
* @var string
*/
protected $id = '';
/**
* Array of supported providers.
*
* @var array
*/
protected $providers = array(
'mailhawk',
'sendwp',
);
/**
* Configures the response returned when `do_remote_install()` is successful.
*
* @since 3.40.0
*
* @return array
*/
abstract protected function do_remote_install_success();
/**
* Retrieve the settings area HTML for the connect button
*
* @since 3.40.0
*
* @return string
*/
abstract protected function get_connect_setting();
/**
* Retrieve description text to be used in the settings area.
*
* @since 3.40.0
*
* @return string
*/
abstract protected function get_description();
/**
* Retrieve the connector's name / title.
*
* @since 3.40.0
*
* @return string
*/
abstract protected function get_title();
/**
* Determines if connector plugin is connected for sending.
*
* @since 3.40.0
*
* @return boolean
*/
abstract protected function is_connected();
/**
* Determines if connector plugin is installed
*
* @since 3.40.0
*
* @return boolean
*/
abstract protected function is_installed();
/**
* Constructor.
*
* @since 3.40.0
*
* @return void
*/
public function __construct() {
/**
* Filter the available email providers
*
* @since 3.40.0
*
* @param string[] $this->providers List of email provider ids.
*/
$this->providers = apply_filters( 'llms_email_delivery_providers', $this->providers );
/**
* Dynamically adjust the priority.
*
* A "connected" provider will always load first, ensuring
* that it can disable the other providers.
*
* When no providers are connected, they'll all load at 10
* and display in alphabetical order as a result of the order
* the files are included.
*/
$priority = $this->is_connected() ? 5 : 10;
add_action( 'admin_init', array( $this, 'init' ), $priority );
}
/**
* Initialize the Connector
*
* @since 3.40.0
*
* @return void
*/
public function init() {
// Disable other email delivery services if the current connector is already connected.
if ( $this->is_connected() ) {
$this->disable_other_providers();
}
/**
* Disable the Connector class and settings
*
* The dynamic portion of this filter, `{$this->id}`, refers
* to the id of the email provider. See `$this->providers` for a list of supported providers.
*
* @since 3.40.0
*
* @param bool $disabled Whether or not this class is disabled.
*/
if ( apply_filters( "llms_disable_{$this->id}", false ) ) {
return;
}
add_filter( 'llms_email_delivery_services', array( $this, 'add_settings' ) );
add_action( 'wp_ajax_llms_' . $this->id . '_remote_install', array( $this, 'ajax_callback_remote_install' ) );
add_action( 'admin_print_footer_scripts', array( $this, 'output_js' ) );
}
/**
* Determines if the plugin is already installed and activates it if it is
*
* @since 3.40.0
*
* @return boolean|WP_Error `true` when plugin is installed and successfully activated.
* `WP_Error` when plugin is installed and there was an error activating it.
* `false` when plugin is not installed.
*/
protected function activate_already_installed_plugin() {
$is_plugin_installed = false;
foreach ( get_plugins() as $path => $details ) {
if ( false === strpos( $path, '/' . $this->id . '.php' ) ) {
continue;
}
$is_plugin_installed = true;
$activate = activate_plugin( $path );
if ( is_wp_error( $activate ) ) {
return $activate;
}
break;
}
return $is_plugin_installed;
}
/**
* Add Settings.
*
* @since 3.40.0
*
* @param array $settings Existing settings.
* @return array
*/
public function add_settings( $settings ) {
// Short circuit if missing authorization.
if ( ! current_user_can( 'install_plugins' ) ) {
return $settings;
}
$new_settings = array(
array(
'id' => $this->id . '_title',
'type' => 'subtitle',
'title' => $this->get_title(),
'desc' => $this->is_connected() ? '' : $this->get_description(),
),
array(
'id' => $this->id . '_connect',
'type' => 'custom-html',
'value' => $this->get_connect_setting(),
),
);
return array_merge( $settings, $new_settings );
}
/**
* Ajax callback for installing the connector's plugin.
*
* @since 3.40.0
*
* @return void
*/
public function ajax_callback_remote_install() {
$ret = $this->do_remote_install();
ob_clean();
wp_send_json( $ret, ! empty( $ret['status'] ) ? $ret['status'] : 200 );
}
/**
* Determines if the current user can perform the remote installation.
*
* @since 3.40.0
*
* @return true|array
*/
protected function can_remote_install() {
if ( ! llms_verify_nonce( '_llms_' . $this->id . '_nonce', 'llms-' . $this->id . '-install' ) ) {
return array(
'code' => 'llms_' . $this->id . '_install_nonce_failure',
'message' => esc_html__( 'Security check failed.', 'lifterlms' ),
'status' => 401,
);
} elseif ( ! current_user_can( 'install_plugins' ) ) {
return array(
'code' => 'llms_' . $this->id . '_install_unauthorized',
'message' => esc_html__( 'You do not have permission to perform this action.', 'lifterlms' ),
'status' => 403,
);
}
return true;
}
/**
* Automatically disables other providers when the current provider is connected.
*
* @since 3.40.0
*
* @return void.
*/
protected function disable_other_providers() {
$disable = array_diff( $this->providers, array( $this->id ) );
foreach ( $disable as $id ) {
add_filter( 'llms_disable_' . $id, '__return_true' );
}
}
/**
* Validate installation request and perform the plugin install or return errors.
*
* @since 3.40.0
*
* @return array
*/
protected function do_remote_install() {
$can_install = $this->can_remote_install();
if ( true !== $can_install ) {
return $can_install;
}
$install = $this->install();
if ( is_wp_error( $install ) ) {
return array(
'code' => $install->get_error_code(),
'message' => $install->get_error_message(),
'status' => 400,
);
}
return $this->do_remote_install_success();
}
/**
* Install the plugin via the WP plugin installer.
*
* @since 3.40.0
*
* @return boolean|WP_Error Error object or `true` when successful.
*/
protected function install() {
// Check if the plugin already exists and activate it if it is.
$ret = $this->activate_already_installed_plugin();
// Plugin doesn't exist, install it.
if ( false === $ret ) {
$ret = $this->install_plugin();
}
// Final check to ensure the connector is installed and activated.
if ( true === $ret && ! $this->is_installed() ) {
// Translators: %s = title of the email delivery plugin.
return new WP_Error( 'llms_' . $this->id . '_not_found', sprtinf( __( '%s plugin not found. Please try again.', 'lifterlms' ), $this->get_title() ), $install );
}
return $ret;
}
/**
* Install the plugin via the WP Plugin Repo.
*
* @since 3.40.0
*
* @return boolean|WP_Error `true` on success, error object otherwise.
*/
protected function install_plugin() {
include_once ABSPATH . 'wp-admin/includes/plugin-install.php';
include_once ABSPATH . 'wp-admin/includes/file.php';
include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
// Use the WordPress Plugins API to get the plugin download link.
$api = plugins_api(
'plugin_information',
array(
'slug' => $this->id,
)
);
if ( is_wp_error( $api ) ) {
return $api;
}
// Use the AJAX upgrader skin to quietly install the plugin.
$upgrader = new Plugin_Upgrader( new WP_Ajax_Upgrader_Skin() );
$install = $upgrader->install( $api->download_link );
if ( is_wp_error( $install ) ) {
return $install;
}
$activate = activate_plugin( $upgrader->plugin_info() );
if ( is_wp_error( $activate ) ) {
return $activate;
}
return true;
}
/**
* Determine if inline scripts and styles should be output.
*
* @since 3.40.0
* @since 5.9.0 Stop using deprecated `FILTER_SANITIZE_STRING`.
*
* @return bool
*/
protected function should_output_inline() {
// Short circuit if unauthorized.
if ( ! current_user_can( 'install_plugins' ) ) {
return false;
}
$screen = get_current_screen();
return ( 'lifterlms_page_llms-settings' === $screen->id && 'engagements' === llms_filter_input( INPUT_GET, 'tab' ) && ! $this->is_connected() );
}
}