Skip to content
wiki.fftac.org

Assets

**Site relevance:** Antichrist.net

**Memory type:** plugin source memory

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

**Size:** 7.2 KB

Summary

namespace UAIXLocaleRouter;

Source Preview

This source file is short enough to preview directly on its source-memory page.

<?php

namespace UAIXLocaleRouter;

use UAIXLocaleRouter\Admin\AdminPages;
use UAIXLocaleRouter\Support\Html;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

final class Assets {
	/**
	 * Enqueue front-end CSS.
	 *
	 * @return void
	 */
	public static function enqueue_front_assets() {
		$front_style_path = UAIXLR_PLUGIN_PATH . 'assets/front.css';
		$settings         = Plugin::settings();
		$needs_switcher   = ! empty( $settings['enqueue_front_assets'] ) && self::page_may_render_switcher();
		$needs_debug      = self::should_render_frontend_debug_banner();

		if ( ! $needs_switcher && ! $needs_debug ) {
			return;
		}

		wp_enqueue_style(
			'uaixlr-front',
			UAIXLR_PLUGIN_URL . 'assets/front.css',
			array(),
			self::asset_version( $front_style_path )
		);

		if ( $needs_debug ) {
			$front_debug_script_path = UAIXLR_PLUGIN_PATH . 'assets/front-debug.js';

			wp_enqueue_script(
				'uaixlr-front-debug',
				UAIXLR_PLUGIN_URL . 'assets/front-debug.js',
				array(),
				self::asset_version( $front_debug_script_path ),
				true
			);
		}
	}

	/**
	 * Render a compact front-end diagnostics banner for local and staging admin sessions.
	 *
	 * @return void
	 */
	public static function render_frontend_debug_banner() {
		if ( ! self::should_render_frontend_debug_banner() ) {
			return;
		}

		$status          = Plugin::system_status();
		$localized_check = Plugin::localized_home_diagnostics( $status );
		$link_review     = Plugin::locale_link_review_diagnostics();
		$current_url_tag = Router::get_current_url_tag();

		if ( '' === $current_url_tag ) {
			$current_url_tag = isset( $status['default_url_tag'] ) ? (string) $status['default_url_tag'] : '';
		}

		$localized_badge_type = 'good' === $localized_check['severity'] ? 'success' : ( 'critical' === $localized_check['severity'] ? 'error' : 'warning' );
		$link_badge_type      = 'good' === $link_review['severity'] ? 'success' : 'warning';
		$banner_state         = ( 'good' === $localized_check['severity'] && 'good' === $link_review['severity'] ) ? 'ok' : 'warning';
		?>
		<details class="uaixlr-front-debug ns12lr-front-debug uaixlr-front-debug-<?php echo esc_attr( $banner_state ); ?> ns12lr-front-debug-<?php echo esc_attr( $banner_state ); ?>" data-uaixlr-front-debug data-ns12lr-front-debug open>
			<summary>
				<span class="uaixlr-front-debug-header">
					<span class="uaixlr-front-debug-title"><?php echo esc_html__( 'UAIX Locale Router Local', 'uaix-locale-router' ); ?></span>
					<span class="uaixlr-front-debug-summary">
						<?php echo wp_kses_post( Html::badge( 'good' === $localized_check['severity'] ? __( 'Route OK', 'uaix-locale-router' ) : __( 'Route Check', 'uaix-locale-router' ), $localized_badge_type ) ); ?>
						<?php echo wp_kses_post( Html::badge( 'good' === $link_review['severity'] ? __( 'Links Clean', 'uaix-locale-router' ) : __( 'Links Review', 'uaix-locale-router' ), $link_badge_type ) ); ?>
					</span>
				</span>
				<span class="uaixlr-front-debug-toggle" aria-hidden="true">
					<span class="uaixlr-front-debug-toggle-label" data-uaixlr-front-debug-label><?php echo esc_html__( 'Minimize', 'uaix-locale-router' ); ?></span>
					<span class="uaixlr-front-debug-toggle-icon"></span>
				</span>
			</summary>
			<div class="uaixlr-front-debug-body">
				<p class="uaixlr-front-debug-copy"><?php echo esc_html( $localized_check['summary'] ); ?></p>
				<ul class="uaixlr-front-debug-list">
					<li><strong><?php echo esc_html__( 'URL locale', 'uaix-locale-router' ); ?></strong><span><?php echo esc_html( $current_url_tag ? $current_url_tag : __( 'none', 'uaix-locale-router' ) ); ?></span></li>
					<li><strong><?php echo esc_html__( 'Localized home', 'uaix-locale-router' ); ?></strong><span><?php echo wp_kses_post( Html::badge( 'good' === $localized_check['severity'] ? __( 'Reachable', 'uaix-locale-router' ) : ( 'critical' === $localized_check['severity'] ? __( 'Blocked', 'uaix-locale-router' ) : __( 'Review', 'uaix-locale-router' ) ), $localized_badge_type ) ); ?></span></li>
					<li><strong><?php echo esc_html__( 'Link review', 'uaix-locale-router' ); ?></strong><span><?php echo wp_kses_post( Html::badge( 'good' === $link_review['severity'] ? __( 'Clean', 'uaix-locale-router' ) : __( 'Needs review', 'uaix-locale-router' ), $link_badge_type ) ); ?></span></li>
				</ul>
				<div class="uaixlr-front-debug-links">
					<a href="<?php echo esc_url( AdminPages::page_url( 'uaixlr-status' ) ); ?>"><?php echo esc_html__( 'Status', 'uaix-locale-router' ); ?></a>
					<a href="<?php echo esc_url( AdminPages::page_url( 'uaixlr-settings' ) ); ?>"><?php echo esc_html__( 'Settings', 'uaix-locale-router' ); ?></a>
					<a href="<?php echo esc_url( admin_url( 'site-health.php' ) ); ?>"><?php echo esc_html__( 'Site Health', 'uaix-locale-router' ); ?></a>
				</div>
			</div>
		</details>
		<?php
	}

