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 / security-headers.php

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

171 lines 3.6 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\Controller;
4
5 use Calotes\Component\Request;
6 use Calotes\Component\Response;
7 use Calotes\Helper\HTTP;
8 use WP_Defender\Component\Config\Config_Hub_Helper;
9 use WP_Defender\Controller;
10
11 /**
12 * Class Security_Headers
13 *
14 * @package WP_Defender\Controller
15 */
16 class Security_Headers extends Controller {
17 /**
18 * Use for cache.
19 *
20 * @var \WP_Defender\Model\Setting\Security_Headers
21 */
22 public $model;
23
24 /**
25 * @throws \DI\DependencyException
26 * @throws \DI\NotFoundException
27 */
28 public function __construct() {
29 add_filter( 'wp_defender_advanced_tools_data', array( $this, 'script_data' ) );
30 $this->model = wd_di()->get( \WP_Defender\Model\Setting\Security_Headers::class );
31 $this->init_headers();
32 $this->register_routes();
33 }
34
35 /**
36 * Safe way to get cached model.
37 *
38 * @return \WP_Defender\Model\Setting\Security_Headers
39 */
40 private function get_model() {
41 if ( is_object( $this->model ) ) {
42 return $this->model;
43 }
44
45 return new \WP_Defender\Model\Setting\Security_Headers();
46 }
47
48 /**
49 * @param array $data
50 *
51 * @return mixed
52 * @throws \ReflectionException
53 */
54 public function script_data( $data ) {
55 $data['security_headers'] = $this->data_frontend();
56
57 return $data;
58 }
59
60 /**
61 * Save settings.
62 * @defender_route
63 */
64 public function save_settings( Request $request ) {
65 $data = $request->get_data_by_model( $this->model );
66 $this->model->import( $data );
67 if ( $this->model->validate() ) {
68 $this->model->save();
69 Config_Hub_Helper::set_clear_active_flag();
70
71 return new Response(
72 true,
73 array_merge(
74 array( 'message' => __( 'Your settings have been updated.', 'wpdef' ) ),
75 $this->data_frontend()
76 )
77 );
78 }
79
80 return new Response( false, array( 'message' => $this->model->get_formatted_errors() ) );
81 }
82
83 /**
84 * Init headers.
85 */
86 public function init_headers() {
87 if ( ! defined( 'DOING_AJAX' ) ) {
88 // Refresh if on admin, on page with headers.
89 if ( ( is_admin() || is_network_admin() )
90 &&
91 (
92 ( 'wdf-advanced-tools' === HTTP::get( 'page' ) )
93 || ( 'wp-defender' === HTTP::get( 'page' ) )
94 )
95 ) {
96 // This meant we don't have any data or data is overdue need to refresh list of headers.
97 $this->model->refresh_headers();
98 } elseif ( defined( 'DOING_CRON' ) ) {
99 // If this is in cronjob, we refresh it too.
100 $this->model->refresh_headers();
101 }
102 }
103
104 foreach ( $this->model->get_headers() as $rule ) {
105 $rule->add_hooks();
106 }
107 }
108
109 public function remove_settings() {}
110
111 public function remove_data() {}
112
113 /**
114 * A summary data for dashboard.
115 *
116 * @return array
117 */
118 public function to_array() {
119 $misc = $this->get_model()->refresh_headers();
120
121 return array_slice( $misc, 0, 3 );
122 }
123
124 /**
125 * Get data about headers.
126 *
127 * @return array
128 */
129 public function get_type_headers() {
130 return $this->get_model()->get_headers_by_type();
131 }
132
133 /**
134 * @return array
135 */
136 public function data_frontend() {
137 $model = $this->get_model();
138
139 return array_merge( [
140 'model' => $model->export(),
141 'misc' => $model->get_headers_as_array( true ),
142 'enabled' => $model->get_enabled_headers( 3 )
143 ], $this->dump_routes_and_nonces() );
144 }
145
146 /**
147 * @return array
148 */
149 public function dashboard_widget() {
150 return array( 'enabled' => $this->get_model()->get_enabled_headers( 3 ) );
151 }
152
153 public function import_data( $data ) {
154 $model = $this->get_model();
155
156 $model->import( $data );
157 if ( $model->validate() ) {
158 $model->save();
159 }
160 }
161
162 /**
163 * @return array
164 */
165 public function export_strings() {
166 return [
167 $this->get_model()->is_any_activated() ? __( 'Active', 'wpdef' ) : __( 'Inactive', 'wpdef' )
168 ];
169 }
170 }
171