> Argument schemas. */ public static function submit_args(): array { return [ 'urls' => [ 'type' => 'array', 'required' => false, 'default' => [], 'maxItems' => self::URLS_MAX, 'items' => [ 'type' => 'string' ], 'sanitize_callback' => [ self::class, 'sanitize_urls' ], 'validate_callback' => 'rest_validate_request_arg', ], 'all' => Controller::bool_arg( false ), ]; } /** * Sanitizes the `urls` argument into a list of absolute URLs. * * Anything that is not a string — an array from `urls[][]=x`, an object * from a JSON body — is dropped rather than passed to `esc_url_raw()`, * which would raise a `TypeError`. * * @param mixed $value Raw argument value. * @return array Unique, non-empty URLs. */ public static function sanitize_urls( $value ): array { $urls = []; foreach ( is_array( $value ) ? $value : [] as $url ) { $clean = is_string( $url ) ? (string) esc_url_raw( $url ) : ''; if ( '' !== $clean ) { $urls[] = $clean; } } return array_slice( array_values( array_unique( $urls ) ), 0, self::URLS_MAX ); } /** * Submits URLs, or queues the whole site. * * @param WP_REST_Request $request The request. * @return array|WP_Error Keys `result` and `log`, or a 400. */ public function submit( WP_REST_Request $request ) { $module = Controller::module( 'indexnow', Modules\IndexNow::class ); if ( ! $module instanceof Modules\IndexNow ) { return new WP_Error( 'acid_aeo_unavailable', __( 'The IndexNow module is not available.', 'acid-aeo' ), [ 'status' => 501 ] ); } if ( ! $module->is_enabled() ) { return new WP_Error( 'acid_aeo_indexnow_off', Modules\IndexNow::switched_off_message(), [ 'status' => 409 ] ); } $all = (bool) $request->get_param( 'all' ); $urls = $request->get_param( 'urls' ); $urls = is_array( $urls ) ? $urls : []; if ( ! $all && [] === $urls ) { return new WP_Error( 'acid_aeo_no_urls', __( 'Send a list of URLs, or all: true to queue the whole site.', 'acid-aeo' ), [ 'status' => 400 ] ); } $result = $all ? [ 'queued' => $module->submit_all() ] : $module->submit( $urls ); return [ 'result' => $result, 'log' => $module->log(), ]; } /** * The submission history and where the key file lives. * * @return array|WP_Error Keys `log` and `key_url`. */ public function log() { $module = Controller::module( 'indexnow', Modules\IndexNow::class ); if ( ! $module instanceof Modules\IndexNow ) { return new WP_Error( 'acid_aeo_unavailable', __( 'The IndexNow module is not available.', 'acid-aeo' ), [ 'status' => 501 ] ); } return [ 'log' => $module->log(), 'key_url' => $module->key_url(), ]; } }