| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package FireBox |
| 4 |
* @version 1.0.0 Free |
| 5 |
* |
| 6 |
* @author FirePlugins <info@fireplugins.com> |
| 7 |
* @link https://www.fireplugins.com |
| 8 |
* @copyright Copyright © 2021 FirePlugins All Rights Reserved |
| 9 |
* @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace FireBox\Core\Admin; |
| 13 |
|
| 14 |
if (!defined('ABSPATH')) |
| 15 |
{ |
| 16 |
exit; // Exit if accessed directly. |
| 17 |
} |
| 18 |
|
| 19 |
class Capabilities |
| 20 |
{ |
| 21 |
public function __construct() |
| 22 |
{ |
| 23 |
$this->setup(); |
| 24 |
} |
| 25 |
|
| 26 |
private function setup() |
| 27 |
{ |
| 28 |
global $wp_roles; |
| 29 |
|
| 30 |
if ( ! class_exists( 'WP_Roles' ) ) { |
| 31 |
return; |
| 32 |
} |
| 33 |
|
| 34 |
if ( ! isset( $wp_roles ) ) { |
| 35 |
$wp_roles = new WP_Roles(); |
| 36 |
} |
| 37 |
|
| 38 |
$capabilities = []; |
| 39 |
|
| 40 |
// get all CPTs |
| 41 |
$cpts = [ |
| 42 |
'firebox' => [ |
| 43 |
'singular' => 'firebox', |
| 44 |
'plural' => 'fireboxes' |
| 45 |
] |
| 46 |
]; |
| 47 |
|
| 48 |
foreach ($cpts as $name => $data) |
| 49 |
{ |
| 50 |
$singular = $data['singular']; |
| 51 |
$plural = $data['plural']; |
| 52 |
|
| 53 |
$capabilities[strtolower($name)] = [ |
| 54 |
'edit_post' => "edit_$singular", |
| 55 |
'read_post' => "read_$singular", |
| 56 |
'delete_post' => "delete_$singular", |
| 57 |
'edit_posts' => "edit_$plural", |
| 58 |
'edit_others_posts' => "edit_others_$plural", |
| 59 |
'publish_posts' => "publish_$plural", |
| 60 |
'read_private_posts' => "read_private_$plural", |
| 61 |
'read' => "read_$singular", |
| 62 |
'delete_posts' => "delete_$plural", |
| 63 |
'delete_private_posts' => "delete_private_$plural", |
| 64 |
'delete_published_posts' => "delete_published_$plural", |
| 65 |
'delete_others_posts' => "delete_others_$plural", |
| 66 |
'edit_private_posts' => "edit_private_$plural", |
| 67 |
'edit_published_posts' => "edit_published_$plural", |
| 68 |
'create_posts' => "edit_$plural", |
| 69 |
]; |
| 70 |
|
| 71 |
// also add taxonomies |
| 72 |
if (isset($data['taxonomies']) && is_array($data['taxonomies']) && count($data['taxonomies'])) |
| 73 |
{ |
| 74 |
foreach ($data['taxonomies'] as $tax) |
| 75 |
{ |
| 76 |
$tmp_tax = $name . '_' . $tax; |
| 77 |
|
| 78 |
$capabilities[strtolower($name)]["manage_{$name}_{$tmp_tax}_terms"] = "manage_{$name}_{$tmp_tax}_terms"; |
| 79 |
$capabilities[strtolower($name)]["edit_{$name}_{$tmp_tax}_terms"] = "edit_{$name}_{$tmp_tax}_terms"; |
| 80 |
$capabilities[strtolower($name)]["assign_{$name}_{$tmp_tax}_terms"] = "assign_{$name}_{$tmp_tax}_terms"; |
| 81 |
$capabilities[strtolower($name)]["delete_{$name}_{$tmp_tax}_terms"] = "delete_{$name}_{$tmp_tax}_terms"; |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
//Set capabilities to all roles with cap:"". |
| 87 |
$roles = $wp_roles->get_names(); |
| 88 |
|
| 89 |
// add capabilities to all roles |
| 90 |
foreach ( $capabilities as $name => $capabilities) { |
| 91 |
$capabilities_data = array_values($capabilities); |
| 92 |
|
| 93 |
// all roles |
| 94 |
foreach ( $roles as $current_role => $role_name ) { |
| 95 |
if ( isset( $wp_roles->roles[ $current_role ][ 'capabilities' ][ 'manage_options' ] ) ) { |
| 96 |
$role = get_role( $current_role ); |
| 97 |
if ( isset( $role ) && is_object( $role ) ) { |
| 98 |
for ( $i = 0, $caps_limit = count( $capabilities_data ); $i < $caps_limit; $i ++ ) { |
| 99 |
if ( ! isset( $wp_roles->roles[ $current_role ][ 'capabilities' ][ $capabilities_data[ $i ] ] ) ) { |
| 100 |
$role->add_cap( $capabilities_data[ $i ] ); |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
// all admins |
| 109 |
$user_admins = get_users( array( |
| 110 |
'role' => 'administrator' |
| 111 |
) ); |
| 112 |
|
| 113 |
if ( is_multisite() ) { |
| 114 |
$super_admins = get_super_admins(); |
| 115 |
|
| 116 |
foreach( $super_admins as $admin ) { |
| 117 |
$super_admin = new \WP_User( $admin ); |
| 118 |
|
| 119 |
if ( ! in_array( $super_admin, $user_admins, true ) ) { |
| 120 |
$user_admins[] = $super_admin; |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
foreach ( $user_admins as $user ) { |
| 126 |
if ( $user->exists() ) { |
| 127 |
for ( $i = 0, $caps_limit = count( $capabilities_data ); $i < $caps_limit; $i ++ ) { |
| 128 |
$user->add_cap( $capabilities_data[ $i ] ); |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
} |