Skip to content

Commit

Permalink
added a way to edit get posts
Browse files Browse the repository at this point in the history
  • Loading branch information
mt-gareth committed Apr 3, 2020
1 parent d726997 commit 8ee40de
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ new BlogFeed( [
] );
```

####Default Args
### Default Args
```php
$args = [
'action' => 'get_posts', //ajax action name used by JS to address this query
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
],
"minimum-stability": "dev",
"require": {
"php": ">=5.4"
"php": ">=7.1"
},
"autoload": {
"files": [
Expand Down
22 changes: 15 additions & 7 deletions src/WP_Ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,11 @@ public function setup_ajax_handlers()

public function ajax_response()
{
$args = $this->getQueryArgs();
$loop = new \WP_Query( $args );
$current_page = (int)$loop->query_vars[ 'paged' ];
$current_page = $current_page ? $current_page : 1;
$max_pages = (int)$loop->max_num_pages;
[$posts, $current_page, $max_pages] = $this->getPostsAndPages();

$html = '';
if ( $this->output_template !== false )
$html = \App\Template( $this->output_template, [ 'posts' => $loop->posts ] );
$html = \App\Template( $this->output_template, [ 'posts' => $posts ] );
$pagination = '';
if ( $max_pages > 1 && $this->pagination_template !== false ) {
$pages_array = self::arrayOfPages( $current_page, $max_pages, $this->pagination_pages_to_show );
Expand All @@ -59,7 +55,7 @@ public function ajax_response()
'pagination' => $pagination,
'currentPage' => $current_page,
'maxPages' => $max_pages,
'posts' => $loop->posts,
'posts' => $posts,
] );
}

Expand All @@ -71,6 +67,18 @@ protected function getQueryArgs()
];
}

protected function getPostsAndPages()
{
$args = $this->getQueryArgs();
$loop = new \WP_Query( $args );
$posts = $loop->posts;
$current_page = (int)$loop->query_vars[ 'paged' ];
$current_page = $current_page ? $current_page : 1;
$max_pages = (int)$loop->max_num_pages;

return [$posts, $current_page, $max_pages];
}

public static function arrayOfPages( $current_page, $max_pages, $pages_to_show = 9 )
{
if ( !( $pages_to_show % 2 ) ) $pages_to_show--; // if $pages_to_show not odd subtract 1 to make it so
Expand Down

0 comments on commit 8ee40de

Please sign in to comment.