| 1 |
<?php |
| 2 |
namespace Depicter\Controllers\Ajax; |
| 3 |
|
| 4 |
use Averta\Core\Utility\Data; |
| 5 |
use Averta\WordPress\Utility\JSON; |
| 6 |
use Depicter\Utility\Sanitize; |
| 7 |
use Psr\Http\Message\ResponseInterface; |
| 8 |
use WPEmerge\Requests\RequestInterface; |
| 9 |
|
| 10 |
class PostsAjaxController |
| 11 |
{ |
| 12 |
/** |
| 13 |
* list available post types with their taxonomies |
| 14 |
* @param RequestInterface $request |
| 15 |
* @param $view |
| 16 |
* |
| 17 |
* @return ResponseInterface |
| 18 |
*/ |
| 19 |
public function getPostTypes( RequestInterface $request, $view ) { |
| 20 |
$postType = !empty( $request->query('postType') ) ? Sanitize::textfield( $request->query('postType') ) : 'all'; |
| 21 |
$result = \Depicter::dataSource()->posts()->getTypes( $postType ); |
| 22 |
|
| 23 |
return \Depicter::json([ |
| 24 |
'hits' => $result |
| 25 |
])->withStatus(200); |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* List available posts for custom post type |
| 30 |
* @param RequestInterface $request |
| 31 |
* @param $view |
| 32 |
* |
| 33 |
* @return ResponseInterface |
| 34 |
*/ |
| 35 |
public function getPosts( RequestInterface $request, $view ) { |
| 36 |
$args = []; |
| 37 |
|
| 38 |
if( !Data::isNullOrEmptyStr( $request->body('postType') ) ){ |
| 39 |
$args['postType'] = Sanitize::textfield( $request->body('postType') ); |
| 40 |
} |
| 41 |
if( !Data::isNullOrEmptyStr( $request->body('perpage') ) ){ |
| 42 |
$args['perpage'] = Sanitize::int( $request->body('perpage') ); |
| 43 |
} |
| 44 |
if( !Data::isNullOrEmptyStr( $request->body('excerptLength') ) ){ |
| 45 |
$args['excerptLength'] = Sanitize::int( $request->body('excerptLength') ); |
| 46 |
} |
| 47 |
if( !Data::isNullOrEmptyStr( $request->body('offset') ) ){ |
| 48 |
$args['offset'] = Sanitize::int( $request->body('offset') ); |
| 49 |
} |
| 50 |
if( !Data::isNullOrEmptyStr( $request->body('linkSlides') ) ){ |
| 51 |
$args['linkSlides'] = Sanitize::textfield( $request->body('linkSlides') ); |
| 52 |
} |
| 53 |
if( !Data::isNullOrEmptyStr( $request->body('orderBy') ) ){ |
| 54 |
$args['orderBy'] = Sanitize::textfield( $request->body('orderBy') ); |
| 55 |
} |
| 56 |
if( !Data::isNullOrEmptyStr( $request->body('order') ) ){ |
| 57 |
$args['order'] = Sanitize::textfield( $request->body('order') ); |
| 58 |
} |
| 59 |
if( !Data::isNullOrEmptyStr( $request->body('imageSource') ) ){ |
| 60 |
$args['imageSource'] = Sanitize::textfield( $request->body('imageSource') ); |
| 61 |
} |
| 62 |
if( !Data::isNullOrEmptyStr( $request->body('excludedIds') ) ){ |
| 63 |
$args['excludedIds'] = JSON::decode( $request->body('excludedIds') ); |
| 64 |
; } |
| 65 |
if( !Data::isNullOrEmptyStr( $request->body('includedIds') ) ){ |
| 66 |
$args['includedIds'] = JSON::decode( $request->body('includedIds') ); |
| 67 |
} |
| 68 |
if( !Data::isNullOrEmptyStr( $request->body('excludeNonThumbnail') ) ){ |
| 69 |
$args['excludeNonThumbnail'] = Sanitize::textfield( $request->body('excludeNonThumbnail') ); |
| 70 |
} |
| 71 |
if( !Data::isNullOrEmptyStr( $request->body('taxonomies') ) ){ |
| 72 |
$args['taxonomies'] = Sanitize::textfield( $request->body('taxonomies') ); |
| 73 |
} |
| 74 |
if( !Data::isNullOrEmptyStr( $request->body('sticky') ) ){ |
| 75 |
$args['sticky'] = Data::isTrue( Sanitize::textfield( $request->body('sticky') ) ); |
| 76 |
} |
| 77 |
|
| 78 |
// check if request is for handpicked data or not |
| 79 |
if ( !Data::isNullOrEmptyStr( $request->body('handpicked') ) ) { |
| 80 |
$args['orderBy'] = 'post__in'; |
| 81 |
} |
| 82 |
|
| 83 |
$posts = \Depicter::dataSource()->posts()->previewRecords( $args ); |
| 84 |
|
| 85 |
return \Depicter::json([ |
| 86 |
'hits' => $posts |
| 87 |
])->withStatus(200); |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Search posts |
| 92 |
* @param RequestInterface $request |
| 93 |
* @param $view |
| 94 |
* |
| 95 |
* @return ResponseInterface |
| 96 |
*/ |
| 97 |
public function searchPosts( RequestInterface $request, $view ) { |
| 98 |
$args = []; |
| 99 |
|
| 100 |
if( !Data::isNullOrEmptyStr( $request->body('postType') ) ){ |
| 101 |
$args['postType'] = Sanitize::textfield( $request->body('postType') ); |
| 102 |
} |
| 103 |
if( !Data::isNullOrEmptyStr( $request->body('perpage') ) ){ |
| 104 |
$args['perpage'] = Sanitize::int( $request->body('perpage') ); |
| 105 |
} else { |
| 106 |
$args['perpage'] = 20; |
| 107 |
} |
| 108 |
if( !Data::isNullOrEmptyStr( $request->body('page') ) ){ |
| 109 |
$args['page'] = Sanitize::int( $request->body('page') ); |
| 110 |
} |
| 111 |
if( !Data::isNullOrEmptyStr( $request->body('s') ) ){ |
| 112 |
$args['s'] = Sanitize::textfield( $request->body('s') ); |
| 113 |
} |
| 114 |
if( !Data::isNullOrEmptyStr( $request->body('excludedIds') ) ){ |
| 115 |
$args['excludedIds'] = JSON::decode( $request->body('excludedIds') ); |
| 116 |
} |
| 117 |
|
| 118 |
$posts = \Depicter::dataSource()->posts()->searchRecordsByTitle( $args ); |
| 119 |
|
| 120 |
return \Depicter::json([ |
| 121 |
'hits' => $posts |
| 122 |
])->withStatus(200); |
| 123 |
} |
| 124 |
} |
| 125 |
|