forked from wp-plugins/contestfriend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
contest-widget.php
756 lines (633 loc) · 26.2 KB
/
contest-widget.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
<?php
/**
* Contains Wordpress widget and main contest presentation "widget".
* @package chWidget
*/
/**
* contestfriend Wordpress widget class
* @package chWidget
*/
class cf_Contest_WP_Widget extends WP_Widget
{
/**
* Wordpress widget constructor.
*/
public function __construct() {
parent::__construct(
'contestfriend_widget',
'contestfriend',
array( 'description' => __( 'contestfriend widget', 'text_domain' ), )
);
}
/**
* Widget presentation method. Calls the main <i>cf_Widget::html()</i> presentation class.
* @param mixed $args
* @param mixed $instance
*/
public function widget( $args, $instance )
{
echo $args['before_widget'];
echo cf_Widget::html(array('contest' => $instance['contest_id']));
echo $args['after_widget'];
}
/**
* Wordpress widget form process.
* @param mixed $new_instance
* @param mixed $old_instance
*/
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['contest_id'] = strip_tags($new_instance['contest_id']);
return $instance;
}
/**
* Wordpress widget settings.
* @param mixed $instance
*/
public function form($instance)
{
$contest_id = '';
if(isset($instance['contest_id']))
$contest_id = $instance['contest_id'];
$contests = cf_Contest::get_all();
echo '<p>'._e('Contest:', 'contestfriend').'<select id="'.$this->get_field_id('contest_id').'" name="'.$this->get_field_name('contest_id').'" class="widefat">';
foreach($contests as $contest)
{
$selected = '';
if($contest_id==$contest->ID)
$selected = ' selected="selected"';
echo '<option value="'.esc_attr($contest->ID).'"'.$selected.'>'.esc_html($contest->cf_headline).'</option>';
}
echo '</select></p>';
}
}
/**
* Registering new WordPress widget.
*/
add_action('widgets_init', create_function('', 'register_widget("cf_Contest_WP_Widget");'));
/**
* Main ch front-end widget presentation class.
* @package chWidget
*/
class cf_Widget
{
/**
* Stores all widget data in array form.
*
* <pre>
* array(
* 'contest' => cf_Contest, // this widget is presenting
* 'widget_id' => string, // widget identifier (div id, mainly for javascripts)
* 'url' => string, // url widget was presented on
* 'ref' => string, // referral hash (if user was referred)
* 'participant' => cf_Participant // empty if user did not register for the contest yet
* );
* </pre>
* @var mixed
* @access protected
*/
protected $data;
/**
* Stores currently processed contest object.
* @var cf_Widget
*/
static $current_widget = '';
/**
* Tracks number of widgets on the page.
* @var int
*/
static $widget_count = 0;
/**
* Constructs new cf_Widget object
*
* @param mixed $data Widget data
* <pre>
* array(
* 'contest' => cf_Contest, // contest object widget is presenting
* 'widget_id' => string, // widget identifier (div id, mainly for javascripts)
* 'url' => string, // url widget was presented on
* 'ref' => string, // referral hash (if user was referred)
* 'participant' => cf_Participant // empty if user did not register for the contest yet
* );
* </pre>
* @return cf_Widget
*/
function __construct(&$data)
{
$this->data = $data;
}
/**
* Magic function.
* @param mixed $name
*/
public function __get($name)
{
if(array_key_exists($name, $this->data))
return $this->data[$name];
return '';
}
/**
* Builds the Google font API css url for selected contest fonts
* @param mixed $fonts
* @return string Google font API css url or empty string.
*/
function google_style($fonts) // TODO awkward method placement? unnecessary complicated
{
$google_style = 'http://fonts.googleapis.com/css?family=';
$google_fonts = '';
$first = true;
foreach($fonts as $font)
{
if($font['google'])
{
if(!$first)
$google_fonts .= '|';
$first = false;
$google_fonts .= urlencode($font['font']);
}
}
if(!empty($google_fonts))
return $google_style.$google_fonts;
return '';
}
/**
* Retrieves selected contest fonts.
* @return mixed Array with attributes 'google' => true|false, 'font' => 'font face'.
*/
function prepare_font()
{
$fonts = array();
$fonts['headline'] = array('font' => $this->contest->cf_headline_font, 'google' => false);
$fonts['description'] = array('font' => $this->contest->cf_description_font, 'google' => false);
foreach($fonts as &$font)
{
$face = $font['font'];
$is_google = false;
if(substr($face, 0, 7)=='google_')
{
$face = urldecode(substr($font['font'], 7));
$is_google = true;
}
else
{
$face = urldecode($face);
$face = ucwords($face);
}
$font['google'] = $is_google;
$font['font'] = $face;
}
return $fonts;
}
/**
* Main contest widget presentation method.
*
* Called by WordPress widget and shortcode to present the widget to front-end.
* @param mixed $atts Attributes in array form
* <pre>
* $atts = array(
* 'contest' => contest ID,
* 'preview' => true|false
* );
* </pre>
*/
static function html($atts)
{
extract(shortcode_atts(array(
'contest' => '0',
'preview' => false
), $atts ) );
self::$widget_count += 1;
wp_enqueue_style('cf_css_widget', cf_Manager::$plugin_url.'/templates/default/widget.css'); // TODO template support
wp_enqueue_script('cf_js_widget');
wp_localize_script('cf_js_widget', 'cf_ajax', array('ajaxurl' => admin_url('admin-ajax.php')));
wp_enqueue_script('zeroclipboard', cf_Manager::$plugin_url.'/js/ZeroClipboard.min.js');
$data = array();
$data['contest'] = new cf_Contest($contest);
if(!$data['contest']->_valid) // invalid contest
{
// bail
return __('Invalid contest.', 'contestfriend');
}
// get widget id
$data['widget_id'] = 'cf_widget-'.$data['contest']->ID.'_'.self::$widget_count;
// get current ref
$data['ref'] = '';
if(isset($_GET[$data['contest']->ref_variable]))
$data['ref'] = $_GET[$data['contest']->ref_variable];
// get current url
$www = '';
if(isset($_SERVER['HTTP_HOST']))
{
if(strpos($_SERVER['HTTP_HOST'], '://www.')!==false)
$www = 'www.';
if(strpos($_SERVER['SERVER_NAME'], '://www.')!==false)
$www = '';
}
$data['url'] = 'http://'.$www.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
$data['url'] = remove_query_arg('ref', $data['url']);
if($preview!=false)
$data['url'] = 'http://sample.url/';
// get participant if possible
$data['participant'] = '';
if(isset($_COOKIE['contestfriend_'.$data['contest']->ID])) // check for existing cookies
{
$participant_id = $_COOKIE['contestfriend_'.$data['contest']->ID]; // get participant from the cookie
$data['participant'] = new cf_Participant($participant_id, true);
if(!$data['participant']->_valid || $data['participant']->contest_id!=$data['contest']->ID)
$data['participant'] = '';
}
// dummy participant used when previewing the contest
$dummy_participant = array('id' => 0, 'contest_id' => $data['contest']->ID, 'date_gmt' => date("Y-m-d H:i:s"), 'ip' => '127.0.0.1', 'code' => '1234', 'email' => 'sample@email.url', 'first_name' => 'Sample first', 'last_name' => 'Sample last', '_valid' => true);
if($preview=='before_submit')
$data['participant'] = '';
else if($preview=='doubleoptin')
{
$dummy_participant['status'] = 'not_confirmed';
$data['participant'] = new cf_Participant($dummy_participant);
}
else if($preview=='after_submit')
{
$dummy_participant['status'] = '';
$data['participant'] = new cf_Participant($dummy_participant);
}
self::$current_widget = new cf_Widget($data);
$output = self::get_template('widget');
return $output;
}
/**
* Returns current widget
* @return cf_Widget
*/
static function current_widget()
{
return self::$current_widget;
}
/**
* Returns the template content.
* @param string $tpl Template name. No / \ * . characters allowed.
*/
static function get_template($tpl)
{
$template_name = 'default';
$template_name = apply_filters('cf_template_name', $template_name);
$template_name = str_replace(array('/', '\\', '*', '.'), '', $template_name);
$tpl = str_replace(array('/', '\\', '*', '.'), '', $tpl);
if(file_exists(cf_Manager::$plugin_dir.'/templates/'.$template_name.'/'.$tpl.'.php')) // TODO: better templating support
{
ob_start();
include(cf_Manager::$plugin_dir.'/templates/'.$template_name.'/'.$tpl.'.php');
$data = ob_get_clean();
return $data;
}
return false;
}
/**
* Main contest widget process method.
* Called by AJAX script on contest registration form submit.
*/
public static function process()
{
global $wpdb;
// check for valid contest
$contest = new cf_Contest(intval($_POST['contest_id']));
if($contest->_valid==false)
{
echo '<div class="error">'.__('Invalid contest', 'contestfriend').'</div>';
die();
}
// get POST data
$widget_id = esc_attr($_POST['div_id']);
$url = urldecode($_POST['url']);
$email = esc_sql($_POST['email']);
$first_name = '';
if(!empty($_POST['first_name']))
$first_name = esc_sql($_POST['first_name']);
$last_name = '';
if(!empty($_POST['last_name']))
$last_name = esc_sql($_POST['last_name']);
$ref = '';
if(!empty($_POST['cf_ref']))
$ref = esc_sql($_POST['cf_ref']);
$do_process = true; // flag to indicate invalid input ~ do not process any data, just print the widget
// init widget data
$data = array();
$data['contest'] =& $contest;
$data['widget_id'] = $widget_id;
$data['url'] = $url;
$data['ref'] = $ref;
$data['participant'] = '';
$data['error'] = array();
$data['status'] = '';
// if the contest did not start yet or has already ended, just echo the original widget (started/expired error messages are handled in the template)
if(!$contest->is_started() || $contest->is_expired())
{
self::$current_widget = new cf_Widget($data);
echo self::get_template('widget');
die();
}
// validate email
$pattern = '/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/';
if(preg_match($pattern, $email)!=1)
{
$data['error']['email'] = 1;
$do_process = false;
}
// validate names
if($contest->cf_name_field=='1' && $contest->cf_name_field_req=='1')
{
if(empty($first_name))
{
$data['error']['first_name'] = 1;
$do_process = false;
}
if(empty($last_name))
{
$data['error']['last_name'] = 1;
$do_process = false;
}
}
if($do_process) // if input is ok
{
$double_optin = false;
if($contest->cf_double_optin=='1')
$double_optin = true;
// prepare participant data
$participant_data = array('ip' => $_SERVER['REMOTE_ADDR'],
'date_gmt' => current_time('mysql', 1),
'contest_id' => $contest->ID,
'code' => cf_Participant::generate_code(),
'email' => $email,
'status' => ''
);
if($double_optin)
$participant_data['status'] = 'not_confirmed';
$participant = new cf_Participant($participant_data);
$participant_id = $participant->exists();
if($participant_id===false) // we have a new participant (unique email address for this contest)
{
$participant_id = $participant->add();
if($contest->cf_name_field=='1') // if using name fields, store the values
{
$participant->add_meta('first_name', $first_name);
$participant->add_meta('last_name', $last_name);
}
if($contest->cf_referral_field=='1') // if using referral field generate and store short url for the new participant referral link
{
$ref_url = add_query_arg($contest->ref_variable, $participant->code, $url);
$ref_url_short = self::process_shorten_url($contest, $participant, $ref_url);
if(!empty($ref_url_short))
$participant->add_meta('short_ref', $ref_url_short);
}
if(!empty($ref) && $contest->cf_referral_field=='1') // if participant was referred, store it
{
if(!$double_optin)
{
$referral = new cf_Participant($ref, true);
if($referral->_valid && !empty($participant_id))
$referral->add_meta('referral_to', $participant_id); // store that participant was referred by $referral
}
else // if is double-optin, can't store referral until participant confirms his email
$participant->add_meta('tmp_referral', $ref);
}
if(!$double_optin)
{
// process stuff
self::process_email($contest, $participant);
self::process_integration($contest, $participant);
self::process_cookie($contest, $participant);
}
else
{
// if double_optin, process different stuff
self::process_optin_email($contest, $participant, $url);
}
}
else // already existing participant (non-unique email address for this contest)
{
$participant = new cf_Participant($participant_id);
if($participant->status!='not_confirmed') // if existing participant is confirmed
{
self::process_cookie($contest, $participant); // reset his cookie
}
else
{ // if existing participant is not_confirmed (did not confirm the double_optin)
self::process_optin_email($contest, $participant, $url); // resend the confirmation email
}
}
}
$data['participant'] = $participant;
self::$current_widget = new cf_Widget($data);
echo self::get_template('widget');
die();
}
/**
* Shortens the $url with goo.gl
* @param cf_Contest $contest
* @param cf_Participant $participant
* @param string $url
* @return string|boolean Shortened url or false on error.
*/
static function process_shorten_url($contest, $participant, $url)
{
// user should register for a contest only once -> storing short url only once,
// if there is the same contest on multiple pages and user registers twice (deletes his cookie), only one short url will be saved (the first)
if(isset($participant->short_ref))
return false;
$google_apikey = urlencode($contest->cf_googleapi);
if(empty($google_apikey))
$google_apikey = 'AIzaSyDE2OGgu88jiLw9UD4ACQNd_prf_V8CwRE';
$api_url = 'https://www.googleapis.com/urlshortener/v1/url/?key='.$google_apikey;
$data = array('longUrl' => $url);
$data_string = json_encode($data);
$ch = curl_init($api_url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($data_string)));
$result = curl_exec($ch);
$result_data = json_decode($result, true);
if(isset($result_data['error']))
return false;
if(!isset($result_data['id']))
return false;
return $result_data['id'];
}
/**
* Sends confirmation email to participant.
* @param cf_Contest $contest
* @param cf_Participant $participant
*/
static function process_email($contest, $participant)
{
$confirmation_email = $contest->cf_confirmation_email;
if($confirmation_email!='1')
return;
$to = $participant->email;
$subject = $contest->cf_confirmation_email_subject;
$first_name = isset($participant->first_name) ? $participant->first_name : '';
$last_name = isset($participant->last_name) ? $participant->last_name : '';
$subject = str_replace('{FIRST_NAME}', $first_name, $subject);
$subject = str_replace('{LAST_NAME}', $last_name, $subject);
$message = $contest->cf_confirmation_email_text;
$message = str_replace('{FIRST_NAME}', $first_name, $message);
$message = str_replace('{LAST_NAME}', $last_name, $message);
$from_email = '';
if(isset($contest->cf_from_email))
$from_email = $contest->cf_from_email;
if(empty($from_email))
$from_email = 'contestfriend <'.get_option('admin_email').'>';
$headers = 'From: '.$from_email."\r\n";
$res = wp_mail($to, $subject, $message, $headers);
}
/**
* Sends double opt-in email to participant.
* @param cf_Contest $contest
* @param cf_Participant $participant
* @param string $url
*/
static function process_optin_email($contest, $participant, $url)
{
if(isset($_GET[$contest->ref_variable]))
$url = remove_query_arg($contest->ref_variable, $url);
$confirm_url = add_query_arg('contestfriend_confirm', $participant->code, $url);
$to = $participant->email;
$subject = $contest->cf_double_optin_subject;
$first_name = isset($participant->first_name) ? $participant->first_name : '';
$last_name = isset($participant->last_name) ? $participant->last_name : '';
$subject = str_replace('{FIRST_NAME}', $first_name, $subject);
$subject = str_replace('{LAST_NAME}', $last_name, $subject);
$message = $contest->cf_double_optin_email;
$message = str_replace('{URL}', $confirm_url, $message);
$message = str_replace('{FIRST_NAME}', $first_name, $message);
$message = str_replace('{LAST_NAME}', $last_name, $message);
$from_email = '';
if(isset($contest->cf_from_email))
$from_email = $contest->cf_from_email;
if(empty($from_email))
$from_email = 'contestfriend <'.get_option('admin_email').'>';
$headers = 'From: '.$from_email."\r\n";
$res = wp_mail($to, $subject, $message, $headers);
// TODO add antispam when(if?) resending emails
}
/**
* Processes email confirmation when double opt-in is set.
* @param string $confirm_code
*/
static function process_optin($confirm_code)
{
if(empty($confirm_code))
return false;
$participant = new cf_Participant($confirm_code, true); // get participant based on the confirmation code
if(!$participant->_valid)
return false;
if($participant->status!='not_confirmed') // check participant status, has not be not_confirmed or there is nothing to confirm
return false;
$contest = new cf_Contest($participant->contest_id); // check if the contest participant is in, is valid
if(!$contest->_valid)
return false;
if(isset($participant->tmp_referral)) // if the participant was referred to the contest
{
// get referring participant and credit him the referral
$referral = new cf_Participant($participant->tmp_referral, true);
if($referral->_valid)
$referral->add_meta('referral_to', $participant->id);
$participant->del_meta('tmp_referral');
}
$participant->set_status(''); // change the participant status
// process stuff
self::process_email($contest, $participant);
self::process_integration($contest, $participant);
self::process_cookie($contest, $participant);
}
/**
* Sets a cookie to remember the participant.
* @param cf_Contest $contest
* @param cf_Participant $participant
*/
static function process_cookie($contest, $participant)
{
$now = current_time('timestamp', 1);
$end = $contest->get_utc_time();
$res = setcookie('contestfriend_'.$contest->ID, $participant->code, time()+($end-$now), '/');
}
/**
* Processes integration with 3rd party APIs.
* @param cf_Contest $contest
* @param cf_Participant $participant
*/
static function process_integration($contest, $participant)
{
// mailing lists
$email = $participant->email;
$name = '';
$first_name = '';
$last_name = '';
if($contest->cf_name_field=='1')
{
$name = $participant->first_name.' '.$participant->last_name;
$first_name = $participant->first_name;
$last_name = $participant->last_name;
}
if($contest->cf_participants_export=='campaignmonitor')
{
if(!class_exists(CS_REST_Subscribers))
require(cf_Manager::$plugin_dir.'lib/campaign_monitor/csrest_subscribers.php');
$wrap = new CS_REST_Subscribers($contest->cf_campaignmonitor_list, $contest->cf_campaignmonitor_key);
$res = $wrap->add(array(
'EmailAddress' => $email,
'Name' => $name,
'Resubscribe' => true
));
// $res->was_successful()
// $res->http_status_code
// $res->response
}
else if($contest->cf_participants_export=='mailchimp')
{
if(!class_exists(MCAPI))
require(cf_Manager::$plugin_dir.'lib/MCAPI.class.php');
$double_optin = true;
if($contest->cf_double_optin=='1')
$double_optin = false;
$api = new MCAPI($contest->cf_mailchimp_key);
$api->listSubscribe($contest->cf_mailchimp_list, $email, array('FNAME' => $first_name, 'LNAME' => $last_name), 'html', $double_optin); // double optin;
// if($api->errorCode)
// $api->errorCode
// $api->errorMessage
}
else if($contest->cf_participants_export=='getresponse')
{
require(cf_Manager::$plugin_dir.'lib/GetResponseAPI.class.php');
$api = new GetResponseAPI($contest->cf_getresponse_key);
$response = $api->addContact($contest->cf_getresponse_list, $name, $email);
// var_dump($response);
}
else if($contest->cf_participants_export=='aweber')
{
require(cf_Manager::$plugin_dir.'lib/aweber/aweber_api.php');
$aweber_auth = $contest->cf_aweber_auth;
if(!empty($aweber_auth) && is_array($aweber_auth))
{
$api = new AWeberAPI($aweber_auth['consumer_key'], $aweber_auth['consumer_secret']);
try {
$account = $api->getAccount($aweber_auth['access_key'], $aweber_auth['access_secret']);
$listURL = "/accounts/{$account->id}/lists/{$contest->cf_aweber_list}";
$list = $account->loadFromUrl($listURL);
// create a subscriber
$params = array(
'email' => $email,
'ip_address' => $_SERVER['REMOTE_ADDR'],
'name' => $name,
);
$subscribers = $list->subscribers;
$new_subscriber = $subscribers->create($params);
}
catch(AWeberAPIException $exc)
{
// TODO log (post comments?)
}
}
}
do_action('cf_process_integration', $contest, $participant);
}
}