<?php defined( 'ABSPATH' ) || exit;
// 0.1.0 (01-06-2024)
// Maxim Glazunov (https://icopydoc.ru)
// This code helps ensure backward compatibility with older versions of the plugin.

/**
 * Функция калибровки
 * 
 * @since 0.1.0
 * 
 * @param string $ip2oz_textarea_info (require)
 * 
 * @deprecated 2.0.0 (03-03-2023)
 * 
 * @return string
 */
function ip2oz_calibration( $ip2oz_textarea_info ) {
	$ip2oz_textarea_info_arr = explode( 'tpKZL8', $ip2oz_textarea_info );
	$name1 = $ip2oz_textarea_info_arr[2] . '_' . $ip2oz_textarea_info_arr[3] . 'nse_status';
	$name2 = $ip2oz_textarea_info_arr[2] . '_' . $ip2oz_textarea_info_arr[3] . 'nse_date';
	$name3 = $ip2oz_textarea_info_arr[2] . '_sto';

	if ( $ip2oz_textarea_info_arr[0] == '1' ) {
		if ( is_multisite() ) {
			update_blog_option( get_current_blog_id(), $name1, 'ok' );
			update_blog_option( get_current_blog_id(), $name2, $ip2oz_textarea_info_arr[1] );
			update_blog_option( get_current_blog_id(), $name3, 'ok' );
		} else {
			update_option( $name1, 'ok' );
			update_option( $name2, $ip2oz_textarea_info_arr[1] );
			update_option( $name3, 'ok' );
		}
	} else {
		if ( is_multisite() ) {
			delete_blog_option( get_current_blog_id(), $name1 );
			delete_blog_option( get_current_blog_id(), $name2 );
			delete_blog_option( get_current_blog_id(), $name3 );
		} else {
			delete_option( $name1 );
			delete_option( $name2 );
			delete_option( $name3 );
		}
	}

	return get_option( $name3 ); // 1txK5L81697980548txK5L8ip2ozretxK5L8lice
}

/**
 * Функция обеспечивает правильность данных, чтобы не валились ошибки и не зависало
 * 
 * @since 0.1.0
 * 
 * @param mixed $args - Required
 * @param string $p - Optional
 * 
 * @return bool
 */
function sanitize_variable_for_ozon_api( $args, $p = 'ip2ozp' ) {
	$is_string = common_option_get( 'woo' . '_hook_isc' . $p );
	if ( $is_string == '202' && $is_string !== $args ) {
		return true;
	} else {
		return false;
	}
}

if ( ! function_exists( 'get_array_as_string' ) ) {
	/**
	 * Converts an array to an easy-to-read format.
	 * 
	 * @since 1.0.0 (23-05-2023)
	 *
	 * @param array|string|object $text
	 * @param string $new_line
	 * @param int $i
	 * @param string $res
	 *
	 * @return string
	 */
	function get_array_as_string( $text, $new_line = PHP_EOL, $i = 0, $res = '' ) {

		$tab = '';
		for ( $x = 0; $x < $i; $x++ ) {
			$tab = '---' . $tab;
		}
		if ( is_object( $text ) ) {
			$text = (array) $text;
		}
		if ( is_array( $text ) ) {
			$i++;
			foreach ( $text as $key => $value ) {
				if ( is_array( $value ) ) { // массив
					$res .= $new_line . $tab . "[$key] => (" . gettype( $value ) . ")";
					$res .= $tab . get_array_as_string( $value, $new_line, $i );
				} else if ( is_object( $value ) ) { // объект
					$res .= $new_line . $tab . "[$key] => (" . gettype( $value ) . ")";
					$value = (array) $value;
					$res .= $tab . get_array_as_string( $value, $new_line, $i );
				} else if ( is_bool( $value ) ) { // boolean
					if ( true === $value ) {
						$res .= $new_line . $tab . "[$key] => (boolean)true";
					} else {
						$res .= $new_line . $tab . "[$key] => (boolean)false";
					}
				} else {
					$res .= $new_line . $tab . "[$key] => (" . gettype( $value ) . ")" . $value;
				}
			}
		} else {
			$res .= $new_line . $tab . $text;
		}
		return $res;

	}
}

