本文整理汇总了PHP中WP_REST_Request::set_body_params方法的典型用法代码示例。如果您正苦于以下问题:PHP WP_REST_Request::set_body_params方法的具体用法?PHP WP_REST_Request::set_body_params怎么用?PHP WP_REST_Request::set_body_params使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类WP_REST_Request
的用法示例。
在下文中一共展示了WP_REST_Request::set_body_params方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: test_update_both
/**
* Test we can update both description and title
*/
public function test_update_both()
{
wp_set_current_user($this->editor_id);
$request = new WP_REST_Request('POST', sprintf('/wp/v2/posts/%d', $this->post_id));
$request->set_body_params(array('_yoast_wpseo_title' => '1 2 3', '_yoast_wpseo_metadesc' => '4 5 6'));
$response = $this->server->dispatch($request);
$this->assertNotInstanceOf('WP_Error', $response);
$response = rest_ensure_response($response);
$this->assertEquals(200, $response->get_status());
$this->assertEquals('1 2 3', get_post_meta($this->post_id, '_yoast_wpseo_title', true));
$this->assertEquals('4 5 6', get_post_meta($this->post_id, '_yoast_wpseo_metadesc', true));
}
示例2: test_update_meta_slashed
/**
* Ensure slashes aren't touched in data
*/
public function test_update_meta_slashed()
{
wp_set_current_user($this->admin_id);
$meta_id = add_comment_meta($this->approved_id, 'testkey', 'testvalue');
$request = new WP_REST_Request('POST', sprintf('/wp/v2/comments/%d/meta/%d', $this->approved_id, $meta_id));
$request->set_body_params(array('key' => 'testkey', 'value' => "test slashed \\' value"));
$this->server->dispatch($request);
$meta = get_comment_meta($this->approved_id, 'testkey', false);
$this->assertNotEmpty($meta);
$this->assertCount(1, $meta);
$this->assertEquals("test slashed \\' value", $meta[0]);
}
示例3: test_create_method
/**
* Test creating a Shipping Zone Method.
* @since 2.7.0
*/
public function test_create_method()
{
wp_set_current_user($this->user);
$zone = $this->create_shipping_zone('Zone 1');
$request = new WP_REST_Request('POST', '/wc/v1/shipping/zones/' . $zone->get_id() . '/methods');
$request->set_body_params(array('method_id' => 'flat_rate', 'enabled' => false, 'order' => 2));
$response = $this->server->dispatch($request);
$data = $response->get_data();
$this->assertFalse($data['enabled']);
$this->assertEquals(2, $data['order']);
$this->assertArrayHasKey('cost', $data['settings']);
$this->assertEquals('0', $data['settings']['cost']['value']);
}
示例4: test_additional_field_update_errors
public function test_additional_field_update_errors()
{
$schema = array('type' => 'integer', 'description' => 'Some integer of mine', 'enum' => array(1, 2, 3, 4), 'context' => array('view', 'edit'));
register_rest_field('user', 'my_custom_int', array('schema' => $schema, 'get_callback' => array($this, 'additional_field_get_callback'), 'update_callback' => array($this, 'additional_field_update_callback')));
wp_set_current_user(1);
if (is_multisite()) {
$current_user = wp_get_current_user(1);
update_site_option('site_admins', array($current_user->user_login));
}
// Check for error on update.
$request = new WP_REST_Request('POST', sprintf('/wp/v2/users/%d', self::$user));
$request->set_body_params(array('my_custom_int' => 'returnError'));
$response = $this->server->dispatch($request);
$this->assertErrorResponse('rest_invalid_param', $response, 400);
global $wp_rest_additional_fields;
$wp_rest_additional_fields = array();
}
示例5: test_products_batch
/**
* Test batch managing products.
*/
public function test_products_batch()
{
wp_set_current_user($this->user);
$product = WC_Helper_Product::create_simple_product();
$product_2 = WC_Helper_Product::create_simple_product();
$request = new WP_REST_Request('POST', '/wc/v1/products/batch');
$request->set_body_params(array('update' => array(array('id' => $product->get_id(), 'description' => 'Updated description.')), 'delete' => array($product_2->get_id()), 'create' => array(array('sku' => 'DUMMY SKU BATCH TEST 1', 'regular_price' => '10', 'name' => 'Test Batch Create 1', 'type' => 'external', 'button_text' => 'Test Button'), array('sku' => 'DUMMY SKU BATCH TEST 2', 'regular_price' => '20', 'name' => 'Test Batch Create 2', 'type' => 'simple'))));
$response = $this->server->dispatch($request);
$data = $response->get_data();
$this->assertContains('Updated description.', $data['update'][0]['description']);
$this->assertEquals('DUMMY SKU BATCH TEST 1', $data['create'][0]['sku']);
$this->assertEquals('DUMMY SKU BATCH TEST 2', $data['create'][1]['sku']);
$this->assertEquals('Test Button', $data['create'][0]['button_text']);
$this->assertEquals('external', $data['create'][0]['type']);
$this->assertEquals('simple', $data['create'][1]['type']);
$this->assertEquals($product_2->get_id(), $data['delete'][0]['id']);
$request = new WP_REST_Request('GET', '/wc/v1/products');
$response = $this->server->dispatch($request);
$data = $response->get_data();
$this->assertEquals(3, count($data));
$product->delete(true);
$product_2->delete(true);
}
示例6: test_delete_value_custom_name
public function test_delete_value_custom_name()
{
add_post_meta(self::$post_id, 'test_custom_name', 'janet');
$current = get_post_meta(self::$post_id, 'test_custom_name', true);
$this->assertEquals('janet', $current);
$this->grant_write_permission();
$data = array('meta' => array('new_name' => null));
$request = new WP_REST_Request('POST', sprintf('/wp/v2/posts/%d', self::$post_id));
$request->set_body_params($data);
$response = $this->server->dispatch($request);
$this->assertEquals(200, $response->get_status());
$meta = get_post_meta(self::$post_id, 'test_custom_name', false);
$this->assertEmpty($meta);
}
示例7: test_get_additional_field_registration
public function test_get_additional_field_registration()
{
$schema = array('type' => 'integer', 'description' => 'Some integer of mine', 'enum' => array(1, 2, 3, 4), 'context' => array('view', 'edit'));
register_api_field('user', 'my_custom_int', array('schema' => $schema, 'get_callback' => array($this, 'additional_field_get_callback'), 'update_callback' => array($this, 'additional_field_update_callback')));
$request = new WP_REST_Request('OPTIONS', '/wp/v2/users');
$response = $this->server->dispatch($request);
$data = $response->get_data();
$this->assertArrayHasKey('my_custom_int', $data['schema']['properties']);
$this->assertEquals($schema, $data['schema']['properties']['my_custom_int']);
wp_set_current_user(1);
if (is_multisite()) {
$current_user = wp_get_current_user(1);
update_site_option('site_admins', array($current_user->user_login));
}
$request = new WP_REST_Request('GET', '/wp/v2/users/1');
$response = $this->server->dispatch($request);
$this->assertArrayHasKey('my_custom_int', $response->data);
$request = new WP_REST_Request('POST', '/wp/v2/users/1');
$request->set_body_params(array('my_custom_int' => 123));
$response = $this->server->dispatch($request);
$this->assertEquals(123, get_user_meta(1, 'my_custom_int', true));
$request = new WP_REST_Request('POST', '/wp/v2/users');
$request->set_body_params(array('my_custom_int' => 123, 'email' => 'joe@foobar.com', 'username' => 'abc123', 'password' => 'hello'));
$response = $this->server->dispatch($request);
$this->assertEquals(123, $response->data['my_custom_int']);
global $wp_rest_additional_fields;
$wp_rest_additional_fields = array();
}
示例8: test_update_meta_slashed
/**
* Ensure slashes aren't touched in data
*/
public function test_update_meta_slashed()
{
$post_id = $this->factory->post->create();
$meta_id = add_post_meta($post_id, 'testkey', 'testvalue');
$data = array('key' => 'testkey', 'value' => "test slashed \\' value");
$request = new WP_REST_Request('POST', sprintf('/wp/v2/posts/%d/meta/%d', $post_id, $meta_id));
$request->set_body_params($data);
$response = $this->server->dispatch($request);
$this->assertNotInstanceOf('WP_Error', $response);
$meta = get_post_meta($post_id, 'testkey', false);
$this->assertNotEmpty($meta);
$this->assertCount(1, $meta);
$this->assertEquals("test slashed \\' value", $meta[0]);
}
示例9: do_request
/**
* Do a REST Request
*
* @param string $method
*
*/
private function do_request($method, $route, $assoc_args)
{
if ('internal' === $this->scope) {
$request = new \WP_REST_Request($method, $route);
if (in_array($method, array('POST', 'PUT'))) {
$request->set_body_params($assoc_args);
} else {
foreach ($assoc_args as $key => $value) {
$request->set_param($key, $value);
}
}
$response = rest_do_request($request);
if ($error = $response->as_error()) {
WP_CLI::error($error);
}
return array($response->get_status(), $response->get_data(), $response->get_headers());
} else {
if ('http' === $this->scope) {
$response = Utils\http_request($method, rtrim($this->api_url, '/') . $route, $assoc_args);
$body = json_decode($response->body, true);
if ($response->status_code >= 400) {
if (!empty($body['message'])) {
WP_CLI::error($body['message'] . ' ' . json_encode(array('status' => $response->status_code)));
} else {
switch ($response->status_code) {
case 404:
WP_CLI::error("No {$this->name} found.");
break;
default:
WP_CLI::error('Could not complete request.');
break;
}
}
}
return array($response->status_code, json_decode($response->body, true), $response->headers);
}
}
WP_CLI::error('Invalid scope for REST command.');
}
示例10: test_orders_batch
/**
* Test batch managing product reviews.
*/
public function test_orders_batch()
{
wp_set_current_user($this->user);
$order1 = WC_Helper_Order::create_order();
$order2 = WC_Helper_Order::create_order();
$order3 = WC_Helper_Order::create_order();
$request = new WP_REST_Request('POST', '/wc/v1/orders/batch');
$request->set_body_params(array('update' => array(array('id' => $order1->get_id(), 'payment_method' => 'updated')), 'delete' => array($order2->get_id(), $order3->get_id())));
$response = $this->server->dispatch($request);
$data = $response->get_data();
$this->assertEquals('updated', $data['update'][0]['payment_method']);
$this->assertEquals($order2->get_id(), $data['delete'][0]['id']);
$this->assertEquals($order3->get_id(), $data['delete'][1]['id']);
$request = new WP_REST_Request('GET', '/wc/v1/orders');
$response = $this->server->dispatch($request);
$data = $response->get_data();
$this->assertEquals(1, count($data));
wp_delete_post($order1->get_id(), true);
wp_delete_post($order2->get_id(), true);
wp_delete_post($order3->get_id(), true);
}
示例11: test_validation_image_width
/**
* Test validation of image_width.
*
* @since 2.7.0
*/
public function test_validation_image_width()
{
wp_set_current_user($this->user);
$response = $this->server->dispatch(new WP_REST_Request('GET', sprintf('/wc/v1/settings/%s/%s', 'products', 'shop_thumbnail_image_size')));
$setting = $response->get_data();
$this->assertEquals(array('width' => 180, 'height' => 180, 'crop' => true), $setting['value']);
// test bogus
$request = new WP_REST_Request('PUT', sprintf('/wc/v1/settings/%s/%s', 'products', 'shop_thumbnail_image_size'));
$request->set_body_params(array('value' => array('width' => 400, 'height' => 200, 'crop' => 'asdasdasd')));
$response = $this->server->dispatch($request);
$setting = $response->get_data();
$this->assertEquals(array('width' => 400, 'height' => 200, 'crop' => true), $setting['value']);
$request = new WP_REST_Request('PUT', sprintf('/wc/v1/settings/%s/%s', 'products', 'shop_thumbnail_image_size'));
$request->set_body_params(array('value' => array('width' => 200, 'height' => 100, 'crop' => false)));
$response = $this->server->dispatch($request);
$setting = $response->get_data();
$this->assertEquals(array('width' => 200, 'height' => 100, 'crop' => false), $setting['value']);
}
示例12: batch_items
/**
* Bulk create, update and delete items.
*
* @since 2.7.0
* @param WP_REST_Request $request Full details about the request.
* @return array Of WP_Error or WP_REST_Response.
*/
public function batch_items($request)
{
// Get the request params.
$items = array_filter($request->get_params());
/*
* Since our batch settings update is group-specific and matches based on the route,
* we inject the URL parameters (containing group) into the batch items
*/
if (!empty($items['update'])) {
$to_update = array();
foreach ($items['update'] as $item) {
$to_update[] = array_merge($request->get_url_params(), $item);
}
$request = new WP_REST_Request($request->get_method());
$request->set_body_params(array('update' => $to_update));
}
return parent::batch_items($request);
}
示例13: do_request
/**
* Do a REST Request
*
* @param string $method
*
*/
private function do_request($method, $route, $assoc_args)
{
if (!defined('REST_REQUEST')) {
define('REST_REQUEST', true);
}
$request = new WP_REST_Request($method, $route);
if (in_array($method, array('POST', 'PUT'))) {
$request->set_body_params($assoc_args);
} else {
foreach ($assoc_args as $key => $value) {
$request->set_param($key, $value);
}
}
if (defined('SAVEQUERIES') && SAVEQUERIES) {
$original_queries = is_array($GLOBALS['wpdb']->queries) ? array_keys($GLOBALS['wpdb']->queries) : array();
}
$response = rest_do_request($request);
if (defined('SAVEQUERIES') && SAVEQUERIES) {
$performed_queries = array();
foreach ((array) $GLOBALS['wpdb']->queries as $key => $query) {
if (in_array($key, $original_queries)) {
continue;
}
$performed_queries[] = $query;
}
usort($performed_queries, function ($a, $b) {
if ($a[1] === $b[1]) {
return 0;
}
return $a[1] > $b[1] ? -1 : 1;
});
$query_count = count($performed_queries);
$query_total_time = 0;
foreach ($performed_queries as $query) {
$query_total_time += $query[1];
}
$slow_query_message = '';
if ($performed_queries && 'wc' === WP_CLI::get_config('debug')) {
$slow_query_message .= '. Ordered by slowness, the queries are:' . PHP_EOL;
foreach ($performed_queries as $i => $query) {
$i++;
$bits = explode(', ', $query[2]);
$backtrace = implode(', ', array_slice($bits, 13));
$seconds = round($query[1], 6);
$slow_query_message .= <<<EOT
{$i}:
- {$seconds} seconds
- {$backtrace}
- {$query[0]}
EOT;
$slow_query_message .= PHP_EOL;
}
} elseif ('wc' !== WP_CLI::get_config('debug')) {
$slow_query_message = '. Use --debug=wc to see all queries.';
}
$query_total_time = round($query_total_time, 6);
WP_CLI::debug("wc command executed {$query_count} queries in {$query_total_time} seconds{$slow_query_message}", 'wc');
}
if ($error = $response->as_error()) {
WP_CLI::error($error);
}
return array($response->get_status(), $response->get_data(), $response->get_headers());
}
示例14: test_batch_coupon
/**
* Test batch operations on coupons.
* @since 2.7.0
*/
public function test_batch_coupon()
{
wp_set_current_user($this->user);
$coupon_1 = WC_Helper_Coupon::create_coupon('dummycoupon-1');
$coupon_2 = WC_Helper_Coupon::create_coupon('dummycoupon-2');
$coupon_3 = WC_Helper_Coupon::create_coupon('dummycoupon-3');
$coupon_4 = WC_Helper_Coupon::create_coupon('dummycoupon-4');
$request = new WP_REST_Request('POST', '/wc/v1/coupons/batch');
$request->set_body_params(array('update' => array(array('id' => $coupon_1->get_id(), 'amount' => '5.15')), 'delete' => array($coupon_2->get_id(), $coupon_3->get_id()), 'create' => array(array('code' => 'new-coupon', 'amount' => '11.00'))));
$response = $this->server->dispatch($request);
$data = $response->get_data();
$this->assertEquals('5.15', $data['update'][0]['amount']);
$this->assertEquals('11.00', $data['create'][0]['amount']);
$this->assertEquals('new-coupon', $data['create'][0]['code']);
$this->assertEquals($coupon_2->get_id(), $data['delete'][0]['id']);
$this->assertEquals($coupon_3->get_id(), $data['delete'][1]['id']);
$request = new WP_REST_Request('GET', '/wc/v1/coupons');
$response = $this->server->dispatch($request);
$data = $response->get_data();
$this->assertEquals(3, count($data));
}
示例15: test_update_meta_slashed
/**
* Ensure slashes aren't touched in data
*/
public function test_update_meta_slashed()
{
$meta_id = add_term_meta($this->category_id, 'testkey', 'testvalue');
$request = new WP_REST_Request('POST', sprintf('/wp/v2/categories/%d/meta/%d', $this->category_id, $meta_id));
$request->set_body_params(array('key' => 'testkey', 'value' => "test slashed \\' value"));
$this->server->dispatch($request);
$meta = get_term_meta($this->category_id, 'testkey', false);
$this->assertNotEmpty($meta);
$this->assertCount(1, $meta);
$this->assertEquals("test slashed \\' value", $meta[0]);
}