<?php
/**
 * Theme Related Functions
 *
 * @package AttorneyPress
 */

/**
 * Custom template tags for this theme.
 */
require get_parent_theme_file_path( '/inc/template-tags.php' );

/**
 * Custom functions that act independently of the theme templates.
 */
require get_parent_theme_file_path( '/inc/extras.php' );

/**
 * Customizer additions.
 */
require get_parent_theme_file_path( '/inc/customizer.php' );

if ( ! function_exists( 'wp_body_open' ) ) {
	/**
	 * Fire the wp_body_open action.
	 *
	 * @return mixed
	 */
	function wp_body_open() {
		do_action( 'wp_body_open' );
	}
}

/**
 * Load Jetpack compatibility file.
 */
require get_parent_theme_file_path( '/inc/jetpack.php' );
/**
 * Load TGMPA file.
 */
require get_parent_theme_file_path( '/inc/tgmpa/tgmpa.php' );

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

/**
 * Registers an editor stylesheet for the theme.
 */
function attorneypress_theme_add_editor_styles() {

}
add_action( 'admin_init', 'attorneypress_theme_add_editor_styles' );

/**
 * AttorneyPress functions and definitions
 *
 * @link https://developer.wordpress.org/themes/basics/theme-functions/
 *
 * @package AttorneyPress
 */
if ( ! function_exists( 'attorneypress_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 attorneypress_setup() {
		/*
		* Make theme available for translation.
		* Translations can be filed in the /languages/ directory.
		* If you're building a theme based on AttorneyPress, use a find and replace
		* to change 'attorneypress' to the name of your theme in all the template files.
		*/
		load_theme_textdomain( 'attorneypress', get_parent_theme_file_path( '/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(
				'top'    => esc_html__( 'Primary', 'attorneypress' ),
				'footer' => esc_html__( 'Footer', 'attorneypress' ),
			)
		);

		/*
		* 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',
			)
		);

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

		add_theme_support( 'custom-header' );

		// Add theme support for selective refresh for widgets.
		add_theme_support( 'customize-selective-refresh-widgets' );

		/**
		 * [attorneypress_remove_panel_css description]
		 *
		 * @return void
		 */
		function attorneypress_remove_panel_css() {
			wp_dequeue_style( 'redux-admin-css' );
		}

		// This example assumes your opt_name is set to attorneypress_theme_option, replace with your opt_name value.
		add_action( 'redux/page/attorneypress_theme_option/enqueue', 'attorneypress_remove_panel_css' );

		// Start.
		// Adding support for core block visual styles.
		add_theme_support( 'wp-block-styles' );

		// Add support for full and wide align images.
		add_theme_support( 'align-wide' );

		// Add support for responsive embeds.
		add_theme_support( 'responsive-embeds' );

		add_theme_support( 'editor-styles' );

		/**
		 * Register custom fonts.
		 * Based on the function from Twenty Seventeen.
		 */
		function radiantthemes_editor_fonts_url() {
			$fonts_url = '';

			/*
			* Translators: If there are characters in your language that are not
			* supported by Roboto, translate this to 'off'. Do not translate
			* into your own language.
			*/
			$roboto = esc_html_x( 'on', 'Roboto font: on or off', 'attorneypress' );

			if ( 'off' !== $roboto ) {
				$font_families = array();

				$font_families[] = 'Roboto:400,500,700';

				$query_args = array(
					'family' => rawurlencode( implode( '|', $font_families ) ),
					'subset' => rawurlencode( 'latin,latin-ext' ),
				);

				$fonts_url = add_query_arg( $query_args, '//fonts.googleapis.com/css' );
			}
			return esc_url_raw( $fonts_url );
		}
		add_editor_style( radiantthemes_editor_fonts_url() );

		/**
		 * Add custom body class.
		 *
		 * @param array $classes custom body class.
		 * @return array
		 */
		function radiantthemes_body_classes( $classes ) {
			$classes[] = 'radiantthemes';
			$classes[] = 'radiantthemes-' . get_template();
			return $classes;
		}
		add_filter( 'body_class', 'radiantthemes_body_classes' );
		// End.

		/**
		 * Undocumented function
		 */
		function radiantthemes_block_style() {
			wp_register_style(
				'radiantthemes_block',
				get_parent_theme_file_uri( '/css/radiantthemes-blocks.css' ),
				array(),
				time(),
				'all'
			);
			wp_enqueue_style( 'radiantthemes_block' );
		}
		add_action( 'enqueue_block_assets', 'radiantthemes_block_style' );

		/**
		 * Undocumented function
		 */
		function radiantthemes_block_editor_style() {
			wp_register_style(
				'radiantthemes_editor',
				get_parent_theme_file_uri( '/css/radiantthemes-editor.css' ),
				array(),
				time(),
				'all'
			);
			wp_enqueue_style( 'radiantthemes_editor' );
		}
		add_action( 'enqueue_block_editor_assets', 'radiantthemes_block_editor_style' );

		/**
		 * [attorneypress_add_and_override_panel_css description]
		 *
		 * @return void
		 */
		function attorneypress_add_and_override_panel_css() {
			wp_register_style( 'attorneypress-override-redux-panel', get_parent_theme_file_uri( '/inc/redux-framework/css/attorneypress-override-redux-panel.css' ), array(), time(), 'all' );
			wp_enqueue_style( 'attorneypress-override-redux-panel' );
		}

		// This example assumes your opt_name is set to attorneypress_theme_option, replace with your opt_name value.
		add_action( 'redux/page/attorneypress_theme_option/enqueue', 'attorneypress_add_and_override_panel_css', 2 );

		/**
		 * [attorneypress_new_icon_font description]
		 *
		 * @return void
		 */
		function attorneypress_new_icon_font() {
			wp_register_style(
				'attorneypress-redux-font-awesome',
				get_parent_theme_file_uri( '/css/font-awesome.min.css' ),
				array(),
				time(),
				'all'
			);
			wp_enqueue_style( 'attorneypress-redux-font-awesome' );
		}

		// This example assumes the opt_name is set to attorneypress_theme_option.  Please replace it with your opt_name value.
		add_action( 'redux/page/attorneypress_theme_option/enqueue', 'attorneypress_new_icon_font' );

		// Requires.
		require_once get_parent_theme_file_path( '/inc/redux-framework/admin-init.php' );
	}
}

