1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Amarkal\Settings; |
4
|
|
|
|
5
|
|
|
class RequestHandler |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @var Singleton The reference to *Singleton* instance of this class |
9
|
|
|
*/ |
10
|
|
|
private static $instance; |
11
|
|
|
|
12
|
|
|
private $request_data = array(); |
13
|
|
|
|
14
|
|
|
const NONCE_ACTION = 'amarkal_settings'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Returns the *Singleton* instance of this class. |
18
|
|
|
* |
19
|
|
|
* @return Singleton The *Singleton* instance. |
20
|
|
|
*/ |
21
|
|
|
public static function get_instance() |
22
|
|
|
{ |
23
|
|
|
if( null === static::$instance ) |
|
|
|
|
24
|
|
|
{ |
25
|
|
|
static::$instance = new static(); |
|
|
|
|
26
|
|
|
} |
27
|
|
|
return static::$instance; |
|
|
|
|
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function init() |
31
|
|
|
{ |
32
|
|
|
\add_action('wp_ajax_amarkal_settings_save', array( $this, 'action_save')); |
33
|
|
|
\add_action('wp_ajax_amarkal_settings_reset_all', array( $this, 'action_reset_all')); |
34
|
|
|
\add_action('wp_ajax_amarkal_settings_reset_section', array( $this, 'action_reset_section')); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function action_save() |
38
|
|
|
{ |
39
|
|
|
$this->set_request_data(); |
40
|
|
|
$settings_page = $this->get_request_settings_page(); |
41
|
|
|
|
42
|
|
|
\wp_send_json($settings_page->update($this->request_data)); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function action_reset_all() |
46
|
|
|
{ |
47
|
|
|
$this->set_request_data(); |
48
|
|
|
$settings_page = $this->get_request_settings_page(); |
49
|
|
|
|
50
|
|
|
\wp_send_json($settings_page->reset()); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function action_reset_section() |
54
|
|
|
{ |
55
|
|
|
$this->set_request_data(); |
56
|
|
|
$settings_page = $this->get_request_settings_page(); |
57
|
|
|
$section = $this->request_data['_amarkal_settings_section']; |
58
|
|
|
|
59
|
|
|
\wp_send_json($settings_page->reset_section($section)); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
private function get_request_settings_page() |
63
|
|
|
{ |
64
|
|
|
$manager = Manager::get_instance(); |
65
|
|
|
$slug = $this->request_data['_amarkal_settings_slug']; |
66
|
|
|
|
67
|
|
|
return $manager->get_settings_page($slug); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
private function set_request_data() |
71
|
|
|
{ |
72
|
|
|
$this->request_data = filter_input(INPUT_POST,'data',FILTER_DEFAULT,FILTER_REQUIRE_ARRAY); |
|
|
|
|
73
|
|
|
$nonce = $this->request_data['_amarkal_settings_nonce']; |
74
|
|
|
|
75
|
|
|
if( !isset( $nonce ) || |
76
|
|
|
!\wp_verify_nonce($nonce, self::NONCE_ACTION) ) |
77
|
|
|
{ |
78
|
|
|
\wp_send_json(array( |
79
|
|
|
'values' => array(), |
80
|
|
|
'errors' => array( |
81
|
|
|
'Your nonce did not verify' |
82
|
|
|
) |
83
|
|
|
)); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
} |
Let’s assume you have a class which uses late-static binding:
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()
on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClass
to useself
instead: