<?php

namespace Blazemedia\Affiliation\AdminUserInterface\MetaBoxes;

use Blazemedia\Affiliation\AdminUserInterface\Settings\PostTypes;

class PaidSettings {

    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(
				'paid_box_id',          // Unique ID
				'Impostazioni Paid', // 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( 'paid_field', $_POST ) ? $_POST['paid_field'] : '';
		
		wp_set_object_terms( $post_id, $value, 'blz_affiliation_attributes' );		
	}


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


		$value = has_term( 'paid', 'blz_affiliation_attributes' );

	
		$checked = '';
		$checked =  $value ? "checked" : '';
		?>
	
		<label for="paid_field">
            <input type="checkbox" name="paid_field" id="paid_field" value="paid" <?php echo $checked; ?> />
			<strong>Paid</strong>
        </label>
		<?php
	}





}