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

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