| 1 |
<?php |
| 2 |
declare(strict_types=1); |
| 3 |
|
| 4 |
namespace Imagify\Abilities; |
| 5 |
|
| 6 |
/** |
| 7 |
* Base class for all Imagify MCP abilities. |
| 8 |
* |
| 9 |
* Provides the `check_permissions()` template method (fires the |
| 10 |
* `imagify_mcp_permission_denied` action on denial) and the |
| 11 |
* `fire_executed()` helper used by concrete `execute()` implementations |
| 12 |
* to fire `imagify_mcp_ability_executed` after every invocation. |
| 13 |
* |
| 14 |
* @since 2.3.0 |
| 15 |
*/ |
| 16 |
abstract class AbstractAbility implements AbilitiesInterface { |
| 17 |
|
| 18 |
/** |
| 19 |
* Returns the ability slug used to identify this ability in hooks and tracking. |
| 20 |
* |
| 21 |
* @return string |
| 22 |
*/ |
| 23 |
abstract public function get_id(): string; |
| 24 |
|
| 25 |
/** |
| 26 |
* Returns the human-readable ability label used in hooks and tracking. |
| 27 |
* |
| 28 |
* @return string |
| 29 |
*/ |
| 30 |
abstract public function get_name(): string; |
| 31 |
|
| 32 |
/** |
| 33 |
* Internal permission check delegated by check_permissions(). |
| 34 |
* |
| 35 |
* @return bool True when the current user may execute the ability. |
| 36 |
*/ |
| 37 |
abstract protected function has_permission(): bool; |
| 38 |
|
| 39 |
/** |
| 40 |
* Check if the current user has permission to execute this ability. |
| 41 |
* |
| 42 |
* Delegates the capability check to has_permission() and fires |
| 43 |
* `imagify_mcp_permission_denied` when access is denied so that |
| 44 |
* tracking and logging subscribers can react. |
| 45 |
* |
| 46 |
* @return bool True when the current user may execute the ability. |
| 47 |
*/ |
| 48 |
public function check_permissions(): bool { |
| 49 |
$allowed = $this->has_permission(); |
| 50 |
|
| 51 |
if ( ! $allowed ) { |
| 52 |
do_action( 'imagify_mcp_permission_denied', $this->get_id(), $this->get_name(), 'manage' ); |
| 53 |
} |
| 54 |
|
| 55 |
return $allowed; |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Fire the `imagify_mcp_ability_executed` action after execute() resolves. |
| 60 |
* |
| 61 |
* Called by every concrete execute() so that tracking and other subscribers |
| 62 |
* receive the result for both success and failure outcomes. |
| 63 |
* |
| 64 |
* @param mixed $result Return value of the ability's do_execute(). |
| 65 |
* @param float $start_time microtime(true) captured before do_execute() ran. |
| 66 |
* @param array $args Raw input args forwarded from execute(). |
| 67 |
* @return void |
| 68 |
*/ |
| 69 |
protected function fire_executed( $result, float $start_time, array $args = [] ): void { |
| 70 |
do_action( 'imagify_mcp_ability_executed', $this->get_id(), $this->get_name(), $result, $start_time, $args ); |
| 71 |
} |
| 72 |
} |
| 73 |
|