This repository was archived by the owner on Dec 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathclass-wp-customize-post-setting.php
More file actions
624 lines (550 loc) · 19.4 KB
/
Copy pathclass-wp-customize-post-setting.php
File metadata and controls
624 lines (550 loc) · 19.4 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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
<?php
/**
* Customize Post Setting class.
*
* @package WordPress
* @subpackage Customize
*/
/**
* Class WP_Customize_Post_Setting
*/
class WP_Customize_Post_Setting extends WP_Customize_Setting {
const SETTING_ID_PATTERN = '/^post\[(?P<post_type>[^\]]+)\]\[(?P<post_id>-?\d+)\]$/';
const TYPE = 'post';
/**
* Type of setting.
*
* @access public
* @var string
*/
public $type = self::TYPE;
/**
* Post ID.
*
* @access public
* @var string
*/
public $post_id;
/**
* Post type.
*
* @access public
* @var string
*/
public $post_type;
/**
* Posts component.
*
* @var WP_Customize_Posts
*/
public $posts_component;
/**
* Default post data.
*
* @var array
*/
public $default = array(
'post_author' => '0',
'post_name' => '',
'post_date' => '0000-00-00 00:00:00',
'post_mime_type' => '',
'post_modified' => '0000-00-00 00:00:00',
'post_content' => '',
'post_content_filtered' => '',
'post_title' => '',
'post_excerpt' => '',
'post_status' => 'draft',
'comment_status' => '',
'ping_status' => '',
'post_password' => '',
'post_parent' => 0,
'menu_order' => 0,
'guid' => '',
);
/**
* WP_Customize_Post_Setting constructor.
*
* @param WP_Customize_Manager $manager Manager.
* @param string $id Setting ID.
* @param array $args Setting args.
* @throws Exception If the ID is in an invalid format.
*/
public function __construct( WP_Customize_Manager $manager, $id, $args = array() ) {
if ( ! preg_match( self::SETTING_ID_PATTERN, $id, $matches ) ) {
throw new Exception( 'Illegal setting id: ' . $id );
}
$args['post_id'] = intval( $matches['post_id'] );
$args['post_type'] = $matches['post_type'];
$post_type_obj = get_post_type_object( $args['post_type'] );
if ( ! $post_type_obj ) {
throw new Exception( 'Unrecognized post type: ' . $args['post_type'] );
}
if ( empty( $manager->posts ) || ! ( $manager->posts instanceof WP_Customize_Posts ) ) {
throw new Exception( 'Posts component not instantiated.' );
}
$this->posts_component = $manager->posts;
if ( empty( $args['capability'] ) ) {
$args['capability'] = sprintf( 'edit_post[%d]', $args['post_id'] );
}
parent::__construct( $manager, $id, $args );
if ( empty( $this->default['post_author'] ) ) {
$this->default['post_author'] = (string) get_current_user_id();
}
}
/**
* Get setting ID for a given post.
*
* @param WP_Post $post Post.
* @return string Setting ID.
*/
static function get_post_setting_id( WP_Post $post ) {
return sprintf( 'post[%s][%d]', $post->post_type, $post->ID );
}
/**
* Apply customized post override to a post.
*
* @param WP_Post $post Post.
* @return bool False if the post data did not apply.
*/
public function override_post_data( WP_Post &$post ) {
if ( $post->ID !== $this->post_id ) {
return false;
}
if ( ! isset( $this->posts_component->preview->previewed_post_settings[ $post->ID ] ) ) {
return false;
}
$post_value = $this->post_value( null );
if ( ! is_array( $post_value ) ) {
// Make sure that empty dates are not used in case of setting invalidity.
$empty_date = '0000-00-00 00:00:00';
$date_fields = array(
'post_date' => false,
'post_date_gmt' => true,
'post_modified' => false,
'post_modified_gmt' => true,
);
foreach ( $date_fields as $date_field => $gmt ) {
if ( $empty_date === $post->$date_field ) {
$post->$date_field = current_time( 'mysql', $gmt );
}
}
return false;
}
if ( empty( $post_value['post_date'] ) || '0000-00-00 00:00:00' === $post_value['post_date'] ) {
$post_value['post_date'] = current_time( 'mysql', false );
}
if ( '0000-00-00 00:00:00' === $post_value['post_modified'] ) {
$post_value['post_modified'] = $post_value['post_date'];
}
$post_value = $this->augment_gmt_dates( $post_value );
$post_data_keys = array_keys( $this->default );
$post_data_keys[] = 'post_modified_gmt';
$post_data_keys[] = 'post_date_gmt';
foreach ( $post_value as $key => $value ) {
if ( in_array( $key, $post_data_keys, true ) ) {
$post->$key = $value;
}
}
return true;
}
/**
* Makes sure that the $post_data has the proper types.
*
* @param array $post_data Post data.
*
* @return array
*/
protected function normalize_post_data( array $post_data ) {
$int_properties = array(
'menu_order',
'post_author',
'post_parent',
);
foreach ( $int_properties as $key ) {
$post_data[ $key ] = intval( $post_data[ $key ] );
}
// See <https://core.trac.wordpress.org/ticket/22324>.
$post_data['post_author'] = (string) $post_data['post_author'];
/*
* For some reason WordPress stores newlines in DB as CRLF when saving via
* the WP Admin, but normally a textarea just represents newlines as LF.
*/
if ( isset( $post_data['post_content'] ) ) {
$post_data['post_content'] = preg_replace( '/\r\n/', "\n", $post_data['post_content'] );
}
if ( isset( $post_data['post_excerpt'] ) ) {
$post_data['post_excerpt'] = preg_replace( '/\r\n/', "\n", $post_data['post_excerpt'] );
}
$post_data = wp_array_slice_assoc(
$post_data,
array_keys( $this->default )
);
if ( 'customize-draft' === $post_data['post_status'] || 'auto-draft' === $post_data['post_status'] ) {
$post_data['post_status'] = 'publish';
}
return $post_data;
}
/**
* Return a post's setting value.
*
* @return array Post data.
*/
public function value() {
$post = get_post( $this->post_id );
if ( $post ) {
$post_data = $this->get_post_data( $post );
} else {
$post_data = $this->default;
}
if ( $this->is_previewed ) {
$input_value = $this->post_value( array() );
if ( null !== $input_value ) {
$post_data = array_merge( $post_data, $input_value );
}
}
$post_data = $this->normalize_post_data( $post_data );
return $post_data;
}
/**
* Sanitize the setting's value for use in JavaScript.
*
* @return array Post data.
*/
public function js_value() {
$value = parent::js_value();
$value['post_author'] = intval( $value['post_author'] );
return $value;
}
/**
* Get the post data to be used in a setting value.
*
* @param WP_Post $post Post.
* @return array
*/
public function get_post_data( WP_Post $post ) {
$post_data = $this->normalize_post_data( $post->to_array() );
return $post_data;
}
/**
* Determines whether the incoming post data conflicts with the existing post data.
*
* @param WP_Post $existing_post Existing post.
* @param array $incoming_post_data Incoming post data.
*
* @return bool
*/
protected function is_post_data_conflicted( WP_Post $existing_post, array $incoming_post_data ) {
unset( $incoming_post_data['post_modified'] );
$existing_post_data = $this->get_post_data( $existing_post );
foreach ( $incoming_post_data as $field_id => $field_value ) {
if ( isset( $existing_post_data[ $field_id ] ) && $field_value !== $existing_post_data[ $field_id ] ) {
return true;
}
}
return false;
}
/**
* Sanitize (and validate) an input.
*
* @see wp_insert_post()
*
* @param array $post_data The value to sanitize.
* @return array|WP_Error|null Sanitized post array or WP_Error if invalid (or null if not WP 4.6-alpha).
*/
public function sanitize( $post_data ) {
global $wpdb;
$has_setting_validation = method_exists( 'WP_Customize_Setting', 'validate' );
$post_data = array_merge( $this->default, $post_data );
$update = ( $this->post_id > 0 );
$post_type_obj = get_post_type_object( $this->post_type );
if ( ! empty( $post_data['post_type'] ) && $post_data['post_type'] !== $this->post_type ) {
return $has_setting_validation ? new WP_Error( 'bad_post_type' ) : null;
}
$post_data['post_type'] = $this->post_type;
if ( $update && did_action( 'customize_save_validation_before' ) ) {
// Check post lock.
require_once ABSPATH . 'wp-admin/includes/post.php';
$locked_user = wp_check_post_lock( $this->post_id );
if ( $locked_user ) {
$user = get_user_by( 'ID', $locked_user );
$error_message = sprintf(
/* translators: %s is user display name */
__( 'Post is currently locked by %s.', 'customize-posts' ),
$user ? $user->display_name : __( '(unknown user)', 'customize-posts' )
);
return $has_setting_validation ? new WP_Error( 'post_locked', $error_message ) : null;
}
// Check post update conflict.
$post = get_post( $this->post_id );
$is_update_conflict = (
! empty( $post )
&&
! empty( $post_data['post_modified'] )
&&
$post_data['post_modified'] < $post->post_modified
&&
$this->is_post_data_conflicted( $post, $post_data )
);
if ( $is_update_conflict ) {
$user = get_user_by( 'ID', get_post_meta( $this->post_id, '_edit_last', true ) );
$error_message = sprintf(
/* translators: %s is user display name */
__( 'Conflict due to concurrent post update by %s.', 'customize-posts' ),
$user ? $user->display_name : __( '(unknown user)', 'customize-posts' )
);
$this->posts_component->update_conflicted_settings[ $this->id ] = $this;
return $has_setting_validation ? new WP_Error( 'post_update_conflict', $error_message ) : null;
}
}
$post_data = wp_slash( $post_data );
$unsanitized_post_data = $post_data;
$post_data = sanitize_post( $post_data, 'db' );
$post_data['post_author'] = (string) intval( $post_data['post_author'] );
$initial_sanitized_post_data = $post_data;
$maybe_empty = 'attachment' !== $this->post_type
&& empty( $post_data['post_content'] ) && empty( $post_data['post_title'] ) && empty( $post_data['post_excerpt'] )
&& post_type_supports( $this->post_type, 'editor' )
&& post_type_supports( $this->post_type, 'title' )
&& post_type_supports( $this->post_type, 'excerpt' );
/** This filter is documented in wp-includes/post.php */
if ( 'trash' !== $post_data['post_status'] && apply_filters( 'wp_insert_post_empty_content', $maybe_empty, $post_data ) ) {
return $has_setting_validation ? new WP_Error( 'empty_content', __( 'Content, title, and excerpt are empty.', 'customize-posts' ), array( 'setting_property' => 'post_content' ) ) : null;
}
if ( empty( $post_data['post_status'] ) ) {
$post_data['post_status'] = 'draft';
}
if ( 'attachment' === $this->post_type && ! in_array( $post_data['post_status'], array( 'inherit', 'private', 'trash' ), true ) ) {
$post_data['post_status'] = 'inherit';
}
// Don't allow contributors to set the post slug for pending review posts.
if ( 'pending' === $post_data['post_status'] && ! current_user_can( 'publish_posts' ) ) {
$post_data['post_name'] = '';
}
/*
* Create a valid post name. Drafts and pending posts are allowed to have
* an empty post name.
*/
if ( empty( $post_data['post_name'] ) ) {
if ( ! in_array( $post_data['post_status'], array( 'draft', 'pending', 'auto-draft' ), true ) ) {
$post_data['post_name'] = sanitize_title( $post_data['post_title'] );
}
} else {
// On updates, we need to check to see if it's using the old, fixed sanitization context.
$check_name = sanitize_title( $post_data['post_name'], '', 'old-save' );
if ( $update && strtolower( urlencode( $post_data['post_name'] ) ) === $post_data['post_name'] && get_post_field( 'post_name', $this->post_id ) === $check_name ) {
$post_data['post_name'] = $check_name;
} else {
// New post, or slug has changed.
$post_data['post_name'] = sanitize_title( $post_data['post_name'] );
}
}
/*
* If the post date is empty (due to having been new or a draft) and status
* is not 'draft' or 'pending', set date to now.
*/
$post_date_gmt = null;
if ( '0000-00-00 00:00:00' !== $post_data['post_date'] ) {
$mm = substr( $post_data['post_date'], 5, 2 );
$jj = substr( $post_data['post_date'], 8, 2 );
$aa = substr( $post_data['post_date'], 0, 4 );
$valid_date = wp_checkdate( $mm, $jj, $aa, $post_data['post_date'] );
if ( ! $valid_date ) {
return $has_setting_validation ? new WP_Error( 'invalid_date', __( 'Whoops, the provided date is invalid.', 'customize-posts' ), array( 'setting_property' => 'post_date' ) ) : null;
}
$post_date_gmt = get_gmt_from_date( $post_data['post_date'] );
}
if ( $update || '0000-00-00 00:00:00' === $post_data['post_date'] ) {
$post_data['post_modified'] = current_time( 'mysql' );
} else {
$post_data['post_modified'] = $post_data['post_date'];
}
if ( 'attachment' !== $this->post_type ) {
$now = gmdate( 'Y-m-d H:i:59' );
if ( ! $post_date_gmt ) {
if ( 'future' === $post_data['post_status'] ) {
$post_data['post_status'] = 'publish';
}
} elseif ( 'publish' === $post_data['post_status'] ) {
if ( mysql2date( 'U', $post_date_gmt, false ) > mysql2date( 'U', $now, false ) ) {
$post_data['post_status'] = 'future';
}
} elseif ( 'future' === $post_data['post_status'] ) {
if ( mysql2date( 'U', $post_date_gmt, false ) <= mysql2date( 'U', $now, false ) ) {
$post_data['post_status'] = 'publish';
}
}
}
// Comment status.
if ( empty( $post_data['comment_status'] ) ) {
if ( $update ) {
$post_data['comment_status'] = 'closed';
} else {
$post_data['comment_status'] = get_default_comment_status( $this->post_type );
}
}
// Ping status.
if ( empty( $post_data['ping_status'] ) ) {
if ( $update ) {
$post_data['ping_status'] = 'closed';
} else {
$post_data['ping_status'] = get_default_comment_status( $this->post_type, 'pingback' );
}
}
if ( empty( $post_data['post_author'] ) || ( ! current_user_can( $post_type_obj->cap->edit_others_posts ) && intval( $post_data['post_author'] ) !== get_current_user_id() ) ) {
$post_data['post_author'] = (string) get_current_user_id();
}
if ( empty( $post_data['menu_order'] ) ) {
$post_data['menu_order'] = 0;
}
$post_data['menu_order'] = intval( $post_data['menu_order'] );
if ( 'private' === $post_data['post_status'] ) {
$post_data['post_password'] = '';
}
if ( empty( $post_data['post_parent'] ) ) {
$post_data['post_parent'] = 0;
}
$post_data['post_parent'] = intval( $post_data['post_parent'] );
/** This filter is documented in wp-includes/post.php */
$post_data['post_parent'] = apply_filters( 'wp_insert_post_parent', $post_data['post_parent'], $this->post_id, $post_data, $initial_sanitized_post_data );
// @todo Note that wp_unique_post_slug is not currently being applied.
$emoji_fields = array( 'post_title', 'post_content', 'post_excerpt' );
foreach ( $emoji_fields as $emoji_field ) {
if ( isset( $post_data[ $emoji_field ] ) ) {
$charset = $wpdb->get_col_charset( $wpdb->posts, $emoji_field );
if ( 'utf8' === $charset ) {
$post_data[ $emoji_field ] = wp_encode_emoji( $post_data[ $emoji_field ] );
}
}
}
if ( 'attachment' === $this->post_type ) {
/** This filter is documented in wp-includes/post.php */
$post_data = apply_filters( 'wp_insert_attachment_data', $post_data, $unsanitized_post_data );
} else {
/** This filter is documented in wp-includes/post.php */
$post_data = apply_filters( 'wp_insert_post_data', $post_data, $unsanitized_post_data );
}
$post_data = wp_unslash( $post_data );
$post_data = $this->normalize_post_data( $post_data );
unset( $post_data['post_type'] );
return $post_data;
}
/**
* Flag this setting as one to be previewed.
*
* Note that the previewing logic is handled by WP_Customize_Posts_Preview.
*
* @return bool
*/
public function preview() {
if ( $this->is_previewed ) {
return true;
}
$this->posts_component->preview->previewed_post_settings[ $this->post_id ] = $this;
$this->posts_component->preview->add_preview_filters();
$this->is_previewed = true;
return true;
}
/**
* Augment post data with GMT post_date and post_modified.
*
* @param array $data Post data.
* @return array Post data augmented with GMT dates.
*/
public function augment_gmt_dates( $data ) {
$empty_date = '0000-00-00 00:00:00';
if ( ! empty( $data['post_date'] ) ) {
if ( $empty_date !== $data['post_date'] ) {
$data['post_date_gmt'] = get_gmt_from_date( $data['post_date'] );
} else {
$data['post_date_gmt'] = $empty_date;
}
}
if ( ! empty( $data['post_modified'] ) ) {
if ( $empty_date !== $data['post_modified'] ) {
$data['post_modified_gmt'] = get_gmt_from_date( $data['post_modified'] );
} else {
$data['post_modified_gmt'] = $empty_date;
}
}
return $data;
}
/**
* Update the post.
*
* Please note that the capability check will have already been done.
*
* @see WP_Customize_Setting::save()
* @see wp_update_post()
* @see wp_trash_post()
* @see wp_untrash_post()
*
* @param string $data The value to update.
* @return bool The result of saving the value.
*/
protected function update( $data ) {
// Inserts are not supported yet.
if ( $this->post_id < 0 ) {
return false;
}
$data['ID'] = $this->post_id;
$data['post_type'] = $this->post_type;
$data = $this->augment_gmt_dates( $data );
$is_trashed = 'trash' === $data['post_status'];
$was_trashed = 'trash' === get_post_status( $this->post_id );
$is_auto_draft = in_array( get_post_status( $this->post_id ), array( 'auto-draft', 'customize-draft' ), true );
$transition_to_trash = $is_trashed && ! $was_trashed;
$transition_from_trash = ! $is_trashed && $was_trashed;
// If trashing an auto-draft, just delete it straight-away and short-circuit.
if ( $is_trashed && $is_auto_draft ) {
return false !== wp_delete_post( $this->post_id, true );
}
if ( $is_trashed ) {
add_filter( 'wp_insert_post_empty_content', '__return_false', 100 );
/*
* Do not transition the post_status to trash, use the current value.
*
* If we were to unset `$data['post_status']`, the post would not be
* properly purged from the Customizer pane. And if we transitioned the
* status in `wp_update_post()` then `wp_trash_post()` would return false.
*/
$data['post_status'] = get_post_status( $this->post_id );
}
// Ensure that we can set an empty date if draft.
$data['edit_date'] = true;
$should_store_empty_date = (
'0000-00-00 00:00:00' === $data['post_date']
&&
in_array( $data['post_status'], array( 'draft', 'pending', 'auto-draft', 'customize-draft' ), true )
);
if ( $should_store_empty_date ) {
add_filter( 'wp_insert_post_data', array( $this->posts_component, 'force_empty_post_dates' ) );
add_filter( 'wp_insert_attachment_data', array( $this->posts_component, 'force_empty_post_dates' ) );
}
if ( $transition_from_trash ) {
/** This action is documented in wp-includes/post.php */
do_action( 'untrash_post', $this->post_id );
// Ensure that the post_name supplied in the setting will be used.
delete_post_meta( $this->post_id, '_wp_desired_post_slug' );
}
$r = wp_update_post( wp_slash( $data ), true );
if ( $should_store_empty_date ) {
remove_filter( 'wp_insert_post_data', array( $this->posts_component, 'force_empty_post_dates' ) );
remove_filter( 'wp_insert_attachment_data', array( $this->posts_component, 'force_empty_post_dates' ) );
}
$result = ! is_wp_error( $r );
if ( $transition_to_trash ) {
$result = wp_trash_post( $this->post_id );
}
if ( $is_trashed ) {
remove_filter( 'wp_insert_post_empty_content', '__return_false', 100 );
}
if ( $transition_from_trash ) {
wp_untrash_post_comments( $this->post_id );
/** This action is documented in wp-includes.php */
do_action( 'untrashed_post', $this->post_id );
delete_post_meta( $this->post_id, '_wp_trash_meta_status' );
delete_post_meta( $this->post_id, '_wp_trash_meta_time' );
}
return $result;
}
}