function delete_plan(WP_REST_Request $request) { $this->set_api_key(); $data = $request->get_params(); if (!isset($data['id'])) { return new WP_Error('data', __('No Customer ID Set'), array('status' => 404)); } try { $plan = \Stripe\Plan::retrieve($data['id']); $plan->delete(); return new WP_REST_Response($plan, 200); } catch (Stripe_AuthenticationError $e) { $body = $e->getJsonBody(); $err = $body['error']; return new WP_Error($err['type'], __($err['message']), array('status' => 403)); } catch (Stripe_Error $e) { $body = $e->getJsonBody(); $err = $body['error']; return new WP_Error($err['type'], __($err['message']), array('status' => 403)); } catch (\Stripe\Error\Base $e) { $body = $e->getJsonBody(); $err = $body['error']; return new WP_Error($err['type'], __($err['message']), array('status' => 403)); } }
function save_settings(WP_REST_Request $data) { $keys = $data->get_params(); if (isset($keys['mode'])) { update_option('stripe_wp_mode', $keys['mode']); } if (isset($keys['keys']['test'])) { update_option('stripe_wp_test_key', $keys['keys']['test']); } if (isset($keys['keys']['prod'])) { update_option('stripe_wp_live_key', $keys['keys']['prod']); } if (isset($keys['confirmation']['type'])) { update_option('stripe_wp_confirmation_type', $keys['confirmation']['type']); } if (isset($keys['confirmation']['page_id'])) { update_option('stripe_wp_confirmation_page', $keys['confirmation']['page_id']); } if (isset($keys['confirmation']['message'])) { update_option('stripe_wp_confirmation_message', $keys['confirmation']['message']); } $settings['mode'] = get_option('stripe_wp_mode', false); $settings['keys']['prod'] = get_option('stripe_wp_live_key', false); $settings['keys']['test'] = get_option('stripe_wp_test_key', false); $settings['confirmation']['type'] = get_option('stripe_wp_confirmation_type', false); $settings['confirmation']['page_id'] = get_option('stripe_wp_confirmation_page', false); $settings['confirmation']['message'] = get_option('stripe_wp_confirmation_message', false); return new WP_REST_Response($settings, 200); }
function save_book(WP_REST_Request $data) { /* * Check if ID set & set $post */ if (!$data['ID']) { return new WP_Error('noID', __('No Book ID', 'js-app-plugin')); } $post = $data->get_params(); $meta = $post['meta']; unset($post[0]); /* * Save Post */ $book['save'] = wp_update_post($post, true); if (is_wp_error($book['save'])) { return new WP_Error('saveError', __($book['save']->get_error_messages(), 'js-app-plugin')); } /* * Save Post Meta */ foreach ($meta as $key => $value) { $book['meta-' . $key] = update_post_meta($post['ID'], $key, $value[0]); } /* * Get post and return */ $book['post'] = get_post($post['ID']); $book['post']->meta = get_post_meta($post['ID']); return new WP_REST_Response($book, 200); }
function verify_admin(WP_REST_Request $request) { $data = $request->get_params(); if (!$this->verify_nonce($data)) { return false; } return current_user_can('edit_theme_options'); }
/** * Save new graph data to WordPress * @param WP_REST_Request $request The request containing the updated data * @return WP_REST_Response */ public function update_items(\WP_REST_Request $request) { $params = $request->get_params(); // First pass to create all new nodes and replace their temp ID with their true ID if (is_array($params['newNodeIndexes'])) { foreach ($params['newNodeIndexes'] as $nodeIndex) { $postID = ''; $oldPostID = $params['nodes'][$nodeIndex]['id']; switch ($params['nodes'][$nodeIndex]['type']) { case 'life': case 'perk': $postID = DataController::getInstance()->getCPT('point-node')->create($params['nodes'][$nodeIndex]); break; } $params['nodes'][$nodeIndex]['id'] = $postID; // Replace all instances of the temp ID with the newly inserted one in link data if (is_array($params['links'])) { foreach ($params['links'] as &$link) { if ($link[0] == $oldPostID) { $link[0] = $postID; } if ($link[1] == $oldPostID) { $link[1] = $postID; } } } } } // Update all data on all nodes foreach ($params['nodes'] as $node) { $links = $this->get_linked_nodes_from_id($node['id'], $params['links']); switch ($node['type']) { // Skill nodes case 'skill': DataController::getInstance()->getCPT('skill')->update_skill_graph_data($node, $links); break; // Upgrade nodes // Upgrade nodes case 'upgrade': DataController::getInstance()->getCPT('skill')->update_upgrade_graph_data($node, $links); break; // Point nodes // Point nodes case 'life': case 'perk': DataController::getInstance()->getCPT('point-node')->update_graph_data($node, $links); break; } } // Delete removed nodes if (is_array($params['deletedNodes'])) { foreach ($params['deletedNodes'] as $node) { wp_delete_post($node, true); } } return new \WP_REST_Response('Zodiaque sauvegardé avec succès!', 200); }
/** * Do search and respond. * * @since 0.0.1 * * @param \WP_REST_Request $request * * @return \WP_REST_Response|WP_Error */ public function the_search($request) { $args = (array) $request->get_params(); $search = new \SWP_Query($args); $query_result = $search->posts; $posts = array(); foreach ($query_result as $post) { $data = $this->prepare_item_for_response($post, $request); $posts[] = $this->prepare_response_for_collection($data); } $response = rest_ensure_response($posts); return $response; }
/** * Get one item from the collection * * @param WP_REST_Request $request Full data about the request. * @return WP_Error|WP_REST_Response */ public function get_item($request) { //get parameters from request $params = $request->get_params(); $item = get_post($params['id']); //do a query, call another class, etc $data = $this->prepare_item_for_response($item, $request); //return a response or error based on some conditional if (1 == 1) { return new WP_REST_Response($data, 200); } else { return new WP_Error('code', __('message', 'text-domain')); } }
function code_check(WP_REST_Request $request) { $data = $request->get_params(); if (!$data['code']) { return new WP_Error('no code', __('you need to pass a code to check'), array('status' => 401)); } $users = new WP_User_Query(array('meta_query' => array(array('key' => '__stripe-wp-referral-code', 'value' => $data['code'])))); if (!empty($users->results)) { $user_id = $users->results[0]->ID; $return = array('code_valid' => true, 'user' => $user_id, 'cus_id' => get_user_meta($user_id, '__stripe_cus_id', true)); } else { $return = array('code_valid' => false); } return new WP_REST_Response($return, 200); }
/** * Update all settings * * @since 0.2.0 * * @param \WP_REST_Request $request Full data about the request. * * @return \WP_Error|\WP_REST_Response */ public function update($request) { $params = $request->get_params(); foreach (array_keys($this->args()) as $setting) { if (isset($params[$setting])) { $saved = \ingot\testing\crud\settings::write($setting, $params[$setting]); if (is_wp_error($saved)) { return rest_ensure_response($saved, 500); } $settings[$setting] = $params[$setting]; } else { $settings[$setting] = \ingot\testing\crud\settings::read($setting); } } return $this->response($request, $settings); }
/** * Update one item from the collection * * @param \WP_REST_Request $request Full data about the request. * * @return \WP_Error|\WP_REST_Request */ public function update_item($request) { $params = $request->get_params(); $color = $params[0]; if (!array_key_exists($color, self::$lights)) { return new \WP_Error('cant-update', __('message', 'particle-api'), array('status' => 500)); } $json = json_decode($request->get_body()); $status = isset($json->status) ? $json->status : null; $string_status = $status ? 'true' : 'false'; $data = new \stdClass(); update_option(self::$lights[$color], $string_status); $data->status = $status; if (is_object($data)) { return new \WP_REST_Response($data, 200); } return new \WP_Error('cant-update', __('message', 'particle-api'), array('status' => 500)); }
/** * Update one item from the collection * * @param \WP_REST_Request $request Full data about the request. * * @return \WP_Error|\WP_REST_Request */ public function update_item($request) { //$item = $this->prepare_item_for_database( $request ); $params = $request->get_params(); $id = intval($params[0]); if (!array_key_exists($id, self::$switches)) { return new \WP_Error('cant-update', __('message', 'particle-api'), array('status' => 500)); } $json = json_decode($request->get_body()); $status = isset($json->status) ? $json->status : null; $data = new \stdClass(); update_option(self::$switches[$id], $status); $data->status = $status; if (is_object($data)) { return new \WP_REST_Response($data, 200); } return new \WP_Error('cant-update', __('message', 'particle-api'), array('status' => 500)); }
/** * Callback that creates the data that send to the endpoint. * * @Override * * @param \WP_REST_Request $request The request. * @return array The array with the data of the endpoint */ public function endpoint_callback(\WP_REST_Request $request) { $params = $request->get_params(); $id = false === $params['id'] ? false : absint($params['id']); $slug = false === $params['slug'] ? false : trim($params['slug'], '/'); if (false === $id && false === $slug) { return new \WP_Error(self::INVALID_PARAMS, 'The request must have either an id or a slug', ['status' => 400]); } if (false !== $id) { $user = get_user_by('id', $id); } else { $user = get_user_by('slug', $slug); } if ($user) { return $this->get_author_data($user); } return []; }
/** * Update settings for the settings object. * * @param WP_REST_Request $request Full detail about the request. * @return WP_Error|array */ public function update_item($request) { $options = $this->get_registered_options(); $params = $request->get_params(); foreach ($options as $name => $args) { if (!array_key_exists($name, $params)) { continue; } // A null value means reset the option, which is essentially deleting it // from the database and then relying on the default value. if (is_null($request[$name])) { delete_option($args['option_name']); } else { update_option($args['option_name'], $request[$name]); } } return $this->get_item($request); }
public function processRegister(WP_REST_Request $request) { $params = $request->get_params(); $option = get_option('qq-app-login'); $appid = $option['appid']; $openid = $params['openId']; $token = $params['accessToken']; $result = json_decode(wp_remote_get("https://api.weixin.qq.com/sns/userinfo?access_token={$token}&openid={$openid}", ['sslcertificates' => dirname(__FILE__) . '/ca-bundle.crt'])['body'], true); if (isset($result['errcode']) && $result['errcode'] != 0) { error_log(print_r($result, true)); return new WP_Error('auth_failed', $result['errmsg'], array('status' => 403)); } $params['image'] = $result['headimgurl']; $params['nickname'] = $result['nickname']; $uid = wp_app_sso_register("wechat", $openid, $token, $params); if (is_wp_error($uid)) { return $uid; } return array('ok' => 1, 'uid' => $uid); }
/** * Get one item from the collection * * @param WP_REST_Request $request Full data about the request. * @return WP_Error|WP_REST_Response */ public function get_item($request) { //get parameters from request $params = $request->get_params(); $allowed = apply_filters('allowed_restful_localized_scripts', $this->allowed_scripts); if (isset($params[0])) { global $wp_scripts; if ($wp_scripts === null) { $wp_scripts = wp_scripts(); } foreach ($wp_scripts->registered as $script) { if ($script->handle == $params[0]) { if (true != $allowed && !in_array($params[0], $allowed)) { return new WP_Error('code', __('Script not authorized to be returned via REST API endpoint. Add script handle with allowed_restful_localized_scripts filter.', 'restful-localized-scripts'), $params[0]); } return new WP_REST_Response($this->prepare_item_for_response($script, $request), 200); } } } return new WP_Error('code', __('No script with the requested handle can be found', 'restful-localized-scripts')); }
function save_settings(WP_REST_Request $request) { $data = $request->get_params(); if (isset($data['mode'])) { update_option('stripe_wp_mode', $data['mode']); } if (isset($data['keys']['test'])) { update_option('stripe_wp_test_key', $data['keys']['test']); } if (isset($data['keys']['prod'])) { update_option('stripe_wp_live_key', $data['keys']['prod']); } if (isset($data['confirmation']['type'])) { update_option('stripe_wp_confirmation_type', $data['confirmation']['type']); } if (isset($data['confirmation']['page_id'])) { update_option('stripe_wp_confirmation_page', $data['confirmation']['page_id']); } if (isset($data['confirmation']['message'])) { update_option('stripe_wp_confirmation_message', $data['confirmation']['message']); } if (isset($data['more_settings']) && !empty($data['more_settings'])) { foreach ($data['more_settings'] as $key => $value) { $settings[$key] = update_option($key, $value); } } $settings['mode'] = get_option('stripe_wp_mode', false); $settings['keys']['prod'] = get_option('stripe_wp_live_key', false); $settings['keys']['test'] = get_option('stripe_wp_test_key', false); $settings['confirmation']['type'] = get_option('stripe_wp_confirmation_type', false); $settings['confirmation']['page_id'] = get_option('stripe_wp_confirmation_page', false); $settings['confirmation']['message'] = get_option('stripe_wp_confirmation_message', false); if (isset($data['more_settings']) && !empty($data['more_settings'])) { foreach ($data['more_settings'] as $key => $value) { $settings[$key] = get_option($key, false); } } return new WP_REST_Response($settings, 200); }
/** * Get the post. * * @param \WP_REST_Request $request The request. * * @return array|\WP_Error */ public function endpoint_callback(\WP_REST_Request $request) { $params = $request->get_params(); $id = $params['id']; $slug = false === $params['slug'] ? false : trim($params['slug'], '/'); if (false === $id && false === $slug) { return new \WP_Error(self::INVALID_PARAMS, 'The request must have either an id or a slug', ['status' => 400]); } $query_args = ['post_type' => 'any', 'no_found_rows' => true, 'update_post_meta_cache' => false, 'update_post_term_cache' => false]; if (false !== $id) { $query_args['p'] = $id; } else { $query_args['name'] = $slug; } $query = new \WP_Query(apply_filters($this->get_query_filter_name(), $query_args, $request)); if ($query->have_posts()) { $query->the_post(); $post = $query->post; $data = ['id' => $post->ID, 'slug' => $post->post_name, 'type' => Type::get($post), 'content' => Content::get($post), 'meta' => Meta\Post::get_all_post_meta($post)]; wp_reset_postdata(); return $this->filter_data($data, $post->ID); } return new \WP_Error(self::NOT_FOUND, 'Nothing found for this query', ['status' => 404]); }
/** * 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) { $items = array_filter($request->get_params()); $params = $request->get_url_params(); $product_id = $params['product_id']; $body_params = array(); foreach (array('update', 'create', 'delete') as $batch_type) { if (!empty($items[$batch_type])) { $injected_items = array(); foreach ($items[$batch_type] as $item) { $injected_items[] = array_merge(array('product_id' => $product_id), $item); } $body_params[$batch_type] = $injected_items; } } $request = new WP_REST_Request($request->get_method()); $request->set_body_params($body_params); return parent::batch_items($request); }
function new_coupon(WP_REST_Request $request) { $data = $request->get_params(); $this->set_api_key(); try { $coupon = \Stripe\Coupon::create($data); return new WP_REST_Response($coupon, 200); } catch (\Stripe\Error\RateLimit $e) { $body = $e->getJsonBody(); $err = $body['error']; return new WP_Error($err['type'], __($err['message']), array('status' => $e->getHttpStatus())); } catch (\Stripe\Error\InvalidRequest $e) { $body = $e->getJsonBody(); $err = $body['error']; return new WP_Error($err['type'], __($err['message']), array('status' => $e->getHttpStatus())); } catch (\Stripe\Error\Authentication $e) { $body = $e->getJsonBody(); $err = $body['error']; return new WP_Error($err['type'], __($err['message']), array('status' => $e->getHttpStatus())); } catch (\Stripe\Error\ApiConnection $e) { $body = $e->getJsonBody(); $err = $body['error']; return new WP_Error($err['type'], __($err['message']), array('status' => $e->getHttpStatus())); } catch (\Stripe\Error\Base $e) { $body = $e->getJsonBody(); $err = $body['error']; return new WP_Error($err['type'], __($err['message']), array('status' => $e->getHttpStatus())); } catch (Exception $e) { $body = $e->getJsonBody(); $err = $body['error']; } }
/** * Hooks into the REST API output to print XML instead of JSON. * * @access private * * @param bool $served Whether the request has already been served. * @param WP_HTTP_ResponseInterface $result Result to send to the client. Usually a WP_REST_Response. * @param WP_REST_Request $request Request used to generate the response. * @param WP_REST_Server $server Server instance. * @return true */ function _oembed_rest_pre_serve_request($served, $result, $request, $server) { $params = $request->get_params(); if ('/wp/v2/oembed' !== $request->get_route() || 'GET' !== $request->get_method()) { return $served; } if (!isset($params['format']) || 'xml' !== $params['format']) { return $served; } // Embed links inside the request. $data = $server->response_to_data($result, false); if (404 === $result->get_status()) { $data = $data[0]; } /** * Filter the XML response. * * @param array $data The original oEmbed response data. */ $result = apply_filters('oembed_xml_response', $data); // Bail if there's no XML. if (!is_string($result)) { status_header(501); die('Not implemented'); } if (!headers_sent()) { $server->send_header('Content-Type', 'text/xml; charset=' . get_option('blog_charset')); } echo $result; return true; }
/** * 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; }
/** * Updates settings for the settings object. * * @since 4.7.0 * @access public * * @param WP_REST_Request $request Full details about the request. * @return array|WP_Error Array on success, or error object on failure. */ public function update_item($request) { $options = $this->get_registered_options(); $params = $request->get_params(); foreach ($options as $name => $args) { if (!array_key_exists($name, $params)) { continue; } /** * Filters whether to preempt a setting value update. * * Allows hijacking the setting update logic and overriding the built-in behavior by * returning true. * * @since 4.7.0 * * @param bool $result Whether to override the default behavior for updating the * value of a setting. * @param string $name Setting name (as shown in REST API responses). * @param mixed $value Updated setting value. * @param array $args Arguments passed to register_setting() for this setting. */ $updated = apply_filters('rest_pre_update_setting', false, $name, $request[$name], $args); if ($updated) { continue; } /* * A null value for an option would have the same effect as * deleting the option from the database, and relying on the * default value. */ if (is_null($request[$name])) { /* * A null value is returned in the response for any option * that has a non-scalar value. * * To protect clients from accidentally including the null * values from a response object in a request, we do not allow * options with non-scalar values to be updated to null. * Without this added protection a client could mistakenly * delete all options that have non-scalar values from the * database. */ if (!is_scalar(get_option($args['option_name'], false))) { return new WP_Error('rest_invalid_stored_value', sprintf(__('The %s property has an invalid stored value, and cannot be updated to null.'), $name), array('status' => 500)); } delete_option($args['option_name']); } else { update_option($args['option_name'], $request[$name]); } } return $this->get_item($request); }
/** * Delete one submission from the collection * * @param WP_REST_Request $request Full data about the request. * @since 7.0 * @return WP_Error|WP_REST_Request */ public function delete_submission($request) { $params = $request->get_params(); $force = false; if (!empty($params['force'])) { $force = (bool) $params['force']; } if ($force) { $deleted = wp_delete_post($params['id'], true); } else { $deleted = wp_trash_post($params['id']); } if ($deleted) { return new WP_REST_Response(true, 200); } return new WP_Error('cant-delete', __('Could not delete submission', 'custom-contact-forms'), array('status' => 500)); }
public function rest_signup(WP_REST_Request $request) { $fields = $request->get_params(); $url_base = Lss_Tools_Option::get_option('url') ?: 'http://admin.localsitesubmit.com'; $api_url = $url_base . '/?func=widget/remote&method=add_new_client'; // Load results from API $data = wp_remote_post($api_url, array('timeout' => 60, 'body' => array("params" => json_encode(array("fields" => $fields))))); $data = json_decode(wp_remote_retrieve_body($data), true); return new WP_REST_Response($data); }
/** * Hooks into the REST API output to print XML instead of JSON. * * @param bool $served Whether the request has already been served. * @param WP_HTTP_ResponseInterface $result Result to send to the client. Usually a WP_REST_Response. * @param WP_REST_Request $request Request used to generate the response. * @param WP_REST_Server $server Server instance. * * @return bool */ public function rest_pre_serve_request($served, $result, $request, $server) { $params = $request->get_params(); if ('/wp/v2/oembed' !== $request->get_route() || 'xml' !== $params['format']) { return $served; } if ('HEAD' === $request->get_method()) { return $served; } if (!headers_sent()) { $server->send_header('Content-Type', 'text/xml; charset=' . get_option('blog_charset')); } // Embed links inside the request. $result = $server->response_to_data($result, false); $oembed = new SimpleXMLElement('<oembed></oembed>'); foreach ($result as $key => $value) { if (is_array($value)) { $element = $oembed->addChild($key); foreach ($value as $k => $v) { $element->addChild($k, $v); } continue; } $oembed->addChild($key, $value); } echo $oembed->asXML(); return true; }
/** * Gets the collection for given relation object * * The same as Read::get_entities_from_model(), except if the relation * is a HABTM relation, in which case it merges any non-foreign-key fields from * the join-model-object into the results * * @param string $id the ID of the thing we are fetching related stuff from * @param \EE_Model_Relation_Base $relation * @param \WP_REST_Request $request * @return array */ public function get_entities_from_relation($id, $relation, $request) { $context = $this->validate_context($request->get_param('caps')); $model = $relation->get_this_model(); $related_model = $relation->get_other_model(); //check if they can access the 1st model object $query_params = array(array($model->primary_key_name() => $id), 'limit' => 1); if ($model instanceof \EEM_Soft_Delete_Base) { $query_params = $model->alter_query_params_so_deleted_and_undeleted_items_included($query_params); } $restricted_query_params = $query_params; $restricted_query_params['caps'] = $context; $this->_set_debug_info('main model query params', $restricted_query_params); $this->_set_debug_info('missing caps', Capabilities::get_missing_permissions_string($related_model, $context)); if (!(Capabilities::current_user_has_partial_access_to($related_model, $context) && $model->exists($restricted_query_params))) { if ($relation instanceof \EE_Belongs_To_Relation) { $related_model_name_maybe_plural = strtolower($related_model->get_this_model_name()); } else { $related_model_name_maybe_plural = \EEH_Inflector::pluralize_and_lower($related_model->get_this_model_name()); } return new \WP_Error(sprintf('rest_%s_cannot_list', $related_model_name_maybe_plural), sprintf(__('Sorry, you are not allowed to list %1$s related to %2$s. Missing permissions: %3$s', 'event_espresso'), $related_model_name_maybe_plural, $relation->get_this_model()->get_this_model_name(), implode(',', array_keys(Capabilities::get_missing_permissions($related_model, $context)))), array('status' => 403)); } $query_params = $this->create_model_query_params($relation->get_other_model(), $request->get_params()); $query_params[0][$relation->get_this_model()->get_this_model_name() . '.' . $relation->get_this_model()->primary_key_name()] = $id; $query_params['default_where_conditions'] = 'none'; $query_params['caps'] = $context; $this->_set_debug_info('model query params', $query_params); /** @type array $results */ $results = $relation->get_other_model()->get_all_wpdb_results($query_params); $nice_results = array(); foreach ($results as $result) { $nice_result = $this->create_entity_from_wpdb_result($relation->get_other_model(), $result, $request->get_param('include'), $query_params['caps']); if ($relation instanceof \EE_HABTM_Relation) { //put the unusual stuff (properties from the HABTM relation) first, and make sure //if there are conflicts we prefer the properties from the main model $join_model_result = $this->create_entity_from_wpdb_result($relation->get_join_model(), $result, $request->get_param('include'), $query_params['caps']); $joined_result = array_merge($nice_result, $join_model_result); //but keep the meta stuff from the main model if (isset($nice_result['meta'])) { $joined_result['meta'] = $nice_result['meta']; } $nice_result = $joined_result; } $nice_results[] = $nice_result; } if ($relation instanceof \EE_Belongs_To_Relation) { return array_shift($nice_results); } else { return $nice_results; } }
/** * Function inherint from the parant Abstract class that is called once the * endpoint has been initiated and the method that returns the data delivered * to the endpoint. * * @Override * * @since 0.1.0 * * @param \WP_REST_Request $request The request object that mimics the request * made by the user. * @return array The data to be delivered to the endpoint */ public function endpoint_callback(\WP_REST_Request $request) { $this->args = $request->get_params(); return $this->filter_data($this->loop()); }
public function processResetPassword(WP_REST_Request $request) { $params = $request->get_params(); $option = get_option('phone-app-login'); $phone = $params['phone']; $code = $params['code']; $result = $this->verify($phone, $code); if (is_wp_error($result)) { return $result; } if (!$result) { return new WP_Error('verify_failed', '验证码验证失败。', array('status' => 403)); } $user = get_user_by('login', $phone); if (is_wp_error($user)) { return $user; } $result = reset_password($user, $params['password']); if (is_wp_error($result)) { return $result; } return array('ok' => 1); }
/** * Hooks into the REST API output to print XML instead of JSON. * * This is only done for the oEmbed API endpoint, * which supports both formats. * * @access private * @since 4.4.0 * * @param bool $served Whether the request has already been served. * @param WP_HTTP_ResponseInterface $result Result to send to the client. Usually a WP_REST_Response. * @param WP_REST_Request $request Request used to generate the response. * @param WP_REST_Server $server Server instance. * @return true */ function _oembed_rest_pre_serve_request($served, $result, $request, $server) { $params = $request->get_params(); if ('/oembed/1.0/embed' !== $request->get_route() || 'GET' !== $request->get_method()) { return $served; } if (!isset($params['format']) || 'xml' !== $params['format']) { return $served; } // Embed links inside the request. $data = $server->response_to_data($result, false); if (404 === $result->get_status()) { $data = $data[0]; } if (!class_exists('SimpleXMLElement')) { status_header(501); die(get_status_header_desc(501)); } $result = _oembed_create_xml($data); // Bail if there's no XML. if (!$result) { status_header(501); return get_status_header_desc(501); } if (!headers_sent()) { $server->send_header('Content-Type', 'text/xml; charset=' . get_option('blog_charset')); } echo $result; return true; }
function new_customer(WP_REST_Request $request) { $data = $request->get_params(); $this->set_api_key(); try { $wp_user_id = wp_insert_user(array('user_login' => $data['username'], 'user_email' => $data['email'], 'user_pass' => $data['pass'])); if (is_wp_error($wp_user_id)) { return new WP_Error('wp-user', __($wp_user_id->get_error_message()), array('status' => 401)); } $customer = \Stripe\Customer::create(array("source" => $this->card_token($data), "email" => $data['email'], "plan" => $data['plan_id'], "metadata" => array('user_id' => $wp_user_id), "shipping" => array("address" => array("line1" => $data['address']['line1'], "line2" => $data['address']['line2'], "city" => $data['address']['city'], "postal_code" => $data['address']['postal_code'], "state" => $data['address']['state']), "name" => $data['name']['first'] . ' ' . $data['name']['last'], "phone" => $data['phone']))); return new WP_REST_Response($customer, 200); } catch (Stripe_AuthenticationError $e) { $body = $e->getJsonBody(); $err = $body['error']; if (!is_wp_error($wp_user_id)) { wp_delete_user($wp_user_id); } return new WP_Error($err['type'], __($err['message']), array('status' => 403)); } catch (Stripe_Error $e) { $body = $e->getJsonBody(); $err = $body['error']; if (!is_wp_error($wp_user_id)) { wp_delete_user($wp_user_id); } return new WP_Error($err['type'], __($err['message']), array('status' => 403)); } catch (\Stripe\Error\Base $e) { $body = $e->getJsonBody(); $err = $body['error']; if (!is_wp_error($wp_user_id)) { wp_delete_user($wp_user_id); } return new WP_Error($err['type'], __($err['message']), array('status' => 403)); } }