<?php

/**********************************
######### Setup WordPress #########
***********************************/

require get_template_directory() . '/inc/hp-cleaner.php';
require get_template_directory() . '/inc/hp-nav-walker.php';
require get_template_directory() . '/inc/hp-templates.php';

function hp_scripts() {
  wp_enqueue_style( 'icons', get_template_directory_uri() . '/css/bootstrap-icons.css' );
  wp_enqueue_style( 'sass', get_template_directory_uri() . '/css/custom.css' );
  wp_enqueue_style( 'style', get_stylesheet_uri() );
	wp_enqueue_script('jquery');
  wp_enqueue_script('script', get_template_directory_uri() . '/js/script.min.js', array(), '', true );
  wp_enqueue_script('init', get_template_directory_uri() . '/js/init.js', array(), '', true );
	if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
		wp_enqueue_script( 'comment-reply' );
	}
}
add_action( 'wp_enqueue_scripts', 'hp_scripts' );

if ( ! function_exists( 'hp_setup' ) ) :
  function hp_setup() {
    add_theme_support( 'automatic-feed-links' );
    add_theme_support( 'title-tag' );
    add_theme_support( 'post-thumbnails' );
    add_theme_support( 'nav-menus' );
    register_nav_menus( array(
      'primary' => __( 'Primary' ),
      'bottom'  =>__( 'Bottom' ),
      'top'     =>__( 'Top' )
    ));
    add_theme_support( 'html5', array(
      'search-form',
      'comment-form',
      'comment-list',
      'caption',
    ));
    add_theme_support( 'customize-selective-refresh-widgets' );
    add_theme_support( 'editor-styles' );


	add_theme_support( 'disable-custom-colors' );
	add_theme_support( 'editor-color-palette', array(
		array(
			'name'  => __( 'Green' ),
			'slug'  => 'green',
			'color' => '#325363',
		),
		array(
			'name'  => __( 'Purple' ),
			'slug'  => 'purple',
			'color'	=> '#343C70',
		),
		array(
			'name'  => __( 'Blue' ),
			'slug'  => 'blue',
			'color' => '#5177B0',
		),
		array(
			'name'	=> __( 'Red' ),
			'slug'	=> 'red',
			'color'	=> '#CC7C84',
		),
		array(
			'name'	=> __( 'Yellow' ),
			'slug'	=> 'yellow',
			'color'	=> '#E0DC67',
		),
		array(
			'name'	=> __( 'Teal' ),
			'slug'	=> 'teal',
			'color'	=> '#50A7AD',
		),
	 ));
  }
endif;
add_action( 'after_setup_theme', 'hp_setup' );


function hp_widgets_init() {
    register_sidebar( array(
        'name'          => esc_html__( 'Sidebar', 'hp-theme' ),
        'id'            => 'sidebar-1',
        'description'   => esc_html__( 'Add widgets here.', 'hp-theme' ),
        'before_widget' => '<section id="%1$s" class="widget %2$s">',
        'after_widget'  => '</section>',
        'before_title'  => '<h3 class="widget-title">',
        'after_title'   => '</h3>',
    ) );
    register_sidebar( array(
        'name'          => esc_html__( 'Footer 1', 'hp-theme' ),
        'id'            => 'footer-1',
        'description'   => esc_html__( 'Add widgets here.', 'hp-theme' ),
        'before_widget' => '<section id="%1$s" class="widget %2$s">',
        'after_widget'  => '</section>',
        'before_title'  => '<h3 class="widget-title">',
        'after_title'   => '</h3>',
    ) );
    register_sidebar( array(
        'name'          => esc_html__( 'Footer 2', 'hp-theme' ),
        'id'            => 'footer-2',
        'description'   => esc_html__( 'Add widgets here.', 'hp-theme' ),
        'before_widget' => '<section id="%1$s" class="widget %2$s">',
        'after_widget'  => '</section>',
        'before_title'  => '<h3 class="widget-title">',
        'after_title'   => '</h3>',
    ) );
    register_sidebar( array(
        'name'          => esc_html__( 'Footer 3', 'hp-theme' ),
        'id'            => 'footer-3',
        'description'   => esc_html__( 'Add widgets here.', 'hp-theme' ),
        'before_widget' => '<section id="%1$s" class="widget %2$s">',
        'after_widget'  => '</section>',
        'before_title'  => '<h3 class="widget-title">',
        'after_title'   => '</h3>',
    ) );
}
add_action( 'widgets_init', 'hp_widgets_init' );


