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

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