<?php

/**
 * This file adds functions to the Olympus WordPress theme.
 *
 * @package Olympus
 * @author  Orphic 
 * @license GNU General Public License v2 or later
 * @link    https://orphic.ca/
 */

if (! function_exists('olympus_setup')) {

  /**
   * Sets up theme defaults and registers support for various WordPress features.
   *
   * Note that this function is hooked into the after_setup_theme hook, which
   * runs before the init hook. The init hook is too late for some features, such
   * as indicating support for post thumbnails.
   *
   * @since 0.8.0
   *
   * @return void
   */
  function olympus_setup() {

    // Make theme available for translation.
    load_theme_textdomain('olympus', get_template_directory() . '/languages');

    // Enqueue editor styles and fonts.
    add_editor_style(
      array(
        './style.css',
      )
    );

    // Remove core block patterns.
    remove_theme_support('core-block-patterns');
  }
}
add_action('after_setup_theme', 'olympus_setup');

// Enqueue style sheet.
add_action('wp_enqueue_scripts', 'olympus_enqueue_style_sheet');
function olympus_enqueue_style_sheet() {

  wp_enqueue_style('olympus', get_template_directory_uri() . '/style.css', array(), wp_get_theme()->get('Version'));
  wp_register_style('splide-css', get_template_directory_uri() . '/assets/css/splide.min.css', array(), wp_get_theme()->get('Version'));

  wp_enqueue_script('olympus-public-js', get_template_directory_uri() . '/assets/js/public.js', array('jquery'), wp_get_theme()->get('Version'), false);

  wp_register_script('splide-js', get_template_directory_uri() . '/assets/js/splide.min.js', array(), wp_get_theme()->get('Version'), false);

  wp_register_script('mio-olympus-blocks-main-nav-block-script', get_template_directory_uri() . '/blocks/main-nav/main-nav.js', array(), wp_get_theme()->get('Version'), false);
  wp_register_script('mio-olympus-blocks-video-lightbox-block-script', get_template_directory_uri() . '/blocks/video-lightbox/video-lightbox.js', array(), wp_get_theme()->get('Version'), false);
  wp_register_script('mio-olympus-blocks-faqs-block-script', get_template_directory_uri() . '/blocks/faqs/faqs.js', array(), wp_get_theme()->get('Version'), false);
  wp_register_script('mio-olympus-blocks-multiple-booking-pages-block-script', get_template_directory_uri() . '/blocks/multiple-booking-pages/multiple-booking-pages.js', array(), wp_get_theme()->get('Version'), false);
  wp_register_script('mio-olympus-blocks-promo-card-block-script', get_template_directory_uri() . '/blocks/promo-card/promo-card.js', array(), wp_get_theme()->get('Version'), false);
  wp_register_script('mio-olympus-simple-nav-block-script', get_template_directory_uri() . '/blocks/simple-nav/simple-nav.js', array(), wp_get_theme()->get('Version'), false);
  wp_register_script('mio-olympus-testimonials-block-script', get_template_directory_uri() . '/blocks/testimonials/testimonials.js', array('splide-js'), wp_get_theme()->get('Version'), false);
}

add_action('admin_enqueue_scripts', 'olypmus_enqueue_admin_scripts');
function olypmus_enqueue_admin_scripts() {
  wp_enqueue_script('olympus-admin-js', get_template_directory_uri() . '/assets/js/admin.js', array('jquery'), wp_get_theme()->get('Version'), false);
}

add_action('acf/include_field_types', 'register_acf_icon_picker_field');
function register_acf_icon_picker_field() {
  require_once(get_template_directory() . '/admin/class-mio-olympus-blocks-acf-icon-picker.php');
}

/**
 * Register block styles.
 *
 * @since 0.9.2
 */
function olympus_register_block_styles() {

  $block_styles = array(
    'core/columns' => array(
      'columns-reverse' => __('Reverse', 'olympus'),
    ),
    'core/group' => array(
      'shadow-light' => __('Shadow', 'olympus'),
      'shadow-solid' => __('Solid', 'olympus'),
    ),
    'core/image' => array(
      'shadow-light' => __('Shadow', 'olympus'),
      'shadow-solid' => __('Solid', 'olympus'),
    ),
    'core/list' => array(
      'no-disc' => __('No Disc', 'olympus'),
    ),
    'core/quote' => array(
      'shadow-light' => __('Shadow', 'olympus'),
      'shadow-solid' => __('Solid', 'olympus'),
    ),
    'core/social-links' => array(
      'outline' => __('Outline', 'olympus'),
    ),
  );

  foreach ($block_styles as $block => $styles) {
    foreach ($styles as $style_name => $style_label) {
      register_block_style(
        $block,
        array(
          'name'  => $style_name,
          'label' => $style_label,
        )
      );
    }
  }
}
add_action('init', 'olympus_register_block_styles');

/**
 * Register block pattern categories.
 *
 * @since 1.0.4
 */
function olympus_register_block_pattern_categories() {

  register_block_pattern_category(
    'page',
    array(
      'label'       => __('Page', 'olympus'),
      'description' => __('Create a full page with multiple patterns that are grouped together.', 'olympus'),
    )
  );
  register_block_pattern_category(
    'miocommerce-pattern',
    array(
      'label'       => __('MioCommerce Patterns', 'olympus'),
      'description' => __('Sections designed for MioCommerce website customers.', 'olympus'),
    )
  );

  register_block_pattern_category(
    'miocommerce-services',
    array(
      'label'       => __('Services', 'olympus'),
      'description' => __('Sections designed to display all services or sections for the service pages.', 'olympus'),
    )
  );
}