add_action( 'after_setup_theme', 'attorneypress_setup' );

/**
 * Enqueue scripts and styles.
 */
function attorneypress_scripts() {
	// ENQUEUE STYLESHEET.
	wp_deregister_style( 'font-awesome' );
	wp_deregister_style( 'font-awesome-css' );
	wp_enqueue_style(
		'bootstrap',
		get_parent_theme_file_uri( '/css/bootstrap.min.css' ),
		array(),
		time()
	);
	wp_enqueue_style(
		'font-awesome',
		get_parent_theme_file_uri( '/css/font-awesome.min.css' ),
		array(),
		time()
	);
	wp_enqueue_style(
		'owl-carousel',
		get_parent_theme_file_uri( '/css/owl.carousel.min.css' ),
		array(),
		time()
	);
	wp_enqueue_style( 'js_composer_front' );
	if ( attorneypress_global_var( 'color_scheme', '', false ) ) {
		wp_enqueue_style(
			'attorneypress-theme-custom',
			get_parent_theme_file_uri( '/css/attorneypress-theme-custom-' . attorneypress_global_var( 'color_scheme', '', false ) . '.css' ),
			array(),
			time()
		);
		// CALL REDUX CUSTOM CSS.
		$custom_css = attorneypress_global_var( 'attorneypress_custom_css', '', false );
		wp_add_inline_style( 'attorneypress-theme-custom', $custom_css );
	} else {
		wp_enqueue_style(
			'attorneypress-theme-custom',
			get_parent_theme_file_uri( '/css/attorneypress-theme-custom-light.css' ),
			array(),
			time()
		);
	}

	wp_enqueue_style(
		'attorneypress-responsive',
		get_parent_theme_file_uri( '/css/attorneypress-responsive.css' ),
		array(),
		time()
	);
	// CALL RESET CSS IF REDUX NOT ACTIVE.
	include_once ABSPATH . 'wp-admin/includes/plugin.php';

	if ( ! class_exists( 'ReduxFrameworkPlugin' ) ) {
		/**
		 * Load Google Fonts
		 *
		 * @return [type] [description]
		 */
		function attorneypress_fonts_url() {
			$font_url = '';

			/*
			Translators: If there are characters in your language that are not supported
			by chosen font(s), translate this to 'off'. Do not translate into your own language.
			*/
			if ( 'off' !== _x( 'on', 'Google font: on or off', 'attorneypress' ) ) {
				$font_url = add_query_arg( 'family', rawurlencode( 'Montserrat:100,200,300,400,500,600,700,800,900,100italic,200italic,300italic,400italic,500italic,600italic,700italic,800italic,900italic|Libre Baskerville:400|Bitter:400' ), '//fonts.googleapis.com/css' );
			}
			return $font_url;
		}
		wp_enqueue_style(
			'google-fonts',
			attorneypress_fonts_url(),
			array(),
			'1.0.0'
		);
		wp_enqueue_style(
			'attorneypress-reset',
			get_parent_theme_file_uri( '/css/attorneypress-reset.css' ),
			array(),
			time()
		);
	}

	if ( attorneypress_global_var( 'header-style', '', false ) ) {
		wp_enqueue_style(
			'attorneypress-' . attorneypress_global_var( 'header-style', '', false ),
			get_parent_theme_file_uri( '/css/attorneypress-' . attorneypress_global_var( 'header-style', '', false ) . '-' . attorneypress_global_var( 'color_scheme', '', false ) . '.css' ),
			array(),
			time()
		);
	} else {
		wp_enqueue_style(
			'attorneypress-header-style-one-light',
			get_parent_theme_file_uri( '/css/attorneypress-header-style-one-light.css' ),
			array(),
			time()
		);
	}
	if ( attorneypress_global_var( 'footer-style', '', false ) ) {
		wp_enqueue_style(
			'attorneypress-' . attorneypress_global_var( 'footer-style', '', false ),
			get_parent_theme_file_uri( '/css/attorneypress-' . attorneypress_global_var( 'footer-style', '', false ) . '-' . attorneypress_global_var( 'color_scheme', '', false ) . '.css' ),
			array(),
			time()
		);
	} else {
		wp_enqueue_style(
			'attorneypress-footer-style-one-light',
			get_parent_theme_file_uri( '/css/attorneypress-footer-style-one-light.css' ),
			array(),
			time()
		);
	}
	wp_enqueue_style(
		'attorneypress-style',
		get_stylesheet_uri(),
		array(),
		time()
	);

	// ENQUEUE SCRIPT.
	wp_enqueue_script(
		'bootstrap',
		get_parent_theme_file_uri( '/js/bootstrap.min.js' ),
		array( 'jquery' ),
		time(),
		true
	);
	wp_enqueue_script(
		'jquery-sidr',
		get_parent_theme_file_uri( '/js/jquery.sidr.min.js' ),
		array( 'jquery' ),
		time(),
		true
	);
	wp_enqueue_script(
		'jquery-matchHeight',
		get_parent_theme_file_uri( '/js/jquery.matchHeight-min.js' ),
		array( 'jquery' ),
		time(),
		true
	);
	wp_enqueue_script(
		'jquery-sticky',
		get_parent_theme_file_uri( '/js/jquery.sticky.min.js' ),
		array( 'jquery' ),
		time(),
		true
	);
	wp_enqueue_script(
		'owl-carousel',
		get_parent_theme_file_uri( '/js/owl.carousel.min.js' ),
		array( 'jquery' ),
		time(),
		true
	);
	wp_enqueue_script(
		'waypoints',
		get_parent_theme_file_uri( '/js/waypoints.min.js' ),
		array( 'jquery' ),
		time(),
		true
	);
	wp_enqueue_script(
		'jquery-counterup',
		get_parent_theme_file_uri( '/js/jquery.counterup.min.js' ),
		array( 'jquery' ),
		time(),
		true
	);
	wp_enqueue_script(
		'isotope',
		get_parent_theme_file_uri( '/js/isotope.pkgd.min.js' ),
		array( 'jquery' ),
		time(),
		true
	);
	wp_enqueue_script(
		'attorneypress-custom',
		get_parent_theme_file_uri( '/js/attorneypress-custom.js' ),
		array( 'jquery' ),
		time(),
		true
	);

	if ( is_singular() && comments_open() && ( get_option( 'thread_comments' ) === 1 ) ) {
		// Load comment-reply.js (into footer).
		wp_enqueue_script( 'comment-reply', 'wp-includes/js/comment-reply', array(), time(), true );
	}
}

