Advertisement
stephenh1988

A very simple booking management add-on for Event Organiser

Feb 21st, 2012
385
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.07 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: My Event Organiser Booking Manager
  4. Plugin URI: http://www.HarrisWebSolutions.co.uk/event-organiser
  5. Description:
  6.          A class that can run alongside the Event Organiser WordPress plugin.
  7.          Adds a very simple booking management functionality to the plug-in,
  8.          and can be used as a base for something more complicated. The 'add-on'
  9.          adds a button to the bottom of single event pages. Clicking this
  10.          'registers' the current user for that event by adding their ID as
  11.          post meta. If the current user is registered a message is displayed
  12.          instead informing them. Bookings for an event can be manually
  13.          configured by editing the appropriate key-values in the custom fields
  14.          metabox. Currently does not necessarily need Event Organiser (in
  15.          particular) to run.
  16. Author: Stephen Harris
  17. Version: 1
  18. */
  19. /*  Copyright 2011 Stephen Harris (stephen@harriswebsolutions.co.uk)
  20.     This program is free software; you can redistribute it and/or modify
  21.     it under the terms of the GNU General Public License as published by
  22.     the Free Software Foundation; either version 2 of the License, or
  23.     (at your option) any later version.
  24.  
  25.     This program is distributed in the hope that it will be useful,
  26.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  27.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28.     GNU General Public License for more details.
  29. */
  30.  
  31. class My_eo_bookings {
  32.         //Name of the action used for nonces & action checks - should be unique to avoid classes with WP/Plugins
  33.     static $action = 'my_eo_booking_add';
  34.         //The meta key used to store users' ID. Again should be unique.
  35.     static $key = 'my_eo_registered_users';
  36.  
  37.     static function load() {
  38.         add_filter( 'init', array( __CLASS__, 'init' ));
  39.     }
  40.    
  41.     static function init(){
  42.         if(is_user_logged_in()){
  43.             //Recommended to run hooked functions 'late', e.g. priority of 20 or more
  44.             if(isset($_POST['action']) && $_POST['action']==self::$action)
  45.                 add_filter( 'init', array( __CLASS__, 'booking_insert' )  ,20);
  46.  
  47.             add_filter( 'the_content', array( __CLASS__, 'booking_markup' )  ,20);     
  48.         }
  49.     }
  50.  
  51.  
  52.     static function booking_markup($content ) {
  53.  
  54.         if(!is_singular('event'))
  55.             return $content;
  56.  
  57.         //The booking mark-up html, in this example just a 'sign me up' button if the user is logged in
  58.         $post_id = get_the_ID();
  59.         $user = wp_get_current_user();
  60.         $user_id = $user->ID;
  61.         $nonce = self::$action.'_'.$post_id.'_'.$user_id;
  62.  
  63.         if(self::is_booked($user_id,$post_id)){
  64.             $html = "<p>You registered for this event </p>";
  65.    
  66.         }else{
  67.             $html = "<p><form method='POST'>";
  68.             $html .= wp_nonce_field( $nonce,'_wpnonce',false,false);
  69.             $html .= "<input type='hidden' name='".self::$action."' value='".$post_id."' />";
  70.             $html .= "<input type='hidden' name='action' value='".self::$action."' />";
  71.             $html .= "<input type='submit' value='Sign me up!' />";
  72.             $html .= "</form></p>";
  73.         }
  74.  
  75.         $content .= $html;
  76.  
  77.             return $content;
  78.     }
  79.  
  80.  
  81.     static function booking_insert() {
  82.         $post_id = (isset($_POST[self::$action]) ? intval($_POST[self::$action]) : 0);
  83.         $user = wp_get_current_user();
  84.         $user_id = $user->ID;
  85.         $nonce = self::$action.'_'.$post_id.'_'.$user_id;
  86.  
  87.         //booking_insert should only run for events, but just in case...
  88.         if(get_post_type( $post_id)!='event')
  89.             return;
  90.  
  91.         //If empty data / non-numeric data sent abort.
  92.         if(empty($user_id) || empty($post_id))
  93.             return;
  94.  
  95.         //Check nonce
  96.         if (! wp_verify_nonce($_POST['_wpnonce'],$nonce) )
  97.             die('Security check');
  98.  
  99.         //Nonces passed, perform any further checks if necessary then add user to post meta
  100.         add_post_meta($post_id, self::$key, $user_id); 
  101.     }
  102.  
  103.     static function is_booked($user_id,$post_id){
  104.         $user_id = (int) $user_id;
  105.         $post_id = (int) $post_id;
  106.        
  107.         if(empty($user_id)||empty($post_id))
  108.             return false;
  109.  
  110.         if(get_post_type( $post_id)!='event')
  111.             return false;
  112.  
  113.         $registered = get_post_meta($post_id, self::$key);
  114.  
  115.         return in_array($user_id, $registered);
  116.     }
  117. }
  118. My_eo_bookings::load();
  119. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement