<?php

defined('UNLIMITED_ELEMENTS_INC') or die('restricted aceess');
class UniteCreatorPluginIntegrations
{
    public static function isWPPopularPostsExists()
    {
        $isExists = defined("WPP_VERSION") && defined("WPP_MIN_PHP_VERSION");
        return $isExists;
    }
    public function WPP_getPopularPosts($args, $addDebug = false)
    {
        $isExists = self::isWPPopularPostsExists();
        if ($isExists == false) {
            return false;
        }
        $postType = UniteFunctionsUC::getVal($args, "post_type");
        if (is_array($postType)) {
            $postType = $postType[0];
        }
        if (empty($postType)) {
            $postType = "post";
        }
        $limit = UniteFunctionsUC::getVal($args, "limit", 5);
        $range = UniteFunctionsUC::getVal($args, "range", "last7days");
        $cat = UniteFunctionsUC::getVal($args, "cat", "");
        if (is_array($cat)) {
            $cat = $cat[0];
        }
        $params = array();
        $params["post_type"] = $postType;
        $params["limit"] = $limit;
        $params["range"] = $range;
        if (!empty($cat)) {
            $params["cat"] = $cat;
        }
        $query = new \WordPressPopularPosts\Query($params);
        $arrPosts = $query->get_posts();
        if (empty($arrPosts)) {
            $arrPosts = array();
        }
        $arrPosts = UniteFunctionsUC::convertStdClassToArray($arrPosts);
        $strDebug = "";
        $arrPostIDs = array();
        if ($addDebug == true) {
            $strDebug .= "Popular posts query arguments:";
            $strDebug .= "<pre>";
            $strDebug .= print_r($params, true);
            $strDebug .= "</pre>";
            $numPosts = count($arrPosts);
            if (!empty($numPosts)) {
                $strDebug .= "Found {$numPosts} posts: <br>";
            }
        }
        foreach ($arrPosts as $index => $post) {
            $num = $index + 1;
            $id = UniteFunctionsUC::getVal($post, "id");
            $title = UniteFunctionsUC::getVal($post, "title");
            $pageviews = UniteFunctionsUC::getVal($post, "pageviews");
            if ($addDebug == true) {
                $strDebug .= "{$num}. {$title} ({$id}): {$pageviews} views <br>";
            }
            $arrPostIDs[] = $id;
        }
        if (empty($arrPosts) && $addDebug == true) {
            $strDebug .= "No popular posts found <br>";
        }
        if (empty($arrPostIDs)) {
            $arrPostIDs = array("0");
        }
        $output = array();
        $output["post_ids"] = $arrPostIDs;
        $output["debug"] = $strDebug;
        return $output;
        return $query;
    }
}