PluginProbe
GetResponse Forms by Optin Cat / 1.3.9
GetResponse Forms by Optin Cat v1.3.9
1.3.4 1.3.5 1.3.6 1.3.8 1.3.9 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 1.5.2 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6.1 1.6.2 1.6.3 1.7.0 1.7.1 1.7.2 1.8.0 1.8.1 2.0.0 All 36 releases
getresponse / includes / eoi-post-types.php

eoi-post-types.php in GetResponse Forms by Optin Cat 1.3.9, at includes/eoi-post-types.php

2,753 lines 85.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class EasyOptInsPostTypes {
4
5 public $settings;
6
7 private $activity_day_interval = array(
8 'form_list' => null,
9 'dashboard_widget' => 30
10 );
11
12 private $targeting_cat_path = 'assets/vendor/targeting-cat/TargetingCat-OptinCat-1.2.min.js';
13
14 private $two_step_ids_on_page = array();
15
16 public function __construct( $settings ) {
17
18 $this->settings = $settings;
19
20 $providers_available = array_keys( $this->settings[ 'providers' ] );
21
22 // Register custom post type
23 add_action( 'init', array( $this, 'register_custom_post_type' ) );
24 add_filter( 'manage_easy-opt-ins_posts_columns', array( $this, 'add_new_columns' ) );
25 add_action( 'manage_easy-opt-ins_posts_custom_column', array( $this, 'set_column_data' ), 10, 2 );
26 add_filter( 'post_row_actions', array( $this, 'post_row_actions' ), 10, 2 );
27
28 // Reset action
29 add_action( 'admin_post_fca_eoi_reset_stats', array( $this, 'reset_stats' ) );
30
31 // Dashboard widget
32 add_action( 'wp_dashboard_setup', array( $this, 'dashboard_setup' ) );
33
34 // Add provider object and post settings to settings
35 add_action( 'init', array( $this, 'more_settings' ) );
36
37 // Handle AJAX submission (unused)
38 add_action( 'init', array( $this, 'handle_submission' ) );
39
40 // Initiate action hooks
41 add_action( 'save_post', array( $this, 'save_meta_box_content' ), 1, 2 );
42
43 // Live preview
44 add_filter( 'the_content', array( $this, 'live_preview' ) );
45
46 // Scripts and styles
47 add_filter( 'admin_enqueue_scripts', array( $this, 'admin_enqueue' ) );
48 add_filter( 'admin_enqueue_scripts', array( $this, 'disable_autosave' ) );
49
50 add_action( 'admin_head', array( $this, 'hide_minor_publishing' ) );
51
52 add_filter( 'admin_body_class', array( $this, 'add_body_class' ) );
53
54 add_filter( 'wp_insert_post_data', array( $this, 'force_published' ) );
55
56 // add_action( 'admin_footer', array( $this, 'disable_metabox_toggle' ) );
57
58 add_action( 'wp_ajax_fca_eoi_subscribe', array( $this, 'ajax_subscribe' ) );
59
60 add_filter( 'get_user_option_screen_layout_easy-opt-ins', array( $this, 'force_one_column' ) );
61
62 add_filter( 'get_user_option_meta-box-order_easy-opt-ins', array( $this, 'order_columns' ) );
63
64 add_filter( 'gettext', array( $this, 'override_text' ) );
65
66 add_filter( 'bulk_actions-edit-easy-opt-ins', array( $this, 'disable_bulk_edit' ) );
67
68 add_filter( 'post_row_actions', array( $this, 'remove_quick_edit' ) );
69
70 add_action( 'admin_notices', array( $this, 'admin_notices' ) );
71
72 add_filter( 'enter_title_here', array( $this, 'change_default_title' ) );
73
74 add_filter( 'init', array( $this, 'bind_content_filter' ), 10 );
75
76 add_filter( 'template_redirect', array( $this, 'parse_tc_condition_request' ), 1 );
77
78 add_filter( 'the_content', array( $this, 'scan_for_shortcodes' ) );
79
80 add_filter( 'init', array( $this, 'request_prepare_lightbox' ) );
81
82 add_filter( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
83
84 add_filter( 'wp_footer', array( $this, 'show_lightbox' ) );
85
86 foreach ( $providers_available as $provider ) {
87 add_action( 'wp_ajax_fca_eoi_' . $provider . '_get_lists', $provider . '_ajax_get_lists' );
88 }
89
90 // Hook provder callback functions
91 foreach ( $providers_available as $provider ) {
92 add_filter( 'fca_eoi_alter_admin_notices', $provider . '_admin_notices', 10, 1 );
93 }
94
95 // Handle licensing
96 if( count( $providers_available ) > 1 ) {
97 require $this->settings[ 'plugin_dir' ] . 'includes/licensing.php';
98 new EasyOptInsLicense( $this->settings );
99 }
100 }
101
102 public function enqueue_scripts() {
103 wp_enqueue_script( 'jquery' );
104 }
105
106 public function more_settings() {
107
108 $providers_available = array_keys( $this->settings[ 'providers' ] );
109
110 // Get the post id
111 if( is_admin() ) {
112 $form_id = K::get_var( 'post', $_GET );
113 } else {
114 $protocol = is_ssl() ? 'https' : 'http';
115 $form_id = K::get_var( 'fca_eoi_form_id', $_POST );
116 }
117
118 // Save form meta into object settings
119 $this->settings[ 'eoi_form_meta' ] = empty ( $form_id )
120 ? false
121 : get_post_meta( $form_id, 'fca_eoi', true )
122 ;
123
124 // Add last 3 posts and there meta
125 // We need to prepare the previous posts
126 $fca_eoi_last_3_forms = array();
127 foreach (query_posts( 'posts_per_page=3&post_type=easy-opt-ins' ) as $i => $f ) {
128 $fca_eoi_last_3_forms[ $i ][ 'post' ] = $f;
129 $fca_eoi_last_3_forms[ $i ][ 'fca_eoi' ] = get_post_meta( $f->ID, 'fca_eoi', true );
130 }
131 // reset query after the loop
132 wp_reset_query();
133 $this->settings[ 'fca_eoi_last_3_forms' ] = $fca_eoi_last_3_forms;
134 }
135
136 public function register_custom_post_type() {
137
138 $labels = array(
139 'name' => __('Optin Forms') ,
140 'singular_name' => __('Optin Form') ,
141 'add_new' => __('Add New') ,
142 'add_new_item' => __('Add New Optin Form') ,
143 'edit_item' => __('Edit Optin Form') ,
144 'new_item' => __('New Optin Form') ,
145 'all_items' => __('All Optin Forms') ,
146 'view_item' => __('View Optin Form') ,
147 'search_items' => __('Search Optin Form') ,
148 'not_found' => __('No Optin Form Found') ,
149 'not_found_in_trash' => __('No Optin Form Found in Trash') ,
150 'parent_item_colon' => '',
151 'menu_name' => __('Optin Cat')
152 );
153 $args = array(
154 'menu_icon' => "{$this->settings['plugin_url']}/icon.png",
155 'labels' => $labels,
156 'public' => false,
157 'exclude_from_search' => true,
158 'publicly_queryable' => true,
159 'show_ui' => true,
160 'show_in_menu' => true,
161 'query_var' => true,
162 'rewrite' => array(
163 'slug' => 'easy-opt-ins',
164 ) ,
165 'capability_type' => 'post',
166 'has_archive' => false,
167 'hierarchical' => false,
168 'menu_position' => 105,
169 'supports' => array(
170 'title',
171 ) ,
172 'register_meta_box_cb' => array(
173 $this,
174 'add_meta_boxes'
175 )
176 );
177 register_post_type('easy-opt-ins', $args);
178 }
179
180 private function enqueue_activity_style() {
181 wp_enqueue_style( 'admin-cpt-easy-opt-ins-activity', $this->settings['plugin_url'] . '/assets/admin/cpt-easy-opt-ins-activity.css' );
182 }
183
184 public function add_new_columns( $columns ) {
185 $new_columns = array();
186
187 if ( ! empty( $columns['cb'] ) ) {
188 $new_columns['cb'] = $columns['cb'];
189 }
190
191 if ( ! empty( $columns['title'] ) ) {
192 $new_columns['title'] = $columns['title'];
193 }
194
195 $this->enqueue_activity_style();
196 $activity = EasyOptInsActivity::get_instance();
197
198 $period =
199 '<span class="fca_eoi_activity_period">(' .
200 $activity->get_text( 'period', null, array( $this->activity_day_interval['form_list'] ) ) .
201 ')</span>';
202
203 foreach ( array( 'impressions', 'conversions', 'conversion_rate' ) as $activity_type ) {
204 $new_columns[ $activity_type ] = esc_html( $activity->get_text( $activity_type, 'form' ) ) . '<br>' . $period;
205 }
206
207 return $new_columns;
208 }
209
210 public function set_column_data( $column_name, $form_id ) {
211 $activity = EasyOptInsActivity::get_instance();
212
213 $stats = $activity->get_form_stats( $this->activity_day_interval['form_list'] );
214 $value = 0;
215
216 if ( ! empty( $stats[ $column_name ][ $form_id ] ) ) {
217 $value = $stats[ $column_name ][ $form_id ];
218 }
219
220 echo $activity->format_column_text( $column_name, $value );
221 }
222
223 public function post_row_actions( $actions, $post ) {
224 if ( $post->post_type == 'easy-opt-ins' ) {
225 $action = 'fca_eoi_reset_stats';
226 $title = __( 'Reset stats for this item' );
227 $label = __( 'Reset Stats' );
228
229 $url = add_query_arg( 'action', $action, admin_url( 'admin-post.php?post=' . $post->ID ) );
230 $url = wp_nonce_url( $url );
231
232 $actions[$action] = $this->confirm_tag(
233 '<a href="' . $url . '" title="' . $title . '">' . $label . '</a>',
234 __( 'Are you sure?' ),
235 __( 'Do you really want to reset this Optin Form\'s stats? This action cannot be undone.' )
236 );
237 }
238 return $actions;
239 }
240
241 public function reset_stats() {
242 if ( wp_verify_nonce( $_REQUEST['_wpnonce'] ) ) {
243 EasyOptInsActivity::get_instance()->reset_stats( (int) $_REQUEST['post'] );
244 wp_redirect( wp_get_referer() );
245 }
246 }
247
248 private function confirm_tag( $tag, $title, $message ) {
249 return preg_replace(
250 '/>/',
251 ' onclick="return confirm(' .
252 esc_html( '"' . $title . '\n\n' . $message . '"' ) .
253 ')">',
254 $tag, 1 );
255 }
256
257 public function dashboard_setup() {
258 add_meta_box(
259 'fca_eoi_dashboard_widget',
260 'Optin Cat Summary',
261 array( $this, 'add_dashboard_widget' ),
262 'dashboard',
263 'normal',
264 'high'
265 );
266 }
267
268 public function add_dashboard_widget() {
269 wp_enqueue_script( 'd3_js', $this->settings['plugin_url'] . '/assets/vendor/nvd3/d3.min.js' );
270 wp_enqueue_script( 'nvd3_js', $this->settings['plugin_url'] . '/assets/vendor/nvd3/nv.d3.min.js' );
271 wp_enqueue_style( 'nvd3_css', $this->settings['plugin_url'] . '/assets/vendor/nvd3/nv.d3.min.css' );
272 $this->enqueue_activity_style();
273
274 $day_interval = $this->activity_day_interval['dashboard_widget'];
275 $activity = EasyOptInsActivity::get_instance();
276 $stats = $activity->get_daily_stats( $day_interval );
277
278 $date_labels = array();
279 foreach ( array_keys( $stats['impressions'] ) as $date ) {
280 $date_labels[] = strftime( '%e %b', strtotime( $date ) );
281 }
282
283 $colors = array(
284 'impressions' => '#5b90bf',
285 'conversions' => '#bf616a'
286 );
287
288 ?>
289 <div class="fca_eoi_activity_chart_title_container">
290 <div class="fca_eoi_activity_chart_legend">
291 <?php foreach ( array( 'impressions', 'conversions' ) as $activity_type ): ?>
292 <div class="fca_eoi_activity_chart_legend_item">
293 <div class="fca_eoi_activity_chart_legend_sample" style="background-color: <?php echo $colors[ $activity_type ] ?>;"></div>
294 <div class="fca_eoi_activity_chart_legend_text">
295 <?php echo esc_html( $activity->get_text( $activity_type, 'total' ) ) ?>
296 </div>
297 </div>
298 <?php endforeach ?>
299 </div>
300 <div class="fca_eoi_activity_chart_period">
301 <?php echo esc_html( $activity->get_text( 'period', null, array( $day_interval ) ) ) ?>
302 -
303 <a href="<?php echo admin_url( 'edit.php?post_type=easy-opt-ins' ) ?>"><?php echo __( 'View All Data' ) ?></a>
304 </div>
305 </div>
306 <div class="fca_eoi_activity_chart" id="fca_eoi_activity_chart"></div>
307 <div class="fca_eoi_activity_chart_stat">
308 <?php foreach ( array( 'impressions', 'conversions', 'conversion_rate' ) as $activity_type ): ?>
309 <div class="fca_eoi_activity_chart_stat_item">
310 <div class="fca_eoi_activity_chart_stat_value">
311 <?php echo $activity->format_column_text( $activity_type, $stats['totals'][ $activity_type ] ) ?>
312 </div>
313 <div class="fca_eoi_activity_chart_stat_title">
314 <?php echo esc_html( $activity->get_text( $activity_type, 'total' ) ) ?>
315 </div>
316 </div>
317 <?php endforeach ?>
318 </div>
319 <script>
320 jQuery( function() {
321 var impressions = <?php echo json_encode( array_values( $stats['impressions'] ) ) ?>;
322 var conversions = <?php echo json_encode( array_values( $stats['conversions'] ) ) ?>;
323 var dates = <?php echo json_encode( $date_labels ) ?>;
324
325 var chart = nv.models.lineChart().options({
326 duration: 0,
327 transitionDuration: 0,
328 useInteractiveGuideline: true,
329 isArea: true,
330 showLegend: false,
331 margin: { top: 10, right: 20, bottom: 30, left: 40 }
332 } );
333
334 chart.xAxis.tickFormat( function( index ) { return dates[ index ]; } );
335 chart.yAxis.tickFormat( d3.format( 'd' ) );
336 chart.forceY( [ 0, d3.max(impressions) || 1 ] );
337
338 var valuesToPoint = function( value, index ) {
339 return { x: index, y: value };
340 };
341
342 d3.select( '#fca_eoi_activity_chart' ).append( 'svg' ).datum( [
343 { color: '<?php echo $colors['impressions'] ?>', key: 'Impressions', values: impressions.map(valuesToPoint) },
344 { color: '<?php echo $colors['conversions'] ?>', key: 'Conversions', values: conversions.map(valuesToPoint) }
345 ] ).call( chart );
346
347 nv.utils.windowResize( chart.update );
348 } );
349 </script>
350 <?php
351 }
352
353 public function add_meta_boxes() {
354 add_meta_box(
355 'fca_eoi_meta_box_nav',
356 __( 'Navigation' ),
357 array( &$this, 'meta_box_content_nav' ),
358 'easy-opt-ins',
359 'side',
360 'high'
361 );
362 add_meta_box(
363 'fca_eoi_meta_box_setup',
364 __( 'Setup' ),
365 array( &$this, 'meta_box_content_setup' ),
366 'easy-opt-ins',
367 'side',
368 'high'
369 );
370 add_meta_box(
371 'fca_eoi_meta_box_build',
372 __( 'Form Builder' ),
373 array( &$this, 'meta_box_content_build' ),
374 'easy-opt-ins',
375 'side',
376 'high'
377 );
378 add_meta_box(
379 'fca_eoi_meta_box_provider',
380 __( 'Email Marketing Provider Integration' ),
381 array( &$this, 'meta_box_content_provider' ),
382 'easy-opt-ins',
383 'side',
384 'high'
385 );
386 add_meta_box(
387 'fca_eoi_meta_box_publish',
388 __( 'Publication' ),
389 array( &$this, 'meta_box_content_publish' ),
390 'easy-opt-ins',
391 'side',
392 'high'
393 );
394 add_meta_box(
395 'fca_eoi_meta_box_thanks',
396 __( 'Thank You Page' ),
397 array( &$this, 'meta_box_content_thanks' ),
398 'easy-opt-ins',
399 'side',
400 'high'
401 );
402 if ( has_action( 'fca_eoi_powerups' ) ) {
403 add_meta_box(
404 'fca_eoi_meta_box_powerups',
405 __( 'Power Ups' ),
406 array( &$this, 'meta_box_content_powerups' ),
407 'easy-opt-ins',
408 'side',
409 'high'
410 );
411 }
412 if ( FCA_EOI_DEBUG ) {
413 add_meta_box(
414 'fca_eoi_meta_box_debug',
415 'Debug',
416 array( &$this, 'meta_box_content_debug' ),
417 'easy-opt-ins',
418 'side',
419 'high'
420 );
421 }
422 }
423
424 public function meta_box_content_nav() {
425 ?>
426 <h2 class="nav-tab-wrapper" style="padding: 0 10px">
427 <a href="#fca_eoi_meta_box_setup" class="nav-tab nav-tab-active">Choose</a>
428 <a href="#fca_eoi_meta_box_build" class="nav-tab ">Build</a>
429 <?php if( FCA_EOI_DEBUG ) : ?>
430 <a href="#fca_eoi_meta_box_debug" class="nav-tab ">Debug</a>
431 <?php endif; ?>
432 </h2>
433 <?php
434 }
435
436 public function meta_box_content_setup() {
437
438 global $post;
439
440 $layouts_types_labels = array(
441 'widget' => 'Sidebar Widgets',
442 'postbox' => 'Post Boxes',
443 'lightbox' => 'Popups',
444 );
445
446 $fca_eoi = get_post_meta( $post->ID, 'fca_eoi', true );
447
448 echo '<h2>' . __( 'Layouts' ) . '</h2>';
449 echo '<script id="fca_eoi_texts" type="application/json">{ "headline_copy": "Headline Copy", "description_copy": "Description Copy", "name_placeholder": "Name Placeholder", "email_placeholder": "Email Placeholder", "button_copy": "Button Copy", "privacy_copy": "Privacy Copy" }</script>';
450
451 // Build the layouts array
452 $layouts = $layouts_types = $layouts_types_found = array();
453 foreach ( glob( $this->settings[ 'plugin_dir' ] . 'layouts/*', GLOB_ONLYDIR ) as $v) {
454 $layouts_types_found[] = basename( $v );
455 }
456
457 $layouts_types_accepted = array_keys( $layouts_types_labels );
458 foreach ( $layouts_types_accepted as $layout_type ) {
459 if ( in_array( $layout_type, $layouts_types_found ) ) {
460 $layouts_types[] = $layout_type;
461 }
462 }
463
464 // Layouts types mini-tabs
465 echo '<ul class="category-tabs" id="layouts_types_tabs">';
466 foreach ( $layouts_types as $layout_type ) {
467 K::wrap(
468 $layouts_types_labels[ $layout_type ]
469 , array(
470 'href' => '#'. 'layouts_type_' . $layout_type,
471 )
472 , array(
473 'html_before' => '<li' . ( 'widget' === $layout_type ? ' class="tabs"' : '' ) . ' >',
474 'html_after' => '</li> ',
475 'in' => 'a',
476 )
477 );
478 }
479 echo '</ul>';
480
481 // Layout types
482 foreach ( $layouts_types as $layout_type ) {
483 echo '<div id="layouts_type_' . $layout_type . '">';
484 foreach ( glob( $this->settings[ 'plugin_dir' ] . "layouts/$layout_type/*", GLOB_ONLYDIR ) as $layout_path ) {
485 // Grab layout details
486 $layout_id = basename( $layout_path );
487
488 $layout_helper = new EasyOptInsLayout( $layout_id );
489 $layout_type = $layout_helper->layout_type;
490 $php_path = $layout_helper->path_to_resource( 'layout', 'php' );
491 $html_path = $layout_helper->path_to_resource( 'layout', 'html' );
492 $html_wrap_path = $layout_helper->path_to_html_wrapper();
493 $scss_path = $layout_helper->path_to_resource( 'layout', 'scss' );
494 $screenshot_path = $layout_helper->path_to_resource( 'screenshot', 'png' );
495 $screenshot_url = $layout_helper->url_to_resource( 'screenshot', 'png' );
496
497 include $php_path;
498 if ( EasyOptInsLayout::uses_new_css() ) {
499 $layout[ 'css' ] = file_exists( $scss_path )
500 ? '#fca_eoi_preview_form_container {' .
501 file_get_contents( $scss_path ) .
502 '.fca_eoi_layout_popup_close {' .
503 'display: none;' .
504 '}' .
505 '}'
506 : ''
507 ;
508 } else {
509 $layout[ 'css' ] = file_exists( $scss_path )
510 ? file_get_contents( $scss_path)
511 : ''
512 ;
513 }
514
515 // Add $ltr SASS variable
516 $layout[ 'css' ] = sprintf( '$ltr: %s;', is_rtl() ? 'false' : 'true' )
517 . $layout[ 'css' ]
518 ;
519 $scss = $layout_helper->new_scss_compiler();
520 $layout[ 'css' ] = $scss->compile( $layout[ 'css' ] );
521 if ( EasyOptInsLayout::uses_new_css() ) {
522 $layout[ 'template' ] = str_replace(
523 '{{{layout}}}',
524 file_get_contents( $html_path ),
525 file_get_contents( $html_wrap_path )
526 );
527 } else {
528 $layout[ 'template' ] = file_get_contents( $html_path );
529 }
530 $layout[ 'template' ] = str_replace(
531 array(
532 '<form',
533 '/form',
534 '{{{description_copy}}}',
535 '{{{headline_copy}}}',
536 '{{{name_field}}}',
537 '{{{email_field}}}',
538 '{{{submit_button}}}',
539 '{{{privacy_copy}}}',
540 '{{{fatcatapps_link}}}'
541 )
542 , array(
543 EasyOptInsLayout::uses_new_css()
544 ? '<div id="fca_eoi_preview_form_container">' .
545 '<div id="fca_eoi_preview_form_wrapper">' .
546 '<div id="fca_eoi_preview_form" class="' .
547 'fca_eoi_layout_' . $layout_helper->layout_number . ' ' .
548 $layout_helper->layout_class .
549 '"'
550 : '<div id="fca_eoi_preview_form" class="fca_eoi_' . $layout_id . '"',
551 EasyOptInsLayout::uses_new_css()
552 ? '/div></div></div'
553 : '/div',
554 '<div data-fca-eoi-fieldset-id ="description">{{{description_copy}}}</div>',
555 EasyOptInsLayout::uses_new_css()
556 ? '<div data-fca-eoi-fieldset-id ="headline" id="fca_eoi_preview_headline_copy">{{{headline_copy}}}</div>'
557 : '<span data-fca-eoi-fieldset-id ="headline" id="fca_eoi_preview_headline_copy">{{{headline_copy}}}</span>',
558 EasyOptInsLayout::uses_new_css()
559 ? '<input class="fca_eoi_form_input_element" type="text" placeholder="{{{name_placeholder}}}">'
560 : '<input data-fca-eoi-fieldset-id ="name_field" type="text" placeholder="{{{name_placeholder}}}" />',
561 EasyOptInsLayout::uses_new_css()
562 ? '<input class="fca_eoi_form_input_element" type="email" placeholder="{{{email_placeholder}}}">'
563 : '<input data-fca-eoi-fieldset-id ="email_field" type="email" placeholder="{{{email_placeholder}}}" />',
564 EasyOptInsLayout::uses_new_css()
565 ? '<input class="fca_eoi_form_button_element" data-fca-eoi-fieldset-id ="button" type="submit" value="{{{button_copy}}}">'
566 : '<input data-fca-eoi-fieldset-id ="button" type="submit" value="{{{button_copy}}}" />',
567 EasyOptInsLayout::uses_new_css()
568 ? '<div data-fca-eoi-fieldset-id="privacy">{{{privacy_copy}}}</div>'
569 : '<span data-fca-eoi-fieldset-id ="privacy">{{{privacy_copy}}}</span>',
570 EasyOptInsLayout::uses_new_css()
571 ? '{{#show_fatcatapps_link}}<div class="fca_eoi_layout_fatcatapps_link_wrapper fca_eoi_form_text_element"><span data-fca-eoi-fieldset-id ="fatcatapps"><a href="#" onclick="javascript:return false">Powered by Optin Cat</a></span></div>{{/show_fatcatapps_link}}'
572 : '{{#show_fatcatapps_link}}<p class="fca_eoi_' . $layout_id . '_fatcatapps_link_wrapper"><span data-fca-eoi-fieldset-id ="fatcatapps"><a href="#" onclick="javascript:return false">Powered by Optin Cat</a></span></p>{{/show_fatcatapps_link}}',
573 )
574 , $layout[ 'template' ]
575 );
576 $layout[ 'template' ] .= sprintf( '<style>%s</style>', $layout[ 'css' ] );
577 $layouts[ $layout_id ] = $layout;
578 // Output the layout image and hidden template
579 $layout_output_tpl = '
580 <div
581 class="fca_eoi_layout has-tip"
582 data-layout-id=":id" data-layout-type=":type"
583 >
584 <img src=":src" />
585 <h3>:name</h3>
586 <script id="fca_eoi_tpl_:id" type="x-tmpl-mustache">:template</script>
587 <script id="fca_eoi_editables_:id" type="application/json">:editables</script>
588 <script id="fca_eoi_texts_:id" type="application/json">:texts</script>
589 </div>
590 ';
591 $layout_output = str_replace(
592 array(
593 ':id',
594 ':type',
595 ':name',
596 ':src',
597 ':template',
598 ':editables',
599 ':texts',
600 ),
601 array(
602 $layout_id,
603 $layout_type,
604 $layout[ 'name' ],
605 file_exists( $screenshot_path )
606 ? $screenshot_url
607 : $this->settings[ 'plugin_url' ] . '/layouts/no-image.jpg'
608 ,
609 $layout[ 'template' ],
610 ! empty( $layout[ 'editables' ] )
611 ? json_encode( $layout[ 'editables' ] )
612 : 'null'
613 ,
614 ! empty( $layout[ 'texts' ] )
615 ? json_encode( $layout[ 'texts' ] )
616 : 'null'
617 ,
618 ),
619 $layout_output_tpl
620 );
621
622 // Add autocolors
623 if( ! empty( $layout[ 'autocolors' ] ) ) {
624 foreach ($layout[ 'autocolors' ] as $autocolor ) {
625 $layout_output .= '
626 <script>
627 jQuery( document ).ready( function( $ ) {
628
629 var source = "[name*=\'' . $autocolor[ 'source' ] . '\']";
630 var destination = "[name*=\'' . $autocolor[ 'destination' ] . '\']";
631
632 $( destination ).closest( "p" ).hide();
633
634 $( document ).on( "change", source, function() {
635 var v = $(this).val();
636 if( ! v ) {
637 $( destination ).val( "" );
638 return ;
639 }
640
641 var c = tinycolor( v );'
642 ;
643 foreach ( $autocolor[ 'operations'] as $op => $val ) {
644 $layout_output .= '
645 c = c.' . $op. '( ' . 1*$val . ' );
646 ';
647 }
648 $layout_output .= '
649 $( destination ).val( c.toString() );
650 $( destination ).blur();
651 } );
652 } );
653 </script>'
654 ;
655 }
656 }
657
658 echo $layout_output;
659 }
660 echo '</div>';
661 }
662 echo '<br clear="all"/>';
663
664 // Prepare layouts array (id->name) for K
665 $layouts_array = array();
666 foreach ( $layouts as $layout_id => $layout ) {
667 $layouts_array[ $layout_id ] = $layout[ 'name' ];
668 }
669
670 // Print hidden select box, it will be controlled by images (UI)
671 K::select(
672 'fca_eoi[layout]',
673 array(
674 'class' => 'hidden',
675 'id' => 'fca_eoi_layout_select',
676 ),
677 array(
678 'options' => $layouts_array,
679 'selected' => K::get_var( 'layout', $fca_eoi ),
680 )
681 );
682
683 // Prepare the properties fields templates
684 $property_templates[ 'color' ] = K::input( '{{property_name}}',
685 array(
686 'class' => sprintf(
687 "color { hash: true, caps: false, required: false, pickerPosition: '%s' }"
688 , is_rtl() ? 'right' : 'left'
689 ),
690 'value' => '{{property_value}}',
691 ),
692 array(
693 'format' => '<p class="clear"><label><span class="control-title">{{property_label}}</span><br />:input <a href="#transparent">None</a></label></p>',
694 'nocolorpicker' => true,
695 'return' => true,
696 )
697 );
698 $property_templates[ 'icon' ] = K::input( '{{property_name}}',
699 array(
700 'data-value-unchecked' => '{{property_value_unchecked}}',
701 'data-is-checked' => '{{property_is_checked}}',
702 'type' => 'checkbox',
703 'value' => '{{property_value}}',
704 ),
705 array(
706 'format' => '<p class="control-property-icon"><label class="fca_eoi_toggle">{{{icon}}} :input</label></p>',
707 'return' => true,
708 )
709 );
710 $property_templates[ 'font-size' ] = K::select( '{{property_name}}',
711 array(
712 'data-selected' => '{{selected}}',
713 ),
714 array(
715 'format' => '<p class="clear"><label><span class="control-title">{{property_label}}</span><br />:select</label></p>',
716 'options' => array(
717 'none' => '',
718 '7px' => '7px',
719 '8px' => '8px',
720 '9px' => '9px',
721 '10px' => '10px',
722 '11px' => '11px',
723 '12px' => '12px',
724 '13px' => '13px',
725 '14px' => '14px',
726 '15px' => '15px',
727 '16px' => '16px',
728 '17px' => '17px',
729 '18px' => '18px',
730 '19px' => '19px',
731 '20px' => '20px',
732 '21px' => '21px',
733 '22px' => '22px',
734 '23px' => '23px',
735 '24px' => '24px',
736 '25px' => '25px',
737 '26px' => '26px',
738 '27px' => '27px',
739 '28px' => '28px',
740 '29px' => '29px',
741 '30px' => '30px',
742 '31px' => '31px',
743 '32px' => '32px',
744 '33px' => '33px',
745 '34px' => '34px',
746 '35px' => '35px',
747 '36px' => '36px',
748 ),
749 'selected' => 'none',
750 'return' => true,
751 )
752 );
753
754 // Print the proprties fields templates
755 foreach ( $property_templates as $prop => $property_template) {
756 echo '<script id="fca_eoi_property_' . $prop . '" type="x-tmpl-mustache">' . $property_template . '</script>';
757 }
758
759 // Print a copy of the current settings
760 echo
761 '<script id="fca_eoi_post_meta" type="application/json">'
762 . ( $fca_eoi ? json_encode( $fca_eoi ) : '{}' )
763 . '</script>'
764 ;
765 }
766
767 public function meta_box_content_provider() {
768
769 global $post;
770 $fca_eoi = get_post_meta( $post->ID, 'fca_eoi', true );
771 $providers_available = array_keys( $this->settings[ 'providers' ] );
772 $providers_options = array();
773 $screen = get_current_screen();
774 $fca_eoi_last_3_forms = $this->settings[ 'fca_eoi_last_3_forms' ];
775
776 // @todo: remove
777 // Hack for mailchimp upgrade
778 $fca_eoi[ 'mailchimp_list_id' ] = K::get_var(
779 'mailchimp_list_id'
780 , $fca_eoi
781 , K::get_var( 'list_id' , $fca_eoi )
782 );
783 if( K::get_var( 'list_id' , $fca_eoi ) ) {
784 $fca_eoi[ 'provider' ] = 'mailchimp';
785 }
786 // End of hack
787 // Hack for campaignmonitor upgrade
788 $fca_eoi[ 'campaignmonitor_list_id' ] = K::get_var(
789 'campaignmonitor_list_id'
790 , $fca_eoi
791 , K::get_var( 'list_id' , $fca_eoi )
792 );
793 if( strlen( K::get_var( 'campaignmonitor_list_id' , $fca_eoi ) ) == 32){
794 $fca_eoi[ 'provider' ] = 'campaignmonitor';
795 }
796 // End of hack
797 // Hack for getresponse upgrade
798 $fca_eoi[ 'getresponse_list_id' ] = K::get_var(
799 'getresponse_list_id'
800 , $fca_eoi
801 , K::get_var( 'list_id' , $fca_eoi )
802 );
803 if( K::get_var( 'list_id' , $fca_eoi ) ) {
804 $fca_eoi[ 'provider' ] = 'getresponse';
805 }
806 // End of hack
807
808 // Prepare providers options
809 foreach ($this->settings[ 'providers' ] as $provider_id => $provider ) {
810 $providers_options[ $provider_id ] = $provider[ 'info' ][ 'name' ];
811 }
812
813 // Provider choice if there are many providers
814 if ( 1 < count( $providers_available) ) {
815
816 $last_date = 0;
817 $provider = 'mailchimp'; // use mailchimp by default
818 foreach ( $fca_eoi_last_3_forms as $form ) {
819 if ( ! empty( $form['fca_eoi'] ) ) {
820 $post_date = strtotime( $form['post']->post_date );
821
822 foreach ( array_keys( $form['fca_eoi'] ) as $key ) {
823 $pos = strpos( $key, '_list_id' );
824 if ( $pos !== false && $post_date > $last_date ) {
825 $provider = substr( $key, 0, $pos );
826 $last_date = $post_date;
827 }
828 }
829 }
830 }
831
832 K::select( 'fca_eoi[provider]'
833 , array(
834 'class' => 'select2',
835 'style' => 'width: 27em;',
836 )
837 , array(
838 'format' => '<p><label>:select</label></p>',
839 'options' => array( '' => 'Not set' ) + $providers_options,
840 'selected' => K::get_var( 'provider', $fca_eoi, $provider ),
841 )
842 );
843 }
844
845 $providers_available = array_keys( $this->settings[ 'providers' ] );
846 foreach ( $providers_available as $provider ) {
847 call_user_func( $provider . '_integration', $this->settings );
848 }
849 }
850
851 public function meta_box_content_publish() {
852
853 global $post;
854 $screen = get_current_screen();
855
856 $fca_eoi = get_post_meta( $post->ID, 'fca_eoi', true );
857
858 // Widgets
859 K::wrap(
860 sprintf(
861 __( 'You can publish this optin box by going to <a href="%s" target="_blank">Appearance › Widgets</a>')
862 , admin_url( 'widgets.php')
863 )
864 , array( 'id' => 'fca_eoi_publish_widget' )
865 , array( 'in' => 'p' )
866 );
867
868 // Post boxes
869 echo '<div id ="fca_eoi_publish_postbox">';
870 K::wrap( __( 'Shortcode')
871 , array( 'style' => 'padding-left: 0px; padding-right: 0px; ' )
872 , array( 'in' => 'h3' )
873 );
874 K::wrap( __( "Copy and paste beneath shortcode anywhere on your site where you'd like this opt-in form to appear." )
875 , null
876 , array( 'in' => 'p' )
877 );
878 K::input( ''
879 , array(
880 'class' => 'regular-text autoselect',
881 'readonly' => 'readonly',
882 'value' => sprintf( '[%s id=%d]', $this->settings[ 'shortcode' ], $post->ID ),
883 )
884 , array( 'format' => '<p>:input</p>', )
885 );
886 K::wrap( __( 'Append to post or page')
887 , array( 'style' => 'padding-left: 0px; padding-right: 0px; ' )
888 , array( 'in' => 'h3' )
889 );
890 K::wrap( __( 'Automatically append this optin to the following posts, categories and/or pages.' )
891 , null
892 , array( 'in' => 'p' )
893 );
894 k_selector( 'fca_eoi[publish_postbox]', K::get_var( 'publish_postbox', $fca_eoi, array() ) );
895 echo '</div>';
896
897 // Lightboxes
898
899 switch ( $this->settings['distribution'] ) {
900 case 'free':
901 $conditions_options = array(
902 'time_on_page' => 'Time on page is at least (second)',
903 );
904 break;
905 case 'premium':
906 $conditions_options = array(
907 'pageviews' => 'Number of pageviews during this visit at least',
908 'scrolled_percent' => 'Scrolled down on current page at least (%)',
909 'time_on_page' => 'Time on page is at least (second)',
910 'include' => 'Only show popup on the following posts/pages',
911 'exclude' => 'Never show popup on the following posts/pages',
912 'exit_intervention' => 'User is about to close the page (Exit Intervention)'
913 );
914 break;
915 }
916
917 $fca_eoi[ 'publish_lightbox' ] = K::get_var( 'publish_lightbox', $fca_eoi, array() );
918 echo '<div id ="fca_eoi_publish_lightbox">';
919
920 if ( 'premium' === $this->settings[ 'distribution' ] ) {
921 K::input( 'fca_eoi[publish_lightbox_mode]'
922 , array(
923 'type' => 'radio',
924 'value' => 'two_step_optin',
925 'checked' => 'two_step_optin' === K::get_var( 'publish_lightbox_mode', $fca_eoi ),
926 )
927 , array(
928 'format' => '<p><label>:input Two-Step Optin (Trigger popup only when the visitor clicks on a call to action link)</label></p>',
929 )
930 );
931 }
932
933 K::input( 'fca_eoi[publish_lightbox_mode]'
934 , array(
935 'type' => 'radio',
936 'value' => 'traditional_popup',
937 'checked' => 'traditional_popup' === K::get_var( 'publish_lightbox_mode', $fca_eoi ),
938 )
939 , array(
940 'format' => '<p><label>:input Traditional Popup (Trigger popup when the visitor is browsing your site)</label></p>',
941 )
942 );
943 ?>
944
945 <hr />
946
947 <?php // Condition always present ?>
948 <div id="fca_eoi_publish_lightbox_mode_traditional_popup" class="hidden">
949 <fieldset class="fca_lightbox_condition fca_lightbox_condition_permanent">
950 <span class="fca_lightbox_condition_text"><?php _e('Show popup'); ?></span><?php
951 K::select(
952 'fca_eoi[publish_lightbox][show_every]'
953 , array()
954 , array(
955 'options' => array(
956 'never' => __( 'Never' ),
957 'always' => __( 'On every pageview' ),
958 'session' => __( 'Once per visit' ),
959 'day' => __( 'Once per day' ),
960 'month' => __( 'Once per month' ),
961 'once' => __( 'Only once' ),
962 ),
963 'selected' => K::get_var( 'show_every', $fca_eoi[ 'publish_lightbox' ] ),
964 )
965 );
966 ?>
967 <p class="fca_eoi_help"><?php _e( 'Set how frequently to show the popup. (Never means the popup is deactivated.)' ); ?></p>
968 </fieldset>
969 <?php
970 // Saved conditions
971 $saved_conditions = array();
972 $saved_conditions = K::get_var( 'conditions', $fca_eoi[ 'publish_lightbox' ], array() );
973
974 // Add default if creating a new form
975 if ( 'add' === $screen->action ) {
976 $saved_conditions = array(
977 '_'.time().'001' => array(
978 'parameter' => 'pageviews',
979 'value' => '2',
980 ),
981 '_'.time().'002' => array(
982 'parameter' => 'scrolled_percent',
983 'value' => '30',
984 ),
985 '_'.time().'003' => array(
986 'parameter' => 'time_on_page',
987 'value' => '30',
988 ),
989 );
990 }
991
992 foreach ( $saved_conditions as $condition_id => $condition ) {
993 // Skip conditions that are not available in version
994 if ( ! array_key_exists( $condition[ 'parameter' ], $conditions_options ) ) {
995 continue;
996 }
997
998 $select = K::select( "fca_eoi[publish_lightbox][conditions][$condition_id][parameter]"
999 , array()
1000 , array(
1001 'selected' => $condition['parameter'],
1002 'options' => $conditions_options,
1003 'return' => 'true',
1004 )
1005 );
1006
1007 if ( in_array( $condition['parameter'], array( 'include', 'exclude' ) ) ) {
1008 $input = k_selector( "fca_eoi[publish_lightbox][conditions][$condition_id][value]"
1009 , K::get_var( 'value', $condition, array() )
1010 , true
1011 );
1012 } else if ( $condition['parameter'] == 'exit_intervention' ) {
1013 $input = K::input( "fca_eoi[publish_lightbox][conditions][$condition_id][value]"
1014 , array(
1015 'type' => 'hidden',
1016 'value' => ''
1017 )
1018 , array(
1019 'return' => true
1020 )
1021 );
1022 } else {
1023 $input = K::input( "fca_eoi[publish_lightbox][conditions][$condition_id][value]"
1024 , array(
1025 'class' => 'medium-text',
1026 'type' => 'text',
1027 'value' => $condition['value'],
1028 )
1029 , array(
1030 'return' => true,
1031 )
1032 );
1033 }
1034
1035 $condition_HTML_inner = ''
1036 . $select
1037 . $input
1038 . ' '
1039 . '<span class="button fca_remove_lightbox_condition"><span style="vertical-align:text-bottom" class="dashicons dashicons-no"></span> Remove Rule</span>'
1040 . '<p class="fca_eoi_help"></p>'
1041 ;
1042 echo '<div class="fca_eoi_and">- And -</div>';
1043 K::wrap( $condition_HTML_inner
1044 , array(
1045 'id' => 'fca_lightbox_condition' . $condition_id,
1046 'class' => 'fca_lightbox_condition',
1047 'style' => '
1048 padding: .5%;
1049 margin:.5% 0;
1050 box-shadow: 0 0 1px rgba(0, 0, 0, .2);
1051 width: 97%;
1052 background:#F7F7F7;
1053 ',
1054 )
1055 , array(
1056 'in' => 'fieldset',
1057 )
1058 );
1059 }
1060 ?>
1061
1062 <?php /* Button to add conditions */ ?>
1063 <p><span class="button" id="fca_add_lightbox_condition"><span style="vertical-align:text-bottom" class="dashicons dashicons-plus"></span> Add Rule</span></p>
1064 </div>
1065
1066 <?php if ( 'premium' === $this->settings[ 'distribution' ] ) { ?>
1067 <div id="fca_eoi_publish_lightbox_mode_two_step_optin" class="hidden">
1068 <?php
1069 K::input( 'fca_eoi[lightbox_cta_text]'
1070 , array(
1071 'value' => K::get_var( 'lightbox_cta_text', $fca_eoi )
1072 ? K::get_var( 'lightbox_cta_text', $fca_eoi )
1073 : __( 'Free Download' )
1074 ,
1075 'class' => 'regular-text',
1076 )
1077 , array(
1078 'format' => '<p><label>Call to action text :input</label></p>',
1079 )
1080 );
1081 K::input( 'fca_eoi[lightbox_cta_link]'
1082 , array(
1083 'readonly' => 'readonly',
1084 'value' => htmlspecialchars( sprintf( '<a href="#" data-optin-cat="%d">%s</a>'
1085 , $post->ID
1086 , __( 'Free Download' )
1087 ) ),
1088 'class' => 'regular-text autoselect',
1089 )
1090 , array(
1091 'format' => '<p><label>Call to action link :input</label></p>',
1092 )
1093 );
1094
1095 K::wrap( __( 'Post above link into the text editor window of your post/page/widget.' )
1096 , array( 'class' => 'description' )
1097 , array( 'in' => 'p' )
1098 );
1099
1100 ?>
1101 </div>
1102 <?php } ?>
1103
1104 <script>
1105 jQuery( document ).ready( function( $ ) {
1106
1107 <?php
1108 $lightbox_help = array(
1109 'scrolled_percent' => __( 'Only show this popup to visitors who have scrolled down at least X% on the current page. Someone who scrolls down is engaging with your content and therefore more likely to convert. (Keep in mind that the entire page length, including comments, is included in this calculation.)' ),
1110 'pageviews' => __( 'Only show this popup to visitors have viewed at least X pages in this visit. Someone who views multiple pages during a visit is very engaged and therefore more likely to convert.' ),
1111 'include' => __( 'Makes sure your popup will only be shown on the posts, pages or categories you select. This is great if you want to set up offers for specific categories or blog posts.' ),
1112 'exclude' => __( 'Makes sure your popup will never be shown on the posts, pages or categories you select. This is great if you want to avoid showing your popups on landing pages, checkout pages, order confirmation pages, etc.' ),
1113 'exit_intervention' => __( 'Only show this popup to visitors who are about to close the current page.' ),
1114 'time_on_page' => __( 'Only show this popup to visitors who have spent at least X seconds on the current page. The longer someone spends on your page, the more engaged he is.' ),
1115 );
1116 ?>
1117
1118 var lightbox_help = <?php echo json_encode( $lightbox_help ) ?>;
1119
1120 // Condition template
1121 <?php if ( 'free' === $this->settings[ 'distribution' ] ) { ?>
1122 var f = ( '<fieldset id="fca_lightbox_condition_x_" class="fca_lightbox_condition"><select name="fca_eoi[publish_lightbox][conditions][_x_][parameter]"><option value="time_on_page" data-default="30">Time on page is at least (second)</option></select><input class="medium-text" type="text" value="" name="fca_eoi[publish_lightbox][conditions][_x_][value]"/> <span class="button fca_remove_lightbox_condition"><span class="dashicons dashicons-no"></span> Remove Rule</span><p class="fca_eoi_help"></p></fieldset>' );
1123 <?php } else if ( 'premium' === $this->settings[ 'distribution' ] ) { ?>
1124 var f = ( '<fieldset id="fca_lightbox_condition_x_" class="fca_lightbox_condition"><select name="fca_eoi[publish_lightbox][conditions][_x_][parameter]"><option value="pageviews" data-default="2">Number of pageviews during this visit at least</option><option value="scrolled_percent" data-default="30">Scrolled down on current page at least (%)</option><option value="time_on_page" data-default="30">Time on page is at least (second)</option><option value="include">Only show popup on the following posts/pages</option><option value="exclude">Never show popup on the following posts/pages</option><option value="exit_intervention">User is about to close the page (Exit Intervention)</option></select><input class="medium-text" type="text" value="" name="fca_eoi[publish_lightbox][conditions][_x_][value]"/> <span class="button fca_remove_lightbox_condition"><span class="dashicons dashicons-no"></span> Remove Rule</span><p class="fca_eoi_help"></p></fieldset>' );
1125 <?php } ?>
1126
1127 <?php
1128 $s = str_replace( array( "\n", '"' )
1129 , array( '', '\"' )
1130 , k_selector( 'fca_eoi[publish_lightbox][conditions][_x_][value]', array(), true )
1131 );
1132 ?>
1133
1134 var s = "<?php echo $s; ?>";
1135
1136 var t = '<input class="medium-text" type="text" name="fca_eoi[publish_lightbox][conditions][_x_][value]"/>';
1137
1138 // Disables all but last condition, and use the right input field
1139 $( document )
1140 .on( 'change'
1141 , "select[name^='fca_eoi[publish_lightbox][conditions]['][name$='][parameter]']"
1142 , function() {
1143 fix_conditions();
1144 }
1145 )
1146 ;
1147 $("select[name^='fca_eoi[publish_lightbox][conditions]['][name$='][parameter]']:first").change()
1148 ;
1149 function fix_conditions() {
1150 var $button = $( '#fca_add_lightbox_condition' );
1151 var $conditions;
1152
1153 $conditions = $( 'select' )
1154 .filter( "[name^='fca_eoi[publish_lightbox][conditions][']" )
1155 .filter( "[name$='][parameter]']" )
1156 ;
1157 // Prevent changing other conditions
1158 $conditions
1159 .not(':last')
1160 .prop( 'disabled', true )
1161 ;
1162 // Allow changing last
1163 $conditions
1164 .last()
1165 .prop( 'disabled', false )
1166 ;
1167 // Show hide plus button
1168 if ( $conditions.length == $(f).find('select:first option').length ) {
1169 $button.hide();
1170 } else {
1171 $button.show();
1172 }
1173
1174 // Update conditions variable
1175 $conditions = $( 'select' )
1176 .filter( "[name^='fca_eoi[publish_lightbox][conditions][']" )
1177 .filter( "[name$='][parameter]']" )
1178 ;
1179
1180 /**
1181 * Show posts selector VS input depending on option.
1182 * Remove previously selected options.
1183 * Update help text
1184 * Use default when applicable (i.e when data-default is here)
1185 * Remove overflow (condition fieldsets without possible options)
1186 */
1187 $conditions.each( function( i ) {
1188 var $this = $( this );
1189 var $fieldset = $this.parent();
1190 var $parameter = $( '[name$="[parameter]"]', $fieldset );
1191 var $value = $( '[name$="[value]"],[name$="[value][]"]', $fieldset );
1192 var $input = $value;
1193 var name = $value.attr( 'name' ).replace(/\[\]/g, '');
1194 var is_textfield = $value.is( 'input' );
1195 var is_hidden = ( 'exit_intervention' === $parameter.val() );
1196 var use_textfield = ( 'include' !== $parameter.val() && 'exclude' !== $parameter.val() );
1197 var cpt, condition, _default;
1198
1199 // Decide what element to use : input VS select
1200 if ( use_textfield && ! is_textfield ) {
1201 $( '.select2', $fieldset ).remove();
1202 $input = $( t ).attr( 'name', name );
1203 $parameter.after( $input );
1204 } else if ( ! use_textfield && is_textfield ) {
1205 $input = $( s ).attr( 'name', name + '[]' );
1206 $value.replaceWith( $input );
1207 }
1208
1209 // Hide or show the input
1210 $input.attr( 'type', is_hidden ? 'hidden' : 'text' );
1211
1212 // Remove previously selected condition parameters
1213 $conditions.not($this).find('option[value='+$this.val()+']').remove();
1214
1215 // Update help text
1216 $( '.fca_eoi_help', $fieldset ).text( lightbox_help[ $parameter.val() ] );
1217
1218 // Use default
1219 if ( _default = $( ':selected', $parameter).data( 'default' ) ) {
1220 $value.val( _default );
1221 $( 'option', $parameter ).data( 'default', '' );
1222 }
1223
1224 // Remove overflow
1225 if ( ! $( 'option', $parameter ).length ) {
1226 $( '.fca_remove_lightbox_condition', $fieldset ).click();
1227 }
1228 } );
1229 $('select.select2').select2();
1230 }
1231
1232 $( document ).on( 'click', '.fca_remove_lightbox_condition', function( i ) {
1233
1234 var $this = $( this );
1235 var $opt = $( 'select:first option:selected', $this.parent() )
1236 .clone()
1237 .removeAttr( 'selected' )
1238 ;
1239
1240 $this.parent().remove(); // remove condition row
1241
1242 $('.fca_eoi_and').remove(); // removes "AND"
1243 $('.fca_lightbox_condition:gt(0)').before( '<div class="fca_eoi_and">- And -</div>' );
1244
1245 $('select')
1246 .filter( "[name^='fca_eoi[publish_lightbox][conditions][']" )
1247 .filter( "[name$='][parameter]']" )
1248 .append( $opt )
1249 ;
1250
1251 fix_conditions();
1252 } );
1253
1254
1255 $( document ).on( 'click', '#fca_add_lightbox_condition', function() {
1256
1257 var $this = $( this );
1258 var ts = Date.now();
1259 var $condition = $( f.replace( /_x_/g, '_' + ts ) );
1260
1261 // Remove already used conditions
1262 $( 'option', $condition ).each( function() {
1263 var $this = $( this );
1264 var val = $this.val();
1265 var remove = $('select')
1266 .filter("[name^='fca_eoi[publish_lightbox][conditions][']")
1267 .filter("[name$='][parameter]']")
1268 .find('option:selected[value=' + val + ']')
1269 .length
1270 ;
1271
1272 if ( remove ) {
1273 $( this ).remove();
1274 }
1275 } );
1276
1277 $this.parent().before( '<div class="fca_eoi_and">- And -</div>' );
1278 $this.parent().before( $condition );
1279
1280 // Bind removing to minus sign
1281 $( '.fca_remove_lightbox_condition', '#fca_lightbox_condition_' + ts ).click( function() {
1282 $( this ).parent().remove();
1283 } );
1284
1285 // Prevent changing previous conditions
1286 fix_conditions();
1287 } );
1288
1289 // Enable disabled conditions fields to allow passing their value to the form action
1290 $( document ).on( 'submit', '#post', function() {
1291 $( 'select' )
1292 .filter( "[name^='fca_eoi[publish_lightbox][conditions][']" )
1293 .filter( "[name$='][parameter]']" )
1294 .prop( 'disabled', false )
1295 ;
1296 } );
1297 } );
1298
1299 </script>
1300 <?php
1301
1302 echo '</div>'; // #fca_eoi_publish_lightbox
1303 }
1304
1305 private function meta_box_field( $id, $title, $controls = array() ) {
1306 $content = '';
1307
1308 foreach ( $controls as $control ) {
1309 $control[2] = empty( $control[2] ) ? array() : $control[2];
1310 $control[3] = empty( $control[3] ) ? array() : $control[3];
1311
1312 $control[3][ 'return' ] = true;
1313
1314 $content .= call_user_func_array( 'K::' . $control[0], array_slice( $control, 1 ) );
1315 }
1316
1317 $content = trim( $content );
1318 $class_name = 'accordion-section-primary-' . ( empty( $content ) ? 'empty' : 'full' );
1319 $content = '<div class="' . $class_name . '">' . $content . '</div>';
1320
1321 K::wrap(
1322 K::wrap(
1323 $title,
1324 array( 'class' => 'accordion-section-title' ),
1325 array( 'return' => true )
1326 ) .
1327 K::wrap(
1328 $content,
1329 array( 'class' => 'accordion-section-content' ),
1330 array( 'return' => true )
1331 ),
1332 array(
1333 'class' => 'accordion-section',
1334 'id' => $id
1335 )
1336 );
1337 }
1338
1339 public function meta_box_content_build() {
1340
1341 wp_enqueue_script( 'accordion' );
1342
1343 global $post;
1344 $fca_eoi = get_post_meta( $post->ID, 'fca_eoi', true );
1345 $providers_available = array_keys( $this->settings[ 'providers' ] );
1346 $providers_options = array();
1347 $screen = get_current_screen();
1348 $fca_eoi_last_3_forms = $this->settings[ 'fca_eoi_last_3_forms' ];
1349
1350 // Prepare providers options
1351 foreach ($this->settings[ 'providers' ] as $provider_id => $provider ) {
1352 $providers_options[ $provider_id ] = $provider[ 'info' ][ 'name' ];
1353 }
1354
1355 K::wrap( '', array( 'id' => 'fca_eoi_preview' ) );
1356
1357 echo '<div id="fca_eoi_settings" class="accordion-container">';
1358
1359 $this->meta_box_field( 'fca_eoi_fieldset_form', 'Form' );
1360
1361 $this->meta_box_field( 'fca_eoi_fieldset_headline', 'Headline', array(
1362 array( 'input', 'fca_eoi[headline_copy]',
1363 array( 'value' => K::get_var( 'headline_copy', $fca_eoi ) ),
1364 array( 'format' => '<p><label><span class="control-title">Headline Copy</span><br />:input</label></p>' )
1365 )
1366 ) );
1367
1368 $this->meta_box_field( 'fca_eoi_fieldset_description', 'Description', array(
1369 array( 'textarea', 'fca_eoi[description_copy]', array(), array(
1370 'format' => ':textarea',
1371 'editor' => true,
1372 'value' => K::get_var( 'description_copy', $fca_eoi ),
1373 ) )
1374 ) );
1375
1376 $this->meta_box_field( 'fca_eoi_fieldset_name_field', 'Name Field', array(
1377 array( 'input', 'fca_eoi[show_name_field]',
1378 array(
1379 'type' => 'checkbox',
1380 'checked' => K::get_var( 'show_name_field', $fca_eoi ),
1381 ),
1382 array(
1383 'format' => '<p><label>:input Show Name Field</label></p>'
1384 ),
1385 ),
1386 array( 'input', 'fca_eoi[name_placeholder]',
1387 array( 'value' => K::get_var( 'name_placeholder', $fca_eoi, 'Your Name' ) ),
1388 array( 'format' => '<p><label><span class="control-title">Placeholder Text</span><br />:input</label></p>' )
1389 )
1390 ) );
1391
1392 $this->meta_box_field( 'fca_eoi_fieldset_email_field', 'Email Field', array(
1393 array( 'input', 'fca_eoi[email_placeholder]',
1394 array( 'value' => K::get_var( 'email_placeholder', $fca_eoi, 'Your Email' ) ),
1395 array( 'format' => '<p><label><span class="control-title">Placeholder Text</span><br />:input</label></p>' )
1396 )
1397 ) );
1398
1399 $this->meta_box_field( 'fca_eoi_fieldset_button', 'Button', array(
1400 array( 'input', 'fca_eoi[button_copy]',
1401 array( 'value' => K::get_var( 'button_copy', $fca_eoi, 'Subscribe Now' ) ),
1402 array( 'format' => '<p><label><span class="control-title">Button Copy</span><br />:input</label></p>' )
1403 )
1404 ) );
1405
1406 $this->meta_box_field( 'fca_eoi_fieldset_privacy', 'Privacy Policy', array(
1407 array( 'textarea', 'fca_eoi[privacy_copy]',
1408 array(
1409 'class' => 'large-text',
1410 ),
1411 array(
1412 'format' => '<p><label><span class="control-title">Privacy Policy Copy</span><br />:textarea</label></p>',
1413 'value' => K::get_var( 'privacy_copy', $fca_eoi ),
1414 )
1415 )
1416 ) );
1417
1418 $this->meta_box_field( 'fca_eoi_fieldset_fatcatapps', 'Branding', array(
1419 array( 'input', 'fca_eoi[show_fatcatapps_link]',
1420 array(
1421 'type' => 'checkbox',
1422 'checked' => K::get_var( 'show_fatcatapps_link', $fca_eoi ),
1423 ),
1424 array(
1425 'format' => '<p><label>:input Show <a href="http://fatcatapps.com/" target="_blank">Easy Opt-ins</a> Branding</label></p>'
1426 )
1427 )
1428 ) );
1429
1430 $this->meta_box_field( 'fca_eoi_fieldset_error_text', 'Error Text', array(
1431 array( 'textarea', 'fca_eoi[error_text_field_required]',
1432 array(
1433 'class' => 'large-text'
1434 ),
1435 array(
1436 'format' => '<p><label><span class="control-title">Field Required</span><br />:textarea</label></p>',
1437 'value' => K::get_var( 'error_text_field_required', $fca_eoi, esc_html( $this->settings['error_text']['field_required'] ) )
1438 )
1439 ),
1440 array( 'textarea', 'fca_eoi[error_text_invalid_email]',
1441 array(
1442 'class' => 'large-text'
1443 ),
1444 array(
1445 'format' => '<p><label><span class="control-title">Invalid Email</span><br />:textarea</label></p>',
1446 'value' => K::get_var( 'error_text_invalid_email', $fca_eoi, esc_html( $this->settings['error_text']['invalid_email'] ) )
1447 )
1448 )
1449 ) );
1450
1451 echo '</div>';
1452 }
1453
1454 public function meta_box_content_thanks() {
1455 global $post;
1456 $fca_eoi = get_post_meta( $post->ID, 'fca_eoi', true );
1457 $screen = get_current_screen();
1458 $fca_eoi_last_3_forms = $this->settings[ 'fca_eoi_last_3_forms' ];
1459
1460 // Get the previous thank you page if this is a new post
1461 $thank_you_page_suggestion = false;
1462 if ( 'add' === $screen->action ) {
1463 foreach ( $fca_eoi_last_3_forms as $fca_eoi_previous_form ) {
1464 try {
1465 if(
1466 K::get_var( 'fca_eoi', $fca_eoi_previous_form )
1467 && K::get_var( 'thank_you_page', $fca_eoi_previous_form[ 'fca_eoi' ] )
1468 ) {
1469 $thank_you_page_suggestion = $fca_eoi_previous_form[ 'fca_eoi' ][ 'thank_you_page' ];
1470 break;
1471 }
1472 } catch ( Exception $e ) {}
1473 }
1474 }
1475
1476 // Prepare options
1477 $pages = array( '~' => __( 'Front page' ) );
1478 $pages_objects = get_pages();
1479 foreach ( $pages_objects as $page_obj ) {
1480 $pages[ $page_obj->ID ] = $page_obj->post_title;
1481 }
1482
1483 K::wrap( 'Redirect user to the following page after submitting the form:'
1484 , null
1485 , array( 'in' => 'p' )
1486 );
1487 K::select( 'fca_eoi[thank_you_page]'
1488 , array(
1489 'class' => 'select2',
1490 'style' => 'width: 27em;',
1491 )
1492 , array(
1493 'format' => '<p><label>:select</label></p>',
1494 'options' => $pages,
1495 'selected' => 'add' === $screen->action
1496 ? $thank_you_page_suggestion
1497 : K::get_var( 'thank_you_page', $fca_eoi, '~' )
1498 ,
1499 )
1500 );
1501 K::wrap( __( 'Create a new "Thank You Page" &rsaquo;' )
1502 , array(
1503 'href' => admin_url( 'post-new.php?post_type=page' ),
1504 'target' => '_blank',
1505 )
1506 , array(
1507 'in' => 'a',
1508 'html_before' => '<p>',
1509 'html_after' => '</p>',
1510 )
1511 );
1512 }
1513
1514 public function meta_box_content_powerups() {
1515
1516 global $post;
1517 $fca_eoi = get_post_meta( $post->ID, 'fca_eoi', true );
1518
1519 do_action('fca_eoi_powerups', $fca_eoi );
1520 }
1521
1522 public function meta_box_content_debug() {
1523
1524 global $post;
1525
1526 $fca_eoi = get_post_meta( $post->ID, 'fca_eoi', true );
1527
1528 !d( $fca_eoi );
1529 }
1530
1531 /**
1532 * Save the Metabox Data
1533 */
1534 public function save_meta_box_content( $post_id, $post ) {
1535
1536 // Save meta data
1537 delete_post_meta( $post->ID, 'fca_eoi' );
1538 if( $meta = K::get_var( 'fca_eoi', $_POST ) ) {
1539 // Add provider if missing (happens on free distros where there is only one provider)
1540 if( ! K::get_var( 'provider', $meta ) ) {
1541 $meta[ 'provider' ] = $this->settings[ 'provider' ];
1542 }
1543
1544 // Keep only the current providers settings, Remove all [provider]_[setting] not belonging to the current provider
1545 $provider = K::get_var( 'provider', $meta );
1546 if( $provider ) {
1547 $providers = array_keys( $this->settings[ 'providers' ] );
1548 $other_providers = array_values( array_diff( $providers, array( $provider ) ) );
1549 foreach ( $meta as $k => $v ) {
1550 $p = explode( '_', $k );
1551 $k_1 = array_shift( $p );
1552 if( in_array( $k_1, $other_providers ) ) {
1553 unset( $meta[ $k ] );
1554 }
1555 }
1556
1557 foreach ( $_POST as $k => $v ) {
1558 if ( strpos( $k, 'fca_eoi_' . $provider . '_' ) === 0 ) {
1559 delete_post_meta( $post->ID, $k );
1560 add_post_meta( $post->ID, $k, $v );
1561 $meta[ substr($k, 8) ] = $v;
1562 }
1563 }
1564 }
1565
1566 // Keep only the current layout inforamtion
1567 foreach ( $meta as $k => $v ) {
1568 if( preg_match( '|^[a-z]+_[0-9]+$|', $k ) && $k !== $meta[ 'layout' ] ) {
1569 unset( $meta[ $k ] );
1570 }
1571 }
1572
1573 // Make sure emtpy value for publish_postbox or publish_lightbox are saved as array(-1)
1574 if( ! K::get_var( 'publish_postbox' , $meta, array() ) ) {
1575 $meta[ 'publish_postbox' ] = array(-1);
1576 }
1577 if( ! K::get_var( 'publish_lightbox' , $meta, array() ) ) {
1578 $meta[ 'publish_lightbox' ] = array(-1);
1579 }
1580
1581
1582 delete_post_meta( $post->ID, 'fca_eoi_layout' );
1583 delete_post_meta( $post->ID, 'fca_eoi_provider' );
1584
1585 $on_save_function = $provider . '_on_save';
1586 if ( function_exists( $on_save_function ) ) {
1587 $meta = $on_save_function( $meta );
1588 }
1589
1590 add_post_meta( $post->ID, 'fca_eoi', $meta );
1591 add_post_meta( $post->ID, 'fca_eoi_layout', $meta[ 'layout' ] );
1592 add_post_meta( $post->ID, 'fca_eoi_provider', $meta[ 'provider' ] );
1593 }
1594 }
1595
1596 public function live_preview( $content ) {
1597 global $post;
1598 if (get_post_type() == 'easy-opt-ins' && is_main_query()) {
1599 $shortcode = sprintf( '[%s id=%d]', $this->settings[ 'shortcode' ], $post->ID );
1600 return do_shortcode($shortcode);
1601 } else {
1602 return $content;
1603 }
1604 }
1605
1606 public function admin_enqueue() {
1607
1608 $protocol = is_ssl() ? 'https' : 'http';
1609 $provider = $this->settings[ 'provider' ];
1610 $providers_available = array_keys( $this->settings[ 'providers' ] );
1611
1612 if ( ( ! empty( $_REQUEST['page'] ) && $_REQUEST['page'] == 'eoi_powerups' ) || has_action( 'fca_eoi_powerups' ) ) {
1613 wp_enqueue_style( 'fca_eoi_powerups', $this->settings['plugin_url'] . '/assets/powerups/fca_eoi_powerups.css' );
1614 }
1615
1616 $screen = get_current_screen();
1617 if( 'easy-opt-ins' === $screen->id ){
1618 wp_enqueue_script( 'tooltipster', $this->settings[ 'plugin_url' ] . '/assets/vendor/tooltipster/jquery.tooltipster.min.js' );
1619 wp_enqueue_style( 'tooltipster', $this->settings[ 'plugin_url' ] . '/assets/vendor/tooltipster/tooltipster.css' );
1620 wp_enqueue_script( 'mustache', $protocol . '://cdnjs.cloudflare.com/ajax/libs/mustache.js/0.8.1/mustache.min.js' );
1621 wp_enqueue_script( 'select2', $protocol . '://cdnjs.cloudflare.com/ajax/libs/select2/3.5.0/select2.js' );
1622 wp_enqueue_script( 'tinycolor', $protocol . '://cdnjs.cloudflare.com/ajax/libs/tinycolor/1.0.0/tinycolor.min.js' );
1623 wp_enqueue_script( 'jscolor', $this->settings['plugin_url'] . '/assets/vendor/jscolor/jscolor.js' );
1624 wp_enqueue_script( 'admin-cpt-easy-opt-ins-providers', $this->settings['plugin_url'] . '/assets/admin/eoi-providers.js' );
1625 wp_enqueue_style( 'admin-cpt-easy-opt-ins-providers', $this->settings['plugin_url'] . '/assets/admin/eoi-providers.css' );
1626 wp_enqueue_script( 'admin-cpt-easy-opt-ins', $this->settings['plugin_url'] . '/assets/admin/cpt-easy-opt-ins.js' );
1627 wp_enqueue_style( 'fca_eoi', $this->settings[ 'plugin_url' ].'/assets/style' . ( EasyOptInsLayout::uses_new_css() ? '-new' : '' ) . '.css' );
1628 foreach ( $providers_available as $provider ) {
1629 wp_enqueue_script( 'admin-cpt-easy-opt-ins-' . $provider, $this->settings['plugin_url'] . '/providers/' . $provider . '/cpt-easy-opt-ins.js' );
1630
1631 $css_path = '/providers/' . $provider . '/cpt-easy-opt-ins.css';
1632 if ( is_readable( $this->settings['plugin_dir'] . $css_path ) ) {
1633 wp_enqueue_style( 'admin-cpt-easy-opt-ins-' . $provider, $this->settings['plugin_url'] . $css_path );
1634 }
1635 }
1636 wp_enqueue_style( 'admin-cpt-easy-opt-ins', $this->settings['plugin_url'] . '/assets/admin/cpt-easy-opt-ins.css' );
1637 if ( EasyOptInsLayout::uses_new_css() ) {
1638 wp_enqueue_style( 'admin-cpt-easy-opt-ins-new', $this->settings['plugin_url'] . '/assets/admin/cpt-easy-opt-ins-new.css' );
1639 } else {
1640 wp_enqueue_style( 'admin-cpt-easy-opt-ins-old', $this->settings['plugin_url'] . '/assets/admin/cpt-easy-opt-ins-old.css' );
1641 }
1642 wp_enqueue_style( 'font-awesome', $protocol . '://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.1.0/css/font-awesome.min.css' );
1643 wp_enqueue_style( 'select2', $protocol . '://cdnjs.cloudflare.com/ajax/libs/select2/3.5.0/select2.min.css' );
1644 wp_enqueue_script('bootstrap-js', $protocol . '://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.2.0/js/bootstrap.min.js');
1645 if ( has_action( 'fca_eoi_powerups' ) ) {
1646 wp_enqueue_script('fca_eoi_powerups', $this->settings['plugin_url'] . '/assets/powerups/fca_eoi_powerups.js');
1647 }
1648 }
1649 if( 'widgets' === $screen->id ){
1650 wp_enqueue_script( 'select2', $protocol . '://cdnjs.cloudflare.com/ajax/libs/select2/3.5.0/select2.js' );
1651 wp_enqueue_style( 'select2', $protocol . '://cdnjs.cloudflare.com/ajax/libs/select2/3.5.0/select2.min.css' );
1652 }
1653 }
1654
1655 /**
1656 * Disable autosaving optin forms since it causes data loss
1657 */
1658 function disable_autosave() {
1659 if ( 'easy-opt-ins' == get_post_type() ) {
1660 wp_dequeue_script( 'autosave' );
1661 }
1662 }
1663
1664 /**
1665 * Hides minor publising form items (status, visibility and publication date)
1666 *
1667 * This function shoud be used along with force_published to prevent
1668 * saving posts as drafts
1669 */
1670 public function hide_minor_publishing() {
1671 $screen = get_current_screen();
1672 if( in_array( $screen->id, array( 'easy-opt-ins' ) ) ) {
1673 echo '<style>#minor-publishing { display: none; }</style>';
1674 }
1675 }
1676
1677 /**
1678 * Disables meta box toggling (collapse/expand) for specified post types
1679 */
1680 public function disable_metabox_toggle() {
1681
1682 $current_screen = get_current_screen();
1683
1684 // Array of post types where we want to remove metabox toggling
1685 $post_types = array(
1686 'easy-opt-ins',
1687 );
1688
1689 if( in_array( $current_screen->id, $post_types ) ) {
1690 ?>
1691 <script type="text/javascript">
1692 jQuery( document ).ready( function($) {
1693 $( '.postbox' ).removeClass( 'closed' );
1694 $( '.postbox .hndle' ).css( 'cursor', 'default' );
1695 $( document ).delegate( '.postbox h3, .postbox .handlediv', 'click', function() {
1696 $( this )
1697 .unbind( 'click.postboxes' )
1698 .parent().removeClass( 'closed' );
1699 } );
1700 } );
1701 </script>
1702 <?php
1703 }
1704 }
1705
1706 /**
1707 * Forces one column
1708 */
1709 public function force_one_column() {
1710
1711 return 1;
1712 }
1713
1714 /**
1715 * Sort metaboxes
1716 */
1717 public function order_columns( $order ) {
1718 return array(
1719 'normal' => join( ",", array(
1720 'submitdiv',
1721 'fca_eoi_meta_box_nav',
1722 'fca_eoi_meta_box_setup',
1723 'fca_eoi_meta_box_build',
1724 'fca_eoi_meta_box_provider',
1725 'fca_eoi_meta_box_thanks',
1726 'fca_eoi_meta_box_publish',
1727 'fca_eoi_meta_box_powerups',
1728 'fca_eoi_meta_box_debug',
1729 ) ),
1730 'side' => '',
1731 'advanced' => '',
1732 );
1733 }
1734
1735 /**
1736 * replacing the default "Enter title here" placeholder text in the title input box to
1737 *
1738 */
1739 public function change_default_title($title) {
1740
1741 $screen = get_current_screen();
1742
1743 if ( 'easy-opt-ins' == $screen->post_type ) {
1744 $title = 'Enter name here';
1745 }
1746
1747 return $title;
1748 }
1749
1750 /**
1751 * Override some strings to match our likings
1752 */
1753 public function override_text( $text ) {
1754
1755 if( $post = K::get_var( 'post', $GLOBALS ) ) {
1756 if ( 'easy-opt-ins' === $post->post_type ) {
1757 switch ( $text ) {
1758 case 'Post published. <a href="%s">View post</a>':
1759 case 'Post updated. <a href="%s">View post</a>':
1760 $text = __( 'Opt-In Form saved.' );
1761 break;
1762 case 'Publish':
1763 $text = __( 'Save' );
1764 break;
1765 case 'Update':
1766 $text = __( 'Save' );
1767 break;
1768 }
1769 }
1770 }
1771 return $text;
1772 }
1773
1774 public function force_published( $post ) {
1775
1776 if( ! in_array( $post[ 'post_status' ], array( 'auto-draft', 'trash') ) ) {
1777 if( in_array( $post[ 'post_type' ], array( 'easy-opt-ins' ) ) ) {
1778 $post['post_status'] = 'publish';
1779 }
1780 }
1781 return $post;
1782 }
1783
1784 /**
1785 * Disables bulk editing
1786 */
1787 public function disable_bulk_edit( $actions ){
1788 unset( $actions[ 'edit' ] );
1789 return $actions;
1790 }
1791
1792 /**
1793 * Removes quick edit
1794 */
1795 public function remove_quick_edit( $actions ) {
1796 global $post;
1797 if( 'easy-opt-ins' === $post->post_type ) {
1798 unset($actions['inline hide-if-no-js']);
1799 }
1800 return $actions;
1801 }
1802
1803 /**
1804 * Add the desired body classes (backend)
1805 */
1806 public function add_body_class( $classes ) {
1807
1808 return "$classes fca_eoi";
1809 }
1810
1811 /**
1812 * Handle Adding a subscriber with Ajax
1813 */
1814 public function ajax_subscribe() {
1815
1816 // Check a list_id is provided
1817 $list_id = K::get_var( 'list_id' , $_POST );
1818 if( ! $list_id ) {
1819 echo '';
1820 exit;
1821 }
1822
1823 // Subscribe user
1824 $status = call_user_func( $this->settings[ 'provider' ] . '_add_user' , $this->settings , $_POST , $list_id );
1825 // Output ✓ or ✗
1826 if( $status ) {
1827 $fca_eoi = get_post_meta( K::get_var( 'form_id' , $_POST ), 'fca_eoi', true );
1828 $double_opt_in = K::get_var( 'mailchimp_double_opt_in', $fca_eoi, '' );
1829 do_action('fca_eoi_after_submission'
1830 , $fca_eoi
1831 , $_POST
1832 );
1833 echo '';
1834 } else {
1835 echo '';
1836 }
1837 exit;
1838 }
1839
1840 /**
1841 * Handle form submissions
1842 */
1843 public function handle_submission() {
1844
1845 // Check that we have a form submission
1846 if ( ! K::get_var( 'fca_eoi', $_POST ) ) {
1847 return;
1848 }
1849
1850 // Check that we have a valid form
1851 $id = K::get_var( 'id', $_POST );
1852 $post = get_post( $id );
1853 if ( $post && 'easy-opt-ins' === $post->post_type ) {
1854 // Nothing, we're good
1855 } else {
1856 return;
1857 }
1858
1859 // Get meta
1860 $fca_eoi = get_post_meta( $id, 'fca_eoi', true );
1861 $provider = K::get_var( 'provider' , $fca_eoi );
1862
1863 // Check a list_id is provided
1864 $list_id = K::get_var( $provider . '_list_id' , $fca_eoi );
1865
1866 // @todo: remove
1867 // Hack for mailchimp upgrade
1868 if( empty( $fca_eoi[ 'provider' ] ) ) {
1869 $list_id = K::get_var(
1870 'mailchimp_list_id'
1871 , $fca_eoi
1872 , K::get_var( 'list_id' , $fca_eoi )
1873 );
1874 $provider = 'mailchimp';
1875 }
1876 // End of Hack
1877
1878 // Hack for campaignmonitor upgrade
1879 if( strlen( K::get_var( 'list_id' , $fca_eoi ) ) == 32){
1880 $list_id = K::get_var(
1881 'campaignmonitor_list_id'
1882 , $fca_eoi
1883 , K::get_var( 'list_id' , $fca_eoi )
1884 );
1885 $provider = 'campaignmonitor';
1886 }
1887 // End of Hack
1888
1889 // Hack for mailchimp upgrade
1890 if( empty( $fca_eoi[ 'provider' ] ) ) {
1891 $list_id = K::get_var(
1892 'getresponse_list_id'
1893 , $fca_eoi
1894 , K::get_var( 'list_id' , $fca_eoi )
1895 );
1896 $provider = 'getresponse';
1897 }
1898 // End of Hack
1899
1900 // Hack for custom form upgrade
1901 if( empty( $fca_eoi[ 'provider' ] ) ) {
1902 $list_id = K::get_var(
1903 'customform_list_id'
1904 , $fca_eoi
1905 , K::get_var( 'list_id' , $fca_eoi )
1906 );
1907 $provider = 'customform';
1908 }
1909 // End of Hack
1910
1911 // Subscribe user
1912 if( $list_id ) {
1913 $status = call_user_func( $provider . '_add_user' , $this->settings , $_POST , $list_id );
1914 $double_opt_in = K::get_var( 'mailchimp_double_opt_in', $fca_eoi, '' );
1915 do_action('fca_eoi_after_submission'
1916 , $fca_eoi
1917 , $_POST
1918 );
1919 }
1920
1921 // Go to thank you page if any
1922 $thank_you_page = K::get_var( 'thank_you_page', $fca_eoi );
1923 $thank_you_page_url = ( $thank_you_page && '~' !== $thank_you_page )
1924 ? get_permalink( $thank_you_page )
1925 : get_home_url()
1926 ;
1927 EasyOptInsActivity::get_instance()->add_conversion( K::get_var( 'fca_eoi_form_id', $_POST ) );
1928 wp_redirect( $thank_you_page_url );
1929 exit;
1930 }
1931
1932 public function admin_notices() {
1933
1934 $current_screen = get_current_screen();
1935
1936 // Exit function if we are not on the opt-in editing page
1937 if ( ! (
1938 'easy-opt-ins' === $current_screen->id
1939 && 'post' === $current_screen->base
1940 && 'edit' === $current_screen->parent_base
1941 && '' === $current_screen->action
1942 ) ) {
1943 return;
1944 }
1945
1946 global $post;
1947 $fca_eoi = get_post_meta( $post->ID, 'fca_eoi', true );
1948 $provider = K::get_var( 'provider', $fca_eoi);
1949 $errors = array();
1950
1951 // Add error for missing thank you page
1952 $confirmation_page_set = ( bool ) K::get_var( 'thank_you_page', $fca_eoi);
1953 if( ! $confirmation_page_set ) {
1954 $errors[] = __( 'No "Thank you" page selected. You will not be able to use this form.' );
1955 }
1956
1957 // Add error for missing list setting for the current provider
1958 $list_set = ( bool ) K::get_var( $provider . '_list_id', $fca_eoi);
1959
1960 // @todo: remove
1961 // Hack for mailchimp upgrade
1962 if( empty( $fca_eoi[ 'provider' ] ) ) {
1963 $fca_eoi[ 'mailchimp_list_id' ] = K::get_var(
1964 'mailchimp_list_id'
1965 , $fca_eoi
1966 , K::get_var( 'list_id' , $fca_eoi )
1967 );
1968 $list_set = ( bool ) K::get_var( 'mailchimp_list_id', $fca_eoi);
1969 }
1970 // End of Hack
1971
1972
1973 if( ! $list_set ) {
1974 $errors[] = __( 'No List selected. You will not be able to use this form.' );
1975 }
1976
1977 $errors = apply_filters( 'fca_eoi_alter_admin_notices', $errors );
1978
1979 foreach ( $errors as $error ) {
1980 echo '<div class="error"><p>' . $error . '</p></div>';
1981 }
1982 }
1983
1984 public function bind_content_filter() {
1985
1986 // Do nothing in backend
1987 if ( is_admin() ) {
1988 return;
1989 }
1990
1991 add_action( 'wp', array( $this, 'content' ), 10 );
1992 }
1993
1994 public function content() {
1995
1996 global $post;
1997
1998 if ( empty( $post ) ) {
1999 return;
2000 }
2001
2002 // Do nothing if viewing an opt-in
2003 if ( 'easy-opt-ins' === $post->post_type ) {
2004 return;
2005 }
2006
2007 // Work only when viewing a post of any type
2008 if ( empty( $post ) ) {
2009 return;
2010 }
2011
2012 $priorities = array();
2013
2014 // Post details
2015 $post_ID = $post->ID;
2016 $post_type = get_post_type( $post_ID );
2017
2018 // Build the array for testing
2019 $post_cond = array(
2020 '*',
2021 $post_type,
2022 '#' . $post_ID,
2023 );
2024 if ( is_front_page() ) {
2025 $post_cond[] = '~';
2026 }
2027
2028 $priorities[] = '#' . $post_ID;
2029
2030 $taxonomies = get_taxonomies('','names');
2031 $post_taxonomies = wp_get_object_terms( $post->ID,$taxonomies);
2032 foreach ( $post_taxonomies as $t ) {
2033 $condition = $post_type . ':' . $t->term_id;
2034
2035 $post_cond[] = $condition;
2036 $priorities[] = $condition;
2037 }
2038
2039 $priorities[] = $post_type;
2040 $priorities[] = '*';
2041
2042 $fca_eoi_last_99_forms = array();
2043 foreach (get_posts( 'posts_per_page=99&post_type=easy-opt-ins' ) as $i => $f ) {
2044 $fca_eoi_last_99_forms[ $i ][ 'post' ] = $f;
2045 $fca_eoi_last_99_forms[ $i ][ 'fca_eoi' ] = get_post_meta( $f->ID, 'fca_eoi', true );
2046 }
2047 // wp_reset_query();
2048
2049 $postboxes = array();
2050
2051 // Append postcode shortcode when the conditions match
2052 foreach( $fca_eoi_last_99_forms as $f) {
2053
2054 // Exclude other layout types
2055 if ( empty ( $f[ 'fca_eoi' ][ 'layout' ] ) ) {
2056 continue;
2057 }
2058 if ( strpos( $f[ 'fca_eoi' ][ 'layout' ], 'postbox_' ) !== 0 ) {
2059 continue;
2060 }
2061
2062 // Get conditions
2063 $eoi_form_cond = K::get_var( 'publish_postbox', $f[ 'fca_eoi' ], array() );
2064
2065 // Append
2066 if ( array_intersect( $eoi_form_cond, $post_cond ) ) {
2067 foreach ( $eoi_form_cond as $cond ) {
2068 if ( empty( $postboxes[ $cond ] ) ) {
2069 $postboxes[ $cond ] = sprintf( '[%s id=%d]', $this->settings['shortcode'], $f['post']->ID );
2070 }
2071 }
2072 }
2073 }
2074
2075 if ( ! empty( $postboxes ) ) {
2076 foreach ( $priorities as $cond ) {
2077 if ( ! empty( $postboxes[ $cond ] ) ) {
2078 $post->post_content .= $postboxes[ $cond ];
2079 return;
2080 }
2081 }
2082
2083 $post->post_content .= reset( $postboxes );
2084 return;
2085 }
2086 }
2087
2088 private function get_tc_token( $form_id ) {
2089 return "$form_id";
2090 }
2091
2092 private function to_call_tc_condition( $name, $arguments ) {
2093 return array_merge( array( $name ), $arguments );
2094 }
2095
2096 private function to_value_tc_condition( $form_id, $name, $eoi_condition ) {
2097 $value = intval( $eoi_condition['value'] );
2098 $params = array( $value );
2099
2100 if ( $name === 'page_views' ) {
2101 $params[] = $this->get_tc_token( $form_id );
2102 } else if ( $name === 'time_on_page' ) {
2103 $params[0] *= 1000;
2104 }
2105
2106 return $this->to_call_tc_condition( $name, $params );
2107 }
2108
2109 private function to_server_pass_tc_condition( $post_id, $parameter, $eoi_condition ) {
2110 $url = add_query_arg( array(
2111 'fca_eoi_tc_condition_pass' => urlencode( $parameter ),
2112 'fca_eoi_tc_condition_value' => urlencode( implode( ',', $eoi_condition['value'] ) ),
2113 'fca_eoi_tc_condition_post_id' => $post_id
2114 ) );
2115
2116 return $this->to_call_tc_condition( 'server_pass', array( $url, 'true' ) );
2117 }
2118
2119 private function to_tc_condition( $form_id, $post_id, $eoi_condition ) {
2120 $eoi_to_tc_value_map = array(
2121 'pageviews' => 'page_views',
2122 'scrolled_percent' => 'scroll_percent',
2123 'time_on_page' => 'time_on_page'
2124 );
2125
2126 $eoi_server_pass_conditions = array( 'include', 'exclude' );
2127
2128 $parameter = $eoi_condition['parameter'];
2129 if ( array_key_exists( $parameter, $eoi_to_tc_value_map ) ) {
2130 return $this->to_value_tc_condition( $form_id, $eoi_to_tc_value_map[ $parameter ], $eoi_condition );
2131 } else if ( in_array( $parameter, $eoi_server_pass_conditions ) ) {
2132 return $this->to_server_pass_tc_condition( $post_id, $parameter, $eoi_condition );
2133 }
2134
2135 return null;
2136 }
2137
2138 private function to_show_every_tc_condition( $form_id, $condition ) {
2139 if ( in_array( $condition, array( 'day', 'month', 'once', 'session' ) ) ) {
2140 return array( $condition, $this->get_tc_token( $form_id ) );
2141 } elseif ( $condition === 'always' ) {
2142 return 'true';
2143 } elseif ( $condition === 'never' ) {
2144 return 'false';
2145 }
2146
2147 return $condition;
2148 }
2149
2150 private function get_tc_conditions( $form_id, $post_id, $conditions ) {
2151 $tc_conditions = array();
2152 $sequence_conditions = array();
2153
2154 if ( ! empty( $conditions['show_every'] ) ) {
2155 $condition = $this->to_show_every_tc_condition( $form_id, $conditions['show_every'] );
2156
2157 if ( $condition === 'false' ) {
2158 return array( 'false' );
2159 } else {
2160 $sequence_conditions[] = $condition;
2161 }
2162 }
2163
2164 if ( ! empty( $conditions['conditions'] ) ) {
2165 foreach ( $conditions['conditions'] as $condition ) {
2166 if ( $condition['parameter'] === 'exit_intervention' ) {
2167 $sequence_conditions[] = 'exit';
2168 continue;
2169 }
2170
2171 $tc_condition = $this->to_tc_condition( $form_id, $post_id, $condition );
2172 if ( ! empty( $tc_condition ) ) {
2173 $tc_conditions[] = $tc_condition;
2174 }
2175 }
2176 }
2177
2178 $has_conditions = ! empty ( $tc_conditions );
2179 if ( $has_conditions ) {
2180 $tc_conditions = array( 'and' => $tc_conditions );
2181 }
2182
2183 if ( $sequence_conditions ) {
2184 if ( $has_conditions ) {
2185 $tc_conditions = array( 'sequence' => array( $tc_conditions ) );
2186 } else {
2187 $tc_conditions = array( 'sequence' => array() );
2188 }
2189 foreach ( $sequence_conditions as $condition ) {
2190 $tc_conditions['sequence'][] = $condition;
2191 }
2192 }
2193
2194 return $tc_conditions;
2195 }
2196
2197 private function echo_tc_conditions_for_form( $form_id, $post_id ) {
2198 $fca_eoi = get_post_meta( $form_id , 'fca_eoi', true );
2199 $publish_lightbox = K::get_var( 'publish_lightbox', $fca_eoi, array() );
2200
2201 header( 'Content-Type: application/json' );
2202 exit( json_encode( $this->get_tc_conditions( $form_id, $post_id, $publish_lightbox ) ) );
2203 }
2204
2205 private function echo_tc_condition_pass( $post_id, $type, $values ) {
2206 $post_cond = array( '*' );
2207
2208 if ( is_front_page() ) {
2209 $post_cond[] = '~';
2210 }
2211
2212 if ( $post_id ) {
2213 $post_type = get_post_type( $post_id );
2214
2215 $post_cond[] = $post_type;
2216 $post_cond[] = '#' . $post_id;
2217
2218 $taxonomies = get_taxonomies( '', 'names' );
2219 $post_taxonomies = wp_get_object_terms( $post_id, $taxonomies );
2220
2221 foreach ( $post_taxonomies as $t ) {
2222 $post_cond[] = $post_type . ':' . $t->term_id;
2223 }
2224 }
2225
2226 $intersect = array_intersect( $values, $post_cond );
2227
2228 if ( $type === 'include' ) {
2229 echo $intersect ? 'true' : 'false';
2230 } else if ( $type === 'exclude' ) {
2231 echo $intersect ? 'false' : 'true';
2232 }
2233
2234 exit;
2235 }
2236
2237 public function parse_tc_condition_request() {
2238 $post_id = empty( $_REQUEST['fca_eoi_tc_condition_post_id'] ) ? 0 : (int) $_REQUEST['fca_eoi_tc_condition_post_id'];
2239
2240 if ( ! empty( $_REQUEST['fca_eoi_tc_conditions_for'] ) ) {
2241 $form_id = (int) $_REQUEST['fca_eoi_tc_conditions_for'];
2242 $this->echo_tc_conditions_for_form( $form_id, $post_id );
2243 }
2244
2245 if ( ! empty( $_REQUEST['fca_eoi_tc_condition_pass'] ) ) {
2246 $this->echo_tc_condition_pass(
2247 $post_id,
2248 $_REQUEST['fca_eoi_tc_condition_pass'],
2249 explode( ',', $_REQUEST['fca_eoi_tc_condition_value'] )
2250 );
2251 }
2252 }
2253
2254 private function get_cookie_configuration() {
2255 $cookie_configuration = array();
2256
2257 if ( defined( 'COOKIEPATH' ) ) {
2258 $path = COOKIEPATH;
2259 if ( ! empty( $path ) ) {
2260 $cookie_configuration['path'] = $path;
2261 }
2262 }
2263
2264 if ( defined( 'COOKIE_DOMAIN' ) ) {
2265 $domain = COOKIE_DOMAIN;
2266 if ( ! empty( $domain ) ) {
2267 $cookie_configuration['domain'] = $domain;
2268 }
2269 }
2270
2271 return $cookie_configuration;
2272 }
2273
2274 public function scan_for_shortcodes( $content ) {
2275 if ( preg_match_all( '/data-optin-cat\s*=\s*["\']?\s*(\d+)/', $content, $matches ) ) {
2276 $this->two_step_ids_on_page = array_map( 'intval', $matches[1] );
2277 }
2278
2279 return $content;
2280 }
2281
2282 public function show_lightbox() {
2283 // Do not show lightbox on mobile if this is the free distribution
2284 if ( $this->settings[ 'distribution' ] === 'free' ) {
2285 $mobile_detect = new Mobile_Detect;
2286 if ( $mobile_detect->isMobile() ) {
2287 return;
2288 }
2289 }
2290
2291 // Get lightboxes
2292 $lightboxes = get_posts( array(
2293 'post_type' => 'easy-opt-ins',
2294 'posts_per_page' => -1,
2295 'orderby' => 'ID',
2296 'meta_key' => 'fca_eoi_layout',
2297 'meta_value' => 'lightbox_',
2298 'meta_compare' => 'like',
2299 ) );
2300
2301 // Exit function if no lightbox found
2302 if( ! $lightboxes ) {
2303 return;
2304 }
2305
2306 $two_step_ids = array();
2307 $traditional_ids = array();
2308
2309 foreach ( $lightboxes as $lightbox ) {
2310
2311 // Get conditions
2312 $lightbox->fca_eoi = get_post_meta( $lightbox->ID , 'fca_eoi', true );
2313 $publish_lightbox_mode = K::get_var( 'publish_lightbox_mode', $lightbox->fca_eoi, array() );
2314
2315 // If on a free distribution, force traditional popup mode
2316 if ( 'free' === $this->settings['distribution'] ) {
2317 $publish_lightbox_mode = 'traditional_popup';
2318 }
2319
2320 if ( 'two_step_optin' === $publish_lightbox_mode ) {
2321 $two_step_ids[] = $lightbox->ID;
2322 } else {
2323 $traditional_ids[] = $lightbox->ID;
2324 }
2325 }
2326
2327 $this->display_traditional_popups($traditional_ids, $this->display_two_step_popups($two_step_ids));
2328 }
2329
2330 private function display_two_step_popups($lightbox_ids) {
2331 $lightbox_ids = array_intersect( $lightbox_ids, $this->two_step_ids_on_page );
2332
2333 if ( empty( $lightbox_ids ) ) {
2334 return false;
2335 }
2336
2337 foreach ( $lightbox_ids as $lightbox_id ) {
2338 $this->prepare_lightbox( $lightbox_id );
2339 }
2340
2341 ?>
2342 <script>
2343 jQuery( function( $ ) {
2344 $( '[data-optin-cat]' ).live( 'click', function( e ) {
2345 var lightbox_id = $( this ).data( 'optin-cat' );
2346 <?php echo EasyOptInsActivity::get_instance()->get_tracking_code( 'lightbox_id', false ) ?>
2347 $.featherlight( $( '#fca_eoi_lightbox_' + lightbox_id ), <?php echo $this->get_featherlight_options() ?> );
2348 e.preventDefault();
2349 } );
2350 } );
2351 </script>
2352 <?php
2353
2354 return true;
2355 }
2356
2357 private function display_traditional_popups( $lightbox_ids, $prerequisites_loaded = false ) {
2358 if ( empty( $lightbox_ids ) ) {
2359 return false;
2360 }
2361
2362 ?>
2363 <script type="text/javascript" src="<?php echo $this->settings['plugin_url'] . '/' . $this->targeting_cat_path ?>"></script>
2364 <script>
2365 (function() {
2366 if ( typeof fca_eoi === "undefined" ) {
2367 fca_eoi = {};
2368 }
2369
2370 var error_text = <?php echo json_encode( $this->settings['error_text'] ); ?>;
2371 Object.keys( error_text ).forEach( function( key ) {
2372 fca_eoi[ key ] = error_text[ key ];
2373 } );
2374
2375 fca_eoi.ajax_url = <?php echo json_encode( admin_url( 'admin-ajax.php' ) ) ?>;
2376 })();
2377
2378 <?php $v = array(
2379 'prerequisites_loaded' => 'p',
2380 'load_prerequisites' => 'l',
2381 'head' => 'h',
2382 'load_script' => 'c',
2383 'load_style' => 'y',
2384 'callback' => 'k',
2385 'vendor_url' => 'v',
2386 'done' => 'n',
2387 'document' => 'd',
2388 'window.location.href' => 'w',
2389 'display_popup' => 's',
2390 'false' => '!1',
2391 'true' => '1',
2392 'evaluable' => 'e',
2393 'descriptors' => 'r',
2394 'form_id' => 'm',
2395 'TargetingCat_OptinCat' => 'g',
2396 'url' => 'u'
2397 ); ?>
2398
2399 <?php ob_start(); ?>
2400
2401 jQuery( function( $ ) {
2402 var
2403 <?php echo $v['document'] ?> = document,
2404 <?php echo $v['window.location.href'] ?> = window.location.href,
2405 <?php echo $v['TargetingCat_OptinCat'] ?> = TargetingCat_OptinCat;
2406
2407 <?php if ( ! $prerequisites_loaded ) { ?>
2408
2409 var
2410 <?php echo $v['prerequisites_loaded'] ?> = <?php echo $v['false'] ?>,
2411 <?php echo $v['head'] ?> = <?php echo $v['document'] ?>.head;
2412
2413 function <?php echo $v['load_script'] ?>( <?php echo $v['url'] ?>, <?php echo $v['callback'] ?> ) {
2414 var e = <?php echo $v['document'] ?>.createElement( 'script' );
2415
2416 e.type = 'text/javascript';
2417 e.src = <?php echo $v['url'] ?>;
2418 e.onload = <?php echo $v['callback'] ?>;
2419
2420 <?php echo $v['head'] ?>.appendChild( e );
2421 }
2422
2423 function <?php echo $v['load_style'] ?>( <?php echo $v['url'] ?>, <?php echo $v['callback'] ?> ) {
2424 var e = <?php echo $v['document'] ?>.createElement( 'link' );
2425
2426 e.rel = 'stylesheet';
2427 e.type = 'text/css';
2428 e.href = <?php echo $v['url'] ?>;
2429 e.onload = <?php echo $v['callback'] ?>;
2430
2431 <?php echo $v['head'] ?>.appendChild( e );
2432 }
2433
2434 function <?php echo $v['load_prerequisites'] ?>( <?php echo $v['callback'] ?> ) {
2435 if ( <?php echo $v['prerequisites_loaded'] ?> ) {
2436 <?php echo $v['callback'] ?>();
2437 return;
2438 }
2439
2440 var t = 7,
2441 <?php echo $v['url'] ?> = <?php echo json_encode( $this->settings[ 'plugin_url' ] . '/assets/' ) ?>,
2442 <?php echo $v['vendor_url'] ?> = <?php echo $v['url'] ?> + 'vendor/',
2443 <?php echo $v['done'] ?> = function() {
2444 if ( ! --t ) {
2445 <?php echo $v['prerequisites_loaded'] ?> = <?php echo $v['true'] ?>;
2446 <?php echo $v['callback'] ?>();
2447 }
2448 };
2449
2450 <?php echo $v['load_style'] ?>( '//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.1.0/css/font-awesome.min.css', <?php echo $v['done'] ?> );
2451
2452 <?php echo $v['load_script'] ?>( <?php echo $v['url'] ?> + 'script.js', <?php echo $v['done'] ?> );
2453 <?php echo $v['load_style'] ?>( <?php echo $v['url'] ?> + 'style<?php echo EasyOptInsLayout::uses_new_css() ? '-new' : '' ?>.css', <?php echo $v['done'] ?> );
2454
2455 <?php echo $v['load_script'] ?>( <?php echo $v['vendor_url'] ?> + 'tooltipster/jquery.tooltipster.min.js', <?php echo $v['done'] ?> );
2456 <?php echo $v['load_style'] ?>( <?php echo $v['vendor_url'] ?> + 'tooltipster/tooltipster.min.css', <?php echo $v['done'] ?> );
2457
2458 <?php echo $v['load_script'] ?>( <?php echo $v['vendor_url'] ?> + 'featherlight/release/featherlight.min.js', <?php echo $v['done'] ?> );
2459 <?php echo $v['load_style'] ?>( <?php echo $v['vendor_url'] ?> + 'featherlight/release/featherlight.min.css', <?php echo $v['done'] ?> );
2460 }
2461
2462 <?php } ?>
2463
2464 function <?php echo $v['display_popup'] ?>( <?php echo $v['form_id'] ?> ) {
2465 var t = <?php echo $prerequisites_loaded ? 1 : 2 ?>,
2466 <?php echo $v['done'] ?> = function() {
2467 if ( ! --t ) {
2468 $.featherlight(
2469 $( '#fca_eoi_lightbox_' + <?php echo $v['form_id'] ?> ),
2470 <?php echo $this->get_featherlight_options() ?>
2471 );
2472 <?php echo EasyOptInsActivity::get_instance()->get_tracking_code( $v['form_id'], false ) ?>
2473 }
2474 };
2475
2476 $.post( <?php echo $v['window.location.href'] ?>, {
2477 fca_eoi_prepare_lightbox: <?php echo $v['form_id'] ?>
2478 }, function( html ) {
2479 <?php echo $v['document'] ?>.body.innerHTML += html;
2480 <?php echo $v['done'] ?>();
2481 } );
2482
2483 <?php if ( ! $prerequisites_loaded ) { ?>
2484 <?php echo $v['load_prerequisites'] ?>( <?php echo $v['done'] ?> );
2485 <?php } ?>
2486 }
2487
2488 <?php echo $v['TargetingCat_OptinCat'] ?>.StorageManagerSessionPermanent.get_instance().default_configuration = <?php echo json_encode( $this->get_cookie_configuration() ) ?>;
2489
2490 <?php echo json_encode( $lightbox_ids ) ?>.forEach( function( <?php echo $v['form_id'] ?> ) {
2491 $.post( <?php echo $v['window.location.href'] ?>, {
2492 <?php if ( is_single() || is_page() ) { ?>
2493 'fca_eoi_tc_condition_post_id': <?php echo get_the_ID() ?>,
2494 <?php } ?>
2495 'fca_eoi_tc_conditions_for': <?php echo $v['form_id'] ?>
2496 }, function( <?php echo $v['descriptors'] ?> ) {
2497 var <?php echo $v['evaluable'] ?> = <?php echo $v['TargetingCat_OptinCat'] ?>.ConditionManager.get_instance().parse_descriptors(
2498 typeof <?php echo $v['descriptors'] ?> === 'string'
2499 ? JSON.parse( <?php echo $v['descriptors'] ?> )
2500 : <?php echo $v['descriptors'] ?>
2501 );
2502 if ( <?php echo $v['evaluable'] ?> ) {
2503 <?php echo $v['evaluable'] ?>.set_pass_callback( function() {
2504 <?php echo $v['display_popup'] ?>( <?php echo $v['form_id'] ?> );
2505 } );
2506 <?php echo $v['evaluable'] ?>.evaluate();
2507 }
2508 } );
2509 } );
2510
2511 $( '.fca_eoi_featherlight' ).live( 'click', function( e ) {
2512 if ( $( e.target ).is( '.fca_eoi_featherlight-inner, .fca_eoi_form_content' ) ) {
2513 $( '.fca_eoi_layout_popup_close' ).click();
2514 }
2515 } );
2516 } );
2517
2518 <?php
2519
2520 echo preg_replace(
2521 array( '/\s+/', '/([-+|\(\){}&=;,:?])\s+/', '/\s+([-+|\(\){}&=?])/', '/;}/' ),
2522 array( ' ', '$1', '$1', '}' ),
2523 trim( ob_get_clean() )
2524 );
2525
2526 ?>
2527 </script>
2528 <?php
2529
2530 return true;
2531 }
2532
2533 public function request_prepare_lightbox() {
2534 if (array_key_exists('fca_eoi_prepare_lightbox', $_POST)) {
2535 $lightbox_id = (int) $_POST['fca_eoi_prepare_lightbox'];
2536 if ( $lightbox_id > 0 ) {
2537 $this->prepare_lightbox( $lightbox_id );
2538 exit;
2539 }
2540 }
2541 }
2542
2543 public function prepare_lightbox( $id ) {
2544 $id = (int) $id;
2545
2546 $featherlight_class = EasyOptInsLayout::uses_new_css() ? 'fca_eoi_featherlight' : 'featherlight';
2547 $content = do_shortcode( "[easy-opt-in id=$id]" );
2548
2549 ?>
2550 <div style="display:none">
2551 <style scoped>.<?php echo $featherlight_class ?>-content { background: transparent !important; }</style>
2552 <div id="fca_eoi_lightbox_<?php echo $id ?>"><?php echo $content ?></div>
2553 </div>
2554 <?php
2555 }
2556
2557 private function get_featherlight_options() {
2558 if ( EasyOptInsLayout::uses_new_css() ) {
2559 $is_iphone = preg_match('/(?:iPhone|iPod);/i', $_SERVER['HTTP_USER_AGENT']);
2560 $is_ios = preg_match( '/(?:iPhone|iPad|iPod).* OS ([\d])_.* Safari/', $_SERVER['HTTP_USER_AGENT'], $matches );
2561
2562 if ( $is_ios ) {
2563 $ios_specific_options = ',
2564 beforeOpen: function() {
2565 var viewport = jQuery( "meta[name=viewport]" );
2566 if ( viewport.length == 0 ) {
2567 viewport = jQuery( "<meta name=\"viewport\" content=\"\"/>" );
2568 jQuery( "head" ).append( viewport );
2569 }
2570
2571 var viewport_content = viewport.attr( "content" );
2572 this.fca_eoi_viewport_content = viewport_content;
2573 viewport.attr( "content", "width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" );
2574
2575 this.fca_eoi_orientation_change_listener = function() {
2576 window.scrollTo( 0, 0 );
2577 jQuery( ".fca_eoi_form input" ).each( function() {
2578 this.blur();
2579 } );
2580 };
2581 window.addEventListener( "orientationchange", this.fca_eoi_orientation_change_listener, false );
2582
2583 this.fca_eoi_touch_move_listener = function( event ) {
2584 event.preventDefault();
2585 };
2586 window.addEventListener( "touchmove", this.fca_eoi_touch_move_listener, false );
2587 },
2588 afterOpen: function() {
2589 jQuery( "body" ).append( "<div class=\"fca_eoi_featherlight_pad\" style=\"display: block; height: 1000px;\"/>" );
2590 },
2591 beforeClose: function() {
2592 jQuery( ".fca_eoi_featherlight_pad" ).remove();
2593 },
2594 afterClose: function() {
2595 if ( this.hasOwnProperty( "fca_eoi_viewport_content" ) ) {
2596 jQuery( "meta[name=viewport]" ).attr( "content", this.fca_eoi_viewport_content );
2597 }
2598
2599 if ( this.hasOwnProperty( "fca_eoi_orientation_change_listener" ) ) {
2600 window.removeEventListener( "orientationchange", this.fca_eoi_orientation_change_listener );
2601 }
2602
2603 if ( this.hasOwnProperty( "fca_eoi_touch_move_listener" ) ) {
2604 window.removeEventListener( "touchmove", this.fca_eoi_touch_move_listener );
2605 }
2606 }';
2607 } else {
2608 $ios_specific_options = '';
2609 }
2610
2611 return '{
2612 namespace: "fca_eoi_featherlight",
2613 otherClose: ".fca_eoi_layout_popup_close"' . $ios_specific_options . ',
2614 variant: ' . ( $is_iphone ? '"fca_eoi_device_iphone"' : 'null' ) . '
2615 }';
2616 } else {
2617 return 'undefined';
2618 }
2619 }
2620 }
2621
2622 function k_selector( $name, $selected_options = array(), $return = false ) {
2623
2624 global $post;
2625 // Dirty fix to restore the global $post
2626 $post_bak = $post;
2627
2628 // Get all post types except media
2629 $post_types = get_post_types( array( 'public' => true ) );
2630 unset( $post_types[ 'attachment' ] );
2631
2632 $ret = ob_start();
2633
2634 // Start ouput
2635 echo '<select data-placeholder="' . __( 'Type to search for posts, categories or pages.' ) . '" name = "' . $name . '[]" class="select2" multiple="multiple" style="width: 27em;">';
2636
2637 // Front page
2638 K::wrap( __( 'Front page' )
2639 , array(
2640 'value' => '~',
2641 'selected' => in_array( '~', $selected_options ),
2642 )
2643 , array( 'in' => 'option' )
2644 );
2645
2646 // All posts
2647 // K::wrap( __( 'All' )
2648 // , array(
2649 // 'value' => '*',
2650 // 'selected' => in_array( '*', $selected_options ),
2651 // )
2652 // , array( 'in' => 'option' )
2653 // );
2654
2655 foreach ($post_types as $post_type => $post_type_args ) {
2656
2657 $post_type_obj = get_post_type_object( $post_type );
2658 $post_type_name = $post_type_obj->labels->singular_name;
2659
2660 $options = array();
2661
2662 // Add taxonomy/terms options
2663 $taxonomies = get_object_taxonomies( $post_type );
2664 foreach ( $taxonomies as $taxonomy ) {
2665 $taxonomy_obj = get_taxonomy( $taxonomy );
2666 $taxonomy_name = $taxonomy_obj->labels->singular_name;
2667 $terms = get_categories("taxonomy=$taxonomy&type=$post_type");
2668 foreach ($terms as $term) {
2669 $options[ 'taxonomies' ][ "$post_type:$term->term_id" ] =
2670 $post_type_name
2671 . "$taxonomy_name"
2672 . "$term->name"
2673 ;
2674 }
2675 }
2676
2677 // Add posts options
2678 $the_query = new WP_Query( "post_type=$post_type&posts_per_page=-1" );
2679 if ( $the_query->have_posts() ) {
2680
2681 while ( $the_query->have_posts() ) {
2682 $the_query->the_post();
2683 $options[ 'posts' ][ '#' . get_the_ID() ] = $post_type_name
2684 . ' ' . __( '' ) . ' '
2685 . '#' . get_the_ID() . ' &ndash; '
2686 . ( get_the_title() ? get_the_title() : __('[Untitled]') )
2687 ;
2688 }
2689 }
2690
2691 // Dirty fix to restore the global $post
2692 $post = $post_bak;
2693
2694 // Posts > All
2695 echo '<optgroup label="' . $post_type_name . '">';
2696 printf(
2697 '<option value="%s" %s >%s</option>'
2698 , $post_type
2699 , ( in_array( $post_type, $selected_options ) ? 'selected' : '' )
2700 , $post_type_name . ' ' . ( '' ) . ' ' . __( 'All' )
2701 );
2702 echo '</optgroup>';
2703
2704 // Posts > Taoxonomies
2705 if ( ! empty( $options[ 'taxonomies' ] ) ) {
2706 printf(
2707 '<optgroup label="%s">'
2708 , $post_type_name . ' ' . __( '' ) . ' ' . __( 'Taxonomies' )
2709 );
2710 foreach ( $options[ 'taxonomies' ] as $k => $v ) {
2711 $selected = ( in_array( $k, $selected_options ) ) ? 'selected="selected"' : '';
2712 printf( '<option value="%s" %s >%s</option>', $k, $selected, $v );
2713 }
2714 echo '</optgroup>';
2715 }
2716
2717 // Posts > content
2718 if ( ! empty( $options[ 'posts' ] ) ) {
2719 printf( '<optgroup label="%s">'
2720 , $post_type_name . ' ' . __( '' ) . ' ' . __( 'Content' )
2721 );
2722 foreach ( $options[ 'posts' ] as $k => $v ) {
2723 $selected = ( in_array( $k, $selected_options ) ) ? 'selected="selected"' : '';
2724 printf( '<option value="%s" %s >%s</option>'
2725 , $k
2726 , $selected
2727 , $v
2728 );
2729 }
2730 echo '</optgroup>';
2731 }
2732 }
2733 echo '</select>';
2734
2735 $ret = ob_get_clean();
2736
2737 if ( $return ) {
2738 return $ret;
2739 } else {
2740 echo $ret;
2741 }
2742 }
2743
2744 function fca_eoi_comp( $a, $b, $op, $negate = false ) {
2745 switch ( $op ) {
2746 case 'eq': return $negate ? ( ! ( $a == $b ) ) : ( $a == $b );
2747 case 'gt': return $negate ? ( ! ( $a > $b ) ) : ( $a > $b );
2748 case 'gte': return $negate ? ( ! ( $a >= $b ) ) : ( $a >= $b );
2749 case 'lt': return $negate ? ( ! ( $a < $b ) ) : ( $a < $b );
2750 case 'lte': return $negate ? ( ! ( $a <= $b ) ) : ( $a <= $b );
2751 }
2752 }
2753