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
AdminAccessService.php
58 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\Permissions; |
| 4 | |
| 5 | /** |
| 6 | * Admin Access Service |
| 7 | */ |
| 8 | class AdminAccessService { |
| 9 | |
| 10 | /** |
| 11 | * Admin access service construct |
| 12 | * |
| 13 | * @return void |
| 14 | */ |
| 15 | public function bootstrap() { |
| 16 | add_action( 'admin_init', [ $this, 'handleAdminAccess' ] ); |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * Prevent admin access |
| 21 | * |
| 22 | * @return method |
| 23 | */ |
| 24 | public function handleAdminAccess() { |
| 25 | if ( ! $this->canAccessAdmin() ) { |
| 26 | return $this->redirectToAdmin(); |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Prevent admin access |
| 32 | * |
| 33 | * @return boolean |
| 34 | */ |
| 35 | public function canAccessAdmin() { |
| 36 | if ( |
| 37 | wp_doing_ajax() || |
| 38 | ! isset( $_SERVER['SCRIPT_FILENAME'] ) || |
| 39 | basename( sanitize_text_field( wp_unslash( $_SERVER['SCRIPT_FILENAME'] ) ) ) === 'admin-post.php' || |
| 40 | current_user_can( 'edit_posts' ) |
| 41 | ) { |
| 42 | return true; |
| 43 | } |
| 44 | |
| 45 | return ! current_user_can( 'sc_customer' ); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Redirect to the admin. |
| 50 | * |
| 51 | * @return void |
| 52 | */ |
| 53 | public function redirectToAdmin() { |
| 54 | wp_safe_redirect( \SureCart::pages()->url( 'dashboard' ) ); |
| 55 | exit; |
| 56 | } |
| 57 | } |
| 58 |