<?php
namespace IdeoFieldPorter;

defined( 'ABSPATH' ) || exit;

/**
 * Profils de mapping, séparés par post type.
 * Option key : ideo_fp_mapping_profiles_{post_type}
 */
class Mapping_Profiles {

	private static function option_key( $post_type ) {
		return 'ideo_fp_mapping_profiles_' . sanitize_key( $post_type );
	}

	public static function get_all( $post_type = 'product' ) {
		return get_option( self::option_key( $post_type ), array() );
	}

	public static function get( $name, $post_type = 'product' ) {
		$all = self::get_all( $post_type );
		return isset( $all[ $name ] ) ? $all[ $name ] : null;
	}

	public static function save( $name, $mapping, $post_type = 'product' ) {
		$name = trim( $name );
		if ( '' === $name ) return false;
		$all          = self::get_all( $post_type );
		$all[ $name ] = $mapping;
		return update_option( self::option_key( $post_type ), $all, false );
	}

	public static function delete( $name, $post_type = 'product' ) {
		$all = self::get_all( $post_type );
		if ( ! isset( $all[ $name ] ) ) return false;
		unset( $all[ $name ] );
		return update_option( self::option_key( $post_type ), $all, false );
	}
}
