<?php

namespace CarDealerPress\Includes\Helpers;

use CarDealerPress\includes\functions\Logger;
use CarDealerPress\includes\helpers\helpers as helpers;

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

class Option {
	use Logger;

	public static function check_for_option( $option, $setting, $else = '' ) {
		$data       = get_option( $option );
		$data_value = helpers::get_value( $data, $setting );
		// if (is_array($data_value)) { return $data_value; }
		if ( empty( $data_value ) ) {
			return $else;
		} else {
			return $data_value;
		}
	}

	public static function get_cdp_admin_options() {
		return get_option( 'cdp_admin_options' );
	}

	public static function get_cdp_theme_options() {
		return get_option( 'cdp_theme_options' );
	}

	public static function delete_options( $args ) {
		$blog_id     = $args['blog_id'] ?? get_current_blog_id();
		$clean_reset = $args['clean_reset'] ?? false;
		self::delete_options_prefixed(
			array(
				'blog_id'     => $blog_id,
				'clean_reset' => $clean_reset,
			)
		);
		delete_option( 'cardealerpress_settings' ); // Legacy version TODO remove in future version.
		delete_option( 'cardealerpress_extender_settings' ); // Legacy version TODO remove in future version.
	}

	public static function delete_options_prefixed( $args ) {
		$prefix      = 'cdp_';
		$blog_id     = $args['blog_id'];
		$clean_reset = $args['clean_reset'] ?? false;
		global $wpdb;
		// @codingStandardsIgnoreStart
		$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->options} WHERE option_name LIKE %s", $prefix . '%' ) );
		if ( $clean_reset ) {
			( new Option() )->append_log( array( 'message' => 'CDP Message: removed CDP options' ) );
		} else {
			error_log( 'CDP Message: removed CDP options on site: ' . $blog_id );
		}
		// @codingStandardsIgnoreEnd
	}

}
