<?php

// generate image sizes from image_sizes.json
$image_sizes_cfg = Inoby_Config::image_sizes();
foreach ($image_sizes_cfg as $image_size) {
  add_image_size(
    $image_size["name"],
    $image_size["width"] ?? 0,
    $image_size["height"] ?? 0,
    $image_size["crop"] ?? false,
  );
}

function inoby_theme_setup()
{
  add_theme_support("menus");
  add_theme_support("title-tag");
  add_theme_support("post-thumbnails");

  register_nav_menus([
    "primary" => "Primary Navigation",
    "mobile" => "Mobile Navigation",
  ]);

  if (ESHOP_ENABLED) {
    add_theme_support("woocommerce");
  }
}

function extend_editor_caps()
{
  // gets the editor role
  $roleObject = get_role("editor");

  if (!$roleObject->has_cap("edit_theme_options")) {
    $roleObject->add_cap("edit_theme_options");
  }
}
add_action("admin_init", "extend_editor_caps");

add_action("after_setup_theme", "inoby_theme_setup");

if (Inoby_Config::show_footer()) {
  function inoby_widgets_init()
  {
    register_sidebar([
      "name" => __("Pätička - Stĺpec 1", "inoby"),
      "id" => "footer-1",
      "before_widget" => '<div class="footer-column-wrap footer-column-1">',
      "after_widget" => "</div>",
      "before_title" => "<h3>",
      "after_title" => "</h3>",
    ]);

    register_sidebar([
      "name" => __("Pätička - Stĺpec 2", "inoby"),
      "id" => "footer-2",
      "before_widget" => '<div class="footer-column-wrap footer-column-2">',
      "after_widget" => "</div>",
      "before_title" => "<h3>",
      "after_title" => "</h3>",
    ]);

    register_sidebar([
      "name" => __("Pätička - Stĺpec 3", "inoby"),
      "id" => "footer-3",
      "before_widget" => '<div class="footer-column-wrap footer-column-3">',
      "after_widget" => "</div>",
      "before_title" => "<h3>",
      "after_title" => "</h3>",
    ]);

    register_sidebar([
      "name" => __("Pätička - Stĺpec 4", "inoby"),
      "id" => "footer-4",
      "before_widget" => '<div class="footer-column-wrap footer-column-4">',
      "after_widget" => "</div>",
      "before_title" => "<h3>",
      "after_title" => "</h3>",
    ]);

    register_sidebar([
      "name" => __("Pätička - Stĺpec 5", "inoby"),
      "id" => "footer-5",
      "before_widget" => '<div class="footer-column-wrap footer-column-5">',
      "after_widget" => "</div>",
      "before_title" => "<h3>",
      "after_title" => "</h3>",
    ]);

    register_sidebar([
      "name" => __("Copyright - Ľavá časť", "inoby"),
      "id" => "copyright-left",
      "before_widget" => '<div class="copyright-column-wrap copyright-left">',
      "after_widget" => "</div>",
      "before_title" => "<h3>",
      "after_title" => "</h3>",
    ]);

    register_sidebar([
      "name" => __("Copyright - Stred", "inoby"),
      "id" => "copyright-center",
      "before_widget" => '<div class="copyright-column-wrap copyright-center">',
      "after_widget" => "</div>",
      "before_title" => "<h3>",
      "after_title" => "</h3>",
    ]);

    register_sidebar([
      "name" => __("Copyright - Pravá časť", "inoby"),
      "id" => "copyright-right",
      "before_widget" => '<div class="copyright-column-wrap copyright-right">',
      "after_widget" => "</div>",
      "before_title" => "<h3>",
      "after_title" => "</h3>",
    ]);
  }

  add_action("widgets_init", "inoby_widgets_init");
}

if (RC_ENABLED) {
  function inoby_checkout_settings($settings)
  {
    $settings["wrp_classes"] = "checkout-form-wrp container";
    $settings["tabs_classes"] = "col-12 tabs-column";
    $settings["step_classes"] = "col-8 col-lg-12 checkout-left-column";
    $settings["totals_classes"] = "col-4 col-lg-12 checkout-right-column";
    $settings["cross_sell_classes"] = "col-12 col-lg-12 cart-cross-sell-wrp";
    return $settings;
  }

  add_filter("rc_checkout_settings", "inoby_checkout_settings");
}

