PluginProbe
Icegram Engage – Popups, Optins, CTAs & Lead Generation / 1.9.24
Icegram Engage – Popups, Optins, CTAs & Lead Generation v1.9.24
3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 3.1.34 3.1.35 3.1.36 3.1.37 3.1.38 3.1.39 3.1.4 3.1.40 3.1.41 3.1.42 3.1.43 trunk 1.1 1.1.1 1.1.2 1.10 1.10.1 1.10.10 1.10.11 1.10.12 All 180 releases
icegram / icegram.php

icegram.php in Icegram Engage – Popups, Optins, CTAs & Lead Generation 1.9.24, at icegram.php

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