<?php

/**
 * Register FLEX Sidebar Meta Box
 */
function flothemes_sidebar_metabox_add_meta_box()
{
	global $public_post_types;

	add_meta_box(
		'flothemes_sidebar_metabox_meta_box',
		__('Sidebar', 'flothemes'),
		'flothemes_sidebar_metabox_metabox_callback',
		$public_post_types,
		'side',
		'low',
		'post',
		array(
			'__back_compat_meta_box' => FALSE,
		)
	);
}
// add_action( 'add_meta_boxes', 'flothemes_sidebar_metabox_add_meta_box' );

/**
 * Flex Sidebar Metabox Callback
 */
function flothemes_sidebar_metabox_metabox_callback($post)
{
	$value = get_post_meta($post->ID, '_flothemes_sidebar_metabox_field', TRUE);

	$sidebars = get_posts(array(
		'numberposts' => -1,
		'post_type'   => 'sidebar',
		'suppress_filters' => TRUE,
	)); ?>
<ul class="acf-radio-list acf-hl" data-allow_null="0" data-other_choice="0">
	<li>
		<label>
			<input type="radio" name="flothemes_sidebar_metabox_field" value="left_sidebar" <?php if ($value === 'left_sidebar') {
		echo 'checked';
	} ?>>
			<img src="https://demo.flothemes.com/app/themes/mono/theme-files/admin-assets/img/layout-images/left_sidebar.png">Left sidebar
		</label>
	</li>
	<li>
		<label class="selected">
			<input type="radio" name="flothemes_sidebar_metabox_field" value="full_width" <?php if ($value === 'full_width') {
		echo 'checked';
	} ?>>
			<img src="https://demo.flothemes.com/app/themes/mono/theme-files/admin-assets/img/layout-images/full_width.png">Full width</label>
	</li>
	<li>
		<label>
			<input type="radio" name="flothemes_sidebar_metabox_field" value="right_sidebar" <?php if ($value === 'right_sidebar') {
		echo 'checked';
	} ?>>
			<img src="https://demo.flothemes.com/app/themes/mono/theme-files/admin-assets/img/layout-images/right_sidebar.png">Right sidebar
		</label>
	</li>
</ul>
<?php
}

/**
 * Save Flex Sidebar Metabox
 */
function flothemes_sidebar_metabox_save_postdata($post_id)
{
	if (array_key_exists('flothemes_sidebar_metabox_field', $_POST)) {
		update_post_meta($post_id, '_flothemes_sidebar_metabox_field', $_POST['flothemes_sidebar_metabox_field']);
	}
}
add_action('save_post', 'flothemes_sidebar_metabox_save_postdata');

/**
 * Register Flex Sidebar Meta Field to Rest API
 */
function flothemes_sidebar_metabox_register_meta()
{
	register_meta(
		'post',
		'_flothemes_sidebar_metabox_field',
		array(
			'type'			=> 'string',
			'single'		=> TRUE,
			'show_in_rest'	=> TRUE,
		)
	);
}
add_action('init', 'flothemes_sidebar_metabox_register_meta');

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

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