posts = $posts; } public function register_routes(): void { $lang = array( 'type' => 'string', 'required' => true, ); register_rest_route( self::NAMESPACE, '/posts/(?P\d+)/translations', array( 'methods' => 'GET', 'callback' => array( $this, 'status' ), 'permission_callback' => array( $this, 'can_view_post' ), ) ); register_rest_route( self::NAMESPACE, '/posts/(?P\d+)/translate/(?P[A-Za-z\-]+)', array( 'methods' => 'POST', 'callback' => array( $this, 'translate' ), 'permission_callback' => array( $this, 'can_translate_lang' ), 'args' => array( 'force' => array( 'type' => 'boolean' ), ), ) ); register_rest_route( self::NAMESPACE, '/posts/(?P\d+)/segments/(?P[A-Za-z\-]+)', array( array( 'methods' => 'GET', 'callback' => array( $this, 'get_segments' ), 'permission_callback' => array( $this, 'can_translate_lang' ), ), array( 'methods' => 'POST', 'callback' => array( $this, 'save_segments' ), 'permission_callback' => array( $this, 'can_translate_lang' ), ), ) ); register_rest_route( self::NAMESPACE, '/posts/(?P\d+)/segments/(?P[A-Za-z\-]+)/machine', array( 'methods' => 'POST', 'callback' => array( $this, 'machine_segments' ), 'permission_callback' => array( $this, 'can_translate_lang' ), ) ); register_rest_route( self::NAMESPACE, '/posts/(?P\d+)/approve/(?P[A-Za-z\-]+)', array( 'methods' => 'POST', 'callback' => array( $this, 'approve' ), 'permission_callback' => array( $this, 'can_review' ), ) ); register_rest_route( self::NAMESPACE, '/translations', array( 'methods' => 'GET', 'callback' => array( $this, 'list' ), 'permission_callback' => array( $this, 'can_translate' ), ) ); register_rest_route( self::NAMESPACE, '/stats', array( 'methods' => 'GET', 'callback' => array( $this, 'stats' ), 'permission_callback' => array( $this, 'can_translate' ), ) ); } // ------------------------------------------------------------------ // Permissions // ------------------------------------------------------------------ public function can_view_post( \WP_REST_Request $r ): bool { return Roles::can_translate() && Roles::can_access_post( (int) $r['id'] ); } /** * Translate permission for the requested language AND access to the requested post. */ public function can_translate_lang( \WP_REST_Request $r ): bool { return Roles::can_translate( (string) $r['lang'] ) && Roles::can_access_post( (int) $r['id'] ); } public function can_review( \WP_REST_Request $r ): bool { return Roles::can_review( (string) $r['lang'] ) && Roles::can_access_post( (int) $r['id'] ); } // ------------------------------------------------------------------ // Handlers // ------------------------------------------------------------------ /** * @return \WP_REST_Response|\WP_Error */ public function status( \WP_REST_Request $r ) { $post = get_post( (int) $r['id'] ); if ( ! $post ) { return new \WP_Error( 'not_found', __( 'Post not found.', 'translateplus' ), array( 'status' => 404 ) ); } return rest_ensure_response( self::post_status( $post ) ); } /** * Translation status of a post (or of the source of a translation). */ public static function post_status( \WP_Post $post ): array { $source_id = Groups::source_id( 'post', (int) $post->ID ); $members = Groups::members( 'post', $source_id ); $langs = array(); foreach ( Languages::targets() as $lang ) { $row = $members[ $lang ] ?? null; $id = $row ? (int) $row['object_id'] : 0; if ( $id && ! get_post( $id ) ) { $id = 0; } $langs[] = array( 'code' => $lang, 'name' => Languages::name( $lang ), 'post_id' => $id, 'status' => $id ? $row['status'] : 'missing', 'post_status' => $id ? get_post_status( $id ) : null, 'edit_url' => $id ? get_edit_post_link( $id, 'raw' ) : null, 'view_url' => $id ? get_permalink( $id ) : null, 'editor_url' => admin_url( 'admin.php?page=translateplus-editor&post=' . $source_id . '&lang=' . rawurlencode( $lang ) ), 'segments' => $id ? Segments::summary( 'post', $source_id, $lang ) : null, ); } return array( 'post_id' => (int) $post->ID, 'source_id' => $source_id, 'is_translation' => $source_id !== (int) $post->ID, 'language' => Groups::lang_of( 'post', (int) $post->ID ), 'source_language' => Languages::source(), 'source_title' => get_the_title( $source_id ), 'source_edit_url' => get_edit_post_link( $source_id, 'raw' ), 'translatable' => PostTypes::is_translatable( $post->post_type ), 'languages' => $langs, ); } /** * @return \WP_REST_Response|\WP_Error */ public function translate( \WP_REST_Request $r ) { $lang = (string) $r['lang']; $reviewer = Roles::can_review( $lang ); $existing = Groups::translation( 'post', Groups::source_id( 'post', (int) $r['id'] ), $lang ); if ( ! $reviewer && ( $r['force'] || Roles::is_live( $existing ) ) ) { return new \WP_Error( 'translateplus_review_required', __( 'Only reviewers can update a published translation. Edit it in the translation editor to suggest changes.', 'translateplus' ), array( 'status' => 403 ) ); } try { $target = $this->posts->translate( (int) $r['id'], $lang, array( 'force' => $reviewer && (bool) $r['force'], 'pending' => ! $reviewer, ) ); } catch ( ApiException $e ) { return $this->api_error( $e ); } catch ( \RuntimeException $e ) { return new \WP_Error( 'translateplus_failed', $e->getMessage(), array( 'status' => 400 ) ); } $post = get_post( $target ); return rest_ensure_response( array( 'translated_id' => $target, 'stats' => $this->posts->translator()->stats(), 'status' => self::post_status( $post ), ) ); } /** * @return \WP_REST_Response|\WP_Error */ public function get_segments( \WP_REST_Request $r ) { $source_id = Groups::source_id( 'post', (int) $r['id'] ); $lang = (string) $r['lang']; $post = get_post( $source_id ); if ( ! $post ) { return new \WP_Error( 'not_found', __( 'Post not found.', 'translateplus' ), array( 'status' => 404 ) ); } $target = Groups::translation( 'post', $source_id, $lang ); $stored = Segments::get( 'post', $source_id, $lang ); $source = $this->posts->collect( $post ); $memory = Memory::lookup( array_values( $source ), Languages::source(), $lang ); $items = array(); foreach ( $source as $key => $text ) { if ( ! Translator::is_translatable( (string) $text ) ) { continue; } $row = $stored[ $key ] ?? null; $current = $row && $row['source_hash'] === md5( (string) $text ); $items[] = array( 'key' => (string) $key, 'group' => self::segment_group( (string) $key ), 'source' => (string) $text, 'translation' => $current ? $row['translation'] : ( $memory[ $text ] ?? '' ), 'status' => $current ? ( '' === $row['translation'] ? 'missing' : $row['status'] ) : ( $row ? Segments::STATUS_OUTDATED : ( isset( $memory[ $text ] ) ? Segments::STATUS_AUTO : 'missing' ) ), 'origin' => $current ? $row['origin'] : ( isset( $memory[ $text ] ) ? Segments::ORIGIN_MEMORY : '' ), 'html' => Translator::is_html( (string) $text ), ); } $row = $target ? Groups::row( 'post', $target ) : null; return rest_ensure_response( array( 'source_id' => $source_id, 'source_title' => get_the_title( $source_id ), 'source_lang' => Languages::source(), 'lang' => $lang, 'lang_name' => Languages::name( $lang ), 'post_type' => get_post_type_object( $post->post_type )->labels->singular_name ?? $post->post_type, 'translated_id' => $target, 'status' => $row ? $row['status'] : 'missing', 'post_status' => $target ? get_post_status( $target ) : null, 'view_url' => $target ? get_permalink( $target ) : null, 'edit_url' => $target ? get_edit_post_link( $target, 'raw' ) : null, 'source_url' => get_permalink( $source_id ), 'can_review' => Roles::can_review( $lang ), 'rtl' => Languages::is_rtl( $lang ), 'segments' => $items, ) ); } private static function segment_group( string $key ): string { if ( 'title' === $key || 'excerpt' === $key ) { return $key; } $prefix = strtok( $key, ':.' ); $map = array( 'content' => 'content', 'meta' => 'fields', 'elementor' => 'content', 'bricks' => 'content', 'woo' => 'product', ); if ( 0 === strpos( $key, 'meta:' ) && preg_match( '/(yoast|rank_math|seopress|aioseo)/', $key ) ) { return 'seo'; } return $map[ $prefix ] ?? 'fields'; } /** * Save manual edits and rebuild the translated post. * * @return \WP_REST_Response|\WP_Error */ public function save_segments( \WP_REST_Request $r ) { $source_id = Groups::source_id( 'post', (int) $r['id'] ); $lang = (string) $r['lang']; $clean = array(); foreach ( (array) $r->get_param( 'segments' ) as $e ) { if ( ! is_array( $e ) || ! isset( $e['key'], $e['translation'] ) ) { continue; } $clean[] = array( 'key' => sanitize_text_field( (string) $e['key'] ), 'translation' => current_user_can( 'unfiltered_html' ) ? (string) $e['translation'] : wp_kses_post( (string) $e['translation'] ), 'status' => isset( $e['status'] ) ? sanitize_key( $e['status'] ) : Segments::STATUS_REVIEWED, ); } $review = new Review( $this->posts ); try { $result = $review->save_edits( $source_id, $lang, $clean ); if ( $r->get_param( 'publish' ) && Roles::can_review( $lang ) ) { $review->approve( $source_id, $lang ); } } catch ( ApiException $e ) { return $this->api_error( $e ); } catch ( \RuntimeException $e ) { return new \WP_Error( 'translateplus_failed', $e->getMessage(), array( 'status' => 400 ) ); } $response = $this->get_segments( $r ); if ( $response instanceof \WP_REST_Response ) { $data = $response->get_data(); $data['saved'] = $result['saved']; $data['pending_review'] = $result['pending_review']; $response->set_data( $data ); } return $response; } /** * Machine-translate specific segments (bypassing memory) for the editor. * * @return \WP_REST_Response|\WP_Error */ public function machine_segments( \WP_REST_Request $r ) { $source_id = Groups::source_id( 'post', (int) $r['id'] ); $post = get_post( $source_id ); $keys = array_map( 'strval', (array) $r->get_param( 'keys' ) ); if ( ! $post ) { return new \WP_Error( 'not_found', __( 'Post not found.', 'translateplus' ), array( 'status' => 404 ) ); } $source = array_intersect_key( $this->posts->collect( $post ), array_flip( $keys ) ); try { $out = $this->posts->translator()->translate( $source, Languages::source(), (string) $r['lang'], array( 'memory' => false ) ); } catch ( ApiException $e ) { return $this->api_error( $e ); } return rest_ensure_response( array( 'translations' => $out ) ); } /** * @return \WP_REST_Response|\WP_Error */ public function approve( \WP_REST_Request $r ) { $source_id = Groups::source_id( 'post', (int) $r['id'] ); if ( ! Groups::translation( 'post', $source_id, (string) $r['lang'] ) ) { return new \WP_Error( 'not_found', __( 'This post has not been translated yet.', 'translateplus' ), array( 'status' => 404 ) ); } try { ( new Review( $this->posts ) )->approve( $source_id, (string) $r['lang'] ); } catch ( \RuntimeException $e ) { return new \WP_Error( 'translateplus_failed', $e->getMessage(), array( 'status' => 400 ) ); } return rest_ensure_response( self::post_status( get_post( $source_id ) ) ); } /** * Paginated list of source posts with per-language status. */ public function list( \WP_REST_Request $r ): \WP_REST_Response { global $wpdb; $types = array_values( array_intersect( (array) ( $r['post_type'] ? array( $r['post_type'] ) : PostTypes::enabled() ), PostTypes::enabled() ) ); $page = max( 1, (int) $r['page'] ); $per_page = min( 100, max( 1, (int) ( $r['per_page'] ?: 20 ) ) ); $lang = (string) $r['lang']; $status = sanitize_key( (string) $r['status'] ); $search = sanitize_text_field( (string) $r['search'] ); if ( ! $types ) { return rest_ensure_response( array( 'items' => array(), 'total' => 0, 'pages' => 0 ) ); } // Filter modes, each expressed in the fixed query below. $like = $search ? '%' . $wpdb->esc_like( $search ) . '%' : ''; $lang_filter = ( $lang && Languages::is_target( $lang ) && $status ) ? $lang : ''; $missing = $lang_filter && 'missing' === $status ? 1 : 0; $any_status = ! $lang_filter && in_array( $status, array( 'outdated', 'review', 'manual' ), true ) ? $status : ''; $type_list = implode( ',', $types ); // Pre-filter in SQL by status (scheduled posts and drafts need edit rights, private // posts read_private rights) and password; the per-post check below is authoritative. $can_edit_others = current_user_can( 'edit_others_posts' ) ? 1 : 0; $statuses = array( 'publish' ); if ( current_user_can( 'read_private_posts' ) ) { $statuses[] = 'private'; } if ( $can_edit_others ) { array_push( $statuses, 'future', 'draft', 'pending' ); } $status_list = implode( ',', $statuses ); // phpcs:disable WordPress.DB.DirectDatabaseQuery $candidates = $wpdb->get_col( $wpdb->prepare( "SELECT p.ID FROM %i p LEFT JOIN %i g ON g.object_type = 'post' AND g.object_id = p.ID WHERE INSTR( CONCAT( ',', %s, ',' ), CONCAT( ',', p.post_status, ',' ) ) > 0 AND ( %d = 1 OR p.post_password = '' ) AND ( g.id IS NULL OR g.group_id = g.object_id ) AND INSTR( CONCAT( ',', %s, ',' ), CONCAT( ',', p.post_type, ',' ) ) > 0 AND ( %s = '' OR p.post_title LIKE %s ) AND ( %s = '' OR ( %d = 1 AND NOT EXISTS ( SELECT 1 FROM %i x WHERE x.object_type = 'post' AND x.group_id = p.ID AND x.lang = %s ) ) OR ( %d = 0 AND EXISTS ( SELECT 1 FROM %i x WHERE x.object_type = 'post' AND x.group_id = p.ID AND x.lang = %s AND x.status = %s ) ) ) AND ( %s = '' OR EXISTS ( SELECT 1 FROM %i x WHERE x.object_type = 'post' AND x.group_id = p.ID AND x.status = %s ) ) ORDER BY p.post_modified DESC", $wpdb->posts, $wpdb->tp_translation_groups, $status_list, $can_edit_others, $type_list, $like, $like, $lang_filter, $missing, $wpdb->tp_translation_groups, $lang_filter, $missing, $wpdb->tp_translation_groups, $lang_filter, $status, $any_status, $wpdb->tp_translation_groups, $any_status ) ); // phpcs:enable // Only posts this user may access are counted and returned. $accessible = array_values( array_filter( array_map( 'intval', $candidates ), array( Roles::class, 'can_access_post' ) ) ); $total = count( $accessible ); $ids = array_slice( $accessible, ( $page - 1 ) * $per_page, $per_page ); $items = array(); foreach ( $ids as $id ) { $post = get_post( (int) $id ); $status_data = self::post_status( $post ); $items[] = array( 'id' => (int) $id, 'title' => get_the_title( $post ) ?: __( '(no title)', 'translateplus' ), 'type' => $post->post_type, 'type_label' => get_post_type_object( $post->post_type )->labels->singular_name ?? $post->post_type, 'post_status' => $post->post_status, 'modified' => mysql_to_rfc3339( $post->post_modified_gmt ), 'edit_url' => get_edit_post_link( $post->ID, 'raw' ), 'view_url' => get_permalink( $post ), 'languages' => $status_data['languages'], ); } return rest_ensure_response( array( 'items' => $items, 'total' => $total, 'pages' => (int) ceil( $total / $per_page ), ) ); } public function stats(): \WP_REST_Response { $coverage = Groups::coverage( PostTypes::enabled() ); $out = array(); foreach ( $coverage as $lang => $c ) { $out[] = array( 'code' => $lang, 'name' => Languages::name( $lang ), 'translated' => $c['translated'], 'outdated' => $c['outdated'], 'review' => $c['review'], 'total' => $c['total'], 'percent' => $c['total'] ? min( 100, round( $c['translated'] / $c['total'] * 100, 1 ) ) : 0, ); } return rest_ensure_response( array( 'coverage' => $out, 'memory' => Memory::stats(), 'glossary' => count( \TranslatePlus\Translation\Glossary::all() ), ) ); } }