<?php
/**
 * Settings storage.
 *
 * Thin wrapper around the option that tracks which elements are enabled.
 *
 * @package CraftAddonsForWPBakery
 */

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

/**
 * Class CAFW_Settings
 */
class CAFW_Settings {

	/**
	 * Option name storing the list of disabled element slugs.
	 *
	 * We store the *disabled* list so that newly added elements are enabled
	 * by default without needing a migration.
	 *
	 * @var string
	 */
	const OPTION = 'cafw_disabled_elements';

	/**
	 * Get the array of disabled element slugs.
	 *
	 * @return string[]
	 */
	public static function get_disabled() {
		$disabled = get_option( self::OPTION, array() );
		return is_array( $disabled ) ? $disabled : array();
	}

	/**
	 * Whether a given element slug is enabled.
	 *
	 * @param string $slug Element slug.
	 * @return bool
	 */
	public static function is_element_enabled( $slug ) {
		return ! in_array( $slug, self::get_disabled(), true );
	}

	/**
	 * Persist the set of enabled element slugs.
	 *
	 * @param string[] $enabled_slugs Slugs the user wants enabled.
	 * @param string[] $all_slugs     All available slugs.
	 * @return void
	 */
	public static function save_enabled( array $enabled_slugs, array $all_slugs ) {
		$disabled = array_values( array_diff( $all_slugs, $enabled_slugs ) );
		update_option( self::OPTION, $disabled );
	}
}
