Completed
Pull Request — develop (#1328)
by Naveen
03:11
created

Entity_Rest_Endpoint::accept_entity()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 1
dl 0
loc 33
rs 9.392
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 WP_REST_Server;
10
11
/**
12
 * @since 1.0.0
13
 * @author Naveen Muthusamy <[email protected]>
14
 */
15
class Entity_Rest_Endpoint {
16
17
	const SAME_AS_META_KEY = 'entity_same_as';
18
	const ALTERNATIVE_LABEL_META_KEY = '_wl_alt_label';
19
	const DESCRIPTION_META_KEY = 'entity_description';
20
	const TYPE_META_KEY = 'entity_type';
21
	const EXTERNAL_ENTITY_META_KEY = '_wl_is_external';
22
	const IGNORE_TAG_FROM_LISTING = '_wl_cmkg_ignore_tag_from_ui';
23
24
25
	public function register_routes() {
26
		$that = $this;
27
		add_action( 'rest_api_init',
28
			function () use ( $that ) {
29
				$that->register_accept_route();
30
				$that->register_undo_route();
31
				$that->register_nomatch_route();
32
			} );
33
	}
34
35
36
	public function accept_entity( $request ) {
37
		$data        = $request->get_params();
38
		$term_id     = (int) $data['term_id'];
39
		$entity_data = (array) $data['entity'];
40
41
42
		$same_as_list = array_merge( $entity_data['sameAs'], array( $entity_data['@id'] ) );
43
44
		// Insert Same As
45
		delete_term_meta( $term_id, self::SAME_AS_META_KEY );
46
		foreach ( $same_as_list as $same_as ) {
47
			add_term_meta( $term_id, self::SAME_AS_META_KEY, $same_as );
48
		}
49
50
		// Insert Alt labels
51
		$alt_labels = array( (string) $entity_data['name'] );
52
		delete_term_meta( $term_id, self::ALTERNATIVE_LABEL_META_KEY );
53
		foreach ( $alt_labels as $alt_label ) {
54
			add_term_meta( $term_id, self::ALTERNATIVE_LABEL_META_KEY, $alt_label );
55
		}
56
57
		// Insert description and type
58
		add_term_meta( $term_id, self::DESCRIPTION_META_KEY, $entity_data['description'], true );
59
		add_term_meta( $term_id, self::TYPE_META_KEY, $entity_data['@type'], true );
60
61
		// Mark as external
62
		add_term_meta( $term_id, $this::EXTERNAL_ENTITY_META_KEY, 1, true );
63
64
		// Mark as ignored from ui
65
		update_term_meta( $term_id, self::IGNORE_TAG_FROM_LISTING, 1 );
66
67
		return $term_id;
68
	}
69
70
	public function undo( $request ) {
71
		$data    = $request->get_params();
72
		$term_id = (int) $data['term_id'];
73
		// Insert Same As
74
		delete_term_meta( $term_id, self::SAME_AS_META_KEY );
75
		delete_term_meta( $term_id, self::ALTERNATIVE_LABEL_META_KEY );
76
		delete_term_meta( $term_id, self::DESCRIPTION_META_KEY );
77
		delete_term_meta( $term_id, self::TYPE_META_KEY );
78
		delete_term_meta( $term_id, self::EXTERNAL_ENTITY_META_KEY );
79
		delete_term_meta( $term_id, self::IGNORE_TAG_FROM_LISTING );
80
81
		return $term_id;
82
	}
83
84
85
	public function mark_as_no_match( $request ) {
86
		$data    = $request->get_params();
87
		$term_id = (int) $data['term_id'];
88
89
		return add_term_meta( $term_id, self::IGNORE_TAG_FROM_LISTING, 1 );
90
	}
91
92
93 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...
94
		register_rest_route(
95
			Api_Config::REST_NAMESPACE,
96
			'/entity/undo',
97
			array(
98
				'methods'             => WP_REST_Server::CREATABLE,
99
				'callback'            => array( $this, 'undo' ),
100
				//@todo : review the permission level
101
				'permission_callback' => function () {
102
					return current_user_can( 'manage_options' );
103
				},
104
				'args'                => array(
105
					'term_id' => array(
106
						'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...
107
							return is_numeric( $param ) && $param;
108
						},
109
						'required'          => true,
110
					),
111
				),
112
			)
113
		);
114
	}
115
116 View Code Duplication
	private function register_accept_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...
117
		register_rest_route(
118
			Api_Config::REST_NAMESPACE,
119
			'/entity/accept',
120
			array(
121
				'methods'             => WP_REST_Server::CREATABLE,
122
				'callback'            => array( $this, 'accept_entity' ),
123
				//@todo : review the permission level
124
				'permission_callback' => function () {
125
					return current_user_can( 'manage_options' );
126
				},
127
				'args'                => array(
128
					'term_id' => array(
129
						'validate_callback' => function ( $param, $request, $key ) {
0 ignored issues
show
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...
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...
130
							return is_numeric( $param ) && $param;
131
						},
132
						'required'          => true,
133
					),
134
					'entity'  => array(
135
						'validate_callback' => function ( $param, $request, $key ) {
0 ignored issues
show
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...
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...
136
							return is_array( $param );
137
						},
138
						'required'          => true,
139
					),
140
				),
141
			)
142
		);
143
	}
144
145 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...
146
		register_rest_route(
147
			Api_Config::REST_NAMESPACE,
148
			'/entity/no_match',
149
			array(
150
				'methods'             => WP_REST_Server::CREATABLE,
151
				'callback'            => array( $this, 'mark_as_no_match' ),
152
				//@todo : review the permission level
153
				'permission_callback' => function () {
154
					return current_user_can( 'manage_options' );
155
				},
156
				'args'                => array(
157
					'term_id' => array(
158
						'validate_callback' => function ( $param, $request, $key ) {
0 ignored issues
show
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...
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...
159
							return is_numeric( $param ) && $param;
160
						},
161
						'required'          => true,
162
					),
163
				),
164
			)
165
		);
166
	}
167
168
}