/** * get_item function. * * returns data about a BuddyPress site * * @access public * @param mixed $request * @return void */ public function get_item($request) { global $bp; $core = array('version' => $bp->version, 'active_components' => $bp->active_components, 'directory_page_ids' => bp_core_get_directory_page_ids()); $core = apply_filters('core_api_data_filter', $core); $response = new WP_REST_Response(); $response->set_data($core); $response = rest_ensure_response($response); return $response; }
function get_items($request) { $templates = array(); $user_templates = get_posts(array('post_type' => 'fl-builder-template', 'orderby' => 'menu_order title', 'order' => 'ASC', 'posts_per_page' => -1, 'fl-builder-template-type' => 'layout')); //$user_templates = FLBuilderModel::get_user_templates(); if (!empty($user_templates)) { foreach ($user_templates as $post) { $layout = get_post_meta($post->ID, '_fl_builder_data', true); if ($layout) { $categories = get_post_meta($post->ID, 'template_categories', true); if (empty($categories)) { $categories = array('saved'); } $templates[] = array('handle' => $post->ID, 'label' => $post->post_title, 'description' => $post->post_excerpt, 'author' => $post->post_author, 'modified' => $post->post_modified, 'edit_link' => FLBuilderModel::get_edit_url($post->ID), 'categories' => $categories, 'type' => 'user', 'is_global' => get_post_meta($post->ID, '_fl_builder_template_global', true), 'is_editable' => true); } } } $template_data = FLBuilderModel::get_templates(); if (!empty($template_data)) { foreach ($template_data as $data) { $templates[] = array('handle' => $data->index, 'label' => $data->name, 'screenshot' => array('thumbnail' => FL_BUILDER_URL . 'img/templates/' . $data->image), 'author' => array('name' => 'Beaver Builder Team'), 'categories' => array($data->category), 'is_premium' => $data->premium, 'is_builtin' => true, 'type' => 'core'); } } $response = rest_ensure_response($templates); return $response; }
public function get_units($_headers) { global $wpdb; $table_name = $wpdb->prefix . "ot_unit"; $table_member = $wpdb->prefix . "ot_member_unit"; $searchsql = 'SELECT * FROM ' . $table_name . ' order by id'; $results = $wpdb->get_results($searchsql); foreach ($results as $unit) { $sql = 'SELECT id FROM ' . $table_name . ' WHERE parent = ' . $unit->id; $unit_ids = $wpdb->get_results($sql); $ids = array(); foreach ($unit_ids as $p) { array_push($ids, $p->id); } $unit->units = $ids; $sql = 'SELECT id FROM ' . $table_member . ' WHERE unit = ' . $unit->id; $member_ids = $wpdb->get_results($sql); $ids = array(); foreach ($member_ids as $p) { array_push($ids, $p->id); } $unit->memberUnits = $ids; } // return array('units' => $results); $response = rest_ensure_response(array('units' => $results)); // $response->header( 'Content-Type', "application/json" ); return $response; }
/** * Prepare a single product category output for response. * * @param WP_Term $item Term object. * @param WP_REST_Request $request * @return WP_REST_Response $response */ public function prepare_item_for_response($item, $request) { // Get category display type. $display_type = get_woocommerce_term_meta($item->term_id, 'display_type'); // Get category order. $menu_order = get_woocommerce_term_meta($item->term_id, 'order'); $data = array('id' => (int) $item->term_id, 'name' => $item->name, 'slug' => $item->slug, 'parent' => (int) $item->parent, 'description' => $item->description, 'display' => $display_type ? $display_type : 'default', 'image' => array(), 'menu_order' => (int) $menu_order, 'count' => (int) $item->count); // Get category image. if ($image_id = get_woocommerce_term_meta($item->term_id, 'thumbnail_id')) { $attachment = get_post($image_id); $data['image'] = array('id' => (int) $image_id, 'date_created' => wc_rest_prepare_date_response($attachment->post_date_gmt), 'date_modified' => wc_rest_prepare_date_response($attachment->post_modified_gmt), 'src' => wp_get_attachment_url($image_id), 'title' => get_the_title($attachment), 'alt' => get_post_meta($image_id, '_wp_attachment_image_alt', true)); } $context = !empty($request['context']) ? $request['context'] : 'view'; $data = $this->add_additional_fields_to_object($data, $request); $data = $this->filter_response_by_context($data, $context); $response = rest_ensure_response($data); $response->add_links($this->prepare_links($item, $request)); /** * Filter a term item returned from the API. * * Allows modification of the term data right before it is returned. * * @param WP_REST_Response $response The response object. * @param object $item The original term object. * @param WP_REST_Request $request Request used to generate the response. */ return apply_filters("woocommerce_rest_prepare_{$this->taxonomy}", $response, $item, $request); }
/** * Prepare a single user output for response * * @param object $user User object. * @param WP_REST_Request $request Request object. * @return WP_REST_Response Response data. */ public function prepare_item_for_response($user, $request) { $roles = $user->roles; if (empty($roles)) { $isadmin = false; } else { $isadmin = hash_equals($roles[0], 'administrator'); } $user_id = $user->ID; $user_blogs = get_blogs_of_user($user_id); $site = urldecode($request['site']); $data = array('id' => $user->ID, 'username' => $user->user_login, 'name' => $user->display_name, 'email' => $user->user_email, 'admin' => $isadmin, 'role' => $roles[0], 'site' => $_SERVER['SERVER_NAME'], 'host' => $_SERVER['HTTP_HOST'], 'blogs' => $user_blogs); $context = !empty($request['context']) ? $request['context'] : 'embed'; $data = $this->filter_response_by_context($data, $context); $data = $this->add_additional_fields_to_object($data, $request); // Wrap the data in a response object $response = rest_ensure_response($data); //$response->add_links( $this->prepare_links( $user ) ); /** * Filter user data returned from the REST API. * * @param WP_REST_Response $response The response object. * @param object $user User object used to create response. * @param WP_REST_Request $request Request object. */ return apply_filters('rest_prepare_user', $response, $user, $request); }
public function delete_items(WP_REST_Request $request) { // TODO refactor $args = array('older_than_seconds' => $request['older-than-seconds']); $db = new WP_REST_API_Log_DB(); return rest_ensure_response(new WP_REST_API_Log_Delete_Response($db->delete($args))); }
/** * Prepare a post status object for serialization * * @param stdClass $status Post status data * @param WP_REST_Request $request * @return WP_REST_Response Post status data */ public function prepare_item_for_response($status, $request) { if (false === $status->public && !is_user_logged_in() || true === $status->internal && is_user_logged_in()) { return new WP_Error('rest_cannot_read_status', __('Cannot view status.'), array('status' => rest_authorization_required_code())); } $data = array('name' => $status->label, 'private' => (bool) $status->private, 'protected' => (bool) $status->protected, 'public' => (bool) $status->public, 'queryable' => (bool) $status->publicly_queryable, 'show_in_list' => (bool) $status->show_in_admin_all_list, 'slug' => $status->name); $context = !empty($request['context']) ? $request['context'] : 'view'; $data = $this->add_additional_fields_to_object($data, $request); $data = $this->filter_response_by_context($data, $context); $response = rest_ensure_response($data); $posts_controller = new WP_REST_Posts_Controller('post'); if ('publish' === $status->name) { $response->add_link('archives', rest_url('/wp/v2/' . $posts_controller->get_post_type_base('post'))); } else { $response->add_link('archives', add_query_arg('status', $status->name, rest_url('/wp/v2/' . $posts_controller->get_post_type_base('post')))); } /** * Filter a status returned from the API. * * Allows modification of the status data right before it is returned. * * @param WP_REST_Response $response The response object. * @param object $status The original status object. * @param WP_REST_Request $request Request used to generate the response. */ return apply_filters('rest_prepare_status', $response, $status, $request); }
/** * Create a single message * * @param WP_REST_Request $request Full details about the request. * @return WP_Error|WP_REST_Response */ public function create_item($request) { if (!empty($request['id'])) { return new WP_Error('bp_json_message_exists', __('Cannot create existing message.', BP_API_PLUGIN_SLUG), array('status' => 400)); } $message = $this->prepare_item_for_database($request); $message_id = messages_new_message(array('sender_id' => $message->sender_id ? $message->sender_id : bp_loggedin_user_id(), 'thread_id' => $message->thread_id, 'recipients' => $message->recipients, 'subject' => $message->subject, 'content' => $message->content, 'date_sent' => $message->date_sent ? $message->date_sent : bp_core_current_time())); if (!$message_id) { return new WP_Error('bp_json_message_create', __('Error creating new message.', BP_API_PLUGIN_SLUG), array('status' => 500)); } $this->update_additional_fields_for_object($message, $request); /** * Fires after a message is created via the REST API * * @param object $message Data used to create message * @param WP_REST_Request $request Request object. * @param bool $bool A boolean that is false. */ do_action('bp_json_insert_message', $message, $request, false); $response = $this->get_item(array('id' => $message_id, 'context' => 'view')); $response = rest_ensure_response($response); $response->set_status(201); $response->header('Location', rest_url('/users/' . $message_id)); return $response; }
public function get_item_props($request) { global $wpdb; $table_item_prop = $wpdb->prefix . "ot_item_prop"; $searchsql = 'SELECT * FROM ' . $table_item_prop . ' order by id'; $results = $wpdb->get_results($searchsql); return rest_ensure_response(array('item_props' => $results)); }
/** * Call dispatch() with the rest_post_dispatch filter */ public function dispatch($request) { $result = parent::dispatch($request); $result = rest_ensure_response($result); if (is_wp_error($result)) { $result = $this->error_to_response($result); } return apply_filters('rest_post_dispatch', rest_ensure_response($result), $this, $request); }
protected function check_get_plugins_response($response, $context = 'view') { $this->assertNotInstanceOf('WP_Error', $response); $response = rest_ensure_response($response); $this->assertEquals(200, $response->get_status()); $theme_data = $response->get_data(); $plugin = array(); // fixme - get theme object $this->check_plugin_data($plugin); }
/** * Get a collection of items * * @param \WP_REST_Request $request Full data about the request. * @return \WP_Error|\WP_REST_Response */ public function get_item($request) { $templateSlug = $request['templateSlug']; ob_start(); get_template_part($templateSlug); $data = ob_get_contents(); ob_end_clean(); $response = rest_ensure_response($data); return $response; }
public function get_props($request) { global $wpdb; $table_prop = $wpdb->prefix . "ot_prop"; $searchsql = 'SELECT * FROM ' . $table_prop . ' order by id'; $results = $wpdb->get_results($searchsql); foreach ($results as $prop) { $prop->item_props = $this->get_itemprops_for_prop($prop->id); } return rest_ensure_response(array('props' => $results)); }
public function get_item($request) { $slug = $request['slug']; $plugin = $this->get_plugin($slug); if (null === $plugin) { return new WP_Error('rest_plugin_invalid_slug', sprintf(__('Plugin with slug %s not found.'), $slug), array('status' => 404)); } $data = $this->prepare_item_for_response($plugin, $request); $response = rest_ensure_response($data); return $response; }
/** * 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)); }
/** * Execute a specific event */ public function run_event($request) { // Parse request for details needed to identify the event to execute // `$timestamp` is, unsurprisingly, the Unix timestamp the event is scheduled for // `$action` is the md5 hash of the action used when the event is registered // `$instance` is the md5 hash of the event's arguments array, which Core uses to index the `cron` option $event = $request->get_json_params(); $timestamp = isset($event['timestamp']) ? absint($event['timestamp']) : null; $action = isset($event['action']) ? trim(sanitize_text_field($event['action'])) : null; $instance = isset($event['instance']) ? trim(sanitize_text_field($event['instance'])) : null; return rest_ensure_response(Events::instance()->run_event($timestamp, $action, $instance)); }
/** * Get a collection of site settings. * * @param WP_REST_Request $request Full details about the request. * @return WP_Error|WP_REST_Response */ public function get_items($request) { $options = $this->get_endpoint_args_for_item_schema(WP_REST_Server::READABLE); $response = array(); foreach ($options as $name => $args) { if (!$this->get_item_mapping($name)) { continue; } $response[$name] = $this->prepare_item_for_response($name, $request); } return rest_ensure_response($response); }
/** * 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 plugins we can use for price tests * * @since 0.2.0 * * @param \WP_REST_Request $request Full data about the request. * @return \WP_Error|\WP_REST_Response */ public function get_plugins($request) { $allowed = ingot_accepted_plugins_for_price_tests(true); if (!empty($allowed)) { foreach ($allowed as $value => $label) { if (ingot_check_ecommerce_active($value)) { $plugins[] = array('value' => $value, 'label' => $label); } } return rest_ensure_response($plugins); } else { return rest_ensure_response('', 404); } }
/** * Get all products from an ecommerce plugin * * @since 0.2.0 * * @param \WP_REST_Request $request Full data about the request. * @return \WP_Error|\WP_REST_Response */ public function get_items($request) { $plugin = $request->get_param('plugin'); if (!in_array($plugin, ingot_accepted_plugins_for_price_tests())) { return new \WP_Error('ingot-invalid-plugin'); } if ('woo' == $plugin) { $products = $this->get_all_woo(); } elseif ('edd' == $plugin) { $products = $this->get_all_edd(); } else { $products = array(); } return rest_ensure_response($products); }
/** * Handles serving an API request. * * Matches the current server URI to a route and runs the first matching * callback then outputs a JSON representation of the returned value. * * @since 4.4.0 * @access public * * @see WP_REST_Server::dispatch() * * @param string $path Optional. The request route. If not set, `$_SERVER['PATH_INFO']` will be used. * Default null. * @return false|null Null if not served and a HEAD request, false otherwise. */ public function serve_request($path = null) { $this->sendInitialHeaders(); if (!Manager::instance()->isEnabled()) { echo $this->json_error('rest_disabled', __('The REST API is disabled on this site.'), 404); return false; } if (false === ($jsonpCallback = $this->discoverJsonpCallback())) { // Error message was sent return false; } if (empty($path)) { if (isset($_SERVER['PATH_INFO'])) { $path = $_SERVER['PATH_INFO']; } else { $path = '/'; } } $request = $this->createRequest($path); $response = $this->check_authentication(); if (!is_wp_error($response)) { $response = $this->dispatch($request); } // Normalize to either WP_Error or WP_REST_Response... $response = rest_ensure_response($response); // ...then convert WP_Error across. if (is_wp_error($response)) { $response = $this->error_to_response($response); } /** * Filter the API response. * * Allows modification of the response before returning. * * @since 4.4.0 * @since 4.5.0 Applied to embedded responses. * * @param WP_HTTP_Response $result Result to send to the client. Usually a WP_REST_Response. * @param WP_REST_Server $this Server instance. * @param WP_REST_Request $request Request used to generate the response. */ $response = apply_filters('rest_post_dispatch', rest_ensure_response($response), $this, $request); // Wrap the response in an envelope if asked for. if (isset($_GET['_envelope'])) { $response = $this->envelope_response($response, isset($_GET['_embed'])); } return $this->sendResponse($request, $response, $jsonpCallback); }
/** * Get a system status info, by section. * * @param WP_REST_Request $request Full details about the request. * @return WP_Error|WP_REST_Response */ public function get_items($request) { $schema = $this->get_item_schema(); $mappings = $this->get_item_mappings(); $response = array(); foreach ($mappings as $section => $values) { settype($values, $schema['properties'][$section]['type']); foreach ($values as $key => $value) { if (isset($schema['properties'][$section]['properties'][$key]['type'])) { settype($values[$key], $schema['properties'][$section]['properties'][$key]['type']); } } $response[$section] = $values; } return rest_ensure_response($response); }
/** * Prepare a single user output for response * * @param object $user User object. * @param WP_REST_Request $request Request object. * @return WP_REST_Response Response data. */ public function prepare_item_for_response($media, $request) { $data = array('id' => $media->ID, 'caption' => $media->post_excerpt, 'media_type' => $media->post_mime_type, 'source_url' => wp_get_attachment_url($media->ID)); $context = !empty($request['context']) ? $request['context'] : 'embed'; $data = $this->filter_response_by_context($data, $context); $data = $this->add_additional_fields_to_object($data, $request); // Wrap the data in a response object $response = rest_ensure_response($data); //$response->add_links( $this->prepare_links( $user ) ); /** * Filter user data returned from the REST API. * * @param WP_REST_Response $response The response object. * @param object $user User object used to create response. * @param WP_REST_Request $request Request object. */ return apply_filters('rest_prepare_media', $response, $media, $request); }
/** * Prepare a post status object for serialization * * @param stdClass $status Post status data * @param WP_REST_Request $request * @return WP_REST_Response Post status data */ public function prepare_item_for_response($status, $request) { if (false === $status->public && !is_user_logged_in() || true === $status->internal && is_user_logged_in()) { return new WP_Error('rest_cannot_read_status', __('Cannot view status.'), array('status' => 403)); } $data = array('name' => $status->label, 'private' => (bool) $status->private, 'protected' => (bool) $status->protected, 'public' => (bool) $status->public, 'queryable' => (bool) $status->publicly_queryable, 'show_in_list' => (bool) $status->show_in_admin_all_list, 'slug' => $status->name); $context = !empty($request['context']) ? $request['context'] : 'view'; $data = $this->filter_response_by_context($data, $context); $data = $this->add_additional_fields_to_object($data, $request); $data = rest_ensure_response($data); $posts_controller = new WP_REST_Posts_Controller('post'); if ('publish' === $status->name) { $data->add_link('archives', rest_url('/wp/v2/' . $posts_controller->get_post_type_base('post'))); } else { $data->add_link('archives', add_query_arg('status', $status->name, rest_url('/wp/v2/' . $posts_controller->get_post_type_base('post')))); } return $data; }
public function get_item($request) { $slug = $request['slug']; $theme = null; $themes = wp_get_themes(); foreach ($themes as $key => $obj) { if ($slug === $key) { $theme = $obj; $theme->slug = $key; break; } } if (!$theme) { return new WP_Error('rest_post_invalid_id', __('Invalid theme slug.'), array('status' => 404)); } $data = $this->prepare_item_for_response($theme, $request); return rest_ensure_response($data); }
public function create_item($request) { $params = $request->get_params(); $item_id = $params['item_id']; $raw_value = $params['value']; $value = null; if ('up' === $raw_value) { $value = 1; } elseif ('down' === $raw_value) { $value = -1; } $item = null; switch ($params['item_type']) { case 'question': $item = new \WeBWorK\Server\Question($item_id); break; case 'response': $item = new \WeBWorK\Server\Response($item_id); break; } $vote = new \WeBWorK\Server\Vote(); $vote->set_user_id(get_current_user_id()); $vote->set_item($item); $vote->populate(); // Don't allow duplicate votes. // This is not really RESTful. On a successful lookup, perform an update. $retval = false; if ($vote->exists() && $value === $vote->get_value()) { // do something } elseif ($value) { $vote->set_value($value); $retval = $vote->save(); } elseif ($vote->exists()) { $retval = $vote->delete(); } $response = rest_ensure_response($retval); if ($retval) { $response->set_status(201); } else { // We return 200 anyway. Not sure how to give good error feedback here. $response->set_status(200); } return $response; }
/** * Prepare a single product shipping class output for response. * * @param obj $item Term object. * @param WP_REST_Request $request * @return WP_REST_Response $response */ public function prepare_item_for_response($item, $request) { $data = array('id' => (int) $item->term_id, 'name' => $item->name, 'slug' => $item->slug, 'description' => $item->description, 'count' => (int) $item->count); $context = !empty($request['context']) ? $request['context'] : 'view'; $data = $this->add_additional_fields_to_object($data, $request); $data = $this->filter_response_by_context($data, $context); $response = rest_ensure_response($data); $response->add_links($this->prepare_links($item, $request)); /** * Filter a term item returned from the API. * * Allows modification of the term data right before it is returned. * * @param WP_REST_Response $response The response object. * @param object $item The original term object. * @param WP_REST_Request $request Request used to generate the response. */ return apply_filters("woocommerce_rest_prepare_{$this->taxonomy}", $response, $item, $request); }
/** * Callback for our API endpoint. * * Returns the JSON object for the post. * * @param WP_REST_Request $request Full details about the request. * * @return WP_Error|WP_REST_Response */ public function get_item(WP_REST_Request $request) { $post_id = url_to_postid($request['url']); /** * Filter the determined post id. * * @param int $post_id The post ID. * @param string $url The requestd URL. */ $post_id = apply_filters('oembed_request_post_id', $post_id, $request['url']); if (0 === $post_id) { return new WP_Error('oembed_invalid_url', __('Invalid URL.', 'oembed-api'), array('status' => 404)); } // Todo: Perhaps just default to json if something invalid is provided. if (!in_array($request['format'], array('json', 'xml'))) { return new WP_Error('oembed_invalid_format', __('Invalid format.', 'oembed-api'), array('status' => 501)); } return rest_ensure_response(get_oembed_response_data($post_id, $request['maxwidth'])); }
/** * Prepare a report object for serialization. * * @param stdClass $report Report data. * @param WP_REST_Request $request Request object. * @return WP_REST_Response $response Response data. */ public function prepare_item_for_response($report, $request) { $data = array('slug' => $report->slug, 'description' => $report->description); $context = !empty($request['context']) ? $request['context'] : 'view'; $data = $this->add_additional_fields_to_object($data, $request); $data = $this->filter_response_by_context($data, $context); // Wrap the data in a response object. $response = rest_ensure_response($data); $response->add_links(array('self' => array('href' => rest_url(sprintf('/%s/%s/%s', $this->namespace, $this->rest_base, $report->slug))), 'collection' => array('href' => rest_url(sprintf('%s/%s', $this->namespace, $this->rest_base))))); /** * Filter a report returned from the API. * * Allows modification of the report data right before it is returned. * * @param WP_REST_Response $response The response object. * @param object $report The original report object. * @param WP_REST_Request $request Request used to generate the response. */ return apply_filters('woocommerce_rest_prepare_report', $response, $report, $request); }
/** * Prepare a report sales object for serialization. * * @param stdClass $top_seller * @param WP_REST_Request $request Request object. * @return WP_REST_Response $response Response data. */ public function prepare_item_for_response($top_seller, $request) { $data = array('name' => $top_seller->name, 'product_id' => $top_seller->product_id, 'quantity' => $top_seller->quantity); $context = !empty($request['context']) ? $request['context'] : 'view'; $data = $this->add_additional_fields_to_object($data, $request); $data = $this->filter_response_by_context($data, $context); // Wrap the data in a response object. $response = rest_ensure_response($data); $response->add_links(array('about' => array('href' => rest_url(sprintf('%s/reports', $this->namespace))), 'product' => array('href' => rest_url(sprintf('/%s/products/%s', $this->namespace, $top_seller->product_id))))); /** * Filter a report top sellers returned from the API. * * Allows modification of the report top sellers data right before it is returned. * * @param WP_REST_Response $response The response object. * @param stdClass $top_seller The original report object. * @param WP_REST_Request $request Request used to generate the response. */ return apply_filters('woocommerce_rest_prepare_report_top_sellers', $response, $top_seller, $request); }