<?php
/**
 * Redirects helper class.
 *
 * @link          https://aleksandrsamokhin.com
 * @since         1.0.0
 *
 * @author        Aleksandr Samokhin
 * @package       Redicl\Core
 *
 * @copyright (c) 2025, Aleksandr Samokhin
 */

namespace Redicl\Core;

// Abort if called directly.
defined( 'WPINC' ) || die;

/**
 * Helper class for managing redirects option.
 */
class Redirects_Helper {

	/**
	 * Get the option name for Yoast SEO Premium redirects.
	 *
	 * @return string The option name.
	 * @since 1.0.0
	 */
	public static function get_option_name() {
		return 'wpseo-premium-redirects-export-plain';
	}

	/**
	 * Get all redirects from the option.
	 *
	 * @param array $default_value Default value if option doesn't exist.
	 * @return array Array of redirects.
	 * @since 1.0.0
	 */
	public static function get_redirects( $default_value = array() ) {
		return get_option( self::get_option_name(), $default_value );
	}

	/**
	 * Update redirects option.
	 *
	 * @param array $redirects Array of redirects to save.
	 * @return bool True if option value has changed, false if not or if update failed.
	 * @since 1.0.0
	 */
	public static function update_redirects( $redirects ) {
		return update_option( self::get_option_name(), $redirects );
	}

	/**
	 * Get homepage redirects from all redirects.
	 *
	 * @param array $redirects All redirects from Yoast.
	 * @param int   $limit Optional. Limit the number of redirects to return. 0 means no limit.
	 * @return array Homepage redirects with full redirect data, keyed by source URL.
	 * @since 1.0.0
	 */
	public static function get_homepage_redirects( $redirects, $limit = 0 ) {
		$homepage_redirects = array();

		// Common homepage patterns.
		$homepage_patterns = array(
			'/',
			'',
			home_url(),
			home_url( '/' ),
			trailingslashit( home_url() ),
		);

		foreach ( $redirects as $old_url => $redirect_data ) {
			$target = isset( $redirect_data['url'] ) ? $redirect_data['url'] : '';
			$type   = $redirect_data['type'] ?? '';

			// Check if redirects to homepage and is type 301 or 302.
			if ( ( 301 === $type || 302 === $type ) && ( in_array( $target, $homepage_patterns, true ) || wp_parse_url( home_url(), PHP_URL_PATH ) === $target ) ) {
				$homepage_redirects[ $old_url ] = $redirect_data;

				if ( $limit > 0 && count( $homepage_redirects ) >= $limit ) {
					break;
				}
			}
		}

		return $homepage_redirects;
	}

	/**
	 * Get the option name for Yoast SEO Premium base redirects.
	 *
	 * This is the numerically-indexed format used internally by Yoast.
	 *
	 * @return string The option name.
	 * @since 1.0.0
	 */
	public static function get_base_option_name() {
		return 'wpseo-premium-redirects-base';
	}

	/**
	 * Get all base redirects from the option.
	 *
	 * Returns numerically-indexed array with entries containing:
	 * - origin: source path
	 * - url: target path
	 * - type: redirect type (301, 302, etc.)
	 * - format: format type ('plain')
	 *
	 * @param array $default_value Default value if option doesn't exist.
	 * @return array Array of redirects.
	 * @since 1.0.0
	 */
	public static function get_base_redirects( $default_value = array() ) {
		return get_option( self::get_base_option_name(), $default_value );
	}

	/**
	 * Update base redirects option.
	 *
	 * @param array $redirects Array of redirects to save.
	 * @return bool True if option value has changed, false if not or if update failed.
	 * @since 1.0.0
	 */
	public static function update_base_redirects( $redirects ) {
		return update_option( self::get_base_option_name(), $redirects );
	}
}
