<?php

/**
 * Roles and Capabilities.
 *
 * @link    https://aiopodcasting.com
 * @since   1.0.0
 *
 * @package AiO_Podcast_Studio
 */

// Exit if accessed directly
if ( ! defined( 'WPINC' ) ) {
	die;
}

/**
 * AIOPOD_Roles class.
 *
 * @since 1.0.0
 */
class AIOPOD_Roles {

	/**
	 * Add new capabilities.
	 *
	 * @since 1.0.0
	 */
	public function add_caps() {	
		global $wp_roles;

		if ( class_exists( 'WP_Roles' ) ) {
			if ( ! isset( $wp_roles ) ) {
				$wp_roles = new WP_Roles();
			}
		}

		if ( is_object( $wp_roles ) ) {
			// Add the "administrator" capabilities
			$capabilities = $this->get_core_caps();
			foreach ( $capabilities as $cap_group ) {
				foreach ( $cap_group as $cap ) {
					$wp_roles->add_cap( 'administrator', $cap );
				}
			}
			$wp_roles->add_cap( 'administrator', 'manage_aiopod_options' );
			
			// Add the "editor" capabilities
			$wp_roles->add_cap( 'editor', 'edit_aiopod_podcasts' );						
			$wp_roles->add_cap( 'editor', 'publish_aiopod_podcasts' );
			$wp_roles->add_cap( 'editor', 'delete_aiopod_podcasts' );
			$wp_roles->add_cap( 'editor', 'delete_published_aiopod_podcasts' );
			$wp_roles->add_cap( 'editor', 'edit_published_aiopod_podcasts' );

			// Add the "author" capabilities
			$wp_roles->add_cap( 'author', 'edit_aiopod_podcasts' );						
			$wp_roles->add_cap( 'author', 'publish_aiopod_podcasts' );
			$wp_roles->add_cap( 'author', 'delete_aiopod_podcasts' );
			$wp_roles->add_cap( 'author', 'delete_published_aiopod_podcasts' );
			$wp_roles->add_cap( 'author', 'edit_published_aiopod_podcasts' );
			
			// Add the "contributor" capabilities
			$wp_roles->add_cap( 'contributor', 'edit_aiopod_podcasts' );						
			$wp_roles->add_cap( 'contributor', 'publish_aiopod_podcasts' );
			$wp_roles->add_cap( 'contributor', 'delete_aiopod_podcasts' );
			$wp_roles->add_cap( 'contributor', 'delete_published_aiopod_podcasts' );
			$wp_roles->add_cap( 'contributor', 'edit_published_aiopod_podcasts' );
			
			// Add the "subscriber" capabilities
			$wp_roles->add_cap( 'subscriber', 'edit_aiopod_podcasts' );						
			$wp_roles->add_cap( 'subscriber', 'publish_aiopod_podcasts' );
			$wp_roles->add_cap( 'subscriber', 'delete_aiopod_podcasts' );
			$wp_roles->add_cap( 'subscriber', 'delete_published_aiopod_podcasts' );
			$wp_roles->add_cap( 'subscriber', 'edit_published_aiopod_podcasts' );			
		}		
	}

	/**
	 * Gets the core post type capabilities.
	 *
	 * @since  1.0.0
	 * @return array $capabilities Core post type capabilities.
	 */
	public function get_core_caps() {	
		$capabilities = array();

		$capability_types = array( 'aiopod_podcast' );

		foreach ( $capability_types as $capability_type ) {		
			$capabilities[ $capability_type ] = array(
				"edit_{$capability_type}",
				"read_{$capability_type}",
				"delete_{$capability_type}",
				"edit_{$capability_type}s",
				"edit_others_{$capability_type}s",
				"publish_{$capability_type}s",
				"read_private_{$capability_type}s",
				"delete_{$capability_type}s",
				"delete_private_{$capability_type}s",
				"delete_published_{$capability_type}s",
				"delete_others_{$capability_type}s",
				"edit_private_{$capability_type}s",
				"edit_published_{$capability_type}s",
			);
		}

		return $capabilities;		
	}
	
