| 1 |
<?php |
| 2 |
namespace Depicter\Controllers\Ajax; |
| 3 |
|
| 4 |
use Averta\WordPress\Utility\Sanitize; |
| 5 |
use WPEmerge\Requests\RequestInterface; |
| 6 |
|
| 7 |
class SubscriberAjaxController |
| 8 |
{ |
| 9 |
/** |
| 10 |
* store subscriber |
| 11 |
* @param RequestInterface $request |
| 12 |
* @param $view |
| 13 |
* |
| 14 |
* @return \Psr\Http\Message\ResponseInterface |
| 15 |
* @throws \Depicter\GuzzleHttp\Exception\GuzzleException |
| 16 |
*/ |
| 17 |
public function store( RequestInterface $request, $view ) { |
| 18 |
|
| 19 |
$bodyParams = [ |
| 20 |
'email' => Sanitize::email( $request->body('email') ), |
| 21 |
'group' => Sanitize::textfield( $request->body('group') ), |
| 22 |
'name' => Sanitize::textfield( $request->body('name') ), |
| 23 |
]; |
| 24 |
|
| 25 |
try { |
| 26 |
if ( \Depicter::client()->subscribe( $bodyParams ) ) { |
| 27 |
// Set the user is subscribed to the newsletter |
| 28 |
\Depicter::options()->set('has_subscribed', 1); |
| 29 |
|
| 30 |
return \Depicter::json([ |
| 31 |
"hits" => 1, |
| 32 |
'message' => "You are successfully subscribed to the newsletter." |
| 33 |
])->withStatus(200 ); |
| 34 |
} else { |
| 35 |
return \Depicter::json([ |
| 36 |
'errors' => "Error while sending subscriber, please try again later" |
| 37 |
])->withStatus(400 ); |
| 38 |
} |
| 39 |
} catch ( \Exception $exception ){ |
| 40 |
return \Depicter::json([ |
| 41 |
'errors' => [ $exception->getMessage() ] |
| 42 |
])->withStatus(400); |
| 43 |
} |
| 44 |
} |
| 45 |
} |
| 46 |
|