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

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