<?php
/**
 * United Blog functions and definitions
 *
 * @link https://developer.wordpress.org/themes/basics/theme-functions/
 *
 * @package United Blog
 */
$theme_version = wp_get_theme()->get('Version');
if (!defined('_UNITED_VERSION')) {
    define('_UNITED_VERSION', $theme_version);
}
if (!function_exists('united_blog_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 united_blog_setup()
    {
        /*
         * Make theme available for translation.
         * Translations can be filed in the /languages/ directory.
         * If you're building a theme based on United Blog, use a find and replace
         * to change 'united-blog' to the name of your theme in all the template files.
         */
        load_theme_textdomain('united-blog', get_template_directory() . '/languages');
        // Add default posts and comments RSS feed links to head.
        add_theme_support('automatic-feed-links');
        /*
         * Let WordPress manage the document title.
         * By adding theme support, we declare that this theme does not use a
         * hard-coded <title> tag in the document head, and expect WordPress to
         * provide it for us.
         */
        add_theme_support('title-tag');
        /*
         * Enable support for Post Thumbnails on posts and pages.
         *
         * @link https://developer.wordpress.org/themes/functionality/featured-images-post-thumbnails/
         */
        add_theme_support('post-thumbnails');
        // This theme uses wp_nav_menu() in one location.
        register_nav_menus(array(
            'primary' => esc_html__('Primary Menu', 'united-blog'),
            'social' => __('Social Menu', 'united-blog'),
        ));
        /*
         * Switch default core markup for search form, comment form, and comments
         * to output valid HTML5.
         */
        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('united_blog_custom_background_args', array(
            'default-color' => 'ffffff',
            'default-image' => '',
        )));
        // Add theme support for selective refresh for widgets.
        add_theme_support('customize-selective-refresh-widgets');
        /**
         * Add support for core custom logo.
         *
         * @link https://codex.wordpress.org/Theme_Logo
         */
        add_theme_support('custom-logo', array(
            'flex-width' => true,
            'flex-height' => true,
        ));
        // Add post-formats support.
        add_theme_support('post-formats', array(
                'aside',
                'image',
                'gallery',
                'status',
                'video',
                'audio',
                'chat',
            )
        );
        /**
         * Add theme support for gutenberg block
         *
         */
        add_theme_support('align-wide');
        add_theme_support('responsive-embeds');
        add_theme_support('wp-block-styles');

        // Remove custom header support
        remove_theme_support('custom-header');
    }
endif;
add_action('after_setup_theme', 'united_blog_setup');
/**
 * Set the content width in pixels, based on the theme's design and stylesheet.
 *
 * Priority 0 to make it available to lower priority callbacks.
 *
 * @global int $content_width
 */
function united_blog_content_width()
{
    $GLOBALS['content_width'] = apply_filters('united_blog_content_width', 640);
}

add_action('after_setup_theme', 'united_blog_content_width', 0);
/**
 * Enqueue scripts and styles.
 */
function united_blog_scripts()
{
    $min = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min';
    wp_enqueue_style('swiper', get_template_directory_uri() . '/assets/swiper/css/swiper-bundle' . $min . '.css');
    wp_enqueue_style('united-blog-style', get_stylesheet_uri(), array(), _UNITED_VERSION);
    wp_style_add_data('united-blog-style', 'rtl', 'replace');
    wp_add_inline_style('united-blog-style', united_blog_get_inline_css());
    if (united_blog_get_option('show_light_dark_switch') || united_blog_get_option('force_dark_mode')) {
        wp_enqueue_style('united-blog-mode-style', get_template_directory_uri() . '/assets/css/dark-mode.css', array(), _UNITED_VERSION);
    }
    wp_enqueue_script('united-blog-navigation', get_template_directory_uri() . '/assets/js/navigation' . $min . '.js', array(), _UNITED_VERSION, true);
    wp_enqueue_script('swiper', get_template_directory_uri() . '/assets/swiper/js/swiper-bundle' . $min . '.js', array(), _UNITED_VERSION, true);
    wp_enqueue_script('united-blog-script', get_template_directory_uri() . '/assets/js/script' . $min . '.js', array(), _UNITED_VERSION, true);
    if (is_singular() && comments_open() && get_option('thread_comments')) {
        wp_enqueue_script('comment-reply');
    }
}

add_action('wp_enqueue_scripts', 'united_blog_scripts');
/**
 * Enqueue admin scripts and styles.
 */
function united_blog_admin_scripts()
{

    $suffix = (defined('SCRIPT_DEBUG') && SCRIPT_DEBUG) ? '' : '.min';

    $file_name = is_rtl() ? 'admin-rtl' . $suffix . '.css' : 'admin' . $suffix . '.css';

    wp_enqueue_style(
        'admin-style',
        get_template_directory_uri() . '/assets/css/' . $file_name,
        array(),
        _UNITED_VERSION
    );

    wp_enqueue_script(
        'united-admin-script',
        get_template_directory_uri() . '/assets/js/admin' . $suffix . '.js',
        array('jquery'),
        _UNITED_VERSION,
        true
    );

}

add_action('admin_enqueue_scripts', 'united_blog_admin_scripts');
/**
 * Custom template tags for this theme.
 */
require get_template_directory() . '/inc/template-tags.php';
/**
 * Custom functions for this theme.
 */
require get_template_directory() . '/inc/custom-functions.php';
/**
 * Custom hooks for this theme.
 */
require get_template_directory() . '/inc/custom-hooks.php';

/**
 * Customizer additions.
 */
require get_template_directory() . '/inc/customizer/customizer.php';
/**
 * SVG icons functions and filters.
 */
require get_template_directory() . '/inc/icon-functions.php';
/**
 * Load Jetpack compatibility file.
 */
if (defined('JETPACK__VERSION')) {
    require get_template_directory() . '/inc/jetpack.php';
}
// Load helper functions for theme.
require get_template_directory() . '/inc/helpers.php';
// Custom page walker.
require get_template_directory() . '/classes/class-united-walker-page.php';

require get_template_directory() . '/assets/tgmpa/recommended-plugins.php';

// custom css
require get_template_directory() . '/assets/css/custom-style.php';

// post category meta
require get_template_directory() . '/classes/category-meta.php';
// Widgets and Sidebars for this theme.
require get_template_directory() . '/inc/widgets/init.php';
/**
 * admin page.
 */
require get_parent_theme_file_path('/inc/admin-page/admin-about.php');

/**
 * Functions which enhance the theme by hooking into WordPress.
 */
require get_template_directory() . '/inc/template-functions.php';


add_action('init', function () {
    $united_blog_favorite_url = basename($_SERVER['REQUEST_URI']);
    if ($united_blog_favorite_url == 'favorites') {
        // load the file if exists
        $load = locate_template('/template-parts/united-favorites.php', true);
        if ($load) {
            exit(); // just exit if template was found and loaded
        }
    }
});

function united_blog_remove_default_favorite_button() {
    remove_action('wp_favorites_favorite_button', 'wp_favorites_favorite_button');
    add_filter('the_content', 'united_blog_remove_favorite_button_from_content', 99);
    add_filter('get_the_excerpt', 'united_blog_remove_favorite_button_from_excerpt', 99);
    remove_action('wp_favorites_after_post', 'wp_favorites_favorite_button');
    remove_action('wp_favorites_before_post', 'wp_favorites_favorite_button');
}
function united_blog_remove_favorite_button_from_content($content) {
    $button_html = do_shortcode('[favorite_button]');
    return str_replace($button_html, '', $content);
}
function united_blog_remove_favorite_button_from_excerpt($excerpt) {
    $button_html = do_shortcode('[favorite_button]');
    return str_replace($button_html, '', $excerpt);
}
add_action('wp_loaded', 'united_blog_remove_default_favorite_button');