<?php
/*
Plugin Name: Trac 16808
Plugin URI: http://wordpress.org
Description: Trac 16808
Author: Alex Koti
Version: 1
Author URI: http://alexkoti.com
*/

/**
 * Activation
 * Create subscriber test user
 * Assign 'argus_visitors' cap to user
 * 
 */
register_activation_hook( __FILE__, 'trac16808_activation' );
function trac16808_activation(){
	if ( ! current_user_can( 'activate_plugins' ) )
		return;
	$plugin = isset( $_REQUEST['plugin'] ) ? $_REQUEST['plugin'] : '';
	check_admin_referer( "activate-plugin_{$plugin}" );
	
	// new subscriber user
	$new_user_id = wp_create_user( 'trac16808', 'trac16808', 'trac16808@trac16808.com' );
	
	// add 'argus_visitors' cap
	$role_subscriber = get_role( 'subscriber' );
	$role_subscriber->add_cap( 'argus_visitors' );
}

/**
 * Deactivation
 * Delete test user and remove 'argus_visitors' cap
 * 
 */
register_deactivation_hook( __FILE__, 'trac16808_deactivation' );
function trac16808_deactivation(){
	if ( ! current_user_can( 'activate_plugins' ) )
		return;
	$plugin = isset( $_REQUEST['plugin'] ) ? $_REQUEST['plugin'] : '';
	check_admin_referer( "deactivate-plugin_{$plugin}" );
	
	// remove 'argus_visitors' cap
	$role_subscriber = get_role( 'subscriber' );
	$role_subscriber->remove_cap( 'argus_visitors' );
	
	$user = get_user_by( 'login', 'trac16808' );
	wp_delete_user( $user->ID, 1 );
}

/**
 * Register post types
 * 
 * 1) Argus > Visitor    : cap > admin, subscriber
 * 2) Argus > Comics     : cap > admin, subscriber
 * 3) Argus > Candies    : cap > admin
 * 4) Top Level > Toys   : cap > admin, subscriber
 * 5) Top Level > Books  : cap > admin
 * 
 */
