PluginProbe
Icegram Engage – Popups, Optins, CTAs & Lead Generation / 1.3
Icegram Engage – Popups, Optins, CTAs & Lead Generation v1.3
3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 3.1.34 3.1.35 3.1.36 3.1.37 3.1.38 3.1.39 3.1.4 3.1.40 3.1.41 3.1.42 3.1.43 trunk 1.1 1.1.1 1.1.2 1.10 1.10.1 1.10.10 1.10.11 1.10.12 All 180 releases
icegram / icegram.php

icegram.php in Icegram Engage – Popups, Optins, CTAs & Lead Generation 1.3, at icegram.php

1,166 lines 52.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 * Plugin Name: Icegram
4 * Plugin URI: http://www.icegram.com/
5 * Description: All in one solution to inspire, convert and engage your audiences. Action bars, Popup windows, Messengers, Toast notifications and more. Awesome themes and powerful rules.
6 * Version: 1.3
7 * Author: Icegram
8 * Author URI: http://www.icegram.com/
9 *
10 * Copyright (c) 2014 Icegram
11 * License: GPLv3
12 * License URI: http://www.gnu.org/licenses/gpl-3.0.html
13 *
14 * Text Domain: icegram
15 * Domain Path: /lang/
16 */
17
18 /**
19 * Main class Icegram
20 */
21 class Icegram {
22
23 var $plugin_url;
24 var $plugin_path;
25 var $version;
26 var $_wpautop_tags;
27 var $message_types;
28 var $message_type_objs;
29 var $shortcode_instances;
30 var $available_headlines;
31
32 public static $current_page_id;
33
34 function __construct() {
35
36 $this->version = "1.3";
37 $this->shortcode_instances = array();
38 $this->plugin_url = untrailingslashit( plugins_url( '/', __FILE__ ) );
39 $this->plugin_path = untrailingslashit( plugin_dir_path( __FILE__ ) );
40 $this->include_classes();
41
42 if( is_admin() && current_user_can( 'manage_options' ) ) {
43 new Icegram_Campaign_Admin();
44 new Icegram_Message_Admin();
45 add_action( 'admin_enqueue_scripts', array( &$this, 'enqueue_admin_styles_and_scripts' ) );
46 add_action( 'admin_print_styles', array( &$this, 'remove_preview_button' ) );
47 add_filter( 'post_row_actions', array( &$this , 'remove_row_actions' ), 10, 2 );
48
49 add_action( 'admin_menu', array( &$this, 'admin_menus') );
50 add_action( 'admin_init', array( &$this, 'welcome' ) );
51
52 add_action( 'icegram_settings_after', array( &$this, 'klawoo_subscribe_form' ) );
53 add_action( 'icegram_about_changelog', array( &$this, 'klawoo_subscribe_form' ) );
54 } else {
55 add_action( 'wp_footer', array( &$this, 'display_messages' ) );
56 //add_action( 'wp_head', array( &$this, 'nofollow_noindex' ) );
57 add_action( 'wp_print_scripts', array( &$this, 'identify_current_page' ) );
58 add_shortcode( 'icegram', array( &$this, 'execute_shortcode' ) );
59 add_filter( 'icegram_branding_data', array( &$this , 'branding_data_remove' ), 10 );
60 add_filter( 'icegram_get_valid_campaigns_sql', array( &$this , 'append_to_valid_campaigns_sql' ), 10, 2 );
61 // WPML compatibility
62 add_filter( 'icegram_identify_current_page', array( &$this, 'wpml_get_parent_id' ), 10 );
63 }
64
65 // common
66 add_action( 'init', array( &$this, 'register_campaign_post_type' ) );
67 add_action( 'init', array( &$this, 'register_message_post_type' ) );
68
69 if ( defined( 'DOING_AJAX' ) ) {
70 add_action( 'wp_ajax_icegram_event_track', array( &$this, 'icegram_event_track' ) );
71 add_action( 'wp_ajax_nopriv_icegram_event_track', array( &$this, 'icegram_event_track' ) );
72 add_action( 'wp_ajax_klawoo_subscribe', array( &$this, 'klawoo_subscribe' ) );
73 }
74
75 }
76
77 public function klawoo_subscribe_form() {
78 ?>
79 <div class="wrap">
80 <?php
81 if ( stripos(get_current_screen()->base, 'settings') !== false ) {
82 echo "<h2>".__( 'Get Updates', 'icegram' )."</h2>";
83 }
84 ?>
85 <table class="form-table">
86 <tr>
87 <th scope="row"><?php _e( 'Join our newsletter', 'icegram' ) ?></th>
88 <td>
89 <form name="klawoo_subscribe" action="#" method="POST" accept-charset="utf-8">
90 <input class="ltr" type="text" name="name" id="name" placeholder="Name"/>
91 <input class="regular-text ltr" type="text" name="email" id="email" placeholder="Email"/>
92 <input type="hidden" name="list" value="7I763v6Ldrs3YhJeee5EOgFA"/>
93 <input type="submit" name="submit" id="submit" class="button button-primary" value="Subscribe">
94 <br/>
95 <div id="klawoo_response"></div>
96 </form>
97 </td>
98 </tr>
99 </table>
100 </div>
101 <script type="text/javascript">
102 jQuery(function () {
103 jQuery("form[name=klawoo_subscribe]").submit(function (e) {
104 e.preventDefault();
105
106 jQuery('#klawoo_response').html('');
107 params = jQuery("form[name=klawoo_subscribe]").serializeArray();
108 params.push( {name: 'action', value: 'klawoo_subscribe' });
109
110 jQuery.ajax({
111 method: 'POST',
112 type: 'text',
113 url: "<?php echo admin_url( 'admin-ajax.php' ); ?>",
114 data: params,
115 success: function(response) {
116 if (response != '') {
117 jQuery('#klawoo_response').html(response);
118 } else {
119 jQuery('#klawoo_response').html('error!');
120 }
121 }
122 });
123 });
124 });
125 </script>
126 <?php
127 }
128
129 public function klawoo_subscribe() {
130 $url = 'http://app.klawoo.com/subscribe';
131
132 if( !empty( $_POST ) ) {
133 $params = $_POST;
134 } else {
135 exit();
136 }
137 $method = 'POST';
138 $qs = http_build_query( $params );
139
140 $options = array(
141 'timeout' => 15,
142 'method' => $method
143 );
144
145 if ( $method == 'POST' ) {
146 $options['body'] = $qs;
147 } else {
148 if ( strpos( $url, '?' ) !== false ) {
149 $url .= '&'.$qs;
150 } else {
151 $url .= '?'.$qs;
152 }
153 }
154
155 $response = wp_remote_request( $url, $options );
156 if ( wp_remote_retrieve_response_code( $response ) == 200 ) {
157 $data = $response['body'];
158 if ( $data != 'error' ) {
159
160 $message_start = substr( $data, strpos( $data,'<body>' ) + 6 );
161 $remove = substr( $message_start, strpos( $message_start,'</body>' ) );
162 $message = trim( str_replace( $remove, '', $message_start ) );
163 echo ( $message );
164 exit();
165 }
166 }
167 exit();
168 }
169
170 public function icegram_event_track() {
171
172 if( !empty( $_POST['event_data'] ) ) {
173
174 $messages_shown = (array) explode(",", html_entity_decode( (!empty($_COOKIE['icegram_messages_shown']) ? stripslashes( $_COOKIE['icegram_messages_shown'] ) : '') ) );
175
176 foreach ( $_POST['event_data'] as $event ) {
177 switch ($event['type']) {
178 case 'shown':
179 if (is_array($event['params']) && !empty($event['params']['message_id'])) {
180 $messages_shown[] = $event['params']['message_id'];
181 }
182 break;
183
184 default:
185 break;
186 }
187
188 // Emit event for other plugins to handle it
189 do_action('icegram_event_track', $event);
190 do_action('icegram_event_track_'.$event['type'], $event['params']);
191 }
192 }
193 $messages_shown = array_values ( array_filter ( array_unique($messages_shown) ) );
194 setcookie('icegram_messages_shown', implode(",", $messages_shown), 0, '/');
195 exit();
196 }
197
198 static function install() {
199 // Redirect to welcome screen
200 delete_option( '_icegram_activation_redirect' );
201 add_option( '_icegram_activation_redirect', 'pending' );
202 }
203
204 public function welcome() {
205
206 $this->db_update();
207 // Bail if no activation redirect transient is set
208 if ( false === get_option( '_icegram_activation_redirect' ) )
209 return;
210
211 // Delete the redirect transient
212 delete_option( '_icegram_activation_redirect' );
213
214 wp_redirect( admin_url( 'edit.php?post_type=ig_campaign&page=icegram-support' ) );
215 exit;
216 }
217
218 function db_update() {
219
220 $current_db_version = get_option( 'icegram_db_version' );
221 if ( !$current_db_version || version_compare( $current_db_version, '1.2', '<' ) ) {
222 include( 'updates/icegram-update-1.2.php' );
223 }
224
225 }
226
227 public function admin_menus() {
228
229 $welcome_page_title = __( 'Welcome to Icegram', 'icegram' );
230 $settings_page_title = __( 'Settings', 'icegram' );
231 $addons_page_title = __( 'Add-ons', 'icegram' );
232
233 /*
234 if ( false === ( $ig_addons = get_transient( 'icegram_addons_data' ) ) ) {
235 $this->check_for_addons();
236 }
237 $addon_count = get_option( 'icegram_no_of_addons' );
238
239 if ($addon_count > 0) {
240 $addons_page_title .= " <span class='awaiting-mod update-plugins count-$addon_count'><span class='addon-count'>" . number_format_i18n( $addon_count ) . "</span></span>" ;
241 }
242 */
243
244 $menu_title = __( 'Docs & Support', 'icegram' );
245 $about = add_submenu_page( 'edit.php?post_type=ig_campaign', $welcome_page_title, $menu_title, 'manage_options', 'icegram-support', array( $this, 'about_screen' ) );
246 $settings = add_submenu_page( 'edit.php?post_type=ig_campaign', $settings_page_title, $settings_page_title, 'manage_options', 'icegram-settings', array( $this, 'settings_screen' ) );
247 $addons = add_submenu_page( 'edit.php?post_type=ig_campaign', $addons_page_title, $addons_page_title, 'manage_options', 'icegram-addons', array( $this, 'addons_screen' ) );
248
249 add_action( 'admin_print_styles-'. $about, array( $this, 'admin_css' ) );
250 add_action( 'admin_print_styles-'. $settings, array( $this, 'admin_css' ) );
251 add_action( 'admin_print_styles-'. $addons, array( $this, 'admin_css' ) );
252
253 }
254
255 public function admin_css() {
256 wp_enqueue_style( 'icegram-activation', $this->plugin_url . '/assets/css/admin.css' );
257 }
258
259 public function about_screen() {
260
261 // Import data if not done already
262 if( false === get_option( 'icegram_sample_data_imported' ) ) {
263 $this->import( $this->get_sample_data() );
264 }
265
266 include ( 'about-icegram.php' );
267 }
268
269 public function settings_screen() {
270 include ( 'settings.php' );
271 }
272
273 public function addons_screen() {
274 $ig_addons = $this->check_for_addons( true );
275 include ( 'addons.php' );
276 }
277
278 public function check_for_addons( $force_update = false ) {
279
280 if ( $force_update === true || false === ( $ig_addons = get_transient( 'icegram_addons_data' ) ) ) {
281 $ig_addons_json = wp_remote_get( 'http://icegram.com/addons.json', array( 'user-agent' => 'Icegram Addons' ) );
282
283 if ( ! is_wp_error( $ig_addons_json ) ) {
284 $ig_addons = json_decode( wp_remote_retrieve_body( $ig_addons_json ) );
285 $addon_count = get_option( 'icegram_no_of_addons', 0 );
286 if ( !empty($ig_addons) && is_array($ig_addons) ) {
287 set_transient( 'icegram_addons_data', $ig_addons, 24 * HOUR_IN_SECONDS ); // 1 day
288 update_option( 'icegram_no_of_addons', count( $ig_addons ) - $addon_count ); // display count of newly added addons
289 }
290 }
291 }
292 return $ig_addons;
293 }
294
295 public function branding_data_remove( $icegram_branding_data ) {
296 if( !empty( $icegram_branding_data ) && 'yes' != get_option('icegram_share_love', 'no') ) {
297 $icegram_branding_data['powered_by_logo'] = '';
298 $icegram_branding_data['powered_by_text'] = '';
299 }
300 return $icegram_branding_data;
301 }
302
303 function execute_shortcode( $atts = array() ) {
304 // When shortcode is called, it will only prepare an array with conditions
305 // And add a placeholder div
306 // Display will happen in footer via display_messages()
307 $i = count($this->shortcode_instances);
308 $this->shortcode_instances[ $i ] = shortcode_atts( array(
309 'campaigns' => '',
310 'messages' => '',
311 'skip_others' => 'no'
312 ), $atts );
313 $html[] = "<div class='ig_shortcode_container' id='icegram_shortcode_{$i}'";
314 foreach ($atts as $key => $value) {
315 $value = str_replace(",", " ", $value);
316 $html[] = " data-{$key}=\"".htmlentities($value)."\" ";
317 }
318 $html[] = " >"."</div>";
319 return implode(" ", $html);
320 }
321
322 // Do not index Icegram campaigns / messages...
323 // Not using currently - made custom post types non public...
324 function nofollow_noindex() {
325 $post = get_queried_object();
326 if ( (!empty($post) && !empty( $post->post_type ) && ( $post->post_type == 'ig_campaign' || $post->post_type == 'ig_message' ))
327 || is_post_type_archive( array('ig_message', 'ig_campaign') ) ) {
328 echo PHP_EOL . '<meta name="robots" content="NOINDEX,NOFOLLOW" />' . PHP_EOL;
329 }
330 }
331
332 function display_messages() {
333
334 $skip_others = $preview_mode = false;
335 $campaign_ids = $message_ids = array();
336
337 // Pull in message and campaign IDs from shortcodes - if set
338 if( !empty( $this->shortcode_instances ) ) {
339 foreach ($this->shortcode_instances as $i => $value) {
340 $cids = array_map( 'trim', (array) explode( ',', $value['campaigns'] ) );
341 $mids = array_map( 'trim', (array) explode( ',', $value['messages'] ) );
342 if ($value['skip_others'] == 'yes' && (!empty($cids) || !empty($mids))) {
343 $skip_others = true;
344 }
345 $campaign_ids = array_merge($campaign_ids, $cids);
346 $message_ids = array_merge($message_ids, $mids);
347 }
348 }
349
350 if( !empty( $_GET['campaign_preview_id'] ) && current_user_can( 'manage_options' ) ) {
351 $campaign_ids = array( $_GET['campaign_preview_id'] );
352 $preview_mode = true;
353 }
354
355 $messages = $this->get_valid_messages( $message_ids, $campaign_ids, $preview_mode, $skip_others );
356
357 if( empty( $messages ) ) {
358 return;
359 }
360
361 $messages_shown = (array) explode(",", html_entity_decode( (!empty($_COOKIE['icegram_messages_shown']) ? stripslashes( $_COOKIE['icegram_messages_shown'] ) : '') ) );
362 $types_shown = array(); $messages_to_show_ids = array();
363
364 foreach ( $messages as $key => $message_data ) {
365
366 if( !is_array( $message_data ) || empty( $message_data ) ) {
367 continue;
368 }
369
370 // Don't show a seen message again - if needed
371 if( !empty( $message_data['id'] ) &&
372 empty( $_GET['campaign_preview_id'] ) &&
373 in_array( $message_data['id'], $messages_shown ) &&
374 !empty( $message_data['retargeting'] ) &&
375 $message_data['retargeting'] == 'yes'
376 ) {
377 unset( $messages[$key] );
378 continue;
379 }
380
381 // Avoid showing the same message twice
382 if (in_array($message_data['id'], $messages_to_show_ids)) {
383 unset ( $messages[$key] );
384 continue;
385 } else {
386 $messages_to_show_ids[] = $message_data['id'];
387 }
388
389 $types_shown[] = $message_data['type'];
390
391 /*
392 // Our own implementation so WP does not mess with script, style and pre tags
393 add_filter('the_content', array( $this, 'before_wpautop' ) , 9);
394 add_filter('the_content', array( $this, 'after_wpautop' ) , 11);
395 $messages[$key]['message'] = apply_filters( 'the_content', $message_data['message'] );
396 remove_filter('the_content', array( $this, 'before_wpautop' ) , 9);
397 remove_filter('the_content', array( $this, 'after_wpautop' ) , 11);
398 */
399 // Redo the_content functionality to avoid other plugins adding extraneous code to messages
400 $content = $message_data['message'];
401 $content = convert_chars( convert_smilies( wptexturize( $content ) ) );
402 if(isset($GLOBALS['wp_embed'])) {
403 $content = $GLOBALS['wp_embed']->autoembed($content);
404 }
405 $content = $this->after_wpautop( wpautop( $this->before_wpautop( $content ) ) );
406 $content = do_shortcode( shortcode_unautop( $content ) );
407 $messages[$key]['message'] = $content;
408
409 }
410
411 if( empty( $messages ) )
412 return;
413
414 wp_register_script( 'icegram_js', $this->plugin_url . '/assets/js/icegram.js', array ( 'jquery' ), '', true);
415 wp_enqueue_style( 'icegram_css', $this->plugin_url . '/assets/css/frontend.css' );
416 wp_enqueue_style( 'dashicons' );
417
418 $icegram_default = apply_filters( 'icegram_branding_data',
419 array ( 'icon' => $this->plugin_url . '/assets/images/icegram-logo-branding-64-grey.png',
420 'powered_by_logo' => $this->plugin_url . '/assets/images/icegram-logo-branding-64-grey.png',
421 'powered_by_text' => __( 'Powered by Icegram', 'icegram' )
422 ) );
423 $messages = apply_filters( 'icegram_messages_to_show', $messages );
424 $icegram = array ( 'messages' => array_values( $messages ),
425 'ajax_url' => admin_url( 'admin-ajax.php' ),
426 'preview_id' => !empty( $_GET['campaign_preview_id'] ) ? $_GET['campaign_preview_id'] : '',
427 'defaults' => $icegram_default
428 );
429 if (empty($icegram['preview_id'])) {
430 unset($icegram['preview_id']);
431 }
432
433 if( !wp_script_is( 'icegram_js' ) ) {
434 wp_enqueue_script( 'icegram_js' );
435 wp_localize_script( 'icegram_js', 'icegram_data', $icegram );
436 }
437
438 // Load JS and default CSS
439 $types_shown = array_unique($types_shown);
440
441 if (in_array('popup', $types_shown)) {
442 wp_enqueue_script( 'thickbox' );
443 wp_enqueue_style( 'thickbox' );
444 }
445
446 foreach ($types_shown as $message_type) {
447 wp_register_script( 'icegram_message_type_'.$message_type, $this->message_types[$message_type]['baseurl'] . "main.js" , array ( 'icegram_js' ), '', true );
448 wp_enqueue_script( 'icegram_message_type_'.$message_type );
449 wp_enqueue_style( 'icegram_css_'.$message_type, $this->message_types[$message_type]['baseurl'] . 'default.css' );
450 }
451
452 // Load theme CSS
453 foreach ($messages as $message) {
454 if (!empty( $this->message_types[ $message['type'] ]['themes'][ $message['theme'] ]) ) {
455 $theme = $this->message_types[ $message['type'] ]['themes'][ $message['theme'] ];
456 wp_enqueue_style( 'icegram_css_'.$message['type'].'_'.$message['theme'], $theme['baseurl'] . $message['theme'].'.css' );
457 }
458 }
459
460 }
461
462 function enqueue_admin_styles_and_scripts() {
463
464 $screen = get_current_screen();
465 if ( !in_array( $screen->id, array( 'ig_campaign', 'ig_message' ), true ) ) return;
466
467 // Register scripts
468 wp_register_script( 'icegram_writepanel', $this->plugin_url . '/assets/js/admin.js' , array ( 'jquery', 'wp-color-picker' ) );
469 wp_register_script( 'icegram_chosen', $this->plugin_url . '/assets/js/chosen.jquery.min.js' , array ( 'jquery' ), '1.0' );
470 wp_register_script( 'icegram_ajax-chosen', $this->plugin_url . '/assets/js/ajax-chosen.jquery.min.js' , array ( 'icegram_chosen' ), '1.0' );
471 wp_register_script( 'icegram_tiptip', $this->plugin_url . '/assets/js/jquery.tipTip.min.js' , array ( 'jquery' ), get_bloginfo( 'version' ) );
472
473 wp_enqueue_script( 'icegram_writepanel' );
474 wp_enqueue_script( 'icegram_ajax-chosen' );
475 wp_enqueue_script( 'icegram_tiptip' );
476 wp_enqueue_script( 'thickbox' );
477
478 $icegram_writepanel_params = array ( 'ajax_url' => admin_url( 'admin-ajax.php' ), 'search_message_nonce' => wp_create_nonce( "search-messages" ) );
479 $this->available_headlines = apply_filters( 'icegram_available_headlines', array() );
480 $icegram_writepanel_params = array_merge( $icegram_writepanel_params, array( 'available_headlines' => $this->available_headlines ) );
481
482 wp_localize_script( 'icegram_writepanel', 'icegram_writepanel_params', $icegram_writepanel_params );
483
484 wp_enqueue_style( 'thickbox' );
485 wp_enqueue_style( 'dashicons' );
486 wp_enqueue_style( 'wp-color-picker' );
487 wp_enqueue_style( 'icegram_admin_styles', $this->plugin_url . '/assets/css/admin.css' );
488 wp_enqueue_style( 'icegram_jquery-ui-style', $this->plugin_url . '/assets/css/jquery-ui.min.css' );
489 wp_enqueue_style( 'icegram_chosen_styles', $this->plugin_url . '/assets/css/chosen.min.css' );
490
491 if ( !wp_script_is( 'jquery-ui-datepicker' ) ) {
492 wp_enqueue_script( 'jquery-ui-datepicker' );
493 }
494
495 }
496
497 public static function get_platform() {
498 $platform = '';
499 $user_agent = trim(strtolower($_SERVER['HTTP_USER_AGENT']));
500 $pattern = '/(android\s\d|blackberry|ip(hone|ad|od)|iemobile|webos|palm|symbian|kindle|windows|win64|wow64|macintosh|intel\smac\sos\sx|ppx\smac\sos\sx|googlebot|googlebot-mobile)/';
501 if ( preg_match( $pattern, $user_agent, $matches ) ) {
502 $platform = $matches[0];
503 }
504
505 switch ( $platform ) {
506
507 /* phones / smartphones */
508 case 'android 1':
509 case 'android 2':
510 case 'blackberry':
511 case 'iphone':
512 case 'ipod':
513 case 'iemobile':
514 case 'webos':
515 case 'palm':
516 case 'symbian':
517 case 'googlebot-mobile':
518 $platform = 'mobile';
519 break;
520
521 /* tablets */
522 case 'android 3':
523 case 'android 4':
524 case 'ipad':
525 case 'kindle':
526 $platform = 'tablet';
527 break;
528
529 /* desktops / laptops */
530 case 'windows':
531 case 'win64':
532 case 'wow64':
533 case 'macintosh':
534 case 'ppx mac os x':
535 case 'intel mac os x':
536 case 'googlebot':
537 $platform = 'laptop';
538 break;
539
540 /* in case nothing else matches */
541 default:
542 $platform = 'laptop';
543 break;
544 }
545 return $platform;
546 }
547
548 function get_message_data( $message_ids = array(), $preview = false ) {
549 global $wpdb;
550
551 $message_data = array();
552 $meta_key = $preview ? 'icegram_message_preview_data' : 'icegram_message_data';
553 $message_data_query = "SELECT post_id, meta_value FROM {$wpdb->prefix}postmeta WHERE meta_key LIKE '$meta_key'";
554 if ( !empty( $message_ids ) && is_array( $message_ids ) ) {
555 // For WPML compatibility
556 if ( function_exists('icl_object_id') ) {
557 $wpml_settings = get_option('icl_sitepress_settings');
558 $original_if_missing = (is_array($wpml_settings) && array_key_exists('show_untranslated_blog_posts', $wpml_settings) && !empty($wpml_settings['show_untranslated_blog_posts']) ) ? true : false;
559 foreach ($message_ids as $i=>$id ) {
560 $message_ids[ $i ] = icl_object_id( $id, 'ig_message', $original_if_missing );
561 }
562 }
563 $message_ids = array_filter(array_unique($message_ids));
564 if ( !empty( $message_ids ) ) {
565 $message_data_query .= " AND post_id IN ( " . implode( ',', $message_ids ) . " )";
566 $message_data_results = $wpdb->get_results( $message_data_query, 'ARRAY_A' );
567 foreach ( $message_data_results as $message_data_result ) {
568 $message_data[$message_data_result['post_id']] = maybe_unserialize( $message_data_result['meta_value'] );
569 }
570 }
571 }
572 return $message_data;
573 }
574
575 function get_valid_messages( $message_ids = array(), $campaign_ids = array(), $preview_mode = false, $skip_others = false) {
576
577 $valid_messages = $valid_campaigns = $message_campaign_map = array();
578
579 $campaign_ids = array_filter(array_unique($campaign_ids));
580 $message_ids = array_filter(array_unique($message_ids));
581
582 if ( !empty( $campaign_ids ) ) {
583 $valid_campaigns = $this->get_valid_campaigns( $campaign_ids, true );
584 }
585 // When skip_others is true, we won't load campaigns / messages from db
586 if (!$skip_others && !$preview_mode) {
587 $campaigns = $this->get_valid_campaigns();
588 if (!empty($campaigns)) {
589 foreach ($campaigns as $id => $campaign) {
590 if (!array_key_exists($id, $valid_campaigns)) {
591 $valid_campaigns[ $id ] = $campaign;
592 }
593 }
594 }
595 }
596 // Create a map to look up campaign id for a given message
597 if( !empty( $valid_campaigns ) ) {
598 foreach ($valid_campaigns as $id => $campaign) {
599 if ($preview_mode) {
600 $campaign->messages = get_post_meta( $id, 'campaign_preview', true );
601 }
602
603 foreach( $campaign->messages as $msg) {
604 $message_ids[] = $msg['id'];
605 if (!array_key_exists( $msg['id'], $message_campaign_map)) {
606 $message_campaign_map[ $msg['id'] ] = $id;
607 }
608 }
609 }
610 }
611
612 // We don't display same message twice...
613 $message_ids = array_unique($message_ids);
614
615 if( empty( $message_ids ) ) {
616 return array();
617 }
618 $valid_messages = $this->get_message_data( $message_ids, $preview_mode );
619
620 foreach ($valid_messages as $id => $message_data) {
621 // Remove message if message type is uninstalled
622 $class_name = 'Icegram_Message_Type_' . str_replace(' ', '_', ucwords(str_replace('-', ' ', $message_data['type'])));
623 if( !class_exists( $class_name ) ) {
624 unset( $valid_messages[$id] );
625 continue;
626 }
627 $message_data['delay_time'] = 0;
628 $message_data['retargeting'] = '';
629 $message_data['campaign_id'] = ($preview_mode) ? $_GET['campaign_preview_id'] : '';
630
631 // Pull display time and retargeting rule from campaign if possible
632 if (!empty($message_campaign_map[ $id ])) {
633 $message_data['campaign_id'] = $message_campaign_map[ $id ];
634 $campaign = $valid_campaigns[ $message_data['campaign_id'] ];
635 if (!empty($campaign) && $campaign instanceof Icegram_Campaign) {
636 $message_meta_from_campaign = $campaign->get_message_meta_by_id( $id );
637 if (!empty($message_meta_from_campaign['time'])) {
638 $message_data['delay_time'] = $message_meta_from_campaign['time'];
639 }
640 $rule_value = $campaign->get_rule_value('retargeting');
641 $message_data['retargeting'] = !empty( $rule_value['retargeting'] ) ? $rule_value['retargeting'] : '';
642 }
643 }
644 $valid_messages[$id] = $message_data;
645 }
646
647 $valid_messages = apply_filters( 'icegram_valid_messages', $valid_messages );
648 return $valid_messages;
649 }
650
651 function get_valid_campaigns( $campaign_ids = array(), $skip_page_check = false ) {
652 global $wpdb;
653
654 if ( empty( $campaign_ids ) ) {
655 $sql = "SELECT pm.post_id
656 FROM {$wpdb->prefix}posts AS p
657 LEFT JOIN {$wpdb->prefix}postmeta AS pm ON ( pm.post_id = p.ID )
658 WHERE p.post_status = 'publish' ";
659 // Filter handler within this file (and possibly others) will append to this SQL
660 // and provide arguments for wpdb->prepare if needed.
661 // First element in the array is SQL, remaining are values for placeholders in SQL
662 $sql_params = apply_filters( 'icegram_get_valid_campaigns_sql', array($sql), array() );
663 $campaign_ids = $wpdb->get_col( $wpdb->prepare( array_shift($sql_params), $sql_params ) );
664 }
665 $valid_campaigns = array();
666 foreach ( (array) $campaign_ids as $campaign_id ) {
667 $campaign = new Icegram_Campaign( $campaign_id );
668 if ( $campaign->is_valid( array('skip_page_check' => $skip_page_check) ) ) {
669 $valid_campaigns[$campaign_id] = $campaign;
670 } else {
671 // Campgain is invalid!
672 }
673 }
674 return $valid_campaigns;
675 }
676
677 function append_to_valid_campaigns_sql( $sql_params = array(), $options = array() ) {
678
679 // Page check conditions
680 $pid = Icegram::get_current_page_id();
681 $sql = " AND (
682 pm.meta_key = 'icegram_campaign_target_rules' AND (
683 ( pm.meta_value LIKE '%%%s%%' )
684 OR ( pm.meta_value LIKE '%%%s%%' AND pm.meta_value LIKE '%%%s%%' AND pm.meta_value LIKE '%%%s%%' )
685 ";
686 $sql_params[] = 's:8:"sitewide";s:3:"yes";';
687 $sql_params[] = 's:10:"other_page";s:3:"yes";';
688 $sql_params[] = 's:7:"page_id";a:';
689 $sql_params[] = serialize( (string) $pid );
690
691 if (is_home() || is_front_page()) {
692 $sql .= " OR ( pm.meta_value LIKE '%%%s%%' )
693 ";
694 $sql_params[] = 's:8:"homepage";s:3:"yes";';
695 }
696 $sql .=" ) )";
697 $sql_params[0] .= $sql;
698
699 //s:9:"logged_in";s:3:"all";
700
701 return $sql_params;
702 }
703
704 // Include all classes required for Icegram plugin
705 function include_classes() {
706
707 $classes = glob( $this->plugin_path . '/classes/*.php' );
708 foreach ( $classes as $file ) {
709 // Files with 'admin' in their name are included only for admin section
710 if ( is_file( $file ) && ( (strpos($file, '-admin') >= 0 && is_admin()) || (strpos($file, '-admin') === false) ) ) {
711 include_once $file;
712 }
713 }
714
715 // Load built in message types
716 $icegram_message_type_basedirs = glob( $this->plugin_path . '/message-types/*' );
717 // Allow other plugins to add new message types
718 $icegram_message_type_basedirs = apply_filters( 'icegram_message_type_basedirs', $icegram_message_type_basedirs );
719 // Set up different message type classes
720 foreach ( $icegram_message_type_basedirs as $dir ) {
721 $type = basename ( $dir );
722 $class_file = $dir . "/main.php";
723 if( is_file( $class_file ) ) {
724 include_once( $class_file );
725 }
726 $class_name = 'Icegram_Message_Type_' . str_replace(' ', '_', ucwords(str_replace('-', ' ', $type)));
727 if (class_exists($class_name)) {
728 $this->message_type_objs[ $type ] = new $class_name();
729 }
730 }
731 $this->message_types = apply_filters( 'icegram_message_types', array() );
732
733 }
734
735 // Register Campaign post type
736 function register_campaign_post_type() {
737 $labels = array(
738 'name' => __( 'Campaigns', 'icegram' ),
739 'singular_name' => __( 'Campaign', 'icegram' ),
740 'add_new' => __( 'Add New Campaign', 'icegram' ),
741 'add_new_item' => __( 'Add New Campaign', 'icegram' ),
742 'edit_item' => __( 'Edit Campaign', 'icegram' ),
743 'new_item' => __( 'New Campaign', 'icegram' ),
744 'all_items' => __( 'Campaigns', 'icegram' ),
745 'view_item' => __( 'View Campaign', 'icegram' ),
746 'search_items' => __( 'Search Campaigns', 'icegram' ),
747 'not_found' => __( 'No campaigns found', 'icegram' ),
748 'not_found_in_trash' => __( 'No campaigns found in Trash', 'icegram' ),
749 'parent_item_colon' => __( '', 'icegram' ),
750 'menu_name' => __( 'Icegram', 'icegram' )
751 );
752
753 $args = array(
754 'labels' => $labels,
755 'menu_icon' => 'dashicons-info',
756 'public' => false,
757 'publicly_queryable' => false,
758 'show_ui' => true,
759 'show_in_menu' => true,
760 'query_var' => true,
761 'rewrite' => array( 'slug' => 'ig_campaign' ),
762 'capability_type' => 'post',
763 'has_archive' => false,
764 'hierarchical' => false,
765 'menu_position' => null,
766 'supports' => array( 'title', 'editor' )
767 );
768
769 register_post_type( 'ig_campaign', $args );
770 }
771
772 // Register Message post type
773 function register_message_post_type() {
774 $labels = array(
775 'name' => __( 'Messages', 'icegram' ),
776 'singular_name' => __( 'Message', 'icegram' ),
777 'add_new' => __( 'Create New', 'icegram' ),
778 'add_new_item' => __( 'Create New Message', 'icegram' ),
779 'edit_item' => __( 'Edit Message', 'icegram' ),
780 'new_item' => __( 'New Message', 'icegram' ),
781 'all_items' => __( 'Messages', 'icegram' ),
782 'view_item' => __( 'View Message', 'icegram' ),
783 'search_items' => __( 'Search Messages', 'icegram' ),
784 'not_found' => __( 'No messages found', 'icegram' ),
785 'not_found_in_trash' => __( 'No messages found in Trash', 'icegram' ),
786 'parent_item_colon' => __( '', 'icegram' ),
787 'menu_name' => __( 'Messages', 'icegram' )
788 );
789
790 $args = array(
791 'labels' => $labels,
792 'public' => false,
793 'publicly_queryable' => false,
794 'show_ui' => true,
795 'show_in_menu' => 'edit.php?post_type=ig_campaign',
796 'query_var' => true,
797 'rewrite' => array( 'slug' => 'ig_message' ),
798 'capability_type' => 'post',
799 'has_archive' => false,
800 'hierarchical' => false,
801 'menu_position' => null,
802 'supports' => array( 'title' )
803 );
804
805 register_post_type( 'ig_message', $args );
806 }
807
808 function import( $data = array() ) {
809
810 if ( empty( $data['campaigns'] ) && empty( $data['messages'] ) ) return;
811
812 $default_theme = $default_type = '';
813 $first_message_type = current( $this->message_types );
814
815 if( is_array( $first_message_type ) ) {
816 $default_type = $first_message_type['type'];
817 if( !empty( $first_message_type['themes'] ) ) {
818 $default_theme = key( $first_message_type['themes'] );
819 }
820 }
821
822 $new_campaign_ids = array();
823 foreach ( (array) $data['campaigns'] as $campaign ) {
824
825 $args = array(
826 'post_content' => ( !empty( $campaign['post_content'] ) ) ? esc_attr( $campaign['post_content'] ) : '',
827 'post_name' => ( !empty( $campaign['post_title'] ) ) ? sanitize_title( $campaign['post_title'] ) : '',
828 'post_title' => ( !empty( $campaign['post_title'] ) ) ? $campaign['post_title'] : '',
829 'post_status' => ( !empty( $campaign['post_status'] ) ) ? $campaign['post_status'] : 'draft',
830 'post_type' => 'ig_campaign'
831 );
832
833 $new_campaign_id = wp_insert_post( $args );
834 $new_campaign_ids[] = $new_campaign_id;
835
836 if ( !empty( $campaign['target_rules'] ) ) {
837
838 $defaults = array (
839 'homepage' => 'yes',
840 'when' => 'always',
841 'from' => '',
842 'to' => '',
843 'mobile' => 'yes',
844 'tablet' => 'yes',
845 'laptop' => 'yes',
846 'logged_in' => 'all'
847 );
848
849 $target_rules = wp_parse_args( $campaign['target_rules'], $defaults );
850 update_post_meta( $new_campaign_id, 'icegram_campaign_target_rules', $target_rules );
851 }
852
853 if ( !empty( $campaign['messages'] ) ) {
854
855 $messages = array();
856
857 foreach ( $campaign['messages'] as $message ) {
858
859 if ( !is_array( $message ) ) continue;
860
861 $args = array(
862 'post_content' => ( !empty( $message['message'] ) ) ? esc_attr( $message['message'] ) : '',
863 'post_name' => ( !empty( $message['post_title'] ) ) ? sanitize_title( $message['post_title'] ) : '',
864 'post_title' => ( !empty( $message['post_title'] ) ) ? $message['post_title'] : '',
865 'post_status' => ( !empty( $message['post_status'] ) ) ? $message['post_status'] : 'publish',
866 'post_type' => 'ig_message'
867 );
868
869 $new_message_id = wp_insert_post( $args );
870 $new_message = array(
871 'id' => $new_message_id,
872 'time' => ( !empty( $message['time'] ) ) ? $message['time'] : 0
873 );
874 $messages[] = $new_message;
875
876 unset( $message['post_content'] );
877 unset( $message['time'] );
878
879 $message['id'] = $new_message_id;
880
881 $defaults = array (
882 'post_title' => '',
883 'type' => $default_type,
884 'theme' => $default_theme,
885 'animation' => '',
886 'headline' => '',
887 'label' => '',
888 'link' => '',
889 'icon' => '',
890 'message' => '',
891 'position' => '',
892 'text_color' => '#000000',
893 'bg_color' => '#ffffff',
894 'id' => ''
895 );
896
897 $icegram_message_data = wp_parse_args( $message, $defaults );
898
899 if ( !empty( $icegram_message_data ) ) {
900 update_post_meta( $new_message_id, 'icegram_message_data', $icegram_message_data );
901 update_post_meta( $new_message_id, 'icegram_message_preview_data', $icegram_message_data );
902 }
903 }
904
905 if ( !empty( $campaign['messages'] ) ) {
906 update_post_meta( $new_campaign_id, 'messages', $messages );
907 update_post_meta( $new_campaign_id, 'campaign_preview', $messages );
908 }
909
910 }
911 }
912 if( !empty( $new_campaign_ids ) ) {
913 update_option( 'icegram_sample_data_imported', $new_campaign_ids );
914 }
915
916 }
917
918 function get_sample_data() {
919
920 return array(
921 'campaigns' => array(
922 array(
923 'post_name' => '',
924 'post_title' => 'My First Icegram Campaign',
925 'target_rules' => array (
926 'homepage' => 'yes',
927 'when' => 'always',
928 'from' => '',
929 'to' => '',
930 'mobile' => 'yes',
931 'tablet' => 'yes',
932 'laptop' => 'yes',
933 'logged_in' => 'all'
934 ),
935 'messages' => array(
936 array (
937 'post_title' => 'Get 2x more Contacts with Your Website',
938 'post_status' => 'publish',
939 'time' => '0',
940 'type' => 'action-bar',
941 'theme' => 'hello',
942 'headline' => 'Get 2x more Contacts with Your Website',
943 'label' => 'Show Me How',
944 'link' => '',
945 'icon' => '',
946 'message' => 'Instant Results Guaranteed',
947 'position' => '01',
948 'text_color' => '#000000',
949 'bg_color' => '#eb593c'
950 ),
951 array (
952 'post_title' => '20% Off Coupon',
953 'post_status' => 'publish',
954 'time' => '4',
955 'type' => 'messenger',
956 'theme' => 'social',
957 'animation' => 'slide',
958 'headline' => '20% Off - for you',
959 'label' => '',
960 'link' => '',
961 'icon' => '',
962 'message' => "Hey there! We are running a <strong>special 20% off this week</strong> for registered users - like you.
963
964 Use coupon <code>LOYALTY20</code> during checkout.",
965 'position' => '22',
966 'text_color' => '#000000',
967 'bg_color' => '#ffffff'
968 ),
969 array (
970 'post_title' => 'How this blog makes over $34,800 / month for FREE.',
971 'post_status' => 'publish',
972 'time' => '10',
973 'type' => 'popup',
974 'theme' => 'air-mail',
975 'headline' => 'How this blog makes over $34,800 / month for FREE.',
976 'label' => 'FREE INSTANT ACCESS',
977 'link' => '',
978 'icon' => '',
979 'message' => "This website earns over $30,000 every month, every single month, almost on autopilot. I have 4 other sites with similar results. All I do is publish new regular content every week.
980
981 <strong>Download my free kit to learn how I do this.</strong>
982
983 <ul>
984 <li>How to choose blog topics that create long term value</li>
985 <li>The type of blog post that will make your site go viral</li>
986 <li>How to free yourself from the routine tasks</li>
987 <li>Resources and tips to get started quickly</li>
988 <li>Private members club to connect with fellow owners</li>
989 </ul>",
990 'text_color' => '#000000',
991 'bg_color' => '#ffffff'
992
993 ),
994 array (
995 'post_title' => 'Exclusive Marketing Report',
996 'post_status' => 'publish',
997 'time' => '6',
998 'type' => 'toast',
999 'theme' => 'stand-out',
1000 'animation' => 'pop',
1001 'headline' => 'Exclusive Marketing Report',
1002 'label' => '',
1003 'link' => '',
1004 'icon' => '',
1005 'message' => 'FREE for every subscriber. Click here to download it.',
1006 'position' => '02',
1007 'text_color' => '#000000',
1008 'bg_color' => '#ffffff'
1009 )
1010
1011 )
1012 )
1013 )
1014 );
1015 }
1016
1017 function remove_preview_button() {
1018 global $post_type;
1019
1020 if( $post_type == 'ig_message' || $post_type == 'ig_campaign' ) {
1021
1022 ?>
1023 <style>
1024 #preview-action { display:none; }
1025 </style>
1026 <?php
1027
1028 }
1029
1030 }
1031
1032 function remove_row_actions( $actions, $post ) {
1033
1034 if ( empty( $post->post_type ) || ( $post->post_type != 'ig_campaign' && $post->post_type != 'ig_message' ) ) return $actions;
1035
1036 unset( $actions['inline hide-if-no-js'] );
1037 unset( $actions['view'] );
1038
1039 return $actions;
1040
1041 }
1042
1043 function identify_current_page() {
1044 global $post, $wpdb;
1045
1046 $obj = get_queried_object();
1047 if( !empty( $obj->has_archive ) ) {
1048 $id = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_name = %s AND post_type='page'", $obj->has_archive ) );
1049 } elseif( is_object( $post ) && isset( $post->ID ) ) {
1050 $id = $post->ID;
1051 }
1052 $id = apply_filters('icegram_identify_current_page', $id );
1053 self::$current_page_id = $id;
1054 }
1055
1056 static function get_current_page_id() {
1057 return self::$current_page_id;
1058 }
1059
1060 function wpml_get_parent_id( $id ) {
1061 global $post;
1062 if (function_exists('icl_object_id') && function_exists('icl_get_default_language') ) {
1063 $id = icl_object_id($id, $post->post_type, true, icl_get_default_language() );
1064 }
1065 return $id;
1066 }
1067
1068
1069 /**
1070 * Our implementation of wpautop to preserve script and style tags
1071 */
1072 function before_wpautop($pee) {
1073 if ( trim($pee) === '' ) {
1074 $this->_wpautop_tags = array();
1075 return '';
1076 }
1077
1078 $tags = array();
1079 // Pull out tags and add placeholders
1080 list( $pee, $tags['pre'] ) = $this->_wpautop_add_tag_placeholders( $pee, 'pre' );
1081 list( $pee, $tags['script'] ) = $this->_wpautop_add_tag_placeholders( $pee, 'script' );
1082 list( $pee, $tags['style'] ) = $this->_wpautop_add_tag_placeholders( $pee, 'style' );
1083 $this->_wpautop_tags = $tags;
1084
1085 if( !empty( $pre_tags ) )
1086 $pee = $this->_wpautop_replace_tag_placeholders( $pee, $pre_tags );
1087 if( !empty( $script_tags ) )
1088 $pee = $this->_wpautop_replace_tag_placeholders( $pee, $script_tags );
1089 if( !empty( $style_tags ) )
1090 $pee = $this->_wpautop_replace_tag_placeholders( $pee, $style_tags );
1091
1092 return $pee;
1093 }
1094
1095 function after_wpautop($pee) {
1096 if ( trim($pee) === '' || empty($this->_wpautop_tags) )
1097 return '';
1098
1099 // Replace placeholders with original content
1100 if (!empty($this->_wpautop_tags['pre'])) {
1101 $pee = $this->_wpautop_replace_tag_placeholders( $pee, $this->_wpautop_tags['pre'] );
1102 }
1103 if (!empty($this->_wpautop_tags['script'])) {
1104 $pee = $this->_wpautop_replace_tag_placeholders( $pee, $this->_wpautop_tags['script'] );
1105 }
1106 if (!empty($this->_wpautop_tags['style'])) {
1107 $pee = $this->_wpautop_replace_tag_placeholders( $pee, $this->_wpautop_tags['style'] );
1108 }
1109
1110 $this->_wpautop_tags = array();
1111
1112 return $pee;
1113 }
1114
1115 function _wpautop_add_tag_placeholders( $pee, $tag ) {
1116 $tags = array();
1117
1118 if ( false !== strpos( $pee, "<{$tag}" ) ) {
1119 $pee_parts = explode( "</{$tag}>", $pee );
1120 $last_pee = array_pop( $pee_parts );
1121 $pee = '';
1122 $i = 0;
1123
1124 foreach ( $pee_parts as $pee_part ) {
1125 $start = strpos( $pee_part, "<{$tag}" );
1126
1127 // Malformed html?
1128 if ( false === $start ) {
1129 $pee .= $pee_part;
1130 continue;
1131 }
1132
1133 $name = "<{$tag} wp-{$tag}-tag-$i></{$tag}>";
1134 $tags[ $name ] = substr( $pee_part, $start ) . "</{$tag}>";
1135
1136 $pee .= substr( $pee_part, 0, $start ) . $name;
1137 $i++;
1138 }
1139
1140 $pee .= $last_pee;
1141 }
1142
1143 return array( $pee, $tags );
1144 }
1145
1146 function _wpautop_replace_tag_placeholders( $pee, $tags ) {
1147 if ( ! empty( $tags ) ) {
1148 $pee = str_replace( array_keys( $tags ), array_values( $tags ), $pee );
1149 }
1150
1151 return $pee;
1152 }
1153
1154 }
1155
1156 function initialize_icegram() {
1157 global $icegram;
1158
1159 // i18n / l10n - load translations
1160 load_plugin_textdomain( 'icegram', false, dirname( plugin_basename( __FILE__ ) ) . '/lang/' );
1161
1162 $icegram = new Icegram();
1163 }
1164
1165 add_action( 'plugins_loaded', 'initialize_icegram' );
1166 register_activation_hook( __FILE__, array( 'Icegram', 'install' ) );