<?php
/**
 * Post list utilities
 *
 * @package Multi
 */

if ( ! defined( 'ABSPATH' ) ) {
	die;
}

/**
 * Prepares meta data flags for post item in a list
 */
function multimagazine_postlist_meta_flags() {
	$meta_cats   = get_theme_mod( 'multi_post_meta_categories', MULTIMAGAZINE_POST_META_CATEGORIES );
	$meta_author = get_theme_mod( 'multi_post_meta_author', MULTIMAGAZINE_POST_META_AUTHOR );
	$meta_date   = get_theme_mod( 'multi_post_meta_date', MULTIMAGAZINE_POST_META_DATE );
	$hide_more   = get_theme_mod( 'multi_postlist_hide_more', false );
	$meta_any    = 'off' !== $meta_cats || 'off' !== $meta_author || 'off' !== $meta_date;
	return array(
		'show_cats'   => $meta_cats,
		'show_author' => $meta_author,
		'show_date'   => $meta_date,
		'show_meta'   => $meta_any,
		'show_more'   => ! $hide_more,
	);
}

/**
 * Gets current page number in a paginated post list
 */
function multimagazine_postlist_page_number() {
	if ( get_query_var( 'paged' ) ) {
		$page_number = get_query_var( 'paged' );
	} elseif ( get_query_var( 'page' ) ) {
		$page_number = get_query_var( 'page' );
	} else {
		$page_number = 1;
	}

	return $page_number;
}

/**
 * Gets oldest post of selected type
 *
 * @param string $type Desired post type.
 */
function multimagazine_get_oldest_post( $type = 'post' ) {
	$options = array(
		'post_type'           => $type,
		'post_status'         => 'publish',
		'order'               => 'ASC',
		'orderby'             => 'date',
		'posts_per_page'      => 1,
		'ignore_sticky_posts' => true,
	);
	$query   = new WP_Query( $options );
	return $query->posts[0];
}

/**
 * Gets newest post of selected type
 *
 * @param string $type Desired post type.
 */
function multimagazine_get_newest_post( $type = 'post' ) {
	$options = array(
		'post_type'           => $type,
		'post_status'         => 'publish',
		'order'               => 'DESC',
		'orderby'             => 'date',
		'posts_per_page'      => 1,
		'ignore_sticky_posts' => true,
	);
	$query   = new WP_Query( $options );
	return $query->posts[0];
}