	/**
	 * Filter a user's capabilities depending on specific context and/or privilege.
	 *
	 * @since  1.0.0
	 * @param  array  $caps    Returns the user's actual capabilities.
	 * @param  string $cap     Capability name.
	 * @param  int    $user_id The user ID.
	 * @param  array  $args    Adds the context to the cap. Typically the object ID.
	 * @return array           Actual capabilities for meta capability.
	 */
	public function meta_caps( $caps, $cap, $user_id, $args ) {
		if ( empty( $args ) ) {
			return $caps;
		}
				
		// If editing, deleting, or reading a listing, assign the required capability
		if ( 'edit_aiopod_podcast' === $cap || 'delete_aiopod_podcast' === $cap || 'read_aiopod_podcast' === $cap ) {
			$post      = get_post( $args[0] );
			$post_type = get_post_type_object( $post->post_type );

			// Set an empty array for the caps.
			$caps = array();

			// If editing a listing, assign the required capability
			if ( 'edit_aiopod_podcast' === $cap ) {
				if ( $user_id == $post->post_author ) {
					$caps[] = $post_type->cap->edit_aiopod_podcasts;
				} else {
					$caps[] = $post_type->cap->edit_others_aiopod_podcasts;
				}
			}

			// If deleting a listing, assign the required capability
			elseif ( 'delete_aiopod_podcast' === $cap ) {
				if ( $user_id == $post->post_author ) {
					$caps[] = $post_type->cap->delete_aiopod_podcasts;
				} else {
					$caps[] = $post_type->cap->delete_others_aiopod_podcasts;
				}
			}

			// If reading a private listing, assign the required capability
			elseif ( 'read_aiopod_podcast' === $cap ) {
				if ( 'private' !== $post->post_status ) {
					$caps[] = 'read';
				} elseif ( $user_id == $post->post_author ) {
					$caps[] = 'read';
				} else {
					$caps[] = $post_type->cap->read_private_aiopod_podcasts;
				}
			}
		}

		// Return the capabilities required by the user
		return $caps;
	}
	
	/**
	 * Remove core post type capabilities (called on uninstall).
	 *
	 * @since 1.0.0
	 */
	public function remove_caps() {
		global $wp_roles;

		if ( class_exists( 'WP_Roles' ) ) {
			if ( ! isset( $wp_roles ) ) {
				$wp_roles = new WP_Roles();
			}
		}

		if ( is_object( $wp_roles ) ) {		
			// Remove the "administrator" capabilities
			$capabilities = $this->get_core_caps();

			foreach ( $capabilities as $cap_group ) {
				foreach ( $cap_group as $cap ) {
					$wp_roles->remove_cap( 'administrator', $cap );
				}
			}
			$wp_roles->remove_cap( 'administrator', 'manage_aiopod_options' );
			
			// Remove the "editor" capabilities
			$wp_roles->remove_cap( 'editor', 'edit_aiopod_podcasts' );						
			$wp_roles->remove_cap( 'editor', 'publish_aiopod_podcasts' );
			$wp_roles->remove_cap( 'editor', 'delete_aiopod_podcasts' );
			$wp_roles->remove_cap( 'editor', 'delete_published_aiopod_podcasts' );
			$wp_roles->remove_cap( 'editor', 'edit_published_aiopod_podcasts' );
			
			// Remove the "author" capabilities
			$wp_roles->remove_cap( 'author', 'edit_aiopod_podcasts' );						
			$wp_roles->remove_cap( 'author', 'publish_aiopod_podcasts' );
			$wp_roles->remove_cap( 'author', 'delete_aiopod_podcasts' );
			$wp_roles->remove_cap( 'author', 'delete_published_aiopod_podcasts' );
			$wp_roles->remove_cap( 'author', 'edit_published_aiopod_podcasts' );
			
			// Remove the "contributor" capabilities
			$wp_roles->remove_cap( 'contributor', 'edit_aiopod_podcasts' );						
			$wp_roles->remove_cap( 'contributor', 'publish_aiopod_podcasts' );
			$wp_roles->remove_cap( 'contributor', 'delete_aiopod_podcasts' );
			$wp_roles->remove_cap( 'contributor', 'delete_published_aiopod_podcasts' );
			$wp_roles->remove_cap( 'contributor', 'edit_published_aiopod_podcasts' );
			
			// Remove the "subscriber" capabilities
			$wp_roles->remove_cap( 'subscriber', 'edit_aiopod_podcasts' );						
			$wp_roles->remove_cap( 'subscriber', 'publish_aiopod_podcasts' );
			$wp_roles->remove_cap( 'subscriber', 'delete_aiopod_podcasts' );
			$wp_roles->remove_cap( 'subscriber', 'delete_published_aiopod_podcasts' );
			$wp_roles->remove_cap( 'subscriber', 'edit_published_aiopod_podcasts' );
		}		
	}
	
}
