1
|
|
|
<?php |
2
|
|
|
namespace WPDFI; |
3
|
|
|
/** |
4
|
|
|
* This class handle all admin stuffs of this plugin |
5
|
|
|
* |
6
|
|
|
* @author Duc Bui Quang <[email protected]> |
7
|
|
|
* @since 1.0.0 |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
use WPDFI\Traits\Singleton; |
11
|
|
|
|
12
|
|
|
final class Admin |
13
|
|
|
{ |
14
|
|
|
use Singleton; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @traitDoc |
18
|
|
|
*/ |
19
|
|
|
public function initializes() |
20
|
|
|
{ |
21
|
|
|
$this->hooks(); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* All WordPress hooks come here |
26
|
|
|
* |
27
|
|
|
* @since 1.0.0 |
28
|
|
|
* @return void |
29
|
|
|
*/ |
30
|
|
|
public function hooks() { |
31
|
|
|
\add_action( 'admin_enqueue_scripts', [$this, 'wpdfi_enqueue_scripts']); |
|
|
|
|
32
|
|
|
\add_action( 'admin_menu', [$this, 'setting_menu'] ); |
33
|
|
|
\add_action( 'init', [$this, 'update_settings']); |
|
|
|
|
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Enqueue styles and scripts |
38
|
|
|
* |
39
|
|
|
* @since 1.0.0 |
40
|
|
|
* @return void |
41
|
|
|
*/ |
42
|
|
|
public function wpdfi_enqueue_scripts() { |
43
|
|
|
\wp_enqueue_media(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Add new setting menu |
48
|
|
|
* |
49
|
|
|
* @since 1.0.0 |
50
|
|
|
* @return void |
51
|
|
|
*/ |
52
|
|
|
public function setting_menu() { |
53
|
|
|
\add_options_page( 'WPDFI', 'WPDFI', 'manage_options', 'wpdfi-settings.php', [$this, 'render_layout']); |
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Render layout for wpdfi setting page |
58
|
|
|
* |
59
|
|
|
* @since 1.0.0 |
60
|
|
|
* @return void |
61
|
|
|
*/ |
62
|
|
|
public function render_layout() { |
|
|
|
|
63
|
|
|
global $pagenow; |
64
|
|
|
// Exit if this is not options and WP Default Thumbnail settings page |
65
|
|
|
if($pagenow != 'options-general.php' or $_GET['page'] != 'wpdfi-settings.php') return; |
|
|
|
|
66
|
|
|
|
67
|
|
|
echo \wpdfi()->layout->get_admin_layout( |
|
|
|
|
68
|
|
|
$this->_get_tabs(), $this->_get_current_tab(), $this->_get_options(), $this->_get_layout_name() |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Retrieve tabs content |
74
|
|
|
* |
75
|
|
|
* @since 1.0.0 |
76
|
|
|
* @return array |
77
|
|
|
*/ |
78
|
|
|
private function _get_tabs() { |
79
|
|
|
return [ |
80
|
|
|
'dfis' => 'DFIs', |
81
|
|
|
'options' => 'Options' |
82
|
|
|
]; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Get the current Admin tab |
87
|
|
|
* |
88
|
|
|
* @since 1.0.0 |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
|
|
private function _get_current_tab() { |
|
|
|
|
92
|
|
|
return (isset($_GET['tab']) and $_GET['tab']) ? $_GET['tab'] : $this->_get_default_tab(); |
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Retrieve default tab |
97
|
|
|
* |
98
|
|
|
* @since 1.0.0 |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
|
|
private function _get_default_tab() { |
102
|
|
|
return 'dfis'; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Get main setting options |
107
|
|
|
* |
108
|
|
|
* @since 1.0.0 |
109
|
|
|
* @return mixed |
110
|
|
|
*/ |
111
|
|
|
private function _get_options() { |
112
|
|
|
return \get_option('wpdfi-settings'); |
|
|
|
|
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Get the name of current layout, it will be default or exist, depending on settings option |
117
|
|
|
* |
118
|
|
|
* @since 1.0.0 |
119
|
|
|
* @return string |
120
|
|
|
*/ |
121
|
|
|
private function _get_layout_name() { |
122
|
|
|
return ($this->get_option($this->_get_current_tab())) ? 'exist' : 'default'; |
|
|
|
|
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Update main setting options |
127
|
|
|
* |
128
|
|
|
* @param array $options |
129
|
|
|
* @since 1.0.0 |
130
|
|
|
* @return boolean |
131
|
|
|
*/ |
132
|
|
|
private function _update_options($options) { |
133
|
|
|
return \update_option('wpdfi-settings', $options); |
|
|
|
|
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Update wpdfi settings option |
138
|
|
|
* |
139
|
|
|
* @since 1.0.0 |
140
|
|
|
* @return void |
141
|
|
|
*/ |
142
|
|
|
public function update_settings() { |
|
|
|
|
143
|
|
|
if(isset($_GET['page']) and $_GET['page'] == 'wpdfi-settings.php' and isset($_POST['_wpnonce'])) { |
|
|
|
|
144
|
|
|
$nonce = $_POST['_wpnonce']; |
|
|
|
|
145
|
|
|
if ( ! \wp_verify_nonce( $nonce, 'wpdfi-settings-page' ) ) { |
146
|
|
|
// This nonce is not valid. |
147
|
|
|
return; |
148
|
|
|
} else { |
149
|
|
|
// Dont need to store wp_nonce value |
150
|
|
|
unset($_POST['_wpnonce']); |
|
|
|
|
151
|
|
|
unset($_POST['_wp_http_referer']); |
|
|
|
|
152
|
|
|
|
153
|
|
|
$options = $this->_get_options(); |
154
|
|
|
foreach($_POST as $key => $value) { |
|
|
|
|
155
|
|
|
$options[$key] = $value; |
|
|
|
|
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
if($this->_update_options($options)) { |
|
|
|
|
159
|
|
|
\wpdfi()->admin_notice->add('Settings Saved.', 'success'); |
|
|
|
|
160
|
|
|
} else { |
161
|
|
|
\wpdfi()->admin_notice->add('Your options are still the same.', 'warning'); |
|
|
|
|
162
|
|
|
} |
|
|
|
|
163
|
|
|
|
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Get single setting option. |
170
|
|
|
* |
171
|
|
|
* @param string $option_key |
172
|
|
|
* @param string $option_detail |
173
|
|
|
* @since 1.0.0 |
174
|
|
|
* @return mixed(string/array) |
|
|
|
|
175
|
|
|
*/ |
176
|
|
|
public function get_option($option_key, $option_detail = null) { |
177
|
|
|
if($option_detail) return $this->_get_options()[$option_key][$option_detail]; |
|
|
|
|
178
|
|
|
return $this->_get_options()[$option_key]; |
|
|
|
|
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
} |