- Timestamp:
- 11/30/2017 11:09:33 PM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php
r41979 r42343 43 43 $this->post_type = $post_type; 44 44 $this->namespace = 'wp/v2'; 45 $obj = get_post_type_object( $post_type );45 $obj = get_post_type_object( $post_type ); 46 46 $this->rest_base = ! empty( $obj->rest_base ) ? $obj->rest_base : $obj->name; 47 47 … … 58 58 public function register_routes() { 59 59 60 register_rest_route( $this->namespace, '/' . $this->rest_base, array( 61 array( 62 'methods' => WP_REST_Server::READABLE, 63 'callback' => array( $this, 'get_items' ), 64 'permission_callback' => array( $this, 'get_items_permissions_check' ), 65 'args' => $this->get_collection_params(), 66 ), 67 array( 68 'methods' => WP_REST_Server::CREATABLE, 69 'callback' => array( $this, 'create_item' ), 70 'permission_callback' => array( $this, 'create_item_permissions_check' ), 71 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ), 72 ), 73 'schema' => array( $this, 'get_public_item_schema' ), 74 ) ); 75 76 $schema = $this->get_item_schema(); 60 register_rest_route( 61 $this->namespace, '/' . $this->rest_base, array( 62 array( 63 'methods' => WP_REST_Server::READABLE, 64 'callback' => array( $this, 'get_items' ), 65 'permission_callback' => array( $this, 'get_items_permissions_check' ), 66 'args' => $this->get_collection_params(), 67 ), 68 array( 69 'methods' => WP_REST_Server::CREATABLE, 70 'callback' => array( $this, 'create_item' ), 71 'permission_callback' => array( $this, 'create_item_permissions_check' ), 72 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ), 73 ), 74 'schema' => array( $this, 'get_public_item_schema' ), 75 ) 76 ); 77 78 $schema = $this->get_item_schema(); 77 79 $get_item_args = array( 78 'context' => $this->get_context_param( array( 'default' => 'view' ) ),80 'context' => $this->get_context_param( array( 'default' => 'view' ) ), 79 81 ); 80 82 if ( isset( $schema['properties']['password'] ) ) { … … 84 86 ); 85 87 } 86 register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', array( 87 'args' => array( 88 'id' => array( 89 'description' => __( 'Unique identifier for the object.' ), 90 'type' => 'integer', 91 ), 92 ), 93 array( 94 'methods' => WP_REST_Server::READABLE, 95 'callback' => array( $this, 'get_item' ), 96 'permission_callback' => array( $this, 'get_item_permissions_check' ), 97 'args' => $get_item_args, 98 ), 99 array( 100 'methods' => WP_REST_Server::EDITABLE, 101 'callback' => array( $this, 'update_item' ), 102 'permission_callback' => array( $this, 'update_item_permissions_check' ), 103 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), 104 ), 105 array( 106 'methods' => WP_REST_Server::DELETABLE, 107 'callback' => array( $this, 'delete_item' ), 108 'permission_callback' => array( $this, 'delete_item_permissions_check' ), 109 'args' => array( 110 'force' => array( 111 'type' => 'boolean', 112 'default' => false, 113 'description' => __( 'Whether to bypass trash and force deletion.' ), 88 register_rest_route( 89 $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', array( 90 'args' => array( 91 'id' => array( 92 'description' => __( 'Unique identifier for the object.' ), 93 'type' => 'integer', 114 94 ), 115 95 ), 116 ), 117 'schema' => array( $this, 'get_public_item_schema' ), 118 ) ); 96 array( 97 'methods' => WP_REST_Server::READABLE, 98 'callback' => array( $this, 'get_item' ), 99 'permission_callback' => array( $this, 'get_item_permissions_check' ), 100 'args' => $get_item_args, 101 ), 102 array( 103 'methods' => WP_REST_Server::EDITABLE, 104 'callback' => array( $this, 'update_item' ), 105 'permission_callback' => array( $this, 'update_item_permissions_check' ), 106 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), 107 ), 108 array( 109 'methods' => WP_REST_Server::DELETABLE, 110 'callback' => array( $this, 'delete_item' ), 111 'permission_callback' => array( $this, 'delete_item_permissions_check' ), 112 'args' => array( 113 'force' => array( 114 'type' => 'boolean', 115 'default' => false, 116 'description' => __( 'Whether to bypass trash and force deletion.' ), 117 ), 118 ), 119 ), 120 'schema' => array( $this, 'get_public_item_schema' ), 121 ) 122 ); 119 123 } 120 124 … … 160 164 // Retrieve the list of registered collection query parameters. 161 165 $registered = $this->get_collection_params(); 162 $args = array();166 $args = array(); 163 167 164 168 /* … … 259 263 * @param WP_REST_Request $request The request used. 260 264 */ 261 $args = apply_filters( "rest_{$this->post_type}_query", $args, $request );265 $args = apply_filters( "rest_{$this->post_type}_query", $args, $request ); 262 266 $query_args = $this->prepare_items_query( $args, $request ); 263 267 … … 265 269 266 270 foreach ( $taxonomies as $taxonomy ) { 267 $base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name;271 $base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name; 268 272 $tax_exclude = $base . '_exclude'; 269 273 … … 312 316 } 313 317 314 $page = (int) $query_args['paged'];318 $page = (int) $query_args['paged']; 315 319 $total_posts = $posts_query->found_posts; 316 320 … … 330 334 } 331 335 332 $response = rest_ensure_response( $posts );336 $response = rest_ensure_response( $posts ); 333 337 334 338 $response->header( 'X-WP-Total', (int) $total_posts ); … … 336 340 337 341 $request_params = $request->get_query_params(); 338 $base = add_query_arg( $request_params, rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ) );342 $base = add_query_arg( $request_params, rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ) ); 339 343 340 344 if ( $page > 1 ) { … … 467 471 468 472 if ( is_post_type_viewable( get_post_type_object( $post->post_type ) ) ) { 469 $response->link_header( 'alternate', get_permalink( $post->ID ), array( 'type' => 'text/html' ) );473 $response->link_header( 'alternate', get_permalink( $post->ID ), array( 'type' => 'text/html' ) ); 470 474 } 471 475 … … 592 596 } 593 597 594 $post = get_post( $post_id );598 $post = get_post( $post_id ); 595 599 $fields_update = $this->update_additional_fields_for_object( $post, $request ); 596 600 … … 718 722 } 719 723 720 $post = get_post( $post_id );724 $post = get_post( $post_id ); 721 725 $fields_update = $this->update_additional_fields_for_object( $post, $request ); 722 726 … … 796 800 $request->set_param( 'context', 'edit' ); 797 801 798 799 802 // If we're forcing, then delete permanently. 800 803 if ( $force ) { 801 804 $previous = $this->prepare_item_for_response( $post, $request ); 802 $result = wp_delete_post( $id, true );805 $result = wp_delete_post( $id, true ); 803 806 $response = new WP_REST_Response(); 804 $response->set_data( array( 'deleted' => true, 'previous' => $previous->get_data() ) ); 807 $response->set_data( 808 array( 809 'deleted' => true, 810 'previous' => $previous->get_data(), 811 ) 812 ); 805 813 } else { 806 814 // If we don't support trashing for this type, error out. … … 817 825 // (Note that internally this falls through to `wp_delete_post` if 818 826 // the trash is disabled.) 819 $result = wp_trash_post( $id );820 $post = get_post( $id );827 $result = wp_trash_post( $id ); 828 $post = get_post( $id ); 821 829 $response = $this->prepare_item_for_response( $post, $request ); 822 830 } … … 992 1000 if ( ! empty( $date_data ) ) { 993 1001 list( $prepared_post->post_date, $prepared_post->post_date_gmt ) = $date_data; 994 $prepared_post->edit_date = true;1002 $prepared_post->edit_date = true; 995 1003 } 996 1004 } elseif ( ! empty( $schema['properties']['date_gmt'] ) && ! empty( $request['date_gmt'] ) ) { … … 999 1007 if ( ! empty( $date_data ) ) { 1000 1008 list( $prepared_post->post_date, $prepared_post->post_date_gmt ) = $date_data; 1001 $prepared_post->edit_date = true;1009 $prepared_post->edit_date = true; 1002 1010 } 1003 1011 } … … 1503 1511 if ( ! empty( $schema['properties']['excerpt'] ) ) { 1504 1512 /** This filter is documented in wp-includes/post-template.php */ 1505 $excerpt = apply_filters( 'the_excerpt', apply_filters( 'get_the_excerpt', $post->post_excerpt, $post ) );1513 $excerpt = apply_filters( 'the_excerpt', apply_filters( 'get_the_excerpt', $post->post_excerpt, $post ) ); 1506 1514 $data['excerpt'] = array( 1507 1515 'raw' => $post->post_excerpt, … … 1571 1579 1572 1580 if ( ! empty( $schema['properties'][ $base ] ) ) { 1573 $terms = get_the_terms( $post, $taxonomy->name );1581 $terms = get_the_terms( $post, $taxonomy->name ); 1574 1582 $data[ $base ] = $terms ? array_values( wp_list_pluck( $terms, 'term_id' ) ) : array(); 1575 1583 } … … 1627 1635 // Entity meta. 1628 1636 $links = array( 1629 'self' => array(1630 'href' => rest_url( trailingslashit( $base ) . $post->ID ),1637 'self' => array( 1638 'href' => rest_url( trailingslashit( $base ) . $post->ID ), 1631 1639 ), 1632 1640 'collection' => array( 1633 'href' => rest_url( $base ),1641 'href' => rest_url( $base ), 1634 1642 ), 1635 1643 'about' => array( 1636 'href' => rest_url( 'wp/v2/types/' . $this->post_type ),1644 'href' => rest_url( 'wp/v2/types/' . $this->post_type ), 1637 1645 ), 1638 1646 ); … … 1737 1745 // Base properties for every Post. 1738 1746 'properties' => array( 1739 'date' => array(1747 'date' => array( 1740 1748 'description' => __( "The date the object was published, in the site's timezone." ), 1741 1749 'type' => 'string', … … 1743 1751 'context' => array( 'view', 'edit', 'embed' ), 1744 1752 ), 1745 'date_gmt' => array(1753 'date_gmt' => array( 1746 1754 'description' => __( 'The date the object was published, as GMT.' ), 1747 1755 'type' => 'string', … … 1749 1757 'context' => array( 'view', 'edit' ), 1750 1758 ), 1751 'guid' => array(1759 'guid' => array( 1752 1760 'description' => __( 'The globally unique identifier for the object.' ), 1753 1761 'type' => 'object', … … 1769 1777 ), 1770 1778 ), 1771 'id' => array(1779 'id' => array( 1772 1780 'description' => __( 'Unique identifier for the object.' ), 1773 1781 'type' => 'integer', … … 1775 1783 'readonly' => true, 1776 1784 ), 1777 'link' => array(1785 'link' => array( 1778 1786 'description' => __( 'URL to the object.' ), 1779 1787 'type' => 'string', … … 1782 1790 'readonly' => true, 1783 1791 ), 1784 'modified' => array(1792 'modified' => array( 1785 1793 'description' => __( "The date the object was last modified, in the site's timezone." ), 1786 1794 'type' => 'string', … … 1789 1797 'readonly' => true, 1790 1798 ), 1791 'modified_gmt' => array(1799 'modified_gmt' => array( 1792 1800 'description' => __( 'The date the object was last modified, as GMT.' ), 1793 1801 'type' => 'string', … … 1796 1804 'readonly' => true, 1797 1805 ), 1798 'slug' => array(1806 'slug' => array( 1799 1807 'description' => __( 'An alphanumeric identifier for the object unique to its type.' ), 1800 1808 'type' => 'string', … … 1804 1812 ), 1805 1813 ), 1806 'status' => array(1814 'status' => array( 1807 1815 'description' => __( 'A named status for the object.' ), 1808 1816 'type' => 'string', … … 1810 1818 'context' => array( 'view', 'edit' ), 1811 1819 ), 1812 'type' => array(1820 'type' => array( 1813 1821 'description' => __( 'Type of Post for the object.' ), 1814 1822 'type' => 'string', … … 1816 1824 'readonly' => true, 1817 1825 ), 1818 'password' => array(1826 'password' => array( 1819 1827 'description' => __( 'A password to protect access to the content and excerpt.' ), 1820 1828 'type' => 'string', … … 1846 1854 'custom-fields', 1847 1855 ); 1848 $fixed_schemas = array(1849 'post' => array(1856 $fixed_schemas = array( 1857 'post' => array( 1850 1858 'title', 1851 1859 'editor', … … 1858 1866 'custom-fields', 1859 1867 ), 1860 'page' => array(1868 'page' => array( 1861 1869 'title', 1862 1870 'editor', … … 1896 1904 ), 1897 1905 'properties' => array( 1898 'raw' => array(1906 'raw' => array( 1899 1907 'description' => __( 'Title for the object, as it exists in the database.' ), 1900 1908 'type' => 'string', … … 1921 1929 ), 1922 1930 'properties' => array( 1923 'raw' => array(1931 'raw' => array( 1924 1932 'description' => __( 'Content for the object, as it exists in the database.' ), 1925 1933 'type' => 'string', 1926 1934 'context' => array( 'edit' ), 1927 1935 ), 1928 'rendered' => array(1936 'rendered' => array( 1929 1937 'description' => __( 'HTML content for the object, transformed for display.' ), 1930 1938 'type' => 'string', … … 1932 1940 'readonly' => true, 1933 1941 ), 1934 'protected' => array(1942 'protected' => array( 1935 1943 'description' => __( 'Whether the content is protected with a password.' ), 1936 1944 'type' => 'boolean', … … 1960 1968 ), 1961 1969 'properties' => array( 1962 'raw' => array(1970 'raw' => array( 1963 1971 'description' => __( 'Excerpt for the object, as it exists in the database.' ), 1964 1972 'type' => 'string', 1965 1973 'context' => array( 'edit' ), 1966 1974 ), 1967 'rendered' => array(1975 'rendered' => array( 1968 1976 'description' => __( 'HTML excerpt for the object, transformed for display.' ), 1969 1977 'type' => 'string', … … 1971 1979 'readonly' => true, 1972 1980 ), 1973 'protected' => array(1981 'protected' => array( 1974 1982 'description' => __( 'Whether the excerpt is protected with a password.' ), 1975 1983 'type' => 'boolean', … … 1996 2004 'context' => array( 'view', 'edit' ), 1997 2005 ); 1998 $schema['properties']['ping_status'] = array(2006 $schema['properties']['ping_status'] = array( 1999 2007 'description' => __( 'Whether or not the object can be pinged.' ), 2000 2008 'type' => 'string', … … 2050 2058 $taxonomies = wp_list_filter( get_object_taxonomies( $this->post_type, 'objects' ), array( 'show_in_rest' => true ) ); 2051 2059 foreach ( $taxonomies as $taxonomy ) { 2052 $base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name;2060 $base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name; 2053 2061 $schema['properties'][ $base ] = array( 2054 2062 /* translators: %s: taxonomy name */ … … 2056 2064 'type' => 'array', 2057 2065 'items' => array( 2058 'type' => 'integer',2066 'type' => 'integer', 2059 2067 ), 2060 2068 'context' => array( 'view', 'edit' ), … … 2078 2086 2079 2087 $query_params['after'] = array( 2080 'description' => __( 'Limit response to posts published after a given ISO8601 compliant date.' ),2081 'type' => 'string',2082 'format' => 'date-time',2088 'description' => __( 'Limit response to posts published after a given ISO8601 compliant date.' ), 2089 'type' => 'string', 2090 'format' => 'date-time', 2083 2091 ); 2084 2092 2085 2093 if ( post_type_supports( $this->post_type, 'author' ) ) { 2086 $query_params['author'] = array(2087 'description' => __( 'Limit result set to posts assigned to specific authors.' ),2088 'type' => 'array',2089 'items' => array(2090 'type' => 'integer',2094 $query_params['author'] = array( 2095 'description' => __( 'Limit result set to posts assigned to specific authors.' ), 2096 'type' => 'array', 2097 'items' => array( 2098 'type' => 'integer', 2091 2099 ), 2092 'default' => array(),2100 'default' => array(), 2093 2101 ); 2094 2102 $query_params['author_exclude'] = array( 2095 'description' => __( 'Ensure result set excludes posts assigned to specific authors.' ),2096 'type' => 'array',2097 'items' => array(2098 'type' => 'integer',2103 'description' => __( 'Ensure result set excludes posts assigned to specific authors.' ), 2104 'type' => 'array', 2105 'items' => array( 2106 'type' => 'integer', 2099 2107 ), 2100 'default' => array(),2108 'default' => array(), 2101 2109 ); 2102 2110 } 2103 2111 2104 2112 $query_params['before'] = array( 2105 'description' => __( 'Limit response to posts published before a given ISO8601 compliant date.' ),2106 'type' => 'string',2107 'format' => 'date-time',2113 'description' => __( 'Limit response to posts published before a given ISO8601 compliant date.' ), 2114 'type' => 'string', 2115 'format' => 'date-time', 2108 2116 ); 2109 2117 2110 2118 $query_params['exclude'] = array( 2111 'description' => __( 'Ensure result set excludes specific IDs.' ),2112 'type' => 'array',2113 'items' => array(2114 'type' => 'integer',2119 'description' => __( 'Ensure result set excludes specific IDs.' ), 2120 'type' => 'array', 2121 'items' => array( 2122 'type' => 'integer', 2115 2123 ), 2116 'default' => array(),2124 'default' => array(), 2117 2125 ); 2118 2126 2119 2127 $query_params['include'] = array( 2120 'description' => __( 'Limit result set to specific IDs.' ),2121 'type' => 'array',2122 'items' => array(2123 'type' => 'integer',2128 'description' => __( 'Limit result set to specific IDs.' ), 2129 'type' => 'array', 2130 'items' => array( 2131 'type' => 'integer', 2124 2132 ), 2125 'default' => array(),2133 'default' => array(), 2126 2134 ); 2127 2135 2128 2136 if ( 'page' === $this->post_type || post_type_supports( $this->post_type, 'page-attributes' ) ) { 2129 2137 $query_params['menu_order'] = array( 2130 'description' => __( 'Limit result set to posts with a specific menu_order value.' ),2131 'type' => 'integer',2138 'description' => __( 'Limit result set to posts with a specific menu_order value.' ), 2139 'type' => 'integer', 2132 2140 ); 2133 2141 } 2134 2142 2135 2143 $query_params['offset'] = array( 2136 'description' => __( 'Offset the result set by a specific number of items.' ),2137 'type' => 'integer',2144 'description' => __( 'Offset the result set by a specific number of items.' ), 2145 'type' => 'integer', 2138 2146 ); 2139 2147 2140 2148 $query_params['order'] = array( 2141 'description' => __( 'Order sort attribute ascending or descending.' ),2142 'type' => 'string',2143 'default' => 'desc',2144 'enum' => array( 'asc', 'desc' ),2149 'description' => __( 'Order sort attribute ascending or descending.' ), 2150 'type' => 'string', 2151 'default' => 'desc', 2152 'enum' => array( 'asc', 'desc' ), 2145 2153 ); 2146 2154 2147 2155 $query_params['orderby'] = array( 2148 'description' => __( 'Sort collection by object attribute.' ),2149 'type' => 'string',2150 'default' => 'date',2151 'enum' => array(2156 'description' => __( 'Sort collection by object attribute.' ), 2157 'type' => 'string', 2158 'default' => 'date', 2159 'enum' => array( 2152 2160 'author', 2153 2161 'date', … … 2170 2178 2171 2179 if ( $post_type->hierarchical || 'attachment' === $this->post_type ) { 2172 $query_params['parent'] = array(2173 'description' => __( 'Limit result set to items with particular parent IDs.' ),2174 'type' => 'array',2175 'items' => array(2176 'type' => 'integer',2180 $query_params['parent'] = array( 2181 'description' => __( 'Limit result set to items with particular parent IDs.' ), 2182 'type' => 'array', 2183 'items' => array( 2184 'type' => 'integer', 2177 2185 ), 2178 'default' => array(),2186 'default' => array(), 2179 2187 ); 2180 2188 $query_params['parent_exclude'] = array( 2181 'description' => __( 'Limit result set to all items except those of a particular parent ID.' ),2182 'type' => 'array',2183 'items' => array(2184 'type' => 'integer',2189 'description' => __( 'Limit result set to all items except those of a particular parent ID.' ), 2190 'type' => 'array', 2191 'items' => array( 2192 'type' => 'integer', 2185 2193 ), 2186 'default' => array(),2194 'default' => array(), 2187 2195 ); 2188 2196 } … … 2192 2200 'type' => 'array', 2193 2201 'items' => array( 2194 'type' => 'string',2202 'type' => 'string', 2195 2203 ), 2196 2204 'sanitize_callback' => 'wp_parse_slug_list', … … 2202 2210 'type' => 'array', 2203 2211 'items' => array( 2204 'enum' => array_merge( array_keys( get_post_stati() ), array( 'any' ) ),2205 'type' => 'string',2212 'enum' => array_merge( array_keys( get_post_stati() ), array( 'any' ) ), 2213 'type' => 'string', 2206 2214 ), 2207 2215 'sanitize_callback' => array( $this, 'sanitize_post_statuses' ), … … 2215 2223 $query_params[ $base ] = array( 2216 2224 /* translators: %s: taxonomy name */ 2217 'description' => sprintf( __( 'Limit result set to all items that have the specified term assigned in the %s taxonomy.' ), $base ),2218 'type' => 'array',2219 'items' => array(2220 'type' => 'integer',2225 'description' => sprintf( __( 'Limit result set to all items that have the specified term assigned in the %s taxonomy.' ), $base ), 2226 'type' => 'array', 2227 'items' => array( 2228 'type' => 'integer', 2221 2229 ), 2222 'default' => array(),2230 'default' => array(), 2223 2231 ); 2224 2232 … … 2228 2236 'type' => 'array', 2229 2237 'items' => array( 2230 'type' => 'integer',2238 'type' => 'integer', 2231 2239 ), 2232 'default' => array(),2240 'default' => array(), 2233 2241 ); 2234 2242 } … … 2236 2244 if ( 'post' === $this->post_type ) { 2237 2245 $query_params['sticky'] = array( 2238 'description' => __( 'Limit result set to items that are sticky.' ),2239 'type' => 'boolean',2246 'description' => __( 'Limit result set to items that are sticky.' ), 2247 'type' => 'boolean', 2240 2248 ); 2241 2249 } … … 2274 2282 2275 2283 // The default status is different in WP_REST_Attachments_Controller 2276 $attributes = $request->get_attributes();2284 $attributes = $request->get_attributes(); 2277 2285 $default_status = $attributes['args']['status']['default']; 2278 2286
Note: See TracChangeset
for help on using the changeset viewer.