<?php

namespace Blazemedia\Affiliation\AdminUserInterface\MetaBoxes;

use Blazemedia\Affiliation\AdminUserInterface\Settings\PostTypes;

class DisclaimerSettings {

    public function __construct() {

        add_action( 'add_meta_boxes', [ get_called_class(), 'add'  ] );
        add_action( 'save_post',      [ get_called_class(), 'save' ] );

    }

    	/**
	 * Set up and add the meta box.
	 */
	public static function add() {
		
		$screens = PostTypes::getPostTypes();
		
		foreach ( $screens as $screen ) {

			add_meta_box(
				'disclaimer_box_id',          // Unique ID
				'Impostazioni Disclaimer', // Box title
				[ self::class, 'html' ],   // Content callback, must be of type callable
				$screen,                  // Post type
				'side'
			);
		}
	}


	/**
	 * Save the meta box selections.
	 *
	 * @param int $post_id  The post ID.
	 */
	public static function save( int $post_id ) {

		$value = array_key_exists( 'disclaimer_field', $_POST ) ? $_POST['disclaimer_field'] : '';
		
		update_post_meta ( $post_id, 'has_disclaimer', $value );		
	}


	/**
	 * Display the meta box HTML to the user.
	 *
	 * @param WP_Post $post   Post object.
	 */
	public static function html( $post ) {

		$value = get_post_meta( $post->ID, 'has_disclaimer', true );
		
		$checked = '';
		$checked = $value == "no" ? "checked" : '';
		?>
	
		<label for="disclaimer_field">
            <input type="checkbox" name="disclaimer_field" id="disclaimer_field" value="no" <?php echo $checked; ?> />
			<strong> No disclaimer</strong>
        </label>
		<?php
	}





}