// Shortcode for Socials
function socials_shortcode($atts)
{
  $social_facebook = rwmb_meta(
    "social-facebook",
    ["object_type" => "setting"],
    "options",
  );
  $social_instagram = rwmb_meta(
    "social-instagram",
    ["object_type" => "setting"],
    "options",
  );
  $social_youtube = rwmb_meta(
    "social-youtube",
    ["object_type" => "setting"],
    "options",
  );
  $social_vimeo = rwmb_meta(
    "social-vimeo",
    ["object_type" => "setting"],
    "options",
  );
  $social_whatsapp = rwmb_meta(
    "social-whatsapp",
    ["object_type" => "setting"],
    "options",
  );
  $social_twitter = rwmb_meta(
    "social-twitter",
    ["object_type" => "setting"],
    "options",
  );
  $social_linkedin = rwmb_meta(
    "social-linkedin",
    ["object_type" => "setting"],
    "options",
  );
  $social_pinterest = rwmb_meta(
    "social-pinterest",
    ["object_type" => "setting"],
    "options",
  );
  $social_apple_podcasts = rwmb_meta(
    "social-apple-podcasts",
    ["object_type" => "setting"],
    "options",
  );
  $social_google_podcasts = rwmb_meta(
    "social-google-podcasts",
    ["object_type" => "setting"],
    "options",
  );
  $social_spotify = rwmb_meta(
    "social-spotify",
    ["object_type" => "setting"],
    "options",
  );
  $social_soundcloud = rwmb_meta(
    "social-soundcloud",
    ["object_type" => "setting"],
    "options",
  );
  $social_podbean = rwmb_meta(
    "social-podbean",
    ["object_type" => "setting"],
    "options",
  );
  $social_anchor_fm = rwmb_meta(
    "social-anchor-fm",
    ["object_type" => "setting"],
    "options",
  );

  $a = shortcode_atts(
    [
      "style" => "icons", //or buttons
    ],
    $atts,
  );

  $fb = "Facebook";
  $ig = "Instagram";
  $yt = "YouTube";
  $vi = "Vimeo";
  $wa = "WhatsApp";
  $tw = "Twitter";
  $li = "LinkedIn";
  $pi = "Pinterest";
  $ap = "Apple Podcasts";
  $gp = "Google Podcasts";
  $sp = "Spotify";
  $sc = "Soundcloud";
  $pb = "Podbean";
  $an = "Anchor.fm";

  ob_start();

  echo '<div class="socials ' . $a["style"] . '">';
  $class = "";
  if ($a["style"] == "buttons") {
    $class = 'class="button-outline"';
  }
  if ($a["style"] == "icons") {
    $fb = "";
    $ig = "";
    $yt = "";
    $vi = "";
    $wa = "";
    $tw = "";
    $li = "";
    $pi = "";
    $ap = "";
    $gp = "";
    $sp = "";
    $sc = "";
    $pb = "";
    $an = "";
  }
  if (!empty($social_facebook)) {
    echo "<a " .
      $class .
      ' href="' .
      $social_facebook .
      '" target="_blank"><img src="' .
      get_theme_file_uri("/assets/icons/facebook.svg") .
      '" class="social-icon social-icon-facebook" alt="Facebook" width="24" height="24" loading="lazy">' .
      $fb .
      "</a>";
  }
  if (!empty($social_instagram)) {
    echo "<a " .
      $class .
      ' href="' .
      $social_instagram .
      '" target="_blank"><img src="' .
      get_theme_file_uri("/assets/icons/instagram.svg") .
      '" class="social-icon social-icon-instagram" alt="Instagram" width="24" height="24" loading="lazy">' .
      $ig .
      "</a>";
  }
  if (!empty($social_youtube)) {
    echo "<a " .
      $class .
      ' href="' .
      $social_youtube .
      '" target="_blank"><img src="' .
      get_theme_file_uri("/assets/icons/youtube.svg") .
      '" class="social-icon social-icon-youtube" alt="YouTube" width="24" height="24" loading="lazy">' .
      $yt .
      "</a>";
  }
  if (!empty($social_vimeo)) {
    echo "<a " .
      $class .
      ' href="' .
      $social_vimeo .
      '" target="_blank"><img src="' .
      get_theme_file_uri("/assets/icons/vimeo.svg") .
      '" class="social-icon social-icon-vimeo" alt="' .
      $vi .
      '" width="24" height="24" loading="lazy">' .
      $vi .
      "</a>";
  }
  if (!empty($social_whatsapp)) {
    echo "<a " .
      $class .
      ' href="' .
      $social_whatsapp .
      '" target="_blank"><img src="' .
      get_theme_file_uri("/assets/icons/whatsapp.svg") .
      '" class="social-icon social-icon-whatsapp" alt="WhatsApp" width="24" height="24" loading="lazy">' .
      $wa .
      "</a>";
  }
  if (!empty($social_twitter)) {
    echo "<a " .
      $class .
      ' href="' .
      $social_twitter .
      '" target="_blank"><img src="' .
      get_theme_file_uri("/assets/icons/twitter.svg") .
      '" class="social-icon social-icon-twitter" alt="Twitter" width="24" height="24" loading="lazy">' .
      $tw .
      "</a>";
  }
  if (!empty($social_linkedin)) {
    echo "<a " .
      $class .
      ' href="' .
      $social_linkedin .
      '" target="_blank"><img src="' .
      get_theme_file_uri("/assets/icons/linkedin.svg") .
      '" class="social-icon social-icon-linkedin" alt="LinkedIn" width="24" height="24" loading="lazy">' .
      $li .
      "</a>";
  }
  if (!empty($social_pinterest)) {
    echo "<a " .
      $class .
      ' href="' .
      $social_pinterest .
      '" target="_blank"><img src="' .
      get_theme_file_uri("/assets/icons/pinterest.svg") .
      '" class="social-icon social-icon-pinterest" alt="Pinterest" width="24" height="24" loading="lazy">' .
      $pi .
      "</a>";
  }
  if (!empty($social_apple_podcasts)) {
    echo "<a " .
      $class .
      ' href="' .
      $social_apple_podcasts .
      '" target="_blank"><img src="' .
      get_theme_file_uri("/assets/icons/apple-podcasts.svg") .
      '" class="social-icon social-icon-apple-podcasts" alt="' .
      $ap .
      '" width="24" height="24" loading="lazy">' .
      $ap .
      "</a>";
  }
  if (!empty($social_google_podcasts)) {
    echo "<a " .
      $class .
      ' href="' .
      $social_google_podcasts .
      '" target="_blank"><img src="' .
      get_theme_file_uri("/assets/icons/google-podcasts.svg") .
      '" class="social-icon social-icon-google-podcasts" alt="' .
      $gp .
      '" width="24" height="24" loading="lazy">' .
      $gp .
      "</a>";
  }
  if (!empty($social_spotify)) {
    echo "<a " .
      $class .
      ' href="' .
      $social_spotify .
      '" target="_blank"><img src="' .
      get_theme_file_uri("/assets/icons/spotify.svg") .
      '" class="social-icon social-icon-spotify" alt="' .
      $sp .
      '" width="24" height="24" loading="lazy">' .
      $sp .
      "</a>";
  }
  if (!empty($social_soundcloud)) {
    echo "<a " .
      $class .
      ' href="' .
      $social_soundcloud .
      '" target="_blank"><img src="' .
      get_theme_file_uri("/assets/icons/soundcloud.svg") .
      '" class="social-icon social-icon-soundcloud" alt="' .
      $sc .
      '" width="24" height="24" loading="lazy">' .
      $sc .
      "</a>";
  }
  if (!empty($social_podbean)) {
    echo "<a " .
      $class .
      ' href="' .
      $social_podbean .
      '" target="_blank"><img src="' .
      get_theme_file_uri("/assets/icons/podbean.svg") .
      '" class="social-icon social-icon-podbean" alt="' .
      $pb .
      '" width="24" height="24" loading="lazy">' .
      $pb .
      "</a>";
  }
  if (!empty($social_anchor_fm)) {
    echo "<a " .
      $class .
      ' href="' .
      $social_anchor_fm .
      '" target="_blank"><img src="' .
      get_theme_file_uri("/assets/icons/anchor.svg") .
      '" class="social-icon social-icon-anchor" alt="' .
      $an .
      '" width="24" height="24" loading="lazy">' .
      $an .
      "</a>";
  }
  echo "</div>";

  return ob_get_clean();
}
add_shortcode("socials", "socials_shortcode");

