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

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