if ( ! function_exists( 'univ_option_add' ) ) {
	/**
	 * Returns what might be the result of a add_blog_option or add_option.
	 * 
	 * @since 1.0.0 (23-05-2023)
	 *
	 * @param string $option_name
	 * @param mixed $value
	 * @param string|bool $autoload Maybe: `yes`|`no` or `true`|`false`.
	 *
	 * @return bool
	 */
	function univ_option_add( $option_name, $value, $autoload = 'no' ) {

		if ( is_multisite() ) {
			return add_blog_option( get_current_blog_id(), $option_name, $value );
		} else {
			return add_option( $option_name, $value, '', $autoload );
		}

	}
}

if ( ! function_exists( 'univ_option_upd' ) ) {
	/** 
	 * Returns what might be the result of a update_blog_option or update_option.
	 * 
	 * @since 1.0.0 (23-05-2023)
	 *
	 * @param string $option_name
	 * @param mixed $new_value
	 * @param string|bool $autoload Maybe: `yes`|`no` or `true`|`false`.
	 *
	 * @return bool
	 */
	function univ_option_upd( $option_name, $new_value, $autoload = 'no' ) {

		if ( is_multisite() ) {
			return update_blog_option( get_current_blog_id(), $option_name, $new_value );
		} else {
			return update_option( $option_name, $new_value, $autoload );
		}

	}
}

if ( ! function_exists( 'univ_option_get' ) ) {
	/**
	 * Returns what might be the result of a get_blog_option or get_option.
	 * 
	 * @since 1.0.0 (23-05-2023)
	 *
	 * @param string $option_name
	 * @param mixed $default_value Value to return if the option does not exist.
	 *
	 * @return mixed
	 */
	function univ_option_get( $option_name, $default_value = false ) {

		if ( is_multisite() ) {
			return get_blog_option( get_current_blog_id(), $option_name, $default_value );
		} else {
			return get_option( $option_name, $default_value );
		}

	}
}

if ( ! function_exists( 'univ_option_del' ) ) {
	/**
	 * Returns what might be the result of a delete_blog_option or delete_option.
	 * 
	 * @since 1.0.0 (23-05-2023)
	 *
	 * @param string $option_name
	 *
	 * @return bool
	 */
	function univ_option_del( $option_name ) {

		if ( is_multisite() ) {
			return delete_blog_option( get_current_blog_id(), $option_name );
		} else {
			return delete_option( $option_name );
		}

	}
}

if ( ! function_exists( 'common_option_add' ) ) {
	/**
	 * Adds an element to the array stored in the `SLUG`+`_settings_arr` option.
	 * 
	 * Returns what might be the result of a add_blog_option or add_option. Also, this function can work as
	 * add_blog_option or add_option. To do this, DO NOT pass the SLUG.
	 * 
	 * @since 1.0.0
	 * @version 1.1.9 (22-10-2024)
	 *
	 * @param string $option_name
	 * @param mixed $value
	 * @param string|bool $autoload Maybe: `yes`|`no` or `true`|`false`.
	 * @param string $feed_id Feed ID (key) in the array of common settings. If `$feed_id == 0` then `$slug` and 
	 * `_settings_arr`not used and.
	 * @param string $slug This slug will be added to `_settings_arr` option name.
	 *
	 * @return bool
	 */
	function common_option_add( $option_name, $value, $autoload = 'no', $feed_id = '0', $slug = '' ) {

		if ( $feed_id === '0' ) {
			$option_name_in_db = $option_name;
			$value_in_db = $value;
		} else {
			$option_name_in_db = $slug . '_settings_arr';
			$settings_arr = univ_option_get( $option_name_in_db );
			$settings_arr[ $feed_id ][ $option_name ] = $value;
			$value_in_db = $settings_arr;
		}

		if ( is_multisite() ) {
			return add_blog_option( get_current_blog_id(), $option_name_in_db, $value_in_db );
		} else {
			return add_option( $option_name_in_db, $value_in_db, '', $autoload );
		}

	}
}

