Example #1
0
 /**
  * Filter posts based on a request type
  *
  * @param  string $requestType    The requested page type (optional)
  */
 public function filterPosts($requestType)
 {
     # Load the query parser
     $parser = new QueryParser();
     # Set the request type
     $this->requestType = $requestType;
     # Save the requested URL
     $uri = rtrim(\Kanso\Kanso::getInstance()->Environment()['REQUEST_URI'], '/');
     # Filter and paginate the posts based on the request type
     if ($requestType === 'home') {
         $perPage = \Kanso\Kanso::getInstance()->Config()['KANSO_POSTS_PER_PAGE'];
         $offset = $this->pageIndex * $perPage;
         $limit = $perPage;
         $this->queryStr = "post_status = published : post_type = post : orderBy = post_created, DESC : limit = {$offset}, {$perPage}";
         $this->posts = $parser->parseQuery($this->queryStr);
         $this->postCount = count($this->posts);
     } else {
         if ($requestType === 'archive') {
             $this->queryStr = 'post_status = published : post_type = post : orderBy = post_created, DESC';
             $this->posts = $parser->parseQuery($this->queryStr);
             $this->postCount = count($this->posts);
         } else {
             if ($requestType === 'tag') {
                 $perPage = \Kanso\Kanso::getInstance()->Config()['KANSO_POSTS_PER_PAGE'];
                 $offset = $this->pageIndex * $perPage;
                 $this->queryStr = 'post_status = published : post_type = post : orderBy = post_created, DESC : tag_slug = ' . explode("/", $uri)[2] . " : limit = {$offset}, {$perPage}";
                 $this->posts = $parser->parseQuery($this->queryStr);
                 $this->postCount = count($this->posts);
             } else {
                 if ($requestType === 'category') {
                     $perPage = \Kanso\Kanso::getInstance()->Config()['KANSO_POSTS_PER_PAGE'];
                     $offset = $this->pageIndex * $perPage;
                     $this->queryStr = 'post_status = published : post_type = post : orderBy = post_created, DESC : category_slug = ' . explode("/", $uri)[2] . " : limit = {$offset}, {$perPage}";
                     $this->posts = $parser->parseQuery($this->queryStr);
                     $this->postCount = count($this->posts);
                 } else {
                     if ($requestType === 'author') {
                         $perPage = \Kanso\Kanso::getInstance()->Config()['KANSO_POSTS_PER_PAGE'];
                         $offset = $this->pageIndex * $perPage;
                         $this->queryStr = ' post_status = published : post_type = post : orderBy = post_created, DESC: author_slug = ' . explode("/", $uri)[2] . ": limit = {$offset}, {$perPage}";
                         $this->posts = $parser->parseQuery($this->queryStr);
                         $this->postCount = count($this->posts);
                     } else {
                         if ($requestType === 'single') {
                             if (strpos($uri, '?draft') !== false) {
                                 $uri = ltrim(str_replace('?draft', '', $uri), '/');
                                 $this->queryStr = 'post_status = draft : post_type = post : post_slug = ' . $uri . '/';
                                 $this->posts = $parser->parseQuery($this->queryStr);
                                 $this->postCount = count($this->posts);
                             } else {
                                 $uri = \Kanso\Utility\Str::GetBeforeLastWord($uri, '/feed');
                                 $uri = ltrim($uri, '/');
                                 $this->queryStr = 'post_status = published : post_type = post : post_slug = ' . $uri . '/';
                                 $this->posts = $parser->parseQuery($this->queryStr);
                                 $this->postCount = count($this->posts);
                             }
                         } else {
                             if ($requestType === 'page') {
                                 if (strpos($uri, '?draft') !== false) {
                                     $uri = ltrim(str_replace('?draft', '', $uri), '/');
                                     $this->queryStr = 'post_status = draft : post_type = page : post_slug = ' . $uri . '/';
                                     $this->posts = $parser->parseQuery($this->queryStr);
                                     $this->postCount = count($this->posts);
                                 } else {
                                     $uri = \Kanso\Utility\Str::GetBeforeLastWord($uri, '/feed');
                                     $uri = ltrim($uri, '/');
                                     $this->queryStr = 'post_status = published : post_type = page : post_slug = ' . $uri . '/';
                                     $this->posts = $parser->parseQuery($this->queryStr);
                                     $this->postCount = count($this->posts);
                                 }
                             } else {
                                 if ($requestType === 'search') {
                                     $perPage = \Kanso\Kanso::getInstance()->Config()['KANSO_POSTS_PER_PAGE'];
                                     $offset = $this->pageIndex * $perPage;
                                     # Get the query
                                     $query = \Kanso\Kanso::getInstance()->Request()->fetch('query');
                                     # Validate the query exts
                                     if (!$query || empty(trim($query))) {
                                         return;
                                     }
                                     # Get the actual search query | sanitize
                                     $query = htmlspecialchars(trim(strtolower(urldecode(\Kanso\Utility\Str::getAfterLastChar($uri, '=')))));
                                     $query = \Kanso\Utility\Str::getBeforeFirstChar($query, '/');
                                     # No need to query empty strings
                                     if (empty($query)) {
                                         return;
                                     }
                                     # Filter the posts
                                     $this->queryStr = "post_status = published : post_type = post : orderBy = post_created, DESC : post_title LIKE {$query} || post_excerpt LIKE {$query} : limit = {$offset}, {$perPage}";
                                     $this->posts = $parser->parseQuery($this->queryStr);
                                     $this->postCount = count($this->posts);
                                     $this->searchQuery = $query;
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
 }
Example #2
0
 /**
  * Get the comments for listing in the admin panel
  *
  * @param  $queries    array     POST data from client
  * @param  $filter     string
  * @return array
  */
 public function loadAllComments($queries, $filter)
 {
     # Get the Kanso Query object
     $Query = \Kanso\Kanso::getInstance()->Query();
     # Get the SQL builder
     $SQL = \Kanso\Kanso::getInstance()->Database->Builder();
     $isSearch = $queries['search'] !== 'false';
     $page = (int) $queries['page'] - 1;
     $comments = [];
     $sort = $queries['sortBy'] === 'newest' ? 'DESC' : 'ASC';
     if ($isSearch) {
         $validKeys = ['ip' => 'ip_address', 'status' => 'status', 'user' => 'name', 'email' => 'email'];
         $searchValue = $queries['search'];
         $searchKey = false;
         if (\Kanso\Utility\Str::contains($searchValue, ':')) {
             $value = \Kanso\Utility\Str::getAfterFirstChar($searchValue, ':');
             $key = \Kanso\Utility\Str::getBeforeFirstChar($searchValue, ':');
             $key = isset($validKeys[$key]) ? $validKeys[$key] : false;
             if ($key) {
                 $searchKey = $key;
                 $searchValue = $value;
             }
         }
         if ($searchKey) {
             $comments = $SQL->SELECT('*')->FROM('comments')->WHERE($searchKey, '=', $searchValue);
         } else {
             $comments = $SQL->SELECT('*')->FROM('comments')->WHERE('content', 'LIKE', "%{$searchValue}%");
         }
         if ($filter === 'all') {
             $comments = $comments->ORDER_BY('date', $sort)->FIND_ALL();
         }
         if ($filter === 'approved') {
             $comments = $comments->WHERE('status', '=', 'approved')->ORDER_BY('date', $sort)->FIND_ALL();
         }
         if ($filter === 'spam') {
             $comments = $comments->WHERE('status', '=', 'spam')->ORDER_BY('date', $sort)->FIND_ALL();
         }
         if ($filter === 'pending') {
             $comments = $comments->WHERE('status', '=', 'pending')->ORDER_BY('date', $sort)->FIND_ALL();
         }
         if ($filter === 'deleted') {
             $comments = $comments->WHERE('status', '=', 'deleted')->ORDER_BY('date', $sort)->FIND_ALL();
         }
     } else {
         if ($filter === 'all') {
             $comments = $SQL->SELECT('*')->FROM('comments')->ORDER_BY('date', $sort)->FIND_ALL();
         }
         if ($filter === 'approved') {
             $comments = $SQL->SELECT('*')->FROM('comments')->WHERE('status', '=', 'approved')->ORDER_BY('date', $sort)->FIND_ALL();
         }
         if ($filter === 'spam') {
             $comments = $SQL->SELECT('*')->FROM('comments')->WHERE('status', '=', 'spam')->ORDER_BY('date', $sort)->FIND_ALL();
         }
         if ($filter === 'pending') {
             $comments = $SQL->SELECT('*')->FROM('comments')->WHERE('status', '=', 'pending')->ORDER_BY('date', $sort)->FIND_ALL();
         }
         if ($filter === 'deleted') {
             $comments = $SQL->SELECT('*')->FROM('comments')->WHERE('status', '=', 'deleted')->ORDER_BY('date', $sort)->FIND_ALL();
         }
     }
     foreach ($comments as $key => $comment) {
         $comments[$key]['permalink'] = $Query->the_permalink($comment['post_id']);
         $comments[$key]['title'] = $Query->the_title($comment['post_id']);
         $comments[$key]['avatar'] = $Query->get_avatar($comment['email'], 100, true);
     }
     $comments = \Kanso\Utility\Arr::paginate($comments, $page, 10);
     return $comments;
 }