Make WordPress Core

Ticket #16808: trac-16808.php

File trac-16808.php, 3.9 KB (added by alexkoti, 12 years ago)
Line 
1<?php
2/*
3Plugin Name: Trac 16808
4Plugin URI: http://wordpress.org
5Description: Trac 16808
6Author: Alex Koti
7Version: 1
8Author URI: http://alexkoti.com
9*/
10
11/**
12 * Activation
13 * Create subscriber test user
14 * Assign 'argus_visitors' cap to user
15 *
16 */
17register_activation_hook( __FILE__, 'trac16808_activation' );
18function trac16808_activation(){
19 if ( ! current_user_can( 'activate_plugins' ) )
20 return;
21 $plugin = isset( $_REQUEST['plugin'] ) ? $_REQUEST['plugin'] : '';
22 check_admin_referer( "activate-plugin_{$plugin}" );
23
24 // new subscriber user
25 $new_user_id = wp_create_user( 'trac16808', 'trac16808', 'trac16808@trac16808.com' );
26
27 // add 'argus_visitors' cap
28 $role_subscriber = get_role( 'subscriber' );
29 $role_subscriber->add_cap( 'argus_visitors' );
30}
31
32/**
33 * Deactivation
34 * Delete test user and remove 'argus_visitors' cap
35 *
36 */
37register_deactivation_hook( __FILE__, 'trac16808_deactivation' );
38function trac16808_deactivation(){
39 if ( ! current_user_can( 'activate_plugins' ) )
40 return;
41 $plugin = isset( $_REQUEST['plugin'] ) ? $_REQUEST['plugin'] : '';
42 check_admin_referer( "deactivate-plugin_{$plugin}" );
43
44 // remove 'argus_visitors' cap
45 $role_subscriber = get_role( 'subscriber' );
46 $role_subscriber->remove_cap( 'argus_visitors' );
47
48 $user = get_user_by( 'login', 'trac16808' );
49 wp_delete_user( $user->ID, 1 );
50}
51
52/**
53 * Register post types
54 * Cap 'argus_visitors' required
55 * 'show_in_menu' set do 'argus' admin page
56 *
57 */
58add_action( 'init', 'trac16808_register_post_types' );
59function trac16808_register_post_types(){
60 $args = array(
61 'labels' => array (
62 'name' => 'Visitors',
63 'singular_name' => 'Visitor',
64 'add_new' => 'Register New',
65 'add_new_item' => 'Register New Visitor',
66 ),
67 'public' => true,
68 'publicly_queryable' => false,
69 'exclude_from_search' => true,
70 'show_ui' => true,
71 'show_in_menu' => 'argus', // remove this line and the subscriber will can add new post
72 'hiearchical' => false,
73 'supports' => array(
74 'title',
75 'editor',
76 ),
77 'capabilities' => array(
78 'edit_post' => 'argus_visitors',
79 'read_post' => 'argus_visitors',
80 'delete_post' => 'argus_visitors',
81 'edit_posts' => 'argus_visitors',
82 'edit_others_posts' => 'argus_visitors',
83 'publish_posts' => 'argus_visitors',
84 'read_private_posts' => 'argus_visitors',
85 'read' => 'argus_visitors',
86 'delete_posts' => 'argus_visitors',
87 'delete_private_posts' => 'argus_visitors',
88 'delete_published_posts' => 'argus_visitors',
89 'delete_others_posts' => 'argus_visitors',
90 'edit_private_posts' => 'argus_visitors',
91 'edit_published_posts' => 'argus_visitors',
92 ),
93 );
94
95 register_post_type( 'visitor', $args );
96}
97
98/**
99 * Add menus
100 * Set priority to 0 just to add submenu on top
101 *
102 */
103add_action( 'admin_menu', 'trac16808_add_menus', 9 );
104function trac16808_add_menus(){
105 add_menu_page( 'Argus', 'Argus Admin', 'argus_visitors', 'argus', 'trac16808_admin_page_render', false, 40 );
106 add_submenu_page( 'argus', 'Argus Administration', 'Control Panel', 'argus_visitors', 'argus', 'trac16808_admin_page_render' );
107}
108
109/**
110 * Render admin page
111 *
112 */
113function trac16808_admin_page_render(){
114 echo '<div class="wrap"><h2>Argus Admin Page</h2></div>';
115}
116
117/**
118 * Workaround
119 * Add submenu pages with 'post-new.php?post_type=visitor'
120 * Based on wp-includes/post.php function _add_post_type_submenus()
121 *
122 */
123//add_action( 'admin_menu', 'trac16808_add_post_type_submenus', 99 );
124function trac16808_add_post_type_submenus() {
125 foreach ( get_post_types( array( 'show_ui' => true ) ) as $ptype ) {
126 $ptype_obj = get_post_type_object( $ptype );
127 // Sub-menus only.
128 if ( ! $ptype_obj->show_in_menu || $ptype_obj->show_in_menu === true )
129 continue;
130 add_submenu_page( $ptype_obj->show_in_menu, $ptype_obj->labels->add_new, $ptype_obj->labels->add_new_item, $ptype_obj->cap->edit_posts, "post-new.php?post_type=$ptype" );
131 }
132}
133