/**
  * Activation hook
  *
  * Create tables if they don't exist and add plugin options
  *
  * @see     http://codex.wordpress.org/Function_Reference/register_activation_hook
  *
  * @global	object	$wpdb
  */
 public static function run()
 {
     global $wpdb;
     /**
      * WordPress database upgrade/creation functions
      */
     require_once ABSPATH . 'wp-admin/includes/upgrade.php';
     // Get the correct character collate
     if (!empty($wpdb->charset)) {
         $charset_collate = "DEFAULT CHARACTER SET {$wpdb->charset}";
     }
     if (!empty($wpdb->collate)) {
         $charset_collate .= " COLLATE {$wpdb->collate}";
     }
     $sql_main = "CREATE TABLE IF NOT EXISTS " . Eab_EventsHub::tablename(Eab_EventsHub::BOOKING_TABLE) . " (\n\t\t\t\t`id` BIGINT NOT NULL AUTO_INCREMENT,\n\t            `event_id` BIGINT NOT NULL ,\n\t            `user_id` BIGINT NOT NULL ,\n\t\t\t\t`timestamp` TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00' ,\n\t\t\t\t`status` ENUM( 'paid', 'yes', 'maybe', 'no' ) NOT NULL DEFAULT 'no' ,\n\t\t    \t\tPRIMARY KEY (`id`),\n\t\t\t\tUNIQUE KEY `event_id_2` (`event_id`,`user_id`),\n\t\t\t\tKEY `event_id` (`event_id`),\n\t\t\t\tKEY `user_id` (`user_id`),\n\t\t\t\tKEY `timestamp` (`timestamp`),\n\t\t\t\tKEY `status` (`status`)\n\t\t\t    ) ENGINE = InnoDB {$charset_collate};";
     dbDelta($sql_main);
     $sql_main = "CREATE TABLE IF NOT EXISTS " . Eab_EventsHub::tablename(Eab_EventsHub::BOOKING_META_TABLE) . " (\n\t\t\t\t`id` BIGINT NOT NULL AUTO_INCREMENT,\n\t\t\t\t`booking_id` BIGINT NOT NULL ,\n\t            `meta_key` VARCHAR(255) NOT NULL ,\n\t            `meta_value` TEXT NOT NULL,\n\t\t    \t\tPRIMARY KEY (`id`),\n\t\t\t\tKEY `booking_id` (`booking_id`),\n\t\t\t\tKEY `meta_key` (`meta_key`)\n\t\t\t    ) ENGINE = InnoDB {$charset_collate};";
     // MySQL strict mode fix, thanks @KJA!
     dbDelta($sql_main);
     if (!get_option('event_default', false)) {
         add_option('event_default', array());
     }
     if (!get_option('eab_activation_redirect', true)) {
         add_option('eab_activation_redirect', true);
     }
 }
 public function notify_rsvp_on_update_event($post_id, $post)
 {
     if (!current_user_can('edit_posts')) {
         return false;
     }
     if (wp_is_post_revision($post_id)) {
         return;
     }
     if ('incsub_event' == get_post_type($post_id)) {
         global $wpdb;
         $event = $post instanceof Eab_EventModel ? $post : new Eab_EventModel($post);
         $all_events = array($event);
         if ($event->is_recurring()) {
             $all_events = Eab_CollectionFactory::get_all_recurring_children_events($event);
         }
         $all_event_ids = array();
         foreach ($all_events as $e) {
             $all_event_ids[] = $e->get_id();
         }
         $all_event_ids = array_filter(array_map('intval', $all_event_ids));
         $bookings = $wpdb->get_results("SELECT id,user_id,event_id FROM " . Eab_EventsHub::tablename(Eab_EventsHub::BOOKING_TABLE) . " WHERE event_id IN(" . join(',', $all_event_ids) . ") ORDER BY timestamp;");
         if (!count($bookings)) {
             return false;
         }
         $eab_alert = $this->_data->get_option('eab_alert');
         $start_dates = $event->get_start_dates();
         foreach ($start_dates as $key => $date) {
             $start = $event->get_start_timestamp($key);
             $end = $event->get_end_timestamp($key);
         }
         foreach ($bookings as $booking) {
             $user_data = get_userdata($booking->user_id);
             $subject = str_replace(array('DISPLAY_NAME', 'EVENT_NAME', 'START_DATE', 'START_TIME', 'END_DATE', 'END_TIME'), array($user_data->display_name, $event->get_title(), date('Y-m-d', $start), date('H:i', $start), date('Y-m-d', $end), date('H:i', $end)), $eab_alert['subject']);
             $content = str_replace(array('DISPLAY_NAME', 'EVENT_NAME', 'START_DATE', 'START_TIME', 'END_DATE', 'END_TIME'), array($user_data->display_name, $event->get_title(), date('Y-m-d', $start), date('H:i', $start), date('Y-m-d', $end), date('H:i', $end)), $eab_alert['content']);
             add_filter('wp_mail_content_type', array($this, 'set_content_type'));
             wp_mail($user_data->user_email, $subject, nl2br($content));
             remove_filter('wp_mail_content_type', array($this, 'set_content_type'));
         }
     }
 }
 function handle_rsvp_form($content, $event = false)
 {
     $post_id = false;
     if ($event && $event instanceof Eab_EventModel) {
         $post_id = $event->get_id();
     } else {
         global $post;
         $post_id = (int) @$post->ID;
     }
     $capacity = (int) get_post_meta($post_id, 'eab_capacity', true);
     if (!$capacity) {
         return $content;
     }
     // No capacity set, we're good to show
     $total = $this->_get_event_total_attendance($post_id);
     global $wpdb, $current_user;
     $users = $wpdb->get_col("SELECT user_id FROM " . Eab_EventsHub::tablename(Eab_EventsHub::BOOKING_TABLE) . " WHERE event_id={$post_id} AND status='yes';");
     if ($capacity > $total) {
         $remaining = $this->get_remaining_capacity(0, $post_id);
         $content .= $this->_data->get_option('eab-limit_capacity-show_remaining') ? '<span class="eab-limit_capacity-remaining">' . apply_filters('eab-rsvps-event_capacity_remaining-message', sprintf(__('%d seats left', Eab_EventsHub::TEXT_DOMAIN), $remaining), $post_id, $remaining) . '</span>' : '';
         return $content;
     }
     return in_array($current_user->id, $users) ? $content : $this->_get_overbooked_message($post_id);
     /*
     		if ($capacity > $total) return $content;
     		return (in_array($current_user->id, $users)) ? $content : $this->_get_overbooked_message();
     */
 }
 public static function get_admin_bookings($status, $post)
 {
     global $wpdb;
     if (!current_user_can('edit_posts')) {
         return false;
     }
     // Basic sanity check
     $event = $post instanceof Eab_EventModel ? $post : new Eab_EventModel($post);
     $statuses = self::get_rsvp_status_list();
     if (!in_array($status, array_keys($statuses))) {
         return false;
     }
     // Unknown status
     $status_name = $statuses[$status];
     //$content = Eab_Template::get_admin_attendance_addition_form($event, $statuses); // Moved to actual bookings areas
     $content .= '<h4>' . __($status_name, Eab_EventsHub::TEXT_DOMAIN) . '</h4>';
     $content .= '<ul class="eab-guest-list">';
     $all_events = array($event);
     if ($event->is_recurring()) {
         $all_events = Eab_CollectionFactory::get_all_recurring_children_events($event);
     }
     $all_event_ids = array();
     foreach ($all_events as $e) {
         $all_event_ids[] = $e->get_id();
     }
     $all_event_ids = array_filter(array_map('intval', $all_event_ids));
     $bookings = $wpdb->get_results($wpdb->prepare("SELECT id,user_id,event_id FROM " . Eab_EventsHub::tablename(Eab_EventsHub::BOOKING_TABLE) . " WHERE event_id IN(" . join(',', $all_event_ids) . ") AND status = %s ORDER BY timestamp;", $status));
     if (!count($bookings)) {
         return false;
     }
     foreach ($bookings as $booking) {
         $user_data = get_userdata($booking->user_id);
         if ($event->get_id() !== $booking->event_id) {
             $event = new Eab_EventModel(get_post($booking->event_id));
         }
         $content .= '<li>';
         $content .= '<div class="eab-guest">';
         $content .= '<a href="user-edit.php?user_id=' . $booking->user_id . '" title="' . $user_data->display_name . '">' . get_avatar($booking->user_id, 32) . '<br />' . apply_filters('eab-guest_list-admin-guest_name', $user_data->display_name, $booking->user_id, $user_data) . '</a>';
         if ($event->is_premium()) {
             if ($event->user_paid($booking->user_id)) {
                 $ticket_count = $event->get_booking_meta($booking->id, 'booking_ticket_count');
                 $ticket_count = $ticket_count ? $ticket_count : 1;
                 $payment_status = '' . '<span class="eab-guest-payment_info-paid">' . __('Paid', Eab_EventsHub::TEXT_DOMAIN) . '</span>' . '&nbsp;' . sprintf(__('(%s tickets)', Eab_EventsHub::TEXT_DOMAIN), $ticket_count) . '';
             } else {
                 $payment_status = '<span class="eab-guest-payment_info-not_paid">' . __('Not paid', Eab_EventsHub::TEXT_DOMAIN) . '</span>';
             }
             // Added by Hakan
             $payment_status = apply_filters('eab-event-payment_status', $payment_status, $booking->user_id, $event);
             $content .= "<div class='eab-guest-payment_info'>{$payment_status}</div>";
         }
         if (in_array($status, array(Eab_EventModel::BOOKING_YES, Eab_EventModel::BOOKING_MAYBE))) {
             $content .= '<div class="eab-guest-actions"><a href="#cancel-attendance" class="eab-guest-cancel_attendance" data-eab-user_id="' . $booking->user_id . '" data-eab-event_id="' . $event->get_id() . '">' . __('Cancel attendance', Eab_EventsHub::TEXT_DOMAIN) . '</a></div>';
         }
         $content .= '<div class="eab-guest-actions"><a href="#delete-attendance" class="eab-guest-delete_attendance" data-eab-user_id="' . $booking->user_id . '" data-eab-event_id="' . $event->get_id() . '">' . __('Delete attendance entirely', Eab_EventsHub::TEXT_DOMAIN) . '</a></div>';
         $content = apply_filters('eab-event-booking_metabox_content', $content, $booking->user_id);
         $content .= '</div>';
         // .eab-guest
         $content .= '</li>';
     }
     $content .= '</ul>';
     $content .= '<div class="clear"></div>';
     return $content;
 }
 /**
  * Converts event RSVPs into a send list.
  */
 public function rsvps_to_member_group($event_id, $bookings = array())
 {
     if (empty($event_id) || empty($bookings)) {
         return false;
     }
     $event = new Eab_EventModel(get_post($event_id));
     $all_events = array($event);
     if ($event->is_recurring()) {
         $all_events = Eab_CollectionFactory::get_all_recurring_children_events($event);
     }
     $all_event_ids = array();
     foreach ($all_events as $e) {
         $all_event_ids[] = $e->get_id();
     }
     $all_event_ids = array_filter(array_map('intval', $all_event_ids));
     $rsvps = $this->_db->get_col("SELECT DISTINCT user_id FROM " . Eab_EventsHub::tablename(Eab_EventsHub::BOOKING_TABLE) . " WHERE event_id IN(" . join(',', $all_event_ids) . ") AND status IN ('" . join("','", $bookings) . "')");
     //if (empty($rsvps)) return false;
     $group_id = $this->_spawn_event_rsvp_group($event, $bookings);
     if (empty($group_id)) {
         return false;
     }
     return !empty($rsvps) ? $this->_newsletter->add_members_to_groups($rsvps, $group_id, false) : true;
 }
 protected function _get_bookings($status)
 {
     $event = new Eab_EventModel($this->_event_id);
     $event_id = "event_id = " . (int) $this->_event_id;
     if ($event->is_recurring()) {
         $all_event_ids = array();
         $events = Eab_CollectionFactory::get_all_recurring_children_events($event);
         foreach ($events as $e) {
             $all_event_ids[] = $e->get_id();
         }
         $all_event_ids = array_filter(array_map('intval', $all_event_ids));
         $event_id = "event_id IN(" . join(',', $all_event_ids) . ")";
     }
     global $wpdb;
     return $wpdb->get_results($wpdb->prepare("SELECT id,user_id,event_id,timestamp FROM " . Eab_EventsHub::tablename(Eab_EventsHub::BOOKING_TABLE) . " WHERE {$event_id} AND status = %s ORDER BY timestamp;", $status));
 }
 public function add_attendance($user_id, $status)
 {
     $user_id = (int) $this->_to_user_id($user_id);
     if (!$user_id) {
         return false;
     }
     if ($this->get_user_booking_id($user_id)) {
         return false;
     }
     // We already have attendance info for this guy.
     global $wpdb;
     return $wpdb->query($wpdb->prepare("INSERT INTO " . Eab_EventsHub::tablename(Eab_EventsHub::BOOKING_TABLE) . " VALUES(null, %d, %d, NOW(), %s) ON DUPLICATE KEY UPDATE `status` = %s;", $this->get_id(), $user_id, $status, $status));
 }
 /**
  * Order is completed = payment done.
  * Update event booking meta to indicate this, for Event-related Products.
  */
 function mp_product_order_paid($order)
 {
     $user_id = $order->post_author;
     $total_payment = $event_ids = array();
     if (is_array($order->mp_cart_info) && count($order->mp_cart_info)) {
         foreach ($order->mp_cart_info as $product_id => $variations) {
             foreach ($variations as $variation) {
                 $event_id = $this->order_to_event_id($product_id, $variation);
                 if (!$event_id) {
                     continue;
                 }
                 $event_ids[] = $event_id;
                 $tickets = !empty($total_payment[$event_id]) ? $total_payment[$event_id] : array('count' => 0, 'order_id' => $order->ID, 'product_id' => $product_id);
                 $tickets['count'] += (int) $variation['quantity'];
                 $total_payment[$event_id] = $tickets;
             }
         }
     }
     $user_bookings = array();
     $event_ids = join(',', array_unique($event_ids));
     global $wpdb;
     $table = Eab_EventsHub::tablename(Eab_EventsHub::BOOKING_TABLE);
     $bookings = $wpdb->get_results($wpdb->prepare("SELECT event_id,id FROM {$table} WHERE user_id=%d AND event_id IN ({$event_ids})", $user_id));
     foreach ($bookings as $booking) {
         $user_bookings[$booking->event_id] = $booking->id;
     }
     foreach ($total_payment as $event_id => $booking_info) {
         if (empty($user_bookings[$event_id])) {
             //continue; // Possibly paying for wrong variation!
             // Well, the guy apparently paid... so book him
             $wpdb->query($wpdb->prepare("INSERT INTO " . Eab_EventsHub::tablename(Eab_EventsHub::BOOKING_TABLE) . " VALUES(null, %d, %d, NOW(), 'yes') ON DUPLICATE KEY UPDATE `status` = 'yes';", $event_id, $user_id));
             $user_bookings[$event_id] = $wpdb->insert_id;
             // --todo: Add to BP activity stream
             do_action('incsub_event_booking_yes', $event_id, $user_id);
             // End booking extras
         }
         $booking_id = $user_bookings[$event_id];
         Eab_EventModel::update_booking_meta($booking_id, 'booking_transaction_key', serialize($booking_info));
         Eab_EventModel::update_booking_meta($booking_id, 'booking_ticket_count', $booking_info['count']);
     }
 }
 public function build_query_args($args)
 {
     global $wpdb;
     $result = $wpdb->get_col("SELECT event_id, COUNT(event_id) as cnt FROM " . Eab_EventsHub::tablename(Eab_EventsHub::BOOKING_TABLE) . " WHERE status IN ('yes', 'maybe') GROUP BY event_id ORDER BY cnt DESC");
     $args = array_merge($args, array('post__in' => array_values($result), 'post_type' => 'incsub_event', 'post_status' => array('publish', Eab_EventModel::RECURRENCE_STATUS), 'posts_per_page' => -1));
     return $args;
 }
 function eab_to_wpml__rewrite_rules($rules)
 {
     global $sitepress;
     $wpml_settings = $sitepress->get_settings();
     if ($wpml_settings['language_negotiation_type'] == 1 && $sitepress->get_current_language() != $sitepress->get_default_language()) {
         $data = Eab_Options::get_instance();
         $slug = $data->get_option('slug');
         $rules = $sitepress->rewrite_rules_filter((array) $rules + Eab_EventsHub::get_rewrite_rules($slug));
     }
     return $rules;
 }
 function send_email_non_paid_members_callback()
 {
     global $wpdb;
     $data = stripslashes_deep($_POST);
     $event_id = !empty($data['event_id']) && (int) $data['event_id'] ? (int) $data['event_id'] : false;
     $status = 'yes';
     //!empty($data['status']) ? $data['status'] : false;
     $response = array('status' => 1, 'sent' => 0);
     if (!$event_id || !$status) {
         die(json_encode($response));
     }
     $event = new Eab_EventModel(get_post($event_id));
     if (!$event->is_premium()) {
         die(json_encode($response));
     }
     // Not a paid event
     $all_events = array($event);
     if ($event->is_recurring()) {
         $all_events = Eab_CollectionFactory::get_all_recurring_children_events($event);
     }
     $all_event_ids = array();
     foreach ($all_events as $e) {
         $all_event_ids[] = $e->get_id();
     }
     $all_event_ids = array_filter(array_map('intval', $all_event_ids));
     $bookings = $wpdb->get_results($wpdb->prepare("SELECT id,user_id,event_id FROM " . Eab_EventsHub::tablename(Eab_EventsHub::BOOKING_TABLE) . " WHERE event_id IN(" . join(',', $all_event_ids) . ") AND status = %s ORDER BY timestamp;", $status));
     if (!count($bookings)) {
         die(json_encode($response));
     }
     foreach ($bookings as $booking) {
         if ($event->user_paid($booking->user_id)) {
             continue;
         }
         $this->_send_notification_email($event->get_id(), $user_id);
         $response['sent'] += 1;
     }
     $response['status'] = 0;
     die(json_encode($response));
 }
 function manage_posts_custom_column($column)
 {
     global $post;
     switch ($column) {
         case "attendees":
             global $wpdb;
             $event = $post instanceof Eab_EventModel ? $post : new Eab_EventModel($post);
             $yes = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM " . Eab_EventsHub::tablename(Eab_EventsHub::BOOKING_TABLE) . " WHERE event_id = %d AND status = %s;", $event->get_id(), Eab_EventModel::BOOKING_YES));
             $no = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM " . Eab_EventsHub::tablename(Eab_EventsHub::BOOKING_TABLE) . " WHERE event_id = %d AND status = %s;", $event->get_id(), Eab_EventModel::BOOKING_NO));
             $maybe = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM " . Eab_EventsHub::tablename(Eab_EventsHub::BOOKING_TABLE) . " WHERE event_id = %d AND status = %s;", $event->get_id(), Eab_EventModel::BOOKING_MAYBE));
             printf('<b>' . __('Attending / Undecided', self::TEXT_DOMAIN) . ':</b> %d / %d<br />', $yes, $maybe);
             printf('<b>' . __('Not Attending', self::TEXT_DOMAIN) . ':</b> %d', $no);
             echo '&nbsp;';
             echo '<a class="button" href="' . admin_url('index.php?eab_export=attendees&event_id=' . $event->get_id()) . '" class="eab-export_attendees">' . __('Export', self::TEXT_DOMAIN) . '</a>';
             break;
         case "start":
             $event = new Eab_EventModel($post);
             $df = get_option('date_format', 'Y-m-d');
             if (!$event->is_recurring()) {
                 echo date_i18n($df, $event->get_start_timestamp()) . ' - ' . date_i18n($df, $event->get_end_timestamp());
             } else {
                 $repeats = $event->get_supported_recurrence_intervals();
                 $title = @$repeats[$event->get_recurrence()];
                 $start = date_i18n($df, $event->get_recurrence_starts());
                 $end = date_i18n($df, $event->get_recurrence_ends());
                 printf(__("From %s, repeats every %s until %s", self::TEXT_DOMAIN), $start, $title, $end);
             }
             break;
         case "venue":
             $event = new Eab_EventModel($post);
             echo $event->get_venue_location();
             break;
         case "event":
             $event = new Eab_EventModel($post);
             $post_type_object = get_post_type_object($post->post_type);
             $edit_link = get_edit_post_link($event->get_id());
             $statuses = array();
             if ('draft' == $post->post_status) {
                 $statuses[] = __('Draft');
             }
             if ('private' == $post->post_status) {
                 $statuses[] = __('Private');
             }
             if ('pending' == $post->post_status) {
                 $statuses[] = __('Pending');
             }
             $status = $statuses ? ' - <span class="post-state">' . join(', ', $statuses) . '</span>' : '';
             $title = current_user_can($post_type_object->cap->edit_post, $event->get_id()) && 'trash' != $post->post_status ? '<strong>' . '<a class="row-title" href="' . $edit_link . '" title="' . esc_attr(sprintf(__('Edit &#8220;%s&#8221;'), $event->get_title())) . '">' . $event->get_title() . '</a>&nbsp;' . $status . '</strong>' : '<strong>' . $event->get_title() . '&nbsp;' . $status . '</strong>';
             if (current_user_can($post_type_object->cap->edit_post, $event->get_id()) && 'trash' != $post->post_status) {
                 $actions['edit'] = '<a title="' . esc_attr(__('Edit Event', self::TEXT_DOMAIN)) . '" href="' . $edit_link . '">' . __('Edit') . '</a>';
                 if (!$event->is_recurring()) {
                     $actions['inline hide-if-no-js'] = '<a href="#" class="editinline" title="' . esc_attr(__('Edit this Event inline', self::TEXT_DOMAIN)) . '">' . __('Quick&nbsp;Edit') . '</a>';
                 }
             }
             if (current_user_can($post_type_object->cap->delete_post, $event->get_id())) {
                 if ('trash' == $post->post_status) {
                     $actions['untrash'] = "<a title='" . esc_attr(__('Restore this Event from the Trash', self::TEXT_DOMAIN)) . "' href='" . wp_nonce_url(admin_url(sprintf($post_type_object->_edit_link . '&amp;action=untrash', $event->get_id())), 'untrash-' . $post->post_type . '_' . $event->get_id()) . "'>" . __('Restore') . "</a>";
                 } else {
                     if (EMPTY_TRASH_DAYS) {
                         $actions['trash'] = '<a class="submitdelete" title="' . esc_attr(__('Move this Event to the Trash', self::TEXT_DOMAIN)) . '" href="' . get_delete_post_link($event->get_id()) . '">' . __('Trash') . '</a>';
                     }
                 }
                 if ('trash' == $post->post_status || !EMPTY_TRASH_DAYS) {
                     $actions['delete'] = "<a class='submitdelete' title='" . esc_attr(__('Delete this Event permanently', self::TEXT_DOMAIN)) . "' href='" . get_delete_post_link($event->get_id(), '', true) . "'>" . __('Delete Permanently') . "</a>";
                 }
             }
             if ('trash' != $post->post_status) {
                 $event_id = $event->get_id();
                 if ($event->is_recurring()) {
                     $children = Eab_CollectionFactory::get_all_recurring_children_events($event);
                     if (!$children || !$children[0] instanceof Eab_EventModel) {
                         $event_id = false;
                     } else {
                         $event_id = $children[0]->get_id();
                     }
                 }
                 if ($event_id) {
                     $actions['view'] = '<a href="' . get_permalink($event_id) . '" title="' . esc_attr(sprintf(__('View &#8220;%s&#8221;'), $event->get_title())) . '" rel="permalink">' . __('View') . '</a>';
                 }
             }
             echo $title;
             if (!empty($actions)) {
                 foreach ($actions as $action => $link) {
                     $actions[$action] = "<span class='{$action}'>{$link}</span>";
                 }
             }
             echo '<div class="row-actions">' . join('&nbsp;|&nbsp;', $actions) . '</div>';
             get_inline_data($post);
             break;
     }
 }