strings = $strings; } public function register_routes(): void { register_rest_route( self::NAMESPACE, '/strings', array( array( 'methods' => 'GET', 'callback' => array( $this, 'list' ), 'permission_callback' => array( $this, 'can_lang' ), ), array( 'methods' => 'POST', 'callback' => array( $this, 'save' ), 'permission_callback' => array( $this, 'can_lang' ), ), ) ); register_rest_route( self::NAMESPACE, '/strings/translate', array( 'methods' => 'POST', 'callback' => array( $this, 'translate' ), 'permission_callback' => array( $this, 'can_lang' ), ) ); register_rest_route( self::NAMESPACE, '/strings/delete', array( 'methods' => 'POST', 'callback' => array( $this, 'delete' ), 'permission_callback' => array( $this, 'can_manage' ), ) ); register_rest_route( self::NAMESPACE, '/strings/files', array( array( 'methods' => 'GET', 'callback' => array( $this, 'files' ), 'permission_callback' => array( $this, 'can_manage' ), ), array( 'methods' => 'POST', 'callback' => array( $this, 'start_file' ), 'permission_callback' => array( $this, 'can_manage' ), ), ) ); register_rest_route( self::NAMESPACE, '/strings/files/(?P[A-Za-z0-9_\-]+)/refresh', array( 'methods' => 'POST', 'callback' => array( $this, 'refresh_file' ), 'permission_callback' => array( $this, 'can_manage' ), ) ); } public function can_lang( \WP_REST_Request $r ): bool { $lang = (string) $r['lang']; return $lang ? Roles::can_translate( $lang ) : Roles::can_translate(); } /** * @return \WP_REST_Response|\WP_Error */ public function list( \WP_REST_Request $r ) { $lang = (string) $r['lang']; if ( ! Languages::is_target( $lang ) ) { return new \WP_Error( 'translateplus_lang', __( 'Choose a language.', 'translateplus' ), array( 'status' => 400 ) ); } return rest_ensure_response( $this->strings->query( array( 'lang' => $lang, 'domain' => sanitize_text_field( (string) $r['domain'] ), 'status' => sanitize_key( (string) $r['status'] ), 'search' => sanitize_text_field( (string) $r['search'] ), 'page' => (int) $r['page'], 'per_page' => (int) $r['per_page'], ) ) ); } public function save( \WP_REST_Request $r ): \WP_REST_Response { $items = array(); foreach ( (array) $r->get_param( 'items' ) as $item ) { if ( ! is_array( $item ) ) { continue; } $id = (int) ( $item['id'] ?? 0 ); // The permission check uses the row's own language when an ID is given, // never only the language named in the request. $lang = $id ? $this->strings->lang_of( $id ) : sanitize_text_field( (string) ( $item['lang'] ?? $r['lang'] ) ); if ( '' === $lang || ! Roles::can_translate( $lang ) ) { continue; } $items[] = array( 'id' => $id, 'hash' => preg_replace( '/[^a-f0-9]/', '', (string) ( $item['hash'] ?? '' ) ), 'lang' => $lang, 'translation' => current_user_can( 'unfiltered_html' ) ? (string) ( $item['translation'] ?? '' ) : wp_kses_post( (string) ( $item['translation'] ?? '' ) ), ); } return rest_ensure_response( array( 'saved' => $this->strings->save( $items ) ) ); } /** * @return \WP_REST_Response|\WP_Error */ public function translate( \WP_REST_Request $r ) { $lang = (string) $r['lang']; if ( ! Languages::is_target( $lang ) ) { return new \WP_Error( 'translateplus_lang', __( 'Choose a language.', 'translateplus' ), array( 'status' => 400 ) ); } try { $res = $this->strings->auto_translate( $lang, min( 300, max( 1, (int) ( $r['limit'] ?: 150 ) ) ), array_map( 'intval', (array) $r['ids'] ), sanitize_text_field( (string) $r['domain'] ) ); } catch ( ApiException $e ) { return $this->api_error( $e ); } return rest_ensure_response( $res ); } public function delete( \WP_REST_Request $r ): \WP_REST_Response { return rest_ensure_response( array( 'deleted' => $this->strings->delete( (array) $r['ids'] ) ) ); } public function files(): \WP_REST_Response { return rest_ensure_response( array( 'sources' => FileJobs::sources(), 'jobs' => array_values( FileJobs::jobs() ), ) ); } /** * @return \WP_REST_Response|\WP_Error */ public function start_file( \WP_REST_Request $r ) { $jobs = FileJobs::instance(); try { $job = $jobs->start( sanitize_text_field( (string) $r['domain'] ), array_map( 'sanitize_text_field', (array) $r['languages'] ) ); } catch ( ApiException $e ) { return $this->api_error( $e ); } catch ( \RuntimeException $e ) { return new \WP_Error( 'translateplus_file', $e->getMessage(), array( 'status' => 400 ) ); } return rest_ensure_response( $job ); } public function refresh_file( \WP_REST_Request $r ): \WP_REST_Response { FileJobs::instance()->poll( (string) $r['job'] ); return rest_ensure_response( FileJobs::jobs()[ (string) $r['job'] ] ?? null ); } }