GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — feature/gutenberg ( 9bd719...01d718 )
by Brad
52:45
created

FooGallery_Rest_Routes   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 120
rs 10
c 0
b 0
f 0
wmc 8
lcom 0
cbo 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A register_routes() 0 17 1
A get_galleries_permissions_check() 0 13 2
A get_galleries() 0 23 3
A get_item_schema() 0 21 1
1
<?php
2
/**
3
 * class for Rest API Routes within FooGallery
4
 *
5
 * @since 1.6.0
6
 */
7
if ( ! class_exists( 'FooGallery_Rest_Routes' ) ) {
8
9
	class FooGallery_Rest_Routes {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
10
		/**
11
		 * Constructs the class.
12
		 *
13
		 * @access public
14
		 */
15
		public function __construct() {
16
			add_action( 'rest_api_init', array( $this, 'register_routes' ) );
17
		}
18
19
		/**
20
		 * Registers the necessary REST API routes for FooGallery
21
		 *
22
		 * @access public
23
		 */
24
		public function register_routes() {
25
			$nonce = wp_create_nonce( 'wp_rest' );
0 ignored issues
show
Unused Code introduced by
$nonce is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
26
27
28
			register_rest_route(
29
				'foogallery/v1',
30
				'galleries',
31
				array(
32
					'methods'  			  => WP_REST_Server::READABLE,
33
					'callback' 			  => array( $this, 'get_galleries' ),
34
					'permission_callback' => array( $this, 'get_galleries_permissions_check' ),
35
					'schema' 			  => array( $this, 'get_galleries_schema' ),
36
				)
37
			);
38
39
40
		}
41
42
		/**
43
		 * Checks if a given request has access to get galleries.
44
		 *
45
		 * @access public
46
		 *
47
		 * @param WP_REST_Request $request Request.
48
		 *
49
		 * @return true|WP_Error True if the request has read access, WP_Error object otherwise.
50
		 */
51
		public function get_galleries_permissions_check( $request ) {
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
52
			if ( ! current_user_can( 'edit_posts' ) ) {
53
				return new WP_Error(
54
					'foogallery_galleries_cannot_read',
55
					__( 'Sorry, you are not allowed to read galleries as this user.', 'foogallery' ),
56
					array(
57
						'status' => rest_authorization_required_code(),
58
					)
59
				);
60
			}
61
62
			return true;
63
		}
64
65
		/**
66
		 * Returns a list of all galleries.
67
		 *
68
		 * @since  2.8.0
69
		 * @access public
70
		 *
71
		 * @param WP_REST_Request $request Full details about the request.
72
		 *
73
		 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
74
		 */
75
		public function get_galleries( $request ) {
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
76
			$args = array(
77
				'post_type'     => FOOGALLERY_CPT_GALLERY,
78
				'post_status'	=> array( 'publish', 'draft' ),
79
				'cache_results' => false,
80
				'nopaging'      => true,
81
			);
82
83
			$gallery_posts = get_posts( $args );
84
85
			$galleries = array();
86
87
			if ( !empty( $gallery_posts ) ) {
88
				foreach ( $gallery_posts as $post ) {
89
					$galleries[] = array(
90
						'ID' => $post->ID,
91
						'Name' => $post->post_title
92
					);
93
				}
94
			}
95
96
			return rest_ensure_response( $galleries );
97
		}
98
99
		/**
100
		 * Retrieves block's output schema, conforming to JSON Schema.
101
		 *
102
		 * @since  2.8.0
103
		 * @access public
104
		 *
105
		 * @return array Item schema data.
106
		 */
107
		public function get_item_schema() {
108
			return array(
109
				'$schema'    => 'http://json-schema.org/schema#',
110
				'title'      => 'foogallery',
111
				'type'       => 'object',
112
				'properties' => array(
113
					'ID' => array(
114
						'description' => __( 'The FooGallery ID.', 'foogallery' ),
115
						'type'        => 'int',
116
						'required'    => true,
117
						'context'     => array( 'edit' ),
118
					),
119
					'Name' => array(
120
						'description' => __( 'The FooGallery Name.', 'foogallery' ),
121
						'type'        => 'string',
122
						'required'    => false,
123
						'context'     => array( 'edit' ),
124
					),
125
				),
126
			);
127
		}
128
	}
129
}
130