PluginProbe
FV Player 8 / trunk
FV Player 8 vtrunk
trunk 8.0.18 8.0.19 8.0.20 8.0.21 8.0.25 8.0.27 8.1 8.1.3
fv-player / models / email-subscription.php

email-subscription.php in FV Player 8 trunk, at models/email-subscription.php

772 lines 30.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) {
4 exit;
5 }
6
7 class FV_Player_Email_Subscription {
8
9 public function __construct() {
10
11 if ( ! defined( 'ABSPATH' ) ) {
12 exit;
13 }
14
15 add_action( 'admin_init', array($this, 'init_options') );
16
17 add_action( 'admin_init', array($this, 'admin__add_meta_boxes') );
18 add_filter( 'fv_flowplayer_popup_html', array($this, 'popup_html') );
19 add_filter( 'fv_player_conf_defaults', array($this, 'conf_defaults') );
20 add_filter( 'fv_flowplayer_settings_save', array($this, 'fv_flowplayer_settings_save'), 10, 2 );
21 add_action( 'wp_ajax_nopriv_fv_wp_flowplayer_email_signup', array($this, 'email_signup') );
22 add_action( 'wp_ajax_fv_wp_flowplayer_email_signup', array($this, 'email_signup') );
23 add_filter( 'fv_player_admin_popups_defaults', array($this,'fv_player_admin_popups_defaults') );
24
25 add_action( 'wp_ajax_fv_player_email_subscription_save', array($this, 'save_settings') );
26
27 add_action('admin_init', array( $this, 'csv_export' ) );
28
29 add_action( 'admin_notices', array($this,'admin_export_screen') );
30
31 add_filter( 'fv_flowplayer_attributes', array( $this, 'popup_preview' ), 10, 3 );
32 }
33
34 /*
35 * SETTINGS
36 */
37
38 public function conf_defaults($conf) {
39 $conf += array(
40 'mailchimp_api' => '',
41 'mailchimp_list' => '',
42 'mailchimp_label' => 'Subscribe for updates',
43 );
44 return $conf;
45 }
46
47 public function init_options() {
48 if( !get_option('fv_player_email_lists') ) {
49 update_option('fv_player_email_lists', array( 1 => array('first_name' => true,
50 'last_name' => false,
51 'integration' => false,
52 'title' => 'Subscribe to list one',
53 'description' => 'Two good reasons to subscribe right now',
54 'disabled' => false
55 ) ) );
56 }
57 }
58
59 public function admin__add_meta_boxes() {
60 add_meta_box('fv_flowplayer_email_lists', __( 'Email Popups', 'fv-player' ), array($this, 'settings_box_lists'), 'fv_flowplayer_settings_actions', 'normal');
61 add_meta_box('fv_flowplayer_email_integration', __( 'Email Integration', 'fv-player' ), array($this, 'settings_box_integration'), 'fv_flowplayer_settings_actions', 'normal');
62 }
63
64 public function fv_flowplayer_settings_save($param1,$param2){
65
66 if ( isset( $_POST['email_lists'] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['fv_player_email_lists_nonce'] ) ), 'fv_player_email_lists' ) ) {
67 $aOptions = array();
68 unset($aOptions['#fv_popup_dummy_key#']);
69
70 foreach( $_POST['email_lists'] AS $key => $value ) {
71 $key = intval($key);
72 $aOptions[$key]['first_name'] = sanitize_text_field( $value['first_name'] );
73 $aOptions[$key]['last_name'] = sanitize_text_field( $value['last_name'] );
74 $aOptions[$key]['integration'] = isset($value['integration']) ? sanitize_text_field( $value['integration'] ) : false;
75 $aOptions[$key]['title'] = sanitize_text_field( $value['title'] );
76 $aOptions[$key]['description'] = sanitize_text_field( $value['description'] );
77
78 }
79 update_option('fv_player_email_lists',$aOptions);
80 }
81
82 return $param1;
83 }
84
85 public function fv_player_admin_popups_defaults($aData){
86 $aPopupData = get_option('fv_player_email_lists');
87 unset($aPopupData['#fv_list_dummy_key#']);
88
89 if( is_array($aPopupData) ) {
90 foreach( $aPopupData AS $key => $aPopupAd ) {
91 $aData['email-' . $key] = $aPopupAd;
92 }
93 }
94
95 return $aData;
96 }
97
98 public function settings_box_integration () {
99 global $fv_fp;
100 ?>
101 <p><?php esc_html_e( 'Enter your service API key and then assign it to a list which you create above.', 'fv-player' ); ?></p>
102 <?php if( version_compare(phpversion(),'5.3.0') >= 0 ) : ?>
103 <table class="form-table2" style="margin: 5px; ">
104 <tr>
105 <td style="width: 250px"><label for="mailchimp_api"><?php esc_html_e( 'Mailchimp API key', 'fv-player' ); ?>:</label></td>
106 <td>
107 <p class="description">
108 <input type="text" name="mailchimp_api" id="mailchimp_api" value="<?php echo esc_attr($fv_fp->_get_option('mailchimp_api')); ?>" />
109 </p>
110 </td>
111 </tr>
112 <tr>
113 <td></td>
114 <td>
115 <a class="fv-wordpress-flowplayer-save button button-primary" href="#"><?php esc_html_e( 'Save', 'fv-player' ); ?></a>
116 </td>
117 </tr>
118 </table>
119 <?php else : ?>
120 <p><?php esc_html_e( 'Please upgrade to PHP 5.3 or above to use the Mailchimp integration.', 'fv-player' ); ?></p>
121 <?php endif;
122 }
123
124 public function settings_box_lists () {
125 global $fv_fp;
126
127 $aListData = get_option('fv_player_email_lists');
128 if( empty($aListData) ) {
129 $aListData = array( 1 => array() );
130 }
131 if(!isset($aListData['#fv_list_dummy_key#'])){
132 $aListData = array( '#fv_list_dummy_key#' => array() ) + $aListData ;
133 }
134 $aMailchimpLists = $this->get_mailchimp_lists();
135 ?>
136 <p><?php esc_html_e( 'Lists defined here can be used for subscription box for each video or for Default Popup above.', 'fv-player' ); ?></p>
137 <table class="form-table2" style="margin: 5px; ">
138 <tr>
139 <td>
140 <table id="fv-player-email_lists-settings">
141 <thead>
142 <tr>
143 <td>ID</td>
144 <td style="width: 40%"><?php esc_html_e( 'Properties', 'fv-player' ); ?></td>
145 <?php if( !empty($aMailchimpLists['result']) ) : ?>
146 <td><?php esc_html_e( 'Target List', 'fv-player' ); ?></td>
147 <?php endif; ?>
148 <td><?php esc_html_e( 'Export', 'fv-player' ); ?></td>
149 <td><?php esc_html_e( 'Options', 'fv-player' ); ?></td>
150 <td><?php esc_html_e( 'Status', 'fv-player' ); ?></td>
151 <td></td>
152 </tr>
153 </thead>
154 <tbody>
155 <?php
156
157 foreach ($aListData AS $key => $aList) {
158 $mailchimpOptions = '';
159
160 foreach($aMailchimpLists['result'] as $mailchimpId => $list){
161 if(!$list)
162 continue;
163 $use = true;
164 foreach($list['fields'] as $field){
165
166 if( $field['required'] && ($field['tag'] === "FNAME" && ( empty($aList['first_name']) || !$aList['first_name'] ) || $field['tag'] === "LNAME" && ( empty($aList['last_name']) || !$aList['last_name'] ) ) ){
167 $use = false;
168 break;
169 }
170
171 }
172 if($use){
173 $mailchimpOptions .= '<option value="mailchimp-' . $list['id'] . '" ' . ( isset($aList['integration']) && 'mailchimp-' . $list['id'] === $aList['integration']?"selected":"" ) . '>' . $list['name'] . '</option>';
174 }
175 }
176
177 if( $aMailchimpLists && $mailchimpOptions ) {
178 $mailchimp_no_option = 'None';
179 } else if( $aMailchimpLists && !$mailchimpOptions ) {
180 $mailchimp_no_option = 'No matching list found';
181 }
182
183 ?>
184 <tr class='data' id="fv-player-list-item-<?php echo esc_attr( $key ); ?>"<?php echo $key === '#fv_list_dummy_key#' ? ' style="display:none"' : ''; ?>>
185 <td class='id'><?php echo esc_html( $key ); ?></td>
186 <td>
187 <table>
188 <tr>
189 <td style="width:16%"><label>Header</label></td>
190 <td><input type='text' name='email_lists[<?php echo esc_attr( $key ); ?>][title]' value='<?php echo isset($aList['title']) ? esc_attr($aList['title']) : ''; ?>' /></td>
191 </tr>
192 <tr>
193 <td><label>Message</label></td>
194 <td><input type='text' name='email_lists[<?php echo esc_attr( $key ); ?>][description]' value='<?php echo isset($aList['description']) ? esc_attr($aList['description']) : ''; ?>' /></td>
195 </tr>
196 </table>
197 </td>
198 <?php if( !empty($aMailchimpLists['result']) ) : ?>
199 <td>
200 <select name="email_lists[<?php echo esc_attr( $key ); ?>][integration]" title="E-mail list">
201 <option value=""><?php echo esc_html( $mailchimp_no_option ); ?></option>
202 <?php echo wp_kses( $mailchimpOptions, array( 'option' => array( 'value' => array() ) ) );?>
203 </select>
204 <br />&nbsp;
205 </td>
206 <?php endif; ?>
207 <td>
208 <a class='fv-player-list-export' href='<?php echo wp_nonce_url( admin_url('admin.php?page=fvplayer&fv-email-export='.$key ), 'fv-email-export', 'nonce' ); ?>' target="_blank" ><?php esc_html_e( 'Download CSV', 'fv-player' ); ?></a>
209 <br />
210 <a class='fv-player-list-export' href='<?php echo wp_nonce_url( admin_url('admin.php?page=fvplayer&fv-email-export-screen='.$key), 'fv-email-show', 'nonce' ); ?>' target="_blank" ><?php esc_html_e( 'View list', 'fv-player' ); ?></a>
211 </td>
212 <td>
213 <input type='hidden' name='email_lists[<?php echo esc_attr( $key ); ?>][first_name]' value='0' />
214 <input id='list-first-name-<?php echo esc_attr( $key ); ?>' title="first name" type='checkbox' name='email_lists[<?php echo esc_attr( $key ); ?>][first_name]' value='1' <?php echo (isset($aList['first_name']) && $aList['first_name'] ? 'checked="checked"' : ''); ?> />
215 <label for='list-first-name-<?php echo esc_attr( $key ); ?>'>First Name</label>
216 <br />
217 <input type='hidden' name='email_lists[<?php echo esc_attr( $key ); ?>][last_name]' value='0' />
218 <input id='list-last-name-<?php echo esc_attr( $key ); ?>' title="last name" type='checkbox' name='email_lists[<?php echo esc_attr( $key ); ?>][last_name]' value='1' <?php echo (isset($aList['last_name']) && $aList['last_name'] ? 'checked="checked"' : ''); ?> />
219 <label for='list-last-name-<?php echo esc_attr( $key ); ?>'>Last Name</label>
220 </td>
221 <td>
222 <input type='hidden' name='email_lists[<?php echo esc_attr( $key ); ?>][disabled]' value='0' />
223 <input id='ListAdDisabled-<?php echo esc_attr( $key ); ?>' type='checkbox' title="disable" name='email_lists[<?php echo esc_attr( $key ); ?>][disabled]' value='1' <?php echo (isset($aList['disabled']) && $aList['disabled'] ? 'checked="checked"' : ''); ?> />
224 <label for='ListAdDisabled-<?php echo esc_attr( $key ); ?>'>Disable</label>
225 <br />
226 <a class='fv-player-list-remove' href=''><?php esc_html_e( 'Remove', 'fv-player' ); ?></a>
227 </td>
228 <td>
229 <input type="button" style="visibility: hidden" class="fv_player_email_list_save button" value="Save & Preview" />
230 </td>
231 </tr>
232 <?php
233 }
234 ?>
235 </tbody>
236 </table>
237 </td>
238 </tr>
239 <tr>
240 <td>
241 <?php wp_nonce_field( 'fv_player_email_lists', 'fv_player_email_lists_nonce' ); ?>
242 <input type="submit" name="fv-wp-flowplayer-submit" class="button-primary" value="Save All Changes">
243 <input type="button" value="<?php esc_html_e( 'Add More Lists', 'fv-player' ); ?>" class="button" id="fv-player-email_lists-add" />
244 </td>
245 </tr>
246 </table>
247
248 <script>
249 jQuery('#fv-player-email_lists-add').on('click', function() {
250 var fv_player_list_index = (parseInt( jQuery('#fv-player-email_lists-settings tr.data:last .id').html() ) || 0 ) + 1;
251 jQuery('#fv-player-email_lists-settings').append(jQuery('#fv-player-email_lists-settings tr.data:first').prop('outerHTML').replace(/#fv_list_dummy_key#/gi,fv_player_list_index + ""));
252 jQuery('#fv-player-list-item-' + fv_player_list_index).show();
253 return false;
254 } );
255
256 jQuery(document).on('click','.fv-player-list-remove', false, function() {
257 if( confirm('Are you sure you want to remove the list?') ){
258 jQuery(this).parents('.data').remove();
259 if(jQuery('#fv-player-email_lists-settings .data').length === 1) {
260 jQuery('#fv-player-email_lists-add').trigger('click');
261 }
262 }
263 return false;
264 } );
265
266 jQuery(document).on('keydown change', '#fv-player-email_lists-settings', function(e) {
267 var row = jQuery(e.target).parents('[id^="fv-player-list-item-"]');
268 row.find('.fv_player_email_list_save').css('visibility','visible');
269 });
270 jQuery(document).on('click', '#fv-player-email_lists-settings input[type=checkbox]', function(e) {
271 var row = jQuery(e.target).parents('[id^="fv-player-list-item-"]');
272 row.find('.fv_player_email_list_save').css('visibility','visible');
273 });
274
275 jQuery(document).on('click', '.fv_player_email_list_save', function() {
276 var button = jQuery(this);
277 var row = button.parents('[id^="fv-player-list-item-"]');
278 var aInputs = row.find('input, select');
279 var key = row.attr('id').replace(/fv-player-list-item-/,'');
280
281 fv_player_open_preview_window(null,720,480);
282
283 button.prop('disabled',true);
284 jQuery.ajax( {
285 type: "POST",
286 url: ajaxurl,
287 data: aInputs.serialize()+'&key='+key+'&action=fv_player_email_subscription_save&_wpnonce=<?php echo wp_create_nonce('fv_player_email_subscription_save'); ?>',
288 success: function(response) {
289 button.css('visibility','hidden');
290 button.prop('disabled', false);
291
292 row.replaceWith( jQuery('#'+row.attr('id'),response) );
293
294 var shortcode = '<?php echo '[fvplayer src="https://player.vimeo.com/external/196881410.hd.mp4?s=24645ecff21ff60079fc5b7715a97c00f90c6a18&profile_id=174&oauth2_token_id=3501005" splash="https://i.vimeocdn.com/video/609485450_1280.jpg" preroll="no" postroll="no" subtitles="'.flowplayer::get_plugin_url().'/images/test-subtitles.vtt" end_popup_preview="true" popup="email-#key#" caption="'.__("This is how the popup will appear at the end of a video", 'fv-player').'"]'; ?>';
295 shortcode = shortcode.replace(/#key#/,key);
296
297 var url = '<?php echo home_url(); ?>?fv_player_preview_nonce=<?php echo wp_create_nonce( "fv_player_preview" ); ?>&fv_player_preview=' + fv_player_editor.b64EncodeUnicode(shortcode);
298 fv_player_open_preview_window(url);
299 },
300 error: function() {
301 button.val('Error saving!');
302 }
303 } );
304 });
305
306 function fv_player_open_preview_window(url, width, height){
307 height = Math.min(window.screen.availHeight * 0.80, height + 25);
308 width = Math.min(window.screen.availWidth * 0.66, width + 100);
309
310 if( typeof fv_player_preview_window == 'undefined' || fv_player_preview_window == null || fv_player_preview_window.self == null || fv_player_preview_window.closed ){
311 fv_player_preview_window = window.open(url,'window','toolbar=no, menubar=no, resizable=yes width=' + width + ' height=' + height);
312 }else{
313 fv_player_preview_window.location.assign(url);
314 fv_player_preview_window.focus();
315 }
316
317 }
318 </script>
319 <?php
320 }
321
322 /*
323 * GENEREATE HTML
324 */
325 public function popup_html($popup) {
326 if ($popup === 'email-no'){
327 return '';
328 }
329
330 if(strpos($popup,'email-') !== 0)
331 {
332 return $popup ;
333 }
334
335 $id = array_reverse(explode('-',$popup));
336 $id = $id[0];
337 $aLists = get_option('fv_player_email_lists',array());
338 $list = isset($aLists[$id]) ? $aLists[$id] : array('disabled' => '1');
339
340 if( empty($list['title']) || isset($list['disabled']) && $list['disabled'] === '1'){
341 return '';
342 }
343 $popupItems = '';
344 $count = 1;
345 foreach($list as $key => $field){
346 if(($key === 'first_name' || $key === 'last_name') && $field == "1"){
347 $count++;
348 $aName = explode('_',$key);
349 foreach($aName as $nameKey => $val){
350 $aName[$nameKey] = ucfirst($aName[$nameKey]);
351 }
352
353 $sName = implode(' ',$aName);
354 $popupItems .= '<input type="text" placeholder="' . $sName . '" name="' . $key . '" required/>';
355 }
356
357 }
358 $popup = '';
359 if( !empty($list['title']) ) $popup .= '<h3>'.$list['title'].'</h3>';
360 if( !empty($list['description']) ) $popup .= '<p>'.$list['description'].'</p>';
361 $popup .= '<form class="mailchimp-form mailchimp-form-' . $count . '">'
362 . '<input type="hidden" name="list" value="' . $id . '" />'
363 . '<input type="email" placeholder="' . esc_attr__( 'Email Address', 'fv-player' ) . '" name="email"/>'
364 . $popupItems . '<input type="submit" value="' . esc_attr__( 'Subscribe', 'fv-player' ) . '"/></form>';
365 return $popup;
366 }
367
368 /*
369 * API CALL
370 */
371 private function get_mailchimp_lists() {
372 if(version_compare(phpversion(),'5.3.0','<')){
373 return array('error' => 'PHP 5.3 or above required.', 'result' => false);
374 }
375
376 global $fv_fp;
377 $aLists = array();
378
379 if (!$fv_fp->_get_option('mailchimp_api')) {
380 update_option('fv_player_mailchimp_lists', $aLists);
381 return array('error' => 'No API key found. ', 'result' => $aLists);
382 }
383
384 $aLists = get_option('fv_player_mailchimp_lists', array());
385 $sTimeout = !$aLists || count($aLists) == 0 ? 60 : 3600;
386
387 if( get_option('fv_player_mailchimp_time', 0 ) + $sTimeout > time() ) return array('error' => false, 'result' => $aLists);
388
389 if( !class_exists('\DrewM\MailChimp\MailChimp') ) {
390 require_once dirname(__FILE__) . '/../includes/mailchimp-api/src/MailChimp.php';
391 }
392 require_once dirname(__FILE__) . '/email-subscription-mailchimp.php';
393
394 $result = fv_player_mailchimp_result();
395 $error = fv_player_mailchimp_last_error();
396 if ($error || !$result) {
397 update_option('fv_player_mailchimp_time', time() - 50 * 60);
398 update_option('fv_player_mailchimp_lists', $aLists);
399 return array('error' => $error, 'result' => $aLists);
400 }
401 $aLists = array();
402 foreach ($result['lists'] as $list) {
403 $item = array(
404 'id' => $list['id'],
405 'name' => $list['name'],
406 'fields' => array()
407 );
408
409
410 foreach ($list['_links'] as $link) {
411 if ($link['rel'] === 'merge-fields') {
412 $mergeFields = fv_player_mailchimp_get($list['id']);
413 foreach ($mergeFields['merge_fields'] as $field) {
414 $item['fields'][] = array(
415 'tag' => $field['tag'],
416 'name' => $field['name'],
417 'required' => $field['required'],
418 );
419 }
420 break;
421 }
422 }
423 $aLists[$list['id']] = $item;
424 }
425
426 update_option('fv_player_mailchimp_time', time() );
427 update_option('fv_player_mailchimp_lists', $aLists);
428 return array('error' => false, 'result' => $aLists);
429 }
430
431 /**
432 * Mailchimp signup
433 *
434 * @param int $list_id List ID.
435 * @param array $data Already sanitized data by sanitize_subscriber_data() method.
436 * @return array Result.
437 */
438 private function mailchimp_signup( $list_id, $data ){
439
440 if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'fv_player_email_signup' ) ) {
441 wp_send_json(
442 array(
443 'status' => 'ERROR',
444 'text' => __( 'Nonce verification error.', 'fv-player' ),
445 )
446 );
447 exit;
448 }
449
450 if( !class_exists('\DrewM\MailChimp\MailChimp') ) {
451 require_once dirname(__FILE__) . '/../includes/mailchimp-api/src/MailChimp.php';
452 }
453 require_once dirname(__FILE__) . '/email-subscription-mailchimp.php';
454
455 $merge_fields = array();
456
457 if ( isset( $data['first_name'] ) ) {
458 $merge_fields['FNAME'] = $data['first_name'];
459 }
460
461 if ( isset( $data['last_name'] ) ) {
462 $merge_fields['LNAME'] = $data['last_name'];
463 }
464
465 $result_data = fv_player_mailchimp_post( $list_id, sanitize_email( $data['email'] ), $merge_fields );
466
467 $result = array(
468 'status' => 'OK',
469 'text' => __( 'Thank You for subscribing.', 'fv-player' ),
470 'error_log' => false,
471 );
472
473
474 if ($result_data['status'] === 400) {
475 if ($result_data['title'] === 'Member Exists') {
476 $result = array(
477 'status' => 'ERROR',
478 'text' => __( 'Email Address already subscribed.', 'fv-player' ),
479 'error_log' => $result_data,
480 );
481 } elseif ($result_data['title'] === 'Invalid Resource') {
482 $result = array(
483 'status' => 'ERROR',
484 'text' => __( 'Email Address not valid', 'fv-player' ),
485 'error_log' => $result_data
486 );
487 } else {
488 $result = array(
489 'status' => 'ERROR',
490 'text' => 'Unknown Error 1. ',
491 'error_log'=> $result_data,
492 );
493 }
494 }elseif($result_data['status'] !== 'subscribed'){
495 $result = array(
496 'status' => 'ERROR',
497 'text' => 'Unknown Error 2.',
498 'error_log'=> $result_data,
499 );
500 }
501 return $result;
502 }
503
504 /**
505 * Sanitize subscriber request data and neutralize spreadsheet formula triggers.
506 *
507 * @param mixed $data Raw field value or array of values.
508 * @return mixed Sanitized field value or array of values.
509 */
510 private function sanitize_subscriber_data( $data ) {
511 if ( is_array( $data ) ) {
512 foreach ( $data as $key => $value ) {
513 $data[ $key ] = $this->sanitize_subscriber_data( $value );
514 }
515 return $data;
516 }
517
518 $data = sanitize_text_field( wp_unslash( $data ) );
519 $data = preg_replace( '/^[\s]*[=+\-@\t\r]+/', '', $data );
520 return $data;
521 }
522
523 /**
524 * Escape a CSV cell value to prevent formula injection in spreadsheet apps.
525 *
526 * @param string $value Field value.
527 * @return string Escaped field value.
528 */
529 private function escape_csv_field( $value ) {
530 $value = str_replace( '"', '', (string) $value );
531 if ( preg_match( '/^[\s]*[=+\-@\t\r]/', $value ) ) {
532 $value = "'" . $value;
533 }
534 return $value;
535 }
536
537 public function email_signup() {
538
539 if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'fv_player_email_signup' ) ) {
540 wp_send_json(
541 array(
542 'status' => 'ERROR',
543 'text' => __( 'Nonce verification error.', 'fv-player' ),
544 )
545 );
546 exit;
547 }
548
549 $list_id = isset( $_POST['list'] ) ? absint( $_POST['list'] ) : 0;
550
551 $data = $this->sanitize_subscriber_data( $_POST );
552
553 $aLists = get_option('fv_player_email_lists');
554
555 $list = isset($aLists[ $list_id ]) ? $aLists[ $list_id ] : array();
556
557 global $wpdb;
558 $table_name = $wpdb->prefix . 'fv_player_emails';
559 if( $wpdb->get_var( $wpdb->prepare( "SHOW TABLES LIKE %s", $table_name ) ) != $table_name ) {
560 $sql = "CREATE TABLE `$table_name` (
561 `id` INT(11) NOT NULL AUTO_INCREMENT,
562 `email` TEXT NULL,
563 `first_name` TEXT NULL,
564 `last_name` TEXT NULL,
565 `id_list` INT(11) NOT NULL,
566 `date` DATETIME NULL DEFAULT NULL,
567 `data` TEXT NULL,
568 `integration` TEXT NULL,
569 `integration_nice` TEXT NULL,
570 `status` TEXT NULL,
571 `error` TEXT NULL,
572 PRIMARY KEY (`id`)
573 )" . $wpdb->get_charset_collate() . ";";
574 require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
575 dbDelta($sql);
576 }
577
578 $result = array(
579 'status' => 'OK',
580 'text' => __( 'Thank You for subscribing.', 'fv-player' )
581 );
582
583 $integration_nice = '';
584
585 if(!empty($list['integration'])){
586 $aLists = get_option('fv_player_mailchimp_lists', array());
587 $integration_nice = $aLists[str_replace('mailchimp-','',$list['integration'])]['name'];
588 $result = $this->mailchimp_signup( str_replace('mailchimp-', '', $list['integration'] ), $data );
589 }
590
591 if ( empty($data['email']) || filter_var( sanitize_text_field( $data['email'] ), FILTER_VALIDATE_EMAIL) === false ){
592 $result['status'] = 'ERROR';
593 $result['text'] = __( 'Malformed Email Address.', 'fv-player' );
594 die( wp_json_encode( $result ) );
595 };
596
597 $count = $wpdb->get_var( $wpdb->prepare("SELECT COUNT(*) FROM ".$wpdb->prefix."fv_player_emails WHERE email = %s AND id_list = %s", $data['email'], intval( $list_id ) ) );
598
599 if(intval($count) === 0){
600 $wpdb->insert( $table_name, array(
601 'email' => sanitize_email( $data['email'] ),
602 'data' => '',
603 'id_list' => intval( $list_id ),
604 'date' => gmdate("Y-m-d H:i:s"),
605 'first_name' => isset( $data['first_name'] ) ? $data['first_name'] : '',
606 'last_name' => isset( $data['last_name'] ) ? $data['last_name'] : '',
607 'integration' => $list['integration'],
608 'integration_nice' => $integration_nice,
609 'status' => $result['status'],
610 'error' => $result['status'] === 'ERROR' ? serialize( $result['error_log'] ) : '',
611 ));
612
613 }elseif($result['status'] === 'OK'){
614 $result = array(
615 'status' => 'ERROR',
616 'text' => __( 'Email Address already subscribed.', 'fv-player' ),
617 );
618
619 // TODO: Does this ever run?
620 } else {
621 $wpdb->insert( $table_name, array(
622 'email' => sanitize_email( $data['email'] ),
623 'data' => '',
624 'id_list' => intval( $list_id ),
625 'date' => gmdate("Y-m-d H:i:s"),
626 'first_name' => isset( $data['first_name'] ) ? $data['first_name'] : '',
627 'last_name' => isset( $data['last_name'] ) ? $data['last_name'] : '',
628 'integration' => $list['integration'],
629 'integration_nice' => $integration_nice,
630 'status' => $result['status'],
631 'error' => $result['status'] === 'ERROR' ? serialize( $result['error_log'] ) : '',
632 ));
633 }
634
635 unset($result['error_log']);
636 die( wp_json_encode( $result ) );
637 }
638
639 function csv_export() {
640 if( isset($_GET['fv-email-export']) && !empty($_GET['page']) && sanitize_key( $_GET['page'] ) === 'fvplayer' && wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['nonce'] ) ), 'fv-email-export' ) ) {
641 $list_id = intval($_GET['fv-email-export']);
642 $aLists = get_option('fv_player_email_lists');
643 $list = $aLists[$list_id];
644 $filename = 'export-lists-' . (empty($list->title) ? $list_id : $list->title) . '-' . gmdate('Y-m-d') . '.csv';
645
646 header("Content-type: text/csv");
647 header("Content-Disposition: attachment; filename=$filename");
648 header("Pragma: no-cache");
649 header("Expires: 0");
650
651 global $wpdb;
652 $results = $wpdb->get_results('SELECT `email`, `first_name`, `last_name`, `date`, `integration`, `integration_nice`, `status`, `error` FROM `' . $wpdb->prefix . 'fv_player_emails` WHERE `id_list` = "' . intval($list_id) . '"');
653
654 echo 'email,first_name,last_name,date,integration,status,error'."\n";
655 if( $results ) {
656 foreach ($results as $row){
657 if(!empty($row->integration)){
658 $row->integration .= ': '.$row->integration_nice;
659 }
660 unset($row->integration_nice);
661
662 if(!empty($row->error)){
663 $tmp = unserialize($row->error);
664 $row->error = $tmp['title'];
665 }
666
667
668 $row_values = array_map( array( $this, 'escape_csv_field' ), array_values( (array) $row ) );
669 echo '"' . implode( '","', $row_values ) . "\"\n";
670 }
671 }
672 die;
673 }
674 }
675
676
677 public function admin_export_screen(){
678 if( isset($_GET['fv-email-export-screen']) && !empty($_GET['page']) && sanitize_key( $_GET['page'] ) === 'fvplayer' && wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['nonce'] ) ), 'fv-email-show' ) ) {
679
680 $list_id = intval($_GET['fv-email-export-screen']);
681
682 global $wpdb;
683 $results = $wpdb->get_results( $wpdb->prepare( "SELECT `email`, `first_name`, `last_name`, `date`, `integration`, `integration_nice`, `status`, `error` FROM {$wpdb->prefix}fv_player_emails WHERE id_list = %d LIMIT 10", $list_id ) );
684
685 ?>
686 <style>
687 #adminmenumain { display: none }
688 #wpcontent { margin-left: 0 }
689 </style>
690
691 <table class="wp-list-table widefat fixed striped posts">
692 <thead>
693 <tr>
694 <th scope="col" class="manage-column">E-mail</th>
695 <th scope="col" class="manage-column">First Name</th>
696 <th scope="col" class="manage-column">Last Name</th>
697 <th scope="col" class="manage-column">Date</th>
698 <th scope="col" class="manage-column">Integration</th>
699 <th scope="col" class="manage-column">Status</th>
700 <th scope="col" class="manage-column">Error</th>
701 </tr>
702 </thead>
703 <tbody>
704 <?php
705 foreach ($results as $row){
706 echo '<tr>';
707 foreach ($row as $key => $item) {
708 if($key === 'integration' && !empty($item)){
709 $item .= ': ' . $row->integration_nice;
710 }elseif($key === 'integration_nice'){
711 continue;
712 }elseif($key === 'error'){
713 $item = '';
714 if( !empty($item) ) {
715 $tmp = unserialize($item);
716 $item = $tmp['title'];
717 }
718 }
719 echo '<td>' . wp_strip_all_tags( $item ) . '</td>';
720 }
721 echo '</tr>';
722 }
723 ?>
724 </tbody>
725 </table>
726 <p>
727 <a class='fv-player-list-export button' href='<?php echo wp_nonce_url( admin_url('admin.php?page=fvplayer&fv-email-export=' . intval( $key ) ), 'fv-email-export', 'nonce' ); ?>' target="_blank" ><?php esc_attr_e( 'Download CSV', 'fv-player' ); ?></a>
728 </p>
729
730 <?php
731
732 die();
733 }
734 }
735
736
737 public function save_settings() {
738 check_ajax_referer('fv_player_email_subscription_save');
739
740 $aLists = get_option('fv_player_email_lists',array());
741 $key = intval($_POST['key']);
742
743 if( !isset($_POST['email_lists'][$key]) ) {
744 header('HTTP/1.0 403 Forbidden');
745 die();
746 }
747
748 foreach ( $_POST['email_lists'] as $index => $values) {
749 foreach ($values as $key => $value) {
750 $aLists[$index][$key] = sanitize_text_field( $value );
751 }
752 }
753 update_option('fv_player_email_lists',$aLists);
754
755 fv_player_admin_page();
756 }
757
758
759 public function popup_preview( $aAttributes ) {
760 global $fv_fp;
761 $aArgs = func_get_args();
762 if( isset($aArgs[2]->aCurArgs['end_popup_preview']) && $aArgs[2]->aCurArgs['end_popup_preview'] ) {
763 $aAttributes['data-end_popup_preview'] = true;
764 }
765 return $aAttributes;
766 }
767
768 }
769
770 global $FV_Player_Email_Subscription;
771 $FV_Player_Email_Subscription = new FV_Player_Email_Subscription();
772