settings = $settings; } /** * Registers hooks. * * @return void */ public function register(): void { if ( aiblackout_is_disabled() ) { return; } if ( $this->settings->is_blackout_enabled( Settings::BLACKOUT_CORE_AI ) ) { add_filter( 'wp_supports_ai', '__return_false', PHP_INT_MAX ); } if ( $this->settings->is_blackout_enabled( Settings::BLACKOUT_ABILITIES ) ) { add_action( 'wp_abilities_api_init', array( $this, 'unregister_abilities' ), PHP_INT_MAX, 1 ); add_filter( 'rest_endpoints', array( $this, 'remove_abilities_routes' ), PHP_INT_MAX ); add_filter( 'rest_index', array( $this, 'remove_abilities_namespace' ), PHP_INT_MAX ); } } /** * Unregisters every registered ability. * * @param mixed $registry The abilities registry, passed by the action. * @return void */ public function unregister_abilities( $registry = null ): void { if ( ! function_exists( 'wp_unregister_ability' ) ) { return; } foreach ( $this->list_abilities( $registry ) as $name => $ability ) { $name = is_string( $name ) ? $name : ''; if ( '' === $name ) { continue; } if ( null !== wp_unregister_ability( $name ) ) { $this->unregistered[] = $name; } } if ( ! empty( $this->unregistered ) ) { /** * Fires after abilities have been unregistered. * * @since 1.0.0 * * @param string[] $names Names of the abilities removed. */ do_action( 'aiblackout_abilities_unregistered', $this->unregistered ); } } /** * Returns every registered ability, filtered by nothing. * * @param mixed $registry Registry instance passed by the action, if any. * @return array Abilities keyed by name. */ private function list_abilities( $registry ): array { if ( is_object( $registry ) && method_exists( $registry, 'get_all_registered' ) ) { $abilities = $registry->get_all_registered(); if ( is_array( $abilities ) ) { return $abilities; } } return function_exists( 'wp_get_abilities' ) ? wp_get_abilities() : array(); } /** * Removes the Abilities REST routes. * * @param array $endpoints Registered REST routes. * @return array Filtered routes. */ public function remove_abilities_routes( $endpoints ): array { if ( ! is_array( $endpoints ) ) { return array(); } $prefix = '/' . self::ABILITIES_NAMESPACE; foreach ( array_keys( $endpoints ) as $route ) { $route = (string) $route; if ( $route === $prefix || 0 === strpos( $route, $prefix . '/' ) ) { unset( $endpoints[ $route ] ); } } return $endpoints; } /** * Removes the Abilities namespace from the REST API index. * * @param mixed $response The index response. * @return mixed The filtered response. */ public function remove_abilities_namespace( $response ) { if ( ! $response instanceof \WP_REST_Response ) { return $response; } $data = $response->get_data(); if ( ! is_array( $data ) || empty( $data['namespaces'] ) || ! is_array( $data['namespaces'] ) ) { return $response; } $data['namespaces'] = array_values( array_filter( $data['namespaces'], static function ( $route_namespace ) { return self::ABILITIES_NAMESPACE !== $route_namespace; } ) ); $response->set_data( $data ); return $response; } /** * Whether core reports AI as supported right now. * * @return bool True when core AI is available. */ public function is_core_ai_active(): bool { return function_exists( 'wp_supports_ai' ) && wp_supports_ai(); } /** * Whether this WordPress ships the core AI client at all. * * @return bool True when the AI client is present. */ public function has_core_ai(): bool { return function_exists( 'wp_supports_ai' ); } /** * Whether this WordPress ships the Abilities API. * * @return bool True when the Abilities API is present. */ public function has_abilities_api(): bool { return function_exists( 'wp_unregister_ability' ); } /** * Returns the abilities removed during this request. * * @return string[] Ability names. */ public function get_unregistered(): array { return $this->unregistered; } /** * Reports whether the AI-key constants or environment variables are set. * * @return string[] Names of the credentials found. */ public function find_environment_keys(): array { $found = array(); foreach ( array( 'OPENAI_API_KEY', 'ANTHROPIC_API_KEY', 'GOOGLE_API_KEY' ) as $name ) { if ( defined( $name ) || false !== getenv( $name ) ) { $found[] = $name; } } return $found; } }