<?php

defined('ABSPATH') || exit; // Exit if accessed directly
if (! class_exists('Gridpress_User_Capabilities')) {
    class Gridpress_User_Capabilities {

        /**
         * Add capabilities to administrator role.
         */
        public static function gridpress_add_role_caps() {

            $gridpress_role = get_role( apply_filters( 'gridpress_capability_role_name', 'administrator' ) );

            if ( ! $gridpress_role ) {
                return;
            }

            $gridpress_capabilities = self::gridpress_get_core_capabilities();

            foreach ( $gridpress_capabilities as $gridpress_cap_group ) {

                foreach ( $gridpress_cap_group as $gridpress_cap ) {
                    if ( ! $gridpress_role->has_cap( $gridpress_cap ) ) {
                        $gridpress_role->add_cap( $gridpress_cap );
                    }
                }
            }
        }

        public static function gridpress_remove_role_caps() {

            $gridpress_role = get_role( 'administrator' );

            if ( ! $gridpress_role ) {
                return;
            }

            $gridpress_capabilities = self::gridpress_get_core_capabilities();

            foreach ( $gridpress_capabilities as $gridpress_cap_group ) {

                foreach ( $gridpress_cap_group as $gridpress_cap ) {
                    $gridpress_role->remove_cap( $gridpress_cap );
                }
            }
        }

        private static function gridpress_get_core_capabilities() {
            $gridpress_capabilities = array(
                'core' => array(
                    'gridpress_settings'
                ),
            );

            $gridpress_capability_types = apply_filters(
                'gridpress_capability_types',
                array( 'gridpress_grid' )
            );

            foreach ( $gridpress_capability_types as $gridpress_capability_type ) {

                $gridpress_capabilities[$gridpress_capability_type] = array(
                    // Post type.
                    "edit_{$gridpress_capability_type}",
                    "read_{$gridpress_capability_type}",
                    "delete_{$gridpress_capability_type}",
                    "edit_{$gridpress_capability_type}s",
                    "edit_others_{$gridpress_capability_type}s",
                    "publish_{$gridpress_capability_type}s",
                    "read_private_{$gridpress_capability_type}s",
                    "delete_{$gridpress_capability_type}s",
                    "delete_private_{$gridpress_capability_type}s",
                    "delete_published_{$gridpress_capability_type}s",
                    "delete_others_{$gridpress_capability_type}s",
                    "edit_private_{$gridpress_capability_type}s",
                    "edit_published_{$gridpress_capability_type}s",
                    // Terms.
                    "manage_{$gridpress_capability_type}_terms",
                    "edit_{$gridpress_capability_type}_terms",
                    "delete_{$gridpress_capability_type}_terms",
                    "assign_{$gridpress_capability_type}_terms",
                );
            }

            return $gridpress_capabilities;
        }
    }
}