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

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