/** * 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; }
/** * get_activity function. * * @access public * @param mixed $filter * @return void */ public function get_activity($filter) { $args = $filter; if (bp_has_activities($args)) { while (bp_activities()) { bp_the_activity(); $activity = array('avatar' => bp_core_fetch_avatar(array('html' => false, 'item_id' => bp_get_activity_id())), 'action' => bp_get_activity_action(), 'content' => bp_get_activity_content_body(), 'activity_id' => bp_get_activity_id(), 'activity_username' => bp_core_get_username(bp_get_activity_user_id()), 'user_id' => bp_get_activity_user_id(), 'comment_count' => bp_activity_get_comment_count(), 'can_comment' => bp_activity_can_comment(), 'can_favorite' => bp_activity_can_favorite(), 'is_favorite' => bp_get_activity_is_favorite(), 'can_delete' => bp_activity_user_can_delete()); $activity = apply_filters('bp_json_prepare_activity', $activity); $activities[] = $activity; } $data = array('activity' => $activities, 'has_more_items' => bp_activity_has_more_items()); $data = apply_filters('bp_json_prepare_activities', $data); } else { return new WP_Error('bp_json_activity', __('No Activity Found.', 'buddypress'), array('status' => 200)); } $response = new WP_REST_Response(); $response->set_data($data); $response = rest_ensure_response($response); return $response; }
/** * This method use for handler request from Github's webhook. * * Because of github not required to response payload on response, so we can make a do_action instead of apply_filters * * @param \WP_REST_Request $request * * @return \WP_REST_Response */ public function onGithubRequest(\WP_REST_Request $request) { // Create the response object $request_payload = json_decode($request->get_body()); $response = new \WP_REST_Response(); if ($request_payload) { ob_start(); $repo = new Repository($request_payload->repository->git_url); if ($repo->exists()) { $event = $request->get_header('X-Github-Event'); do_action('wppm_webhook_recived_' . $event, $repo, $request_payload); } else { $response->set_status(404); echo "Repo is not exist."; } $out_put = ob_get_clean(); $response->set_data($out_put); } else { $response->set_status(500); } return $response; }
/** * Handles OPTIONS requests for the server. * * This is handled outside of the server code, as it doesn't obey normal route * mapping. * * @since 4.4.0 * * @param mixed $response Current response, either response or `null` to indicate pass-through. * @param WP_REST_Server $handler ResponseHandler instance (usually WP_REST_Server). * @param WP_REST_Request $request The request that was used to make current response. * @return WP_REST_Response Modified response, either response or `null` to indicate pass-through. */ function rest_handle_options_request($response, $handler, $request) { if (!empty($response) || $request->get_method() !== 'OPTIONS') { return $response; } $response = new WP_REST_Response(); $data = array(); foreach ($handler->get_routes() as $route => $endpoints) { $match = preg_match('@^' . $route . '$@i', $request->get_route()); if (!$match) { continue; } $data = $handler->get_data_for_route($route, $endpoints, 'help'); $response->set_matched_route($route); break; } $response->set_data($data); return $response; }
/** * Handles OPTIONS requests for the server. * * This is handled outside of the server code, as it doesn't obey normal route * mapping. * * @since 4.4.0 * * @param mixed $response Current response, either response or `null` to indicate pass-through. * @param WP_REST_Server $handler ResponseHandler instance (usually WP_REST_Server). * @param WP_REST_Request $request The request that was used to make current response. * @return WP_REST_Response Modified response, either response or `null` to indicate pass-through. */ function rest_handle_options_request($response, $handler, $request) { if (!empty($response) || $request->get_method() !== 'OPTIONS') { return $response; } $response = new WP_REST_Response(); $data = array(); $accept = array(); foreach ($handler->get_routes() as $route => $endpoints) { $match = preg_match('@^' . $route . '$@i', $request->get_route(), $args); if (!$match) { continue; } $data = $handler->get_data_for_route($route, $endpoints, 'help'); $accept = array_merge($accept, $data['methods']); break; } $response->header('Accept', implode(', ', $accept)); $response->set_data($data); return $response; }
/** * Deletes a single user. * * @since 4.7.0 * @access public * * @param WP_REST_Request $request Full details about the request. * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. */ public function delete_item($request) { $id = (int) $request['id']; $reassign = isset($request['reassign']) ? absint($request['reassign']) : null; $force = isset($request['force']) ? (bool) $request['force'] : false; // We don't support trashing for this type, error out. if (!$force) { return new WP_Error('rest_trash_not_supported', __('Users do not support trashing. Set force=true to delete.'), array('status' => 501)); } $user = get_userdata($id); if (!$user) { return new WP_Error('rest_user_invalid_id', __('Invalid resource id.'), array('status' => 404)); } if (!empty($reassign)) { if ($reassign === $id || !get_userdata($reassign)) { return new WP_Error('rest_user_invalid_reassign', __('Invalid resource id for reassignment.'), array('status' => 400)); } } $request->set_param('context', 'edit'); $previous = $this->prepare_item_for_response($user, $request); /** Include admin user functions to get access to wp_delete_user() */ require_once ABSPATH . 'wp-admin/includes/user.php'; $result = wp_delete_user($id, $reassign); if (!$result) { return new WP_Error('rest_cannot_delete', __('The resource cannot be deleted.'), array('status' => 500)); } $response = new WP_REST_Response(); $response->set_data(array('deleted' => true, 'previous' => $previous->get_data())); /** * Fires immediately after a user is deleted via the REST API. * * @since 4.7.0 * * @param WP_User $user The user data. * @param WP_REST_Response $response The response returned from the API. * @param WP_REST_Request $request The request sent to the API. */ do_action('rest_delete_user', $user, $response, $request); return $response; }
/** * Deletes a single post. * * @since 4.7.0 * @access public * * @param WP_REST_Request $request Full details about the request. * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. */ public function delete_item($request) { $id = (int) $request['id']; $force = (bool) $request['force']; $post = get_post($id); if (empty($id) || empty($post->ID) || $this->post_type !== $post->post_type) { return new WP_Error('rest_post_invalid_id', __('Invalid post ID.'), array('status' => 404)); } $supports_trash = EMPTY_TRASH_DAYS > 0; if ('attachment' === $post->post_type) { $supports_trash = $supports_trash && MEDIA_TRASH; } /** * Filters whether a post is trashable. * * The dynamic portion of the hook name, `$this->post_type`, refers to the post type slug. * * Pass false to disable trash support for the post. * * @since 4.7.0 * * @param bool $supports_trash Whether the post type support trashing. * @param WP_Post $post The Post object being considered for trashing support. */ $supports_trash = apply_filters("rest_{$this->post_type}_trashable", $supports_trash, $post); if (!$this->check_delete_permission($post)) { return new WP_Error('rest_user_cannot_delete_post', __('Sorry, you are not allowed to delete this post.'), array('status' => rest_authorization_required_code())); } $request->set_param('context', 'edit'); // If we're forcing, then delete permanently. if ($force) { $previous = $this->prepare_item_for_response($post, $request); $result = wp_delete_post($id, true); $response = new WP_REST_Response(); $response->set_data(array('deleted' => true, 'previous' => $previous->get_data())); } else { // If we don't support trashing for this type, error out. if (!$supports_trash) { return new WP_Error('rest_trash_not_supported', __('The post does not support trashing. Set force=true to delete.'), array('status' => 501)); } // Otherwise, only trash if we haven't already. if ('trash' === $post->post_status) { return new WP_Error('rest_already_trashed', __('The post has already been deleted.'), array('status' => 410)); } // (Note that internally this falls through to `wp_delete_post` if // the trash is disabled.) $result = wp_trash_post($id); $post = get_post($id); $response = $this->prepare_item_for_response($post, $request); } if (!$result) { return new WP_Error('rest_cannot_delete', __('The post cannot be deleted.'), array('status' => 500)); } /** * Fires immediately after a single post is deleted or trashed via the REST API. * * They dynamic portion of the hook name, `$this->post_type`, refers to the post type slug. * * @since 4.7.0 * * @param object $post The deleted or trashed post. * @param WP_REST_Response $response The response data. * @param WP_REST_Request $request The request sent to the API. */ do_action("rest_delete_{$this->post_type}", $post, $response, $request); return $response; }
/** * Adds EE metadata to the index * @param \WP_REST_Response $rest_response_obj * @return \WP_REST_Response */ public static function filter_ee_metadata_into_index(\WP_REST_Response $rest_response_obj) { $response_data = $rest_response_obj->get_data(); $addons = array(); foreach (\EE_Registry::instance()->addons as $addon) { $addon_json = array('name' => $addon->name(), 'version' => $addon->version()); $addons[$addon_json['name']] = $addon_json; } $response_data['ee'] = array('version' => \EEM_System_Status::instance()->get_ee_version(), 'addons' => $addons, 'maintenance_mode' => \EE_Maintenance_Mode::instance()->real_level(), 'served_core_versions' => array_keys(\EED_Core_Rest_Api::versions_served())); $rest_response_obj->set_data($response_data); return $rest_response_obj; }
/** * Deletes a single revision. * * @since 4.7.0 * @access public * * @param WP_REST_Request $request Full details about the request. * @return true|WP_Error True on success, or WP_Error object on failure. */ public function delete_item($request) { $force = isset($request['force']) ? (bool) $request['force'] : false; // We don't support trashing for revisions. if (!$force) { return new WP_Error('rest_trash_not_supported', __('Revisions do not support trashing. Set force=true to delete.'), array('status' => 501)); } $revision = get_post($request['id']); $previous = $this->prepare_item_for_response($revision, $request); $result = wp_delete_post($request['id'], true); /** * Fires after a revision is deleted via the REST API. * * @since 4.7.0 * * @param (mixed) $result The revision object (if it was deleted or moved to the trash successfully) * or false (failure). If the revision was moved to to the trash, $result represents * its new state; if it was deleted, $result represents its state before deletion. * @param WP_REST_Request $request The request sent to the API. */ do_action('rest_delete_revision', $result, $request); if (!$result) { return new WP_Error('rest_cannot_delete', __('The post cannot be deleted.'), array('status' => 500)); } $response = new WP_REST_Response(); $response->set_data(array('deleted' => true, 'previous' => $previous->get_data())); return $response; }
/** * Deletes a single term from a taxonomy. * * @since 4.7.0 * @access public * * @param WP_REST_Request $request Full details about the request. * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. */ public function delete_item($request) { $force = isset($request['force']) ? (bool) $request['force'] : false; // We don't support trashing for terms. if (!$force) { return new WP_Error('rest_trash_not_supported', __('Terms do not support trashing. Set force=true to delete.'), array('status' => 501)); } $term = get_term((int) $request['id'], $this->taxonomy); $request->set_param('context', 'view'); $previous = $this->prepare_item_for_response($term, $request); $retval = wp_delete_term($term->term_id, $term->taxonomy); if (!$retval) { return new WP_Error('rest_cannot_delete', __('The term cannot be deleted.'), array('status' => 500)); } $response = new WP_REST_Response(); $response->set_data(array('deleted' => true, 'previous' => $previous->get_data())); /** * Fires after a single term is deleted via the REST API. * * The dynamic portion of the hook name, `$this->taxonomy`, refers to the taxonomy slug. * * @since 4.7.0 * * @param WP_Term $term The deleted term. * @param WP_REST_Response $response The response data. * @param WP_REST_Request $request The request sent to the API. */ do_action("rest_delete_{$this->taxonomy}", $term, $response, $request); return $response; }
/** * Sends a response to the API request. * * @since 1.4 * @param int $status_code Status code. * @return void */ public function output($status_code = 200) { $response = new WP_REST_Response(array('result' => true)); $response->set_status($status_code); $response->header('Content-type', 'application/json'); $response->set_data($this->data); echo wp_json_encode($response); die; }
/** * Deletes a comment. * * @since 4.7.0 * @access public * * @param WP_REST_Request $request Full details about the request. * @return WP_Error|WP_REST_Response Response object on success, or error object on failure. */ public function delete_item($request) { $id = (int) $request['id']; $force = isset($request['force']) ? (bool) $request['force'] : false; $comment = get_comment($id); if (empty($comment)) { return new WP_Error('rest_comment_invalid_id', __('Invalid comment ID.'), array('status' => 404)); } /** * Filters whether a comment can be trashed. * * Return false to disable trash support for the post. * * @since 4.7.0 * * @param bool $supports_trash Whether the post type support trashing. * @param WP_Post $comment The comment object being considered for trashing support. */ $supports_trash = apply_filters('rest_comment_trashable', EMPTY_TRASH_DAYS > 0, $comment); $request->set_param('context', 'edit'); if ($force) { $previous = $this->prepare_item_for_response($comment, $request); $result = wp_delete_comment($comment->comment_ID, true); $response = new WP_REST_Response(); $response->set_data(array('deleted' => true, 'previous' => $previous->get_data())); } else { // If this type doesn't support trashing, error out. if (!$supports_trash) { return new WP_Error('rest_trash_not_supported', __('The comment does not support trashing. Set force=true to delete.'), array('status' => 501)); } if ('trash' === $comment->comment_approved) { return new WP_Error('rest_already_trashed', __('The comment has already been trashed.'), array('status' => 410)); } $result = wp_trash_comment($comment->comment_ID); $comment = get_comment($comment->comment_ID); $response = $this->prepare_item_for_response($comment, $request); } if (!$result) { return new WP_Error('rest_cannot_delete', __('The comment cannot be deleted.'), array('status' => 500)); } /** * Fires after a comment is deleted via the REST API. * * @since 4.7.0 * * @param WP_Comment $comment The deleted comment data. * @param WP_REST_Response $response The response returned from the API. * @param WP_REST_Request $request The request sent to the API. */ do_action('rest_delete_comment', $comment, $response, $request); return $response; }