/** * 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 mysql_to_rfc3339($date); } return mysql_to_rfc3339($date_gmt); }
/** * Parses and formats a MySQL datetime (Y-m-d H:i:s) for ISO8601/RFC3339. * * Requered WP 4.4 or later. * See https://developer.wordpress.org/reference/functions/mysql_to_rfc3339/ * * @since 2.6.0 * @param string $date * @return string|null ISO8601/RFC3339 formatted datetime. */ function wc_rest_prepare_date_response($date) { // Check if mysql_to_rfc3339 exists first! if (!function_exists('mysql_to_rfc3339')) { return null; } // Return null if $date is empty/zeros. if ('0000-00-00 00:00:00' === $date || empty($date)) { return null; } // Return the formatted datetime. return mysql_to_rfc3339($date); }
/** * Parses and formats a MySQL datetime (Y-m-d H:i:s) for ISO8601/RFC3339. * * Required WP 4.4 or later. * See https://developer.wordpress.org/reference/functions/mysql_to_rfc3339/ * * @since 2.6.0 * @param string $date * @return string|null ISO8601/RFC3339 formatted datetime. */ function wc_rest_prepare_date_response($date) { if (false === strpos($date, '-')) { $date = date('Y-m-d H:i:s', $date); } // Check if mysql_to_rfc3339 exists first! if (!function_exists('mysql_to_rfc3339')) { return null; } // Return null if $date is empty/zeros. if ('0000-00-00 00:00:00' === $date || empty($date)) { return null; } // Return the formatted datetime. return mysql_to_rfc3339($date); }
/** * Parses and formats a MySQL datetime (Y-m-d H:i:s) for ISO8601/RFC3339. * * Requered WP 4.4 or later. * See https://developer.wordpress.org/reference/functions/mysql_to_rfc3339/ * * @since 2.6.0 * @param string $date_gmt * @param string|null $date * @return string|null ISO8601/RFC3339 formatted datetime. */ function wc_rest_prepare_date_response($date_gmt, $date = null) { // Check if mysql_to_rfc3339 exists first! if (!function_exists('mysql_to_rfc3339')) { return null; } // Use the date if passed. if (isset($date)) { return mysql_to_rfc3339($date); } // Return null if $date_gmt is empty/zeros. if ('0000-00-00 00:00:00' === $date_gmt) { return null; } // Return the formatted datetime. return mysql_to_rfc3339($date_gmt); }
/** * @ticket 36054 * @dataProvider datetime_provider */ function test_mysql_to_rfc3339($expected, $actual) { $date_return = mysql_to_rfc3339($actual); $this->assertTrue(is_string($date_return), 'The date return must be a string'); $this->assertNotEmpty($date_return, 'The date return could not be an empty string'); $this->assertEquals($expected, $date_return, 'The date does not match'); $this->assertEquals(new DateTime($expected), new DateTime($date_return), 'The date is not the same after the call method'); }
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(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(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(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(mysql_to_rfc3339($post->post_modified_gmt), $data['modified_gmt']); } } }
/** * Changes database results into REST API entities * @param \EEM_Base $model * @param array $db_row like results from $wpdb->get_results() * @param string $include string indicating which fields to include in the response, * including fields on related entities. * Eg, when querying for events, an include string like: * "...&include=EVT_name,EVT_desc,Datetime, Datetime.Ticket.TKT_ID, Datetime.Ticket.TKT_name, Datetime.Ticket.TKT_price" * instructs us to only include the event's name and description, * each related datetime, and each related datetime's ticket's name and price. * Eg json would be: * '{ * "EVT_ID":12, * "EVT_name":"star wars party", * "EVT_desc":"this is the party you are looking for...", * "datetimes":[{ * "DTT_ID":123,..., * "tickets":[{ * "TKT_ID":234, * "TKT_name":"student rate", * "TKT_price":32.0 * },...] * }] * }', * ie, events with all their associated datetimes * (including ones that are trashed) embedded in the json object, * and each datetime also has each associated ticket embedded in its json object. * @param string $context one of the return values from EEM_Base::valid_cap_contexts() * @return array ready for being converted into json for sending to client */ public function create_entity_from_wpdb_result($model, $db_row, $include, $context) { if ($include == null) { $include = '*'; } if ($context == null) { $context = \EEM_Base::caps_read; } $result = $model->deduce_fields_n_values_from_cols_n_values($db_row); $result = array_intersect_key($result, $this->get_model_version_info()->fields_on_model_in_this_version($model)); foreach ($result as $field_name => $raw_field_value) { $field_obj = $model->field_settings_for($field_name); $field_value = $field_obj->prepare_for_set_from_db($raw_field_value); if ($this->is_subclass_of_one($field_obj, $this->get_model_version_info()->fields_ignored())) { unset($result[$field_name]); } elseif ($this->is_subclass_of_one($field_obj, $this->get_model_version_info()->fields_that_have_rendered_format())) { $result[$field_name] = array('raw' => $field_obj->prepare_for_get($field_value), 'rendered' => $field_obj->prepare_for_pretty_echoing($field_value)); } elseif ($this->is_subclass_of_one($field_obj, $this->get_model_version_info()->fields_that_have_pretty_format())) { $result[$field_name] = array('raw' => $field_obj->prepare_for_get($field_value), 'pretty' => $field_obj->prepare_for_pretty_echoing($field_value)); } elseif ($field_obj instanceof \EE_Datetime_Field) { if ($raw_field_value instanceof \DateTime) { $raw_field_value = $raw_field_value->format('c'); } $result[$field_name] = mysql_to_rfc3339($raw_field_value); } else { $value_prepared = $field_obj->prepare_for_get($field_value); $result[$field_name] = $value_prepared === INF ? EE_INF_IN_DB : $value_prepared; } } if ($model instanceof \EEM_CPT_Base) { $attachment = wp_get_attachment_image_src(get_post_thumbnail_id($db_row[$model->get_primary_key_field()->get_qualified_column()]), 'full'); $result['featured_image_url'] = !empty($attachment) ? $attachment[0] : null; $result['link'] = get_permalink($db_row[$model->get_primary_key_field()->get_qualified_column()]); } //add links to related data $result['_links'] = array('self' => array(array('href' => $this->get_versioned_link_to(\EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) . '/' . $result[$model->primary_key_name()]))), 'collection' => array(array('href' => $this->get_versioned_link_to(\EEH_Inflector::pluralize_and_lower($model->get_this_model_name()))))); global $wp_rest_server; if ($model instanceof \EEM_CPT_Base && $wp_rest_server instanceof \WP_REST_Server && $wp_rest_server->get_route_options('/wp/v2/posts')) { $result['_links'][\EED_Core_Rest_Api::ee_api_link_namespace . 'self_wp_post'] = array(array('href' => rest_url('/wp/v2/posts/' . $db_row[$model->get_primary_key_field()->get_qualified_column()]), 'single' => true)); } //filter fields if specified $includes_for_this_model = $this->extract_includes_for_this_model($include); if (!empty($includes_for_this_model)) { if ($model->has_primary_key_field()) { //always include the primary key $includes_for_this_model[] = $model->primary_key_name(); } $result = array_intersect_key($result, array_flip($includes_for_this_model)); } //add meta links and possibly include related models $relation_settings = apply_filters('FHEE__Read__create_entity_from_wpdb_result__related_models_to_include', $model->relation_settings()); foreach ($relation_settings as $relation_name => $relation_obj) { $related_model_part = $this->get_related_entity_name($relation_name, $relation_obj); if (empty($includes_for_this_model) || isset($includes_for_this_model['meta'])) { $result['_links'][\EED_Core_Rest_Api::ee_api_link_namespace . $related_model_part] = array(array('href' => $this->get_versioned_link_to(\EEH_Inflector::pluralize_and_lower($model->get_this_model_name()) . '/' . $result[$model->primary_key_name()] . '/' . $related_model_part), 'single' => $relation_obj instanceof \EE_Belongs_To_Relation ? true : false)); } $related_fields_to_include = $this->extract_includes_for_this_model($include, $relation_name); if ($related_fields_to_include) { $pretend_related_request = new \WP_REST_Request(); $pretend_related_request->set_query_params(array('caps' => $context, 'include' => $this->extract_includes_for_this_model($include, $relation_name))); $related_results = $this->get_entities_from_relation($result[$model->primary_key_name()], $relation_obj, $pretend_related_request); $result[$related_model_part] = $related_results instanceof \WP_Error ? null : $related_results; } } $result = apply_filters('FHEE__Read__create_entity_from_wpdb_results__entity_before_inaccessible_field_removal', $result, $model, $context); $result_without_inaccessible_fields = Capabilities::filter_out_inaccessible_entity_fields($result, $model, $context, $this->get_model_version_info()); $this->_set_debug_info('inaccessible fields', array_keys(array_diff_key($result, $result_without_inaccessible_fields))); return apply_filters('FHEE__Read__create_entity_from_wpdb_results__entity_return', $result_without_inaccessible_fields, $model, $context); }
public function test_update_post_ignore_readonly() { wp_set_current_user(self::$editor_id); $new_content = rand_str(); $expected_modified = current_time('mysql'); $request = new WP_REST_Request('PUT', sprintf('/wp/v2/posts/%d', self::$post_id)); $params = $this->set_post_data(array('modified' => '2010-06-01T02:00:00Z', 'content' => $new_content)); $request->set_body_params($params); $response = $this->server->dispatch($request); // The readonly modified param should be ignored, request should be a success. $data = $response->get_data(); $new_post = get_post($data['id']); $this->assertEquals($new_content, $data['content']['raw']); $this->assertEquals($new_content, $new_post->post_content); // The modified date should equal the current time. $this->assertEquals(date('Y-m-d', strtotime(mysql_to_rfc3339($expected_modified))), date('Y-m-d', strtotime($data['modified']))); $this->assertEquals(date('Y-m-d', strtotime($expected_modified)), date('Y-m-d', strtotime($new_post->post_modified))); }
/** * Prepare a single comment output for response. * * @param object $comment Comment object. * @param WP_REST_Request $request Request object. * @return WP_REST_Response */ 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' => mysql_to_rfc3339($comment->comment_date), 'date_gmt' => 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 $response = rest_ensure_response($data); $response->add_links($this->prepare_links($comment)); /** * Filter a comment returned from the API. * * Allows modification of the comment right before it is returned. * * @param WP_REST_Response $response The response object. * @param object $comment The original comment object. * @param WP_REST_Request $request Request used to generate the response. */ return apply_filters('rest_prepare_comment', $response, $comment, $request); }
protected function check_comment_data($data, $context, $links) { $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(mysql_to_rfc3339($comment->comment_date), $data['date']); $this->assertEquals(mysql_to_rfc3339($comment->comment_date_gmt), $data['date_gmt']); $this->assertEquals(get_comment_link($comment), $data['link']); $this->assertContains('author_avatar_urls', $data); $this->assertEqualSets(array('self', 'collection', 'up'), array_keys($links)); 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($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('raw', $data['content']); $this->assertArrayNotHasKey('karma', $data); } }
protected function check_post_data($post, $data, $context, $links) { $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_gmt']); } $this->assertEquals(mysql_to_rfc3339($post->post_date), $data['date']); if ('0000-00-00 00:00:00' === $post->post_modified_gmt) { $this->assertNull($data['modified_gmt']); } $this->assertEquals(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 ('post' === $post->post_type && 'edit' === $context) { $this->assertEquals($post->post_password, $data['password']); } 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_media']); } else { $this->assertFalse(isset($data['featured_media'])); } // 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')) { add_filter('protected_title_format', array($this, 'protected_title_format')); $this->assertEquals(get_the_title($post->ID), $data['title']['rendered']); remove_filter('protected_title_format', array($this, 'protected_title_format')); 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. if (!$post->post_password) { $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 { // TODO: better testing for excerpts for password protected posts. } 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']); if ('0000-00-00 00:00:00' === $post->post_date_gmt) { $this->assertNull($data['date_gmt']); } else { $this->assertEquals(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(mysql_to_rfc3339($post->post_modified_gmt), $data['modified_gmt']); } } $taxonomies = wp_list_filter(get_object_taxonomies($post->post_type, 'objects'), array('show_in_rest' => true)); foreach ($taxonomies as $taxonomy) { $this->assertTrue(isset($data[$taxonomy->rest_base])); $terms = wp_get_object_terms($post->ID, $taxonomy->name, array('fields' => 'ids')); sort($terms); sort($data[$taxonomy->rest_base]); $this->assertEquals($terms, $data[$taxonomy->rest_base]); } // test links if ($links) { $links = test_rest_expand_compact_links($links); $post_type = get_post_type_object($data['type']); $this->assertEquals($links['self'][0]['href'], rest_url('wp/v2/' . $post_type->rest_base . '/' . $data['id'])); $this->assertEquals($links['collection'][0]['href'], rest_url('wp/v2/' . $post_type->rest_base)); $this->assertEquals($links['about'][0]['href'], rest_url('wp/v2/types/' . $data['type'])); if (post_type_supports($post->post_type, 'author') && $data['author']) { $this->assertEquals($links['author'][0]['href'], rest_url('wp/v2/users/' . $data['author'])); } if (post_type_supports($post->post_type, 'comments')) { $this->assertEquals($links['replies'][0]['href'], add_query_arg('post', $data['id'], rest_url('wp/v2/comments'))); } if (post_type_supports($post->post_type, 'revisions')) { $this->assertEquals($links['version-history'][0]['href'], rest_url('wp/v2/' . $post_type->rest_base . '/' . $data['id'] . '/revisions')); } if ($post_type->hierarchical && !empty($data['parent'])) { $this->assertEquals($links['up'][0]['href'], rest_url('wp/v2/' . $post_type->rest_base . '/' . $data['parent'])); } if (!in_array($data['type'], array('attachment', 'nav_menu_item', 'revision'), true)) { $this->assertEquals($links['https://api.w.org/attachment'][0]['href'], add_query_arg('parent', $data['id'], rest_url('wp/v2/media'))); } if (!empty($data['featured_media'])) { $this->assertEquals($links['https://api.w.org/featuredmedia'][0]['href'], rest_url('wp/v2/media/' . $data['featured_media'])); } $num = 0; foreach ($taxonomies as $key => $taxonomy) { $this->assertEquals($taxonomy->name, $links['https://api.w.org/term'][$num]['attributes']['taxonomy']); $this->assertEquals(add_query_arg('post', $data['id'], rest_url('wp/v2/' . $taxonomy->rest_base)), $links['https://api.w.org/term'][$num]['href']); $num++; } } }
/** * Prepare a single comment output for response. * * @param object $comment Comment object. * @param WP_REST_Request $request Request object. * @return WP_REST_Response */ 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' => mysql_to_rfc3339($comment->comment_date), 'date_gmt' => 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); }
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']); $rendered_content = apply_filters('the_content', $revision->post_content); $this->assertEquals($rendered_content, $response['content']['rendered']); $this->assertEquals(mysql_to_rfc3339($revision->post_date), $response['date']); $this->assertEquals(mysql_to_rfc3339($revision->post_date_gmt), $response['date_gmt']); $rendered_excerpt = apply_filters('the_excerpt', apply_filters('get_the_excerpt', $revision->post_excerpt, $revision)); $this->assertEquals($rendered_excerpt, $response['excerpt']['rendered']); $rendered_guid = apply_filters('get_the_guid', $revision->guid); $this->assertEquals($rendered_guid, $response['guid']['rendered']); $this->assertEquals($revision->ID, $response['id']); $this->assertEquals(mysql_to_rfc3339($revision->post_modified), $response['modified']); $this->assertEquals(mysql_to_rfc3339($revision->post_modified_gmt), $response['modified_gmt']); $this->assertEquals($revision->post_name, $response['slug']); $rendered_title = get_the_title($revision->ID); $this->assertEquals($rendered_title, $response['title']['rendered']); $parent = get_post($revision->post_parent); $parent_controller = new WP_REST_Posts_Controller($parent->post_type); $parent_object = get_post_type_object($parent->post_type); $parent_base = !empty($parent_object->rest_base) ? $parent_object->rest_base : $parent_object->name; $this->assertEquals(rest_url('/wp/v2/' . $parent_base . '/' . $revision->post_parent), $links['parent'][0]['href']); }
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(mysql_to_rfc3339($revision->post_date), $response['date']); $this->assertEquals(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(mysql_to_rfc3339($revision->post_modified), $response['modified']); $this->assertEquals(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']); }
/** * Checks the post_date_gmt or modified_gmt and prepare any post or * modified date for single post output. * * @since 4.7.0 * @access protected * * @param string $date_gmt GMT publication time. * @param string|null $date Optional. Local publication time. Default null. * @return string|null ISO8601/RFC3339 formatted datetime. */ protected function prepare_date_response($date_gmt, $date = null) { // Use the date if passed. if (isset($date)) { return mysql_to_rfc3339($date); } // Return null if $date_gmt is empty/zeros. if ('0000-00-00 00:00:00' === $date_gmt) { return null; } // Return the formatted datetime. return mysql_to_rfc3339($date_gmt); }
/** * Parses and formats a MySQL datetime (Y-m-d H:i:s) for ISO8601/RFC3339. * * Explicitly strips timezones, as datetimes are not saved with any timezone * information. Including any information on the offset could be misleading. * * @deprecated WPAPI-2.0 mysql_to_rfc3339() * * @param string $date_string Date string to parse and format. * @return string Date formatted for ISO8601/RFC3339. */ function rest_mysql_to_rfc3339($date_string) { _deprecated_function('rest_mysql_to_rfc3339', 'WPAPI-2.0', 'mysql_to_rfc3339'); return mysql_to_rfc3339($date_string); }
/** * Prepares a field's value for display in the API * @param \EE_Model_Field_Base $field_obj * @param mixed $original_value * @return mixed */ public static function prepare_field_value_for_json($field_obj, $original_value, $requested_version) { if ($original_value === EE_INF) { $new_value = Model_Data_Translator::ee_inf_in_rest; } elseif ($field_obj instanceof \EE_Datetime_Field) { if ($original_value instanceof \DateTime) { $new_value = $original_value->format('Y-m-d H:i:s'); } elseif (is_int($original_value)) { $new_value = date('Y-m-d H:i:s', $original_value); } else { $new_value = $original_value; } $new_value = mysql_to_rfc3339($new_value); } else { $new_value = $original_value; } return apply_filters('FHEE__EventEspresso\\core\\libraries\\rest_api\\Model_Data_Translator__prepare_field_for_rest_api', $new_value, $field_obj, $original_value, $requested_version); }