Advertisement
foomagoo

New custom review Type

Mar 23rd, 2011
552
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.28 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Test Plug
  4. Plugin URI: http://www.nebraskadigital.com/2010/12/27/plugin-organizer/
  5. Description: Test Plugin
  6. Version: 0.1
  7. Author: Jeff Sterup
  8. Author URI: http://www.jsterup.com
  9. */
  10.  
  11.  
  12.  
  13. /* ****************************************** START CUSTOM POST TYPES *************************************************** */
  14.  
  15.  
  16. add_action( 'init', 'add_post_type' );
  17.  
  18.  
  19. function add_post_type() {
  20.  
  21. $labels = array(
  22. 'name' => __( 'Reviews + Custom Fields' ),
  23. 'singular_name' => __( 'Post Your Review' ),
  24. 'add_new' => __( 'Add A New Review' ),
  25. 'add_new_item' => __( 'Add A New Review' ),
  26. 'edit' => __( 'Edit Reviews' ),
  27. 'edit_item' => __( 'Edit Reviews' ),
  28. 'new_item' => __( 'New Review' ),
  29. 'view' => __( 'View This Review' ),
  30. 'view_item' => __( 'View This Review' ),
  31. 'search_items' => __( 'Search Reviews' ),
  32. 'not_found' => __( 'No Reviews Found' ),
  33. 'not_found_in_trash' => __( 'No Reviews Found In Trash' ),
  34. 'parent' => __( 'Parent Review' ),
  35. );
  36.  
  37.  
  38. $settings = array(
  39. 'labels' => $labels,
  40. 'show_ui' => true,
  41. 'publicly_queryable' => true,
  42. 'exclude_from_search' => false,
  43. 'description' => __( 'Brilliant Reviews ' ),
  44. 'menu_position' => 5,
  45. 'menu_icon' => get_stylesheet_directory_uri() . '/images/review.png',
  46. 'hierarchical' => false,
  47. 'query_var' => true,
  48. 'capability_type' => 'review',
  49. 'supports' => array( 'title', 'editor', 'comments','trackbacks', 'revisions','author', 'excerpt', 'thumbnail','custom-fields', 'page-attributes' ),
  50. '_builtin' => false,
  51. 'rewrite' => array( 'slug' => 'blog', 'with_front' => false ),
  52. 'taxonomies' => array( 'book_category', 'other_books'),
  53. 'can_export' => true,
  54. //Permalink endpoint mask, whatever that is.
  55. );
  56. register_post_type('review',$settings );
  57. }
  58.  
  59.  
  60. /* ******************************************* END CUSTOM POST TYPES ************************************************ */
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69. /* ************************************** START MAPPING META CAPABILITIES ******************************************* */
  70.  
  71. /* ************************************** START MAPPING META CAPABILITIES ******************************************* */
  72.  
  73. add_filter( 'map_meta_cap', 'my_map_meta_cap', 10, 4 );
  74.  
  75. function my_map_meta_cap( $caps, $cap, $user_id, $args ) {
  76.  
  77. // If editing, deleting, or reading a review, get the post and post type object.
  78. if ( 'edit_review' == $cap || 'delete_review' == $cap || 'read_review' == $cap ) {
  79. $post = get_post( $args[0] );
  80. $post_type = get_post_type_object( $post->post_type );
  81.  
  82. // Set an empty array for the caps.
  83. $caps = array();
  84. }
  85.  
  86. // If editing a review, assign the required capability.
  87. if ( 'edit_review' == $cap ) {
  88. if ( $user_id == $post->post_author )
  89. $caps[] = $post_type->cap->edit_posts;
  90. else
  91. $caps[] = $post_type->cap->edit_others_posts;
  92. }
  93.  
  94. // If deleting a review, assign the required capability.
  95. elseif ( 'delete_review' == $cap ) {
  96. if ( $user_id == $post->post_author )
  97. $caps[] = $post_type->cap->delete_posts;
  98. else
  99. $caps[] = $post_type->cap->delete_others_posts;
  100. }
  101.  
  102. // If reading a private review, assign the required capability.
  103. elseif ( 'read_review' == $cap ) {
  104.  
  105. if ( 'private' != $post->post_status )
  106. $caps[] = 'read';
  107. elseif ( $user_id == $post->post_author )
  108. $caps[] = 'read';
  109. else
  110. $caps[] = $post_type->cap->read_private_posts;
  111. }
  112.  
  113. // Return the capabilities required by the user.
  114. return $caps;
  115. }
  116.  
  117.  
  118. // With thanks to: http://justintadlock.com/archives/2010/07/10/meta-capabilities-for-custom-post-types#comment-232645
  119.  
  120. /* ************************************ END MAPPING META CAPABILITIES ****************************************** */
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131. /* ************************************ START ADDING ROLES **************************************************** */
  132. function add_new_roles() {
  133. //remove_role('reviewer');
  134.  
  135. $AdaptRole = get_role('subscriber');
  136. $caps = $AdaptRole->capabilities;
  137. $AdaptRole = add_role('reviewer', 'Reviewer', $caps);
  138. if (!$AdaptRole) {
  139. $AdaptRole = get_role('reviewer');
  140. }
  141. //$AdaptRole = get_role('reviewer'); // Get the role Reviewer, and then add some of the capabilities below.
  142.  
  143. // $AdaptRole -> add_cap('delete_others_reviews');
  144. $AdaptRole -> add_cap('delete_review');
  145. $AdaptRole -> add_cap('delete_reviews');
  146. //$AdaptRole -> add_cap('delete_private_reviews');
  147. //$AdaptRole -> add_cap('delete_published_reviews');
  148. // $AdaptRole -> add_cap('edit_others_reviews');
  149. // $AdaptRole -> add_cap('edit_private_reviews');
  150. //$AdaptRole -> add_cap('edit_published_reviews');
  151. //$AdaptRole -> add_cap('publish_reviews');
  152. //$AdaptRole -> add_cap('read_private_reviews');
  153. $AdaptRole -> add_cap('edit_review');
  154. $AdaptRole -> add_cap('edit_reviews');
  155. $AdaptRole -> add_cap('read_reviews');
  156. //$AdaptRole -> add_cap('edit_posts');
  157. $AdaptRole -> add_cap('upload_files');
  158. $AdaptRole -> add_cap('manage_book_category');
  159. $AdaptRole -> add_cap('manage_other_books');
  160.  
  161.  
  162.  
  163.  
  164. $AdaptRole2 = get_role('administrator'); // So the administrator has access as well :-)
  165.  
  166. $AdaptRole2 -> add_cap('delete_others_reviews');
  167. $AdaptRole2 -> add_cap('delete_review');
  168. $AdaptRole2 -> add_cap('delete_reviews');
  169. $AdaptRole2 -> add_cap('delete_private_reviews');
  170. $AdaptRole2 -> add_cap('delete_published_reviews');
  171. $AdaptRole2 -> add_cap('edit_others_reviews');
  172. $AdaptRole2 -> add_cap('edit_review');
  173. $AdaptRole2 -> add_cap('edit_reviews');
  174. $AdaptRole2 -> add_cap('edit_private_reviews');
  175. $AdaptRole2 -> add_cap('edit_published_reviews');
  176. $AdaptRole2 -> add_cap('publish_reviews');
  177. $AdaptRole2 -> add_cap('read_private_reviews');
  178. $AdaptRole2 -> add_cap('read_reviews');
  179. $AdaptRole2 -> add_cap('manage_other_books');
  180. }
  181.  
  182. add_action( 'init', 'add_new_roles'); // Having created your taxonomy with the function create_my_taxonomies this line calls it.
  183. /* ***************************************** END ADDING ROLES *************************************************** */
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192. /* *********************************** START CUSTOM TAXONOMIES ************************************************* */
  193.  
  194. function create_my_taxonomies() {
  195.  
  196.  
  197. $labels = array(
  198. 'name' => __( 'Other Books By This Author' ),
  199. 'singular_name' => __( 'Another Book By This Author' ),
  200. 'search_items' => __( 'Search All Other Books By This Author' ),
  201. 'popular_items' => __( 'Popular Books By This Author' ),
  202. 'all_items' => __( 'All Other Books' ),
  203. 'parent_item' => __( 'Parent Term' ),
  204. 'parent_item_colon' => __( 'Parent Other Books:' ),
  205. 'edit_item' => __( 'Edit This Term' ),
  206. 'update_item' => __( 'Update Other Books' ),
  207. 'add_new_item' => __( 'Add Another Book By This Author' ),
  208. 'new_item_name' => __( 'New Other Books Name' ),
  209. );
  210.  
  211. $settings = array(
  212. 'public' => true,
  213. 'show_ui' => true,
  214. 'show_tagcloud' => true,
  215. 'show_in_nav_menus' => true,
  216. 'hierarchical' => false,
  217. 'labels' => $labels,
  218. 'query_var' => 'other_books',
  219. 'rewrite' => array( 'slug' => 'otherbookslug', 'with_front' => false),
  220. 'capability_type' => 'other_books',
  221. 'capabilities' => array('assign_terms'=>'edit_reviews','manage_terms' => 'manage_other_books','edit_terms' => 'manage_other_books','delete_terms' => 'manage_other_books'),
  222.  
  223. );
  224. register_taxonomy( 'other_books', array( 'review' ), $settings);
  225.  
  226. $labels = array(
  227. 'name' => __( 'Book Categories' ),
  228. 'singular_name' => __( 'Book Category' ),
  229. 'search_items' => __( 'Search Book Categories' ),
  230. 'popular_items' => __( 'Popular Book Categories' ),
  231. 'all_items' => __( 'All Book Categories' ),
  232. 'parent_item' => __( 'Parent Book Category' ),
  233. 'parent_item_colon' => __( 'Parent Book Category:' ),
  234. 'edit_item' => __( 'Edit This Book Category' ),
  235. 'update_item' => __( 'Update Book Category' ),
  236. 'add_new_item' => __( 'Add Another Book Category' ),
  237. 'new_item_name' => __( 'New Book Category' ),
  238. );
  239.  
  240. $settings = array(
  241. 'public' => true,
  242. 'show_ui' => true,
  243. 'hierarchical' => true,
  244. 'labels' => $labels,
  245. 'rewrite' => array( 'slug' => 'book_category'),
  246. 'capability_type' => 'book_category',
  247. 'capabilities' => array('assign_terms'=>'edit_reviews','manage_terms' => 'manage_book_category','edit_terms' => 'manage_book_category','delete_terms' => 'manage_book_category'),
  248.  
  249. );
  250. register_taxonomy( 'book_category', array( 'review' ), $settings);
  251. }
  252.  
  253. add_action( 'init', 'create_my_taxonomies', 0 ); // Having created your taxonomy with the function create_my_taxonomies this line calls it.
  254.  
  255.  
  256. remove_filter( 'pre_term_description', 'wp_filter_kses' );
  257. // Allow XHTML in term descriptions e.g. the terms in a taxonomy can have a description, and this allows XHTML (such as an image) in that description.
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264. /* ******* Start sending specific terms to a single post (where comments are allowed) ********************* */
  265.  
  266. add_action( 'template_redirect', 'my_redirect_term_to_post' );
  267.  
  268. function my_redirect_term_to_post() {
  269. global $wp_query;
  270.  
  271. if ( is_tax() ) {
  272. $term = $wp_query->get_queried_object();
  273.  
  274.  
  275.  
  276. if ( 'other_books' == $term->taxonomy ) {
  277. $post_id = my_get_post_id_by_slug( $term->slug, 'post' ); // single-default.php will be used to display the content of the post
  278.  
  279. // Change post to page or your custom post type e.g. review e.g. $post_id = my_get_post_id_by_slug( $term->slug, 'review' );
  280. // single-review.php would be used to display the content of the post
  281.  
  282. if ( !empty( $post_id ) )
  283. wp_redirect( get_permalink( $post_id ), 301 );
  284. }
  285. }
  286. }
  287.  
  288.  
  289. function my_get_post_id_by_slug( $slug, $post_type ) {
  290. global $wpdb;
  291.  
  292. $slug = rawurlencode( urldecode( $slug ) );
  293. $slug = sanitize_title( basename( $slug ) );
  294.  
  295.  
  296. $post_id = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_name = %s AND post_type = %s", $slug, $post_type ) );
  297.  
  298.  
  299. if ( is_array( $post_id ) )
  300. return $post_id[0];
  301. elseif ( !empty( $post_id ) );
  302. return $post_id;
  303.  
  304. return false;
  305. }
  306.  
  307.  
  308. /* ******* End sending specific terms to a single post (where comments are allowed) ********************* */
  309.  
  310.  
  311.  
  312.  
  313. /* ***************************************** END CUSTOM TAXONOMIES ************************************** */
  314. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement