share( BingClient::class, static fn() => new BingClient() ); } public function boot( Container $container ): void { add_action( 'rest_api_init', function () use ( $container ): void { $client = $container->get( BingClient::class ); register_rest_route( self::NAMESPACE, '/bing', array( array( 'methods' => 'GET', 'callback' => static fn() => new WP_REST_Response( array( 'has_key' => $client->has_key(), 'key_hint' => $client->key_hint(), 'site' => untrailingslashit( (string) home_url() ), ) ), 'permission_callback' => static fn(): bool => current_user_can( 'manage_options' ), ), array( 'methods' => 'PUT', 'callback' => static function ( WP_REST_Request $request ) use ( $client ) { $key = trim( (string) $request->get_param( 'api_key' ) ); if ( '' === $key ) { $client->clear_key(); } else { $client->save_key( $key ); } return new WP_REST_Response( array( 'has_key' => $client->has_key(), 'key_hint' => $client->key_hint(), ) ); }, 'permission_callback' => static fn(): bool => current_user_can( 'manage_options' ), 'args' => array( 'api_key' => array( 'type' => 'string' ), // Write-only. ), ), ) ); register_rest_route( self::NAMESPACE, '/bing/verify', array( 'methods' => 'POST', 'callback' => static fn() => self::respond( $client->verify() ), 'permission_callback' => static fn(): bool => current_user_can( 'manage_options' ), ) ); register_rest_route( self::NAMESPACE, '/bing/stats', array( 'methods' => 'GET', 'callback' => static fn( WP_REST_Request $request ) => self::respond( $client->stats( (bool) $request->get_param( 'refresh' ) ) ), 'permission_callback' => static fn(): bool => current_user_can( 'manage_options' ), 'args' => array( 'refresh' => array( 'type' => 'boolean' ), ), ) ); register_rest_route( self::NAMESPACE, '/bing/keywords', array( 'methods' => 'GET', 'callback' => static fn( WP_REST_Request $request ) => self::respond( $client->keywords( (bool) $request->get_param( 'refresh' ) ) ), 'permission_callback' => static fn(): bool => current_user_can( 'manage_options' ), 'args' => array( 'refresh' => array( 'type' => 'boolean' ), ), ) ); register_rest_route( self::NAMESPACE, '/bing/quota', array( 'methods' => 'GET', 'callback' => static fn() => self::respond( $client->quota() ), 'permission_callback' => static fn(): bool => current_user_can( 'manage_options' ), ) ); register_rest_route( self::NAMESPACE, '/bing/submit', array( 'methods' => 'POST', 'callback' => static fn( WP_REST_Request $request ) => self::respond( $client->submit( (array) ( $request->get_param( 'urls' ) ?: array() ) ) ), // Submission spends a published daily allowance, so the // rate limit here protects the customer's quota, not us. 'permission_callback' => static function () { if ( ! current_user_can( 'manage_options' ) ) { return self::forbidden(); } return self::spend( 'bingsubmit', 40 ); }, 'args' => array( 'urls' => array( 'type' => 'array', 'required' => true, ), ), ) ); // "Submit everything published recently" — the action people // actually want after a migration or a bulk edit. register_rest_route( self::NAMESPACE, '/bing/submit-recent', array( 'methods' => 'POST', 'callback' => static function ( WP_REST_Request $request ) use ( $client ) { $limit = max( 1, min( 100, (int) ( $request->get_param( 'limit' ) ?: 25 ) ) ); $ids = get_posts( array( 'post_type' => array_values( array_diff( get_post_types( array( 'public' => true ) ), array( 'attachment' ) ) ), 'post_status' => 'publish', 'posts_per_page' => $limit, 'fields' => 'ids', 'orderby' => 'modified', 'order' => 'DESC', 'no_found_rows' => true, ) ); $urls = array_values( array_filter( array_map( static fn( $id ): string => (string) get_permalink( (int) $id ), $ids ) ) ); return self::respond( $client->submit( $urls ) ); }, 'permission_callback' => static function () { if ( ! current_user_can( 'manage_options' ) ) { return self::forbidden(); } return self::spend( 'bingsubmit', 40 ); }, 'args' => array( 'limit' => array( 'type' => 'integer', 'minimum' => 1, 'maximum' => 100, 'default' => 25, ), ), ) ); } ); } /** * @param array|WP_Error $result Client result. */ private static function respond( array|WP_Error $result ): WP_REST_Response|WP_Error { return $result instanceof WP_Error ? $result : new WP_REST_Response( $result ); } }