class-ld-rest-exams-questions-gutenberg-posts-controller.php
7.18 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
<?php
/**
* LearnDash REST API Questions Gutenberg Controller.
*
* This Controller class is used to GET/UPDATE/DELETE the LearnDash
* custom post type question (ld_question).
*
* This class extends the WP_REST_Posts_Controller class.
*
* @since 4.0.0
* @package LearnDash
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ( ! class_exists( 'LD_REST_Exams_Questions_Gutenberg_Controller' ) ) && ( class_exists( 'WP_REST_Posts_Controller' ) ) ) {
/**
* Class LearnDash REST API Questions Gutenberg Controller.
*
* @since 4.0.0
* @uses WP_REST_Posts_Controller
*/
class LD_REST_Exams_Questions_Gutenberg_Controller extends WP_REST_Posts_Controller {
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound
// ToDo: Better error handling.
/**
* Current Post Extra Rest Fields
*
* @var array $fields.
*/
protected $fields = array();
/**
* Public constructor for class
*
* @since 4.0.0
*
* @param string $post_type Post type.
*/
public function __construct( $post_type = '' ) {
if ( empty( $post_type ) ) {
$post_type = learndash_get_post_type_slug( 'exam_question' );
}
$this->post_type = $post_type;
parent::__construct( $this->post_type );
$this->register_fields();
}
/**
* Prepare the LearnDash Post Type Settings.
*
* @since 4.0.0
*/
protected function register_fields() {
$this->fields = array(
'question_block_content' => array(
'schema' => array(
'field_key' => 'question_block_content',
'type' => 'string',
'required' => false,
'default' => '',
'context' => array( 'view', 'edit' ),
),
'get_callback' => array( $this, 'get_rest_settings_field_value' ),
),
);
foreach ( $this->fields as $field_key => $field_args ) {
register_rest_field(
$this->post_type,
$field_key,
$field_args
);
}
}
/**
* Get REST Setting Field value.
*
* @since 4.0.0
*
* @param array $postdata Post data array.
* @param string $field_name Field Name for $postdata value.
* @param WP_REST_Request $request Request object.
* @param string $post_type Post Type for request.
* @return null|array REST field value.
*/
public function get_rest_settings_field_value( array $postdata, $field_name, WP_REST_Request $request, $post_type ) {
if ( ! $postdata['content']['raw'] || ! isset( $this->fields[ $field_name ] ) ) {
return null;
}
switch ( $field_name ) {
case 'question_block_content':
$blocks = parse_blocks( $postdata['content']['raw'] );
if ( ! empty( $blocks[0] ) ) {
$blocks[0]['innerHTML'] = null;
$blocks[0]['innerContent'] = null;
return $blocks;
}
return null;
default:
return null;
}
}
/**
* Retrieves a single post (ld_question).
*
* @since 4.0.0
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
*/
public function get_item( $request ) {
try {
$response = parent::get_item( $request );
if ( 'edit' !== $request['context'] || empty( $request['id'] ) ) {
return $response;
}
$response->data['content']['raw'] = $this->get_ld_question_block_content( $request['id'] );
return $response;
} catch ( Exception $e ) {
return new WP_Error(
'rest_get-ld_question',
__( 'Error while trying to get the item', 'learndash' ),
array( 'status' => 400 )
);
}
}
/**
* Block content from ld_question.
*
* @since 4.0.0
*
* @param int $post_id Post id.
* @return string
*/
private function get_ld_question_block_content( $post_id ) {
$post = $this->get_post( $post_id );
$answer_meta = get_post_meta( $post_id, 'answer', true );
$answer = array(
'answer' => $answer_meta ?? array(),
);
$question_title = $post->post_title;
return serialize_block(
array(
'blockName' => 'learndash/ld-exam-question',
'innerContent' => array( $post->post_content ),
'attrs' => array(
'question_title' => $question_title,
'answer' => $answer,
),
)
);
}
/**
* Updates a single post.
*
* @since 4.0.0
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
*/
public function update_item( $request ) {
$block_ld_question = $this->get_block_ld_question( $request );
if ( is_wp_error( $block_ld_question ) ) {
return $block_ld_question;
}
$update_item_response = parent::update_item( $request );
if ( is_wp_error( $update_item_response ) ) {
return $update_item_response;
}
$update_question_item = $this->update_question_item( $request['id'], $block_ld_question, $request['status'] );
if ( is_wp_error( $update_question_item ) ) {
return $update_question_item;
}
return $update_item_response;
}
/**
* Updates a single ld_question post.
*
* @since 4.0.0
*
* @param int $post_id Post id.
* @param array $block Block.
* @param string $status Post status.
* @return int|WP_Error Response int on success, or WP_Error object on failure.
*/
private function update_question_item( $post_id, $block, $status = 'publish' ) {
// ToDo: Check why is changing the $status to draft even when is not requested.
try {
$attrs = $block['attrs'];
$inner_blocks = serialize_blocks( $block['innerBlocks'] );
$question = array_merge( $attrs, array( 'inner_blocks_content' => $inner_blocks ) );
if ( empty( $question['question_title'] ) ) {
return new WP_Error(
'rest_post-ld_question',
__( 'Missing question_title', 'learndash' ),
array( 'status' => 400 )
);
}
$post_args = array(
'ID' => $post_id,
'post_title' => $question['question_title'],
'post_type' => 'ld_question',
'post_content' => $question['inner_blocks_content'],
'post_status' => $status,
'meta_input' => $question['answer'],
);
return wp_insert_post( $post_args );
} catch ( Exception $e ) {
return new WP_Error(
'rest_post-ld_question',
__( 'Error', 'learndash' ),
array( 'status' => 400 )
);
}
}
/**
* Get learndash/ld-question block from a ld_question post.
*
* @since 4.0.0
*
* @param WP_REST_Request $request WP_REST_Request object.
* @return array|WP_Error Response array on success, or WP_Error object on failure.
*/
private function get_block_ld_question( $request ) {
if ( empty( $request['id'] ) || empty( $request['content'] ) ) {
return new WP_Error(
'rest_post-ld_question',
__( 'Missing content', 'learndash' ),
array( 'status' => 400 )
);
}
$block = parse_blocks( trim( $request['content'] ) );
if ( empty( $block[0] ) || 'learndash/ld-exam-question' !== $block[0]['blockName'] ) {
return new WP_Error(
'rest_post-ld_question',
__( 'Missing learndash/ld-exam-question block', 'learndash' ),
array( 'status' => 400 )
);
}
return $block[0];
}
// End of functions.
}
}