← All changes
|
php/REST_API/Snippets/Snippets_REST_Controller.php
+45
-4
3.10.0
→
4.0.0-beta.2
View file →
| @@ -15,8 +15,9 @@ | ||
| 15 | 15 | use function Code_Snippets\clean_active_snippets_cache; |
| 16 | 16 | use function Code_Snippets\code_snippets; |
| 17 | 17 | use function Code_Snippets\deactivate_snippet; |
| 18 | 18 | use function Code_Snippets\delete_snippet; |
| 19 | +use function Code_Snippets\get_snippet_author; | |
| 19 | 20 | use function Code_Snippets\get_snippet; |
| 20 | 21 | use function Code_Snippets\get_snippets; |
| 21 | 22 | use function Code_Snippets\restore_snippet; |
| 22 | 23 | use function Code_Snippets\save_snippet; |
| @@ -819,14 +820,33 @@ | ||
| 819 | 820 | * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
| 820 | 821 | */ |
| 821 | 822 | public function prepare_item_for_response( $item, $request ) { |
| 822 | 823 | $schema = $this->get_item_schema(); |
| 823 | - $response = []; | |
| 824 | 824 | |
| 825 | - foreach ( array_keys( $schema['properties'] ) as $property ) { | |
| 826 | - $response[ $property ] = $item->$property; | |
| 827 | - } | |
| 825 | + $properties = array_keys( $schema['properties'] ); | |
| 828 | 826 | |
| 827 | + $response_data = array_map( | |
| 828 | + function ( $property ) use ( $item ) { | |
| 829 | + switch ( $property ) { | |
| 830 | + case 'created_by': | |
| 831 | + return get_snippet_author( $item->created_by ); | |
| 832 | + | |
| 833 | + case 'updated_by': | |
| 834 | + return get_snippet_author( $item->updated_by ); | |
| 835 | + | |
| 836 | + default: | |
| 837 | + return $item->$property; | |
| 838 | + } | |
| 839 | + }, | |
| 840 | + $properties | |
| 841 | + ); | |
| 842 | + | |
| 843 | + $response = array_combine( $properties, $response_data ); | |
| 844 | + | |
| 845 | + // The schema declares this as a date-time, so send one: the stored value | |
| 846 | + // is UTC without an offset, which clients read as local time. | |
| 847 | + $response['modified'] = $item->modified_iso; | |
| 848 | + | |
| 829 | 849 | return rest_ensure_response( $response ); |
| 830 | 850 | } |
| 831 | 851 | |
| 832 | 852 | /** |
| @@ -882,8 +902,9 @@ | ||
| 882 | 902 | ], |
| 883 | 903 | 'trashed' => [ |
| 884 | 904 | 'description' => esc_html__( 'Whether the snippet is marked as deleted.', 'code-snippets' ), |
| 885 | 905 | 'type' => 'boolean', |
| 906 | + 'readonly' => true, | |
| 886 | 907 | ], |
| 887 | 908 | 'locked' => [ |
| 888 | 909 | 'description' => esc_html__( 'Whether the snippet is locked from modification or deletion.', 'code-snippets' ), |
| 889 | 910 | 'type' => 'boolean', |
| @@ -923,8 +944,28 @@ | ||
| 923 | 944 | 'code_error_trace' => [ |
| 924 | 945 | 'description' => esc_html__( 'Stack trace for the most recent snippet code error.', 'code-snippets' ), |
| 925 | 946 | 'type' => [ 'string', 'null' ], |
| 926 | 947 | 'readonly' => true, |
| 948 | + ], | |
| 949 | + 'created_by' => [ | |
| 950 | + 'description' => esc_html__( 'The snippet author.', 'code-snippets' ), | |
| 951 | + 'type' => [ 'object', 'null' ], | |
| 952 | + 'readonly' => true, | |
| 953 | + 'properties' => [ | |
| 954 | + 'id' => [ 'type' => 'integer' ], | |
| 955 | + 'display_name' => [ 'type' => 'string' ], | |
| 956 | + 'avatar_url' => [ 'type' => 'string' ], | |
| 957 | + ], | |
| 958 | + ], | |
| 959 | + 'updated_by' => [ | |
| 960 | + 'description' => esc_html__( 'The most recent editor.', 'code-snippets' ), | |
| 961 | + 'type' => [ 'object', 'null' ], | |
| 962 | + 'readonly' => true, | |
| 963 | + 'properties' => [ | |
| 964 | + 'id' => [ 'type' => 'integer' ], | |
| 965 | + 'display_name' => [ 'type' => 'string' ], | |
| 966 | + 'avatar_url' => [ 'type' => 'string' ], | |
| 967 | + ], | |
| 927 | 968 | ], |
| 928 | 969 | ], |
| 929 | 970 | ]; |
| 930 | 971 | |