-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore.php
More file actions
150 lines (130 loc) · 3.12 KB
/
Copy pathcore.php
File metadata and controls
150 lines (130 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
/**
* Core plugin functionality.
*
* @package SecuritytxtManager
*/
namespace SecuritytxtManager\Core;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
use const SecuritytxtManager\Constants\CAPABILITY;
use const SecuritytxtManager\Constants\QUERY_VAR;
/**
* Default setup routine
*
* @return void
*/
function setup() {
add_action( 'init', __NAMESPACE__ . '\\add_rewrite_rules' );
add_action( 'template_redirect', __NAMESPACE__ . '\\display_security_txt' );
add_action( 'admin_init', __NAMESPACE__ . '\\add_capability' );
add_filter( 'query_vars', __NAMESPACE__ . '\\query_vars' );
add_filter( 'security_txt_content', __NAMESPACE__ . '\\maybe_add_credits' );
}
/**
* Add rewrite rules for security.txt endpoints.
*
* @return void
*/
function add_rewrite_rules() {
add_rewrite_rule( '^security\.txt$', 'index.php?' . QUERY_VAR . '=1', 'top' );
add_rewrite_rule( '^\.well-known/security\.txt$', 'index.php?' . QUERY_VAR . '=1', 'top' );
}
/**
* Register query vars.
*
* @param array $vars Public query vars.
*
* @return array
*/
function query_vars( $vars ) {
$vars[] = QUERY_VAR;
return $vars;
}
/**
* Handle security.txt requests
*
* @return void
*/
function display_security_txt() {
if ( ! is_security_txt_request() ) {
return;
}
$settings = \SecuritytxtManager\Utils\get_settings();
if ( ! empty( $settings['content'] ) ) {
status_header( 200 );
nocache_headers();
header( 'Content-Type: text/plain; charset=utf-8' );
echo esc_html( apply_filters( 'security_txt_content', $settings['content'] ) );
exit;
}
status_header( 404 );
nocache_headers();
exit;
}
/**
* Determine if the current request is for security.txt.
*
* @return bool
*/
function is_security_txt_request() {
if ( (bool) get_query_var( QUERY_VAR ) ) {
return true;
}
if ( empty( $_SERVER['REQUEST_URI'] ) ) {
return false;
}
$request_uri = sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) );
$request_path = wp_parse_url( $request_uri, PHP_URL_PATH );
$allowed_requests = [ '/security.txt', '/.well-known/security.txt' ];
return in_array( $request_path, $allowed_requests, true );
}
/**
* Add credits info to security.txt output
*
* @param string $content Security.txt content
*
* @return mixed|string
*/
function maybe_add_credits( $content ) {
$settings = \SecuritytxtManager\Utils\get_settings();
if ( $settings['credits'] ) {
$content .= PHP_EOL;
$content .= esc_html__( '# Generated by Security.txt Manager: https://wordpress.org/plugins/security-txt-manager', 'security-txt-manager' );
}
return $content;
}
/**
* Map the capability administrator role
*
* @return void
*/
function add_capability() {
$role = get_role( 'administrator' );
if ( $role && ! $role->has_cap( CAPABILITY ) ) {
$role->add_cap( CAPABILITY );
}
}
/**
* Activation routine.
*
* @return void
*/
function activate() {
add_capability();
add_rewrite_rules();
flush_rewrite_rules();
}
/**
* Remove capability on deactivation
*
* @return void
*/
function deactivate() {
$role = get_role( 'administrator' );
if ( $role ) {
$role->remove_cap( CAPABILITY );
}
flush_rewrite_rules();
}