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