|
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, 'save_settings')); |
|
33
|
|
|
\add_action('wp_ajax_amarkal_settings_reset', array( $this, 'reset_settings')); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function save_settings() |
|
37
|
|
|
{ |
|
38
|
|
|
$this->set_request_data(); |
|
39
|
|
|
$child_page = $this->get_request_child_page(); |
|
40
|
|
|
|
|
41
|
|
|
\wp_send_json($child_page->update($this->request_data)); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function reset_settings() |
|
45
|
|
|
{ |
|
46
|
|
|
$this->set_request_data(); |
|
47
|
|
|
$child_page = $this->get_request_child_page(); |
|
48
|
|
|
|
|
49
|
|
|
\wp_send_json($child_page->reset()); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
private function get_request_child_page() |
|
53
|
|
|
{ |
|
54
|
|
|
$manager = Manager::get_instance(); |
|
55
|
|
|
$slug = $this->request_data['_amarkal_settings_slug']; |
|
56
|
|
|
$parent_slug = $this->request_data['_amarkal_settings_parent_slug']; |
|
57
|
|
|
|
|
58
|
|
|
return $manager->get_child_page($slug, $parent_slug); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
private function set_request_data() |
|
62
|
|
|
{ |
|
63
|
|
|
\parse_str(filter_input(INPUT_POST,'data'),$this->request_data); |
|
64
|
|
|
$nonce = $this->request_data['_amarkal_settings_nonce']; |
|
65
|
|
|
|
|
66
|
|
|
if( !isset( $nonce ) || |
|
67
|
|
|
!\wp_verify_nonce($nonce, self::NONCE_ACTION) ) |
|
68
|
|
|
{ |
|
69
|
|
|
\wp_send_json(array( |
|
70
|
|
|
'values' => array(), |
|
71
|
|
|
'errors' => array( |
|
72
|
|
|
'Your nonce did not verify' |
|
73
|
|
|
) |
|
74
|
|
|
)); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
} |
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
SomeClassto useselfinstead: