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

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

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