Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Plugin Name: My Event Organiser Booking Manager
- Plugin URI: http://www.HarrisWebSolutions.co.uk/event-organiser
- Description:
- A class that can run alongside the Event Organiser WordPress plugin.
- Adds a very simple booking management functionality to the plug-in,
- and can be used as a base for something more complicated. The 'add-on'
- adds a button to the bottom of single event pages. Clicking this
- 'registers' the current user for that event by adding their ID as
- post meta. If the current user is registered a message is displayed
- instead informing them. Bookings for an event can be manually
- configured by editing the appropriate key-values in the custom fields
- metabox. Currently does not necessarily need Event Organiser (in
- particular) to run.
- Author: Stephen Harris
- Version: 1
- */
- /* Copyright 2011 Stephen Harris (stephen@harriswebsolutions.co.uk)
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- */
- class My_eo_bookings {
- //Name of the action used for nonces & action checks - should be unique to avoid classes with WP/Plugins
- static $action = 'my_eo_booking_add';
- //The meta key used to store users' ID. Again should be unique.
- static $key = 'my_eo_registered_users';
- static function load() {
- add_filter( 'init', array( __CLASS__, 'init' ));
- }
- static function init(){
- if(is_user_logged_in()){
- //Recommended to run hooked functions 'late', e.g. priority of 20 or more
- if(isset($_POST['action']) && $_POST['action']==self::$action)
- add_filter( 'init', array( __CLASS__, 'booking_insert' ) ,20);
- add_filter( 'the_content', array( __CLASS__, 'booking_markup' ) ,20);
- }
- }
- static function booking_markup($content ) {
- if(!is_singular('event'))
- return $content;
- //The booking mark-up html, in this example just a 'sign me up' button if the user is logged in
- $post_id = get_the_ID();
- $user = wp_get_current_user();
- $user_id = $user->ID;
- $nonce = self::$action.'_'.$post_id.'_'.$user_id;
- if(self::is_booked($user_id,$post_id)){
- $html = "<p>You registered for this event </p>";
- }else{
- $html = "<p><form method='POST'>";
- $html .= wp_nonce_field( $nonce,'_wpnonce',false,false);
- $html .= "<input type='hidden' name='".self::$action."' value='".$post_id."' />";
- $html .= "<input type='hidden' name='action' value='".self::$action."' />";
- $html .= "<input type='submit' value='Sign me up!' />";
- $html .= "</form></p>";
- }
- $content .= $html;
- return $content;
- }
- static function booking_insert() {
- $post_id = (isset($_POST[self::$action]) ? intval($_POST[self::$action]) : 0);
- $user = wp_get_current_user();
- $user_id = $user->ID;
- $nonce = self::$action.'_'.$post_id.'_'.$user_id;
- //booking_insert should only run for events, but just in case...
- if(get_post_type( $post_id)!='event')
- return;
- //If empty data / non-numeric data sent abort.
- if(empty($user_id) || empty($post_id))
- return;
- //Check nonce
- if (! wp_verify_nonce($_POST['_wpnonce'],$nonce) )
- die('Security check');
- //Nonces passed, perform any further checks if necessary then add user to post meta
- add_post_meta($post_id, self::$key, $user_id);
- }
- static function is_booked($user_id,$post_id){
- $user_id = (int) $user_id;
- $post_id = (int) $post_id;
- if(empty($user_id)||empty($post_id))
- return false;
- if(get_post_type( $post_id)!='event')
- return false;
- $registered = get_post_meta($post_id, self::$key);
- return in_array($user_id, $registered);
- }
- }
- My_eo_bookings::load();
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement