Repository navigation
Expand file tree
/
Copy pathFreshnessSetting.php
More file actions
218 lines (197 loc) · 6.32 KB
/
Copy pathFreshnessSetting.php
File metadata and controls
218 lines (197 loc) · 6.32 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
<?php
/**
* Content Freshness.
*
* @package MWPD\ContentFreshness
* @author Alain Schlesser <alain.schlesser@gmail.com>
* @license MIT
* @link https://github.com/mwpd/content-freshness
* @copyright 2026 Alain Schlesser
*/
declare( strict_types=1 );
namespace MWPD\ContentFreshness\Settings;
use MWPD\ContentFreshness\Exception\ContentFreshnessException;
use MWPD\ContentFreshness\Exception\InvalidMaxAge;
use MWPD\ContentFreshness\Freshness\MaxAge;
use MWPD\ContentFreshness\Freshness\OptionFreshnessPolicy;
use MWPD\ContentFreshness\Infrastructure\{
DefaultRegistrationPriority,
Delayed,
Lifecycle\SiteContext,
Registerable,
Service,
Service\DebugMode,
SiteUninstallable,
ViewFactory
};
/**
* Lets site owners choose, under Settings › Reading, after how many days a
* post counts as outdated.
*
* The service owns the option OptionFreshnessPolicy reads: it registers it,
* sanitizes what is saved into it, and removes it when the plugin is deleted.
*
* It is not Conditional. Waiting for admin_init already keeps it out of every
* request that is not for the admin, so an is_admin() condition would add
* nothing. It would also cost something: lifecycle events skip a Conditional
* service that is not needed, and uninstalling through WP-CLI is not an admin
* request, so the option would be left behind.
*/
final class FreshnessSetting implements Service, Registerable, Delayed, SiteUninstallable {
// Nothing else on "admin_init" needs to run before or after the setting,
// so it registers at the default priority.
use DefaultRegistrationPriority;
/**
* Settings page the field is shown on, and the option group it is saved
* with. It is WordPress's own Reading page.
*
* @var non-empty-string
*/
private const PAGE = 'reading';
/**
* Section of the page the field is shown in.
*
* @var non-empty-string
*/
private const SECTION = 'default';
/**
* View that renders the field.
*
* @var non-empty-string
*/
private const VIEW = 'views/freshness-setting-field';
/**
* View factory to render the field with.
*/
private ViewFactory $view_factory;
/**
* Debug mode, which decides how loudly a failure shows.
*/
private DebugMode $debug_mode;
/**
* Get the action to register the service at.
*
* @return non-empty-string Registration action.
*/
public static function get_registration_action(): string {
return 'admin_init';
}
/**
* Instantiate a FreshnessSetting object.
*
* @param ViewFactory $view_factory View factory to render the field with.
* @param DebugMode $debug_mode Debug mode, which decides how loudly a failure shows.
*/
public function __construct( ViewFactory $view_factory, DebugMode $debug_mode ) {
$this->view_factory = $view_factory;
$this->debug_mode = $debug_mode;
}
/**
* Register the service.
*/
public function register(): void {
\register_setting(
self::PAGE,
OptionFreshnessPolicy::OPTION_NAME,
[
'type' => 'integer',
'description' => \__( 'Days after which a post counts as outdated.', 'content-freshness' ),
'sanitize_callback' => [ $this, 'sanitize' ],
'default' => MaxAge::DEFAULT_DAYS,
'show_in_rest' => false,
]
);
\add_settings_field(
OptionFreshnessPolicy::OPTION_NAME,
\__( 'Posts become outdated after', 'content-freshness' ),
[ $this, 'render_field' ],
self::PAGE,
self::SECTION,
[ 'label_for' => OptionFreshnessPolicy::OPTION_NAME ]
);
}
/**
* Remove the option the service owns from a site when the plugin is
* uninstalled.
*
* The plugin calls this for every site, with the site switched to, so the
* setting goes from each site of a network too.
*
* @param SiteContext $context Site to clean up, switched to.
*/
public function uninstall_site( SiteContext $context ): void {
\delete_option( OptionFreshnessPolicy::OPTION_NAME );
}
/**
* Sanitize a submitted maximum age into a number of days.
*
* WordPress calls this for every value saved into the option. A value that
* is not a valid maximum age is reported to the user on the settings page,
* and the option keeps its current value.
*
* @param mixed $value Submitted value.
*
* @return int Number of days to store.
*/
public function sanitize( $value ): int {
try {
return MaxAge::from_value( $value )->in_days();
} catch ( InvalidMaxAge $exception ) {
// Wrong input is the user's to fix, not a bug, so it becomes a
// message on the settings page in debug mode too.
\add_settings_error(
OptionFreshnessPolicy::OPTION_NAME,
'invalid-max-age',
\sprintf(
/* translators: 1: Smallest number of days allowed, 2: largest number of days allowed. */
\__( 'Enter a whole number of days from %1$d to %2$d. Nothing was changed.', 'content-freshness' ),
MaxAge::MINIMUM_DAYS,
MaxAge::MAXIMUM_DAYS
)
);
return $this->get_stored_max_age()->in_days();
}
}
/**
* Render the field.
*
* This is where control comes back from WordPress, so failures stop here:
* with debug mode on they surface, with it off the field is left out and
* the rest of the settings page works.
*
* @throws ContentFreshnessException If the field fails in debug mode.
*/
public function render_field(): void {
$context = [
'name' => OptionFreshnessPolicy::OPTION_NAME,
'days' => $this->get_stored_max_age()->in_days(),
'minimum' => MaxAge::MINIMUM_DAYS,
'maximum' => MaxAge::MAXIMUM_DAYS,
];
try {
$view = $this->view_factory->create( self::VIEW );
// The view escapes what it prints. PHPCS cannot tell, see phpcs.xml.dist.
echo $view->render( $context ); // phpcs:ignore WordPress.Security.EscapeOutput
} catch ( ContentFreshnessException $exception ) {
if ( $this->debug_mode->is_debug_mode() ) {
throw $exception;
}
}
}
/**
* Get the maximum age the option holds.
*
* The settings page is where a broken value gets fixed, so a value that is
* not a valid maximum age shows as the default here instead of failing:
* saving the form then stores a valid one.
*
* @return MaxAge Stored maximum age, or the default.
*/
private function get_stored_max_age(): MaxAge {
try {
return MaxAge::from_value( \get_option( OptionFreshnessPolicy::OPTION_NAME, MaxAge::DEFAULT_DAYS ) );
} catch ( InvalidMaxAge $exception ) {
return MaxAge::from_days( MaxAge::DEFAULT_DAYS );
}
}
}