<?php

include_once __DIR__ . "/classes/inoby-duplicate-records.php";

use Inoby\DuplicateRecords;

function inoby_disable_editor($id = false)
{
  $excluded_templates = Inoby_Config::allowed_editors();

  if (empty($id)) {
    return false;
  }

  $id = intval($id);
  $template = get_page_template_slug($id);

  return $template && !in_array($template, $excluded_templates);
}

function inoby_disable_gutenberg($can_edit, $post_type)
{
  if (!(is_admin() && !empty($_GET["post"]))) {
    return $can_edit;
  }

  if (inoby_disable_editor($_GET["post"])) {
    $can_edit = false;
  }

  return $can_edit;
}

function inoby_disable_classic_editor()
{
  $screen = get_current_screen();
  if ("page" !== $screen->id || !isset($_GET["post"])) {
    return;
  }

  if (inoby_disable_editor($_GET["post"])) {
    remove_post_type_support("page", "editor");
  }
}

function remove_product_editor()
{
  remove_post_type_support("product", "editor");
}
// Ondrej: toto neviem preco sa zakazovalo ale ked to zakazem tak zmizne description z produktu
// add_action("init", "remove_product_editor");

# DISABLE EDITORS FOR SPECIFIC PAGES
add_action("admin_head", "inoby_disable_classic_editor");
add_filter("gutenberg_can_edit_post_type", "inoby_disable_gutenberg", 10, 2);
add_filter("use_block_editor_for_post_type", "inoby_disable_gutenberg", 10, 2);

// add post duplication support
(new DuplicateRecords())->init_hooks();

// update check for plugins hosted on plugins.inoby.sk
add_filter(
  "update_plugins_plugins.inoby.sk",
  function ($update, $plugin_data, $plugin_file, $locales) {
    $response = file_get_contents($plugin_data["UpdateURI"]);
    if ($response) {
      $data = json_decode($response);
      if ($data) {
        return [
          "version" => $data->version,
          "package" => $data->url,
        ];
      }
    }
    return $update;
  },
  10,
  4,
);

/**
 * Pridavam reusable blocks do nastaveni vzhladu
 */
add_action("admin_menu", function () {
  $title = _x("Reusable blocks", "post type general name");
  add_submenu_page("themes.php", $title, $title, "edit_posts", "edit.php?post_type=wp_block");
});

/**
 * Pridavam kod reusable bloku do zoznamu blokov
 */
add_filter("manage_wp_block_posts_columns", function ($columns) {
  $columns["slug"] = __("Post name");
  return $columns;
});

add_action(
  "manage_posts_custom_column",
  function ($column_name, $post_id) {
    if ($column_name == "slug") {
      echo get_post_field("post_name", $post_id);
    }
  },
  10,
  2,
);

/*
 * Povolujeme len nase komponenty
 */
add_filter("allowed_block_types_all", "allow_only_inoby_blocks", 25, 2);

function allow_only_inoby_blocks($allowed_blocks, $editor_context)
{
  switch ($editor_context->name ?? "") {
    case "core/edit-post":
      $config = Inoby_Config::allowed_blocks();
      if (is_array($config)) {
        // v child teme som povolil len niektore, tak vratim len tie
        $allowed_blocks = $config;
      } else {
        // povolujem vsetky metaboxove
        $meta_box_registry = rwmb_get_registry("field");
        $blocks = $meta_box_registry->get_by_object_type("block") ?: [];
        $allowed_blocks = [];
        foreach ($blocks as $code => $block) {
          $allowed_blocks[] = "meta-box/{$code}";
        }
      }
      // povolujem reusable blocks
      $allowed_blocks[] = "core/block";
      // povolujem shortcody
      $allowed_blocks[] = "core/shortcode";
      break;
    default:
      $allowed_blocks = ["core/paragraph", "core/heading", "core/html", "core/image", "core/spacer", "core/shortcode", "core/group", "core/navigation-link", "core/home-link", "core/navigation-submenu", "core/navigation", "core/post-navigation-link", "core/list", "core/list-item"];
      break;
  }
  return $allowed_blocks;
}

/*
 * Inoby weby ti urobí v admine
 */
function remove_footer_admin()
{
  echo '<span id="footer-thankyou"><a href="https://www.inoby.sk" target="_blank">Inoby</a> weby ti urobí</span>';
}

add_filter("admin_footer_text", "remove_footer_admin");