| 1 |
<?php |
| 2 |
declare(strict_types=1); |
| 3 |
|
| 4 |
namespace Imagify\Abilities; |
| 5 |
|
| 6 |
use Imagify\Stats\StatInterface; |
| 7 |
|
| 8 |
/** |
| 9 |
* MCP ability that reports next-gen coverage for optimized media. |
| 10 |
* |
| 11 |
* Returns the number of optimized media missing a next-gen version and the |
| 12 |
* current next-gen format configured in Imagify settings. |
| 13 |
* |
| 14 |
* @since 2.3.0 |
| 15 |
*/ |
| 16 |
class GetNextgenCoverage extends AbstractAbility { |
| 17 |
|
| 18 |
/** |
| 19 |
* Returns the ability slug. |
| 20 |
* |
| 21 |
* @return string |
| 22 |
*/ |
| 23 |
public function get_id(): string { |
| 24 |
return 'imagify/get-nextgen-coverage'; |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Returns the ability label. |
| 29 |
* |
| 30 |
* @return string |
| 31 |
*/ |
| 32 |
public function get_name(): string { |
| 33 |
return 'Get next-gen coverage'; |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Stat service that counts optimized media without next-gen versions. |
| 38 |
* |
| 39 |
* @var StatInterface |
| 40 |
*/ |
| 41 |
private $stat; |
| 42 |
|
| 43 |
/** |
| 44 |
* Constructor. |
| 45 |
* |
| 46 |
* @param StatInterface $stat Stat service injected via DI. |
| 47 |
*/ |
| 48 |
public function __construct( StatInterface $stat ) { |
| 49 |
$this->stat = $stat; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Registers the ability with the WP Abilities API. |
| 54 |
* |
| 55 |
* No-ops when `wp_register_ability` is not available (WP < 6.9). |
| 56 |
* |
| 57 |
* @since 2.3.0 |
| 58 |
* |
| 59 |
* @return void |
| 60 |
*/ |
| 61 |
public function register(): void { |
| 62 |
if ( ! function_exists( 'wp_register_ability' ) ) { |
| 63 |
return; |
| 64 |
} |
| 65 |
|
| 66 |
wp_register_ability( |
| 67 |
'imagify/get-nextgen-coverage', |
| 68 |
[ |
| 69 |
'label' => __( 'Get next-gen coverage', 'imagify' ), |
| 70 |
'description' => __( 'Returns the number of optimized images missing a next-gen version and the configured next-gen format.', 'imagify' ), |
| 71 |
'category' => 'imagify', |
| 72 |
'output_schema' => [ |
| 73 |
'type' => 'object', |
| 74 |
'properties' => [ |
| 75 |
'missing_nextgen_count' => [ 'type' => 'integer' ], |
| 76 |
'nextgen_format' => [ 'type' => 'string' ], |
| 77 |
], |
| 78 |
], |
| 79 |
'execute_callback' => [ $this, 'execute' ], |
| 80 |
'permission_callback' => [ $this, 'check_permissions' ], |
| 81 |
'meta' => [ |
| 82 |
'show_in_rest' => true, |
| 83 |
'mcp' => [ 'public' => true ], |
| 84 |
'annotations' => [ |
| 85 |
'readonly' => true, |
| 86 |
'destructive' => false, |
| 87 |
'idempotent' => true, |
| 88 |
], |
| 89 |
], |
| 90 |
] |
| 91 |
); |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Routes through Imagify's capability abstraction so the `imagify_capacity` |
| 96 |
* filter and multisite network-admin logic are honoured. |
| 97 |
* |
| 98 |
* @since 2.3.0 |
| 99 |
* |
| 100 |
* @return bool True when the current user has the Imagify `manage` capability. |
| 101 |
*/ |
| 102 |
protected function has_permission(): bool { |
| 103 |
return imagify_get_context( 'wp' )->current_user_can( 'manage' ); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Executes the ability and returns next-gen coverage data. |
| 108 |
* |
| 109 |
* @since 2.3.0 |
| 110 |
* |
| 111 |
* @return array{missing_nextgen_count: int, nextgen_format: string} |
| 112 |
*/ |
| 113 |
public function execute(): array { |
| 114 |
$start_time = microtime( true ); |
| 115 |
$result = $this->do_execute(); |
| 116 |
|
| 117 |
$this->fire_executed( $result, $start_time ); |
| 118 |
|
| 119 |
return $result; |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Internal execution logic for the ability. |
| 124 |
* |
| 125 |
* @return array{missing_nextgen_count: int, nextgen_format: string} |
| 126 |
*/ |
| 127 |
private function do_execute(): array { |
| 128 |
return [ |
| 129 |
'missing_nextgen_count' => (int) $this->stat->get_cached_stat(), |
| 130 |
'nextgen_format' => get_imagify_option( 'optimization_format' ), |
| 131 |
]; |
| 132 |
} |
| 133 |
} |
| 134 |
|