<?php
/**
 * Functions that affect parts of the front-end that aren't included elsewhere.
 * Ex: body class, breadcrumbs, title, plugin customizations
 *
 * Functions that modify items in the <head>, are in functions-html-head.php
 *
 * This file is included by the AUX parent theme functions.php. If a child theme
 * has a file of the same name in its 'inc' folder, that file will be included
 * immediately AFTER this file.
 *
 * @since 1.0.0
 */
if (!defined("ABSPATH")) {
  exit();
} // Exit if accessed directly

/**
 * Tell WordPress that we have moved default template files to the page-templates folder.
 * Based on code from: https://www.reddit.com/r/Wordpress/comments/ffhjvw/moving_wordpress_theme_template_files_to/
 * Template hierarchies found in https://developer.wordpress.org/reference/hooks/type_template_hierarchy/ https://core.trac.wordpress.org/browser/tags/5.8.1/src/wp-includes/template.php.
 *
 * @param array $templates a list of candidates template files
 * @since 1.0.0
 */
function aux_change_template_path($templates):array {
  // The custom sub-directory for page templates in your theme.
  $custom_sub_dir = "templates";

  // Don't use the custom template directory in unexpected cases.
  if (empty($templates) || !is_array($templates)) {
    return $templates;
  }

  $page_template_id = 0;
  $count = count($templates);
  if ($templates[0] === get_page_template_slug()) {
    // if there is a custom template, then our page-{slug}.php template is at the next index
    $page_template_id = 1;
  }

  // The last one in $templates is page.php, single.php, or archives.php depending on the type of template hierarchy being read.
  // Paths of all items starting from $page_template_id will get updated
  for ($i = $page_template_id; $i < $count; ++$i) {
    $templates[$i] = $custom_sub_dir . "/" . $templates[$i];
  }

  return $templates;
}

// Add filters to override the path for each WP template hierarchy.
// These are all the template hierarchy filters in WP.
$templateHierarchies = [
  '404_template_hierarchy',
  'archive_template_hierarchy',
  'attachment_template_hierarchy',
  'author_template_hierarchy',
  'category_template_hierarchy',
  'date_template_hierarchy',
  'embed_template_hierarchy',
  'frontpage_template_hierarchy',
  'home_template_hierarchy',
  'index_template_hierarchy',
  'page_template_hierarchy',
  'paged_template_hierarchy',
  'privacypolicy_template_hierarchy',
  'search_template_hierarchy',
  'single_template_hierarchy',
  'singular_template_hierarchy',
  'tag_template_hierarchy',
  'taxonomy_template_hierarchy',
];

foreach ($templateHierarchies as $templateHierarchy) {
  add_filter($templateHierarchy, 'aux_change_template_path');
}

/**
 * Remove author archive sitemap from WordPress sitemap, wp-sitemap-users-1.xml
 * wp_sitemaps_add_provider – Filters the sitemap provider before it is added.
 * https://make.wordpress.org/core/2020/07/22/new-xml-sitemaps-functionality-in-wordpress-5-5/
 * https://developer.wordpress.org/reference/hooks/wp_sitemaps_add_provider/.
 *
 * @param WP_Sitemaps_Provider $provider instance of a WP_Sitemaps_Provider
 * @param string               $name     name of the sitemap provider
 *
 * @return WP_Sitemaps_Provider $provider - Instance of a WP_Sitemaps_Provider
 *
 * @since 1.0.0
 */
function aux_remove_author_archive_from_sitemap(\WP_Sitemaps_Provider $provider, string $name) {
  if ("users" === $name) {
    return false;
  }

  return $provider;
}

add_filter(
  "wp_sitemaps_add_provider",
  "aux_remove_author_archive_from_sitemap",
  10,
  2
);

// Remove Gravity Forms styles
add_filter('gform_disable_css', '__return_true');

/**
 * Remove the author link from post meta
 *
 * @since 1.0.0
 */
function remove_author_link(): void {
  // Remove author link from the post meta
  remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10);
  add_filter('the_author_posts_link', '__return_false');
}
add_action('init', 'remove_author_link');

/**
 * Filter the author display to an empty string
 *
 * @since 1.0.0
 */
function hide_author_name(?string $author) {
  return '';
}
add_filter('the_author', 'hide_author_name');
add_filter('get_the_author', 'hide_author_name');

/**
 * @todo Refactor with constants in Search.php
 */
add_filter('get_search_query', function (string $searchString): string {
  $pattern = '/[^a-z 0-9]/i';

  $searchString = preg_replace($pattern, ' ', $searchString);

  if (strlen($searchString) > 100) {
    $searchString = mb_substr($searchString, 0, 100) . '...';
  }

  return $searchString;
});

function add_class_to_excerpt($excerpt): string {
  return str_replace('<p', '<p class="page-header__description"', $excerpt);
}
add_filter( "the_excerpt", "add_class_to_excerpt" );

function header_spacing_kluge($classes): array
{
  $classes[] = 'h-spacing-kluge';

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

/**
 * Wrap content from the Classic Editor block in a section class="section-wrap"
 *
 * @param   string $block_content  Block content from Gutenberg
 * @param   array  $block          The full block, including name and attributes.
 *
 * @since   1.0.0
 */
add_filter('render_block', function($block_content, $block) {
  $block_content = trim($block_content);

  // Make sure the content area isn't empty and check for the Classic block
  if (!empty($block_content) && ($block['blockName'] === 'core/freeform' || $block['blockName'] === null)) {
    return <<<HTML
      <section class="section-wrap">
        <div class="custom-content__wrapper">
          <div class="custom-content__content">
            $block_content
          </div>
        </div>
      </section>
    HTML;
  }
  return $block_content;
}, 10, 2);

/**
 * Disable the Gravity Forms CSS themes. They can override the AUX form styles.
 * Thank you to Mark Bennett for the suggestion and solution!
 * @link https://docs.gravityforms.com/gform_disable_form_theme_css/
 *
 * @since 1.0.2
 */
add_filter('gform_disable_form_theme_css', '__return_true');
