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

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