AjaxGeneralOptions.php
1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
namespace AC\Controller;
use AC\Ajax;
use AC\Capabilities;
use AC\Registerable;
use AC\Request;
use AC\Settings\GeneralOption;
class AjaxGeneralOptions implements Registerable {
/**
* @var GeneralOption
*/
private $general_option;
public function __construct( GeneralOption $general_option ) {
$this->general_option = $general_option;
}
public function register() {
$this->get_ajax_handler()->register();
}
/**
* @return Ajax\Handler
*/
private function get_ajax_handler() {
$handler = new Ajax\Handler();
$handler
->set_action( 'ac_admin_general_options' )
->set_callback( [ $this, 'handle_request' ] );
return $handler;
}
public function handle_request() {
$this->get_ajax_handler()->verify_request();
if ( ! current_user_can( Capabilities::MANAGE ) ) {
exit;
}
$request = new Request();
$name = (string) $request->filter( 'option_name' );
$value = (string) $request->filter( 'option_value' );
$options = $this->general_option->get();
$options[ $name ] = $value;
$this->general_option->save( $options );
exit;
}
}