add_action( 'wp_enqueue_scripts', 'attorneypress_scripts' );

/**
 * Register widget area.
 *
 * @link https://developer.wordpress.org/themes/functionality/sidebars/#registering-a-sidebar
 */
function attorneypress_widgets_init() {
	// Attorneypress Blog | Search.
	register_sidebar(
		array(
			'name'          => esc_html__( 'Blog Sidebar', 'attorneypress' ),
			'id'            => 'sidebar-1',
			'description'   => esc_html__( 'Add widgets here.', 'attorneypress' ),
			'before_widget' => '<section id="%1$s" class="widget %2$s">',
			'after_widget'  => '</section>',
			'before_title'  => '<h2 class="widget-title">',
			'after_title'   => '</h2>',
		)
	);
		// Practice Areas Sidebar.
	register_sidebar(
		array(
			'name'          => esc_html__( 'Practice Areas Sidebar', 'attorneypress' ),
			'description'   => esc_html__( 'Add widgets here.', 'attorneypress' ),
			'id'            => 'practice-areas-sidebar',
			'before_widget' => '<section id="%1$s" class="sidebar-widget %2$s">',
			'after_widget'  => '</section>',
			'before_title'  => '<h6 class="widget-title">',
			'after_title'   => '</h6>',
		)
	);

	// Attorney Details Sidebar.
	register_sidebar(
		array(
			'name'          => esc_html__( 'Attorney Details Sidebar', 'attorneypress' ),
			'description'   => esc_html__( 'Add widgets here.', 'attorneypress' ),
			'id'            => 'attorney-listing-sidebar',
			'before_widget' => '<section id="%1$s" class="sidebar-widget %2$s">',
			'after_widget'  => '</section>',
			'before_title'  => '<h6 class="widget-title">',
			'after_title'   => '</h6>',
		)
	);

	// Attorneypress Footer Areas.
	for ( $j = 1; $j <= 4; $j++ ) {
		register_sidebar(
			array(
				'name'          => esc_html__( 'Footer | #', 'attorneypress' ) . $j . '',
				'attorneypress',
				'id'            => 'attorney-footer-area-' . $j,
				'description'   => esc_html__( 'Add widgets here.', 'attorneypress' ),
				'before_widget' => '<section id="%1$s" class="widget %2$s">',
				'after_widget'  => '</section>',
				'before_title'  => '<h6 class="widget-title">',
				'after_title'   => '</h6>',
			)
		);
	}
}

