<?php
/**
 * Functions which enhance the theme by hooking into WordPress
 *
 * @package WordPress
 * @subpackage amaThambo
 * @since 1.0
 * @version 1.0
 */

if ( ! function_exists( 'amathambo_body_classes' ) ) {
	/**
	 * Adds custom classes to the array of body classes.
	 *
	 * @param array $classes Classes for the body element.
	 * @return array
	 */
	function amathambo_body_classes( $classes ) {
		// Adds a class of hfeed to non-singular pages.
		if ( ! is_singular() ) {
			$classes[] = 'hfeed';
		}

		return $classes;
	}
	add_filter( 'body_class', 'amathambo_body_classes' );
}

if ( ! function_exists( 'amathambo_pingback_header' ) ) {
	/**
	 * Add a pingback url auto-discovery header for single posts, pages, or attachments.
	 */
	function amathambo_pingback_header() {
		if ( is_singular() && pings_open() ) {
			echo '<link rel="pingback" href="', esc_url( get_bloginfo( 'pingback_url' ) ), '">';
		}
	}
	add_action( 'wp_head', 'amathambo_pingback_header' );
}

if ( ! function_exists( 'amathambo_dynamic_widgets' ) ) {
	/**
	 * Add dynamic widget areas in footer.
	 */
	function amathambo_dynamic_widgets() {

		$widget_area = sanitize_html_class( 'footer-widget-area' );
		$widget = sanitize_html_class( 'widget-area-' );
		$sidebar_prefix = 'footer-';

		echo '<aside class="' . esc_attr( $widget_area ) . '" role="complementary">';

		$active = 0;

		for ( $count = 1; $count <= 3; $count++ ) {
			$sidebar = $sidebar_prefix;
			$sidebar .= (int) $count;
			if ( is_active_sidebar( $sidebar ) ) {
				$active++;
			}
		}
		if ( $active > 0 ) {
			$widget .= (int) $active;
			for ( $count = 1; $count <= 3; $count++ ) {
				$sidebar = $sidebar_prefix;
				$sidebar .= (int) $count;
				if ( is_active_sidebar( $sidebar ) ) {
					echo '<div class="widget ' . esc_attr( $widget ) . '">';
					dynamic_sidebar( $sidebar );
					echo '</div><!-- .widget-area-? -->';
				}
			}
		}
		echo '</aside><!-- .footer-widget-area -->';
	}
} // End if().

if ( ! function_exists( 'amathambo_custom_excerpt_length' ) ) {
	/**
	 * Filter the except length to 25 words.
	 *
	 * @param int $length Excerpt length.
	 * @return int (Maybe) modified excerpt length.
	 */
	function amathambo_custom_excerpt_length( $length ) {
		return 25;
	}
	add_filter( 'excerpt_length', 'amathambo_custom_excerpt_length', 999 );

	/**
	 * Change ending of excerpt.
	 *
	 * @param int $more excerpt ending.
	 * @return string.
	 */
	function amathambo_excerpt_more( $more ) {
		return '...';
	}
	add_filter( 'excerpt_more', 'amathambo_excerpt_more' );
}

if ( ! function_exists( 'amathambo_gutenberg_color_palette' ) ) {
	/**
	 * Add support for custom color palettes in Gutenberg.
	 */
	function amathambo_gutenberg_color_palette() {
		add_theme_support(
			'editor-color-palette', array(
				array(
					'name'  => esc_html__( 'White', 'amathambo' ),
					'slug' => 'white',
					'color' => '#ffffff',
				),
				array(
					'name'  => esc_html__( 'Lightest Grey', 'amathambo' ),
					'slug' => 'lightest-grey',
					'color' => '#f5f5f5',
				),
				array(
					'name'  => esc_html__( 'Lighter Grey', 'amathambo' ),
					'slug' => 'lighter-grey',
					'color' => '#d1d1d1',
				),
				array(
					'name'  => esc_html__( 'Light Grey', 'amathambo' ),
					'slug' => 'light-grey',
					'color' => '#575757',
				),
				array(
					'name'  => esc_html__( 'Red', 'amathambo' ),
					'slug' => 'red',
					'color' => '#a30f1e',
				),
				array(
					'name'  => esc_html__( 'Blue', 'amathambo' ),
					'slug' => 'blue',
					'color' => '#15587e',
				),
				array(
					'name'  => esc_html__( 'Black', 'amathambo' ),
					'slug' => 'black',
					'color' => '#000000',
				),
			)
		);
	}
	add_action( 'after_setup_theme', 'amathambo_gutenberg_color_palette' );
} // End if().

if ( ! function_exists( 'amathambo_search_filter' ) ) {
	if ( ! is_admin() ) {
		/**
		 * Exclude pages from WordPress search results.
		 *
		 * @param string $query search query.
		 * @return string search results without pages.
		 */
		function amathambo_search_filter( $query ) {
			if ( $query->is_search ) {
				$query->set( 'post_type', 'post' );
			}
			return $query;
		}
		add_filter( 'pre_get_posts','amathambo_search_filter' );
	}
}

if ( ! function_exists( 'amathambo_comment_args' ) ) {
	/**
	 * Sets arguments for comments
	 * Sets comment policy, the reply title on the comment form as well as sets the textarea to required.
	 */
	function amathambo_comment_args() {
		$commenter = wp_get_current_commenter();
		$req = get_option( 'require_name_email' );
		$aria_req = ( $req ? " aria-required='true'" : '' );

		$fields = array(
			'author' => '<p class="comment-form-author"><label for="author">' . __( 'Name', 'amathambo' ) . '</label> ' . ( $req ? '<span class="required">*</span>' : '' ) . '<br /><input id="author" name="author" type="text" value="' . esc_attr( $commenter['comment_author'] ) . '" size="30"' . $aria_req . ' /></p>',
			'email' => '<p class="comment-form-email"><label for="email">' . __( 'Email', 'amathambo' ) . '</label> ' . ( $req ? '<span class="required">*</span>' : '' ) .
			'<br /><input id="email" name="email" type="text" value="' . esc_attr( $commenter['comment_author_email'] ) . '" size="30"' . $aria_req . ' /></p>',
			'url' => '<p class="comment-form-url"><label for="url">' . __( 'Website', 'amathambo' ) . '</label> <br /><input id="url" name="url" type="text" value="' . esc_attr( $commenter['comment_author_url'] ) . '" size="30" /></p>',
		);
		$consent = empty( $commenter['comment_author_email'] ) ? '' : ' checked="checked"';
		$fields['cookies'] = '<p class="comment-form-cookies-consent"><input id="wp-comment-cookies-consent" name="wp-comment-cookies-consent" type="checkbox" value="yes"' . $consent . ' /><label for="wp-comment-cookies-consent" class="consent-label">' . __( 'Save my name, email, and website in this browser for the next time I comment.', 'amathambo' ) . '</label></p>';

		if ( isset( $args['fields'] ) && ! isset( $args['fields']['cookies'] ) ) {
			$args['fields']['cookies'] = $fields['cookies'];
		}
		$comments_args = array(
			'title_reply' => __( 'Start or Join the Conversation', 'amathambo' ),
			'comment_notes_after' => '',
			'comment_field' => '<p style="margin-bottom: 0;" class="comment-form-comment"><label for="comment">' . __( 'Comment', 'amathambo' ) . '<span class="required"> *</span></label><br /><textarea id="comment" name="comment" aria-required="true"></textarea></p>',
			'fields' => apply_filters( 'comment_form_default_fields', $fields ),
		);
		comment_form( $comments_args );

	}
	add_action( 'amathambo_comment_args', 'amathambo_comment_args' );
}

if ( ! function_exists( 'amathambo_top_link' ) ) {
	/**
	 * Add scroll to top button
	 */
	function amathambo_top_link() {
		echo '<a href="#" class="topbutton"><span class="anchor">Top</span><span class="screen-reader-text">Scroll to top of screen</span></a>';
	}
	add_action( 'wp_footer', 'amathambo_top_link' );
}

if ( ! function_exists( 'amathambo_dynamic_widgets' ) ) {
	/**
	 * Add dynamic widget areas in footer.
	 */
	function amathambo_dynamic_widgets() {

		$widget_area = sanitize_html_class( 'footer-widget-area' );
		$widget = sanitize_html_class( 'widget-area-' );
		$sidebar_prefix = 'footer-';

		echo '<div class="' . esc_attr( $widget_area ) . '">';

		$active = 0;

		for ( $count = 1; $count <= 3; $count++ ) {
			$sidebar = $sidebar_prefix;
			$sidebar .= (int) $count;
			if ( is_active_sidebar( $sidebar ) ) {
				$active++;
			}
		}
		if ( $active > 0 ) {
			$widget .= (int) $active;
			for ( $count = 1; $count <= 3; $count++ ) {
				$sidebar = $sidebar_prefix;
				$sidebar .= (int) $count;
				if ( is_active_sidebar( $sidebar ) ) {
					echo '<div class="' . esc_attr( $widget ) . '">';
					dynamic_sidebar( $sidebar );
					echo '</div>';
				}
			}
			echo '</div>';
		}
		echo '</div>';?>

	<?php } ?>

<?php } // End if().

if ( ! function_exists( 'amathambo_starter_filter_login_head' ) ) {
	/**
	 * Can only happen if there is a custom logo
	 */
	if ( has_custom_logo() ) {
		/**
		 * Adds custom logo to WordPress login page.
		 */
		function amathambo_starter_filter_login_head() {
				$image = wp_get_attachment_image_src( get_theme_mod( 'custom_logo' ), 'full' );?>
				<style type="text/css">
					.login h1 a {
						background-image: url(<?php echo esc_url( $image[0] ); ?>);
						-webkit-background-size: <?php echo absint( $image[1] )?>px;
						background-size: <?php echo absint( $image[1] ) ?>px;
						height: <?php echo absint( $image[2] ) ?>px;
						width: <?php echo absint( $image[1] ) ?>px;
					}
				</style>
		<?php }
		add_action( 'login_head', 'amathambo_starter_filter_login_head', 100 );
	}
}

if ( ! function_exists( 'amathambo_add_img_alt' ) ) {
	/**
	 * Adds alt attribute to featured images.
	 *
	 * @param string $html relevant html.
	 * @param string $post_id relevant post id.
	 * @param string $post_thumbnail_id relevant thumbnail id.
	 * @param string $size relevant size of image.
	 * @param string $attr relevant attributes of image.
	 * @return string returns string with alt added.
	 */
	function amathambo_add_img_alt( $html, $post_id, $post_thumbnail_id, $size, $attr ) {

		$post_title = get_the_title();
		$html = preg_replace( '/(alt=")(.*?)(")/i', '$1Featured image for the post ' . esc_attr( $post_title ) . '$3', $html );

		return $html;

	}
	add_filter( 'post_thumbnail_html', 'amathambo_add_img_alt', 10, 5 );
}

if ( ! function_exists( 'amathambo_add_search_form' ) ) {
	/**
	 * Appends search form to primary menu.
	 *
	 * @param string $items string to which we are appending.
	 * @param array  $args is this the correct menu.
	 * @return string returns string with search added.
	 */
	function amathambo_add_search_form( $items, $args ) {
		if ( 'primary' === $args->theme_location ) {
			  $items .= '<li class="search"><form role="search" method="get" class="search-form" action="' . esc_url( home_url( '/' ) ) . '"><label><span class="screen-reader-text">' . esc_attr__( 'Search for', 'amathambo' ) . '</span><input type="search" class="search-field" placeholder="' . esc_attr__( 'Search &hellip;', 'amathambo' ) . '" value="' . get_search_query() . '" name="s" /></label><button type="submit" class="search-submit"><span class="screen-reader-text">' . esc_attr__( 'Search', 'amathambo' ) . '</span></button></form></li>';
		}
		return $items;
	}
	add_filter( 'wp_nav_menu_items', 'amathambo_add_search_form', 10, 2 );
}

if ( ! function_exists( 'amathambo_custom_logo_in_title_css' ) ) {
	/**
	 * Adds custom logo to site-title.
	 */
	function amathambo_custom_logo_in_title_css() {

		$custom_logo_id = get_theme_mod( 'custom_logo' );
		$image = wp_get_attachment_image_src( $custom_logo_id , 'full' );
		$amathambo_title_background_image = $image[0];

		if ( is_rtl() ) {
			$css = '.wp-custom-logo .site-title a {background-image: url("%1$s");background-size: 32px 32px;background-position: right center;background-repeat: no-repeat;padding-right: 37px;}';
		} else {
			$css = '.wp-custom-logo .site-title a {background-image: url("%1$s");background-size: 32px 32px;background-position: left center;background-repeat: no-repeat;padding-left: 37px;}';
		}

		wp_add_inline_style( 'amathambo-style', sprintf( $css, $amathambo_title_background_image ) );
	}
	add_action( 'wp_enqueue_scripts', 'amathambo_custom_logo_in_title_css', 11 );
}

if ( ! function_exists( 'amathambo_custom_required' ) ) {
	/**
	 * Customize required field labels on JetPack contact form.
	 */
	function amathambo_custom_required() {
		return __( ' *', 'amathambo' );
	}
	add_filter( 'jetpack_required_field_text', 'amathambo_custom_required' );
}
