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