add_action( 'widgets_init', 'attorneypress_widgets_init' );

if ( ! function_exists( 'attorneypress_pagination' ) ) {

	/**
	 * Displays pagination on archive pages
	 */
	function attorneypress_pagination() {

		global $wp_query;

		$big = 999999999; // need an unlikely integer.

		$paginate_links = paginate_links(
			array(
				'base'      => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
				'format'    => '?paged=%#%',
				'current'   => max( 1, get_query_var( 'paged' ) ),
				'total'     => $wp_query->max_num_pages,
				'next_text' => esc_html__( 'Next Page &raquo;', 'attorneypress' ),
				'prev_text' => esc_html__( '&laquo; Previous Page', 'attorneypress' ),
				'end_size'  => 5,
				'mid_size'  => 5,
				'add_args'  => false,
			)
		);

		// Display the pagination if more than one page is found.
		if ( $paginate_links ) :
			?>
			<div class="pagination clearfix">
				<?php echo wp_kses_post( $paginate_links ); ?>
			</div>
			<?php
		endif;
	}
}



/**
 * Display the breadcrumbs.
 */
function attorneypress_breadcrumbs() {

	$show_on_home = 0; // 1 - show breadcrumbs on the homepage, 0 - don't show.
	$delimiter    = '<span class="gap"></span>'; // delimiter between crumbs.
	$home         = esc_html__( 'Home', 'attorneypress' ); // text for the 'Home' link.
	$show_current = 1; // 1 - show current post/page title in breadcrumbs, 0 - don't show.
	$before       = '<span class="current">'; // tag before the current crumb.
	$after        = '</span>'; // tag after the current crumb.

	global $post;
	$home_link = get_home_url( 'url' );

	if ( is_home() && is_front_page() ) {

		if ( 1 === $show_on_home ) {
			if ( 'dark' == ( attorneypress_global_var( 'color_scheme', '', false ) ) ) {
				echo '<div id="crumbs"><a href="' . esc_url( $home_link ) . '">Home</a></div>';
			} else {
				echo '<div id="crumbs"><a href="' . esc_url( $home_link ) . '"><i class="fa fa-home"></i></a></div>';
			}
		}
	} else {
		if ( 'dark' == ( attorneypress_global_var( 'color_scheme', '', false ) ) ) {
			echo '<div id="crumbs"><a href="' . esc_url( $home_link ) . '">Home</a> ' . wp_kses_post( $delimiter ) . ' ';
		} else {
			echo '<div id="crumbs"><a href="' . esc_url( $home_link ) . '"><i class="fa fa-home"></i></a> ' . wp_kses_post( $delimiter ) . ' ';
		}
		if ( is_home() ) {
			echo wp_kses_post( $before ) . get_the_title( get_option( 'page_for_posts', true ) ) . wp_kses_post( $after );
		} elseif ( is_category() ) {
			$this_cat = get_category( get_query_var( 'cat' ), false );
			if ( 0 != $this_cat->parent ) {
				echo get_category_parents( $this_cat->parent, true, ' ' . wp_kses_post( $delimiter ) . ' ' );
			}
			echo wp_kses_post( $before ) . esc_html__( 'Archive by category "', 'attorneypress' ) . single_cat_title( '', false ) . '"' . wp_kses_post( $after );
		} elseif ( is_search() ) {
			echo wp_kses_post( $before ) . esc_html__( 'Search results for "', 'attorneypress' ) . get_search_query() . '"' . wp_kses_post( $after );
		} elseif ( is_day() ) {
			echo '<a href="' . get_year_link( get_the_time( 'Y' ) ) . '">' . get_the_time( 'Y' ) . '</a> ' . wp_kses_post( $delimiter ) . ' ';
			echo '<a href="' . get_month_link( get_the_time( 'Y' ), get_the_time( 'm' ) ) . '">' . get_the_time( 'F' ) . '</a> ' . wp_kses_post( $delimiter ) . ' ';
			echo wp_kses_post( $before ) . get_the_time( 'd' ) . wp_kses_post( $after );
		} elseif ( is_month() ) {
			echo '<a href="' . get_year_link( get_the_time( 'Y' ) ) . '">' . get_the_time( 'Y' ) . '</a> ' . wp_kses_post( $delimiter ) . ' ';
			echo wp_kses_post( $before ) . get_the_time( 'F' ) . wp_kses_post( $after );
		} elseif ( is_year() ) {
			echo wp_kses_post( $before ) . get_the_time( 'Y' ) . wp_kses_post( $after );
		} elseif ( is_single() && ! is_attachment() ) {
			if ( 'post' != get_post_type() ) {
				$post_type = get_post_type_object( get_post_type() );
				$slug      = $post_type->rewrite;
				echo '<a href="' . esc_url( $home_link ) . '/' . esc_attr( $slug['slug'] ) . '/">' . esc_html( $post_type->labels->singular_name ) . '</a>';
				if ( 1 == $show_current ) {
					echo ' ' . wp_kses_post( $delimiter ) . ' ' . wp_kses_post( $before ) . get_the_title() . wp_kses_post( $after );
				}
			} else {
				$cat  = get_the_category();
				$cat  = $cat[0];
				$cats = get_category_parents( $cat, true, ' ' . wp_kses_post( $delimiter ) . ' ' );
				if ( 0 == $show_current ) {
					$cats = preg_replace( "#^(.+)\s$delimiter\s$#", '$1', $cats );
				}
				echo wp_kses_post( $cats );
				if ( 1 == $show_current ) {
					echo wp_kses_post( $before ) . get_the_title() . wp_kses_post( $after );
				}
			}
		} elseif ( ! is_single() && ! is_page() && 'post' != get_post_type() && ! is_404() ) {
			$post_type = get_post_type_object( get_post_type() );
			echo wp_kses_post( $before ) . esc_html( $post_type->labels->singular_name ) . wp_kses_post( $after );
		} elseif ( is_attachment() ) {
			$parent = get_post( $post->post_parent );
			$cat    = get_the_category( $parent->ID );
			$cat    = $cat[0];
			echo wp_kses_post( get_category_parents( $cat, true, ' ' . $delimiter . ' ' ) );
			echo '<a href="' . esc_url( get_permalink( $parent ) ) . '">' . esc_html( $parent->post_title ) . '</a>';
			if ( 1 == $show_current ) {
				echo ' ' . wp_kses_post( $delimiter ) . ' ' . wp_kses_post( $before ) . get_the_title() . wp_kses_post( $after );
			}
		} elseif ( is_page() && ! $post->post_parent ) {
			if ( 1 == $show_current ) {
				echo wp_kses_post( $before ) . get_the_title() . wp_kses_post( $after );
			}
		} elseif ( is_page() && $post->post_parent ) {
			$parent_id   = $post->post_parent;
			$breadcrumbs = array();
			while ( $parent_id ) {
				$page          = get_page( $parent_id );
				$breadcrumbs[] = '<a href="' . get_permalink( $page->ID ) . '">' . get_the_title( $page->ID ) . '</a>';
				$parent_id     = $page->post_parent;
			}
			$breadcrumbs       = array_reverse( $breadcrumbs );
			$count_breadcrumbs = count( $breadcrumbs );
			for ( $i = 0; $i < $count_breadcrumbs; $i++ ) {
				echo wp_kses_post( $breadcrumbs[ $i ] );
				if ( ( count( $breadcrumbs ) - 1 ) != $i ) {
					echo ' ' . wp_kses_post( $delimiter ) . ' ';
				}
			}
			if ( 1 == $show_current ) {
				echo ' ' . wp_kses_post( $delimiter ) . ' ' . wp_kses_post( $before ) . get_the_title() . wp_kses_post( $after );
			}
		} elseif ( is_tag() ) {
			echo wp_kses_post( $before ) . esc_html__( 'Posts tagged "', 'attorneypress' ) . single_tag_title( '', false ) . '"' . wp_kses_post( $after );
		} elseif ( is_author() ) {
			global $author;
			$userdata = get_userdata( $author );
			echo wp_kses_post( $before ) . esc_html__( 'Articles posted by ', 'attorneypress' ) . esc_html( $userdata->display_name ) . wp_kses_post( $after );
		} elseif ( is_404() ) {
			echo wp_kses_post( $before ) . esc_html__( 'Error 404', 'attorneypress' ) . wp_kses_post( $after );
		}

		if ( get_query_var( 'paged' ) ) {
			if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) {
				echo ' (';
			}
			echo esc_html_e( 'Page', 'attorneypress' ) . ' ' . get_query_var( 'paged' );
			if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) {
				echo ')';
			}
		}

		echo '</div>';
	}
}

