Advertisement
Guest User

Untitled

a guest
Sep 17th, 2012
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 14.47 KB | None | 0 0
  1. <?php
  2.  
  3. // registration code for booking post type
  4.     function register_booking_posttype() {
  5.         $labels = array(
  6.             'name'              => _x( 'Bookings', 'post type general name' ),
  7.             'singular_name'     => _x( 'Booking', 'post type singular name' ),
  8.             'add_new'           => __( 'Add New' ),
  9.             'add_new_item'      => __( 'Booking' ),
  10.             'edit_item'         => __( 'Booking' ),
  11.             'new_item'          => __( 'Booking' ),
  12.             'view_item'         => __( 'Booking' ),
  13.             'search_items'      => __( 'Booking' ),
  14.             'not_found'         => __( 'Booking' ),
  15.             'not_found_in_trash'=> __( 'Booking' ),
  16.             'parent_item_colon' => __( 'Booking' ),
  17.             'menu_name'         => __( 'Bookings' )
  18.         );
  19.        
  20.         $taxonomies = array();
  21.        
  22.         $supports = array('title','editor','author','thumbnail','excerpt','custom-fields','comments','revisions');
  23.        
  24.         $post_type_args = array(
  25.             'labels'            => $labels,
  26.             'singular_label'    => __('Booking'),
  27.             'public'            => true,
  28.             'show_ui'           => true,
  29.             'publicly_queryable'=> true,
  30.             'query_var'         => true,
  31.             'capability_type'   => 'post',
  32.             'has_archive'       => true,
  33.             'hierarchical'      => false,
  34.             'rewrite'           => array('slug' => 'booking', 'with_front' => false ),
  35.             'supports'          => $supports,
  36.             'menu_position'     => 5,
  37.             'menu_icon'         => 'http://tccoemanager.com/3/wp-content/plugins/easy-content-types//includes/images/icon.png',
  38.             'taxonomies'        => $taxonomies
  39.          );
  40.          register_post_type('booking',$post_type_args);
  41.     }
  42.     add_action('init', 'register_booking_posttype');
  43.  
  44.     $bookings_1_metabox = array(
  45.     'id' => 'bookings',
  46.     'title' => 'Bookings',
  47.     'page' => array('booking'),
  48.     'context' => 'normal',
  49.     'priority' => 'default',
  50.     'fields' => array(
  51.  
  52.                
  53.                 array(
  54.                     'name'          => 'Start Date',
  55.                     'desc'          => '',
  56.                     'id'                => 'iphx_booking_start_date',
  57.                     'class'             => 'iphx_booking_start_date',
  58.                     'type'          => 'date',
  59.                     'rich_editor'   => 0,          
  60.                     'max'           => 0               
  61.                 ),
  62.                            
  63.                 array(
  64.                     'name'          => 'Booking End Date',
  65.                     'desc'          => '',
  66.                     'id'                => 'iphx_end_date',
  67.                     'class'             => 'iphx_end_date',
  68.                     'type'          => 'date',
  69.                     'rich_editor'   => 1,          
  70.                     'max'           => 0               
  71.                 ),
  72.                 )
  73. );         
  74.            
  75. add_action('admin_menu', 'iphx_add_bookings_1_meta_box');
  76. function iphx_add_bookings_1_meta_box() {
  77.  
  78.     global $bookings_1_metabox;    
  79.  
  80.     foreach($bookings_1_metabox['page'] as $page) {
  81.         add_meta_box($bookings_1_metabox['id'], $bookings_1_metabox['title'], 'iphx_show_bookings_1_box', $page, 'normal', 'default', $bookings_1_metabox);
  82.     }
  83. }
  84.  
  85. // function to show meta boxes
  86. function iphx_show_bookings_1_box() {
  87.     global $post;
  88.     global $bookings_1_metabox;
  89.     global $iphx_prefix;
  90.     global $wp_version;
  91.    
  92.     // Use nonce for verification
  93.     echo '<input type="hidden" name="iphx_bookings_1_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
  94.    
  95.     echo '<table class="form-table">';
  96.  
  97.     foreach ($bookings_1_metabox['fields'] as $field) {
  98.         // get current post meta data
  99.  
  100.         $meta = get_post_meta($post->ID, $field['id'], true);
  101.        
  102.         echo '<tr>',
  103.                 '<th style="width:20%"><label for="', $field['id'], '">', stripslashes($field['name']), '</label></th>',
  104.                 '<td class="iphx_field_type_' . str_replace(' ', '_', $field['type']) . '">';
  105.         switch ($field['type']) {
  106.             case 'text':
  107.                 echo '<input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" size="30" style="width:97%" /><br/>', '', stripslashes($field['desc']);
  108.                 break;
  109.             case 'date':
  110.                 if($meta) { $value = iphx_timestamp_to_date($meta); } else {  $value = ''; }
  111.                 echo '<input type="text" class="iphx_datepicker" name="' . $field['id'] . '" id="' . $field['id'] . '" value="'. $value . '" size="30" style="width:97%" />' . '' . stripslashes($field['desc']);
  112.                 break;
  113.             case 'upload':
  114.                 echo '<input type="text" class="iphx_upload_field" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" size="30" style="width:80%" /><input class="iphx_upload_image_button" type="button" value="Upload Image" /><br/>', '', stripslashes($field['desc']);
  115.                 break;
  116.             case 'textarea':
  117.            
  118.                 if($field['rich_editor'] == 1) {
  119.                     if($wp_version >= 3.3) {
  120.                         echo wp_editor($meta, $field['id'], array('textarea_name' => $field['id']));
  121.                     } else {
  122.                         // older versions of WP
  123.                         $editor = '';
  124.                         if(!post_type_supports($post->post_type, 'editor')) {
  125.                             $editor = wp_tiny_mce(true, array('editor_selector' => $field['class'], 'remove_linebreaks' => false) );
  126.                         }
  127.                         $field_html = '<div style="width: 97%; border: 1px solid #DFDFDF;"><textarea name="' . $field['id'] . '" class="' . $field['class'] . '" id="' . $field['id'] . '" cols="60" rows="8" style="width:100%">'. $meta . '</textarea></div><br/>' . __(stripslashes($field['desc']));
  128.                         echo $editor . $field_html;
  129.                     }
  130.                 } else {
  131.                     echo '<div style="width: 100%;"><textarea name="', $field['id'], '" class="', $field['class'], '" id="', $field['id'], '" cols="60" rows="8" style="width:97%">', $meta ? $meta : $field['std'], '</textarea></div>', '', stripslashes($field['desc']);            
  132.                 }
  133.                
  134.                 break;
  135.             case 'select':
  136.                 echo '<select name="', $field['id'], '" id="', $field['id'], '">';
  137.                 foreach ($field['options'] as $option) {
  138.                     echo '<option value="' . $option . '"', $meta == $option ? ' selected="selected"' : '', '>', $option, '</option>';
  139.                 }
  140.                 echo '</select>', '', stripslashes($field['desc']);
  141.                 break;
  142.             case 'radio':
  143.                 foreach ($field['options'] as $option) {
  144.                     echo '<input type="radio" name="', $field['id'], '" value="', $option, '"', $meta == $option ? ' checked="checked"' : '', ' /> ', $option;
  145.                 }
  146.                 echo '<br/>' . stripslashes($field['desc']);
  147.                 break;
  148.             case 'multicheck':
  149.                 foreach ($field['options'] as $option) {
  150.                     echo '<input type="checkbox" name="' . $field['id'] . '[' . $option . ']" value="' . $option . '"' . checked( true, in_array( $option, $meta ), false ) . '/> ' . $option;
  151.                 }
  152.                 echo '<br/>' . stripslashes($field['desc']);
  153.                 break;
  154.             case 'checkbox':
  155.                 echo '<input type="checkbox" name="', $field['id'], '" id="', $field['id'], '"', $meta ? ' checked="checked"' : '', ' /> ';
  156.                 echo stripslashes($field['desc']);
  157.                 break;
  158.             case 'slider':
  159.                 echo '<input type="text" rel="' . $field['max'] . '" name="' . $field['id'] . '" id="' . $field['id'] . '" value="' . $meta . '" size="1" style="float: left; margin-right: 5px" />';
  160.                 echo '<div class="ecpt-slider" rel="' . $field['id'] . '" style="float: left; width: 60%; margin: 5px 0 0 0;"></div>';     
  161.                 echo '<div style="width: 100%; clear: both;">' . stripslashes($field['desc']) . '</div>';
  162.                 break;
  163.             case 'repeatable' :
  164.                
  165.                 $field_html = '<input type="hidden" id="' . $field['id'] . '" class="iphx_repeatable_field_name" value=""/>';
  166.                 if(is_array($meta)) {
  167.                     $count = 1;
  168.                     foreach($meta as $key => $value) {
  169.                         $field_html .= '<div class="iphx_repeatable_wrapper"><input type="text" class="iphx_repeatable_field" name="' . $field['id'] . '[]" id="' . $field['id'] . '[]" value="' . $meta[$key] . '" size="30" style="width:90%" />';
  170.                         if($count > 1) {
  171.                             $field_html .= '<a href="#" class="iphx_remove_repeatable button-secondary">x</a><br/>';
  172.                         }
  173.                         $field_html .= '</div>';
  174.                         $count++;
  175.                     }
  176.                 } else {
  177.                     $field_html .= '<div class="iphx_repeatable_wrapper"><input type="text" class="iphx_repeatable_field" name="' . $field['id'] . '[]" id="' . $field['id'] . '[]" value="' . $meta . '" size="30" style="width:90%" /></div>';
  178.                 }
  179.                 $field_html .= '<button class="iphx_add_new_field button-secondary">' . __('Add New', 'ecpt') . '</button>  ' . __(stripslashes($field['desc']));
  180.                
  181.                 echo $field_html;
  182.                
  183.                 break;
  184.            
  185.             case 'repeatable upload' :
  186.            
  187.                 $field_html = '<input type="hidden" id="' . $field['id'] . '" class="iphx_repeatable_upload_field_name" value=""/>';
  188.                 if(is_array($meta)) {
  189.                     $count = 1;
  190.                     foreach($meta as $key => $value) {
  191.                         $field_html .= '<div class="iphx_repeatable_upload_wrapper"><input type="text" class="iphx_repeatable_upload_field iphx_upload_field" name="' . $field['id'] . '[]" id="' . $field['id'] . '[]" value="' . $meta[$key] . '" size="30" style="width:80%" /><button class="button-secondary iphx_upload_image_button">Upload File</button>';
  192.                         if($count > 1) {
  193.                             $field_html .= '<a href="#" class="iphx_remove_repeatable button-secondary">x</a><br/>';
  194.                         }
  195.                         $field_html .= '</div>';
  196.                         $count++;
  197.                     }
  198.                 } else {
  199.                     $field_html .= '<div class="iphx_repeatable_upload_wrapper"><input type="text" class="iphx_repeatable_upload_field iphx_upload_field" name="' . $field['id'] . '[]" id="' . $field['id'] . '[]" value="' . $meta . '" size="30" style="width:80%" /><input class="button-secondary iphx_upload_image_button" type="button" value="Upload File" /></div>';
  200.                 }
  201.                 $field_html .= '<button class="iphx_add_new_upload_field button-secondary">' . __('Add New', 'ecpt') . '</button>  ' . __(stripslashes($field['desc']));       
  202.            
  203.                 echo $field_html;
  204.            
  205.                 break;
  206.         }
  207.         echo     '<td>',
  208.             '</tr>';
  209.     }
  210.    
  211.     echo '</table>';
  212. }  
  213.  
  214. // Save data from meta box
  215. add_action('save_post', 'iphx_bookings_1_save');
  216. function iphx_bookings_1_save($post_id) {
  217.     global $post;
  218.     global $bookings_1_metabox;
  219.    
  220.     // verify nonce
  221.     if (!wp_verify_nonce($_POST['iphx_bookings_1_meta_box_nonce'], basename(__FILE__))) {
  222.         return $post_id;
  223.     }
  224.  
  225.     // check autosave
  226.     if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
  227.         return $post_id;
  228.     }
  229.  
  230.     // check permissions
  231.     if ('page' == $_POST['post_type']) {
  232.         if (!current_user_can('edit_page', $post_id)) {
  233.             return $post_id;
  234.         }
  235.     } elseif (!current_user_can('edit_post', $post_id)) {
  236.         return $post_id;
  237.     }
  238.    
  239.     foreach ($bookings_1_metabox['fields'] as $field) {
  240.    
  241.         $old = get_post_meta($post_id, $field['id'], true);
  242.         $new = $_POST[$field['id']];
  243.        
  244.         if ($new && $new != $old) {
  245.             if($field['type'] == 'date') {
  246.                 $new = iphx_format_date($new);
  247.                 update_post_meta($post_id, $field['id'], $new);
  248.             } else {
  249.                 if(is_string($new)) {
  250.                     $new = $new;
  251.                 }
  252.                 update_post_meta($post_id, $field['id'], $new);
  253.                
  254.                
  255.             }
  256.         } elseif ('' == $new && $old) {
  257.             delete_post_meta($post_id, $field['id'], $old);
  258.         }
  259.     }
  260. }
  261.  
  262.  
  263. function iphx_export_ui_scripts() {
  264.  
  265.     global $iphx_options;
  266.     ?>
  267.     <script type="text/javascript">
  268.             jQuery(document).ready(function($)
  269.             {
  270.                
  271.                 if($('.form-table .iphx_upload_field').length > 0 ) {
  272.                     // Media Uploader
  273.                     window.formfield = '';
  274.    
  275.                     $('.iphx_upload_image_button').live('click', function() {
  276.                     window.formfield = $('.iphx_upload_field',$(this).parent());
  277.                         tb_show('', 'media-upload.php?type=file&TB_iframe=true');
  278.                                         return false;
  279.                         });
  280.    
  281.                         window.original_send_to_editor = window.send_to_editor;
  282.                         window.send_to_editor = function(html) {
  283.                             if (window.formfield) {
  284.                                 imgurl = $('a','<div>'+html+'</div>').attr('href');
  285.                                 window.formfield.val(imgurl);
  286.                                 tb_remove();
  287.                             }
  288.                             else {
  289.                                 window.original_send_to_editor(html);
  290.                             }
  291.                             window.formfield = '';
  292.                             window.imagefield = false;
  293.                         }
  294.                 }
  295.                 if($('.form-table .ecpt-slider').length > 0 ) {
  296.                     $('.ecpt-slider').each(function(){
  297.                         var $this = $(this);
  298.                         var id = $this.attr('rel');
  299.                         var val = $('#' + id).val();
  300.                         var max = $('#' + id).attr('rel');
  301.                         max = parseInt(max);
  302.                         //var step = $('#' + id).closest('input').attr('rel');
  303.                         $this.slider({
  304.                             value: val,
  305.                             max: max,
  306.                             step: 1,
  307.                             slide: function(event, ui) {
  308.                                 $('#' + id).val(ui.value);
  309.                             }
  310.                         });
  311.                     });
  312.                 }
  313.                
  314.                 if($('.form-table .iphx_datepicker').length > 0 ) {
  315.                     var dateFormat = 'mm/dd/yy';
  316.                     $('.iphx_datepicker').datepicker({dateFormat: dateFormat});
  317.                 }
  318.                
  319.                 // add new repeatable field
  320.                 $(".iphx_add_new_field").on('click', function() {                  
  321.                     var field = $(this).closest('td').find("div.iphx_repeatable_wrapper:last").clone(true);
  322.                     var fieldLocation = $(this).closest('td').find('div.iphx_repeatable_wrapper:last');
  323.                     // set the new field val to blank
  324.                     $('input', field).val("");
  325.                     field.insertAfter(fieldLocation, $(this).closest('td'));
  326.    
  327.                     return false;
  328.                 });    
  329.    
  330.                 // add new repeatable upload field
  331.                 $(".iphx_add_new_upload_field").on('click', function() {   
  332.                     var container = $(this).closest('tr');
  333.                     var field = $(this).closest('td').find("div.iphx_repeatable_upload_wrapper:last").clone(true);
  334.                     var fieldLocation = $(this).closest('td').find('div.iphx_repeatable_upload_wrapper:last');
  335.                     // set the new field val to blank
  336.                     $('input[type="text"]', field).val("");
  337.                
  338.                     field.insertAfter(fieldLocation, $(this).closest('td'));
  339.    
  340.                     return false;
  341.                 });
  342.                
  343.                 // remove repeatable field
  344.                 $('.iphx_remove_repeatable').on('click', function(e) {
  345.                     e.preventDefault();
  346.                     var field = $(this).parent();
  347.                     $('input', field).val("");
  348.                     field.remove();            
  349.                     return false;
  350.                 });                                        
  351.                                                        
  352.             });
  353.       </script>
  354.     <?php
  355. }
  356.  
  357. function iphx_export_datepicker_ui_scripts() {
  358.     global $iphx_base_dir;
  359.     wp_enqueue_script('jquery-ui-datepicker');
  360.     wp_enqueue_script('jquery-ui-slider');
  361. }
  362. function iphx_export_datepicker_ui_styles() {
  363.     global $iphx_base_dir;
  364.     wp_enqueue_style('jquery-ui-css', 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css', false, '1.8', 'all');
  365. }
  366.  
  367. // these are for newest versions of WP
  368. add_action('admin_print_scripts-post.php', 'iphx_export_datepicker_ui_scripts');
  369. add_action('admin_print_scripts-edit.php', 'iphx_export_datepicker_ui_scripts');
  370. add_action('admin_print_scripts-post-new.php', 'iphx_export_datepicker_ui_scripts');
  371. add_action('admin_print_styles-post.php', 'iphx_export_datepicker_ui_styles');
  372. add_action('admin_print_styles-edit.php', 'iphx_export_datepicker_ui_styles');
  373. add_action('admin_print_styles-post-new.php', 'iphx_export_datepicker_ui_styles');
  374.  
  375. if ((isset($_GET['post']) && (isset($_GET['action']) && $_GET['action'] == 'edit') ) || (strstr($_SERVER['REQUEST_URI'], 'wp-admin/post-new.php')))
  376. {
  377.     add_action('admin_head', 'iphx_export_ui_scripts');
  378. }
  379.  
  380. // converts a time stamp to date string for meta fields
  381. if(!function_exists('iphx_timestamp_to_date')) {
  382.     function iphx_timestamp_to_date($date) {
  383.        
  384.         return date('m/d/Y', $date);
  385.     }
  386. }
  387. if(!function_exists('iphx_format_date')) {
  388.     function iphx_format_date($date) {
  389.  
  390.         $date = strtotime($date);
  391.        
  392.         return $date;
  393.     }
  394. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement