PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 3.0.1
Defender Security – Malware Scanner, Login Security & Firewall v3.0.1
6.3.0 6.2.3 6.2.4 6.2.0 6.2.1 6.2.2 6.1.0 5.3.1 5.4.0 5.4.1 5.5.0 5.5.1 5.6.0 5.6.1 5.6.2 5.7.0 5.7.1 5.7.2 5.8.0 5.8.1 5.9.0 6.0.0 6.0.1 3.0.1 3.1.0 All 141 releases
defender-security / src / controller.php

controller.php in Defender Security – Malware Scanner, Login Security & Firewall 3.0.1, at src/controller.php

204 lines 4.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WP_Defender;
4
5 use Calotes\Helper\HTTP;
6 use Calotes\Model\Setting;
7 use WP_Defender\Traits\Permission;
8
9 abstract class Controller extends \Calotes\Base\Controller {
10 use \WP_Defender\Traits\IO;
11 use \WP_Defender\Traits\User;
12 use Permission;
13
14 /**
15 * @var string
16 */
17 protected $parent_slug = 'wp-defender';
18
19
20 /**
21 * All the variables that we will show on frontend, both in the main page, or dashboard widget.
22 *
23 * @return array
24 */
25 abstract public function data_frontend();
26
27 /**
28 * Export the data of this module, we will use this for export to HUB, create a preset etc.
29 *
30 * @return array
31 */
32 abstract public function to_array();
33
34 /**
35 * Import the data of other source into this, it can be when HUB trigger the import, or user apply a preset.
36 *
37 * @param $data array
38 *
39 * @return boolean
40 */
41 abstract public function import_data( $data );
42
43 /**
44 * Remove all settings, configs generated in this container runtime.
45 *
46 * @return mixed
47 */
48 abstract public function remove_settings();
49
50 /**
51 * Remove all data.
52 *
53 * @return mixed
54 */
55 abstract public function remove_data();
56
57 /**
58 * Export strings.
59 *
60 * @return array
61 */
62 abstract public function export_strings();
63
64 /**
65 * An internal cache.
66 *
67 * @var array
68 */
69 private $cache = array();
70
71 /**
72 * Queue mandatory assets.
73 */
74 public function enqueue_main_assets() {
75 if ( $this->is_page_active() ) {
76 wp_enqueue_script( 'clipboard' );
77 wp_enqueue_style( 'defender' );
78 wp_enqueue_script( 'wpmudev-sui' );
79 }
80 }
81
82 /**
83 * This too check if the current page is active, so we can queue right assets.
84 *
85 * @return bool
86 */
87 public function is_page_active() {
88 $current = HTTP::get( 'page' );
89
90 return $current === $this->slug;
91 }
92
93 /**
94 * Quick handler to check nonce.
95 *
96 * @param string $intention
97 * @param string $method
98 *
99 * @return bool
100 */
101 protected function verify_nonce( $intention, $method = 'get' ) {
102 $nonce = 'get' === $method ? HTTP::get( '_def_nonce' ) : HTTP::post( '_def_nonce' );
103 if ( ! wp_verify_nonce( $nonce, $intention ) ) {
104 return false;
105 }
106
107 return true;
108 }
109
110 /**
111 * Quick way for saving settings.
112 * Todo: need?
113 *
114 * @param Setting $model
115 * @param $category
116 *
117 * @return bool
118 */
119 public function default_update_settings( Setting &$model, $category ) {
120 if ( ! $this->check_permission() ) {
121 return false;
122 }
123 if ( ! $this->verify_nonce( 'update_settings' . $category ) ) {
124 return false;
125 }
126
127 $data = HTTP::post( 'data' );
128 $data = apply_filters( 'defender_filtering_data_settings', $data );
129 $model->import( $data );
130 if ( $model->validate() ) {
131 $model->save();
132
133 // Bind for submit data to DEV.
134 if ( ! wp_next_scheduled( 'defender_hub_sync' ) ) {
135 wp_schedule_single_event( time(), 'defender_hub_sync' );
136 }
137
138 return true;
139 }
140
141 return false;
142 }
143
144 // Todo: need?
145 public function queue_to_sync_with_hub() {
146 if ( ! wp_next_scheduled( 'defender_hub_sync' ) ) {
147 wp_schedule_single_event( time(), 'defender_hub_sync' );
148 }
149 }
150
151 /**
152 * Read through this class and generate a list of intention method, register it with the central.
153 * The methods that have annotation @defender_method will be registered automatically.
154 */
155 public function register_routes() {
156 foreach ( $this->get_methods() as $method ) {
157 $doc_block = $method->getDocComment();
158 if ( stristr( $doc_block, '@defender_route' ) ) {
159 if ( 'register_routes' === $method->getName() ) {
160 continue;
161 }
162 $is_public = stristr( $doc_block, '@is_public' );
163 wd_central()->add_route( $method->getName(), static::class, ! $is_public );
164 }
165 }
166 }
167
168 /**
169 * Return all methods from current class.
170 *
171 * @return \ReflectionMethod[]
172 */
173 private function get_methods() {
174 $class = new \ReflectionClass( static::class );
175
176 return $class->getMethods( \ReflectionMethod::IS_PUBLIC );
177 }
178
179 /**
180 * Dump the routes and nonces.
181 *
182 * @return array[]
183 */
184 public function dump_routes_and_nonces() {
185 $nonces = array();
186 $routes = array();
187 foreach ( $this->get_methods() as $method ) {
188 $doc_block = $method->getDocComment();
189 if ( stristr( $doc_block, '@defender_route' ) ) {
190 if ( 'register_routes' === $method->getName() ) {
191 continue;
192 }
193 $nonces[ $method->getName() ] = wd_central()->get_nonce( $method->getName(), static::class );
194 $routes[ $method->getName() ] = wd_central()->get_route( $method->getName(), static::class );
195 }
196 }
197
198 return array(
199 'routes' => $routes,
200 'nonces' => $nonces,
201 );
202 }
203 }
204