/**
 * Make search for blog post only
 *
 * @param  [type] $query description.
 * @return [type]
 */
function attorneypress_searchfilter( $query ) {
	if ( $query->is_search ) {
		$query->set( 'post_type', 'post' );
	}
	return $query;
}

add_filter( 'pre_get_posts', 'attorneypress_searchfilter' );



if ( ! function_exists( 'attorneypress_import_files' ) ) :

	/**
	 * [attorneypress_import_files description]
	 */
	function attorneypress_import_files() {
		return array(
			array(
				'import_file_name'         => esc_html__( 'Demo One', 'attorneypress' ),
				'local_import_file'        => get_parent_theme_file_path( 'inc/import/dark/content.xml' ),
				'local_import_widget_file' => get_parent_theme_file_path( 'inc/import/dark/widgets.wie' ),
				'local_import_redux'       => array(
					array(
						'file_path'   => get_parent_theme_file_path( 'inc/import/dark/redux.json' ),
						'option_name' => 'attorneypress_theme_option',
					),
				),
				'import_preview_image_url' => 'http://themes.radiantthemes.com/attorneypress/preview_import_image-01.jpg',
				'preview_url'              => 'http://themes.radiantthemes.com/attorneypress/dark/',

			),
			array(
				'import_file_name'         => esc_html__( 'Demo Two', 'attorneypress' ),
				'local_import_file'        => get_parent_theme_file_path( 'inc/import/light/content.xml' ),
				'local_import_widget_file' => get_parent_theme_file_path( 'inc/import/light/widgets.wie' ),
				'local_import_redux'       => array(
					array(
						'file_path'   => get_parent_theme_file_path( 'inc/import/light/redux.json' ),
						'option_name' => 'attorneypress_theme_option',
					),
				),
				'import_preview_image_url' => 'http://themes.radiantthemes.com/attorneypress/preview_import_image-02.jpg',
				'preview_url'              => 'http://themes.radiantthemes.com/attorneypress/light/',
			),
			array(
				'import_file_name'         => esc_html__( 'Demo Three', 'attorneypress' ),
				'local_import_file'        => get_parent_theme_file_path( 'inc/import/dark-two/content.xml' ),
				'local_import_widget_file' => get_parent_theme_file_path( 'inc/import/dark-two/widgets.wie' ),
				'local_import_redux'       => array(
					array(
						'file_path'   => get_parent_theme_file_path( 'inc/import/dark-two/redux.json' ),
						'option_name' => 'attorneypress_theme_option',
					),
				),
				'import_preview_image_url' => 'http://themes.radiantthemes.com/attorneypress/preview_import_image-03.jpg',
				'preview_url'              => 'http://themes.radiantthemes.com/attorneypress/dark-two/',
			),

		);
	}

	add_filter( 'pt-ocdi/import_files', 'attorneypress_import_files' );
