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
3 months ago
locations
3 months ago
orders
3 months ago
services
3 months ago
abstract-ability.php
3 months ago
class-latepoint-abilities.php
3 months ago
abstract-ability.php
111 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Abstract base class for all LatePoint abilities. |
| 4 | * |
| 5 | * @package LatePoint\Abilities |
| 6 | * @since 5.3.0 |
| 7 | */ |
| 8 | |
| 9 | if ( ! defined( 'ABSPATH' ) ) { |
| 10 | exit; |
| 11 | } |
| 12 | |
| 13 | abstract class LatePointAbstractAbility { |
| 14 | |
| 15 | protected string $id; |
| 16 | protected string $category = 'latepoint'; |
| 17 | protected string $label = ''; |
| 18 | protected string $description = ''; |
| 19 | protected string $permission = 'manage_options'; |
| 20 | protected bool $read_only = true; |
| 21 | protected bool $destructive = false; |
| 22 | protected bool $idempotent = false; |
| 23 | |
| 24 | public function __construct() { |
| 25 | $this->configure(); |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Set $id, $label, $description, $permission, $read_only, $destructive. |
| 30 | */ |
| 31 | abstract protected function configure(): void; |
| 32 | |
| 33 | abstract public function get_input_schema(): array; |
| 34 | |
| 35 | abstract public function get_output_schema(): array; |
| 36 | |
| 37 | /** |
| 38 | * @param array $args |
| 39 | * @return array|\WP_Error |
| 40 | */ |
| 41 | abstract public function execute( array $args ); |
| 42 | |
| 43 | public function get_id(): string { |
| 44 | return $this->id; |
| 45 | } |
| 46 | |
| 47 | public function check_permission(): bool { |
| 48 | return OsRolesHelper::can_user( $this->permission ); |
| 49 | } |
| 50 | |
| 51 | public function to_definition(): array { |
| 52 | return [ |
| 53 | 'label' => $this->label, |
| 54 | 'description' => $this->description, |
| 55 | 'category' => $this->category, |
| 56 | 'permission_callback' => [ $this, 'check_permission' ], |
| 57 | 'input_schema' => $this->get_input_schema(), |
| 58 | 'output_schema' => $this->get_output_schema(), |
| 59 | 'execute_callback' => [ $this, 'execute' ], |
| 60 | 'meta' => $this->build_meta(), |
| 61 | ]; |
| 62 | } |
| 63 | |
| 64 | protected function build_meta(): array { |
| 65 | |
| 66 | // Default annotations, normally read-only |
| 67 | $annotations = [ |
| 68 | 'readOnlyHint' => $this->read_only, |
| 69 | 'idempotentHint' => $this->idempotent, |
| 70 | 'destructiveHint' => false, |
| 71 | 'priority' => $this->read_only ? 1.0 : 2.0, |
| 72 | ]; |
| 73 | |
| 74 | // Destructive overrides everything. |
| 75 | if ( $this->destructive ) { |
| 76 | $annotations['readOnlyHint'] = false; |
| 77 | $annotations['destructiveHint'] = true; |
| 78 | $annotations['priority'] = 3.0; |
| 79 | } |
| 80 | |
| 81 | $meta = [ |
| 82 | 'annotations' => $annotations, |
| 83 | 'show_in_rest' => true, |
| 84 | 'mcp' => [ |
| 85 | 'public' => true, |
| 86 | 'type' => 'tool', |
| 87 | ], |
| 88 | ]; |
| 89 | |
| 90 | return $meta; |
| 91 | } |
| 92 | |
| 93 | protected static function pagination(): array { |
| 94 | return [ |
| 95 | 'page' => [ |
| 96 | 'type' => 'integer', |
| 97 | 'default' => 1, |
| 98 | 'minimum' => 1, |
| 99 | 'description' => __( 'Page number.', 'latepoint' ), |
| 100 | ], |
| 101 | 'per_page' => [ |
| 102 | 'type' => 'integer', |
| 103 | 'default' => 20, |
| 104 | 'minimum' => 1, |
| 105 | 'maximum' => 100, |
| 106 | 'description' => __( 'Items per page.', 'latepoint' ), |
| 107 | ], |
| 108 | ]; |
| 109 | } |
| 110 | } |
| 111 |