本文整理汇总了PHP中WP_REST_Request::set_query_params方法的典型用法代码示例。如果您正苦于以下问题:PHP WP_REST_Request::set_query_params方法的具体用法?PHP WP_REST_Request::set_query_params怎么用?PHP WP_REST_Request::set_query_params使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类WP_REST_Request
的用法示例。
在下文中一共展示了WP_REST_Request::set_query_params方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: test_optional_param
public function test_optional_param()
{
register_rest_route('optional', '/test', array('methods' => array('GET'), 'callback' => '__return_null', 'args' => array('foo' => array())));
$request = new WP_REST_Request('GET', '/optional/test');
$request->set_query_params(array());
$response = $this->server->dispatch($request);
$this->assertInstanceOf('WP_REST_Response', $response);
$this->assertEquals(200, $response->get_status());
$this->assertArrayNotHasKey('foo', (array) $request);
}
示例2: testUpdate
public function testUpdate()
{
wp_set_current_user(1);
$expected = array('click_tracking' => 0, 'anon_tracking' => 0, 'license_code' => 'batman');
foreach ($expected as $setting => $value) {
\ingot\testing\crud\settings::write($setting, $value);
}
$request = new \WP_REST_Request('POST', $this->namespaced_route);
$request->set_query_params(array('click_tracking' => 'true', 'anon_tracking' => 'true', 'license_code' => 'allthespidermen'));
$response = $this->server->dispatch($request);
$response = rest_ensure_response($response);
$this->assertEquals(200, $response->get_status());
$data = (array) $response->get_data();
foreach ($expected as $setting => $value) {
$this->assertArrayHasKey($setting, $data);
if ('license_code' != $setting) {
$data[$setting] = intval($data[$setting]);
$this->assertEquals($value, $data[$setting], $setting);
}
}
}
示例3: create_item
/**
* Add meta to an object.
*
* @param WP_REST_Request $request
* @return WP_REST_Response|WP_Error
*/
public function create_item($request)
{
$parent_id = (int) $request['parent_id'];
if (!$this->is_valid_meta_data($request['value'])) {
$code = $this->parent_type === 'post' ? 'rest_post_invalid_action' : 'rest_meta_invalid_action';
// for now let's not allow updating of arrays, objects or serialized values.
return new WP_Error($code, __('Invalid provided meta data for action.'), array('status' => 400));
}
if (empty($request['key'])) {
return new WP_Error('rest_meta_invalid_key', __('Invalid meta key.'), array('status' => 400));
}
/* We should be able to update protected meta if we want to
if ( is_protected_meta( $request['key'] ) ) {
return new WP_Error( 'rest_meta_protected', sprintf( __( '%s is marked as a protected field.' ), $request['key'] ), array( 'status' => 403 ) );
} */
$meta_key = wp_slash($request['key']);
$value = wp_slash($request['value']);
$mid = add_metadata($this->parent_type, $parent_id, $meta_key, $value);
if (!$mid) {
return new WP_Error('rest_meta_could_not_add', __('Could not add meta.'), array('status' => 400));
}
$request = new WP_REST_Request('GET');
$request->set_query_params(array('context' => 'edit', 'parent_id' => $parent_id, 'id' => $mid));
$response = rest_ensure_response($this->get_item($request));
$response->set_status(201);
$data = $response->get_data();
$response->header('Location', rest_url('wp/v2' . '/' . $this->parent_base . '/' . $parent_id . '/meta/' . $data['id']));
/* This action is documented in lib/endpoints/class-wp-rest-meta-controller.php */
do_action('rest_insert_meta', $data, $request, true);
return $response;
}
示例4: test_update_post_with_categories
public function test_update_post_with_categories()
{
wp_set_current_user(self::$editor_id);
$category = wp_insert_term('Test Category', 'category');
$request = new WP_REST_Request('PUT', sprintf('/wp/v2/posts/%d', self::$post_id));
$params = $this->set_post_data(array('title' => 'Tester', 'categories' => array($category['term_id'])));
$request->set_body_params($params);
$response = $this->server->dispatch($request);
$new_data = $response->get_data();
$this->assertEquals(array($category['term_id']), $new_data['categories']);
$categories_path = '';
$links = $response->get_links();
foreach ($links['https://api.w.org/term'] as $link) {
if ('category' === $link['attributes']['taxonomy']) {
$categories_path = $link['href'];
}
}
$query = parse_url($categories_path, PHP_URL_QUERY);
parse_str($query, $args);
$request = new WP_REST_Request('GET', $args['rest_route']);
unset($args['rest_route']);
$request->set_query_params($args);
$response = $this->server->dispatch($request);
$data = $response->get_data();
$this->assertCount(1, $data);
$this->assertEquals('Test Category', $data[0]['name']);
}
示例5: widget
/**
* Widget instance.
*
* @access public
*
* @param array $args Display arguments including 'before_title', 'after_title',
* 'before_widget', and 'after_widget'.
* @param array $instance The settings for the particular instance of the widget.
*/
public function widget($args, $instance)
{
$instance = array_merge($this->get_default_instance(), $instance);
$exported_args = $args;
unset($exported_args['before_widget']);
unset($exported_args['after_widget']);
$data = array('args' => $exported_args, 'posts' => null);
$wp_rest_server = $this->plugin->get_rest_server();
$request = new \WP_REST_Request('GET', '/wp/v2/posts');
$request->set_query_params(array('filter' => array('posts_per_page' => $instance['number'])));
$response = $wp_rest_server->dispatch($request);
if (!$response->is_error()) {
$data['posts'] = $wp_rest_server->response_to_data($response, true);
$instance['has_more'] = $instance['number'] < $response->headers['X-WP-Total'];
}
$data['instance'] = $instance;
echo $args['before_widget'];
// WPCS: xss ok.
echo '<script type="application/json">';
echo wp_json_encode($data);
echo '</script>';
echo $args['after_widget'];
// WPCS: xss ok.
}
示例6: test_prepare_item
public function test_prepare_item()
{
wp_set_current_user($this->editor_id);
$request = new WP_REST_Request('GET', sprintf('/wp/v2/posts/%d', $this->post_id));
$request->set_query_params(array('context' => 'edit'));
$response = $this->server->dispatch($request);
$this->check_get_post_response($response, 'edit');
}
示例7: from_url
/**
* Retrieves a WP_REST_Request object from a full URL.
*
* @static
* @since 4.5.0
* @access public
*
* @param string $url URL with protocol, domain, path and query args.
* @return WP_REST_Request|false WP_REST_Request object on success, false on failure.
*/
public static function from_url($url)
{
$bits = parse_url($url);
$query_params = array();
if (!empty($bits['query'])) {
wp_parse_str($bits['query'], $query_params);
}
$api_root = rest_url();
if (get_option('permalink_structure') && 0 === strpos($url, $api_root)) {
// Pretty permalinks on, and URL is under the API root
$api_url_part = substr($url, strlen(untrailingslashit($api_root)));
$route = parse_url($api_url_part, PHP_URL_PATH);
} elseif (!empty($query_params['rest_route'])) {
// ?rest_route=... set directly
$route = $query_params['rest_route'];
unset($query_params['rest_route']);
}
$request = false;
if (!empty($route)) {
$request = new WP_REST_Request('GET', $route);
$request->set_query_params($query_params);
}
/**
* Filter the request generated from a URL.
*
* @since 4.5.0
*
* @param WP_REST_Request|false $request Generated request object, or false if URL
* could not be parsed.
* @param string $url URL the request was generated from.
*/
return apply_filters('rest_request_from_url', $request, $url);
}
示例8: test_get_pages_params
public function test_get_pages_params()
{
$this->factory->post->create_many(8, array('post_type' => 'page'));
$request = new WP_REST_Request('GET', '/wp/v2/pages');
$request->set_query_params(array('page' => 2, 'per_page' => 4));
$response = $this->server->dispatch($request);
$this->assertEquals(200, $response->get_status());
$headers = $response->get_headers();
$this->assertEquals(8, $headers['X-WP-Total']);
$this->assertEquals(2, $headers['X-WP-TotalPages']);
$all_data = $response->get_data();
$this->assertEquals(4, count($all_data));
foreach ($all_data as $post) {
$this->assertEquals('page', $post['type']);
}
}
示例9: testGetEDDPrice
/**
* Test GET /products for EDD
*
* @since 1.1.0
*
* @group products_api
* @group edd
* @group price
*
* @covers ingot\testing\api\rest/products::get_price()
*/
public function testGetEDDPrice()
{
wp_set_current_user(1);
ingot_test_data_price::edd_create_simple_download(72);
$product = ingot_test_data_price::edd_create_simple_download(7);
$request = new \WP_REST_Request('GET', $this->namespaced_route . '/price/' . $product->ID);
$request->set_query_params(array('plugin' => 'edd'));
$response = $this->server->dispatch($request);
$response = rest_ensure_response($response);
$this->assertEquals(200, $response->get_status());
$data = (array) $response->get_data();
$this->assertArrayHasKey('price', $data);
$this->assertSame(ingot_sanitize_amount(7), $data['price']);
}
示例10: array
function wp_enqueue_scripts()
{
global $post;
$rest_server = rest_get_server();
if ($this->has_fee()) {
wp_enqueue_style('wp-core-ui', $this->url('/css/wp-core-ui.css'), false, self::VERSION, 'screen');
wp_enqueue_style('wp-core-ui-colors', $this->url('/css/wp-core-ui-colors.css'), false, self::VERSION, 'screen');
wp_enqueue_style('wp-auth-check');
wp_enqueue_script('wp-auth-check');
wp_enqueue_script('fee-tinymce', $this->url('/vendor/tinymce.js'), array(), self::TINYMCE_VERSION, true);
wp_enqueue_script('fee-tinymce-image', $this->url('/js/tinymce.image.js'), array('fee-tinymce'), self::VERSION, true);
wp_enqueue_script('fee-tinymce-theme', $this->url('/js/tinymce.theme.js'), array('fee-tinymce'), self::VERSION, true);
foreach (array('lists', 'paste', 'wordpress', 'wplink', 'wptextpattern', 'wpview') as $plugin) {
wp_enqueue_script('fee-' . $plugin, $this->url('/vendor/' . $plugin . '.js'), array('fee-tinymce'), self::VERSION, true);
}
$tinymce_plugins = array('wordpress', 'feeImage', 'wptextpattern', 'wplink', 'wpview', 'paste', 'lists');
$tinymce_toolbar = array('bold', 'italic', 'strikethrough', 'link');
$tinymce = array('selector' => '.fee-content', 'plugins' => implode(' ', array_unique(apply_filters('fee_tinymce_plugins', $tinymce_plugins))), 'toolbar' => apply_filters('fee_tinymce_toolbar', $tinymce_toolbar), 'theme' => 'fee', 'inline' => true, 'relative_urls' => false, 'convert_urls' => false, 'browser_spellcheck' => true, 'placeholder' => apply_filters('fee_content_placeholder', __('Just write…')), 'wpeditimage_html5_captions' => current_theme_supports('html5', 'caption'), 'end_container_on_empty_block' => true);
$request = new WP_REST_Request('GET', '/wp/v2/' . ($post->post_type === 'page' ? 'pages' : 'posts') . '/' . $post->ID);
$request->set_query_params(array('context' => 'edit'));
$result = $rest_server->dispatch($request);
wp_enqueue_script('fee', $this->url('/js/fee.js'), array('fee-tinymce', 'wp-util', 'heartbeat', 'editor', 'wp-api', 'media-views'), self::VERSION, true);
wp_localize_script('fee', 'feeData', array('tinymce' => apply_filters('fee_tinymce_config', $tinymce), 'post' => $result->get_data(), 'lock' => !wp_check_post_lock($post->ID) ? implode(':', wp_set_post_lock($post->ID)) : false, 'titlePlaceholder' => apply_filters('enter_title_here', __('Enter title here'), $post), 'editURL' => get_edit_post_link()));
$request = new WP_REST_Request('GET', '/wp/v2');
$result = $rest_server->dispatch($request);
wp_localize_script('wp-api', 'wpApiSettings', array('root' => esc_url_raw(get_rest_url()), 'nonce' => wp_create_nonce('wp_rest'), 'versionString' => 'wp/v2/', 'schema' => $result->get_data(), 'cacheSchema' => true));
wp_enqueue_media(array('post' => $post));
wp_deregister_script('mce-view');
wp_enqueue_script('mce-view', $this->url('/vendor/mce-view.js'), array('shortcode', 'jquery', 'media-views', 'media-audiovideo'), self::VERSION, true);
wp_enqueue_script('mce-view-register', $this->url('/js/mce-view-register.js'), array('mce-view', 'fee'), self::VERSION, true);
wp_localize_script('mce-view-register', 'mce_view_register', array('post_id' => $post->ID));
wp_enqueue_style('tinymce-core', $this->url('/css/tinymce.core.css'), false, self::VERSION, 'screen');
wp_enqueue_style('tinymce-view', $this->url('/css/tinymce.view.css'), false, self::VERSION, 'screen');
wp_enqueue_style('fee', $this->url('/css/fee.css'), false, self::VERSION, 'screen');
wp_enqueue_style('dashicons');
}
if (current_user_can('edit_posts')) {
if (is_singular()) {
require_once ABSPATH . '/wp-admin/includes/post.php';
$user_id = wp_check_post_lock($post->ID);
$user = get_userdata($user_id);
}
wp_enqueue_script('fee-adminbar', $this->url('/js/fee-adminbar.js'), array('wp-util', 'wp-api'), self::VERSION, true);
wp_localize_script('fee-adminbar', 'fee_adminbar', array('lock' => is_singular() && $user_id ? $user->display_name : false, 'supportedPostTypes' => $this->get_supported_post_types(), 'postNew' => admin_url('post-new.php'), 'nonce' => wp_create_nonce('fee-new')));
}
}
示例11: testCreateItem
/**
* Test deleting one item
*
* @since 0.4.0
*
* @group rest
* @group group_rest
* @group group
*
* @covers ingot\testing\api\rest\groups::create_item()
*/
public function testCreateItem()
{
wp_set_current_user(1);
$request = new \WP_REST_Request('POST', $this->namespaced_route);
$request->set_query_params(array('type' => 'click', 'sub_type' => 'button_color', 'name' => 'x2'));
$response = $this->server->dispatch($request);
$response = rest_ensure_response($response);
$this->assertEquals(201, $response->get_status());
$data = (array) $response->get_data();
$this->assertArrayHasKey('ID', $data);
$group = \ingot\testing\crud\group::read($data['ID']);
$fields = $this->get_fields_to_check_for();
$this->assertTrue(is_array($data));
$this->assertEquals(count($fields), count($data), var_export($data, true));
foreach ($fields as $field) {
$this->assertArrayHasKey($field, $data);
$this->assertSame($group[$field], $data[$field]);
}
}
示例12: _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;
}
示例13: test_handle_request_get_one__registration_include_answers_and_question_bare_min_from_each
public function test_handle_request_get_one__registration_include_answers_and_question_bare_min_from_each()
{
$this->set_current_user_to_new();
$r = $this->new_model_obj_with_dependencies('Registration');
$this->new_model_obj_with_dependencies('Answer', array('REG_ID' => $r->ID()));
$req = new \WP_REST_Request('GET', \EED_Core_Rest_Api::ee_api_namespace . '4.8.29/registrations/' . $r->ID());
$req->set_query_params(array('include' => 'Answer.ATT_ID, Answer.Question.QST_ID'));
$req->set_url_params(array('id' => $r->ID()));
$response = Read::handle_request_get_one($req);
$entity = $response->get_data();
$this->assertArrayHasKey('answers', $entity);
$answers = $entity['answers'];
foreach ($answers as $answer) {
$this->assertArrayHasKey('question', $answer);
}
}
示例14: 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;
}
示例15: test_prepare_item
public function test_prepare_item()
{
wp_set_current_user(self::$admin_id);
$request = new WP_REST_Request('GET', sprintf('/wp/v2/comments/%d', self::$approved_id));
$request->set_query_params(array('context' => 'edit'));
$response = $this->server->dispatch($request);
$this->assertEquals(200, $response->get_status());
$data = $response->get_data();
$this->check_comment_data($data, 'edit', $response->get_links());
}