| 1 |
<?php |
| 2 |
namespace Depicter\Controllers\Ajax; |
| 3 |
|
| 4 |
use Averta\WordPress\Utility\JSON; |
| 5 |
use Averta\WordPress\Utility\Sanitize; |
| 6 |
use Depicter\GuzzleHttp\Exception\GuzzleException; |
| 7 |
use Psr\Http\Message\ResponseInterface; |
| 8 |
use WPEmerge\Requests\RequestInterface; |
| 9 |
|
| 10 |
class AppInfoAjaxController { |
| 11 |
|
| 12 |
/** |
| 13 |
* Retrieves Lists of all entries. (GET) |
| 14 |
* |
| 15 |
* @param RequestInterface $request |
| 16 |
* @param string $view |
| 17 |
* |
| 18 |
* @return ResponseInterface |
| 19 |
* @throws GuzzleException |
| 20 |
*/ |
| 21 |
public function changelogs( RequestInterface $request, string $view) |
| 22 |
{ |
| 23 |
try { |
| 24 |
$response = \Depicter::remote()->get( 'v1/core/changelogs', [ 'query' => $request->getQueryParams() ] ); |
| 25 |
return \Depicter::json( |
| 26 |
JSON::decode( $response->getBody(), true ) |
| 27 |
)->withStatus( 200 ); |
| 28 |
|
| 29 |
} catch( \Exception $exception ) { |
| 30 |
return \Depicter::json([ |
| 31 |
'errors' => [ $exception->getMessage() ] |
| 32 |
])->withStatus( 503 ); |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Retrieves the existing promotion |
| 38 |
* |
| 39 |
* @param RequestInterface $request |
| 40 |
* @param string $view |
| 41 |
* |
| 42 |
* @return ResponseInterface |
| 43 |
* @throws GuzzleException |
| 44 |
*/ |
| 45 |
public function getPromotion( RequestInterface $request, string $view) |
| 46 |
{ |
| 47 |
try { |
| 48 |
$response = \Depicter::remote()->get( 'v1/core/announcements', [ 'query' => [ 'category' => 'promotions-wp-dash' ] ] ); |
| 49 |
$promotions = JSON::decode( $response->getBody(), true ); |
| 50 |
$currentPromotion = []; |
| 51 |
|
| 52 |
if( ! empty( $promotions['hits'] ) ){ |
| 53 |
$currentPromotion = reset($promotions['hits'] ); |
| 54 |
} |
| 55 |
return \Depicter::json( $currentPromotion )->withStatus( 200 ); |
| 56 |
|
| 57 |
} catch( \Exception $exception ) { |
| 58 |
return \Depicter::json([ |
| 59 |
'errors' => [ $exception->getMessage() ] |
| 60 |
])->withStatus( 503 ); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Retrieves the notifications |
| 66 |
* |
| 67 |
* @param RequestInterface $request |
| 68 |
* @param string $view |
| 69 |
* |
| 70 |
* @return ResponseInterface |
| 71 |
* @throws GuzzleException |
| 72 |
*/ |
| 73 |
public function getNotifications( RequestInterface $request, string $view ) { |
| 74 |
$perPage = !empty( $request->query('perpage' ) ) ? Sanitize::int( $request->query( 'perpage' ) ) : 20; |
| 75 |
$page = !empty( $request->query('page' ) ) ? Sanitize::int( $request->query( 'page' ) ) : 1; |
| 76 |
|
| 77 |
try { |
| 78 |
$response = \Depicter::remote()->get( 'v1/core/notifications', [ |
| 79 |
'query' => [ |
| 80 |
'page' => $page, |
| 81 |
'perPage' => $perPage |
| 82 |
] |
| 83 |
]); |
| 84 |
|
| 85 |
$result = JSON::decode( $response->getBody(), true ); |
| 86 |
|
| 87 |
$result['lastSeenID'] = \Depicter::options()->get('lastSeenNotificationID' , ''); |
| 88 |
$result['lastSeenDate'] = \Depicter::options()->get('lastSeenNotificationDate', ''); |
| 89 |
|
| 90 |
return \Depicter::json( $result )->withStatus( 200 ); |
| 91 |
|
| 92 |
} catch( \Exception $exception ) { |
| 93 |
return \Depicter::json([ |
| 94 |
'errors' => [ $exception->getMessage() ] |
| 95 |
])->withStatus( 503 ); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Updates status info for notifications |
| 101 |
* |
| 102 |
* @param RequestInterface $request |
| 103 |
* @param string $view |
| 104 |
* |
| 105 |
* @return ResponseInterface |
| 106 |
*/ |
| 107 |
public function updateNotificationsStatus( RequestInterface $request, string $view ) { |
| 108 |
$lastSeenID = !empty( $request->body('lastSeenID' ) ) ? Sanitize::int( $request->body( 'lastSeenID' ) ) : 0; |
| 109 |
$lastSeenDate = !empty( $request->body('lastSeenDate' ) ) ? Sanitize::textfield( $request->body( 'lastSeenDate' ) ) : ''; |
| 110 |
|
| 111 |
if( empty( $lastSeenID ) && empty( $lastSeenDate ) ){ |
| 112 |
return \Depicter::json([ |
| 113 |
'errors' => [ 'Last seen ID or Date is required.' ] |
| 114 |
])->withStatus( 400 ); |
| 115 |
} |
| 116 |
|
| 117 |
if( !empty( $lastSeenID ) ){ |
| 118 |
\Depicter::options()->set('lastSeenNotificationID', $lastSeenID ); |
| 119 |
} |
| 120 |
if( !empty( $lastSeenDate ) ){ |
| 121 |
\Depicter::options()->set('lastSeenNotificationDate', $lastSeenDate ); |
| 122 |
} |
| 123 |
|
| 124 |
return \Depicter::json( [ 'message' => "Last seen is successfully updated." ] )->withStatus( 200 ); |
| 125 |
} |
| 126 |
|
| 127 |
} |
| 128 |
|