is_default() || ! self::should_buffer() ) { return; } ob_start( array( __CLASS__, 'process' ) ); } private static function should_buffer() { if ( is_admin() || wp_doing_ajax() || is_feed() || is_robots() || is_trackback() || is_embed() ) { return false; } if ( ( defined( 'REST_REQUEST' ) && REST_REQUEST ) || is_customize_preview() ) { return false; } if ( get_query_var( 'sitemap' ) || get_query_var( 'sitemap-stylesheet' ) ) { return false; } $router = Router::instance(); if ( $router->is_excluded_path( $router->request_path() ) ) { return false; } /** * Filters whether the current request should be translated. * * @param bool $translate */ return (bool) apply_filters( 'langferry_should_translate', true ); } /** * ob_start callback. Must never break the page: any failure returns the * original HTML. */ public static function process( $html ) { if ( ! is_string( $html ) || strlen( $html ) < 20 || ! self::is_html_response( $html ) ) { return $html; } try { $out = self::translate_page( $html, Router::instance()->current() ); return null === $out ? $html : $out; } catch ( \Throwable $e ) { if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { error_log( 'Langferry: translation failed: ' . $e->getMessage() . ' in ' . $e->getFile() . ':' . $e->getLine() ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions } return $html; } } private static function is_html_response( $html ) { foreach ( headers_list() as $header ) { if ( 0 === stripos( $header, 'content-type:' ) && false === stripos( $header, 'text/html' ) ) { return false; } } $code = http_response_code(); if ( $code && ( $code >= 300 && $code < 400 ) ) { return false; } return false !== stripos( $html, 'for_purpose( 'email' ); // sent by the shop, not requested by a visitor } else { $decision = Traffic::decision(); $translator->allow_new_text( $decision['allowed'], $decision['reason'] ); if ( Privacy::is_private_request() ) { $translator->mark_private(); } } $stats = $translator->translate( $doc, $router->request_path() ); // How complete the page is now, for the order and the listing (Pages). Public renders of the // page itself only: never an email, a signed-in visitor's page, a cart, checkout or account // page, or a response that isn't a 200 (ruling R38). The router has already turned a translated // address into its source. if ( 'email' !== $purpose && ! is_user_logged_in() && ! Privacy::is_private_request() && self::is_page_itself() ) { Pages::record( $router->current_url_in( $router->default_language() ), $lang, $stats, $decision['reason'] ); } if ( $rewrite_links ) { self::rewrite_links( $doc, $lang ); } Seo::apply_to_document( $doc, $lang, $stats ); $out = $doc->serialize(); // Safety net: a serialization that lost most of the page means the parser // choked on something — serve the original rather than a broken page. if ( strlen( $out ) < strlen( $html ) * 0.5 ) { return null; } self::$last_stats = $stats; if ( defined( 'WP_DEBUG' ) && WP_DEBUG && ! headers_sent() ) { header( sprintf( 'X-Langferry: lang=%s; segments=%d; translated=%d; cloud=%d; new=%s', $lang, $stats['segments'], $stats['translated'], $stats['cloud_calls'], $stats['new_text'] ) ); } return $out; } /** * Whether this response is the page itself: answered 200, or with no status at all (false: WP-CLI, * the plugin's own tests). A maintenance mode's 503 page and a 404 template render through the * theme too, and recorded they would count as the page (ruling R38, PRIO-PAGES-007). */ private static function is_page_itself() { /** * Filters the HTTP status Langferry reads for the response it is translating, to decide * whether to record how complete the page is: only a 200, or no status (false, outside a web * request), records. * * @param int|false $status http_response_code(). */ $status = apply_filters( 'langferry_render_status', http_response_code() ); return ! is_int( $status ) || 200 === $status; } /** * Point internal links at the same language. */ public static function rewrite_links( Html $doc, $lang ) { $router = Router::instance(); $targets = array( 'a' => 'href', 'area' => 'href', 'form' => 'action', 'link' => 'href', 'meta' => 'content', 'button' => 'formaction', ); $elements = array(); self::collect_link_elements( $doc->root(), $targets, $elements ); if ( Settings::effective()['seo']['translate_slugs'] ) { $paths = array(); foreach ( $elements as $item ) { $path = wp_parse_url( (string) $item[0]->getAttribute( $item[1] ), PHP_URL_PATH ); if ( $path ) { $paths[] = $path; } } Slugs::prefetch( $paths, $lang ); } foreach ( $elements as $item ) { list( $el, $attr ) = $item; $value = (string) $el->getAttribute( $attr ); $new = $router->translate_url( $value, $lang ); if ( $new !== $value ) { $el->setAttribute( $attr, $new ); } } // Links inside