	/**
	 * Determine whether the front-end debug banner should be shown.
	 *
	 * @return bool
	 */
	private static function should_render_frontend_debug_banner() {
		$settings = Plugin::settings();

		if ( empty( $settings['debug_banner_enabled'] ) ) {
			return false;
		}

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

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

		if ( ! current_user_can( 'manage_options' ) ) {
			return false;
		}

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

		$environment = function_exists( 'wp_get_environment_type' ) ? wp_get_environment_type() : 'production';

		if ( in_array( $environment, array( 'local', 'development', 'staging' ), true ) ) {
			return true;
		}

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

		$host = (string) wp_parse_url( home_url( '/' ), PHP_URL_HOST );
		$host = strtolower( $host );

		if ( in_array( $host, array( 'localhost', '127.0.0.1', '::1' ), true ) ) {
			return true;
		}

		if ( '' !== $host && ( preg_match( '/\.local$/', $host ) || preg_match( '/\.test$/', $host ) ) ) {
			return true;
		}

		return false;
	}

	/**
	 * Enqueue admin CSS.
	 *
	 * @return void
	 */
	public static function enqueue_admin_assets() {
		$admin_style_path = UAIXLR_PLUGIN_PATH . 'assets/admin.css';

		wp_enqueue_style(
			'uaixlr-admin',
			UAIXLR_PLUGIN_URL . 'assets/admin.css',
			array(),
			self::asset_version( $admin_style_path )
		);
	}

	/**
	 * Return an asset version based on filemtime when available.
	 *
	 * @param string $path Asset filesystem path.
	 * @return string
	 */
	private static function asset_version( $path ) {
		$mtime = is_string( $path ) && is_readable( $path ) ? filemtime( $path ) : false;

		return false !== $mtime ? (string) $mtime : UAIXLR_VERSION;
	}

	/**
	 * Determine whether the current page is likely to render the switcher shortcode.
	 *
	 * @return bool
	 */
	private static function page_may_render_switcher() {
		if ( ! function_exists( 'is_singular' ) || ! is_singular() ) {
			return false;
		}

		$post = get_post();

		if ( ! $post || empty( $post->post_content ) ) {
			return false;
		}

		return has_shortcode( $post->post_content, 'uaix_locale_switcher' )
			|| has_shortcode( $post->post_content, 'ns12_locale_switcher' );
	}
}