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

1,727 lines 83.8 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 - Popups, Optins, CTAs & lot more...
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.21
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.21";
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 add_filter( 'rainmaker_validate_request', array(&$this,'form_submission_validate_request'), 10, 2);
90
91 if ( defined( 'DOING_AJAX' ) ) {
92 if($this->cache_compatibility === 'yes'){
93 add_action( 'wp_ajax_display_messages', array( &$this, 'display_messages' ) );
94 add_action( 'wp_ajax_nopriv_display_messages', array( &$this, 'display_messages' ) );
95 }
96 add_action( 'wp_ajax_icegram_event_track', array( &$this, 'icegram_event_track' ) );
97 add_action( 'wp_ajax_nopriv_icegram_event_track', array( &$this, 'icegram_event_track' ) );
98 add_action( 'wp_ajax_klawoo_subscribe', array( &$this, 'klawoo_subscribe' ) );
99 add_action( 'wp_ajax_icegram_run_housekeeping', array( &$this, 'run_housekeeping' ) );
100
101 }
102
103 }
104 function ig_plugin_settings_link($links, $file){
105 if ($file == plugin_basename(__FILE__)){
106 $addons_link = '<a href="edit.php?post_type=ig_campaign&page=icegram-addons" >'.__('Addons', 'icegram').'</a>';
107 $settings_link = '<a href="edit.php?post_type=ig_campaign&page=icegram-settings">'.__('Settings', 'icegram').'</a>';
108 $support_link = '<a href="edit.php?post_type=ig_campaign&page=icegram-support">'.__('Support', 'icegram').'</a>';
109 array_unshift($links, $support_link);
110 array_unshift($links, $settings_link);
111 array_unshift($links, $addons_link);
112 }
113 return $links;
114
115 }
116
117 public function load_compat_classes() {
118
119 $compat_classes = (array) glob( $this->plugin_path . '/classes/compat/class-icegram-compat-*.php' );
120 if (empty($compat_classes)) {
121 return;
122 }
123
124 $active_plugins = (array) get_option('active_plugins', array());
125 if (is_multisite()) {
126 $active_plugins = array_merge($active_plugins, get_site_option('active_sitewide_plugins', array()));
127 }
128 $active_plugins = array_unique( array_merge( array_values($active_plugins), array_keys($active_plugins)) );
129 $active_plugins_with_slugs = array();
130 foreach ($active_plugins as $key => $value) {
131 $slug = dirname( $value );
132 if ($slug == '.') {
133 unset($active_plugins[$key]);
134 } else {
135 $active_plugins[ $key ] = $slug;
136 }
137 }
138
139 foreach ($compat_classes as $file) {
140 if (is_file ( $file )) {
141 $slug = str_replace('class-icegram-compat-', '', str_replace(".php", "", basename( $file )) );
142 if ( in_array($slug, $active_plugins)) {
143 include_once( $file );
144 $class_name = 'Icegram_Compat_'. str_replace('-', '_', $slug);
145 if ( class_exists( $class_name ) ) {
146 new $class_name();
147 }
148 }
149 }
150 }
151 }
152 public function add_admin_notices(){
153 $screen = get_current_screen();
154 if ( !in_array( $screen->id, array( 'ig_campaign', 'ig_message','edit-ig_message','edit-ig_campaign' ), true ) ) return;
155 $active_plugins = get_option( 'active_plugins', array() );
156 ?>
157 <style type="text/css">
158 div.icegram_notice {
159 border-left: 4px solid #7bcf3a;
160 padding: 1px 12px;
161 background-color: #FFF;
162 -webkit-box-shadow: 0 1px 1px 0 rgba(0, 0, 0, 0.1);
163 box-shadow: 0 1px 1px 0 rgba(0, 0, 0, 0.1);
164 margin: 15px 20px 10px 6px;
165 }
166 a.ig-admin-btn{
167 margin-left: 10px;
168 padding: 4px 8px;
169 position: relative;
170 text-decoration: none;
171 border: none;
172 -webkit-border-radius: 2px;
173 border-radius: 2px;
174 background: #e0e0e0;
175 text-shadow: none;
176 font-weight: 600;
177 font-size: 13px;
178 }
179 a.ig-admin-btn-secondary{
180 background: #fafafa;
181 margin-left: 20px;
182 font-weight: 400;
183 }
184
185 a.ig-admin-btn:hover{
186 color: #FFF;
187 background-color: #363b3f;
188 }
189 </style>
190 <?php
191 // Admin notices for free bonuses!
192 $admin_notice_text = '';
193 if(!get_option('dismiss_admin_notice_from_icegram') && !in_array('ig-analytics/ig-analytics.php', $active_plugins)){
194 //analytics
195 $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>";
196 $no_offer_text = __('No, I don\'t like offers...','icegram');
197 $option_name = 'dismiss_admin_notice_from';
198 }
199 $offer_content = array();
200 $offer_content = apply_filters('ig_in_app_offers', $offer_content);
201
202 if(!empty($offer_content['option_name']) && !get_option($offer_content['option_name']) && in_array('ig-analytics/ig-analytics.php', $active_plugins)){
203 $admin_notice_text = !empty($offer_content['admin_notice_text']) ? $offer_content['admin_notice_text'] : '';
204 $no_offer_text_set = array('save_money' => __('No, I don\'t want to save money ','icegram'),
205 'full_price' => __('No, I will buy at full price later','icegram'),
206 'insider_deals' => __('No, I don\'t like insider deals','icegram'),
207 );
208 $key = array_rand($no_offer_text_set);
209 $no_offer_text = $no_offer_text_set[$key];
210 $option_name = $offer_content['option_name'];
211 }
212 if($admin_notice_text != ''){
213 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.
214 '<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>
215 </div>';
216 }
217
218 //Admin notice for Rainmaker
219 $admin_notice_for_rm = '';
220 if(('install_plugins') && !in_array('icegram-rainmaker/icegram-rainmaker.php', $active_plugins) && !get_option('ig_rm_notice_icegram')){
221 $url = wp_nonce_url(self_admin_url('update.php?action=install-plugin&plugin=' . 'icegram-rainmaker'), 'install-plugin_' . 'icegram-rainmaker');
222 $admin_notice_text_for_rm = "Icegram recommends <b>Rainmaker</b>";
223 echo '<div class="notice notice-warning"> <p> '.$admin_notice_text_for_rm.'<a style="display:inline-block" class="ig-admin-btn" href="'.$url.'">'.__('Begin installation','icegram').'</a><a style="display:inline-block" class="ig-admin-btn ig-admin-btn-secondary" href="?dismiss_admin_notice=1&option_name=ig_rm_notice">'.__('No, I don\'t need it', 'icegram').'</a></p></div>';
224 }
225
226
227 }
228 public function dismiss_admin_notice(){
229 if(isset($_GET['dismiss_admin_notice']) && $_GET['dismiss_admin_notice'] == '1' && isset($_GET['option_name'])){
230 $option_name = sanitize_text_field($_GET['option_name']);
231 update_option($option_name.'_icegram', true);
232 wp_safe_redirect($_SERVER['HTTP_REFERER']);
233 exit();
234 }
235 }
236
237 //add in app promotion offers here
238 public function add_in_app_offer($offer_content){
239 return $offer_content;
240 }
241
242 public function klawoo_subscribe_form() {
243 ?>
244 <div class="wrap">
245 <?php
246 if ( stripos(get_current_screen()->base, 'settings') !== false ) {
247 echo "<h2>".__( 'Free Add-ons, Proven Marketing Tricks and Updates', 'icegram' )."</h2>";
248 }
249 ?>
250 <table class="form-table">
251 <tr>
252 <th scope="row"><?php _e( 'Get add-ons and tips...', 'icegram' ) ?></th>
253 <td>
254 <form name="klawoo_subscribe" action="#" method="POST" accept-charset="utf-8">
255 <input class="ltr" type="text" name="name" id="name" placeholder="Name"/>
256 <input class="regular-text ltr" type="text" name="email" id="email" placeholder="Email"/>
257 <input type="hidden" name="list" value="7I763v6Ldrs3YhJeee5EOgFA"/>
258 <input type="submit" name="submit" id="submit" class="button button-primary" value="Subscribe">
259 <br/>
260 <div id="klawoo_response"></div>
261 </form>
262 </td>
263 </tr>
264 </table>
265 </div>
266 <script type="text/javascript">
267 jQuery(function () {
268 jQuery("form[name=klawoo_subscribe]").submit(function (e) {
269 e.preventDefault();
270
271 jQuery('#klawoo_response').html('');
272 params = jQuery("form[name=klawoo_subscribe]").serializeArray();
273 params.push( {name: 'action', value: 'klawoo_subscribe' });
274
275 jQuery.ajax({
276 method: 'POST',
277 type: 'text',
278 url: "<?php echo admin_url( 'admin-ajax.php' ); ?>",
279 data: params,
280 success: function(response) {
281 if (response != '') {
282 jQuery('#klawoo_response').html(response);
283 } else {
284 jQuery('#klawoo_response').html('error!');
285 }
286 }
287 });
288 });
289 });
290 </script>
291 <?php
292 }
293
294
295 public function klawoo_subscribe() {
296 $url = 'http://app.klawoo.com/subscribe';
297
298 if( !empty( $_POST ) ) {
299 $params = $_POST;
300 } else {
301 exit();
302 }
303 $method = 'POST';
304 $qs = http_build_query( $params );
305
306 $options = array(
307 'timeout' => 15,
308 'method' => $method
309 );
310
311 if ( $method == 'POST' ) {
312 $options['body'] = $qs;
313 } else {
314 if ( strpos( $url, '?' ) !== false ) {
315 $url .= '&'.$qs;
316 } else {
317 $url .= '?'.$qs;
318 }
319 }
320
321 $response = wp_remote_request( $url, $options );
322 if ( wp_remote_retrieve_response_code( $response ) == 200 ) {
323 $data = $response['body'];
324 if ( $data != 'error' ) {
325
326 $message_start = substr( $data, strpos( $data,'<body>' ) + 6 );
327 $remove = substr( $message_start, strpos( $message_start,'</body>' ) );
328 $message = trim( str_replace( $remove, '', $message_start ) );
329 echo ( $message );
330 exit();
331 }
332 }
333 exit();
334 }
335
336 public function icegram_houskeeping(){
337 ?>
338 <div class="wrap">
339 <?php
340 if ( stripos(get_current_screen()->base, 'settings') !== false ) {
341 ?>
342 <form name="icegram_housekeeping" action="#" method="POST" accept-charset="utf-8">
343 <h2><?php _e( 'Housekeeping', 'icegram' ) ?></h2>
344 <p class="ig_housekeeping">
345 <label for="icegram_remove_shortcodes">
346 <input type="checkbox" name="icegram_remove_shortcodes" value="yes" />
347 <?php _e( 'Remove all Icegram shortcodes', 'icegram' ); ?>
348 </label>
349 <br/><br/>
350 <label for="icegram_remove_all_data">
351 <input type="checkbox" name="icegram_remove_all_data" value="yes" />
352 <?php _e( 'Remove all Icegram campaigns and messages', 'icegram' ); ?>
353 </label>
354 <br/><br/>
355 <img alt="" src="<?php echo admin_url( 'images/wpspin_light.gif' ) ?>" class="ig_loader" style="vertical-align:middle;display:none" />
356 <input type="submit" name="submit" id="submit" class="button button-primary" value="<?php _e( 'Clean Up', 'icegram' ); ?>">
357 <div id="icegram_housekeeping_response"></div>
358 </p>
359 </form>
360
361 </div>
362 <script type="text/javascript">
363 jQuery(function () {
364 jQuery("form[name=icegram_housekeeping]").submit(function (e) {
365 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){
366 e.preventDefault();
367 jQuery('.ig_loader').show();
368 jQuery('#icegram_housekeeping_response').text("");
369 params = jQuery("form[name=icegram_housekeeping]").serializeArray();
370 params.push( {name: 'action', value: 'icegram_run_housekeeping' });
371 params.push( {name: 'security', value: <?php echo wp_create_nonce('ig_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 check_ajax_referer('ig_run_housekeeping', 'security');
392 global $wpdb, $current_user;
393 $params = $_POST;
394 $_POST = array();
395 if(current_user_can( 'manage_options' ) && !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.min.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_register_script('ig_gallery_js', $this->plugin_url . '/assets/js/gallery.min.js', array ( 'jquery','backbone','wp-backbone','wp-a11y','wp-util' ), $this->version, true);
569 if( !wp_script_is( 'ig_gallery_js' ) ) {
570 wp_enqueue_script( 'ig_gallery_js' );
571 $imported_gallery_items = get_option('ig_imported_gallery_items',true);
572 wp_localize_script( 'ig_gallery_js', '_wpThemeSettings', array(
573 'themes' => array(),
574 'settings' => array(
575 'canInstall' => ( ! is_multisite() && ( 'install_themes' ) ),
576 'installURI' => ( ! is_multisite() && ( 'install_themes' ) ) ? admin_url( 'theme-install.php' ) : null,
577 'confirmDelete' => __( "Are you sure you want to delete this theme?\n\nClick 'Cancel' to go back, 'OK' to confirm the delete." ),
578 'adminUrl' => parse_url( admin_url(), PHP_URL_PATH ),
579 ),
580 'l10n' => array(
581 'addNew' => __( 'Add New Gallery Item' ),
582 'search' => __( 'Search Installed Gallery Item' ),
583 'searchPlaceholder' => __( 'Search installed Gallery Item...' ), // placeholder (no ellipsis)
584 'themesFound' => __( 'Number of Gallery Item found: %d' ),
585 'noThemesFound' => __( 'No Gallery Item found. Try a different search.' ),
586 ),
587 'installedThemes' => $imported_gallery_items
588 ) );
589 //wp_localize_script( 'ig_gallery_js', 'icegram_pre_data' , $icegram_pre_data);
590 }
591 }
592
593 public function check_for_addons( $force_update = false ) {
594
595 if ( $force_update === true || false === ( $ig_addons = get_transient( 'icegram_addons_data' ) ) ) {
596 $ig_addons_json = wp_remote_get( 'http://icegram.com/addons.json', array( 'user-agent' => 'Icegram Addons' ) );
597
598 if ( ! is_wp_error( $ig_addons_json ) ) {
599 $ig_addons = json_decode( wp_remote_retrieve_body( $ig_addons_json ));
600 $addon_count = get_option( 'icegram_no_of_addons', 0 );
601 if ( !empty($ig_addons) && is_array($ig_addons) ) {
602 set_transient( 'icegram_addons_data', $ig_addons, 24 * HOUR_IN_SECONDS ); // 1 day
603 update_option( 'icegram_no_of_addons', count( $ig_addons ) - $addon_count ); // display count of newly added addons
604 }
605 }
606 }
607 return $ig_addons;
608 }
609
610 public function branding_data_remove( $icegram_branding_data ) {
611 if( !empty( $icegram_branding_data ) && 'yes' != get_option('icegram_share_love', 'no') ) {
612 $icegram_branding_data['powered_by_logo'] = '';
613 $icegram_branding_data['powered_by_text'] = '';
614 }
615 return $icegram_branding_data;
616 }
617
618 //Execute Form shortcode
619 function execute_form_shortcode( $atts = array() ) {
620 return '<div class="ig_form_container layout_inline"></div>';
621 }
622
623 function execute_shortcode( $atts = array() , $content = null ) {
624 // When shortcode is called, it will only prepare an array with conditions
625 // And add a placeholder div
626 // Display will happen in footer via display_messages()
627 $i = count($this->shortcode_instances);
628 $this->shortcode_instances[ $i ] = shortcode_atts( array(
629 'campaigns' => '',
630 'messages' => '',
631 'skip_others' => 'no'
632 ), $atts );
633
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.min.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 $this->shortcode_instances = ($this->cache_compatibility == 'yes' && !empty($_REQUEST['shortcodes'])) ? $_REQUEST['shortcodes'] : $this->shortcode_instances;
673 // Pull in message and campaign IDs from shortcodes - if set
674 if( !empty( $this->shortcode_instances ) ) {
675 foreach ($this->shortcode_instances as $i => $value) {
676 $cids = array_map( 'trim', (array) explode( ',', $value['campaigns'] ) );
677 $mids = array_map( 'trim', (array) explode( ',', $value['messages'] ) );
678 if (!empty($value['skip_others']) && $value['skip_others'] == 'yes' && (!empty($cids) || !empty($mids))) {
679 $skip_others = true;
680 }
681 $campaign_ids = array_merge($campaign_ids, $cids);
682 $message_ids = array_merge($message_ids, $mids);
683 }
684 }
685 if( !empty( $_REQUEST['campaign_preview_id'] ) && ( 'edit_posts' ) ) {
686 $campaign_ids = array( $_REQUEST['campaign_preview_id'] );
687 $preview_mode = true;
688 }
689
690
691 $messages = $this->get_valid_messages( $message_ids, $campaign_ids, $preview_mode, $skip_others );
692
693 if( empty( $messages ) ) {
694 //wp_die(0);
695 return;
696 }
697
698 $messages_to_show_ids = array();
699 foreach ( $messages as $key => $message_data ) {
700
701 if( !is_array( $message_data ) || empty( $message_data ) ) {
702 continue;
703 }
704
705 // Don't show a seen message again - if needed
706 // change to campaign targetting in v1.9.1
707 if( !empty( $message_data['id'] ) &&
708 empty( $_GET['campaign_preview_id'] ) &&
709 !empty( $message_data['retargeting'] ) &&
710 $message_data['retargeting'] == 'yes'
711 ) {
712 if(!empty($_COOKIE['icegram_messages_shown_'.$message_data['id']]) || !empty($_COOKIE['icegram_campaign_shown_'.floor($message_data['campaign_id'])])){
713 unset( $messages[$key] );
714 continue;
715 }
716 }
717 if( !empty( $message_data['id'] ) &&
718 empty( $_GET['campaign_preview_id'] ) &&
719 !empty( $message_data['retargeting_clicked'] ) &&
720 $message_data['retargeting_clicked'] == 'yes'
721 ) {
722 if(!empty($_COOKIE['icegram_messages_clicked_'.$message_data['id']]) || !empty($_COOKIE['icegram_campaign_clicked_'.floor($message_data['campaign_id'])])){
723 unset( $messages[$key] );
724 continue;
725 }
726 }
727
728 // Avoid showing the same message twice
729 if (in_array($message_data['id'], $messages_to_show_ids)) {
730 unset ( $messages[$key] );
731 continue;
732 } else {
733 $messages_to_show_ids[] = $message_data['id'];
734 }
735
736
737 /*
738 // Our own implementation so WP does not mess with script, style and pre tags
739 add_filter('the_content', array( $this, 'before_wpautop' ) , 9);
740 add_filter('the_content', array( $this, 'after_wpautop' ) , 11);
741 $messages[$key]['message'] = apply_filters( 'the_content', $message_data['message'] );
742 remove_filter('the_content', array( $this, 'before_wpautop' ) , 9);
743 remove_filter('the_content', array( $this, 'after_wpautop' ) , 11);
744 */
745 // Redo the_content functionality to avoid other plugins adding extraneous code to messages
746 $this->process_message_body($messages[$key]);
747 }
748
749 if( empty( $messages ) )
750 return;
751 $icegram_default = apply_filters( 'icegram_branding_data',
752 array ( 'icon' => $this->plugin_url . '/assets/images/icegram-logo-branding-64-grey.png',
753 'powered_by_logo' => $this->plugin_url . '/assets/images/icegram-logo-branding-64-grey.png',
754 'powered_by_text' => __( 'Powered by Icegram', 'icegram' )
755 ) );
756 $messages = apply_filters( 'icegram_messages_to_show', $messages );
757 $icegram_data = apply_filters( 'icegram_data', array ( 'messages' => array_values( $messages ),
758 'ajax_url' => admin_url( 'admin-ajax.php' ),
759 'preview_id' => !empty( $_GET['campaign_preview_id'] ) ? $_GET['campaign_preview_id'] : '',
760 'defaults' => $icegram_default
761 ));
762 if (empty($icegram_data['preview_id'])) {
763 unset($icegram_data['preview_id']);
764 }
765 do_action('icegram_print_js_css_data', $icegram_data);
766
767 do_action('icegram_data_printed');
768 }
769
770 function print_js_css_data( $icegram_data ) {
771
772 $this->collect_js_and_css($icegram_data);
773 if($this->cache_compatibility === 'yes'){
774 echo json_encode($icegram_data);
775 wp_die();
776 }else{
777 wp_localize_script('icegram_main_js', 'icegram_data', $icegram_data);
778 }
779 }
780
781 function collect_js_and_css(&$icegram_data){
782 $types_shown = array();
783 $scripts = array();
784 $css = array();
785 foreach ($icegram_data['messages'] as $key => $message_data) {
786 $types_shown[] = $message_data['type'];
787 }
788 $types_shown = array_unique($types_shown);
789 $ver_psfix = '?var=' .$this->version;
790
791 $scripts[] = $this->plugin_url ."/assets/js/icegram.min.js". $ver_psfix;
792 $css[] = $this->plugin_url ."/assets/css/frontend.min.css". $ver_psfix;
793 //minify and combine only for default msg type
794 $ig_core_message_types = array('popup', 'action-bar', 'toast', 'messenger');
795 // Load JS and default CSS
796 foreach ($types_shown as $message_type) {
797 if(!in_array($message_type, $ig_core_message_types)){
798 $scripts[] = $this->message_types[$message_type]['baseurl'] ."main.js";
799 $css[] = $this->message_types[$message_type]['baseurl'] . "default.css";
800 }else{
801 $css[] = $this->message_types[$message_type]['baseurl'].'themes/'. $message_type. ".min.css". $ver_psfix;
802 }
803 }
804 //TODO :: add theme pack theme css files too.
805 // Load theme CSS
806 foreach ($icegram_data['messages'] as $key => $message) {
807 if ( !empty( $this->message_types[ $message['type'] ]['themes'][ $message['theme'] ]) ) {
808 $theme = $this->message_types[ $message['type'] ]['themes'][ $message['theme'] ]['baseurl'] . $message['theme'].'.css';
809 }else{
810 $theme_default = $this->message_types[ $message['type']] ['settings']['theme']['default'];
811 $theme = $this->message_types[ $message['type'] ]['themes'][ $theme_default]['baseurl'] . $theme_default.'.css';
812 $icegram_data['messages'][$key]['theme'] = $theme_default;
813 }
814 if(!preg_match('/icegram\/message-types/i', $theme)){
815 $css [] = $theme;
816 }
817 }
818 $css = array_unique($css);
819 $icegram_data['scripts'] = apply_filters('add_icegram_script' , $scripts);
820 $icegram_data['css'] = apply_filters('add_icegram_css' , $css);
821 return $icegram_data;
822 }
823
824 // Process
825 function process_message_body(&$message_data){
826 global $wp_scripts;
827 global $wp_styles;
828
829 if($this->cache_compatibility == 'yes'){
830 $q_script = !empty($wp_scripts->queue) ? $wp_scripts->queue : array();
831 $q_style = !empty($wp_styles->queue) ? $wp_styles->queue : array();
832 }
833 $content = $message_data['message'];
834 $content = convert_chars(convert_smilies( wptexturize( $content )));
835 if(isset($GLOBALS['wp_embed'])) {
836 $content = $GLOBALS['wp_embed']->autoembed($content);
837 }
838 $content = $this->after_wpautop( wpautop( $this->before_wpautop( $content ) ) );
839 $content = do_shortcode( shortcode_unautop( $content ) );
840 $message_data['message'] = $content;
841
842 //do_shortcode in headline
843 $message_data['headline'] = do_shortcode( shortcode_unautop( $message_data['headline'] ) );
844 //shortcode support for Third party forms and Rainmaker
845 $form_html_original = !empty($message_data["rainmaker_form_code"])
846 ? ('[rainmaker_form id="'. $message_data["rainmaker_form_code"] .'"]')
847 :(!empty($message_data['form_html_original']) ? $message_data['form_html_original'] : '');
848 if(!empty($form_html_original)){
849 $message_data['form_html'] = do_shortcode( shortcode_unautop( $form_html_original) );
850 }
851 //TODO :: Handle case for inline style and script
852 if($this->cache_compatibility == 'yes'){
853 $handles = !empty($wp_scripts->queue) ? array_diff($wp_scripts->queue, $q_script) : array();
854 unset($q_script);
855 if(!empty($handles)){
856 if(empty($message_data['assets']))
857 $message_data['assets'] = array();
858
859 ob_start();
860 $wp_scripts->do_items($handles);
861 $message_data['assets']['scripts'] = array_filter(explode('<script', ob_get_clean()));
862 }
863
864 //TODO :: do_items if required
865 $handles = !empty($wp_styles->queue) ? array_diff($wp_styles->queue, $q_style) : array();
866 unset($q_style);
867 if(!empty($handles)){
868 if(empty($message_data['assets']))
869 $message_data['assets'] = array();
870
871 foreach ($handles as $handle) {
872 ob_start();
873 $wp_styles->do_item($handle);
874 $message_data['assets']['styles'][$handle] = ob_get_clean();
875 }
876 }
877 }
878 }
879
880 function enqueue_admin_styles_and_scripts() {
881
882 $screen = get_current_screen();
883 if ( !in_array( $screen->id, array( 'ig_campaign', 'ig_message', 'edit-ig_campaign' ), true ) ) return;
884
885 // Register scripts
886 wp_register_script( 'icegram_writepanel', $this->plugin_url . '/assets/js/admin.min.js', array ( 'jquery', 'wp-color-picker' ), $this->version );
887
888 wp_enqueue_script( 'icegram_writepanel' );
889
890 $icegram_writepanel_params = array ( 'ajax_url' => admin_url( 'admin-ajax.php' ), 'search_message_nonce' => wp_create_nonce( "search-messages" ), 'home_url' => home_url( '/' ) );
891 $this->available_headlines = apply_filters( 'icegram_available_headlines', array() );
892 $icegram_writepanel_params = array_merge( $icegram_writepanel_params, array( 'available_headlines' => $this->available_headlines ) );
893
894 wp_localize_script( 'icegram_writepanel', 'icegram_writepanel_params', $icegram_writepanel_params );
895
896 wp_enqueue_style( 'dashicons' );
897 wp_enqueue_style( 'wp-color-picker' );
898 wp_enqueue_style( 'icegram_admin_styles', $this->plugin_url . '/assets/css/admin.min.css', array(), $this->version );
899
900 if ( !wp_script_is( 'jquery-ui-datepicker' ) ) {
901 wp_enqueue_script( 'jquery-ui-datepicker' );
902 }
903
904 }
905
906 //execute shortcode in text widget
907 function ig_widget_text_filter($content){
908 if ( ! preg_match( '/\[[\r\n\t ]*icegram?[\r\n\t ].*?\]/', $content ) )
909 return $content;
910
911 $content = do_shortcode( $content );
912
913 return $content;
914 }
915 public static function get_platform() {
916 $mobile_detect = new Ig_Mobile_Detect();
917 $mobile_detect->setUserAgent();
918 if($mobile_detect->isMobile()){
919 return ($mobile_detect->isTablet()) ? 'tablet' : 'mobile';
920 }else if($mobile_detect->isTablet()){
921 return 'tablet';
922 }
923 return 'laptop';
924 }
925
926 function get_message_data( $message_ids = array(), $preview = false ) {
927 global $wpdb;
928 $message_data = array();
929 $original_message_id_map = array();
930 $meta_key = $preview ? 'icegram_message_preview_data' : 'icegram_message_data';
931 $message_data_query = "SELECT post_id, meta_value FROM {$wpdb->prefix}postmeta WHERE meta_key LIKE '$meta_key'";
932 if ( !empty( $message_ids ) && is_array( $message_ids ) ) {
933 // For WPML compatibility
934 if ( function_exists('icl_object_id') ) {
935 $wpml_settings = get_option('icl_sitepress_settings');
936 $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;
937
938 foreach ($message_ids as $i=>$id ) {
939 $translated = icl_object_id( $id, 'ig_message', $original_if_missing );
940 $message_ids[ $i ] = $translated;
941 $original_message_id_map[ $translated ] = $id;
942 }
943 }
944 $message_ids = array_filter(array_unique($message_ids));
945 if ( !empty( $message_ids ) ) {
946 $message_data_query .= " AND post_id IN ( " . implode( ',', $message_ids ) . " )";
947 $message_data_results = $wpdb->get_results( $message_data_query, 'ARRAY_A' );
948 foreach ( $message_data_results as $message_data_result ) {
949 $data = maybe_unserialize( $message_data_result['meta_value'] );
950 if (!empty($data)) {
951 $message_data[$message_data_result['post_id']] = $data;
952 // For WPML compatibility
953 if (!empty( $original_message_id_map[ $message_data_result['post_id'] ])) {
954 $message_data[$message_data_result['post_id']]['original_message_id'] = $original_message_id_map[ $message_data_result['post_id'] ];
955 }
956 }
957 }
958 }
959 }
960
961 return $message_data;
962 }
963
964 function get_valid_messages( $message_ids = array(), $campaign_ids = array(), $preview_mode = false, $skip_others = false) {
965 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));
966
967 $valid_messages = $valid_campaigns = $message_campaign_map = array();
968
969 $campaign_ids = array_filter(array_unique( (array) $campaign_ids));
970 $message_ids = array_filter(array_unique( (array) $message_ids));
971 if ( !empty( $campaign_ids ) ) {
972 $valid_campaigns = $this->get_valid_campaigns( $campaign_ids, true ,$preview_mode);
973 }
974
975 // When skip_others is true, we won't load campaigns / messages from db
976 if (!$skip_others && !$preview_mode) {
977 $campaigns = $this->get_valid_campaigns();
978 if (!empty($campaigns)) {
979 foreach ($campaigns as $id => $campaign) {
980 if (!array_key_exists($id, $valid_campaigns)) {
981 $valid_campaigns[ $id ] = $campaign;
982 }
983 }
984 }
985 }
986
987 // Create a map to look up campaign id for a given message
988 if( !empty( $valid_campaigns ) ) {
989 foreach ($valid_campaigns as $id => $campaign) {
990 if ($preview_mode) {
991 $campaign->messages = get_post_meta( $id, 'campaign_preview', true );
992 }
993 if( !empty( $campaign->messages ) ) {
994 foreach( $campaign->messages as $msg) {
995 $message_ids[] = $msg['id'];
996 if (!array_key_exists( $msg['id'], $message_campaign_map)) {
997 $message_campaign_map[ $msg['id'] ] = $id;
998 }
999 }
1000 }
1001 }
1002 }
1003
1004 // We don't display same message twice...
1005 $message_ids = array_unique($message_ids);
1006
1007 if( empty( $message_ids ) ) {
1008 return array();
1009 }
1010 $valid_messages = $this->get_message_data( $message_ids, $preview_mode );
1011
1012 foreach ($valid_messages as $id => $message_data) {
1013 // Remove message if required fields are missing
1014 if (empty($message_data) || empty($message_data['type'])) {
1015 unset( $valid_messages[$id] );
1016 continue;
1017 }
1018 // Remove message if message type is uninstalled
1019 $class_name = 'Icegram_Message_Type_' . str_replace(' ', '_', ucwords(str_replace('-', ' ', $message_data['type'])));
1020 if( !class_exists( $class_name ) ) {
1021 unset( $valid_messages[$id] );
1022 continue;
1023 }
1024 $message_data['delay_time'] = 0;
1025 $message_data['retargeting'] = '';
1026 $message_data['campaign_id'] = ($preview_mode) ? $_REQUEST['campaign_preview_id'] : '';
1027
1028 // Pull display time and retargeting rule from campaign if possible
1029 $message_id = (!empty($message_data['original_message_id'])) ? $message_data['original_message_id'] : $id;
1030 if (!empty($message_campaign_map[ $message_id ])) {
1031 //modify campaign id
1032 $message_data['campaign_id'] = apply_filters('modify_campaing_id' , $message_campaign_map[ $message_id ] , $message_id) ;
1033 $campaign = $valid_campaigns[ floor($message_data['campaign_id']) ];
1034 if (!empty($campaign) && $campaign instanceof Icegram_Campaign) {
1035 $message_meta_from_campaign = $campaign->get_message_meta_by_id( $message_id );
1036 if (!empty($message_meta_from_campaign['time'])) {
1037 $message_data['delay_time'] = $message_meta_from_campaign['time'];
1038 }
1039 //set delay time -1 if shortcode with content
1040 foreach ($this->shortcode_instances as $i => $value) {
1041 $campaign_ids = explode( ',', $value['campaigns']);
1042 if(!empty($value['with_content']) && in_array($message_data['campaign_id'], $campaign_ids)){
1043 $message_data['delay_time'] = -1;
1044 }
1045 }
1046 $rule_value = $campaign->get_rule_value('retargeting');
1047 $message_data['retargeting'] = !empty( $rule_value['retargeting'] ) ? $rule_value['retargeting'] : '';
1048 $message_data['expiry_time'] = !empty( $rule_value['retargeting'] ) ? $rule_value['expiry_time'] : '';
1049 $rule_value_retargeting_clicked = $campaign->get_rule_value('retargeting_clicked');
1050 $message_data['retargeting_clicked'] = !empty( $rule_value_retargeting_clicked['retargeting_clicked'] ) ? $rule_value_retargeting_clicked['retargeting_clicked'] : '';
1051 $message_data['expiry_time_clicked'] = !empty( $rule_value_retargeting_clicked['retargeting_clicked'] ) ? $rule_value_retargeting_clicked['expiry_time_clicked'] : '';
1052
1053 }
1054 }
1055 $valid_messages[$id] = $message_data;
1056 }
1057 $valid_messages = apply_filters( 'icegram_valid_messages', $valid_messages );
1058
1059 return $valid_messages;
1060 }
1061
1062 function get_valid_campaigns( $campaign_ids = array(), $skip_page_check = false ,$preview_mode = false) {
1063 global $wpdb;
1064 if ( empty( $campaign_ids ) ) {
1065 $sql = "SELECT pm.post_id
1066 FROM {$wpdb->prefix}posts AS p
1067 LEFT JOIN {$wpdb->prefix}postmeta AS pm ON ( pm.post_id = p.ID )
1068 WHERE p.post_status = 'publish' ";
1069 // Filter handler within this file (and possibly others) will append to this SQL
1070 // and provide arguments for wpdb->prepare if needed.
1071 // First element in the array is SQL, remaining are values for placeholders in SQL
1072 $sql_params = apply_filters( 'icegram_get_valid_campaigns_sql', array($sql), array() );
1073 $campaign_ids = $wpdb->get_col( $wpdb->prepare( array_shift($sql_params), $sql_params ) );
1074 }
1075 $valid_campaigns = array();
1076 foreach ( (array) $campaign_ids as $campaign_id ) {
1077 $campaign = new Icegram_Campaign( $campaign_id );
1078 if ( $preview_mode || $campaign->is_valid( array('skip_page_check' => $skip_page_check) ) ) {
1079 $valid_campaigns[$campaign_id] = $campaign;
1080 } else {
1081 // Campgain is invalid!
1082 }
1083
1084 }
1085 $valid_campaigns = apply_filters('icegram_valid_campaigns', $valid_campaigns);
1086 return $valid_campaigns;
1087 }
1088
1089 function append_to_valid_campaigns_sql( $sql_params = array(), $options = array() ) {
1090 // Page check conditions
1091 //$pid = $_GET['page_id'];
1092 $pid = Icegram::get_current_page_id();
1093 $sql = " AND (
1094 pm.meta_key = 'icegram_campaign_target_rules' AND (
1095 ( pm.meta_value LIKE '%%%s%%' )
1096 OR ( pm.meta_value LIKE '%%%s%%' AND pm.meta_value LIKE '%%%s%%' AND pm.meta_value LIKE '%%%s%%' )
1097 ";
1098 $sql_params[] = 's:8:"sitewide";s:3:"yes";';
1099 $sql_params[] = 's:10:"other_page";s:3:"yes";';
1100 $sql_params[] = 's:7:"page_id";a:';
1101 $sql_params[] = serialize( (string) $pid );
1102 //local url
1103 $sql .= " OR ( pm.meta_value LIKE '%%%s%%' )";
1104 $sql_params[] = 's:9:"local_url";s:3:"yes";';
1105 if(!empty($_REQUEST['cache_compatibility']) && $_REQUEST['cache_compatibility'] == 'yes'){
1106 $is_home = (!empty($_REQUEST['is_home']) && $_REQUEST['is_home'] === 'true') ? true : false ;
1107 }else{
1108 $is_home = (is_home() || is_front_page()) ? true : false;
1109 }
1110 if ($is_home === true ) {
1111 $sql .= " OR ( pm.meta_value LIKE '%%%s%%' )";
1112 $sql_params[] = 's:8:"homepage";s:3:"yes";';
1113 }
1114 $sql .=" ) )";
1115
1116 $sql_params[0] .= $sql;
1117 //s:9:"logged_in";s:3:"all";
1118
1119 return $sql_params;
1120 }
1121
1122 // Include all classes required for Icegram plugin
1123 function include_classes() {
1124
1125 $classes = glob( $this->plugin_path . '/classes/*.php' );
1126 foreach ( $classes as $file ) {
1127 // Files with 'admin' in their name are included only for admin section
1128 if ( is_file( $file ) && ( (strpos($file, '-admin') >= 0 && is_admin()) || (strpos($file, '-admin') === false) ) ) {
1129 include_once $file;
1130 }
1131 }
1132
1133 // Load built in message types
1134 $icegram_message_type_basedirs = glob( $this->plugin_path . '/message-types/*' );
1135 // Allow other plugins to add new message types
1136 $icegram_message_type_basedirs = apply_filters( 'icegram_message_type_basedirs', $icegram_message_type_basedirs );
1137 // Set up different message type classes
1138 foreach ( $icegram_message_type_basedirs as $dir ) {
1139 $type = basename ( $dir );
1140 $class_file = $dir . "/main.php";
1141 if( is_file( $class_file ) ) {
1142 include_once( $class_file );
1143 }
1144 $class_name = 'Icegram_Message_Type_' . str_replace(' ', '_', ucwords(str_replace('-', ' ', $type)));
1145 if (class_exists($class_name)) {
1146 $this->message_type_objs[ $type ] = new $class_name();
1147 }
1148 }
1149 $this->message_types = apply_filters( 'icegram_message_types', array() );
1150
1151 }
1152
1153 // Register Campaign post type
1154 function register_campaign_post_type() {
1155 $labels = array(
1156 'name' => __( 'Campaigns', 'icegram' ),
1157 'singular_name' => __( 'Campaign', 'icegram' ),
1158 'add_new' => __( 'Add New Campaign', 'icegram' ),
1159 'add_new_item' => __( 'Add New Campaign', 'icegram' ),
1160 'edit_item' => __( 'Edit Campaign', 'icegram' ),
1161 'new_item' => __( 'New Campaign', 'icegram' ),
1162 'all_items' => __( 'Campaigns', 'icegram' ),
1163 'view_item' => __( 'View Campaign', 'icegram' ),
1164 'search_items' => __( 'Search Campaigns', 'icegram' ),
1165 'not_found' => __( 'No campaigns found', 'icegram' ),
1166 'not_found_in_trash' => __( 'No campaigns found in Trash', 'icegram' ),
1167 'parent_item_colon' => __( '', 'icegram' ),
1168 'menu_name' => __( 'Icegram', 'icegram' )
1169 );
1170
1171 $args = array(
1172 'labels' => $labels,
1173 'menu_icon' => 'dashicons-info',
1174 'public' => false,
1175 'publicly_queryable' => false,
1176 'show_ui' => true,
1177 'show_in_menu' => true,
1178 'query_var' => true,
1179 'rewrite' => array( 'slug' => 'ig_campaign' ),
1180 'capability_type' => 'post',
1181 'has_archive' => false,
1182 'hierarchical' => false,
1183 'menu_position' => null,
1184 'menu_icon' => $this->plugin_url . '/assets/images/icegram-logo-branding-18-white.png' ,
1185 'supports' => array( 'title', 'editor' )
1186 );
1187
1188 register_post_type( 'ig_campaign', $args );
1189 }
1190
1191 // Register Message post type
1192 function register_message_post_type() {
1193 $labels = array(
1194 'name' => __( 'Messages', 'icegram' ),
1195 'singular_name' => __( 'Message', 'icegram' ),
1196 'add_new' => __( 'Create New', 'icegram' ),
1197 'add_new_item' => __( 'Create New Message', 'icegram' ),
1198 'edit_item' => __( 'Edit Message', 'icegram' ),
1199 'new_item' => __( 'New Message', 'icegram' ),
1200 'all_items' => __( 'Messages', 'icegram' ),
1201 'view_item' => __( 'View Message', 'icegram' ),
1202 'search_items' => __( 'Search Messages', 'icegram' ),
1203 'not_found' => __( 'No messages found', 'icegram' ),
1204 'not_found_in_trash' => __( 'No messages found in Trash', 'icegram' ),
1205 'parent_item_colon' => __( '', 'icegram' ),
1206 'menu_name' => __( 'Messages', 'icegram' )
1207 );
1208
1209 $args = array(
1210 'labels' => $labels,
1211 'public' => false,
1212 'publicly_queryable' => false,
1213 'show_ui' => true,
1214 'show_in_menu' => 'edit.php?post_type=ig_campaign',
1215 'query_var' => true,
1216 'rewrite' => array( 'slug' => 'ig_message' ),
1217 'capability_type' => 'post',
1218 'has_archive' => false,
1219 'hierarchical' => false,
1220 'menu_position' => null,
1221 'supports' => array( 'title' )
1222 );
1223
1224 register_post_type( 'ig_message', $args );
1225 }
1226
1227
1228 function import( $data = array() ) {
1229 if ( empty( $data['campaigns'] ) && empty( $data['messages'] ) ) return;
1230
1231 $default_theme = $default_type = '';
1232 $first_message_type = current( $this->message_types );
1233
1234 if( is_array( $first_message_type ) ) {
1235 $default_type = $first_message_type['type'];
1236 if( !empty( $first_message_type['themes'] ) ) {
1237 $default_theme = key( $first_message_type['themes'] );
1238 }
1239 }
1240
1241 $new_campaign_ids = array();
1242 foreach ( (array) $data['campaigns'] as $campaign ) {
1243
1244 $args = array(
1245 'post_content' => ( !empty( $campaign['post_content'] ) ) ? esc_attr( $campaign['post_content'] ) : '',
1246 'post_name' => ( !empty( $campaign['post_title'] ) ) ? sanitize_title( $campaign['post_title'] ) : '',
1247 'post_title' => ( !empty( $campaign['post_title'] ) ) ? $campaign['post_title'] : '',
1248 'post_status' => ( !empty( $campaign['post_status'] ) ) ? $campaign['post_status'] : 'draft',
1249 'post_type' => 'ig_campaign'
1250 );
1251
1252 $new_campaign_id = wp_insert_post( $args );
1253 $new_campaign_ids[] = $new_campaign_id;
1254
1255 if ( !empty( $campaign['target_rules'] ) ) {
1256
1257 $defaults = array (
1258 'homepage' => 'yes',
1259 'when' => 'always',
1260 'from' => '',
1261 'to' => '',
1262 'mobile' => 'yes',
1263 'tablet' => 'yes',
1264 'laptop' => 'yes',
1265 'logged_in' => 'all'
1266 );
1267
1268 $target_rules = wp_parse_args( $campaign['target_rules'], $defaults );
1269 update_post_meta( $new_campaign_id, 'icegram_campaign_target_rules', $target_rules );
1270 }
1271
1272 if ( !empty( $campaign['messages'] ) ) {
1273
1274 $messages = array();
1275
1276 foreach ( $campaign['messages'] as $message ) {
1277
1278 if ( !is_array( $message ) ) continue;
1279
1280 $args = array(
1281 'post_content' => ( !empty( $message['message'] ) ) ? esc_attr( $message['message'] ) : '',
1282 'post_name' => ( !empty( $message['post_title'] ) ) ? sanitize_title( $message['post_title'] ) : '',
1283 'post_title' => ( !empty( $message['post_title'] ) ) ? $message['post_title'] : '',
1284 'post_status' => ( !empty( $message['post_status'] ) ) ? $message['post_status'] : 'publish',
1285 'post_type' => 'ig_message'
1286 );
1287
1288 $new_message_id = wp_insert_post( $args );
1289 $new_message = array(
1290 'id' => $new_message_id,
1291 'time' => ( !empty( $message['time'] ) ) ? $message['time'] : 0
1292 );
1293 //for gallery + CTA another message
1294 if(!empty($message['cta']) && $message['cta'] == 'cta_another_message' && !empty($message['cta_linked_message_id']) && $message['cta_linked_message_id'] == 'auto'){
1295 $prev_message = end($messages);
1296 $message['cta_linked_message_id'] = $prev_message['id'];
1297 array_pop($messages);
1298 }
1299 $messages[] = $new_message;
1300
1301 unset( $message['post_content'] );
1302 unset( $message['time'] );
1303
1304 $message['id'] = $new_message_id;
1305
1306 $defaults = array (
1307 'post_title' => '',
1308 'type' => $default_type,
1309 'theme' => $default_theme,
1310 'animation' => '',
1311 'headline' => '',
1312 'label' => '',
1313 'link' => '',
1314 'icon' => '',
1315 'message' => '',
1316 'position' => '',
1317 'text_color' => '#000000',
1318 'bg_color' => '#ffffff',
1319 'custom_code' => '',
1320 'id' => ''
1321 );
1322 $icegram_message_data = wp_parse_args( $message, $defaults );
1323 if ( !empty( $icegram_message_data ) ) {
1324 update_post_meta( $new_message_id, 'icegram_message_data', $icegram_message_data );
1325 update_post_meta( $new_message_id, 'icegram_message_preview_data', $icegram_message_data );
1326 }
1327 }//foreach
1328
1329 if ( !empty( $campaign['messages'] ) ) {
1330 update_post_meta( $new_campaign_id, 'messages', $messages );
1331 update_post_meta( $new_campaign_id, 'campaign_preview', $messages );
1332 }
1333 }//if
1334 }
1335 return $new_campaign_ids;
1336
1337 }
1338
1339 function import_sample_data ($data = array() ){
1340 $new_campaign_ids = $this->import($data);
1341 if( !empty( $new_campaign_ids ) ) {
1342 update_option( 'icegram_sample_data_imported', $new_campaign_ids );
1343 }
1344 }
1345
1346 function import_gallery_item(){
1347 if(!empty($_REQUEST['action']) && $_REQUEST['action'] == 'fetch_messages' && !empty($_REQUEST['campaign_id']) && !empty($_REQUEST['gallery_item'])){
1348 $params = $_REQUEST;
1349 $imported_gallery_items = array();
1350 // $url = 'http://192.168.0.112/~sandhya/icegram-andrea/wp-admin/admin-ajax.php';
1351 $url = 'http://www.icegram.com/gallery/wp-admin/admin-ajax.php';
1352 $options = array(
1353 'timeout' => 15,
1354 'method' => 'POST',
1355 'body' => http_build_query( $params )
1356 );
1357 $response = wp_remote_request( $url, $options );
1358 $response_code = wp_remote_retrieve_response_code( $response );
1359 if ( $response_code == 200 ) {
1360 $new_campaign_ids = $this->import(json_decode($response['body'] ,true));
1361 if(!empty($new_campaign_ids)){
1362 $imported_gallery_items = get_option('ig_imported_gallery_items');
1363 $imported_gallery_items[] = $_REQUEST['gallery_item'];
1364 update_option( 'ig_imported_gallery_items', $imported_gallery_items );
1365 $location = admin_url( 'post.php?post='.$new_campaign_ids[0].'&action=edit');
1366 header('Location:'.$location);
1367 exit;
1368 }else{
1369 wp_safe_redirect($_SERVER['HTTP_REFERER']);
1370 }
1371 }
1372 }
1373 }
1374 function get_sample_data() {
1375
1376 return array(
1377 'campaigns' => array(
1378 array(
1379 'post_name' => '',
1380 'post_title' => 'My First Icegram Campaign',
1381 'target_rules' => array (
1382 'homepage' => 'yes',
1383 'when' => 'always',
1384 'from' => '',
1385 'to' => '',
1386 'mobile' => 'yes',
1387 'tablet' => 'yes',
1388 'laptop' => 'yes',
1389 'logged_in' => 'all'
1390 ),
1391 'messages' => array(
1392 array (
1393 'post_title' => 'Get 2x more Contacts with Your Website',
1394 'post_status' => 'publish',
1395 'time' => '0',
1396 'type' => 'action-bar',
1397 'theme' => 'hello',
1398 'headline' => 'Get 2x more Contacts with Your Website',
1399 'label' => 'Show Me How',
1400 'link' => '',
1401 'icon' => '',
1402 'message' => 'Instant Results Guaranteed',
1403 'position' => '01',
1404 'text_color' => '#000000',
1405 'bg_color' => '#eb593c'
1406 ),
1407 array (
1408 'post_title' => '20% Off Coupon',
1409 'post_status' => 'publish',
1410 'time' => '4',
1411 'type' => 'messenger',
1412 'theme' => 'social',
1413 'animation' => 'slide',
1414 'headline' => '20% Off - for you',
1415 'label' => '',
1416 'link' => '',
1417 'icon' => '',
1418 'message' => "Hey there! We are running a <strong>special 20% off this week</strong> for registered users - like you.
1419
1420 Use coupon <code>LOYALTY20</code> during checkout.",
1421 'position' => '22',
1422 'text_color' => '#000000',
1423 'bg_color' => '#ffffff'
1424 ),
1425 array (
1426 'post_title' => 'How this blog makes over $34,800 / month for FREE.',
1427 'post_status' => 'publish',
1428 'time' => '10',
1429 'type' => 'popup',
1430 'theme' => 'air-mail',
1431 'headline' => 'How this blog makes over $34,800 / month for FREE.',
1432 'label' => 'FREE INSTANT ACCESS',
1433 'link' => '',
1434 'icon' => '',
1435 '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.
1436
1437 <strong>Download my free kit to learn how I do this.</strong>
1438
1439 <ul>
1440 <li>How to choose blog topics that create long term value</li>
1441 <li>The type of blog post that will make your site go viral</li>
1442 <li>How to free yourself from the routine tasks</li>
1443 <li>Resources and tips to get started quickly</li>
1444 <li>Private members club to connect with fellow owners</li>
1445 </ul>",
1446 'text_color' => '#000000',
1447 'bg_color' => '#ffffff'
1448
1449 ),
1450 array (
1451 'post_title' => 'Exclusive Marketing Report',
1452 'post_status' => 'publish',
1453 'time' => '6',
1454 'type' => 'toast',
1455 'theme' => 'stand-out',
1456 'animation' => 'pop',
1457 'headline' => 'Exclusive Marketing Report',
1458 'label' => '',
1459 'link' => '',
1460 'icon' => '',
1461 'message' => 'FREE for every subscriber. Click here to download it.',
1462 'position' => '02',
1463 'text_color' => '#000000',
1464 'bg_color' => '#ffffff'
1465 )
1466
1467 )
1468 )
1469 )
1470 );
1471 }
1472
1473 function remove_preview_button() {
1474 global $post_type;
1475 if( $post_type == 'ig_message' || $post_type == 'ig_campaign' ) {
1476 ?>
1477 <style type="text/css">
1478 #message.updated.below-h2{ display: none; }
1479 #preview-action { display:none; }
1480 </style>
1481 <?php
1482 }
1483 }
1484
1485
1486 function remove_row_actions( $actions, $post ) {
1487
1488 if ( empty( $post->post_type ) || ( $post->post_type != 'ig_campaign' && $post->post_type != 'ig_message' ) ) return $actions;
1489
1490 unset( $actions['inline hide-if-no-js'] );
1491 unset( $actions['view'] );
1492
1493 return $actions;
1494
1495 }
1496
1497 function identify_current_page() {
1498 global $post, $wpdb;
1499
1500 $obj = get_queried_object();
1501 $id = 0;
1502 if( !empty( $obj->has_archive ) ) {
1503 $id = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_name = %s AND post_type='page'", $obj->has_archive ) );
1504 } elseif( is_object( $post ) && isset( $post->ID ) ) {
1505 $id = $post->ID;
1506 }
1507 $id = apply_filters('icegram_identify_current_page', $id );
1508 self::$current_page_id = $id;
1509 }
1510
1511 static function get_current_page_id() {
1512 global $post;
1513 if (!empty($_REQUEST['page_id']) && is_numeric($_REQUEST['page_id'])) {
1514 $post = get_post($_REQUEST['page_id']);
1515 setup_postdata( $post );
1516 // WPML check
1517 $id = apply_filters('icegram_identify_current_page', $post->ID );
1518 self::$current_page_id = $id;
1519 }
1520 return self::$current_page_id;
1521 }
1522 static function get_current_page_url() {
1523 if(!empty($_REQUEST['cache_compatibility']) && $_REQUEST['cache_compatibility'] == 'yes'){
1524 $pageURL = (!empty($_REQUEST['referral_url'])) ? $_REQUEST['referral_url'] : '';
1525 }else{
1526 $pageURL = 'http';
1527 if( isset($_SERVER["HTTPS"]) ) {
1528 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
1529 }
1530 $pageURL .= "://";
1531 if ($_SERVER["SERVER_PORT"] != "80") {
1532 $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
1533 } else {
1534 $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
1535 }
1536 }
1537 return $pageURL;
1538 }
1539
1540 function wpml_get_parent_id( $id ) {
1541 global $post;
1542 if (function_exists('icl_object_id') && function_exists('icl_get_default_language') ) {
1543 $id = icl_object_id($id, $post->post_type, true, icl_get_default_language() );
1544 }
1545 return $id;
1546 }
1547
1548
1549 /**
1550 * Our implementation of wpautop to preserve script and style tags
1551 */
1552 function before_wpautop($pee) {
1553 if ( trim($pee) === '' ) {
1554 $this->_wpautop_tags = array();
1555 return '';
1556 }
1557
1558 $tags = array();
1559 // Pull out tags and add placeholders
1560 list( $pee, $tags['pre'] ) = $this->_wpautop_add_tag_placeholders( $pee, 'pre' );
1561 list( $pee, $tags['script'] ) = $this->_wpautop_add_tag_placeholders( $pee, 'script' );
1562 list( $pee, $tags['style'] ) = $this->_wpautop_add_tag_placeholders( $pee, 'style' );
1563 $this->_wpautop_tags = $tags;
1564
1565 if( !empty( $pre_tags ) )
1566 $pee = $this->_wpautop_replace_tag_placeholders( $pee, $pre_tags );
1567 if( !empty( $script_tags ) )
1568 $pee = $this->_wpautop_replace_tag_placeholders( $pee, $script_tags );
1569 if( !empty( $style_tags ) )
1570 $pee = $this->_wpautop_replace_tag_placeholders( $pee, $style_tags );
1571
1572 return $pee;
1573 }
1574
1575 function after_wpautop($pee) {
1576 if ( trim($pee) === '' || empty($this->_wpautop_tags) )
1577 return '';
1578
1579 // Replace placeholders with original content
1580 if (!empty($this->_wpautop_tags['pre'])) {
1581 $pee = $this->_wpautop_replace_tag_placeholders( $pee, $this->_wpautop_tags['pre'] );
1582 }
1583 if (!empty($this->_wpautop_tags['script'])) {
1584 $pee = $this->_wpautop_replace_tag_placeholders( $pee, $this->_wpautop_tags['script'] );
1585 }
1586 if (!empty($this->_wpautop_tags['style'])) {
1587 $pee = $this->_wpautop_replace_tag_placeholders( $pee, $this->_wpautop_tags['style'] );
1588 }
1589
1590 $this->_wpautop_tags = array();
1591
1592 return $pee;
1593 }
1594
1595 function _wpautop_add_tag_placeholders( $pee, $tag ) {
1596 $tags = array();
1597
1598 if ( false !== strpos( $pee, "<{$tag}" ) ) {
1599 $pee_parts = explode( "</{$tag}>", $pee );
1600 $last_pee = array_pop( $pee_parts );
1601 $pee = '';
1602 $i = 0;
1603
1604 foreach ( $pee_parts as $pee_part ) {
1605 $start = strpos( $pee_part, "<{$tag}" );
1606
1607 // Malformed html?
1608 if ( false === $start ) {
1609 $pee .= $pee_part;
1610 continue;
1611 }
1612
1613 $name = "<{$tag} wp-{$tag}-tag-$i></{$tag}>";
1614 $tags[ $name ] = substr( $pee_part, $start ) . "</{$tag}>";
1615
1616 $pee .= substr( $pee_part, 0, $start ) . $name;
1617 $i++;
1618 }
1619
1620 $pee .= $last_pee;
1621 }
1622
1623 return array( $pee, $tags );
1624 }
1625
1626 function _wpautop_replace_tag_placeholders( $pee, $tags ) {
1627 if ( ! empty( $tags ) ) {
1628 $pee = str_replace( array_keys( $tags ), array_values( $tags ), $pee );
1629 }
1630
1631 return $pee;
1632 }
1633
1634 static function duplicate_in_db( $original_id){
1635 // Get access to the database
1636 global $wpdb;
1637 // Get the post as an array
1638 $duplicate = get_post( $original_id, 'ARRAY_A' );
1639 // Modify some of the elements
1640 $duplicate['post_title'] = $duplicate['post_title'].' '.__('Copy', 'icegram');
1641 $duplicate['post_status'] = 'draft';
1642 // Set the post date
1643 $timestamp = current_time('timestamp', 0);
1644
1645 $duplicate['post_date'] = date('Y-m-d H:i:s', $timestamp);
1646
1647 // Remove some of the keys
1648 unset( $duplicate['ID'] );
1649 unset( $duplicate['guid'] );
1650 unset( $duplicate['comment_count'] );
1651
1652 // Insert the post into the database
1653 $duplicate_id = wp_insert_post( $duplicate );
1654
1655 // Duplicate all taxonomies/terms
1656 $taxonomies = get_object_taxonomies( $duplicate['post_type'] );
1657
1658 foreach( $taxonomies as $taxonomy ) {
1659 $terms = wp_get_post_terms( $original_id, $taxonomy, array('fields' => 'names') );
1660 wp_set_object_terms( $duplicate_id, $terms, $taxonomy );
1661 }
1662
1663 // Duplicate all custom fields
1664 $custom_fields = get_post_custom( $original_id );
1665 foreach ( $custom_fields as $key => $value ) {
1666 if($key === 'messages'){
1667 $messages = unserialize($value[0]);
1668 foreach ($messages as &$message) {
1669 $clone_msg_id = Icegram::duplicate_in_db($message['id']);
1670 $message['id'] = $clone_msg_id;
1671 }
1672 $value[0] = serialize($messages);
1673 }
1674 add_post_meta( $duplicate_id, $key, maybe_unserialize($value[0]) );
1675 }
1676 return $duplicate_id;
1677 }
1678
1679 static function duplicate( $original_id){
1680 $duplicate_id = Icegram::duplicate_in_db($original_id);
1681 $location = admin_url( 'post.php?post='.$duplicate_id.'&action=edit');
1682 header('Location:'.$location);
1683 exit;
1684 }
1685
1686 public static function form_submission_validate_request($request_data){
1687 if(!empty($request_data)){
1688 // Check for Remote Rainmaker form submission request
1689 $request_data['is_remote'] = false;
1690 if(!empty($request_data['ig_mode']) && $request_data['ig_mode'] === 'remote'){
1691 $ig_remote_url = $request_data['ig_remote_url'];
1692 if(!empty($request_data['ig_campaign_id'])){
1693 $rules = get_post_meta( $request_data['ig_campaign_id'], 'icegram_campaign_target_rules', true );
1694 if( !empty($rules['remote_urls']) && is_array($rules['remote_urls']) ){
1695 foreach ($rules['remote_urls'] as $remote_url_pattern) {
1696 $valid = Icegram_Campaign::is_valid_url($remote_url_pattern , $ig_remote_url);
1697 if( $valid ){
1698 $request_data['is_remote'] = true;
1699 break;
1700 }
1701 }
1702 //TODO :: discard the the remote request and data
1703 // if($request_data['is_remote'] == false){
1704 // return array();
1705 // }
1706 }
1707
1708 }
1709 }
1710 }
1711 return $request_data;
1712 }
1713 }
1714
1715
1716 function initialize_icegram() {
1717 global $icegram;
1718
1719 // i18n / l10n - load translations
1720 load_plugin_textdomain( 'icegram', false, dirname( plugin_basename( __FILE__ ) ) . '/lang/' );
1721
1722 $icegram = new Icegram();
1723 do_action('icegram_loaded');
1724 }
1725
1726 add_action( 'plugins_loaded', 'initialize_icegram' );
1727 register_activation_hook( __FILE__, array( 'Icegram', 'install' ) );