PluginProbe
Icegram Engage – Popups, Optins, CTAs & Lead Generation / 1.8.10
Icegram Engage – Popups, Optins, CTAs & Lead Generation v1.8.10
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.8.10, at icegram.php

1,442 lines 69.1 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.8.10
7 * Author: icegram
8 * Author URI: http://www.icegram.com/
9 *
10 * Copyright (c) 2014-15 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 var $mode;
32 var $cache_compatibility;
33
34 public static $current_page_id;
35
36 function __construct() {
37
38 $this->version = "1.8.10";
39 $this->shortcode_instances = array();
40 $this->mode = 'local';
41 $this->plugin_url = untrailingslashit( plugins_url( '/', __FILE__ ) );
42 $this->plugin_path = untrailingslashit( plugin_dir_path( __FILE__ ) );
43 $this->include_classes();
44 $this->cache_compatibility = get_option('icegram_cache_compatibility', 'no');
45
46 if( is_admin() && current_user_can( 'manage_options' ) ) {
47 $ig_campaign_admin = Icegram_Campaign_Admin::getInstance();
48 $ig_message_admin = Icegram_Message_Admin::getInstance();
49 add_action( 'admin_enqueue_scripts', array( &$this, 'enqueue_admin_styles_and_scripts' ) );
50 add_action( 'admin_print_styles', array( &$this, 'remove_preview_button' ) );
51 add_filter( 'post_row_actions', array( &$this , 'remove_row_actions' ), 10, 2 );
52
53 add_action( 'admin_menu', array( &$this, 'admin_menus') );
54 add_action( 'admin_init', array( &$this, 'welcome' ) );
55 add_action( 'admin_init', array( &$this, 'dismiss_admin_notice' ) );
56
57 add_action( 'icegram_settings_after', array( &$this, 'klawoo_subscribe_form' ) );
58 add_action( 'icegram_about_changelog', array( &$this, 'klawoo_subscribe_form' ) );
59 add_action( 'icegram_settings_after', array( &$this, 'icegram_houskeeping' ) );
60 add_action( 'admin_notices', array( &$this,'add_admin_notices'));
61 } else {
62 add_action( 'wp_footer', array( &$this, 'icegram_load_data' ));
63 }
64 if($this->cache_compatibility === 'no'){
65 add_action( 'wp_footer', array( &$this, 'display_messages' ) );
66 }
67 add_shortcode( 'icegram', array( &$this, 'execute_shortcode' ) );
68 // WPML compatibility
69 add_filter( 'icegram_identify_current_page', array( &$this, 'wpml_get_parent_id' ), 10 );
70
71 add_filter( 'icegram_branding_data', array( &$this , 'branding_data_remove' ), 10 );
72 add_action( 'wp_enqueue_scripts', array( &$this, 'identify_current_page' ) );
73 add_filter( 'icegram_get_valid_campaigns_sql', array( &$this , 'append_to_valid_campaigns_sql' ), 10, 2 );
74 add_action( 'icegram_print_js_css_data', array( &$this, 'print_js_css_data' ), 10, 1);
75 // common
76 add_action( 'init', array( &$this, 'register_campaign_post_type' ) );
77 add_action( 'init', array( &$this, 'register_message_post_type' ) );
78
79 if ( defined( 'DOING_AJAX' ) ) {
80 if($this->cache_compatibility === 'yes'){
81 add_action( 'wp_ajax_display_messages', array( &$this, 'display_messages' ) );
82 add_action( 'wp_ajax_nopriv_display_messages', array( &$this, 'display_messages' ) );
83 }
84 add_action( 'wp_ajax_icegram_event_track', array( &$this, 'icegram_event_track' ) );
85 add_action( 'wp_ajax_nopriv_icegram_event_track', array( &$this, 'icegram_event_track' ) );
86 add_action( 'wp_ajax_klawoo_subscribe', array( &$this, 'klawoo_subscribe' ) );
87 add_action( 'wp_ajax_icegram_run_housekeeping', array( &$this, 'run_housekeeping' ) );
88 }
89
90 }
91 public function add_admin_notices(){
92 $active_plugins = get_option( 'active_plugins', array() );
93 ?>
94 <style>
95 p.icegram_notice {
96 border-left: 4px solid hsl(94, 61%, 52%);
97 padding: 10px 12px;
98 background-color: hsl(0, 100%, 100%);
99 -webkit-box-shadow: 0 1px 1px 0 hsla(0, 0%, 0%, 0.1);
100 box-shadow: 0 1px 1px 0 hsla(0, 0%, 0%, 0.1);
101 margin-right: 20px;
102 }
103 a.ig-admin-btn{
104 margin-left: 10px;
105 padding: 4px 8px;
106 position: relative;
107 text-decoration: none;
108 border: none;
109 -webkit-border-radius: 2px;
110 border-radius: 2px;
111 background: hsl(0, 0%, 88%);
112 text-shadow: none;
113 font-weight: 600;
114 font-size: 13px;
115 }
116 a.ig-admin-btn-secondary{
117 background: hsl(0, 0%, 98%);
118 margin-left: 20px;
119 font-weight: 400;
120 }
121
122 a.ig-admin-btn:hover{
123 color: hsl(0, 100%, 100%);
124 background-color: hsl(207, 8%, 23%);
125 }
126 </style>
127 <?php
128 // Admin notices for free bonuses!
129 if(!get_option('dismiss_admin_notice_from_icegram')){
130 $admin_notice_text = '';
131 if(in_array('ig-analytics/ig-analytics.php' ,$active_plugins) && !in_array('ig-themes-pack-1/ig-themes-pack-1.php' , $active_plugins)){
132 // themes
133 $admin_notice_text = "Get <b>17 bonus themes</b> for Icegram. Free!! <a class='ig-admin-btn' href='http://www.icegram.com/addons/theme-pack-1/?utm_source=inapp&utm_campaign=freebonus&utm_medium=notices' target='_blank'>Yes, I want them!</a>";
134 }else if(in_array('ig-themes-pack-1/ig-themes-pack-1.php', $active_plugins) && !in_array('ig-analytics/ig-analytics.php', $active_plugins)){
135 //analytics
136 $admin_notice_text = "Measure performance of your Icegram messages with the <b>free Analytics addon</b>. <a class='ig-admin-btn' href='http://www.icegram.com/addons/analytics/?utm_source=inapp&utm_campaign=freebonus&utm_medium=notices' target='_blank'>Cool, Let's begin</a>";
137 }else if(!in_array('ig-analytics/ig-analytics.php', $active_plugins) && !in_array('ig-themes-pack-1/ig-themes-pack-1.php', $active_plugins)){
138 // both
139 $admin_notice_text = "Claim your Icegram bonuses today. <b>17 themes &amp; Analytics addon</b> for free! <a class='ig-admin-btn' href='http://www.icegram.com/product-category/addons/free/?utm_source=inapp&utm_campaign=freebonus&utm_medium=notices' target='_blank'>Yes, Let's begin</a>";
140 }
141 if ($admin_notice_text != '') {
142 echo "<p class='icegram_notice'>".$admin_notice_text." <a class='ig-admin-btn ig-admin-btn-secondary' href='?dismiss_admin_notice=1'>No, I don't like free bonuses...</a></p>";
143 }
144 }
145 }
146 public function dismiss_admin_notice(){
147 if(isset($_GET['dismiss_admin_notice']) && $_GET['dismiss_admin_notice'] == '1'){
148 update_option('dismiss_admin_notice_from_icegram', true);
149 wp_safe_redirect($_SERVER['HTTP_REFERER']);
150 exit();
151 }
152 }
153 public function klawoo_subscribe_form() {
154 ?>
155 <div class="wrap">
156 <?php
157 if ( stripos(get_current_screen()->base, 'settings') !== false ) {
158 echo "<h2>".__( 'Free Add-ons, Proven Marketing Tricks and Updates', 'icegram' )."</h2>";
159 }
160 ?>
161 <table class="form-table">
162 <tr>
163 <th scope="row"><?php _e( 'Get add-ons and tips...', 'icegram' ) ?></th>
164 <td>
165 <form name="klawoo_subscribe" action="#" method="POST" accept-charset="utf-8">
166 <input class="ltr" type="text" name="name" id="name" placeholder="Name"/>
167 <input class="regular-text ltr" type="text" name="email" id="email" placeholder="Email"/>
168 <input type="hidden" name="list" value="7I763v6Ldrs3YhJeee5EOgFA"/>
169 <input type="submit" name="submit" id="submit" class="button button-primary" value="Subscribe">
170 <br/>
171 <div id="klawoo_response"></div>
172 </form>
173 </td>
174 </tr>
175 </table>
176 </div>
177 <script type="text/javascript">
178 jQuery(function () {
179 jQuery("form[name=klawoo_subscribe]").submit(function (e) {
180 e.preventDefault();
181
182 jQuery('#klawoo_response').html('');
183 params = jQuery("form[name=klawoo_subscribe]").serializeArray();
184 params.push( {name: 'action', value: 'klawoo_subscribe' });
185
186 jQuery.ajax({
187 method: 'POST',
188 type: 'text',
189 url: "<?php echo admin_url( 'admin-ajax.php' ); ?>",
190 data: params,
191 success: function(response) {
192 if (response != '') {
193 jQuery('#klawoo_response').html(response);
194 } else {
195 jQuery('#klawoo_response').html('error!');
196 }
197 }
198 });
199 });
200 });
201 </script>
202 <?php
203 }
204
205
206 public function klawoo_subscribe() {
207 $url = 'http://app.klawoo.com/subscribe';
208
209 if( !empty( $_POST ) ) {
210 $params = $_POST;
211 } else {
212 exit();
213 }
214 $method = 'POST';
215 $qs = http_build_query( $params );
216
217 $options = array(
218 'timeout' => 15,
219 'method' => $method
220 );
221
222 if ( $method == 'POST' ) {
223 $options['body'] = $qs;
224 } else {
225 if ( strpos( $url, '?' ) !== false ) {
226 $url .= '&'.$qs;
227 } else {
228 $url .= '?'.$qs;
229 }
230 }
231
232 $response = wp_remote_request( $url, $options );
233 if ( wp_remote_retrieve_response_code( $response ) == 200 ) {
234 $data = $response['body'];
235 if ( $data != 'error' ) {
236
237 $message_start = substr( $data, strpos( $data,'<body>' ) + 6 );
238 $remove = substr( $message_start, strpos( $message_start,'</body>' ) );
239 $message = trim( str_replace( $remove, '', $message_start ) );
240 echo ( $message );
241 exit();
242 }
243 }
244 exit();
245 }
246
247 public function icegram_houskeeping(){
248 ?>
249 <div class="wrap">
250 <?php
251 if ( stripos(get_current_screen()->base, 'settings') !== false ) {
252 ?>
253 <form name="icegram_housekeeping" action="#" method="POST" accept-charset="utf-8">
254 <h2><?php _e( 'Housekeeping', 'icegram' ) ?></h2>
255 <p class="ig_housekeeping">
256 <label for="icegram_remove_shortcodes">
257 <input type="checkbox" name="icegram_remove_shortcodes" value="yes" />
258 <?php _e( 'Remove all Icegram shortcodes', 'icegram' ); ?>
259 </label>
260 <br/><br/>
261 <label for="icegram_remove_all_data">
262 <input type="checkbox" name="icegram_remove_all_data" value="yes" />
263 <?php _e( 'Remove all Icegram campaigns and messages', 'icegram' ); ?>
264 </label>
265 <br/><br/>
266 <img alt="" src="<?php echo admin_url( 'images/wpspin_light.gif' ) ?>" class="ig_loader" style="vertical-align:middle;display:none" />
267 <input type="submit" name="submit" id="submit" class="button button-primary" value="<?php _e( 'Clean Up', 'icegram' ); ?>">
268 <div id="icegram_housekeeping_response"></div>
269 </p>
270 </form>
271
272 </div>
273 <script type="text/javascript">
274 jQuery(function () {
275 jQuery("form[name=icegram_housekeeping]").submit(function (e) {
276 if(confirm("<?php _e( 'You won\'t be able to recover this data once you proceed. Do you really want to perform this action?', 'icegram' ); ?>") == true){
277 e.preventDefault();
278 jQuery('.ig_loader').show();
279 jQuery('#icegram_housekeeping_response').text("");
280 params = jQuery("form[name=icegram_housekeeping]").serializeArray();
281 params.push( {name: 'action', value: 'icegram_run_housekeeping' });
282
283 jQuery.ajax({
284 method: 'POST',
285 type: 'text',
286 url: "<?php echo admin_url( 'admin-ajax.php' ); ?>",
287 data: params,
288 success: function(response) {
289 jQuery('.ig_loader').hide();
290 jQuery('#icegram_housekeeping_response').text("<?php _e('Done!', 'icegram'); ?>");
291 }
292 });
293 }
294 });
295 });
296 </script>
297 <?php
298 }
299 }
300 public function run_housekeeping() {
301 global $wpdb;
302 $params = $_POST;
303 $_POST = array();
304
305 if(!empty($params['icegram_remove_shortcodes']) && $params['icegram_remove_shortcodes'] == 'yes') {
306 // first get all posts with [icegram] shortcode in them
307 $sql = "SELECT * FROM `$wpdb->posts` WHERE `post_content` LIKE '%[icegram %]%' and `post_type` != 'revision' ";
308 $posts = $wpdb->get_results($sql, OBJECT);
309 if ( !empty($posts) && is_array($posts) ) {
310 foreach ($posts as $post) {
311 $post_content = $post->post_content;
312 // remove shortcode with regexp now
313 $re = "/\\[icegram(.)*\\]/i";
314 $post_content = preg_replace($re, '', $post_content);
315 // save post content back
316 if ($post_content && $post_content != $post->post_content) {
317 wp_update_post( array ( 'ID' => $post->ID,
318 'post_content' => $post_content
319 ) );
320 }
321 }
322 }
323 }
324
325 if(!empty($params['icegram_remove_all_data']) && $params['icegram_remove_all_data'] == 'yes') {
326 $posts = get_posts( array( 'post_type' => array( 'ig_campaign', 'ig_message' ) ) );
327 if ( !empty($posts) && is_array($posts) ) {
328 foreach ($posts as $post) {
329 wp_delete_post( $post->ID, true);
330 }
331 }
332 do_action('icegram_remove_all_data');
333 }
334 $_POST = $params;
335 }
336
337 public function icegram_event_track() {
338
339 if( !empty( $_POST['event_data'] ) ) {
340
341 foreach ( $_POST['event_data'] as $event ) {
342 switch ($event['type']) {
343 case 'shown':
344 if (is_array($event['params']) && !empty($event['params']['message_id'])) {
345 $messages_shown[] = $event['params']['message_id'];
346 if(!empty($event['params']['expiry_time'])){
347 if($event['params']['expiry_time'] =='today'){
348 $event['params']['expiry_time'] = strtotime('+1 day', mktime(0, 0, 0));
349 }else if($event['params']['expiry_time'] == 'current_session'){
350 $event['params']['expiry_time'] = 0;
351 }else{
352 $event['params']['expiry_time'] = strtotime($event['params']['expiry_time']);
353 }
354 setcookie('icegram_messages_shown_'.$event['params']['message_id'],true , $event['params']['expiry_time'] , '/');
355 }
356 }
357 break;
358 case 'clicked':
359 if (is_array($event['params']) && !empty($event['params']['message_id'])) {
360 $messages_clicked[] = $event['params']['message_id'];
361 if(!empty($event['params']['expiry_time_clicked'])){
362 if($event['params']['expiry_time_clicked'] =='today'){
363 $event['params']['expiry_time_clicked'] = strtotime('+1 day', mktime(0, 0, 0));
364 }else if($event['params']['expiry_time_clicked'] == 'current_session'){
365 $event['params']['expiry_time_clicked'] = 0;
366 }else{
367 $event['params']['expiry_time_clicked'] = strtotime($event['params']['expiry_time_clicked']);
368 }
369 setcookie('icegram_messages_clicked_'.$event['params']['message_id'],true , $event['params']['expiry_time_clicked'] , '/' );
370 }
371 }
372 break;
373
374 default:
375 break;
376 }
377
378 // Emit event for other plugins to handle it
379 do_action('icegram_event_track', $event);
380 do_action('icegram_event_track_'.$event['type'], $event['params']);
381 }
382 }
383 exit();
384 }
385
386 static function install() {
387 // Redirect to welcome screen
388 delete_option( '_icegram_activation_redirect' );
389 add_option( '_icegram_activation_redirect', 'pending' );
390 }
391
392 public function welcome() {
393
394 $this->db_update();
395 // Bail if no activation redirect transient is set
396 if ( false === get_option( '_icegram_activation_redirect' ) )
397 return;
398
399 // Delete the redirect transient
400 delete_option( '_icegram_activation_redirect' );
401
402 wp_redirect( admin_url( 'edit.php?post_type=ig_campaign&page=icegram-support' ) );
403 exit;
404 }
405
406 function db_update() {
407 $current_db_version = get_option( 'icegram_db_version' );
408 if ( !$current_db_version || version_compare( $current_db_version, '1.2', '<' ) ) {
409 include( 'updates/icegram-update-1.2.php' );
410 }
411 }
412
413 public function admin_menus() {
414
415 $welcome_page_title = __( 'Welcome to Icegram', 'icegram' );
416 $settings_page_title = __( 'Settings', 'icegram' );
417 $addons_page_title = __( 'Add-ons', 'icegram' );
418
419 /*
420 if ( false === ( $ig_addons = get_transient( 'icegram_addons_data' ) ) ) {
421 $this->check_for_addons();
422 }
423 $addon_count = get_option( 'icegram_no_of_addons' );
424
425 if ($addon_count > 0) {
426 $addons_page_title .= " <span class='awaiting-mod update-plugins count-$addon_count'><span class='addon-count'>" . number_format_i18n( $addon_count ) . "</span></span>" ;
427 }
428 */
429
430 $menu_title = __( 'Docs & Support', 'icegram' );
431 $about = add_submenu_page( 'edit.php?post_type=ig_campaign', $welcome_page_title, $menu_title, 'manage_options', 'icegram-support', array( $this, 'about_screen' ) );
432 $settings = add_submenu_page( 'edit.php?post_type=ig_campaign', $settings_page_title, $settings_page_title, 'manage_options', 'icegram-settings', array( $this, 'settings_screen' ) );
433 $addons = add_submenu_page( 'edit.php?post_type=ig_campaign', $addons_page_title, $addons_page_title, 'manage_options', 'icegram-addons', array( $this, 'addons_screen' ) );
434
435 add_action( 'admin_print_styles-'. $about, array( $this, 'admin_css' ) );
436 add_action( 'admin_print_styles-'. $settings, array( $this, 'admin_css' ) );
437 add_action( 'admin_print_styles-'. $addons, array( $this, 'admin_css' ) );
438
439 }
440
441 public function admin_css() {
442 wp_enqueue_style( 'icegram-activation', $this->plugin_url . '/assets/css/admin.css' );
443 }
444
445 public function about_screen() {
446
447 // Import data if not done already
448 if( false === get_option( 'icegram_sample_data_imported' ) ) {
449 $this->import( $this->get_sample_data() );
450 }
451
452 include ( 'about-icegram.php' );
453 }
454
455 public function settings_screen() {
456 include ( 'settings.php' );
457 }
458
459 public function addons_screen() {
460 $ig_addons = $this->check_for_addons( true );
461 include ( 'addons.php' );
462 }
463
464 public function check_for_addons( $force_update = false ) {
465
466 if ( $force_update === true || false === ( $ig_addons = get_transient( 'icegram_addons_data' ) ) ) {
467 $ig_addons_json = wp_remote_get( 'http://icegram.com/addons.json', array( 'user-agent' => 'Icegram Addons' ) );
468
469 if ( ! is_wp_error( $ig_addons_json ) ) {
470 $ig_addons = json_decode( wp_remote_retrieve_body( $ig_addons_json ) );
471 $addon_count = get_option( 'icegram_no_of_addons', 0 );
472 if ( !empty($ig_addons) && is_array($ig_addons) ) {
473 set_transient( 'icegram_addons_data', $ig_addons, 24 * HOUR_IN_SECONDS ); // 1 day
474 update_option( 'icegram_no_of_addons', count( $ig_addons ) - $addon_count ); // display count of newly added addons
475 }
476 }
477 }
478 return $ig_addons;
479 }
480
481 public function branding_data_remove( $icegram_branding_data ) {
482 if( !empty( $icegram_branding_data ) && 'yes' != get_option('icegram_share_love', 'no') ) {
483 $icegram_branding_data['powered_by_logo'] = '';
484 $icegram_branding_data['powered_by_text'] = '';
485 }
486 return $icegram_branding_data;
487 }
488
489 function execute_shortcode( $atts = array() ) {
490 // When shortcode is called, it will only prepare an array with conditions
491 // And add a placeholder div
492 // Display will happen in footer via display_messages()
493 $i = count($this->shortcode_instances);
494 $this->shortcode_instances[ $i ] = shortcode_atts( array(
495 'campaigns' => '',
496 'messages' => '',
497 'skip_others' => 'no'
498 ), $atts );
499 $html[] = "<div class='ig_shortcode_container' id='icegram_shortcode_{$i}'";
500 foreach ($atts as $key => $value) {
501 $value = str_replace(",", " ", $value);
502 $html[] = " data-{$key}=\"".htmlentities($value)."\" ";
503 }
504
505 $html[] = " >"."</div>";
506 return implode(" ", $html);
507 }
508
509 // Do not index Icegram campaigns / messages...
510 // Not using currently - made custom post types non public...
511 function icegram_load_data() {
512 global $post;
513 $icegram_pre_data['ajax_url'] = admin_url( 'admin-ajax.php' );
514 $icegram_pre_data['post_obj'] = $_GET;
515 $icegram_pre_data['post_obj']['is_home'] = (is_home() || is_front_page()) ? true : false;
516 $icegram_pre_data['post_obj']['page_id'] = $post->ID;
517 $icegram_pre_data['post_obj']['action'] = 'display_messages';
518 $icegram_pre_data['post_obj']['shortcodes'] = $this->shortcode_instances;
519 $icegram_pre_data['post_obj']['cache_compatibility'] = $this->cache_compatibility;
520 wp_register_script('icegram_main_js' ,$this->plugin_url . '/assets/js/main.js', array ( 'jquery' ), $this->version,true);
521 if( !wp_script_is( 'icegram_main_js' ) ) {
522 wp_enqueue_script( 'icegram_main_js' );
523 wp_localize_script( 'icegram_main_js', 'icegram_pre_data' , $icegram_pre_data);
524 }
525 }
526
527 function display_messages() {
528
529 $skip_others = $preview_mode = false;
530 $campaign_ids = $message_ids = array();
531
532 $this->shortcode_instances = ($this->cache_compatibility == 'yes' && !empty($_REQUEST['shortcodes'])) ? $_REQUEST['shortcodes'] : $this->shortcode_instances;
533 // Pull in message and campaign IDs from shortcodes - if set
534 if( !empty( $this->shortcode_instances ) ) {
535 foreach ($this->shortcode_instances as $i => $value) {
536 $cids = array_map( 'trim', (array) explode( ',', $value['campaigns'] ) );
537 $mids = array_map( 'trim', (array) explode( ',', $value['messages'] ) );
538 if ($value['skip_others'] == 'yes' && (!empty($cids) || !empty($mids))) {
539 $skip_others = true;
540 }
541 $campaign_ids = array_merge($campaign_ids, $cids);
542 $message_ids = array_merge($message_ids, $mids);
543 }
544 }
545 if( !empty( $_REQUEST['campaign_preview_id'] ) && current_user_can( 'manage_options' ) ) {
546 $campaign_ids = array( $_REQUEST['campaign_preview_id'] );
547 $preview_mode = true;
548 }
549
550 $messages = $this->get_valid_messages( $message_ids, $campaign_ids, $preview_mode, $skip_others );
551
552 if( empty( $messages ) ) {
553 //wp_die(0);
554 return;
555 }
556
557 $messages_to_show_ids = array();
558 foreach ( $messages as $key => $message_data ) {
559
560 if( !is_array( $message_data ) || empty( $message_data ) ) {
561 continue;
562 }
563
564
565 // Don't show a seen message again - if needed
566 if( !empty( $message_data['id'] ) &&
567 empty( $_GET['campaign_preview_id'] ) &&
568 !empty($_COOKIE['icegram_messages_shown_'.$message_data['id']])&&
569 !empty( $message_data['retargeting'] ) &&
570 $message_data['retargeting'] == 'yes'
571 ) {
572 unset( $messages[$key] );
573 continue;
574 }
575 if( !empty( $message_data['id'] ) &&
576 empty( $_GET['campaign_preview_id'] ) &&
577 !empty($_COOKIE['icegram_messages_clicked_'.$message_data['id']]) &&
578 !empty( $message_data['retargeting_clicked'] ) &&
579 $message_data['retargeting_clicked'] == 'yes'
580 ) {
581 unset( $messages[$key] );
582 continue;
583 }
584
585 // Avoid showing the same message twice
586 if (in_array($message_data['id'], $messages_to_show_ids)) {
587 unset ( $messages[$key] );
588 continue;
589 } else {
590 $messages_to_show_ids[] = $message_data['id'];
591 }
592
593
594 /*
595 // Our own implementation so WP does not mess with script, style and pre tags
596 add_filter('the_content', array( $this, 'before_wpautop' ) , 9);
597 add_filter('the_content', array( $this, 'after_wpautop' ) , 11);
598 $messages[$key]['message'] = apply_filters( 'the_content', $message_data['message'] );
599 remove_filter('the_content', array( $this, 'before_wpautop' ) , 9);
600 remove_filter('the_content', array( $this, 'after_wpautop' ) , 11);
601 */
602 // Redo the_content functionality to avoid other plugins adding extraneous code to messages
603 $this->process_message_body($messages[$key]);
604 }
605
606 if( empty( $messages ) )
607 return;
608 $icegram_default = apply_filters( 'icegram_branding_data',
609 array ( 'icon' => $this->plugin_url . '/assets/images/icegram-logo-branding-64-grey.png',
610 'powered_by_logo' => $this->plugin_url . '/assets/images/icegram-logo-branding-64-grey.png',
611 'powered_by_text' => __( 'Powered by Icegram', 'icegram' )
612 ) );
613 $messages = apply_filters( 'icegram_messages_to_show', $messages );
614 $icegram_data = apply_filters( 'icegram_data', array ( 'messages' => array_values( $messages ),
615 'ajax_url' => admin_url( 'admin-ajax.php' ),
616 'preview_id' => !empty( $_GET['campaign_preview_id'] ) ? $_GET['campaign_preview_id'] : '',
617 'defaults' => $icegram_default
618 ));
619
620 if (empty($icegram_data['preview_id'])) {
621 unset($icegram_data['preview_id']);
622 }
623
624 do_action('icegram_print_js_css_data', $icegram_data);
625 }
626
627 function print_js_css_data( $icegram_data ) {
628 $this->collect_js_and_css($icegram_data);
629 if($this->cache_compatibility === 'yes'){
630 echo json_encode($icegram_data);
631 wp_die();
632 }else{
633 wp_localize_script('icegram_main_js' , 'icegram_data' ,$icegram_data);
634 }
635 }
636
637 function collect_js_and_css(&$icegram_data){
638 $types_shown = array();
639 $scripts = array();
640 $css = array();
641 foreach ($icegram_data['messages'] as $key => $message_data) {
642 $types_shown[] = $message_data['type'];
643 }
644 $types_shown = array_unique($types_shown);
645
646 $scripts[] = $this->plugin_url ."/assets/js/icegram.js";
647 $css[] = $this->plugin_url ."/assets/css/frontend.css";
648
649 // Load JS and default CSS
650 foreach ($types_shown as $message_type) {
651 $scripts[] = $this->message_types[$message_type]['baseurl'] ."main.js";
652 $css[] = $this->message_types[$message_type]['baseurl'] . "default.css";
653 }
654 // Load theme CSS
655 foreach ($icegram_data['messages'] as $key => $message) {
656 if (!empty( $this->message_types[ $message['type'] ]['themes'][ $message['theme'] ]) ) {
657 $theme = $this->message_types[ $message['type'] ]['themes'][ $message['theme'] ];
658 $css [] = $theme['baseurl'] . $message['theme'].'.css';
659 }else{
660 $theme_default = $this->message_types[ $message['type']] ['settings']['theme']['default'];
661 $theme = $this->message_types[ $message['type'] ]['themes'][ $theme_default];
662 $css [] = $theme['baseurl'] . $theme_default.'.css';
663 $icegram_data['messages'][$key]['theme'] = $theme_default;
664 }
665 }
666 $icegram_data['scripts'] = apply_filters('add_icegram_script' , $scripts);
667 $icegram_data['css'] = apply_filters('add_icegram_css' , $css);
668 return $icegram_data;
669 }
670
671 // Process
672 function process_message_body(&$message_data){
673 $content = $message_data['message'];
674 if(isset($GLOBALS['wp_embed'])) {
675 $content = $GLOBALS['wp_embed']->autoembed($content);
676 }
677 $content = $this->after_wpautop( wpautop( $this->before_wpautop( $content ) ) );
678 $content = do_shortcode( shortcode_unautop( $content ) );
679 $content = convert_chars( convert_smilies( wptexturize( $content ) ) );
680 $message_data['message'] = $content;
681 //do_shortcode in headline
682 $message_data['headline'] = do_shortcode( shortcode_unautop( $message_data['headline'] ) );
683 }
684
685 function enqueue_admin_styles_and_scripts() {
686
687 $screen = get_current_screen();
688 if ( !in_array( $screen->id, array( 'ig_campaign', 'ig_message' ), true ) ) return;
689
690 // Register scripts
691 wp_register_script( 'icegram_writepanel', $this->plugin_url . '/assets/js/admin.js' , array ( 'jquery', 'wp-color-picker' ), $this->version );
692 wp_register_script( 'icegram_chosen', $this->plugin_url . '/assets/js/chosen.jquery.min.js' , array ( 'jquery' ), $this->version );
693 wp_register_script( 'icegram_ajax-chosen', $this->plugin_url . '/assets/js/ajax-chosen.jquery.min.js' , array ( 'icegram_chosen' ), $this->version );
694 wp_register_script( 'icegram_tiptip', $this->plugin_url . '/assets/js/jquery.tipTip.min.js' , array ( 'jquery' ), $this->version );
695
696 wp_enqueue_script( 'icegram_writepanel' );
697 wp_enqueue_script( 'icegram_ajax-chosen' );
698 wp_enqueue_script( 'icegram_tiptip' );
699 wp_register_script( 'magnific_popup_js', $this->plugin_url . '/assets/js/magnific-popup.js', array ( 'jquery' ), $this->version, true);
700 if( !wp_script_is( 'magnific_popup_js' ) ) {
701 wp_enqueue_script( 'magnific_popup_js' );
702 }
703
704 $icegram_writepanel_params = array ( 'ajax_url' => admin_url( 'admin-ajax.php' ), 'search_message_nonce' => wp_create_nonce( "search-messages" ), 'home_url' => home_url( '/' ) );
705 $this->available_headlines = apply_filters( 'icegram_available_headlines', array() );
706 $icegram_writepanel_params = array_merge( $icegram_writepanel_params, array( 'available_headlines' => $this->available_headlines ) );
707
708 wp_localize_script( 'icegram_writepanel', 'icegram_writepanel_params', $icegram_writepanel_params );
709
710 wp_enqueue_style( 'dashicons' );
711 wp_enqueue_style( 'wp-color-picker' );
712 wp_enqueue_style( 'icegram_admin_styles', $this->plugin_url . '/assets/css/admin.css', array(), $this->version );
713 wp_enqueue_style( 'icegram_jquery-ui-style', $this->plugin_url . '/assets/css/jquery-ui.min.css', array(), $this->version );
714 wp_enqueue_style( 'icegram_chosen_styles', $this->plugin_url . '/assets/css/chosen.min.css', array(), $this->version );
715 wp_enqueue_style( 'magnific_popup_css', $this->plugin_url . '/assets/css/magnific-popup.css', array(), $this->version );
716
717 if ( !wp_script_is( 'jquery-ui-datepicker' ) ) {
718 wp_enqueue_script( 'jquery-ui-datepicker' );
719 }
720
721 }
722
723 public static function get_platform() {
724 $mobile_detect = new Ig_Mobile_Detect();
725 $mobile_detect->setUserAgent();
726 if($mobile_detect->isMobile()){
727 return ($mobile_detect->isTablet()) ? 'tablet' : 'mobile';
728 }else if($mobile_detect->isTablet()){
729 return 'tablet';
730 }
731 return 'laptop';
732 }
733
734 function get_message_data( $message_ids = array(), $preview = false ) {
735 global $wpdb;
736 $message_data = array();
737 $original_message_id_map = array();
738 $meta_key = $preview ? 'icegram_message_preview_data' : 'icegram_message_data';
739 $message_data_query = "SELECT post_id, meta_value FROM {$wpdb->prefix}postmeta WHERE meta_key LIKE '$meta_key'";
740 if ( !empty( $message_ids ) && is_array( $message_ids ) ) {
741 // For WPML compatibility
742 if ( function_exists('icl_object_id') ) {
743 $wpml_settings = get_option('icl_sitepress_settings');
744 $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;
745
746 foreach ($message_ids as $i=>$id ) {
747 $translated = icl_object_id( $id, 'ig_message', $original_if_missing );
748 $message_ids[ $i ] = $translated;
749 $original_message_id_map[ $translated ] = $id;
750 }
751 }
752 $message_ids = array_filter(array_unique($message_ids));
753 if ( !empty( $message_ids ) ) {
754 $message_data_query .= " AND post_id IN ( " . implode( ',', $message_ids ) . " )";
755 $message_data_results = $wpdb->get_results( $message_data_query, 'ARRAY_A' );
756 foreach ( $message_data_results as $message_data_result ) {
757 $data = maybe_unserialize( $message_data_result['meta_value'] );
758 if (!empty($data)) {
759 $message_data[$message_data_result['post_id']] = $data;
760 // For WPML compatibility
761 if (!empty( $original_message_id_map[ $message_data_result['post_id'] ])) {
762 $message_data[$message_data_result['post_id']]['original_message_id'] = $original_message_id_map[ $message_data_result['post_id'] ];
763 }
764 }
765 }
766 }
767 }
768
769 return $message_data;
770 }
771
772 function get_valid_messages( $message_ids = array(), $campaign_ids = array(), $preview_mode = false, $skip_others = false) {
773
774 list($message_ids, $campaign_ids, $preview_mode, $skip_others) = apply_filters('icegram_get_valid_messages_params', array( $message_ids, $campaign_ids, $preview_mode, $skip_others));
775
776 $valid_messages = $valid_campaigns = $message_campaign_map = array();
777
778 $campaign_ids = array_filter(array_unique( (array) $campaign_ids));
779 $message_ids = array_filter(array_unique( (array) $message_ids));
780 if ( !empty( $campaign_ids ) ) {
781 $valid_campaigns = $this->get_valid_campaigns( $campaign_ids, true ,$preview_mode);
782 }
783
784 // When skip_others is true, we won't load campaigns / messages from db
785 if (!$skip_others && !$preview_mode) {
786 $campaigns = $this->get_valid_campaigns();
787 if (!empty($campaigns)) {
788 foreach ($campaigns as $id => $campaign) {
789 if (!array_key_exists($id, $valid_campaigns)) {
790 $valid_campaigns[ $id ] = $campaign;
791 }
792 }
793 }
794 }
795
796 // Create a map to look up campaign id for a given message
797 if( !empty( $valid_campaigns ) ) {
798 foreach ($valid_campaigns as $id => $campaign) {
799 if ($preview_mode) {
800 $campaign->messages = get_post_meta( $id, 'campaign_preview', true );
801 }
802 if( !empty( $campaign->messages ) ) {
803 foreach( $campaign->messages as $msg) {
804 $message_ids[] = $msg['id'];
805 if (!array_key_exists( $msg['id'], $message_campaign_map)) {
806 $message_campaign_map[ $msg['id'] ] = $id;
807 }
808 }
809 }
810 }
811 }
812
813 // We don't display same message twice...
814 $message_ids = array_unique($message_ids);
815
816 if( empty( $message_ids ) ) {
817 return array();
818 }
819 $valid_messages = $this->get_message_data( $message_ids, $preview_mode );
820
821 foreach ($valid_messages as $id => $message_data) {
822 // Remove message if required fields are missing
823 if (empty($message_data) || empty($message_data['type'])) {
824 unset( $valid_messages[$id] );
825 continue;
826 }
827 // Remove message if message type is uninstalled
828 $class_name = 'Icegram_Message_Type_' . str_replace(' ', '_', ucwords(str_replace('-', ' ', $message_data['type'])));
829 if( !class_exists( $class_name ) ) {
830 unset( $valid_messages[$id] );
831 continue;
832 }
833 $message_data['delay_time'] = 0;
834 $message_data['retargeting'] = '';
835 $message_data['campaign_id'] = ($preview_mode) ? $_REQUEST['campaign_preview_id'] : '';
836
837 // Pull display time and retargeting rule from campaign if possible
838 $message_id = (!empty($message_data['original_message_id'])) ? $message_data['original_message_id'] : $id;
839 if (!empty($message_campaign_map[ $message_id ])) {
840 //modify campaign id
841 $message_data['campaign_id'] = apply_filters('modify_campaing_id' , $message_campaign_map[ $message_id ] , $message_id) ;
842 $campaign = $valid_campaigns[ floor($message_data['campaign_id']) ];
843 if (!empty($campaign) && $campaign instanceof Icegram_Campaign) {
844 $message_meta_from_campaign = $campaign->get_message_meta_by_id( $message_id );
845 if (!empty($message_meta_from_campaign['time'])) {
846 $message_data['delay_time'] = $message_meta_from_campaign['time'];
847 }
848 $rule_value = $campaign->get_rule_value('retargeting');
849 $message_data['retargeting'] = !empty( $rule_value['retargeting'] ) ? $rule_value['retargeting'] : '';
850 $message_data['expiry_time'] = !empty( $rule_value['retargeting'] ) ? $rule_value['expiry_time'] : '';
851 $rule_value_retargeting_clicked = $campaign->get_rule_value('retargeting_clicked');
852 $message_data['retargeting_clicked'] = !empty( $rule_value_retargeting_clicked['retargeting_clicked'] ) ? $rule_value_retargeting_clicked['retargeting_clicked'] : '';
853 $message_data['expiry_time_clicked'] = !empty( $rule_value_retargeting_clicked['retargeting_clicked'] ) ? $rule_value_retargeting_clicked['expiry_time_clicked'] : '';
854
855 }
856 }
857 $valid_messages[$id] = $message_data;
858 }
859 $valid_messages = apply_filters( 'icegram_valid_messages', $valid_messages );
860
861 return $valid_messages;
862 }
863
864 function get_valid_campaigns( $campaign_ids = array(), $skip_page_check = false ,$preview_mode = false) {
865 global $wpdb;
866 if ( empty( $campaign_ids ) ) {
867 $sql = "SELECT pm.post_id
868 FROM {$wpdb->prefix}posts AS p
869 LEFT JOIN {$wpdb->prefix}postmeta AS pm ON ( pm.post_id = p.ID )
870 WHERE p.post_status = 'publish' ";
871 // Filter handler within this file (and possibly others) will append to this SQL
872 // and provide arguments for wpdb->prepare if needed.
873 // First element in the array is SQL, remaining are values for placeholders in SQL
874 $sql_params = apply_filters( 'icegram_get_valid_campaigns_sql', array($sql), array() );
875 $campaign_ids = $wpdb->get_col( $wpdb->prepare( array_shift($sql_params), $sql_params ) );
876 }
877 $valid_campaigns = array();
878 foreach ( (array) $campaign_ids as $campaign_id ) {
879 $campaign = new Icegram_Campaign( $campaign_id );
880 if ( $preview_mode || $campaign->is_valid( array('skip_page_check' => $skip_page_check) ) ) {
881 $valid_campaigns[$campaign_id] = $campaign;
882 } else {
883 // Campgain is invalid!
884 }
885 }
886
887 return $valid_campaigns;
888 }
889
890 function append_to_valid_campaigns_sql( $sql_params = array(), $options = array() ) {
891 // Page check conditions
892 //$pid = $_GET['page_id'];
893 $pid = Icegram::get_current_page_id();
894 $sql = " AND (
895 pm.meta_key = 'icegram_campaign_target_rules' AND (
896 ( pm.meta_value LIKE '%%%s%%' )
897 OR ( pm.meta_value LIKE '%%%s%%' AND pm.meta_value LIKE '%%%s%%' AND pm.meta_value LIKE '%%%s%%' )
898 ";
899 $sql_params[] = 's:8:"sitewide";s:3:"yes";';
900 $sql_params[] = 's:10:"other_page";s:3:"yes";';
901 $sql_params[] = 's:7:"page_id";a:';
902 $sql_params[] = serialize( (string) $pid );
903 //local url
904 $sql .= " OR ( pm.meta_value LIKE '%%%s%%' )";
905 $sql_params[] = 's:9:"local_url";s:3:"yes";';
906 if (!empty($_REQUEST['is_home']) && $_REQUEST['is_home'] === 'true' ) {
907 $sql .= " OR ( pm.meta_value LIKE '%%%s%%' )";
908 $sql_params[] = 's:8:"homepage";s:3:"yes";';
909 }
910 $sql .=" ) )";
911
912 $sql_params[0] .= $sql;
913 //s:9:"logged_in";s:3:"all";
914
915 return $sql_params;
916 }
917
918 // Include all classes required for Icegram plugin
919 function include_classes() {
920
921 $classes = glob( $this->plugin_path . '/classes/*.php' );
922 foreach ( $classes as $file ) {
923 // Files with 'admin' in their name are included only for admin section
924 if ( is_file( $file ) && ( (strpos($file, '-admin') >= 0 && is_admin()) || (strpos($file, '-admin') === false) ) ) {
925 include_once $file;
926 }
927 }
928
929 // Load built in message types
930 $icegram_message_type_basedirs = glob( $this->plugin_path . '/message-types/*' );
931 // Allow other plugins to add new message types
932 $icegram_message_type_basedirs = apply_filters( 'icegram_message_type_basedirs', $icegram_message_type_basedirs );
933 // Set up different message type classes
934 foreach ( $icegram_message_type_basedirs as $dir ) {
935 $type = basename ( $dir );
936 $class_file = $dir . "/main.php";
937 if( is_file( $class_file ) ) {
938 include_once( $class_file );
939 }
940 $class_name = 'Icegram_Message_Type_' . str_replace(' ', '_', ucwords(str_replace('-', ' ', $type)));
941 if (class_exists($class_name)) {
942 $this->message_type_objs[ $type ] = new $class_name();
943 }
944 }
945 $this->message_types = apply_filters( 'icegram_message_types', array() );
946
947 }
948
949 // Register Campaign post type
950 function register_campaign_post_type() {
951 $labels = array(
952 'name' => __( 'Campaigns', 'icegram' ),
953 'singular_name' => __( 'Campaign', 'icegram' ),
954 'add_new' => __( 'Add New Campaign', 'icegram' ),
955 'add_new_item' => __( 'Add New Campaign', 'icegram' ),
956 'edit_item' => __( 'Edit Campaign', 'icegram' ),
957 'new_item' => __( 'New Campaign', 'icegram' ),
958 'all_items' => __( 'Campaigns', 'icegram' ),
959 'view_item' => __( 'View Campaign', 'icegram' ),
960 'search_items' => __( 'Search Campaigns', 'icegram' ),
961 'not_found' => __( 'No campaigns found', 'icegram' ),
962 'not_found_in_trash' => __( 'No campaigns found in Trash', 'icegram' ),
963 'parent_item_colon' => __( '', 'icegram' ),
964 'menu_name' => __( 'Icegram', 'icegram' )
965 );
966
967 $args = array(
968 'labels' => $labels,
969 'menu_icon' => 'dashicons-info',
970 'public' => false,
971 'publicly_queryable' => false,
972 'show_ui' => true,
973 'show_in_menu' => true,
974 'query_var' => true,
975 'rewrite' => array( 'slug' => 'ig_campaign' ),
976 'capability_type' => 'post',
977 'has_archive' => false,
978 'hierarchical' => false,
979 'menu_position' => null,
980 'supports' => array( 'title', 'editor' )
981 );
982
983 register_post_type( 'ig_campaign', $args );
984 }
985
986 // Register Message post type
987 function register_message_post_type() {
988 $labels = array(
989 'name' => __( 'Messages', 'icegram' ),
990 'singular_name' => __( 'Message', 'icegram' ),
991 'add_new' => __( 'Create New', 'icegram' ),
992 'add_new_item' => __( 'Create New Message', 'icegram' ),
993 'edit_item' => __( 'Edit Message', 'icegram' ),
994 'new_item' => __( 'New Message', 'icegram' ),
995 'all_items' => __( 'Messages', 'icegram' ),
996 'view_item' => __( 'View Message', 'icegram' ),
997 'search_items' => __( 'Search Messages', 'icegram' ),
998 'not_found' => __( 'No messages found', 'icegram' ),
999 'not_found_in_trash' => __( 'No messages found in Trash', 'icegram' ),
1000 'parent_item_colon' => __( '', 'icegram' ),
1001 'menu_name' => __( 'Messages', 'icegram' )
1002 );
1003
1004 $args = array(
1005 'labels' => $labels,
1006 'public' => false,
1007 'publicly_queryable' => false,
1008 'show_ui' => true,
1009 'show_in_menu' => 'edit.php?post_type=ig_campaign',
1010 'query_var' => true,
1011 'rewrite' => array( 'slug' => 'ig_message' ),
1012 'capability_type' => 'post',
1013 'has_archive' => false,
1014 'hierarchical' => false,
1015 'menu_position' => null,
1016 'supports' => array( 'title' )
1017 );
1018
1019 register_post_type( 'ig_message', $args );
1020 }
1021
1022 function import( $data = array() ) {
1023
1024 if ( empty( $data['campaigns'] ) && empty( $data['messages'] ) ) return;
1025
1026 $default_theme = $default_type = '';
1027 $first_message_type = current( $this->message_types );
1028
1029 if( is_array( $first_message_type ) ) {
1030 $default_type = $first_message_type['type'];
1031 if( !empty( $first_message_type['themes'] ) ) {
1032 $default_theme = key( $first_message_type['themes'] );
1033 }
1034 }
1035
1036 $new_campaign_ids = array();
1037 foreach ( (array) $data['campaigns'] as $campaign ) {
1038
1039 $args = array(
1040 'post_content' => ( !empty( $campaign['post_content'] ) ) ? esc_attr( $campaign['post_content'] ) : '',
1041 'post_name' => ( !empty( $campaign['post_title'] ) ) ? sanitize_title( $campaign['post_title'] ) : '',
1042 'post_title' => ( !empty( $campaign['post_title'] ) ) ? $campaign['post_title'] : '',
1043 'post_status' => ( !empty( $campaign['post_status'] ) ) ? $campaign['post_status'] : 'draft',
1044 'post_type' => 'ig_campaign'
1045 );
1046
1047 $new_campaign_id = wp_insert_post( $args );
1048 $new_campaign_ids[] = $new_campaign_id;
1049
1050 if ( !empty( $campaign['target_rules'] ) ) {
1051
1052 $defaults = array (
1053 'homepage' => 'yes',
1054 'when' => 'always',
1055 'from' => '',
1056 'to' => '',
1057 'mobile' => 'yes',
1058 'tablet' => 'yes',
1059 'laptop' => 'yes',
1060 'logged_in' => 'all'
1061 );
1062
1063 $target_rules = wp_parse_args( $campaign['target_rules'], $defaults );
1064 update_post_meta( $new_campaign_id, 'icegram_campaign_target_rules', $target_rules );
1065 }
1066
1067 if ( !empty( $campaign['messages'] ) ) {
1068
1069 $messages = array();
1070
1071 foreach ( $campaign['messages'] as $message ) {
1072
1073 if ( !is_array( $message ) ) continue;
1074
1075 $args = array(
1076 'post_content' => ( !empty( $message['message'] ) ) ? esc_attr( $message['message'] ) : '',
1077 'post_name' => ( !empty( $message['post_title'] ) ) ? sanitize_title( $message['post_title'] ) : '',
1078 'post_title' => ( !empty( $message['post_title'] ) ) ? $message['post_title'] : '',
1079 'post_status' => ( !empty( $message['post_status'] ) ) ? $message['post_status'] : 'publish',
1080 'post_type' => 'ig_message'
1081 );
1082
1083 $new_message_id = wp_insert_post( $args );
1084 $new_message = array(
1085 'id' => $new_message_id,
1086 'time' => ( !empty( $message['time'] ) ) ? $message['time'] : 0
1087 );
1088 $messages[] = $new_message;
1089
1090 unset( $message['post_content'] );
1091 unset( $message['time'] );
1092
1093 $message['id'] = $new_message_id;
1094
1095 $defaults = array (
1096 'post_title' => '',
1097 'type' => $default_type,
1098 'theme' => $default_theme,
1099 'animation' => '',
1100 'headline' => '',
1101 'label' => '',
1102 'link' => '',
1103 'icon' => '',
1104 'message' => '',
1105 'position' => '',
1106 'text_color' => '#000000',
1107 'bg_color' => '#ffffff',
1108 'id' => ''
1109 );
1110
1111 $icegram_message_data = wp_parse_args( $message, $defaults );
1112
1113 if ( !empty( $icegram_message_data ) ) {
1114 update_post_meta( $new_message_id, 'icegram_message_data', $icegram_message_data );
1115 update_post_meta( $new_message_id, 'icegram_message_preview_data', $icegram_message_data );
1116 }
1117 }
1118
1119 if ( !empty( $campaign['messages'] ) ) {
1120 update_post_meta( $new_campaign_id, 'messages', $messages );
1121 update_post_meta( $new_campaign_id, 'campaign_preview', $messages );
1122 }
1123
1124 }
1125 }
1126 if( !empty( $new_campaign_ids ) ) {
1127 update_option( 'icegram_sample_data_imported', $new_campaign_ids );
1128 }
1129
1130 }
1131 function get_sample_data() {
1132
1133 return array(
1134 'campaigns' => array(
1135 array(
1136 'post_name' => '',
1137 'post_title' => 'My First Icegram Campaign',
1138 'target_rules' => array (
1139 'homepage' => 'yes',
1140 'when' => 'always',
1141 'from' => '',
1142 'to' => '',
1143 'mobile' => 'yes',
1144 'tablet' => 'yes',
1145 'laptop' => 'yes',
1146 'logged_in' => 'all'
1147 ),
1148 'messages' => array(
1149 array (
1150 'post_title' => 'Get 2x more Contacts with Your Website',
1151 'post_status' => 'publish',
1152 'time' => '0',
1153 'type' => 'action-bar',
1154 'theme' => 'hello',
1155 'headline' => 'Get 2x more Contacts with Your Website',
1156 'label' => 'Show Me How',
1157 'link' => '',
1158 'icon' => '',
1159 'message' => 'Instant Results Guaranteed',
1160 'position' => '01',
1161 'text_color' => '#000000',
1162 'bg_color' => '#eb593c'
1163 ),
1164 array (
1165 'post_title' => '20% Off Coupon',
1166 'post_status' => 'publish',
1167 'time' => '4',
1168 'type' => 'messenger',
1169 'theme' => 'social',
1170 'animation' => 'slide',
1171 'headline' => '20% Off - for you',
1172 'label' => '',
1173 'link' => '',
1174 'icon' => '',
1175 'message' => "Hey there! We are running a <strong>special 20% off this week</strong> for registered users - like you.
1176
1177 Use coupon <code>LOYALTY20</code> during checkout.",
1178 'position' => '22',
1179 'text_color' => '#000000',
1180 'bg_color' => '#ffffff'
1181 ),
1182 array (
1183 'post_title' => 'How this blog makes over $34,800 / month for FREE.',
1184 'post_status' => 'publish',
1185 'time' => '10',
1186 'type' => 'popup',
1187 'theme' => 'air-mail',
1188 'headline' => 'How this blog makes over $34,800 / month for FREE.',
1189 'label' => 'FREE INSTANT ACCESS',
1190 'link' => '',
1191 'icon' => '',
1192 '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.
1193
1194 <strong>Download my free kit to learn how I do this.</strong>
1195
1196 <ul>
1197 <li>How to choose blog topics that create long term value</li>
1198 <li>The type of blog post that will make your site go viral</li>
1199 <li>How to free yourself from the routine tasks</li>
1200 <li>Resources and tips to get started quickly</li>
1201 <li>Private members club to connect with fellow owners</li>
1202 </ul>",
1203 'text_color' => '#000000',
1204 'bg_color' => '#ffffff'
1205
1206 ),
1207 array (
1208 'post_title' => 'Exclusive Marketing Report',
1209 'post_status' => 'publish',
1210 'time' => '6',
1211 'type' => 'toast',
1212 'theme' => 'stand-out',
1213 'animation' => 'pop',
1214 'headline' => 'Exclusive Marketing Report',
1215 'label' => '',
1216 'link' => '',
1217 'icon' => '',
1218 'message' => 'FREE for every subscriber. Click here to download it.',
1219 'position' => '02',
1220 'text_color' => '#000000',
1221 'bg_color' => '#ffffff'
1222 )
1223
1224 )
1225 )
1226 )
1227 );
1228 }
1229
1230 function remove_preview_button() {
1231 global $post_type;
1232 if( $post_type == 'ig_message' || $post_type == 'ig_campaign' ) {
1233 ?>
1234 <style type="text/css">
1235 #message.updated.below-h2{ display: none; }
1236 #preview-action { display:none; }
1237 </style>
1238 <?php
1239 }
1240 }
1241
1242
1243 function remove_row_actions( $actions, $post ) {
1244
1245 if ( empty( $post->post_type ) || ( $post->post_type != 'ig_campaign' && $post->post_type != 'ig_message' ) ) return $actions;
1246
1247 unset( $actions['inline hide-if-no-js'] );
1248 unset( $actions['view'] );
1249
1250 return $actions;
1251
1252 }
1253
1254 function identify_current_page() {
1255 global $post, $wpdb;
1256
1257 $obj = get_queried_object();
1258 $id = 0;
1259 if( !empty( $obj->has_archive ) ) {
1260 $id = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_name = %s AND post_type='page'", $obj->has_archive ) );
1261 } elseif( is_object( $post ) && isset( $post->ID ) ) {
1262 $id = $post->ID;
1263 }
1264 $id = apply_filters('icegram_identify_current_page', $id );
1265 self::$current_page_id = $id;
1266 }
1267
1268 static function get_current_page_id() {
1269 global $post;
1270 if (!empty($_REQUEST['page_id']) && is_numeric($_REQUEST['page_id'])) {
1271 $post = get_post($_REQUEST['page_id']);
1272 setup_postdata( $post );
1273 // WPML check
1274 $id = apply_filters('icegram_identify_current_page', $post->ID );
1275 self::$current_page_id = $id;
1276 }
1277 return self::$current_page_id;
1278 }
1279 static function get_current_page_url() {
1280 if(!empty($_REQUEST['cache_compatibility']) && $_REQUEST['cache_compatibility'] == 'yes'){
1281 $pageURL = (!empty($_REQUEST['referral_url'])) ? $_REQUEST['referral_url'] : '';
1282 }else{
1283 $pageURL = 'http';
1284 if( isset($_SERVER["HTTPS"]) ) {
1285 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
1286 }
1287 $pageURL .= "://";
1288 if ($_SERVER["SERVER_PORT"] != "80") {
1289 $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
1290 } else {
1291 $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
1292 }
1293 }
1294 return $pageURL;
1295 }
1296
1297 function wpml_get_parent_id( $id ) {
1298 global $post;
1299 if (function_exists('icl_object_id') && function_exists('icl_get_default_language') ) {
1300 $id = icl_object_id($id, $post->post_type, true, icl_get_default_language() );
1301 }
1302 return $id;
1303 }
1304
1305
1306 /**
1307 * Our implementation of wpautop to preserve script and style tags
1308 */
1309 function before_wpautop($pee) {
1310 if ( trim($pee) === '' ) {
1311 $this->_wpautop_tags = array();
1312 return '';
1313 }
1314
1315 $tags = array();
1316 // Pull out tags and add placeholders
1317 list( $pee, $tags['pre'] ) = $this->_wpautop_add_tag_placeholders( $pee, 'pre' );
1318 list( $pee, $tags['script'] ) = $this->_wpautop_add_tag_placeholders( $pee, 'script' );
1319 list( $pee, $tags['style'] ) = $this->_wpautop_add_tag_placeholders( $pee, 'style' );
1320 $this->_wpautop_tags = $tags;
1321
1322 if( !empty( $pre_tags ) )
1323 $pee = $this->_wpautop_replace_tag_placeholders( $pee, $pre_tags );
1324 if( !empty( $script_tags ) )
1325 $pee = $this->_wpautop_replace_tag_placeholders( $pee, $script_tags );
1326 if( !empty( $style_tags ) )
1327 $pee = $this->_wpautop_replace_tag_placeholders( $pee, $style_tags );
1328
1329 return $pee;
1330 }
1331
1332 function after_wpautop($pee) {
1333 if ( trim($pee) === '' || empty($this->_wpautop_tags) )
1334 return '';
1335
1336 // Replace placeholders with original content
1337 if (!empty($this->_wpautop_tags['pre'])) {
1338 $pee = $this->_wpautop_replace_tag_placeholders( $pee, $this->_wpautop_tags['pre'] );
1339 }
1340 if (!empty($this->_wpautop_tags['script'])) {
1341 $pee = $this->_wpautop_replace_tag_placeholders( $pee, $this->_wpautop_tags['script'] );
1342 }
1343 if (!empty($this->_wpautop_tags['style'])) {
1344 $pee = $this->_wpautop_replace_tag_placeholders( $pee, $this->_wpautop_tags['style'] );
1345 }
1346
1347 $this->_wpautop_tags = array();
1348
1349 return $pee;
1350 }
1351
1352 function _wpautop_add_tag_placeholders( $pee, $tag ) {
1353 $tags = array();
1354
1355 if ( false !== strpos( $pee, "<{$tag}" ) ) {
1356 $pee_parts = explode( "</{$tag}>", $pee );
1357 $last_pee = array_pop( $pee_parts );
1358 $pee = '';
1359 $i = 0;
1360
1361 foreach ( $pee_parts as $pee_part ) {
1362 $start = strpos( $pee_part, "<{$tag}" );
1363
1364 // Malformed html?
1365 if ( false === $start ) {
1366 $pee .= $pee_part;
1367 continue;
1368 }
1369
1370 $name = "<{$tag} wp-{$tag}-tag-$i></{$tag}>";
1371 $tags[ $name ] = substr( $pee_part, $start ) . "</{$tag}>";
1372
1373 $pee .= substr( $pee_part, 0, $start ) . $name;
1374 $i++;
1375 }
1376
1377 $pee .= $last_pee;
1378 }
1379
1380 return array( $pee, $tags );
1381 }
1382
1383 function _wpautop_replace_tag_placeholders( $pee, $tags ) {
1384 if ( ! empty( $tags ) ) {
1385 $pee = str_replace( array_keys( $tags ), array_values( $tags ), $pee );
1386 }
1387
1388 return $pee;
1389 }
1390
1391 static function duplicate( $original_id ){
1392 // Get access to the database
1393 global $wpdb;
1394 // Get the post as an array
1395 $duplicate = get_post( $original_id, 'ARRAY_A' );
1396 // Modify some of the elements
1397 $duplicate['post_title'] = $duplicate['post_title'].' '.__('Copy', 'icegram');
1398 $duplicate['post_status'] = 'draft';
1399 // Set the post date
1400 $timestamp = current_time('timestamp',0);
1401
1402 $duplicate['post_date'] = date('Y-m-d H:i:s', $timestamp);
1403
1404 // Remove some of the keys
1405 unset( $duplicate['ID'] );
1406 unset( $duplicate['guid'] );
1407 unset( $duplicate['comment_count'] );
1408
1409 // Insert the post into the database
1410 $duplicate_id = wp_insert_post( $duplicate );
1411
1412 // Duplicate all taxonomies/terms
1413 $taxonomies = get_object_taxonomies( $duplicate['post_type'] );
1414 foreach( $taxonomies as $taxonomy ) {
1415 $terms = wp_get_post_terms( $original_id, $taxonomy, array('fields' => 'names') );
1416 wp_set_object_terms( $duplicate_id, $terms, $taxonomy );
1417 }
1418
1419 // Duplicate all custom fields
1420 $custom_fields = get_post_custom( $original_id );
1421 foreach ( $custom_fields as $key => $value ) {
1422 add_post_meta( $duplicate_id, $key, maybe_unserialize($value[0]) );
1423 }
1424 $location = admin_url( 'post.php?post='.$duplicate_id.'&action=edit');
1425 header('Location:'.$location);
1426 exit;
1427 }
1428
1429 }
1430
1431 function initialize_icegram() {
1432 global $icegram;
1433
1434 // i18n / l10n - load translations
1435 load_plugin_textdomain( 'icegram', false, dirname( plugin_basename( __FILE__ ) ) . '/lang/' );
1436
1437 $icegram = new Icegram();
1438 do_action('icegram_loaded');
1439 }
1440
1441 add_action( 'plugins_loaded', 'initialize_icegram' );
1442 register_activation_hook( __FILE__, array( 'Icegram', 'install' ) );