<?php
/**
 * Product Manufacturing functions and definitions
 *
 * @package Product Manufacturing
 */

if ( ! function_exists( 'product_manufacturing_setup' ) ) :
function product_manufacturing_setup() {

	global $content_width;

	if ( ! isset( $content_width ) ) {
		$content_width = 640;
	}

	// Theme version
	if ( ! defined( 'PRODUCT_MANUFACTURING_VERSION' ) ) {
		define( 'PRODUCT_MANUFACTURING_VERSION', wp_get_theme()->get( 'Version' ) );
	}

	// Load text domain
	load_theme_textdomain( 'product-manufacturing', get_template_directory() . '/languages' );

	// Theme supports
	add_theme_support( 'post-thumbnails' );
	add_theme_support( 'title-tag' );

	add_theme_support( 'custom-logo', array(
		'height'      => 240,
		'width'       => 240,
		'flex-height' => true,
	) );

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

	add_theme_support( 'wp-block-styles' );
	add_theme_support( 'align-wide' );
	add_theme_support( 'responsive-embeds' );

	// WooCommerce
	add_theme_support( 'woocommerce' );

	// Editor styles
	add_editor_style( array( 'assets/css/editor-style.css' ) );

	require get_parent_theme_file_path( '/inc/dashboard/dashboard.php' );

	require get_parent_theme_file_path( '/inc/admin/theme-upsell.php' );

    require get_parent_theme_file_path( '/inc/customizer/customizer.php' );

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

/**
 * Enqueue scripts and styles
 */
function product_manufacturing_scripts() {

	$product_manufacturing_version = PRODUCT_MANUFACTURING_VERSION;

	// Main stylesheet
	wp_enqueue_style(
		'product-manufacturing-basic-style',
		get_stylesheet_uri(),
		array(),
		$product_manufacturing_version
	);

	// Animate CSS
	wp_enqueue_style(
		'animate-css',
		get_template_directory_uri() . '/assets/css/animate.css',
		array(),
		$product_manufacturing_version
	);

	// Font Awesome
	wp_enqueue_style(
		'fontawesome',
		get_template_directory_uri() . '/assets/font-awesome/css/all.css',
		array(),
		'7.0.0'
	);

	// Owl Carousel CSS
	wp_enqueue_style(
		'owl-carousel-style',
		get_template_directory_uri() . '/assets/css/owl-carousel.css',
		array(),
		$product_manufacturing_version
	);

	// WOW JS
	wp_enqueue_script(
		'wow-js',
		get_template_directory_uri() . '/assets/js/wow.js',
		array( 'jquery' ),
		$product_manufacturing_version,
		true
	);

	// Owl Carousel JS
	wp_enqueue_script(
		'owl-carousel-js',
		get_template_directory_uri() . '/assets/js/owl-carousel.js',
		array( 'jquery' ),
		$product_manufacturing_version,
		true
	);

	// Main Script
	wp_enqueue_script(
		'product-manufacturing-main-script',
		get_template_directory_uri() . '/assets/js/script.js',
		array( 'jquery', 'wow-js' ),
		$product_manufacturing_version,
		true
	);

	// RTL support
	wp_style_add_data( 'product-manufacturing-basic-style', 'rtl', 'replace' );
}
add_action( 'wp_enqueue_scripts', 'product_manufacturing_scripts' );


function product_manufacturing_enqueue_admin_script() {
	wp_enqueue_script(
		'product-manufacturing-welcome-notice',
		get_template_directory_uri() . '/inc/dashboard/product-manufacturing-welcome-notice.js',
		array( 'jquery' ),
		'',
		true
	);

	wp_localize_script(
		'product-manufacturing-welcome-notice',
		'product_manufacturing_localize',
		array(
			'ajax_url'      => admin_url( 'admin-ajax.php' ),
			'nonce'         => wp_create_nonce( 'product_manufacturing_welcome_nonce' ),
			'dismiss_nonce' => wp_create_nonce( 'product_manufacturing_welcome_nonce' ),
			'redirect_url'  => admin_url( 'themes.php?page=product-manufacturing-guide-page' ),
		)
	);
}
add_action( 'admin_enqueue_scripts', 'product_manufacturing_enqueue_admin_script' );

function product_manufacturing_admin_theme_style() {
	wp_enqueue_style('product-manufacturing-custom-admin-style', esc_url(get_template_directory_uri()) . '/inc/dashboard/dashboard.css');
   	wp_enqueue_style('product-manufacturing-admin-upsell-style', esc_url(get_template_directory_uri()) . '/assets/css/admin-style.css');
}
add_action( 'admin_enqueue_scripts', 'product_manufacturing_admin_theme_style' );

// Block Patterns
require get_template_directory() . '/block-patterns.php';
require get_template_directory() .'/inc/TGM/tgm.php';
require_once get_template_directory() . '/inc/dashboard/welcome-notice.php';

/**
 * Redirect to Pro upsell page after theme activation
 */

function product_manufacturing_redirect_after_activation() {
    if ( ! get_option( 'product_manufacturing_activation_redirect', false ) ) {
        update_option( 'product_manufacturing_activation_redirect', true );
        wp_safe_redirect( admin_url( 'themes.php?page=product-manufacturing-pro' ) );
        exit;
    }
}
add_action('after_switch_theme', 'product_manufacturing_redirect_after_activation');

function product_manufacturing_add_upgrade_button( $product_manufacturing_wp_admin_bar ) {

    $product_manufacturing_theme_name = wp_get_theme()->get( 'Name' );

    $product_manufacturing_args = array(
        'id'    => 'product_manufacturing_upgrade',
        'title' => '<span style="color:#fff;font-weight:600;">
            🚀 Upgrade to ' . esc_html( $product_manufacturing_theme_name ) . ' Pro - 20% OFF 
            <span style="background:#ff5722;color:#fff;padding:2px 8px;border-radius:3px;margin-left:6px;">
                Buy Now
            </span>
        </span>',
        'href'  => esc_url( PRODUCT_MANUFACTURING_BUY_PRO ),
        'meta'  => array(
            'class'  => 'product-manufacturing-upgrade-btn',
            'title'  => 'Upgrade to Pro',
            'target' => '_blank'
        )
    );

    $product_manufacturing_wp_admin_bar->add_node( $product_manufacturing_args );
}
add_action( 'admin_bar_menu', 'product_manufacturing_add_upgrade_button', 100 );