<commit_before><?php

/**
 * @file
 * Contains App\Controller\Post class used as a default extension of
 * Timber\Post.
 */

namespace App\Controller;

use App;
use Timber;
use TimberHelper;

class Post extends Timber\Post
{
    /** @var int The duration any template cache should stay fresh */
    public $cache_duration = DAY_IN_SECONDS;

    /**
     * Disable expensive get_post_class() funciton call which we do not use.
     */
    public function post_class($class = '')
    {
        return $class;
    }

    /**
     * Return related posts.
     */
    public function get_related($posts_per_page = 3, $args = [])
    {
        $cid = $this->generate_cid('related', func_get_args());
        if (!isset($this->related)) {
            $this->related = [];
        }

        if (isset($this->related[$cid])) {
            return $this->related[$cid];
        }

        $post = $this;
        $this->related[$cid] = TimberHelper::transient($cid, function () use ($post, $posts_per_page, $args) {
            if ($this->is_crp_active()) {
                return $post->get_related_by_crp($posts_per_page, $args);
            } else {
                return $post->get_related_by_terms($posts_per_page);
            }
        }, App\config('timber.cache') ? $this->cache_duration : false);

        return $this->related[$cid];
    }

    /**
     * Return related posts based on Contextual Related Posts result.
     */
    protected function get_related_by_crp($posts_per_page = 3, $args = [])
    {
        $related = get_crp_posts_id(array_merge($args, [
            'post_id' => $this->ID,
            'limit' => $posts_per_page,
        ]));
        if (empty($related)) {
            return [];
        }
        $related = wp_list_pluck($related, 'ID');
        return (new Timber\PostQuery($related))->get_posts();
    }

    /**
     * Return related posts based on terms.
     */
    protected function get_related_by_terms($posts_per_page = 3)
    {
        global $wpdb;
        $terms = $this->terms();
        $tids = implode(',', array_column($terms, 'id'));
        $querystr = "
            SELECT      p.*, COUNT(t.term_id) AS score
            FROM        $wpdb->posts AS p
            INNER JOIN  $wpdb->term_relationships AS tr ON p.ID = tr.object_id
            INNER JOIN  $wpdb->terms AS t ON tr.term_taxonomy_id = t.term_id
            WHERE       p.post_type = '{$this->type}'
                        AND t.term_id IN ({$tids})
                        AND p.ID NOT IN ({$this->ID})
                        AND p.post_status = 'publish'
            GROUP BY    p.ID
            ORDER BY    score DESC
            LIMIT       $posts_per_page
        ";
        $posts = $wpdb->get_results($querystr, OBJECT);
        return (new Timber\PostQuery($posts))->get_posts();
    }

    /**
     * Check if post has Contextual Related Post functionality active.
     */
    protected function is_crp_active()
    {
        if (!function_exists('get_crp_posts_id')) {
            return false;
        }
        // @see https://github.com/WebberZone/contextual-related-posts/blob/ec1ec84df057dca5f1b61695fd450776cc181dbe/includes/content.php#L57
        global $crp_settings;<commit_msg>fix sql syntax error when post has no terms<commit_after><?php

/**
 * @file
 * Contains App\Controller\Post class used as a default extension of
 * Timber\Post.
 */

namespace App\Controller;

use App;
use Timber;
use TimberHelper;

class Post extends Timber\Post
{
    /** @var int The duration any template cache should stay fresh */
    public $cache_duration = DAY_IN_SECONDS;

    /**
     * Disable expensive get_post_class() funciton call which we do not use.
     */
    public function post_class($class = '')
    {
        return $class;
    }

    /**
     * Return related posts.
     */
    public function get_related($posts_per_page = 3, $args = [])
    {
        $cid = $this->generate_cid('related', func_get_args());
        if (!isset($this->related)) {
            $this->related = [];
        }

        if (isset($this->related[$cid])) {
            return $this->related[$cid];
        }

        $post = $this;
        $this->related[$cid] = TimberHelper::transient($cid, function () use ($post, $posts_per_page, $args) {
            if ($this->is_crp_active()) {
                return $post->get_related_by_crp($posts_per_page, $args);
            } else {
                return $post->get_related_by_terms($posts_per_page);
            }
        }, App\config('timber.cache') ? $this->cache_duration : false);

        return $this->related[$cid];
    }

    /**
     * Return related posts based on Contextual Related Posts result.
     */
    protected function get_related_by_crp($posts_per_page = 3, $args = [])
    {
        $related = get_crp_posts_id(array_merge($args, [
            'post_id' => $this->ID,
            'limit' => $posts_per_page,
        ]));
        if (empty($related)) {
            return [];
        }
        $related = wp_list_pluck($related, 'ID');
        return (new Timber\PostQuery($related))->get_posts();
    }

    /**
     * Return related posts based on terms.
     */
    protected function get_related_by_terms($posts_per_page = 3)
    {
        global $wpdb;
        $terms = $this->terms();
        if (!empty($terms))
        {
            $tids = implode(',', array_column($terms, 'id'));
            $querystr = "
                SELECT      p.*, COUNT(t.term_id) AS score
                FROM        $wpdb->posts AS p
                INNER JOIN  $wpdb->term_relationships AS tr ON p.ID = tr.object_id
                INNER JOIN  $wpdb->terms AS t ON tr.term_taxonomy_id = t.term_id
                WHERE       p.post_type = '{$this->type}'
                            AND t.term_id IN ({$tids})
                            AND p.ID NOT IN ({$this->ID})
                            AND p.post_status = 'publish'
                GROUP BY    p.ID
                ORDER BY    score DESC
                LIMIT       $posts_per_page
            ";
            $posts = $wpdb->get_results($querystr, OBJECT);
            return (new Timber\PostQuery($posts))->get_posts();
        }
    }

    /**
     * Check if post has Contextual Related Post functionality active.
     */
    protected function is_crp_active()
    {
        if (!function_exists('get_crp_posts_id')) {
            return false;
        }
        // @see https://github.com/WebberZone/contextual-related-posts/blob/ec1ec84df057dca5f1b61695fd450776cc181dbe/includes/content.php#L57
        global $crp_settings;
{'fraction_non_alphanumeric': 0.1294942108470445, 'fraction_numerical': 0.008226691042047532, 'mean_word_length': 1.9113082039911309, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 49, 'https://': 2, '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': '1530980', 'n_tokens_mistral': 2074, 'n_tokens_neox': 1986, 'n_words': 439}