<?php

/**
 * This file adds functions to the NewSpare WordPress theme.
 *
 * @package NewSpare
 * @author  AF themes
 * @license GNU General Public License v2 or later
 * @link    https://afthemes.com/
 */

if (!function_exists('newspare_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.0.1
   *
   * @return void
   */
  function newspare_setup()
  {

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

    // 1. Core Block Theme defaults are already on. 
    // We only need to tell WP where our specific CSS lives.
    add_editor_style('style.css');

    // 2. These are the only "Classic" features still needed in FSE.
    add_theme_support('automatic-feed-links');

    // 3. WooCommerce: Mandatory as it's a plugin, not core.
    add_theme_support('woocommerce', [
      'single_image_width'    => 940, // Matches your theme.json contentSize exactly
      'thumbnail_image_width' => 450, // Perfect for 2 or 3 columns without bloat
      'product_grid'          => [
        'default_rows'    => 3,
        'min_rows'        => 2,
        'max_rows'        => 6,
        'default_columns' => 3,
        'min_columns'     => 2,
        'max_columns'     => 4,
      ],
    ]);
    add_theme_support('wc-product-gallery-zoom');
    add_theme_support('wc-product-gallery-lightbox');
    add_theme_support('wc-product-gallery-slider');
  }
}
add_action('after_setup_theme', 'newspare_setup');

// Disable remote pattern directory
add_filter('should_load_remote_block_patterns', '__return_false');

// Enqueue style sheet.
add_action('wp_enqueue_scripts', 'newspare_enqueue_style_sheet');
function newspare_enqueue_style_sheet()
{
  $min = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min';
  
  wp_enqueue_style('newspare', get_template_directory_uri() . '/style.css', array(), wp_get_theme()->get('Version'));
}


/**
 * Register Block Pattern Category
 */

// Hook
add_action('init', 'newspare_block_pattern_category');

// Callback
if (!function_exists('newspare_block_pattern_category')) :
  function newspare_block_pattern_category()
  {

    register_block_pattern_category(
      'newspare-patterns',
      array(
        'label'       => __('NewSpare Patterns', 'newspare'),
        'description' => __('Patterns for NewSpare Theme', 'newspare'),
      )
    );
  }
endif;

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

  $block_styles = array(


    'core/list' => array(
      'secondary' => __('Secondary', 'newspare'),

    ),
    'core/navigation-link' => array(
      'outline' => __('Outline', 'newspare'),
    ),
    'core/quote' => array(
      'secondary' => __('Secondary', 'newspare'),

    ),
    'core/pullquote' => array(
      'secondary' => __('Secondary', 'newspare'),

    ),
    'core/social-links' => array(
      'square' => __('Square', 'newspare'),
    ),
    'core/post-terms' => array(
      'solid' => __('Solid', 'newspare'),

    ),
    'core/post-title' => array(
      'secondary' => __('Secondary', 'newspare'),

    ),
    'core/post-excerpt' => array(
      'secondary' => __('Secondary', 'newspare'),

    ),
    'core/post-author' => array(
      'secondary' => __('Secondary', 'newspare'),


    ),
    'core/post-author-name' => array(
      'with-icon' => __('With Icon', 'newspare'),

    ),
    'core/post-date' => array(
      'with-icon' => __('With Icon', 'newspare'),

    ),
    'core/query-pagination' => array(
      'secondary' => __('Secondary', 'newspare'),
    ),

    'core/navigation' => array(
      'secondary' => __('Secondary', 'newspare'),


    ),
    'core/navigation-submenu' => array(
      'secondary' => __('Secondary', 'newspare'),


    ),
    'core/search' => array(
      'secondary' => __('Secondary', 'newspare'),


    ),
    'core/heading' => array(
      'secondary' => __('Secondary', 'newspare'),

    ),
    'core/categories' => array(
      'secondary' => __('Secondary', 'newspare'),
    ),
    'core/button' => array(
      'secondary' => __('Secondary', 'newspare'),

    ),
    'core/page-list' => array(
      'secondary' => __('Secondary', 'newspare'),

    ),
    'core/calendar' => array(
      'secondary' => __('Secondary', 'newspare'),

    ),
    'core/columns' => array(
      'sticky-top' => __('Sticky Top', 'newspare'),
      'sticky-bottom' => __('Sticky Bottom', 'newspare'),

    ),
  );

  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', 'newspare_register_block_styles');


/* Display Excerpt */
if (!function_exists('newspare_excerpt_length')) :

  /**
   * Simple excerpt length.
   *
   * @since 1.0.0
   */

  function newspare_excerpt_length($length)
  {


    if (is_admin()) {
      return $length;
    }
    return 25;
  }

endif;
add_filter('excerpt_length', 'newspare_excerpt_length', 999);




/* Display Breadcrumbs */
if (!function_exists('newspare_excerpt_more')) :

  /**
   * Simple excerpt more.
   *
   * @since 1.0.0
   */
  function newspare_excerpt_more($more)
  {
    if (is_admin()) {
      return $more;
    }
    global $post;
    return '..';
  }

endif;
add_filter('excerpt_more', 'newspare_excerpt_more');


/**
 * admin dashboard
 */
require get_template_directory() . '/admin-dashboard/admin_dashboard.php';

/**
 * Page layout blocks section additions.
 */
require get_template_directory() . '/lib/tgm/hook-tgm.php';

/**
 * Theme review.
 */
require get_template_directory() . '/lib/review.php';

/**
 * Theme upgrade.
 */
require get_template_directory() . '/lib/upgrade.php';

/**
 * Custom Multi Author tags for this theme.
 */
require get_template_directory() . '/lib/multi-author.php';