// Shortcode for Socials
function share_post_on_socials_shortcode()
{
  ob_start();

  if (is_singular()) {

    $post_title = str_replace(" ", "%20", get_the_title());
    $post_url = get_permalink();

    $twitter =
      "https://twitter.com/intent/tweet?text=" .
      $post_title .
      "&amp;url=" .
      $post_url .
      "&amp;";
    $facebook = "https://www.facebook.com/sharer.php?u=" . $post_url;
    $linkedIn =
      "https://www.linkedin.com/shareArticle?mini=true&url=" .
      $post_url .
      "&amp;title=" .
      $post_title;
?>

    <div class="post-share-wrap">
      <span id="share-trigger"><?= __("Zdieľať", "inoby") ?></span>
      <ul class="social-share">
        <li><a class="twitter-share" href="<?= $twitter ?>" title="Tweet on Twitter" target="_blank" rel="nofollow"><img
              src="<?= get_theme_file_uri(
                      "/assets/icons/twitter.svg",
                    ) ?>" class="social-icon social-icon-twitter" alt="Twitter" width="24" height="24"
              loading="lazy">Twitter</a>
        </li>
        <li><a class="facebook-share" href="<?= $facebook ?>" title="Share on facebook" target="_blank" rel="nofollow"><img
              src="<?= get_theme_file_uri(
                      "/assets/icons/facebook.svg",
                    ) ?>" class="social-icon social-icon-facebook" alt="Facebook" width="24" height="24"
              loading="lazy">Facebook</a></li>
        <li><a class="linkedin-share" href="<?= $linkedIn ?>" title="Share on LinkedIn" target="_blank" rel="nofollow"><img
              src="<?= get_theme_file_uri(
                      "/assets/icons/linkedin.svg",
                    ) ?>" class="social-icon social-icon-linkedin" alt="LinkedIn" width="24" height="24"
              loading="lazy">LinkedIn</a></li>
      </ul>
    </div>
<?php
  }
  return ob_get_clean();
}
add_shortcode("post_share", "share_post_on_socials_shortcode");


function inoby_contact_info_shortcode($atts) {
    $atts = shortcode_atts(
        [
            "email" => "",
            "phone" => "",
        ],
        $atts,
        "inoby_contact_info",
    );
    $contact_info = "";
    if (!empty($atts["email"])) {
        $contact_info .=
            '<a class="email-btn" href="mailto:' .
            esc_attr($atts["email"]) .
            '">' .
            file_get_contents(get_stylesheet_directory_uri() . "/assets/svg/mail.svg") .
            esc_html($atts["email"]) .
            "</a>";
    }
    if (!empty($atts["phone"])) {
        $contact_info .=
            '<a class="phone-btn" href="tel:' .
            esc_attr($atts["phone"]) .
            '">' .
            file_get_contents(get_stylesheet_directory_uri() . "/assets/svg/phone-call.svg") .
            esc_html($atts["phone"]) .
            "</a>";
    }

    return '<div class="contact-info">' . $contact_info . "</div>";
}
add_shortcode("inoby_contact_info", "inoby_contact_info_shortcode");

require_once INOBY_BASE . "/inoby-plugins/cart-upsell/cart-upsell.php";
require_once INOBY_BASE . "/inoby-plugins/minicart/minicart.php";
require_once INOBY_BASE . "/inoby-plugins/newsletter/newsletter.php";
require_once INOBY_BASE . "/inoby-plugins/contact-form/contact-form.php";
require_once INOBY_BASE .
  "/inoby-plugins/wpbs-order-details/wpbs-order-details.php";
require_once INOBY_BASE . "/inoby-plugins/wpbs-search/wpbs-search.php";
require_once INOBY_BASE .
  "/inoby-plugins/wpbs-form-extend/wpbs-form-extend.php";
require_once INOBY_BASE . "/inoby-plugins/wpbs/index.php";
require_once INOBY_BASE . "/inoby-plugins/promo-banner/promo-banner.php";
require_once INOBY_BASE . "/inoby-plugins/wc-gift-cards/wc-gift-cards.php";
require_once INOBY_BASE . "/inoby-plugins/cookies/cookies.php";
require_once INOBY_BASE .
  "/inoby-plugins/wpbs-calendar-drag/wpbs-calendar-drag.php";
require_once INOBY_BASE . "/inoby-plugins/mega-menu/mega-menu.php";
require_once INOBY_BASE . "/inoby-plugins/filter-and-pagination/index.php";
if (ESHOP_ENABLED && Inoby_Config::wishlist()) {
  require_once INOBY_BASE . "/inoby-plugins/wishlist/wishlist.php";
}
if (ESHOP_ENABLED && Inoby_Config::free_shipping_notice()) {
  require_once INOBY_BASE . "/inoby-plugins/free-shipping/free-shipping-notice.php";
}
if (ESHOP_ENABLED && Inoby_Config::b2b()) {
  require_once INOBY_BASE . "/inoby-plugins/b2b/b2b.php";
}

if (ESHOP_ENABLED && Inoby_Config::advanced_order_by_wrap()) {
  require_once INOBY_BASE . "/inoby-plugins/order-by-wrap/order-by-wrap.php";
}


if (ESHOP_ENABLED && Inoby_Config::cart_products()) {
  
  function create_cart_products_blocks() {
      // Check if post already exists
      $existing_minicart_products = get_page_by_title('Produkty minicart', OBJECT, 'wp_block');
      
      if (!$existing_minicart_products) {
          $post_data = array(
              'post_title'    => "Produkty minicart",
              'post_status'   => 'publish',
              'post_type'     => 'wp_block',
              'post_author'   => 1,
          );
          wp_insert_post($post_data);
      }

      // Check if post already exists
      $existing_novinky_kosik = get_page_by_title('Novinky kosik', OBJECT, 'wp_block');
      
      if (!$existing_novinky_kosik) {
          $post_data = array(
              'post_title'    => "Novinky kosik",
              'post_status'   => 'publish',
              'post_type'     => 'wp_block',
              'post_author'   => 1,
          );
          wp_insert_post($post_data);
      }
  }
  add_action('init', 'create_cart_products_blocks');

  function inoby_minicart_products(){
    rwmb_meta_render_reusable('produkty-minicart');
  }

  add_action('woocommerce_after_popup_content', 'inoby_minicart_products');
}


if (ESHOP_ENABLED && Inoby_Config::icon_benefits()) {
  
  function create_icon_benefits_blocks() {
      // Check if post already exists
      $existing_minicart_products = get_page_by_title('Benefity produkt', OBJECT, 'wp_block');
      
      if (!$existing_minicart_products) {
          $post_data = array(
              'post_title'    => "Benefity produkt",
              'post_status'   => 'publish',
              'post_type'     => 'wp_block',
              'post_author'   => 1,
          );
          wp_insert_post($post_data);
      }

      // Check if post already exists
      $existing_novinky_kosik = get_page_by_title('Benefity kosik', OBJECT, 'wp_block');
      
      if (!$existing_novinky_kosik) {
          $post_data = array(
              'post_title'    => "Benefity kosik",
              'post_status'   => 'publish',
              'post_type'     => 'wp_block',
              'post_author'   => 1,
          );
          wp_insert_post($post_data);
      }
  }
  add_action('init', 'create_icon_benefits_blocks');

} 
