-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass.bll-settings.php
More file actions
260 lines (232 loc) · 9.71 KB
/
Copy pathclass.bll-settings.php
File metadata and controls
260 lines (232 loc) · 9.71 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
<?php
/**
* Broken Link Logger Plugin
*
* @package Broken_Link_Logger
* @version 1.0.0
*/
if ( ! class_exists( 'BLL_Settings' ) ) {
class BLL_Settings {
/**
* Static property to hold the plugin settings
*/
public static $options;
/**
* Constructor to initialize the settings class
*/
public function __construct() {
self::$options = wp_parse_args( get_option( BLL_OPTION_SETTINGS ), self::defaults() ); // Load the plugin settings, falling back to defaults
add_action( 'admin_menu', array( $this, 'add_settings_page' ) ); // Register the settings submenu page
add_action( 'admin_init', array( $this, 'admin_init' ) ); // Register the settings
}
/**
* Default values for every option this plugin stores.
*
* @return array
*/
public static function defaults() {
return array(
'ignore_logged_in_users' => 1,
'ignore_bots' => 1,
'ignore_patterns' => "/wp-content/uploads/*\n/favicon.ico\n*.map",
'retention_days' => 90,
);
}
/**
* Convenience accessor used by the rest of the plugin.
*
* @param string $key Option key.
* @param mixed $default Fallback if not set.
* @return mixed
*/
public static function get( $key, $default = null ) {
if ( null === self::$options ) {
self::$options = wp_parse_args( get_option( BLL_OPTION_SETTINGS ), self::defaults() );
}
return isset( self::$options[ $key ] ) ? self::$options[ $key ] : $default;
}
/**
* The ignore_patterns option, split into a clean array of lines.
*
* @return string[]
*/
public static function ignore_patterns_array() {
$raw = (string) self::get( 'ignore_patterns', '' );
$lines = preg_split( '/\r\n|\r|\n/', $raw );
return array_values( array_filter( array_map( 'trim', $lines ) ) );
}
/**
* Add the "Broken Link Logger" item under Settings.
*
* @return void
*/
public function add_settings_page() {
add_options_page(
esc_html__( 'Broken Link Logger Settings', 'broken-link-logger' ), // Page title
esc_html__( 'Broken Link Logger', 'broken-link-logger' ), // Menu title
'manage_options', // Capability
'broken-link-logger-settings', // Menu slug
array( $this, 'settings_page' ) // Callback function to display the page content
);
}
/**
* Display the settings page content.
*
* @return void
*/
public function settings_page() {
if ( ! current_user_can( 'manage_options' ) ) {
return; // Exit if the user doesn't have the required capability
}
if ( isset( $_GET['settings-updated'] ) ) {
add_settings_error( 'bll_settings_messages', 'bll_settings_message', esc_html__( 'Settings saved.', 'broken-link-logger' ), 'updated' );
}
settings_errors( 'bll_settings_messages' );
require( BLL_PATH . 'views/settings-page.php' );
}
/**
* Register the settings, sections, and fields for the plugin
*
* @return void
*/
public function admin_init() {
register_setting(
'bll_settings_group', // Option group
BLL_OPTION_SETTINGS, // Option name
array( $this, 'validate' ) // Sanitize callback
);
add_settings_section(
'bll_main_section', // Section ID
esc_html__( 'What gets logged', 'broken-link-logger' ), // Section title
null, // Callback function to display the section description
'bll_settings_page' // Page on which to display the section
);
add_settings_field(
'ignore_logged_in_users', // Field ID
esc_html__( 'Ignore logged-in users', 'broken-link-logger' ), // Field title
array( $this, 'ignore_logged_in_users_callback' ), // Callback function to display the field
'bll_settings_page', // Page on which to display the field
'bll_main_section', // Section in which to display the field
array(
'label_for' => 'bll_ignore_logged_in_users', // Associate the label with the field
)
);
add_settings_field(
'ignore_bots',
esc_html__( 'Ignore known bots', 'broken-link-logger' ),
array( $this, 'ignore_bots_callback' ),
'bll_settings_page',
'bll_main_section',
array(
'label_for' => 'bll_ignore_bots',
)
);
add_settings_field(
'retention_days',
esc_html__( 'Retention (days)', 'broken-link-logger' ),
array( $this, 'retention_days_callback' ),
'bll_settings_page',
'bll_main_section',
array(
'label_for' => 'bll_retention_days',
)
);
add_settings_field(
'ignore_patterns',
esc_html__( 'Ignore URL patterns', 'broken-link-logger' ),
array( $this, 'ignore_patterns_callback' ),
'bll_settings_page',
'bll_main_section',
array(
'label_for' => 'bll_ignore_patterns',
)
);
}
/**
* Callback function to render the "ignore logged-in users" field.
*
* @return void
*/
public function ignore_logged_in_users_callback( $args ) {
?>
<input
type="checkbox"
name="<?php echo esc_attr( BLL_OPTION_SETTINGS ); ?>[ignore_logged_in_users]"
id="bll_ignore_logged_in_users"
value="1"
<?php checked( 1, (int) self::get( 'ignore_logged_in_users' ), true ); ?>>
<label for="bll_ignore_logged_in_users"><?php esc_html_e( "Don't log 404s hit while logged in (recommended — avoids logging your own admin/editing noise).", 'broken-link-logger' ); ?></label>
<?php
}
/**
* Callback function to render the "ignore bots" field.
*
* @return void
*/
public function ignore_bots_callback( $args ) {
?>
<input
type="checkbox"
name="<?php echo esc_attr( BLL_OPTION_SETTINGS ); ?>[ignore_bots]"
id="bll_ignore_bots"
value="1"
<?php checked( 1, (int) self::get( 'ignore_bots' ), true ); ?>>
<label for="bll_ignore_bots"><?php esc_html_e( 'Skip requests from common crawler/bot User-Agents.', 'broken-link-logger' ); ?></label>
<?php
}
/**
* Callback function to render the retention-days field.
*
* @return void
*/
public function retention_days_callback( $args ) {
?>
<input
type="number"
min="0"
max="3650"
name="<?php echo esc_attr( BLL_OPTION_SETTINGS ); ?>[retention_days]"
id="bll_retention_days"
class="small-text"
value="<?php echo esc_attr( (int) self::get( 'retention_days' ) ); ?>" />
<span><?php esc_html_e( 'Hits not seen again within this many days are auto-deleted. 0 = keep forever.', 'broken-link-logger' ); ?></span>
<?php
}
/**
* Callback function to render the ignore-patterns field.
*
* @return void
*/
public function ignore_patterns_callback( $args ) {
?>
<textarea
name="<?php echo esc_attr( BLL_OPTION_SETTINGS ); ?>[ignore_patterns]"
id="bll_ignore_patterns"
rows="5"
class="large-text code"><?php echo esc_textarea( self::get( 'ignore_patterns' ) ); ?></textarea>
<p class="description"><?php esc_html_e( 'One pattern per line. "*" matches anything, e.g. /wp-content/uploads/*', 'broken-link-logger' ); ?></p>
<?php
}
/**
* Validate and sanitize the plugin settings before saving them to the database.
*
* @param array $input The input values from the settings form.
* @return array The validated and sanitized input values.
*/
public function validate( $input ) {
$validated_input = self::defaults();
$validated_input['ignore_logged_in_users'] = ! empty( $input['ignore_logged_in_users'] ) ? 1 : 0;
$validated_input['ignore_bots'] = ! empty( $input['ignore_bots'] ) ? 1 : 0;
$validated_input['retention_days'] = isset( $input['retention_days'] )
? max( 0, min( 3650, (int) $input['retention_days'] ) )
: $validated_input['retention_days'];
if ( isset( $input['ignore_patterns'] ) ) {
$lines = preg_split( '/\r\n|\r|\n/', (string) $input['ignore_patterns'] );
$lines = array_filter( array_map( 'trim', $lines ) );
$validated_input['ignore_patterns'] = implode( "\n", $lines );
}
self::$options = $validated_input;
return $validated_input;
}
}
}