<?php
/**
 * Tree Plantation functions and definitions
 *
 * @link https://developer.wordpress.org/themes/basics/theme-functions/
 *
 * @package Tree Plantation
 */

if ( ! defined( 'TREE_PLANTATION_VERSION' ) ) {
	// Replace the version number of the theme on each release.
	define( 'TREE_PLANTATION_VERSION', wp_get_theme()->get( 'Version' ) );
}

if ( ! function_exists( 'tree_plantation_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 tree_plantation_setup() {

		load_theme_textdomain( 'tree-plantation', get_template_directory() . '/languages' );

		// Add default posts and comments RSS feed links to head.
		add_theme_support( 'automatic-feed-links' );

		add_theme_support( 'align-wide' );

		add_theme_support( 'woocommerce' );

		// Add support for block styles.
		add_theme_support( 'wp-block-styles' );

		// Enqueue editor styles.
		add_editor_style( 'style.css' );

		// Experimental support for adding blocks inside nav menus
		add_theme_support( 'block-nav-menus' );

		// Add support for experimental link color control.
		add_theme_support( 'experimental-link-color' );

        add_theme_support( 'custom-background', apply_filters( 'tree_plantation_custom_background', array(
            'default-color' => 'ffffff',
            'default-image' => '',
        )));

        require_once get_theme_file_path( '/inc/customizer.php' );

	}
endif;
add_action( 'after_setup_theme', 'tree_plantation_setup' );

function tree_plantation_scripts() {
    wp_enqueue_style('tree-plantation-style', get_stylesheet_uri(), array() );
    wp_enqueue_script('jquery-wow', get_template_directory_uri() . '/assets/js/wow.js', array('jquery'));
    wp_enqueue_style('animate-css', get_template_directory_uri() . '/assets/css/animate.css');

	//font-awesome
	wp_enqueue_style( 'fontawesome', get_template_directory_uri() . '/font-awesome/css/all.css', array(), '6.7.0' );
    wp_enqueue_style( 'owl.carousel-style', get_template_directory_uri().'/assets/css/owl.carousel.css', array(), TREE_PLANTATION_VERSION );
	wp_enqueue_script( 'owl.carousel-js', get_template_directory_uri(). '/assets/js/owl.carousel.js', array('jquery') ,TREE_PLANTATION_VERSION,true);
    
    // Custom scripts should depend on owl JS and jQuery
    wp_enqueue_script('tree-plantation-custom-scripts', get_template_directory_uri() . '/assets/js/custom.js', array('jquery', 'jquery'), '', true);
    wp_style_add_data( 'tree-plantation-style', 'rtl', 'replace' );
}
add_action( 'wp_enqueue_scripts', 'tree_plantation_scripts' );

/**
 * Enqueue block editor style
 */
function tree_plantation_add_block_styles() {
    wp_enqueue_block_style(
        'core/paragraph',
        array(
            'handle' => 'tree-plantation-paragraph-editor-style',
            'src'    => get_theme_file_uri( '/assets/css/block-editor.css' ),
            'path'   => get_theme_file_path( '/assets/css/block-editor.css' ),
        )
    );
}
add_action( 'init', 'tree_plantation_add_block_styles' );

function tree_plantation_init_setup() {

	// Add block patterns
	require get_template_directory() . '/inc/block-patterns.php';

}
add_action( 'after_setup_theme', 'tree_plantation_init_setup' );


//////////////////////////////////////////////   Function for Translation Error   //////////////////////////////////////////////////////
function tree_plantation_enqueue_function() {

    /**
    * GET START.
    */
    require get_template_directory() . '/getstarted/tree_plantation_about_page.php';

    require_once get_theme_file_path( '/inc/customizer.php' );

    define('TREE_PLANTATION_FREE_BUY_NOW',__('https://www.revolutionwp.com/products/tree-plantation','tree-plantation'));
    define('TREE_PLANTATION_FREE_SUPPORT',__('https://wordpress.org/support/theme/tree-plantation/','tree-plantation'));
    define('TREE_PLANTATION_PRO_SUPPORT',__('https://www.revolutionwp.com/support/revolution-wp/','tree-plantation'));
    define('TREE_PLANTATION_REVIEW',__('https://wordpress.org/support/theme/tree-plantation/reviews/','tree-plantation'));
    define('TREE_PLANTATION_BUY_NOW',__('https://www.revolutionwp.com/products/lawn-care-wordpress-theme','tree-plantation'));
    define('TREE_PLANTATION_LIVE_DEMO',__('https://demo.revolutionwp.com/lawn-care-pro/','tree-plantation'));
    define('TREE_PLANTATION_PRO_DOC',__('https://demo.revolutionwp.com/wpdocs/lawn-care-pro/','tree-plantation'));
    define('TREE_PLANTATION_LITE_DOC',__('https://demo.revolutionwp.com/wpdocs/tree-plantation-free/','tree-plantation'));
    
}
add_action( 'after_setup_theme', 'tree_plantation_enqueue_function' );

// Add admin notice
function tree_plantation_admin_notice() { 
    global $pagenow;
    $tree_plantation_theme_args      = wp_get_theme();
    $tree_plantation_meta            = get_option( 'tree_plantation_admin_notice' );
    $name            = $tree_plantation_theme_args->__get( 'Name' );
    $tree_plantation_current_screen  = get_current_screen();

    if( !$tree_plantation_meta ){
        if( is_network_admin() ){
            return;
        }

        if( ! current_user_can( 'manage_options' ) ){
            return;
        } 
        
        if( $tree_plantation_current_screen->base !== 'appearance_page_tree_plantation_guide' && 
            $tree_plantation_current_screen->base !== 'toplevel_page_treeplantation-demoimport' ) { ?>

            <div class="notice notice-success tree-plantation-welcome-notice">
                <p class="tree-plantation-dismiss-link">
                    <strong>
                        <a href="<?php echo esc_url( add_query_arg( 'tree_plantation_admin_notice', '1' ) ); ?>">
                            <?php esc_html_e( 'Dismiss', 'tree-plantation' ); ?>
                        </a>
                    </strong>
                </p>

                <div class="tree-plantation-welcome-notice-wrap">
                    <h2 class="tree-plantation-notice-title">
                        <span class="dashicons dashicons-admin-home"></span> 
                        <?php 
                            $tree_plantation_theme_name = wp_get_theme()->get( 'Name' );
                            /* translators: %s!: Theme Name. */
                            echo esc_html( sprintf( __( 'Welcome to the free theme: %s!', 'tree-plantation' ), $tree_plantation_theme_name ) );
                        ?>
                    </h2>
                    <p class="tree-plantation-notice-desc">
                        <?php esc_html_e( 'Get started by exploring the features of your new theme. Customize your design, add your content, and create a site that fits your vision.', 'tree-plantation' ); ?>
                    </p>

                    <div class="tree-plantation-welcome-info">
                        <div class="tree-plantation-welcome-thumb">
                            <img src="<?php echo esc_url( get_stylesheet_directory_uri() . '/screenshot.png' ); ?>" alt="<?php esc_attr_e( 'Theme Screenshot', 'tree-plantation' ); ?>">
                        </div>

                        <div class="tree-plantation-welcome-getting-started">
                            <h3><span class="dashicons dashicons-art"></span> <?php esc_html_e( 'Customize Your Theme', 'tree-plantation' ); ?></h3>
                            <p><?php esc_html_e( 'Want to make it truly yours? Explore the Getting Started Guide to personalize your site to suit your needs.', 'tree-plantation' ); ?></p>
                            <p><a class="info-link button" href="<?php echo esc_url( admin_url( 'themes.php?page=tree-plantation-getstart-page' ) ); ?>"><?php esc_html_e( 'View Getting Started Guide', 'tree-plantation' ); ?></a></p>
                        </div>
                    </div>
                </div>
            </div>

            <?php
        }

    }
}

add_action( 'admin_notices', 'tree_plantation_admin_notice' );

if( ! function_exists( 'tree_plantation_update_admin_notice' ) ) :
/**
 * Updating admin notice on dismiss
*/
function tree_plantation_update_admin_notice(){
    if ( isset( $_GET['tree_plantation_admin_notice'] ) && $_GET['tree_plantation_admin_notice'] = '1' ) {
        update_option( 'tree_plantation_admin_notice', true );
    }
}
endif;
add_action( 'admin_init', 'tree_plantation_update_admin_notice' );

add_action('after_switch_theme', 'tree_plantation_setup_options');
function tree_plantation_setup_options () {
    update_option('tree_plantation_admin_notice', FALSE );
}

// Enqueue custom admin notice CSS
function tree_plantation_enqueue_admin_notice_css( $hook ) {
    // Load CSS only on wp-admin pages (not the front end)
    wp_enqueue_style(
        'tree-plantation-admin-notice-style',
        get_template_directory_uri() . '/assets/css/admin-notice.css',
        array(),
        wp_get_theme()->get( 'Version' )
    );
}
add_action( 'admin_enqueue_scripts', 'tree_plantation_enqueue_admin_notice_css' );


// changelog
function tree_plantation_get_changelog_from_readme() {
    $tree_plantation_file_path = get_template_directory() . '/readme.txt'; // Adjust path if necessary

    if (file_exists($tree_plantation_file_path)) {
        $tree_plantation_content = file_get_contents($tree_plantation_file_path);

        // Extract changelog section
        $tree_plantation_changelog_start = strpos($tree_plantation_content, "== Changelog ==");
        $tree_plantation_changelog = substr($tree_plantation_content, $tree_plantation_changelog_start);

        // Split changelog into versions
        preg_match_all('/\*\s([\d\.]+)\s-\s(.+?)\n((?:\t-\s.+?\n)+)/', $tree_plantation_changelog, $tree_plantation_matches, PREG_SET_ORDER);
        
        return $tree_plantation_matches;
    }
    return [];
}