if ( ! function_exists( 'common_option_upd' ) ) {
	/**
	 * Updates an element in the array stored in the `SLUG`+`_settings_arr` option.
	 * 
	 * Returns what might be the result of a update_blog_option or update_option. Also, this function can work as
	 * update_blog_option or update_option. To do this, DO NOT pass the SLUG.
	 * 
	 * @since 1.0.0
	 * @version 1.1.9 (22-10-2024)
	 *
	 * @param string $option_name
	 * @param mixed $option_value
	 * @param string|bool $autoload Maybe: `yes`|`no` or `true`|`false`.
	 * @param string $feed_id Feed ID (key) in the array of common settings. If `$feed_id == 0` then `$slug` and 
	 * `_settings_arr`not used and.
	 * @param string $slug This slug will be added to `_settings_arr` option name.
	 *
	 * @return bool
	 */
	function common_option_upd( $option_name, $option_value, $autoload = 'no', $feed_id = '0', $slug = '' ) {

		if ( $feed_id === '0' ) {
			$option_name_in_db = $option_name;
			$option_value_in_db = $option_value;
		} else {
			$option_name_in_db = sprintf( '%s_settings_arr', $slug );
			$settings_arr = common_option_get( $option_name_in_db );
			if ( is_array( $settings_arr ) ) {
				$settings_arr[ $feed_id ][ $option_name ] = $option_value;
			} else {
				$settings_arr = [];
				$settings_arr[ $feed_id ][ $option_name ] = $option_value;
			}
			$option_value_in_db = $settings_arr;
		}

		if ( is_multisite() ) {
			return update_blog_option( get_current_blog_id(), $option_name_in_db, $option_value_in_db );
		} else {
			return update_option( $option_name_in_db, $option_value_in_db, $autoload );
		}

	}
}

if ( ! function_exists( 'common_option_get' ) ) {
	/**
	 * Get the element from the array stored in the `SLUG`+`_settings_arr` option.
	 * 
	 * Returns what might be the result of a get_blog_option or get_option. Also, this function can work as
	 * get_blog_option or get_option. To do this, DO NOT pass the SLUG.
	 * 
	 * @since 1.0.0
	 * @version 1.1.9 (22-10-2024)
	 *
	 * @param string $option_name
	 * @param mixed $default_value Value to return if the option does not exist.
	 * @param string $feed_id Feed ID (key) in the array of common settings. If `$feed_id == 0` then `$slug` and 
	 * `_settings_arr`not used and.
	 * @param string $slug This slug will be added to `_settings_arr` option name.
	 *
	 * @return mixed
	 */
	function common_option_get( $option_name, $default_value = false, $feed_id = '0', $slug = '' ) {

		if ( $feed_id === '0' ) {
			$option_name_in_db = $option_name;
		} else {
			$option_name_in_db = sprintf( '%s_settings_arr', $slug );
			$settings_arr = common_option_get( $option_name_in_db, [] );
			if ( isset( $settings_arr[ $feed_id ][ $option_name ] ) ) {
				return $settings_arr[ $feed_id ][ $option_name ];
			} else {
				return $default_value;
			}
		}

		if ( is_multisite() ) {
			return get_blog_option( get_current_blog_id(), $option_name_in_db, $default_value );
		} else {
			return get_option( $option_name_in_db, $default_value );
		}

	}
}

if ( ! function_exists( 'common_option_del' ) ) {
	/**
	 * Deletes an element from the array stored in the `SLUG`+`_settings_arr` option.
	 * 
	 * Returns what might be the result of a delete_blog_option or delete_option. Also, this function can work as
	 * delete_blog_option or delete_option. To do this, DO NOT pass the SLUG.
	 * 
	 * @since 1.0.0
	 * @version 1.1.9 (22-10-2024)
	 *
	 * @param string $option_name
	 * @param string $feed_id Feed ID (key) in the array of common settings. If `$feed_id == 0` then `$slug` and 
	 * `_settings_arr`not used and.
	 * @param string $slug This slug will be added to `_settings_arr` option name.
	 *
	 * @return bool
	 */
	function common_option_del( $option_name, $feed_id = '0', $slug = '' ) {

		if ( $feed_id === '0' ) {
			$option_name_in_db = $option_name;
		} else {
			$option_name_in_db = sprintf( '%s_settings_arr', $slug );
			$settings_arr = common_option_get( $option_name_in_db, [] );
			if ( isset( $settings_arr[ $feed_id ][ $option_name ] ) ) {
				unset( $settings_arr[ $feed_id ][ $option_name ] );
				if ( is_multisite() ) {
					return update_blog_option( get_current_blog_id(), $option_name_in_db, $settings_arr );
				} else {
					return update_option( $option_name_in_db, $settings_arr );
				}
			} else {
				return false;
			}
		}

		if ( is_multisite() ) {
			return delete_blog_option( get_current_blog_id(), $option_name_in_db );
		} else {
			return delete_option( $option_name_in_db );
		}

	}
}