Models
2 years ago
WPConfig
3 years ago
AdminAccessService.php
3 years ago
PermissionsService.php
3 years ago
RolesService.php
3 years ago
RolesServiceProvider.php
3 years ago
RolesService.php
239 lines
| 1 | <?php |
| 2 | namespace SureCart\Permissions; |
| 3 | |
| 4 | /** |
| 5 | * Adds roles and capabilities. |
| 6 | */ |
| 7 | class RolesService { |
| 8 | /** |
| 9 | * Create roles and caps. |
| 10 | * |
| 11 | * @return void |
| 12 | */ |
| 13 | public function create() { |
| 14 | $this->addRoles(); |
| 15 | $this->addCaps(); |
| 16 | } |
| 17 | |
| 18 | /** |
| 19 | * Create roles and caps. |
| 20 | * |
| 21 | * @return void |
| 22 | */ |
| 23 | public function delete() { |
| 24 | $this->removeRoles(); |
| 25 | // $this->removeCaps(); |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Add Roles. |
| 30 | * |
| 31 | * @return void |
| 32 | */ |
| 33 | public function addRoles() { |
| 34 | add_role( |
| 35 | 'sc_shop_manager', |
| 36 | __( 'SureCart Shop Manager', 'surecart' ), |
| 37 | [ |
| 38 | 'read' => true, |
| 39 | 'edit_posts' => true, |
| 40 | 'delete_posts' => true, |
| 41 | 'unfiltered_html' => true, |
| 42 | 'upload_files' => true, |
| 43 | 'export' => true, |
| 44 | 'import' => true, |
| 45 | 'delete_others_pages' => true, |
| 46 | 'delete_others_posts' => true, |
| 47 | 'delete_pages' => true, |
| 48 | 'delete_private_pages' => true, |
| 49 | 'delete_private_posts' => true, |
| 50 | 'delete_published_pages' => true, |
| 51 | 'delete_published_posts' => true, |
| 52 | 'edit_others_pages' => true, |
| 53 | 'edit_others_posts' => true, |
| 54 | 'edit_pages' => true, |
| 55 | 'edit_private_pages' => true, |
| 56 | 'edit_private_posts' => true, |
| 57 | 'edit_published_pages' => true, |
| 58 | 'edit_published_posts' => true, |
| 59 | 'manage_categories' => true, |
| 60 | 'manage_links' => true, |
| 61 | 'moderate_comments' => true, |
| 62 | 'publish_pages' => true, |
| 63 | 'publish_posts' => true, |
| 64 | 'read_private_pages' => true, |
| 65 | 'read_private_posts' => true, |
| 66 | ] |
| 67 | ); |
| 68 | |
| 69 | add_role( |
| 70 | 'sc_shop_accountant', |
| 71 | __( 'SureCart Accountant', 'surecart' ), |
| 72 | [ |
| 73 | 'read' => true, |
| 74 | 'edit_posts' => false, |
| 75 | 'delete_posts' => false, |
| 76 | ] |
| 77 | ); |
| 78 | |
| 79 | add_role( |
| 80 | 'sc_shop_worker', |
| 81 | __( 'SureCart Shop Worker', 'surecart' ), |
| 82 | [ |
| 83 | 'read' => true, |
| 84 | 'edit_posts' => false, |
| 85 | 'upload_files' => true, |
| 86 | 'delete_posts' => false, |
| 87 | ] |
| 88 | ); |
| 89 | |
| 90 | add_role( |
| 91 | 'sc_customer', |
| 92 | __( 'SureCart Customer', 'surecart' ), |
| 93 | [ |
| 94 | 'read' => true, |
| 95 | ] |
| 96 | ); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Add new shop-specific capabilities |
| 101 | * |
| 102 | * @since 1.4.4 |
| 103 | * @global WP_Roles $wp_roles |
| 104 | * @return void |
| 105 | */ |
| 106 | public function addCaps() { |
| 107 | global $wp_roles; |
| 108 | |
| 109 | if ( class_exists( 'WP_Roles' ) ) { |
| 110 | if ( ! isset( $wp_roles ) ) { |
| 111 | // phpcs:ignore |
| 112 | $wp_roles = new \WP_Roles(); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | if ( is_object( $wp_roles ) ) { |
| 117 | $wp_roles->add_cap( 'sc_shop_manager', 'view_sc_shop_reports' ); |
| 118 | $wp_roles->add_cap( 'sc_shop_manager', 'view_sc_shop_sensitive_data' ); |
| 119 | $wp_roles->add_cap( 'sc_shop_manager', 'export_sc_shop_reports' ); |
| 120 | $wp_roles->add_cap( 'sc_shop_manager', 'manage_sc_shop_settings' ); |
| 121 | |
| 122 | $wp_roles->add_cap( 'administrator', 'view_sc_shop_reports' ); |
| 123 | $wp_roles->add_cap( 'administrator', 'view_sc_shop_sensitive_data' ); |
| 124 | $wp_roles->add_cap( 'administrator', 'export_sc_shop_reports' ); |
| 125 | $wp_roles->add_cap( 'administrator', 'manage_sc_shop_settings' ); |
| 126 | $wp_roles->add_cap( 'administrator', 'manage_sc_account_settings' ); |
| 127 | |
| 128 | // Add the main model capabilities |
| 129 | $capabilities = $this->getModelCaps(); |
| 130 | foreach ( $capabilities as $cap_group ) { |
| 131 | foreach ( $cap_group as $cap ) { |
| 132 | $wp_roles->add_cap( 'administrator', $cap ); |
| 133 | $wp_roles->add_cap( 'sc_shop_manager', $cap ); |
| 134 | $wp_roles->add_cap( 'sc_shop_worker', $cap ); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | $wp_roles->add_cap( 'sc_shop_accountant', 'edit_sc_products' ); |
| 139 | $wp_roles->add_cap( 'sc_shop_accountant', 'view_sc_shop_reports' ); |
| 140 | $wp_roles->add_cap( 'sc_shop_accountant', 'export_sc_shop_reports' ); |
| 141 | $wp_roles->add_cap( 'sc_shop_accountant', 'edit_sc_shop_charges' ); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Gets the core post type capabilities |
| 147 | * |
| 148 | * @since 1.4.4 |
| 149 | * @return array $capabilities Core post type capabilities |
| 150 | */ |
| 151 | public function getModelCaps() { |
| 152 | $capabilities = []; |
| 153 | |
| 154 | $capability_types = [ |
| 155 | 'sc_coupon', |
| 156 | 'sc_promotion', |
| 157 | 'sc_balance_transaction', |
| 158 | 'sc_checkout', |
| 159 | 'sc_purchase', |
| 160 | 'sc_webhook', |
| 161 | 'sc_product', |
| 162 | 'sc_license', |
| 163 | 'sc_customer', |
| 164 | 'sc_order', |
| 165 | 'sc_invoice', |
| 166 | 'sc_price', |
| 167 | 'sc_refund', |
| 168 | 'sc_charge', |
| 169 | 'sc_media', |
| 170 | 'sc_payment_method', |
| 171 | 'sc_subscription', |
| 172 | ]; |
| 173 | |
| 174 | foreach ( $capability_types as $capability_type ) { |
| 175 | $capabilities[ $capability_type ] = array( |
| 176 | // Models. |
| 177 | "read_{$capability_type}", // read. |
| 178 | "read_{$capability_type}s", // read. |
| 179 | "delete_{$capability_type}", // delete. |
| 180 | "edit_{$capability_type}", // edit. |
| 181 | "edit_{$capability_type}s", // edit all. |
| 182 | "edit_others_{$capability_type}s", // edit others. |
| 183 | "publish_{$capability_type}s", // publish. |
| 184 | "delete_{$capability_type}s", // delete. |
| 185 | "delete_others_{$capability_type}s", // delete others. |
| 186 | |
| 187 | // Stats. |
| 188 | "view_{$capability_type}_stats", |
| 189 | ); |
| 190 | } |
| 191 | |
| 192 | return $capabilities; |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Remove Roles. |
| 197 | * |
| 198 | * @return void |
| 199 | */ |
| 200 | public function removeRoles() { |
| 201 | remove_role( 'sc_shop_manager' ); |
| 202 | remove_role( 'sc_shop_accountant' ); |
| 203 | remove_role( 'sc_shop_worker' ); |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Remove Caps. |
| 208 | * |
| 209 | * @return void |
| 210 | */ |
| 211 | public function removeCaps() { |
| 212 | global $wp_roles; |
| 213 | |
| 214 | if ( class_exists( 'WP_Roles' ) ) { |
| 215 | if ( ! isset( $wp_roles ) ) { |
| 216 | // phpcs:ignore |
| 217 | $wp_roles = new \WP_Roles(); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | $delete_caps = array_merge( |
| 222 | [ |
| 223 | 'view_sc_shop_reports', |
| 224 | 'view_sc_shop_sensitive_data', |
| 225 | 'export_sc_shop_reports', |
| 226 | 'manage_sc_shop_settings', |
| 227 | 'manage_sc_account_settings', |
| 228 | ], |
| 229 | $this->getModelCaps() |
| 230 | ); |
| 231 | |
| 232 | foreach ( $delete_caps as $cap ) { |
| 233 | foreach ( array_keys( $wp_roles->roles ) as $role ) { |
| 234 | $wp_roles->remove_cap( $role, $cap ); |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 |