public static function lookup($keywords, $onlyLive = 1, $limit = 100) { $foundPages = []; if (!empty($keywords)) { PageSearchLog::add_term($keywords); // create array containing, whole term then each individual word (or just whole term if one word) $keywordsArray = array_merge([$keywords], explode(' ', $keywords)); $keywordsArray = count($keywordsArray) == 2 ? [$keywordsArray[0]] : $keywordsArray; $numberOfKeywords = count($keywordsArray); // load search objects $searchObjects = [new Cms($onlyLive), new WordPress($onlyLive, config('coaster::blog.url') ? Setting::blogConnection() : false)]; event(new Search($searchObjects, $onlyLive)); // run search functions foreach ($keywordsArray as $i => $keyword) { $keywordAdditionalWeight = ($numberOfKeywords - $i) * 0.1 + ($i == 0 && $numberOfKeywords > 1 ? 5 : 0); foreach ($searchObjects as $searchObject) { $searchObject->run($keyword, $keywordAdditionalWeight); } } // get search results $weights = []; $pages = []; foreach ($searchObjects as $i => $searchObject) { $weights = $weights + $searchObject->getWeights(); $pages = $pages + $searchObject->getPages(); } // order, limit and create page data array if ($weights) { arsort($weights); if (count($weights) > $limit) { $weights = array_slice($weights, 0, 100, true); } $pageIds = array_keys($weights); foreach ($pageIds as $pageId) { if (array_key_exists($pageId, $pages)) { $foundPages[] = $pages[$pageId]; } } } } return $foundPages; }
/** * @param int $getPosts * @param string $where * @return \PDOStatement */ public function blogPosts($getPosts = 3, $where = 'post_type = "post" AND post_status = "publish"') { $prefix = config('coaster::blog.prefix'); $where = $where ? 'WHERE ' . $where : ''; $query = "SELECT * FROM {$prefix}posts {$where} ORDER BY post_date DESC LIMIT {$getPosts}"; try { return Setting::blogConnection()->query($query); } catch (\Exception $e) { return new \PDOStatement(); } }