-
Notifications
You must be signed in to change notification settings - Fork 23
/
plugin.php
326 lines (258 loc) · 8.69 KB
/
plugin.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
<?php
/**
Plugin Name: Sailthru for WordPress
Plugin URI: http://sailthru.com/
Description: Add the power of Sailthru to your WordPress set up.
Version: 4.3.8
Requires at least: 5.5
Author: Sailthru
Author URI: http://sailthru.com
Author Email: [email protected]
License:
Copyright 2013 (Sailthru)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
**/
/**
* The current version of the plugin.
*
* @since 3.0.6
* @var const $version The current version of the plugin.
*/
if ( ! defined( 'SAILTHRU_PLUGIN_VERSION' ) ) {
define( 'SAILTHRU_PLUGIN_VERSION', '4.3.8' );
}
if ( ! defined( 'SAILTHRU_PLUGIN_PATH' ) ) {
define( 'SAILTHRU_PLUGIN_PATH', plugin_dir_path( __FILE__ ) );
}
if ( ! defined( 'SAILTHRU_PLUGIN_URL' ) ) {
define( 'SAILTHRU_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
}
/*
* Sailthru PHP5 Developer Library.
* Source: http://getstarted.sailthru.com/developers/client-libraries/set-config-file/php5.
*/
require_once SAILTHRU_PLUGIN_PATH . 'lib/Sailthru_Util.php';
require_once SAILTHRU_PLUGIN_PATH . 'lib/Sailthru_Client.php';
require_once SAILTHRU_PLUGIN_PATH . 'lib/Sailthru_Client_Exception.php';
require_once SAILTHRU_PLUGIN_PATH . 'classes/class-wp-sailthru-client.php';
/*
* Get Sailthru for WordPress plugin classes
*/
require_once SAILTHRU_PLUGIN_PATH . 'classes/class-sailthru-horizon.php';
require_once SAILTHRU_PLUGIN_PATH . 'classes/class-sailthru-content.php';
require_once SAILTHRU_PLUGIN_PATH . 'classes/class-sailthru-meta-box.php';
require_once SAILTHRU_PLUGIN_PATH . 'classes/class-sailthru-scout.php';
require_once SAILTHRU_PLUGIN_PATH . 'classes/class-sailthru-mailer.php';
/*
* Get Sailthru Custom Subscribe Fields classes
*/
require_once SAILTHRU_PLUGIN_PATH . 'classes/class-sailthru-subscribe-fields.php';
/*
* Sailthru for WordPress admin view settings and registrations.
*/
require_once SAILTHRU_PLUGIN_PATH . 'views/admin.functions.php';
/*
* Grab and activate the Sailthru Subscribe widget.
*/
require_once SAILTHRU_PLUGIN_PATH . 'widget.subscribe.php';
/*
* Add the email handler
*/
require_once SAILTHRU_PLUGIN_PATH . 'sailthru_mail.php';
/*
* Horizon handles the foundational actions like adding menus, meta tags,
* and javascript files.
*/
if ( class_exists( 'Sailthru_Horizon' ) ) {
$sailthru_horizon = new Sailthru_Horizon();
// add a record in the db to keep track of the version of this plugin
if ( false === get_option( 'sailthru_plugin_version' ) ) {
add_option( 'sailthru_plugin_version', SAILTHRU_PLUGIN_VERSION );
} else {
update_option( 'sailthru_plugin_version', SAILTHRU_PLUGIN_VERSION );
} // end if
if ( class_exists( 'Sailthru_Scout' ) ) {
$sailthru_scout = new Sailthru_Scout();
}
}
/**
* Register hooks that are fired when the plugin is activated,
* deactivated, and uninstalled, respectively.
*/
register_activation_hook( __FILE__, 'sailthru_activate' );
register_deactivation_hook( __FILE__, 'sailthru_deactivate' );
register_uninstall_hook( __FILE__, 'sailthru_uninstall' );
/**
* Fired when the plugin is activated.
*
* @param boolean $network_wide True if WPMU superadmin
* uses "Network Activate" action, false if WPMU is
* disabled or plugin is activated on an individual blog
*/
function sailthru_activate( $network_wide ) {
if ( ! current_user_can( 'activate_plugins' ) ) {
return;
}
if ( false === get_option( 'sailthru_content_settings' ) ) {
$defaults = ['sailthru_content_api_status' => "true",
'sailthru_content_post_types' => ['post']
];
update_option( 'sailthru_content_settings', $defaults );
}
// signal that it's ok to override WordPress's built-in email functions
if ( false === get_option( 'sailthru_override_wp_mail' ) ) {
add_option( 'sailthru_override_wp_mail', 1 );
} // end if
if ( true === get_option( 'sailthru_setup_complete' ) || true === get_option( 'sailthru_api_validated' ) ) {
update_option( 'sailthru_api_validated', true );
update_option( 'sailthru_setup_complete', true );
} else {
update_option( 'sailthru_api_validated', false );
update_option( 'sailthru_setup_complete', false );
}
} // end activate
/**
* Fired when the plugin is deactivated.
*
* @param boolean $network_wide True if WPMU superadmin
* uses "Network Activate" action, false if WPMU is
* disabled or plugin is activated on an individual blog
*/
function sailthru_deactivate( $network_wide ) {
if ( ! current_user_can( 'deactivate_plugins' ) ) {
return;
}
remove_options();
} // end deactivate
/**
* Fired when the plugin is uninstalled.
*
* @param boolean $network_wide True if WPMU superadmin
* uses "Network Activate" action, false if WPMU is
* disabled or plugin is activated on an individual blog
*/
function sailthru_uninstall( $network_wide ) {
remove_options();
}
function remove_options() {
$options = ['sailthru_override_wp_mail',
'sailthru_setup_complete',
'sailthru_setup_options',
'sailthru_concierge_options',
'sailthru_scout_options',
'sailthru_forms_options',
'sailthru_customfields_order_widget',
'sailthru_customfields_order',
'sailthru_forms_key',
'sailthru_integrations_options',
'sailthru_setup_complete',
'sailthru_api_validated',
'sailthru_content_settings',
'sailthru_plugin_version'
];
foreach ($options as $option) {
if ( false !== get_option( $option ) ) {
delete_option( $option );
}
}
}
// This is called from sailthru_setup_handler()
function sailthru_create_wordpress_template() {
$wordpress_template = 'WordPress Template';
if ( sailthru_verify_setup() ) {
$sailthru = get_option( 'sailthru_setup_options' );
$api_key = $sailthru['sailthru_api_key'];
$api_secret = $sailthru['sailthru_api_secret'];
$client = new WP_Sailthru_Client( $api_key, $api_secret );
// Find out if 'WordPress Template' already exists
$template_exists = false;
try {
if ( $client ) {
// if we try to grab a template by name that doesn't exist
// the world blows up. Grab them all and loop through
$response = $client->getTemplates();
$templates = $response['templates'];
foreach ( $templates as $template ) {
foreach ( $template as $key => $value ) {
if ( 'name' === $key ) {
if ( $value === $wordpress_template ) {
$template_exists = true;
}
}
}
}
}
} catch ( Sailthru_Client_Exception $e ) {
//silently fail
return;
}
// the Template doesn't exist, so we need to create it.
if ( false === $template_exists ) {
try {
if ( $client ) {
$client->saveTemplate(
'wordpress-template',
array(
'name' => $wordpress_template,
'subject' => '{subject}',
'content_html' => "<html>\n<head>\n<body>\n{body}\n</body>\n</html>",
)
);
}
} catch ( Sailthru_Client_Exception $e ) {
//silently fail
return;
}
}
}
}
// Add and action to handle when a user logs in.
add_action( 'wp_login', 'sailthru_user_login', 10, 2 );
function sailthru_user_login( $user_login, $user ) {
if ( get_option( 'sailthru_setup_complete' ) ) {
$sailthru = get_option( 'sailthru_setup_options' );
$api_key = $sailthru['sailthru_api_key'];
$api_secret = $sailthru['sailthru_api_secret'];
$client = new WP_Sailthru_Client( $api_key, $api_secret );
$id = $user->user_email;
$options = [
'login' => [
'user_agent' => isset( $_SERVER['HTTP_USER_AGENT'] ) ? sanitize_text_field( $_SERVER['HTTP_USER_AGENT'] ) : '',
'key' => 'email',
'ip' => isset( $_SERVER['SERVER_ADDR'] ) ? sanitize_text_field( $_SERVER['SERVER_ADDR'] ) : '',
'site' => isset( $_SERVER['HTTP_HOST'] ) ? sanitize_text_field( $_SERVER['HTTP_HOST'] ) : '',
],
'fields' => [
'keys' => 1
],
];
try {
if ( $client ) {
$st = $client->saveUser( $id, $options );
}
} catch ( Sailthru_Client_Exception $e ) {
//silently fail.
return;
}
}
}
if ( ! function_exists( 'write_log' ) ) {
function write_log( $log ) {
if ( defined( 'WP_DEBUG' ) && true === WP_DEBUG ) {
if ( is_array( $log ) || is_object( $log ) ) {
error_log( print_r( $log, true ) );
} else {
error_log( $log );
}
}
}
}