<commit_before><?php
/*
 * This file is part of the Hierarchy package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Brain\Hierarchy;

/**
 * @author  <NAME> <<EMAIL>>
 * @license http://opensource.org/licenses/MIT MIT
 */
class PostTemplates
{

    /**
     * @var array[]
     */
    private $templates = [];

    /**
     * @param \WP_Post $post
     * @return string
     */
    public function findFor(\WP_Post $post)
    {
        if (!$post->ID || !$post->post_type) {
            return '';
        }

        $stored = filter_var(get_page_template_slug($post), FILTER_SANITIZE_URL);
        if (!$stored || validate_file($stored) !== 0) {
            return '';
        }

        $stored = wp_normalize_path($stored);
        $templates = $this->templatesForType($post->post_type);

        foreach ($templates as $template) {
            if ($template === $stored) {
                $dir = dirname($template);
                $filename = pathinfo($template, PATHINFO_FILENAME);

                return $dir === '.' ? $filename : "{$dir}/{$filename}";
            }
        }

        return '';
    }

    /**
     * @param string $postType
     * @return string[]
     */
    private function templatesForType($postType)
    {
        if (array_key_exists($postType, $this->templates)) {
            return $this->templates[$postType];
        }

        $this->templates[$postType] = [];
        $templates = (array)wp_get_theme()->get_page_templates(null, $postType);
        foreach ($templates as $template => $header) {
            if ($template && is_string($template)) {
                $this->templates[$postType][] = wp_normalize_path($template);<commit_msg>Fix templateForType not matching $stored when using åäö in template file name on custom post type<commit_after><?php
/*
 * This file is part of the Hierarchy package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Brain\Hierarchy;

/**
 * @author  <NAME> <<EMAIL>>
 * @license http://opensource.org/licenses/MIT MIT
 */
class PostTemplates
{

    /**
     * @var array[]
     */
    private $templates = [];

    /**
     * @param \WP_Post $post
     * @return string
     */
    public function findFor(\WP_Post $post)
    {
        if (!$post->ID || !$post->post_type) {
            return '';
        }

        $stored = filter_var(get_page_template_slug($post), FILTER_SANITIZE_URL);
        if (!$stored || validate_file($stored) !== 0) {
            return '';
        }

        $stored = wp_normalize_path($stored);
        $templates = $this->templatesForType($post->post_type);

        foreach ($templates as $template) {
            if ($template === $stored) {
                $dir = dirname($template);
                $filename = pathinfo($template, PATHINFO_FILENAME);

                return $dir === '.' ? $filename : "{$dir}/{$filename}";
            }
        }

        return '';
    }

    /**
     * @param string $postType
     * @return string[]
     */
    private function templatesForType($postType)
    {
        if (array_key_exists($postType, $this->templates)) {
            return $this->templates[$postType];
        }

        $this->templates[$postType] = [];
        $templates = (array)wp_get_theme()->get_page_templates(null, $postType);
        foreach ($templates as $template => $header) {
            if ($template && is_string($template)) {
                $sanitized = filter_var($template, FILTER_SANITIZE_URL);
                $this->templates[$postType][] = wp_normalize_path($sanitized);
{'fraction_non_alphanumeric': 0.15021231422505307, 'fraction_numerical': 0.0005307855626326964, 'mean_word_length': 2.254749568221071, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4785040', 'n_tokens_mistral': 1128, 'n_tokens_neox': 1094, 'n_words': 245}