# wplingua/1.0.4/inc/html-updater.php

Translate and Go multilingual – Automatic AI translation – wpLingua, version 1.0.4. 246 lines.

- Page: https://pluginprobe.com/plugins/wplingua/1.0.4/code/inc/html-updater.php
- Raw: https://pluginprobe.com/plugins/wplingua/1.0.4/raw/inc/html-updater.php
- Modified: 2024-02-04T17:31:40+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/wplingua/1.0.4/code/inc/html-updater.php#L10-L20`.

```php
<?php

// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
	die;
}


/**
 * Print HTML of alternate link with hreflang for each available languages
 *
 * @return void
 */
function wplng_link_alternate_hreflang() {

	if ( ! wplng_url_is_translatable() ) {
		return;
	}

	$html             = PHP_EOL . PHP_EOL;
	$language_website = wplng_get_language_website();
	$languages_target = wplng_get_languages_target();

	if ( empty( $language_website ) || empty( $languages_target ) ) {
		return;
	}

	// Create the starting comment
	$html .= '<!-- This site is made multilingual with the wpLingua plugin -->';
	$html .= PHP_EOL;

	// Create meta generator
	$html .= '<meta ';
	$html .= 'name="generator" ';
	$html .= 'content="wpLingua ' . esc_attr( WPLNG_PLUGIN_VERSION ) . '" />';
	$html .= PHP_EOL;

	// Create alternate link for website language
	$html .= '<link ';
	$html .= 'rel="alternate" ';
	$html .= 'hreflang="' . esc_attr( $language_website['id'] ) . '" ';
	$html .= 'href="' . esc_url( wplng_get_url_original() ) . '" />';
	$html .= PHP_EOL;

	// Create alternate link for each target languages
	foreach ( $languages_target as $language_target ) {

		$url = wplng_get_url_current_for_language( $language_target['id'] );

		$html .= '<link ';
		$html .= 'rel="alternate" ';
		$html .= 'hreflang="' . esc_attr( $language_target['id'] ) . '" ';
		$html .= 'href="' . esc_url( $url ) . '" />';
		$html .= PHP_EOL;
	}

	// Create the ending comment
	$html .= '<!-- / wpLingua plugin. -->' . PHP_EOL . PHP_EOL;

	echo $html;
}


/**
 * Get wpLingua excluded selectors
 *
 * @return array
 */
function wplng_get_selector_exclude() {

	$selector_exclude = array();

	// Get, check and sanitize excluded selector as string
	$option = get_option( 'wplng_excluded_selectors' );
	if ( empty( $option ) || ! is_string( $option ) ) {
		wplng_data_excluded_selector_default();
	}
	$option = sanitize_textarea_field( $option );

	// Get exclude selector as array
	$option = explode( PHP_EOL, $option );

	// Sanitize each selectors
	foreach ( $option as $selector ) {
		$selector = esc_attr( trim( $selector ) );
		if ( ! empty( $selector ) ) {
			$selector_exclude[] = $selector;
		}
	}

	// Add default selectors
	$selector_exclude = array_merge(
		wplng_data_excluded_selector_default(), // Make HTML parsed smaller
		$selector_exclude
	);

	// Remove duplicate
	$selector_exclude = array_unique( $selector_exclude );

	// Apply wplng_selector_exclude filters
	$selector_exclude = apply_filters(
		'wplng_selector_exclude',
		$selector_exclude
	);

	return $selector_exclude;
}


/**
 * Replace all excluded elements by empty tags with wplng-tag-exclude attribute
 *
 * @param string $html
 * @param array $excluded_elements
 * @return string HTML
 */
function wplng_html_set_exclude_tag( $html, &$excluded_elements ) {

	$selector_exclude = wplng_get_selector_exclude();

	if ( empty( $selector_exclude ) ) {
		return $html;
	}

	$dom = wplng_sdh_str_get_html( $html );

	if ( false === $dom ) {
		return $html;
	}

	foreach ( $selector_exclude as $selector ) {
		foreach ( $dom->find( $selector ) as $element ) {
			$excluded_elements[] = $element->outertext;
			$attr                = count( $excluded_elements ) - 1;
			$element->outertext  = '<div wplng-tag-exclude="' . esc_attr( $attr ) . '"></div>';
		}
	}

	$dom->save();

	return (string) wplng_sdh_str_get_html( $dom );
}


/**
 * Replace all tags with wplng-tag-exclude attribute by value in $excluded_elements
 *
 * @param string $html
 * @param array $excluded_elements
 * @return string
 */
function wplng_html_replace_exclude_tag( $html, $excluded_elements ) {

	if ( empty( $excluded_elements ) ) {
		return $html;
	}

	$dom = wplng_sdh_str_get_html( $html );

	if ( false === $dom ) {
		return $html;
	}

	foreach ( $dom->find( '[wplng-tag-exclude]' ) as $element ) {

		if ( isset( $element->attr['wplng-tag-exclude'] ) ) {

			$exclude_index = (int) $element->attr['wplng-tag-exclude'];

			if ( isset( $excluded_elements[ $exclude_index ] ) ) {
				$element->outertext = $excluded_elements[ $exclude_index ];
			}
		}
	}

	$dom->save();

	return (string) wplng_sdh_str_get_html( $dom );
}


/**
 * wpLingua init function
 *
 * @return void
 */
function wplng_ob_start() {

	if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
		ob_start( 'wplng_ob_callback_ajax' );
		return;
	}

	if ( wplng_get_language_website_id() === wplng_get_language_current_id() ) {
		return;
	}

	global $wplng_request_uri;
	$current_path = $wplng_request_uri;

	if ( ! is_string( $current_path ) ) {
		return;
	}

	$origin_path = '/' . substr( $current_path, 4, strlen( $current_path ) - 1 );
	$origin_path = sanitize_url( $origin_path );

	if ( ! wplng_url_is_translatable( $origin_path ) ) {
		wp_redirect( $origin_path );
		exit;
	}

	$_SERVER['REQUEST_URI'] = $origin_path;

	if ( current_user_can( 'edit_posts' ) ) {
		if ( isset( $_GET['wplingua-editor'] ) ) {

			ob_start( 'wplng_ob_callback_editor' );

		} elseif ( isset( $_GET['wplingua-list'] ) ) {

			add_filter(
				'body_class', function( $classes ) {
					return array_merge( $classes, array( 'wplingua-list' ) );
				}
			);

			ob_start( 'wplng_ob_callback_list' );

		} else {

			add_filter(
				'body_class', function( $classes ) {
					return array_merge( $classes, array( 'wplingua-editor' ) );
				}
			);

			ob_start( 'wplng_ob_callback_translate' );

		}
	} else {
		ob_start( 'wplng_ob_callback_translate' );
	}

}

```
