$post->post_title, 'excerpt' => $post->post_excerpt, 'content' => $post->post_content, ); $builder = page_builder( $post ); if ( $builder && ( $document = builder_document( $post, $builder ) ) ) { unset( $fields['content'] ); $fields['builder'] = $document; } foreach ( SEO_FIELDS as $key ) { if ( metadata_exists( 'post', $post->ID, $key ) ) { $fields[ $key ] = (string) get_post_meta( $post->ID, $key, true ); } } return $fields; } function source_version( \WP_Post $post, array $fields ): string { return fingerprint( array( $post->post_type, $post->post_status, $post->post_password, pll_get_post_language( $post->ID ), $fields ) ); } function is_supported_source( ?\WP_Post $post ): bool { if ( ! $post || ! isset( translation_post_types()[ $post->post_type ] ) || 'publish' !== $post->post_status || '' !== $post->post_password ) { return false; } $builder = page_builder( $post ); return ! $builder || ( builder_available( $builder ) && null !== builder_document( $post, $builder ) ); } /** * Run the same deterministic traversal for extraction and reconstruction. * Each segment ID depends on the source text and how it is used. */ function transform_fields( array $fields, callable $translate, array &$unsupported ): array { foreach ( $fields as $key => $text ) { $fields[ $key ] = match ( $key ) { 'content' => transform_blocks( parse_blocks( $text ), $translate, $unsupported ), 'builder' => transform_builder( $text, $translate, $unsupported ), 'excerpt' => transform_html( $text, $translate ), default => translate_segment( $text, $key, $translate ), }; } return $fields; } function translate_segment( string $value, string $kind, callable $translate ): string { $core = preg_replace( '/^\s+|\s+$/u', '', $value ); if ( ! is_string( $core ) || ! visible_text( $core ) ) { return $value; } $id = fingerprint( array( $kind, $core ) ); $result = $translate( $id, $core, $kind ); $start = strpos( $value, $core ); return substr( $value, 0, $start ) . $result . substr( $value, $start + strlen( $core ) ); } function transform_blocks( array $blocks, callable $translate, array &$unsupported ): string { $output = ''; foreach ( $blocks as $block ) { $name = $block['blockName']; $type = $name ? \WP_Block_Type_Registry::get_instance()->get_registered( $name ) : null; $dynamic = $type && $type->is_dynamic() && ! in_array( $name, STORED_CONTENT_BLOCKS, true ); if ( $name && ( ! str_starts_with( $name, 'core/' ) || in_array( $name, PRESERVE_BLOCKS, true ) || $dynamic ) ) { $unsupported[ $name ] = true; $output .= serialize_block( $block ); continue; } $first_chunk = $block['innerContent'][0] ?? ''; if ( $name && $first_chunk ) { $root = new \WP_HTML_Tag_Processor( $first_chunk ); if ( $root->next_tag() && preserve_element( $root ) ) { $output .= serialize_block( $block ); continue; } } if ( 'core/image' === $name && isset( $block['attrs']['alt'] ) && is_string( $block['attrs']['alt'] ) ) { $block['attrs']['alt'] = translate_segment( $block['attrs']['alt'], 'alt', $translate ); } $inner = ''; $index = 0; foreach ( $block['innerContent'] as $chunk ) { if ( null === $chunk ) { $inner .= transform_blocks( array( $block['innerBlocks'][ $index++ ] ), $translate, $unsupported ); } else { $inner .= transform_html( $chunk, $translate ); } } $output .= $name ? get_comment_delimited_block_content( $name, $block['attrs'], $inner ) : $inner; } return $output; } function preserve_element( \WP_HTML_Tag_Processor $processor ): bool { return 'no' === strtolower( (string) $processor->get_attribute( 'translate' ) ) || $processor->has_class( 'notranslate' ) || null !== $processor->get_attribute( 'data-no-translate' ); } function transform_html( string $html, callable $translate, string $language = '' ): string { $processor = new \WP_HTML_Tag_Processor( $html ); $protected = array(); while ( $processor->next_token() ) { if ( '#text' === $processor->get_token_type() && ! $protected ) { $text = $processor->get_modifiable_text(); $next = translate_segment( $text, 'body', $translate ); if ( $next !== $text ) { $processor->set_modifiable_text( $next ); } continue; } $tag = $processor->get_tag(); if ( ! $tag || in_array( $tag, ATOMIC_TAGS, true ) ) { continue; } if ( $processor->is_tag_closer() ) { if ( $protected && $tag === end( $protected ) ) { array_pop( $protected ); } continue; } if ( $protected || preserve_element( $processor ) || in_array( $tag, PRESERVE_TAGS, true ) ) { if ( ! in_array( $tag, VOID_TAGS, true ) && ! $processor->has_self_closing_flag() ) { $protected[] = $tag; } continue; } foreach ( HTML_ATTRIBUTES as $attribute ) { $value = $processor->get_attribute( $attribute ); if ( is_string( $value ) ) { $next = translate_segment( $value, $attribute, $translate ); if ( $value !== $next ) { $processor->set_attribute( $attribute, $next ); } } } } $output = $processor->get_updated_html(); return '' === $language ? $output : localize_internal_links( $output, $language ); } function word_count( string $text, string $locale ): int { if ( class_exists( '\IntlBreakIterator' ) ) { $iterator = \IntlBreakIterator::createWordInstance( $locale ); if ( $iterator && $iterator->setText( $text ) ) { $count = 0; foreach ( $iterator as $position ) { $count += (int) ( $iterator->getRuleStatus() >= \IntlBreakIterator::WORD_NUMBER ); } return $count; } } // Give scripts commonly written without spaces individually countable letters. $expanded = preg_replace( '/([\x{0E00}-\x{0EFF}\x{1000}-\x{109F}\x{1780}-\x{17FF}\x{3040}-\x{30FF}\x{3400}-\x{9FFF}\x{F900}-\x{FAFF}\x{20000}-\x{323AF}])/u', ' $1 ', $text ); return preg_match_all( '/[\p{L}\p{N}][\p{L}\p{N}\p{M}]*(?:[’\x27-][\p{L}\p{N}][\p{L}\p{N}\p{M}]*)*/u', $expanded ?: '' ) ?: 0; } /** * Merge changes when top-level block structure still lines up. WordPress stores * inter-block whitespace as freeform blocks, so compare serialized blocks. */ function merge_edited_content( string $current, string $baseline, string $incoming ): ?array { if ( $current === $baseline ) { return array( 'html' => $incoming, 'kept' => 0, 'conflicts' => 0 ); } $versions = array_map( 'parse_blocks', array( $current, $baseline, $incoming ) ); $shape = static fn( $blocks ) => array_column( $blocks, 'blockName' ); if ( $shape( $versions[0] ) !== $shape( $versions[1] ) ) { return null; } $blocks = array_map( static fn( $items ) => array_map( 'serialize_block', $items ), $versions ); $original = array_count_values( $blocks[1] ); $edited_positions = array_flip( $blocks[0] ); $incoming_positions = array_flip( $blocks[2] ); $incoming_counts = array_count_values( $blocks[2] ); $same_layout = $shape( $versions[1] ) === $shape( $versions[2] ); foreach ( $blocks[1] as $index => $value ) { if ( 1 !== $original[ $value ] ) { continue; } if ( isset( $edited_positions[ $value ] ) && $edited_positions[ $value ] !== $index ) { return null; } if ( ( $incoming_counts[ $value ] ?? 0 ) === 1 && $incoming_positions[ $value ] !== $index ) { $same_layout = false; } } $kept = 0; $conflict = 0; foreach ( $blocks[0] as $index => $edited ) { $value = $blocks[1][ $index ]; if ( $edited === $value ) { continue; } $matches = $incoming_counts[ $value ] ?? 0; if ( 1 === $original[ $value ] && 1 === $matches ) { $destination = $incoming_positions[ $value ]; } elseif ( $same_layout && ! $matches && 1 === $original[ $value ] ) { $destination = $index; } else { // Ambiguous matches must stay in the existing edited field. return null; } ++$kept; $conflict += (int) ( $value !== $blocks[2][ $destination ] && $edited !== $blocks[2][ $destination ] ); $blocks[2][ $destination ] = $edited; } return array( 'html' => implode( '', $blocks[2] ), 'kept' => $kept, 'conflicts' => $conflict ); } /** * Use published Polylang siblings for ordinary internal page links. * The original link remains usable while the linked page is awaiting translation. */ function localize_internal_links( string $html, string $language ): string { $home = wp_parse_url( home_url( '/' ) ); $processor = new \WP_HTML_Tag_Processor( $html ); $cache = array(); while ( $processor->next_tag( 'A' ) ) { $href = $processor->get_attribute( 'href' ); if ( ! is_string( $href ) || '' === $href || str_starts_with( $href, '#' ) ) { continue; } $parts = wp_parse_url( $href ); if ( ! $parts || ( isset( $parts['user'] ) || isset( $parts['pass'] ) ) || ( isset( $parts['scheme'] ) && ! in_array( $parts['scheme'], array( 'http', 'https' ), true ) ) ) { continue; } if ( isset( $parts['host'] ) && ( strcasecmp( $parts['host'], $home['host'] ) || ( $parts['port'] ?? null ) !== ( $home['port'] ?? null ) ) ) { continue; } if ( ! isset( $parts['host'] ) ) { if ( ! str_starts_with( $href, '/' ) || str_starts_with( $href, '//' ) ) { continue; } $absolute = $home['scheme'] . '://' . $home['host'] . ( isset( $home['port'] ) ? ':' . $home['port'] : '' ) . $href; } else { $absolute = isset( $parts['scheme'] ) ? $href : $home['scheme'] . ':' . $href; } if ( ! array_key_exists( $absolute, $cache ) ) { $source_id = url_to_postid( $absolute ); $target_id = $source_id ? pll_get_post( $source_id, $language ) : 0; $target = $target_id ? get_post( $target_id ) : null; $cache[ $absolute ] = $target && 'publish' === $target->post_status && '' === $target->post_password ? get_permalink( $target ) : ''; } if ( $cache[ $absolute ] ) { $query = array(); wp_parse_str( $parts['query'] ?? '', $query ); unset( $query['p'], $query['page_id'], $query['name'], $query['lang'] ); $localized = $query ? add_query_arg( $query, $cache[ $absolute ] ) : $cache[ $absolute ]; $processor->set_attribute( 'href', $localized . ( isset( $parts['fragment'] ) ? '#' . $parts['fragment'] : '' ) ); } } return $processor->get_updated_html(); }