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

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