@@ -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 | } |
@@ -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 | } |
@@ -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 If there was a problem generating the list items. |
|
| 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 If there was a problem generating the list items. |
|
| 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 | } |
@@ -12,91 +12,91 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class Post_Entity_Match_Service extends Match_Service { |
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * List items. |
|
| 17 | - * |
|
| 18 | - * @param $args |
|
| 19 | - * |
|
| 20 | - * @return array |
|
| 21 | - * |
|
| 22 | - * @throws \Exception If there was a problem generating the list items. |
|
| 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 If there was a problem generating the list items. |
|
| 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 | - 'post_type' => null, |
|
| 36 | - 'has_match' => null, |
|
| 37 | - 'post_status' => null, |
|
| 38 | - ) |
|
| 39 | - ); |
|
| 40 | - /** |
|
| 41 | - * @var $sort Sort |
|
| 42 | - */ |
|
| 43 | - $sort = new Sort( $params['sort'] ); |
|
| 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 | + 'post_type' => null, |
|
| 36 | + 'has_match' => null, |
|
| 37 | + 'post_status' => null, |
|
| 38 | + ) |
|
| 39 | + ); |
|
| 40 | + /** |
|
| 41 | + * @var $sort Sort |
|
| 42 | + */ |
|
| 43 | + $sort = new Sort( $params['sort'] ); |
|
| 44 | 44 | |
| 45 | - $query_builder = new Query_Builder( |
|
| 46 | - $params, |
|
| 47 | - $sort |
|
| 48 | - ); |
|
| 45 | + $query_builder = new Query_Builder( |
|
| 46 | + $params, |
|
| 47 | + $sort |
|
| 48 | + ); |
|
| 49 | 49 | |
| 50 | - $items = $wpdb->get_results( |
|
| 51 | - // Each function above is preparing `$sql` by using `$wpdb->prepare`. |
|
| 52 | - // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
| 53 | - $wpdb->prepare( $query_builder->get(), Object_Type_Enum::POST ) |
|
| 54 | - ); |
|
| 50 | + $items = $wpdb->get_results( |
|
| 51 | + // Each function above is preparing `$sql` by using `$wpdb->prepare`. |
|
| 52 | + // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
| 53 | + $wpdb->prepare( $query_builder->get(), Object_Type_Enum::POST ) |
|
| 54 | + ); |
|
| 55 | 55 | |
| 56 | - $sort->apply( $items ); |
|
| 56 | + $sort->apply( $items ); |
|
| 57 | 57 | |
| 58 | - return $this->map( $items ); |
|
| 59 | - } |
|
| 58 | + return $this->map( $items ); |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | - /** |
|
| 62 | - * Returns an array of rows where each row contains: |
|
| 63 | - * |
|
| 64 | - * 'post_title' => The title of the post |
|
| 65 | - * 'id' => The id of the post |
|
| 66 | - * 'post_link' => The edit post link |
|
| 67 | - * 'post_status' => The status of the post. |
|
| 68 | - * 'parent_post_title' => The title of the post linked to this post via wprm_parent_post_id property |
|
| 69 | - * ( this is only applicable when the post is wprm_recipe, returns null if not present ) |
|
| 70 | - * 'parent_post_id' => The id of the linked parent post. |
|
| 71 | - * 'parent_post_link' => The link to parent post. |
|
| 72 | - * 'view link' => The permalink to the post. |
|
| 73 | - * 'preview link' => The preview link to the post. |
|
| 74 | - * 'match_jsonld' => The matched `about_jsonld` column from wl_entities. |
|
| 75 | - * 'match_id' => This id points to id column of wl_entities table. |
|
| 76 | - * |
|
| 77 | - * @param array $items |
|
| 78 | - * |
|
| 79 | - * @return array |
|
| 80 | - */ |
|
| 81 | - private function map( array $items ) { |
|
| 82 | - return array_map( |
|
| 83 | - function ( $item ) { |
|
| 84 | - $data = json_decode( $item->match_jsonld, true ); |
|
| 85 | - $item->match_name = $data && is_array( $data ) && array_key_exists( 'name', $data ) ? $data['name'] : null; |
|
| 61 | + /** |
|
| 62 | + * Returns an array of rows where each row contains: |
|
| 63 | + * |
|
| 64 | + * 'post_title' => The title of the post |
|
| 65 | + * 'id' => The id of the post |
|
| 66 | + * 'post_link' => The edit post link |
|
| 67 | + * 'post_status' => The status of the post. |
|
| 68 | + * 'parent_post_title' => The title of the post linked to this post via wprm_parent_post_id property |
|
| 69 | + * ( this is only applicable when the post is wprm_recipe, returns null if not present ) |
|
| 70 | + * 'parent_post_id' => The id of the linked parent post. |
|
| 71 | + * 'parent_post_link' => The link to parent post. |
|
| 72 | + * 'view link' => The permalink to the post. |
|
| 73 | + * 'preview link' => The preview link to the post. |
|
| 74 | + * 'match_jsonld' => The matched `about_jsonld` column from wl_entities. |
|
| 75 | + * 'match_id' => This id points to id column of wl_entities table. |
|
| 76 | + * |
|
| 77 | + * @param array $items |
|
| 78 | + * |
|
| 79 | + * @return array |
|
| 80 | + */ |
|
| 81 | + private function map( array $items ) { |
|
| 82 | + return array_map( |
|
| 83 | + function ( $item ) { |
|
| 84 | + $data = json_decode( $item->match_jsonld, true ); |
|
| 85 | + $item->match_name = $data && is_array( $data ) && array_key_exists( 'name', $data ) ? $data['name'] : null; |
|
| 86 | 86 | |
| 87 | - if ( $item->id ) { |
|
| 88 | - $item->post_link = get_edit_post_link( $item->id, 'ui' ); |
|
| 89 | - $item->view_link = get_permalink( $item->id ); |
|
| 90 | - $item->preview_link = get_preview_post_link( $item->id ); |
|
| 91 | - } |
|
| 87 | + if ( $item->id ) { |
|
| 88 | + $item->post_link = get_edit_post_link( $item->id, 'ui' ); |
|
| 89 | + $item->view_link = get_permalink( $item->id ); |
|
| 90 | + $item->preview_link = get_preview_post_link( $item->id ); |
|
| 91 | + } |
|
| 92 | 92 | |
| 93 | - if ( $item->parent_post_id ) { |
|
| 94 | - $item->parent_post_link = get_edit_post_link( $item->parent_post_id, 'ui' ); |
|
| 95 | - } |
|
| 93 | + if ( $item->parent_post_id ) { |
|
| 94 | + $item->parent_post_link = get_edit_post_link( $item->parent_post_id, 'ui' ); |
|
| 95 | + } |
|
| 96 | 96 | |
| 97 | - return $item; |
|
| 98 | - }, |
|
| 99 | - $items |
|
| 100 | - ); |
|
| 101 | - } |
|
| 97 | + return $item; |
|
| 98 | + }, |
|
| 99 | + $items |
|
| 100 | + ); |
|
| 101 | + } |
|
| 102 | 102 | } |
@@ -11,24 +11,24 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | class Sort extends Match_Sort { |
| 13 | 13 | |
| 14 | - /** |
|
| 15 | - * Get field name. |
|
| 16 | - * |
|
| 17 | - * @return string|null |
|
| 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 This block will be filled in the future when occurrences handling is implemented |
|
| 29 | - return null; |
|
| 30 | - } |
|
| 14 | + /** |
|
| 15 | + * Get field name. |
|
| 16 | + * |
|
| 17 | + * @return string|null |
|
| 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 This block will be filled in the future when occurrences handling is implemented |
|
| 29 | + return null; |
|
| 30 | + } |
|
| 31 | 31 | |
| 32 | - return 't.name'; |
|
| 33 | - } |
|
| 32 | + return 't.name'; |
|
| 33 | + } |
|
| 34 | 34 | } |
@@ -7,89 +7,89 @@ |
||
| 7 | 7 | |
| 8 | 8 | class Rest_Controller { |
| 9 | 9 | |
| 10 | - /** |
|
| 11 | - * @var Synchronization_Service $synchronization_service |
|
| 12 | - */ |
|
| 13 | - private $synchronization_service; |
|
| 10 | + /** |
|
| 11 | + * @var Synchronization_Service $synchronization_service |
|
| 12 | + */ |
|
| 13 | + private $synchronization_service; |
|
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * @param Synchronization_Service $synchronization_service |
|
| 17 | - */ |
|
| 18 | - public function __construct( $synchronization_service ) { |
|
| 19 | - $this->synchronization_service = $synchronization_service; |
|
| 20 | - } |
|
| 15 | + /** |
|
| 16 | + * @param Synchronization_Service $synchronization_service |
|
| 17 | + */ |
|
| 18 | + public function __construct( $synchronization_service ) { |
|
| 19 | + $this->synchronization_service = $synchronization_service; |
|
| 20 | + } |
|
| 21 | 21 | |
| 22 | - public function register_hooks() { |
|
| 23 | - add_action( 'rest_api_init', array( $this, 'rest_api_init' ) ); |
|
| 24 | - } |
|
| 22 | + public function register_hooks() { |
|
| 23 | + add_action( 'rest_api_init', array( $this, 'rest_api_init' ) ); |
|
| 24 | + } |
|
| 25 | 25 | |
| 26 | - public function rest_api_init() { |
|
| 27 | - register_rest_route( |
|
| 28 | - 'wl-dashboard/v1', |
|
| 29 | - '/synchronizations', |
|
| 30 | - array( |
|
| 31 | - 'methods' => 'POST', |
|
| 32 | - 'callback' => array( $this, 'create_sync' ), |
|
| 33 | - 'permission_callback' => '__return_true', |
|
| 34 | - ) |
|
| 35 | - ); |
|
| 26 | + public function rest_api_init() { |
|
| 27 | + register_rest_route( |
|
| 28 | + 'wl-dashboard/v1', |
|
| 29 | + '/synchronizations', |
|
| 30 | + array( |
|
| 31 | + 'methods' => 'POST', |
|
| 32 | + 'callback' => array( $this, 'create_sync' ), |
|
| 33 | + 'permission_callback' => '__return_true', |
|
| 34 | + ) |
|
| 35 | + ); |
|
| 36 | 36 | |
| 37 | - register_rest_route( |
|
| 38 | - 'wl-dashboard/v1', |
|
| 39 | - '/synchronizations', |
|
| 40 | - array( |
|
| 41 | - 'methods' => 'GET', |
|
| 42 | - 'callback' => array( $this, 'list_syncs' ), |
|
| 43 | - 'permission_callback' => '__return_true', |
|
| 44 | - 'args' => array( |
|
| 45 | - 'is_running' => array( |
|
| 46 | - 'description' => esc_html__( 'Filter the is_running', 'wordlift' ), |
|
| 47 | - 'type' => 'boolean', |
|
| 48 | - ), |
|
| 49 | - ), |
|
| 50 | - ) |
|
| 51 | - ); |
|
| 37 | + register_rest_route( |
|
| 38 | + 'wl-dashboard/v1', |
|
| 39 | + '/synchronizations', |
|
| 40 | + array( |
|
| 41 | + 'methods' => 'GET', |
|
| 42 | + 'callback' => array( $this, 'list_syncs' ), |
|
| 43 | + 'permission_callback' => '__return_true', |
|
| 44 | + 'args' => array( |
|
| 45 | + 'is_running' => array( |
|
| 46 | + 'description' => esc_html__( 'Filter the is_running', 'wordlift' ), |
|
| 47 | + 'type' => 'boolean', |
|
| 48 | + ), |
|
| 49 | + ), |
|
| 50 | + ) |
|
| 51 | + ); |
|
| 52 | 52 | |
| 53 | - register_rest_route( |
|
| 54 | - 'wl-dashboard/v1', |
|
| 55 | - '/synchronizations', |
|
| 56 | - array( |
|
| 57 | - 'methods' => 'DELETE', |
|
| 58 | - 'callback' => array( $this, 'delete_syncs' ), |
|
| 59 | - 'permission_callback' => '__return_true', |
|
| 60 | - ) |
|
| 61 | - ); |
|
| 62 | - } |
|
| 53 | + register_rest_route( |
|
| 54 | + 'wl-dashboard/v1', |
|
| 55 | + '/synchronizations', |
|
| 56 | + array( |
|
| 57 | + 'methods' => 'DELETE', |
|
| 58 | + 'callback' => array( $this, 'delete_syncs' ), |
|
| 59 | + 'permission_callback' => '__return_true', |
|
| 60 | + ) |
|
| 61 | + ); |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | - public function create_sync() { |
|
| 65 | - try { |
|
| 66 | - return rest_ensure_response( $this->synchronization_service->create() ); |
|
| 67 | - } catch ( \Exception $e ) { |
|
| 68 | - return new \WP_Error( |
|
| 69 | - 'wl_error_synchronization_running', |
|
| 70 | - esc_html__( 'Another synchronization is already running.', 'wordlift' ), |
|
| 71 | - array( 'status' => 409 ) |
|
| 72 | - ); |
|
| 73 | - } |
|
| 74 | - } |
|
| 64 | + public function create_sync() { |
|
| 65 | + try { |
|
| 66 | + return rest_ensure_response( $this->synchronization_service->create() ); |
|
| 67 | + } catch ( \Exception $e ) { |
|
| 68 | + return new \WP_Error( |
|
| 69 | + 'wl_error_synchronization_running', |
|
| 70 | + esc_html__( 'Another synchronization is already running.', 'wordlift' ), |
|
| 71 | + array( 'status' => 409 ) |
|
| 72 | + ); |
|
| 73 | + } |
|
| 74 | + } |
|
| 75 | 75 | |
| 76 | - public function list_syncs( WP_REST_Request $request ) { |
|
| 77 | - $last_synchronization = $this->synchronization_service->load(); |
|
| 78 | - $is_running_all = ! $request->has_param( 'is_running' ); |
|
| 79 | - if ( is_a( $last_synchronization, 'Wordlift\Modules\Dashboard\Synchronization\Synchronization' ) |
|
| 80 | - && ( $is_running_all || $request->get_param( 'is_running' ) === $last_synchronization->is_running() ) ) { |
|
| 81 | - $data = array( $last_synchronization ); |
|
| 82 | - } else { |
|
| 83 | - $data = array(); |
|
| 84 | - } |
|
| 76 | + public function list_syncs( WP_REST_Request $request ) { |
|
| 77 | + $last_synchronization = $this->synchronization_service->load(); |
|
| 78 | + $is_running_all = ! $request->has_param( 'is_running' ); |
|
| 79 | + if ( is_a( $last_synchronization, 'Wordlift\Modules\Dashboard\Synchronization\Synchronization' ) |
|
| 80 | + && ( $is_running_all || $request->get_param( 'is_running' ) === $last_synchronization->is_running() ) ) { |
|
| 81 | + $data = array( $last_synchronization ); |
|
| 82 | + } else { |
|
| 83 | + $data = array(); |
|
| 84 | + } |
|
| 85 | 85 | |
| 86 | - return rest_ensure_response( array( 'items' => $data ) ); |
|
| 87 | - } |
|
| 86 | + return rest_ensure_response( array( 'items' => $data ) ); |
|
| 87 | + } |
|
| 88 | 88 | |
| 89 | - public function delete_syncs() { |
|
| 90 | - $this->synchronization_service->delete_syncs(); |
|
| 89 | + public function delete_syncs() { |
|
| 90 | + $this->synchronization_service->delete_syncs(); |
|
| 91 | 91 | |
| 92 | - return new WP_REST_Response(); |
|
| 93 | - } |
|
| 92 | + return new WP_REST_Response(); |
|
| 93 | + } |
|
| 94 | 94 | |
| 95 | 95 | } |
@@ -4,75 +4,75 @@ |
||
| 4 | 4 | |
| 5 | 5 | abstract class Match_Sort { |
| 6 | 6 | |
| 7 | - /** |
|
| 8 | - * The sort string, example: +name,-name,-id,+id |
|
| 9 | - * |
|
| 10 | - * @var string |
|
| 11 | - */ |
|
| 12 | - protected $sort; |
|
| 7 | + /** |
|
| 8 | + * The sort string, example: +name,-name,-id,+id |
|
| 9 | + * |
|
| 10 | + * @var string |
|
| 11 | + */ |
|
| 12 | + protected $sort; |
|
| 13 | 13 | |
| 14 | - public function __construct( $sort ) { |
|
| 15 | - $this->sort = $sort; |
|
| 16 | - } |
|
| 17 | - abstract public function get_field_name(); |
|
| 14 | + public function __construct( $sort ) { |
|
| 15 | + $this->sort = $sort; |
|
| 16 | + } |
|
| 17 | + abstract public function get_field_name(); |
|
| 18 | 18 | |
| 19 | - public function property_name() { |
|
| 20 | - $tmp_sort_field_name = substr( $this->sort, 1 ); |
|
| 21 | - // @todo: Need to discuss this code due to unit tests error - Undefined property: stdClass::$name. |
|
| 22 | - if ( 'id' === $tmp_sort_field_name || str_contains( $tmp_sort_field_name, 'date_modified_gmt' ) ) { |
|
| 23 | - return 'id'; |
|
| 24 | - } else { |
|
| 25 | - return 'name'; |
|
| 26 | - } |
|
| 27 | - } |
|
| 19 | + public function property_name() { |
|
| 20 | + $tmp_sort_field_name = substr( $this->sort, 1 ); |
|
| 21 | + // @todo: Need to discuss this code due to unit tests error - Undefined property: stdClass::$name. |
|
| 22 | + if ( 'id' === $tmp_sort_field_name || str_contains( $tmp_sort_field_name, 'date_modified_gmt' ) ) { |
|
| 23 | + return 'id'; |
|
| 24 | + } else { |
|
| 25 | + return 'name'; |
|
| 26 | + } |
|
| 27 | + } |
|
| 28 | 28 | |
| 29 | - public function is_ascending() { |
|
| 30 | - return strpos( $this->sort, '-' ) !== 0; |
|
| 31 | - } |
|
| 29 | + public function is_ascending() { |
|
| 30 | + return strpos( $this->sort, '-' ) !== 0; |
|
| 31 | + } |
|
| 32 | 32 | |
| 33 | - private function get_sort_order( $direction, $sort_ascending ) { |
|
| 34 | - switch ( array( $sort_ascending, $direction ) ) { |
|
| 35 | - case array( true, 'ASCENDING' ): |
|
| 36 | - case array( false, 'DESCENDING' ): |
|
| 37 | - return 'ASC'; |
|
| 38 | - case array( true, 'DESCENDING' ): |
|
| 39 | - case array( false, 'ASCENDING' ): |
|
| 40 | - return 'DESC'; |
|
| 41 | - } |
|
| 33 | + private function get_sort_order( $direction, $sort_ascending ) { |
|
| 34 | + switch ( array( $sort_ascending, $direction ) ) { |
|
| 35 | + case array( true, 'ASCENDING' ): |
|
| 36 | + case array( false, 'DESCENDING' ): |
|
| 37 | + return 'ASC'; |
|
| 38 | + case array( true, 'DESCENDING' ): |
|
| 39 | + case array( false, 'ASCENDING' ): |
|
| 40 | + return 'DESC'; |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | - return 'ASC'; |
|
| 44 | - } |
|
| 43 | + return 'ASC'; |
|
| 44 | + } |
|
| 45 | 45 | |
| 46 | - public function get_orderby_clause( $direction ) { |
|
| 47 | - $sort_order = $this->get_sort_order( $direction, $this->is_ascending() ); |
|
| 48 | - return " ORDER BY {$this->get_field_name()} $sort_order"; |
|
| 49 | - } |
|
| 46 | + public function get_orderby_clause( $direction ) { |
|
| 47 | + $sort_order = $this->get_sort_order( $direction, $this->is_ascending() ); |
|
| 48 | + return " ORDER BY {$this->get_field_name()} $sort_order"; |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | - public function apply( &$items ) { |
|
| 52 | - $sort_ascending = $this->is_ascending(); |
|
| 53 | - $sort_property_name = $this->property_name(); |
|
| 54 | - usort( |
|
| 55 | - $items, |
|
| 56 | - function ( $a, $b ) use ( $sort_ascending, $sort_property_name ) { |
|
| 57 | - if ( $a->{$sort_property_name} === $b->{$sort_property_name} ) { |
|
| 58 | - return 0; |
|
| 59 | - } |
|
| 51 | + public function apply( &$items ) { |
|
| 52 | + $sort_ascending = $this->is_ascending(); |
|
| 53 | + $sort_property_name = $this->property_name(); |
|
| 54 | + usort( |
|
| 55 | + $items, |
|
| 56 | + function ( $a, $b ) use ( $sort_ascending, $sort_property_name ) { |
|
| 57 | + if ( $a->{$sort_property_name} === $b->{$sort_property_name} ) { |
|
| 58 | + return 0; |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | - switch ( array( |
|
| 62 | - $sort_ascending, |
|
| 63 | - $a->{$sort_property_name} > $b->{$sort_property_name}, |
|
| 64 | - ) ) { |
|
| 65 | - case array( true, true ): |
|
| 66 | - case array( false, false ): |
|
| 67 | - return 1; |
|
| 68 | - case array( true, false ): |
|
| 69 | - case array( false, true ): |
|
| 70 | - return - 1; |
|
| 71 | - } |
|
| 61 | + switch ( array( |
|
| 62 | + $sort_ascending, |
|
| 63 | + $a->{$sort_property_name} > $b->{$sort_property_name}, |
|
| 64 | + ) ) { |
|
| 65 | + case array( true, true ): |
|
| 66 | + case array( false, false ): |
|
| 67 | + return 1; |
|
| 68 | + case array( true, false ): |
|
| 69 | + case array( false, true ): |
|
| 70 | + return - 1; |
|
| 71 | + } |
|
| 72 | 72 | |
| 73 | - return 0; |
|
| 74 | - } |
|
| 75 | - ); |
|
| 76 | - } |
|
| 73 | + return 0; |
|
| 74 | + } |
|
| 75 | + ); |
|
| 76 | + } |
|
| 77 | 77 | |
| 78 | 78 | } |
@@ -17,239 +17,239 @@ |
||
| 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 | - WL_REST_ROUTE_DEFAULT_NAMESPACE, |
|
| 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', 'all' ), |
|
| 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 | - WL_REST_ROUTE_DEFAULT_NAMESPACE, |
|
| 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 | - 'type' => 'integer', |
|
| 109 | - ), |
|
| 110 | - ), |
|
| 111 | - 'permission_callback' => function () { |
|
| 112 | - return current_user_can( 'manage_options' ); |
|
| 113 | - }, |
|
| 114 | - ) |
|
| 115 | - ); |
|
| 116 | - |
|
| 117 | - // Update an existing post match |
|
| 118 | - register_rest_route( |
|
| 119 | - WL_REST_ROUTE_DEFAULT_NAMESPACE, |
|
| 120 | - '/post-matches/(?P<post_id>\d+)/matches/(?P<match_id>\d+)', |
|
| 121 | - array( |
|
| 122 | - 'methods' => 'PUT', |
|
| 123 | - 'callback' => array( $this, 'update_post_match' ), |
|
| 124 | - 'args' => array( |
|
| 125 | - 'post_id' => array( |
|
| 126 | - 'required' => true, |
|
| 127 | - 'validate_callback' => 'rest_validate_request_arg', |
|
| 128 | - 'type' => 'integer', |
|
| 129 | - ), |
|
| 130 | - 'match_id' => array( |
|
| 131 | - 'required' => true, |
|
| 132 | - 'validate_callback' => 'rest_validate_request_arg', |
|
| 133 | - 'type' => 'integer', |
|
| 134 | - ), |
|
| 135 | - ), |
|
| 136 | - 'permission_callback' => function () { |
|
| 137 | - |
|
| 138 | - return current_user_can( 'manage_options' ); |
|
| 139 | - }, |
|
| 140 | - ) |
|
| 141 | - ); |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - /** |
|
| 145 | - * Get the term matches by taxonomy name. |
|
| 146 | - * |
|
| 147 | - * @var $request \WP_REST_Request |
|
| 148 | - */ |
|
| 149 | - public function get_post_matches( $request ) { |
|
| 150 | - |
|
| 151 | - $cursor = $request->get_param( 'cursor' ); |
|
| 152 | - if ( $request->has_param( 'limit' ) ) { |
|
| 153 | - $cursor['limit'] = $request->get_param( 'limit' ); |
|
| 154 | - } |
|
| 155 | - if ( $request->has_param( 'sort' ) ) { |
|
| 156 | - $cursor['sort'] = $request->get_param( 'sort' ); |
|
| 157 | - } |
|
| 158 | - if ( $request->has_param( 'post_types' ) ) { |
|
| 159 | - $cursor['query']['post_types'] = $request->get_param( 'post_types' ); |
|
| 160 | - } |
|
| 161 | - if ( $request->has_param( 'has_match' ) ) { |
|
| 162 | - $cursor['query']['has_match'] = $request->get_param( 'has_match' ); |
|
| 163 | - } |
|
| 164 | - if ( $request->has_param( 'post_status' ) ) { |
|
| 165 | - $cursor['query']['post_status'] = $request->get_param( 'post_status' ); |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - // Query. |
|
| 169 | - $post_types = isset( $cursor['query']['post_types'] ) ? $cursor['query']['post_types'] : apply_filters( |
|
| 170 | - 'wl_dashboard__post_entity_match__post_types', |
|
| 171 | - array( |
|
| 172 | - 'post', |
|
| 173 | - 'page', |
|
| 174 | - ) |
|
| 175 | - ); |
|
| 176 | - $has_match = isset( $cursor['query']['has_match'] ) ? $cursor['query']['has_match'] : null; |
|
| 177 | - |
|
| 178 | - $post_status = isset( $cursor['query']['post_status'] ) ? $cursor['query']['post_status'] : null; |
|
| 179 | - |
|
| 180 | - $items = $this->match_service->list_items( |
|
| 181 | - array( |
|
| 182 | - // Query |
|
| 183 | - 'post_types' => $post_types, |
|
| 184 | - 'has_match' => $has_match, |
|
| 185 | - 'post_status' => $post_status, |
|
| 186 | - // Cursor-Pagination |
|
| 187 | - 'position' => $cursor['position'], |
|
| 188 | - 'element' => $cursor['element'], |
|
| 189 | - 'direction' => $cursor['direction'], |
|
| 190 | - // `+1` to check if we have other results. |
|
| 191 | - 'limit' => $cursor['limit'] + 1, |
|
| 192 | - 'sort' => $cursor['sort'], |
|
| 193 | - ) |
|
| 194 | - ); |
|
| 195 | - |
|
| 196 | - return new Cursor_Page( |
|
| 197 | - $items, |
|
| 198 | - $cursor['position'], |
|
| 199 | - $cursor['element'], |
|
| 200 | - $cursor['direction'], |
|
| 201 | - $cursor['sort'], |
|
| 202 | - $cursor['limit'], |
|
| 203 | - $cursor['query'] |
|
| 204 | - ); |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - /** |
|
| 208 | - * Create a new match for a post. |
|
| 209 | - * |
|
| 210 | - * @param $request \WP_REST_Request |
|
| 211 | - * |
|
| 212 | - * @throws \Exception If there was a problem creating the match. |
|
| 213 | - */ |
|
| 214 | - public function create_post_match( $request ) { |
|
| 215 | - $post_id = $request->get_param( 'post_id' ); |
|
| 216 | - |
|
| 217 | - // If we dont have a entry on the match table, then add one. |
|
| 218 | - $content_id = Wordpress_Content_Id::create_post( $post_id ); |
|
| 219 | - if ( ! Wordpress_Content_Service::get_instance()->get_entity_id( $content_id ) ) { |
|
| 220 | - $uri = Entity_Uri_Generator::create_uri( $content_id->get_type(), $content_id->get_id() ); |
|
| 221 | - Wordpress_Content_Service::get_instance()->set_entity_id( $content_id, $uri ); |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - $match_id = $this->match_service->get_id( |
|
| 225 | - $post_id, |
|
| 226 | - Object_Type_Enum::POST |
|
| 227 | - ); |
|
| 228 | - |
|
| 229 | - return $this->match_service->set_jsonld( |
|
| 230 | - $post_id, |
|
| 231 | - Object_Type_Enum::POST, |
|
| 232 | - $match_id, |
|
| 233 | - $request->get_json_params() |
|
| 234 | - ); |
|
| 235 | - } |
|
| 236 | - |
|
| 237 | - /** |
|
| 238 | - * Update post match. |
|
| 239 | - * |
|
| 240 | - * @param $request \WP_REST_Request |
|
| 241 | - * |
|
| 242 | - * @return Match_Entry |
|
| 243 | - * |
|
| 244 | - * @throws \Exception If there was a problem updating the match. |
|
| 245 | - */ |
|
| 246 | - public function update_post_match( $request ) { |
|
| 247 | - |
|
| 248 | - return $this->match_service->set_jsonld( |
|
| 249 | - $request->get_param( 'post_id' ), |
|
| 250 | - Object_Type_Enum::POST, |
|
| 251 | - $request->get_param( 'match_id' ), |
|
| 252 | - $request->get_json_params() |
|
| 253 | - ); |
|
| 254 | - } |
|
| 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 | + WL_REST_ROUTE_DEFAULT_NAMESPACE, |
|
| 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', 'all' ), |
|
| 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 | + WL_REST_ROUTE_DEFAULT_NAMESPACE, |
|
| 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 | + 'type' => 'integer', |
|
| 109 | + ), |
|
| 110 | + ), |
|
| 111 | + 'permission_callback' => function () { |
|
| 112 | + return current_user_can( 'manage_options' ); |
|
| 113 | + }, |
|
| 114 | + ) |
|
| 115 | + ); |
|
| 116 | + |
|
| 117 | + // Update an existing post match |
|
| 118 | + register_rest_route( |
|
| 119 | + WL_REST_ROUTE_DEFAULT_NAMESPACE, |
|
| 120 | + '/post-matches/(?P<post_id>\d+)/matches/(?P<match_id>\d+)', |
|
| 121 | + array( |
|
| 122 | + 'methods' => 'PUT', |
|
| 123 | + 'callback' => array( $this, 'update_post_match' ), |
|
| 124 | + 'args' => array( |
|
| 125 | + 'post_id' => array( |
|
| 126 | + 'required' => true, |
|
| 127 | + 'validate_callback' => 'rest_validate_request_arg', |
|
| 128 | + 'type' => 'integer', |
|
| 129 | + ), |
|
| 130 | + 'match_id' => array( |
|
| 131 | + 'required' => true, |
|
| 132 | + 'validate_callback' => 'rest_validate_request_arg', |
|
| 133 | + 'type' => 'integer', |
|
| 134 | + ), |
|
| 135 | + ), |
|
| 136 | + 'permission_callback' => function () { |
|
| 137 | + |
|
| 138 | + return current_user_can( 'manage_options' ); |
|
| 139 | + }, |
|
| 140 | + ) |
|
| 141 | + ); |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + /** |
|
| 145 | + * Get the term matches by taxonomy name. |
|
| 146 | + * |
|
| 147 | + * @var $request \WP_REST_Request |
|
| 148 | + */ |
|
| 149 | + public function get_post_matches( $request ) { |
|
| 150 | + |
|
| 151 | + $cursor = $request->get_param( 'cursor' ); |
|
| 152 | + if ( $request->has_param( 'limit' ) ) { |
|
| 153 | + $cursor['limit'] = $request->get_param( 'limit' ); |
|
| 154 | + } |
|
| 155 | + if ( $request->has_param( 'sort' ) ) { |
|
| 156 | + $cursor['sort'] = $request->get_param( 'sort' ); |
|
| 157 | + } |
|
| 158 | + if ( $request->has_param( 'post_types' ) ) { |
|
| 159 | + $cursor['query']['post_types'] = $request->get_param( 'post_types' ); |
|
| 160 | + } |
|
| 161 | + if ( $request->has_param( 'has_match' ) ) { |
|
| 162 | + $cursor['query']['has_match'] = $request->get_param( 'has_match' ); |
|
| 163 | + } |
|
| 164 | + if ( $request->has_param( 'post_status' ) ) { |
|
| 165 | + $cursor['query']['post_status'] = $request->get_param( 'post_status' ); |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + // Query. |
|
| 169 | + $post_types = isset( $cursor['query']['post_types'] ) ? $cursor['query']['post_types'] : apply_filters( |
|
| 170 | + 'wl_dashboard__post_entity_match__post_types', |
|
| 171 | + array( |
|
| 172 | + 'post', |
|
| 173 | + 'page', |
|
| 174 | + ) |
|
| 175 | + ); |
|
| 176 | + $has_match = isset( $cursor['query']['has_match'] ) ? $cursor['query']['has_match'] : null; |
|
| 177 | + |
|
| 178 | + $post_status = isset( $cursor['query']['post_status'] ) ? $cursor['query']['post_status'] : null; |
|
| 179 | + |
|
| 180 | + $items = $this->match_service->list_items( |
|
| 181 | + array( |
|
| 182 | + // Query |
|
| 183 | + 'post_types' => $post_types, |
|
| 184 | + 'has_match' => $has_match, |
|
| 185 | + 'post_status' => $post_status, |
|
| 186 | + // Cursor-Pagination |
|
| 187 | + 'position' => $cursor['position'], |
|
| 188 | + 'element' => $cursor['element'], |
|
| 189 | + 'direction' => $cursor['direction'], |
|
| 190 | + // `+1` to check if we have other results. |
|
| 191 | + 'limit' => $cursor['limit'] + 1, |
|
| 192 | + 'sort' => $cursor['sort'], |
|
| 193 | + ) |
|
| 194 | + ); |
|
| 195 | + |
|
| 196 | + return new Cursor_Page( |
|
| 197 | + $items, |
|
| 198 | + $cursor['position'], |
|
| 199 | + $cursor['element'], |
|
| 200 | + $cursor['direction'], |
|
| 201 | + $cursor['sort'], |
|
| 202 | + $cursor['limit'], |
|
| 203 | + $cursor['query'] |
|
| 204 | + ); |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + /** |
|
| 208 | + * Create a new match for a post. |
|
| 209 | + * |
|
| 210 | + * @param $request \WP_REST_Request |
|
| 211 | + * |
|
| 212 | + * @throws \Exception If there was a problem creating the match. |
|
| 213 | + */ |
|
| 214 | + public function create_post_match( $request ) { |
|
| 215 | + $post_id = $request->get_param( 'post_id' ); |
|
| 216 | + |
|
| 217 | + // If we dont have a entry on the match table, then add one. |
|
| 218 | + $content_id = Wordpress_Content_Id::create_post( $post_id ); |
|
| 219 | + if ( ! Wordpress_Content_Service::get_instance()->get_entity_id( $content_id ) ) { |
|
| 220 | + $uri = Entity_Uri_Generator::create_uri( $content_id->get_type(), $content_id->get_id() ); |
|
| 221 | + Wordpress_Content_Service::get_instance()->set_entity_id( $content_id, $uri ); |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + $match_id = $this->match_service->get_id( |
|
| 225 | + $post_id, |
|
| 226 | + Object_Type_Enum::POST |
|
| 227 | + ); |
|
| 228 | + |
|
| 229 | + return $this->match_service->set_jsonld( |
|
| 230 | + $post_id, |
|
| 231 | + Object_Type_Enum::POST, |
|
| 232 | + $match_id, |
|
| 233 | + $request->get_json_params() |
|
| 234 | + ); |
|
| 235 | + } |
|
| 236 | + |
|
| 237 | + /** |
|
| 238 | + * Update post match. |
|
| 239 | + * |
|
| 240 | + * @param $request \WP_REST_Request |
|
| 241 | + * |
|
| 242 | + * @return Match_Entry |
|
| 243 | + * |
|
| 244 | + * @throws \Exception If there was a problem updating the match. |
|
| 245 | + */ |
|
| 246 | + public function update_post_match( $request ) { |
|
| 247 | + |
|
| 248 | + return $this->match_service->set_jsonld( |
|
| 249 | + $request->get_param( 'post_id' ), |
|
| 250 | + Object_Type_Enum::POST, |
|
| 251 | + $request->get_param( 'match_id' ), |
|
| 252 | + $request->get_json_params() |
|
| 253 | + ); |
|
| 254 | + } |
|
| 255 | 255 | } |
@@ -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 If there was a problem with SQL query. |
|
| 19 | - */ |
|
| 20 | - public function build() { |
|
| 15 | + /** |
|
| 16 | + * Build. |
|
| 17 | + * |
|
| 18 | + * @throws \Exception If there was a problem with SQL query. |
|
| 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,48 +34,48 @@ 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 When supplied argument is not an array, throw exception. |
|
| 52 | - */ |
|
| 53 | - public function taxonomy() { |
|
| 54 | - $taxonomies = $this->params['taxonomies']; |
|
| 46 | + /** |
|
| 47 | + * Taxonomy. |
|
| 48 | + * |
|
| 49 | + * @return $this |
|
| 50 | + * |
|
| 51 | + * @throws \Exception When supplied argument is not an array, throw 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 | - /** |
|
| 66 | - * Ingredient name contains. |
|
| 67 | - * |
|
| 68 | - * @return $this |
|
| 69 | - */ |
|
| 70 | - public function ingredient_name_contains() { |
|
| 71 | - global $wpdb; |
|
| 65 | + /** |
|
| 66 | + * Ingredient name contains. |
|
| 67 | + * |
|
| 68 | + * @return $this |
|
| 69 | + */ |
|
| 70 | + public function ingredient_name_contains() { |
|
| 71 | + global $wpdb; |
|
| 72 | 72 | |
| 73 | - // If the ingredient_name_contains value is a non-empty string, add the filter |
|
| 74 | - if ( is_string( $this->params['ingredient_name_contains'] ) && ! empty( $this->params['ingredient_name_contains'] ) ) { |
|
| 75 | - $ingredient_name_contains = $this->params['ingredient_name_contains']; |
|
| 76 | - $this->sql .= $wpdb->prepare( ' AND t.name LIKE %s', '%' . $ingredient_name_contains . '%' ); |
|
| 77 | - } |
|
| 73 | + // If the ingredient_name_contains value is a non-empty string, add the filter |
|
| 74 | + if ( is_string( $this->params['ingredient_name_contains'] ) && ! empty( $this->params['ingredient_name_contains'] ) ) { |
|
| 75 | + $ingredient_name_contains = $this->params['ingredient_name_contains']; |
|
| 76 | + $this->sql .= $wpdb->prepare( ' AND t.name LIKE %s', '%' . $ingredient_name_contains . '%' ); |
|
| 77 | + } |
|
| 78 | 78 | |
| 79 | - return $this; |
|
| 80 | - } |
|
| 79 | + return $this; |
|
| 80 | + } |
|
| 81 | 81 | } |