~canonical-sysadmins/wordpress/5.1

« back to all changes in this revision

Viewing changes to wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

  • Committer: Jamon Camisso
  • Date: 2017-01-12 15:30:45 UTC
  • mto: This revision was merged to the branch mainline in revision 25.
  • Revision ID: jamon.camisso@canonical.com-20170112153045-dekfwsu8mcsdxa7x
New upstream release 4.7.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
77
77
                        'schema' => array( $this, 'get_public_item_schema' ),
78
78
                ) );
79
79
 
 
80
                $schema = $this->get_item_schema();
 
81
                $get_item_args = array(
 
82
                        'context'  => $this->get_context_param( array( 'default' => 'view' ) ),
 
83
                );
 
84
                if ( isset( $schema['properties']['password'] ) ) {
 
85
                        $get_item_args['password'] = array(
 
86
                                'description' => __( 'The password for the post if it is password protected.' ),
 
87
                                'type'        => 'string',
 
88
                        );
 
89
                }
80
90
                register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', array(
81
91
                        array(
82
92
                                'methods'             => WP_REST_Server::READABLE,
83
93
                                'callback'            => array( $this, 'get_item' ),
84
94
                                'permission_callback' => array( $this, 'get_item_permissions_check' ),
85
 
                                'args'                => array(
86
 
                                        'context'  => $this->get_context_param( array( 'default' => 'view' ) ),
87
 
                                        'password' => array(
88
 
                                                'description' => __( 'The password for the post if it is password protected.' ),
89
 
                                                'type'        => 'string',
90
 
                                        ),
91
 
                                ),
 
95
                                'args'                => $get_item_args,
92
96
                        ),
