Skip to content

Commit 9e6fad6

Browse files
authored
Merge pull request #71 from positiondev/addMoreWpAggregate
Add more splices to wpAggregates; update Changelog
2 parents cddacc6 + 4421edc commit 9e6fad6

File tree

5 files changed

+71
-5
lines changed

5 files changed

+71
-5
lines changed

CHANGELOG.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
# Changelog
22

3-
## June 12 2019
3+
## April 2 2020
4+
* Add `wpTotalItems`, `wpHasMultiplePages`, `wpHasSinglePage`, `wpHasPreviousPages`, `wpHasNoPreviousPages` to `wpAggregates`
5+
* Use MVar instead of IntSet for storing displayed posts - the IntSet wasn't working with `apply` across templates.
6+
7+
## June 12 2019
48
* Add `Q` and `QM` `Field`s for fields accessed via an additional request using an ID or slug in the original request.
59
* Add `PV` `Field` for custom-parsed field without requiring a specific JSON type.
610

711
## October 29 2018
812
* Add Splice for a version of `wpPosts` called `wpPostsAggregate`
913
* `wpPostsAggregate` will allow access to some information from the headers, like how many posts and pages of posts there are.
10-
* Unlike `wpPosts`, the posts are wrapped in `wpPostsItem`.
14+
* Unlike `wpPosts`, the posts are wrapped in `wpPostsItem`.
1115
* There's also a separate `wpPostsMeta` that currently has the following splices:
1216
* `wpTotalPages` - displays how many pages of results are there from this query
1317
* `wpHasMorePages` - shows child markup if there are more pages (uses the post query from `wpPostsAggregate`'s attributes)

offset.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: offset
2-
version: 0.2.1
2+
version: 0.3.0
33
synopsis: A library that communicates with wordpress over its api.
44
-- description:
55
homepage: https://github.com/positiondev/offset

spec/Common.hs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,11 @@ fauxRequester _ "/object_array" [] =
226226
fauxRequester _ "/number_array" [] = toWPResp $ enc $ object ["some_array" .= [1 :: Int, 2 :: Int, 3 :: Int]]
227227
fauxRequester _ "/string_array" [] = toWPResp $ enc $ object ["some_array" .= ["a" :: Text, "b" :: Text, "c" :: Text]]
228228
fauxRequester _ "/many-pages" [] =
229-
return $ Right $ WPResponse [(CI.mk "X-WP-TotalPages", "478")] (enc [article1])
229+
return $ Right $ WPResponse [(CI.mk "X-WP-TotalPages", "478")
230+
,(CI.mk "X-WP-Total", "7337")] (enc [article1])
231+
fauxRequester _ "/single-page" [] =
232+
return $ Right $ WPResponse [(CI.mk "X-WP-TotalPages", "1")
233+
,(CI.mk "X-WP-Total", "5")] (enc [article1])
230234
fauxRequester mRecord rqPath rqParams = do
231235
case mRecord of
232236
Just record -> modifyMVar_ record $ return . (<> [mkUrlUnescape rqPath rqParams])

spec/Main.hs

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,46 @@ wpCustomAggregateTests = do
273273
\</wpNoMorePages>\
274274
\ <wpCustomMeta >\
275275
\</wpCustomAggregate>" `shouldRender` "<i>Foo</i> bar No more pages"
276-
276+
it "should be able to show the number of pages" $ do
277+
"<wpCustomAggregate endpoint=\"many-pages\">\
278+
\ <wpCustomItem><wpTitle><wpRendered /></wpTitle></wpCustomItem>\
279+
\ <wpCustomMeta>\
280+
\<wpTotalPages />\
281+
\ <wpCustomMeta >\
282+
\</wpCustomAggregate>" `shouldRender` "<i>Foo</i> bar 478"
283+
it "should be able to show the number of items" $ do
284+
"<wpCustomAggregate endpoint=\"many-pages\">\
285+
\ <wpCustomItem><wpTitle><wpRendered /></wpTitle></wpCustomItem>\
286+
\ <wpCustomMeta>\
287+
\<wpTotalItems />\
288+
\ <wpCustomMeta >\
289+
\</wpCustomAggregate>" `shouldRender` "<i>Foo</i> bar 7337"
290+
it "should be able to display based on number pages (many pages, first page)" $ do
291+
"<wpCustomAggregate endpoint=\"many-pages\">\
292+
\ <wpCustomItem><wpTitle><wpRendered /></wpTitle></wpCustomItem>\
293+
\ <wpCustomMeta page=\"1\">\
294+
\<wpHasMultiplePages>Has Multiple</wpHasMultiplePages>\
295+
\<wpHasSinglePage>Has Single</wpHasSinglePage>\
296+
\<wpHasNoPreviousPages>Has No Previous Pages</wpHasNoPreviousPages>\
297+
\ <wpCustomMeta >\
298+
\</wpCustomAggregate>" `shouldRender` "<i>Foo</i> bar Has Multiple Has No Previous Pages"
299+
it "should be able to display based on number pages (many pages, second page)" $ do
300+
"<wpCustomAggregate endpoint=\"many-pages\">\
301+
\ <wpCustomItem><wpTitle><wpRendered /></wpTitle></wpCustomItem>\
302+
\ <wpCustomMeta page=\"2\">\
303+
\<wpHasMultiplePages>Has Multiple</wpHasMultiplePages>\
304+
\<wpHasPreviousPages>Has Previous Pages</wpHasPreviousPages>\
305+
\<wpHasNoPreviousPages>Has No Previous Pages</wpHasNoPreviousPages>\
306+
\ <wpCustomMeta >\
307+
\</wpCustomAggregate>" `shouldRender` "<i>Foo</i> bar Has Multiple Has Previous Pages"
308+
it "should be able to display based on number pages (single pages)" $ do
309+
"<wpCustomAggregate endpoint=\"single-page\">\
310+
\ <wpCustomItem><wpTitle><wpRendered /></wpTitle></wpCustomItem>\
311+
\ <wpCustomMeta>\
312+
\<wpHasMultiplePages>Has Multiple</wpHasMultiplePages>\
313+
\<wpHasSinglePage>Has Single</wpHasSinglePage>\
314+
\ <wpCustomMeta >\
315+
\</wpCustomAggregate>" `shouldRender` "<i>Foo</i> bar Has Single"
277316

278317
cacheTests :: Spec
279318
cacheTests = do

src/Web/Offset/Splices.hs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,16 +221,35 @@ wpAggregateMetaFill :: Either StatusCode WPResponse -> Maybe Int -> Fill s
221221
wpAggregateMetaFill (Right (WPResponse headers _)) mCurrentPage = do
222222
let totalPagesText = maybe "" T.decodeUtf8
223223
(lookup "x-wp-totalpages" headers)
224+
totalItemsText = maybe "" T.decodeUtf8
225+
(lookup "x-wp-total" headers)
224226
totalPages = fromMaybe 1 (readSafe totalPagesText) :: Int
225227
currentPage = fromMaybe 1 mCurrentPage
226228
fillChildrenWith $
227229
subs [ ("wpTotalPages", textFill totalPagesText )
230+
, ("wpTotalItems", textFill totalItemsText)
228231
, ("wpHasMorePages",
229232
if currentPage < totalPages
230233
then fillChildren
231234
else textFill "")
232235
, ("wpNoMorePages",
233236
if currentPage < totalPages
237+
then textFill ""
238+
else fillChildren)
239+
, ("wpHasMultiplePages",
240+
if totalPages > 1
241+
then fillChildren
242+
else textFill "")
243+
, ("wpHasSinglePage",
244+
if totalPages > 1
245+
then textFill ""
246+
else fillChildren)
247+
, ("wpHasPreviousPages",
248+
if currentPage > 1
249+
then fillChildren
250+
else textFill "")
251+
, ("wpHasNoPreviousPages",
252+
if currentPage > 1
234253
then textFill ""
235254
else fillChildren)]
236255
wpPostsMetaFill _ _ = textFill ""

0 commit comments

Comments
 (0)