<?php 
namespace newscope_Customizer_Reset;

/**
 * Enqueue scripts and localizations.
 */

	function newscope_enqueue_scripts() {
		$file_mod_time = strval( filemtime( get_template_directory() . '/assets/js/customizer-reset.js' ) );
	
		wp_enqueue_script(
			'newscope-customizer-reset',
			get_template_directory_uri() . '/assets/js/customizer-reset.js',
			array( 'jquery' ),
			$file_mod_time,
			false
		);
		wp_localize_script(
			'newscope-customizer-reset',
			'_newscopeCustomizerReset',
			array(
				'reset'   => __( 'Reset', 'newscope' ),
				'confirm' => __( "Please Note: Removed All Your Style And Settings. and Show Default All Options ", 'newscope' ),
				'nonce'   => array(
					'reset' => wp_create_nonce( 'customizer-reset' ),
				),
			)
		);
	}
	add_action( 'customize_controls_print_scripts', __NAMESPACE__ . '\newscope_enqueue_scripts' );
	
	
	/**
	 * Run methods
	 */
	function newscope_remove_theme_modifications() {
		global $wp_customize, $options;
	
		// we are not in preview mode.
		if ( ! $wp_customize->is_preview() ) {
			wp_send_json_error( 'not_preview' );
		}
	
		// if nonce is invalid.
		if ( ! check_ajax_referer( 'customizer-reset', 'nonce', false ) ) {
			wp_send_json_error( 'invalid_nonce' );
		}
	
		/**
		 * Filter the settings that will be removed.
		 */
		$settings = apply_filters( 'customizer_reset_settings', $wp_customize->settings() );
	
		if ( ! empty( $settings ) ) {
			foreach ( $settings as $setting ) {
				if ( 'theme_mod' === $setting->type ) {
					remove_theme_mod( $setting->id );
				}
			}
		}
		wp_send_json_success();
	}
	add_action( 'wp_ajax_customizer_reset', __NAMESPACE__ . '\newscope_remove_theme_modifications' );
