Plugin Directory

source: nextgen-gallery/trunk/src/REST/Admin/Block.php

Last change on this file was 3157076, checked in by poxtron, 7 months ago

Release 3.59.5

File size: 2.2 KB
Line 
1<?php
2/**
3 * REST API for the NextGEN Gallery Block
4 *
5 * @package NextGEN Gallery
6 */
7
8namespace Imagely\NGG\REST\Admin;
9
10use Imagely\NGG\DataMappers\Image as ImageMapper;
11use Imagely\NGG\DataStorage\Manager as StorageManager;
12
13/**
14 * Class Block represents the REST API for the NextGEN Gallery Block
15 */
16class Block extends \WP_REST_Controller {
17
18        /**
19         * Block constructor.
20         */
21        public function __construct() {
22                $this->namespace = 'ngg/v1';
23                $this->rest_base = 'admin/block/image';
24        }
25
26        /**
27         * Register the routes for the objects of the controller.
28         */
29        public function register_routes() {
30                \register_rest_route(
31                        $this->namespace,
32                        '/' . $this->rest_base . '/(?P<image_id>.*)/',
33                        [
34                                'args' => [
35                                        'image_id' => [
36                                                'description' => \__( 'Image ID', 'nggallery' ),
37                                                'type'        => 'integer',
38                                                'required'    => true,
39                                        ],
40                                ],
41                                [
42                                        'methods'             => \WP_REST_Server::READABLE,
43                                        'callback'            => [ $this, 'get_item' ],
44                                        'permission_callback' => [ $this, 'get_item_permissions_check' ],
45                                ],
46                        ]
47                );
48        }
49
50        /**
51         * Check if a given request has access to get information about a specific item.
52         *
53         * @param \WP_REST_Request $request Full data about the request.
54         *
55         * @return bool
56         */
57        public function get_item_permissions_check( $request ): bool {
58                // Verify the nonce.
59                $nonce = $request->get_header('X-WP-Nonce');
60
61                if ( ! $nonce || ! wp_verify_nonce( $nonce, 'wp_rest' ) ) {
62                        return false;
63                }
64
65                // Check if the user has the capability to edit posts.
66                return current_user_can( 'edit_posts' );
67        }
68
69        /** Get the specific image.
70         *
71         * @param \WP_REST_Request $request Full data about the request.
72         * @return \WP_Error|\WP_REST_Response
73         */
74        public function get_item( $request ) {
75                $id = $request->get_param( 'image_id' );
76
77                $image = ImageMapper::get_instance()->find( $id );
78
79                if ( ! $image ) {
80                        return new \WP_Error(
81                                'invalid_image_id',
82                                'Invalid image ID',
83                                [ 'status' => 404 ]
84                        );
85                }
86
87                $storage              = StorageManager::get_instance();
88                $image->thumbnail_url = $storage->get_image_url( $image, 'thumb' );
89                $image->image_url     = $storage->get_image_url( $image, 'full' );
90
91                return new \WP_REST_Response(
92                        [
93                                'success' => true,
94                                'image'   => $image,
95                        ]
96                );
97        }
98}
Note: See TracBrowser for help on using the repository browser.