forked from sailthru/sailthru-wordpress-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.php
More file actions
320 lines (263 loc) · 9.45 KB
/
Copy pathplugin.php
File metadata and controls
320 lines (263 loc) · 9.45 KB
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
<?php
/**
* @wordpress-plugin
* Plugin Name: Sailthru for WordPress (HTV)
* Plugin URI: http://sailthru.com/
* Description: Add the power of Sailthru to your WordPress set up.
* Version: 3.3.0
* Author: Sailthru
* Author URI: http://sailthru.com
* Author Email: integrations@sailthru.com
* 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 string $version The current version of the plugin.
*/
if ( ! defined( 'SAILTHRU_PLUGIN_VERSION' ) ) {
define( 'SAILTHRU_PLUGIN_VERSION', '3.3.0' );
}
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-concierge.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_Horizon', 'activate' ] );
register_deactivation_hook( __FILE__, [ 'Sailthru_Horizon', 'deactivate' ] );
register_uninstall_hook( __FILE__, [ 'Sailthru_Horizon', 'uninstall' ] );
// 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',
[
'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' => sanitize_text_field( $_SERVER['HTTP_USER_AGENT'] ),
'key' => 'email',
'ip' => sanitize_text_field( $_SERVER['SERVER_ADDR'] ),
'site' => 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;
}
}
}
/**
* Capture the saving of a post and make a Content API call to add
* the page details and tags to Sailthru's Horizon API for recommendations
*
* @param integer $post_id
*/
function sailthru_save_post( $post_id, $post, $post_before ) {
// Check to see if Content API is disabled
if ( false === apply_filters( 'sailthru_content_api_enable', true ) ) {
return;
}
if ( 'publish' === $post->post_status ) {
// Make sure Sailthru is setup
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 );
try {
if ( $client ) {
$site_metadata = get_option( 'vl_metadata' );
$data = [];
// Prepare the Content API Params
$data['url'] = get_permalink( $post->ID );
$data['title'] = $post->post_title;
$data['author'] = get_the_author_meta( 'display_name', $post->post_author );
$data['date'] = $post->post_date;
$data['vars']['post_type'] = $post->post_type;
$data['vars']['postID'] = $post->ID;
$data['vars']['siteID'] = $site_metadata['site_id'];
$data['spider'] = 1;
$data['description'] = ! empty( $post->post_excerpt ) ? $post->post_excerpt : $post->post_content;
// image & thumbnail
if ( has_post_thumbnail( $post->ID ) ) {
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
$thumb = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'concierge-thumb' );
$post_image = $image[0];
$data['images']['full']['url'] = esc_attr( $post_image );
$post_thumbnail = $thumb[0];
$data['images']['thumb']['url'] = $post_thumbnail;
}
// Use categories as tags
$post_categories = get_the_category( $post->ID );
$post_tags = wp_list_pluck( $post_categories, 'name' );
// Add site id to the tags
$post_tags[] = $data['vars']['siteID'];
$data['tags'] = join( ', ', $post_tags );
$post_expiration = get_post_meta( $post->ID, 'sailthru_post_expiration', true );
if ( ! empty( $post_expiration ) ) {
$data['expire_date'] = esc_attr( $post_expiration );
} else {
// set the expiry date in the future as you can't unset the value via the API
$data['expire_date'] = date( 'Y-m-d', strtotime( '+5 years' ) );
}
// get all the custom fields and add them to the vars
$custom_fields = get_post_custom( $post_id );
// exclude tags
$exclude_fields = [ '_edit_lock', '_edit_last', '_encloseme', 'sailthru_post_expiration' ];
foreach ( $custom_fields as $key => $val ) {
if ( ! in_array( $key, $exclude_fields, true ) ) {
if ( is_array( $val ) ) {
$data['vars'][ $key ] = implode( ',', $val );
} else {
$data['vars'][ $key ] = $val;
}
}
}
// Make the API call to Sailthru
$api = $client->apiPost( 'content', $data );
}
} catch ( Sailthru_Client_Exception $e ) {
//silently fail
return;
}
}
}
}
add_action( 'save_post', 'sailthru_save_post', 10, 3 );
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 );
}
}
}
}