public function get_posts($filter = array(), $context = 'view', $type = 'location', $page = 1) { global $wpdb; $params = array('post_type' => 'location', 'post_status' => 'publish', 'orderby' => 'name', 'posts_per_page' => -1); $query = new WP_Query(); $results = $query->query($params); $data = array(); $response = new WP_JSON_Response(); if (!$results) { $response->set_data(array()); return $response; } foreach ($results as $loc) { $gallery_count = (int) $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->posts} AS p LEFT JOIN {$wpdb->postmeta} AS m ON p.ID = m.post_id WHERE p.post_type = 'gallery' AND m.meta_key = 'location_id' AND m.meta_value = '{$loc->ID}'"); $case_study_count = (int) $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->posts} AS p LEFT JOIN {$wpdb->postmeta} AS m ON p.ID = m.post_id WHERE p.post_type = 'case_study' AND m.meta_key = 'location_id' AND m.meta_value = '{$loc->ID}'"); $permalink = get_permalink($loc->ID); $item = get_object_vars($loc); unset($item['post_title']); unset($item['post_content']); $item['title'] = apply_filters('the_title', $loc->post_title); $item['content'] = apply_filters('the_content', $loc->post_content); $item['galleries'] = $gallery_count; $item['case_studies'] = $case_study_count; $item['has_events'] = $gallery_count + $case_study_count > 0; $item['latitude'] = (double) get_post_meta($loc->ID, 'latitude', true); $item['longitude'] = (double) get_post_meta($loc->ID, 'longitude', true); $item['links'] = array('permalink' => $permalink, 'json' => site_url('/wp-json/fwe/locations/' . $loc->ID)); $data[] = $item; } $response->set_data($data); return $response; }
function save_style_guide($data) { $return['data'] = $data; $response = new WP_JSON_Response(); $response->set_data($return); return $response; }
public function create_subscription($data = null) { $settings = get_option('fwe_settings'); if (!fwe_theme_option_exists('mailchimp_api_key')) { return new WP_Error('apikey', 'Please enter a MailChimp API key in Theme Options'); } if (!fwe_theme_option_exists('mailchimp_list_id')) { return new WP_Error('nolist', 'Please select a MailChimp list in Theme Options'); } if (!array_key_exists('name', $data) || empty($data['name'])) { return new WP_Error('noname', 'Please enter your name and try again!'); } if (!array_key_exists('email', $data) || empty($data['email']) || !is_email($data['email'])) { return new WP_Error('noemail', 'Please enter a valid email address and try again!'); } $first_name = fwe_get_given_name($data['name']); $last_name = fwe_get_surname($data['name']); $chimp = new \Drewm\MailChimp($settings['mailchimp_api_key']); $result = $chimp->call('lists/subscribe', array('id' => $settings['mailchimp_list_id'], 'email' => array('email' => $data['email']), 'merge_vars' => array('FNAME' => $first_name, 'LNAME' => $last_name))); if (array_key_exists('status', $result) && $result['status'] === 'error') { return new WP_Error('error', $result['error']); } $resp = new WP_JSON_Response(); $result['status'] = 'success'; $result['message'] = $settings['subscribe_success_message']; $resp->set_data($result); return $resp; }
function get_post_by_slug() { $slug = $_GET['slug']; $return['slug'] = $slug; $return['post'] = get_page_by_path($slug, ARRAY_A, 'post'); $response = new WP_JSON_Response(); $response->set_data($return); return $response; }
/** * Add meta to a post. * * Ensures that the correct location header is sent with the response. * * @param int $id Post ID * @param array $data { * @type string|null $key Meta key * @type string|null $key Meta value * } * @return bool|WP_Error */ public function add_meta($id, $data) { $response = parent::add_meta($id, $data); if (is_wp_error($response)) { return $response; } $data = (object) $response->get_data(); $response = new WP_JSON_Response(); $response->header('Location', json_url('/posts/' . $id . '/meta/' . $data->ID)); $response->set_data($data); $response = json_ensure_response($response); return $response; }
public function get_related($id = '', $filter = array(), $context = 'view') { $option = get_option('sirp_options'); $num = !empty($filter['num']) ? (int) $filter['num'] : (int) $option['display_num']; $ids = sirp_get_related_posts_id_api($num, $id); $posts_list = array(); foreach ($ids as $id) { $posts_list[] = get_post($id['ID']); } $response = new WP_JSON_Response(); if (!$posts_list) { $response->set_data(array()); return $response; } $struct = array(); $response->header('Last-Modified', mysql2date('D, d M Y H:i:s', get_lastpostmodified('GMT'), 0) . ' GMT'); foreach ($posts_list as $post) { $post = get_object_vars($post); if (!$this->check_read_permission($post)) { continue; } $response->link_header('item', json_url('/posts/' . $post['ID']), array('title' => $post['post_title'])); $post_data = $this->prepare_post($post, $context); if (is_wp_error($post_data)) { continue; } $struct[] = $post_data; } $response->set_data($struct); return $response; }
/** * Retrieve ranking * * Overrides the $type to set to 'post', then passes through to the post * endpoints. * * @see WP_JSON_Posts::get_posts() */ public function get_ranking($filter = array(), $context = 'view') { $ids = sga_ranking_get_date($filter); $posts_list = array(); foreach ($ids as $id) { $posts_list[] = get_post($id); } $response = new WP_JSON_Response(); if (!$posts_list) { $response->set_data(array()); return $response; } // holds all the posts data $struct = array(); $response->header('Last-Modified', mysql2date('D, d M Y H:i:s', get_lastpostmodified('GMT'), 0) . ' GMT'); foreach ($posts_list as $post) { $post = get_object_vars($post); // Do we have permission to read this post? if (!$this->check_read_permission($post)) { continue; } $response->link_header('item', json_url('/posts/' . $post['ID']), array('title' => $post['post_title'])); $post_data = $this->prepare_post($post, $context); if (is_wp_error($post_data)) { continue; } $struct[] = $post_data; } $response->set_data($struct); return $response; }
public function create_submission($data = null) { $settings = get_option('fwe_settings'); $form_id = $settings['hire_us_form_id']; $upload_dir = WP_CONTENT_DIR . '/rfp-uploads'; $success_msg = array_key_exists('hire_us_success_message', $settings) ? $settings['hire_us_success_message'] : 'Thank you!'; $mime_type = $data[19]; if ($mime_type && !in_array($mime_type, $this->allowed_mime_types)) { return new WP_Error('That file type is not allowed.'); } $invalid_fields = $this->validate_submission($data); if (!empty($invalid_fields)) { return new WP_Error('The following required fields were invalid: ' . implode(', ', $invalid_fields)); } // Make the uploads folder if it doesn't exist if (!is_dir($upload_dir)) { $mkdir_success = wp_mkdir_p($upload_dir); if (!$mkdir_success) { return new WP_Error('Could not create upload directory.'); } } // Save the RFP file data as a file on the server if (!empty($data['rfp_file_data'])) { $filename = wp_unique_filename($upload_dir, $data[20]); $file_data = $this->get_binary_data($data['rfp_file_data']); $upload_path = trailingslashit($upload_dir) . $filename; $file_url = content_url('/rfp-uploads/' . $filename); if (!file_put_contents($upload_path, $file_data)) { return new WP_Error('Error saving RFP file.'); } // Don't store base64 garbage in the database unset($data['rfp_file_data']); } // Store the form responses in Gravity Forms $data['form_id'] = $form_id; $data['date_created'] = strftime('%Y-%m-%d %H:%M'); $data[21] = $file_url; $entry_id = GFAPI::add_entry($data); // Give the user back a sanitized version of their input for displaying on the Thank You message $response_data = array_merge($data, array('status' => 'OK', 'entry_id' => $entry_id, 'message' => $success_msg)); unset($response_data[21]); $response = new WP_JSON_Response(); $response->set_data($response_data); return $response; }
public function tax_query($data) { $allowed = array('post_type', 'tax_query'); foreach ($data as $key => $value) { if (!in_array($key, $allowed)) { unset($data[$key]); } } if (!is_array($data) || empty($data) || !isset($data['tax_query'])) { return new WP_Error('jp_api_tax_query', __('Invalid tax query.'), array('status' => 500)); } $post_query = new WP_Query(); $posts_list = $post_query->query($data); $response = new WP_JSON_Response(); $response->query_navigation_headers($post_query); if (!$posts_list) { $response->set_data(array()); return $response; } // holds all the posts data $struct = array(); $response->header('Last-Modified', mysql2date('D, d M Y H:i:s', get_lastpostmodified('GMT'), 0) . ' GMT'); foreach ($posts_list as $post) { $post = get_object_vars($post); // Do we have permission to read this post? if (json_check_post_permission($post, 'read')) { continue; } $response->link_header('item', json_url('/posts/' . $post['ID']), array('title' => $post['post_title'])); $post_data = $this->prepare_post($post, 'view'); if (is_wp_error($post_data)) { continue; } $struct[] = $post_data; } $response->set_data($struct); return $response; }
/** * Create a new user. * * @param $data * @return mixed */ public function create_user($data) { $status = true; if (!current_user_can('create_users')) { json_error(BigAppErr::$user['code'], BigAppErr::$user['msg'], "Sorry, you are not allowed to create users.."); } if (!empty($data['ID'])) { json_error(BigAppErr::$user['code'], BigAppErr::$user['msg'], "Cannot create existing user.."); } $user_id = $this->insert_user($data); if (is_wp_error($user_id)) { $status = false; } $response = $this->get_user($user_id); if (!$response instanceof WP_JSON_ResponseInterface) { $response = new WP_JSON_Response($response); } $response->set($status); return $response; }
/** * Edit a milestone. * * @param int $project_id * @param int $milestone_id * @param array $data * * - milestone_name (string, required) * - milestone_detail (string) * - milestone_privacy (string) * - milestone_due (string) * * - Method: POST * - URL: http://example.com/cpm-json/projects/project_id/milestones/milestone_id * * @since 1.2 * @return array $response */ public function edit_milestone($project_id, $milestone_id, $data) { $project_id = intval($project_id); $milestone_id = intval($milestone_id); if (!$project_id) { return new WP_Error('milestone_id', __('Invalid project id', 'cpm')); } if (!$milestone_id) { return new WP_Error('milestone_id', __('Invalid milestoe id', 'cpm')); } if (!isset($data['milestone_name'])) { return new WP_Error('milestone_name', __('Milestone Name Required', 'cpm')); } if (empty($data['milestone_name'])) { return new WP_Error('milestone_name', __('Milestone Name Required', 'cpm')); } $milestone = get_post($milestone_id); if (!cpm_user_can_delete_edit($project_id, $milestone)) { return new WP_Error('permission', __('Sorry! you do not have permission to edit this milestone', 'cpm'), array('status' => 404)); } $milestone_id = cpm()->milestone->update($project_id, $milestone_id); $get_milestone = cpm()->milestone->get($milestone_id); $response = new WP_JSON_Response(); $response->set_data($get_milestone); return $response; }
/** * Get all comments for individual message, todo list, task * * @param int $project_id * @param int $post_id * * @since 1.2 * @return array $response */ public function get_comments($project_id, $post_id) { $premission = $this->check_get_comment_permission($project_id, $post_id); if ($premission !== true) { return new WP_Error($premission['key'], $premission['message'], array('status' => 404)); } remove_filter('comments_clauses', 'cpm_hide_comments', 99); $comments = cpm()->comment->get_comments($post_id); $response = new WP_JSON_Response(); $response->set_data($comments); return $response; }
/** * Edit project * * @param int $project_id * @param array $data * * - project_name string (required) * - project_description string * - project_cat int * - role array * * - URL: http://example.com/cpm-json/projects/project_id * - Method: POST * * @since 1.2 * @return array $response */ public function edit_project($project_id, $data) { $id = intval($project_id); if (!$id) { return new WP_Error('json_post_invalid_id', __('Invalid project ID.'), array('status' => 404)); } $manage_capability = cpm_manage_capability(); if (!$manage_capability && !cpm_project_user_role_pre_chache($id)) { return new WP_Error('permission', __('Sorry! you are not assigned in this project', 'cpm'), array('status' => 404)); } if (!cpm_user_can_access($id)) { return new WP_Error('project_edit_capability', __('You do not have permission to edit this project', 'cpm')); } if (empty($data['project_name'])) { return new WP_Error('project_name', __('Project Name Required', 'cpm')); } $data['project_notify'] = false; $project_id = cpm()->project->update($id, $data); if (!$project_id) { return new WP_Error('project_edit_capability', __('Can not edit project, something worong!', 'cpm')); } $get_project = cpm()->project->get($project_id); $response = new WP_JSON_Response(); $response->set_data($get_project); return $response; }
/** * Retrieve a post. * * @uses get_post() * @param int $id Post ID * @param array $fields Post fields to return (optional) * @return array Post entity */ public function display_post($id, $context = 'view') { $id = (int) $id; if (empty($id)) { $this->set_status(404); return array('message' => __('Invalid Request ID.')); } $post = get_post($id, ARRAY_A); if (empty($post['ID'])) { $this->set_status(404); return array('message' => __('Invalid Request ID.')); } if (!$this->check_read_permission($post)) { //return new WP_Error('json_user_cannot_read', __('Sorry, you cannot read this post.'), array('status' => 401)); } // Link headers (see RFC 5988) $response = new WP_JSON_Response(); $response->header('Last-Modified', mysql2date('D, d M Y H:i:s', $post['post_modified_gmt']) . 'GMT'); $post = $this->prepare_post($post, $context); if (is_wp_error($post)) { return $post; } foreach ($post['meta']['links'] as $rel => $url) { $response->link_header($rel, $url); } $post = $this->format_get_data($post, $id); $response->link_header('alternate', get_permalink($id), array('type' => 'text/html')); $response->set_data($post); return $response; }
/** * Create a new user. * * @param $data * @return mixed */ public function create_user($data) { if (!current_user_can('create_users')) { return new WP_Error('json_cannot_create', __('Sorry, you are not allowed to create users.'), array('status' => 403)); } if (!empty($data['ID'])) { return new WP_Error('json_user_exists', __('Cannot create existing user.'), array('status' => 400)); } $user_id = $this->insert_user($data); if (is_wp_error($user_id)) { return $user_id; } $response = $this->get_user($user_id); if (!$response instanceof WP_JSON_ResponseInterface) { $response = new WP_JSON_Response($response); } $response->set_status(201); $response->header('Location', json_url('/users/' . $user_id)); return $response; }
private function create_response($return) { $response = new WP_JSON_Response(); $response->set_data($return); return $response; }
/** * Retrieve a post. * * @uses get_post() * @param int $id Post ID * @param string $context The context; 'view' (default) or 'edit'. * @return array Post entity */ public function get_post($id, $context = 'view', $is_featured_image = false) { $id = (int) $id; $post = get_post($id, ARRAY_A); show_debug($post, __FILE__, __LINE__); if (empty($id) || empty($post['ID'])) { json_error(BigAppErr::$post['code'], "get post faild!", $id); } $checked_permission = 'read'; $checked_post = $post; if ('inherit' === $post['post_status'] && $post['post_parent'] > 0) { $_temp_post = get_post($post['post_parent'], ARRAY_A); show_debug($_temp_post, __FILE__, __LINE__); if ($post['post_type'] != 'attachment') { //fix for 特色图关联的文章被删的情况 $checked_post = $_temp_post; if ('revision' === $post['post_type']) { $checked_permission = 'edit'; } } } if (!json_check_post_permission($checked_post, $checked_permission)) { json_error(BigAppErr::$post['code'], BigAppErr::$post['msg'], "cant read this post.", $id); } $response = new WP_JSON_Response(); $data = $this->prepare_post($post, $context); if ($data == false) { json_error(BigAppErr::$post['code'], "prepare post faild.", $id); } $favorite_model = new WP_JSON_Favorite($this->server); $data['is_favorited'] = $favorite_model->check_favorited($id); //true/false 是否收藏标示 $post_views = new WP_JSON_PostViews($this->server); $post_views->process_postviews($id); //记录浏览量 if ($is_featured_image == true) { $response->set_data(array($data)); //如果是特征图,返回数据,为了兼容多图模式 } else { $response->set_data($data); //非特征图修改为非数组格式,用于前端缓存及兼容. } return $response; }
public function test_update_user() { $pw_before = $this->user_obj->user_pass; $data = array('first_name' => 'New Name'); $response = $this->endpoint->edit_user($this->user, $data); $this->assertNotInstanceOf('WP_Error', $response); if (!$response instanceof WP_JSON_ResponseInterface) { $response = new WP_JSON_Response($response); } // Check that we succeeded $this->assertEquals(200, $response->get_status()); // Check that the name has been updated correctly $new_data = $response->get_data(); $this->assertEquals($data['first_name'], $new_data['first_name']); $user = get_userdata($this->user); $this->assertEquals($user->first_name, $data['first_name']); // Check that we haven't inadvertently changed the user's password, // as per https://core.trac.wordpress.org/ticket/21429 $this->assertEquals($pw_before, $user->user_pass); }
public function test_update_user_role() { $admin_id = $this->factory->user->create(array('role' => 'administrator')); $user_id = $this->factory->user->create(array('role' => 'author')); wp_set_current_user($admin_id); $admin = wp_get_current_user($admin_id); $this->allow_user_to_create_users($admin); $response = $this->endpoint->edit_user($user_id, array('role' => 'editor')); $this->assertNotInstanceOf('WP_Error', $response); if (!$response instanceof WP_JSON_ResponseInterface) { $response = new WP_JSON_Response($response); } // Check that we succeeded $this->assertEquals(200, $response->get_status()); $user = get_userdata($user_id); $this->assertArrayHasKey('editor', $user->caps); }
/** * 删除之前收藏的帖子 */ public function remove_favorite($post_id) { $status = true; if (empty($post_id)) { json_error(BigAppErr::$favorite['code'], BigAppErr::$favorite['msg'], __lan("post id is empty")); } if ($this->favroite_enabled == true) { wpfp_remove_favorite($post_id); } else { if ($this->_do_remove_favorite($post_id)) { // removed, now? do_action('wpfp_after_remove', $post_id); #if (wpfp_get_option('statistics')) wpfp_update_post_meta($post_id, -1); } } $response = new WP_JSON_Response(); $response->set_data($status); return $response; }
/** * Edit todo list * * @param int $project_id * @param int $list_id * @param array $data * * - tasklist_name (string, required) * - tasklist_detail (string) * - tasklist_privacy (string) * - tasklist_milestone (integer) * * - Method: POST * - URL: http://example.com/cpm-json/projects/project_id/lists/list_id * * @since 1.2 * @return array $response; */ public function edit_list($project_id, $list_id, $data) { $list_id = (int) $list_id; $project_id = (int) $project_id; if (!$project_id) { return new WP_Error('project_id', __('Invalid project id.', 'cpm')); } if (!$list_id) { return new WP_Error('list_id', __('Invalid list id', 'cpm')); } if (empty($data['tasklist_name'])) { return new WP_Error('task_list_name', __('Task List Name Required', 'cpm')); } $post = get_post($list_id); if (empty($post->ID)) { return new WP_Error('json_post_invalid_id', __('Invalid post ID.'), array('status' => 404)); } $post = cpm()->task->get_task_list($list_id); if (cpm_user_can_delete_edit($project_id, $post)) { $list_id = cpm()->task->update_list($project_id, $data, $list_id); $get_list = cpm()->task->get_task_list($list_id); $response = new WP_JSON_Response(); $response->set_data($get_list); return $response; } else { return new WP_Error('create_capability', __('Sorry! permission deny to edit todolist'), array('status' => 404)); } }
public function get_dln_posts($filter = array(), $context = 'view', $type = 'post', $page = 1) { $query = array(); // Validate post types and permissions $query['post_type'] = array(); foreach ((array) $type as $type_name) { $post_type = get_post_type_object($type_name); if (!(bool) $post_type || !$post_type->show_in_json) { return new WP_Error('json_invalid_post_type', sprintf(__('The post type "%s" is not valid'), $type_name), array('status' => 403)); } $query['post_type'][] = $post_type->name; } global $wp; // Allow the same as normal WP $valid_vars = apply_filters('query_vars', $wp->public_query_vars); // Define our own in addition to WP's normal vars $json_valid = array('posts_per_page'); $valid_vars = array_merge($valid_vars, $json_valid); // Filter and flip for querying $valid_vars = apply_filters('json_query_vars', $valid_vars); $valid_vars = array_flip($valid_vars); // Exclude the post_type query var to avoid dodging the permission // check above unset($valid_vars['post_type']); foreach ($valid_vars as $var => $index) { if (isset($filter[$var])) { $query[$var] = apply_filters('json_query_var-' . $var, $filter[$var]); } } // Special parameter handling $query['paged'] = absint($page); $post_query = new WP_Query(); $posts_list = $post_query->query($query); $response = new WP_JSON_Response(); $response->query_navigation_headers($post_query); if (!$posts_list) { $response->set_data(array()); return $response; } // holds all the posts data $struct = array(); $response->header('Last-Modified', mysql2date('D, d M Y H:i:s', get_lastpostmodified('GMT'), 0) . ' GMT'); foreach ($posts_list as $post) { $post = get_object_vars($post); // Do we have permission to read this post? //if ( ! $this->check_read_permission( $post ) ) // continue; $response->link_header('item', json_url('/posts/' . $post['ID']), array('title' => $post['post_title'])); $struct[] = $this->prepare_post($post, $context); } $response->set_data($struct); return $response; }
/** * Get fields. This is an API endpoint. * * @param int $id * @since 6.0 * @return WP_Error|WP_JSON_Response */ public function get_fields($id) { $id = (int) $id; if (empty($id)) { return new WP_Error('json_invalid_id_ccf_form', esc_html__('Invalid form ID.', 'custom-contact-forms'), array('status' => 404)); } $post_type = get_post_type_object('ccf_form'); if (!current_user_can($post_type->cap->edit_posts, $id)) { return new WP_Error('json_cannot_view_ccf_forms', esc_html__('Sorry, you cannot view forms.', 'custom-contact-forms'), array('status' => 403)); } $fields = $this->_get_fields($id); $response = new WP_JSON_Response(); $response->set_status(200); $response->set_data($fields); return $response; }
/** * Edit a message. * * @param int $project_id * @param int $message_id * @param array $data * * - message_title (string, required) * - message_detail (string) * - message_privacy (string) * - milestone (integer) * * - Method: POST * - URL: http://example.com/cpm-json/projects/project_id/messages/message_id * * @since 1.2 * @return array $response */ public function edit_message($project_id, $message_id, $data) { $project_id = intval($project_id); $message_id = intval($message_id); if (!$project_id) { return new WP_Error('json_post_invalid_id', __('Invalid project ID.'), array('status' => 404)); } if (!$message_id) { return new WP_Error('message_id', __('Invalid message id', 'cpm')); } if (empty($data['message_title'])) { return new WP_Error('message_title', __('Message Name Required', 'cpm')); } $message = get_post($message_id); if (!cpm_user_can_delete_edit($project_id, $message)) { return new WP_Error('permission', __('Sorry! you do not have permission to edit this message', 'cpm'), array('status' => 404)); } $message_id = cpm()->message->update($message_id, $data, $message_id); $get_message = cpm()->message->get($message_id); $response = new WP_JSON_Response(); $response->set_data($get_message); return $response; }
/** * Retrieve a post. * * @uses get_post() * @param int $id Post ID * @param string $context The context; 'view' (default) or 'edit'. * @return array Post entity */ public function get_post($id, $context = 'view') { $id = (int) $id; $post = get_post($id, ARRAY_A); if (empty($id) || empty($post['ID'])) { return new WP_Error('json_post_invalid_id', __('Invalid post ID.'), array('status' => 404)); } $checked_permission = 'read'; if ('inherit' === $post['post_status'] && $post['post_parent'] > 0) { $checked_post = get_post($post['post_parent'], ARRAY_A); if ('revision' === $post['post_type']) { $checked_permission = 'edit'; } } else { $checked_post = $post; } if (!json_check_post_permission($checked_post, $checked_permission)) { return new WP_Error('json_user_cannot_read', __('Sorry, you cannot read this post.'), array('status' => 401)); } // Link headers (see RFC 5988) $response = new WP_JSON_Response(); $response->header('Last-Modified', mysql2date('D, d M Y H:i:s', $post['post_modified_gmt']) . 'GMT'); $post = $this->prepare_post($post, $context); if (is_wp_error($post)) { return $post; } // foreach ( $post['meta']['links'] as $rel => $url ) { // $response->link_header( $rel, $url ); // } // $response->link_header( 'alternate', get_permalink( $id ), array( 'type' => 'text/html' ) ); $response->set_data($post); return $response; }
public function new_user() { $data = null; if (isset($_POST['data'])) { $data = json_decode(stripslashes($_POST['data']), ARRAY_N); } if (empty($data)) { return new WP_Error('json_money_invalid_data', __('Invalid data parameters.'), array('status' => 404)); } if (!DLN_Helper_Decrypt::get_decrypt()) { return new WP_Error('json_user_invalid_code', __('Invalid data verify code.'), array('status' => 404)); } //if ( ! current_user_can( 'create_users' ) ) { // return new WP_Error( 'json_cannot_create', __( 'Sorry, you are not allowed to create users.' ), array( 'status' => 403 ) ); //} if (!empty($data['ID'])) { return new WP_Error('json_user_exists', __('Cannot create existing user.'), array('status' => 400)); } $user_id = $this->insert_user($data); if (is_wp_error($user_id)) { return $user_id; } $response = $this->get_user($user_id); if (!$response instanceof WP_JSON_ResponseInterface) { $response = new WP_JSON_Response($response); } $response->set_status(201); $response->header('Location', json_url('/users/' . $user_id)); return $response; }
/** * Handle OPTIONS requests for the server * * This is handled outside of the server code, as it doesn't obey normal route * mapping. * * @param mixed $response Current response, either response or `null` to indicate pass-through * @param WP_JSON_Server $handler ResponseHandler instance (usually WP_JSON_Server) * @return WP_JSON_ResponseHandler Modified response, either response or `null` to indicate pass-through */ function json_handle_options_request($response, $handler) { if (!empty($response) || $handler->method !== 'OPTIONS') { return $response; } $response = new WP_JSON_Response(); $accept = array(); $handler_class = get_class($handler); $class_vars = get_class_vars($handler_class); $map = $class_vars['method_map']; foreach ($handler->get_routes() as $route => $endpoints) { $match = preg_match('@^' . $route . '$@i', $handler->path, $args); if (!$match) { continue; } foreach ($endpoints as $endpoint) { foreach ($map as $type => $bitmask) { if ($endpoint[1] & $bitmask) { $accept[] = $type; } } } break; } $accept = array_unique($accept); $response->header('Accept', implode(', ', $accept)); return $response; }
/** * Edit a task. * * @param int $project_id * @param int $list_id * @param int $task_id * @param array $data * * - task_text (string, required) * - task_privacy (string) * - task_assign (array) * - task_due (date format) * - task_start (date format) * * - Method: POST * - URL: http://example.com/cpm-json/projects/project_id/lists/list_id/tasks/task_id * * @since 1.2 * @return array $response */ public function edit_task($project_id, $list_id, $task_id, $data) { $list_id = (int) $list_id; $task_id = (int) $task_id; $project_id = (int) $project_id; if (!$project_id) { return new WP_Error('json_post_invalid_id', __('Invalid project ID.'), array('status' => 404)); } if (!$list_id) { return new WP_Error('list_id', __('Invalid list id', 'cpm')); } if (!$task_id) { return new WP_Error('task_id', __('Invalid task id', 'cpm')); } if (empty($data['task_text'])) { return new WP_Error('task_text', __('Task Name Required', 'cpm')); } $task = get_post($task_id); if (!cpm_user_can_delete_edit($project_id, $task)) { return new WP_Error('permission', __('Sorry! you do not have permission to edit this task', 'cpm'), array('status' => 404)); } add_filter('cpm_new_task_notification', array($this, 'change_notification_status')); $task_id = cpm()->task->update_task($list_id, $data, $task_id); $get_task = cpm()->task->get_task($task_id); $response = new WP_JSON_Response(); $response->set_data($get_task); return $response; }