<?php
function gallery_metabox_enqueue($hook)
{
  global $post_type;
  $theme_data = wp_get_theme();
  $version=$theme_data->get( 'Version' );

  if ( in_array($hook, array('post.php', 'post-new.php','edit.php')) and 'gallery' == $post_type) {
		wp_enqueue_script('flothemes_gallery_metabox', get_template_directory_uri() . '/flo-app/configuration-files/metaboxes/gallery-metabox/js/gallery-metabox.js', array('jquery', 'jquery-ui-sortable'),$version);
		wp_enqueue_style('flothemes_gallery_metabox', get_template_directory_uri() . '/flo-app/configuration-files/metaboxes/gallery-metabox/css/gallery-metabox.css','',$version);
	}
}
add_action('admin_enqueue_scripts', 'gallery_metabox_enqueue');

function add_gallery_metabox($post_type)
{
	add_meta_box(
		'flothemes_gallery_metabox',
		'Gallery',
		'gallery_meta_callback',
		['gallery'],
		'normal',
		'high'
	);
}
add_action('add_meta_boxes', 'add_gallery_metabox');


function gallery_meta_callback($post) {
  wp_nonce_field(basename(__FILE__), 'gallery_meta_nonce');
  $ids = get_post_meta($post->ID, '_post_image_gallery', true);
?>
  <div class="flothemes_gallery_sorting_select">
    <div class="select">
      <div class="select-trigger">
        <i class="fgt-icon-sorting-list"></i>
        <span>Gallery Filter</span>
        <i class="fgt-icon-triangle" id="arrow"></i>
      </div>
      <div class="select-options">
        <span class="select-option selected" data-value="none">None</span>
        <span class="select-option" data-value="upload_date">Upload date</span>
        <span class="select-option" data-value="ascending">Name A - Z</span>
        <span class="select-option" data-value="descending">Name Z - A</span>
      </div>
    </div>
  </div>
  <table class="form-table">
    <tr>
      <td>
        <a class="gallery-add button" href="#" data-uploader-title="Add image(s) to gallery" data-uploader-button-text="Add image(s)">Add Images <i class="fgt-icon-add-small"></i></a>
        <input type="hidden" id="gallery_ids" value='<?php echo json_encode($ids ? $ids : []) ?>' />

        <ul id="flothemes_gallery_metabox-list">
          <?php if ($ids && count($ids) > 1) { ?>
            <li class="fgt-gal-first-child"><i class="fgt-icon-image-add"></i></li>
          <?php } ?>
          <?php
            if ($ids) : foreach ($ids as $key => $value) : 
              $image_metadata = wp_get_attachment_metadata($value);            
              $image_url = wp_get_attachment_image_src($value);
              if (is_array($image_url) && isset($image_url[0])) $image_url = $image_url[0];
              if ($image_url) {
            ?>
            <li class="fgt-gallery-item" data-file="<?php echo $image_metadata['file'] ?>">
              <input type="hidden" name="_post_image_gallery[<?php echo $key; ?>]" value="<?php echo $value; ?>">
              <img class="image-preview" src="<?php echo $image_url ?>">
              <a class="change-image button button-small" href="#" data-uploader-title="Change image" data-uploader-button-text="Change image">
                <i class="fgt-icon-reload"></i>
              </a>
              <a class="remove-image" href="#">
                <i class="fgt-icon-trash"></i>
              </a>
            </li>
            <?php
	            }
	            endforeach;
	            endif; 
            ?>
        </ul>

      </td>
    </tr>
  </table>
<?php
}

function gallery_meta_save($post_id)
{
	if (!isset($_POST['gallery_meta_nonce']) || !wp_verify_nonce($_POST['gallery_meta_nonce'], basename(__FILE__))) {
		return;
	}

	if (!current_user_can('edit_post', $post_id)) {
		return;
	}

	if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
		return;
	}

	if (isset($_POST['_post_image_gallery'])) {
		update_post_meta($post_id, '_post_image_gallery', $_POST['_post_image_gallery']);
	} else {
		delete_post_meta($post_id, '_post_image_gallery');
	}
}
add_action('save_post', 'gallery_meta_save');

?>