array( 'type' => 'integer', 'required' => true, ), ); // Machine-translate a source post into this post's language; returns fields, saves nothing. register_rest_route( self::NS, '/post/(?P\d+)/machine', array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => $this->safely( array( $this, 'post' ) ), 'permission_callback' => array( $this, 'can_machine_translate_post' ), 'args' => array_merge( self::id_arg(), $source, array( 'lang' => array( 'type' => 'string' ) ) ), ) ); // Machine-translate the SEO fields of a source post into this post (saved immediately). register_rest_route( self::NS, '/post/(?P\d+)/translate-meta', array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => $this->safely( array( $this, 'meta' ) ), 'permission_callback' => array( $this, 'can_machine_translate_post' ), 'args' => array_merge( self::id_arg(), $source ), ) ); register_rest_route( self::NS, '/translate-strings', array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => $this->safely( array( $this, 'strings' ) ), 'permission_callback' => array( self::class, 'can_machine_translate' ), 'args' => array( 'strings' => array( 'type' => 'array', 'required' => true, 'items' => array( 'type' => 'string' ), ), 'kinds' => array( 'type' => 'array', 'items' => array( 'type' => 'string', 'enum' => array( 'html', 'text' ), ), ), 'from' => array( 'type' => 'string', 'required' => true, ), 'to' => array( 'type' => 'string', 'required' => true, ), 'hints' => array( 'type' => 'array', 'items' => array( 'type' => 'string' ), ), 'context' => array( 'type' => 'object', 'properties' => array( 'title' => array( 'type' => 'string' ), 'type' => array( 'type' => 'string' ), 'outline' => array( 'type' => 'string' ), ), 'additionalProperties' => false, ), ), ) ); } /** @param WP_REST_Request> $request */ public function post( WP_REST_Request $request ): WP_REST_Response|WP_Error { $source = get_post( (int) $request['source'] ); if ( ! $source || ! current_user_can( 'read_post', $source->ID ) ) { return new WP_Error( 'blocklingo_no_post', __( 'Source post not found.', 'blocklingo' ), array( 'status' => 404 ) ); } $from = $this->posts->language_of( $source->ID ); $to = $request['lang'] ? (string) $request['lang'] : $this->posts->language_of( (int) $request['id'] ); if ( $from === $to ) { return new WP_Error( 'blocklingo_same_lang', __( 'Source is already in this language.', 'blocklingo' ), array( 'status' => 400 ) ); } self::allow_long_request(); $result = $this->translator->translate_post( $source, $from, $to ); return $result instanceof WP_Error ? self::fail( $result, 502 ) : new WP_REST_Response( $result ); } /** @param WP_REST_Request> $request */ public function meta( WP_REST_Request $request ): WP_REST_Response|WP_Error { $target = (int) $request['id']; $source = get_post( (int) $request['source'] ); if ( ! $source || ! current_user_can( 'read_post', $source->ID ) ) { return new WP_Error( 'blocklingo_no_post', __( 'Source post not found.', 'blocklingo' ), array( 'status' => 404 ) ); } if ( ! in_array( $source->ID, array_map( 'intval', $this->posts->group( $target ) ), true ) ) { return new WP_Error( 'blocklingo_not_linked', __( 'Source is not a translation of this post.', 'blocklingo' ), array( 'status' => 400 ) ); } $result = $this->meta->translate( $source->ID, $target ); if ( $result instanceof WP_Error ) { return self::fail( $result, 'blocklingo_same_lang' === $result->get_error_code() ? 400 : 502 ); } return new WP_REST_Response( array( 'count' => count( $result ), 'fields' => (object) $result, ) ); } /** @param WP_REST_Request> $request */ public function strings( WP_REST_Request $request ): WP_REST_Response|WP_Error { $strings = (array) $request['strings']; if ( count( $strings ) > self::MAX_STRINGS ) { return new WP_Error( 'blocklingo_too_many', __( 'Too many strings in one request.', 'blocklingo' ), array( 'status' => 400 ) ); } self::allow_long_request(); $result = $this->translator->translate_strings( $strings, $request['kinds'] ? (array) $request['kinds'] : array(), (string) $request['from'], (string) $request['to'], null, array( 'hints' => $request['hints'] ? (array) $request['hints'] : array(), 'context' => $request['context'] ? (array) $request['context'] : array(), ) ); if ( $result instanceof WP_Error ) { return self::fail( $result, 502 ); } return new WP_REST_Response( array( 'strings' => array_values( $result ) ) ); } }