$input Ability input. * @return true|WP_Error */ public static function assert_confirmed( array $input ) { if ( empty( $input['confirm'] ) ) { return new WP_Error( 'confirmation_required', __( 'This operation is irreversible. Pass confirm: true to proceed.', 'acrossai-abilities-manager' ) ); } return true; } /** * Build the permission_callback for an ability. * * Redirection's own default capability is manage_options * (Redirection_Capabilities::CAP_DEFAULT), matching the convention across the * rest of includes/Abilities/. A single filter governs the whole suite. * * @param string $floor WordPress capability floor. * @return callable(): bool */ public static function can( string $floor = 'manage_options' ): callable { return static function () use ( $floor ): bool { $allowed = current_user_can( $floor ); /** * Filter whether the current user may execute a Redirection ability. * * @param bool $allowed Result of the capability floor check. * @param string $floor WordPress capability floor. */ return (bool) apply_filters( self::PERMISSION_FILTER, $allowed, $floor ); }; } /** * Build a success envelope. * * @param array $payload Ability-specific keys. * @param string $message Human-readable summary. * @return array */ public static function ok( array $payload, string $message ): array { unset( $payload['success'], $payload['message'], $payload['error_code'] ); return array_merge( array( 'success' => true ), $payload, array( 'message' => $message ) ); } /** * Build a failure envelope from a WP_Error. * * @param WP_Error $error Error to unwrap. * @param array $context Optional identifying input to echo back. * @return array */ public static function fail( WP_Error $error, array $context = array() ): array { return self::error( (string) $error->get_error_code(), (string) $error->get_error_message(), $context ); } /** * Build a failure envelope from an explicit code and message. * * @param string $code Machine-readable error code. * @param string $message Human-readable message. * @param array $context Optional identifying input to echo back. * @return array */ public static function error( string $code, string $message, array $context = array() ): array { // Context often echoes caller-supplied input back. Strip the reserved // envelope keys so a caller can never spoof success or the error code. unset( $context['success'], $context['message'], $context['error_code'] ); return array_merge( array( 'success' => false ), $context, array( 'message' => $message, 'error_code' => $code, ) ); } }