Skip to content
wiki.fftac.org

Htaccessmanager - Source Excerpt 02

Back to Htaccessmanager

Summary

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

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

$instructions[ $line ] = '# ' . $text;
		}

		return apply_filters( 'insert_with_markers_inline_instructions', $instructions, $marker_name );
	}

	/**
	 * Return the full marker payload that should exist inside .htaccess.
	 *
	 * @return array
	 */
	private static function htaccess_marker_insert_lines() {
		return array_merge(
			self::htaccess_marker_instruction_lines(),
			self::htaccess_marker_body_lines()
		);
	}

	/**
	 * Return the full marker payload that should exist inside the WordPress marker.
	 *
	 * @return array
	 */
	private static function wordpress_htaccess_marker_insert_lines() {
		return array_merge(
			self::htaccess_marker_instruction_lines_for( self::wordpress_htaccess_marker_name() ),
			self::wordpress_htaccess_marker_body_lines()
		);
	}

	/**
	 * Return the exact manual snippet for the site-root .htaccess rewrite blocks.
	 *
	 * @return string
	 */
	public static function htaccess_manual_block() {
		return implode(
			"\n",
			array_merge(
				array( '# BEGIN ' . self::htaccess_marker_name() ),
				self::htaccess_marker_insert_lines(),
				array(
					'# END ' . self::htaccess_marker_name(),
					'',
					'# BEGIN ' . self::wordpress_htaccess_marker_name(),
				),
				self::wordpress_htaccess_marker_insert_lines(),
				array( '# END ' . self::wordpress_htaccess_marker_name() )
			)
		);
	}

	/**
	 * Return the rewrite base for the current site path.
	 *
	 * @return string
	 */
	private static function htaccess_rewrite_base() {
		$path = wp_parse_url( (string) get_option( 'home' ), PHP_URL_PATH );
		$path = is_string( $path ) ? trim( $path ) : '';

		if ( '' === $path || '/' === $path ) {
			return '/';
		}

		return trailingslashit( '/' . trim( $path, '/' ) );
	}

	/**
	 * Return the index target path for locale-prefixed rewrite handoff.
	 *
	 * @return string
	 */
	private static function htaccess_index_target() {
		$rewrite_base = self::htaccess_rewrite_base();

		if ( '/' === $rewrite_base ) {
			return '/index.php';
		}

		return untrailingslashit( $rewrite_base ) . '/index.php';
	}

	/**
	 * Return an Apache-safe alternation pattern of enabled locale URL tags.
	 *
	 * @return string
	 */
	private static function htaccess_enabled_locale_pattern() {
		$locales  = LocaleRepository::get_enabled_locales();
		$patterns = array();

		foreach ( $locales as $locale ) {
			$url_tag = isset( $locale['urlTag'] ) ? LocaleRepository::normalize_url_tag( $locale['urlTag'] ) : '';

			if ( '' === $url_tag ) {
				continue;
			}

			$patterns[] = preg_quote( $url_tag, '/' );
		}

		if ( empty( $patterns ) ) {
			$patterns[] = preg_quote( LocaleRepository::get_default_url_tag(), '/' );
		}

		return implode( '|', array_unique( $patterns ) );
	}

	/**
	 * Insert or refresh the rewrite marker blocks the localized site depends on.
	 *
	 * @return array
	 */
	public static function repair_htaccess_block() {
		$diagnostics = self::htaccess_diagnostics();

		if ( ! $diagnostics['supports_htaccess'] ) {
			return array(
				'success'     => false,
				'message'     => __( 'This server does not use the Apache/LiteSpeed .htaccess flow that UAIX Locale Router can repair automatically.', 'uaix-locale-router' ),
				'diagnostics' => $diagnostics,
			);
		}

		if ( ! $diagnostics['auto_repair_available'] ) {
			return array(
				'success'     => false,
				'message'     => $diagnostics['manual_reason'],
				'diagnostics' => $diagnostics,
			);
		}

		if ( ! function_exists( 'insert_with_markers' ) ) {
			require_once ABSPATH . 'wp-admin/includes/misc.php';
		}

		$wrote = insert_with_markers(
			$diagnostics['path'],
			self::htaccess_marker_name(),
			self::htaccess_marker_body_lines()
		);

		if ( ! $wrote ) {
			return array(
				'success'     => false,
				'message'     => __( 'WordPress could not update the site-root .htaccess file automatically.', 'uaix-locale-router' ),
				'diagnostics' => self::htaccess_diagnostics(),
			);
		}

		if ( empty( $diagnostics['wordpress_marker_current'] ) ) {
			$wrote = insert_with_markers(
				$diagnostics['path'],
				self::wordpress_htaccess_marker_name(),
				self::wordpress_htaccess_marker_body_lines()
			);

			if ( ! $wrote ) {
				return array(
					'success'     => false,
					'message'     => __( 'WordPress could not refresh the site-root WordPress rewrite block automatically.', 'uaix-locale-router' ),
					'diagnostics' => self::htaccess_diagnostics(),
				);
			}
		}

		Plugin::clear_localized_home_diagnostics();

		return array(
			'success'     => true,
			'message'     => __( 'The site-root .htaccess rewrite blocks were updated successfully.', 'uaix-locale-router' ),
			'diagnostics' => self::htaccess_diagnostics(),
		);
	}
}