Skip to content

Commit

Permalink
Merge pull request #71 from positiondev/addMoreWpAggregate
Browse files Browse the repository at this point in the history
Add more splices to wpAggregates; update Changelog
  • Loading branch information
emhoracek authored Apr 2, 2020
2 parents cddacc6 + 4421edc commit 9e6fad6
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 5 deletions.
8 changes: 6 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
# Changelog

## June 12 2019
## April 2 2020
* Add `wpTotalItems`, `wpHasMultiplePages`, `wpHasSinglePage`, `wpHasPreviousPages`, `wpHasNoPreviousPages` to `wpAggregates`
* Use MVar instead of IntSet for storing displayed posts - the IntSet wasn't working with `apply` across templates.

## June 12 2019
* Add `Q` and `QM` `Field`s for fields accessed via an additional request using an ID or slug in the original request.
* Add `PV` `Field` for custom-parsed field without requiring a specific JSON type.

## October 29 2018
* Add Splice for a version of `wpPosts` called `wpPostsAggregate`
* `wpPostsAggregate` will allow access to some information from the headers, like how many posts and pages of posts there are.
* Unlike `wpPosts`, the posts are wrapped in `wpPostsItem`.
* Unlike `wpPosts`, the posts are wrapped in `wpPostsItem`.
* There's also a separate `wpPostsMeta` that currently has the following splices:
* `wpTotalPages` - displays how many pages of results are there from this query
* `wpHasMorePages` - shows child markup if there are more pages (uses the post query from `wpPostsAggregate`'s attributes)
Expand Down
2 changes: 1 addition & 1 deletion offset.cabal
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: offset
version: 0.2.1
version: 0.3.0
synopsis: A library that communicates with wordpress over its api.
-- description:
homepage: https://github.com/positiondev/offset
Expand Down
6 changes: 5 additions & 1 deletion spec/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,11 @@ fauxRequester _ "/object_array" [] =
fauxRequester _ "/number_array" [] = toWPResp $ enc $ object ["some_array" .= [1 :: Int, 2 :: Int, 3 :: Int]]
fauxRequester _ "/string_array" [] = toWPResp $ enc $ object ["some_array" .= ["a" :: Text, "b" :: Text, "c" :: Text]]
fauxRequester _ "/many-pages" [] =
return $ Right $ WPResponse [(CI.mk "X-WP-TotalPages", "478")] (enc [article1])
return $ Right $ WPResponse [(CI.mk "X-WP-TotalPages", "478")
,(CI.mk "X-WP-Total", "7337")] (enc [article1])
fauxRequester _ "/single-page" [] =
return $ Right $ WPResponse [(CI.mk "X-WP-TotalPages", "1")
,(CI.mk "X-WP-Total", "5")] (enc [article1])
fauxRequester mRecord rqPath rqParams = do
case mRecord of
Just record -> modifyMVar_ record $ return . (<> [mkUrlUnescape rqPath rqParams])
Expand Down
41 changes: 40 additions & 1 deletion spec/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,46 @@ wpCustomAggregateTests = do
\</wpNoMorePages>\
\ <wpCustomMeta >\
\</wpCustomAggregate>" `shouldRender` "<i>Foo</i> bar No more pages"

it "should be able to show the number of pages" $ do
"<wpCustomAggregate endpoint=\"many-pages\">\
\ <wpCustomItem><wpTitle><wpRendered /></wpTitle></wpCustomItem>\
\ <wpCustomMeta>\
\<wpTotalPages />\
\ <wpCustomMeta >\
\</wpCustomAggregate>" `shouldRender` "<i>Foo</i> bar 478"
it "should be able to show the number of items" $ do
"<wpCustomAggregate endpoint=\"many-pages\">\
\ <wpCustomItem><wpTitle><wpRendered /></wpTitle></wpCustomItem>\
\ <wpCustomMeta>\
\<wpTotalItems />\
\ <wpCustomMeta >\
\</wpCustomAggregate>" `shouldRender` "<i>Foo</i> bar 7337"
it "should be able to display based on number pages (many pages, first page)" $ do
"<wpCustomAggregate endpoint=\"many-pages\">\
\ <wpCustomItem><wpTitle><wpRendered /></wpTitle></wpCustomItem>\
\ <wpCustomMeta page=\"1\">\
\<wpHasMultiplePages>Has Multiple</wpHasMultiplePages>\
\<wpHasSinglePage>Has Single</wpHasSinglePage>\
\<wpHasNoPreviousPages>Has No Previous Pages</wpHasNoPreviousPages>\
\ <wpCustomMeta >\
\</wpCustomAggregate>" `shouldRender` "<i>Foo</i> bar Has Multiple Has No Previous Pages"
it "should be able to display based on number pages (many pages, second page)" $ do
"<wpCustomAggregate endpoint=\"many-pages\">\
\ <wpCustomItem><wpTitle><wpRendered /></wpTitle></wpCustomItem>\
\ <wpCustomMeta page=\"2\">\
\<wpHasMultiplePages>Has Multiple</wpHasMultiplePages>\
\<wpHasPreviousPages>Has Previous Pages</wpHasPreviousPages>\
\<wpHasNoPreviousPages>Has No Previous Pages</wpHasNoPreviousPages>\
\ <wpCustomMeta >\
\</wpCustomAggregate>" `shouldRender` "<i>Foo</i> bar Has Multiple Has Previous Pages"
it "should be able to display based on number pages (single pages)" $ do
"<wpCustomAggregate endpoint=\"single-page\">\
\ <wpCustomItem><wpTitle><wpRendered /></wpTitle></wpCustomItem>\
\ <wpCustomMeta>\
\<wpHasMultiplePages>Has Multiple</wpHasMultiplePages>\
\<wpHasSinglePage>Has Single</wpHasSinglePage>\
\ <wpCustomMeta >\
\</wpCustomAggregate>" `shouldRender` "<i>Foo</i> bar Has Single"

cacheTests :: Spec
cacheTests = do
Expand Down
19 changes: 19 additions & 0 deletions src/Web/Offset/Splices.hs
Original file line number Diff line number Diff line change
Expand Up @@ -221,16 +221,35 @@ wpAggregateMetaFill :: Either StatusCode WPResponse -> Maybe Int -> Fill s
wpAggregateMetaFill (Right (WPResponse headers _)) mCurrentPage = do
let totalPagesText = maybe "" T.decodeUtf8
(lookup "x-wp-totalpages" headers)
totalItemsText = maybe "" T.decodeUtf8
(lookup "x-wp-total" headers)
totalPages = fromMaybe 1 (readSafe totalPagesText) :: Int
currentPage = fromMaybe 1 mCurrentPage
fillChildrenWith $
subs [ ("wpTotalPages", textFill totalPagesText )
, ("wpTotalItems", textFill totalItemsText)
, ("wpHasMorePages",
if currentPage < totalPages
then fillChildren
else textFill "")
, ("wpNoMorePages",
if currentPage < totalPages
then textFill ""
else fillChildren)
, ("wpHasMultiplePages",
if totalPages > 1
then fillChildren
else textFill "")
, ("wpHasSinglePage",
if totalPages > 1
then textFill ""
else fillChildren)
, ("wpHasPreviousPages",
if currentPage > 1
then fillChildren
else textFill "")
, ("wpHasNoPreviousPages",
if currentPage > 1
then textFill ""
else fillChildren)]
wpPostsMetaFill _ _ = textFill ""
Expand Down

0 comments on commit 9e6fad6

Please sign in to comment.