<?php
/**
 * Redirects.
 *
 * Thin, guarded bridge to Yoast Premium's redirect manager. If Yoast Premium
 * (with the redirects module) isn't active, every method is a safe no-op and
 * the caller is told no redirect was created — we never pretend.
 *
 * The plugin does NOT maintain its own redirect store; redirects live in
 * Yoast so there's one source of truth, exactly as agreed.
 *
 * @package Platform\Helm\Modules\Content_Auditor
 */

declare( strict_types = 1 );

namespace Platform\Helm\Modules\Content_Auditor;

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

final class Redirects {

	public static function available(): bool {
		return class_exists( '\WPSEO_Redirect_Manager' ) && class_exists( '\WPSEO_Redirect' );
	}

	/**
	 * Create a 301 from $origin (a path relative to site root) to $target.
	 *
	 * @return bool True if a redirect was created.
	 */
	public static function create_301( string $origin, string $target ): bool {
		if ( ! self::available() ) {
			return false;
		}
		$origin = ltrim( trim( $origin ), '/' );
		$target = trim( $target );
		if ( '' === $origin || '' === $target ) {
			return false;
		}
		try {
			$manager  = new \WPSEO_Redirect_Manager();
			$redirect = new \WPSEO_Redirect( $origin, $target, 301, \WPSEO_Redirect::FORMAT_PLAIN );
			$manager->create_redirect( $redirect );
			return true;
		} catch ( \Throwable $e ) {
			return false;
		}
	}
}
