Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- *This plugin provides the route handling that helps creating routes
- */
- class wit_witchroute
- {
- /*
- Register the new posttype:wit-route
- Load the metaboxes for the route
- Register the ajax methods for data handling
- */
- public function __construct()
- {
- $this->register_post_type();
- $this->metaboxes();
- $wit_date = wit_date::getInstance();
- if ( is_admin() )
- {
- add_action('wp_ajax_remove_date_from_route', function()
- {
- $wit_date = wit_date::getInstance();
- $wit_date->removeDate($_POST);
- });
- add_action('wp_ajax_insert_date_to_route', function()
- {
- $wit_date = wit_date::getInstance();
- $wit_date->insertDate($_POST);
- });
- }
- }
- /*
- Declare the new post stype and add it
- */
- public function register_post_type()
- {
- $labels = array(
- 'name' => __('Routen'),
- 'singular_name' => __('Routen Einzelansicht'),
- 'add_new' => _x('Hinzufügen', 'Listing'),
- 'add_new_item' => __('Füge neue Route hinzu'),
- 'edit_item' => __('Route bearbeiten'),
- 'new_item' => __('Neue Route'),
- 'all_items' => __('Zeige Alle'),
- 'view_item' => __('Route anzeigen'),
- 'search_items' => __('Route suchen'),
- 'not_found' => __('Keine Route gefunden'),
- 'not_found_in_trash' => __('Keine Route im Müll gefunden'),
- 'parent_item_colon' => '',
- 'menu_name' => 'Routen'
- );
- $args = array(
- 'labels' => $labels,
- 'public' => true,
- 'publicly_queryable' => true,
- 'show_ui' => true,
- 'show_in_menu' => true,
- 'query_var' => 'route',
- 'menu_position' => 5,
- 'has_archive' => true,
- 'rewrite' => array( 'slug' => 'routen-ansicht' ),
- 'supports' => array('title', 'editor', 'thumbnail','excerpt')
- );
- register_post_type('wit-route', $args);
- }
- /*
- This requires the nextgen gallery plugin to be installed
- It will get the gallery names and adds them to the plugin
- so that the user is able to choose from a gallery for a specific route
- */
- public static function getGalleries()
- {
- global $wpdb;
- $data = wit_witchroute::getMetaBox();
- $sql = "SELECT gid, name FROM ". $wpdb->prefix . "ngg_gallery";
- $galleries = $wpdb->get_results($sql);
- foreach($galleries as $gal)
- {
- $data['fields'][6]['options'][] = array('name' => $gal->name, 'value' => $gal->gid );
- }
- return $data;
- }
- /*
- These are the metaboxes that will be added to the admin panel when creating a new route
- */
- public function metaboxes()
- {
- //add the meta boxes
- add_action('add_meta_boxes', function() {
- $meta_box = wit_witchroute::getMetaBox();
- add_meta_box($meta_box['id'], $meta_box['title'], 'witchroute_show_box', $meta_box['page']);
- });
- // Callback function to show fields in meta box
- function witchroute_show_box( $post ) {
- $meta_box = wit_witchroute::getGalleries();
- // Use nonce for verification
- echo '<input type="hidden" name="witchroute_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
- echo '<table class="form-table">';
- foreach ($meta_box['fields'] as $field) {
- // get current post meta data
- $meta = get_post_meta($post->ID, $field['id'], true);
- echo '<tr>',
- '<th style="width:20%"><label for="', $field['id'], '">', $field['name'], '</label></th>',
- '<td>';
- switch ($field['type']) {
- case 'text':
- echo '<input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" size="30" style="width:97%" />', '<br />', $field['desc'];
- break;
- case 'textarea':
- echo '<textarea name="', $field['id'], '" id="', $field['id'], '" cols="60" rows="4" style="width:97%">', $meta ? $meta : $field['std'], '</textarea>', '<br />', $field['desc'];
- break;
- case 'select':
- echo '<select name="', $field['id'], '" id="', $field['id'], '">';
- foreach ($field['options'] as $option) {
- echo '<option', $meta == $option['value'] ? ' selected="selected"' : '', '>', $option['name'], '</option>';
- }
- echo '</select>';
- break;
- case 'radio':
- foreach ($field['options'] as $option) {
- echo '<input type="radio" name="', $field['id'], '" value="', $option['value'], '"', $meta == $option['value'] ? ' checked="checked"' : '', ' />', $option['name'];
- }
- break;
- case 'checkbox':
- echo '<input type="checkbox" name="', $field['id'], '" id="', $field['id'], '"', $meta ? ' checked="checked"' : '', ' />';
- break;
- }
- echo '<td>',
- '</tr>';
- }
- echo '</table>';
- }
- add_action('save_post', 'witchroute_save_data');
- /*
- Save the data
- */
- function witchroute_save_data($post_id)
- {
- $meta_box = wit_witchroute::getMetaBox();
- // verify nonce
- if (!wp_verify_nonce($_POST['witchroute_meta_box_nonce'], basename(__FILE__))) {
- return $post_id;
- }
- // check autosave
- if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
- return $post_id;
- }
- // check permissions
- if ('page' == $_POST['post_type']) {
- if (!current_user_can('edit_page', $post_id)) {
- return $post_id;
- }
- } elseif (!current_user_can('edit_post', $post_id)) {
- return $post_id;
- }
- foreach ($meta_box['fields'] as $field) {
- $old = get_post_meta($post_id, $field['id'], true);
- $new = $_POST[$field['id']];
- if ($new && $new != $old) {
- update_post_meta($post_id, $field['id'], $new);
- } elseif ('' == $new && $old) {
- delete_post_meta($post_id, $field['id'], $old);
- }
- }
- }
- }
- /*
- Declare the custom fields for the metabox
- which are: difficulty, curves, length, country, gallery, start and destination
- */
- public static function getMetaBox()
- {
- $prefix = 'witchroute_';
- //create the meta boxes
- return array(
- 'id' => 'witchroute-meta-box',
- 'title' => 'Route Details',
- 'page' => 'wit-route',
- 'fields' => array(
- array(
- 'name' => 'Schwierigkeit',
- 'id' => $prefix . 'level',
- 'type' => 'select',
- 'options' => array('0','1', '2', '3', '4')
- ),
- array(
- 'name' => 'Anzahl an Kurven',
- 'id' => $prefix . 'curves',
- 'type' => 'text',
- 'std' => '100'
- ),
- array(
- 'name' => 'Streckenlänge',
- 'id' => $prefix . 'length',
- 'type' => 'text',
- 'std' => '250'
- ),
- array(
- 'name' => 'Land',
- 'id' => $prefix . 'country',
- 'type' => 'select',
- 'options' => array(array("value"=>"Deutschland", 'name'=>"Deutschland"),
- array("value"=>"Österreich", 'name'=>"Österreich"),
- array("value"=>"Schweiz", 'name'=>"Schweiz"),
- array("value"=>"Frankreich", 'name'=>"Frankreich"),
- array("value"=>"England", 'name'=>"England"))
- ),
- array(
- 'name' => 'Gallery',
- 'id' => $prefix . 'gallery',
- 'type' => 'select',
- 'options' => array()
- ),
- array(
- 'name' => 'Start Ort',
- 'id' => $prefix . 'start',
- 'type' => 'text',
- 'std' => 'Hamburg'
- ),
- array(
- 'name' => 'Ziel Ort',
- 'id' => $prefix . 'destination',
- 'type' => 'text',
- 'std' => 'Hamburg'
- ),
- )
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment