<?php
/**
 * Tento subor sa nevola priamo ale vola ho WP cez mechanizmus Must Use pluginov
 * https://wordpress.org/support/article/must-use-plugins/
 *
 * aby vsetko fungovalo treba do wp-configu pridat (este pred volanie wp-settings):
 *
 * define("WPMU_PLUGIN_DIR", ABSPATH . "wp-content/themes/inoby-theme/plugins");
 * define("WPMU_PLUGIN_URL", $_SERVER["HTTP_ORIGIN"] . "/wp-content/themes/inoby-theme/plugins");
 */

add_filter("mb_settings_pages", function ($settings_pages) {
  $tabs = [
    "help" => [
      "label" => "Nápoveda",
      "icon" => "dashicons-editor-help",
      "sort" => 10,
    ],
    "socials" => [
      "label" => "Sociálne siete",
      "icon" => "dashicons-share",
      "sort" => 30,
    ],
    "topbar" => [
      "label" => "Horná lišta",
      "icon" => "dashicons-table-row-before",
      "sort" => 40,
    ],
    "footer" => [
      "label" => "Pätička",
      "icon" => "dashicons-table-row-after",
      "sort" => 50,
    ],
    "contact" => [
      "label" => "Kontakt",
      "icon" => "dashicons-phone",
      "sort" => 60,
    ],
    "promo" => [
      "label" => "Promo lišta",
      "icon" => "dashicons-info",
      "sort" => 70,
    ],
    "advanced" => [
      "label" => "Pokročilé",
      "icon" => "dashicons-editor-code",
      "sort" => 90,
    ],
  ];

  if(Inoby_Config::adds_banner()){
    $tabs['adds-banner'] = [
        "label" => "Reklamný banner",
        "icon" => "dashicons-format-aside",
        "sort" => 20,
    ];
  }

  if(Inoby_Config::b2b()){
    $tabs['b2b'] = [
        "label" => "Nastavenia B2B",
        "icon" => "dashicons-format-aside",
        "sort" => 20,
    ];
  }

  if (const_or_default("RC_ENABLED") && Inoby_Config::cookie_bar()) {
    $tabs["cookies"] = [
      "label" => "Cookies",
      "icon" => "dashicons-art",
      "sort" => 80,
    ];
  }

  if (Inoby_Config::child_theme_settings_tab()) {
    $tabs["template"] = [
      "label" => "Vaša šablóna",
      "icon" => "dashicons-admin-appearance",
      "sort" => 15,
    ];
  }

  if (Inoby_Config::custom_theme_options()) {
    $tabs["fonts"] = [
      "label" => "Typografia",
      "icon" => "dashicons-editor-textcolor",
      "sort" => 95,
    ];
    $tabs["colors"] = [
      "label" => "Farebná schéma",
      "icon" => "dashicons-art",
      "sort" => 96,
    ];
    $tabs["layout"] = [
      "label" => "Rozloženie",
      "icon" => "dashicons-layout",
      "sort" => 97,
    ];
  }

  $tabs = apply_filters("inoby_theme_settings_tabs", $tabs);

  uasort($tabs, function ($a, $b) {
    $asort = intval($a["sort"] ?? 999);
    $bsort = intval($b["sort"] ?? 999);
    return $asort - $bsort;
  });

  $my_settings_page = [
    "id" => "options",
    "option_name" => "options",
    "menu_title" => "Nastavenie témy",
    "class" => "options-wrap",
    "parent" => "themes.php",
    "capability" => "edit_pages",
    "icon_url" => "dashicons-editor-help",
    "style" => "no-boxes",
    "tab_style" => "left",
    "submit_button" => __("Uložiť nastavenia", "inoby"),
    "message" => __("Nastavenie uložené", "inoby"),
    "columns" => 1,
    "tabs" => $tabs,
  ];

  $settings_pages[] = $my_settings_page;

  return $settings_pages;
});

/**
 * Add a settings page in admin toolbar
 */
add_action("wp_before_admin_bar_render", "wpdd_admin_bar_edit");
function wpdd_admin_bar_edit() {
  global $wp_admin_bar;
  $wp_admin_bar->add_menu([
    "parent" => "site-name", // site-name is the ID for home menu so we use it as the parent ID.
    "id" => "theme-options", // You can add any value here as you are adding something very new here.
    "title" => __("Nastavenie témy", "inoby"), // The anchor text.
    "href" => admin_url("themes.php?page=options"), // URL to link to. Don't change this. Just kidding.
  ]);
}

add_filter("rwmb_meta_boxes", function ($meta_boxes) {
  if(Inoby_Config::adds_banner()){
    $meta_boxes[] = [
      'id'  => 'mb-adds-banner',
      'title' => __("Reklamný banner", 'inoby'),
      'settings_pages'  => 'options',
      'context' => 'normal',
      'tab' => 'adds-banner',
      'fields'  => [
        [
          "id" => "adds-banner-show",
          "type" => "checkbox",
          "std" => 0,
          "name" => __("Zobraziť reklamný banner", "inoby"),
        ],
        [
            'id'    => 'adds-banner-icon',
            'name'  => __("Ikona", 'inoby'),
            'type'  => 'image_advanced',
            'max_file_uploads' => 1,
        ],
        [
            'id'    => 'adds-banner-subheading',
            'name'  => __("Podnadpis", 'inoby'),
            'type'  => 'text',
        ],
        [
            'id'    => 'adds-banner-heading',
            'name'  => __("Nadpis", 'inoby'),
            'type'  => 'text',
        ],
        [
            'id'    => 'adds-banner-btn-name',
            'name'  => __("Tlačidlo - Nadpis", 'inoby'),
            'type'  => 'text',
        ],
        [
            'id'    => 'adds-banner-btn-link',
            'name'  => __("Tlačidlo - Url", 'inoby'),
            'type'  => 'text',
        ],
      ],
    ];
  }

  if(Inoby_Config::b2b()){
    $meta_boxes[] = [
      'id'  => 'mb-b2b',
      'title' => __("B2B", 'inoby'),
      'settings_pages'  => 'options',
      'context' => 'normal',
      'tab' => 'b2b',
      'fields'  => [
        [
          "id" => "b2b-global-sale",
          "type" => "checkbox",
          "std" => 0,
          "name" => __("Advanced B2B", "inoby"),
        ],
        [
          "id" => "b2b-use-tax",
          "type" => "checkbox",
          "std" => 0,
          "name" => __("B2B cena je cena bez DPH", "inoby"),
        ],
        [
            'id'    => 'b2b-percentage',
            'name'  => __("B2B percentuálna zľava", 'inoby'),
            'type'  => 'number',
            'min' => 0,
            'max' => 100,
            'step'  => 1,
            'hidden'  => ['b2b-use-tax', true],
        ],
      ],
    ];
  }

  $meta_boxes[] = [
    "id" => "mb-help",
    "title" => "Nápoveda",
    "settings_pages" => "options",
    "context" => "normal",
    "tab" => "help",
    "fields" => [
      [
        "type" => "custom_html",
        "std" =>
          '<div class="settings-hero"><h3>Sociálne siete</h3><p>Vložte odkazy na svoje sociálne siete, ktoré sa budú zobrazovať na vašej webovej stránke.</p><a target="_blank" href="' .
          admin_url("themes.php?page=options#tab-socials") .
          '">Pre nastavenie kliknite sem</a></div>',
      ],
      [
        "type" => "divider",
      ],
      [
        "type" => "custom_html",
        "std" =>
          '<div class="settings-hero"><h3>Horná lišta</h3><p>Ak chcete zobrazovať hornú lištu na webovej stránke, vyberte rozloženie a vložte položky, ktoré chcete zobrazovať. Jednotlivým položkám môžete pridať odkazy či vlastné triedy.</p><a target="_blank" href="' .
          admin_url("themes.php?page=options#tab-topbar") .
          '">Pre nastavenie kliknite sem</a></div>',
      ],
      [
        "type" => "divider",
      ],
      [
        "type" => "custom_html",
        "std" =>
          '<div class="settings-hero"><h3>Pätička</h3><p>Pätička či copyright sú miesta, kde nájdete dôležité odkazy, či dôležité informácie o vašej webovej stránke. Vyberte si ich rozloženie.</p><a target="_blank" href="' .
          admin_url("themes.php?page=options#tab-footer") .
          '">Pre nastavenie kliknite sem</a></div>',
      ],
      [
        "type" => "divider",
      ],
      [
        "type" => "custom_html",
        "std" =>
          '<div class="settings-hero"><h3>Kontakt</h3><p>Nastavte emailovú adresu na ktorú budú chodiť dopyty z vášho kontaktného formulára.</p><a target="_blank" href="' .
          admin_url("themes.php?page=options#tab-contact") .
          '">Pre nastavenie kliknite sem</a></div>',
      ],
      [
        "type" => "divider",
      ],
      [
        "type" => "custom_html",
        "std" =>
          '<div class="settings-hero"><h3>Promo lišta</h3><p>Máte nejakú akciu alebo text, ktorý chcete zobraziť pre návštevníkov vašej webovej stránky? Použite promo lištu, túto lištu môže návštevník jednoducho skryť.</p><a target="_blank" href="' .
          admin_url("themes.php?page=options#tab-promo") .
          '">Pre nastavenie kliknite sem</a></div>',
      ],
      [
        "type" => "divider",
      ],
      [
        "type" => "custom_html",
        "std" =>
          '<div class="settings-hero"><h3>Cookies</h3><p>Súbor cookie je krátky jednoduchý súbor, ktorý sa odosiela spolu so stránkami tejto webstránky a váš prehliadač ho ukladá na pevnom disku vášho počítača alebo iného zariadenia. Informácie, ktoré sú v ňom uložené, môžu byť počas vašej ďalšej návštevy odoslané späť na naše servery alebo na servery príslušných tretích strán. Podľa rôznych špecifikácií rozlišujeme niekoľko druhov cookies. Tu nájdete nastavenie cookies lišty, ktorá je navrhnutá na mieru podľa UX auditu (analýza správania a používateľskej prívetivosti).</p><a target="_blank" href="' .
          admin_url("themes.php?page=options#tab-cookies") .
          '">Pre nastavenie kliknite sem</a></div>',
      ],
      [
        "type" => "divider",
      ],
      [
        "type" => "custom_html",
        "std" =>
          '<div class="settings-hero"><h3>Pokročilé</h3><p>Ste znalý webovým technológiám a chcete pridať vlastné skripty na vašu webovú stránku? Pozrite sa do sekcie pre pokročilých.</p><a target="_blank" href="' .
          admin_url("themes.php?page=options#tab-advanced") .
          '">Pre nastavenie kliknite sem</a></div>',
      ],
    ],
  ];

  if (Inoby_Config::custom_theme_options()) {
    // generate font cards from theme-fonts.json
    $theme_fonts_cfg = Inoby_Config::theme_fonts();
    $theme_fonts_cards = [];
    foreach ($theme_fonts_cfg as $theme_fonts_args) {
      $theme_fonts_cards[] = $theme_fonts_args["card"];
    }
    // generate color scheme cards from theme-color-schemes.json
    $theme_color_schemes_cfg = Inoby_Config::theme_color_schemes();
    $color_schemes_cards = [];
    foreach ($theme_color_schemes_cfg as $color_schemes_args) {
      $color_schemes_cards[] = $color_schemes_args["card"];
    }
    $meta_boxes[] = [
      "id" => "mb-fonts",
      "title" => "Typografia",
      "settings_pages" => "options",
      "context" => "normal",
      "tab" => "fonts",
      "fields" => [
        [
          "type" => "custom_html",
          "std" =>
            "<h1>Typografia</h1><p>Zvoľte si typografickú schému pre vašu webstránku.</p>",
        ],
        [
          "id" => "theme-fonts-select",
          "type" => "image_select",
          "options" => $theme_fonts_cards,
          "std" => 1,
        ],
        [
          "name" => __("Veľkosť písma", "inoby"),
          "id" => "theme-fonts-size",
          "type" => "slider",
          "label_description" => __(
            "Tento koeficient ovplyvňuje veľkosť písma. Odporúčaná hodnota je <strong>1</strong>.",
            "inoby",
          ),
          "js_options" => [
            "min" => 0.75,
            "max" => 2,
            "step" => 0.01,
          ],

          "std" => 0,
        ],
      ],
    ];
    $meta_boxes[] = [
      "id" => "mb-colors",
      "title" => "Farebná schéma",
      "settings_pages" => "options",
      "context" => "normal",
      "tab" => "colors",
      "fields" => [
        [
          "type" => "custom_html",
          "std" =>
            "<h1>Farebná schéma</h1><p>Zvoľte si farebnú schému pre vašu webstránku.</p>",
        ],
        [
          "id" => "theme-color-scheme-select",
          "type" => "image_select",
          "options" => $color_schemes_cards,
          "std" => 0,
        ],
      ],
    ];
    $meta_boxes[] = [
      "id" => "mb-layout",
      "title" => "Rozloženie",
      "settings_pages" => "options",
      "context" => "normal",
      "tab" => "layout",
      "fields" => [
        [
          "type" => "custom_html",
          "std" =>
            "<h1>Rozloženie</h1><p>Zvoľte si rozloženie pre vašu webstránku.</p>",
        ],
        [
          "type" => "custom_html",
          "std" => "<h3>Logo</h3><p>Nahrajte hlavné logo</p>",
        ],
        [
          "id" => "main-logo",
          "title" => __("Obrázok", "inoby"),
          "type" => "image_advanced",
          "force_delete" => false,
          "max_file_uploads" => 1,
          "max_status" => false,
          "image_size" => "thumbnail",
        ],
        [
          "type" => "custom_html",
          "std" =>
            "<h4>Svetlá verzia loga</h4><p>Nahrajte svetlú verziu loga. Používa sa na tmavom pozadí.</p>",
        ],
        [
          "id" => "light-logo",
          "title" => __("Obrázok", "inoby"),
          "type" => "image_advanced",
          "force_delete" => false,
          "max_file_uploads" => 1,
          "max_status" => false,
          "image_size" => "thumbnail",
        ],
      ],
    ];
  }

  $meta_boxes[] = [
    "id" => "mb-socials",
    "title" => "Sociálne siete",
    "settings_pages" => "options",
    "context" => "normal",
    "tab" => "socials",
    "fields" => [
      [
        "name" => "Facebook",
        "id" => "social-facebook",
        "type" => "url",
        "placeholder" => "Tu vložte odkaz",
      ],
      [
        "name" => "Instagram",
        "id" => "social-instagram",
        "type" => "url",
        "placeholder" => "Tu vložte odkaz",
      ],
      [
        "name" => "Youtube",
        "id" => "social-youtube",
        "type" => "url",
        "placeholder" => "Tu vložte odkaz",
      ],
      [
        "name" => "Vimeo",
        "id" => "social-vimeo",
        "type" => "url",
        "placeholder" => "Tu vložte odkaz",
      ],
      [
        "name" => "WhatsApp",
        "id" => "social-whatsapp",
        "type" => "url",
        "placeholder" => "Tu vložte odkaz",
      ],
      [
        "name" => "Twitter",
        "id" => "social-twitter",
        "type" => "url",
        "placeholder" => "Tu vložte odkaz",
      ],
      [
        "name" => "LinkedIn",
        "id" => "social-linkedin",
        "type" => "url",
        "placeholder" => "Tu vložte odkaz",
      ],
      [
        "name" => "Pinterest",
        "id" => "social-pinterest",
        "type" => "url",
        "placeholder" => "Tu vložte odkaz",
      ],
      [
        "name" => "Apple Podcasts",
        "id" => "social-apple-podcasts",
        "type" => "url",
        "placeholder" => "Tu vložte odkaz",
      ],
      [
        "name" => "Google Podcasts",
        "id" => "social-google-podcasts",
        "type" => "url",
        "placeholder" => "Tu vložte odkaz",
      ],
      [
        "name" => "Spotify",
        "id" => "social-spotify",
        "type" => "url",
        "placeholder" => "Tu vložte odkaz",
      ],
      [
        "name" => "Soundcloud",
        "id" => "social-soundcloud",
        "type" => "url",
        "placeholder" => "Tu vložte odkaz",
      ],
      [
        "name" => "Podbean",
        "id" => "social-podbean",
        "type" => "url",
        "placeholder" => "Tu vložte odkaz",
      ],
      [
        "name" => "Anchor.fm",
        "id" => "social-anchor-fm",
        "type" => "url",
        "placeholder" => "Tu vložte odkaz",
      ],
    ],
  ];

  $meta_boxes[] = [
    "id" => "mb-options",
    "title" => "Pokročilé",
    "settings_pages" => "options",
    "context" => "normal",
    "tab" => "advanced",
    "fields" => [
      [
        "name" => "Pridať fonty",
        "add_button" => __("Pridať ďalší", "inoby"),
        "label_description" => "Google fonts",
        "id" => "theme-fonts",
        "desc" =>
          "Napríklad: https://fonts.googleapis.com/css2?family=Poppins:wght@400;600;700&display=swap",
        "type" => "text",
        "clone" => true,
        "placeholder" => "Vložiť odkaz",
      ],
      [
        "name" => __("Vlastné skripty", "inoby"),
        "label_description" => "Vkladajú sa do hlavičky (head) webu",
        "id" => "theme-custom-head-scripts",
        "type" => "text",
        "clone" => true,
        "add_button" => __("Pridať ďalší", "inoby"),
        "sanitize_callback" => "none",
        "placeholder" => "Vložiť script",
      ],
      [
        "type" => "custom_html",
        "std" => "<h1>SMTP</h1><p>Nastavenia servera odchádzajúcej pošty.</p>",
      ],
      [
        "name" => __("E-mailová adresa", "inoby"),
        "desc" => "E-mailová adresa, ktorú chcete nastaviť",
        "id" => "theme_smtp_user",
        "type" => "email",
        "placeholder" => "Zadajte e-mailovú adresu",
      ],
      [
        "name" => __("Heslo", "inoby"),
        "desc" => "Heslo priradené k vášmu e-mailovému kontu",
        "id" => "theme_smtp_pass",
        "type" => "text",
        "placeholder" => "Zadajte heslo",
      ],
      [
        "name" => __("Názov hostiteľa SMTP", "inoby"),
        "desc" =>
          "Názov servera odchádzajúcej pošty. Najčastejšie smtp.vasposkytovatel.com. Napríklad teda smtp.websupport.sk alebo smtp.office365.com",
        "id" => "theme_smtp_host",
        "type" => "text",
        "placeholder" => "Zadajte názov servera",
      ],
      [
        "name" => __("Port", "inoby"),
        "desc" =>
          "Číslo portu, ktorý používa server prichádzajúcej pošty. Väčšina používa port 465",
        "id" => "theme_smtp_port",
        "type" => "number",
        "placeholder" => "Zadajte port",
      ],
      [
        "name" => __("Zabezpečenie", "inoby"),
        "id" => "theme_smtp_secure",
        "type" => "radio",
        "inline" => true,
        "options" => [
          "ssl" => "SSL",
          "tls" => "TLS",
        ],
      ],
      [
        "name" => __("SMTP Authentication", "inoby"),
        "id" => "theme_smtp_auth",
        "type" => "switch",
        "style" => "rounded",
        "on_label" => "Áno",
        "off_label" => "Nie",
      ],
      [
        "name" => __("SMTP debug", "inoby"),
        "id" => "theme_smtp_debug",
        "type" => "switch",
        "style" => "rounded",
        "on_label" => "Áno",
        "off_label" => "Nie",
      ],
      [
        "type" => "custom_html",
        "std" => "<h1>MAILCHIMP</h1><p>Pripojenie na mailchimp.</p>",
      ],
      [
        "name" => __("MAILCHIMP_REGION", "inoby"),
        "id" => "theme_mailchimp_region",
        "type" => "text",
        "placeholder" => "Zadajte MAILCHIMP_REGION",
      ],
      [
        "name" => __("MAILCHIMP_LIST_ID", "inoby"),
        "id" => "theme_mailchimp_list_id",
        "type" => "text",
        "placeholder" => "Zadajte MAILCHIMP_LIST_ID",
      ],
      [
        "name" => __("MAILCHIMP_API_KEY", "inoby"),
        "id" => "theme_mailchimp_api_key",
        "type" => "text",
        "placeholder" => "Zadajte MAILCHIMP_API_KEY",
      ],
      [
        "type" => "custom_html",
        "std" => "<h1>reCAPTCHA</h1><p>Nastavenia Google reCAPTCHA v3 pre ochranu formulárov pred spamom. Kľúče získate na <a href='https://www.google.com/recaptcha/admin' target='_blank'>Google reCAPTCHA Admin</a>.</p>",
      ],
      [
        "name" => __("reCAPTCHA Site Key", "inoby"),
        "desc" => "Verejný kľúč (Site Key) z Google reCAPTCHA",
        "id" => "theme_recaptcha_site_key",
        "type" => "text",
        "placeholder" => "Zadajte reCAPTCHA Site Key",
      ],
      [
        "name" => __("reCAPTCHA Secret Key", "inoby"),
        "desc" => "Súkromný kľúč (Secret Key) z Google reCAPTCHA",
        "id" => "theme_recaptcha_secret_key",
        "type" => "text",
        "placeholder" => "Zadajte reCAPTCHA Secret Key",
      ],
    ],
  ];

  $meta_boxes[] = [
    "id" => "mb-topbar",
    "title" => "Horná lišta",
    "settings_pages" => "options",
    "tab" => "topbar",
    "context" => "normal",
    "fields" => [
      [
        "id" => "mb-topbar-layout",
        "name" => "Rozloženie",
        "type" => "select",
        "options" => [
          "topbar-none" => "Bez hornej lišty",
          "topbar-left" => "Len obsah vľavo",
          "topbar-center" => "Len obsah v strede",
          "topbar-right" => "Len obsah vpravo",
          "topbar-both" => "Obsah vľavo a vpravo",
        ],
      ],
      [
        "id" => "topbar-items-left",
        "name" => "Položky vľavo",
        "type" => "text_list",
        "clone" => true,
        "sort_clone" => true,
        "add_button" => "Pridať položku",
        "options" => [
          "Napr: Poštovné zdarma" => "Text",
          "/postovne-zdarma/" => "Odkaz",
          "" => "Vlastná trieda",
        ],
        "visible" => ["mb-topbar-layout", "in", ["topbar-left", "topbar-both"]],
      ],
      [
        "id" => "topbar-items-center",
        "name" => "Položky v strede",
        "type" => "text_list",
        "clone" => true,
        "sort_clone" => true,
        "add_button" => "Pridať položku",
        "options" => [
          "Napr: Poštovné zdarma" => "Text",
          "/postovne-zdarma/" => "Odkaz",
          "" => "Vlastná trieda",
        ],
        "visible" => ["mb-topbar-layout", "in", ["topbar-center"]],
      ],
      [
        "id" => "topbar-items-right",
        "name" => "Položky vpravo",
        "type" => "text_list",

        "clone" => true,
        "sort_clone" => true,
        "add_button" => "Pridať položku",
        "options" => [
          "Napr: Poštovné zdarma" => "Text",
          "/postovne-zdarma/" => "Odkaz",
          "" => "Vlastná trieda",
        ],
        "visible" => [
          "mb-topbar-layout",
          "in",
          ["topbar-right", "topbar-both"],
        ],
      ],
    ],
  ];

  $meta_boxes[] = [
    "id" => "mb-footer",
    "title" => "Pätička",
    "settings_pages" => "options",
    "tab" => "footer",
    "context" => "normal",
    "label_description" => "Label description",
    "fields" => [
      [
        "id" => "mb-footer-layout",
        "name" => "Rozloženie pätičky",
        "type" => "select",
        "options" => [
          "footer-none" => "Bez pätičky",
          "footer-one" => "Jeden stĺpec",
          "footer-two" => "Dva stĺpce",
          "footer-three" => "Tri stĺpce",
          "footer-four" => "Štyri stĺpce",
          "footer-five" => "Päť stĺpcov",
        ],
      ],
      [
        "id" => "mb-copyright-layout",
        "name" => "Rozloženie copyrightu",
        "type" => "select",
        "options" => [
          "copyright-none" => "Bez copyrightu",
          "copyright-left-and-right" => "Copyright s obsahom vľavo aj vpravo",
          "copyright-center" => "Copyright s obsahom v strede",
          "copyright-left-right-center" =>
            "Copyright s obsahom vľavo v strede aj vpravo",
        ],
      ],
      [
        "type" => "custom_html",
        "std" =>
          '<p>Vyberte rozloženie, pätičku a copyright následne môžete upravovať vo widgetoch. <a target="_blank" href="' .
          admin_url("widgets.php") .
          '">Pre úpravu kliknite sem</a></p>',
      ],
    ],
  ];

  return $meta_boxes;
});
