forked from nebojsac/Campayn-WordPress-Plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcampayn.php
299 lines (255 loc) · 9.34 KB
/
campayn.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
<?php
/*
Plugin Name: Campayn
Plugin URI: https://github.com/nebojsac/Campayn-WordPress-Plugin
Description: Plugin for using the campayn.com API with Wordpress
Version: 0.191
Author: Zoltan Lengyel
Author URI: http://brow.hu/
License: GPL2
*/
include('pest/PestJSON.php');
include('campayn_widget.php');
include('campayn_settings.php');
include('campayn_install.php');
function campayn_init() {
global $campayn_api;
global $badApi;
$badApi = false;
$apikey = get_option('ob_campayn_apikey');
$apikey = $apikey['text_string'];
$campayn_api = new Pest('http://api.campayn.com/api/v1/');
//Test URLs
//$campayn_api = new Pest('http://campayn.dev/api/v1/');
//$campayn_api = new Pest('http://localhost:6666');
$campayn_api->curl_opts[CURLOPT_HTTPHEADER] = array('Authorization:TRUEREST apikey='.$apikey);
try {
$json = $campayn_api->get('/forms.json?filter[form_type]=1');
} catch (Exception $e) {
$badApi = true;
}
} add_action('init','campayn_init');
// downloads the form list from the campayn api
function api_get_forms() {
global $wpdb;
global $campayn_api;
global $badApi;
$apikey = get_option('ob_campayn_apikey');
$apikey = $apikey['text_string'];
if (empty($apikey)) {
return;
}
if ($badApi) {
return;
}
try {
$json = $campayn_api->get('/forms.json?filter[form_type]=1');
$forms = json_decode($json,true); // we will get the data as an array
if (sizeof($forms)) {
$ft = get_option('campayn_forms_table');
$format = array('%d','%d','%s','%s','%s','%s');
$wpdb->get_results('delete from '.$ft);
foreach ($forms as $f) {
$form = array('id' => $f['id'],
'contact_list_id' => $f['contact_list_id'],
'form_title' => $f['form_title'],
'form_type' => $f['form_type'],
'wp_form' => $f['wp_form'],
'list_name' => $f['list_name']); //just to make sure the order matches $formats
$wpdb->insert($ft,$form,$format);
}
}
}
catch (Exception $e) {
print _e("<br>Caught exception when sending message : " . $e->getMessage());
}
}
// grab the forms from the db and display them
function do_form_list() {
global $wpdb;
global $badApi;
$apikey = get_option('ob_campayn_apikey');
$apikey = $apikey['text_string'];
$ft = get_option('campayn_forms_table');
if (empty($apikey)) {
print _e('You must enter your API key before lists are shown.');
return;
}
if ($badApi) {
echo("-");
return;
}
$fs = $wpdb->get_results('select * from '.$ft);
if (empty($fs)) {
print 'You don\'t have a signup form set up in Campayn. Click on the <a href="http://campayn.com/contacts">CONTACTS</a> tab and select \'Add Sign Up Form\' by clicking on the Options arrow to the right of you contact list.';
return;
}
print '<table>';
foreach ($fs as $f) {
if (!empty($f->wp_form)) { // sometimes the form is null
$shortcode = "[campayn form=\"{$f->id}\"]";
} else {
$shortcode = 'There is no wordpress version of this form';
}
print "<tr style=\"background-color:#FFF\"><td style=\"font-weight:bold;\">{$f->form_title}</td><td>{$f->list_name}</td><td>{$shortcode}</td></tr>";
}
print '</table>';
}
// this will be called from the settings page, as the section 'Forms'
// downloads and displays the forms.
function ob_api_key_callback() {
api_get_forms();
do_form_list();
}
//Is the API key valid?
function campaynApiKeyCheck() {
global $badApi;
if ($badApi) {
echo('<div style="background-color:red; color:#FFF; padding:4px; display:inline-block;">Check your API key, something\'s not right...</div>');
} else {
echo('<div style="background-color:green; color:#FFF; padding:4px; display:inline-block;">API key is functional</div>');
}
}
//text after 'Sign up free' in admin area
function campayn_signup_callback() {
print "If you don't have a Campayn account you can sign up for free <a href=\"http://campayn.com/signup\">here</a>.";
}
//return with the form of the given id
function campayn_get_form($id,$widget = 0) {
global $wpdb;
if (!$id) return;
$ft = get_option('campayn_forms_table');
$f = $wpdb->get_row($wpdb->prepare('select * from '.$ft.' where id = %d',$id));
if (!empty($_SERVER['HTTPS'])) {
$proto = 'https://';
} else {
$proto = 'http://';
}
$url = preg_replace('/([?&])formError=[^&]+(&|$)/','$1',$_SERVER["REQUEST_URI"]);
$url = preg_replace('/([?&])errorReason=[^&]+(&|$)/','$1',$url);
$url = preg_replace('/([?&])formSuccess=[^&]+(&|$)/','$1',$url);
$url = preg_replace('/([?&])formId=[^&]+(&|$)/','$1',$url);
if ($widget) {
$class = ' class="widget-title" ';
} else {
$class = '';
}
$form = '<h3 '.$class.'><span>'.$f->form_title.'</span></h3>';
$form .= str_replace('{redirectUrl}',$proto . $_SERVER["HTTP_HOST"] . $url ,$f->wp_form);
return $form;
}
// return with either the form or the appropriate thanks/error message in its stead
function campayn_get_form_message($id,$widget = 0) {
$rv = ''; // just to be sure
if (is_array($id)) { //called from the shortcode
$id = $id['form'];
}
if ($_GET['formId'] == $id) { // the get variables will contain messages to given (formId) forms
if ($_GET['formSuccess'] == 1) {
return $_GET['thanks'];
}
if ($_GET['formError']) {
$rv = $_GET['errorReason']; // we don't return, because we need the message AND the form
}
}
return campayn_get_form($id,$widget).' '.$rv; // as I said, form + error message (if there is one)
} add_shortcode('campayn',campayn_get_form_message);
function campayn_get_forms_as_options($selected) {
global $wpdb;
$ft = get_option('campayn_forms_table');
$fs = $wpdb->get_results('select * from '.$ft);
if (empty($fs)) {
return;
}
foreach ($fs as $f) {
if (!$f->wp_form) { // we need forms we can display
continue;
}
if ($selected == $f->id) {
$s = 'selected';
} else {
$s = '';
}
$rv .= "<option value=\"{$f->id}\" {$s}>{$f->form_title}</option>";
}
return $rv;
}
// downloads the form list from the campayn api
function campayn_get_lists() {
global $wpdb;
global $campayn_api;
global $badApi;
$apikey = get_option('ob_campayn_apikey');
$apikey = $apikey['text_string'];
if (empty($apikey)) {
return;
}
if ($badApi) {
return;
}
try {
$json = $campayn_api->get('/lists.json');
$forms = json_decode($json,true); // we will get the data as an array
if (empty($forms)) {
return NULL;
}
return $forms;
//print_r($forms);
}
catch (Exception $e) {
print _e("<br>Caught exception when sending message : " . $e->getMessage());
}
}
//creating the dropdown from the email lists ofr the settings page
function campayn_setting_dropdown() {
global $badApi;
if ($badApi) {
echo("-");
return;
}
$options = get_option('ob_campayn_list');
$default_value = NULL;
if (isset($options['text_string'])) {
$current_value = $options['text_string'];
} else {
$current_value = "";
}
$chooseFrom = array();
$choices = campayn_get_lists(); // Array ( [0] => Array ( [id] => 1589 [list_name] => Sample Contact List [tags] => [contact_count] => 14 ) )
if (!empty($choices)) foreach($choices AS $option) {
$key = $option['id'];
$option = $option['list_name'];
$chooseFrom[]= sprintf('<option value="%s" %s>%s</option>',$key,($current_value == $key ) ? ' selected="selected"' : NULL,$option); }
printf('<select id="%s" name="%1$s[text_string]">%2$s</select>%3$s','ob_campayn_list',implode("",$chooseFrom),(!empty ($value['description'])) ? sprintf
("<br /><em>%s</em>",$value['description']) : NULL);
}
function campayn_setting_checkbox() {
$value = get_option('ob_campayn_enabled');
print '<input name="ob_campayn_enabled" id="ob_campayn_enabled" type="checkbox" value="1" class="code" '.checked(1,$value,false ).'/> <label for="ob_campayn_enabled"><em>Allow people to subscribe to your newsletter when making comments to your blog.</em></label>';
}
function campayn_add_comment_fields($fields) {
if ( 1 != get_option('ob_campayn_enabled')) {
return $fields;
}
$text = get_option('ob_campayn_text');
$text = $text['text_string'];
$fields['subscribe'] = '<p class="comment-form-subscribe"><label for="subscribe">'.$text.'</label><input id="subscribe" name="subscribe" type="checkbox" value="1"/></p>';
return $fields;
} add_filter('comment_form_default_fields','campayn_add_comment_fields');
function campayn_subscribe() {
global $campayn_api;
// just to be safe
if ( 1 != get_option('ob_campayn_enabled')) {
return;
}
if (1 != $_POST['subscribe']) {
return;
}
$list = get_option('ob_campayn_list');
$list = $list['text_string'];
$email = array('email' => wp_filter_nohtml_kses($_POST['email']));
// we could do the try-catch stuff here, but it would confuse the commenter so why bother
$uri = '/lists/'.$list.'/contacts.json';
$json = $campayn_api->post($uri,json_encode($email));
} add_action('wp_insert_comment',campayn_subscribe);
?>