<?php 
/**
* Sidebar Layout panel
*
* @package chic-lifestyle
*
*/ 

/**
 * Register the sidebar layout meta for posts and pages.
 */
function chic_lifestyle_register_sidebar_layout_meta() {
  $args = array(
    'type'         => 'string',
    'single'       => true,
    'show_in_rest' => true,
    'default'      => '',
  );
  register_post_meta( 'post', 'chic_lifestyle_sidebar_layout', $args );
  register_post_meta( 'page', 'chic_lifestyle_sidebar_layout', $args );
}
add_action( 'init', 'chic_lifestyle_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 chic_lifestyle_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 chic_lifestyle_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;
    }
  }

  wp_enqueue_script(
    'chic-lifestyle-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 = chic_lifestyle_is_sidebar_disabled(
    array(
      get_option('page_on_front'),
      get_option('page_for_posts'),
    )
  );
  wp_localize_script(
    'chic-lifestyle-sidebar-panel',
    'chic_lifestyle_sidebar_settings', array(
      'disable_sidebar' => $disable_sidebar,
    )
  );
}
add_action( 'enqueue_block_editor_assets', 'chic_lifestyle_enqueue_editor_sidebar_script' );

/**
 * Enqueue the sidebar panel CSS for the Gutenberg editor.
 */
function chic_lifestyle_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;
    }
  }

  wp_enqueue_style(
    'chic-lifestyle-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', 'chic_lifestyle_enqueue_editor_styles' );