Completed
Push — develop ( aa5263...ada409 )
by Naveen
01:25 queued 13s
created
gardening-kg/includes/Term_Entity/Gardening_Kg_Term_Entity_Runner.php 2 patches
Indentation   +124 added lines, -124 removed lines patch added patch discarded remove patch
@@ -9,129 +9,129 @@
 block discarded – undo
9 9
 
10 10
 class Gardening_Kg_Term_Entity_Runner implements Runner {
11 11
 
12
-	/**
13
-	 * @var Store $store
14
-	 */
15
-	private $store;
16
-
17
-	/**
18
-	 * @var Api_Service
19
-	 */
20
-	private $api_service;
21
-
22
-	/**
23
-	 * @paramm Gardening_Kg_Store $store
24
-	 */
25
-	public function __construct( Store $store, Api_Service $api_service ) {
26
-		$this->store       = $store;
27
-		$this->api_service = $api_service;
28
-	}
29
-
30
-	// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
31
-	public function run( $last_id ) {
32
-		$batch_size = 100;
33
-		$items      = (array) $this->store->list_items( $last_id, $batch_size );
34
-
35
-		foreach ( $items as $item ) {
36
-			$this->process( $item );
37
-		}
38
-
39
-		// Count the processed items.
40
-		$count_items = count( $items );
41
-
42
-		// We're done, since the number of items is less than the requested qty.
43
-		if ( $count_items < $batch_size ) {
44
-			return array( $count_items, null );
45
-		}
46
-
47
-		// Get the last ID.
48
-		$last_id = end( $items );
49
-
50
-		// Finally return the count.
51
-		return array( $count_items, $last_id );
52
-	}
53
-
54
-	// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
55
-	private function process( $item ) {
56
-		global $wpdb;
57
-
58
-		// Get the entity data for non lifted abouts.
59
-		$id = $wpdb->get_var(
60
-			$wpdb->prepare(
61
-				"SELECT id FROM {$wpdb->prefix}wl_entities WHERE content_id = %d AND content_type = %d AND about_jsonld IS NULL",
62
-				$item,
63
-				Object_Type_Enum::TERM
64
-			)
65
-		);
66
-
67
-		// Exit if not found.
68
-		if ( ! isset( $id ) ) {
69
-			return;
70
-		}
71
-
72
-		// Lift.
73
-		$term = get_term( $item );
74
-
75
-		// Skip if this post must not be processed, e.g. it's not the right post_type, it's not `publish`, ...
76
-		if ( ! $this->should_process( $term ) ) {
77
-			return;
78
-		}
79
-
80
-		$title = wp_strip_all_tags( $term->name );
81
-
82
-		$response = $this->api_service->request(
83
-			'POST',
84
-			'/thirdparty/cafemedia/gardening-kg/matches',
85
-			array(
86
-				'accept'       => 'text/plain',
87
-				'content-type' => 'text/plain',
88
-			),
89
-			$title
90
-		);
91
-
92
-		$response_body = $response->get_body();
93
-		$lines         = explode( "\n", $response_body );
94
-		$fields        = explode( "\t", $lines[0] );
95
-		$jsonld        = isset( $fields[1] ) ? trim( $fields[1] ) : '';
96
-		// No results.
97
-		if ( empty( $jsonld ) ) {
98
-			return;
99
-		}
100
-
101
-		// Store the results in the database
102
-		$wpdb->query(
103
-			$wpdb->prepare(
104
-				"UPDATE {$wpdb->prefix}wl_entities SET about_jsonld = %s WHERE id = %d",
105
-				$fields[1],
106
-				$item
107
-			)
108
-		);
109
-	}
110
-
111
-	private function should_process( $term ) {
112
-
113
-		return is_a( $term, 'WP_Term' ) &&
114
-			in_array(
115
-				$term->taxonomy,
116
-				array(
117
-					'post_tag',
118
-					'category',
119
-				),
120
-				true
121
-			);
122
-	}
123
-
124
-	/**
125
-	 * Get the total number of posts to process.
126
-	 *
127
-	 * We only count published posts.
128
-	 *
129
-	 * @return int
130
-	 */
131
-	public function get_total() {
132
-		global $wpdb;
133
-
134
-		return intval( $wpdb->get_var( "SELECT COUNT(1) FROM $wpdb->posts WHERE post_status = 'publish'" ) );
135
-	}
12
+    /**
13
+     * @var Store $store
14
+     */
15
+    private $store;
16
+
17
+    /**
18
+     * @var Api_Service
19
+     */
20
+    private $api_service;
21
+
22
+    /**
23
+     * @paramm Gardening_Kg_Store $store
24
+     */
25
+    public function __construct( Store $store, Api_Service $api_service ) {
26
+        $this->store       = $store;
27
+        $this->api_service = $api_service;
28
+    }
29
+
30
+    // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
31
+    public function run( $last_id ) {
32
+        $batch_size = 100;
33
+        $items      = (array) $this->store->list_items( $last_id, $batch_size );
34
+
35
+        foreach ( $items as $item ) {
36
+            $this->process( $item );
37
+        }
38
+
39
+        // Count the processed items.
40
+        $count_items = count( $items );
41
+
42
+        // We're done, since the number of items is less than the requested qty.
43
+        if ( $count_items < $batch_size ) {
44
+            return array( $count_items, null );
45
+        }
46
+
47
+        // Get the last ID.
48
+        $last_id = end( $items );
49
+
50
+        // Finally return the count.
51
+        return array( $count_items, $last_id );
52
+    }
53
+
54
+    // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
55
+    private function process( $item ) {
56
+        global $wpdb;
57
+
58
+        // Get the entity data for non lifted abouts.
59
+        $id = $wpdb->get_var(
60
+            $wpdb->prepare(
61
+                "SELECT id FROM {$wpdb->prefix}wl_entities WHERE content_id = %d AND content_type = %d AND about_jsonld IS NULL",
62
+                $item,
63
+                Object_Type_Enum::TERM
64
+            )
65
+        );
66
+
67
+        // Exit if not found.
68
+        if ( ! isset( $id ) ) {
69
+            return;
70
+        }
71
+
72
+        // Lift.
73
+        $term = get_term( $item );
74
+
75
+        // Skip if this post must not be processed, e.g. it's not the right post_type, it's not `publish`, ...
76
+        if ( ! $this->should_process( $term ) ) {
77
+            return;
78
+        }
79
+
80
+        $title = wp_strip_all_tags( $term->name );
81
+
82
+        $response = $this->api_service->request(
83
+            'POST',
84
+            '/thirdparty/cafemedia/gardening-kg/matches',
85
+            array(
86
+                'accept'       => 'text/plain',
87
+                'content-type' => 'text/plain',
88
+            ),
89
+            $title
90
+        );
91
+
92
+        $response_body = $response->get_body();
93
+        $lines         = explode( "\n", $response_body );
94
+        $fields        = explode( "\t", $lines[0] );
95
+        $jsonld        = isset( $fields[1] ) ? trim( $fields[1] ) : '';
96
+        // No results.
97
+        if ( empty( $jsonld ) ) {
98
+            return;
99
+        }
100
+
101
+        // Store the results in the database
102
+        $wpdb->query(
103
+            $wpdb->prepare(
104
+                "UPDATE {$wpdb->prefix}wl_entities SET about_jsonld = %s WHERE id = %d",
105
+                $fields[1],
106
+                $item
107
+            )
108
+        );
109
+    }
110
+
111
+    private function should_process( $term ) {
112
+
113
+        return is_a( $term, 'WP_Term' ) &&
114
+            in_array(
115
+                $term->taxonomy,
116
+                array(
117
+                    'post_tag',
118
+                    'category',
119
+                ),
120
+                true
121
+            );
122
+    }
123
+
124
+    /**
125
+     * Get the total number of posts to process.
126
+     *
127
+     * We only count published posts.
128
+     *
129
+     * @return int
130
+     */
131
+    public function get_total() {
132
+        global $wpdb;
133
+
134
+        return intval( $wpdb->get_var( "SELECT COUNT(1) FROM $wpdb->posts WHERE post_status = 'publish'" ) );
135
+    }
136 136
 
137 137
 }
