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

1,097 lines 49.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 * Plugin Name: Icegram
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.2
7 * Author: Icegram
8 * Author URI: http://www.icegram.com/
9 *
10 * Copyright (c) 2014 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
32 public static $current_page_id;
33
34 function __construct() {
35
36 $this->version = "1.2";
37 $this->shortcode_instances = array();
38 $this->plugin_url = untrailingslashit( plugins_url( '/', __FILE__ ) );
39 $this->plugin_path = untrailingslashit( plugin_dir_path( __FILE__ ) );
40 $this->include_classes();
41
42 if( is_admin() && current_user_can( 'manage_options' ) ) {
43 new Icegram_Campaign_Admin();
44 new Icegram_Message_Admin();
45 add_action( 'admin_enqueue_scripts', array( &$this, 'enqueue_admin_styles_and_scripts' ) );
46 add_action( 'admin_print_styles', array( &$this, 'remove_preview_button' ) );
47 add_filter( 'post_row_actions', array( &$this , 'remove_row_actions' ), 10, 2 );
48
49 add_action( 'admin_menu', array( &$this, 'admin_menus') );
50 add_action( 'admin_init', array( &$this, 'welcome' ) );
51
52 add_action( 'icegram_settings_after', array( &$this, 'klawoo_subscribe_form' ) );
53 add_action( 'icegram_about_changelog', array( &$this, 'klawoo_subscribe_form' ) );
54 } else {
55 add_action( 'wp_footer', array( &$this, 'display_messages' ) );
56 add_action( 'wp_print_scripts', array( &$this, 'identify_current_page' ) );
57 add_shortcode( 'icegram', array( &$this, 'execute_shortcode' ) );
58 add_filter( 'icegram_branding_data', array( &$this , 'branding_data_remove' ), 10 );
59 add_filter( 'icegram_get_valid_campaigns_sql', array( &$this , 'append_to_valid_campaigns_sql' ), 10, 2 );
60 // WPML compatibility
61 add_filter( 'icegram_identify_current_page', array( &$this, 'wpml_get_parent_id' ), 10 );
62 }
63
64 // common
65 add_action( 'init', array( &$this, 'register_campaign_post_type' ) );
66 add_action( 'init', array( &$this, 'register_message_post_type' ) );
67
68 if ( defined( 'DOING_AJAX' ) ) {
69 add_action( 'wp_ajax_icegram_event_track', array( &$this, 'icegram_event_track' ) );
70 add_action( 'wp_ajax_nopriv_icegram_event_track', array( &$this, 'icegram_event_track' ) );
71 add_action( 'wp_ajax_klawoo_subscribe', array( &$this, 'klawoo_subscribe' ) );
72 }
73
74 }
75
76 public function klawoo_subscribe_form() {
77 ?>
78 <div class="wrap">
79 <?php
80 if ( stripos(get_current_screen()->base, 'settings') !== false ) {
81 echo "<h2>".__( 'Get Updates', 'icegram' )."</h2>";
82 }
83 ?>
84 <table class="form-table">
85 <tr>
86 <th scope="row"><?php _e( 'Join our newsletter', 'icegram' ) ?></th>
87 <td>
88 <form name="klawoo_subscribe" action="#" method="POST" accept-charset="utf-8">
89 <input class="ltr" type="text" name="name" id="name" placeholder="Name"/>
90 <input class="regular-text ltr" type="text" name="email" id="email" placeholder="Email"/>
91 <input type="hidden" name="list" value="7I763v6Ldrs3YhJeee5EOgFA"/>
92 <input type="submit" name="submit" id="submit" class="button button-primary" value="Subscribe">
93 <br/>
94 <div id="klawoo_response"></div>
95 </form>
96 </td>
97 </tr>
98 </table>
99 </div>
100 <script type="text/javascript">
101 jQuery(function () {
102 jQuery("form[name=klawoo_subscribe]").submit(function (e) {
103 e.preventDefault();
104
105 jQuery('#klawoo_response').html('');
106 params = jQuery("form[name=klawoo_subscribe]").serializeArray();
107 params.push( {name: 'action', value: 'klawoo_subscribe' });
108
109 jQuery.ajax({
110 method: 'POST',
111 type: 'text',
112 url: "<?php echo admin_url( 'admin-ajax.php' ); ?>",
113 data: params,
114 success: function(response) {
115 if (response != '') {
116 jQuery('#klawoo_response').html(response);
117 } else {
118 jQuery('#klawoo_response').html('error!');
119 }
120 }
121 });
122 });
123 });
124 </script>
125 <?php
126 }
127
128 public function klawoo_subscribe() {
129 $url = 'http://app.klawoo.com/subscribe';
130
131 if( !empty( $_POST ) ) {
132 $params = $_POST;
133 } else {
134 exit();
135 }
136 $method = 'POST';
137 $qs = http_build_query( $params );
138
139 $options = array(
140 'timeout' => 15,
141 'method' => $method
142 );
143
144 if ( $method == 'POST' ) {
145 $options['body'] = $qs;
146 } else {
147 if ( strpos( $url, '?' ) !== false ) {
148 $url .= '&'.$qs;
149 } else {
150 $url .= '?'.$qs;
151 }
152 }
153
154 $response = wp_remote_request( $url, $options );
155 if ( wp_remote_retrieve_response_code( $response ) == 200 ) {
156 $data = $response['body'];
157 if ( $data != 'error' ) {
158
159 $message_start = substr( $data, strpos( $data,'<body>' ) + 6 );
160 $remove = substr( $message_start, strpos( $message_start,'</body>' ) );
161 $message = trim( str_replace( $remove, '', $message_start ) );
162 echo ( $message );
163 exit();
164 }
165 }
166 exit();
167 }
168
169 public function icegram_event_track() {
170
171 if( !empty( $_POST['event_data'] ) ) {
172
173 $messages_shown = (array) explode(",", html_entity_decode( (!empty($_COOKIE['icegram_messages_shown']) ? stripslashes( $_COOKIE['icegram_messages_shown'] ) : '') ) );
174
175 foreach ( $_POST['event_data'] as $event ) {
176 switch ($event['type']) {
177 case 'shown':
178 if (is_array($event['params']) && !empty($event['params']['message_id'])) {
179 $messages_shown[] = $event['params']['message_id'];
180 }
181 break;
182
183 default:
184 break;
185 }
186
187 // Emit event for other plugins to handle it
188 do_action('icegram_event_track', $event);
189 do_action('icegram_event_track_'.$event['type'], $event['params']);
190 }
191 }
192 $messages_shown = array_values ( array_filter ( array_unique($messages_shown) ) );
193 setcookie('icegram_messages_shown', implode(",", $messages_shown), 0, '/');
194 exit();
195 }
196
197 static function install() {
198 // Redirect to welcome screen
199 delete_option( '_icegram_activation_redirect' );
200 add_option( '_icegram_activation_redirect', 'pending' );
201 }
202
203 public function welcome() {
204
205 $this->db_update();
206 // Bail if no activation redirect transient is set
207 if ( false === get_option( '_icegram_activation_redirect' ) )
208 return;
209
210 // Delete the redirect transient
211 delete_option( '_icegram_activation_redirect' );
212
213 wp_redirect( admin_url( 'edit.php?post_type=ig_campaign&page=icegram-support' ) );
214 exit;
215 }
216
217 function db_update() {
218
219 $current_db_version = get_option( 'icegram_db_version' );
220 if ( !$current_db_version || version_compare( $current_db_version, '1.2', '<' ) ) {
221 include( 'updates/icegram-update-1.2.php' );
222 }
223
224 }
225
226 public function admin_menus() {
227
228 $welcome_page_title = __( 'Welcome to Icegram', 'icegram' );
229 $settings_page_title = __( 'Settings', 'icegram' );
230 $menu_title = __( 'Docs & Support', 'icegram' );
231 $about = add_submenu_page( 'edit.php?post_type=ig_campaign', $welcome_page_title, $menu_title, 'manage_options', 'icegram-support', array( $this, 'about_screen' ) );
232 $settings = add_submenu_page( 'edit.php?post_type=ig_campaign', $settings_page_title, $settings_page_title, 'manage_options', 'icegram-settings', array( $this, 'settings_screen' ) );
233
234 add_action( 'admin_print_styles-'. $about, array( $this, 'admin_css' ) );
235 add_action( 'admin_print_styles-'. $settings, array( $this, 'admin_css' ) );
236
237 }
238
239 public function admin_css() {
240 wp_enqueue_style( 'icegram-activation', $this->plugin_url . '/assets/css/admin.css' );
241 }
242
243 public function about_screen() {
244
245 // Import data if not done already
246 if( false === get_option( 'icegram_sample_data_imported' ) ) {
247 $this->import( $this->get_sample_data() );
248 }
249
250 include ( 'about-icegram.php' );
251 }
252
253 public function settings_screen() {
254 include ( 'settings.php' );
255 }
256
257 public function branding_data_remove( $icegram_branding_data ) {
258 if( !empty( $icegram_branding_data ) && 'yes' != get_option('icegram_share_love', 'no') ) {
259 $icegram_branding_data['powered_by_logo'] = '';
260 $icegram_branding_data['powered_by_text'] = '';
261 }
262 return $icegram_branding_data;
263 }
264
265 function execute_shortcode( $atts = array() ) {
266 // When shortcode is called, it will only prepare an array with conditions
267 // And add a placeholder div
268 // Display will happen in footer via display_messages()
269 $i = count($this->shortcode_instances);
270 //echo "Shortcode counter - {$i} - ";
271 $this->shortcode_instances[ $i ] = shortcode_atts( array(
272 'campaigns' => '',
273 'messages' => '',
274 'skip_others' => 'no'
275 ), $atts );
276 $html[] = "<div class='ig_shortcode_container' id='icegram_shortcode_{$i}'";
277 foreach ($atts as $key => $value) {
278 $html[] = " data-{$key}=\"".htmlentities($value)."\" ";
279 }
280 $html[] = " >"."</div>";
281 return implode(" ", $html);
282 }
283
284 function display_messages() {
285
286 $skip_others = $preview_mode = false;
287 $campaign_ids = $message_ids = array();
288
289 // Pull in message and campaign IDs from shortcodes - if set
290 if( !empty( $this->shortcode_instances ) ) {
291 foreach ($this->shortcode_instances as $i => $value) {
292 $cids = array_map( 'trim', (array) explode( ',', $value['campaigns'] ) );
293 $mids = array_map( 'trim', (array) explode( ',', $value['messages'] ) );
294 if ($value['skip_others'] == 'yes' && (!empty($cids) || !empty($mids))) {
295 $skip_others = true;
296 }
297 $campaign_ids = array_merge($campaign_ids, $cids);
298 $message_ids = array_merge($message_ids, $mids);
299 }
300 }
301
302 if( !empty( $_GET['campaign_preview_id'] ) && current_user_can( 'manage_options' ) ) {
303 $campaign_ids[] = $_GET['campaign_preview_id'];
304 $preview_mode = true;
305 }
306
307 $messages = $this->get_valid_messages( $message_ids, $campaign_ids, $preview_mode, $skip_others );
308
309 if( empty( $messages ) ) {
310 return;
311 }
312
313 $messages_shown = (array) explode(",", html_entity_decode( (!empty($_COOKIE['icegram_messages_shown']) ? stripslashes( $_COOKIE['icegram_messages_shown'] ) : '') ) );
314 $types_shown = array(); $messages_to_show_ids = array();
315
316 foreach ( $messages as $key => $message_data ) {
317
318 if( !is_array( $message_data ) || empty( $message_data ) ) {
319 continue;
320 }
321
322 // Don't show a seen message again - if needed
323 if( !empty( $message_data['id'] ) &&
324 empty( $_GET['campaign_preview_id'] ) &&
325 in_array( $message_data['id'], $messages_shown ) &&
326 !empty( $message_data['retargeting'] ) &&
327 $message_data['retargeting'] == 'yes'
328 ) {
329 unset( $messages[$key] );
330 continue;
331 }
332
333 // Avoid showing the same message twice
334 if (in_array($message_data['id'], $messages_to_show_ids)) {
335 unset ( $messages[$key] );
336 continue;
337 } else {
338 $messages_to_show_ids[] = $message_data['id'];
339 }
340
341 $types_shown[] = $message_data['type'];
342
343 // Our own implementation so WP does not mess with script, style and pre tags
344 add_filter('the_content', array( $this, 'before_wpautop' ) , 9);
345 add_filter('the_content', array( $this, 'after_wpautop' ) , 11);
346 $messages[$key]['message'] = apply_filters( 'the_content', $message_data['message'] );
347 remove_filter('the_content', array( $this, 'before_wpautop' ) , 9);
348 remove_filter('the_content', array( $this, 'after_wpautop' ) , 11);
349 }
350
351 if( empty( $messages ) )
352 return;
353
354 wp_register_script( 'icegram_js', $this->plugin_url . '/assets/js/icegram.js', array ( 'jquery' ), '', true);
355 wp_enqueue_style( 'icegram_css', $this->plugin_url . '/assets/css/frontend.css' );
356 wp_enqueue_style( 'dashicons' );
357
358 $icegram_default = apply_filters( 'icegram_branding_data',
359 array ( 'icon' => $this->plugin_url . '/assets/images/icegram-logo-branding-64-grey.png',
360 'powered_by_logo' => $this->plugin_url . '/assets/images/icegram-logo-branding-64-grey.png',
361 'powered_by_text' => __( 'Powered by Icegram', 'icegram' )
362 ) );
363 $messages = apply_filters( 'icegram_messages_to_show', $messages );
364 $icegram = array ( 'messages' => array_values( $messages ),
365 'ajax_url' => admin_url( 'admin-ajax.php' ),
366 'preview_id' => !empty( $_GET['campaign_preview_id'] ) ? $_GET['campaign_preview_id'] : '',
367 'defaults' => $icegram_default
368 );
369 if (empty($icegram['preview_id'])) {
370 unset($icegram['preview_id']);
371 }
372
373 if( !wp_script_is( 'icegram_js' ) ) {
374 wp_enqueue_script( 'icegram_js' );
375 wp_localize_script( 'icegram_js', 'icegram_data', $icegram );
376 }
377
378 // Load JS and default CSS
379 $types_shown = array_unique($types_shown);
380
381 if (in_array('popup', $types_shown)) {
382 wp_enqueue_script( 'thickbox' );
383 wp_enqueue_style( 'thickbox' );
384 }
385
386 foreach ($types_shown as $message_type) {
387 wp_register_script( 'icegram_message_type_'.$message_type, $this->message_types[$message_type]['baseurl'] . "main.js" , array ( 'icegram_js' ), '', true );
388 wp_enqueue_script( 'icegram_message_type_'.$message_type );
389 wp_enqueue_style( 'icegram_css_'.$message_type, $this->message_types[$message_type]['baseurl'] . 'default.css' );
390 }
391
392 // Load theme CSS
393 foreach ($messages as $message) {
394 if (!empty( $this->message_types[ $message['type'] ]['themes'][ $message['theme'] ]) ) {
395 $theme = $this->message_types[ $message['type'] ]['themes'][ $message['theme'] ];
396 wp_enqueue_style( 'icegram_css_'.$message['type'].'_'.$message['theme'], $theme['baseurl'] . $message['theme'].'.css' );
397 }
398 }
399
400 }
401
402 function enqueue_admin_styles_and_scripts() {
403
404 $screen = get_current_screen();
405 if ( !in_array( $screen->id, array( 'ig_campaign', 'ig_message' ), true ) ) return;
406
407 // Register scripts
408 wp_register_script( 'icegram_writepanel', $this->plugin_url . '/assets/js/admin.js' , array ( 'jquery', 'wp-color-picker' ) );
409 wp_register_script( 'icegram_chosen', $this->plugin_url . '/assets/js/chosen.jquery.min.js' , array ( 'jquery' ), '1.0' );
410 wp_register_script( 'icegram_ajax-chosen', $this->plugin_url . '/assets/js/ajax-chosen.jquery.min.js' , array ( 'icegram_chosen' ), '1.0' );
411 wp_register_script( 'icegram_tiptip', $this->plugin_url . '/assets/js/jquery.tipTip.min.js' , array ( 'jquery' ), get_bloginfo( 'version' ) );
412
413 wp_enqueue_script( 'icegram_writepanel' );
414 wp_enqueue_script( 'icegram_ajax-chosen' );
415 wp_enqueue_script( 'icegram_tiptip' );
416 wp_enqueue_script( 'thickbox' );
417
418 $icegram_writepanel_params = array ( 'ajax_url' => admin_url( 'admin-ajax.php' ), 'search_message_nonce' => wp_create_nonce( "search-messages" ) );
419 $this->available_headlines = apply_filters( 'icegram_available_headlines', array() );
420 $icegram_writepanel_params = array_merge( $icegram_writepanel_params, array( 'available_headlines' => $this->available_headlines ) );
421
422 wp_localize_script( 'icegram_writepanel', 'icegram_writepanel_params', $icegram_writepanel_params );
423
424 wp_enqueue_style( 'thickbox' );
425 wp_enqueue_style( 'dashicons' );
426 wp_enqueue_style( 'wp-color-picker' );
427 wp_enqueue_style( 'icegram_admin_styles', $this->plugin_url . '/assets/css/admin.css' );
428 wp_enqueue_style( 'icegram_jquery-ui-style', $this->plugin_url . '/assets/css/jquery-ui.min.css' );
429 wp_enqueue_style( 'icegram_chosen_styles', $this->plugin_url . '/assets/css/chosen.min.css' );
430
431 if ( !wp_script_is( 'jquery-ui-datepicker' ) ) {
432 wp_enqueue_script( 'jquery-ui-datepicker' );
433 }
434
435 }
436
437 public static function get_platform() {
438 $platform = '';
439 $user_agent = trim(strtolower($_SERVER['HTTP_USER_AGENT']));
440 $pattern = '/(android\s\d|blackberry|ip(hone|ad|od)|iemobile|webos|palm|symbian|kindle|windows|win64|wow64|macintosh|intel\smac\sos\sx|ppx\smac\sos\sx|googlebot|googlebot-mobile)/';
441 if ( preg_match( $pattern, $user_agent, $matches ) ) {
442 $platform = $matches[0];
443 }
444
445 switch ( $platform ) {
446
447 /* phones / smartphones */
448 case 'android 1':
449 case 'android 2':
450 case 'blackberry':
451 case 'iphone':
452 case 'ipod':
453 case 'iemobile':
454 case 'webos':
455 case 'palm':
456 case 'symbian':
457 case 'googlebot-mobile':
458 $platform = 'mobile';
459 break;
460
461 /* tablets */
462 case 'android 3':
463 case 'android 4':
464 case 'ipad':
465 case 'kindle':
466 $platform = 'tablet';
467 break;
468
469 /* desktops / laptops */
470 case 'windows':
471 case 'win64':
472 case 'wow64':
473 case 'macintosh':
474 case 'ppx mac os x':
475 case 'intel mac os x':
476 case 'googlebot':
477 $platform = 'laptop';
478 break;
479
480 /* in case nothing else matches */
481 default:
482 $platform = 'laptop';
483 break;
484 }
485 return $platform;
486 }
487
488 function get_message_data( $message_ids = array(), $preview = false ) {
489 global $wpdb;
490
491 $message_data = array();
492 $meta_key = $preview ? 'icegram_message_preview_data' : 'icegram_message_data';
493 $message_data_query = "SELECT post_id, meta_value FROM {$wpdb->prefix}postmeta WHERE meta_key LIKE '$meta_key'";
494 if ( !empty( $message_ids ) && is_array( $message_ids ) ) {
495 // For WPML compatibility
496 if ( function_exists('icl_object_id') ) {
497 foreach ($message_ids as $i=>$id ) {
498 $message_ids[ $i ] = icl_object_id( $id, 'ig_message', true );
499 }
500 }
501 $message_data_query .= " AND post_id IN ( " . implode( ',', $message_ids ) . " )";
502 }
503
504 $message_data_results = $wpdb->get_results( $message_data_query, 'ARRAY_A' );
505 $message_data = array();
506 foreach ( $message_data_results as $message_data_result ) {
507 $message_data[$message_data_result['post_id']] = maybe_unserialize( $message_data_result['meta_value'] );
508 }
509 return $message_data;
510 }
511
512 function get_valid_messages( $message_ids = array(), $campaign_ids = array(), $preview_mode = false, $skip_others = false) {
513
514 $valid_messages = $valid_campaigns = $message_campaign_map = array();
515
516 if ( !empty( $campaign_ids ) ) {
517 $valid_campaigns = $this->get_valid_campaigns( $campaign_ids, true );
518 }
519 // When skip_others is true, we won't load campaigns / messages from db
520 if (!$skip_others) {
521 $campaigns = $this->get_valid_campaigns();
522 if (!empty($campaigns)) {
523 foreach ($campaigns as $id => $campaign) {
524 if (!array_key_exists($id, $valid_campaigns)) {
525 $valid_campaigns[ $id ] = $campaign;
526 }
527 }
528 }
529 }
530
531 // Create a map to look up campaign id for a given message
532 if( !empty( $valid_campaigns ) ) {
533 foreach ($valid_campaigns as $id => $campaign) {
534 if ($preview_mode) {
535 $campaign->messages = get_post_meta( $id, 'campaign_preview', true );
536 }
537
538 foreach( $campaign->messages as $msg) {
539 $message_ids[] = $msg['id'];
540 if (!array_key_exists( $msg['id'], $message_campaign_map)) {
541 $message_campaign_map[ $msg['id'] ] = $id;
542 }
543 }
544 }
545 }
546
547 // We don't display same message twice...
548 $message_ids = array_unique($message_ids);
549
550 if( empty( $message_ids ) ) {
551 return array();
552 }
553 $valid_messages = $this->get_message_data( $message_ids, $preview_mode );
554
555 foreach ($valid_messages as $id => $message_data) {
556 // Remove message if message type is uninstalled
557 $class_name = 'Icegram_Message_Type_' . str_replace(' ', '_', ucwords(str_replace('-', ' ', $message_data['type'])));
558 if( !class_exists( $class_name ) ) {
559 unset( $valid_messages[$id] );
560 continue;
561 }
562 $message_data['delay_time'] = 0;
563 $message_data['retargeting'] = '';
564 $message_data['campaign_id'] = ($preview_mode) ? $_GET['campaign_preview_id'] : '';
565
566 // Pull display time and retargeting rule from campaign if possible
567 if (!empty($message_campaign_map[ $id ])) {
568 $message_data['campaign_id'] = $message_campaign_map[ $id ];
569 $campaign = $valid_campaigns[ $message_data['campaign_id'] ];
570 if (!empty($campaign) && $campaign instanceof Icegram_Campaign) {
571 $message_meta_from_campaign = $campaign->get_message_meta_by_id( $id );
572 if (!empty($message_meta_from_campaign['time'])) {
573 $message_data['delay_time'] = $message_meta_from_campaign['time'];
574 }
575 $rule_value = $campaign->get_rule_value('retargeting');
576 $message_data['retargeting'] = !empty( $rule_value['retargeting'] ) ? $rule_value['retargeting'] : '';
577 }
578 }
579 $valid_messages[$id] = $message_data;
580 }
581
582 $valid_messages = apply_filters( 'icegram_valid_messages', $valid_messages );
583 return $valid_messages;
584 }
585
586 function get_valid_campaigns( $campaign_ids = array(), $skip_page_check = false ) {
587 global $wpdb;
588
589 if ( empty( $campaign_ids ) ) {
590 $sql = "SELECT pm.post_id
591 FROM {$wpdb->prefix}posts AS p
592 LEFT JOIN {$wpdb->prefix}postmeta AS pm ON ( pm.post_id = p.ID )
593 WHERE p.post_status = 'publish' ";
594 // Filter handler within this file (and possibly others) will append to this SQL
595 // and provide arguments for wpdb->prepare if needed.
596 // First element in the array is SQL, remaining are values for placeholders in SQL
597 $sql_params = apply_filters( 'icegram_get_valid_campaigns_sql', array($sql), array() );
598 $campaign_ids = $wpdb->get_col( $wpdb->prepare( array_shift($sql_params), $sql_params ) );
599 }
600 $valid_campaigns = array();
601 foreach ( (array) $campaign_ids as $campaign_id ) {
602 $campaign = new Icegram_Campaign( $campaign_id );
603 if ( $campaign->is_valid( array('skip_page_check' => $skip_page_check) ) ) {
604 $valid_campaigns[$campaign_id] = $campaign;
605 } else {
606 // Campgain is invalid!
607 }
608 }
609 return $valid_campaigns;
610 }
611
612 function append_to_valid_campaigns_sql( $sql_params = array(), $options = array() ) {
613
614 // Page check conditions
615 $pid = Icegram::get_current_page_id();
616 $sql = " AND (
617 pm.meta_key = 'icegram_campaign_target_rules' AND (
618 ( pm.meta_value LIKE '%%%s%%' )
619 OR ( pm.meta_value LIKE '%%%s%%' AND pm.meta_value LIKE '%%%s%%' AND pm.meta_value LIKE '%%%s%%' )
620 ";
621 $sql_params[] = 's:8:"sitewide";s:3:"yes";';
622 $sql_params[] = 's:10:"other_page";s:3:"yes";';
623 $sql_params[] = 's:7:"page_id";a:1:{';
624 $sql_params[] = serialize( (string) $pid );
625
626 if (is_home() || is_front_page()) {
627 $sql .= " OR ( pm.meta_value LIKE '%%%s%%' )
628 ";
629 $sql_params[] = 's:8:"homepage";s:3:"yes";';
630 }
631 $sql .=" ) )";
632 $sql_params[0] .= $sql;
633
634 //s:9:"logged_in";s:3:"all";
635
636 return $sql_params;
637 }
638
639 // Include all classes required for Icegram plugin
640 function include_classes() {
641
642 $classes = glob( $this->plugin_path . '/classes/*.php' );
643 foreach ( $classes as $file ) {
644 // Files with 'admin' in their name are included only for admin section
645 if ( is_file( $file ) && ( (strpos($file, '-admin') >= 0 && is_admin()) || (strpos($file, '-admin') === false) ) ) {
646 include_once $file;
647 }
648 }
649
650 // Load built in message types
651 $icegram_message_type_basedirs = glob( $this->plugin_path . '/message-types/*' );
652 // Allow other plugins to add new message types
653 $icegram_message_type_basedirs = apply_filters( 'icegram_message_type_basedirs', $icegram_message_type_basedirs );
654 // Set up different message type classes
655 foreach ( $icegram_message_type_basedirs as $dir ) {
656 $type = basename ( $dir );
657 $class_file = $dir . "/main.php";
658 if( is_file( $class_file ) ) {
659 include_once( $class_file );
660 }
661 $class_name = 'Icegram_Message_Type_' . str_replace(' ', '_', ucwords(str_replace('-', ' ', $type)));
662 if (class_exists($class_name)) {
663 $this->message_type_objs[ $type ] = new $class_name();
664 }
665 }
666 $this->message_types = apply_filters( 'icegram_message_types', array() );
667
668 }
669
670 // Register Campaign post type
671 function register_campaign_post_type() {
672 $labels = array(
673 'name' => __( 'Campaigns', 'icegram' ),
674 'singular_name' => __( 'Campaign', 'icegram' ),
675 'add_new' => __( 'Add New Campaign', 'icegram' ),
676 'add_new_item' => __( 'Add New Campaign', 'icegram' ),
677 'edit_item' => __( 'Edit Campaign', 'icegram' ),
678 'new_item' => __( 'New Campaign', 'icegram' ),
679 'all_items' => __( 'Campaigns', 'icegram' ),
680 'view_item' => __( 'View Campaign', 'icegram' ),
681 'search_items' => __( 'Search Campaigns', 'icegram' ),
682 'not_found' => __( 'No campaigns found', 'icegram' ),
683 'not_found_in_trash' => __( 'No campaigns found in Trash', 'icegram' ),
684 'parent_item_colon' => __( '', 'icegram' ),
685 'menu_name' => __( 'Icegram', 'icegram' )
686 );
687
688 $args = array(
689 'labels' => $labels,
690 'menu_icon' => 'dashicons-info',
691 'public' => true,
692 'publicly_queryable' => true,
693 'show_ui' => true,
694 'show_in_menu' => true,
695 'query_var' => true,
696 'rewrite' => array( 'slug' => 'ig_campaign' ),
697 'capability_type' => 'post',
698 'has_archive' => true,
699 'hierarchical' => false,
700 'menu_position' => null,
701 'supports' => array( 'title', 'editor' )
702 );
703
704 register_post_type( 'ig_campaign', $args );
705 }
706
707 // Register Message post type
708 function register_message_post_type() {
709 $labels = array(
710 'name' => __( 'Messages', 'icegram' ),
711 'singular_name' => __( 'Message', 'icegram' ),
712 'add_new' => __( 'Create New', 'icegram' ),
713 'add_new_item' => __( 'Create New Message', 'icegram' ),
714 'edit_item' => __( 'Edit Message', 'icegram' ),
715 'new_item' => __( 'New Message', 'icegram' ),
716 'all_items' => __( 'Messages', 'icegram' ),
717 'view_item' => __( 'View Message', 'icegram' ),
718 'search_items' => __( 'Search Messages', 'icegram' ),
719 'not_found' => __( 'No messages found', 'icegram' ),
720 'not_found_in_trash' => __( 'No messages found in Trash', 'icegram' ),
721 'parent_item_colon' => __( '', 'icegram' ),
722 'menu_name' => __( 'Messages', 'icegram' )
723 );
724
725 $args = array(
726 'labels' => $labels,
727 'public' => true,
728 'publicly_queryable' => true,
729 'show_ui' => true,
730 'show_in_menu' => 'edit.php?post_type=ig_campaign',
731 'query_var' => true,
732 'rewrite' => array( 'slug' => 'ig_message' ),
733 'capability_type' => 'post',
734 'has_archive' => true,
735 'hierarchical' => false,
736 'menu_position' => null,
737 'supports' => array( 'title' )
738 );
739
740 register_post_type( 'ig_message', $args );
741 }
742
743 function import( $data = array() ) {
744
745 if ( empty( $data['campaigns'] ) && empty( $data['messages'] ) ) return;
746
747 $default_theme = $default_type = '';
748 $first_message_type = current( $this->message_types );
749
750 if( is_array( $first_message_type ) ) {
751 $default_type = $first_message_type['type'];
752 if( !empty( $first_message_type['themes'] ) ) {
753 $default_theme = key( $first_message_type['themes'] );
754 }
755 }
756
757 $new_campaign_ids = array();
758 foreach ( (array) $data['campaigns'] as $campaign ) {
759
760 $args = array(
761 'post_content' => ( !empty( $campaign['post_content'] ) ) ? esc_attr( $campaign['post_content'] ) : '',
762 'post_name' => ( !empty( $campaign['post_title'] ) ) ? sanitize_title( $campaign['post_title'] ) : '',
763 'post_title' => ( !empty( $campaign['post_title'] ) ) ? $campaign['post_title'] : '',
764 'post_status' => ( !empty( $campaign['post_status'] ) ) ? $campaign['post_status'] : 'draft',
765 'post_type' => 'ig_campaign'
766 );
767
768 $new_campaign_id = wp_insert_post( $args );
769 $new_campaign_ids[] = $new_campaign_id;
770
771 if ( !empty( $campaign['target_rules'] ) ) {
772
773 $defaults = array (
774 'homepage' => 'yes',
775 'when' => 'always',
776 'from' => '',
777 'to' => '',
778 'mobile' => 'yes',
779 'tablet' => 'yes',
780 'laptop' => 'yes',
781 'logged_in' => 'all'
782 );
783
784 $target_rules = wp_parse_args( $campaign['target_rules'], $defaults );
785 update_post_meta( $new_campaign_id, 'icegram_campaign_target_rules', $target_rules );
786 }
787
788 if ( !empty( $campaign['messages'] ) ) {
789
790 $messages = array();
791
792 foreach ( $campaign['messages'] as $message ) {
793
794 if ( !is_array( $message ) ) continue;
795
796 $args = array(
797 'post_content' => ( !empty( $message['message'] ) ) ? esc_attr( $message['message'] ) : '',
798 'post_name' => ( !empty( $message['post_title'] ) ) ? sanitize_title( $message['post_title'] ) : '',
799 'post_title' => ( !empty( $message['post_title'] ) ) ? $message['post_title'] : '',
800 'post_status' => ( !empty( $message['post_status'] ) ) ? $message['post_status'] : 'publish',
801 'post_type' => 'ig_message'
802 );
803
804 $new_message_id = wp_insert_post( $args );
805 $new_message = array(
806 'id' => $new_message_id,
807 'time' => ( !empty( $message['time'] ) ) ? $message['time'] : 0
808 );
809 $messages[] = $new_message;
810
811 unset( $message['post_content'] );
812 unset( $message['time'] );
813
814 $message['id'] = $new_message_id;
815
816 $defaults = array (
817 'post_title' => '',
818 'type' => $default_type,
819 'theme' => $default_theme,
820 'animation' => '',
821 'headline' => '',
822 'label' => '',
823 'link' => '',
824 'icon' => '',
825 'message' => '',
826 'position' => '',
827 'text_color' => '#000000',
828 'bg_color' => '#ffffff',
829 'id' => ''
830 );
831
832 $icegram_message_data = wp_parse_args( $message, $defaults );
833
834 if ( !empty( $icegram_message_data ) ) {
835 update_post_meta( $new_message_id, 'icegram_message_data', $icegram_message_data );
836 update_post_meta( $new_message_id, 'icegram_message_preview_data', $icegram_message_data );
837 }
838 }
839
840 if ( !empty( $campaign['messages'] ) ) {
841 update_post_meta( $new_campaign_id, 'messages', $messages );
842 update_post_meta( $new_campaign_id, 'campaign_preview', $messages );
843 }
844
845 }
846 }
847 if( !empty( $new_campaign_ids ) ) {
848 update_option( 'icegram_sample_data_imported', $new_campaign_ids );
849 }
850
851 }
852
853 function get_sample_data() {
854
855 return array(
856 'campaigns' => array(
857 array(
858 'post_name' => '',
859 'post_title' => 'My First Icegram Campaign',
860 'target_rules' => array (
861 'homepage' => 'yes',
862 'when' => 'always',
863 'from' => '',
864 'to' => '',
865 'mobile' => 'yes',
866 'tablet' => 'yes',
867 'laptop' => 'yes',
868 'logged_in' => 'all'
869 ),
870 'messages' => array(
871 array (
872 'post_title' => 'Get 2x more Contacts with Your Website',
873 'post_status' => 'publish',
874 'time' => '0',
875 'type' => 'action-bar',
876 'theme' => 'hello',
877 'headline' => 'Get 2x more Contacts with Your Website',
878 'label' => 'Show Me How',
879 'link' => '',
880 'icon' => '',
881 'message' => 'Instant Results Guaranteed',
882 'position' => '01',
883 'text_color' => '#000000',
884 'bg_color' => '#eb593c'
885 ),
886 array (
887 'post_title' => '20% Off Coupon',
888 'post_status' => 'publish',
889 'time' => '4',
890 'type' => 'messenger',
891 'theme' => 'social',
892 'animation' => 'slide',
893 'headline' => '20% Off - for you',
894 'label' => '',
895 'link' => '',
896 'icon' => '',
897 'message' => "Hey there! We are running a <strong>special 20% off this week</strong> for registered users - like you.
898
899 Use coupon <code>LOYALTY20</code> during checkout.",
900 'position' => '22',
901 'text_color' => '#000000',
902 'bg_color' => '#ffffff'
903 ),
904 array (
905 'post_title' => 'How this blog makes over $34,800 / month for FREE.',
906 'post_status' => 'publish',
907 'time' => '10',
908 'type' => 'popup',
909 'theme' => 'air-mail',
910 'headline' => 'How this blog makes over $34,800 / month for FREE.',
911 'label' => 'FREE INSTANT ACCESS',
912 'link' => '',
913 'icon' => '',
914 '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.
915
916 <strong>Download my free kit to learn how I do this.</strong>
917
918 <ul>
919 <li>How to choose blog topics that create long term value</li>
920 <li>The type of blog post that will make your site go viral</li>
921 <li>How to free yourself from the routine tasks</li>
922 <li>Resources and tips to get started quickly</li>
923 <li>Private members club to connect with fellow owners</li>
924 </ul>",
925 'text_color' => '#000000',
926 'bg_color' => '#ffffff'
927
928 ),
929 array (
930 'post_title' => 'Exclusive Marketing Report',
931 'post_status' => 'publish',
932 'time' => '6',
933 'type' => 'toast',
934 'theme' => 'stand-out',
935 'animation' => 'pop',
936 'headline' => 'Exclusive Marketing Report',
937 'label' => '',
938 'link' => '',
939 'icon' => '',
940 'message' => 'FREE for every subscriber. Click here to download it.',
941 'position' => '02',
942 'text_color' => '#000000',
943 'bg_color' => '#ffffff'
944 )
945
946 )
947 )
948 )
949 );
950 }
951
952 function remove_preview_button() {
953 global $post_type;
954
955 if( $post_type == 'ig_message' || $post_type == 'ig_campaign' ) {
956
957 ?>
958 <style>
959 #preview-action { display:none; }
960 </style>
961 <?php
962
963 }
964
965 }
966
967 function remove_row_actions( $actions, $post ) {
968
969 if ( empty( $post->post_type ) || ( $post->post_type != 'ig_campaign' && $post->post_type != 'ig_message' ) ) return $actions;
970
971 unset( $actions['inline hide-if-no-js'] );
972 unset( $actions['view'] );
973
974 return $actions;
975
976 }
977
978 function identify_current_page() {
979 global $post;
980 if( is_object( $post ) && isset( $post->ID ) ) {
981 $id = $post->ID;
982 $id = apply_filters('icegram_identify_current_page', $id );
983 self::$current_page_id = $id;
984 }
985 }
986
987 static function get_current_page_id() {
988 return self::$current_page_id;
989 }
990
991 function wpml_get_parent_id( $id ) {
992 global $post;
993 if (function_exists('icl_object_id') && function_exists('icl_get_default_language') ) {
994 $id = icl_object_id($id, $post->post_type, true, icl_get_default_language() );
995 }
996 return $id;
997 }
998
999
1000 /**
1001 * Our implementation of wpautop to preserve script and style tags
1002 */
1003 function before_wpautop($pee) {
1004 if ( trim($pee) === '' ) {
1005 $this->_wpautop_tags = array();
1006 return '';
1007 }
1008
1009 $tags = array();
1010 // Pull out tags and add placeholders
1011 list( $pee, $tags['pre'] ) = $this->_wpautop_add_tag_placeholders( $pee, 'pre' );
1012 list( $pee, $tags['script'] ) = $this->_wpautop_add_tag_placeholders( $pee, 'script' );
1013 list( $pee, $tags['style'] ) = $this->_wpautop_add_tag_placeholders( $pee, 'style' );
1014 $this->_wpautop_tags = $tags;
1015
1016 if( !empty( $pre_tags ) )
1017 $pee = $this->_wpautop_replace_tag_placeholders( $pee, $pre_tags );
1018 if( !empty( $script_tags ) )
1019 $pee = $this->_wpautop_replace_tag_placeholders( $pee, $script_tags );
1020 if( !empty( $style_tags ) )
1021 $pee = $this->_wpautop_replace_tag_placeholders( $pee, $style_tags );
1022
1023 return $pee;
1024 }
1025
1026 function after_wpautop($pee) {
1027 if ( trim($pee) === '' || empty($this->_wpautop_tags) )
1028 return '';
1029
1030 // Replace placeholders with original content
1031 if (!empty($this->_wpautop_tags['pre'])) {
1032 $pee = $this->_wpautop_replace_tag_placeholders( $pee, $this->_wpautop_tags['pre'] );
1033 }
1034 if (!empty($this->_wpautop_tags['script'])) {
1035 $pee = $this->_wpautop_replace_tag_placeholders( $pee, $this->_wpautop_tags['script'] );
1036 }
1037 if (!empty($this->_wpautop_tags['style'])) {
1038 $pee = $this->_wpautop_replace_tag_placeholders( $pee, $this->_wpautop_tags['style'] );
1039 }
1040
1041 $this->_wpautop_tags = array();
1042
1043 return $pee;
1044 }
1045
1046 function _wpautop_add_tag_placeholders( $pee, $tag ) {
1047 $tags = array();
1048
1049 if ( false !== strpos( $pee, "<{$tag}" ) ) {
1050 $pee_parts = explode( "</{$tag}>", $pee );
1051 $last_pee = array_pop( $pee_parts );
1052 $pee = '';
1053 $i = 0;
1054
1055 foreach ( $pee_parts as $pee_part ) {
1056 $start = strpos( $pee_part, "<{$tag}" );
1057
1058 // Malformed html?
1059 if ( false === $start ) {
1060 $pee .= $pee_part;
1061 continue;
1062 }
1063
1064 $name = "<{$tag} wp-{$tag}-tag-$i></{$tag}>";
1065 $tags[ $name ] = substr( $pee_part, $start ) . "</{$tag}>";
1066
1067 $pee .= substr( $pee_part, 0, $start ) . $name;
1068 $i++;
1069 }
1070
1071 $pee .= $last_pee;
1072 }
1073
1074 return array( $pee, $tags );
1075 }
1076
1077 function _wpautop_replace_tag_placeholders( $pee, $tags ) {
1078 if ( ! empty( $tags ) ) {
1079 $pee = str_replace( array_keys( $tags ), array_values( $tags ), $pee );
1080 }
1081
1082 return $pee;
1083 }
1084
1085 }
1086
1087 function initialize_icegram() {
1088 global $icegram;
1089
1090 // i18n / l10n - load translations
1091 load_plugin_textdomain( 'icegram', false, dirname( plugin_basename( __FILE__ ) ) . '/lang/' );
1092
1093 $icegram = new Icegram();
1094 }
1095
1096 add_action( 'plugins_loaded', 'initialize_icegram' );
1097 register_activation_hook( __FILE__, array( 'Icegram', 'install' ) );