-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblock-editor.php
More file actions
98 lines (93 loc) · 2.97 KB
/
block-editor.php
File metadata and controls
98 lines (93 loc) · 2.97 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
<?php
/**
* Support for block editor.
*
* @package tscp
*/
/**
* Register block editor assets.
*/
add_action( 'enqueue_block_editor_assets', function () {
// Register script
wp_enqueue_script( 'tscp-editor-input' );
wp_enqueue_style( 'tscp-editor-input' );
// Register variables.
wp_localize_script( 'tscp-editor-input', 'TscpEditorInput', [
'postTypes' => tscp_post_types(),
] );
} );
/**
* Register REST API for custom fields.
*/
add_action( 'rest_api_init', function () {
$permission_callback = function ( WP_REST_Request $request ) {
return current_user_can( 'edit_post', $request->get_param( 'post_id' ) );
};
$args = [
'post_type' => [
'required' => true,
'type' => 'string',
'validate_callback' => function ( $post_type ) {
return tscp_post_type_can_expire( $post_type );
},
],
'post_id' => [
'required' => true,
'type' => 'int',
'validate_callback' => function ( $post_id ) {
return is_numeric( $post_id ) && get_post( $post_id );
},
],
];
register_rest_route( 'clockwork/v1', '(?P<post_type>[^/]+)/(?P<post_id>\d+)/expiration', [
[
'methods' => 'GET',
'args' => $args,
'permission_callback' => $permission_callback,
'callback' => function ( WP_REST_Request $request ) {
$post_id = $request->get_param( 'post_id' );
return new WP_REST_Response( [
'should_expires' => (bool) get_post_meta( $post_id, '_tscp_should_expire', true ),
'expires' => get_post_meta( $post_id, '_tscp_expires', true ),
] );
},
],
[
'methods' => 'POST',
'args' => array_merge( $args, [
'should' => [
'required' => true,
'type' => 'bool',
],
'expires' => [
'required' => true,
'type' => 'string',
'validate_callback' => function ( $date ) {
return empty( $date ) || preg_match( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/u', $date );
},
],
] ),
'permission_callback' => $permission_callback,
'callback' => function ( WP_REST_Request $request ) {
$post_id = $request->get_param( 'post_id' );
$should_expire = $request->get_param( 'should' );
$expires_at = $request->get_param( 'expires' );
update_post_meta( $post_id, '_tscp_should_expire', $should_expire );
update_post_meta( $post_id, '_tscp_expires', $expires_at );
if ( ! $should_expire ) {
$message = __( 'This post won\'t be expired.', 'tscp' );
} elseif ( empty( $expires_at ) ) {
$message = __( 'This post should be expired but no date set.', 'tscp' );
} else {
// translators: %s is expired at.
$message = sprintf( __( 'This post will be expired at %s.', 'tscp' ), mysql2date( get_option( 'date_format' ) . ' H:i', $expires_at ) );
}
return new WP_REST_Response( [
'message' => $message,
'should_expires' => $should_expire,
'expires' => $expires_at,
] );
},
],
] );
} );