endif;

if ( ! function_exists( 'attorneypress_after_import' ) ) :
	/**
	 * [attorneypress_after_import description]
	 *
	 * @param  [type] $selected_import description.
	 */
	function attorneypress_after_import( $selected_import ) {

			// Set Menu.
			$main_menu   = get_term_by( 'name', 'Main Menu', 'nav_menu' );
			$footer_menu = get_term_by( 'name', 'Footer Menu', 'nav_menu' );
			set_theme_mod(
				'nav_menu_locations',
				array(
					'top'    => $main_menu->term_id,
					'footer' => $footer_menu->term_id,
				)
			);

			// Set Front page.
			$page         = get_page_by_title( 'Home Version 1' );
			$blog_page_id = get_page_by_title( 'Blog' );
		if ( isset( $page->ID ) ) {
			update_option( 'page_on_front', $page->ID );
			update_option( 'show_on_front', 'page' );
		}
		if ( isset( $blog_page_id->ID ) ) {
			update_option( 'page_for_posts', $blog_page_id->ID );
		}

		if ( 'Demo One' === $selected_import['import_file_name'] ) {
			$path = '/inc/import/dark/slider.zip';
		} elseif ( 'Demo Three' === $selected_import['import_file_name'] ) {
			$path = '/inc/import/dark-two/slider.zip';
		} else {
			$path = '/inc/import/light/slider.zip';
		}
			// Import Revolution Slider.
		if ( class_exists( 'RevSlider' ) ) {
			$slider_array = array(
				get_parent_theme_file_path( $path ),
			);

			$slider = new RevSlider();

			foreach ( $slider_array as $filepath ) {
				$slider->importSliderFromPost( true, true, $filepath );
			}

			echo esc_html__( ' Slider processed', 'attorneypress' );
		}

	}

	add_action( 'pt-ocdi/after_import', 'attorneypress_after_import' );
