From cedbe86f8c0a432d6680bcbf74321ec060cb1fc3 Mon Sep 17 00:00:00 2001 From: Ned Zimmerman Date: Tue, 5 Sep 2017 12:11:40 -0300 Subject: [PATCH] Optimize catalog further. --- app/controllers/App.php | 32 +++++++++++-------- app/controllers/FrontPage.php | 7 +--- app/controllers/PageCatalog.php | 5 --- resources/views/page-catalog.blade.php | 16 +++++----- .../partials/front-page-catalog.blade.php | 8 ++--- 5 files changed, 31 insertions(+), 37 deletions(-) diff --git a/app/controllers/App.php b/app/controllers/App.php index c7409c9..63e0b5e 100644 --- a/app/controllers/App.php +++ b/app/controllers/App.php @@ -100,24 +100,26 @@ class App extends Controller return (get_query_var('orderby')) ? get_query_var('orderby') : 'title'; } - public static function previousPage($page) + public function previousPage() { + if (is_front_page()) { + $page = (get_query_var('page')) ? get_query_var('page') : 1; + } else { + $page = (get_query_var('paged')) ? get_query_var('paged') : 1; + } + return ($page > 1) ? $page - 1 : 0; } - public static function nextPage($page, $per_page = 10) + public function nextPage() { - return ($page < App::totalPages($per_page)) ? $page + 1 : 0; - } + if (is_front_page()) { + $page = (get_query_var('page')) ? get_query_var('page') : 1; + } else { + $page = (get_query_var('paged')) ? get_query_var('paged') : 1; + } - public static function totalPages($per_page = 10) - { - $request = new \WP_REST_Request('GET', '/pressbooks/v2/books'); - $request->set_query_params([ - 'per_page' => $per_page, - ]); - $response = rest_do_request($request); - return $response->headers['X-WP-TotalPages']; + return $page + 1; } public static function books($page = 1, $per_page = 10, $orderby = 'title', $license = '', $subject = '') @@ -128,6 +130,7 @@ class App extends Controller 'per_page' => $per_page, ]); $response = rest_do_request($request); + $pages = $response->headers['X-WP-TotalPages']; $data = rest_get_server()->response_to_data($response, true); $books = []; foreach ($data as $key => $book) { @@ -139,9 +142,10 @@ class App extends Controller $books[] = $book; } if ($orderby === 'latest') { - return wp_list_sort($books, $orderby, 'desc'); + $books = wp_list_sort($books, $orderby, 'desc'); } else { - return wp_list_sort($books, $orderby); + $books = wp_list_sort($books, $orderby); } + return ['pages' => $pages, 'books' => $books]; } } diff --git a/app/controllers/FrontPage.php b/app/controllers/FrontPage.php index 0cc5f5b..da6d494 100644 --- a/app/controllers/FrontPage.php +++ b/app/controllers/FrontPage.php @@ -29,14 +29,9 @@ class FrontPage extends Controller return __('Our Latest Titles', 'aldine'); } - public function totalPages() - { - return App::totalPages(3); - } - public function books() { - $page = (get_query_var('paged')) ? get_query_var('paged') : 1; + $page = (get_query_var('page')) ? get_query_var('page') : 1; return App::books($page, 3); } } diff --git a/app/controllers/PageCatalog.php b/app/controllers/PageCatalog.php index eea4c4d..c4e7b63 100644 --- a/app/controllers/PageCatalog.php +++ b/app/controllers/PageCatalog.php @@ -100,11 +100,6 @@ class PageCatalog extends Controller ]; } - public function totalPages() - { - return App::totalPages(9); - } - public function books() { $page = (get_query_var('paged')) ? get_query_var('paged') : 1; diff --git a/resources/views/page-catalog.blade.php b/resources/views/page-catalog.blade.php index e201fc5..18926d9 100644 --- a/resources/views/page-catalog.blade.php +++ b/resources/views/page-catalog.blade.php @@ -33,22 +33,22 @@
{{ __('Sort by', 'aldine') }}
- @foreach($books as $book) + @foreach($books['books'] as $book) @include('partials.book', ['book' => $book]) @endforeach
- @if($total_pages > 1) + @if($books['pages'] > 1) @endif diff --git a/resources/views/partials/front-page-catalog.blade.php b/resources/views/partials/front-page-catalog.blade.php index 581a3f2..51dd6b3 100644 --- a/resources/views/partials/front-page-catalog.blade.php +++ b/resources/views/partials/front-page-catalog.blade.php @@ -1,8 +1,8 @@

{{ $latest_books_title }}

-
- @foreach($books as $book) +
+ @foreach($books['books'] as $book)
@if(isset($book['metadata']['keywords']))

@@ -18,8 +18,8 @@

@endforeach
- @if(App::previousPage($current_page))@endif - @if(App::nextPage($current_page, 3))@endif + @if($previous_page)@endif + @if($next_page <= $books['pages'])@endif