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

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