<?php

// Exit if accessed directly
if (!defined('ABSPATH')) {
    exit;
}
Kanban_Terms::init();
class Kanban_Terms
{
    static $instance = false;
    static function init()
    {
        self::$instance = self::get_instance();
    }
    static function get_terms_for_posts($post_ids_arr, $post_type)
    {
        global $wpdb;
        $post_ids_str = implode(',', $post_ids_arr);
        // build look up of post taxonomies by post id
        $sql = "SELECT `{$wpdb->prefix}term_relationships`.`object_id` AS 'post_id',\n\t\t\t\t`{$wpdb->prefix}term_relationships`.`term_taxonomy_id`,\n\t\t\t\t`{$wpdb->prefix}term_taxonomy`.`taxonomy`\n\t\t\t\tFROM `{$wpdb->prefix}term_relationships`\n\t\t\t\tJOIN `{$wpdb->prefix}term_taxonomy`\n\t\t\t\tON `{$wpdb->prefix}term_taxonomy`.`term_taxonomy_id` = `{$wpdb->prefix}term_relationships`.`term_taxonomy_id`\n\t\t\t\tWHERE  `object_id` IN ({$post_ids_str})\n\t\t";
        $sql = apply_filters(sprintf('%s_sql_get_terms_for_posts', Kanban::$instance->settings->basename), $sql);
        $records = $wpdb->get_results($sql);
        $terms = array();
        foreach ($records as $record) {
            if (!isset($terms[$record->post_id])) {
                $terms[$record->post_id] = (object) array();
            }
            if (!isset($terms[$record->post_id]->{$record->taxonomy})) {
                $terms[$record->post_id]->{$record->taxonomy} = array();
            }
            $terms[$record->post_id]->{$record->taxonomy}[] = $record->term_taxonomy_id;