/**
  * Return an instance of this class.
  *
  * @since 2.0.0
  * @return erpRatingSystem
  */
 public static function get_instance(erpRelData $relData)
 {
     // If the single instance hasn't been set, set it now.
     if (null == self::$instance) {
         self::$instance = new self($relData);
     }
     if (!(isset(self::$instance->relData->relTable) && isset($relData->relTable) && self::$instance->relData->relTable == $relData->relTable)) {
         self::$instance->relData = $relData;
     }
     return self::$instance;
 }
 public function doRating($pid, $best = false)
 {
     $taxoOperator = $best ? 'and' : 'in';
     $qForm = new erpQueryFormater();
     // Make sure relData is populated, this can happen when do rating
     // is called outside of $this->getRelated
     if ($this->relData == null) {
         $this->relData = new erpRelData($pid, erpDefaults::$criticalOpts);
     }
     $ratingSystem = erpRatingSystem::get_instance($this->relData);
     $qForm->setMainArgs($pid);
     $postCats = get_the_category($pid);
     $postTags = get_the_tags($pid);
     $relTable = array();
     if (!empty($postCats)) {
         $qForm->clearTags()->clearPostInParam()->clearPostTypes()->setCategories($postCats, $taxoOperator);
         $qForm->exPostTypes($this->options->getValue('postTypes'))->exCategories($this->options->getValue('categories'))->exTags($this->options->getValue('tags'));
         $wpq = $this->relData->setQueryLimit($this->queryLimit, 0)->setWP_Query($qForm->getArgsArray(), $this->queryLimit, 0)->getResult();
         $postsArray = $wpq->posts;
         if (!empty($postsArray)) {
             foreach ($postsArray as $key => $value) {
                 $relTable[$value->ID]['score2_cats'] = $ratingSystem->rateBasedOnCats($pid, $value->ID);
                 $relTable[$value->ID]['score1_cats'] = $ratingSystem->rateBasedOnCats($value->ID, $pid);
                 $relTable[$value->ID]['score2_tags'] = $ratingSystem->rateBasedOnTags($pid, $value->ID);
                 $relTable[$value->ID]['score1_tags'] = $ratingSystem->rateBasedOnTags($value->ID, $pid);
                 $relTable[$value->ID]['post_date1'] = get_the_time('Y-m-d', $pid);
                 $relTable[$value->ID]['post_date2'] = get_the_time('Y-m-d', $value->ID);
                 $relTable[$value->ID]['pid1'] = $pid;
                 $relTable[$value->ID]['pid2'] = $value->ID;
             }
         }
     }
     if (!empty($postTags)) {
         $qForm->clearCategories()->clearPostInParam()->clearPostTypes()->setTags($postTags, $taxoOperator);
         $qForm->exPostTypes($this->options->getValue('postTypes'))->exCategories($this->options->getValue('categories'))->exTags($this->options->getValue('tags'));
         $wpq = $this->relData->setQueryLimit($this->queryLimit, 0)->setWP_Query($qForm->getArgsArray(), $this->queryLimit, 0)->getResult();
         $postsArray = $wpq->posts;
         if (!empty($postsArray)) {
             $inserted = array_keys($relTable);
             foreach ($postsArray as $key => $value) {
                 if (!in_array($value->ID, $inserted)) {
                     $relTable[$value->ID]['score2_cats'] = $ratingSystem->rateBasedOnCats($pid, $value->ID);
                     $relTable[$value->ID]['score1_cats'] = $ratingSystem->rateBasedOnCats($value->ID, $pid);
                     $relTable[$value->ID]['score2_tags'] = $ratingSystem->rateBasedOnTags($pid, $value->ID);
                     $relTable[$value->ID]['score1_tags'] = $ratingSystem->rateBasedOnTags($value->ID, $pid);
                     $relTable[$value->ID]['post_date1'] = get_the_time('Y-m-d H:i:s', $pid);
                     $relTable[$value->ID]['post_date2'] = get_the_time('Y-m-d H:i:s', $value->ID);
                     $relTable[$value->ID]['pid1'] = $pid;
                     $relTable[$value->ID]['pid2'] = $value->ID;
                 }
             }
         }
     }
     wp_reset_postdata();
     return $relTable;
 }