<?php

/**
 * Register FLEX Global Header Meta Box
 */
function flothemes_global_header_metabox_add_meta_box()
{
	add_meta_box(
		'flothemes_global_header_metabox_meta_box',
		__('Header', 'flothemes'),
		'flothemes_global_header_metabox_metabox_callback',
		['fgt_header'],
		'side',
		'core',
		array(
			'__back_compat_meta_box' => FALSE,
		)
	);
}
add_action('add_meta_boxes', 'flothemes_global_header_metabox_add_meta_box');

/**
 * FLEX Global Header Metabox Callback
 */
function flothemes_global_header_metabox_metabox_callback($post)
{
	$isGlobal = get_post_meta($post->ID, '_flothemes_global_header_metabox_field', TRUE);
	$isSticky = get_post_meta($post->ID, '_flothemes_sticky_header_metabox_field', TRUE); ?>
	<input
		class="fgt-metaboxes-switch"
		type="checkbox"
		data-title="<?php _e('Global', 'flothemes') ?>"
		name="flothemes_global_header_metabox_field"
		<?php if ($isGlobal === 'on') {
		echo 'checked="checked" disabled="true"';
	} ?>
	/>
	<input
		class="fgt-metaboxes-switch"
		type="checkbox"
		data-title="<?php _e('Sticky', 'flothemes') ?>"
		name="flothemes_sticky_header_metabox_field"
		value="true"
		<?php if ($isSticky === 'true') {
		echo 'checked';
	} ?>
	/>
	<?php
}

/**
 * Save FLEX Global Header Metabox
 */
function flothemes_global_header_metabox_save_postdata($post_id, $post, $update)
{
	if (get_post_type($post_id) === 'fgt_header' && $update) {
		if (array_key_exists('flothemes_global_header_metabox_field', $_POST)) {
			/* Get other headers and reset global option */
			$headers = get_posts(array(
				'numberposts' 	=> -1,
				'exclude' 		=> $post_id,
				'post_type'   	=> 'fgt_header',
				'fields'      	=> 'ids',
				'suppress_filters' => TRUE,
				'meta_query' 	=> array(
					array(
						'key' 		=> '_flothemes_global_header_metabox_field',
						'value' 	=> 'on',
						'compare' 	=> '=',
					)
				)
			));

			foreach ($headers as $key => $value) {
				update_post_meta($value, '_flothemes_global_header_metabox_field', '');
			}

			update_post_meta($post_id, '_flothemes_global_header_metabox_field', $_POST['flothemes_global_header_metabox_field']);
			/* Get other headers and reset global option */
		}

		if (!(isset($_POST['action']) && $_POST['action'] === 'flexSaveGutenberg')) {
			if (array_key_exists('flothemes_sticky_header_metabox_field', $_POST)) {
				update_post_meta($post_id, '_flothemes_sticky_header_metabox_field', $_POST['flothemes_sticky_header_metabox_field']);
			} else {
				update_post_meta($post_id, '_flothemes_sticky_header_metabox_field', FALSE);
			}
		}
	}
}
add_action('save_post', 'flothemes_global_header_metabox_save_postdata', 10, 3);

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

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

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