← All changes
|
php/REST_API/Snippets/Snippets_REST_Controller.php
+89
-22
4.0.0-beta.1
→
trunk
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; |
| @@ -207,8 +208,19 @@ | ||
| 207 | 208 | ); |
| 208 | 209 | } |
| 209 | 210 | |
| 210 | 211 | /** |
| 212 | + * Determine whether the request has permission to access snippets. | |
| 213 | + * | |
| 214 | + * @param WP_REST_Request $request Incoming HTTP request. | |
| 215 | + * | |
| 216 | + * @return bool | |
| 217 | + */ | |
| 218 | + public function permission_callback( WP_REST_Request $request ): bool { | |
| 219 | + return code_snippets()->current_user_can(); | |
| 220 | + } | |
| 221 | + | |
| 222 | + /** | |
| 211 | 223 | * Determine whether a request targets network-scoped snippets. |
| 212 | 224 | * |
| 213 | 225 | * Only the literal boolean `true` (or its common string/integer equivalents) |
| 214 | 226 | * is treated as a network-scoped request. A missing or null `network` param |
| @@ -229,9 +241,9 @@ | ||
| 229 | 241 | return $network; |
| 230 | 242 | } |
| 231 | 243 | |
| 232 | 244 | if ( is_string( $network ) ) { |
| 233 | - return in_array( strtolower( $network ), [ '1', 'true', 'yes' ], true ); | |
| 245 | + return ! in_array( strtolower( $network ), [ '0', 'false', 'no', '' ], true ); | |
| 234 | 246 | } |
| 235 | 247 | |
| 236 | 248 | return (bool) $network; |
| 237 | 249 | } |
| @@ -461,12 +473,12 @@ | ||
| 461 | 473 | |
| 462 | 474 | /** |
| 463 | 475 | * Retrieve and merge shared network snippets. |
| 464 | 476 | * |
| 465 | - * @param array<Snippet> $all_snippets List of snippets to merge with. | |
| 466 | - * @param bool|null $network Whether fetching network snippets. | |
| 477 | + * @param Snippet[] $all_snippets List of snippets to merge with. | |
| 478 | + * @param bool|null $network Whether fetching network snippets. | |
| 467 | 479 | * |
| 468 | - * @return array<Snippet> Modified list of snippets. | |
| 480 | + * @return Snippet[] Modified list of snippets. | |
| 469 | 481 | */ |
| 470 | 482 | private function get_network_items( array $all_snippets, ?bool $network ): array { |
| 471 | 483 | if ( ! is_multisite() || $network ) { |
| 472 | 484 | return $all_snippets; |
| @@ -620,18 +632,21 @@ | ||
| 620 | 632 | ); |
| 621 | 633 | } |
| 622 | 634 | |
| 623 | 635 | /** |
| 624 | - * Activate one item in the collection. | |
| 636 | + * Fetch snippet using data from request. | |
| 625 | 637 | * |
| 626 | - * @param WP_REST_Request $request Full data about the request. | |
| 638 | + * @param WP_REST_Request $request Request containing 'id' and 'network' parameters. | |
| 627 | 639 | * |
| 628 | - * @return WP_Error|WP_REST_Response | |
| 640 | + * @return Snippet|WP_Error | |
| 629 | 641 | */ |
| 630 | - public function activate_item( WP_REST_Request $request ) { | |
| 631 | - $item = $this->prepare_item_for_database( $request ); | |
| 632 | - $snippet = $item ? get_snippet( $item->id, $item->network ) : null; | |
| 642 | + private function get_requested_snippet( WP_REST_Request $request ) { | |
| 643 | + $id = $request->get_param( 'id' ); | |
| 633 | 644 | |
| 645 | + $snippet = $id && is_numeric( $id ) | |
| 646 | + ? get_snippet( $id, $request->get_param( 'network' ) ) | |
| 647 | + : null; | |
| 648 | + | |
| 634 | 649 | if ( ! $snippet || ! $snippet->id ) { |
| 635 | 650 | return new WP_Error( |
| 636 | 651 | 'rest_cannot_activate', |
| 637 | 652 | __( 'The snippet could not be found.', 'code-snippets' ), |
| @@ -638,8 +653,25 @@ | ||
| 638 | 653 | [ 'status' => 404 ] |
| 639 | 654 | ); |
| 640 | 655 | } |
| 641 | 656 | |
| 657 | + return $snippet; | |
| 658 | + } | |
| 659 | + | |
| 660 | + /** | |
| 661 | + * Activate one item in the collection. | |
| 662 | + * | |
| 663 | + * @param WP_REST_Request $request Full data about the request. | |
| 664 | + * | |
| 665 | + * @return WP_Error|WP_REST_Response | |
| 666 | + */ | |
| 667 | + public function activate_item( WP_REST_Request $request ) { | |
| 668 | + $snippet = $this->get_requested_snippet( $request ); | |
| 669 | + | |
| 670 | + if ( is_wp_error( $snippet ) ) { | |
| 671 | + return rest_ensure_response( $snippet ); | |
| 672 | + } | |
| 673 | + | |
| 642 | 674 | if ( $snippet->shared_network ) { |
| 643 | 675 | $this->set_shared_network_active( $snippet->id, true ); |
| 644 | 676 | $snippet->active = true; |
| 645 | 677 | return rest_ensure_response( $snippet ); |
| @@ -663,17 +695,12 @@ | ||
| 663 | 695 | * |
| 664 | 696 | * @return WP_Error|WP_REST_Response |
| 665 | 697 | */ |
| 666 | 698 | public function deactivate_item( WP_REST_Request $request ) { |
| 667 | - $item = $this->prepare_item_for_database( $request ); | |
| 668 | - $snippet = $item ? get_snippet( $item->id, $item->network ) : null; | |
| 699 | + $snippet = $this->get_requested_snippet( $request ); | |
| 669 | 700 | |
| 670 | - if ( ! $snippet || ! $snippet->id ) { | |
| 671 | - return new WP_Error( | |
| 672 | - 'rest_cannot_activate', | |
| 673 | - __( 'The snippet could not be found.', 'code-snippets' ), | |
| 674 | - [ 'status' => 404 ] | |
| 675 | - ); | |
| 701 | + if ( is_wp_error( $snippet ) ) { | |
| 702 | + return rest_ensure_response( $snippet ); | |
| 676 | 703 | } |
| 677 | 704 | |
| 678 | 705 | if ( $snippet->shared_network ) { |
| 679 | 706 | $this->set_shared_network_active( $snippet->id, false ); |
| @@ -793,14 +820,33 @@ | ||
| 793 | 820 | * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
| 794 | 821 | */ |
| 795 | 822 | public function prepare_item_for_response( $item, $request ) { |
| 796 | 823 | $schema = $this->get_item_schema(); |
| 797 | - $response = []; | |
| 798 | 824 | |
| 799 | - foreach ( array_keys( $schema['properties'] ) as $property ) { | |
| 800 | - $response[ $property ] = $item->$property; | |
| 801 | - } | |
| 825 | + $properties = array_keys( $schema['properties'] ); | |
| 802 | 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 | + | |
| 803 | 849 | return rest_ensure_response( $response ); |
| 804 | 850 | } |
| 805 | 851 | |
| 806 | 852 | /** |
| @@ -856,8 +902,9 @@ | ||
| 856 | 902 | ], |
| 857 | 903 | 'trashed' => [ |
| 858 | 904 | 'description' => esc_html__( 'Whether the snippet is marked as deleted.', 'code-snippets' ), |
| 859 | 905 | 'type' => 'boolean', |
| 906 | + 'readonly' => true, | |
| 860 | 907 | ], |
| 861 | 908 | 'locked' => [ |
| 862 | 909 | 'description' => esc_html__( 'Whether the snippet is locked from modification or deletion.', 'code-snippets' ), |
| 863 | 910 | 'type' => 'boolean', |
| @@ -897,8 +944,28 @@ | ||
| 897 | 944 | 'code_error_trace' => [ |
| 898 | 945 | 'description' => esc_html__( 'Stack trace for the most recent snippet code error.', 'code-snippets' ), |
| 899 | 946 | 'type' => [ 'string', 'null' ], |
| 900 | 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 | + ], | |
| 901 | 968 | ], |
| 902 | 969 | ], |
| 903 | 970 | ]; |
| 904 | 971 | |