<?php

/**
 * Register FLEX Footer Meta Box
 */
function flothemes_footer_metabox_add_meta_box()
{
	global $public_post_types;

	add_meta_box(
		'flothemes_footer_metabox_meta_box',
		'<i class="fgt-icon-meta-footer"></i>' . __('Footer', 'flothemes'),
		'flothemes_footer_metabox_metabox_callback',
		$public_post_types,
		'side',
		'core',
		array(
			'__back_compat_meta_box' => FALSE,
		)
	);
}
add_action('add_meta_boxes', 'flothemes_footer_metabox_add_meta_box');

/**
 * FLEX Footer Metabox Callback
 */
function flothemes_footer_metabox_metabox_callback($post)
{
	$value = get_post_meta($post->ID, '_flothemes_footer_metabox_field', TRUE);
	$useGlobal = get_post_meta($post->ID, '_flothemes_use_global_footer_metabox_field', TRUE);

	$footers = get_posts(array(
		'numberposts' => -1,
		'post_type'   => 'fgt_footer',
		'suppress_filters' => TRUE,
	)); ?>

	<!-- If this flag is present, proceed on parsing true/false (which are inexistent) keys for switch inputs -->
	<input name="flothemes_use_meta_flag" type="hidden" value="flo-meta-flag" />

	<input class="fgt-metaboxes-switch" id="flothemes_use_global_footer_metabox_field" name="flothemes_use_global_footer_metabox_field" data-title="<?php _e('Use Global', 'flothemes') ?>" value="true" type="checkbox" <?php if ($useGlobal === "true" || $useGlobal === "") {
		echo 'checked';
	} ?> />
	<select class="fgt-metaboxes-select" name="flothemes_footer_metabox_field" id="flothemes_footer_metabox_field" data-condition-checked-false="flothemes_use_global_footer_metabox_field">
		<?php foreach ($footers as $footer) { ?>
			<option value="<?php echo $footer->ID; ?>" <?php
														if ((int) $value === $footer->ID) {
															echo 'selected';
														} ?>>
				<?php echo $footer->post_title; ?>
			</option>
		<?php } ?>
		<option value="DO_NOT_USE_FOOTER" <?php if ($value === 'DO_NOT_USE_FOOTER') {
															echo 'selected';
														} ?>>
			Remove Footer
		</option>
	</select>
<?php
}

/**
 * Save FLEX Footer Metabox
 */
function flothemes_footer_metabox_save_postdata($post_id)
{
	if (empty($_POST['flothemes_use_meta_flag']) || $_POST['flothemes_use_meta_flag'] !== 'flo-meta-flag') {
		return;
	}

	if (array_key_exists('flothemes_footer_metabox_field', $_POST)) {
		update_post_meta($post_id, '_flothemes_footer_metabox_field', $_POST['flothemes_footer_metabox_field']);
	}

	if (array_key_exists('flothemes_use_global_footer_metabox_field', $_POST)) {
		update_post_meta($post_id, '_flothemes_use_global_footer_metabox_field', $_POST['flothemes_use_global_footer_metabox_field']);
	} else {
		update_post_meta($post_id, '_flothemes_use_global_footer_metabox_field', 'false');
	}
}
add_action('save_post', 'flothemes_footer_metabox_save_postdata');

/**
 * Register FLEX Footer Meta Field to Rest API
 */
function flothemes_footer_metabox_register_meta()
{
	register_meta(
		'post',
		'_flothemes_footer_metabox_field',
		array(
			'type'			=> 'string',
			'single'		=> TRUE,
			'show_in_rest'	=> TRUE,
		)
	);
}
add_action('init', 'flothemes_footer_metabox_register_meta');

/**
 * Register FLEX Footer Metabox to Rest API
 */
function flothemes_footer_metabox_api_posts_meta_field()
{
	register_rest_route(
		'flothemes/v1',
		'/update-meta',
		array(
			'methods'  => 'POST',
			'callback' => 'flothemes_footer_metabox_update_callback',
			'args'     => array(
				'id' => array(
					'sanitize_callback' => 'absint',
				),
			),
			'permission_callback' => '__return_true'
		)
	);
}
add_action('rest_api_init', 'flothemes_footer_metabox_api_posts_meta_field');

/**
 * FLEX Footer REST API Callback for Gutenberg
 */
function flothemes_footer_metabox_update_callback($data)
{
	return update_post_meta($data['id'], $data['key'], $data['value']);
}