/**********************************
######## Gutenberg Editor #########
***********************************/

function hp_add_editor_styles() {
  add_editor_style([
    'css/custom.css',
    'style.css'
  ] );
}
add_action( 'admin_init', 'hp_add_editor_styles' );

function hp_gutenberg_scripts() {
	wp_enqueue_script( 'hp-editor', get_stylesheet_directory_uri() . '/js/editor.js', array( 'wp-blocks', 'wp-dom' ), filemtime( get_stylesheet_directory() . '/js/editor.js' ), true );
}
add_action( 'enqueue_block_editor_assets', 'hp_gutenberg_scripts' );

function hp_gutenberg_style() {
    echo '<style>.wp-block{max-width:992px;}.wp-block[data-align="wide"]{}.wp-block[data-align="full"]{max-width:none;}.editor-post-title__block .editor-post-title__input,.edit-post-visual-editor, .edit-post-visual-editor p{font-family: Helvetica, Arial, sans-serif;}</style>';
}
add_action('admin_head', 'hp_gutenberg_style');

add_filter( 'render_block', function( $block_content, $block ) {
	$block_content = str_replace(
		'wp-block-button__link',
		'wp-block-button__link btn',
		$block_content
	);
	return $block_content;
}, 5, 2 );

/**********************************
###### Seamless Donations #########
***********************************/

function hp_seamless_style() {
  if ( !is_page('donate') ) {
    wp_deregister_style('seamless_donations_css');
  }
}
add_action('wp_print_styles', 'hp_seamless_style', 100);

function hp_seamless_scripts() {
    if ( !is_page('donate') ) {
        wp_dequeue_script( 'seamless_javascript_code' );
        wp_deregister_script( 'seamless_javascript_code' );
        wp_dequeue_script( 'seamless_javascript_uuid' );
        wp_deregister_script( 'seamless_javascript_uuid' );
    }
}
add_action( 'wp_enqueue_scripts', 'hp_seamless_scripts', 100);

function hp_seamless_script() {
    if ( is_page(200) ) { ?>
      <script type="text/javascript">
        document.getElementById("dgx-donate-designated").checked = true;
        jQuery(document).ready(function($){
          $('.specific-fund').show();
        });
      </script>
      <div data-reveal='.specific-fund'></div>
    <?php }
}
add_action('wp_footer', 'hp_seamless_script', 0);

function hp_seamless_redirect() {
  if ( is_singular(['donor','donation','funds'])) {
    wp_redirect( 'donate', 301 );
    exit;
  }
}
add_action( 'template_redirect', 'hp_seamless_redirect' );

function hp_exclude_seamless_post_type($query) {
  if(is_admin() || !$query->is_main_query()) return;
  if($query->is_search()) {
    $post_type_to_remove = 'funds';
    $searchable = get_post_types(array('exclude_from_search' => false));
    if(is_array($searchable) && in_array($post_type_to_remove, $searchable)){
      $query->set('post_type', $searchable);
    }
  }
}
add_action('pre_get_posts', 'hp_exclude_seamless_post_type', 99 );

function hp_remove_fund_custom_type() {
	global $wp_post_types;
	$wp_post_types['funds']->exclude_from_search = true;
}
add_action( 'init', 'hp_remove_fund_custom_type', 99 );

function hp_remove_donor_custom_type() {
	global $wp_post_types;
	$wp_post_types['donor']->exclude_from_search = true;
}
add_action( 'init', 'hp_remove_donor_custom_type', 99 );