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

1,729 lines 83.6 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: https://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.10.20
7 * Author: icegram
8 * Author URI: https://www.icegram.com/
9 * Copyright (c) 2014-16 Icegram
10 * License: GPLv3
11 * License URI: http://www.gnu.org/licenses/gpl-3.0.html
12 *
13 * Text Domain: icegram
14 * Domain Path: /lang/
15 */
16
17 /**
18 * Main class Icegram
19 */
20 class Icegram {
21
22 var $plugin_url;
23 var $plugin_path;
24 var $version;
25 var $_wpautop_tags;
26 var $message_types;
27 var $message_type_objs;
28 var $shortcode_instances;
29 var $available_headlines;
30 var $mode;
31 var $cache_compatibility;
32
33 public static $current_page_id;
34
35 function __construct() {
36 $this->version = "1.10.20";
37 $this->shortcode_instances = array();
38 $this->mode = 'local';
39 $this->plugin_url = untrailingslashit( plugins_url( '/', __FILE__ ) );
40 $this->plugin_path = untrailingslashit( plugin_dir_path( __FILE__ ) );
41 $this->include_classes();
42 $this->cache_compatibility = get_option('icegram_cache_compatibility', 'no');
43
44 if( is_admin() && current_user_can( 'edit_posts' ) ) {
45 $ig_campaign_admin = Icegram_Campaign_Admin::getInstance();
46 $ig_message_admin = Icegram_Message_Admin::getInstance();
47 add_action( 'admin_enqueue_scripts', array( &$this, 'enqueue_admin_styles_and_scripts' ) );
48 add_action( 'admin_print_styles', array( &$this, 'remove_preview_button' ) );
49 add_filter( 'post_row_actions', array( &$this , 'remove_row_actions' ), 10, 2 );
50
51 add_action( 'admin_menu', array( &$this, 'admin_menus') );
52 add_action( 'admin_init', array( &$this, 'welcome' ) );
53 add_action( 'admin_init', array( &$this, 'dismiss_admin_notice' ) );
54
55 add_action( 'admin_init', array( &$this, 'import_gallery_item' ) );
56
57 add_action( 'icegram_settings_after', array( &$this, 'klawoo_subscribe_form' ) );
58 add_action( 'icegram_about_changelog', array( &$this, 'klawoo_subscribe_form' ) );
59 add_action( 'icegram_settings_after', array( &$this, 'icegram_houskeeping' ) );
60 add_action( 'admin_notices', array( &$this,'add_admin_notices'));
61 add_filter( 'plugin_action_links', array($this, 'ig_plugin_settings_link'), 10, 2);
62 } else {
63 add_action( 'wp_footer', array( &$this, 'icegram_load_data' ));
64 }
65 if($this->cache_compatibility === 'no'){
66 add_action( 'wp_footer', array( &$this, 'display_messages' ) );
67 }
68 add_shortcode( 'icegram', array( &$this, 'execute_shortcode' ) );
69 add_shortcode( 'ig_form', array( &$this, 'execute_form_shortcode' ) );
70 // WPML compatibility
71 add_filter( 'icegram_identify_current_page', array( &$this, 'wpml_get_parent_id' ), 10 );
72
73 add_filter( 'icegram_branding_data', array( &$this , 'branding_data_remove' ), 10 );
74 add_action( 'wp_enqueue_scripts', array( &$this, 'identify_current_page' ) );
75 add_filter( 'icegram_get_valid_campaigns_sql', array( &$this , 'append_to_valid_campaigns_sql' ), 10, 2 );
76 add_action( 'icegram_print_js_css_data', array( &$this, 'print_js_css_data' ), 10, 1);
77 // common
78 add_action( 'init', array( &$this, 'register_campaign_post_type' ) );
79 add_action( 'init', array( &$this, 'register_message_post_type' ) );
80
81 add_action( 'icegram_loaded', array( &$this, 'load_compat_classes') );
82
83 // execute shortcode in sidebar
84 add_filter( 'widget_text', array(&$this, 'ig_widget_text_filter') );
85
86 add_filter( 'rainmaker_validate_request', array(&$this,'form_submission_validate_request'), 10, 2);
87 add_filter( 'icegram_data', array( $this, 'two_step_mobile_popup' ), 100, 1);
88
89 add_action ( 'wp_ajax_ig_submit_survey', array(&$this,'ig_submit_survey' ));
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 $settings_link = '<a href="edit.php?post_type=ig_campaign&page=icegram-settings">'.__('Settings', 'icegram').'</a>';
107 $support_link = '<a href="edit.php?post_type=ig_campaign&page=icegram-support">'.__('Support', 'icegram').'</a>';
108 array_unshift($links, $support_link);
109 array_unshift($links, $settings_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 $timezone_format = _x('Y-m-d', 'timezone date format');
154 $ig_current_date = strtotime(date_i18n($timezone_format));
155 $ig_offer_start = strtotime("2017-12-8");
156 $ig_offer_end = strtotime("2017-12-26");
157 if(($ig_current_date >= $ig_offer_start) && ($ig_current_date <= $ig_offer_end)) {
158 include_once('ig-offer.php');
159 }
160 }
161 public function dismiss_admin_notice(){
162 if(isset($_GET['ig_dismiss_admin_notice']) && $_GET['ig_dismiss_admin_notice'] == '1' && isset($_GET['ig_option_name'])){
163 $option_name = sanitize_text_field($_GET['ig_option_name']);
164 update_option($option_name.'_icegram', true);
165 header("Location: https://www.icegram.com/?utm_source=in_app&utm_medium=ig_banner&utm_campaign=christmas2017_30");
166 exit();
167 }
168 }
169
170 public function klawoo_subscribe_form() {
171 ?>
172 <div class="wrap">
173 <?php
174 if ( stripos(get_current_screen()->base, 'settings') !== false ) {
175 echo "<h2>".__( 'Free Add-ons, Proven Marketing Tricks and Updates', 'icegram' )."</h2>";
176 }
177 ?>
178 <table class="form-table">
179 <tr>
180 <th scope="row"><?php _e( 'Get add-ons and tips...', 'icegram' ) ?></th>
181 <td>
182 <form name="klawoo_subscribe" action="#" method="POST" accept-charset="utf-8">
183 <input class="ltr" type="text" name="name" id="name" placeholder="Name"/>
184 <input class="regular-text ltr" type="text" name="email" id="email" placeholder="Email"/>
185 <input type="hidden" name="list" value="7I763v6Ldrs3YhJeee5EOgFA"/>
186 <input type="submit" name="submit" id="submit" class="button button-primary" value="Subscribe">
187 <br><br>
188 <input type="checkbox" name="es-gdpr-agree" id ="es-gdpr-agree" value="1" required="required">
189 <label for="es-gdpr-agree"><?php echo sprintf(__( 'I have read and agreed to our %s.', 'icegram' ), '<a href="https://www.icegram.com/privacy-policy/" target="_blank">' . __( 'Privacy Policy', 'icegram' ) . '</a>' ); ?></label>
190 <br>
191 </form>
192 <div id="klawoo_response"></div>
193 </td>
194 </tr>
195 </table>
196 </div>
197 <script type="text/javascript">
198 jQuery(function () {
199 jQuery("form[name=klawoo_subscribe]").submit(function (e) {
200 e.preventDefault();
201
202 jQuery('#klawoo_response').html('');
203 params = jQuery("form[name=klawoo_subscribe]").serializeArray();
204 params.push( {name: 'action', value: 'klawoo_subscribe' });
205
206 jQuery.ajax({
207 method: 'POST',
208 type: 'text',
209 url: "<?php echo admin_url( 'admin-ajax.php' ); ?>",
210 data: params,
211 success: function(response) {
212 if (response != '') {
213 jQuery('#klawoo_response').html(response);
214 } else {
215 jQuery('#klawoo_response').html('error!');
216 }
217 }
218 });
219 });
220 });
221 </script>
222 <?php
223 }
224
225
226 public function klawoo_subscribe() {
227 $url = 'http://app.klawoo.com/subscribe';
228
229 if( !empty( $_POST ) ) {
230 $params = $_POST;
231 } else {
232 exit();
233 }
234 $method = 'POST';
235 $qs = http_build_query( $params );
236
237 $options = array(
238 'timeout' => 15,
239 'method' => $method
240 );
241
242 if ( $method == 'POST' ) {
243 $options['body'] = $qs;
244 } else {
245 if ( strpos( $url, '?' ) !== false ) {
246 $url .= '&'.$qs;
247 } else {
248 $url .= '?'.$qs;
249 }
250 }
251
252 $response = wp_remote_request( $url, $options );
253 if ( wp_remote_retrieve_response_code( $response ) == 200 ) {
254 $data = $response['body'];
255 if ( $data != 'error' ) {
256
257 $message_start = substr( $data, strpos( $data,'<body>' ) + 6 );
258 $remove = substr( $message_start, strpos( $message_start,'</body>' ) );
259 $message = trim( str_replace( $remove, '', $message_start ) );
260 echo ( $message );
261 exit();
262 }
263 }
264 exit();
265 }
266
267 public function icegram_houskeeping(){
268 ?>
269 <div class="wrap">
270 <?php
271 if ( stripos(get_current_screen()->base, 'settings') !== false ) {
272 ?>
273 <form name="icegram_housekeeping" action="#" method="POST" accept-charset="utf-8">
274 <h2><?php _e( 'Housekeeping', 'icegram' ) ?></h2>
275 <p class="ig_housekeeping">
276 <label for="icegram_remove_shortcodes">
277 <input type="checkbox" name="icegram_remove_shortcodes" value="yes" />
278 <?php _e( 'Remove all Icegram shortcodes', 'icegram' ); ?>
279 </label>
280 <br/><br/>
281 <label for="icegram_remove_all_data">
282 <input type="checkbox" name="icegram_remove_all_data" value="yes" />
283 <?php _e( 'Remove all Icegram campaigns and messages', 'icegram' ); ?>
284 </label>
285 <br/><br/>
286 <img alt="" src="<?php echo admin_url( 'images/wpspin_light.gif' ) ?>" class="ig_loader" style="vertical-align:middle;display:none" />
287 <input type="submit" name="submit" id="submit" class="button button-primary" value="<?php _e( 'Clean Up', 'icegram' ); ?>">
288 <div id="icegram_housekeeping_response"></div>
289 </p>
290 </form>
291
292 </div>
293 <script type="text/javascript">
294 jQuery(function () {
295 jQuery("form[name=icegram_housekeeping]").submit(function (e) {
296 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){
297 e.preventDefault();
298 jQuery('.ig_loader').show();
299 jQuery('#icegram_housekeeping_response').text("");
300 params = jQuery("form[name=icegram_housekeeping]").serializeArray();
301 params.push( {name: 'action', value: 'icegram_run_housekeeping' });
302 params.push( {name: 'security', value: '<?php echo wp_create_nonce('ig_run_housekeeping'); ?> '});
303
304 jQuery.ajax({
305 method: 'POST',
306 type: 'text',
307 url: "<?php echo admin_url( 'admin-ajax.php' ); ?>",
308 data: params,
309 success: function(response) {
310 jQuery('.ig_loader').hide();
311 jQuery('#icegram_housekeeping_response').text("<?php _e('Done!', 'icegram'); ?>");
312 }
313 });
314 }
315 });
316 });
317 </script>
318 <?php
319 }
320 }
321 public function run_housekeeping() {
322 check_ajax_referer('ig_run_housekeeping', 'security');
323 global $wpdb, $current_user;
324 $params = $_POST;
325 $_POST = array();
326 if(current_user_can( 'manage_options' ) && !empty($params['icegram_remove_shortcodes']) && $params['icegram_remove_shortcodes'] == 'yes') {
327 // first get all posts with [icegram] shortcode in them
328 $sql = "SELECT * FROM `$wpdb->posts` WHERE `post_content` LIKE '%[icegram %]%' and `post_type` != 'revision' ";
329 $posts = $wpdb->get_results($sql, OBJECT);
330 if ( !empty($posts) && is_array($posts) ) {
331 foreach ($posts as $post) {
332 $post_content = $post->post_content;
333 // remove shortcode with regexp now
334 $re = "/\\[icegram(.)*\\]/i";
335 $post_content = preg_replace($re, '', $post_content);
336 // save post content back
337 if ($post_content && $post_content != $post->post_content) {
338 wp_update_post( array ( 'ID' => $post->ID,
339 'post_content' => $post_content
340 ) );
341 }
342 }
343 }
344 }
345
346 if(!empty($params['icegram_remove_all_data']) && $params['icegram_remove_all_data'] == 'yes') {
347 $posts = get_posts( array( 'post_type' => array( 'ig_campaign', 'ig_message' ) ) );
348 if ( !empty($posts) && is_array($posts) ) {
349 foreach ($posts as $post) {
350 wp_delete_post( $post->ID, true);
351 }
352 }
353 do_action('icegram_remove_all_data');
354 }
355 $_POST = $params;
356 }
357
358 public function icegram_event_track() {
359 if( !empty($_POST['ig_local_url_cs']) && isset($_SERVER['HTTP_ORIGIN']) ){
360 $parts = parse_url($_POST['ig_local_url_cs']);
361 $base_url = $parts["scheme"] . "://" . $parts["host"];
362 header('Access-Control-Allow-Origin: '.$base_url);
363 header('Access-Control-Allow-Credentials: true');
364 }
365
366 if( !empty( $_POST['event_data'] ) ) {
367 foreach ( $_POST['event_data'] as $event ) {
368 switch ($event['type']) {
369 case 'shown':
370 if (is_array($event['params']) && !empty($event['params']['message_id'])) {
371 $messages_shown[] = $event['params']['message_id'];
372 if(!empty($event['params']['expiry_time'])){
373 if($event['params']['expiry_time'] =='today'){
374 $event['params']['expiry_time'] = strtotime('+1 day', mktime(0, 0, 0));
375 }else if($event['params']['expiry_time'] == 'current_session'){
376 $event['params']['expiry_time'] = 0;
377 }else{
378 $event['params']['expiry_time'] = strtotime($event['params']['expiry_time']);
379 }
380
381 $event['default'] = true;
382 $event = apply_filters('icegram_check_event_track', $event);
383 if($event['default']){
384 setcookie('icegram_campaign_shown_'.floor($event['params']['campaign_id']),true , $event['params']['expiry_time'] , '/');
385 }
386 }
387 }
388 break;
389 case 'clicked':
390 if (is_array($event['params']) && !empty($event['params']['message_id'])) {
391 $messages_clicked[] = $event['params']['message_id'];
392 if(!empty($event['params']['expiry_time_clicked'])){
393 if($event['params']['expiry_time_clicked'] =='today'){
394 $event['params']['expiry_time_clicked'] = strtotime('+1 day', mktime(0, 0, 0));
395 }else if($event['params']['expiry_time_clicked'] == 'current_session'){
396 $event['params']['expiry_time_clicked'] = 0;
397 }else{
398 $event['params']['expiry_time_clicked'] = strtotime($event['params']['expiry_time_clicked']);
399 }
400 //setcookie('icegram_messages_clicked_'.$event['params']['message_id'],true , $event['params']['expiry_time_clicked'] , '/' );
401 setcookie('icegram_campaign_clicked_'.floor($event['params']['campaign_id']),true , $event['params']['expiry_time_clicked'] , '/' );
402 }
403 }
404 break;
405
406 default:
407 break;
408 }
409
410 // Emit event for other plugins to handle it
411 do_action('icegram_event_track', $event);
412 do_action('icegram_event_track_'.$event['type'], $event['params']);
413 }
414 }
415 exit();
416 }
417
418 static function install() {
419 // Redirect to welcome screen
420 delete_option( '_icegram_activation_redirect' );
421 add_option( '_icegram_activation_redirect', 'pending' );
422 }
423
424 public function welcome() {
425
426 $this->db_update();
427 // Bail if no activation redirect transient is set
428 if ( false === get_option( '_icegram_activation_redirect' ) )
429 return;
430
431 // Delete the redirect transient
432 delete_option( '_icegram_activation_redirect' );
433
434 wp_redirect( admin_url( 'edit.php?post_type=ig_campaign&page=icegram-support' ) );
435 exit;
436 }
437
438 function db_update() {
439 $current_db_version = get_option( 'icegram_db_version' );
440 if ( !$current_db_version || version_compare( $current_db_version, '1.2', '<' ) ) {
441 include( 'updates/icegram-update-1.2.php' );
442 }
443 }
444
445 public function admin_menus() {
446
447 $welcome_page_title = __( 'Welcome to Icegram', 'icegram' );
448 $gallery_page_title = '<span style="color:#f18500;font-weight:bolder;">' . __( 'Gallery', 'icegram' ).'<span>';
449 $gallery = add_submenu_page( 'edit.php?post_type=ig_campaign', $gallery_page_title, $gallery_page_title, 'manage_options', 'icegram-gallery', array( $this, 'gallery_screen' ) );
450 $settings_page_title = __( 'Settings', 'icegram' );
451 $upgrade_page_title = '<span style="color:#f18500;font-weight:bolder;">'.__( 'Upgrade', 'icegram' ) .'</span>';
452
453 $menu_title = __( 'Docs & Support', 'icegram' );
454 $about = add_submenu_page( 'edit.php?post_type=ig_campaign', $welcome_page_title, $menu_title, 'manage_options', 'icegram-support', array( $this, 'about_screen' ) );
455 $settings = add_submenu_page( 'edit.php?post_type=ig_campaign', $settings_page_title, $settings_page_title, 'manage_options', 'icegram-settings', array( $this, 'settings_screen' ) );
456 $upgrade = add_submenu_page( 'edit.php?post_type=ig_campaign', $upgrade_page_title, $upgrade_page_title, 'manage_options', 'icegram-upgrade', array( $this, 'upgrade_screen' ) );
457
458
459 add_action( 'admin_print_styles-'. $about, array( $this, 'admin_css' ) );
460 add_action( 'admin_print_styles-'. $settings, array( $this, 'admin_css' ) );
461 add_action( 'admin_print_styles-'. $upgrade, array( $this, 'admin_css' ) );
462
463 }
464
465 public function admin_css() {
466 wp_enqueue_style( 'icegram-activation', $this->plugin_url . '/assets/css/admin.min.css' );
467 }
468
469 public function about_screen() {
470
471 // Import data if not done already
472 if( false === get_option( 'icegram_sample_data_imported' ) ) {
473 $this->import_sample_data( $this->get_sample_data() );
474 }
475
476 include ( 'about-icegram.php' );
477 }
478
479 public function settings_screen() {
480 include ( 'settings.php' );
481 }
482
483 public function upgrade_screen() {
484 include ( 'addons.php' );
485 }
486
487 public function check_for_gallery_items( $force_update = false ) {
488 global $icegram;
489 if ( $force_update === true || false === ( $ig_last_gallery_item_update = get_transient( 'ig_last_gallery_item_update' ) ) ) {
490 $ig_gallery_json = wp_remote_get( 'https://www.icegram.com/gallery/wp-json/wp/v2/galleryitem?per_page=100&page=1', array( 'user-agent' => 'Icegram Addons' ) );
491 if ( ! is_wp_error( $ig_gallery_json ) ) {
492 $ig_gallery_json = ( wp_remote_retrieve_body( $ig_gallery_json ) );
493 if ( !empty($ig_gallery_json) ) {
494 update_option( 'ig_last_gallery_items', $ig_gallery_json );
495 }
496 }
497 $url = 'https://www.icegram.com/gallery/wp-json/wp/v2/custom_cat?filter[orderby]=parent&order=desc';
498 $options = array(
499 'timeout' => 15,
500 'method' => 'GET',
501 'body' => ''
502 );
503 $response = wp_remote_request( $url, $options );
504 $response_code = wp_remote_retrieve_response_code( $response );
505 // $body = json_decode($response['body'] ,true);
506 if ( $response_code == 200 ) {
507 $categories = json_decode($response['body'] ,true);
508
509 $cat_list = array();
510 foreach ($categories as $category) {
511 if($category['parent'] == 0 ){
512 $cat_list[$category['term_id']]['name'] = $category['name'] ;
513 $cat_list[$category['term_id']]['slug'] = $category['slug'] ;
514 }else{
515 $cat_list[$category['parent']]['list'][] = $category;
516 }
517 }
518 update_option( 'ig_cat_list', $cat_list );
519 }
520 set_transient( 'ig_last_gallery_item_update', current_time( 'timestamp') , 24 * HOUR_IN_SECONDS ); // 1 day
521 }
522 }
523
524 public static function gallery_screen() {
525 global $icegram;
526 //check for new gallery item
527 $ig_last_gallery_item_update = get_transient('ig_last_gallery_item_update' );
528 if(empty($ig_last_gallery_item_update)){
529 $icegram->check_for_gallery_items(true);
530 }
531 $ig_gallery_items = get_option('ig_last_gallery_items',true);
532 $cat_list = get_option('ig_cat_list',true);
533 include ( 'gallery.php' );
534 wp_register_script('ig_gallery_js', $icegram->plugin_url . '/assets/js/gallery.min.js', array ( 'jquery', 'backbone', 'wp-backbone', 'wp-a11y', 'wp-util' ), $icegram->version, true);
535 if( !wp_script_is( 'ig_gallery_js' ) ) {
536 wp_enqueue_script( 'ig_gallery_js' );
537 $imported_gallery_items = get_option('ig_imported_gallery_items',true);
538 $ig_plan = get_option('ig_engage_plan');
539 $ig_plan = (!empty($ig_plan)) ? (($ig_plan == 'plus') ? 1 :(($ig_plan == 'pro') ? 2 : (($ig_plan == 'max') ? 3 : 0))) : 0;
540 $ig_gallery_json =
541 wp_localize_script( 'ig_gallery_js', '_wpThemeSettings', array(
542 'themes' => json_decode($ig_gallery_items, true),
543 'settings' => array(
544 'canInstall' => ( ! is_multisite() && ( 'install_themes' ) ),
545 'isInstall' => true,
546 'installURI' => ( ! is_multisite() && ( 'install_themes' ) ) ? admin_url( 'theme-install.php' ) : null,
547 'confirmDelete' => __( "Are you sure you want to delete this theme?\n\nClick 'Cancel' to go back, 'OK' to confirm the delete." ),
548 'adminUrl' => parse_url( admin_url(), PHP_URL_PATH ),
549 'ig_plan' => $ig_plan,
550 'cat_list' => $cat_list
551 ),
552 'l10n' => array(
553 'addNew' => __( 'Add New Gallery Templates' ),
554 'search' => __( 'Search Gallery Templates' ),
555 'searchPlaceholder' => __( 'Search Design Templates' ), // placeholder (no ellipsis)
556 'themesFound' => __( 'Number of Gallery Templates found: %d' ),
557 'noThemesFound' => __( 'No Gallery Templates found. Try a different search.' ),
558 ),
559 'installedThemes' => $imported_gallery_items
560 ) );
561 }
562 }
563
564 public function branding_data_remove( $icegram_branding_data ) {
565 if( !empty( $icegram_branding_data ) && 'yes' != get_option('icegram_share_love', 'no') ) {
566 $icegram_branding_data['powered_by_logo'] = '';
567 $icegram_branding_data['powered_by_text'] = '';
568 }
569 return $icegram_branding_data;
570 }
571
572 //Execute Form shortcode
573 function execute_form_shortcode( $atts = array() ) {
574 return '<div class="ig_form_container layout_inline"></div>';
575 }
576
577 function execute_shortcode( $atts = array() , $content = null ) {
578 // When shortcode is called, it will only prepare an array with conditions
579 // And add a placeholder div
580 // Display will happen in footer via display_messages()
581 $i = count($this->shortcode_instances);
582 $this->shortcode_instances[ $i ] = shortcode_atts( array(
583 'campaigns' => '',
584 'messages' => '',
585 'skip_others' => 'no'
586 ), $atts );
587
588 $class[] = "ig_shortcode_container";
589 $html[] = "<div id='icegram_shortcode_{$i}'";
590 if(!empty($atts['campaigns']) && !empty($content)){
591 $this->shortcode_instances[ $i ]['with_content'] = true;
592 $class[] = "trigger_onclick";
593 }
594 foreach ($atts as $key => $value) {
595 $value = str_replace(",", " ", $value);
596 $html[] = " data-{$key}=\"".htmlentities($value)."\" ";
597 }
598 $class = implode(" ", $class);
599 $html[] = "class='".$class."' >".$content."</div>";
600 return implode(" ", $html);
601 }
602
603 // Do not index Icegram campaigns / messages...
604 // Not using currently - made custom post types non public...
605 function icegram_load_data() {
606 global $post;
607 $icegram_pre_data['ajax_url'] = admin_url( 'admin-ajax.php' );
608 $icegram_pre_data['post_obj'] = $_GET;
609 $icegram_pre_data['post_obj']['is_home'] = (is_home() || is_front_page()) ? true : false;
610 $icegram_pre_data['post_obj']['page_id'] = is_object($post) && isset($post->ID) ? $post->ID : 0;
611 $icegram_pre_data['post_obj']['action'] = 'display_messages';
612 $icegram_pre_data['post_obj']['shortcodes'] = $this->shortcode_instances;
613 $icegram_pre_data['post_obj']['cache_compatibility'] = $this->cache_compatibility;
614 $icegram_pre_data['post_obj']['device'] = $this->get_platform();
615 wp_register_script('icegram_main_js', $this->plugin_url . '/assets/js/main.min.js', array ( 'jquery' ), $this->version, true);
616 if( !wp_script_is( 'icegram_main_js' ) ) {
617 wp_enqueue_script( 'icegram_main_js' );
618 wp_localize_script( 'icegram_main_js', 'icegram_pre_data', $icegram_pre_data);
619 }
620 }
621
622 function display_messages() {
623
624 $skip_others = $preview_mode = false;
625 $campaign_ids = $message_ids = array();
626 $this->shortcode_instances = ($this->cache_compatibility == 'yes' && !empty($_REQUEST['shortcodes'])) ? $_REQUEST['shortcodes'] : $this->shortcode_instances;
627 // Pull in message and campaign IDs from shortcodes - if set
628 if( !empty( $this->shortcode_instances ) ) {
629 foreach ($this->shortcode_instances as $i => $value) {
630 $cids = array_map( 'trim', (array) explode( ',', $value['campaigns'] ) );
631 $mids = array_map( 'trim', (array) explode( ',', $value['messages'] ) );
632 if (!empty($value['skip_others']) && $value['skip_others'] == 'yes' && (!empty($cids) || !empty($mids))) {
633 $skip_others = true;
634 }
635 $campaign_ids = array_merge($campaign_ids, $cids);
636 $message_ids = array_merge($message_ids, $mids);
637 }
638 }
639 if( !empty( $_REQUEST['campaign_preview_id'] ) && ( 'edit_posts' ) ) {
640 $campaign_ids = array( $_REQUEST['campaign_preview_id'] );
641 $preview_mode = true;
642 }
643
644
645 $messages = $this->get_valid_messages( $message_ids, $campaign_ids, $preview_mode, $skip_others );
646
647 if( empty( $messages ) ) {
648 //wp_die(0);
649 return;
650 }
651
652 $messages_to_show_ids = array();
653 foreach ( $messages as $key => $message_data ) {
654
655 if( !is_array( $message_data ) || empty( $message_data ) ) {
656 continue;
657 }
658
659 // Don't show a seen message again - if needed
660 // change to campaign targetting in v1.9.1
661 if( !empty( $message_data['id'] ) &&
662 empty( $_GET['campaign_preview_id'] ) &&
663 !empty( $message_data['retargeting'] ) &&
664 $message_data['retargeting'] == 'yes'
665 ) {
666 if(!empty($_COOKIE['icegram_messages_shown_'.$message_data['id']]) || !empty($_COOKIE['icegram_campaign_shown_'.floor($message_data['campaign_id'])])){
667 unset( $messages[$key] );
668 continue;
669 }
670 }
671 if( !empty( $message_data['id'] ) &&
672 empty( $_GET['campaign_preview_id'] ) &&
673 !empty( $message_data['retargeting_clicked'] ) &&
674 $message_data['retargeting_clicked'] == 'yes'
675 ) {
676 if(!empty($_COOKIE['icegram_messages_clicked_'.$message_data['id']]) || !empty($_COOKIE['icegram_campaign_clicked_'.floor($message_data['campaign_id'])])){
677 unset( $messages[$key] );
678 continue;
679 }
680 }
681
682 // Avoid showing the same message twice
683 if (in_array($message_data['id'], $messages_to_show_ids)) {
684 unset ( $messages[$key] );
685 continue;
686 } else {
687 $messages_to_show_ids[] = $message_data['id'];
688 }
689
690 $this->process_message_body($messages[$key]);
691 }
692 if( empty( $messages ) )
693 return;
694 $icegram_default = apply_filters( 'icegram_branding_data',
695 array ( 'icon' => $this->plugin_url . '/assets/images/icegram-logo-branding-64-grey.png',
696 'powered_by_logo' => $this->plugin_url . '/assets/images/icegram-logo-branding-64-grey.png',
697 'powered_by_text' => __( 'Powered by Icegram', 'icegram' )
698 ) );
699 $messages = apply_filters( 'icegram_messages_to_show', $messages );
700 $icegram_data = apply_filters( 'icegram_data', array ( 'messages' => array_values( $messages ),
701 'ajax_url' => admin_url( 'admin-ajax.php' ),
702 'preview_id' => !empty( $_GET['campaign_preview_id'] ) ? $_GET['campaign_preview_id'] : '',
703 'defaults' => $icegram_default
704 ));
705 if (empty($icegram_data['preview_id'])) {
706 unset($icegram_data['preview_id']);
707 }
708
709 do_action('icegram_print_js_css_data', $icegram_data);
710
711 do_action('icegram_data_printed');
712 }
713
714 function two_step_mobile_popup( $icegram_data ) {
715
716 $temp = array();
717 foreach ($icegram_data['messages'] as $message_id => $message) {
718
719 if(!empty($message['ig_mobile_popup']) && $message['ig_mobile_popup'] == true){
720 $action_bar = $message;
721 $action_bar['type'] = 'action-bar';
722 $action_bar['theme'] = 'hello';
723 $action_bar['position'] = '21';
724 $action_bar['message'] = '';
725 $action_bar['label'] = __('Show More', 'icegram');
726 $action_bar['id'] = $action_bar['id'].'_00';
727 $action_bar['use_custom_code'] = 'yes';
728 $action_bar['form_html'] = '';
729 $action_bar['form_html_original'] = '';
730 $action_bar['rainmaker_form_code'] = '';
731 $action_bar['link'] = '';
732 $action_bar['redirect_to_link'] = '';
733 $action_bar['cta'] = '';
734 $action_bar['alt_cta'] = '';
735 $action_bar['add_alt_cta'] = '';
736 $action_bar['custom_css'] = '#ig_this_message .ig_close{display:none;}';
737 $action_bar['custom_js'] = "<script type='text/javascript'>jQuery('#icegram_message_".$action_bar['id']."').on('click', '.ig_button', function(){icegram.get_message_by_id('".$action_bar['id']."').hide(); icegram.get_message_by_id('".$message['id']."').show(); });</script>";
738 unset($action_bar['ig_mobile_popup']);
739 $temp[] = $action_bar;
740 }
741 }
742 $icegram_data['messages'] = array_merge($icegram_data['messages'], $temp);
743 unset($temp);
744
745 return $icegram_data;
746 }
747
748
749 function print_js_css_data( $icegram_data ) {
750
751 $this->collect_js_and_css($icegram_data);
752 if($this->cache_compatibility === 'yes'){
753 echo json_encode($icegram_data);
754 wp_die();
755 }else{
756 wp_localize_script('icegram_main_js', 'icegram_data', $icegram_data);
757 }
758 }
759
760 function collect_js_and_css(&$icegram_data){
761
762 $types_shown = array();
763 $scripts = array();
764 $css = array();
765 foreach ($icegram_data['messages'] as $key => $message_data) {
766 $types_shown[] = $message_data['type'];
767 }
768
769 $types_shown = array_unique($types_shown);
770 $ver_psfix = '?var=' .$this->version;
771
772 $scripts[] = $this->plugin_url ."/assets/js/icegram.min.js". $ver_psfix;
773 $css[] = $this->plugin_url ."/assets/css/frontend.min.css". $ver_psfix;
774 //minify and combine only for default msg type
775 $ig_core_message_types = array('popup', 'action-bar', 'toast', 'messenger');
776 // Load JS and default CSS
777 foreach ($types_shown as $message_type) {
778 if(!in_array($message_type, $ig_core_message_types)){
779 $scripts[] = $this->message_types[$message_type]['baseurl'] ."main.js". $ver_psfix;
780 $css[] = $this->message_types[$message_type]['baseurl'] . "default.css". $ver_psfix;
781 }else{
782 $css[] = $this->message_types[$message_type]['baseurl'].'themes/'. $message_type. ".min.css". $ver_psfix;
783 }
784 }
785
786 //TODO :: add theme pack theme css files too.
787 // Load theme CSS
788 foreach ($icegram_data['messages'] as $key => $message) {
789 if ( !empty( $this->message_types[ $message['type'] ]['themes'][ $message['theme'] ]) ) {
790 $theme = $this->message_types[ $message['type'] ]['themes'][ $message['theme'] ]['baseurl'] . $message['theme'].'.css'. $ver_psfix;
791 }else{
792 $theme_default = $this->message_types[ $message['type']] ['settings']['theme']['default'];
793 $theme = $this->message_types[ $message['type'] ]['themes'][ $theme_default]['baseurl'] . $theme_default.'.css'. $ver_psfix;
794 $icegram_data['messages'][$key]['theme'] = $theme_default;
795 }
796 if(!preg_match('/icegram\/message-types/i', $theme)){
797 $css [] = $theme;
798 }
799 }
800 $css = array_unique($css);
801 $icegram_data['scripts'] = apply_filters('add_icegram_script' , $scripts);
802 $icegram_data['css'] = apply_filters('add_icegram_css' , $css);
803 return $icegram_data;
804 }
805
806 // Process
807 function process_message_body(&$message_data){
808 global $wp_scripts;
809 global $wp_styles;
810
811 if($this->cache_compatibility == 'yes'){
812 $q_script = !empty($wp_scripts->queue) ? $wp_scripts->queue : array();
813 $q_style = !empty($wp_styles->queue) ? $wp_styles->queue : array();
814 }
815 $content = $message_data['message'];
816 $content = convert_chars(convert_smilies( wptexturize( $content )));
817 if(isset($GLOBALS['wp_embed'])) {
818 $content = $GLOBALS['wp_embed']->autoembed($content);
819 }
820 $content = $this->after_wpautop( wpautop( $this->before_wpautop( $content ) ) );
821 $content = do_shortcode( shortcode_unautop( $content ) );
822 $message_data['message'] = $content;
823
824 //do_shortcode in headline
825 $message_data['headline'] = do_shortcode( shortcode_unautop( $message_data['headline'] ) );
826 //shortcode support for Third party forms and Rainmaker
827 $form_html_original = !empty($message_data["rainmaker_form_code"])
828 ? ('[rainmaker_form id="'. $message_data["rainmaker_form_code"] .'"]')
829 :(!empty($message_data['form_html_original']) ? $message_data['form_html_original'] : '');
830 if(!empty($form_html_original)){
831 $message_data['form_html'] = do_shortcode( shortcode_unautop( $form_html_original) );
832 }
833 //TODO :: Handle case for inline style and script
834 if($this->cache_compatibility == 'yes'){
835 $handles = !empty($wp_scripts->queue) ? array_diff($wp_scripts->queue, $q_script) : array();
836 unset($q_script);
837 if(!empty($handles)){
838 if(empty($message_data['assets']))
839 $message_data['assets'] = array();
840
841 ob_start();
842 $wp_scripts->do_items($handles);
843 $message_data['assets']['scripts'] = array_filter(explode('<script', ob_get_clean()));
844 }
845
846 //TODO :: do_items if required
847 $handles = !empty($wp_styles->queue) ? array_diff($wp_styles->queue, $q_style) : array();
848 unset($q_style);
849 if(!empty($handles)){
850 if(empty($message_data['assets']))
851 $message_data['assets'] = array();
852
853 foreach ($handles as $handle) {
854 ob_start();
855 $wp_styles->do_item($handle);
856 $message_data['assets']['styles'][$handle] = ob_get_clean();
857 }
858 }
859 }
860 }
861
862 function enqueue_admin_styles_and_scripts() {
863
864 $screen = get_current_screen();
865 if ( !in_array( $screen->id, array( 'ig_campaign', 'ig_message', 'edit-ig_campaign' ), true ) ) return;
866
867 // Register scripts
868 wp_register_script( 'icegram_writepanel', $this->plugin_url . '/assets/js/admin.min.js', array ( 'jquery', 'wp-color-picker' ), $this->version );
869
870 wp_enqueue_script( 'icegram_writepanel' );
871
872 $icegram_writepanel_params = array ( 'ajax_url' => admin_url( 'admin-ajax.php' ), 'search_message_nonce' => wp_create_nonce( "search-messages" ), 'home_url' => home_url( '/' ) );
873 $this->available_headlines = apply_filters( 'icegram_available_headlines', array() );
874 $icegram_writepanel_params = array_merge( $icegram_writepanel_params, array( 'available_headlines' => $this->available_headlines ) );
875
876 wp_localize_script( 'icegram_writepanel', 'icegram_writepanel_params', $icegram_writepanel_params );
877
878 wp_enqueue_style( 'dashicons' );
879 wp_enqueue_style( 'wp-color-picker' );
880 wp_enqueue_style( 'icegram_admin_styles', $this->plugin_url . '/assets/css/admin.min.css', array(), $this->version );
881
882 if ( !wp_script_is( 'jquery-ui-datepicker' ) ) {
883 wp_enqueue_script( 'jquery-ui-datepicker' );
884 }
885
886 }
887
888 //execute shortcode in text widget
889 function ig_widget_text_filter($content){
890 if ( ! preg_match( '/\[[\r\n\t ]*icegram?[\r\n\t ].*?\]/', $content ) )
891 return $content;
892
893 $content = do_shortcode( $content );
894
895 return $content;
896 }
897
898 public static function get_platform() {
899 $mobile_detect = new Ig_Mobile_Detect();
900 $mobile_detect->setUserAgent();
901 if($mobile_detect->isMobile()){
902 return ($mobile_detect->isTablet()) ? 'tablet' : 'mobile';
903 }else if($mobile_detect->isTablet()){
904 return 'tablet';
905 }
906 return 'laptop';
907 }
908
909 function get_message_data( $message_ids = array(), $preview = false ) {
910 global $wpdb;
911 $message_data = array();
912 $original_message_id_map = array();
913 $meta_key = $preview ? 'icegram_message_preview_data' : 'icegram_message_data';
914 $message_data_query = "SELECT post_id, meta_value FROM {$wpdb->prefix}postmeta WHERE meta_key LIKE '$meta_key'";
915 if ( !empty( $message_ids ) && is_array( $message_ids ) ) {
916 // For WPML compatibility
917 if ( function_exists('icl_object_id') ) {
918 $wpml_settings = get_option('icl_sitepress_settings');
919 $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;
920
921 foreach ($message_ids as $i=>$id ) {
922 $translated = icl_object_id( $id, 'ig_message', $original_if_missing );
923 $message_ids[ $i ] = $translated;
924 $original_message_id_map[ $translated ] = $id;
925 }
926 }
927 $message_ids = array_filter(array_unique($message_ids));
928 if ( !empty( $message_ids ) ) {
929 $message_data_query .= " AND post_id IN ( " . implode( ',', $message_ids ) . " )";
930 $message_data_results = $wpdb->get_results( $message_data_query, 'ARRAY_A' );
931 foreach ( $message_data_results as $message_data_result ) {
932 $data = maybe_unserialize( $message_data_result['meta_value'] );
933 if (!empty($data)) {
934 $message_data[$message_data_result['post_id']] = $data;
935 // For WPML compatibility
936 if (!empty( $original_message_id_map[ $message_data_result['post_id'] ])) {
937 $message_data[$message_data_result['post_id']]['original_message_id'] = $original_message_id_map[ $message_data_result['post_id'] ];
938 }
939 }
940 }
941 }
942 }
943
944 return $message_data;
945 }
946
947 function get_valid_messages( $message_ids = array(), $campaign_ids = array(), $preview_mode = false, $skip_others = false) {
948 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));
949
950 $valid_messages = $valid_campaigns = $message_campaign_map = array();
951
952 $campaign_ids = array_filter(array_unique( (array) $campaign_ids));
953 $message_ids = array_filter(array_unique( (array) $message_ids));
954 if ( !empty( $campaign_ids ) ) {
955 $valid_campaigns = $this->get_valid_campaigns( $campaign_ids, true ,$preview_mode);
956 }
957
958 // When skip_others is true, we won't load campaigns / messages from db
959 if (!$skip_others && !$preview_mode) {
960 $campaigns = $this->get_valid_campaigns();
961 if (!empty($campaigns)) {
962 foreach ($campaigns as $id => $campaign) {
963 if (!array_key_exists($id, $valid_campaigns)) {
964 $valid_campaigns[ $id ] = $campaign;
965 }
966 }
967 }
968 }
969
970 // Create a map to look up campaign id for a given message
971 if( !empty( $valid_campaigns ) ) {
972 foreach ($valid_campaigns as $id => $campaign) {
973 if ($preview_mode) {
974 $campaign->messages = get_post_meta( $id, 'campaign_preview', true );
975 }
976 if( !empty( $campaign->messages ) ) {
977 foreach( $campaign->messages as $msg) {
978 $message_ids[] = $msg['id'];
979 if (!array_key_exists( $msg['id'], $message_campaign_map)) {
980 $message_campaign_map[ $msg['id'] ] = $id;
981 }
982 }
983 }
984 }
985 }
986
987 // We don't display same message twice...
988 $message_ids = array_unique($message_ids);
989
990 if( empty( $message_ids ) ) {
991 return array();
992 }
993 $valid_messages = $this->get_message_data( $message_ids, $preview_mode );
994
995 foreach ($valid_messages as $id => $message_data) {
996 // Remove message if required fields are missing
997 if (empty($message_data) || empty($message_data['type'])) {
998 unset( $valid_messages[$id] );
999 continue;
1000 }
1001 // Remove message if message type is uninstalled
1002 $class_name = 'Icegram_Message_Type_' . str_replace(' ', '_', ucwords(str_replace('-', ' ', $message_data['type'])));
1003 if( !class_exists( $class_name ) ) {
1004 unset( $valid_messages[$id] );
1005 continue;
1006 }
1007 $message_data['delay_time'] = 0;
1008 $message_data['retargeting'] = '';
1009 $message_data['campaign_id'] = ($preview_mode) ? $_REQUEST['campaign_preview_id'] : '';
1010
1011 // Pull display time and retargeting rule from campaign if possible
1012 $message_id = (!empty($message_data['original_message_id'])) ? $message_data['original_message_id'] : $id;
1013 if (!empty($message_campaign_map[ $message_id ])) {
1014 //modify campaign id
1015 $message_data['campaign_id'] = apply_filters('modify_campaing_id' , $message_campaign_map[ $message_id ] , $message_id) ;
1016 $campaign = $valid_campaigns[ floor($message_data['campaign_id']) ];
1017 if (!empty($campaign) && $campaign instanceof Icegram_Campaign) {
1018 $message_meta_from_campaign = $campaign->get_message_meta_by_id( $message_id );
1019 if (!empty($message_meta_from_campaign['time'])) {
1020 $message_data['delay_time'] = $message_meta_from_campaign['time'];
1021 }
1022
1023 //check if campaign is targeted to mobile at zero
1024 $device_rule = $campaign->get_rule_value('device');
1025 if($this->get_platform() !== 'laptop' && !empty($device_rule['mobile']) && $device_rule['mobile'] == 'yes'&& $message_data['delay_time'] == 0 && $message_data['type'] == 'popup'){
1026 $message_data['ig_mobile_popup'] = true;
1027 if(!empty($message_data['triggers']) && !empty($message_data['triggers']['when_to_show'])){
1028 $message_data['ig_mobile_popup'] = ($message_data['triggers']['when_to_show'] == 'duration_on_page' && $message_data['triggers']['duration_on_page'] == 0 ) ? true : false;
1029 }
1030 }
1031 //set delay time -1 if shortcode with content
1032 foreach ($this->shortcode_instances as $i => $value) {
1033 $campaign_ids = explode( ',', $value['campaigns']);
1034 if(!empty($value['with_content']) && in_array($message_data['campaign_id'], $campaign_ids)){
1035 $message_data['delay_time'] = -1;
1036 }
1037 }
1038 $rule_value = $campaign->get_rule_value('retargeting');
1039 $message_data['retargeting'] = !empty( $rule_value['retargeting'] ) ? $rule_value['retargeting'] : '';
1040 $message_data['expiry_time'] = !empty( $rule_value['retargeting'] ) ? $rule_value['expiry_time'] : '';
1041 $rule_value_retargeting_clicked = $campaign->get_rule_value('retargeting_clicked');
1042 $message_data['retargeting_clicked'] = !empty( $rule_value_retargeting_clicked['retargeting_clicked'] ) ? $rule_value_retargeting_clicked['retargeting_clicked'] : '';
1043 $message_data['expiry_time_clicked'] = !empty( $rule_value_retargeting_clicked['retargeting_clicked'] ) ? $rule_value_retargeting_clicked['expiry_time_clicked'] : '';
1044
1045 }
1046 }
1047 $valid_messages[$id] = $message_data;
1048 }
1049 $valid_messages = apply_filters( 'icegram_valid_messages', $valid_messages );
1050
1051 return $valid_messages;
1052 }
1053
1054 function get_valid_campaigns( $campaign_ids = array(), $skip_page_check = false ,$preview_mode = false) {
1055 global $wpdb;
1056 if ( empty( $campaign_ids ) ) {
1057 $sql = "SELECT pm.post_id
1058 FROM {$wpdb->prefix}posts AS p
1059 LEFT JOIN {$wpdb->prefix}postmeta AS pm ON ( pm.post_id = p.ID )
1060 WHERE p.post_status = 'publish' ";
1061 // Filter handler within this file (and possibly others) will append to this SQL
1062 // and provide arguments for wpdb->prepare if needed.
1063 // First element in the array is SQL, remaining are values for placeholders in SQL
1064 $sql_params = apply_filters( 'icegram_get_valid_campaigns_sql', array($sql), array() );
1065 $campaign_ids = $wpdb->get_col( $wpdb->prepare( array_shift($sql_params), $sql_params ) );
1066 }
1067 $valid_campaigns = array();
1068 foreach ( (array) $campaign_ids as $campaign_id ) {
1069 $campaign = new Icegram_Campaign( $campaign_id );
1070 if ( $preview_mode || $campaign->is_valid( array('skip_page_check' => $skip_page_check) ) ) {
1071 $valid_campaigns[$campaign_id] = $campaign;
1072 } else {
1073 // Campgain is invalid!
1074 }
1075
1076 }
1077 $valid_campaigns = apply_filters('icegram_valid_campaigns', $valid_campaigns);
1078 return $valid_campaigns;
1079 }
1080
1081 function append_to_valid_campaigns_sql( $sql_params = array(), $options = array() ) {
1082 // Page check conditions
1083 //$pid = $_GET['page_id'];
1084 $pid = Icegram::get_current_page_id();
1085 $sql = " AND (
1086 pm.meta_key = 'icegram_campaign_target_rules' AND (
1087 ( pm.meta_value LIKE '%%%s%%' )
1088 OR ( pm.meta_value LIKE '%%%s%%' AND pm.meta_value LIKE '%%%s%%' AND pm.meta_value LIKE '%%%s%%' )
1089 ";
1090 $sql_params[] = 's:8:"sitewide";s:3:"yes";';
1091 $sql_params[] = 's:10:"other_page";s:3:"yes";';
1092 $sql_params[] = 's:7:"page_id";a:';
1093 $sql_params[] = serialize( (string) $pid );
1094 //local url
1095 $sql .= " OR ( pm.meta_value LIKE '%%%s%%' )";
1096 $sql_params[] = 's:9:"local_url";s:3:"yes";';
1097 if(!empty($_REQUEST['cache_compatibility']) && $_REQUEST['cache_compatibility'] == 'yes'){
1098 $is_home = (!empty($_REQUEST['is_home']) && $_REQUEST['is_home'] === 'true') ? true : false ;
1099 }else{
1100 $is_home = (is_home() || is_front_page()) ? true : false;
1101 }
1102 if ($is_home === true ) {
1103 $sql .= " OR ( pm.meta_value LIKE '%%%s%%' )";
1104 $sql_params[] = 's:8:"homepage";s:3:"yes";';
1105 }
1106 $sql .=" ) )";
1107
1108 $sql_params[0] .= $sql;
1109 //s:9:"logged_in";s:3:"all";
1110
1111 return $sql_params;
1112 }
1113
1114 // Include all classes required for Icegram plugin
1115 function include_classes() {
1116
1117 $classes = glob( $this->plugin_path . '/classes/*.php' );
1118 foreach ( $classes as $file ) {
1119 // Files with 'admin' in their name are included only for admin section
1120 if ( is_file( $file ) && ( (strpos($file, '-admin') >= 0 && is_admin()) || (strpos($file, '-admin') === false) ) ) {
1121 include_once $file;
1122 }
1123 }
1124
1125 // Load built in message types
1126 $icegram_message_type_basedirs = glob( $this->plugin_path . '/message-types/*' );
1127 // Allow other plugins to add new message types
1128 $icegram_message_type_basedirs = apply_filters( 'icegram_message_type_basedirs', $icegram_message_type_basedirs );
1129 // Set up different message type classes
1130 foreach ( $icegram_message_type_basedirs as $dir ) {
1131 $type = basename ( $dir );
1132 $class_file = $dir . "/main.php";
1133 if( is_file( $class_file ) ) {
1134 include_once( $class_file );
1135 }
1136 $class_name = 'Icegram_Message_Type_' . str_replace(' ', '_', ucwords(str_replace('-', ' ', $type)));
1137 if (class_exists($class_name)) {
1138 $this->message_type_objs[ $type ] = new $class_name();
1139 }
1140 }
1141 do_action('ig_file_include');
1142 $this->message_types = apply_filters( 'icegram_message_types', array() );
1143 }
1144
1145 // Register Campaign post type
1146 function register_campaign_post_type() {
1147 $labels = array(
1148 'name' => __( 'Campaigns', 'icegram' ),
1149 'singular_name' => __( 'Campaign', 'icegram' ),
1150 'add_new' => __( 'Add New Campaign', 'icegram' ),
1151 'add_new_item' => __( 'Add New Campaign', 'icegram' ),
1152 'edit_item' => __( 'Edit Campaign', 'icegram' ),
1153 'new_item' => __( 'New Campaign', 'icegram' ),
1154 'all_items' => __( 'Campaigns', 'icegram' ),
1155 'view_item' => __( 'View Campaign', 'icegram' ),
1156 'search_items' => __( 'Search Campaigns', 'icegram' ),
1157 'not_found' => __( 'No campaigns found', 'icegram' ),
1158 'not_found_in_trash' => __( 'No campaigns found in Trash', 'icegram' ),
1159 'parent_item_colon' => __( '', 'icegram' ),
1160 'menu_name' => __( 'Icegram', 'icegram' )
1161 );
1162
1163 $args = array(
1164 'labels' => $labels,
1165 // 'menu_icon' => 'dashicons-info',
1166 'public' => false,
1167 'publicly_queryable' => false,
1168 'show_ui' => true,
1169 'show_in_menu' => true,
1170 'query_var' => true,
1171 'rewrite' => array( 'slug' => 'ig_campaign' ),
1172 'capability_type' => 'post',
1173 'has_archive' => false,
1174 'hierarchical' => false,
1175 'menu_position' => null,
1176 'menu_icon' => $this->plugin_url . '/assets/images/icegram-logo-branding-18-white.png' ,
1177 'supports' => array( 'title', 'editor' )
1178 );
1179
1180 register_post_type( 'ig_campaign', $args );
1181 }
1182
1183 // Register Message post type
1184 function register_message_post_type() {
1185 $labels = array(
1186 'name' => __( 'Messages', 'icegram' ),
1187 'singular_name' => __( 'Message', 'icegram' ),
1188 'add_new' => __( 'Create New', 'icegram' ),
1189 'add_new_item' => __( 'Create New Message', 'icegram' ),
1190 'edit_item' => __( 'Edit Message', 'icegram' ),
1191 'new_item' => __( 'New Message', 'icegram' ),
1192 'all_items' => __( 'Messages', 'icegram' ),
1193 'view_item' => __( 'View Message', 'icegram' ),
1194 'search_items' => __( 'Search Messages', 'icegram' ),
1195 'not_found' => __( 'No messages found', 'icegram' ),
1196 'not_found_in_trash' => __( 'No messages found in Trash', 'icegram' ),
1197 'parent_item_colon' => __( '', 'icegram' ),
1198 'menu_name' => __( 'Messages', 'icegram' )
1199 );
1200
1201 $args = array(
1202 'labels' => $labels,
1203 'public' => false,
1204 'publicly_queryable' => false,
1205 'show_ui' => true,
1206 'show_in_menu' => 'edit.php?post_type=ig_campaign',
1207 'query_var' => true,
1208 'rewrite' => array( 'slug' => 'ig_message' ),
1209 'capability_type' => 'post',
1210 'has_archive' => false,
1211 'hierarchical' => false,
1212 'menu_position' => null,
1213 'supports' => array( 'title' )
1214 );
1215
1216 register_post_type( 'ig_message', $args );
1217 }
1218
1219
1220 function import( $data = array() ) {
1221 if ( empty( $data['campaigns'] ) && empty( $data['messages'] ) ) return;
1222
1223 $default_theme = $default_type = '';
1224 $first_message_type = current( $this->message_types );
1225
1226 if( is_array( $first_message_type ) ) {
1227 $default_type = $first_message_type['type'];
1228 if( !empty( $first_message_type['themes'] ) ) {
1229 $default_theme = key( $first_message_type['themes'] );
1230 }
1231 }
1232
1233 $new_campaign_ids = array();
1234 foreach ( (array) $data['campaigns'] as $campaign ) {
1235
1236 $args = array(
1237 'post_content' => ( !empty( $campaign['post_content'] ) ) ? esc_attr( $campaign['post_content'] ) : '',
1238 'post_name' => ( !empty( $campaign['post_title'] ) ) ? sanitize_title( $campaign['post_title'] ) : '',
1239 'post_title' => ( !empty( $campaign['post_title'] ) ) ? $campaign['post_title'] : '',
1240 'post_status' => ( !empty( $campaign['post_status'] ) ) ? $campaign['post_status'] : 'draft',
1241 'post_type' => 'ig_campaign'
1242 );
1243
1244 $new_campaign_id = wp_insert_post( $args );
1245 $new_campaign_ids[] = $new_campaign_id;
1246
1247 if ( !empty( $campaign['target_rules'] ) ) {
1248
1249 $defaults = array (
1250 'homepage' => 'yes',
1251 'when' => 'always',
1252 'from' => '',
1253 'to' => '',
1254 'mobile' => 'yes',
1255 'tablet' => 'yes',
1256 'laptop' => 'yes',
1257 'logged_in' => 'all'
1258 );
1259
1260 $target_rules = wp_parse_args( $campaign['target_rules'], $defaults );
1261 update_post_meta( $new_campaign_id, 'icegram_campaign_target_rules', $target_rules );
1262 }
1263
1264 if ( !empty( $campaign['messages'] ) ) {
1265
1266 $messages = array();
1267
1268 foreach ( $campaign['messages'] as $message ) {
1269
1270 if ( !is_array( $message ) ) continue;
1271
1272 $args = array(
1273 'post_content' => ( !empty( $message['message'] ) ) ? esc_attr( $message['message'] ) : '',
1274 'post_name' => ( !empty( $message['post_title'] ) ) ? sanitize_title( $message['post_title'] ) : '',
1275 'post_title' => ( !empty( $message['post_title'] ) ) ? $message['post_title'] : '',
1276 'post_status' => ( !empty( $message['post_status'] ) ) ? $message['post_status'] : 'publish',
1277 'post_type' => 'ig_message'
1278 );
1279
1280 $new_message_id = wp_insert_post( $args );
1281 $new_message = array(
1282 'id' => $new_message_id,
1283 'time' => ( !empty( $message['time'] ) ) ? $message['time'] : 0
1284 );
1285 //for gallery + CTA another message
1286 if(!empty($message['cta']) && $message['cta'] == 'cta_another_message' && !empty($message['cta_linked_message_id']) && $message['cta_linked_message_id'] == 'auto'){
1287 $prev_message = end($messages);
1288 $message['cta_linked_message_id'] = $prev_message['id'];
1289 array_pop($messages);
1290 }
1291 $messages[] = $new_message;
1292
1293 unset( $message['post_content'] );
1294 unset( $message['time'] );
1295
1296 $message['id'] = $new_message_id;
1297
1298 $defaults = array (
1299 'post_title' => '',
1300 'type' => $default_type,
1301 'theme' => $default_theme,
1302 'animation' => '',
1303 'headline' => '',
1304 'label' => '',
1305 'link' => '',
1306 'icon' => '',
1307 'message' => '',
1308 'position' => '',
1309 'text_color' => '#000000',
1310 'bg_color' => '#ffffff',
1311 'custom_code' => '',
1312 'id' => ''
1313 );
1314 $icegram_message_data = wp_parse_args( $message, $defaults );
1315 if ( !empty( $icegram_message_data ) ) {
1316 update_post_meta( $new_message_id, 'icegram_message_data', $icegram_message_data );
1317 update_post_meta( $new_message_id, 'icegram_message_preview_data', $icegram_message_data );
1318 }
1319 }//foreach
1320
1321 if ( !empty( $campaign['messages'] ) ) {
1322 update_post_meta( $new_campaign_id, 'messages', $messages );
1323 update_post_meta( $new_campaign_id, 'campaign_preview', $messages );
1324 }
1325 }//if
1326 }
1327 return $new_campaign_ids;
1328
1329 }
1330
1331 function import_gallery_item(){
1332 if(!empty($_REQUEST['action']) && $_REQUEST['action'] == 'fetch_messages' && !empty($_REQUEST['campaign_id']) && !empty($_REQUEST['gallery_item'])){
1333 $params = $_REQUEST;
1334 $imported_gallery_items = array();
1335 $url = 'https://www.icegram.com/gallery/wp-admin/admin-ajax.php?utm_source=ig_inapp&utm_campaign=ig_gallery&utm_medium='.$_REQUEST['campaign_id'];
1336 $options = array(
1337 'timeout' => 15,
1338 'method' => 'POST',
1339 'body' => http_build_query( $params )
1340 );
1341 $response = wp_remote_request( $url, $options );
1342 $response_code = wp_remote_retrieve_response_code( $response );
1343 if ( $response_code == 200 ) {
1344 $new_campaign_ids = $this->import(json_decode($response['body'] ,true));
1345 if(!empty($new_campaign_ids)){
1346 $imported_gallery_items = get_option('ig_imported_gallery_items');
1347 $imported_gallery_items[] = $_REQUEST['campaign_id'];
1348 update_option( 'ig_imported_gallery_items', $imported_gallery_items);
1349 $location = admin_url( 'post.php?post='.$new_campaign_ids[0].'&action=edit');
1350 header('Location:'.$location);
1351 exit;
1352 }else{
1353 wp_safe_redirect($_SERVER['HTTP_REFERER']);
1354 }
1355 }
1356 }
1357 }
1358
1359 function import_sample_data ($data = array() ){
1360 $new_campaign_ids = $this->import($data);
1361 if( !empty( $new_campaign_ids ) ) {
1362 update_option( 'icegram_sample_data_imported', $new_campaign_ids );
1363 }
1364 }
1365
1366
1367
1368
1369 function get_sample_data() {
1370
1371 return array(
1372 'campaigns' => array(
1373 array(
1374 'post_name' => '',
1375 'post_title' => 'My First Icegram Campaign',
1376 'target_rules' => array (
1377 'homepage' => 'yes',
1378 'when' => 'always',
1379 'from' => '',
1380 'to' => '',
1381 'mobile' => 'yes',
1382 'tablet' => 'yes',
1383 'laptop' => 'yes',
1384 'logged_in' => 'all'
1385 ),
1386 'messages' => array(
1387 array (
1388 'post_title' => 'Get 2x more Contacts with Your Website',
1389 'post_status' => 'publish',
1390 'time' => '0',
1391 'type' => 'action-bar',
1392 'theme' => 'hello',
1393 'headline' => 'Get 2x more Contacts with Your Website',
1394 'label' => 'Show Me How',
1395 'link' => '',
1396 'icon' => '',
1397 'message' => 'Instant Results Guaranteed',
1398 'position' => '01',
1399 'text_color' => '#000000',
1400 'bg_color' => '#eb593c'
1401 ),
1402 array (
1403 'post_title' => '20% Off Coupon',
1404 'post_status' => 'publish',
1405 'time' => '4',
1406 'type' => 'messenger',
1407 'theme' => 'social',
1408 'animation' => 'slide',
1409 'headline' => '20% Off - for you',
1410 'label' => '',
1411 'link' => '',
1412 'icon' => '',
1413 'message' => "Hey there! We are running a <strong>special 20% off this week</strong> for registered users - like you.
1414
1415 Use coupon <code>LOYALTY20</code> during checkout.",
1416 'position' => '22',
1417 'text_color' => '#000000',
1418 'bg_color' => '#ffffff'
1419 ),
1420 array (
1421 'post_title' => 'How this blog makes over $34,800 / month for FREE.',
1422 'post_status' => 'publish',
1423 'time' => '10',
1424 'type' => 'popup',
1425 'theme' => 'air-mail',
1426 'headline' => 'How this blog makes over $34,800 / month for FREE.',
1427 'label' => 'FREE INSTANT ACCESS',
1428 'link' => '',
1429 'icon' => '',
1430 '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.
1431
1432 <strong>Download my free kit to learn how I do this.</strong>
1433
1434 <ul>
1435 <li>How to choose blog topics that create long term value</li>
1436 <li>The type of blog post that will make your site go viral</li>
1437 <li>How to free yourself from the routine tasks</li>
1438 <li>Resources and tips to get started quickly</li>
1439 <li>Private members club to connect with fellow owners</li>
1440 </ul>",
1441 'text_color' => '#000000',
1442 'bg_color' => '#ffffff'
1443
1444 ),
1445 array (
1446 'post_title' => 'Exclusive Marketing Report',
1447 'post_status' => 'publish',
1448 'time' => '6',
1449 'type' => 'toast',
1450 'theme' => 'stand-out',
1451 'animation' => 'pop',
1452 'headline' => 'Exclusive Marketing Report',
1453 'label' => '',
1454 'link' => '',
1455 'icon' => '',
1456 'message' => 'FREE for every subscriber. Click here to download it.',
1457 'position' => '02',
1458 'text_color' => '#000000',
1459 'bg_color' => '#ffffff'
1460 )
1461
1462 )
1463 )
1464 )
1465 );
1466 }
1467
1468 function remove_preview_button() {
1469 global $post_type;
1470 if( $post_type == 'ig_message' || $post_type == 'ig_campaign' ) {
1471 ?>
1472 <style type="text/css">
1473 #message.updated.below-h2{ display: none; }
1474 #preview-action { display:none; }
1475 /*Remove add new campaign and message*/
1476 .page-title-action{
1477 display:none;
1478 }
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_in_db( $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
1657 foreach( $taxonomies as $taxonomy ) {
1658 $terms = wp_get_post_terms( $original_id, $taxonomy, array('fields' => 'names') );
1659 wp_set_object_terms( $duplicate_id, $terms, $taxonomy );
1660 }
1661
1662 // Duplicate all custom fields
1663 $custom_fields = get_post_custom( $original_id );
1664 foreach ( $custom_fields as $key => $value ) {
1665 if($key === 'messages'){
1666 $messages = unserialize($value[0]);
1667 foreach ($messages as &$message) {
1668 $clone_msg_id = Icegram::duplicate_in_db($message['id']);
1669 $message['id'] = $clone_msg_id;
1670 }
1671 $value[0] = serialize($messages);
1672 }
1673 add_post_meta( $duplicate_id, $key, maybe_unserialize($value[0]) );
1674 }
1675 return $duplicate_id;
1676 }
1677
1678 static function duplicate( $original_id){
1679 $duplicate_id = Icegram::duplicate_in_db($original_id);
1680 $location = admin_url( 'post.php?post='.$duplicate_id.'&action=edit');
1681 header('Location:'.$location);
1682 exit;
1683 }
1684
1685 public static function form_submission_validate_request($request_data){
1686 if(!empty($request_data)){
1687 // Check for Remote Rainmaker form submission request
1688 $request_data['ig_is_remote'] = false;
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['ig_is_remote'] = true;
1699 $request_data['is_remote'] = true;
1700 break;
1701 }
1702 }
1703 //TODO :: discard the the remote request and data
1704 // if($request_data['ig_is_remote'] == false){
1705 // return array();
1706 // }
1707 }
1708
1709 }
1710 }
1711 }
1712 return $request_data;
1713 }
1714
1715 }
1716
1717
1718 function initialize_icegram() {
1719 global $icegram;
1720
1721 // i18n / l10n - load translations
1722 load_plugin_textdomain( 'icegram', false, dirname( plugin_basename( __FILE__ ) ) . '/lang/' );
1723
1724 $icegram = new Icegram();
1725 do_action('icegram_loaded');
1726 }
1727
1728 add_action( 'plugins_loaded', 'initialize_icegram' );
1729 register_activation_hook( __FILE__, array( 'Icegram', 'install' ) );