protected function check_post_data($post, $data, $context) { $post_type_obj = get_post_type_object($post->post_type); // Standard fields $this->assertEquals($post->ID, $data['id']); $this->assertEquals($post->post_name, $data['slug']); $this->assertEquals(get_permalink($post->ID), $data['link']); if ('0000-00-00 00:00:00' === $post->post_date_gmt) { $this->assertNull($data['date']); } else { $this->assertEquals(rest_mysql_to_rfc3339($post->post_date), $data['date']); } if ('0000-00-00 00:00:00' === $post->post_modified_gmt) { $this->assertNull($data['modified']); } else { $this->assertEquals(rest_mysql_to_rfc3339($post->post_modified), $data['modified']); } // author if (post_type_supports($post->post_type, 'author')) { $this->assertEquals($post->post_author, $data['author']); } else { $this->assertEmpty($data['author']); } // post_parent if ($post_type_obj->hierarchical) { $this->assertArrayHasKey('parent', $data); if ($post->post_parent) { if (is_int($data['parent'])) { $this->assertEquals($post->post_parent, $data['parent']); } else { $this->assertEquals($post->post_parent, $data['parent']['id']); $this->check_get_post_response($data['parent'], get_post($data['parent']['id']), 'view-parent'); } } else { $this->assertEmpty($data['parent']); } } else { $this->assertFalse(isset($data['parent'])); } // page attributes if ($post_type_obj->hierarchical && post_type_supports($post->post_type, 'page-attributes')) { $this->assertEquals($post->menu_order, $data['menu_order']); } else { $this->assertFalse(isset($data['menu_order'])); } // Comments if (post_type_supports($post->post_type, 'comments')) { $this->assertEquals($post->comment_status, $data['comment_status']); $this->assertEquals($post->ping_status, $data['ping_status']); } else { $this->assertFalse(isset($data['comment_status'])); $this->assertFalse(isset($data['ping_status'])); } if ('post' === $post->post_type) { $this->assertEquals(is_sticky($post->ID), $data['sticky']); } if ('page' === $post->post_type) { $this->assertEquals(get_page_template_slug($post->ID), $data['template']); } if (post_type_supports($post->post_type, 'thumbnail')) { $this->assertEquals((int) get_post_thumbnail_id($post->ID), $data['featured_image']); } else { $this->assertFalse(isset($data['featured_image'])); } // Check post format. if (post_type_supports($post->post_type, 'post-formats')) { $post_format = get_post_format($post->ID); if (empty($post_format)) { $this->assertEquals('standard', $data['format']); } else { $this->assertEquals(get_post_format($post->ID), $data['format']); } } else { $this->assertFalse(isset($data['format'])); } // Check filtered values. if (post_type_supports($post->post_type, 'title')) { $this->assertEquals(get_the_title($post->ID), $data['title']['rendered']); if ('edit' === $context) { $this->assertEquals($post->post_title, $data['title']['raw']); } else { $this->assertFalse(isset($data['title']['raw'])); } } else { $this->assertFalse(isset($data['title'])); } if (post_type_supports($post->post_type, 'editor')) { // TODO: apply content filter for more accurate testing. $this->assertEquals(wpautop($post->post_content), $data['content']['rendered']); if ('edit' === $context) { $this->assertEquals($post->post_content, $data['content']['raw']); } else { $this->assertFalse(isset($data['content']['raw'])); } } else { $this->assertFalse(isset($data['content'])); } if (post_type_supports($post->post_type, 'excerpt')) { if (empty($post->post_password)) { // TODO: apply excerpt filter for more accurate testing. $this->assertEquals(wpautop($post->post_excerpt), $data['excerpt']['rendered']); } else { $this->assertEquals('There is no excerpt because this is a protected post.', $data['excerpt']['rendered']); } if ('edit' === $context) { $this->assertEquals($post->post_excerpt, $data['excerpt']['raw']); } else { $this->assertFalse(isset($data['excerpt']['raw'])); } } else { $this->assertFalse(isset($data['excerpt'])); } $this->assertEquals($post->guid, $data['guid']['rendered']); if ('edit' === $context) { $this->assertEquals($post->guid, $data['guid']['raw']); $this->assertEquals($post->post_status, $data['status']); $this->assertEquals($post->post_password, $data['password']); if ('0000-00-00 00:00:00' === $post->post_date_gmt) { $this->assertNull($data['date_gmt']); } else { $this->assertEquals(rest_mysql_to_rfc3339($post->post_date_gmt), $data['date_gmt']); } if ('0000-00-00 00:00:00' === $post->post_modified_gmt) { $this->assertNull($data['modified_gmt']); } else { $this->assertEquals(rest_mysql_to_rfc3339($post->post_modified_gmt), $data['modified_gmt']); } } }
protected function check_comment_data($data, $context) { $comment = get_comment($data['id']); $this->assertEquals($comment->comment_ID, $data['id']); $this->assertEquals($comment->comment_post_ID, $data['post']); $this->assertEquals($comment->comment_parent, $data['parent']); $this->assertEquals($comment->user_id, $data['author']); $this->assertEquals($comment->comment_author, $data['author_name']); $this->assertEquals($comment->comment_author_url, $data['author_url']); $this->assertEquals(wpautop($comment->comment_content), $data['content']['rendered']); $this->assertEquals(rest_mysql_to_rfc3339($comment->comment_date), $data['date']); $this->assertEquals(get_comment_link($comment), $data['link']); $this->assertContains('author_avatar_urls', $data); if ('edit' === $context) { $this->assertEquals($comment->comment_author_email, $data['author_email']); $this->assertEquals($comment->comment_author_IP, $data['author_ip']); $this->assertEquals($comment->comment_agent, $data['author_user_agent']); $this->assertEquals(rest_mysql_to_rfc3339($comment->comment_date_gmt), $data['date_gmt']); $this->assertEquals($comment->comment_content, $data['content']['raw']); $this->assertEquals($comment->comment_karma, $data['karma']); } if ('edit' !== $context) { $this->assertArrayNotHasKey('author_email', $data); $this->assertArrayNotHasKey('author_ip', $data); $this->assertArrayNotHasKey('author_user_agent', $data); $this->assertArrayNotHasKey('date_gmt', $data); $this->assertArrayNotHasKey('raw', $data['content']); $this->assertArrayNotHasKey('karma', $data); } }
protected function check_get_revision_response($response, $revision) { if ($response instanceof WP_REST_Response) { $links = $response->get_links(); $response = $response->get_data(); } else { $this->assertArrayHasKey('_links', $response); $links = $response['_links']; } $this->assertEquals($revision->post_author, $response['author']); $this->assertEquals($revision->post_content, $response['content']); $this->assertEquals(rest_mysql_to_rfc3339($revision->post_date), $response['date']); $this->assertEquals(rest_mysql_to_rfc3339($revision->post_date_gmt), $response['date_gmt']); $this->assertEquals($revision->post_excerpt, $response['excerpt']); $this->assertEquals($revision->guid, $response['guid']); $this->assertEquals($revision->ID, $response['id']); $this->assertEquals(rest_mysql_to_rfc3339($revision->post_modified), $response['modified']); $this->assertEquals(rest_mysql_to_rfc3339($revision->post_modified_gmt), $response['modified_gmt']); $this->assertEquals($revision->post_name, $response['slug']); $this->assertEquals($revision->post_title, $response['title']); $parent = get_post($revision->post_parent); $parent_controller = new WP_REST_Posts_Controller($parent->post_type); $parent_base = $parent_controller->get_post_type_base($parent->post_type); $this->assertEquals(rest_url('wp/' . $parent_base . '/' . $revision->post_parent), $links['parent'][0]['href']); }
/** * Check the post_date_gmt or modified_gmt and prepare any post or * modified date for single post output. * * @param string $date_gmt * @param string|null $date * @return string|null ISO8601/RFC3339 formatted datetime. */ protected function prepare_date_response($date_gmt, $date = null) { if ('0000-00-00 00:00:00' === $date_gmt) { return null; } if (isset($date)) { return rest_mysql_to_rfc3339($date); } return rest_mysql_to_rfc3339($date_gmt); }
/** * Prepare a single comment output for response. * * @param object $comment Comment object. * @param WP_REST_Request $request Request object. * @return array $fields */ public function prepare_item_for_response($comment, $request) { $data = array('id' => (int) $comment->comment_ID, 'post' => (int) $comment->comment_post_ID, 'parent' => (int) $comment->comment_parent, 'author' => (int) $comment->user_id, 'author_name' => $comment->comment_author, 'author_email' => $comment->comment_author_email, 'author_url' => $comment->comment_author_url, 'author_ip' => $comment->comment_author_IP, 'author_avatar_urls' => rest_get_avatar_urls($comment->comment_author_email), 'author_user_agent' => $comment->comment_agent, 'date' => rest_mysql_to_rfc3339($comment->comment_date), 'date_gmt' => rest_mysql_to_rfc3339($comment->comment_date_gmt), 'content' => array('rendered' => apply_filters('comment_text', $comment->comment_content, $comment), 'raw' => $comment->comment_content), 'karma' => (int) $comment->comment_karma, 'link' => get_comment_link($comment), 'status' => $this->prepare_status_response($comment->comment_approved), 'type' => get_comment_type($comment->comment_ID)); $context = !empty($request['context']) ? $request['context'] : 'view'; $data = $this->filter_response_by_context($data, $context); $data = $this->add_additional_fields_to_object($data, $request); // Wrap the data in a response object $data = rest_ensure_response($data); $data->add_links($this->prepare_links($comment)); return apply_filters('rest_prepare_comment', $data, $comment, $request); }