PluginProbe
Code Snippets / 3.1.0
Code Snippets v3.1.0
3.10.2 3.10.1 3.10.0 3.10.0-beta.2 3.10.0-beta.1 4.0.0-beta.1 3.9.6 trunk 2.10.0 2.10.1 2.12.0 2.12.1 2.13.0 2.13.1 2.13.2 2.13.3 2.14.0 2.14.1 2.14.2 2.14.3 2.14.4 2.14.5 2.14.6 3.0.0 3.0.1 All 64 releases
code-snippets / php / class-rest-api.php

class-rest-api.php in Code Snippets 3.1.0, at php/class-rest-api.php

75 lines 1.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Code_Snippets;
4
5 use WP_REST_Request;
6 use WP_REST_Response;
7 use WP_REST_Server;
8
9 /**
10 * Allows fetching snippet data through the WordPress REST API.
11 *
12 * @since 3.0.0
13 * @package Code_Snippets
14 */
15 class REST_API {
16
17 /**
18 * Current API version.
19 */
20 const VERSION = 1;
21
22 /**
23 * Namespace.
24 */
25 const BASE = 'code-snippets/v' . self::VERSION;
26
27 /**
28 * Class constructor.
29 */
30 public function __construct() {
31 add_action( 'rest_api_init', array( $this, 'register_routes' ) );
32 }
33
34 /**
35 * Register REST routes.
36 */
37 public function register_routes() {
38 register_rest_route(
39 self::BASE,
40 '/snippets-info',
41 array(
42 'methods' => WP_REST_Server::READABLE,
43 'callback' => [ $this, 'get_snippets_info' ],
44 'permission_callback' => function () {
45 return current_user_can( 'edit_posts' );
46 },
47 )
48 );
49 }
50
51 /**
52 * Fetch snippet data in response to a request.
53 *
54 * @param WP_REST_Request $request Request object.
55 *
56 * @return WP_REST_Response
57 */
58 public function get_snippets_info( WP_REST_Request $request ) {
59 $snippets = get_snippets();
60 $data = [];
61
62 /** Snippet @var Snippet $snippet */
63 foreach ( $snippets as $snippet ) {
64 $data[] = [
65 'id' => $snippet->id,
66 'name' => $snippet->name,
67 'type' => $snippet->type,
68 'active' => $snippet->active,
69 ];
70 }
71
72 return new WP_REST_Response( $data, 200 );
73 }
74 }
75