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

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