forked from sailthru/sailthru-wordpress-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sailthru.php
228 lines (184 loc) · 6.27 KB
/
sailthru.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
<?php
/**
* @package Sailthru
* @author Jon Tascher
*/
/*
Plugin Name: Sailthru
Plugin URI: http://www.sailthru.com
Description: Intergrate Sailthru API functionality into your WordPress blog.
Author: Jon Tascher
Version: 2.1
Author URI: http://www.jontascher.com
*/
ob_start();
if(!class_exists('Sailthru')) {
require_once('sailthru_form.php');
class Sailthru {
function Sailthru() {
$this->php4_json();
register_activation_hook(__FILE__, array(&$this, 'activate'));
//actions
add_action('admin_menu', array(&$this, 'create_options_menu'));
add_action('admin_print_scripts-settings_page_sailthru', array(&$this, 'admin_header'));
add_action('wp_print_scripts', array(&$this, 'public_header'));
add_action('wp_ajax_sailthru_save_datafeed', array(&$this, 'save_datafeed'));
add_action('wp_ajax_sailthru_delete_datafeed', array(&$this, 'delete_datafeed'));
add_action('wp_ajax_sailthru_get_datafeed_html', array(&$this, 'get_datafeed_html'));
add_action('wp_ajax_sailthru_get_template_info', array(&$this, 'get_template_info'));
//filters
add_filter('the_content', array(&$this, 'content_filter'));
add_filter('get_footer', array(&$this, 'footer_filter'));
add_filter('widget_text', array(&$this, 'content_filter'));
$sailthru_forms = sailthru_form::get_forms();
}
function public_header() {
wp_enqueue_script('sailthru', $this->get_plugin_url() . '/js/sailthru_public.js', array('jquery'));
}
function footer_filter() {
if ((is_single() /* || is_page() */) && $subdomain = get_option('horizon_subdomain')) {
global $post;
$tags = get_the_tags($post->ID);
$t = array();
if ($tags) {
foreach ($tags as $tag) {
$t[] = $tag->name;
}
}
$tags = json_encode($t);
echo <<<EOT
<!-- Sailthru Horizon -->
<script type="text/javascript">sailthru_horizon = { domain: '{$subdomain}', tag: {$tags}}</script>
<script type="text/javascript" src="//ak.sail-horizon.com/horizon/v1.js"></script>
EOT;
}
}
function content_filter($content) {
//check to see if there are any sailthru tags in the post
if(preg_match('/\[sailthru ([\d]+)\]/i', $content, $matches)) {
$sailthru_forms = sailthru_form::get_forms();
$form_id = $matches[1];
if(isset($sailthru_forms[$form_id])) {
try {
$form = new sailthru_form($form_id);
}
catch(Exception $e) {
}
}
if(is_object($form)) {
return preg_replace('/\[sailthru ([\d]+)\]/i', $form->get_form(), $content);
}
}
return $content;
}
function activate() {
update_option('sailthru_id', 1);
// update_option('sailthru_forms', array());
}
function admin_header() {
wp_enqueue_script('sailthru', $this->get_plugin_url() . '/js/sailthru_admin.js', array('jquery'));
}
function get_plugin_url() {
return plugins_url(plugin_basename(dirname(__FILE__)));
}
function get_plugin_path() {
return preg_replace('|^http://[^/].*?/|i', '/', plugins_url(plugin_basename(dirname(__FILE__))));
}
function create_options_menu() {
add_options_page('Sailthru Options', 'Sailthru', 'edit_pages', 'sailthru', array(&$this, 'options_page'));
}
function options_page() {
include('sailthru_options.php');
}
function get_datafeed_html() {
require_once('client/requires.php');
$client = new Sailthru_Client(get_option('sailthru_api_key'), get_option('sailthru_secret'));
echo json_encode($client->sailthru_eval($_POST['datafeed'], $_POST['template']));
die();
}
function delete_datafeed() {
$datafeeds = get_option('sailthru_datafeeds');
if (isset($_POST['name']) && $_POST['name'] && isset($datafeeds[$_POST['name']])) {
unset($datafeeds[$_POST['name']]);
}
update_option('sailthru_datafeeds', $datafeeds);
echo json_encode($datafeeds);
die();
}
function save_datafeed() {
$datafeeds = get_option('sailthru_datafeeds');
$datafeeds[$_POST['name']] = $_POST['url'];
if (isset($_POST['del']) && $_POST['del'] && isset($datafeeds[$_POST['del']])) {
unset($datafeeds[$_POST['del']]);
}
update_option('sailthru_datafeeds', $datafeeds);
echo json_encode($datafeeds);
die();
}
function get_template_info() {
require_once('client/requires.php');
$client = new Sailthru_Client(get_option('sailthru_api_key'), get_option('sailthru_secret'));
$res = $client->getTemplate($_POST['template']);
echo json_encode($res);
die();
}
function get_template_list() {
require_once('client/requires.php');
$client = new Sailthru_Client(get_option('sailthru_api_key'), get_option('sailthru_secret'));
$res = $client->getTemplates();
if (isset($res['templates']) && $res['templates']) {
$return = array();
foreach ($res['templates'] as $template) {
$return[] = $template['name'];
}
return $return;
} else {
return array();
}
}
function get_sailthru_form($form_id) {
$sailthru_forms = sailthru_form::get_forms();
if(isset($sailthru_forms[$form_id])) {
$form = new sailthru_form($form_id);
return $form->get_form();
}
return '';
}
function php4_json() {
if(!function_exists('json_encode')) {
require_once('json.php');
function json_encode($data) {
$json = new Services_JSON();
return($json->encode($data));
}
}
if(!function_exists('json_decode')) {
require_once('json.php');
function json_decode($data) {
$json = new Services_JSON();
return($json->decode($data));
}
}
}
}
}
if(class_exists('Sailthru')) {
$sailthru = new Sailthru();
}
if(get_option('sailthru_all_email') && !function_exists('wp_mail')) {
function wp_mail($to, $subject, $message, $headers = '', $attachments = array()) {
extract(apply_filters('wp_mail', compact('to', 'subject', 'message', 'headers', 'attachments')));
//can sailthru handle attachments? I don't think it can at the moment..
if (!is_array($attachments)) {
$attachments = explode("\n", $attachments);
}
$replacements = array(
'subject' => $subject,
'body' => $message
);
require_once('client/requires.php');
$client = new Sailthru_Client(get_option('sailthru_api_key'), get_option('sailthru_secret'));
$r = $client->send('WordPress Template', $to, $replacements, array());
return true;
}
}