Advertisement
Guest User

Questions

a guest
Mar 29th, 2013
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.10 KB | None | 0 0
  1. // The initial set-up requires initialising two post types, questions and answers,
  2. // also managing capabilities for these.
  3. function register_question_post_types() {
  4.    
  5.     register_post_type(
  6.         'question',
  7.         array(
  8.             'label' => 'Questions',
  9.             'labels' => array(
  10.                 'name' => 'Questions',
  11.                 'singular_name' => 'Question',
  12.                 'edit_item' => 'Edit Question',
  13.                 'view_item' => 'View Question'
  14.             ),
  15.             'exclude_from_search' => false,
  16.             'publicly_queryable' => true,
  17.             'show_ui' => true,
  18.             'capability_type' => 'question',
  19.             'capabilites' => array(
  20.                 'publish_posts' => 'publish_questions',
  21.                 'edit_posts' => 'edit_questions',
  22.                 'edit_others_posts' => 'edit_others_questions',
  23.                 'delete_posts' => 'delete_questions',
  24.                 'delete_others_posts' => 'delete_others_questions',
  25.                 'read_private_posts' => 'read_private_questions',
  26.                 'edit_post' => 'edit_question',
  27.                 'delete_post' => 'delete_question',
  28.                 'read_post' => 'read_question',
  29.             ),
  30.             'supports' => array( 'title', 'editor', 'author', 'excerpt', 'comments', 'revisions' ),
  31.             'has_archive' => true
  32.         )
  33.     );
  34.  
  35.     register_post_type(
  36.         'answer',
  37.         array(
  38.             'label' => 'Answers',
  39.             'labels' => array(
  40.                 'name' => 'Answers',
  41.                 'singular_name' => 'Answer',
  42.                 'edit_item' => 'Edit Answer',
  43.                 'view_item' => 'View Question'
  44.             ),
  45.             'exclude_from_search' => false,
  46.             'publicly_queryable' => false,
  47.             'show_ui' => true,
  48.             'show_in_menu' => 'edit.php?post_type=question', // Display within the "Questions" menu
  49.             'capability_type' => 'answer',
  50.             'capabilites' => array(
  51.                 'publish_posts' => 'publish_answers',
  52.                 'edit_posts' => 'edit_answers',
  53.                 'edit_others_posts' => 'edit_others_answers',
  54.                 'delete_posts' => 'delete_answers',
  55.                 'delete_others_posts' => 'delete_others_answers',
  56.                 'read_private_posts' => 'read_private_answers',
  57.                 'edit_post' => 'edit_answer',
  58.                 'delete_post' => 'delete_answer',
  59.                 'read_post' => 'read_answer',
  60.             ),
  61.             'supports' => array( 'title', 'editor', 'author', 'comments', 'revisions' )
  62.         )
  63.     );
  64.  
  65.     add_filter( 'map_meta_cap', 'questions_map_meta_cap', 10, 4 );
  66.     add_filter( 'map_meta_cap', 'answers_map_meta_cap', 10, 4 );
  67.  
  68.     // Mapping the meta capabilities so that users can edit their own questions/answers, publish
  69.     // them, delete them, etc. Borrowed from Justin Tadlock:
  70.     // [ http://justintadlock.com/archives/2010/07/10/meta-capabilities-for-custom-post-types ]
  71.     function questions_map_meta_cap( $caps, $cap, $user_id, $args ) {
  72.  
  73.         /* If editing, deleting, or reading a question, get the post and post type object. */
  74.         if ( 'edit_question' == $cap || 'delete_question' == $cap || 'read_question' == $cap ) {
  75.             $post = get_post( $args[0] );
  76.             $post_type = get_post_type_object( $post->post_type );
  77.  
  78.             /* Set an empty array for the caps. */
  79.             $caps = array();
  80.         }
  81.  
  82.         /* If editing a question, assign the required capability. */
  83.         if ( 'edit_question' == $cap ) {
  84.             if ( $user_id == $post->post_author )
  85.                 $caps[] = $post_type->cap->edit_posts;
  86.             else
  87.                 $caps[] = $post_type->cap->edit_others_posts;
  88.         }
  89.  
  90.         /* If deleting a question, assign the required capability. */
  91.         elseif ( 'delete_question' == $cap ) {
  92.             if ( $user_id == $post->post_author )
  93.                 $caps[] = $post_type->cap->delete_posts;
  94.             else
  95.                 $caps[] = $post_type->cap->delete_others_posts;
  96.         }
  97.  
  98.         /* If reading a private question, assign the required capability. */
  99.         elseif ( 'read_question' == $cap ) {
  100.  
  101.             if ( 'private' != $post->post_status )
  102.                 $caps[] = 'read';
  103.             elseif ( $user_id == $post->post_author )
  104.                 $caps[] = 'read';
  105.             else
  106.                 $caps[] = $post_type->cap->read_private_posts;
  107.         }
  108.  
  109.         /* Return the capabilities required by the user. */
  110.         return $caps;
  111.     }
  112.  
  113.     // Do it all again for answers
  114.     function answers_map_meta_cap( $caps, $cap, $user_id, $args ) {
  115.  
  116.         /* If editing, deleting, or reading a answer, get the post and post type object. */
  117.         if ( 'edit_answer' == $cap || 'delete_answer' == $cap || 'read_answer' == $cap ) {
  118.             $post = get_post( $args[0] );
  119.             $post_type = get_post_type_object( $post->post_type );
  120.  
  121.             /* Set an empty array for the caps. */
  122.             $caps = array();
  123.         }
  124.  
  125.         /* If editing a answer, assign the required capability. */
  126.         if ( 'edit_answer' == $cap ) {
  127.             if ( $user_id == $post->post_author )
  128.                 $caps[] = $post_type->cap->edit_posts;
  129.             else
  130.                 $caps[] = $post_type->cap->edit_others_posts;
  131.         }
  132.  
  133.         /* If deleting a answer, assign the required capability. */
  134.         elseif ( 'delete_answer' == $cap ) {
  135.             if ( $user_id == $post->post_author )
  136.                 $caps[] = $post_type->cap->delete_posts;
  137.             else
  138.                 $caps[] = $post_type->cap->delete_others_posts;
  139.         }
  140.  
  141.         /* If reading a private answer, assign the required capability. */
  142.         elseif ( 'read_answer' == $cap ) {
  143.  
  144.             if ( 'private' != $post->post_status )
  145.                 $caps[] = 'read';
  146.             elseif ( $user_id == $post->post_author )
  147.                 $caps[] = 'read';
  148.             else
  149.                 $caps[] = $post_type->cap->read_private_posts;
  150.         }
  151.  
  152.         /* Return the capabilities required by the user. */
  153.         return $caps;
  154.     }
  155.  
  156. }
  157.  
  158. // Run all that at init
  159. add_action( 'init', 'register_question_post_types' );
  160.  
  161. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement