Browse Source

Paged navigation for front page catalog.

pull/9/head
Ned Zimmerman 7 years ago
parent
commit
e611e86094
No known key found for this signature in database
GPG Key ID: FF56334A013120CA
  1. 37
      app/controllers/FrontPage.php
  2. 36
      resources/views/front-page.blade.php
  3. 22
      resources/views/partials/front-page-catalog.blade.php

37
app/controllers/FrontPage.php

@ -4,7 +4,7 @@ namespace Aldine;
use Sober\Controller\Controller; use Sober\Controller\Controller;
class Home extends Controller class FrontPage extends Controller
{ {
public function blockCount() public function blockCount()
{ {
@ -13,8 +13,7 @@ class Home extends Controller
'home-block-1', 'home-block-1',
'home-block-2', 'home-block-2',
'home-block-3', 'home-block-3',
'home-block-4', 'home-block-4'
'home-block-5'
] as $block) { ] as $block) {
if (is_active_sidebar($block)) { if (is_active_sidebar($block)) {
$c++; $c++;
@ -23,10 +22,10 @@ class Home extends Controller
return $c; return $c;
} }
public function homeBlocks() public function blocks()
{ {
$blocks = []; $blocks = [];
for ($i = 0; $i < 5; $i++) { for ($i = 0; $i < 4; $i++) {
if (is_active_sidebar("home-block-$i")) { if (is_active_sidebar("home-block-$i")) {
$blocks[] = "home-block-$i"; $blocks[] = "home-block-$i";
} }
@ -35,8 +34,32 @@ class Home extends Controller
return $blocks; return $blocks;
} }
public function latestBooksBlock() public function totalPages()
{ {
return is_active_widget(false, false, 'latestbooks'); $books = wp_remote_get(network_home_url('/wp-json/pressbooks/v2/books?per_page=3'));
return $books['headers']['x-wp-totalpages'];
}
public function currentPage()
{
return get_query_var('page', 1);
}
public function previousPage()
{
return (get_query_var('page', 1) > 1) ? get_query_var('page') - 1 : 0;
}
public function nextPage()
{
return (get_query_var('page', 1) < FrontPage::totalPages()) ? get_query_var('page', 1) + 1 : 0;
}
public static function latestBooks($offset = null)
{
$path = ($offset) ? "/wp-json/pressbooks/v2/books?per_page=3&page=$offset" : '/wp-json/pressbooks/v2/books?per_page=3';
$books = wp_remote_get(network_home_url($path));
$books = json_decode($books['body'], true);
return $books;
} }
} }

36
resources/views/front-page.blade.php

@ -10,53 +10,31 @@
</div> </div>
@elseif($block_count < 4) @elseif($block_count < 4)
@for($i = 0; $i < $block_count; $i++) @for($i = 0; $i < $block_count; $i++)
<div class="block block-{{ $i + 1 }}@if($latest_books_block === $home_blocks[$i]) latest-books @endif"> <div class="block block-{{ $i + 1 }}">
<div class="inside"> <div class="inside">
@php(dynamic_sidebar($home_blocks[$i])) @php(dynamic_sidebar($blocks[$i]))
</div> </div>
</div> </div>
@endfor @endfor
@elseif($block_count === 4) @elseif($block_count === 4)
<div class="one-two"> <div class="one-two">
@for($i = 0; $i < 2; $i++) @for($i = 0; $i < 2; $i++)
<div class="block block-{{ $i + 1 }}@if($latest_books_block === $home_blocks[$i]) latest-books @endif"> <div class="block block-{{ $i + 1 }}">
<div class="inside"> <div class="inside">
@php(dynamic_sidebar($home_blocks[$i])) @php(dynamic_sidebar($blocks[$i]))
</div> </div>
</div> </div>
@endfor @endfor
</div> </div>
@for($i = 2; $i < $block_count; $i++) @for($i = 2; $i < $block_count; $i++)
<div class="block block-{{ $i + 1 }}@if($latest_books_block === $home_blocks[$i]) latest-books @endif"> <div class="block block-{{ $i + 1 }}">
<div class="inside"> <div class="inside">
@php(dynamic_sidebar($home_blocks[$i])) @php(dynamic_sidebar($blocks[$i]))
</div> </div>
</div> </div>
@endfor @endfor
@endif @endif
@if(get_theme_mod('pb_front_page_catalog')) @if(get_theme_mod('pb_front_page_catalog'))
<div class="block latest-books"> @include('partials.front-page-catalog')
<div class="inside">
<h3>{{ get_theme_mod('pb_front_page_catalog_title') }}</h3>
@php
$books = wp_remote_get(home_url('/wp-json/pressbooks/v2/books'));
$books = json_decode($books['body'], true);
echo '<div class="books">';
for ($i = 0; $i < 3; $i++) {
printf(
'<div class="book">
<a class="subject" href="">TK</a>
<a class="title" href="%1$s">%2$s</a>
<a class="read-more" href="%1$s">%3$s</a>
</div>',
$books[$i]['link'],
$books[$i]['metadata']['name'],
__('About this book &rarr;', 'aldine')
);
}
echo '</div>';
@endphp
</div>
</div>
@endif @endif
@endsection @endsection

22
resources/views/partials/front-page-catalog.blade.php

@ -0,0 +1,22 @@
<div class="block latest-books">
<div class="inside">
<h3>{{ get_theme_mod('pb_front_page_catalog_title') }}</h3>
<div class="books">
@foreach( FrontPage::latestBooks( $current_page ) as $book )
<div class="book">
<a class="subject" href="">TK</a>
<a class="title" href="{{ $book['link'] }}">{{ $book['metadata']['name'] }}</a>
<a class="read-more" href="{{ $book['link'] }}">{{ __('About this book &rarr;', 'aldine') }}</a>
</div>
@endforeach
</div>
<div class="navigation mt2">
@if($previous_page)
<a class="previous f1" href="{{ network_home_url("/page/$previous_page/") }}">&larr;</a>
@endif
@if($next_page)
<a class="next f1" href="{{ network_home_url("/page/$next_page/") }}">&rarr;</a>
@endif
</div>
</div>
</div>
Loading…
Cancel
Save