add_action('init', 'olympus_register_block_pattern_categories');

/**
 * Register custom block categories.
 *
 * @since 1.0.4
 */
function olympus_register_custom_block_categories($block_categories, $editor_context) {

  if (! empty($editor_context->post)) {

    array_splice($block_categories, 3, 0, array(
      array(
        'slug'  => 'miocommerce-blocks',
        'title' => __('MioCommerce Blocks', 'olympus'),
        'icon'  => null,
      )
    ));
  }
  return $block_categories;
}

add_filter('block_categories_all', 'olympus_register_custom_block_categories', 10, 2);


function wpcontent_svg_mime_type($mimes = array()) {
  $mimes['svg']  = 'image/svg+xml';
  $mimes['svgz'] = 'image/svg+xml';
  return $mimes;
}
add_filter('upload_mimes', 'wpcontent_svg_mime_type');

if (! function_exists('olympus_block_styles')) :

  function olympus_block_styles() {
    $theme_version  = wp_get_theme()->get('Version');
    $version_string = is_string($theme_version) ? $theme_version : false;

    wp_enqueue_script(
      'olympus-block-variations',
      get_theme_file_uri('/assets/js/block-variations.js'),
      array('wp-blocks'),
      $theme_version,
      true
    );

    wp_enqueue_script(
      'olympus-block-styles',
      get_theme_file_uri('/assets/js/block-styles.js'),
      array('wp-blocks'),
      $theme_version,
      true
    );
  }

endif;

add_action('enqueue_block_editor_assets', 'olympus_block_styles');

if (! function_exists('olympus_inline_editor_styles')) :
  function olympus_inline_editor_styles() {

    // Add editor styles inline.
    wp_add_inline_style('wp-block-library', olympus_editor_styles());
    wp_add_inline_style('wp-block-library', olympus_service_tags_styles());
  }

endif;

add_action('admin_init', 'olympus_inline_editor_styles');

if (! function_exists('olympus_inline_frontend_styles')) :
  function olympus_inline_frontend_styles() {

    // Add frontend styles inline.
    wp_add_inline_style('olympus', olympus_service_tags_styles());
  }

endif;

add_action('wp_enqueue_scripts', 'olympus_inline_frontend_styles');


if (! function_exists('olympus_editor_styles')) :

  function olympus_editor_styles() {

    $palette = wp_get_global_settings(array('layout', 'wideSize'));

    return "
		.wp-block-group.hide-on-desktop{
			opacity: 0.8;
    		border: 1px solid rgba(0,0,0,0.1);
		}
		";
  }

endif;

if (! function_exists('olympus_service_tags_styles')) :

  function olympus_service_tags_styles() {

    if (function_exists('get_field')) {
      $alertBackgroundCol = get_field('alert_background_color', 'option');
      $alertTextCol = get_field('alert_text_color', 'option');
      $alertBorderCol = get_field('alert_border_color', 'option');

      return "
        .service-tag{
            color: {$alertTextCol};
            background-color: {$alertBackgroundCol};
            border: 1px solid {$alertBorderCol};
        }
		";
    }
    return "";
  }

endif;


function olympus_register_acf_blocks() {
  if (function_exists('register_block_type')) {
    register_block_type(__DIR__ . '/blocks/main-nav');
    register_block_type(__DIR__ . '/blocks/home-hero');
    register_block_type(__DIR__ . '/blocks/price-card');
    register_block_type(__DIR__ . '/blocks/icon-cards');
    register_block_type(__DIR__ . '/blocks/icon-card');
    register_block_type(__DIR__ . '/blocks/services');
    register_block_type(__DIR__ . '/blocks/footer');
    register_block_type(__DIR__ . '/blocks/map');
    register_block_type(__DIR__ . '/blocks/video-lightbox');
    register_block_type(__DIR__ . '/blocks/faqs');
    register_block_type(__DIR__ . '/blocks/multiple-booking-pages');
    register_block_type(__DIR__ . '/blocks/promo-card');
    register_block_type(__DIR__ . '/blocks/simple-nav');
    register_block_type(__DIR__ . '/blocks/big-button');
    register_block_type(__DIR__ . '/blocks/why-choose-us');
    register_block_type(__DIR__ . '/blocks/check-emphasis');
    register_block_type(__DIR__ . '/blocks/testimonials');
    register_block_type(__DIR__ . '/blocks/theme-spacer');
    register_block_type(__DIR__ . '/blocks/icon-list');
  }
}
add_action('init', 'olympus_register_acf_blocks');

add_action('admin_init', function () {
  // Redirect any user trying to access comments page
  global $pagenow;

  if ($pagenow === 'edit-comments.php') {
    wp_safe_redirect(admin_url());
    exit;
  }

  // Remove comments metabox from dashboard
  remove_meta_box('dashboard_recent_comments', 'dashboard', 'normal');

  // Disable support for comments and trackbacks in post types
  foreach (get_post_types() as $post_type) {
    if (post_type_supports($post_type, 'comments')) {
      remove_post_type_support($post_type, 'comments');
      remove_post_type_support($post_type, 'trackbacks');
    }
  }
});

// Close comments on the front-end
add_filter('comments_open', '__return_false', 20, 2);
add_filter('pings_open', '__return_false', 20, 2);

// Hide existing comments
add_filter('comments_array', '__return_empty_array', 10, 2);

// Remove comments page in menu
add_action('admin_menu', function () {
  remove_menu_page('edit-comments.php');
});

// Remove comments links from admin bar
add_action('init', function () {
  if (is_admin_bar_showing()) {
    remove_action('admin_bar_menu', 'wp_admin_bar_comments_menu', 60);
  }
});
