| 1 |
<?php |
| 2 |
namespace Depicter\Controllers\Ajax; |
| 3 |
|
| 4 |
use Averta\Core\Utility\Arr; |
| 5 |
use Averta\WordPress\Utility\JSON; |
| 6 |
use Averta\WordPress\Utility\Sanitize; |
| 7 |
use Psr\Http\Message\ResponseInterface; |
| 8 |
use WPEmerge\Requests\RequestInterface; |
| 9 |
|
| 10 |
class OptionsAjaxController { |
| 11 |
|
| 12 |
/** |
| 13 |
* update setting |
| 14 |
* |
| 15 |
* @param RequestInterface $request |
| 16 |
* @param $view |
| 17 |
* |
| 18 |
* @return ResponseInterface |
| 19 |
*/ |
| 20 |
public function update( RequestInterface $request, $view ) { |
| 21 |
$id = Sanitize::textfield( $request->body('id', '') ); |
| 22 |
|
| 23 |
if ( $id != 'allow_unfiltered_data_upload' ) { |
| 24 |
return \Depicter::json([ |
| 25 |
'errors' => [__('You only has the right to update uploading unfiltered data option', 'depicter' ) ] |
| 26 |
])->withStatus(403); |
| 27 |
} |
| 28 |
|
| 29 |
$value = (bool) $request->body('value', false) ? 'on' : 'off'; |
| 30 |
if ( \Depicter::options()->set( 'allow_unfiltered_data_upload', $value ) ) { |
| 31 |
return \Depicter::json([ |
| 32 |
'success' => true, |
| 33 |
'value' => $value |
| 34 |
])->withStatus(200); |
| 35 |
} |
| 36 |
|
| 37 |
return \Depicter::json([ |
| 38 |
'errors' => [ __('Option value not changed.' , 'depicter' ) ] |
| 39 |
])->withStatus(400); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* get settings list |
| 44 |
* |
| 45 |
* @param RequestInterface $request |
| 46 |
* @param string $view |
| 47 |
* |
| 48 |
* @return ResponseInterface |
| 49 |
*/ |
| 50 |
public function index( RequestInterface $request, $view ): ResponseInterface |
| 51 |
{ |
| 52 |
$options = \Depicter::settings()->getAll(); |
| 53 |
$response = []; |
| 54 |
foreach( $options as $option ) { |
| 55 |
$response[ $option['id'] ] = $option['type'] == 'password' ? "*************" : \Depicter::settings()->getSettingValue( $option['id'] ); |
| 56 |
} |
| 57 |
return \Depicter::json( $response )->withStatus(200); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* get settings list |
| 62 |
* |
| 63 |
* @param RequestInterface $request |
| 64 |
* @param string $view |
| 65 |
* |
| 66 |
* @return ResponseInterface |
| 67 |
*/ |
| 68 |
public function get( RequestInterface $request, $view ): ResponseInterface |
| 69 |
{ |
| 70 |
return \Depicter::json([ |
| 71 |
'hits' => \Depicter::settings()->getAll() |
| 72 |
])->withStatus(200); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* update setting |
| 77 |
* |
| 78 |
* @param RequestInterface $request |
| 79 |
* @param string $view |
| 80 |
* |
| 81 |
* @return ResponseInterface |
| 82 |
*/ |
| 83 |
public function store( RequestInterface $request, $view ): ResponseInterface |
| 84 |
{ |
| 85 |
$settings = ! empty( $request->body('settings', '') ) ? Sanitize::textfield( $request->body('settings', '') ) : Sanitize::textfield( $request->getBody()->getContents(), '' ); |
| 86 |
$settings = JSON::decode( $settings, true ); |
| 87 |
|
| 88 |
if ( empty( $settings ) ) { |
| 89 |
return \Depicter::json([ |
| 90 |
'errors' => ['missing settings!'] |
| 91 |
])->withStatus(400); |
| 92 |
} |
| 93 |
|
| 94 |
$settings = \Depicter::settings()->map( $settings ); |
| 95 |
$failedJobs = []; |
| 96 |
foreach ( $settings as $settingKey => $settingValue ) { |
| 97 |
if ( ! \Depicter::settings()->store( Sanitize::textfield( $settingKey ), Sanitize::textfield( $settingValue ) ) ) { |
| 98 |
$failedJobs[] = $settingKey; |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
if ( empty( $failedJobs ) ) { |
| 103 |
return \Depicter::json([ |
| 104 |
'success' => true |
| 105 |
])->withStatus(200); |
| 106 |
} |
| 107 |
|
| 108 |
return \Depicter::json([ |
| 109 |
/* translators: failed option keys while saving */ |
| 110 |
'errors' => [ sprintf( __( 'Saving the following options failed: %s. Please try again!', 'depicter' ) , implode( ',', $failedJobs ) ) ] |
| 111 |
])->withStatus(400); |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Flush documents cache |
| 116 |
* |
| 117 |
* @param RequestInterface $request |
| 118 |
* @param string $view |
| 119 |
* |
| 120 |
* @return ResponseInterface |
| 121 |
*/ |
| 122 |
public function flushCache( RequestInterface $request, $view ): ResponseInterface |
| 123 |
{ |
| 124 |
depicter_flush_documents_cache(); |
| 125 |
return \Depicter::json([ |
| 126 |
'success' => true |
| 127 |
])->withStatus(200); |
| 128 |
} |
| 129 |
} |
| 130 |
|