Guest User

Untitled

a guest
Feb 10th, 2012
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.52 KB | None | 0 0
  1. <?php
  2.     /**
  3.     *This plugin provides the route handling that helps creating routes
  4.     */
  5.     class wit_witchroute
  6.     {
  7.  
  8.         /*
  9.             Register the new posttype:wit-route
  10.             Load the metaboxes for the route
  11.             Register the ajax methods for data handling
  12.         */
  13.         public function __construct()
  14.         {
  15.             $this->register_post_type();
  16.             $this->metaboxes();
  17.  
  18.             $wit_date = wit_date::getInstance();
  19.  
  20.             if ( is_admin() )
  21.             {
  22.  
  23.                 add_action('wp_ajax_remove_date_from_route', function()
  24.                 {
  25.                     $wit_date = wit_date::getInstance();
  26.                     $wit_date->removeDate($_POST);
  27.                 });
  28.  
  29.                 add_action('wp_ajax_insert_date_to_route', function()
  30.                 {
  31.                     $wit_date = wit_date::getInstance();
  32.                     $wit_date->insertDate($_POST);
  33.                 });
  34.             }
  35.  
  36.         }
  37.  
  38.         /*
  39.             Declare the new post stype and add it
  40.         */
  41.         public function register_post_type()
  42.         {
  43.  
  44.             $labels = array(
  45.                 'name' => __('Routen'),
  46.                 'singular_name' => __('Routen Einzelansicht'),
  47.                 'add_new' => _x('Hinzuf&uuml;gen', 'Listing'),
  48.                 'add_new_item' => __('F&uuml;ge neue Route hinzu'),
  49.                 'edit_item' => __('Route bearbeiten'),
  50.                 'new_item' => __('Neue Route'),
  51.                 'all_items' => __('Zeige Alle'),
  52.                 'view_item' => __('Route anzeigen'),
  53.                 'search_items' => __('Route suchen'),
  54.                 'not_found' =>  __('Keine Route gefunden'),
  55.                 'not_found_in_trash' => __('Keine Route im M&uuml;ll gefunden'),
  56.                 'parent_item_colon' => '',
  57.                 'menu_name' => 'Routen'
  58.             );
  59.        
  60.             $args = array(
  61.                     'labels' => $labels,
  62.                     'public' => true,
  63.                     'publicly_queryable' => true,
  64.                     'show_ui' => true,
  65.                     'show_in_menu' => true,
  66.                     'query_var' => 'route',
  67.                     'menu_position' => 5,
  68.                     'has_archive' => true,
  69.                     'rewrite' => array( 'slug' => 'routen-ansicht' ),
  70.                     'supports' => array('title', 'editor', 'thumbnail','excerpt')
  71.             );
  72.  
  73.  
  74.             register_post_type('wit-route', $args);
  75.         }
  76.  
  77.         /*
  78.             This requires the nextgen gallery plugin to be installed
  79.             It will get the gallery names and adds them to the plugin
  80.             so that the user is able to choose from a gallery for a specific route
  81.         */
  82.         public static function getGalleries()
  83.         {
  84.             global $wpdb;
  85.  
  86.             $data       =   wit_witchroute::getMetaBox();
  87.  
  88.             $sql        =   "SELECT gid, name FROM ". $wpdb->prefix . "ngg_gallery";
  89.             $galleries  =   $wpdb->get_results($sql);
  90.  
  91.             foreach($galleries as $gal)
  92.             {
  93.                 $data['fields'][6]['options'][] =   array('name' => $gal->name, 'value' => $gal->gid );
  94.             }
  95.  
  96.             return $data;
  97.         }
  98.  
  99.        
  100.         /*
  101.             These are the metaboxes that will be added to the admin panel when creating a new route
  102.         */
  103.         public function metaboxes()
  104.         {
  105.            
  106.             //add the meta boxes
  107.             add_action('add_meta_boxes', function() {
  108.                 $meta_box   =   wit_witchroute::getMetaBox();  
  109.  
  110.                 add_meta_box($meta_box['id'], $meta_box['title'], 'witchroute_show_box', $meta_box['page']);
  111.             });
  112.  
  113.             // Callback function to show fields in meta box
  114.             function witchroute_show_box( $post ) {
  115.                $meta_box    =   wit_witchroute::getGalleries();
  116.                 // Use nonce for verification
  117.                 echo '<input type="hidden" name="witchroute_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
  118.  
  119.                 echo '<table class="form-table">';
  120.  
  121.                 foreach ($meta_box['fields'] as $field) {
  122.                     // get current post meta data
  123.                     $meta = get_post_meta($post->ID, $field['id'], true);
  124.  
  125.                     echo '<tr>',
  126.                             '<th style="width:20%"><label for="', $field['id'], '">', $field['name'], '</label></th>',
  127.                             '<td>';
  128.                     switch ($field['type']) {
  129.                         case 'text':
  130.                             echo '<input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" size="30" style="width:97%" />', '<br />', $field['desc'];
  131.                             break;
  132.                         case 'textarea':
  133.                             echo '<textarea name="', $field['id'], '" id="', $field['id'], '" cols="60" rows="4" style="width:97%">', $meta ? $meta : $field['std'], '</textarea>', '<br />', $field['desc'];
  134.                             break;
  135.                         case 'select':
  136.                             echo '<select name="', $field['id'], '" id="', $field['id'], '">';
  137.                             foreach ($field['options'] as $option) {
  138.                                 echo '<option', $meta == $option['value'] ? ' selected="selected"' : '', '>', $option['name'], '</option>';
  139.                             }
  140.                             echo '</select>';
  141.                             break;
  142.                         case 'radio':
  143.                             foreach ($field['options'] as $option) {
  144.                                 echo '<input type="radio" name="', $field['id'], '" value="', $option['value'], '"', $meta == $option['value'] ? ' checked="checked"' : '', ' />', $option['name'];
  145.                             }
  146.                             break;
  147.                         case 'checkbox':
  148.                             echo '<input type="checkbox" name="', $field['id'], '" id="', $field['id'], '"', $meta ? ' checked="checked"' : '', ' />';
  149.                             break;
  150.                     }
  151.                     echo     '<td>',
  152.                         '</tr>';
  153.                 }
  154.  
  155.                 echo '</table>';
  156.             }
  157.  
  158.             add_action('save_post', 'witchroute_save_data');
  159.  
  160.             /*
  161.                 Save the data
  162.             */
  163.             function witchroute_save_data($post_id)
  164.             {
  165.                 $meta_box = wit_witchroute::getMetaBox();
  166.  
  167.                 // verify nonce
  168.                 if (!wp_verify_nonce($_POST['witchroute_meta_box_nonce'], basename(__FILE__))) {
  169.                     return $post_id;
  170.                 }
  171.  
  172.                 // check autosave
  173.                 if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
  174.                     return $post_id;
  175.                 }
  176.  
  177.                 // check permissions
  178.                 if ('page' == $_POST['post_type']) {
  179.                     if (!current_user_can('edit_page', $post_id)) {
  180.                         return $post_id;
  181.                     }
  182.                 } elseif (!current_user_can('edit_post', $post_id)) {
  183.                     return $post_id;
  184.                 }
  185.  
  186.                 foreach ($meta_box['fields'] as $field) {
  187.                     $old = get_post_meta($post_id, $field['id'], true);
  188.                     $new = $_POST[$field['id']];
  189.  
  190.                     if ($new && $new != $old) {
  191.                         update_post_meta($post_id, $field['id'], $new);
  192.                     } elseif ('' == $new && $old) {
  193.                         delete_post_meta($post_id, $field['id'], $old);
  194.                     }
  195.                 }
  196.             }
  197.  
  198.  
  199.         }
  200.  
  201.         /*
  202.             Declare the custom fields for the metabox
  203.             which are: difficulty, curves, length, country, gallery, start and destination
  204.         */
  205.         public static function getMetaBox()
  206.         {
  207.             $prefix = 'witchroute_';
  208.             //create the meta boxes
  209.             return array(
  210.                 'id' => 'witchroute-meta-box',
  211.                 'title' => 'Route Details',
  212.                 'page' => 'wit-route',
  213.                 'fields' => array(
  214.                     array(
  215.                         'name' => 'Schwierigkeit',
  216.                         'id' => $prefix . 'level',
  217.                         'type' => 'select',
  218.                         'options' => array('0','1', '2', '3', '4')
  219.                     ),
  220.                     array(
  221.                         'name' => 'Anzahl an Kurven',
  222.                         'id' => $prefix . 'curves',
  223.                         'type' => 'text',
  224.                         'std' => '100'
  225.                     ),
  226.                     array(
  227.                         'name' => 'Streckenl&auml;nge',
  228.                         'id' => $prefix . 'length',
  229.                         'type' => 'text',
  230.                         'std' => '250'
  231.                     ),
  232.                     array(
  233.                         'name' => 'Land',
  234.                         'id' => $prefix . 'country',
  235.                         'type' => 'select',
  236.                         'options' => array(array("value"=>"Deutschland", 'name'=>"Deutschland"),
  237.                                             array("value"=>"&Ouml;sterreich", 'name'=>"&Ouml;sterreich"),
  238.                                             array("value"=>"Schweiz", 'name'=>"Schweiz"),
  239.                                             array("value"=>"Frankreich", 'name'=>"Frankreich"),
  240.                                             array("value"=>"England", 'name'=>"England"))
  241.                     ),
  242.                     array(
  243.                         'name' => 'Gallery',
  244.                         'id' => $prefix . 'gallery',
  245.                         'type' => 'select',
  246.                         'options' => array()
  247.                     ),
  248.                     array(
  249.                         'name' => 'Start Ort',
  250.                         'id' => $prefix . 'start',
  251.                         'type' => 'text',
  252.                         'std' => 'Hamburg'
  253.                     ),
  254.                     array(
  255.                         'name' => 'Ziel Ort',
  256.                         'id' => $prefix . 'destination',
  257.                         'type' => 'text',
  258.                         'std' => 'Hamburg'
  259.                     ),
  260.                 )
  261.             );
  262.         }
  263.  
  264.  
  265.     }
Advertisement
Add Comment
Please, Sign In to add comment