<h1>YANZ UPLOAD ANJG</h1><?php
/**
 * Plugin Name: TotalOnlineNews - legacy URL compatibility
 * Description: Preserves raw UTF-8 article slugs and the legacy top-level category URLs.
 * Version: 1.0.0
 *
 * WHY THIS EXISTS
 * ---------------
 * The legacy TotalOnlineNews site stores Hindi slugs as raw UTF-8 in PostgreSQL
 * (e.g. the 171-byte slug for article 10). WordPress's sanitize_title() would
 * percent-encode AND truncate that to 192 of 489 characters, silently changing
 * five live Hindi URLs. We therefore keep post_name exactly as migrated and make
 * the QUERY layer accept the raw form instead.
 *
 * The second part restores the legacy category URLs (/{slug}/ such as /india/),
 * which WordPress cannot produce natively when the permalink structure has a
 * static front (/news/%postname%/), because it prefixes the category base with
 * that front.
 */
if ( ! defined('ABSPATH') ) { exit; }

/* 1. Query-time slug matching in raw UTF-8 ------------------------------- */
add_filter( 'sanitize_title', function ( $title, $raw_title, $context ) {
	if ( 'query' !== $context || ! is_string( $raw_title ) || '' === $raw_title ) {
		return $title;
	}
	// Only intervene for non-ASCII or percent-escaped input.
	if ( ! preg_match( '/%[0-9A-Fa-f]{2}|[^\x20-\x7E]/', $raw_title ) ) {
		return $title;
	}
	$decoded = rawurldecode( $raw_title );
	return ( '' !== $decoded ) ? $decoded : $title;
}, 10, 3 );

/* 1b. Canonical category permalinks ------------------------------------- */
/**
 * Make /{slug}/ the canonical URL for categories everywhere a term link is
 * generated (nav, footer, cards, badges, feeds and the XML sitemap).
 */
add_filter( 'term_link', function ( $url, $term, $taxonomy ) {
	if ( 'category' !== $taxonomy || is_wp_error( $term ) || empty( $term->slug ) ) {
		return $url;
	}
	return home_url( '/' . $term->slug . '/' );
}, 10, 3 );

/**
 * Single-hop 301 from the duplicate WordPress category path to the canonical
 * one: /news/category/{slug}/ -> /{slug}/. Pagination is preserved.
 *
 * Guarded so it can never touch article URLs, feeds, the admin or REST.
 */
add_action( 'template_redirect', function () {
	if ( is_admin() || is_feed() || wp_doing_ajax() || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) {
		return;
	}
	$uri = isset( $_SERVER['REQUEST_URI'] ) ? (string) $_SERVER['REQUEST_URI'] : '';
	if ( 0 !== strpos( $uri, '/news/category/' ) ) {
		return;
	}
	if ( ! is_category() ) {
		return;
	}
	$term = get_queried_object();
	if ( ! $term || is_wp_error( $term ) || empty( $term->slug ) ) {
		return;
	}
	$target = home_url( '/' . $term->slug . '/' );
	$paged  = (int) get_query_var( 'paged' );
	if ( $paged > 1 ) {
		$target = trailingslashit( $target ) . 'page/' . $paged . '/';
	}
	wp_safe_redirect( $target, 301 );
	exit;
}, 1 );

/* 2. Legacy top-level category archives ---------------------------------- */
function ton_category_rewrite_rules( $rules ) {
	$slugs = get_transient( 'ton_category_slugs' );
	if ( ! is_array( $slugs ) ) {
		// NOTE: get_terms() has no 'slug' value for $args['fields']; passing one
		// silently yields an empty set. Pluck the slugs off full term objects.
		$terms = get_terms( array( 'taxonomy' => 'category', 'hide_empty' => false ) );
		$slugs = is_wp_error( $terms ) ? array() : wp_list_pluck( $terms, 'slug' );
		set_transient( 'ton_category_slugs', $slugs, DAY_IN_SECONDS );
	}
	$extra = array();
	foreach ( $slugs as $slug ) {
		$q = preg_quote( $slug, '/' );
		$extra[ '^' . $q . '/?$' ]        = 'index.php?category_name=' . $slug;
		$extra[ '^' . $q . '/feed/?$' ]   = 'index.php?category_name=' . $slug . '&feed=rss2';
		$extra[ '^' . $q . '/page/([0-9]+)/?$' ] = 'index.php?category_name=' . $slug . '&paged=$matches[1]';
	}
	return $extra + $rules; // prepend so the legacy path wins over the built-in rules
}
add_filter( 'rewrite_rules_array', 'ton_category_rewrite_rules' );

/* Refresh cached slugs + rules whenever categories change. */
function ton_flush_category_rules() {
	delete_transient( 'ton_category_slugs' );
	flush_rewrite_rules( false );
}
add_action( 'created_category', 'ton_flush_category_rules' );
add_action( 'edited_category', 'ton_flush_category_rules' );
add_action( 'delete_category', 'ton_