本文整理汇总了PHP中WP_REST_Request::set_default_params方法的典型用法代码示例。如果您正苦于以下问题:PHP WP_REST_Request::set_default_params方法的具体用法?PHP WP_REST_Request::set_default_params怎么用?PHP WP_REST_Request::set_default_params使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类WP_REST_Request
的用法示例。
在下文中一共展示了WP_REST_Request::set_default_params方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: batch_items
/**
* Bulk create, update and delete items.
*
* @param WP_REST_Request $request Full details about the request.
* @return array Of WP_Error or WP_REST_Response.
*/
public function batch_items($request)
{
/** @var WP_REST_Server $wp_rest_server */
global $wp_rest_server;
// Get the request params.
$items = array_filter($request->get_params());
$response = array();
// Check batch limit.
$limit = $this->check_batch_limit($items);
if (is_wp_error($limit)) {
return $limit;
}
if (!empty($items['create'])) {
foreach ($items['create'] as $item) {
$_item = new WP_REST_Request('POST');
// Default parameters.
$defaults = array();
$schema = $this->get_public_item_schema();
foreach ($schema['properties'] as $arg => $options) {
if (isset($options['default'])) {
$defaults[$arg] = $options['default'];
}
}
$_item->set_default_params($defaults);
// Set request parameters.
$_item->set_body_params($item);
$_response = $this->create_item($_item);
if (is_wp_error($_response)) {
$response['create'][] = array('id' => 0, 'error' => array('code' => $_response->get_error_code(), 'message' => $_response->get_error_message(), 'data' => $_response->get_error_data()));
} else {
$response['create'][] = $wp_rest_server->response_to_data($_response, '');
}
}
}
if (!empty($items['update'])) {
foreach ($items['update'] as $item) {
$_item = new WP_REST_Request('PUT');
$_item->set_body_params($item);
$_response = $this->update_item($_item);
if (is_wp_error($_response)) {
$response['update'][] = array('id' => $item['id'], 'error' => array('code' => $_response->get_error_code(), 'message' => $_response->get_error_message(), 'data' => $_response->get_error_data()));
} else {
$response['update'][] = $wp_rest_server->response_to_data($_response, '');
}
}
}
if (!empty($items['delete'])) {
foreach ($items['delete'] as $id) {
$_item = new WP_REST_Request('DELETE');
$_item->set_query_params(array('id' => $id, 'force' => true));
$_response = $this->delete_item($_item);
if (is_wp_error($_response)) {
$response['delete'][] = array('id' => $id, 'error' => array('code' => $_response->get_error_code(), 'message' => $_response->get_error_message(), 'data' => $_response->get_error_data()));
} else {
$response['delete'][] = $wp_rest_server->response_to_data($_response, '');
}
}
}
return $response;
}