Click here now
* becomes the template `Click here now`. Inline elements are
* reduced to `` placeholders so attributes are never sent for
* translation, cost nothing, and can't be mangled. Translating whole
* sentences (instead of loose text nodes) is what gives natural grammar.
* - text: a standalone text node.
* - attr: a translatable attribute (alt, title, placeholder, meta description…).
*
* Each template is hashed and looked up in the local translation memory; only
* unknown segments go to Langferry Cloud. If a provider returns a block with
* broken placeholders, that block falls back to per-text-node segments.
*
* @package Langferry
*/
namespace Langferry;
defined( 'ABSPATH' ) || exit;
final class Translator {
const ELEMENT = 1;
const TEXT = 3;
const COMMENT = 8;
/** Never translated, never descended into. */
const SKIP = array(
'script' => 1,
'style' => 1,
'noscript' => 1,
'template' => 1,
'code' => 1,
'pre' => 1,
'textarea' => 1,
'svg' => 1,
'math' => 1,
'iframe' => 1,
'object' => 1,
'embed' => 1,
'canvas' => 1,
'kbd' => 1,
'samp' => 1,
'var' => 1,
'audio' => 1,
'video' => 1,
'base' => 1,
'link' => 1,
);
/** Phrasing elements that may live inside a translated sentence. */
const INLINE = array(
'a' => 1,
'abbr' => 1,
'b' => 1,
'bdi' => 1,
'bdo' => 1,
'cite' => 1,
'data' => 1,
'dfn' => 1,
'em' => 1,
'i' => 1,
'mark' => 1,
'q' => 1,
's' => 1,
'small' => 1,
'span' => 1,
'strong' => 1,
'sub' => 1,
'sup' => 1,
'time' => 1,
'u' => 1,
'del' => 1,
'ins' => 1,
'font' => 1,
'big' => 1,
'tt' => 1,
'acronym' => 1,
'label' => 1,
'button' => 1,
'output' => 1,
'meter' => 1,
'picture' => 1,
);
/** Inline elements kept verbatim as placeholders (no closing content sent). */
const VOID_INLINE = array(
'br' => 1,
'img' => 1,
'wbr' => 1,
'input' => 1,
'source' => 1,
);
const TRANSLATABLE_ATTRS = array( 'title', 'alt', 'placeholder', 'aria-label' );
const META_CONTENT = array(
'description' => 1,
'og:title' => 1,
'og:description' => 1,
'og:image:alt' => 1,
'twitter:title' => 1,
'twitter:description' => 1,
'twitter:image:alt' => 1,
);
/** Safety limits per page. */
const MAX_SEGMENTS = 6000;
/**
* Longest segment (in characters) the cloud accepts: it refuses a batch holding a longer one.
* Such a segment is left in the original language and never sent.
*/
const MAX_SEGMENT_CHARS = 10000;
/** @var string */
private $source;
/** @var string */
private $target;
/** @var array */
private $config;
/** @var Html */
private $html;
/** @var \SplObjectStorage Nodes excluded by selector. */
private $excluded;
/** @var \SplObjectStorage memo for inline_only(). */
private $inline_memo;
/** @var array */
private $segments = array();
/** @var array */
private $stats = array(
'segments' => 0,
'translated' => 0,
'chars' => 0,
'translated_chars' => 0,
'cloud_calls' => 0,
'fallbacks' => 0,
'pending' => 0, // Segments with no translation after this render, oversized ones not counted.
'oversized' => 0, // Segments over MAX_SEGMENT_CHARS: left untranslated, never sent.
'error' => null,
'new_text' => 'sent', // sent | cache_only (see Traffic)
);
/** @var bool Whether text not in the translation memory may be sent to the cloud. */
private $allow_new = true;
/** @var string page | email: what the text is for (Langferry Cloud translates email text on some plans). */
private $purpose = 'page';
/** @var bool Personal data: kept out of the cloud's shared cache and expired here (see Privacy). */
private $private = false;
/** @var string Who asked for this page (Traffic): person | search_engine | pretranslate. */
private $requester = 'person';
/** @var bool Pre-translation of a main page: its request's signed value says m (Fetch_Signature, ruling R66). */
private $main = false;
/**
* Transients set by a paced answer from the cloud, one per requester (docs/qa/scenarios/plugin-traffic.md,
* ruling R67 as amended): people's stops everyone; search engines' stops only them; pre-translation's
* is kept per tier, a main page's (main: true) apart from the other pages', so a paced answer for the
* deep pages never holds back the main pages the cloud still serves. 2.8.0's shared
* langferry_paced_crawlers is read by no one and expires on its own.
*/
const PACED_PEOPLE = 'langferry_paced_people';
const PACED_SEARCH = 'langferry_paced_search';
const PACED_PRETRANSLATE = 'langferry_paced_pretranslate';
const PACED_PRETRANSLATE_MAIN = 'langferry_paced_pretranslate_main';
const PLAN_PAUSE = 'langferry_not_in_plan_';
public function __construct( $source, $target, array $config ) {
$this->source = $source;
$this->target = $target;
$this->config = $config;
}
/**
* Translate a parsed document in place.
*
* @param Html $html Parsed document.
* @param string $url Request path, stored with new strings for editor context.
* @return array Stats (segments, translated, chars, translated_chars, error…).
*/
/**
* Cache only: translations already in the memory are applied, new text stays as it is.
*/
public function allow_new_text( $allow, $requester = 'person' ) {
$this->allow_new = (bool) $allow;
$this->stats['new_text'] = $this->allow_new ? 'sent' : 'cache_only';
$this->requester = in_array( $requester, array( 'person', 'search_engine', 'pretranslate' ), true ) ? $requester : 'person';
// The cloud lets a main page's pre-translation use the part of the budget kept for people (ruling
// R66): only pre-translation, and only when this request's signed value says it is a main page.
$this->main = 'pretranslate' === $this->requester && Fetch_Signature::request_is_main();
}
private function is_crawler() {
return 'person' !== $this->requester;
}
/**
* The paced transient of this render's requester (ruling R67 as amended): people's, search engines',
* or pre-translation's for its tier.
*/
private function paced_transient() {
if ( 'pretranslate' === $this->requester ) {
return $this->main ? self::PACED_PRETRANSLATE_MAIN : self::PACED_PRETRANSLATE;
}
return 'search_engine' === $this->requester ? self::PACED_SEARCH : self::PACED_PEOPLE;
}
public function for_purpose( $purpose ) {
$this->purpose = 'email' === $purpose ? 'email' : 'page';
if ( 'email' === $this->purpose ) {
$this->private = true; // order emails carry names, addresses and order details
}
}
public function mark_private() {
$this->private = true;
}
/**
* Forget "not in your plan" answers, e.g. after a plan change.
*/
public static function clear_plan_pauses() {
delete_transient( self::PLAN_PAUSE . 'email' );
delete_transient( self::PLAN_PAUSE . 'slug' );
}
public function translate( Html $html, $url = '' ) {
$this->html = $html;
$this->excluded = new \SplObjectStorage();
$this->inline_memo = new \SplObjectStorage();
$selectors = array_merge( array( '#wpadminbar', '.notranslate', '.lf-notranslate', '[translate=no]', '[data-no-translation]' ), (array) $this->config['exclusions']['selectors'] );
foreach ( $selectors as $selector ) {
foreach ( $html->select( $selector ) as $node ) {
$this->excluded->offsetSet( $node ); // attach() is deprecated in PHP 8.5
}
}
$root = $html->root();
if ( ! $root ) {
return $this->stats;
}
$this->translate_templates( $url );
$this->collect_attributes( $root );
$this->walk( $root );
if ( empty( $this->segments ) ) {
return $this->stats;
}
$translations = $this->resolve( $this->segments, $url );
// Attributes first: block restoration clones inline elements, and the
// clones must carry the already-translated attributes.
$failed_blocks = array();
foreach ( array( 'attr', 'text', 'block' ) as $type ) {
foreach ( $this->segments as $segment ) {
if ( $segment['type'] !== $type ) {
continue;
}
$translation = $translations[ $segment['source'] ] ?? null;
if ( null === $translation ) {
$this->count_untranslated( $segment );
continue;
}
if ( ! $this->apply( $segment, $translation ) ) {
$failed_blocks[] = $segment;
} else {
++$this->stats['translated'];
$this->stats['translated_chars'] += $segment['len'];
}
}
}
if ( $failed_blocks ) {
$this->fallback( $failed_blocks, $url );
}
return $this->stats;
}
/* ------------------------------------------------------------------ */
/* Collection */
/* ------------------------------------------------------------------ */
private function is_excluded( $el ) {
if ( $this->excluded->offsetExists( $el ) ) { // contains() is deprecated in PHP 8.5
return true;
}
if ( 'no' === strtolower( (string) $el->getAttribute( 'translate' ) ) ) {
return true;
}
return false;
}
/**
* Translate what's inside elements, which scripts copy into the page (PHP 8.4+
* parser: the contents aren't child nodes, so each is translated as a document of its own and
* written back). With libxml the contents are child nodes and walk() goes into them instead.
*/
private function translate_templates( $url ) {
$this->html->each_template(
function ( Html $contents, $template, $inner ) use ( $url ) {
if ( ! $this->template_translatable( $template, $inner ) ) {
return false;
}
$sub = new Translator( $this->source, $this->target, $this->config );
$sub->allow_new = $this->allow_new;
$sub->requester = $this->requester;
$sub->main = $this->main;
$sub->purpose = $this->purpose;
$sub->private = $this->private;
$stats = $sub->translate( $contents, $url );
foreach ( array( 'segments', 'translated', 'chars', 'translated_chars', 'cloud_calls', 'fallbacks', 'pending', 'oversized' ) as $key ) {
$this->stats[ $key ] += (int) ( $stats[ $key ] ?? 0 );
}
if ( ! empty( $stats['error'] ) ) {
$this->stats['error'] = $stats['error'];
}
return true;
}
);
}
/**
* Is this one whose text visitors see? Not if it, or an element around it, is excluded
* from translation, and not if it holds a script's own placeholders ({{ }}, {% %}, <% %>, ${ }).
*/
private function template_translatable( $template, $inner ) {
for ( $node = $template; $node && self::ELEMENT === $node->nodeType; $node = $node->parentNode ) {
if ( $this->is_excluded( $node ) ) {
return false;
}
$tag = Html::tag( $node );
if ( $node !== $template && 'template' !== $tag && isset( self::SKIP[ $tag ] ) ) {
return false;
}
}
return ! preg_match( Html::SCRIPT_TEMPLATE, (string) $inner );
}
/** libxml only: an eligible template's children are walked like any element's. */
private function walk_into_template( $template ) {
return ! $this->html->modern && $this->template_translatable( $template, Html::inner_html( $template ) );
}
/**
* Walk elements and register text / block segments.
*/
private function walk( $node ) {
foreach ( iterator_to_array( $node->childNodes ) as $child ) {
if ( count( $this->segments ) >= self::MAX_SEGMENTS ) {
return;
}
if ( self::TEXT === $child->nodeType ) {
$this->add_text_segment( $child );
continue;
}
if ( self::ELEMENT !== $child->nodeType || $this->is_excluded( $child ) ) {
continue;
}
$tag = Html::tag( $child );
if ( 'template' === $tag && $this->walk_into_template( $child ) ) {
$this->walk( $child );
continue;
}
if ( isset( self::SKIP[ $tag ] ) || isset( self::VOID_INLINE[ $tag ] ) ) {
continue;
}
if ( $this->inline_only( $child ) ) {
$root = $this->unwrap( $child );
if ( $this->has_direct_text( $root ) ) {
$this->add_block_segment( $root );
} else {
$this->walk( $root );
}
continue;
}
$this->walk( $child );
}
}
/**
* True when an element's content is only text and inline elements.
*/
private function inline_only( $el ) {
if ( $this->inline_memo->offsetExists( $el ) ) { // contains() is deprecated in PHP 8.5
return $this->inline_memo[ $el ];
}
$result = true;
foreach ( $el->childNodes as $child ) {
if ( self::TEXT === $child->nodeType || self::COMMENT === $child->nodeType ) {
continue;
}
if ( self::ELEMENT !== $child->nodeType ) {
$result = false;
break;
}
$tag = Html::tag( $child );
if ( $this->is_opaque( $child, $tag ) || isset( self::VOID_INLINE[ $tag ] ) ) {
continue;
}
if ( ! isset( self::INLINE[ $tag ] ) || ! $this->inline_only( $child ) ) {
$result = false;
break;
}
}
$this->inline_memo[ $el ] = $result;
return $result;
}
/**
* Inline-level element whose content must be kept verbatim.
*/
private function is_opaque( $el, $tag ) {
return ( isset( self::SKIP[ $tag ] ) && ! in_array( $tag, array( 'base', 'link' ), true ) ) || $this->is_excluded( $el );
}
private function has_direct_text( $el ) {
foreach ( $el->childNodes as $child ) {
if ( self::TEXT === $child->nodeType && self::has_letters( $child->nodeValue ) ) {
return true;
}
}
return false;
}
/**
* Descend through single-child wrappers (Home → ) so the
* segment is just "Home" and is shared with every other "Home" on the site.
*/
private function unwrap( $el ) {
while ( ! $this->has_direct_text( $el ) ) {
$only = null;
$n = 0;
foreach ( $el->childNodes as $child ) {
if ( self::COMMENT === $child->nodeType || ( self::TEXT === $child->nodeType && '' === trim( $child->nodeValue ) ) ) {
continue;
}
++$n;
$only = $child;
}
if ( 1 !== $n || self::ELEMENT !== $only->nodeType ) {
return $el;
}
$tag = Html::tag( $only );
if ( isset( self::VOID_INLINE[ $tag ] ) || $this->is_opaque( $only, $tag ) ) {
return $el;
}
$el = $only;
}
return $el;
}
private function add_text_segment( $text_node ) {
$value = $text_node->nodeValue;
if ( ! self::has_letters( $value ) || self::looks_technical( $value ) ) {
return;
}
preg_match( '/^[ \t\n\r\f]*/', $value, $lead );
preg_match( '/[ \t\n\r\f]*$/', $value, $trail );
$source = self::escape( self::collapse( $value ) );
$this->add_segment(
array(
'type' => 'text',
'node' => $text_node,
'lead' => $lead[0],
'trail' => $trail[0],
),
$source
);
}
private function add_block_segment( $el ) {
$map = array();
$n = 0;
$source = trim( $this->template( $el, $map, $n ) );
if ( '' === $source || ! self::has_letters( wp_strip_all_tags( $source ) ) ) {
return;
}
$this->add_segment(
array(
'type' => 'block',
'node' => $el,
'map' => $map,
),
$source
);
}
/**
* Serialize an inline-only element's children into a placeholder template.
*/
private function template( $el, array &$map, &$n ) {
$out = '';
foreach ( $el->childNodes as $child ) {
if ( self::TEXT === $child->nodeType ) {
$out .= self::escape( preg_replace( '/[ \t\n\r\f]+/', ' ', $child->nodeValue ) );
continue;
}
if ( self::ELEMENT !== $child->nodeType ) {
continue; // Comments are dropped from translated sentences.
}
$tag = Html::tag( $child );
$map[ ++$n ] = $child;
$id = $n;
if ( isset( self::VOID_INLINE[ $tag ] ) ) {
$out .= '<' . $tag . ' i="' . $id . '">';
} elseif ( $this->is_opaque( $child, $tag ) ) {
// Kept verbatim; a short neutral tag keeps the provider from translating it.
$out .= '';
} else {
$out .= '<' . $tag . ' i="' . $id . '">' . $this->template( $child, $map, $n ) . '' . $tag . '>';
}
}
return preg_replace( '/ {2,}/', ' ', $out );
}
/**
* Register translatable attributes for every element in the tree.
*/
private function collect_attributes( $node ) {
foreach ( $node->childNodes as $child ) {
if ( self::ELEMENT !== $child->nodeType || $this->is_excluded( $child ) ) {
continue;
}
$tag = Html::tag( $child );
if ( 'template' === $tag && $this->walk_into_template( $child ) ) {
$this->collect_attributes( $child );
continue;
}
if ( in_array( $tag, array( 'script', 'style', 'template', 'svg', 'math', 'code', 'pre', 'link', 'base' ), true ) ) {
continue;
}
if ( 'meta' === $tag ) {
$key = strtolower( (string) ( $child->getAttribute( 'name' ) ?: $child->getAttribute( 'property' ) ) );
if ( isset( self::META_CONTENT[ $key ] ) ) {
$this->add_attr_segment( $child, 'content' );
}
continue;
}
foreach ( self::TRANSLATABLE_ATTRS as $attr ) {
if ( $child->hasAttribute( $attr ) ) {
$this->add_attr_segment( $child, $attr );
}
}
if ( 'input' === $tag && in_array( strtolower( (string) $child->getAttribute( 'type' ) ), array( 'submit', 'button', 'reset' ), true ) && $child->hasAttribute( 'value' ) ) {
$this->add_attr_segment( $child, 'value' );
}
if ( $child->hasChildNodes() ) {
$this->collect_attributes( $child );
}
}
}
private function add_attr_segment( $el, $attr ) {
$value = trim( (string) $el->getAttribute( $attr ) );
if ( ! self::has_letters( $value ) || self::looks_technical( $value ) ) {
return;
}
$this->add_segment(
array(
'type' => 'attr',
'node' => $el,
'attr' => $attr,
),
self::escape( self::collapse( $value ) )
);
}
private function add_segment( array $segment, $source ) {
$segment['source'] = $source;
$segment['len'] = mb_strlen( $source );
$this->segments[] = $segment;
++$this->stats['segments'];
$this->stats['chars'] += $segment['len'];
}
/* ------------------------------------------------------------------ */
/* Resolution */
/* ------------------------------------------------------------------ */
/**
* Map each unique source to a translation (or null) using TM + cloud.
*
* @return array
*/
private function resolve( array $segments, $url ) {
$sources = array();
foreach ( $segments as $segment ) {
$sources[ $segment['source'] ] = Translation_Memory::hash( $segment['source'] );
}
$known = Translation_Memory::lookup( array_values( $sources ), $this->target );
$out = array();
$miss = array();
$oversized = array();
$private_hits = array();
foreach ( $sources as $source => $hash ) {
$row = $known[ $hash ] ?? null;
if ( $row && null !== $row['translation'] && '' !== $row['translation'] ) {
$out[ $source ] = $row['translation'];
if ( Privacy::STATUS === $row['status'] ) {
$private_hits[] = $hash;
}
} else {
$out[ $source ] = null;
if ( mb_strlen( $source ) > self::MAX_SEGMENT_CHARS ) {
$oversized[] = $source; // The cloud would refuse the whole batch: leave it as it is.
} else {
$miss[] = $source;
}
}
}
if ( $private_hits ) {
Translation_Memory::touch_private( $private_hits, $this->target, ! $this->private );
}
if ( $miss && $this->may_call_cloud() ) {
++$this->stats['cloud_calls'];
$result = Cloud::translate( $miss, $this->source, $this->target, $this->purpose, $this->private, $this->requester, $this->main );
$rows = array();
foreach ( $miss as $i => $source ) {
$translation = $result['translations'][ $i ] ?? null;
$out[ $source ] = $translation;
$rows[] = array(
'source' => $source,
'translation' => $translation,
);
}
Translation_Memory::store( $rows, $this->target, $url, $this->private );
if ( $result['error'] ) {
$this->note_cloud_error( $result['error'] );
}
if ( ! empty( $result['paced'] ) ) {
// The site's budget says not yet: this kind of requester waits; the misses stay pending.
$wait = max( 60, min( HOUR_IN_SECONDS, (int) $result['paced'] ) );
set_transient( $this->paced_transient(), 1, $wait );
}
}
return $out;
}
/** A segment that has no translation after this render: oversized, or still waiting for one. */
private function count_untranslated( array $segment ) {
if ( $segment['len'] > self::MAX_SEGMENT_CHARS ) {
++$this->stats['oversized'];
} else {
++$this->stats['pending'];
}
}
private function may_call_cloud() {
if ( ! $this->allow_new || false !== get_transient( self::PLAN_PAUSE . $this->purpose ) ) {
return false;
}
$conn = Settings::connection();
if ( empty( $conn['api_key'] ) || ! empty( $conn['api_key_invalid'] ) ) {
return false;
}
if ( false !== get_transient( 'langferry_pause_' . $this->target ) || false !== get_transient( 'langferry_quota_exceeded' ) ) {
return false;
}
if ( false !== get_transient( self::PACED_PEOPLE ) || ( $this->is_crawler() && false !== get_transient( $this->paced_transient() ) ) ) {
return false;
}
return ! Cloud::in_backoff();
}
private function note_cloud_error( \WP_Error $error ) {
$this->stats['error'] = $error->get_error_code();
$data = $error->get_error_data();
$status = (int) ( $data['status'] ?? 0 );
if ( 'langferry_not_in_plan' === $error->get_error_code() ) {
// The plan doesn't include this kind of text (e.g. emails): stop asking for a while,
// without pausing page translation.
set_transient( self::PLAN_PAUSE . $this->purpose, 1, HOUR_IN_SECONDS );
} elseif ( 402 === $status ) {
// The code tells the admin screen why: the allowance, the account's one free site, or a check still pending.
set_transient( 'langferry_quota_exceeded', $error->get_error_code(), HOUR_IN_SECONDS );
} elseif ( 403 === $status ) {
// Config drifted from the cloud (e.g. language removed in the dashboard).
set_transient( 'langferry_pause_' . $this->target, 1, 10 * MINUTE_IN_SECONDS );
wp_schedule_single_event( time(), 'langferry_sync' );
}
}
/* ------------------------------------------------------------------ */
/* Application */
/* ------------------------------------------------------------------ */
/**
* @return bool False when a block's placeholders didn't survive translation.
*/
private function apply( array $segment, $translation ) {
switch ( $segment['type'] ) {
case 'attr':
$segment['node']->setAttribute( $segment['attr'], self::to_text( $translation ) );
return true;
case 'text':
$segment['node']->nodeValue = $segment['lead'] . self::to_text( $translation ) . $segment['trail'];
return true;
case 'block':
return $this->restore_block( $segment['node'], $translation, $segment['map'] );
}
return false;
}
/**
* Replace a block's children with the translated template, re-attaching the
* original inline elements by their i="N" placeholder.
*/
private function restore_block( $el, $translation, array $map ) {
$nodes = $this->html->fragment_nodes( $translation );
if ( null === $nodes ) {
return false;
}
$seen = array();
if ( ! $this->check_placeholders( $nodes, $map, $seen ) || count( $seen ) !== count( $map ) ) {
return false;
}
$new = array();
foreach ( $nodes as $node ) {
foreach ( $this->rebuild( $node, $map ) as $built ) {
$new[] = $built;
}
}
while ( $el->firstChild ) {
$el->removeChild( $el->firstChild );
}
foreach ( $new as $node ) {
$el->appendChild( $node );
}
return true;
}
private function check_placeholders( array $nodes, array $map, array &$seen ) {
foreach ( $nodes as $node ) {
if ( self::ELEMENT !== $node->nodeType ) {
continue;
}
$i = $node->getAttribute( 'i' );
if ( '' !== $i && null !== $i ) {
$i = (int) $i;
if ( ! isset( $map[ $i ] ) || isset( $seen[ $i ] ) ) {
return false;
}
$seen[ $i ] = true;
}
if ( $node->hasChildNodes() && ! $this->check_placeholders( iterator_to_array( $node->childNodes ), $map, $seen ) ) {
return false;
}
}
return true;
}
/**
* @return array Nodes to insert.
*/
private function rebuild( $node, array $map ) {
if ( self::TEXT === $node->nodeType ) {
return array( $this->html->create_text( $node->nodeValue ) );
}
if ( self::ELEMENT !== $node->nodeType ) {
return array();
}
$i = $node->getAttribute( 'i' );
if ( '' === $i || null === $i ) {
// Element invented by the provider: keep its text, drop the tag.
$out = array();
foreach ( $node->childNodes as $child ) {
$out = array_merge( $out, $this->rebuild( $child, $map ) );
}
return $out;
}
$original = $map[ (int) $i ];
$tag = Html::tag( $original );
if ( isset( self::VOID_INLINE[ $tag ] ) || $this->is_opaque( $original, $tag ) ) {
return array( $original->cloneNode( true ) );
}
$clone = $original->cloneNode( false );
foreach ( $node->childNodes as $child ) {
foreach ( $this->rebuild( $child, $map ) as $built ) {
$clone->appendChild( $built );
}
}
return array( $clone );
}
/**
* Second round for blocks whose placeholders were mangled: translate their
* text nodes one by one.
*/
private function fallback( array $blocks, $url ) {
$this->segments = array();
foreach ( $blocks as $block ) {
++$this->stats['fallbacks'];
$this->collect_text_nodes( $block['node'] );
}
if ( empty( $this->segments ) ) {
return;
}
$translations = $this->resolve( $this->segments, $url );
foreach ( $this->segments as $segment ) {
$translation = $translations[ $segment['source'] ] ?? null;
if ( null === $translation ) {
$this->count_untranslated( $segment );
} elseif ( $this->apply( $segment, $translation ) ) {
$this->stats['translated_chars'] += $segment['len'];
}
}
}
private function collect_text_nodes( $el ) {
foreach ( $el->childNodes as $child ) {
if ( self::TEXT === $child->nodeType ) {
$this->add_text_segment( $child );
} elseif ( self::ELEMENT === $child->nodeType && ! $this->is_opaque( $child, Html::tag( $child ) ) ) {
$this->collect_text_nodes( $child );
}
}
}
/* ------------------------------------------------------------------ */
/* Helpers */
/* ------------------------------------------------------------------ */
public static function has_letters( $text ) {
return (bool) preg_match( '/\p{L}/u', (string) $text );
}
/**
* URLs, e-mail addresses, file names and code-like tokens are not prose.
*/
public static function looks_technical( $text ) {
$text = trim( (string) $text );
return (bool) preg_match( '~^(https?://|www\.|mailto:|tel:|/[\w/.-]*$|[\w.+-]+@[\w-]+\.[\w.-]+$|[\w-]+\.(?:jpe?g|png|gif|webp|svg|pdf|zip|php|js|css)$|\{\{.*\}\}$)~i', $text );
}
private static function collapse( $text ) {
return trim( preg_replace( '/[ \t\n\r\f]+/', ' ', (string) $text ) );
}
private static function escape( $text ) {
return htmlspecialchars( (string) $text, ENT_NOQUOTES | ENT_SUBSTITUTE, 'UTF-8' );
}
/**
* Provider output for text/attr segments is HTML-escaped text.
*/
private static function to_text( $translation ) {
return html_entity_decode( wp_strip_all_tags( (string) $translation ), ENT_QUOTES | ENT_HTML5, 'UTF-8' );
}
}