PluginProbe
GetResponse Forms by Optin Cat / 1.5.8
GetResponse Forms by Optin Cat v1.5.8
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.5.8, at includes/eoi-post-types.php

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