<?php
/**
 * @package The7
 */

defined( 'ABSPATH' ) || exit;

abstract class The7_Demo_Tracker {

	const HISTORY_OPTION_ID = 'the7_demo_history';

	protected $demo_type;

	protected $demo_history;

	public function __construct( $demo_type ) {
		$this->demo_type    = $demo_type;
		$this->demo_history = static::read_history_option();
	}

	public static function get_demo_history( $demo_id ) {
		$demo_history = static::read_history_option();

		if ( ! array_key_exists( $demo_id, $demo_history ) ) {
			return [];
		}

		return is_array( $demo_history[ $demo_id ] ) ? $demo_history[ $demo_id ] : [];
	}

	/**
	 * Reads the demo history option, guaranteeing an array.
	 *
	 * get_option()'s default only applies when the row is absent; a row holding
	 * a corrupted (non-array) value - e.g. a serialized `false` left behind by a
	 * migration tool, or an `option_*` filter from another plugin - is returned
	 * as is and used to blow up the dashboard.
	 *
	 * @return array
	 */
	public static function read_history_option() {
		$demo_history = get_option( static::HISTORY_OPTION_ID, [] );

		return is_array( $demo_history ) ? $demo_history : [];
	}

	abstract public function get( $key );

	abstract public function set( $key, $value );

	abstract public function add( $key, $value );

	abstract public function remove( $key );

	abstract public function get_demo_type();

	abstract public function track_imported_items();

	abstract public function keep_demo_content();

	abstract public function remove_demo();
}
