Completed
Push — add/carousel-lightbox-single-i... ( 204ac6...43c884 )
by
unknown
09:26
created

Grunion_Contact_Form_Endpoint   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 38
Duplicated Lines 57.89 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 22
loc 38
rs 10
c 0
b 0
f 0
wmc 4
lcom 0
cbo 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A get_items_permissions_check() 11 11 2
A get_item_permissions_check() 11 11 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/*
4
 * Plugin Name: Feedback CPT Permissions over-ride
5
 */
6
7
if ( class_exists( 'WP_REST_Posts_Controller' ) ) {
8
9
	/**
10
	 * Class Grunion_Contact_Form_Endpoint
11
	 * Used as 'rest_controller_class' parameter when 'feedback' post type is registered in modules/contact-form/grunion-contact-form.php.
12
	 */
13
	class Grunion_Contact_Form_Endpoint extends WP_REST_Posts_Controller {
14
		/**
15
		 * Check whether a given request has proper authorization to view feedback items.
16
		 *
17
		 * @param  WP_REST_Request $request Full details about the request.
18
		 * @return WP_Error|boolean
19
		 */
20 View Code Duplication
		public function get_items_permissions_check( $request ) {
21
			if ( ! is_user_member_of_blog( get_current_user_id(), get_current_blog_id() ) ) {
22
				return new WP_Error(
23
					'rest_cannot_view',
24
					esc_html__( 'Sorry, you cannot view this resource.', 'jetpack' ),
25
					array( 'status' => 401 )
26
				);
27
			}
28
29
			return true;
30
		}
31
32
		/**
33
		 * Check whether a given request has proper authorization to view feedback item.
34
		 *
35
		 * @param  WP_REST_Request $request Full details about the request.
36
		 * @return WP_Error|boolean
37
		 */
38 View Code Duplication
		public function get_item_permissions_check( $request ) {
39
			if ( ! is_user_member_of_blog( get_current_user_id(), get_current_blog_id() ) ) {
40
				return new WP_Error(
41
					'rest_cannot_view',
42
					esc_html__( 'Sorry, you cannot view this resource.', 'jetpack' ),
43
					array( 'status' => 401 )
44
				);
45
			}
46
47
			return true;
48
		}
49
50
	}
51
52
}
53