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

Entity_Rest_Endpoint::register_undo_route()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 22

Duplication

Lines 22
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
nc 1
nop 0
dl 22
loc 22
rs 9.568
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\Vocabulary\Data\Entity\Entity_Factory;
10
use WP_REST_Server;
11
12
/**
13
 * @since 1.0.0
14
 * @author Naveen Muthusamy <[email protected]>
15
 */
16
class Entity_Rest_Endpoint {
17
18
	const SAME_AS_META_KEY = 'entity_same_as';
19
	const ALTERNATIVE_LABEL_META_KEY = '_wl_alt_label';
20
	const DESCRIPTION_META_KEY = 'entity_description';
21
	const TYPE_META_KEY = 'entity_type';
22
	const EXTERNAL_ENTITY_META_KEY = '_wl_is_external';
23
	const IGNORE_TAG_FROM_LISTING = '_wl_cmkg_ignore_tag_from_ui';
24
25
26
	public function register_routes() {
27
		$that = $this;
28
		add_action( 'rest_api_init',
29
			function () use ( $that ) {
30
				$that->register_accept_route();
31
				$that->register_undo_route();
32
				$that->register_nomatch_route();
33
			} );
34
	}
35
36
37
	public function accept_entity( $request ) {
38
		$data        = $request->get_params();
39
		$term_id     = (int) $data['term_id'];
40
		$entity_data = (array) $data['entity'];
41
		$entity      = Entity_Factory::get_instance( $term_id );
42
		$entity->save_jsonld_data( $entity_data );
43
		update_term_meta( $term_id, self::IGNORE_TAG_FROM_LISTING, 1 );
44
45
		return $term_id;
46
	}
47
48
	public function undo( $request ) {
49
		$data    = $request->get_params();
50
		$term_id = (int) $data['term_id'];
51
		// Insert Same As
52
		delete_term_meta( $term_id, self::SAME_AS_META_KEY );
53
		delete_term_meta( $term_id, self::ALTERNATIVE_LABEL_META_KEY );
54
		delete_term_meta( $term_id, self::DESCRIPTION_META_KEY );
55
		delete_term_meta( $term_id, self::TYPE_META_KEY );
56
		delete_term_meta( $term_id, self::EXTERNAL_ENTITY_META_KEY );
57
		delete_term_meta( $term_id, self::IGNORE_TAG_FROM_LISTING );
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
67
		return add_term_meta( $term_id, self::IGNORE_TAG_FROM_LISTING, 1 );
68
	}
69
70
71 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...
72
		register_rest_route(
73
			Api_Config::REST_NAMESPACE,
74
			'/entity/undo',
75
			array(
76
				'methods'             => WP_REST_Server::CREATABLE,
77
				'callback'            => array( $this, 'undo' ),
78
				//@todo : review the permission level
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 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...
95
		register_rest_route(
96
			Api_Config::REST_NAMESPACE,
97
			'/entity/accept',
98
			array(
99
				'methods'             => WP_REST_Server::CREATABLE,
100
				'callback'            => array( $this, 'accept_entity' ),
101
				//@todo : review the permission level
102
				'permission_callback' => function () {
103
					return current_user_can( 'manage_options' );
104
				},
105
				'args'                => array(
106
					'term_id' => array(
107
						'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...
108
							return is_numeric( $param ) && $param;
109
						},
110
						'required'          => true,
111
					),
112
					'entity'  => array(
113
						'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...
114
							return is_array( $param );
115
						},
116
						'required'          => true,
117
					),
118
				),
119
			)
120
		);
121
	}
122
123 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...
124
		register_rest_route(
125
			Api_Config::REST_NAMESPACE,
126
			'/entity/no_match',
127
			array(
128
				'methods'             => WP_REST_Server::CREATABLE,
129
				'callback'            => array( $this, 'mark_as_no_match' ),
130
				//@todo : review the permission level
131
				'permission_callback' => function () {
132
					return current_user_can( 'manage_options' );
133
				},
134
				'args'                => array(
135
					'term_id' => array(
136
						'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...
137
							return is_numeric( $param ) && $param;
138
						},
139
						'required'          => true,
140
					),
141
				),
142
			)
143
		);
144
	}
145
146
}