Please login to merge, or discard this patch.
Spacing   +22 added lines, -22 removed lines patch added patch discarded remove patch
@@ -22,37 +22,37 @@  discard block
 block discarded – undo
22 22
 	/**
23 23
 	 * @paramm Gardening_Kg_Store $store
24 24
 	 */
25
-	public function __construct( Store $store, Api_Service $api_service ) {
25
+	public function __construct(Store $store, Api_Service $api_service) {
26 26
 		$this->store       = $store;
27 27
 		$this->api_service = $api_service;
28 28
 	}
29 29
 
30 30
 	// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
31
-	public function run( $last_id ) {
31
+	public function run($last_id) {
32 32
 		$batch_size = 100;
33
-		$items      = (array) $this->store->list_items( $last_id, $batch_size );
33
+		$items      = (array) $this->store->list_items($last_id, $batch_size);
34 34
 
35
-		foreach ( $items as $item ) {
36
-			$this->process( $item );
35
+		foreach ($items as $item) {
36
+			$this->process($item);
37 37
 		}
38 38
 
39 39
 		// Count the processed items.
40
-		$count_items = count( $items );
40
+		$count_items = count($items);
41 41
 
42 42
 		// We're done, since the number of items is less than the requested qty.
43
-		if ( $count_items < $batch_size ) {
44
-			return array( $count_items, null );
43
+		if ($count_items < $batch_size) {
44
+			return array($count_items, null);
45 45
 		}
46 46
 
47 47
 		// Get the last ID.
48
-		$last_id = end( $items );
48
+		$last_id = end($items);
49 49
 
50 50
 		// Finally return the count.
51
-		return array( $count_items, $last_id );
51
+		return array($count_items, $last_id);
52 52
 	}
53 53
 
54 54
 	// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
55
-	private function process( $item ) {
55
+	private function process($item) {
56 56
 		global $wpdb;
57 57
 
58 58
 		// Get the entity data for non lifted abouts.
@@ -65,19 +65,19 @@  discard block
 block discarded – undo
65 65
 		);
66 66
 
67 67
 		// Exit if not found.
68
-		if ( ! isset( $id ) ) {
68
+		if ( ! isset($id)) {
69 69
 			return;
70 70
 		}
71 71
 
72 72
 		// Lift.
73
-		$term = get_term( $item );
73
+		$term = get_term($item);
74 74
 
75 75
 		// Skip if this post must not be processed, e.g. it's not the right post_type, it's not `publish`, ...
76
-		if ( ! $this->should_process( $term ) ) {
76
+		if ( ! $this->should_process($term)) {
77 77
 			return;
78 78
 		}
79 79
 
80
-		$title = wp_strip_all_tags( $term->name );
80
+		$title = wp_strip_all_tags($term->name);
81 81
 
82 82
 		$response = $this->api_service->request(
83 83
 			'POST',
@@ -90,11 +90,11 @@  discard block
 block discarded – undo
90 90
 		);
91 91
 
92 92
 		$response_body = $response->get_body();
93
-		$lines         = explode( "\n", $response_body );
94
-		$fields        = explode( "\t", $lines[0] );
95
-		$jsonld        = isset( $fields[1] ) ? trim( $fields[1] ) : '';
93
+		$lines         = explode("\n", $response_body);
94
+		$fields        = explode("\t", $lines[0]);
95
+		$jsonld        = isset($fields[1]) ? trim($fields[1]) : '';
96 96
 		// No results.
97
-		if ( empty( $jsonld ) ) {
97
+		if (empty($jsonld)) {
98 98
 			return;
99 99
 		}
100 100
 
@@ -108,9 +108,9 @@  discard block
 block discarded – undo
108 108
 		);
109 109
 	}
110 110
 
111
-	private function should_process( $term ) {
111
+	private function should_process($term) {
112 112
 
113
-		return is_a( $term, 'WP_Term' ) &&
113
+		return is_a($term, 'WP_Term') &&
114 114
 			in_array(
115 115
 				$term->taxonomy,
116 116
 				array(
@@ -131,7 +131,7 @@  discard block
 block discarded – undo
131 131
 	public function get_total() {
132 132
 		global $wpdb;
133 133
 
134
-		return intval( $wpdb->get_var( "SELECT COUNT(1) FROM $wpdb->posts WHERE post_status = 'publish'" ) );
134
+		return intval($wpdb->get_var("SELECT COUNT(1) FROM $wpdb->posts WHERE post_status = 'publish'"));
135 135
 	}
136 136
 
137 137
 }
Please login to merge, or discard this patch.
dashboard/includes/Term_Entity_Match/Term_Entity_Match_Rest_Controller.php 1 patch
Indentation   +198 added lines, -198 removed lines patch added patch discarded remove patch
@@ -11,203 +11,203 @@
 block discarded – undo
11 11
 
12 12
 class Term_Entity_Match_Rest_Controller extends \WP_REST_Controller {
13 13
 
14
-	private $match_service;
15
-
16
-	public function __construct( $match_service ) {
17
-		$this->match_service = $match_service;
18
-	}
19
-
20
-	public function register_hooks() {
21
-		add_action( 'rest_api_init', array( $this, 'register_routes' ) );
22
-	}
23
-
24
-	/**
25
-	 * Register the routes for the objects of the controller.
26
-	 */
27
-	public function register_routes() {
28
-
29
-		// Get term matches by taxonomy name
30
-		register_rest_route(
31
-			'wordlift/v1',
32
-			'/term-matches',
33
-			array(
34
-				'methods'             => 'GET',
35
-				'callback'            => array( $this, 'get_term_matches' ),
36
-				'args'                => array(
37
-					'cursor'     => array(
38
-						'type'              => 'string',
39
-						'default'           => Cursor::EMPTY_CURSOR_AS_BASE64_STRING,
40
-						'validate_callback' => 'rest_validate_request_arg',
41
-						'sanitize_callback' => array( Cursor::class, 'rest_sanitize_request_arg' ),
42
-					),
43
-					'limit'      => array(
44
-						'type'              => 'integer',
45
-						'validate_callback' => 'rest_validate_request_arg',
46
-						'default'           => 10,
47
-						'minimum'           => 1,
48
-						'maximum'           => 100,
49
-						'sanitize_callback' => 'absint',
50
-					),
51
-					'taxonomies' => array(
52
-						'type'              => 'array',
53
-						'validate_callback' => 'rest_validate_request_arg',
54
-					),
55
-					'has_match'  => array(
56
-						'type'              => 'boolean',
57
-						'required'          => false,
58
-						'validate_callback' => 'rest_validate_request_arg',
59
-					),
60
-				),
61
-				'permission_callback' => function () {
62
-					return current_user_can( 'manage_options' );
63
-				},
64
-			)
65
-		);
66
-
67
-		// Create a new match for a term
68
-		register_rest_route(
69
-			'/wordlift/v1',
70
-			'/term-matches/(?P<term_id>\d+)/matches',
71
-			array(
72
-				'methods'             => 'POST',
73
-				'callback'            => array( $this, 'create_term_match' ),
74
-				'args'                => array(
75
-					'term_id' => array(
76
-						'required'          => true,
77
-						'validate_callback' => 'rest_validate_request_arg',
78
-					),
79
-				),
80
-				'permission_callback' => function () {
81
-					return current_user_can( 'manage_options' );
82
-				},
83
-			)
84
-		);
85
-
86
-		// Update an existing term match
87
-		register_rest_route(
88
-			'/wordlift/v1',
89
-			'/term-matches/(?P<term_id>\d+)/matches/(?P<match_id>\d+)',
90
-			array(
91
-				'methods'             => 'PUT',
92
-				'callback'            => array( $this, 'update_term_match' ),
93
-				'args'                => array(
94
-					'term_id'  => array(
95
-						'required'          => true,
96
-						'validate_callback' => 'rest_validate_request_arg',
97
-					),
98
-					'match_id' => array(
99
-						'required'          => true,
100
-						'validate_callback' => 'rest_validate_request_arg',
101
-					),
102
-				),
103
-				'permission_callback' => function () {
104
-
105
-					return current_user_can( 'manage_options' );
106
-				},
107
-			)
108
-		);
109
-	}
110
-
111
-	/**
112
-	 * Get the term matches by taxonomy name.
113
-	 *
114
-	 * @var $request \WP_REST_Request
115
-	 */
116
-	public function get_term_matches( $request ) {
117
-
118
-		$cursor = $request->get_param( 'cursor' );
119
-		if ( $request->has_param( 'limit' ) ) {
120
-			$cursor['limit'] = $request->get_param( 'limit' );
121
-		}
122
-		if ( $request->has_param( 'sort' ) ) {
123
-			$cursor['sort'] = $request->get_param( 'sort' );
124
-		}
125
-		if ( $request->has_param( 'taxonomies' ) ) {
126
-			$cursor['query']['taxonomies'] = $request->get_param( 'taxonomies' );
127
-		}
128
-		if ( $request->has_param( 'has_match' ) ) {
129
-			$cursor['query']['has_match'] = $request->get_param( 'has_match' );
130
-		}
131
-
132
-		// Query.
133
-		$taxonomies = isset( $cursor['query']['taxonomies'] ) ? $cursor['query']['taxonomies'] : apply_filters(
134
-			'wl_dashboard__post_entity_match__taxonomies',
135
-			array(
136
-				'post_tag',
137
-				'category',
138
-			)
139
-		);
140
-
141
-		$has_match = isset( $cursor['query']['has_match'] ) ? $cursor['query']['has_match'] : null;
142
-
143
-		$items = $this->match_service->list_items(
144
-			array(
145
-				// Query
146
-				'taxonomies' => $taxonomies,
147
-				'has_match'  => $has_match,
148
-				// Cursor-Pagination
149
-				'position'   => $cursor['position'],
150
-				'element'    => $cursor['element'],
151
-				'direction'  => $cursor['direction'],
152
-				// `+1` to check if we have other results.
153
-				'limit'      => $cursor['limit'] + 1,
154
-				'sort'       => $cursor['sort'],
155
-			)
156
-		);
157
-
158
-		return new Cursor_Page(
159
-			$items,
160
-			$cursor['position'],
161
-			$cursor['element'],
162
-			$cursor['direction'],
163
-			$cursor['sort'],
164
-			$cursor['limit'],
165
-			$cursor['query']
166
-		);
167
-	}
168
-
169
-	/**
170
-	 * Create a new match for a term.
171
-	 *
172
-	 * @var $request \WP_REST_Request
173
-	 */
174
-	public function create_term_match( $request ) {
175
-
176
-		$term_id = $request->get_param( 'term_id' );
177
-
178
-		// If we dont have a entry on the match table, then add one.
179
-		$content_id = Wordpress_Content_Id::create_term( $term_id );
180
-		if ( ! Wordpress_Content_Service::get_instance()
181
-										->get_entity_id( $content_id ) ) {
182
-			$uri = Entity_Uri_Generator::create_uri( $content_id->get_type(), $content_id->get_id() );
183
-			Wordpress_Content_Service::get_instance()->set_entity_id( $content_id, $uri );
184
-		}
185
-
186
-		$match_id = $this->match_service->get_id(
187
-			$term_id,
188
-			Object_Type_Enum::TERM
189
-		);
190
-
191
-		return $this->match_service->set_jsonld(
192
-			$term_id,
193
-			Object_Type_Enum::TERM,
194
-			$match_id,
195
-			$request->get_json_params()
196
-		);
197
-
198
-	}
199
-
200
-	/**
201
-	 * @var $request \WP_REST_Request
202
-	 */
203
-	public function update_term_match( $request ) {
204
-
205
-		return $this->match_service->set_jsonld(
206
-			$request->get_param( 'term_id' ),
207
-			Object_Type_Enum::TERM,
208
-			$request->get_param( 'match_id' ),
209
-			$request->get_json_params()
210
-		);
211
-	}
14
+    private $match_service;
15
+
16
+    public function __construct( $match_service ) {
17
+        $this->match_service = $match_service;
18
+    }
19
+
20
+    public function register_hooks() {
21
+        add_action( 'rest_api_init', array( $this, 'register_routes' ) );
22
+    }
23
+
24
+    /**
25
+     * Register the routes for the objects of the controller.
26
+     */
27
+    public function register_routes() {
28
+
29
+        // Get term matches by taxonomy name
30
+        register_rest_route(
31
+            'wordlift/v1',
32
+            '/term-matches',
33
+            array(
34
+                'methods'             => 'GET',
35
+                'callback'            => array( $this, 'get_term_matches' ),
36
+                'args'                => array(
37
+                    'cursor'     => array(
38
+                        'type'              => 'string',
39
+                        'default'           => Cursor::EMPTY_CURSOR_AS_BASE64_STRING,
40
+                        'validate_callback' => 'rest_validate_request_arg',
41
+                        'sanitize_callback' => array( Cursor::class, 'rest_sanitize_request_arg' ),
42
+                    ),
43
+                    'limit'      => array(
44
+                        'type'              => 'integer',
45
+                        'validate_callback' => 'rest_validate_request_arg',
46
+                        'default'           => 10,
47
+                        'minimum'           => 1,
48
+                        'maximum'           => 100,
49
+                        'sanitize_callback' => 'absint',
50
+                    ),
51
+                    'taxonomies' => array(
52
+                        'type'              => 'array',
53
+                        'validate_callback' => 'rest_validate_request_arg',
54
+                    ),
55
+                    'has_match'  => array(
56
+                        'type'              => 'boolean',
57
+                        'required'          => false,
58
+                        'validate_callback' => 'rest_validate_request_arg',
59
+                    ),
60
+                ),
61
+                'permission_callback' => function () {
62
+                    return current_user_can( 'manage_options' );
63
+                },
64
+            )
65
+        );
66
+
67
+        // Create a new match for a term
68
+        register_rest_route(
69
+            '/wordlift/v1',
70
+            '/term-matches/(?P<term_id>\d+)/matches',
71
+            array(
72
+                'methods'             => 'POST',
73
+                'callback'            => array( $this, 'create_term_match' ),
74
+                'args'                => array(
75
+                    'term_id' => array(
76
+                        'required'          => true,
77
+                        'validate_callback' => 'rest_validate_request_arg',
78
+                    ),
79
+                ),
80
+                'permission_callback' => function () {
81
+                    return current_user_can( 'manage_options' );
82
+                },
83
+            )
84
+        );
85
+
86
+        // Update an existing term match
87
+        register_rest_route(
88
+            '/wordlift/v1',
89
+            '/term-matches/(?P<term_id>\d+)/matches/(?P<match_id>\d+)',
90
+            array(
91
+                'methods'             => 'PUT',
92
+                'callback'            => array( $this, 'update_term_match' ),
93
+                'args'                => array(
94
+                    'term_id'  => array(
95
+                        'required'          => true,
96
+                        'validate_callback' => 'rest_validate_request_arg',
97
+                    ),
98
+                    'match_id' => array(
99
+                        'required'          => true,
100
+                        'validate_callback' => 'rest_validate_request_arg',
101
+                    ),
102
+                ),
103
+                'permission_callback' => function () {
104
+
105
+                    return current_user_can( 'manage_options' );
106
+                },
107
+            )
108
+        );
109
+    }
110
+
111
+    /**
112
+     * Get the term matches by taxonomy name.
113
+     *
114
+     * @var $request \WP_REST_Request
115
+     */
116
+    public function get_term_matches( $request ) {
117
+
118
+        $cursor = $request->get_param( 'cursor' );
119
+        if ( $request->has_param( 'limit' ) ) {
120
+            $cursor['limit'] = $request->get_param( 'limit' );
121
+        }
122
+        if ( $request->has_param( 'sort' ) ) {
123
+            $cursor['sort'] = $request->get_param( 'sort' );
124
+        }
125
+        if ( $request->has_param( 'taxonomies' ) ) {
126
+            $cursor['query']['taxonomies'] = $request->get_param( 'taxonomies' );
127
+        }
128
+        if ( $request->has_param( 'has_match' ) ) {
129
+            $cursor['query']['has_match'] = $request->get_param( 'has_match' );
130
+        }
131
+
132
+        // Query.
133
+        $taxonomies = isset( $cursor['query']['taxonomies'] ) ? $cursor['query']['taxonomies'] : apply_filters(
134
+            'wl_dashboard__post_entity_match__taxonomies',
135
+            array(
136
+                'post_tag',
137
+                'category',
138
+            )
139
+        );
140
+
141
+        $has_match = isset( $cursor['query']['has_match'] ) ? $cursor['query']['has_match'] : null;
142
+
143
+        $items = $this->match_service->list_items(
144
+            array(
145
+                // Query
146
+                'taxonomies' => $taxonomies,
147
+                'has_match'  => $has_match,
148
+                // Cursor-Pagination
149
+                'position'   => $cursor['position'],
150
+                'element'    => $cursor['element'],
151
+                'direction'  => $cursor['direction'],
152
+                // `+1` to check if we have other results.
153
+                'limit'      => $cursor['limit'] + 1,
154
+                'sort'       => $cursor['sort'],
155
+            )
156
+        );
157
+
158
+        return new Cursor_Page(
159
+            $items,
160
+            $cursor['position'],
161
+            $cursor['element'],
162
+            $cursor['direction'],
163
+            $cursor['sort'],
164
+            $cursor['limit'],
165
+            $cursor['query']
166
+        );
167
+    }
168
+
169
+    /**
170
+     * Create a new match for a term.
171
+     *
172
+     * @var $request \WP_REST_Request
173
+     */
174
+    public function create_term_match( $request ) {
175
+
176
+        $term_id = $request->get_param( 'term_id' );
177
+
178
+        // If we dont have a entry on the match table, then add one.
179
+        $content_id = Wordpress_Content_Id::create_term( $term_id );
180
+        if ( ! Wordpress_Content_Service::get_instance()
181
+                                        ->get_entity_id( $content_id ) ) {
182
+            $uri = Entity_Uri_Generator::create_uri( $content_id->get_type(), $content_id->get_id() );
183
+            Wordpress_Content_Service::get_instance()->set_entity_id( $content_id, $uri );
184
+        }
185
+
186
+        $match_id = $this->match_service->get_id(
187
+            $term_id,
188
+            Object_Type_Enum::TERM
189
+        );
190
+
191
+        return $this->match_service->set_jsonld(
192
+            $term_id,
193
+            Object_Type_Enum::TERM,
194
+            $match_id,
195
+            $request->get_json_params()
196
+        );
197
+
198
+    }
199
+
200
+    /**
201
+     * @var $request \WP_REST_Request
202
+     */
203
+    public function update_term_match( $request ) {
204
+
205
+        return $this->match_service->set_jsonld(
206
+            $request->get_param( 'term_id' ),
207
+            Object_Type_Enum::TERM,
208
+            $request->get_param( 'match_id' ),
209
+            $request->get_json_params()
210
+        );
211
+    }
212 212
 
213 213
 }
Please login to merge, or discard this patch.
modules/dashboard/includes/Term_Entity_Match/Term_Entity_Match_Service.php 1 patch
Indentation   +50 added lines, -50 removed lines patch added patch discarded remove patch
@@ -7,55 +7,55 @@
 block discarded – undo
7 7
 
8 8
 class Term_Entity_Match_Service extends Match_Service {
9 9
 
10
-	public function list_items( $args ) {
11
-		global $wpdb;
12
-
13
-		$params = wp_parse_args(
14
-			$args,
15
-			array(
16
-				'position'  => null,
17
-				'element'   => 'INCLUDED',
18
-				'direction' => 'ASCENDING',
19
-				'limit'     => 10,
20
-				'sort'      => '+id',
21
-				// Query.
22
-				'taxonomy'  => null,
23
-				'has_match' => null,
24
-			)
25
-		);
26
-
27
-		/**
28
-		 * @var $sort Sort
29
-		 */
30
-		$sort = new Sort( $params['sort'] );
31
-
32
-		$query_builder = new Query_Builder(
33
-			$params,
34
-			$sort
35
-		);
36
-		$query         = $query_builder
37
-			->get();
38
-
39
-		$items = $wpdb->get_results(
40
-		// Each function above is preparing `$sql` by using `$wpdb->prepare`.
41
-		// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
42
-			$wpdb->prepare( $query, Object_Type_Enum::TERM )
43
-		);
44
-
45
-		$sort->apply( $items );
46
-
47
-		return $this->map( $items );
48
-	}
49
-
50
-	private function map( array $items ) {
51
-		return array_map(
52
-			function ( $item ) {
53
-				$data             = json_decode( $item->match_jsonld, true );
54
-				$item->match_name = $data && is_array( $data ) && array_key_exists( 'name', $data ) ? $data['name'] : null;
55
-				return $item;
56
-			},
57
-			$items
58
-		);
59
-	}
10
+    public function list_items( $args ) {
11
+        global $wpdb;
12
+
13
+        $params = wp_parse_args(
14
+            $args,
15
+            array(
16
+                'position'  => null,
17
+                'element'   => 'INCLUDED',
18
+                'direction' => 'ASCENDING',
19
+                'limit'     => 10,
20
+                'sort'      => '+id',
21
+                // Query.
22
+                'taxonomy'  => null,
23
+                'has_match' => null,
24
+            )
25
+        );
26
+
27
+        /**
28
+         * @var $sort Sort
29
+         */
30
+        $sort = new Sort( $params['sort'] );
31
+
32
+        $query_builder = new Query_Builder(
33
+            $params,
34
+            $sort
35
+        );
36
+        $query         = $query_builder
37
+            ->get();
38
+
39
+        $items = $wpdb->get_results(
40
+        // Each function above is preparing `$sql` by using `$wpdb->prepare`.
41
+        // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
42
+            $wpdb->prepare( $query, Object_Type_Enum::TERM )
43
+        );
44
+
45
+        $sort->apply( $items );
46
+
47
+        return $this->map( $items );
48
+    }
49
+
50
+    private function map( array $items ) {
51
+        return array_map(
52
+            function ( $item ) {
53
+                $data             = json_decode( $item->match_jsonld, true );
54
+                $item->match_name = $data && is_array( $data ) && array_key_exists( 'name', $data ) ? $data['name'] : null;
55
+                return $item;
56
+            },
57
+            $items
58
+        );
59
+    }
60 60
 
61 61
 }
Please login to merge, or discard this patch.
src/modules/dashboard/includes/Match/Match_Query_Builder.php 2 patches
Indentation   +106 added lines, -106 removed lines patch added patch discarded remove patch
@@ -5,111 +5,111 @@
 block discarded – undo
5 5
 use Wordlift\Assertions;
6 6
 
7 7
 abstract class Match_Query_Builder {
8
-	/**
9
-	 * @var array
10
-	 */
11
-	protected $params;
12
-	/**
13
-	 * @var Match_Sort
14
-	 */
15
-	protected $sort;
16
-
17
-	protected $sql = '';
18
-
19
-	/**
20
-	 * @param $params array
21
-	 * @param $sort Match_Sort
22
-	 *
23
-	 * @throws \Exception Throws Exception if the parameters arent of right type.
24
-	 */
25
-	public function __construct( $params, $sort ) {
26
-
27
-		Assertions::is_array( $params );
28
-		Assertions::is_a( $sort, Match_Sort::class );
29
-
30
-		$this->params = $params;
31
-		$this->sort   = $sort;
32
-	}
33
-
34
-	/**
35
-	 * This method should build the query from params and sort object,
36
-	 * then return the prepared query.
37
-	 *
38
-	 * @return string
39
-	 */
40
-	public function get() {
41
-		$this->build();
42
-
43
-		return $this->sql;
44
-	}
45
-
46
-	/**
47
-	 * @return void
48
-	 */
49
-	abstract protected function build();
50
-
51
-	/**
52
-	 * Apply the sort for the cursor.
53
-	 *
54
-	 * @return Match_Query_Builder
55
-	 */
56
-	protected function cursor() {
57
-		global $wpdb;
58
-
59
-		// If there is no position set, the condition doesnt need to be applied
60
-		// This is necessary in case of `last` and `first` attribute.
61
-		if ( empty( $this->params['position'] ) ) {
62
-			return $this;
63
-		}
64
-
65
-		$tmp_sql             = " AND {$this->sort->get_field_name()} ";
66
-		$is_included         = ( $this->params['element'] !== 'EXCLUDED' );
67
-		$is_ascending        = ( $this->params['direction'] !== 'DESCENDING' );
68
-		$is_sorted_ascending = $this->sort->is_ascending();
69
-		switch ( array( $is_ascending, $is_sorted_ascending ) ) {
70
-			case array( true, true ):   // Forward & Ascending Order
71
-			case array( false, false ): // Backward & Descending Order
72
-				$tmp_sql .= ' >';
73
-				break;
74
-			case array( true, false ):  // Forward & Ascending Order
75
-			case array( false, true ):  // Backward & Descending Order
76
-				$tmp_sql .= ' <';
77
-				break;
78
-		}
79
-		if ( $is_included ) {
80
-			$tmp_sql .= '=';
81
-		}
82
-		$tmp_sql .= ' %s';
83
-
84
-		// `$tmp_sql` is built dynamically in this function
85
-		// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
86
-		$this->sql .= $wpdb->prepare( $tmp_sql, $this->params['position'] );
87
-		return $this;
88
-	}
89
-
90
-	protected function has_match() {
91
-		$value = $this->params['has_match'];
92
-
93
-		if ( true === $value ) {
94
-			$this->sql .= ' AND e.about_jsonld IS NOT NULL ';
95
-		} elseif ( false === $value ) {
96
-			$this->sql .= ' AND e.about_jsonld IS NULL ';
97
-		}
98
-
99
-		return $this;
100
-	}
101
-
102
-	protected function limit() {
103
-		$limit = $this->params['limit'];
104
-		global $wpdb;
105
-		$this->sql .= $wpdb->prepare( ' LIMIT %d', $limit );
106
-		return $this;
107
-	}
108
-
109
-	protected function order_by() {
110
-		$direction  = $this->params['direction'];
111
-		$this->sql .= $this->sort->get_orderby_clause( $direction );
112
-		return $this;
113
-	}
8
+    /**
9
+     * @var array
10
+     */
11
+    protected $params;
12
+    /**
13
+     * @var Match_Sort
14
+     */
15
+    protected $sort;
16
+
17
+    protected $sql = '';
18
+
19
+    /**
20
+     * @param $params array
21
+     * @param $sort Match_Sort
22
+     *
23
+     * @throws \Exception Throws Exception if the parameters arent of right type.
24
+     */
25
+    public function __construct( $params, $sort ) {
26
+
27
+        Assertions::is_array( $params );
28
+        Assertions::is_a( $sort, Match_Sort::class );
29
+
30
+        $this->params = $params;
31
+        $this->sort   = $sort;
32
+    }
33
+
34
+    /**
35
+     * This method should build the query from params and sort object,
36
+     * then return the prepared query.
37
+     *
38
+     * @return string
39
+     */
40
+    public function get() {
41
+        $this->build();
42
+
43
+        return $this->sql;
44
+    }
45
+
46
+    /**
47
+     * @return void
48
+     */
49
+    abstract protected function build();
50
+
51
+    /**
52
+     * Apply the sort for the cursor.
53
+     *
54
+     * @return Match_Query_Builder
55
+     */
56
+    protected function cursor() {
57
+        global $wpdb;
58
+
59
+        // If there is no position set, the condition doesnt need to be applied
60
+        // This is necessary in case of `last` and `first` attribute.
61
+        if ( empty( $this->params['position'] ) ) {
62
+            return $this;
63
+        }
64
+
65
+        $tmp_sql             = " AND {$this->sort->get_field_name()} ";
66
+        $is_included         = ( $this->params['element'] !== 'EXCLUDED' );
67
+        $is_ascending        = ( $this->params['direction'] !== 'DESCENDING' );
68
+        $is_sorted_ascending = $this->sort->is_ascending();
69
+        switch ( array( $is_ascending, $is_sorted_ascending ) ) {
70
+            case array( true, true ):   // Forward & Ascending Order
71
+            case array( false, false ): // Backward & Descending Order
72
+                $tmp_sql .= ' >';
73
+                break;
74
+            case array( true, false ):  // Forward & Ascending Order
75
+            case array( false, true ):  // Backward & Descending Order
76
+                $tmp_sql .= ' <';
77
+                break;
78
+        }
79
+        if ( $is_included ) {
80
+            $tmp_sql .= '=';
81
+        }
82
+        $tmp_sql .= ' %s';
83
+
84
+        // `$tmp_sql` is built dynamically in this function
85
+        // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
86
+        $this->sql .= $wpdb->prepare( $tmp_sql, $this->params['position'] );
87
+        return $this;
88
+    }
89
+
90
+    protected function has_match() {
91
+        $value = $this->params['has_match'];
92
+
93
+        if ( true === $value ) {
94
+            $this->sql .= ' AND e.about_jsonld IS NOT NULL ';
95
+        } elseif ( false === $value ) {
96
+            $this->sql .= ' AND e.about_jsonld IS NULL ';
97
+        }
98
+
99
+        return $this;
100
+    }
101
+
102
+    protected function limit() {
103
+        $limit = $this->params['limit'];
104
+        global $wpdb;
105
+        $this->sql .= $wpdb->prepare( ' LIMIT %d', $limit );
106
+        return $this;
107
+    }
108
+
109
+    protected function order_by() {
110
+        $direction  = $this->params['direction'];
111
+        $this->sql .= $this->sort->get_orderby_clause( $direction );
112
+        return $this;
113
+    }
114 114
 
115 115
 }
Please login to merge, or discard this patch.
Spacing   +17 added lines, -17 removed lines patch added patch discarded remove patch
@@ -22,10 +22,10 @@  discard block
 block discarded – undo
22 22
 	 *
23 23
 	 * @throws \Exception Throws Exception if the parameters arent of right type.
24 24
 	 */
25
-	public function __construct( $params, $sort ) {
25
+	public function __construct($params, $sort) {
26 26
 
27
-		Assertions::is_array( $params );
28
-		Assertions::is_a( $sort, Match_Sort::class );
27
+		Assertions::is_array($params);
28
+		Assertions::is_a($sort, Match_Sort::class);
29 29
 
30 30
 		$this->params = $params;
31 31
 		$this->sort   = $sort;
@@ -58,41 +58,41 @@  discard block
 block discarded – undo
58 58
 
59 59
 		// If there is no position set, the condition doesnt need to be applied
60 60
 		// This is necessary in case of `last` and `first` attribute.
61
-		if ( empty( $this->params['position'] ) ) {
61
+		if (empty($this->params['position'])) {
62 62
 			return $this;
63 63
 		}
64 64
 
65 65
 		$tmp_sql             = " AND {$this->sort->get_field_name()} ";
66
-		$is_included         = ( $this->params['element'] !== 'EXCLUDED' );
67
-		$is_ascending        = ( $this->params['direction'] !== 'DESCENDING' );
66
+		$is_included         = ($this->params['element'] !== 'EXCLUDED');
67
+		$is_ascending        = ($this->params['direction'] !== 'DESCENDING');
68 68
 		$is_sorted_ascending = $this->sort->is_ascending();
69
-		switch ( array( $is_ascending, $is_sorted_ascending ) ) {
70
-			case array( true, true ):   // Forward & Ascending Order
71
-			case array( false, false ): // Backward & Descending Order
69
+		switch (array($is_ascending, $is_sorted_ascending)) {
70
+			case array(true, true):   // Forward & Ascending Order
71
+			case array(false, false): // Backward & Descending Order
72 72
 				$tmp_sql .= ' >';
73 73
 				break;
74
-			case array( true, false ):  // Forward & Ascending Order
75
-			case array( false, true ):  // Backward & Descending Order
74
+			case array(true, false):  // Forward & Ascending Order
75
+			case array(false, true):  // Backward & Descending Order
76 76
 				$tmp_sql .= ' <';
77 77
 				break;
78 78
 		}
79
-		if ( $is_included ) {
79
+		if ($is_included) {
80 80
 			$tmp_sql .= '=';
81 81
 		}
82 82
 		$tmp_sql .= ' %s';
83 83
 
84 84
 		// `$tmp_sql` is built dynamically in this function
85 85
 		// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
86
-		$this->sql .= $wpdb->prepare( $tmp_sql, $this->params['position'] );
86
+		$this->sql .= $wpdb->prepare($tmp_sql, $this->params['position']);
87 87
 		return $this;
88 88
 	}
89 89
 
90 90
 	protected function has_match() {
91 91
 		$value = $this->params['has_match'];
92 92
 
93
-		if ( true === $value ) {
93
+		if (true === $value) {
94 94
 			$this->sql .= ' AND e.about_jsonld IS NOT NULL ';
95
-		} elseif ( false === $value ) {
95
+		} elseif (false === $value) {
96 96
 			$this->sql .= ' AND e.about_jsonld IS NULL ';
97 97
 		}
98 98
 
@@ -102,13 +102,13 @@  discard block
 block discarded – undo
102 102
 	protected function limit() {
103 103
 		$limit = $this->params['limit'];
104 104
 		global $wpdb;
105
-		$this->sql .= $wpdb->prepare( ' LIMIT %d', $limit );
105
+		$this->sql .= $wpdb->prepare(' LIMIT %d', $limit);
106 106
 		return $this;
107 107
 	}
108 108
 
109 109
 	protected function order_by() {
110 110
 		$direction  = $this->params['direction'];
111
-		$this->sql .= $this->sort->get_orderby_clause( $direction );
111
+		$this->sql .= $this->sort->get_orderby_clause($direction);
112 112
 		return $this;
113 113
 	}
114 114
 
Please login to merge, or discard this patch.
modules/dashboard/includes/Post_Entity_Match/Post_Entity_Match_Service.php 1 patch
Indentation   +57 added lines, -57 removed lines patch added patch discarded remove patch
@@ -8,71 +8,71 @@
 block discarded – undo
8 8
 
9 9
 class Post_Entity_Match_Service extends Match_Service {
10 10
 
11
-	public function list_items( $args ) {
12
-		global $wpdb;
11
+    public function list_items( $args ) {
12
+        global $wpdb;
13 13
 
14
-		$params = wp_parse_args(
15
-			$args,
16
-			array(
17
-				'position'  => null,
18
-				'element'   => 'INCLUDED',
19
-				'direction' => 'ASCENDING',
20
-				'limit'     => 10,
21
-				'sort'      => '+id',
22
-				'post_type' => null,
23
-				'has_match' => null,
24
-			)
25
-		);
26
-		/**
27
-		 * @var $sort Sort
28
-		 */
29
-		$sort = new Sort( $params['sort'] );
14
+        $params = wp_parse_args(
15
+            $args,
16
+            array(
17
+                'position'  => null,
18
+                'element'   => 'INCLUDED',
19
+                'direction' => 'ASCENDING',
20
+                'limit'     => 10,
21
+                'sort'      => '+id',
22
+                'post_type' => null,
23
+                'has_match' => null,
24
+            )
25
+        );
26
+        /**
27
+         * @var $sort Sort
28
+         */
29
+        $sort = new Sort( $params['sort'] );
30 30
 
31
-		$query_builder = new Query_Builder(
32
-			$params,
33
-			$sort
34
-		);
31
+        $query_builder = new Query_Builder(
32
+            $params,
33
+            $sort
34
+        );
35 35
 
36
-		$items = $wpdb->get_results(
37
-			// Each function above is preparing `$sql` by using `$wpdb->prepare`.
38
-			// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
39
-			$wpdb->prepare( $query_builder->get(), Object_Type_Enum::POST )
40
-		);
36
+        $items = $wpdb->get_results(
37
+            // Each function above is preparing `$sql` by using `$wpdb->prepare`.
38
+            // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
39
+            $wpdb->prepare( $query_builder->get(), Object_Type_Enum::POST )
40
+        );
41 41
 
42
-		$sort->apply( $items );
42
+        $sort->apply( $items );
43 43
 
44
-		return $this->map( $items );
45
-	}
44
+        return $this->map( $items );
45
+    }
46 46
 
47
-	/** Returns an array of rows where each row contains
48
-	 * 'post_title' => The title of the post
49
-	 * 'id'   => The id of the post
50
-	 * 'post_link' => The edit post link
51
-	 * 'parent_post_title' => The title of the post linked to this post via wprm_parent_post_id property
52
-	 * ( this is only applicable when the post is wprm_recipe, returns null if not present )
53
-	 * 'parent_post_id'  => The id of the linked parent post.
54
-	 * 'parent_post_link' => The link to parent post.
55
-	 * 'match_jsonld' => The matched `about_jsonld` column from wl_entities.
56
-	 * 'match_id' => This id points to id column of wl_entities table.
57
-	 */
58
-	private function map( array $items ) {
59
-		return array_map(
60
-			function ( $item ) {
61
-				$data             = json_decode( $item->match_jsonld, true );
62
-				$item->match_name = $data && is_array( $data ) && array_key_exists( 'name', $data ) ? $data['name'] : null;
47
+    /** Returns an array of rows where each row contains
48
+     * 'post_title' => The title of the post
49
+     * 'id'   => The id of the post
50
+     * 'post_link' => The edit post link
51
+     * 'parent_post_title' => The title of the post linked to this post via wprm_parent_post_id property
52
+     * ( this is only applicable when the post is wprm_recipe, returns null if not present )
53
+     * 'parent_post_id'  => The id of the linked parent post.
54
+     * 'parent_post_link' => The link to parent post.
55
+     * 'match_jsonld' => The matched `about_jsonld` column from wl_entities.
56
+     * 'match_id' => This id points to id column of wl_entities table.
57
+     */
58
+    private function map( array $items ) {
59
+        return array_map(
60
+            function ( $item ) {
61
+                $data             = json_decode( $item->match_jsonld, true );
62
+                $item->match_name = $data && is_array( $data ) && array_key_exists( 'name', $data ) ? $data['name'] : null;
63 63
 
64
-				if ( $item->id ) {
65
-					$item->post_link = get_edit_post_link( $item->id, 'ui' );
66
-				}
64
+                if ( $item->id ) {
65
+                    $item->post_link = get_edit_post_link( $item->id, 'ui' );
66
+                }
67 67
 
68
-				if ( $item->parent_post_id ) {
69
-					$item->parent_post_link = get_edit_post_link( $item->parent_post_id, 'ui' );
70
-				}
68
+                if ( $item->parent_post_id ) {
69
+                    $item->parent_post_link = get_edit_post_link( $item->parent_post_id, 'ui' );
70
+                }
71 71
 
72
-				return $item;
73
-			},
74
-			$items
75
-		);
76
-	}
72
+                return $item;
73
+            },
74
+            $items
75
+        );
76
+    }
77 77
 
78 78
 }
Please login to merge, or discard this patch.
dashboard/includes/Post_Entity_Match/Post_Entity_Match_Rest_Controller.php 1 patch
Indentation   +200 added lines, -200 removed lines patch added patch discarded remove patch
@@ -11,204 +11,204 @@
 block discarded – undo
11 11
 
12 12
 class Post_Entity_Match_Rest_Controller extends \WP_REST_Controller {
13 13
 
14
-	/**
15
-	 * @var Post_Entity_Match_Service
16
-	 */
17
-	private $match_service;
18
-
19
-	public function __construct( $match_service ) {
20
-		$this->match_service = $match_service;
21
-	}
22
-
23
-	public function register_hooks() {
24
-		add_action( 'rest_api_init', array( $this, 'register_routes' ) );
25
-	}
26
-
27
-	/**
28
-	 * Register the routes for the objects of the controller.
29
-	 */
30
-	public function register_routes() {
31
-
32
-		// Get term matches by taxonomy name
33
-		register_rest_route(
34
-			'wordlift/v1',
35
-			'/post-matches',
36
-			array(
37
-				'methods'             => 'GET',
38
-				'callback'            => array( $this, 'get_post_matches' ),
39
-				'args'                => array(
40
-					'cursor'     => array(
41
-						'type'              => 'string',
42
-						'default'           => Cursor::EMPTY_CURSOR_AS_BASE64_STRING,
43
-						'validate_callback' => 'rest_validate_request_arg',
44
-						'sanitize_callback' => array( Cursor::class, 'rest_sanitize_request_arg' ),
45
-					),
46
-					'limit'      => array(
47
-						'type'              => 'integer',
48
-						'validate_callback' => 'rest_validate_request_arg',
49
-						'default'           => 10,
50
-						'minimum'           => 1,
51
-						'maximum'           => 100,
52
-						'sanitize_callback' => 'absint',
53
-					),
54
-					'post_types' => array(
55
-						'type'              => 'array',
56
-						'validate_callback' => 'rest_validate_request_arg',
57
-					),
58
-					'has_match'  => array(
59
-						'type'              => 'boolean',
60
-						'required'          => false,
61
-						'validate_callback' => 'rest_validate_request_arg',
62
-					),
63
-				),
64
-				'permission_callback' => function () {
65
-					return current_user_can( 'manage_options' );
66
-				},
67
-			)
68
-		);
69
-
70
-		// Create a new match for a post
71
-		register_rest_route(
72
-			'wordlift/v1',
73
-			'/post-matches/(?P<post_id>\d+)/matches',
74
-			array(
75
-				'methods'             => 'POST',
76
-				'callback'            => array( $this, 'create_post_match' ),
77
-				'args'                => array(
78
-					'post_id' => array(
79
-						'required'          => true,
80
-						'validate_callback' => 'rest_validate_request_arg',
81
-					),
82
-				),
83
-				'permission_callback' => function () {
84
-					return current_user_can( 'manage_options' );
85
-				},
86
-			)
87
-		);
88
-
89
-		// Update an existing post match
90
-		register_rest_route(
91
-			'wordlift/v1',
92
-			'/post-matches/(?P<post_id>\d+)/matches/(?P<match_id>\d+)',
93
-			array(
94
-				'methods'             => 'PUT',
95
-				'callback'            => array( $this, 'update_post_match' ),
96
-				'args'                => array(
97
-					'post_id'  => array(
98
-						'required'          => true,
99
-						'validate_callback' => 'rest_validate_request_arg',
100
-					),
101
-					'match_id' => array(
102
-						'required'          => true,
103
-						'validate_callback' => 'rest_validate_request_arg',
104
-					),
105
-				),
106
-				'permission_callback' => function () {
107
-
108
-					return current_user_can( 'manage_options' );
109
-				},
110
-			)
111
-		);
112
-
113
-	}
114
-
115
-	/**
116
-	 * Get the term matches by taxonomy name.
117
-	 *
118
-	 * @var $request \WP_REST_Request
119
-	 */
120
-	public function get_post_matches( $request ) {
121
-
122
-		$cursor = $request->get_param( 'cursor' );
123
-		if ( $request->has_param( 'limit' ) ) {
124
-			$cursor['limit'] = $request->get_param( 'limit' );
125
-		}
126
-		if ( $request->has_param( 'sort' ) ) {
127
-			$cursor['sort'] = $request->get_param( 'sort' );
128
-		}
129
-		if ( $request->has_param( 'post_types' ) ) {
130
-			$cursor['query']['post_types'] = $request->get_param( 'post_types' );
131
-		}
132
-		if ( $request->has_param( 'has_match' ) ) {
133
-			$cursor['query']['has_match'] = $request->get_param( 'has_match' );
134
-		}
135
-
136
-		// Query.
137
-		$post_types = isset( $cursor['query']['post_types'] ) ? $cursor['query']['post_types'] : apply_filters(
138
-			'wl_dashboard__post_entity_match__post_types',
139
-			array(
140
-				'post',
141
-				'page',
142
-			)
143
-		);
144
-		$has_match  = isset( $cursor['query']['has_match'] ) ? $cursor['query']['has_match'] : null;
145
-
146
-		$items = $this->match_service->list_items(
147
-			array(
148
-				// Query
149
-				'post_types' => $post_types,
150
-				'has_match'  => $has_match,
151
-				// Cursor-Pagination
152
-				'position'   => $cursor['position'],
153
-				'element'    => $cursor['element'],
154
-				'direction'  => $cursor['direction'],
155
-				// `+1` to check if we have other results.
156
-				'limit'      => $cursor['limit'] + 1,
157
-				'sort'       => $cursor['sort'],
158
-			)
159
-		);
160
-
161
-		return new Cursor_Page(
162
-			$items,
163
-			$cursor['position'],
164
-			$cursor['element'],
165
-			$cursor['direction'],
166
-			$cursor['sort'],
167
-			$cursor['limit'],
168
-			$cursor['query']
169
-		);
170
-	}
171
-
172
-	/**
173
-	 * Create a new match for a post.
174
-	 *
175
-	 * @var $request \WP_REST_Request
176
-	 */
177
-	public function create_post_match( $request ) {
178
-		$post_id = $request->get_param( 'post_id' );
179
-
180
-		// If we dont have a entry on the match table, then add one.
181
-		$content_id = Wordpress_Content_Id::create_post( $post_id );
182
-		if ( ! Wordpress_Content_Service::get_instance()
183
-										->get_entity_id( $content_id ) ) {
184
-			$uri = Entity_Uri_Generator::create_uri( $content_id->get_type(), $content_id->get_id() );
185
-			Wordpress_Content_Service::get_instance()->set_entity_id( $content_id, $uri );
186
-		}
187
-
188
-		$match_id = $this->match_service->get_id(
189
-			$post_id,
190
-			Object_Type_Enum::POST
191
-		);
192
-
193
-		return $this->match_service->set_jsonld(
194
-			$post_id,
195
-			Object_Type_Enum::POST,
196
-			$match_id,
197
-			$request->get_json_params()
198
-		);
199
-
200
-	}
201
-
202
-	/**
203
-	 * @var $request \WP_REST_Request
204
-	 */
205
-	public function update_post_match( $request ) {
206
-
207
-		return $this->match_service->set_jsonld(
208
-			$request->get_param( 'post_id' ),
209
-			Object_Type_Enum::POST,
210
-			$request->get_param( 'match_id' ),
211
-			$request->get_json_params()
212
-		);
213
-	}
14
+    /**
15
+     * @var Post_Entity_Match_Service
16
+     */
17
+    private $match_service;
18
+
19
+    public function __construct( $match_service ) {
20
+        $this->match_service = $match_service;
21
+    }
22
+
23
+    public function register_hooks() {
24
+        add_action( 'rest_api_init', array( $this, 'register_routes' ) );
25
+    }
26
+
27
+    /**
28
+     * Register the routes for the objects of the controller.
29
+     */
30
+    public function register_routes() {
31
+
32
+        // Get term matches by taxonomy name
33
+        register_rest_route(
34
+            'wordlift/v1',
35
+            '/post-matches',
36
+            array(
37
+                'methods'             => 'GET',
38
+                'callback'            => array( $this, 'get_post_matches' ),
39
+                'args'                => array(
40
+                    'cursor'     => array(
41
+                        'type'              => 'string',
42
+                        'default'           => Cursor::EMPTY_CURSOR_AS_BASE64_STRING,
43
+                        'validate_callback' => 'rest_validate_request_arg',
44
+                        'sanitize_callback' => array( Cursor::class, 'rest_sanitize_request_arg' ),
45
+                    ),
46
+                    'limit'      => array(
47
+                        'type'              => 'integer',
48
+                        'validate_callback' => 'rest_validate_request_arg',
49
+                        'default'           => 10,
50
+                        'minimum'           => 1,
51
+                        'maximum'           => 100,
52
+                        'sanitize_callback' => 'absint',
53
+                    ),
54
+                    'post_types' => array(
55
+                        'type'              => 'array',
56
+                        'validate_callback' => 'rest_validate_request_arg',
57
+                    ),
58
+                    'has_match'  => array(
59
+                        'type'              => 'boolean',
60
+                        'required'          => false,
61
+                        'validate_callback' => 'rest_validate_request_arg',
62
+                    ),
63
+                ),
64
+                'permission_callback' => function () {
65
+                    return current_user_can( 'manage_options' );
66
+                },
67
+            )
68
+        );
69
+
70
+        // Create a new match for a post
71
+        register_rest_route(
72
+            'wordlift/v1',
73
+            '/post-matches/(?P<post_id>\d+)/matches',
74
+            array(
75
+                'methods'             => 'POST',
76
+                'callback'            => array( $this, 'create_post_match' ),
77
+                'args'                => array(
78
+                    'post_id' => array(
79
+                        'required'          => true,
80
+                        'validate_callback' => 'rest_validate_request_arg',
81
+                    ),
82
+                ),
83
+                'permission_callback' => function () {
84
+                    return current_user_can( 'manage_options' );
85
+                },
86
+            )
87
+        );
88
+
89
+        // Update an existing post match
90
+        register_rest_route(
91
+            'wordlift/v1',
92
+            '/post-matches/(?P<post_id>\d+)/matches/(?P<match_id>\d+)',
93
+            array(
94
+                'methods'             => 'PUT',
95
+                'callback'            => array( $this, 'update_post_match' ),
96
+                'args'                => array(
97
+                    'post_id'  => array(
98
+                        'required'          => true,
99
+                        'validate_callback' => 'rest_validate_request_arg',
100
+                    ),
101
+                    'match_id' => array(
102
+                        'required'          => true,
103
+                        'validate_callback' => 'rest_validate_request_arg',
104
+                    ),
105
+                ),
106
+                'permission_callback' => function () {
107
+
108
+                    return current_user_can( 'manage_options' );
109
+                },
110
+            )
111
+        );
112
+
113
+    }
114
+
115
+    /**
116
+     * Get the term matches by taxonomy name.
117
+     *
118
+     * @var $request \WP_REST_Request
119
+     */
120
+    public function get_post_matches( $request ) {
121
+
122
+        $cursor = $request->get_param( 'cursor' );
123
+        if ( $request->has_param( 'limit' ) ) {
124
+            $cursor['limit'] = $request->get_param( 'limit' );
125
+        }
126
+        if ( $request->has_param( 'sort' ) ) {
127
+            $cursor['sort'] = $request->get_param( 'sort' );
128
+        }
129
+        if ( $request->has_param( 'post_types' ) ) {
130
+            $cursor['query']['post_types'] = $request->get_param( 'post_types' );
131
+        }
132
+        if ( $request->has_param( 'has_match' ) ) {
133
+            $cursor['query']['has_match'] = $request->get_param( 'has_match' );
134
+        }
135
+
136
+        // Query.
137
+        $post_types = isset( $cursor['query']['post_types'] ) ? $cursor['query']['post_types'] : apply_filters(
138
+            'wl_dashboard__post_entity_match__post_types',
139
+            array(
140
+                'post',
141
+                'page',
142
+            )
143
+        );
144
+        $has_match  = isset( $cursor['query']['has_match'] ) ? $cursor['query']['has_match'] : null;
145
+
146
+        $items = $this->match_service->list_items(
147
+            array(
148
+                // Query
149
+                'post_types' => $post_types,
150
+                'has_match'  => $has_match,
151
+                // Cursor-Pagination
152
+                'position'   => $cursor['position'],
153
+                'element'    => $cursor['element'],
154
+                'direction'  => $cursor['direction'],
155
+                // `+1` to check if we have other results.
156
+                'limit'      => $cursor['limit'] + 1,
157
+                'sort'       => $cursor['sort'],
158
+            )
159
+        );
160
+
161
+        return new Cursor_Page(
162
+            $items,
163
+            $cursor['position'],
164
+            $cursor['element'],
165
+            $cursor['direction'],
166
+            $cursor['sort'],
167
+            $cursor['limit'],
168
+            $cursor['query']
169
+        );
170
+    }
171
+
172
+    /**
173
+     * Create a new match for a post.
174
+     *
175
+     * @var $request \WP_REST_Request
176
+     */
177
+    public function create_post_match( $request ) {
178
+        $post_id = $request->get_param( 'post_id' );
179
+
180
+        // If we dont have a entry on the match table, then add one.
181
+        $content_id = Wordpress_Content_Id::create_post( $post_id );
182
+        if ( ! Wordpress_Content_Service::get_instance()
183
+                                        ->get_entity_id( $content_id ) ) {
184
+            $uri = Entity_Uri_Generator::create_uri( $content_id->get_type(), $content_id->get_id() );
185
+            Wordpress_Content_Service::get_instance()->set_entity_id( $content_id, $uri );
186
+        }
187
+
188
+        $match_id = $this->match_service->get_id(
189
+            $post_id,
190
+            Object_Type_Enum::POST
191
+        );
192
+
193
+        return $this->match_service->set_jsonld(
194
+            $post_id,
195
+            Object_Type_Enum::POST,
196
+            $match_id,
197
+            $request->get_json_params()
198
+        );
199
+
200
+    }
201
+
202
+    /**
203
+     * @var $request \WP_REST_Request
204
+     */
205
+    public function update_post_match( $request ) {
206
+
207
+        return $this->match_service->set_jsonld(
208
+            $request->get_param( 'post_id' ),
209
+            Object_Type_Enum::POST,
210
+            $request->get_param( 'match_id' ),
211
+            $request->get_json_params()
212
+        );
213
+    }
214 214
 }
Please login to merge, or discard this patch.