Completed
Push — develop ( 72a91d...14a321 )
by
unknown
04:09 queued 12s
created

Entity_Rest_Endpoint::reject_entity()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 9
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 9
loc 9
rs 9.9666
c 0
b 0
f 0
1
<?php
2
/**
3
 * @since 1.0.0
4
 * @author Naveen Muthusamy <[email protected]>
5
 */
6
7
namespace Wordlift\Vocabulary\Api;
8
9
use Wordlift\Cache\Ttl_Cache;
10
use Wordlift\Vocabulary\Data\Entity_List\Entity_List_Factory;
11
use WP_REST_Server;
12
13
/**
14
 * @since 1.0.0
15
 * @author Naveen Muthusamy <[email protected]>
16
 */
17
class Entity_Rest_Endpoint {
18
19
	const SAME_AS_META_KEY = 'entity_same_as';
20
	const ALTERNATIVE_LABEL_META_KEY = '_wl_alt_label';
21
	const DESCRIPTION_META_KEY = 'entity_description';
22
	const TYPE_META_KEY = 'entity_type';
23
	const EXTERNAL_ENTITY_META_KEY = '_wl_is_external';
24
	const IGNORE_TAG_FROM_LISTING = '_wl_cmkg_ignore_tag_from_ui';
25
26
27
	public function register_routes() {
28
		$that = $this;
29
		add_action( 'rest_api_init',
30
			function () use ( $that ) {
31
				$that->register_accept_route();
32
				$that->register_undo_route();
33
				$that->register_nomatch_route();
34
				$that->register_reject_route();
35
			} );
36
	}
37
38
39 View Code Duplication
	public function accept_entity( $request ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
40
		$data        = $request->get_params();
41
		$term_id     = (int) $data['term_id'];
42
		$entity_data = (array) $data['entity'];
43
		$entity      = Entity_List_Factory::get_instance( $term_id );
44
		$entity->save_jsonld_data( $entity_data );
45
		update_term_meta( $term_id, self::IGNORE_TAG_FROM_LISTING, 1 );
46
		Ttl_Cache::flush_all();
47
48
		return $term_id;
49
	}
50
51
	public function undo( $request ) {
52
		$data    = $request->get_params();
53
		$term_id = (int) $data['term_id'];
54
		$entity  = Entity_List_Factory::get_instance( $term_id );
55
		$entity->clear_data();
56
		delete_term_meta( $term_id, self::IGNORE_TAG_FROM_LISTING );
57
		Ttl_Cache::flush_all();
58
59
		return $term_id;
60
	}
61
62
63
	public function mark_as_no_match( $request ) {
64
		$data    = $request->get_params();
65
		$term_id = (int) $data['term_id'];
66
		Ttl_Cache::flush_all();
67
68
		return update_term_meta( $term_id, self::IGNORE_TAG_FROM_LISTING, 1 );
69
	}
70
71
72 View Code Duplication
	private function register_undo_route() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
73
		register_rest_route(
74
			Api_Config::REST_NAMESPACE,
75
			'/entity/undo',
76
			array(
77
				'methods'             => WP_REST_Server::CREATABLE,
78
				'callback'            => array( $this, 'undo' ),
79
				'permission_callback' => function () {
80
					return current_user_can( 'manage_options' );
81
				},
82
				'args'                => array(
83
					'term_id' => array(
84
						'validate_callback' => function ( $param, $request, $key ) {
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...
Unused Code introduced by
The parameter $key 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...
85
							return is_numeric( $param ) && $param;
86
						},
87
						'required'          => true,
88
					),
89
				),
90
			)
91
		);
92
	}
93
94
	private function register_accept_route() {
95
		$route = '/entity/accept';
96
		$this->register_entity_accept_or_reject_route( $route, array( $this, 'accept_entity' ) );
97
	}
98
99
	private function register_reject_route() {
100
		$route = '/entity/reject';
101
		$this->register_entity_accept_or_reject_route( $route, array( $this, 'reject_entity' ) );
102
	}
103
104 View Code Duplication
	public function reject_entity( $request ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
105
		$data        = $request->get_params();
106
		$term_id     = (int) $data['term_id'];
107
		$entity_data = (array) $data['entity'];
108
		$entity      = Entity_List_Factory::get_instance( $term_id );
109
		$entity->remove_entity_by_id( $entity_data['@id'] );
110
		Ttl_Cache::flush_all();
111
		return $term_id;
112
	}
113
114 View Code Duplication
	private function register_nomatch_route() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
115
		register_rest_route(
116
			Api_Config::REST_NAMESPACE,
117
			'/entity/no_match',
118
			array(
119
				'methods'             => WP_REST_Server::CREATABLE,
120
				'callback'            => array( $this, 'mark_as_no_match' ),
121
				'permission_callback' => function () {
122
					return current_user_can( 'manage_options' );
123
				},
124
				'args'                => array(
125
					'term_id' => array(
126
						'validate_callback' => function ( $param, $request, $key ) {
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...
Unused Code introduced by
The parameter $key 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...
127
							return is_numeric( $param ) && $param;
128
						},
129
						'required'          => true,
130
					),
131
				),
132
			)
133
		);
134
	}
135
136
137
	/**
138
	 * @param $accept_route
139
	 */
140
	private function register_entity_accept_or_reject_route( $accept_route, $callback ) {
141
142
		register_rest_route(
143
			Api_Config::REST_NAMESPACE,
144
			$accept_route,
145
			array(
146
				'methods'             => WP_REST_Server::CREATABLE,
147
				'callback'            => $callback,
148
				'permission_callback' => function () {
149
					return current_user_can( 'manage_options' );
150
				},
151
				'args'                => array(
152
					'term_id' => array(
153
						'validate_callback' => function ( $param, $request, $key ) {
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...
Unused Code introduced by
The parameter $key 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...
154
							return is_numeric( $param ) && $param;
155
						},
156
						'required'          => true,
157
					),
158
					'entity'  => array(
159
						'validate_callback' => function ( $param, $request, $key ) {
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...
Unused Code introduced by
The parameter $key 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...
160
							return is_array( $param );
161
						},
162
						'required'          => true,
163
					),
164
				),
165
			)
166
		);
167
168
	}
169
170
}