@@ -8,76 +8,76 @@ |
||
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 | - 'post_status' => null, |
|
25 | - ) |
|
26 | - ); |
|
27 | - /** |
|
28 | - * @var $sort Sort |
|
29 | - */ |
|
30 | - $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 | + 'post_status' => null, |
|
25 | + ) |
|
26 | + ); |
|
27 | + /** |
|
28 | + * @var $sort Sort |
|
29 | + */ |
|
30 | + $sort = new Sort( $params['sort'] ); |
|
31 | 31 | |
32 | - $query_builder = new Query_Builder( |
|
33 | - $params, |
|
34 | - $sort |
|
35 | - ); |
|
32 | + $query_builder = new Query_Builder( |
|
33 | + $params, |
|
34 | + $sort |
|
35 | + ); |
|
36 | 36 | |
37 | - $items = $wpdb->get_results( |
|
38 | - // Each function above is preparing `$sql` by using `$wpdb->prepare`. |
|
39 | - // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
40 | - $wpdb->prepare( $query_builder->get(), Object_Type_Enum::POST ) |
|
41 | - ); |
|
37 | + $items = $wpdb->get_results( |
|
38 | + // Each function above is preparing `$sql` by using `$wpdb->prepare`. |
|
39 | + // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
40 | + $wpdb->prepare( $query_builder->get(), Object_Type_Enum::POST ) |
|
41 | + ); |
|
42 | 42 | |
43 | - $sort->apply( $items ); |
|
43 | + $sort->apply( $items ); |
|
44 | 44 | |
45 | - return $this->map( $items ); |
|
46 | - } |
|
45 | + return $this->map( $items ); |
|
46 | + } |
|
47 | 47 | |
48 | - /** Returns an array of rows where each row contains |
|
49 | - * 'post_title' => The title of the post |
|
50 | - * 'id' => The id of the post |
|
51 | - * 'post_link' => The edit post link |
|
48 | + /** Returns an array of rows where each row contains |
|
49 | + * 'post_title' => The title of the post |
|
50 | + * 'id' => The id of the post |
|
51 | + * 'post_link' => The edit post link |
|
52 | 52 | * 'post_status' => The status of the post. |
53 | - * 'parent_post_title' => The title of the post linked to this post via wprm_parent_post_id property |
|
54 | - * ( this is only applicable when the post is wprm_recipe, returns null if not present ) |
|
55 | - * 'parent_post_id' => The id of the linked parent post. |
|
56 | - * 'parent_post_link' => The link to parent post. |
|
53 | + * 'parent_post_title' => The title of the post linked to this post via wprm_parent_post_id property |
|
54 | + * ( this is only applicable when the post is wprm_recipe, returns null if not present ) |
|
55 | + * 'parent_post_id' => The id of the linked parent post. |
|
56 | + * 'parent_post_link' => The link to parent post. |
|
57 | 57 | * 'view link' => The permalink to the post. |
58 | 58 | * 'preview link' => The preview link to the post. |
59 | - * 'match_jsonld' => The matched `about_jsonld` column from wl_entities. |
|
60 | - * 'match_id' => This id points to id column of wl_entities table. |
|
61 | - */ |
|
62 | - private function map( array $items ) { |
|
63 | - return array_map( |
|
64 | - function ( $item ) { |
|
65 | - $data = json_decode( $item->match_jsonld, true ); |
|
66 | - $item->match_name = $data && is_array( $data ) && array_key_exists( 'name', $data ) ? $data['name'] : null; |
|
59 | + * 'match_jsonld' => The matched `about_jsonld` column from wl_entities. |
|
60 | + * 'match_id' => This id points to id column of wl_entities table. |
|
61 | + */ |
|
62 | + private function map( array $items ) { |
|
63 | + return array_map( |
|
64 | + function ( $item ) { |
|
65 | + $data = json_decode( $item->match_jsonld, true ); |
|
66 | + $item->match_name = $data && is_array( $data ) && array_key_exists( 'name', $data ) ? $data['name'] : null; |
|
67 | 67 | |
68 | - if ( $item->id ) { |
|
69 | - $item->post_link = get_edit_post_link( $item->id, 'ui' ); |
|
70 | - $item->view_link = get_permalink( $item->id ); |
|
71 | - $item->preview_link = get_preview_post_link( $item->id ); |
|
72 | - } |
|
68 | + if ( $item->id ) { |
|
69 | + $item->post_link = get_edit_post_link( $item->id, 'ui' ); |
|
70 | + $item->view_link = get_permalink( $item->id ); |
|
71 | + $item->preview_link = get_preview_post_link( $item->id ); |
|
72 | + } |
|
73 | 73 | |
74 | - if ( $item->parent_post_id ) { |
|
75 | - $item->parent_post_link = get_edit_post_link( $item->parent_post_id, 'ui' ); |
|
76 | - } |
|
74 | + if ( $item->parent_post_id ) { |
|
75 | + $item->parent_post_link = get_edit_post_link( $item->parent_post_id, 'ui' ); |
|
76 | + } |
|
77 | 77 | |
78 | - return $item; |
|
79 | - }, |
|
80 | - $items |
|
81 | - ); |
|
82 | - } |
|
78 | + return $item; |
|
79 | + }, |
|
80 | + $items |
|
81 | + ); |
|
82 | + } |
|
83 | 83 | } |
@@ -8,7 +8,7 @@ discard block |
||
8 | 8 | |
9 | 9 | class Post_Entity_Match_Service extends Match_Service { |
10 | 10 | |
11 | - public function list_items( $args ) { |
|
11 | + public function list_items($args) { |
|
12 | 12 | global $wpdb; |
13 | 13 | |
14 | 14 | $params = wp_parse_args( |
@@ -27,7 +27,7 @@ discard block |
||
27 | 27 | /** |
28 | 28 | * @var $sort Sort |
29 | 29 | */ |
30 | - $sort = new Sort( $params['sort'] ); |
|
30 | + $sort = new Sort($params['sort']); |
|
31 | 31 | |
32 | 32 | $query_builder = new Query_Builder( |
33 | 33 | $params, |
@@ -37,12 +37,12 @@ discard block |
||
37 | 37 | $items = $wpdb->get_results( |
38 | 38 | // Each function above is preparing `$sql` by using `$wpdb->prepare`. |
39 | 39 | // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
40 | - $wpdb->prepare( $query_builder->get(), Object_Type_Enum::POST ) |
|
40 | + $wpdb->prepare($query_builder->get(), Object_Type_Enum::POST) |
|
41 | 41 | ); |
42 | 42 | |
43 | - $sort->apply( $items ); |
|
43 | + $sort->apply($items); |
|
44 | 44 | |
45 | - return $this->map( $items ); |
|
45 | + return $this->map($items); |
|
46 | 46 | } |
47 | 47 | |
48 | 48 | /** Returns an array of rows where each row contains |
@@ -59,20 +59,20 @@ discard block |
||
59 | 59 | * 'match_jsonld' => The matched `about_jsonld` column from wl_entities. |
60 | 60 | * 'match_id' => This id points to id column of wl_entities table. |
61 | 61 | */ |
62 | - private function map( array $items ) { |
|
62 | + private function map(array $items) { |
|
63 | 63 | return array_map( |
64 | - function ( $item ) { |
|
65 | - $data = json_decode( $item->match_jsonld, true ); |
|
66 | - $item->match_name = $data && is_array( $data ) && array_key_exists( 'name', $data ) ? $data['name'] : null; |
|
64 | + function($item) { |
|
65 | + $data = json_decode($item->match_jsonld, true); |
|
66 | + $item->match_name = $data && is_array($data) && array_key_exists('name', $data) ? $data['name'] : null; |
|
67 | 67 | |
68 | - if ( $item->id ) { |
|
69 | - $item->post_link = get_edit_post_link( $item->id, 'ui' ); |
|
70 | - $item->view_link = get_permalink( $item->id ); |
|
71 | - $item->preview_link = get_preview_post_link( $item->id ); |
|
68 | + if ($item->id) { |
|
69 | + $item->post_link = get_edit_post_link($item->id, 'ui'); |
|
70 | + $item->view_link = get_permalink($item->id); |
|
71 | + $item->preview_link = get_preview_post_link($item->id); |
|
72 | 72 | } |
73 | 73 | |
74 | - if ( $item->parent_post_id ) { |
|
75 | - $item->parent_post_link = get_edit_post_link( $item->parent_post_id, 'ui' ); |
|
74 | + if ($item->parent_post_id) { |
|
75 | + $item->parent_post_link = get_edit_post_link($item->parent_post_id, 'ui'); |
|
76 | 76 | } |
77 | 77 | |
78 | 78 | return $item; |
@@ -16,25 +16,25 @@ discard block |
||
16 | 16 | */ |
17 | 17 | class Query_Builder extends Match_Query_Builder { |
18 | 18 | |
19 | - public function build() { |
|
20 | - |
|
21 | - global $wpdb; |
|
22 | - /** |
|
23 | - * Why not use JSON_EXTRACT() to extract the match_name ? |
|
24 | - * As of now the min wp compatibility is 5.3 which requires min mysql version |
|
25 | - * 5.6, The JSON_* functions are introduced on 5.7 which will break the |
|
26 | - * compatibility. |
|
27 | - * |
|
28 | - * Returns an array of rows where each row contains |
|
29 | - * 'post_title' => The title of the post |
|
30 | - * 'id' => The id of the post |
|
31 | - * 'parent_post_title' => The title of the post linked to this post via wprm_parent_post_id property |
|
32 | - * ( this is only applicable when the post is wprm_recipe, returns null if not present ) |
|
33 | - * 'parent_post_id' => The id of the linked parent post. |
|
34 | - * 'match_jsonld' => The matched `about_jsonld` column from wl_entities. |
|
35 | - * 'match_id' => This id points to id column of wl_entities table. |
|
36 | - */ |
|
37 | - $this->sql = " |
|
19 | + public function build() { |
|
20 | + |
|
21 | + global $wpdb; |
|
22 | + /** |
|
23 | + * Why not use JSON_EXTRACT() to extract the match_name ? |
|
24 | + * As of now the min wp compatibility is 5.3 which requires min mysql version |
|
25 | + * 5.6, The JSON_* functions are introduced on 5.7 which will break the |
|
26 | + * compatibility. |
|
27 | + * |
|
28 | + * Returns an array of rows where each row contains |
|
29 | + * 'post_title' => The title of the post |
|
30 | + * 'id' => The id of the post |
|
31 | + * 'parent_post_title' => The title of the post linked to this post via wprm_parent_post_id property |
|
32 | + * ( this is only applicable when the post is wprm_recipe, returns null if not present ) |
|
33 | + * 'parent_post_id' => The id of the linked parent post. |
|
34 | + * 'match_jsonld' => The matched `about_jsonld` column from wl_entities. |
|
35 | + * 'match_id' => This id points to id column of wl_entities table. |
|
36 | + */ |
|
37 | + $this->sql = " |
|
38 | 38 | SELECT p.ID as id, |
39 | 39 | p.post_title as post_title, |
40 | 40 | p.post_status as post_status, |
@@ -49,48 +49,48 @@ discard block |
||
49 | 49 | WHERE 1=1 |
50 | 50 | "; |
51 | 51 | |
52 | - $this->cursor() |
|
53 | - ->post_type() |
|
54 | - ->post_status() |
|
55 | - ->has_match() |
|
56 | - ->order_by() |
|
57 | - ->limit(); |
|
58 | - } |
|
59 | - |
|
60 | - private function post_status() { |
|
61 | - global $wpdb; |
|
62 | - |
|
63 | - // If a value has been provided and it's either 'draft' or 'publish', we add the related filter. |
|
64 | - if ( is_string( $this->params['post_status'] ) && in_array( |
|
65 | - $this->params['post_status'], |
|
66 | - array( |
|
67 | - 'publish', |
|
68 | - 'draft', |
|
69 | - ), |
|
70 | - true |
|
71 | - ) ) { |
|
72 | - |
|
73 | - $this->sql .= $wpdb->prepare( ' AND p.post_status = %s', $this->params['post_status'] ); |
|
74 | - |
|
75 | - return $this; |
|
76 | - } |
|
77 | - |
|
78 | - // By default we filter on 'draft' and 'publish'. |
|
79 | - $this->sql .= " AND p.post_status IN ( 'draft', 'publish' )"; |
|
80 | - |
|
81 | - return $this; |
|
82 | - } |
|
83 | - |
|
84 | - public function post_type() { |
|
85 | - $post_types = $this->params['post_types']; |
|
86 | - |
|
87 | - if ( ! isset( $post_types ) ) { |
|
88 | - return $this; |
|
89 | - } |
|
90 | - $post_types_sql = Escape::sql_array( $post_types ); |
|
91 | - $this->sql .= " AND p.post_type IN ({$post_types_sql}) "; |
|
92 | - |
|
93 | - return $this; |
|
94 | - } |
|
52 | + $this->cursor() |
|
53 | + ->post_type() |
|
54 | + ->post_status() |
|
55 | + ->has_match() |
|
56 | + ->order_by() |
|
57 | + ->limit(); |
|
58 | + } |
|
59 | + |
|
60 | + private function post_status() { |
|
61 | + global $wpdb; |
|
62 | + |
|
63 | + // If a value has been provided and it's either 'draft' or 'publish', we add the related filter. |
|
64 | + if ( is_string( $this->params['post_status'] ) && in_array( |
|
65 | + $this->params['post_status'], |
|
66 | + array( |
|
67 | + 'publish', |
|
68 | + 'draft', |
|
69 | + ), |
|
70 | + true |
|
71 | + ) ) { |
|
72 | + |
|
73 | + $this->sql .= $wpdb->prepare( ' AND p.post_status = %s', $this->params['post_status'] ); |
|
74 | + |
|
75 | + return $this; |
|
76 | + } |
|
77 | + |
|
78 | + // By default we filter on 'draft' and 'publish'. |
|
79 | + $this->sql .= " AND p.post_status IN ( 'draft', 'publish' )"; |
|
80 | + |
|
81 | + return $this; |
|
82 | + } |
|
83 | + |
|
84 | + public function post_type() { |
|
85 | + $post_types = $this->params['post_types']; |
|
86 | + |
|
87 | + if ( ! isset( $post_types ) ) { |
|
88 | + return $this; |
|
89 | + } |
|
90 | + $post_types_sql = Escape::sql_array( $post_types ); |
|
91 | + $this->sql .= " AND p.post_type IN ({$post_types_sql}) "; |
|
92 | + |
|
93 | + return $this; |
|
94 | + } |
|
95 | 95 | |
96 | 96 | } |
@@ -61,16 +61,16 @@ discard block |
||
61 | 61 | global $wpdb; |
62 | 62 | |
63 | 63 | // If a value has been provided and it's either 'draft' or 'publish', we add the related filter. |
64 | - if ( is_string( $this->params['post_status'] ) && in_array( |
|
64 | + if (is_string($this->params['post_status']) && in_array( |
|
65 | 65 | $this->params['post_status'], |
66 | 66 | array( |
67 | 67 | 'publish', |
68 | 68 | 'draft', |
69 | 69 | ), |
70 | 70 | true |
71 | - ) ) { |
|
71 | + )) { |
|
72 | 72 | |
73 | - $this->sql .= $wpdb->prepare( ' AND p.post_status = %s', $this->params['post_status'] ); |
|
73 | + $this->sql .= $wpdb->prepare(' AND p.post_status = %s', $this->params['post_status']); |
|
74 | 74 | |
75 | 75 | return $this; |
76 | 76 | } |
@@ -84,10 +84,10 @@ discard block |
||
84 | 84 | public function post_type() { |
85 | 85 | $post_types = $this->params['post_types']; |
86 | 86 | |
87 | - if ( ! isset( $post_types ) ) { |
|
87 | + if ( ! isset($post_types)) { |
|
88 | 88 | return $this; |
89 | 89 | } |
90 | - $post_types_sql = Escape::sql_array( $post_types ); |
|
90 | + $post_types_sql = Escape::sql_array($post_types); |
|
91 | 91 | $this->sql .= " AND p.post_type IN ({$post_types_sql}) "; |
92 | 92 | |
93 | 93 | return $this; |
@@ -6,17 +6,17 @@ |
||
6 | 6 | |
7 | 7 | class Sort extends Match_Sort { |
8 | 8 | |
9 | - public function get_field_name() { |
|
10 | - $tmp_sort_field_name = substr( $this->sort, 1 ); |
|
11 | - if ( 'id' === $tmp_sort_field_name ) { |
|
12 | - return 'p.ID'; |
|
13 | - } |
|
9 | + public function get_field_name() { |
|
10 | + $tmp_sort_field_name = substr( $this->sort, 1 ); |
|
11 | + if ( 'id' === $tmp_sort_field_name ) { |
|
12 | + return 'p.ID'; |
|
13 | + } |
|
14 | 14 | elseif ('date_modified_gmt' === $tmp_sort_field_name) { |
15 | 15 | return 'p.post_modified_gmt'; |
16 | 16 | } |
17 | 17 | else { |
18 | - return 'p.post_title'; |
|
19 | - } |
|
20 | - } |
|
18 | + return 'p.post_title'; |
|
19 | + } |
|
20 | + } |
|
21 | 21 | |
22 | 22 | } |
@@ -7,8 +7,8 @@ |
||
7 | 7 | class Sort extends Match_Sort { |
8 | 8 | |
9 | 9 | public function get_field_name() { |
10 | - $tmp_sort_field_name = substr( $this->sort, 1 ); |
|
11 | - if ( 'id' === $tmp_sort_field_name ) { |
|
10 | + $tmp_sort_field_name = substr($this->sort, 1); |
|
11 | + if ('id' === $tmp_sort_field_name) { |
|
12 | 12 | return 'p.ID'; |
13 | 13 | } |
14 | 14 | elseif ('date_modified_gmt' === $tmp_sort_field_name) { |
@@ -10,11 +10,9 @@ |
||
10 | 10 | $tmp_sort_field_name = substr( $this->sort, 1 ); |
11 | 11 | if ( 'id' === $tmp_sort_field_name ) { |
12 | 12 | return 'p.ID'; |
13 | - } |
|
14 | - elseif ('date_modified_gmt' === $tmp_sort_field_name) { |
|
13 | + } elseif ('date_modified_gmt' === $tmp_sort_field_name) { |
|
15 | 14 | return 'p.post_modified_gmt'; |
16 | - } |
|
17 | - else { |
|
15 | + } else { |
|
18 | 16 | return 'p.post_title'; |
19 | 17 | } |
20 | 18 | } |
@@ -11,221 +11,221 @@ |
||
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 | - 'post_status' => array( |
|
64 | - 'type' => 'string', |
|
65 | - 'required' => false, |
|
66 | - 'enum' => array( 'publish', 'draft' ), |
|
67 | - 'validate_callback' => 'rest_validate_request_arg', |
|
68 | - ), |
|
69 | - 'sort' => array( |
|
70 | - 'type' => 'string', |
|
71 | - 'required' => false, |
|
72 | - 'enum' => array( '+date_modified_gmt', '-date_modified_gmt' ), |
|
73 | - 'validate_callback' => 'rest_validate_request_arg', |
|
74 | - 'default' => '+date_modified_gmt', |
|
75 | - ), |
|
76 | - ), |
|
77 | - 'permission_callback' => function () { |
|
78 | - return current_user_can( 'manage_options' ); |
|
79 | - }, |
|
80 | - ) |
|
81 | - ); |
|
82 | - |
|
83 | - // Create a new match for a post |
|
84 | - register_rest_route( |
|
85 | - 'wordlift/v1', |
|
86 | - '/post-matches/(?P<post_id>\d+)/matches', |
|
87 | - array( |
|
88 | - 'methods' => 'POST', |
|
89 | - 'callback' => array( $this, 'create_post_match' ), |
|
90 | - 'args' => array( |
|
91 | - 'post_id' => array( |
|
92 | - 'required' => true, |
|
93 | - 'validate_callback' => 'rest_validate_request_arg', |
|
94 | - ), |
|
95 | - ), |
|
96 | - 'permission_callback' => function () { |
|
97 | - return current_user_can( 'manage_options' ); |
|
98 | - }, |
|
99 | - ) |
|
100 | - ); |
|
101 | - |
|
102 | - // Update an existing post match |
|
103 | - register_rest_route( |
|
104 | - 'wordlift/v1', |
|
105 | - '/post-matches/(?P<post_id>\d+)/matches/(?P<match_id>\d+)', |
|
106 | - array( |
|
107 | - 'methods' => 'PUT', |
|
108 | - 'callback' => array( $this, 'update_post_match' ), |
|
109 | - 'args' => array( |
|
110 | - 'post_id' => array( |
|
111 | - 'required' => true, |
|
112 | - 'validate_callback' => 'rest_validate_request_arg', |
|
113 | - ), |
|
114 | - 'match_id' => array( |
|
115 | - 'required' => true, |
|
116 | - 'validate_callback' => 'rest_validate_request_arg', |
|
117 | - ), |
|
118 | - ), |
|
119 | - 'permission_callback' => function () { |
|
120 | - |
|
121 | - return current_user_can( 'manage_options' ); |
|
122 | - }, |
|
123 | - ) |
|
124 | - ); |
|
125 | - } |
|
126 | - |
|
127 | - /** |
|
128 | - * Get the term matches by taxonomy name. |
|
129 | - * |
|
130 | - * @var $request \WP_REST_Request |
|
131 | - */ |
|
132 | - public function get_post_matches( $request ) { |
|
133 | - |
|
134 | - $cursor = $request->get_param( 'cursor' ); |
|
135 | - if ( $request->has_param( 'limit' ) ) { |
|
136 | - $cursor['limit'] = $request->get_param( 'limit' ); |
|
137 | - } |
|
138 | - if ( $request->has_param( 'sort' ) ) { |
|
139 | - $cursor['sort'] = $request->get_param( 'sort' ); |
|
140 | - } |
|
141 | - if ( $request->has_param( 'post_types' ) ) { |
|
142 | - $cursor['query']['post_types'] = $request->get_param( 'post_types' ); |
|
143 | - } |
|
144 | - if ( $request->has_param( 'has_match' ) ) { |
|
145 | - $cursor['query']['has_match'] = $request->get_param( 'has_match' ); |
|
146 | - } |
|
147 | - if ( $request->has_param( 'post_status' ) ) { |
|
148 | - $cursor['query']['post_status'] = $request->get_param( 'post_status' ); |
|
149 | - } |
|
150 | - |
|
151 | - // Query. |
|
152 | - $post_types = isset( $cursor['query']['post_types'] ) ? $cursor['query']['post_types'] : apply_filters( |
|
153 | - 'wl_dashboard__post_entity_match__post_types', |
|
154 | - array( |
|
155 | - 'post', |
|
156 | - 'page', |
|
157 | - ) |
|
158 | - ); |
|
159 | - $has_match = isset( $cursor['query']['has_match'] ) ? $cursor['query']['has_match'] : null; |
|
160 | - |
|
161 | - $post_status = isset( $cursor['query']['post_status'] ) ? $cursor['query']['post_status'] : null; |
|
162 | - |
|
163 | - $items = $this->match_service->list_items( |
|
164 | - array( |
|
165 | - // Query |
|
166 | - 'post_types' => $post_types, |
|
167 | - 'has_match' => $has_match, |
|
168 | - 'post_status' => $post_status, |
|
169 | - // Cursor-Pagination |
|
170 | - 'position' => $cursor['position'], |
|
171 | - 'element' => $cursor['element'], |
|
172 | - 'direction' => $cursor['direction'], |
|
173 | - // `+1` to check if we have other results. |
|
174 | - 'limit' => $cursor['limit'] + 1, |
|
175 | - 'sort' => $cursor['sort'], |
|
176 | - ) |
|
177 | - ); |
|
178 | - |
|
179 | - return new Cursor_Page( |
|
180 | - $items, |
|
181 | - $cursor['position'], |
|
182 | - $cursor['element'], |
|
183 | - $cursor['direction'], |
|
184 | - $cursor['sort'], |
|
185 | - $cursor['limit'], |
|
186 | - $cursor['query'] |
|
187 | - ); |
|
188 | - } |
|
189 | - |
|
190 | - /** |
|
191 | - * Create a new match for a post. |
|
192 | - * |
|
193 | - * @var $request \WP_REST_Request |
|
194 | - */ |
|
195 | - public function create_post_match( $request ) { |
|
196 | - $post_id = $request->get_param( 'post_id' ); |
|
197 | - |
|
198 | - // If we dont have a entry on the match table, then add one. |
|
199 | - $content_id = Wordpress_Content_Id::create_post( $post_id ); |
|
200 | - if ( ! Wordpress_Content_Service::get_instance() |
|
201 | - ->get_entity_id( $content_id ) ) { |
|
202 | - $uri = Entity_Uri_Generator::create_uri( $content_id->get_type(), $content_id->get_id() ); |
|
203 | - Wordpress_Content_Service::get_instance()->set_entity_id( $content_id, $uri ); |
|
204 | - } |
|
205 | - |
|
206 | - $match_id = $this->match_service->get_id( |
|
207 | - $post_id, |
|
208 | - Object_Type_Enum::POST |
|
209 | - ); |
|
210 | - |
|
211 | - return $this->match_service->set_jsonld( |
|
212 | - $post_id, |
|
213 | - Object_Type_Enum::POST, |
|
214 | - $match_id, |
|
215 | - $request->get_json_params() |
|
216 | - ); |
|
217 | - } |
|
218 | - |
|
219 | - /** |
|
220 | - * @var $request \WP_REST_Request |
|
221 | - */ |
|
222 | - public function update_post_match( $request ) { |
|
223 | - |
|
224 | - return $this->match_service->set_jsonld( |
|
225 | - $request->get_param( 'post_id' ), |
|
226 | - Object_Type_Enum::POST, |
|
227 | - $request->get_param( 'match_id' ), |
|
228 | - $request->get_json_params() |
|
229 | - ); |
|
230 | - } |
|
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 | + 'post_status' => array( |
|
64 | + 'type' => 'string', |
|
65 | + 'required' => false, |
|
66 | + 'enum' => array( 'publish', 'draft' ), |
|
67 | + 'validate_callback' => 'rest_validate_request_arg', |
|
68 | + ), |
|
69 | + 'sort' => array( |
|
70 | + 'type' => 'string', |
|
71 | + 'required' => false, |
|
72 | + 'enum' => array( '+date_modified_gmt', '-date_modified_gmt' ), |
|
73 | + 'validate_callback' => 'rest_validate_request_arg', |
|
74 | + 'default' => '+date_modified_gmt', |
|
75 | + ), |
|
76 | + ), |
|
77 | + 'permission_callback' => function () { |
|
78 | + return current_user_can( 'manage_options' ); |
|
79 | + }, |
|
80 | + ) |
|
81 | + ); |
|
82 | + |
|
83 | + // Create a new match for a post |
|
84 | + register_rest_route( |
|
85 | + 'wordlift/v1', |
|
86 | + '/post-matches/(?P<post_id>\d+)/matches', |
|
87 | + array( |
|
88 | + 'methods' => 'POST', |
|
89 | + 'callback' => array( $this, 'create_post_match' ), |
|
90 | + 'args' => array( |
|
91 | + 'post_id' => array( |
|
92 | + 'required' => true, |
|
93 | + 'validate_callback' => 'rest_validate_request_arg', |
|
94 | + ), |
|
95 | + ), |
|
96 | + 'permission_callback' => function () { |
|
97 | + return current_user_can( 'manage_options' ); |
|
98 | + }, |
|
99 | + ) |
|
100 | + ); |
|
101 | + |
|
102 | + // Update an existing post match |
|
103 | + register_rest_route( |
|
104 | + 'wordlift/v1', |
|
105 | + '/post-matches/(?P<post_id>\d+)/matches/(?P<match_id>\d+)', |
|
106 | + array( |
|
107 | + 'methods' => 'PUT', |
|
108 | + 'callback' => array( $this, 'update_post_match' ), |
|
109 | + 'args' => array( |
|
110 | + 'post_id' => array( |
|
111 | + 'required' => true, |
|
112 | + 'validate_callback' => 'rest_validate_request_arg', |
|
113 | + ), |
|
114 | + 'match_id' => array( |
|
115 | + 'required' => true, |
|
116 | + 'validate_callback' => 'rest_validate_request_arg', |
|
117 | + ), |
|
118 | + ), |
|
119 | + 'permission_callback' => function () { |
|
120 | + |
|
121 | + return current_user_can( 'manage_options' ); |
|
122 | + }, |
|
123 | + ) |
|
124 | + ); |
|
125 | + } |
|
126 | + |
|
127 | + /** |
|
128 | + * Get the term matches by taxonomy name. |
|
129 | + * |
|
130 | + * @var $request \WP_REST_Request |
|
131 | + */ |
|
132 | + public function get_post_matches( $request ) { |
|
133 | + |
|
134 | + $cursor = $request->get_param( 'cursor' ); |
|
135 | + if ( $request->has_param( 'limit' ) ) { |
|
136 | + $cursor['limit'] = $request->get_param( 'limit' ); |
|
137 | + } |
|
138 | + if ( $request->has_param( 'sort' ) ) { |
|
139 | + $cursor['sort'] = $request->get_param( 'sort' ); |
|
140 | + } |
|
141 | + if ( $request->has_param( 'post_types' ) ) { |
|
142 | + $cursor['query']['post_types'] = $request->get_param( 'post_types' ); |
|
143 | + } |
|
144 | + if ( $request->has_param( 'has_match' ) ) { |
|
145 | + $cursor['query']['has_match'] = $request->get_param( 'has_match' ); |
|
146 | + } |
|
147 | + if ( $request->has_param( 'post_status' ) ) { |
|
148 | + $cursor['query']['post_status'] = $request->get_param( 'post_status' ); |
|
149 | + } |
|
150 | + |
|
151 | + // Query. |
|
152 | + $post_types = isset( $cursor['query']['post_types'] ) ? $cursor['query']['post_types'] : apply_filters( |
|
153 | + 'wl_dashboard__post_entity_match__post_types', |
|
154 | + array( |
|
155 | + 'post', |
|
156 | + 'page', |
|
157 | + ) |
|
158 | + ); |
|
159 | + $has_match = isset( $cursor['query']['has_match'] ) ? $cursor['query']['has_match'] : null; |
|
160 | + |
|
161 | + $post_status = isset( $cursor['query']['post_status'] ) ? $cursor['query']['post_status'] : null; |
|
162 | + |
|
163 | + $items = $this->match_service->list_items( |
|
164 | + array( |
|
165 | + // Query |
|
166 | + 'post_types' => $post_types, |
|
167 | + 'has_match' => $has_match, |
|
168 | + 'post_status' => $post_status, |
|
169 | + // Cursor-Pagination |
|
170 | + 'position' => $cursor['position'], |
|
171 | + 'element' => $cursor['element'], |
|
172 | + 'direction' => $cursor['direction'], |
|
173 | + // `+1` to check if we have other results. |
|
174 | + 'limit' => $cursor['limit'] + 1, |
|
175 | + 'sort' => $cursor['sort'], |
|
176 | + ) |
|
177 | + ); |
|
178 | + |
|
179 | + return new Cursor_Page( |
|
180 | + $items, |
|
181 | + $cursor['position'], |
|
182 | + $cursor['element'], |
|
183 | + $cursor['direction'], |
|
184 | + $cursor['sort'], |
|
185 | + $cursor['limit'], |
|
186 | + $cursor['query'] |
|
187 | + ); |
|
188 | + } |
|
189 | + |
|
190 | + /** |
|
191 | + * Create a new match for a post. |
|
192 | + * |
|
193 | + * @var $request \WP_REST_Request |
|
194 | + */ |
|
195 | + public function create_post_match( $request ) { |
|
196 | + $post_id = $request->get_param( 'post_id' ); |
|
197 | + |
|
198 | + // If we dont have a entry on the match table, then add one. |
|
199 | + $content_id = Wordpress_Content_Id::create_post( $post_id ); |
|
200 | + if ( ! Wordpress_Content_Service::get_instance() |
|
201 | + ->get_entity_id( $content_id ) ) { |
|
202 | + $uri = Entity_Uri_Generator::create_uri( $content_id->get_type(), $content_id->get_id() ); |
|
203 | + Wordpress_Content_Service::get_instance()->set_entity_id( $content_id, $uri ); |
|
204 | + } |
|
205 | + |
|
206 | + $match_id = $this->match_service->get_id( |
|
207 | + $post_id, |
|
208 | + Object_Type_Enum::POST |
|
209 | + ); |
|
210 | + |
|
211 | + return $this->match_service->set_jsonld( |
|
212 | + $post_id, |
|
213 | + Object_Type_Enum::POST, |
|
214 | + $match_id, |
|
215 | + $request->get_json_params() |
|
216 | + ); |
|
217 | + } |
|
218 | + |
|
219 | + /** |
|
220 | + * @var $request \WP_REST_Request |
|
221 | + */ |
|
222 | + public function update_post_match( $request ) { |
|
223 | + |
|
224 | + return $this->match_service->set_jsonld( |
|
225 | + $request->get_param( 'post_id' ), |
|
226 | + Object_Type_Enum::POST, |
|
227 | + $request->get_param( 'match_id' ), |
|
228 | + $request->get_json_params() |
|
229 | + ); |
|
230 | + } |
|
231 | 231 | } |
@@ -16,12 +16,12 @@ discard block |
||
16 | 16 | */ |
17 | 17 | private $match_service; |
18 | 18 | |
19 | - public function __construct( $match_service ) { |
|
19 | + public function __construct($match_service) { |
|
20 | 20 | $this->match_service = $match_service; |
21 | 21 | } |
22 | 22 | |
23 | 23 | public function register_hooks() { |
24 | - add_action( 'rest_api_init', array( $this, 'register_routes' ) ); |
|
24 | + add_action('rest_api_init', array($this, 'register_routes')); |
|
25 | 25 | } |
26 | 26 | |
27 | 27 | /** |
@@ -35,13 +35,13 @@ discard block |
||
35 | 35 | '/post-matches', |
36 | 36 | array( |
37 | 37 | 'methods' => 'GET', |
38 | - 'callback' => array( $this, 'get_post_matches' ), |
|
38 | + 'callback' => array($this, 'get_post_matches'), |
|
39 | 39 | 'args' => array( |
40 | 40 | 'cursor' => array( |
41 | 41 | 'type' => 'string', |
42 | 42 | 'default' => Cursor::EMPTY_CURSOR_AS_BASE64_STRING, |
43 | 43 | 'validate_callback' => 'rest_validate_request_arg', |
44 | - 'sanitize_callback' => array( Cursor::class, 'rest_sanitize_request_arg' ), |
|
44 | + 'sanitize_callback' => array(Cursor::class, 'rest_sanitize_request_arg'), |
|
45 | 45 | ), |
46 | 46 | 'limit' => array( |
47 | 47 | 'type' => 'integer', |
@@ -63,19 +63,19 @@ discard block |
||
63 | 63 | 'post_status' => array( |
64 | 64 | 'type' => 'string', |
65 | 65 | 'required' => false, |
66 | - 'enum' => array( 'publish', 'draft' ), |
|
66 | + 'enum' => array('publish', 'draft'), |
|
67 | 67 | 'validate_callback' => 'rest_validate_request_arg', |
68 | 68 | ), |
69 | 69 | 'sort' => array( |
70 | 70 | 'type' => 'string', |
71 | 71 | 'required' => false, |
72 | - 'enum' => array( '+date_modified_gmt', '-date_modified_gmt' ), |
|
72 | + 'enum' => array('+date_modified_gmt', '-date_modified_gmt'), |
|
73 | 73 | 'validate_callback' => 'rest_validate_request_arg', |
74 | 74 | 'default' => '+date_modified_gmt', |
75 | 75 | ), |
76 | 76 | ), |
77 | - 'permission_callback' => function () { |
|
78 | - return current_user_can( 'manage_options' ); |
|
77 | + 'permission_callback' => function() { |
|
78 | + return current_user_can('manage_options'); |
|
79 | 79 | }, |
80 | 80 | ) |
81 | 81 | ); |
@@ -86,15 +86,15 @@ discard block |
||
86 | 86 | '/post-matches/(?P<post_id>\d+)/matches', |
87 | 87 | array( |
88 | 88 | 'methods' => 'POST', |
89 | - 'callback' => array( $this, 'create_post_match' ), |
|
89 | + 'callback' => array($this, 'create_post_match'), |
|
90 | 90 | 'args' => array( |
91 | 91 | 'post_id' => array( |
92 | 92 | 'required' => true, |
93 | 93 | 'validate_callback' => 'rest_validate_request_arg', |
94 | 94 | ), |
95 | 95 | ), |
96 | - 'permission_callback' => function () { |
|
97 | - return current_user_can( 'manage_options' ); |
|
96 | + 'permission_callback' => function() { |
|
97 | + return current_user_can('manage_options'); |
|
98 | 98 | }, |
99 | 99 | ) |
100 | 100 | ); |
@@ -105,7 +105,7 @@ discard block |
||
105 | 105 | '/post-matches/(?P<post_id>\d+)/matches/(?P<match_id>\d+)', |
106 | 106 | array( |
107 | 107 | 'methods' => 'PUT', |
108 | - 'callback' => array( $this, 'update_post_match' ), |
|
108 | + 'callback' => array($this, 'update_post_match'), |
|
109 | 109 | 'args' => array( |
110 | 110 | 'post_id' => array( |
111 | 111 | 'required' => true, |
@@ -116,9 +116,9 @@ discard block |
||
116 | 116 | 'validate_callback' => 'rest_validate_request_arg', |
117 | 117 | ), |
118 | 118 | ), |
119 | - 'permission_callback' => function () { |
|
119 | + 'permission_callback' => function() { |
|
120 | 120 | |
121 | - return current_user_can( 'manage_options' ); |
|
121 | + return current_user_can('manage_options'); |
|
122 | 122 | }, |
123 | 123 | ) |
124 | 124 | ); |
@@ -129,36 +129,36 @@ discard block |
||
129 | 129 | * |
130 | 130 | * @var $request \WP_REST_Request |
131 | 131 | */ |
132 | - public function get_post_matches( $request ) { |
|
132 | + public function get_post_matches($request) { |
|
133 | 133 | |
134 | - $cursor = $request->get_param( 'cursor' ); |
|
135 | - if ( $request->has_param( 'limit' ) ) { |
|
136 | - $cursor['limit'] = $request->get_param( 'limit' ); |
|
134 | + $cursor = $request->get_param('cursor'); |
|
135 | + if ($request->has_param('limit')) { |
|
136 | + $cursor['limit'] = $request->get_param('limit'); |
|
137 | 137 | } |
138 | - if ( $request->has_param( 'sort' ) ) { |
|
139 | - $cursor['sort'] = $request->get_param( 'sort' ); |
|
138 | + if ($request->has_param('sort')) { |
|
139 | + $cursor['sort'] = $request->get_param('sort'); |
|
140 | 140 | } |
141 | - if ( $request->has_param( 'post_types' ) ) { |
|
142 | - $cursor['query']['post_types'] = $request->get_param( 'post_types' ); |
|
141 | + if ($request->has_param('post_types')) { |
|
142 | + $cursor['query']['post_types'] = $request->get_param('post_types'); |
|
143 | 143 | } |
144 | - if ( $request->has_param( 'has_match' ) ) { |
|
145 | - $cursor['query']['has_match'] = $request->get_param( 'has_match' ); |
|
144 | + if ($request->has_param('has_match')) { |
|
145 | + $cursor['query']['has_match'] = $request->get_param('has_match'); |
|
146 | 146 | } |
147 | - if ( $request->has_param( 'post_status' ) ) { |
|
148 | - $cursor['query']['post_status'] = $request->get_param( 'post_status' ); |
|
147 | + if ($request->has_param('post_status')) { |
|
148 | + $cursor['query']['post_status'] = $request->get_param('post_status'); |
|
149 | 149 | } |
150 | 150 | |
151 | 151 | // Query. |
152 | - $post_types = isset( $cursor['query']['post_types'] ) ? $cursor['query']['post_types'] : apply_filters( |
|
152 | + $post_types = isset($cursor['query']['post_types']) ? $cursor['query']['post_types'] : apply_filters( |
|
153 | 153 | 'wl_dashboard__post_entity_match__post_types', |
154 | 154 | array( |
155 | 155 | 'post', |
156 | 156 | 'page', |
157 | 157 | ) |
158 | 158 | ); |
159 | - $has_match = isset( $cursor['query']['has_match'] ) ? $cursor['query']['has_match'] : null; |
|
159 | + $has_match = isset($cursor['query']['has_match']) ? $cursor['query']['has_match'] : null; |
|
160 | 160 | |
161 | - $post_status = isset( $cursor['query']['post_status'] ) ? $cursor['query']['post_status'] : null; |
|
161 | + $post_status = isset($cursor['query']['post_status']) ? $cursor['query']['post_status'] : null; |
|
162 | 162 | |
163 | 163 | $items = $this->match_service->list_items( |
164 | 164 | array( |
@@ -192,15 +192,15 @@ discard block |
||
192 | 192 | * |
193 | 193 | * @var $request \WP_REST_Request |
194 | 194 | */ |
195 | - public function create_post_match( $request ) { |
|
196 | - $post_id = $request->get_param( 'post_id' ); |
|
195 | + public function create_post_match($request) { |
|
196 | + $post_id = $request->get_param('post_id'); |
|
197 | 197 | |
198 | 198 | // If we dont have a entry on the match table, then add one. |
199 | - $content_id = Wordpress_Content_Id::create_post( $post_id ); |
|
199 | + $content_id = Wordpress_Content_Id::create_post($post_id); |
|
200 | 200 | if ( ! Wordpress_Content_Service::get_instance() |
201 | - ->get_entity_id( $content_id ) ) { |
|
202 | - $uri = Entity_Uri_Generator::create_uri( $content_id->get_type(), $content_id->get_id() ); |
|
203 | - Wordpress_Content_Service::get_instance()->set_entity_id( $content_id, $uri ); |
|
201 | + ->get_entity_id($content_id)) { |
|
202 | + $uri = Entity_Uri_Generator::create_uri($content_id->get_type(), $content_id->get_id()); |
|
203 | + Wordpress_Content_Service::get_instance()->set_entity_id($content_id, $uri); |
|
204 | 204 | } |
205 | 205 | |
206 | 206 | $match_id = $this->match_service->get_id( |
@@ -219,12 +219,12 @@ discard block |
||
219 | 219 | /** |
220 | 220 | * @var $request \WP_REST_Request |
221 | 221 | */ |
222 | - public function update_post_match( $request ) { |
|
222 | + public function update_post_match($request) { |
|
223 | 223 | |
224 | 224 | return $this->match_service->set_jsonld( |
225 | - $request->get_param( 'post_id' ), |
|
225 | + $request->get_param('post_id'), |
|
226 | 226 | Object_Type_Enum::POST, |
227 | - $request->get_param( 'match_id' ), |
|
227 | + $request->get_param('match_id'), |
|
228 | 228 | $request->get_json_params() |
229 | 229 | ); |
230 | 230 | } |
@@ -7,16 +7,16 @@ discard block |
||
7 | 7 | |
8 | 8 | class Query_Builder extends Match_Query_Builder { |
9 | 9 | |
10 | - public function build() { |
|
11 | - |
|
12 | - global $wpdb; |
|
13 | - /** |
|
14 | - * Why not use JSON_EXTRACT() to extract the match_name ? |
|
15 | - * As of now the min wp compatibility is 5.3 which requires min mysql version |
|
16 | - * 5.6, The JSON_* functions are introduced on 5.7 which will break the |
|
17 | - * compatibility. |
|
18 | - */ |
|
19 | - $this->sql = " |
|
10 | + public function build() { |
|
11 | + |
|
12 | + global $wpdb; |
|
13 | + /** |
|
14 | + * Why not use JSON_EXTRACT() to extract the match_name ? |
|
15 | + * As of now the min wp compatibility is 5.3 which requires min mysql version |
|
16 | + * 5.6, The JSON_* functions are introduced on 5.7 which will break the |
|
17 | + * compatibility. |
|
18 | + */ |
|
19 | + $this->sql = " |
|
20 | 20 | SELECT t.term_id as id, e.about_jsonld as match_jsonld, |
21 | 21 | t.name, e.id AS match_id FROM {$wpdb->prefix}terms t |
22 | 22 | INNER JOIN {$wpdb->prefix}term_taxonomy tt ON t.term_id = tt.term_id |
@@ -25,36 +25,36 @@ discard block |
||
25 | 25 | "; |
26 | 26 | |
27 | 27 | |
28 | - $this->cursor() |
|
29 | - ->ingredient_name_contains() |
|
30 | - ->taxonomy() |
|
31 | - ->has_match() |
|
32 | - ->order_by() |
|
33 | - ->limit(); |
|
28 | + $this->cursor() |
|
29 | + ->ingredient_name_contains() |
|
30 | + ->taxonomy() |
|
31 | + ->has_match() |
|
32 | + ->order_by() |
|
33 | + ->limit(); |
|
34 | 34 | |
35 | - } |
|
35 | + } |
|
36 | 36 | |
37 | - public function taxonomy() { |
|
38 | - $taxonomies = $this->params['taxonomies']; |
|
37 | + public function taxonomy() { |
|
38 | + $taxonomies = $this->params['taxonomies']; |
|
39 | 39 | |
40 | - if ( ! isset( $taxonomies ) ) { |
|
41 | - return $this; |
|
42 | - } |
|
43 | - $sql = Escape::sql_array( $taxonomies ); |
|
44 | - $this->sql .= " AND tt.taxonomy IN ($sql)"; |
|
45 | - return $this; |
|
46 | - } |
|
40 | + if ( ! isset( $taxonomies ) ) { |
|
41 | + return $this; |
|
42 | + } |
|
43 | + $sql = Escape::sql_array( $taxonomies ); |
|
44 | + $this->sql .= " AND tt.taxonomy IN ($sql)"; |
|
45 | + return $this; |
|
46 | + } |
|
47 | 47 | |
48 | - public function ingredient_name_contains() { |
|
49 | - global $wpdb; |
|
48 | + public function ingredient_name_contains() { |
|
49 | + global $wpdb; |
|
50 | 50 | |
51 | - // If the ingredient_name_contains value is a non-empty string, add the filter |
|
52 | - if ( is_string( $this->params['ingredient_name_contains'] ) && ! empty( $this->params['ingredient_name_contains'] ) ) { |
|
53 | - $ingredient_name_contains = $this->params['ingredient_name_contains']; |
|
54 | - $this->sql .= $wpdb->prepare( ' AND t.name LIKE %s', '%' . $ingredient_name_contains . '%' ); |
|
55 | - } |
|
51 | + // If the ingredient_name_contains value is a non-empty string, add the filter |
|
52 | + if ( is_string( $this->params['ingredient_name_contains'] ) && ! empty( $this->params['ingredient_name_contains'] ) ) { |
|
53 | + $ingredient_name_contains = $this->params['ingredient_name_contains']; |
|
54 | + $this->sql .= $wpdb->prepare( ' AND t.name LIKE %s', '%' . $ingredient_name_contains . '%' ); |
|
55 | + } |
|
56 | 56 | |
57 | - return $this; |
|
58 | - } |
|
57 | + return $this; |
|
58 | + } |
|
59 | 59 | |
60 | 60 | } |
@@ -37,10 +37,10 @@ discard block |
||
37 | 37 | public function taxonomy() { |
38 | 38 | $taxonomies = $this->params['taxonomies']; |
39 | 39 | |
40 | - if ( ! isset( $taxonomies ) ) { |
|
40 | + if ( ! isset($taxonomies)) { |
|
41 | 41 | return $this; |
42 | 42 | } |
43 | - $sql = Escape::sql_array( $taxonomies ); |
|
43 | + $sql = Escape::sql_array($taxonomies); |
|
44 | 44 | $this->sql .= " AND tt.taxonomy IN ($sql)"; |
45 | 45 | return $this; |
46 | 46 | } |
@@ -49,9 +49,9 @@ discard block |
||
49 | 49 | global $wpdb; |
50 | 50 | |
51 | 51 | // If the ingredient_name_contains value is a non-empty string, add the filter |
52 | - if ( is_string( $this->params['ingredient_name_contains'] ) && ! empty( $this->params['ingredient_name_contains'] ) ) { |
|
52 | + if (is_string($this->params['ingredient_name_contains']) && ! empty($this->params['ingredient_name_contains'])) { |
|
53 | 53 | $ingredient_name_contains = $this->params['ingredient_name_contains']; |
54 | - $this->sql .= $wpdb->prepare( ' AND t.name LIKE %s', '%' . $ingredient_name_contains . '%' ); |
|
54 | + $this->sql .= $wpdb->prepare(' AND t.name LIKE %s', '%'.$ingredient_name_contains.'%'); |
|
55 | 55 | } |
56 | 56 | |
57 | 57 | return $this; |
@@ -11,219 +11,219 @@ |
||
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 | - 'ingredient_name_contains' => array( |
|
61 | - 'type' => 'string', |
|
62 | - 'required' => false, |
|
63 | - 'validate_callback' => 'rest_validate_request_arg' |
|
64 | - ), |
|
65 | - 'sort' => array( |
|
66 | - 'type' => 'string', |
|
67 | - 'required' => 'false', |
|
68 | - 'enum' => array( '+ingredient_term', '-ingredient_term', '+matched_ingredient', '-matched_ingredient', '+occurences', '-occurences'), |
|
69 | - 'validate_callback' => 'rest_validate_request_arg', |
|
70 | - ) |
|
71 | - ), |
|
72 | - 'permission_callback' => function () { |
|
73 | - return current_user_can( 'manage_options' ); |
|
74 | - }, |
|
75 | - ) |
|
76 | - ); |
|
77 | - |
|
78 | - // Create a new match for a term |
|
79 | - register_rest_route( |
|
80 | - '/wordlift/v1', |
|
81 | - '/term-matches/(?P<term_id>\d+)/matches', |
|
82 | - array( |
|
83 | - 'methods' => 'POST', |
|
84 | - 'callback' => array( $this, 'create_term_match' ), |
|
85 | - 'args' => array( |
|
86 | - 'term_id' => array( |
|
87 | - 'required' => true, |
|
88 | - 'validate_callback' => 'rest_validate_request_arg', |
|
89 | - ), |
|
90 | - ), |
|
91 | - 'permission_callback' => function () { |
|
92 | - return current_user_can( 'manage_options' ); |
|
93 | - }, |
|
94 | - ) |
|
95 | - ); |
|
96 | - |
|
97 | - // Update an existing term match |
|
98 | - register_rest_route( |
|
99 | - '/wordlift/v1', |
|
100 | - '/term-matches/(?P<term_id>\d+)/matches/(?P<match_id>\d+)', |
|
101 | - array( |
|
102 | - 'methods' => 'PUT', |
|
103 | - 'callback' => array( $this, 'update_term_match' ), |
|
104 | - 'args' => array( |
|
105 | - 'term_id' => array( |
|
106 | - 'required' => true, |
|
107 | - 'validate_callback' => 'rest_validate_request_arg', |
|
108 | - ), |
|
109 | - 'match_id' => array( |
|
110 | - 'required' => true, |
|
111 | - 'validate_callback' => 'rest_validate_request_arg', |
|
112 | - ), |
|
113 | - ), |
|
114 | - 'permission_callback' => function () { |
|
115 | - |
|
116 | - return current_user_can( 'manage_options' ); |
|
117 | - }, |
|
118 | - ) |
|
119 | - ); |
|
120 | - } |
|
121 | - |
|
122 | - /** |
|
123 | - * Get the term matches by taxonomy name. |
|
124 | - * |
|
125 | - * @var $request \WP_REST_Request |
|
126 | - */ |
|
127 | - public function get_term_matches( $request ) { |
|
128 | - |
|
129 | - $cursor = $request->get_param( 'cursor' ); |
|
130 | - if ( $request->has_param( 'limit' ) ) { |
|
131 | - $cursor['limit'] = $request->get_param( 'limit' ); |
|
132 | - } |
|
133 | - if ( $request->has_param( 'sort' ) ) { |
|
134 | - $cursor['sort'] = $request->get_param( 'sort' ); |
|
135 | - } |
|
136 | - if ( $request->has_param( 'taxonomies' ) ) { |
|
137 | - $cursor['query']['taxonomies'] = $request->get_param( 'taxonomies' ); |
|
138 | - } |
|
139 | - if ( $request->has_param( 'has_match' ) ) { |
|
140 | - $cursor['query']['has_match'] = $request->get_param( 'has_match' ); |
|
141 | - } |
|
142 | - if ( $request->has_param( 'ingredient_name_contains' )) { |
|
143 | - $cursor['query']['ingredient_name_contains'] = $request->get_param('ingredient_name_contains'); |
|
144 | - } |
|
145 | - |
|
146 | - // Query. |
|
147 | - $taxonomies = isset( $cursor['query']['taxonomies'] ) ? $cursor['query']['taxonomies'] : apply_filters( |
|
148 | - 'wl_dashboard__post_entity_match__taxonomies', |
|
149 | - array( |
|
150 | - 'post_tag', |
|
151 | - 'category', |
|
152 | - ) |
|
153 | - ); |
|
154 | - |
|
155 | - $has_match = isset( $cursor['query']['has_match'] ) ? $cursor['query']['has_match'] : null; |
|
156 | - $ingredient_name_contains = isset( $cursor['query']['ingredient_name_contains']) ? $cursor['query']['ingredient_name_contains'] : null; |
|
157 | - |
|
158 | - $items = $this->match_service->list_items( |
|
159 | - array( |
|
160 | - // Query |
|
161 | - 'taxonomies' => $taxonomies, |
|
162 | - 'has_match' => $has_match, |
|
163 | - 'ingredient_name_contains' => $ingredient_name_contains, |
|
164 | - // Cursor-Pagination |
|
165 | - 'position' => $cursor['position'], |
|
166 | - 'element' => $cursor['element'], |
|
167 | - 'direction' => $cursor['direction'], |
|
168 | - // `+1` to check if we have other results. |
|
169 | - 'limit' => $cursor['limit'] + 1, |
|
170 | - 'sort' => $cursor['sort'], |
|
171 | - ) |
|
172 | - ); |
|
173 | - |
|
174 | - return new Cursor_Page( |
|
175 | - $items, |
|
176 | - $cursor['position'], |
|
177 | - $cursor['element'], |
|
178 | - $cursor['direction'], |
|
179 | - $cursor['sort'], |
|
180 | - $cursor['limit'], |
|
181 | - $cursor['query'] |
|
182 | - ); |
|
183 | - } |
|
184 | - |
|
185 | - /** |
|
186 | - * Create a new match for a term. |
|
187 | - * |
|
188 | - * @var $request \WP_REST_Request |
|
189 | - */ |
|
190 | - public function create_term_match( $request ) { |
|
191 | - |
|
192 | - $term_id = $request->get_param( 'term_id' ); |
|
193 | - |
|
194 | - // If we dont have a entry on the match table, then add one. |
|
195 | - $content_id = Wordpress_Content_Id::create_term( $term_id ); |
|
196 | - if ( ! Wordpress_Content_Service::get_instance() |
|
197 | - ->get_entity_id( $content_id ) ) { |
|
198 | - $uri = Entity_Uri_Generator::create_uri( $content_id->get_type(), $content_id->get_id() ); |
|
199 | - Wordpress_Content_Service::get_instance()->set_entity_id( $content_id, $uri ); |
|
200 | - } |
|
201 | - |
|
202 | - $match_id = $this->match_service->get_id( |
|
203 | - $term_id, |
|
204 | - Object_Type_Enum::TERM |
|
205 | - ); |
|
206 | - |
|
207 | - return $this->match_service->set_jsonld( |
|
208 | - $term_id, |
|
209 | - Object_Type_Enum::TERM, |
|
210 | - $match_id, |
|
211 | - $request->get_json_params() |
|
212 | - ); |
|
213 | - |
|
214 | - } |
|
215 | - |
|
216 | - /** |
|
217 | - * @var $request \WP_REST_Request |
|
218 | - */ |
|
219 | - public function update_term_match( $request ) { |
|
220 | - |
|
221 | - return $this->match_service->set_jsonld( |
|
222 | - $request->get_param( 'term_id' ), |
|
223 | - Object_Type_Enum::TERM, |
|
224 | - $request->get_param( 'match_id' ), |
|
225 | - $request->get_json_params() |
|
226 | - ); |
|
227 | - } |
|
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 | + 'ingredient_name_contains' => array( |
|
61 | + 'type' => 'string', |
|
62 | + 'required' => false, |
|
63 | + 'validate_callback' => 'rest_validate_request_arg' |
|
64 | + ), |
|
65 | + 'sort' => array( |
|
66 | + 'type' => 'string', |
|
67 | + 'required' => 'false', |
|
68 | + 'enum' => array( '+ingredient_term', '-ingredient_term', '+matched_ingredient', '-matched_ingredient', '+occurences', '-occurences'), |
|
69 | + 'validate_callback' => 'rest_validate_request_arg', |
|
70 | + ) |
|
71 | + ), |
|
72 | + 'permission_callback' => function () { |
|
73 | + return current_user_can( 'manage_options' ); |
|
74 | + }, |
|
75 | + ) |
|
76 | + ); |
|
77 | + |
|
78 | + // Create a new match for a term |
|
79 | + register_rest_route( |
|
80 | + '/wordlift/v1', |
|
81 | + '/term-matches/(?P<term_id>\d+)/matches', |
|
82 | + array( |
|
83 | + 'methods' => 'POST', |
|
84 | + 'callback' => array( $this, 'create_term_match' ), |
|
85 | + 'args' => array( |
|
86 | + 'term_id' => array( |
|
87 | + 'required' => true, |
|
88 | + 'validate_callback' => 'rest_validate_request_arg', |
|
89 | + ), |
|
90 | + ), |
|
91 | + 'permission_callback' => function () { |
|
92 | + return current_user_can( 'manage_options' ); |
|
93 | + }, |
|
94 | + ) |
|
95 | + ); |
|
96 | + |
|
97 | + // Update an existing term match |
|
98 | + register_rest_route( |
|
99 | + '/wordlift/v1', |
|
100 | + '/term-matches/(?P<term_id>\d+)/matches/(?P<match_id>\d+)', |
|
101 | + array( |
|
102 | + 'methods' => 'PUT', |
|
103 | + 'callback' => array( $this, 'update_term_match' ), |
|
104 | + 'args' => array( |
|
105 | + 'term_id' => array( |
|
106 | + 'required' => true, |
|
107 | + 'validate_callback' => 'rest_validate_request_arg', |
|
108 | + ), |
|
109 | + 'match_id' => array( |
|
110 | + 'required' => true, |
|
111 | + 'validate_callback' => 'rest_validate_request_arg', |
|
112 | + ), |
|
113 | + ), |
|
114 | + 'permission_callback' => function () { |
|
115 | + |
|
116 | + return current_user_can( 'manage_options' ); |
|
117 | + }, |
|
118 | + ) |
|
119 | + ); |
|
120 | + } |
|
121 | + |
|
122 | + /** |
|
123 | + * Get the term matches by taxonomy name. |
|
124 | + * |
|
125 | + * @var $request \WP_REST_Request |
|
126 | + */ |
|
127 | + public function get_term_matches( $request ) { |
|
128 | + |
|
129 | + $cursor = $request->get_param( 'cursor' ); |
|
130 | + if ( $request->has_param( 'limit' ) ) { |
|
131 | + $cursor['limit'] = $request->get_param( 'limit' ); |
|
132 | + } |
|
133 | + if ( $request->has_param( 'sort' ) ) { |
|
134 | + $cursor['sort'] = $request->get_param( 'sort' ); |
|
135 | + } |
|
136 | + if ( $request->has_param( 'taxonomies' ) ) { |
|
137 | + $cursor['query']['taxonomies'] = $request->get_param( 'taxonomies' ); |
|
138 | + } |
|
139 | + if ( $request->has_param( 'has_match' ) ) { |
|
140 | + $cursor['query']['has_match'] = $request->get_param( 'has_match' ); |
|
141 | + } |
|
142 | + if ( $request->has_param( 'ingredient_name_contains' )) { |
|
143 | + $cursor['query']['ingredient_name_contains'] = $request->get_param('ingredient_name_contains'); |
|
144 | + } |
|
145 | + |
|
146 | + // Query. |
|
147 | + $taxonomies = isset( $cursor['query']['taxonomies'] ) ? $cursor['query']['taxonomies'] : apply_filters( |
|
148 | + 'wl_dashboard__post_entity_match__taxonomies', |
|
149 | + array( |
|
150 | + 'post_tag', |
|
151 | + 'category', |
|
152 | + ) |
|
153 | + ); |
|
154 | + |
|
155 | + $has_match = isset( $cursor['query']['has_match'] ) ? $cursor['query']['has_match'] : null; |
|
156 | + $ingredient_name_contains = isset( $cursor['query']['ingredient_name_contains']) ? $cursor['query']['ingredient_name_contains'] : null; |
|
157 | + |
|
158 | + $items = $this->match_service->list_items( |
|
159 | + array( |
|
160 | + // Query |
|
161 | + 'taxonomies' => $taxonomies, |
|
162 | + 'has_match' => $has_match, |
|
163 | + 'ingredient_name_contains' => $ingredient_name_contains, |
|
164 | + // Cursor-Pagination |
|
165 | + 'position' => $cursor['position'], |
|
166 | + 'element' => $cursor['element'], |
|
167 | + 'direction' => $cursor['direction'], |
|
168 | + // `+1` to check if we have other results. |
|
169 | + 'limit' => $cursor['limit'] + 1, |
|
170 | + 'sort' => $cursor['sort'], |
|
171 | + ) |
|
172 | + ); |
|
173 | + |
|
174 | + return new Cursor_Page( |
|
175 | + $items, |
|
176 | + $cursor['position'], |
|
177 | + $cursor['element'], |
|
178 | + $cursor['direction'], |
|
179 | + $cursor['sort'], |
|
180 | + $cursor['limit'], |
|
181 | + $cursor['query'] |
|
182 | + ); |
|
183 | + } |
|
184 | + |
|
185 | + /** |
|
186 | + * Create a new match for a term. |
|
187 | + * |
|
188 | + * @var $request \WP_REST_Request |
|
189 | + */ |
|
190 | + public function create_term_match( $request ) { |
|
191 | + |
|
192 | + $term_id = $request->get_param( 'term_id' ); |
|
193 | + |
|
194 | + // If we dont have a entry on the match table, then add one. |
|
195 | + $content_id = Wordpress_Content_Id::create_term( $term_id ); |
|
196 | + if ( ! Wordpress_Content_Service::get_instance() |
|
197 | + ->get_entity_id( $content_id ) ) { |
|
198 | + $uri = Entity_Uri_Generator::create_uri( $content_id->get_type(), $content_id->get_id() ); |
|
199 | + Wordpress_Content_Service::get_instance()->set_entity_id( $content_id, $uri ); |
|
200 | + } |
|
201 | + |
|
202 | + $match_id = $this->match_service->get_id( |
|
203 | + $term_id, |
|
204 | + Object_Type_Enum::TERM |
|
205 | + ); |
|
206 | + |
|
207 | + return $this->match_service->set_jsonld( |
|
208 | + $term_id, |
|
209 | + Object_Type_Enum::TERM, |
|
210 | + $match_id, |
|
211 | + $request->get_json_params() |
|
212 | + ); |
|
213 | + |
|
214 | + } |
|
215 | + |
|
216 | + /** |
|
217 | + * @var $request \WP_REST_Request |
|
218 | + */ |
|
219 | + public function update_term_match( $request ) { |
|
220 | + |
|
221 | + return $this->match_service->set_jsonld( |
|
222 | + $request->get_param( 'term_id' ), |
|
223 | + Object_Type_Enum::TERM, |
|
224 | + $request->get_param( 'match_id' ), |
|
225 | + $request->get_json_params() |
|
226 | + ); |
|
227 | + } |
|
228 | 228 | |
229 | 229 | } |
@@ -13,12 +13,12 @@ discard block |
||
13 | 13 | |
14 | 14 | private $match_service; |
15 | 15 | |
16 | - public function __construct( $match_service ) { |
|
16 | + public function __construct($match_service) { |
|
17 | 17 | $this->match_service = $match_service; |
18 | 18 | } |
19 | 19 | |
20 | 20 | public function register_hooks() { |
21 | - add_action( 'rest_api_init', array( $this, 'register_routes' ) ); |
|
21 | + add_action('rest_api_init', array($this, 'register_routes')); |
|
22 | 22 | } |
23 | 23 | |
24 | 24 | /** |
@@ -32,13 +32,13 @@ discard block |
||
32 | 32 | '/term-matches', |
33 | 33 | array( |
34 | 34 | 'methods' => 'GET', |
35 | - 'callback' => array( $this, 'get_term_matches' ), |
|
35 | + 'callback' => array($this, 'get_term_matches'), |
|
36 | 36 | 'args' => array( |
37 | 37 | 'cursor' => array( |
38 | 38 | 'type' => 'string', |
39 | 39 | 'default' => Cursor::EMPTY_CURSOR_AS_BASE64_STRING, |
40 | 40 | 'validate_callback' => 'rest_validate_request_arg', |
41 | - 'sanitize_callback' => array( Cursor::class, 'rest_sanitize_request_arg' ), |
|
41 | + 'sanitize_callback' => array(Cursor::class, 'rest_sanitize_request_arg'), |
|
42 | 42 | ), |
43 | 43 | 'limit' => array( |
44 | 44 | 'type' => 'integer', |
@@ -65,12 +65,12 @@ discard block |
||
65 | 65 | 'sort' => array( |
66 | 66 | 'type' => 'string', |
67 | 67 | 'required' => 'false', |
68 | - 'enum' => array( '+ingredient_term', '-ingredient_term', '+matched_ingredient', '-matched_ingredient', '+occurences', '-occurences'), |
|
68 | + 'enum' => array('+ingredient_term', '-ingredient_term', '+matched_ingredient', '-matched_ingredient', '+occurences', '-occurences'), |
|
69 | 69 | 'validate_callback' => 'rest_validate_request_arg', |
70 | 70 | ) |
71 | 71 | ), |
72 | - 'permission_callback' => function () { |
|
73 | - return current_user_can( 'manage_options' ); |
|
72 | + 'permission_callback' => function() { |
|
73 | + return current_user_can('manage_options'); |
|
74 | 74 | }, |
75 | 75 | ) |
76 | 76 | ); |
@@ -81,15 +81,15 @@ discard block |
||
81 | 81 | '/term-matches/(?P<term_id>\d+)/matches', |
82 | 82 | array( |
83 | 83 | 'methods' => 'POST', |
84 | - 'callback' => array( $this, 'create_term_match' ), |
|
84 | + 'callback' => array($this, 'create_term_match'), |
|
85 | 85 | 'args' => array( |
86 | 86 | 'term_id' => array( |
87 | 87 | 'required' => true, |
88 | 88 | 'validate_callback' => 'rest_validate_request_arg', |
89 | 89 | ), |
90 | 90 | ), |
91 | - 'permission_callback' => function () { |
|
92 | - return current_user_can( 'manage_options' ); |
|
91 | + 'permission_callback' => function() { |
|
92 | + return current_user_can('manage_options'); |
|
93 | 93 | }, |
94 | 94 | ) |
95 | 95 | ); |
@@ -100,7 +100,7 @@ discard block |
||
100 | 100 | '/term-matches/(?P<term_id>\d+)/matches/(?P<match_id>\d+)', |
101 | 101 | array( |
102 | 102 | 'methods' => 'PUT', |
103 | - 'callback' => array( $this, 'update_term_match' ), |
|
103 | + 'callback' => array($this, 'update_term_match'), |
|
104 | 104 | 'args' => array( |
105 | 105 | 'term_id' => array( |
106 | 106 | 'required' => true, |
@@ -111,9 +111,9 @@ discard block |
||
111 | 111 | 'validate_callback' => 'rest_validate_request_arg', |
112 | 112 | ), |
113 | 113 | ), |
114 | - 'permission_callback' => function () { |
|
114 | + 'permission_callback' => function() { |
|
115 | 115 | |
116 | - return current_user_can( 'manage_options' ); |
|
116 | + return current_user_can('manage_options'); |
|
117 | 117 | }, |
118 | 118 | ) |
119 | 119 | ); |
@@ -124,27 +124,27 @@ discard block |
||
124 | 124 | * |
125 | 125 | * @var $request \WP_REST_Request |
126 | 126 | */ |
127 | - public function get_term_matches( $request ) { |
|
127 | + public function get_term_matches($request) { |
|
128 | 128 | |
129 | - $cursor = $request->get_param( 'cursor' ); |
|
130 | - if ( $request->has_param( 'limit' ) ) { |
|
131 | - $cursor['limit'] = $request->get_param( 'limit' ); |
|
129 | + $cursor = $request->get_param('cursor'); |
|
130 | + if ($request->has_param('limit')) { |
|
131 | + $cursor['limit'] = $request->get_param('limit'); |
|
132 | 132 | } |
133 | - if ( $request->has_param( 'sort' ) ) { |
|
134 | - $cursor['sort'] = $request->get_param( 'sort' ); |
|
133 | + if ($request->has_param('sort')) { |
|
134 | + $cursor['sort'] = $request->get_param('sort'); |
|
135 | 135 | } |
136 | - if ( $request->has_param( 'taxonomies' ) ) { |
|
137 | - $cursor['query']['taxonomies'] = $request->get_param( 'taxonomies' ); |
|
136 | + if ($request->has_param('taxonomies')) { |
|
137 | + $cursor['query']['taxonomies'] = $request->get_param('taxonomies'); |
|
138 | 138 | } |
139 | - if ( $request->has_param( 'has_match' ) ) { |
|
140 | - $cursor['query']['has_match'] = $request->get_param( 'has_match' ); |
|
139 | + if ($request->has_param('has_match')) { |
|
140 | + $cursor['query']['has_match'] = $request->get_param('has_match'); |
|
141 | 141 | } |
142 | - if ( $request->has_param( 'ingredient_name_contains' )) { |
|
142 | + if ($request->has_param('ingredient_name_contains')) { |
|
143 | 143 | $cursor['query']['ingredient_name_contains'] = $request->get_param('ingredient_name_contains'); |
144 | 144 | } |
145 | 145 | |
146 | 146 | // Query. |
147 | - $taxonomies = isset( $cursor['query']['taxonomies'] ) ? $cursor['query']['taxonomies'] : apply_filters( |
|
147 | + $taxonomies = isset($cursor['query']['taxonomies']) ? $cursor['query']['taxonomies'] : apply_filters( |
|
148 | 148 | 'wl_dashboard__post_entity_match__taxonomies', |
149 | 149 | array( |
150 | 150 | 'post_tag', |
@@ -152,8 +152,8 @@ discard block |
||
152 | 152 | ) |
153 | 153 | ); |
154 | 154 | |
155 | - $has_match = isset( $cursor['query']['has_match'] ) ? $cursor['query']['has_match'] : null; |
|
156 | - $ingredient_name_contains = isset( $cursor['query']['ingredient_name_contains']) ? $cursor['query']['ingredient_name_contains'] : null; |
|
155 | + $has_match = isset($cursor['query']['has_match']) ? $cursor['query']['has_match'] : null; |
|
156 | + $ingredient_name_contains = isset($cursor['query']['ingredient_name_contains']) ? $cursor['query']['ingredient_name_contains'] : null; |
|
157 | 157 | |
158 | 158 | $items = $this->match_service->list_items( |
159 | 159 | array( |
@@ -187,16 +187,16 @@ discard block |
||
187 | 187 | * |
188 | 188 | * @var $request \WP_REST_Request |
189 | 189 | */ |
190 | - public function create_term_match( $request ) { |
|
190 | + public function create_term_match($request) { |
|
191 | 191 | |
192 | - $term_id = $request->get_param( 'term_id' ); |
|
192 | + $term_id = $request->get_param('term_id'); |
|
193 | 193 | |
194 | 194 | // If we dont have a entry on the match table, then add one. |
195 | - $content_id = Wordpress_Content_Id::create_term( $term_id ); |
|
195 | + $content_id = Wordpress_Content_Id::create_term($term_id); |
|
196 | 196 | if ( ! Wordpress_Content_Service::get_instance() |
197 | - ->get_entity_id( $content_id ) ) { |
|
198 | - $uri = Entity_Uri_Generator::create_uri( $content_id->get_type(), $content_id->get_id() ); |
|
199 | - Wordpress_Content_Service::get_instance()->set_entity_id( $content_id, $uri ); |
|
197 | + ->get_entity_id($content_id)) { |
|
198 | + $uri = Entity_Uri_Generator::create_uri($content_id->get_type(), $content_id->get_id()); |
|
199 | + Wordpress_Content_Service::get_instance()->set_entity_id($content_id, $uri); |
|
200 | 200 | } |
201 | 201 | |
202 | 202 | $match_id = $this->match_service->get_id( |
@@ -216,12 +216,12 @@ discard block |
||
216 | 216 | /** |
217 | 217 | * @var $request \WP_REST_Request |
218 | 218 | */ |
219 | - public function update_term_match( $request ) { |
|
219 | + public function update_term_match($request) { |
|
220 | 220 | |
221 | 221 | return $this->match_service->set_jsonld( |
222 | - $request->get_param( 'term_id' ), |
|
222 | + $request->get_param('term_id'), |
|
223 | 223 | Object_Type_Enum::TERM, |
224 | - $request->get_param( 'match_id' ), |
|
224 | + $request->get_param('match_id'), |
|
225 | 225 | $request->get_json_params() |
226 | 226 | ); |
227 | 227 | } |
@@ -7,56 +7,56 @@ |
||
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 | - 'ingredient_name_contains' => null, |
|
25 | - ) |
|
26 | - ); |
|
27 | - |
|
28 | - /** |
|
29 | - * @var $sort Sort |
|
30 | - */ |
|
31 | - $sort = new Sort( $params['sort'] ); |
|
32 | - |
|
33 | - $query_builder = new Query_Builder( |
|
34 | - $params, |
|
35 | - $sort |
|
36 | - ); |
|
37 | - $query = $query_builder |
|
38 | - ->get(); |
|
39 | - |
|
40 | - $items = $wpdb->get_results( |
|
41 | - // Each function above is preparing `$sql` by using `$wpdb->prepare`. |
|
42 | - // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
43 | - $wpdb->prepare( $query, Object_Type_Enum::TERM ) |
|
44 | - ); |
|
45 | - |
|
46 | - $sort->apply( $items ); |
|
47 | - |
|
48 | - return $this->map( $items ); |
|
49 | - } |
|
50 | - |
|
51 | - private function map( array $items ) { |
|
52 | - return array_map( |
|
53 | - function ( $item ) { |
|
54 | - $data = json_decode( $item->match_jsonld, true ); |
|
55 | - $item->match_name = $data && is_array( $data ) && array_key_exists( 'name', $data ) ? $data['name'] : null; |
|
56 | - return $item; |
|
57 | - }, |
|
58 | - $items |
|
59 | - ); |
|
60 | - } |
|
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 | + 'ingredient_name_contains' => null, |
|
25 | + ) |
|
26 | + ); |
|
27 | + |
|
28 | + /** |
|
29 | + * @var $sort Sort |
|
30 | + */ |
|
31 | + $sort = new Sort( $params['sort'] ); |
|
32 | + |
|
33 | + $query_builder = new Query_Builder( |
|
34 | + $params, |
|
35 | + $sort |
|
36 | + ); |
|
37 | + $query = $query_builder |
|
38 | + ->get(); |
|
39 | + |
|
40 | + $items = $wpdb->get_results( |
|
41 | + // Each function above is preparing `$sql` by using `$wpdb->prepare`. |
|
42 | + // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
43 | + $wpdb->prepare( $query, Object_Type_Enum::TERM ) |
|
44 | + ); |
|
45 | + |
|
46 | + $sort->apply( $items ); |
|
47 | + |
|
48 | + return $this->map( $items ); |
|
49 | + } |
|
50 | + |
|
51 | + private function map( array $items ) { |
|
52 | + return array_map( |
|
53 | + function ( $item ) { |
|
54 | + $data = json_decode( $item->match_jsonld, true ); |
|
55 | + $item->match_name = $data && is_array( $data ) && array_key_exists( 'name', $data ) ? $data['name'] : null; |
|
56 | + return $item; |
|
57 | + }, |
|
58 | + $items |
|
59 | + ); |
|
60 | + } |
|
61 | 61 | |
62 | 62 | } |
@@ -6,11 +6,11 @@ discard block |
||
6 | 6 | |
7 | 7 | class Sort extends Match_Sort { |
8 | 8 | |
9 | - public function get_field_name() { |
|
10 | - $tmp_sort_field_name = substr( $this->sort, 1 ); |
|
11 | - if ( 'id' === $tmp_sort_field_name ) { |
|
12 | - return 't.term_id'; |
|
13 | - } elseif('ingredient_term') { |
|
9 | + public function get_field_name() { |
|
10 | + $tmp_sort_field_name = substr( $this->sort, 1 ); |
|
11 | + if ( 'id' === $tmp_sort_field_name ) { |
|
12 | + return 't.term_id'; |
|
13 | + } elseif('ingredient_term') { |
|
14 | 14 | return 't.name'; |
15 | 15 | } elseif('matched_ingredient'){ |
16 | 16 | //@todo -not sure what this is |
@@ -18,8 +18,8 @@ discard block |
||
18 | 18 | //@todo |
19 | 19 | } |
20 | 20 | else { |
21 | - return 't.name'; |
|
22 | - } |
|
23 | - } |
|
21 | + return 't.name'; |
|
22 | + } |
|
23 | + } |
|
24 | 24 | |
25 | 25 | } |
@@ -7,14 +7,14 @@ |
||
7 | 7 | class Sort extends Match_Sort { |
8 | 8 | |
9 | 9 | public function get_field_name() { |
10 | - $tmp_sort_field_name = substr( $this->sort, 1 ); |
|
11 | - if ( 'id' === $tmp_sort_field_name ) { |
|
10 | + $tmp_sort_field_name = substr($this->sort, 1); |
|
11 | + if ('id' === $tmp_sort_field_name) { |
|
12 | 12 | return 't.term_id'; |
13 | - } elseif('ingredient_term') { |
|
13 | + } elseif ('ingredient_term') { |
|
14 | 14 | return 't.name'; |
15 | - } elseif('matched_ingredient'){ |
|
15 | + } elseif ('matched_ingredient') { |
|
16 | 16 | //@todo -not sure what this is |
17 | - } elseif('occurrences'){ |
|
17 | + } elseif ('occurrences') { |
|
18 | 18 | //@todo |
19 | 19 | } |
20 | 20 | else { |
@@ -16,8 +16,7 @@ |
||
16 | 16 | //@todo -not sure what this is |
17 | 17 | } elseif('occurrences'){ |
18 | 18 | //@todo |
19 | - } |
|
20 | - else { |
|
19 | + } else { |
|
21 | 20 | return 't.name'; |
22 | 21 | } |
23 | 22 | } |