<?php

namespace CarDealerPress\includes\helpers;

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

class helpers {
	public static function array_dig( $input, $setting, $else = '' ) {
		$data = array_filter( $setting );

		return self::get_value( $input, $data ) ?? $else;
	}

	//https://stackoverflow.com/questions/6851625/php-pass-string-containing-array-keys-to-find-array-value
	public static function get_value( $value, array $keys ) {
		if ( $keys === array() ) {
			return $value; // got to the end of the list of keys
		}
		$key = array_shift( $keys );
		if ( is_array( $value ) && array_key_exists( $key, $value ) ) {
			return self::get_value( $value[ $key ], $keys );
		}

		// handle errors here
		return null;
	}

	public static function rewrite_rules() {
		$admin              = array();
		$rules              = get_option( 'rewrite_rules' );
		$admin['link_new']  = isset( $rules['^(inventory)'] ) ? '/inventory/New/' : '?taxonomy=inventory&saleclass=New';
		$admin['link_used'] = isset( $rules['^(inventory)'] ) ? '/inventory/Used/' : '?taxonomy=inventory&saleclass=Used';
		$admin['link_site'] = '<span style="white-space:nowrap;"><a href="https://www.dealertrend.com" target="_blank" title="DealerTrend, Inc: Shift Everything">DealerTrend, Inc.</a></span>';

		return $admin;
	}

	public static function company_id() {
		return Option::check_for_option( 'cdp_admin_options', array( 'admin', 'general', 'company_id' ), '0' );
	}

	public static function output_wp_kses($content){
		echo wp_kses( $content, 'post' );
	}

	public static function sanitize_get( $arr ): array {
		$data = array();
		foreach ( $arr as $key => $item ) {
			if ( is_array( $item ) ) {
				$data[$key] = self::sanitize_get( $item );
			} else {
				$sanitized_value = sanitize_text_field(wp_unslash($item));
				$data[$key] = $sanitized_value;
			}
		}
		return $data;
	}

}



