Skip to content
wiki.fftac.org

Seometadata - Source Excerpt 03

Back to Seometadata

Summary

This source excerpt preserves a bounded section of Antichrist.net/wp-content/plugins/uaix-locale-router/src/SeoMetadata.php so readers can inspect the evidence without opening the full source file.

**Source path:** Antichrist.net/wp-content/plugins/uaix-locale-router/src/SeoMetadata.php

/**
	 * Alternate Open Graph locale values.
	 *
	 * @param string $current_locale Current locale tag.
	 * @return array
	 */
	private static function alternate_open_graph_locales( $current_locale ) {
		$alternates = array();

		foreach ( LocaleRepository::get_enabled_locales() as $locale ) {
			if ( $locale['tag'] === $current_locale ) {
				continue;
			}

			$alternates[] = self::open_graph_locale( $locale['tag'] );
		}

		return $alternates;
	}

	/**
	 * Post date in ISO 8601 format.
	 *
	 * @param int    $post_id Post ID.
	 * @param string $field Post date field.
	 * @return string
	 */
	private static function post_date_iso8601( $post_id, $field ) {
		if ( ! function_exists( 'get_post_field' ) ) {
			return '';
		}

		$value = (string) get_post_field( $field, $post_id );

		if ( '' === $value || '0000-00-00 00:00:00' === $value ) {
			return '';
		}

		if ( function_exists( 'mysql2date' ) ) {
			return (string) mysql2date( 'c', $value, false );
		}

		$timestamp = strtotime( $value . ' UTC' );

		return false !== $timestamp ? gmdate( 'c', $timestamp ) : '';
	}

	/**
	 * Determine whether metadata should not run for the request.
	 *
	 * @return bool
	 */
	private static function should_bypass() {
		$settings = Plugin::settings();

		if ( empty( $settings['enabled'] ) || empty( $settings['seo_metadata_enabled'] ) ) {
			return true;
		}

		if ( is_admin() || wp_doing_ajax() || wp_doing_cron() ) {
			return true;
		}

		if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
			return true;
		}

		if ( function_exists( 'is_feed' ) && is_feed() ) {
			return true;
		}

		return Router::is_excluded_path( Router::get_original_request_path() );
	}

	/**
	 * Render a name/content meta tag.
	 *
	 * @param string $name Meta name.
	 * @param string $content Meta content.
	 * @return void
	 */
	private static function render_named_meta( $name, $content ) {
		if ( '' === self::clean_text( $content ) ) {
			return;
		}

		echo '<meta name="' . esc_attr( $name ) . '" content="' . esc_attr( $content ) . "\" />\n";
	}

	/**
	 * Render a property/content meta tag.
	 *
	 * @param string $property Meta property.
	 * @param string $content Meta content.
	 * @return void
	 */
	private static function render_property_meta( $property, $content ) {
		if ( '' === self::clean_text( $content ) ) {
			return;
		}

		echo '<meta property="' . esc_attr( $property ) . '" content="' . esc_attr( $content ) . "\" />\n";
	}

	/**
	 * Detect unresolved SEO dictionary keys before they leak into metadata.
	 *
	 * @param mixed $value Value.
	 * @return bool
	 */
	private static function is_unresolved_seo_key( $value ) {
		return is_string( $value ) && 1 === preg_match( '/^seo\.(?:home|site|post\.\d+|page\.\d+|path\.[a-z0-9.]+|archive\.[a-z0-9.]+)\.(?:title|description)$/', trim( $value ) );
	}

	/**
	 * Clean text for metadata output.
	 *
	 * @param mixed $value Value.
	 * @return string
	 */
	private static function clean_text( $value ) {
		$value = html_entity_decode( strip_tags( (string) $value ), ENT_QUOTES, 'UTF-8' );
		$value = preg_replace( '/\s+/', ' ', $value );

		return trim( (string) $value );
	}
}