93
97
                        array(
94
98
                                'methods'             => WP_REST_Server::EDITABLE,
1919
1923
 
1920
1924
                                case 'post-formats':
1921
1925
                                        $supports_formats = get_theme_support( 'post-formats' );
 
1926
 
 
1927
                                        // Force to an array. Supports formats can return true even if empty in some cases.
 
1928
                                        $supports_formats = is_array( $supports_formats ) ? array_values( $supports_formats[0] ) : array();
 
1929
 
 
1930
                                        $supported_formats = array_merge( array( 'standard' ), $supports_formats );
 
1931
 
1922
1932
                                        $schema['properties']['format'] = array(
1923
1933
                                                'description' => __( 'The format for the object.' ),
1924
1934
                                                'type'        => 'string',
1925
 
                                                'enum'        => array_merge( array( 'standard' ), $supports_formats ? array_values( $supports_formats[0] ) : array() ),
 
1935
                                                'enum'        => $supported_formats,
1926
1936
                                                'context'     => array( 'view', 'edit' ),
1927
1937
                                        );
1928
1938
                                        break;
1975
1985
         * @return array Collection parameters.
1976
1986
         */
1977
1987
        public function get_collection_params() {
1978
 
                $params = parent::get_collection_params();
1979
 
 
1980
 
                $params['context']['default'] = 'view';
1981
 
 
1982
 
                $params['after'] = array(
 
1988
                $query_params = parent::get_collection_params();
 
1989
 
 
1990
                $query_params['context']['default'] = 'view';
 
1991
 
 
1992
                $query_params['after'] = array(
1983
1993
                        'description'        => __( 'Limit response to posts published after a given ISO8601 compliant date.' ),
1984
1994
                        'type'               => 'string',
1985
1995
                        'format'             => 'date-time',
1986
1996
                );
1987
1997
 
1988
1998
                if ( post_type_supports( $this->post_type, 'author' ) ) {
1989
 
                        $params['author'] = array(
 
1999
                        $query_params['author'] = array(
1990
2000
                                'description'         => __( 'Limit result set to posts assigned to specific authors.' ),
1991
2001
                                'type'                => 'array',
1992
2002
                                'items'               => array(
1994
2004
                                ),
1995
2005
                                'default'             => array(),
1996
2006
                        );
1997
 
                        $params['author_exclude'] = array(
 
2007
                        $query_params['author_exclude'] = array(
1998
2008
                                'description'         => __( 'Ensure result set excludes posts assigned to specific authors.' ),
1999
2009
                                'type'                => 'array',
2000
2010
                                'items'               => array(
2004
2014
                        );
2005
2015
                }
2006
2016
 
2007
 
                $params['before'] = array(
 
2017
                $query_params['before'] = array(
2008
2018
                        'description'        => __( 'Limit response to posts published before a given ISO8601 compliant date.' ),
2009
2019
                        'type'               => 'string',
2010
2020
                        'format'             => 'date-time',
2011
2021
                );
2012
2022
 
2013
 
                $params['exclude'] = array(
 
2023
                $query_params['exclude'] = array(
2014
2024
                        'description'        => __( 'Ensure result set excludes specific IDs.' ),
2015
2025
                        'type'               => 'array',
2016
2026
                        'items'              => array(
2019
2029
                        'default'            => array(),
2020
2030
                );
2021
2031
 
2022
 
                $params['include'] = array(
 
2032
                $query_params['include'] = array(
2023
2033
                        'description'        => __( 'Limit result set to specific IDs.' ),
2024
2034
                        'type'               => 'array',
2025
2035
                        'items'              => array(
2029
2039
                );
2030
2040
 
2031
2041
                if ( 'page' === $this->post_type || post_type_supports( $this->post_type, 'page-attributes' ) ) {
2032
 
                        $params['menu_order'] = array(
 
2042
                        $query_params['menu_order'] = array(
2033
2043
                                'description'        => __( 'Limit result set to posts with a specific menu_order value.' ),
2034
2044
                                'type'               => 'integer',
2035
2045
                        );
2036
2046
                }
2037
2047
 
2038
 
                $params['offset'] = array(
 
2048
                $query_params['offset'] = array(
2039
2049
                        'description'        => __( 'Offset the result set by a specific number of items.' ),
2040
2050
                        'type'               => 'integer',
2041
2051
                );
2042
2052
 
2043
 
                $params['order'] = array(
 
2053
                $query_params['order'] = array(
2044
2054
                        'description'        => __( 'Order sort attribute ascending or descending.' ),
2045
2055
                        'type'               => 'string',
2046
2056
                        'default'            => 'desc',
2047
2057
                        'enum'               => array( 'asc', 'desc' ),
2048
2058
                );
2049
2059
 
2050
 
                $params['orderby'] = array(
 
2060
                $query_params['orderby'] = array(
2051
2061
                        'description'        => __( 'Sort collection by object attribute.' ),
2052
2062
                        'type'               => 'string',
2053
2063
                        'default'            => 'date',
2062
2072
                );
2063
2073
 
2064
2074
                if ( 'page' === $this->post_type || post_type_supports( $this->post_type, 'page-attributes' ) ) {
2065
 
                        $params['orderby']['enum'][] = 'menu_order';
 
2075
                        $query_params['orderby']['enum'][] = 'menu_order';
2066
2076
                }
2067
2077
 
2068
 
                $post_type_obj = get_post_type_object( $this->post_type );
 
2078
                $post_type = get_post_type_object( $this->post_type );
2069
2079
 
2070
 
                if ( $post_type_obj->hierarchical || 'attachment' === $this->post_type ) {
2071
 
                        $params['parent'] = array(
 
2080
                if ( $post_type->hierarchical || 'attachment' === $this->post_type ) {
 
2081
                        $query_params['parent'] = array(
2072
2082
                                'description'       => __( 'Limit result set to those of particular parent IDs.' ),
2073
2083
                                'type'              => 'array',
2074
2084
                                'items'             => array(
2076
2086
                                ),
2077
2087
                                'default'           => array(),
2078
2088
                        );
2079
 
                        $params['parent_exclude'] = array(
 
2089
                        $query_params['parent_exclude'] = array(
2080
2090
                                'description'       => __( 'Limit result set to all items except those of a particular parent ID.' ),
2081
2091
                                'type'              => 'array',
2082
2092
                                'items'             => array(
2086
2096
                        );
2087
2097
                }
2088
2098
 
2089
 
                $params['slug'] = array(
 
2099
                $query_params['slug'] = array(
2090
2100
                        'description'       => __( 'Limit result set to posts with one or more specific slugs.' ),
2091
2101
                        'type'              => 'array',
2092
2102
                        'items'             => array(
2095
2105
                        'sanitize_callback' => 'wp_parse_slug_list',
2096
2106
                );
2097
2107
 
2098
 
                $params['status'] = array(
 
2108
                $query_params['status'] = array(
2099
2109
                        'default'           => 'publish',
2100
2110
                        'description'       => __( 'Limit result set to posts assigned one or more statuses.' ),
2101
2111
                        'type'              => 'array',
2111
2121
                foreach ( $taxonomies as $taxonomy ) {
2112
2122
                        $base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name;
2113
2123
 
2114
 
                        $params[ $base ] = array(
 
2124
                        $query_params[ $base ] = array(
2115
2125
                                /* translators: %s: taxonomy name */
2116
2126
                                'description'       => sprintf( __( 'Limit result set to all items that have the specified term assigned in the %s taxonomy.' ), $base ),
2117
2127
                                'type'              => 'array',
2121
2131
                                'default'           => array(),
2122
2132
                        );
2123
2133
 
2124
 
                        $params[ $base . '_exclude' ] = array(
 
2134
                        $query_params[ $base . '_exclude' ] = array(
2125
2135
                                /* translators: %s: taxonomy name */
2126
2136
                                'description' => sprintf( __( 'Limit result set to all items except those that have the specified term assigned in the %s taxonomy.' ), $base ),
2127
2137
                                'type'        => 'array',
2133
2143
                }
2134
2144
 
2135
2145
                if ( 'post' === $this->post_type ) {
2136
 
                        $params['sticky'] = array(
 
2146
                        $query_params['sticky'] = array(
2137
2147
                                'description'       => __( 'Limit result set to items that are sticky.' ),
2138
2148
                                'type'              => 'boolean',
2139
2149
                        );
2151
2161
                 *
2152
2162
                 * @since 4.7.0
2153
2163
                 *
2154
 
                 * @param $params JSON Schema-formatted collection parameters.
2155
 
                 * @param WP_Post_Type $post_type_obj Post type object.
 
2164
                 * @param array        $query_params JSON Schema-formatted collection parameters.
 
2165
                 * @param WP_Post_Type $post_type    Post type object.
2156
2166
                 */
2157
 
                return apply_filters( "rest_{$this->post_type}_collection_params", $params, $post_type_obj );
 
2167
                return apply_filters( "rest_{$this->post_type}_collection_params", $query_params, $post_type );
2158
2168
        }
2159
2169
 
2160
2170
        /**