endif;

add_filter( 'pt-ocdi/regenerate_thumbnails_in_content_import', '__return_false' );
add_filter( 'pt-ocdi/disable_pt_branding', '__return_true' );

/**
 * Set Site Icon
 */
function attorneypress_site_icon() {
	if ( ! function_exists( 'has_site_icon' ) || ! has_site_icon() ) {
		if ( attorneypress_global_var( 'favicon', 'url', true ) ) :
			?>
			<link rel="icon" href="<?php echo esc_url( attorneypress_global_var( 'favicon', 'url', true ) ); ?>" sizes="32x32" />
			<link rel="icon" href="<?php echo esc_url( attorneypress_global_var( 'apple-icon', 'url', true ) ); ?>" sizes="192x192">
			<link rel="apple-touch-icon-precomposed" href="<?php echo esc_url( attorneypress_global_var( 'apple-icon', 'url', true ) ); ?>" />
			<meta name="msapplication-TileImage" content="<?php echo esc_url( attorneypress_global_var( 'apple-icon', 'url', true ) ); ?>" />
		<?php else : ?>
			<link rel="icon" href="<?php echo esc_url( get_parent_theme_file_uri( '/images/favicon.ico' ) ); ?>" sizes="32x32" />
			<link rel="icon" href="<?php echo esc_url( get_parent_theme_file_uri( '/images/favicon.ico' ) ); ?>" sizes="192x192">
			<link rel="apple-touch-icon-precomposed" href="<?php echo esc_url( get_parent_theme_file_uri( '/images/favicon.ico' ) ); ?>" />
			<meta name="msapplication-TileImage" content="<?php echo esc_url( get_parent_theme_file_uri( '/images/favicon.ico' ) ); ?>" />
		<?php endif; ?>
		<?php
	}
}
add_filter( 'wp_head', 'attorneypress_site_icon' );

add_filter(
	'wp_prepare_attachment_for_js',
	function( $response, $attachment, $meta ) {
		if (
			'image/x-icon' === $response['mime'] &&
			isset( $response['url'] ) &&
			! isset( $response['sizes']['full'] )
		) {
			$response['sizes'] = array(
				'full' => array(
					'url' => $response['url'],
				),
			);
		}
		return $response;
	},
	10,
	3
);

/**
 * Display custom wp admin notice
 */
function attorneypress_admin_notice() {
	$ver = phpversion();
	$ver = ( explode( '.', $ver ) );
	$ver = $ver[0] . '.' . $ver[1]; // ver=5.5.
	if ( $ver <= 5.5 ) {
		?>
		<div class="error">
			<p><?php esc_html_e( 'Upgrade your PHP version your running PHP version ', 'attorneypress' ); ?>
				<?php echo esc_attr( phpversion() ); ?>
			</p>
		</div>
		<?php
	}
}
add_action( 'admin_notices', 'attorneypress_admin_notice' );
