PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / 2.3.0
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF v2.3.0
2.3.4 2.3.3 2.3.2 2.3.1 2.3.0 2.2.9 2.2.8 trunk 1.10 1.3.3 1.3.4 1.3.5 1.3.5.1 1.3.5.2 1.3.6 1.3.6.1 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.5 All 103 releases
imagify / classes / Abilities / AbstractAbility.php

AbstractAbility.php in Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF 2.3.0, at classes/Abilities/AbstractAbility.php

73 lines 2.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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