3v4l.org

run code in 300+ PHP versions simultaneously
<?php /* Plugin Name: Swapp Event Description: A WordPress plugin for event management Version: 1 Author: Courtney Edgar */ Class SWAPP_Event { protected static $instance = NULL; public static function get_instance() { NULL === self::$instance and self::$instance = new self; return self::$instance; } function __construct() { add_action('init', array($this, 'swapp_event_init')); register_activation_hook(__FILE__, array($this, 'swapp_event_activate')); //register_deactivation_hook(__FILE__, array($this, 'wpoa_deactivate')); } function swapp_event_activate() { $this->swapp_event_register_post_type(); // ATTENTION: This is *only* done during plugin activation hook. // NEVER EVER do this on every page load!! flush_rewrite_rules(); global $wpdb; $table_name = $wpdb->prefix.'swapp_people'; $charset_collate = $wpdb->get_charset_collate(); $sql = "CREATE TABLE $table_name ( id mediumint(9) NOT NULL AUTO_INCREMENT, event_id bigint(20)NOT NULL, user_id bigint(20) NOT NULL, seat_id int(11) NOT NULL, time datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, UNIQUE KEY id (id) ) $charset_collate;"; require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); dbDelta( $sql ); } function swapp_event_init() { $this->swapp_event_register_post_type(); add_filter('post_updated_messages', array($this, 'swapp_event_set_post_messages')); add_action('manage_posts_custom_column', array($this, 'swapp_event_custom_columns')); add_filter('manage_event_posts_columns', array($this, 'swapp_event_edit_columns')); add_filter('manage_edit-events_sortable_columns', array($this, 'swapp_event_date_column_register_sortable')); add_filter('request', array($this, 'swapp_event_date_column_orderby' )); add_action('admin_init', array($this, 'swapp_event_admin_init')); add_action('save_post', array($this, 'swapp_event_save_details')); add_action('admin_enqueue_scripts', array($this, 'swapp_event_admin_enqueue')); add_action('wp_enqueue_scripts', array($this, 'swapp_event_enqueue')); add_filter('single_template', array($this, 'swapp_event_template')); add_action('wp_ajax_user_profile', array($this, 'swapp_event_ajax_user_profile')); } function swapp_event_register_post_type() { register_post_type('event', array( 'labels' => array( 'name' => __('Events'), 'singular_name' => __('Event'), 'menu_name' => __('Events'), 'name_admin_bar' => __('Event'), 'add_new' => __('Add New'), 'add_new_item' => __('Add New Event'), 'new_item' => __('New Event'), 'edit_item' => __('Edit Event'), 'view_item' => __('View Event'), 'all_items' => __('All Events'), 'search_items' => __('Search Events'), 'parent_item_colon' => __('Parent Events:'), 'not_found' => __('No events found.'), 'not_found_in_trash' => __('No events found in bin.') ), 'public' => true, 'publicly_queryable' => true, 'show_ui' => true, 'show_in_menu' => true, 'show_in_admin_bar' => true, 'menu_icon' => 'dashicons-calendar-alt', 'query_var' => true, 'rewrite' => array( 'slug' => 'event' ), 'capability_type' => 'post', 'has_archive' => false, 'hierarchical' => false, 'supports' => array('title', 'editor', 'thumbnail') )); } function swapp_event_set_post_messages($messages) { $post = get_post(); $post_type = get_post_type($post); $post_type_object = get_post_type_object($post_type); $messages[$post_type] = array( 0 => '', // Unused. Messages start at index 1. 1 => 'Event updated.', 2 => 'Field updated.', 3 => 'Field deleted.', 4 => 'Event updated.', 5 => isset($_GET['revision']) ? sprintf('Event restored to revision from %s', wp_post_revision_title( (int) $_GET['revision'], false ) ) : false, 6 => 'Event published.', 7 => 'Event saved.', 8 => 'Event submitted.', 9 => sprintf('Event scheduled for: <strong>%1$s</strong>.', date_i18n('M j, Y @ G:i', strtotime($post->post_date))), 10 => 'Event draft updated.' ); if ($post_type_object->publicly_queryable) { $permalink = get_permalink($post->ID); $view_link = sprintf( ' <a href="%s">%s</a>', esc_url( $permalink ), 'View event'); $messages[$post_type][1] .= $view_link; $messages[$post_type][6] .= $view_link; $messages[$post_type][9] .= $view_link; $preview_permalink = add_query_arg( 'preview', 'true', $permalink ); $preview_link = sprintf(' <a target="_blank" href="%s">%s</a>', esc_url( $preview_permalink ), 'Preview event'); $messages[$post_type][8] .= $preview_link; $messages[$post_type][10] .= $preview_link; } return $messages; } function swapp_event_edit_columns($columns){ $columns = array( 'cb' => '<input type="checkbox" />', 'title' => 'Event', 'signup_date' => 'Signup Date', 'event_date' => 'Event Date', 'event_location' => 'Location', 'event_tables' => 'Tables' ); return $columns; } function swapp_event_custom_columns($column){ global $post; $custom = get_post_custom(); switch ($column) { case 'signup_date': echo 'Start: '.$this->swapp_event_format_datetime($custom['signup_start'][0]).'<br />End: '.$this->swapp_event_format_datetime($custom['signup_end'][0]); break; case 'event_date': echo 'Start: '.$this->swapp_event_format_datetime($custom['event_start'][0]).'<br />End: '.$this->swapp_event_format_datetime($custom['event_end'][0]); break; case 'event_location': echo $custom['event_location'][0].', '.$custom['event_postcode'][0]; break; case 'event_tables': echo $custom['event_tables'][0]; break; } } function swapp_event_format_datetime($time) { return date('jS M Y H:i', $time); } function swapp_event_format_date($time) { return date('d/m/Y', $time); } function swapp_event_format_time($time) { return date('H:i', $time); } function swapp_event_date_column_register_sortable( $columns ) { $columns['signup_date'] = 'signup_date'; $columns['event_date'] = 'event_date'; return $columns; } function swapp_event_date_column_orderby( $vars ) { if (isset($vars['orderby']) && 'event_date' == $vars['orderby'] ) { $vars = array_merge( $vars, array( 'meta_key' => 'event_start_date', 'orderby' => 'meta_value_num' ) ); } return $vars; } function swapp_event_admin_init(){ add_meta_box('event_meta', 'Event Details', array($this, 'swapp_event_details_meta'), 'event', 'normal', 'default'); } function swapp_event_details_meta() { ?> <p><label>Signup Start: </label><input class="swapp-date-input" type="text" placeholder="DD/MM/YYYY" maxlength="10" name="signup_start_date" value="<?php echo $this->swapp_event_field_date('signup_start'); ?>" /><input type="text" placeholder="HH:MM" maxlength="5" name="signup_start_time" value="<?php echo $this->swapp_event_field_time('signup_start'); ?>" /></p> <p><label>Signup End: </label><input class="swapp-date-input" type="text" placeholder="DD/MM/YYYY" maxlength="10" name="signup_end_date" value="<?php echo $this->swapp_event_field_date('signup_end'); ?>" /><input type="text" placeholder="HH:MM" maxlength="5" name="signup_end_time" value="<?php echo $this->swapp_event_field_time('signup_end'); ?>" /></p> <p><label>Event Start: </label><input class="swapp-date-input" type="text" placeholder="DD/MM/YYYY" maxlength="10" name="event_start_date" value="<?php echo $this->swapp_event_field_date('event_start'); ?>" /><input type="text" placeholder="HH:MM" maxlength="5" name="event_start_time" value="<?php echo $this->swapp_event_field_time('event_start'); ?>" /></p> <p><label>Event End: </label><input class="swapp-date-input" type="text" placeholder="DD/MM/YYYY" maxlength="10" name="event_end_date" value="<?php echo $this->swapp_event_field_date('event_end'); ?>" /><input type="text" placeholder="HH:MM" maxlength="5" name="event_end_time" value="<?php echo $this->swapp_event_field_time('event_end'); ?>" /></p> <p><label for="event_location">Location: </label><input id="event_location" type="text" placeholder="Location" maxlength="70" name="event_location" value="<?php echo $this->swapp_event_field('event_location'); ?>" /></p> <p><label for="event_postcode">Postcode: </label><input id="event_postcode" type="text" placeholder="Postcode" maxlength="10" name="event_postcode" value="<?php echo $this->swapp_event_field('event_postcode'); ?>" /></p> <p><label for="event_tables">Tables: </label><input id="event_tables" type="text" placeholder="Tables" maxlength="4" name="event_tables" value="<?php echo $this->swapp_event_field('event_tables'); ?>" /></p> <p><label for="event_price">Price: </label><input id="event_price" type="text" placeholder="Price" maxlength="7" name="event_price" value="<?php echo $this->swapp_event_field('event_price'); ?>" /></p> <?php } function swapp_event_field($event_field) { global $post; $custom = get_post_custom($post->ID); if (isset($custom[$event_field])) { return $custom[$event_field][0]; } } function swapp_event_field_date($event_field) { global $post; $custom = get_post_custom($post->ID); if (isset($custom[$event_field])) { return $this->swapp_event_format_date($custom[$event_field][0]); } } function swapp_event_field_time($event_field) { global $post; $custom = get_post_custom($post->ID); if (isset($custom[$event_field])) { return $this->swapp_event_format_time($custom[$event_field][0]); } } function swapp_event_save_details(){ global $post; if (defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE) { return; } if (get_post_type($post) != 'event') { return; } foreach (array('signup_start', 'signup_end', 'event_start', 'event_end') as $v) { if(isset($_POST[$v.'_date']) && isset($_POST[$v.'_time'])) { $date = explode('/', sanitize_text_field($_POST[$v.'_date'])); $time = explode(':', sanitize_text_field($_POST[$v.'_time'])); $datetime = mktime($time[0], $time[1], 0, $date[1], $date[0], $date[2]); update_post_meta($post->ID, $v, $datetime); } } if(isset($_POST['event_location']) && isset($_POST['event_postcode'])) { $location = $this->swapp_event_geocode($_POST['event_location'].', '.$_POST['event_postcode']); update_post_meta($post->ID, 'event_latitude', round($location['lat'], 6)); update_post_meta($post->ID, 'event_longitude', round($location['lng'], 6)); } foreach(array('event_location', 'event_tables', 'event_postcode') as $v) { if(isset($_POST[$v])) { update_post_meta($post->ID, $v, sanitize_text_field($_POST[$v])); } } if (isset($_POST['event_price'])) { update_post_meta($post->ID, 'event_price', str_replace('£', '', sanitize_text_field($_POST['event_price']))); } } function swapp_event_geocode($address) { $c = curl_init(); curl_setopt($c, CURLOPT_RETURNTRANSFER, 1); curl_setopt($c, CURLOPT_URL, 'http://maps.google.com/maps/api/geocode/json?sensor=false&address='.urlencode($address)); $contents = curl_exec($c); curl_close($c); if ($contents) { $resp_json = $contents; } else { $resp_json = FALSE; } $resp = json_decode($resp_json, true); if($resp['status'] == 'OK'){ return $resp['results'][0]['geometry']['location']; } else { return false; } } function swapp_event_admin_enqueue() { global $post_type; if ($post_type == 'event') { wp_enqueue_script('jquery-ui-datepicker'); wp_enqueue_style('jquery-style', 'https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.3/themes/ui-darkness/jquery-ui.css'); wp_enqueue_script('swapp-event-admin', plugin_dir_url( __FILE__ ) . 'js/swapp-event-admin.js', array('jquery')); } } function swapp_event_enqueue() { wp_enqueue_style('swapp-event', plugin_dir_url(__FILE__).'css/swapp-event.css'); wp_enqueue_script('swapp-event', plugin_dir_url( __FILE__ ) . 'js/swapp-event.js', array('jquery')); wp_localize_script('swapp-event', 'ajax_url', array('ajax_url' => admin_url( 'admin-ajax.php' ))); } function swapp_event_template($single_template) { global $post; if ($post->post_type == 'event') { if (is_user_logged_in()) { wp_enqueue_script('google-maps', 'https://maps.googleapis.com/maps/api/js?key=AIzaSyASW0ZZ6BjqrDYNFXE0YNiFYwsBWygE9Ok'); return dirname( __FILE__ ) . '/templates/single-event.php'; } else { wp_redirect(home_url('/wp-login.php', 401)); exit; } } return $single_template; } function swapp_event_ajax_user_profile() { /* if (!empty($_POST['table_id']) && is_numeric($_POST['table_id']) && !empty($_POST['event_id']) && is_numeric($_POST['event_id'])) { $table_id = $_POST['table_id']; $event_id = $_POST['event_id']; $seat_list = array($table_id + 1, $table_id + 2, $table_id + 3, $table_id + 4); global $wpdb; $table_name = $wpdb->prefix.'swapp_people'; $results = $wpdb->get_results( "SELECT * FROM $table_name WHERE event_id = $event_id AND (seat_id = $seat_list[1] OR seat_id = $seat_list[2] OR seat_id = $seat_list[3] OR seat_id = $seat_list[4])", ARRAY_A ); $seats = array(); foreach ($results as $r) { $user = get_userdata($r['user_id']); $seats[$r['seat_id']] = array( 'status' => 'Reserved', 'first_name' => $user->first_name, 'last_name' => $user->last_name, 'picture' => $user->picture, 'url' => $user->user_url, 'gender' => $user->gender, 'description' => $user->description, 'age' => $user->age ); } foreach ($seat_list as $k => $v) { if (!array_key_exists($v, $seats)) { $seats[$v] = array( 'status' => 'Empty', ); } } echo json_encode($seats); } else { echo json_encode(array('result' => 'error')); } */ echo json_encode(array(array( 'status' => 'Reserved', 'first_name' => 'Courtney', 'last_name' => 'Edgar', 'picture' => 'http://graph.facebook.com/958588757493415/picture', 'url' => 'https://www.facebook.com/app_scoped_user_id/958588757493415/', 'gender' => 'Male', 'description' => 'My biographical info', 'age' => '22' ), array( 'status' => 'Reserved', 'first_name' => 'Courtney', 'last_name' => 'Edgar', 'picture' => 'http://graph.facebook.com/958588757493415/picture', 'url' => 'https://www.facebook.com/app_scoped_user_id/958588757493415/', 'gender' => 'Male', 'description' => 'My biographical info', 'age' => '22' ), array( 'status' => 'Empty' ), array( 'status' => 'Empty' ))); wp_die(); } function swapp_event_ajax_get_people() { $event_id = $_POST['event_id']; if (is_numeric($event_id)) { global $wpdb; $table_name = $wpdb->prefix.'swapp_people'; $results = $wpdb->get_results( "SELECT * FROM $table_name WHERE event_id = $event_id", ARRAY_A ); echo json_encode($results); } else { echo json_encode(array('status' => 'error')); } wp_die(); } } SWAPP_EVENT::get_instance();
Output for git.master, git.master_jit, rfc.property-hooks
Fatal error: Uncaught Error: Call to undefined function add_action() in /in/R45u0:19 Stack trace: #0 /in/R45u0(14): SWAPP_Event->__construct() #1 /in/R45u0(406): SWAPP_Event::get_instance() #2 {main} thrown in /in/R45u0 on line 19
Process exited with code 255.

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
51.4 ms | 401 KiB | 8 Q