activities
3 months ago
agents
3 months ago
analytics
3 months ago
bookings
3 months ago
calendar
3 months ago
configs
3 months ago
customers
2 months ago
locations
3 months ago
orders
3 months ago
services
3 months ago
abstract-ability.php
1 month ago
class-latepoint-abilities.php
1 month ago
class-latepoint-abilities.php
96 lines
| 1 | <?php |
| 2 | /** |
| 3 | * LatePoint Abilities — Main loader & hook wiring. |
| 4 | * |
| 5 | * Registers the LatePoint category and all abilities with the WordPress |
| 6 | * Abilities API (requires WordPress 6.9+). The guarded loader in latepoint.php |
| 7 | * ensures this file is only included when the API is available. |
| 8 | * |
| 9 | * @package LatePoint\Abilities |
| 10 | * @since 5.3.0 |
| 11 | */ |
| 12 | |
| 13 | if ( ! defined( 'ABSPATH' ) ) { |
| 14 | exit; |
| 15 | } |
| 16 | |
| 17 | class LatePointAbilities { |
| 18 | |
| 19 | /** @var string[] Config class map: module slug => class name */ |
| 20 | private static array $config_modules = [ |
| 21 | 'bookings' => 'LatePointAbilitiesBookings', |
| 22 | 'customers' => 'LatePointAbilitiesCustomers', |
| 23 | 'services' => 'LatePointAbilitiesServices', |
| 24 | 'agents' => 'LatePointAbilitiesAgents', |
| 25 | 'orders' => 'LatePointAbilitiesOrders', |
| 26 | 'locations' => 'LatePointAbilitiesLocations', |
| 27 | 'calendar' => 'LatePointAbilitiesCalendar', |
| 28 | 'analytics' => 'LatePointAbilitiesAnalytics', |
| 29 | 'activities' => 'LatePointAbilitiesActivities', |
| 30 | ]; |
| 31 | |
| 32 | /** |
| 33 | * Boot: include module files and wire hooks. |
| 34 | */ |
| 35 | public static function init(): void { |
| 36 | self::include_modules(); |
| 37 | |
| 38 | // Reset LatePoint's current user on every request to ensure it reflects the latest WP user state. |
| 39 | // This is crucial for accurate permission checks in abilities, especially when user roles or capabilities have changed during the request lifecycle. |
| 40 | add_action( 'set_current_user', [ OsAuthHelper::class, 'reset_current_user' ], 1 ); |
| 41 | |
| 42 | add_action( 'wp_abilities_api_categories_init', [ __CLASS__, 'register_category' ] ); |
| 43 | add_action( 'wp_abilities_api_init', [ __CLASS__, 'register_all' ] ); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Include abstract base and all config module files. |
| 48 | */ |
| 49 | private static function include_modules(): void { |
| 50 | $base = LATEPOINT_ABSPATH . 'lib/abilities/'; |
| 51 | require_once $base . 'abstract-ability.php'; |
| 52 | foreach ( array_keys( self::$config_modules ) as $slug ) { |
| 53 | require_once $base . 'configs/class-latepoint-abilities-' . $slug . '.php'; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Register the top-level LatePoint category. |
| 59 | */ |
| 60 | public static function register_category(): void { |
| 61 | wp_register_ability_category( |
| 62 | 'latepoint', |
| 63 | [ |
| 64 | 'label' => __( 'LatePoint', 'latepoint' ), |
| 65 | 'description' => __( 'Appointment scheduling — bookings, customers, services, and staff.', 'latepoint' ), |
| 66 | ] |
| 67 | ); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Register all abilities from every module. |
| 72 | * |
| 73 | * Read-only abilities register whenever the master toggle is on. |
| 74 | * Mutating abilities require the write toggle; destructive ones |
| 75 | * require the delete toggle. |
| 76 | */ |
| 77 | public static function register_all(): void { |
| 78 | $allow_write = OsSettingsHelper::is_on( 'latepoint_abilities_api_edit' ); |
| 79 | $allow_delete = OsSettingsHelper::is_on( 'latepoint_abilities_api_delete' ); |
| 80 | |
| 81 | foreach ( self::$config_modules as $class ) { |
| 82 | foreach ( $class::get_abilities() as $ability ) { |
| 83 | if ( $ability->is_destructive() && ! $allow_delete ) { |
| 84 | continue; |
| 85 | } |
| 86 | if ( ! $ability->is_read_only() && ! $ability->is_destructive() && ! $allow_write ) { |
| 87 | continue; |
| 88 | } |
| 89 | wp_register_ability( $ability->get_id(), $ability->to_definition() ); |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | LatePointAbilities::init(); |
| 96 |