-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnewsletter-subscription-form.php
336 lines (270 loc) · 11.3 KB
/
newsletter-subscription-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
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
325
326
327
328
329
330
331
332
333
334
335
336
<?php
/*
Plugin Name: Newsletter Subscription Widget
Description: Collect new subscribers to your newsletter through your WordPress website
Version: 1.0
*/
error_reporting( E_ALL );
require_once ( __DIR__ . '/inc/request.php' );
require_once ( __DIR__ . '/inc/api.php' );
/**
* Add newsletter widget.
*/
class NewsletterSubscriptionForm extends WP_Widget {
public function __construct($number = null) {
load_plugin_textdomain( 'newsletter-subscription-widget', false, 'newsletter-subscription-widget/locale' );
include dirname(__FILE__) . '/newsletter-subscription-form-overrides.php';
$baseId = 'newslettersubscriptionform';
parent::__construct(
$baseId,
$name,
array( 'description' => $description )
);
if($number){
$this->id = $baseId.'-'.$number;
$this->number = $number;
}
$plugin_dir = plugin_basename(__DIR__);
// Register style content
wp_register_style( 'newsletter-subscription-backend', plugins_url($plugin_dir . '/css/newsletter_subscription_backend.css') );
wp_register_style( 'newsletter-subscription-backend-overrides', plugins_url($plugin_dir . '/css/newsletter_subscription_backend_overrides.css') );
wp_register_style( 'newsletter-subscription-frontend', plugins_url($plugin_dir . '/css/newsletter_subscription_frontend.css') );
// Register script content
wp_register_script( 'newsletter-base', plugins_url($plugin_dir . '/js/newsletter_base.js') );
wp_register_script( 'newsletter-subscription-backend', plugins_url($plugin_dir . '/js/newsletter_subscription_backend.js') );
wp_register_script( 'newsletter-subscription-frontend', plugins_url($plugin_dir . '/js/newsletter_subscription_frontend.js') );
}
/**
* Front-end display of widget.
*
* @see WP_Widget::widget()
*
* @param array $args Widget arguments.
* @param array $instance Saved values from database.
*/
public function widget( $args, $instance ) {
if( !$instance['registered'] || !$instance['selected_list'] )
return false;
$instance['plugin_dir'] = plugin_basename(__DIR__);
wp_enqueue_script('jquery');
wp_enqueue_script('jquery-ui-core');
wp_enqueue_script('newsletter-base');
wp_enqueue_script('newsletter-subscription-frontend');
wp_enqueue_style( 'newsletter-subscription-frontend' );
include dirname(__FILE__) . '/views/newsletter_subscription_frontend.php';
}
/**
* Back-end widget form.
*
* @see WP_Widget::form()
*
* @param array $instance Previously saved values from database.
*/
public function form( $instance )
{
include dirname(__FILE__) . '/newsletter-subscription-form-overrides.php';
$instance['username'] = isset( $instance[ 'username' ] ) ? $instance[ 'username' ] : '';
$instance['title'] = isset( $instance[ 'title' ] ) ? $instance[ 'title' ] : $widgetTitle;
$instance['description'] = isset( $instance[ 'description' ] ) ? $instance[ 'description' ] : $widgetDescription;
$instance['confirmationmessage'] = isset( $instance[ 'confirmationmessage' ] ) ? $instance[ 'confirmationmessage' ] : $widgetConfirmationMessage;
$instance['submit_txt'] = isset( $instance[ 'submit_txt' ] ) ? $instance[ 'submit_txt' ] : $widgetSubmitButton;
$instance['registered'] = isset( $instance[ 'registered' ]) ? ($instance[ 'registered' ] !== true ? false : true) : false;
$instance['is_lists_open'] = isset( $instance[ 'is_lists_open' ] ) ? $instance[ 'is_lists_open' ] : 0;
$instance['is_settings_open'] = isset( $instance[ 'is_settings_open' ] ) && $instance[ 'is_settings_open' ] != '' ? $instance[ 'is_settings_open' ] : 1;
$instance['plugin_dir'] = plugin_basename(__DIR__);
$instance['registered'] = $instance['registered'] && !isset($instance['user']->id) ? false : $instance['registered'];
$instance['widget_id'] = $this->id;
if( isset($instance['registered']) && $instance['registered'] !== false ) {
$this->getLists($instance);
}
wp_enqueue_script('newsletter-base');
wp_enqueue_script('newsletter-subscription-backend');
wp_enqueue_style( 'newsletter-subscription-backend' );
wp_enqueue_style( 'newsletter-subscription-backend-overrides' );
include dirname(__FILE__) . '/views/newsletter_subscription_backend.php';
return $instance;
}
/**
* Sanitize widget form values as they are saved.
*
* @see WP_Widget::update()
*
* @param array $new_instance Values just sent to be saved.
* @param array $old_instance Previously saved values from database.
*
* @return array Updated safe values to be saved.
*/
public function update( $new_instance, $old_instance )
{
$instance = $new_instance;
$instance['error_code'] = 0;
if( (isset($instance['password']) && $instance['password'] != '') ) {
$this->loginUser($instance);
}
else {
$instance['user'] = isset($old_instance['user']) ? $old_instance['user'] : null;
$instance['registered'] = isset($old_instance['registered']) ? $old_instance['registered'] : false;
}
if( $instance['registered'] ) {
$this->getUser($instance);
$this->getClient($instance);
$this->getLists($instance);
}
$instance['widget_id'] = $this->id;
unset( $instance['password'] );
return $instance;
}
/**
*
*/
public function getHTMLFields( $instance )
{
ob_start();
include dirname(__FILE__) . "/views/newsletter_subscription_backend_fields.php";
return ob_get_clean();
}
/**
* Login the user
*
* @param array &$instance Values just sent to be saved.
*
*/
private function loginUser( &$instance )
{
$instance['registered'] = false;
$instance['user'] = NewsletterAPI::getLogin($instance['username'], $instance['password']);
if(!$instance['user']) {
$instance['error_code'] = 1;
return false;
}
$instance['registered'] = true;
return $instance['user'];
}
/**
* Retrieve user information.
*
* @param array &$instance Values just sent to be saved.
*
* @return bool Is the process completed successfully.
*/
private function getUser( &$instance )
{
$instance['user'] = NewsletterAPI::getUser($instance['user']->user_key, $instance['user']->user_id);
$instance['user']->user_id = $instance['user']->id;
$instance['user']->gravatar = 'https://www.gravatar.com/avatar/' . md5(strtolower(trim($instance['user']->email)));
return $instance['user'];
}
/**
* Retrieve client information.
*
* @param array &$instance Values just sent to be saved.
*
* @return bool Is the process completed successfully.
*/
private function getClient( &$instance )
{
$instance['client'] = NewsletterAPI::getClient($instance['user']->user_key);
return $instance['client'];
}
/**
* Retrieve user's lists.
*
* @param array &$instance Values just sent to be saved.
*
* @return bool Is the process completed successfully.
*/
private function getLists( &$instance )
{
$this->refreshLists($instance);
$this->getSelectedList($instance);
update_option('newsletter_subscription_widget_'.$this->id.'_lists', $instance['lists']);
return $instance['lists'];
}
/**
* Refresh list with saved data subscribers lists.
*
* @param array &$instance Values just sent to be saved.
*
*/
private function refreshLists( &$instance )
{
$lists = NewsletterAPI::getLists($instance['user']->user_key);
// If the user has no list, just create one
if( count($lists->lists) == 0 ){
$this->createList($instance);
$lists = NewsletterAPI::getLists($instance['user']->user_key);
}
$instance['lists'] = array();
foreach ($lists->lists as $id => $list) {
$this->refreshFields($instance, $list);
}
return $instance['lists'];
}
/**
* Creates subscribers lists.
*
* @param array &$instance Values just sent to be saved.
*
*/
private function createList( $instance )
{
$listCreateParams = array(
'user_key' => $instance['user']->user_key,
'name' => 'Wordpress Default List',
'sender_name' => $instance['user']->first_name . " " . $instance['user']->last_name,
'sender_email' => $instance['user']->email,
'list_policy' => 'accepted',
'list_setup' => 'true'
);
return NewsletterAPI::createList($listCreateParams);
}
/**
*
*/
private function refreshFields( &$instance, $list )
{
$list->fields = array();
$instance['lists'][$list->id] = $list;
foreach ((array)NewsletterAPI::getFields($instance['user']->user_key, $list->id) as $name => $values) {
$list->fields[$name] = array( 'type' => $values );
}
}
public function getSelectedList( &$instance )
{
$instance['selected_list'] = isset($instance['opt-lists']) && $instance['opt-lists'] !== '' && isset($instance['lists'][$instance['opt-lists']])
? $instance['lists'][$instance['opt-lists']]
: reset($instance['lists']);
foreach ($instance['selected_list']->fields as $name => $values) {
$show = isset($instance['field-chk-'.$name]) && $instance['field-chk-'.$name] == 'on' ? true : $name == 'email' ? true : false;
$label = isset($instance['field-'.$name]) ? $instance['field-'.$name] : "";
$index = isset($instance['field-chk-index-'.$name]) && $instance['field-chk-index-'.$name] != "" ? $instance['field-chk-index-'.$name] : 999 - $show;
$instance['selected_list']->fields[$name]['show'] = $show;
$instance['selected_list']->fields[$name]['label'] = $label;
$instance['selected_list']->fields[$name]['index'] = $index;
}
uasort($instance['selected_list']->fields, "self::sortByFieldOrder");
$instance['lists'][$instance['selected_list']->id] = $instance['selected_list'];
return $instance['selected_list'];
}
/**
*
*/
public static function sortByFieldOrder( $el1, $el2 )
{
return $el1['index'] - $el2['index'];
}
}
add_action('widgets_init',
create_function('', 'return register_widget("NewsletterSubscriptionForm");')
);
function newsletter_subscription_form_get_fields(){
$widget = new NewsletterSubscriptionForm($_POST['widget_index']);
$instance = $widget->get_settings();
$instance = $instance[$_POST['widget_index']];
$instance['plugin_dir'] = plugin_basename(__DIR__);
$instance['opt-lists'] = $_POST['list_id'];
$widget->getSelectedList($instance);
die($widget->getHTMLFields($instance));
}
add_action( 'wp_ajax_get_fields','newsletter_subscription_form_get_fields');
?>