/**
 * get popular term of each post
 *
 * @since 1.1
 *
 */
function stt2extat_popular_each_post_id($args = array())
{
    $data_query = stt2extat_data_query($args);
    $list = array_column($data_query, 'post_id');
    $list = array_values(array_unique($list));
    $data = array();
    $count = count($list);
    for ($i = 0; $i < $count; $i++) {
        $post_meta = stt2extat_get_post_terms($list[$i]);
        $maxvalue = 0;
        while (list($key, $value) = each($post_meta)) {
            if ($value[1] > $maxvalue) {
                $maxvalue = $value[1];
                $maxindex = $key;
            }
        }
        $data[$maxvalue] = $data_query[$maxindex];
    }
    krsort($data);
    return $data;
}
 /**
  * fire to delete unused terms
  * argument was set on plugin panel
  *
  * @since 1.1
  *
  */
 public function delete_unused_terms()
 {
     global $stt2extat_settings;
     $set = $stt2extat_settings;
     $args = array('date_query' => array('before' => intval(-$set['schedule']['post_modified']) . ' day'), 'schedule' => array('count' => absint($set['schedule']['count'])));
     $data = stt2extat_data_query($args);
     if (0 == count($data)) {
         exit;
     }
     $list = array_column($data, 'post_id');
     $list = array_unique($list);
     $func = function ($terms, $list, $meta_key) {
         $update = array();
         foreach ($list as $k) {
             $prev_value = stt2extat_get_post_terms($k);
             if (empty($prev_value)) {
                 delete_post_meta($k, $meta_key);
                 continue;
             }
             $meta_value = array();
             foreach ($prev_value as $key => $val) {
                 if (isset($terms[$key])) {
                     continue;
                 }
                 $meta_value[$key] = $val;
             }
             if (array_filter($meta_value)) {
                 $update['update'][] = update_post_meta($k, $meta_key, $meta_value);
             } else {
                 $update['delete'][] = delete_post_meta($k, $meta_key);
             }
         }
         return $update;
     };
     $success = call_user_func_array($func, array($data, $list, '_stt2extat'));
     if (isset($success['update'])) {
         echo 'Update ' . count($success['update']) . ' postmeta.<br />';
     }
     if (isset($success['delete'])) {
         echo 'Delete ' . count($success['delete']) . ' postmeta.';
     }
     exit;
 }
/**
 * process term list by argument
 *
 * @since 1.1
 *
*/
function stt2extat_terms_list($args = array(), $widget = false)
{
    global $stt2extat_settings;
    $default = array('text_header', 'html_heading', 'number', 'display', 'count', 'convert');
    $set = wp_array_slice_assoc($args, $default);
    if (is_object($widget) && wp_validate_boolean($widget->is_widget)) {
        if (isset($args['tax_query']) || isset($args['p'])) {
            $query = new STT2EXTAT_Query($args);
            $data = $query->terms;
        } else {
            $data = stt2extat_data_query($args);
        }
    } else {
        $set = wp_parse_args($set, $stt2extat_settings);
        if (!array_filter($set)) {
            return;
        }
        $post_id = isset($_POST['post_ID']) && defined('DOING_AJAX') && DOING_AJAX ? absint($_POST['post_ID']) : get_the_ID();
        $data = stt2extat_get_terms(array('post_id' => $post_id));
        uasort($data, '_usort_terms_by_count');
        $data = array_reverse($data, true);
    }
    if (array_filter($data)) {
        $result = stt2extat_data_format($data, $set, $widget);
        return $result;
    }
}