is_public_page( $current_path ) ) { return; } // Check if this is a trainer page if ( $this->is_trainer_page( $current_path ) ) { $this->check_trainer_access( $current_path ); } } /** * Check if current page is public * * @param string $path Current page path * @return bool */ private function is_public_page( $path ) { foreach ( self::$public_pages as $public_page ) { if ( $path === $public_page || strpos( $path, $public_page ) === 0 ) { return true; } } return false; } /** * Check if current page is a trainer page * * @param string $path Current page path * @return bool */ private function is_trainer_page( $path ) { foreach ( self::$trainer_pages as $trainer_page ) { if ( $path === $trainer_page || strpos( $path, $trainer_page ) === 0 ) { return true; } } // Also check for pages that start with 'trainer/' if ( strpos( $path, 'trainer/' ) === 0 ) { return true; } return false; } /** * Check trainer access to protected pages * * @param string $path Current page path */ private function check_trainer_access( $path ) { // First check if user is logged in if ( ! is_user_logged_in() ) { wp_safe_redirect( home_url( '/community-login/' ) ); exit; } $user_id = get_current_user_id(); $user = wp_get_current_user(); // Allow administrators full access if ( current_user_can( 'manage_options' ) ) { return; } // Check if user has trainer role if ( ! in_array( 'hvac_trainer', $user->roles ) && ! in_array( 'hvac_master_trainer', $user->roles ) ) { // Not a trainer, show access denied $this->show_access_denied(); return; } // Get trainer status if ( ! class_exists( 'HVAC_Trainer_Status' ) ) { require_once HVAC_PLUGIN_DIR . 'includes/class-hvac-trainer-status.php'; } $status = HVAC_Trainer_Status::get_trainer_status( $user_id ); // Handle based on status switch ( $status ) { case HVAC_Trainer_Status::STATUS_PENDING: // Redirect to pending page if ( $path !== 'trainer-account-pending' && strpos( $path, 'trainer-account-pending' ) !== 0 ) { wp_safe_redirect( home_url( '/trainer-account-pending/' ) ); exit; } break; case HVAC_Trainer_Status::STATUS_DISABLED: // Redirect to disabled page if ( $path !== 'trainer-account-disabled' && strpos( $path, 'trainer-account-disabled' ) !== 0 ) { wp_safe_redirect( home_url( '/trainer-account-disabled/' ) ); exit; } break; case HVAC_Trainer_Status::STATUS_APPROVED: case HVAC_Trainer_Status::STATUS_ACTIVE: case HVAC_Trainer_Status::STATUS_INACTIVE: // Allow access break; default: // Unknown status, treat as pending wp_safe_redirect( home_url( '/trainer-account-pending/' ) ); exit; } } /** * Show access denied page */ private function show_access_denied() { get_header(); ?>