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

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