PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 5.6.0
Defender Security – Malware Scanner, Login Security & Firewall v5.6.0
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 / class-controller.php

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

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