<?php
/**
 * Custom functions that act independently of the theme templates
 */


/**
 * Display image caption
 *
 * @param $val
 * @param $attr
 * @param null $content
 *
 * @return string
 */
function upright_img_caption_shortcode_filter( $val, $attr, $content = null ) {
	$attr = shortcode_atts( array(
		'id'      => '',
		'align'   => 'alignnone',
		'width'   => '',
		'caption' => ''
	), $attr );

	if ( 1 > (int) $attr['width'] || empty( $caption ) ) {
		return $val;
	}

	$id = '';
	if ( $attr['id'] ) {
		$id = 'id="' . esc_attr( $attr['id'] ) . '" ';
	}

	return '<div ' . $id . 'class="wp-caption ' . esc_attr( $attr['align'] ) . '" style="width: ' . intval( $attr['width'] ) . 'px">'
	       . do_shortcode( $content ) . '<p class="wp-caption-text">' . $attr['caption'] . '</p></div>';
}

//add_filter( 'img_caption_shortcode', 'upright_img_caption_shortcode_filter', 10, 3 );

////////////

/**
 * adding a class to the ul of menu, this is used in fallback menu
 *
 * @param $menu
 *
 * @return string
 */
function upright_strip_div_menu_page( $menu ) {
	$menu = str_replace( '<ul>', '<ul class="group">', $menu );

	return strip_tags( $menu, '<ul><li><a><span>' );
}


/**
 * Get our wp_nav_menu() fallback, wp_page_menu(), to show a home link.
 */
function upright_page_menu_args( $args ) {
	$args['show_home'] = true;

	return $args;
}

add_filter( 'wp_page_menu_args', 'upright_page_menu_args' );


/**
 * Adds custom classes to the array of body classes.
 */
function upright_body_classes( $classes ) {

	$classes[] = upright_get_option( 'layout_default' ) ? upright_get_option( 'layout_default' ) : 'left-sidebar';

	if ( upright_get_option( 'hide_layout_toggle' ) ) {
		$classes[] = 'hide-layout-toggle';
	}

	// Adds a class of group-blog to blogs with more than 1 published author
	if ( is_multi_author() ) {
		$classes[] = 'group-blog';
	}

	return $classes;
}

add_filter( 'body_class', 'upright_body_classes' );


/**
 * Filter in a link to a content ID attribute for the next/previous image links on image attachment pages
 *
 * @param $url
 * @param $id
 *
 * @return string
 */
function upright_enhanced_image_navigation( $url, $id ) {
	if ( ! is_attachment() && ! wp_attachment_is_image( $id ) ) {
		return $url;
	}

	$image = get_post( $id );
	if ( ! empty( $image->post_parent ) && $image->post_parent != $id ) {
		$url .= '#main';
	}

	return $url;
}

add_filter( 'attachment_link', 'upright_enhanced_image_navigation', 10, 2 );


/**
 * Set the excerpt length.
 *
 * @param $length
 *
 * @return int
 */
function upright_excerpt_length( $length ) {
	return 20;
}

add_filter( 'excerpt_length', 'upright_excerpt_length', 999 );


/**
 * Set the excerpt more text
 *
 * @param $more
 *
 * @return string
 */
function upright_excerpt_more( $more ) {
	return '...';
}

add_filter( 'excerpt_more', 'upright_excerpt_more' );


/**
 * Exclude some posts with certain categories from recent posts stream
 *
 * @param WP_Query $query
 */
function upright_exclude_categories( $query ) {

	if ( $query->is_home() && $query->is_main_query() && upright_get_option( 'exclude_categories' ) ) {
		$query->query_vars['category__not_in'] = upright_get_option( 'exclude_categories' );
	}

}

add_action( 'pre_get_posts', 'upright_exclude_categories' );
