Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /* Custom post type for registering Trail Maintenance activity, including custom metaboxes.
- For further reference, see:
- http://codex.wordpress.org/Function_Reference/register_post_type
- http://blurback.com/post/1479456356/permissions-with-wordpress-custom-post-types
- https://github.com/jaredatch/Custom-Metaboxes-and-Fields-for-WordPress
- */
- add_action( 'init', 'create_post_type' );
- function create_post_type() {
- register_post_type( 'report',
- array(
- 'labels' => array(
- 'name' => __( 'Trail Maintenance' ),
- 'singular_name' => _x('Event', 'post type singular name'),
- 'all_items' => __('All Activity'),
- 'add_new' => _x('Add new event', 'events'),
- 'add_new_item' => __('Add new trail maintenance event'),
- 'edit_item' => __('Edit event/Add Report'),
- 'new_item' => __('New event'),
- 'view_item' => __('View event'),
- 'search_items' => __('Search in events'),
- 'not_found' => __('No events found'),
- 'not_found_in_trash' => __('No events found in trash'),
- ),
- 'public' => true,
- 'menu_icon' => get_bloginfo('url') . '/wp-content/themes/friendsofthecumberlandtrail/images/map.png',
- 'menu_position' => 0,
- 'capability_type' => 'report',
- 'supports' => array('title','comments', 'revisions'),
- 'has_archive' => 'Trail Maintenance Events',
- array(
- 'public' => true,
- 'capability_type' => 'report'
- )
- )
- );
- $set = get_option('post_type_rules_flased_POST-TYPE-NAME-HERE');
- if ($set !== true){
- flush_rewrite_rules(false);
- update_option('post_type_rules_flased_POST-TYPE-NAME-HERE',true);
- }
- }
- add_filter( 'map_meta_cap', 'my_map_meta_cap', 10, 4 );
- function my_map_meta_cap( $caps, $cap, $user_id, $args ) {
- // almost identical to 'edit page' from
- // <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>
- if ( $cap == 'edit_report'){
- // remove meta cap
- $key = array_search('edit_report', $caps);
- if($key !== false) unset($caps[$key]);
- $author_data = get_userdata( $user_id );
- //echo "post ID: {$args[0]}<br />";
- $page = get_page( $args[0] );
- //print_r($page);
- $page_author_data = get_userdata( $page->post_author );
- //echo "current user id : $user_id, page author id: " . $page_author_data->ID . "<br />";
- // If the user is the author...
- if ( is_object( $page_author_data ) && $user_id == $page_author_data->ID ) {
- // If the page is published...
- if ( 'publish' == $page->post_status ) {
- $caps[] = 'edit_published_reports';
- } elseif ( 'trash' == $page->post_status ) {
- if ('publish' == get_post_meta($page->ID, '_wp_trash_meta_status', true) )
- $caps[] = 'edit_published_reports';
- } else {
- // If the page is draft...
- $caps[] = 'edit_reports';
- }
- } else {
- // The user is trying to edit someone else's page.
- $caps[] = 'edit_others_reports';
- // The page is published, extra cap required.
- if ( 'publish' == $page->post_status )
- $caps[] = 'edit_published_reports';
- elseif ( 'private' == $page->post_status )
- $caps[] = 'edit_private_reports';
- }
- }
- /* Return the capabilities required by the user. */
- return $caps;
- }
- // Custom taxonomy for trail location
- $segments_labels = array(
- 'name' => _x( 'Trail Segment', 'taxonomy general name' ),
- 'singular_name' => _x( 'Segment', 'taxonomy singular name' ),
- 'search_items' => __( 'Search in Trail Segments' ),
- 'popular_items' => __( 'Popular Trail Segments' ),
- 'all_items' => __( 'All Trail Segments' ),
- 'most_used_items' => null,
- 'parent_item' => null,
- 'parent_item_colon' => null,
- 'edit_item' => __( 'Edit Trail Segment' ),
- 'update_item' => __( 'Update Segment' ),
- 'add_new_item' => __( 'Add new Segment' ),
- 'new_item_name' => __( 'New Segment name' ),
- 'separate_items_with_commas' => __( 'Separate Segments with commas' ),
- 'add_or_remove_items' => __( 'Add or remove Segments' ),
- 'choose_from_most_used' => __( 'Choose from the most used Segments' ),
- 'menu_name' => __( 'Trail Segments' ),
- );
- register_taxonomy('trail-segment', array('report'), array(
- 'hierarchical' => true,
- 'labels' => $segments_labels,
- 'show_ui' => true,
- 'context' => 'normal',
- 'priority' => 'high',
- 'capabilities' => array (
- 'manage_terms' => 'manage_options', //by default only admin
- 'edit_terms' => 'manage_options',
- 'delete_terms' => 'manage_options',
- 'assign_terms' => 'edit_reports', // trail management user level
- ),
- 'update_count_callback' => '_update_post_term_count',
- 'query_var' => true,
- 'rewrite' => array('slug' => 'Segment' )
- ));
- //Initialize the metabox class
- function wpb_initialize_cmb_meta_boxes() {
- if ( ! class_exists( 'cmb_Meta_Box' ) )
- require_once(ABSPATH . '/wp-content/themes/friendsofthecumberlandtrail/lib/metabox/init.php');
- }
- add_action( 'init', 'wpb_initialize_cmb_meta_boxes', 9999 );
- //Add Meta Boxes
- function date_time_metaboxes( $meta_boxes ) {
- $prefix = '_cmb_'; // Prefix for all fields
- $meta_boxes[] = array(
- 'id' => 'date-time',
- 'title' => 'Date and Time:',
- 'pages' => array('report'), // post type
- 'context' => 'side',
- 'priority' => 'high',
- 'show_names' => true, // Show field names on the left
- 'pages' => array('report'), // post type
- 'fields' => array(
- array(
- 'name' => 'Start:',
- 'desc' => '',
- 'id' => $prefix . 'start_datetime_timestamp',
- 'type' => 'text_datetime_timestamp',
- ),
- array(
- 'name' => 'End:',
- 'desc' => '',
- 'id' => $prefix . 'end_datetime_timestamp',
- 'type' => 'text_datetime_timestamp',
- ),
- ),
- );
- return $meta_boxes;
- }
- function contact_metaboxes( $meta_boxes2 ) {
- $prefix = '_cmb_'; // Prefix for all fields
- $meta_boxes2[] = array(
- 'id' => 'contact-box',
- 'title' => 'Work Group:',
- 'pages' => array('report'), // post type
- 'context' => 'normal',
- 'priority' => 'high',
- 'show_names' => true, // Show field names on the left
- 'pages' => array('report'), // post type
- 'fields' => array(
- array(
- 'name' => 'Work Group Name',
- 'desc' => '',
- 'id' => $prefix . 'workgroupname',
- 'type' => 'text_medium',
- ),
- array(
- 'name' => 'Contact Cell:',
- 'desc' => '',
- 'id' => $prefix . 'contactnumber',
- 'type' => 'text_medium',
- ),
- array(
- 'name' => 'Names in Party:',
- 'desc' => '',
- 'id' => $prefix . 'namesinparty',
- 'type' => 'text',
- ),
- ),
- );
- return $meta_boxes2;
- }
- function description_metaboxes( $meta_boxes3 ) {
- $prefix = '_cmb_'; // Prefix for all fields
- $meta_boxes3[] = array(
- 'id' => 'description',
- 'title' => 'Planned Maintenance:',
- 'pages' => array('report'), // post type
- 'context' => 'normal',
- 'priority' => 'high',
- 'show_names' => false, // Show field names on the left
- 'pages' => array('report'), // post type
- 'fields' => array(
- array(
- 'name' => 'description',
- 'desc' => 'please describe the maintenance you will be performing',
- 'id' => $prefix . 'description_wysiwyg',
- 'type' => 'wysiwyg',
- 'options' => array(),
- ),
- ),
- );
- return $meta_boxes3;
- }
- function report_metaboxes( $meta_boxes4 ) {
- $prefix = '_cmb_'; // Prefix for all fields
- $meta_boxes4[] = array(
- 'id' => 'report',
- 'title' => 'Maintenance Completion Report:',
- 'pages' => array('report'), // post type
- 'context' => 'normal',
- 'priority' => 'high',
- 'show_names' => false, // Show field names on the left
- 'pages' => array('report'), // post type
- 'fields' => array(
- array(
- 'name' => 'travel_time',
- 'desc' => 'hours of travel time to site',
- 'id' => $prefix . 'traveltime',
- 'type' => 'text_small',
- ),
- array(
- 'name' => 'travel_time',
- 'desc' => 'hours of maintenance completed',
- 'id' => $prefix . 'maint_hours',
- 'type' => 'text_small',
- ),
- array(
- 'name' => 'hazards_blowdowns',
- 'desc' => 'please describe in other comments.',
- 'id' => $prefix . 'test_multicheckbox',
- 'type' => 'multicheck',
- 'options' => array(
- 'check1' => 'Blowdowns?',
- 'check2' => 'Other Hazards?',
- ),
- ),
- ),
- );
- return $meta_boxes4;
- }
- function othercomments_metaboxes( $meta_boxes5 ) {
- $prefix = '_cmb_'; // Prefix for all fields
- $meta_boxes5[] = array(
- 'id' => 'other_comments',
- 'title' => 'Other Comments:',
- 'pages' => array('report'), // post type
- 'context' => 'normal',
- 'priority' => 'high',
- 'show_names' => false, // Show field names on the left
- 'pages' => array('report'), // post type
- 'fields' => array(
- array(
- 'name' => 'other_comments',
- 'desc' => 'please provide additional information about hazards & blowdowns, and other comments.',
- 'id' => $prefix . 'comments_wysiwyg',
- 'type' => 'wysiwyg',
- 'options' => array(),
- ),
- ),
- );
- return $meta_boxes5;
- }
- //keep this
- add_filter( 'cmb_meta_boxes', 'date_time_metaboxes' );
- add_filter( 'cmb_meta_boxes', 'contact_metaboxes' );
- add_filter( 'cmb_meta_boxes', 'description_metaboxes' );
- add_filter( 'cmb_meta_boxes', 'report_metaboxes' );
- add_filter( 'cmb_meta_boxes', 'othercomments_metaboxes' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement