<?php
/**
 * Functions which enhance the theme by hooking into WordPress
 *
 * @package MarComm-AUX
 */

use WP_AUX\Entities\PaginationEntity;
use WP_AUX\Services\PageTemplate\LayoutService;

/**
 * Return AUX pagination as a string
 *
 * @return  string
 */
function aux_pagination(): string {
  $pagination = new PaginationEntity();

  return $pagination->hasLinks() ? $pagination : '';
}

/* Breadcrumbs!! */
function aux_breadcrumbs(bool $onDark = false): void {
  $breadcrumbs = LayoutService::breadcrumbs();

  if ($onDark) {
    $breadcrumbs->addClasses(['breadcrumbs--on-dark']);
  }

  echo $breadcrumbs;
}

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

	// Adds a class of no-sidebar when there is no sidebar present.
	if ( ! is_active_sidebar( 'sidebar-1' ) ) {
		$classes[] = 'no-sidebar';
	}

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

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


/**
 * Get the URL of the featured image thumbnail.
 * This theme's custom image sizes are defined in wp-content/themes/functions-images.php
 *
 * @param   array   $post      WordPress post array
 * @param   string  $img_size  Thumbnail size from options available in functions-images.php
 *
 * @return  null|string        The featured image thumbnail URL
 *
 * @since   1.0.0
 */
function aux_get_featured_img_url($post, $img_size) {
  $thumbnail_id = get_post_thumbnail_id($post['id']);
  if ($thumbnail_id) {
    $thumbnail_url = wp_get_attachment_image_src($thumbnail_id, $img_size);
    if ($thumbnail_url) {
      return $thumbnail_url[0];
    }
  }
  return null;
}
