<?php

/**
 * Version: 2.5
 *
 *  Novo tratamento dos campos repetidos, Agora, não é mais exibido um por default, ele aparece vazio com textxo indicando que não tem itens adicinados.
 * 
 */

define('O2M_VERSION', '2.5');

function o2_import_files($path) {
  // Obter a lista de arquivos PHP no diretório especificado
  $arquivos = glob(__DIR__ . $path . '/*.php');

  // Incluir cada arquivo encontrado
  foreach ($arquivos as $arquivo) {
    if (basename($arquivo)[0] != '_')
      require_once $arquivo;
  }
}

function o2_import_scripts_admin() {

  /* Chosen */
  wp_enqueue_style('chosen-css',  get_template_directory_uri() . '/app/o2m/include/chosen/chosen.min.css');
  wp_enqueue_script('chosen-js', get_template_directory_uri() . '/app/o2m/include/chosen/chosen.jquery.min.js', ['jquery'], null, true);

  /* Spectrum */
  wp_enqueue_style('spectrum-css', get_template_directory_uri() . '/app/o2m/include/spectrum/spectrum.css', []);
  wp_enqueue_script('spectrum-js', get_template_directory_uri() . '/app/o2m/include/spectrum/spectrum.js', ['jquery'], null, true);

  /* Main */
  wp_enqueue_style('o2m-style', get_template_directory_uri() . '/app/o2m/style.css', [], O2M_VERSION);
  wp_enqueue_script('o2m-script', get_template_directory_uri() . '/app/o2m/js/script.js', ['jquery'], O2M_VERSION, true);
}

function o2_import_files_admin() {
  o2_import_files('/src');
  o2_import_files('/src/controllers');
  o2_import_files('/src/fields');
  o2_import_files('/src/frontend');
  o2_import_files('/src/functions');

  require __DIR__ . "/ajax.php";

  add_filter('script_loader_tag', function ($tag, $handle) {
    if ($handle === 'o2m-script') {
      $tag = str_replace(' src', ' type="module" src', $tag);
    }
    return $tag;
  }, 10, 2);

  /* Imports Head */
  add_action('admin_head', 'o2_import_scripts_admin');

  /* Modals */
  add_action('admin_footer', function () {
    require_once __DIR__ . '/src/modal/link.php';
  });


  /**
   * Metaboxes depois do titulo
   */
  add_action('edit_form_after_title', function () {
    # Get the globals:
    global $post, $wp_meta_boxes;

    # Output the "advanced" meta boxes:
    do_meta_boxes(get_current_screen(), 'after_title', $post);

    # Remove the initial "advanced" meta boxes:
    unset($wp_meta_boxes['post']['after_title']);
  });

  /**
   * Altera o tamanho do editor
   */
  function o2_custom_wp_editor_settings($settings) {
    $settings['tinymce']['height'] = 300; // Ajuste a altura conforme necessário
    return $settings;
  }
  add_filter('wp_editor_settings', 'o2_custom_wp_editor_settings');

  /**
   * Verificar se as dependencias foram carregadas
   */
  if (!did_action('wp_enqueue_media')) {
    add_action('admin_enqueue_scripts', function () {
      wp_enqueue_media();
    });
  }

  /**
   * Carregar outras dependencias
   * 
   */
  wp_enqueue_editor();
}

function o2_import_files_frontend() {
  require_once get_template_directory() . '/app/o2m/src/Helper.php';
  require_once get_template_directory() . '/app/o2m/src/controllers/ArrayHandlerController.php';
  require_once get_template_directory() . '/app/o2m/src/frontend/ArrayHandler.php';
  require_once get_template_directory() . '/app/o2m/src/functions/frontend.php';
}


if (is_admin())
  o2_import_files_admin();
else
  o2_import_files_frontend();
