-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpost-discussion-toggle.php
More file actions
307 lines (258 loc) · 9.57 KB
/
post-discussion-toggle.php
File metadata and controls
307 lines (258 loc) · 9.57 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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
<?php
/*
Plugin Name: Post Discussion Toggle
Description: Per-post controls to force comments and pingbacks on or off, overriding global Discussion settings.
Version: 1.1.0
Author: Your Name
Text Domain: post-discussion-toggle
License: GPL2
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // I'm not running this outside WordPress. Nice try.
}
class Post_Discussion_Toggle {
const VERSION = '1.1.0';
// Meta keys: I only store explicit overrides, never "default".
const META_COMMENTS = '_pdt_comments_status'; // 'open' | 'closed'
const META_PINGS = '_pdt_pings_status'; // 'open' | 'closed'
// Status values.
const STATUS_DEFAULT = 'default';
const STATUS_OPEN = 'open';
const STATUS_CLOSED = 'closed';
public function __construct() {
// Load translations if someone cares enough to localise this.
add_action( 'plugins_loaded', array( $this, 'load_textdomain' ) );
// Editor UI.
add_action( 'add_meta_boxes', array( $this, 'add_meta_box' ) );
add_action( 'save_post', array( $this, 'save_meta_box_data' ) );
// Runtime behaviour.
add_filter( 'comments_open', array( $this, 'filter_comments_open' ), 10, 2 );
add_filter( 'pings_open', array( $this, 'filter_pings_open' ), 10, 2 );
}
/**
* Load the text domain for translations.
*
* I'm wiring this in properly even if I never ship .po files.
*/
public function load_textdomain() {
load_plugin_textdomain(
'post-discussion-toggle',
false,
dirname( plugin_basename( __FILE__ ) ) . '/languages/'
);
}
/**
* Decide which post types get the meta box.
*
* My rule:
* - Public post types only.
* - They must support comments.
* Anything else is probably not meant to play in this space.
*/
protected function get_supported_post_types() {
$post_types = get_post_types(
array(
'public' => true,
),
'objects'
);
$supported = array();
foreach ( $post_types as $type => $obj ) {
if ( post_type_supports( $type, 'comments' ) ) {
$supported[] = $type;
}
}
// Failsafe: if something strange happens, I at least support posts and pages.
if ( empty( $supported ) ) {
$supported = array( 'post', 'page' );
}
return $supported;
}
/**
* Attach the meta box to all supported post types.
*/
public function add_meta_box() {
$screen_types = $this->get_supported_post_types();
foreach ( $screen_types as $screen ) {
add_meta_box(
'pdt_discussion_controls',
__( 'Discussion controls', 'post-discussion-toggle' ),
array( $this, 'render_meta_box' ),
$screen,
'side',
'default'
);
}
}
/**
* Options I want to show in the dropdown.
*/
protected function get_status_options() {
return array(
self::STATUS_DEFAULT => __( 'Use global setting', 'post-discussion-toggle' ),
self::STATUS_OPEN => __( 'Always allow', 'post-discussion-toggle' ),
self::STATUS_CLOSED => __( 'Always disallow', 'post-discussion-toggle' ),
);
}
/**
* Work out what the UI should display for a given meta key.
*
* Stored values are only 'open' or 'closed'. Anything else means "default".
*/
protected function get_status_for_display( $post_id, $meta_key ) {
$raw = get_post_meta( $post_id, $meta_key, true );
if ( $raw === self::STATUS_OPEN || $raw === self::STATUS_CLOSED ) {
return $raw;
}
return self::STATUS_DEFAULT;
}
/**
* Render the meta box UI.
*
* This works in both Classic and Block editor, because meta boxes still exist
* in Gutenberg, hiding in the advanced panel.
*
* If I ever feel like over-engineering, I can add a proper sidebar plugin
* backed by register_post_meta() with show_in_rest.
*/
public function render_meta_box( $post ) {
wp_nonce_field( 'pdt_save_meta', 'pdt_meta_nonce' );
$comments_status = $this->get_status_for_display( $post->ID, self::META_COMMENTS );
$pings_status = $this->get_status_for_display( $post->ID, self::META_PINGS );
$options = $this->get_status_options();
?>
<p>
<strong><?php esc_html_e( 'Comments', 'post-discussion-toggle' ); ?></strong><br>
<label for="pdt_comments_status" class="screen-reader-text">
<?php esc_html_e( 'Comments status override', 'post-discussion-toggle' ); ?>
</label>
<select name="pdt_comments_status" id="pdt_comments_status">
<?php foreach ( $options as $value => $label ) : ?>
<option value="<?php echo esc_attr( $value ); ?>" <?php selected( $comments_status, $value ); ?>>
<?php echo esc_html( $label ); ?>
</option>
<?php endforeach; ?>
</select>
</p>
<p>
<strong><?php esc_html_e( 'Pings / trackbacks', 'post-discussion-toggle' ); ?></strong><br>
<label for="pdt_pings_status" class="screen-reader-text">
<?php esc_html_e( 'Pings and trackbacks status override', 'post-discussion-toggle' ); ?>
</label>
<select name="pdt_pings_status" id="pdt_pings_status">
<?php foreach ( $options as $value => $label ) : ?>
<option value="<?php echo esc_attr( $value ); ?>" <?php selected( $pings_status, $value ); ?>>
<?php echo esc_html( $label ); ?>
</option>
<?php endforeach; ?>
</select>
</p>
<p class="description">
<?php esc_html_e( 'These options override the global Discussion settings for this item only.', 'post-discussion-toggle' ); ?>
</p>
<?php
}
/**
* Normalise a status value coming in from $_POST.
*
* I'm deliberately strict here: anything unexpected is treated as "ignore".
*/
protected function sanitise_status_from_request( $key ) {
if ( ! isset( $_POST[ $key ] ) ) {
return null;
}
$value = sanitize_text_field( wp_unslash( $_POST[ $key ] ) );
$allowed = array(
self::STATUS_DEFAULT,
self::STATUS_OPEN,
self::STATUS_CLOSED,
);
if ( ! in_array( $value, $allowed, true ) ) {
return null;
}
return $value;
}
/**
* Write a given status to post meta, following my own storage rules:
*
* - 'default' never gets stored (I delete any existing meta).
* - 'open' or 'closed' are stored explicitly.
*
* This keeps the database clean instead of full of "default" noise.
*/
protected function persist_status( $post_id, $meta_key, $status ) {
if ( $status === null ) {
return;
}
if ( $status === self::STATUS_DEFAULT ) {
delete_post_meta( $post_id, $meta_key );
return;
}
update_post_meta( $post_id, $meta_key, $status );
}
/**
* Save handler for the meta box.
*
* Everything here is defensive:
* - No autosave.
* - No revisions.
* - Nonce check.
* - Capability check.
*/
public function save_meta_box_data( $post_id ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
if ( wp_is_post_revision( $post_id ) ) {
return;
}
if ( ! isset( $_POST['pdt_meta_nonce'] ) || ! wp_verify_nonce( $_POST['pdt_meta_nonce'], 'pdt_save_meta' ) ) {
return;
}
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
$comments_status = $this->sanitise_status_from_request( 'pdt_comments_status' );
$pings_status = $this->sanitise_status_from_request( 'pdt_pings_status' );
$this->persist_status( $post_id, self::META_COMMENTS, $comments_status );
$this->persist_status( $post_id, self::META_PINGS, $pings_status );
}
/**
* Single place where I decide whether to override WordPress' opinion.
*/
protected function apply_override_status( $open, $post_id, $meta_key ) {
$status = get_post_meta( $post_id, $meta_key, true );
if ( $status === self::STATUS_OPEN ) {
// I'm forcing this open regardless of the global setting.
return true;
}
if ( $status === self::STATUS_CLOSED ) {
// And here I'm forcing it shut. Silence.
return false;
}
// No override, I respect whatever WordPress already decided.
return $open;
}
/**
* comments_open filter handler.
*/
public function filter_comments_open( $open, $post_id ) {
$post_id = (int) $post_id;
if ( $post_id <= 0 ) {
// If WP gave me nonsense, I won't make it worse.
return $open;
}
return $this->apply_override_status( $open, $post_id, self::META_COMMENTS );
}
/**
* pings_open filter handler.
*/
public function filter_pings_open( $open, $post_id ) {
$post_id = (int) $post_id;
if ( $post_id <= 0 ) {
return $open;
}
return $this->apply_override_status( $open, $post_id, self::META_PINGS );
}
}
new Post_Discussion_Toggle();