add_action( 'init', 'trac16808_register_post_types' );
function trac16808_register_post_types(){
	
	/**
	 * CPT Visitors inside Argus admin page 
	 * Subscriber have permission to add new posts
	 * 
	 */
	$args = array(
		'labels' => array (
			'name' => 'Visitors',
			'singular_name' => 'Visitor',
			'add_new' => 'Register New',
			'add_new_item' => 'Register New Visitor',
		),
		'public' => true,
		'publicly_queryable' => false,
		'exclude_from_search' => true,
		'show_ui' => true,
		'show_in_menu' => 'argus', // remove this line and the subscriber will can add new post
		'hiearchical' => false,
		'supports' => array(
			'title',
			'editor',
		),
		'capabilities' => array(
			'edit_post'              => 'argus_visitors',
			'read_post'              => 'argus_visitors',
			'delete_post'            => 'argus_visitors',
			'edit_posts'             => 'argus_visitors',
			'edit_others_posts'      => 'argus_visitors',
			'publish_posts'          => 'argus_visitors',
			'read_private_posts'     => 'argus_visitors',
			'read'                   => 'argus_visitors',
			'delete_posts'           => 'argus_visitors',
			'delete_private_posts'   => 'argus_visitors',
			'delete_published_posts' => 'argus_visitors',
			'delete_others_posts'    => 'argus_visitors',
			'edit_private_posts'     => 'argus_visitors',
			'edit_published_posts'   => 'argus_visitors',
		),
	);
	register_post_type( 'visitor', $args );
	
	/**
	 * CPT Comics inside Argus admin page 
	 * Subscriber have permission to add new posts
	 * 
	 */
	$args = array(
		'labels' => array (
			'name' => 'Comics',
			'singular_name' => 'Comic',
			'add_new' => 'Add New',
			'add_new_item' => 'Add New Comic',
		),
		'public' => true,
		'publicly_queryable' => false,
		'exclude_from_search' => true,
		'show_ui' => true,
		'show_in_menu' => 'argus', // remove this line and the subscriber will can add new post
		'hiearchical' => false,
		'supports' => array(
			'title',
			'editor',
		),
		'capabilities' => array(
			'edit_post'              => 'argus_visitors',
			'read_post'              => 'argus_visitors',
			'delete_post'            => 'argus_visitors',
			'edit_posts'             => 'argus_visitors',
			'edit_others_posts'      => 'argus_visitors',
			'publish_posts'          => 'argus_visitors',
			'read_private_posts'     => 'argus_visitors',
			'read'                   => 'argus_visitors',
			'delete_posts'           => 'argus_visitors',
			'delete_private_posts'   => 'argus_visitors',
			'delete_published_posts' => 'argus_visitors',
			'delete_others_posts'    => 'argus_visitors',
			'edit_private_posts'     => 'argus_visitors',
			'edit_published_posts'   => 'argus_visitors',
		),
	);
	register_post_type( 'comic', $args );
	
	/**
	 * CPT Candies inside Argus admin page 
	 * Subscriber don't have permission to add new posts
	 * 
	 */
	$args = array(
		'labels' => array (
			'name' => 'Candies',
			'singular_name' => 'Candy',
			'add_new' => 'Add New',
			'add_new_item' => 'Add New Candy',
		),
		'public' => true,
		'publicly_queryable' => false,
		'exclude_from_search' => true,
		'show_ui' => true,
		'show_in_menu' => 'argus',
		'hiearchical' => false,
		'supports' => array(
			'title',
			'editor',
		),
		'capabilities' => array(
			'edit_post'              => 'manage_options',
			'read_post'              => 'manage_options',
			'delete_post'            => 'manage_options',
			'edit_posts'             => 'manage_options',
			'edit_others_posts'      => 'manage_options',
			'publish_posts'          => 'manage_options',
			'read_private_posts'     => 'manage_options',
			'read'                   => 'manage_options',
			'delete_posts'           => 'manage_options',
			'delete_private_posts'   => 'manage_options',
			'delete_published_posts' => 'manage_options',
			'delete_others_posts'    => 'manage_options',
			'edit_private_posts'     => 'manage_options',
			'edit_published_posts'   => 'manage_options',
		),
	);
	register_post_type( 'candy', $args );
	
	/**
	 * Regular CPT Books
	 * Subscriber have permission to add new posts
	 * 
	 */
	$args = array(
		'labels' => array (
			'name' => 'Toys',
			'singular_name' => 'Toy',
			'add_new' => 'Add New',
			'add_new_item' => 'Add New Toy',
		),
		'public' => true,
		'publicly_queryable' => true,
		'exclude_from_search' => false,
		'show_ui' => true,
		'show_in_menu' => true, 
		'hiearchical' => false,
		'supports' => array(
			'title',
			'editor',
		),
		'capabilities' => array(
			'edit_post'              => 'argus_visitors',
			'read_post'              => 'argus_visitors',
			'delete_post'            => 'argus_visitors',
			'edit_posts'             => 'argus_visitors',
			'edit_others_posts'      => 'argus_visitors',
			'publish_posts'          => 'argus_visitors',
			'read_private_posts'     => 'argus_visitors',
			'read'                   => 'argus_visitors',
			'delete_posts'           => 'argus_visitors',
			'delete_private_posts'   => 'argus_visitors',
			'delete_published_posts' => 'argus_visitors',
			'delete_others_posts'    => 'argus_visitors',
			'edit_private_posts'     => 'argus_visitors',
			'edit_published_posts'   => 'argus_visitors',
		),
	);
	register_post_type( 'toy', $args );
	
	/**
	 * Regular CPT Book
	 * Subscriber don't have permission to add new posts
	 * 
	 */
	$args = array(
		'labels' => array (
			'name' => 'Books',
			'singular_name' => 'Book',
			'add_new' => 'Add New',
			'add_new_item' => 'Add New Book',
		),
		'public' => true,
		'publicly_queryable' => true,
		'exclude_from_search' => false,
		'show_ui' => true,
		'show_in_menu' => true, 
		'hiearchical' => false,
		'supports' => array(
			'title',
			'editor',
		),
		'capabilities' => array(
			'edit_post'              => 'manage_options',
			'read_post'              => 'manage_options',
			'delete_post'            => 'manage_options',
			'edit_posts'             => 'manage_options',
			'edit_others_posts'      => 'manage_options',
			'publish_posts'          => 'manage_options',
			'read_private_posts'     => 'manage_options',
			'read'                   => 'manage_options',
			'delete_posts'           => 'manage_options',
			'delete_private_posts'   => 'manage_options',
			'delete_published_posts' => 'manage_options',
			'delete_others_posts'    => 'manage_options',
			'edit_private_posts'     => 'manage_options',
			'edit_published_posts'   => 'manage_options',
		),
	);
	register_post_type( 'book', $args );
}

/**
 * Add menus
 * Set priority to 0 just to add submenu on top
 * 
 */
add_action( 'admin_menu', 'trac16808_add_menus', 9 );
function trac16808_add_menus(){
	add_menu_page( 'Argus', 'Argus Admin', 'argus_visitors', 'argus', 'trac16808_admin_page_render', false, 40 );
	add_submenu_page( 'argus', 'Argus Administration', 'Control Panel', 'argus_visitors', 'argus', 'trac16808_admin_page_render' );
}

/**
 * Render admin page
 * 
 */
function trac16808_admin_page_render(){
	echo '<div class="wrap"><h2>Argus Admin Page</h2></div>';
}

/**
 * Workaround
 * Add submenu pages with 'post-new.php?post_type=visitor'
 * Based on wp-includes/post.php function _add_post_type_submenus()
 * 
 */
//add_action( 'admin_menu', 'trac16808_add_post_type_submenus', 99 );
function trac16808_add_post_type_submenus(){
	foreach ( get_post_types( array( 'show_ui' => true ) ) as $ptype ) {
		$ptype_obj = get_post_type_object( $ptype );
		// Sub-menus only.
		if ( ! $ptype_obj->show_in_menu || $ptype_obj->show_in_menu === true )
			continue;
		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" );
	}
}
