forked from sailthru/sailthru-wordpress-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sailthru_form.php
152 lines (116 loc) · 4.04 KB
/
sailthru_form.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
<?php
class sailthru_form {
var $id;
var $name;
var $mailing_lists = array();
var $content;
function sailthru_form($id = false) {
$sailthru_forms = self::get_forms();
if($id && isset($sailthru_forms[$id])) {
$this->id = $sailthru_forms[$id]->id;
$this->name = $sailthru_forms[$id]->name;
$this->mailing_lists = $sailthru_forms[$id]->mailing_lists;
$this->content = $sailthru_forms[$id]->content;
}
elseif($id) {
throw new Exception('sailthru_form constructor passed invalid form id');
}
}
function required_fields() {
$fields = array('email' => 'You must enter an email address.');
if(preg_match('/\[fname\*/i', $this->content)) {
$fields['fname'] = 'You must enter a first name.';
}
if(preg_match('/\[lname\*/i', $this->content)) {
$fields['lname'] = 'You must enter a last name.';
}
return $fields;
}
function has_lists() {
return preg_match('/\[optin-[\d]+/', $this->content);
}
function get_form() {
$form = $this->content;
//email fields
$form = preg_replace('/\[email( "([^"]+)")?\]/i', '<input id="sailthru-email-' . $this->id . '" type="text" class="required $2" />', $form);
//first name
$form = preg_replace('/\[fname(\*?)( "([^"]+)")?\]/i', '<input id="sailthru-fname-' . $this->id . '" type="text" class="$1$3" />', $form);
//last name
$form = preg_replace('/\[lname(\*?)( "([^"]+)")?\]/i', '<input id="sailthru-lname-' . $this->id . '" type="text" class="$1$3" />', $form);
//submit
$form = preg_replace('/\[submit "([^"]+)"( "([^"]+)")?\]/i', '<input id="sailthru-submit-' . $this->id . '" onclick="return false;" type="submit" class="$3" value="$1" /> <img id="sailthru_loader-' . $this->id . '" src="' . Sailthru::get_plugin_url() . '/loading.gif" style="display: none;"/>', $form);
//each list
$lists = sailthru_form::get_all_lists();
foreach ($lists as $id => $list) {
$form = preg_replace("/\[optin-{$id}( \"([^\"]+)\")?\]/i", "<label class=\"$2\"><input checked=\"checked\" type=\"checkbox\" value=\"true\" id=\"sailthru-optin-{$this->id}-{$id}\" /> {$list}</label>", $form);
}
//replace the asterisks with "required"
//remove empty class=""
$form = str_replace(
array(
'class="*',
' class=""'
),
array(
'class="required ',
''
),
$form
);
$form .= '<input type="hidden" id="sailthru_plugin_url" value="' . Sailthru::get_plugin_path() . '" />';
$form = "<form id=\"sailthru-{$this->id}\" class=\"sailthru-form\">{$form}</form>";
return $form;
}
function get_form_code() {
if($this->id) {
return "[sailthru {$this->id}]";
}
return 'You must save this form before a code can be generated';
}
function get_all_lists() {
require_once('client/requires.php');
$client = new Sailthru_Client(get_option('sailthru_api_key'), get_option('sailthru_secret'));
//@TODO: need to do an API call here
//return array('426' => 'test');
//return array('pheonix', 'seed');
$lists = $client->apiGet('list',array());
$list_names = array();
foreach ($lists as $list) {
if (is_array($list)) {
foreach ($list as $list_name) {
$list_names[] = $list_name['name'];
}
}
}
return $list_names;
}
function save() {
$sailthru_forms = self::get_forms();
if(!$this->id) {
$this->id = self::get_new_id();
}
$sailthru_forms[$this->id] = $this;
update_option('sailthru_forms', $sailthru_forms);
return $sailthru_forms;
}
function delete_form($form_id) {
$sailthru_forms = self::get_forms();
unset($sailthru_forms[$form_id]);
update_option('sailthru_forms', $sailthru_forms);
return $sailthru_forms;
}
function get_new_id() {
$id = get_option('sailthru_id');
$id++;
update_option('sailthru_id', $id);
return $id;
}
function get_forms() {
$sailthru_forms = get_option('sailthru_forms');
if(!isset($sailthru_forms) || !is_array($sailthru_forms)) {
$sailthru_forms = array();
}
update_option('sailthru_forms', $sailthru_forms);
return $sailthru_forms;
}
}