<?php

/**
 * Register FLEX Global Footer Meta Box
 */
function flothemes_global_footer_metabox_add_meta_box()
{
	add_meta_box(
		'flothemes_global_footer_metabox_meta_box',
		__('Footer', 'flothemes'),
		'flothemes_global_footer_metabox_metabox_callback',
		['fgt_footer'],
		'side',
		'core',
		array(
			'__back_compat_meta_box' => FALSE,
		)
	);
}
add_action('add_meta_boxes', 'flothemes_global_footer_metabox_add_meta_box');

/**
 * FLEX Global Footer Metabox Callback
 */
function flothemes_global_footer_metabox_metabox_callback($post)
{
	$value = get_post_meta($post->ID, '_flothemes_global_footer_metabox_field', TRUE); ?>

	<input
		class="fgt-metaboxes-switch"
		type="checkbox"
		data-title="<?php _e('Global', 'flothemes') ?>"
		name="flothemes_global_footer_metabox_field"
		<?php if ($value === 'on') {
		echo 'checked="checked" disabled="true"';
	} ?>
	/>

	<?php
}

/**
 * Save FLEX Global Footer Metabox
 */
function flothemes_global_footer_metabox_save_postdata($post_id, $post, $update)
{
	if (get_post_type($post_id) === 'fgt_footer' && $update) {
		if (array_key_exists('flothemes_global_footer_metabox_field', $_POST)) {
			/* Get other footers and reset global option */
			$footers = get_posts(array(
				'numberposts' 	=> -1,
				'exclude' 		=> $post_id,
				'post_type'   	=> 'fgt_footer',
				'fields'      	=> 'ids',
				'suppress_filters' => TRUE,
				'meta_query' 	=> array(
					array(
						'key' 		=> '_flothemes_global_footer_metabox_field',
						'value' 	=> 'on',
						'compare' 	=> '=',
					)
				)
			));

			foreach ($footers as $key => $value) {
				update_post_meta($value, '_flothemes_global_footer_metabox_field', '');
			}
			/* Get other footers and reset global option */
		}

		if (array_key_exists('flothemes_global_footer_metabox_field', $_POST)) {
			update_post_meta($post_id, '_flothemes_global_footer_metabox_field', $_POST['flothemes_global_footer_metabox_field']);
		}
	}
}
add_action('save_post', 'flothemes_global_footer_metabox_save_postdata', 10, 3);

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

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

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