<?php
/**
 * @package Chateau
 */

// Set the content width based on the chosen layout.
if ( ! isset( $content_width ) )
	$content_width = chateau_attachment_width();

/**
 * Returns the value of content_width depending on the current layout.
 *
 */
function chateau_attachment_width() {
	$options = get_option( 'chateau_theme_options' );
	$current_layout = $options['theme_layout'] ?? null;

	if ( 'content' == $current_layout )
		return 791;
	else
		return 529;
}

// Tell WordPress to run chateau_setup() when the 'after_setup_theme' hook is run.
add_action( 'after_setup_theme', 'chateau_setup' );

if ( ! function_exists( 'chateau_setup' ) ):

function chateau_setup() {

	/* Make Chateau available for translation.
	 * Translations can be added to the /languages/ directory.
	 * If you're building a theme based on Chateau, use a find and replace
	 * to change 'chateau' to the name of your theme in all the template files.
	 */
	load_theme_textdomain( 'chateau', get_template_directory() . '/languages' );

	// This theme styles the visual editor with editor-style.css to match the theme style.
	add_editor_style();

	// Load theme options
	require_once( get_template_directory() . '/inc/theme-options.php' );

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

	// Add support for Post Formats
	add_theme_support( 'post-formats', array( 'aside', 'image', 'video', 'quote', 'link', 'gallery' ) );

	// This theme uses wp_nav_menu() in one location.
	register_nav_menu( 'primary', __( 'Primary Menu', 'chateau' ) );

	// Add support for custom backgrounds
	add_theme_support( 'custom-background' );

	// This theme uses Featured Images (also known as post thumbnails) for per-post/per-page Custom Header images
	add_theme_support( 'post-thumbnails' );

	/*
	 * We'll be using post thumbnails for custom header images on posts and pages.
	 * We want them to be the size of the header image that we just defined
	 * Larger images will be auto-cropped to fit, smaller ones will be ignored.
	 *
	 * See header.php.
	 * See inc/custom-header.php
	 */
	set_post_thumbnail_size( 960, 260, true );
}
endif; // end chateau_setup

/**
 * Enqueue scripts and styles
 */
function chateau_scripts() {
	wp_enqueue_style( 'chateau', get_stylesheet_uri() );

	if ( is_singular() && comments_open() && get_option( 'thread_comments' ) )
		wp_enqueue_script( 'comment-reply' );
}
add_action( 'wp_enqueue_scripts', 'chateau_scripts' );

// Allow custom colors to match the background color of some elements
function chateau_custom_background_color() {
	if ( '' != get_background_color() ) {
	$custombackgroundcolor = get_background_color();
?>

		<style type="text/css">
		#menu li,
		.post-date em,
		.sticky .entry-format,
		.more-posts .sticky h2.entry-format,
		.more-posts h2.entry-format,
		#comments h3 span {
			background-color: #<?php echo $custombackgroundcolor; ?>;
		}

		#menu a {
			border-color: #<?php echo $custombackgroundcolor; ?>
		}
		</style>
<?php
	}
	if ( '' != get_background_image() ) {
?>
		<style type="text/css">
		#menu li,
		.sticky .entry-format,
		.more-posts .sticky h2.entry-format,
		.more-posts h2.entry-format,
		#comments h3 span {
			background-color: transparent;
		}

		#menu a {
			border-color: transparent;
		}
		</style>
<?php
	}
}
add_action( 'wp_head', 'chateau_custom_background_color' );

// Register widgetized areas
function chateau_widgets_init() {
	register_sidebar( array(
		'name' => __( 'Sidebar Widget Area', 'chateau' ),
		'id' => 'sidebar',
		'description' => __( 'The Sidebar', 'chateau' ),
		'before_widget' => '<aside id="%1$s" class="sidebar-widget %2$s">',
		'after_widget' => '</aside>',
		'before_title' => '<h1 class="widget-title">',
		'after_title' => '</h1>',
	));

	register_sidebar( array(
		'name' => __( 'Upper Footer Widget Area', 'chateau' ),
		'id' => 'upper-footer-widget-area',
		'description' => __( 'A single full-width footer column', 'chateau' ),
		'before_widget' => '<aside id="%1$s" class="footer-widget %2$s clear-fix">',
		'after_widget' => '</aside>',
		'before_title' => '<h1>',
		'after_title' => '</h1>',
	));

	register_sidebar( array(
		'name' => __( 'First Footer Widget Area', 'chateau' ),
		'id' => 'first-footer-widget-area',
		'description' => __( 'The first column below the Upper Footer Widget Area', 'chateau' ),
		'before_widget' => '<aside id="%1$s" class="footer-widget %2$s">',
		'after_widget' => '</aside>',
		'before_title' => '<h1>',
		'after_title' => '</h1>',
	));

	register_sidebar( array(
		'name' => __( 'Second Footer Widget Area', 'chateau' ),
		'id' => 'second-footer-widget-area',
		'description' => __( 'The second column below the Upper Footer Widget Area', 'chateau' ),
		'before_widget' => '<aside id="%1$s" class="footer-widget %2$s">',
		'after_widget' => '</aside>',
		'before_title' => '<h1>',
		'after_title' => '</h1>',
	));

	register_sidebar( array(
		'name' => __( 'Third Footer Widget Area', 'chateau' ),
		'id' => 'third-footer-widget-area',
		'description' => __( 'The third column below the Upper Footer Widget Area', 'chateau' ),
		'before_widget' => '<aside id="%1$s" class="footer-widget %2$s">',
		'after_widget' => '</aside>',
		'before_title' => '<h1>',
		'after_title' => '</h1>',
	));

	register_sidebar( array(
		'name' => __( 'Fourth Footer Widget Area', 'chateau' ),
		'id' => 'fourth-footer-widget-area',
		'description' => __( 'The fourth column below the Upper Footer Widget Area', 'chateau' ),
		'before_widget' => '<aside id="%1$s" class="footer-widget %2$s">',
		'after_widget' => '</aside>',
		'before_title' => '<h1>',
		'after_title' => '</h1>',
	));
}

add_action( 'widgets_init', 'chateau_widgets_init' );

//Changed wp_page_menu structure to get rid of the wrapped div and add menu_class arguments to <ul>
function chateau_add_menu_class ( $page_markup ) {
	preg_match( '/^<div class=\"([a-z0-9-_ ]+)\">/i', $page_markup, $matches );
	$divclass = $matches[1];
	$toreplace = array( '<div class="'.$divclass.'">', '</div>' );
	$new_markup = str_replace( $toreplace, '', $page_markup );
	$new_markup = preg_replace( '/^<ul>/i', '<ul class="'.$divclass.'">', $new_markup );
	return $new_markup;
}
add_filter( 'wp_page_menu', 'chateau_add_menu_class' );

// Display navigation to next/previous pages when applicable
function chateau_content_nav( $nav_id ) {
	global $wp_query;

	if ( $wp_query->max_num_pages > 1 ) : ?>
		<nav id="<?php echo esc_attr( $nav_id ); ?>" class="clear-fix">

			<div class="nav-previous"><?php next_posts_link( __( '&larr; Older posts', 'chateau' ) ); ?></div>
			<div class="nav-next"><?php previous_posts_link( __( 'Newer posts &rarr;', 'chateau' ) ); ?></div>
		</nav><!-- #nav-below -->
	<?php endif;
}

/**
 * Sets the post excerpt length to 20 words.
 *
 * To override this length in a child theme, remove the filter and add your own
 * function tied to the excerpt_length filter hook.
 */
function chateau_excerpt_length( $length ) {
	return 20;
}
add_filter( 'excerpt_length', 'chateau_excerpt_length' );

/**
 * Returns a "Continue Reading" link for excerpts
 */
function chateau_continue_reading_link() {
	return '<p><a href="'. esc_url( get_permalink() ) . '">' . __( 'Continue reading <span class="meta-nav">&rarr;</span>', 'chateau' ) . '</a></p>';
}

/**
 * Replaces "[...]" (appended to automatically generated excerpts) with an ellipsis and chateau_continue_reading_link().
 *
 * To override this in a child theme, remove the filter and add your own
 * function tied to the excerpt_more filter hook.
 */
function chateau_auto_excerpt_more( $more ) {
	return ' &hellip;' . chateau_continue_reading_link();
}
add_filter( 'excerpt_more', 'chateau_auto_excerpt_more' );

/**
 * Adds a pretty "Continue Reading" link to custom post excerpts.
 *
 * To override this link in a child theme, remove the filter and add your own
 * function tied to the get_the_excerpt filter hook.
 */
function chateau_custom_excerpt_more( $output ) {
	if ( has_excerpt() && ! is_attachment() ) {
		$output .= chateau_continue_reading_link();
	}
	return $output;
}
add_filter( 'get_the_excerpt', 'chateau_custom_excerpt_more' );

if ( ! function_exists( 'chateau_comment' ) ) :
/**
 * Template for comments and pingbacks.
 *
 * To override this walker in a child theme without modifying the comments template
 * simply create your own chateau_comment(), and that function will be used instead.
 *
 * Used as a callback by wp_list_comments() for displaying the comments.
 */
	function chateau_comment( $comment, $args, $depth ) {
		$GLOBALS['comment'] = $comment;
		switch ( $comment->comment_type ) :
			case 'pingback' :
			case 'trackback' :
			?>
			<li class="comment pingback">
				<div class="comment-text">
					<p><?php _e( 'Pingback:', 'chateau' ); ?> <?php comment_author_link(); ?></p>
					<p class="edit-comment"><?php edit_comment_link( __( 'Edit', 'chateau' ), ' ' ); ?></p>
				</div>
			<?php
					break;
				default :
			?>

			<li <?php comment_class( empty( $args['has_children'] ) ? '' : 'parent' ); ?> id="comment-<?php comment_ID(); ?>">

				<div class="comment-heading clear-fix">
					<p class="comment-author">
						<?php if ( $args['avatar_size'] != 0 ) echo get_avatar( $comment, $args['avatar_size'] ); ?>
						<?php printf( __( '<span>%s</span> <em>said:</em>', 'chateau' ), get_comment_author_link() ); ?>
					</p>
					<p class="comment-date">
						<?php
							printf( '<a href="%1$s"><time pubdate datetime="%2$s">%3$s</time></a>',
								esc_url( get_comment_link( $comment->comment_ID ) ),
								get_comment_time( 'c' ),
								sprintf(__( '%1$s at %2$s', 'chateau' ), get_comment_date(),  get_comment_time() )
							);
						?>
					</p>
				</div>
				<div class="comment-text">
					<?php comment_text(); ?>
					<?php if ( $comment->comment_approved == '0' ) : ?>
					<p class="waiting4mod"><?php _e( 'Your comment is awaiting moderation.', 'chateau' ); ?></p>
					<?php endif; ?>
					<p class="reply-link"><?php comment_reply_link( array_merge( $args, array( 'reply_text' => __( 'Reply', 'chateau' ), 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?></p>
					<p class="edit-comment"><?php edit_comment_link( __( '[Edit Comment]', 'chateau' ),'','' ); ?></p>
				</div>
		<?php
				break;
		endswitch;
	}
endif; // ends check for chateau_comment()

function chateau_post_date() {
	$date_format = get_option( 'date_format' );
	if ( 'l, F j, Y' == $date_format ) : ?>
		<p class="post-date one-line"><?php the_time( get_option( 'date_format' ) ); ?></p>
	<?php else: ?>
		<p class="post-date">
			<strong><?php the_time( 'd' ); ?></strong>
			<em><?php the_time( 'l' ); ?></em>
			<span><?php the_time( 'M Y' ); ?></span>
		</p>
	<?php endif;
}

// Show post-date and post-info for use in loop
function chateau_post_info() {
?>
	<a href="<?php the_permalink(); ?>" class="post-permalink"><?php chateau_post_date(); ?></a>
	<div class="post-info clear-fix">
		<p>
			<?php
				printf( __ ( 'Posted <span class="by-author"> by <span class="author vcard"><a class="url fn n" href="%1$s" title="%2$s" rel="author">%3$s</a></span></span> in %4$s'),
					esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
					esc_attr( sprintf( __( 'View all posts by %s', 'chateau' ), get_the_author() ) ),
					esc_html( get_the_author() ),
					get_the_category_list( ', ' )
				);
			?>
		</p>
		<p class="post-com-count">
			<strong>&asymp; <?php comments_popup_link( __( 'Leave a comment', 'chateau' ), __( '1 Comment', 'chateau' ), __( '% Comments', 'chateau' ) ); ?></strong>
		</p>
	</div><!-- end .post-info -->
<?php
}

// Show post-extras for use in loop
function chateau_post_extra() { ?>
	<div class="post-extras">
		<?php edit_post_link( __( 'Edit', 'chateau' ), '<p>[', ']</p>'); ?>
		<?php the_tags( '<p><strong>' . __( 'Tags','chateau' ) . '</strong></p><p>', ', ', '</p>' ); ?>
	</div><!-- end .post-extras -->
<?php
}

/**
 * Filters wp_title to print a neat <title> tag based on what is being viewed.
 *
 * @since Chateau 1.0
 */
function chateau_wp_title( $title, $sep ) {
	global $page, $paged;

	if ( is_feed() )
		return $title;

	// Add the blog name
	$title .= get_bloginfo( 'name' );

	// Add the blog description for the home/front page.
	$site_description = get_bloginfo( 'description', 'display' );
	if ( $site_description && ( is_home() || is_front_page() ) )
		$title .= " $sep $site_description";

	// Add a page number if necessary:
	if ( $paged >= 2 || $page >= 2 )
		$title .= " $sep " . sprintf( __( 'Page %s', 'chateau' ), max( $paged, $page ) );

	return $title;
}
add_filter( 'wp_title', 'chateau_wp_title', 10, 2 );

/*
 * Implement the Custom Header feature
 */
require( get_template_directory() . '/inc/custom-header.php' );