Advertisement
friendsoftheCT

customposttypes.php

Feb 15th, 2013
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.34 KB | None | 0 0
  1. <?php
  2.  
  3. /* Custom post type for registering Trail Maintenance activity, including custom metaboxes.
  4. For further reference, see:
  5. http://codex.wordpress.org/Function_Reference/register_post_type
  6. http://blurback.com/post/1479456356/permissions-with-wordpress-custom-post-types
  7. https://github.com/jaredatch/Custom-Metaboxes-and-Fields-for-WordPress
  8. */
  9.  
  10. add_action( 'init', 'create_post_type' );
  11. function create_post_type() {
  12.   register_post_type( 'report',
  13.     array(
  14.       'labels' => array(
  15.         'name' => __( 'Trail Maintenance' ),
  16.         'singular_name' => _x('Event', 'post type singular name'),
  17.         'all_items' => __('All Activity'),
  18.         'add_new' => _x('Add new event', 'events'),
  19.         'add_new_item' => __('Add new trail maintenance event'),
  20.         'edit_item' => __('Edit event/Add Report'),
  21.         'new_item' => __('New event'),
  22.         'view_item' => __('View event'),
  23.         'search_items' => __('Search in events'),
  24.         'not_found' =>  __('No events found'),
  25.         'not_found_in_trash' => __('No events found in trash'),
  26.       ),
  27.       'public' => true,
  28.       'menu_icon' => get_bloginfo('url') . '/wp-content/themes/friendsofthecumberlandtrail/images/map.png',
  29.       'menu_position' => 0,
  30.       'capability_type' => 'report',
  31.       'supports' => array('title','comments', 'revisions'),
  32.         'has_archive' => 'Trail Maintenance Events',
  33.        
  34.       array(
  35.         'public' => true,
  36.         'capability_type' => 'report'
  37.        
  38.       )
  39.     )
  40.   );
  41.   $set = get_option('post_type_rules_flased_POST-TYPE-NAME-HERE');
  42. if ($set !== true){
  43.    flush_rewrite_rules(false);
  44.    update_option('post_type_rules_flased_POST-TYPE-NAME-HERE',true);
  45. }
  46.    
  47. }
  48.  
  49.  
  50. add_filter( 'map_meta_cap', 'my_map_meta_cap', 10, 4 );
  51. function my_map_meta_cap( $caps, $cap, $user_id, $args ) {
  52.  
  53.   // almost identical to 'edit page' from
  54.   // <a href="http://core.trac.wordpress.org/browser/trunk/wp-includes/capabilities.php?rev=15919#L867">http://core.trac.wordpress.org/browser/trunk/wp-includes/capabilities.php?rev=15919#L867</a>
  55.   if ( $cap == 'edit_report'){
  56.  
  57.     // remove meta cap
  58.     $key = array_search('edit_report', $caps);
  59.     if($key !== false) unset($caps[$key]);
  60.  
  61.     $author_data = get_userdata( $user_id );
  62.     //echo "post ID: {$args[0]}<br />";
  63.     $page = get_page( $args[0] );
  64.     //print_r($page);
  65.     $page_author_data = get_userdata( $page->post_author );
  66.     //echo "current user id : $user_id, page author id: " . $page_author_data->ID . "<br />";
  67.     // If the user is the author...
  68.     if ( is_object( $page_author_data ) && $user_id == $page_author_data->ID ) {
  69.       // If the page is published...
  70.       if ( 'publish' == $page->post_status ) {
  71.         $caps[] = 'edit_published_reports';
  72.       } elseif ( 'trash' == $page->post_status ) {
  73.         if ('publish' == get_post_meta($page->ID, '_wp_trash_meta_status', true) )
  74.           $caps[] = 'edit_published_reports';
  75.       } else {
  76.         // If the page is draft...
  77.         $caps[] = 'edit_reports';
  78.       }
  79.     } else {
  80.       // The user is trying to edit someone else's page.
  81.       $caps[] = 'edit_others_reports';
  82.       // The page is published, extra cap required.
  83.       if ( 'publish' == $page->post_status )
  84.         $caps[] = 'edit_published_reports';
  85.       elseif ( 'private' == $page->post_status )
  86.         $caps[] = 'edit_private_reports';
  87.     }
  88.  
  89.   }
  90.  
  91.   /* Return the capabilities required by the user. */
  92.   return $caps;
  93. }
  94.  
  95.     // Custom taxonomy for trail location
  96.    
  97.     $segments_labels = array(
  98.         'name' => _x( 'Trail Segment', 'taxonomy general name' ),
  99.         'singular_name' => _x( 'Segment', 'taxonomy singular name' ),
  100.         'search_items' =>  __( 'Search in Trail Segments' ),
  101.         'popular_items' => __( 'Popular Trail Segments' ),
  102.         'all_items' => __( 'All Trail Segments' ),
  103.         'most_used_items' => null,
  104.         'parent_item' => null,
  105.         'parent_item_colon' => null,
  106.         'edit_item' => __( 'Edit Trail Segment' ),
  107.         'update_item' => __( 'Update Segment' ),
  108.         'add_new_item' => __( 'Add new Segment' ),
  109.         'new_item_name' => __( 'New Segment name' ),
  110.         'separate_items_with_commas' => __( 'Separate Segments with commas' ),
  111.         'add_or_remove_items' => __( 'Add or remove Segments' ),
  112.         'choose_from_most_used' => __( 'Choose from the most used Segments' ),
  113.         'menu_name' => __( 'Trail Segments' ),
  114.     );
  115.     register_taxonomy('trail-segment', array('report'), array(
  116.         'hierarchical' => true,
  117.         'labels' => $segments_labels,
  118.         'show_ui' => true,
  119.         'context' => 'normal',
  120.         'priority' => 'high',
  121.         'capabilities' => array (
  122.             'manage_terms' => 'manage_options', //by default only admin
  123.             'edit_terms' => 'manage_options',
  124.             'delete_terms' => 'manage_options',
  125.             'assign_terms' => 'edit_reports',  // trail management user level
  126.             ),
  127.         'update_count_callback' => '_update_post_term_count',
  128.         'query_var' => true,
  129.         'rewrite' => array('slug' => 'Segment' )
  130.     ));
  131.    
  132.    
  133.  
  134. //Initialize the metabox class
  135. function wpb_initialize_cmb_meta_boxes() {
  136.     if ( ! class_exists( 'cmb_Meta_Box' ) )
  137.         require_once(ABSPATH . '/wp-content/themes/friendsofthecumberlandtrail/lib/metabox/init.php');
  138. }
  139.  
  140. add_action( 'init', 'wpb_initialize_cmb_meta_boxes', 9999 );
  141.  
  142. //Add Meta Boxes
  143.  
  144. function date_time_metaboxes( $meta_boxes ) {
  145.     $prefix = '_cmb_'; // Prefix for all fields
  146.  
  147.     $meta_boxes[] = array(
  148.         'id' => 'date-time',
  149.         'title' => 'Date and Time:',
  150.         'pages' => array('report'), // post type
  151.         'context' => 'side',
  152.         'priority' => 'high',
  153.         'show_names' => true, // Show field names on the left
  154.         'pages' => array('report'), // post type
  155.         'fields' => array(
  156.  
  157.             array(
  158.                 'name' => 'Start:',
  159.                 'desc' => '',
  160.                 'id'   => $prefix . 'start_datetime_timestamp',
  161.                 'type' => 'text_datetime_timestamp',
  162.             ),
  163.                 array(
  164.                 'name' => 'End:',
  165.                 'desc' => '',
  166.                 'id'   => $prefix . 'end_datetime_timestamp',
  167.                 'type' => 'text_datetime_timestamp',
  168.             ),
  169.         ),
  170.     );
  171.  
  172.     return $meta_boxes;
  173. }
  174.  
  175. function contact_metaboxes( $meta_boxes2 ) {
  176.     $prefix = '_cmb_'; // Prefix for all fields
  177.  
  178.     $meta_boxes2[] = array(
  179.         'id' => 'contact-box',
  180.         'title' => 'Work Group:',
  181.         'pages' => array('report'), // post type
  182.         'context' => 'normal',
  183.         'priority' => 'high',
  184.         'show_names' => true, // Show field names on the left
  185.         'pages' => array('report'), // post type
  186.         'fields' => array(     
  187.  
  188.         array(
  189.                 'name' => 'Work Group Name',
  190.                 'desc' => '',
  191.                 'id'   => $prefix . 'workgroupname',
  192.                 'type' => 'text_medium',
  193.             ),
  194.            
  195.                     array(
  196.                 'name' => 'Contact Cell:',
  197.                 'desc' => '',
  198.                 'id'   => $prefix . 'contactnumber',
  199.                 'type' => 'text_medium',
  200.             ),
  201.        
  202.             array(
  203.                 'name' => 'Names in Party:',
  204.                 'desc' => '',
  205.                 'id'   => $prefix . 'namesinparty',
  206.                 'type' => 'text',
  207.             ),
  208.            
  209.  
  210.         ),
  211.     );
  212.  
  213.     return $meta_boxes2;
  214. }
  215.  
  216. function description_metaboxes( $meta_boxes3 ) {
  217.     $prefix = '_cmb_'; // Prefix for all fields
  218.  
  219.     $meta_boxes3[] = array(
  220.         'id' => 'description',
  221.         'title' => 'Planned Maintenance:',
  222.         'pages' => array('report'), // post type
  223.         'context' => 'normal',
  224.         'priority' => 'high',
  225.         'show_names' => false, // Show field names on the left
  226.         'pages' => array('report'), // post type
  227.         'fields' => array( 
  228.  
  229. array(
  230.     'name' => 'description',
  231.     'desc' => 'please describe the maintenance you will be performing',
  232.     'id' => $prefix . 'description_wysiwyg',
  233.     'type' => 'wysiwyg',
  234.     'options' => array(),
  235. ),
  236. ),
  237. );
  238.     return $meta_boxes3;
  239. }
  240.  
  241. function report_metaboxes( $meta_boxes4 ) {
  242.     $prefix = '_cmb_'; // Prefix for all fields
  243.  
  244.     $meta_boxes4[] = array(
  245.         'id' => 'report',
  246.         'title' => 'Maintenance Completion Report:',
  247.         'pages' => array('report'), // post type
  248.         'context' => 'normal',
  249.         'priority' => 'high',
  250.         'show_names' => false, // Show field names on the left
  251.         'pages' => array('report'), // post type
  252.         'fields' => array( 
  253.         array(
  254.                 'name' => 'travel_time',
  255.                 'desc' => 'hours of travel time to site',
  256.                 'id'   => $prefix . 'traveltime',
  257.                 'type' => 'text_small',
  258.             ),
  259.         array(
  260.                 'name' => 'travel_time',
  261.                 'desc' => 'hours of maintenance completed',
  262.                 'id'   => $prefix . 'maint_hours',
  263.                 'type' => 'text_small',
  264.             ),
  265.        
  266.         array(
  267.                 'name'    => 'hazards_blowdowns',
  268.                 'desc'    => 'please describe in other comments.',
  269.                 'id'      => $prefix . 'test_multicheckbox',
  270.                 'type'    => 'multicheck',
  271.                 'options' => array(
  272.                     'check1' => 'Blowdowns?',
  273.                     'check2' => 'Other Hazards?',
  274.                 ),
  275. ),
  276. ),
  277. );
  278.     return $meta_boxes4;
  279. }
  280.  
  281. function othercomments_metaboxes( $meta_boxes5 ) {
  282.     $prefix = '_cmb_'; // Prefix for all fields
  283.  
  284.     $meta_boxes5[] = array(
  285.         'id' => 'other_comments',
  286.         'title' => 'Other Comments:',
  287.         'pages' => array('report'), // post type
  288.         'context' => 'normal',
  289.         'priority' => 'high',
  290.         'show_names' => false, // Show field names on the left
  291.         'pages' => array('report'), // post type
  292.         'fields' => array( 
  293.  
  294. array(
  295.     'name' => 'other_comments',
  296.     'desc' => 'please provide additional information about hazards & blowdowns, and other comments.',
  297.     'id' => $prefix . 'comments_wysiwyg',
  298.     'type' => 'wysiwyg',
  299.     'options' => array(),
  300. ),
  301. ),
  302. );
  303.     return $meta_boxes5;
  304. }
  305.  
  306.  
  307.  
  308. //keep this
  309.  
  310. add_filter( 'cmb_meta_boxes', 'date_time_metaboxes' );
  311. add_filter( 'cmb_meta_boxes', 'contact_metaboxes' );
  312. add_filter( 'cmb_meta_boxes', 'description_metaboxes' );
  313. add_filter( 'cmb_meta_boxes', 'report_metaboxes' );
  314. add_filter( 'cmb_meta_boxes', 'othercomments_metaboxes' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement