<?php

/**
 * cleanlogic functions and definitions.
 *
 * @link https://developer.wordpress.org/themes/basics/theme-functions/
 *
 * @package cleanlogic
 */

if (! function_exists('cleanlogic_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.
	 */
	function cleanlogic_setup()
	{
		/*
	 * Make theme available for translation.
	 * Translations can be filed in the /languages/ directory.
	 * If you're building a theme based on cleanlogic, use a find and replace
	 * to change 'cleanlogic' to the name of your theme in all the template files.
	 */
		load_theme_textdomain('cleanlogic', get_template_directory() . '/languages');

		add_theme_support('automatic-feed-links');
		add_theme_support('title-tag');
		add_theme_support('post-thumbnails');
		add_theme_support('wp-block-styles');
		add_theme_support('responsive-embeds');

		add_theme_support('custom-logo', [
			'height'      => 100,
			'width'       => 400,
			'flex-width'  => true,
			'flex-height' => true,
			'unlink-homepage-logo' => false,
		]);

		add_theme_support('align-wide');
		add_editor_style('editor-style.css');
		register_nav_menus(array(
			'primary' => esc_html__('Primary', 'cleanlogic'),
		));
		add_theme_support('custom-header', apply_filters('cleanlogic_custom_header_args', array(
			'default-image'      => get_parent_theme_file_uri('/assets/images/bridge_golden.jpg'),
			'width' => 2000,
			'height' => 1200,
			'default-text-color' => '000000',
			'flex-height'        => true,
			'video'              => false,
			'wp-head-callback' => 'cleanlogic_header_style',
		)));
		add_theme_support('html5', array(
			'search-form',
			'comment-form',
			'comment-list',
			'gallery',
			'caption',
		));

		// Set up the WordPress core custom background feature.
		add_theme_support('custom-background', apply_filters('cleanlogic_custom_background_args', array(
			'default-color' => 'f3f3f3',
			'default-image' => '',
		)));
	}
endif;
add_action('after_setup_theme', 'cleanlogic_setup');


add_action('admin_enqueue_scripts', function ($hook) {
	$allowed = array('widgets.php', 'nav-menus.php', 'themes.php');

	if (! in_array($hook, $allowed, true)) {
		return;
	}

	$rel = 'assets/css/customize-controls.css';
	wp_enqueue_style(
		'cleanlogic-admin',
		get_theme_file_uri($rel),
		array(),
		file_exists(get_theme_file_path($rel)) ? filemtime(get_theme_file_path($rel)) : null
	);
});

add_action('after_setup_theme', 'cleanlogic_setup');
function cleanlogic_content_width()
{
	$GLOBALS['content_width'] = apply_filters('cleanlogic_content_width', 640);
}
add_action('after_setup_theme', 'cleanlogic_content_width', 0);
function cleanlogic_widgets_init()
{
	register_sidebar(array(
		'name'          => esc_html__('Sidebar', 'cleanlogic'),
		'id'            => 'sidebar-1',
		'description'   => '',
		'before_widget' => '<section id="%1$s" class="widget %2$s">',
		'after_widget'  => '</section>',
		'before_title'  => '<h2 class="widget-title">',
		'after_title'   => '</h2>',
	));
}
add_action('widgets_init', 'cleanlogic_widgets_init');

/**
 * Enqueue scripts and styles.
 */
function cleanlogic_scripts()
{
	wp_enqueue_style('cleanlogic-style', get_stylesheet_uri());

	wp_enqueue_style(
		'cleanlogic-fonts',
		get_template_directory_uri() . '/assets/css/fonts.css',
		array(),
		'1.0'
	);
	

	wp_enqueue_style('font-awesome', get_template_directory_uri() . '/assets/css/font-awesome.css', array());

	wp_enqueue_script('cleanlogic-navigation', get_template_directory_uri() . '/assets/js/navigation.js', array(), '20120206', true);

	wp_enqueue_script('cleanlogic-skip-link-focus-fix', get_template_directory_uri() . '/assets/js/skip-link-focus-fix.js', array(), '20130115', true);

	if (is_singular() && comments_open() && get_option('thread_comments')) {
		wp_enqueue_script('comment-reply');
	}
}
add_action('wp_enqueue_scripts', 'cleanlogic_scripts');



// Admin-side styles for the Customizer controls panel.
add_action('customize_controls_enqueue_scripts', function () {
	wp_enqueue_style('cleanlogic-customizer-controls', get_template_directory_uri() . '/assets/admin/css/customizer-controls.css', array(), '1.0');
}); 


/**
 * CleanLogic teaser for archive / blog list.
 * - Keeps <a> tags from the first paragraph / before <!--more-->
 * - Adds a "Read More" link pointing to the single post
 */
function cleanlogic_teaser() {

    // Get raw post content
    $raw_content = get_the_content( '' );

    // Try to split at <!--more--> if author used it
    $parts = get_extended( $raw_content );

    if ( ! empty( $parts['main'] ) && $parts['main'] !== $raw_content ) {
        // There IS a <!--more--> break
        $teaser_html = apply_filters( 'the_content', $parts['main'] );

    } else {
        // No <!--more--> break:
        // We'll grab ONLY the first <p>…</p> from full content
        $full_html = apply_filters( 'the_content', $raw_content );

        if ( preg_match( '/<p\b[^>]*>.*?<\/p>/is', $full_html, $match ) ) {
            // first paragraph incl. <a>, <strong>, etc.
            $teaser_html = $match[0];
        } else {
            // Fallback: plain text trimmed to ~40 words
            $teaser_html = '<p>' . esc_html(
                wp_trim_words(
                    wp_strip_all_tags( $raw_content ),
                    40,
                    '…'
                )
            ) . '</p>';
        }
    }

    // Whitelist which HTML tags/attributes are allowed in teaser
    $allowed_tags = array(
        'p'      => array(
            'class' => true,
        ),
        'a'      => array(
            'href'   => true,
            'title'  => true,
            'rel'    => true,
            'target' => true,
        ),
        'strong' => array(),
        'em'     => array(),
        'br'     => array(),
    );

    // Safe output teaser with wp_kses()
    echo wp_kses( $teaser_html, $allowed_tags );

    // Output Read More link (fully escaped)
    echo '<p class="read-more-link"><a href="' . esc_url( get_permalink() ) . '">' . esc_html__( 'Read More »', 'cleanlogic' ) . '</a></p>';
}

/**
 * Implement the Custom Header feature.
 */
require get_template_directory() . '/inc/custom-header.php';

/**
 * Cleanlogic template tags for this theme.
 */
require get_template_directory() . '/inc/template-tags.php';

/**
 * Cleanlogic functions that act independently of the theme templates.
 */
require get_template_directory() . '/inc/cleanlogic-body-classes.php';

/**
 * Cleanlogic menu toggel class.
 */
require get_template_directory() . '/inc/class-cleanlogic-toggle-control.php';

/**
 * Cleanlogic Load Theme Customizer logic.
 */
require get_template_directory() . '/inc/class-cleanlogic-customizer.php';