| @@ 894-951 (lines=58) @@ | ||
| 891 | * @param WP_REST_Request $request Full details about the request. |
|
| 892 | * @return array Fields to be included in the response. |
|
| 893 | */ |
|
| 894 | public function get_fields_for_response( $request ) { |
|
| 895 | $schema = $this->get_item_schema(); |
|
| 896 | $properties = isset( $schema['properties'] ) ? $schema['properties'] : array(); |
|
| 897 | ||
| 898 | $additional_fields = $this->get_additional_fields(); |
|
| 899 | foreach ( $additional_fields as $field_name => $field_options ) { |
|
| 900 | // For back-compat, include any field with an empty schema |
|
| 901 | // because it won't be present in $this->get_item_schema(). |
|
| 902 | if ( is_null( $field_options['schema'] ) ) { |
|
| 903 | $properties[ $field_name ] = $field_options; |
|
| 904 | } |
|
| 905 | } |
|
| 906 | ||
| 907 | // Exclude fields that specify a different context than the request context. |
|
| 908 | $context = $request['context']; |
|
| 909 | if ( $context ) { |
|
| 910 | foreach ( $properties as $name => $options ) { |
|
| 911 | if ( ! empty( $options['context'] ) && ! in_array( $context, $options['context'], true ) ) { |
|
| 912 | unset( $properties[ $name ] ); |
|
| 913 | } |
|
| 914 | } |
|
| 915 | } |
|
| 916 | ||
| 917 | $fields = array_keys( $properties ); |
|
| 918 | ||
| 919 | if ( ! isset( $request['_fields'] ) ) { |
|
| 920 | return $fields; |
|
| 921 | } |
|
| 922 | $requested_fields = wpinv_parse_list( $request['_fields'] ); |
|
| 923 | if ( 0 === count( $requested_fields ) ) { |
|
| 924 | return $fields; |
|
| 925 | } |
|
| 926 | // Trim off outside whitespace from the comma delimited list. |
|
| 927 | $requested_fields = array_map( 'trim', $requested_fields ); |
|
| 928 | // Always persist 'id', because it can be needed for add_additional_fields_to_object(). |
|
| 929 | if ( in_array( 'id', $fields, true ) ) { |
|
| 930 | $requested_fields[] = 'id'; |
|
| 931 | } |
|
| 932 | // Return the list of all requested fields which appear in the schema. |
|
| 933 | return array_reduce( |
|
| 934 | $requested_fields, |
|
| 935 | function( $response_fields, $field ) use ( $fields ) { |
|
| 936 | if ( in_array( $field, $fields, true ) ) { |
|
| 937 | $response_fields[] = $field; |
|
| 938 | return $response_fields; |
|
| 939 | } |
|
| 940 | // Check for nested fields if $field is not a direct match. |
|
| 941 | $nested_fields = explode( '.', $field ); |
|
| 942 | // A nested field is included so long as its top-level property is |
|
| 943 | // present in the schema. |
|
| 944 | if ( in_array( $nested_fields[0], $fields, true ) ) { |
|
| 945 | $response_fields[] = $field; |
|
| 946 | } |
|
| 947 | return $response_fields; |
|
| 948 | }, |
|
| 949 | array() |
|
| 950 | ); |
|
| 951 | } |
|
| 952 | ||
| 953 | /** |
|
| 954 | * Retrieves the item's schema, conforming to JSON Schema. |
|
| @@ 868-925 (lines=58) @@ | ||
| 865 | * @param WP_REST_Request $request Full details about the request. |
|
| 866 | * @return array Fields to be included in the response. |
|
| 867 | */ |
|
| 868 | public function get_fields_for_response( $request ) { |
|
| 869 | $schema = $this->get_item_schema(); |
|
| 870 | $properties = isset( $schema['properties'] ) ? $schema['properties'] : array(); |
|
| 871 | ||
| 872 | $additional_fields = $this->get_additional_fields(); |
|
| 873 | foreach ( $additional_fields as $field_name => $field_options ) { |
|
| 874 | // For back-compat, include any field with an empty schema |
|
| 875 | // because it won't be present in $this->get_item_schema(). |
|
| 876 | if ( is_null( $field_options['schema'] ) ) { |
|
| 877 | $properties[ $field_name ] = $field_options; |
|
| 878 | } |
|
| 879 | } |
|
| 880 | ||
| 881 | // Exclude fields that specify a different context than the request context. |
|
| 882 | $context = $request['context']; |
|
| 883 | if ( $context ) { |
|
| 884 | foreach ( $properties as $name => $options ) { |
|
| 885 | if ( ! empty( $options['context'] ) && ! in_array( $context, $options['context'], true ) ) { |
|
| 886 | unset( $properties[ $name ] ); |
|
| 887 | } |
|
| 888 | } |
|
| 889 | } |
|
| 890 | ||
| 891 | $fields = array_keys( $properties ); |
|
| 892 | ||
| 893 | if ( ! isset( $request['_fields'] ) ) { |
|
| 894 | return $fields; |
|
| 895 | } |
|
| 896 | $requested_fields = wpinv_parse_list( $request['_fields'] ); |
|
| 897 | if ( 0 === count( $requested_fields ) ) { |
|
| 898 | return $fields; |
|
| 899 | } |
|
| 900 | // Trim off outside whitespace from the comma delimited list. |
|
| 901 | $requested_fields = array_map( 'trim', $requested_fields ); |
|
| 902 | // Always persist 'id', because it can be needed for add_additional_fields_to_object(). |
|
| 903 | if ( in_array( 'id', $fields, true ) ) { |
|
| 904 | $requested_fields[] = 'id'; |
|
| 905 | } |
|
| 906 | // Return the list of all requested fields which appear in the schema. |
|
| 907 | return array_reduce( |
|
| 908 | $requested_fields, |
|
| 909 | function( $response_fields, $field ) use ( $fields ) { |
|
| 910 | if ( in_array( $field, $fields, true ) ) { |
|
| 911 | $response_fields[] = $field; |
|
| 912 | return $response_fields; |
|
| 913 | } |
|
| 914 | // Check for nested fields if $field is not a direct match. |
|
| 915 | $nested_fields = explode( '.', $field ); |
|
| 916 | // A nested field is included so long as its top-level property is |
|
| 917 | // present in the schema. |
|
| 918 | if ( in_array( $nested_fields[0], $fields, true ) ) { |
|
| 919 | $response_fields[] = $field; |
|
| 920 | } |
|
| 921 | return $response_fields; |
|
| 922 | }, |
|
| 923 | array() |
|
| 924 | ); |
|
| 925 | } |
|
| 926 | ||
| 927 | /** |
|
| 928 | * Retrieves the invoice's schema, conforming to JSON Schema. |
|