本文整理汇总了PHP中WP_REST_Request::add_header方法的典型用法代码示例。如果您正苦于以下问题:PHP WP_REST_Request::add_header方法的具体用法?PHP WP_REST_Request::add_header怎么用?PHP WP_REST_Request::add_header使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类WP_REST_Request
的用法示例。
在下文中一共展示了WP_REST_Request::add_header方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: get_queue
/**
* Retrieve the current event queue
*
* @subcommand get-queue
*/
public function get_queue($args, $assoc_args)
{
// Build and make request
$queue_request = new \WP_REST_Request('POST', '/' . \Automattic\WP\Cron_Control\REST_API::API_NAMESPACE . '/' . \Automattic\WP\Cron_Control\REST_API::ENDPOINT_LIST);
$queue_request->add_header('Content-Type', 'application/json');
$queue_request->set_body(wp_json_encode(array('secret' => \WP_CRON_CONTROL_SECRET)));
$queue_request = rest_do_request($queue_request);
// Oh well
if ($queue_request->is_error()) {
\WP_CLI::error($queue_request->as_error()->get_error_message());
}
// Get the decoded JSON object returned by the API
$queue_response = $queue_request->get_data();
// No events, nothing more to do
if (empty($queue_response['events'])) {
\WP_CLI::warning(__('No events in the current queue', 'automattic-cron-control'));
return;
}
// Prepare items for display
$events_for_display = $this->format_events($queue_response['events']);
$total_events_to_display = count($events_for_display);
\WP_CLI::line(sprintf(_n('Displaying one event', 'Displaying %s events', $total_events_to_display, 'automattic-cron-control'), number_format_i18n($total_events_to_display)));
// And reformat
$format = 'table';
if (isset($assoc_args['format'])) {
if ('ids' === $assoc_args['format']) {
\WP_CLI::error(__('Invalid output format requested', 'automattic-cron-control'));
} else {
$format = $assoc_args['format'];
}
}
\WP_CLI\Utils\format_items($format, $events_for_display, array('timestamp', 'action', 'instance', 'scheduled_for', 'internal_event', 'schedule_name', 'event_args'));
}
示例2: test_update_locations
/**
* Test Shipping Zone Locations update endpoint.
* @since 2.7.0
*/
public function test_update_locations()
{
wp_set_current_user($this->user);
$zone = $this->create_shipping_zone('Test Zone');
$request = new WP_REST_Request('PUT', '/wc/v1/shipping/zones/' . $zone->get_id() . '/locations');
$request->add_header('Content-Type', 'application/json');
$request->set_body(json_encode(array(array('code' => 'UK', 'type' => 'country'), array('code' => 'US'), array('code' => 'SW1A0AA', 'type' => 'postcode'), array('type' => 'continent'))));
$response = $this->server->dispatch($request);
$data = $response->get_data();
$this->assertEquals(count($data), 2);
$this->assertEquals(array(array('code' => 'UK', 'type' => 'country', '_links' => array('collection' => array(array('href' => rest_url('/wc/v1/shipping/zones/' . $zone->get_id() . '/locations'))), 'describes' => array(array('href' => rest_url('/wc/v1/shipping/zones/' . $zone->get_id()))))), array('code' => 'SW1A0AA', 'type' => 'postcode', '_links' => array('collection' => array(array('href' => rest_url('/wc/v1/shipping/zones/' . $zone->get_id() . '/locations'))), 'describes' => array(array('href' => rest_url('/wc/v1/shipping/zones/' . $zone->get_id())))))), $data);
}
示例3: test_update_user_invalid_id
public function test_update_user_invalid_id()
{
$this->allow_user_to_manage_multisite();
wp_set_current_user(self::$user);
$params = array('id' => '156', 'username' => 'lisasimpson', 'password' => 'DavidHasselhoff', 'email' => 'smartgirl63_@yahoo.com');
$request = new WP_REST_Request('PUT', sprintf('/wp/v2/users/%d', self::$editor));
$request->add_header('content-type', 'application/x-www-form-urlencoded');
$request->set_body_params($params);
$response = $this->server->dispatch($request);
$this->assertErrorResponse('rest_user_invalid_id', $response, 404);
}
示例4: test_rest_update_post_raw
public function test_rest_update_post_raw()
{
wp_set_current_user(self::$editor_id);
$request = new WP_REST_Request('PUT', sprintf('/wp/v2/posts/%d', self::$post_id));
$request->add_header('content-type', 'application/json');
$params = $this->set_raw_post_data();
$request->set_body(wp_json_encode($params));
$response = $this->server->dispatch($request);
$this->check_update_post_response($response);
$new_data = $response->get_data();
$this->assertEquals(self::$post_id, $new_data['id']);
$this->assertEquals($params['title']['raw'], $new_data['title']['raw']);
$this->assertEquals($params['content']['raw'], $new_data['content']['raw']);
$this->assertEquals($params['excerpt']['raw'], $new_data['excerpt']['raw']);
$post = get_post(self::$post_id);
$this->assertEquals($params['title']['raw'], $post->post_title);
$this->assertEquals($params['content']['raw'], $post->post_content);
$this->assertEquals($params['excerpt']['raw'], $post->post_excerpt);
}
示例5: test_update_comment_content_too_long
/**
* @ticket 38477
*/
public function test_update_comment_content_too_long()
{
wp_set_current_user(self::$admin_id);
$params = array('content' => rand_long_str(66525));
$request = new WP_REST_Request('PUT', sprintf('/wp/v2/comments/%d', self::$approved_id));
$request->add_header('content-type', 'application/json');
$request->set_body(wp_json_encode($params));
$response = $this->server->dispatch($request);
$this->assertErrorResponse('comment_content_column_length', $response, 400);
}
示例6: test_update_comment_invalid_permission
public function test_update_comment_invalid_permission()
{
wp_set_current_user(0);
$params = array('content' => 'Disco Stu likes disco music.');
$request = new WP_REST_Request('PUT', sprintf('/wp/v2/comments/%d', $this->hold_id));
$request->add_header('content-type', 'application/json');
$request->set_body(wp_json_encode($params));
$response = $this->server->dispatch($request);
$this->assertErrorResponse('rest_cannot_edit', $response, 403);
}
示例7: _include_requested_models
/**
* Adds the included models indicated in the request to the entity provided
* @param \EEM_Base $model
* @param \WP_REST_Request $rest_request
* @param array $entity_array
* @return array the modified entity
*/
protected function _include_requested_models(\EEM_Base $model, \WP_REST_Request $rest_request, $entity_array)
{
$includes_for_this_model = $this->explode_and_get_items_prefixed_with($rest_request->get_param('include'), '');
$includes_for_this_model = $this->_remove_model_names_from_array($includes_for_this_model);
//if they passed in * or didn't specify any includes, return everything
if (!in_array('*', $includes_for_this_model) && !empty($includes_for_this_model)) {
if ($model->has_primary_key_field()) {
//always include the primary key. ya just gotta know that at least
$includes_for_this_model[] = $model->primary_key_name();
}
if ($this->explode_and_get_items_prefixed_with($rest_request->get_param('calculate'), '')) {
$includes_for_this_model[] = '_calculated_fields';
}
$entity_array = array_intersect_key($entity_array, array_flip($includes_for_this_model));
}
$relation_settings = $this->get_model_version_info()->relation_settings($model);
foreach ($relation_settings as $relation_name => $relation_obj) {
$related_fields_to_include = $this->explode_and_get_items_prefixed_with($rest_request->get_param('include'), $relation_name);
$related_fields_to_calculate = $this->explode_and_get_items_prefixed_with($rest_request->get_param('calculate'), $relation_name);
//did they specify they wanted to include a related model, or
//specific fields from a related model?
//or did they specify to calculate a field from a related model?
if ($related_fields_to_include || $related_fields_to_calculate) {
//if so, we should include at least some part of the related model
$pretend_related_request = new \WP_REST_Request();
$pretend_related_request->set_query_params(array('caps' => $rest_request->get_param('caps'), 'include' => $related_fields_to_include, 'calculate' => $related_fields_to_calculate));
$pretend_related_request->add_header('no_rest_headers', true);
$related_results = $this->get_entities_from_relation($entity_array[$model->primary_key_name()], $relation_obj, $pretend_related_request);
$entity_array[Read::get_related_entity_name($relation_name, $relation_obj)] = $related_results instanceof \WP_Error ? null : $related_results;
}
}
return $entity_array;
}