<?php 
/**
* Sidebar Layout panel
*
* @package Travel Tour
*
*/ 

/**
 * Register the sidebar layout meta for posts and pages.
 */
function travel_tour_register_sidebar_layout_meta() {
  $args = array(
    'type'         => 'string',
    'single'       => true,
    'show_in_rest' => true,
    'default'      => '',
  );
  register_post_meta( 'post', 'travel_tour_sidebar_layout', $args );
  register_post_meta( 'page', 'travel_tour_sidebar_layout', $args );
}
add_action( 'init', 'travel_tour_register_sidebar_layout_meta' );


/**
 * Returns true if the frontend post ID of the current editor page is included in the given $disabled_post_ids
 * Returns false otherwise
 * 
 * @param array $disabled_post_ids Array of post IDs for which the sidebar panel should be disabled.
 * @return bool True if the sidebar should be disabled; false otherwise.
 */
function travel_tour_is_sidebar_disabled( $disabled_post_ids = array() ) {
  if ( isset( $_GET['post'] ) ) {
    $post_id = absint( $_GET['post'] );
    $disabled_post_ids = array_map( 'absint', $disabled_post_ids );   
    if ( in_array( $post_id, $disabled_post_ids, true ) ) {
        return true;
    }
  }
  return false;
}


/**
 * Enqueue the sidebar panel JavaScript for the Gutenberg editor.
 */
function travel_tour_enqueue_editor_sidebar_script() {
  // Exclude widget editor and site editor
  if ( is_admin() && function_exists( 'get_current_screen' ) ) {
    $screen = get_current_screen();
    if ( isset( $screen->id ) && in_array( $screen->id, array( 'widgets', 'site-editor' ) ) ) {
      return;
    }
    
    // Only enqueue for post and page post types
    if ( isset( $screen->post_type ) && ! in_array( $screen->post_type, array( 'post', 'page' ) ) ) {
      return;
    }
  }

  wp_enqueue_script(
    'travel-tour-sidebar-panel',
    get_template_directory_uri() . '/inc/gutenberg/sidebar/sidebar_panel.js',
    array(
      'wp-plugins',
      'wp-edit-post',
      'wp-element',
      'wp-components',
      'wp-data',
      'wp-compose'
    ),
    filemtime( get_template_directory() . '/inc/gutenberg/sidebar/sidebar_panel.js' )
  );

  // Decide whether sidebar panel should be enabled on current page
  $disable_sidebar = travel_tour_is_sidebar_disabled(
    array(
      get_option('page_on_front'),
      get_option('page_for_posts'),
    )
  );
  wp_localize_script(
    'travel-tour-sidebar-panel',
    'travel_tour_sidebar_settings', array(
      'disable_sidebar' => $disable_sidebar,
    )
  );
}
add_action( 'enqueue_block_editor_assets', 'travel_tour_enqueue_editor_sidebar_script' );

/**
 * Enqueue the sidebar panel CSS for the Gutenberg editor.
 */
function travel_tour_enqueue_editor_styles() {
  // Exclude widget editor and site editor
  if ( is_admin() && function_exists( 'get_current_screen' ) ) {
    $screen = get_current_screen();
    if ( isset( $screen->id ) && in_array( $screen->id, array( 'widgets', 'site-editor' ) ) ) {
      return;
    }
    
    // Only enqueue for post and page post types
    if ( isset( $screen->post_type ) && ! in_array( $screen->post_type, array( 'post', 'page' ) ) ) {
      return;
    }
  }

  wp_enqueue_style(
    'travel-tour-sidebar-panel',
    get_template_directory_uri() . '/inc/gutenberg/sidebar/sidebar_panel.css',
    array( 'wp-edit-blocks' ),
    filemtime( get_template_directory() . '/inc/gutenberg/sidebar/sidebar_panel.css' )
  );
}
add_action( 'enqueue_block_editor_assets', 'travel_tour_enqueue_editor_styles' );