@@ -5,51 +5,51 @@ discard block |
||
| 5 | 5 | use WP_REST_Request; |
| 6 | 6 | |
| 7 | 7 | class Post_Query { |
| 8 | - /** |
|
| 9 | - * @var WP_REST_Request |
|
| 10 | - */ |
|
| 11 | - private $request; |
|
| 12 | - /** |
|
| 13 | - * @var mixed |
|
| 14 | - */ |
|
| 15 | - private $position; |
|
| 16 | - /** |
|
| 17 | - * @var mixed |
|
| 18 | - */ |
|
| 19 | - private $element; |
|
| 20 | - /** |
|
| 21 | - * @var mixed |
|
| 22 | - */ |
|
| 23 | - private $direction; |
|
| 24 | - |
|
| 25 | - private $sort; |
|
| 26 | - |
|
| 27 | - private $sortby; |
|
| 28 | - /** |
|
| 29 | - * @var mixed |
|
| 30 | - */ |
|
| 31 | - private $limit; |
|
| 32 | - |
|
| 33 | - /** @var Cursor_Sort $cursor_sort */ |
|
| 34 | - private $cursor_sort; |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * @param WP_REST_Request $request |
|
| 38 | - * @param Cursor $cursor |
|
| 39 | - */ |
|
| 40 | - public function __construct( $request, $cursor, $cursor_sort, $limit ) { |
|
| 41 | - global $wpdb; |
|
| 42 | - |
|
| 43 | - $this->request = $request; |
|
| 44 | - $this->position = $cursor->get_position(); |
|
| 45 | - $this->element = $cursor->get_element(); |
|
| 46 | - $this->direction = $cursor->get_direction(); |
|
| 47 | - $this->limit = $limit; |
|
| 48 | - $this->cursor_sort = $cursor_sort; |
|
| 49 | - |
|
| 50 | - $this->set_sort(); |
|
| 51 | - |
|
| 52 | - $this->sql = " |
|
| 8 | + /** |
|
| 9 | + * @var WP_REST_Request |
|
| 10 | + */ |
|
| 11 | + private $request; |
|
| 12 | + /** |
|
| 13 | + * @var mixed |
|
| 14 | + */ |
|
| 15 | + private $position; |
|
| 16 | + /** |
|
| 17 | + * @var mixed |
|
| 18 | + */ |
|
| 19 | + private $element; |
|
| 20 | + /** |
|
| 21 | + * @var mixed |
|
| 22 | + */ |
|
| 23 | + private $direction; |
|
| 24 | + |
|
| 25 | + private $sort; |
|
| 26 | + |
|
| 27 | + private $sortby; |
|
| 28 | + /** |
|
| 29 | + * @var mixed |
|
| 30 | + */ |
|
| 31 | + private $limit; |
|
| 32 | + |
|
| 33 | + /** @var Cursor_Sort $cursor_sort */ |
|
| 34 | + private $cursor_sort; |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * @param WP_REST_Request $request |
|
| 38 | + * @param Cursor $cursor |
|
| 39 | + */ |
|
| 40 | + public function __construct( $request, $cursor, $cursor_sort, $limit ) { |
|
| 41 | + global $wpdb; |
|
| 42 | + |
|
| 43 | + $this->request = $request; |
|
| 44 | + $this->position = $cursor->get_position(); |
|
| 45 | + $this->element = $cursor->get_element(); |
|
| 46 | + $this->direction = $cursor->get_direction(); |
|
| 47 | + $this->limit = $limit; |
|
| 48 | + $this->cursor_sort = $cursor_sort; |
|
| 49 | + |
|
| 50 | + $this->set_sort(); |
|
| 51 | + |
|
| 52 | + $this->sql = " |
|
| 53 | 53 | SELECT p.ID as id, |
| 54 | 54 | p.post_title, |
| 55 | 55 | p.post_status, |
@@ -62,131 +62,131 @@ discard block |
||
| 62 | 62 | WHERE 1=1 |
| 63 | 63 | "; |
| 64 | 64 | |
| 65 | - $this->cursor(); |
|
| 66 | - $this->has_match(); |
|
| 67 | - $this->post_types(); |
|
| 68 | - $this->post_status(); |
|
| 69 | - $this->sort(); |
|
| 70 | - $this->limit(); |
|
| 71 | - |
|
| 72 | - // p.post_modified_gmt $condition %s |
|
| 73 | - // AND p.post_types IN ( '" . implode( "','", $post_types ) . "' ) |
|
| 74 | - // AND p.post_status = %s |
|
| 75 | - // ORDER BY p.post_modified_gmt $sort |
|
| 76 | - // LIMIT %d |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - public function get_results() { |
|
| 80 | - global $wpdb; |
|
| 81 | - |
|
| 82 | - // The `sql` is prepared in each delegated function in this class. |
|
| 83 | - // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
| 84 | - $items = $wpdb->get_results( $this->sql ); |
|
| 85 | - |
|
| 86 | - $sort = ( $this->sort === 'ASC' ? SORT_ASC : SORT_DESC ); |
|
| 87 | - array_multisort( array_column( $items, $this->cursor_sort->get_sort_property() ), $sort, $items ); |
|
| 88 | - $items = array_map( array( $this, 'map_item' ), $items ); |
|
| 89 | - |
|
| 90 | - return $items; |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - public function map_item( $item ) { |
|
| 94 | - $item->post_link = get_edit_post_link( $item->id, 'ui' ); |
|
| 95 | - $item->view_link = get_permalink( $item->id ); |
|
| 96 | - $item->preview_link = get_preview_post_link( $item->id ); |
|
| 97 | - |
|
| 98 | - return $item; |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - private function post_types() { |
|
| 102 | - $post_types = $this->request->has_param( 'post_types' ) |
|
| 103 | - ? (array) $this->request->get_param( 'post_types' ) |
|
| 104 | - : array( 'post', 'page' ); |
|
| 105 | - $value = array_map( 'esc_sql', $post_types ); |
|
| 106 | - $this->sql .= " AND p.post_type IN ( '" . implode( "', '", $value ) . "' )"; |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - private function limit() { |
|
| 110 | - $value = is_numeric( $this->limit ) ? $this->limit : 10; |
|
| 111 | - $this->sql .= ' LIMIT ' . esc_sql( $value ); |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - private function has_match() { |
|
| 115 | - if ( ! $this->request->has_param( 'has_match' ) ) { |
|
| 116 | - return; |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - $value = (bool) $this->request->get_param( 'has_match' ); |
|
| 120 | - |
|
| 121 | - if ( $value ) { |
|
| 122 | - $this->sql .= ' AND e.about_jsonld IS NOT NULL'; |
|
| 123 | - } else { |
|
| 124 | - $this->sql .= ' AND e.about_jsonld IS NULL'; |
|
| 125 | - } |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - private function sort() { |
|
| 129 | - switch ( $this->direction . '$' . $this->sort ) { |
|
| 130 | - case 'ASCENDING$ASC': |
|
| 131 | - case 'DESCENDING$DESC': |
|
| 132 | - $sort = 'ASC'; |
|
| 133 | - break; |
|
| 134 | - case 'ASCENDING$DESC': |
|
| 135 | - case 'DESCENDING$ASC': |
|
| 136 | - $sort = 'DESC'; |
|
| 137 | - break; |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - $this->sql .= ' ORDER BY p.' . $this->sortby . ' ' . $sort; |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - private function post_status() { |
|
| 144 | - if ( ! $this->request->has_param( 'post_status' ) ) { |
|
| 145 | - $this->sql .= " AND p.post_status IN ( 'draft', 'publish' ) "; |
|
| 146 | - |
|
| 147 | - return; |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - global $wpdb; |
|
| 151 | - $value = $this->request->get_param( 'post_status' ); |
|
| 152 | - $this->sql .= $wpdb->prepare( ' AND p.post_status = %s', $value ); |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - private function cursor() { |
|
| 156 | - if ( ! isset( $this->position ) ) { |
|
| 157 | - return; |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - switch ( $this->direction . '$' . $this->sort ) { |
|
| 161 | - case 'ASCENDING$ASC': |
|
| 162 | - case 'DESCENDING$DESC': |
|
| 163 | - $condition = '>'; |
|
| 164 | - break; |
|
| 165 | - case 'ASCENDING$DESC': |
|
| 166 | - case 'DESCENDING$ASC': |
|
| 167 | - $condition = '<'; |
|
| 168 | - break; |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - $condition .= ( $this->element === 'INCLUDED' ? '=' : '' ); |
|
| 172 | - global $wpdb; |
|
| 173 | - // We control the vars in this method. |
|
| 174 | - // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
| 175 | - $this->sql .= $wpdb->prepare( ' AND p.' . esc_sql( $this->sortby ) . ' ' . $condition . ' %s', $this->position ); |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - private function set_sort() { |
|
| 179 | - $sortby_to_col = array( |
|
| 180 | - 'date_modified_gmt' => 'post_modified_gmt', |
|
| 181 | - ); |
|
| 182 | - |
|
| 183 | - $value = $this->request->has_param( 'sort' ) |
|
| 184 | - ? $this->request->get_param( 'sort' ) |
|
| 185 | - : '-date_modified_gmt'; |
|
| 186 | - |
|
| 187 | - $sortby = substr( $value, 1 ); |
|
| 188 | - $this->sortby = isset( $sortby_to_col[ $sortby ] ) ? $sortby_to_col[ $sortby ] : $sortby; |
|
| 189 | - $this->sort = substr( $value, 0, 1 ) === '+' ? 'ASC' : 'DESC'; |
|
| 190 | - } |
|
| 65 | + $this->cursor(); |
|
| 66 | + $this->has_match(); |
|
| 67 | + $this->post_types(); |
|
| 68 | + $this->post_status(); |
|
| 69 | + $this->sort(); |
|
| 70 | + $this->limit(); |
|
| 71 | + |
|
| 72 | + // p.post_modified_gmt $condition %s |
|
| 73 | + // AND p.post_types IN ( '" . implode( "','", $post_types ) . "' ) |
|
| 74 | + // AND p.post_status = %s |
|
| 75 | + // ORDER BY p.post_modified_gmt $sort |
|
| 76 | + // LIMIT %d |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + public function get_results() { |
|
| 80 | + global $wpdb; |
|
| 81 | + |
|
| 82 | + // The `sql` is prepared in each delegated function in this class. |
|
| 83 | + // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
| 84 | + $items = $wpdb->get_results( $this->sql ); |
|
| 85 | + |
|
| 86 | + $sort = ( $this->sort === 'ASC' ? SORT_ASC : SORT_DESC ); |
|
| 87 | + array_multisort( array_column( $items, $this->cursor_sort->get_sort_property() ), $sort, $items ); |
|
| 88 | + $items = array_map( array( $this, 'map_item' ), $items ); |
|
| 89 | + |
|
| 90 | + return $items; |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + public function map_item( $item ) { |
|
| 94 | + $item->post_link = get_edit_post_link( $item->id, 'ui' ); |
|
| 95 | + $item->view_link = get_permalink( $item->id ); |
|
| 96 | + $item->preview_link = get_preview_post_link( $item->id ); |
|
| 97 | + |
|
| 98 | + return $item; |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + private function post_types() { |
|
| 102 | + $post_types = $this->request->has_param( 'post_types' ) |
|
| 103 | + ? (array) $this->request->get_param( 'post_types' ) |
|
| 104 | + : array( 'post', 'page' ); |
|
| 105 | + $value = array_map( 'esc_sql', $post_types ); |
|
| 106 | + $this->sql .= " AND p.post_type IN ( '" . implode( "', '", $value ) . "' )"; |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + private function limit() { |
|
| 110 | + $value = is_numeric( $this->limit ) ? $this->limit : 10; |
|
| 111 | + $this->sql .= ' LIMIT ' . esc_sql( $value ); |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + private function has_match() { |
|
| 115 | + if ( ! $this->request->has_param( 'has_match' ) ) { |
|
| 116 | + return; |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + $value = (bool) $this->request->get_param( 'has_match' ); |
|
| 120 | + |
|
| 121 | + if ( $value ) { |
|
| 122 | + $this->sql .= ' AND e.about_jsonld IS NOT NULL'; |
|
| 123 | + } else { |
|
| 124 | + $this->sql .= ' AND e.about_jsonld IS NULL'; |
|
| 125 | + } |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + private function sort() { |
|
| 129 | + switch ( $this->direction . '$' . $this->sort ) { |
|
| 130 | + case 'ASCENDING$ASC': |
|
| 131 | + case 'DESCENDING$DESC': |
|
| 132 | + $sort = 'ASC'; |
|
| 133 | + break; |
|
| 134 | + case 'ASCENDING$DESC': |
|
| 135 | + case 'DESCENDING$ASC': |
|
| 136 | + $sort = 'DESC'; |
|
| 137 | + break; |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + $this->sql .= ' ORDER BY p.' . $this->sortby . ' ' . $sort; |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + private function post_status() { |
|
| 144 | + if ( ! $this->request->has_param( 'post_status' ) ) { |
|
| 145 | + $this->sql .= " AND p.post_status IN ( 'draft', 'publish' ) "; |
|
| 146 | + |
|
| 147 | + return; |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + global $wpdb; |
|
| 151 | + $value = $this->request->get_param( 'post_status' ); |
|
| 152 | + $this->sql .= $wpdb->prepare( ' AND p.post_status = %s', $value ); |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + private function cursor() { |
|
| 156 | + if ( ! isset( $this->position ) ) { |
|
| 157 | + return; |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + switch ( $this->direction . '$' . $this->sort ) { |
|
| 161 | + case 'ASCENDING$ASC': |
|
| 162 | + case 'DESCENDING$DESC': |
|
| 163 | + $condition = '>'; |
|
| 164 | + break; |
|
| 165 | + case 'ASCENDING$DESC': |
|
| 166 | + case 'DESCENDING$ASC': |
|
| 167 | + $condition = '<'; |
|
| 168 | + break; |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + $condition .= ( $this->element === 'INCLUDED' ? '=' : '' ); |
|
| 172 | + global $wpdb; |
|
| 173 | + // We control the vars in this method. |
|
| 174 | + // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
|
| 175 | + $this->sql .= $wpdb->prepare( ' AND p.' . esc_sql( $this->sortby ) . ' ' . $condition . ' %s', $this->position ); |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + private function set_sort() { |
|
| 179 | + $sortby_to_col = array( |
|
| 180 | + 'date_modified_gmt' => 'post_modified_gmt', |
|
| 181 | + ); |
|
| 182 | + |
|
| 183 | + $value = $this->request->has_param( 'sort' ) |
|
| 184 | + ? $this->request->get_param( 'sort' ) |
|
| 185 | + : '-date_modified_gmt'; |
|
| 186 | + |
|
| 187 | + $sortby = substr( $value, 1 ); |
|
| 188 | + $this->sortby = isset( $sortby_to_col[ $sortby ] ) ? $sortby_to_col[ $sortby ] : $sortby; |
|
| 189 | + $this->sort = substr( $value, 0, 1 ) === '+' ? 'ASC' : 'DESC'; |
|
| 190 | + } |
|
| 191 | 191 | |
| 192 | 192 | } |
@@ -37,7 +37,7 @@ discard block |
||
| 37 | 37 | * @param WP_REST_Request $request |
| 38 | 38 | * @param Cursor $cursor |
| 39 | 39 | */ |
| 40 | - public function __construct( $request, $cursor, $cursor_sort, $limit ) { |
|
| 40 | + public function __construct($request, $cursor, $cursor_sort, $limit) { |
|
| 41 | 41 | global $wpdb; |
| 42 | 42 | |
| 43 | 43 | $this->request = $request; |
@@ -81,44 +81,44 @@ discard block |
||
| 81 | 81 | |
| 82 | 82 | // The `sql` is prepared in each delegated function in this class. |
| 83 | 83 | // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 84 | - $items = $wpdb->get_results( $this->sql ); |
|
| 84 | + $items = $wpdb->get_results($this->sql); |
|
| 85 | 85 | |
| 86 | - $sort = ( $this->sort === 'ASC' ? SORT_ASC : SORT_DESC ); |
|
| 87 | - array_multisort( array_column( $items, $this->cursor_sort->get_sort_property() ), $sort, $items ); |
|
| 88 | - $items = array_map( array( $this, 'map_item' ), $items ); |
|
| 86 | + $sort = ($this->sort === 'ASC' ? SORT_ASC : SORT_DESC); |
|
| 87 | + array_multisort(array_column($items, $this->cursor_sort->get_sort_property()), $sort, $items); |
|
| 88 | + $items = array_map(array($this, 'map_item'), $items); |
|
| 89 | 89 | |
| 90 | 90 | return $items; |
| 91 | 91 | } |
| 92 | 92 | |
| 93 | - public function map_item( $item ) { |
|
| 94 | - $item->post_link = get_edit_post_link( $item->id, 'ui' ); |
|
| 95 | - $item->view_link = get_permalink( $item->id ); |
|
| 96 | - $item->preview_link = get_preview_post_link( $item->id ); |
|
| 93 | + public function map_item($item) { |
|
| 94 | + $item->post_link = get_edit_post_link($item->id, 'ui'); |
|
| 95 | + $item->view_link = get_permalink($item->id); |
|
| 96 | + $item->preview_link = get_preview_post_link($item->id); |
|
| 97 | 97 | |
| 98 | 98 | return $item; |
| 99 | 99 | } |
| 100 | 100 | |
| 101 | 101 | private function post_types() { |
| 102 | - $post_types = $this->request->has_param( 'post_types' ) |
|
| 103 | - ? (array) $this->request->get_param( 'post_types' ) |
|
| 104 | - : array( 'post', 'page' ); |
|
| 105 | - $value = array_map( 'esc_sql', $post_types ); |
|
| 106 | - $this->sql .= " AND p.post_type IN ( '" . implode( "', '", $value ) . "' )"; |
|
| 102 | + $post_types = $this->request->has_param('post_types') |
|
| 103 | + ? (array) $this->request->get_param('post_types') |
|
| 104 | + : array('post', 'page'); |
|
| 105 | + $value = array_map('esc_sql', $post_types); |
|
| 106 | + $this->sql .= " AND p.post_type IN ( '".implode("', '", $value)."' )"; |
|
| 107 | 107 | } |
| 108 | 108 | |
| 109 | 109 | private function limit() { |
| 110 | - $value = is_numeric( $this->limit ) ? $this->limit : 10; |
|
| 111 | - $this->sql .= ' LIMIT ' . esc_sql( $value ); |
|
| 110 | + $value = is_numeric($this->limit) ? $this->limit : 10; |
|
| 111 | + $this->sql .= ' LIMIT '.esc_sql($value); |
|
| 112 | 112 | } |
| 113 | 113 | |
| 114 | 114 | private function has_match() { |
| 115 | - if ( ! $this->request->has_param( 'has_match' ) ) { |
|
| 115 | + if ( ! $this->request->has_param('has_match')) { |
|
| 116 | 116 | return; |
| 117 | 117 | } |
| 118 | 118 | |
| 119 | - $value = (bool) $this->request->get_param( 'has_match' ); |
|
| 119 | + $value = (bool) $this->request->get_param('has_match'); |
|
| 120 | 120 | |
| 121 | - if ( $value ) { |
|
| 121 | + if ($value) { |
|
| 122 | 122 | $this->sql .= ' AND e.about_jsonld IS NOT NULL'; |
| 123 | 123 | } else { |
| 124 | 124 | $this->sql .= ' AND e.about_jsonld IS NULL'; |
@@ -126,7 +126,7 @@ discard block |
||
| 126 | 126 | } |
| 127 | 127 | |
| 128 | 128 | private function sort() { |
| 129 | - switch ( $this->direction . '$' . $this->sort ) { |
|
| 129 | + switch ($this->direction.'$'.$this->sort) { |
|
| 130 | 130 | case 'ASCENDING$ASC': |
| 131 | 131 | case 'DESCENDING$DESC': |
| 132 | 132 | $sort = 'ASC'; |
@@ -137,27 +137,27 @@ discard block |
||
| 137 | 137 | break; |
| 138 | 138 | } |
| 139 | 139 | |
| 140 | - $this->sql .= ' ORDER BY p.' . $this->sortby . ' ' . $sort; |
|
| 140 | + $this->sql .= ' ORDER BY p.'.$this->sortby.' '.$sort; |
|
| 141 | 141 | } |
| 142 | 142 | |
| 143 | 143 | private function post_status() { |
| 144 | - if ( ! $this->request->has_param( 'post_status' ) ) { |
|
| 144 | + if ( ! $this->request->has_param('post_status')) { |
|
| 145 | 145 | $this->sql .= " AND p.post_status IN ( 'draft', 'publish' ) "; |
| 146 | 146 | |
| 147 | 147 | return; |
| 148 | 148 | } |
| 149 | 149 | |
| 150 | 150 | global $wpdb; |
| 151 | - $value = $this->request->get_param( 'post_status' ); |
|
| 152 | - $this->sql .= $wpdb->prepare( ' AND p.post_status = %s', $value ); |
|
| 151 | + $value = $this->request->get_param('post_status'); |
|
| 152 | + $this->sql .= $wpdb->prepare(' AND p.post_status = %s', $value); |
|
| 153 | 153 | } |
| 154 | 154 | |
| 155 | 155 | private function cursor() { |
| 156 | - if ( ! isset( $this->position ) ) { |
|
| 156 | + if ( ! isset($this->position)) { |
|
| 157 | 157 | return; |
| 158 | 158 | } |
| 159 | 159 | |
| 160 | - switch ( $this->direction . '$' . $this->sort ) { |
|
| 160 | + switch ($this->direction.'$'.$this->sort) { |
|
| 161 | 161 | case 'ASCENDING$ASC': |
| 162 | 162 | case 'DESCENDING$DESC': |
| 163 | 163 | $condition = '>'; |
@@ -168,11 +168,11 @@ discard block |
||
| 168 | 168 | break; |
| 169 | 169 | } |
| 170 | 170 | |
| 171 | - $condition .= ( $this->element === 'INCLUDED' ? '=' : '' ); |
|
| 171 | + $condition .= ($this->element === 'INCLUDED' ? '=' : ''); |
|
| 172 | 172 | global $wpdb; |
| 173 | 173 | // We control the vars in this method. |
| 174 | 174 | // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 175 | - $this->sql .= $wpdb->prepare( ' AND p.' . esc_sql( $this->sortby ) . ' ' . $condition . ' %s', $this->position ); |
|
| 175 | + $this->sql .= $wpdb->prepare(' AND p.'.esc_sql($this->sortby).' '.$condition.' %s', $this->position); |
|
| 176 | 176 | } |
| 177 | 177 | |
| 178 | 178 | private function set_sort() { |
@@ -180,13 +180,13 @@ discard block |
||
| 180 | 180 | 'date_modified_gmt' => 'post_modified_gmt', |
| 181 | 181 | ); |
| 182 | 182 | |
| 183 | - $value = $this->request->has_param( 'sort' ) |
|
| 184 | - ? $this->request->get_param( 'sort' ) |
|
| 183 | + $value = $this->request->has_param('sort') |
|
| 184 | + ? $this->request->get_param('sort') |
|
| 185 | 185 | : '-date_modified_gmt'; |
| 186 | 186 | |
| 187 | - $sortby = substr( $value, 1 ); |
|
| 188 | - $this->sortby = isset( $sortby_to_col[ $sortby ] ) ? $sortby_to_col[ $sortby ] : $sortby; |
|
| 189 | - $this->sort = substr( $value, 0, 1 ) === '+' ? 'ASC' : 'DESC'; |
|
| 187 | + $sortby = substr($value, 1); |
|
| 188 | + $this->sortby = isset($sortby_to_col[$sortby]) ? $sortby_to_col[$sortby] : $sortby; |
|
| 189 | + $this->sort = substr($value, 0, 1) === '+' ? 'ASC' : 'DESC'; |
|
| 190 | 190 | } |
| 191 | 191 | |
| 192 | 192 | } |
@@ -17,251 +17,251 @@ |
||
| 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; |
|
| 20 | + /** |
|
| 21 | + * @var Post_Entity_Match_Service |
|
| 22 | + */ |
|
| 23 | + private $match_service; |
|
| 24 | 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 | - } |
|
| 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 | 33 | |
| 34 | - /** |
|
| 35 | - * Register hooks. |
|
| 36 | - */ |
|
| 37 | - public function register_hooks() { |
|
| 38 | - add_action( 'rest_api_init', array( $this, 'register_routes' ) ); |
|
| 39 | - } |
|
| 34 | + /** |
|
| 35 | + * Register hooks. |
|
| 36 | + */ |
|
| 37 | + public function register_hooks() { |
|
| 38 | + add_action( 'rest_api_init', array( $this, 'register_routes' ) ); |
|
| 39 | + } |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * Register the routes for the objects of the controller. |
|
| 43 | - */ |
|
| 44 | - public function register_routes() { |
|
| 41 | + /** |
|
| 42 | + * Register the routes for the objects of the controller. |
|
| 43 | + */ |
|
| 44 | + public function register_routes() { |
|
| 45 | 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 | - ); |
|
| 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 | 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 | - ); |
|
| 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 | 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 () { |
|
| 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 | 137 | |
| 138 | - return current_user_can( 'manage_options' ); |
|
| 139 | - }, |
|
| 140 | - ) |
|
| 141 | - ); |
|
| 142 | - } |
|
| 138 | + return current_user_can( 'manage_options' ); |
|
| 139 | + }, |
|
| 140 | + ) |
|
| 141 | + ); |
|
| 142 | + } |
|
| 143 | 143 | |
| 144 | - /** |
|
| 145 | - * @param \WP_REST_Request $request |
|
| 146 | - * |
|
| 147 | - * @return array|object|\stdClass[]|null |
|
| 148 | - * @throws Exception when some of the parameters are not in the accepted format. |
|
| 149 | - */ |
|
| 150 | - public function get_post_matches( $request ) { |
|
| 151 | - $limit = $request->has_param( 'limit' ) ? $request->get_param( 'limit' ) : 10; |
|
| 152 | - $cursor = $request->has_param( 'cursor' ) |
|
| 153 | - ? Cursor::from_base64_string( $request->get_param( 'cursor' ) ) |
|
| 154 | - : Cursor::empty_cursor(); |
|
| 155 | - $cursor_sort = new Cursor_Sort( |
|
| 156 | - $request->has_param( 'sort' ) ? $request->get_param( 'sort' ) : '-date_modified_gmt' |
|
| 157 | - ); |
|
| 144 | + /** |
|
| 145 | + * @param \WP_REST_Request $request |
|
| 146 | + * |
|
| 147 | + * @return array|object|\stdClass[]|null |
|
| 148 | + * @throws Exception when some of the parameters are not in the accepted format. |
|
| 149 | + */ |
|
| 150 | + public function get_post_matches( $request ) { |
|
| 151 | + $limit = $request->has_param( 'limit' ) ? $request->get_param( 'limit' ) : 10; |
|
| 152 | + $cursor = $request->has_param( 'cursor' ) |
|
| 153 | + ? Cursor::from_base64_string( $request->get_param( 'cursor' ) ) |
|
| 154 | + : Cursor::empty_cursor(); |
|
| 155 | + $cursor_sort = new Cursor_Sort( |
|
| 156 | + $request->has_param( 'sort' ) ? $request->get_param( 'sort' ) : '-date_modified_gmt' |
|
| 157 | + ); |
|
| 158 | 158 | |
| 159 | - // If we're looking for recipes, we need the Recipe_Query. |
|
| 160 | - if ( in_array( 'wprm_recipe', (array) $request->get_param( 'post_types' ), true ) ) { |
|
| 161 | - $query = new Recipe_Query( $request, $cursor, $cursor_sort, $limit + 1 ); |
|
| 162 | - } else { |
|
| 163 | - $query = new Post_Query( $request, $cursor, $cursor_sort, $limit + 1 ); |
|
| 164 | - } |
|
| 159 | + // If we're looking for recipes, we need the Recipe_Query. |
|
| 160 | + if ( in_array( 'wprm_recipe', (array) $request->get_param( 'post_types' ), true ) ) { |
|
| 161 | + $query = new Recipe_Query( $request, $cursor, $cursor_sort, $limit + 1 ); |
|
| 162 | + } else { |
|
| 163 | + $query = new Post_Query( $request, $cursor, $cursor_sort, $limit + 1 ); |
|
| 164 | + } |
|
| 165 | 165 | |
| 166 | - $results = $query->get_results(); |
|
| 167 | - $items = $cursor->get_direction() === 'ASCENDING' |
|
| 168 | - ? array_slice( $results, 0, min( count( $results ), $limit ) ) |
|
| 169 | - : array_slice( $results, count( $results ) > $limit ? 1 : 0 ); |
|
| 170 | - $position = current( $items )->{$cursor_sort->get_sort_property()}; |
|
| 171 | - $next_position = end( $items )->{$cursor_sort->get_sort_property()}; |
|
| 166 | + $results = $query->get_results(); |
|
| 167 | + $items = $cursor->get_direction() === 'ASCENDING' |
|
| 168 | + ? array_slice( $results, 0, min( count( $results ), $limit ) ) |
|
| 169 | + : array_slice( $results, count( $results ) > $limit ? 1 : 0 ); |
|
| 170 | + $position = current( $items )->{$cursor_sort->get_sort_property()}; |
|
| 171 | + $next_position = end( $items )->{$cursor_sort->get_sort_property()}; |
|
| 172 | 172 | |
| 173 | - $self = $cursor; |
|
| 174 | - $first = new Cursor( null, 'INCLUDED', 'ASCENDING' ); |
|
| 175 | - $last = new Cursor( null, 'INCLUDED', 'DESCENDING' ); |
|
| 173 | + $self = $cursor; |
|
| 174 | + $first = new Cursor( null, 'INCLUDED', 'ASCENDING' ); |
|
| 175 | + $last = new Cursor( null, 'INCLUDED', 'DESCENDING' ); |
|
| 176 | 176 | |
| 177 | - return new Page( |
|
| 178 | - $items, |
|
| 179 | - $self->to_base64_string(), |
|
| 180 | - $first->to_base64_string(), |
|
| 181 | - $this->get_prev_cursor_as_base64_string( $cursor, $results, $limit, $position ), |
|
| 182 | - $this->get_next_cursor_as_base64_string( $cursor, $results, $limit, $next_position ), |
|
| 183 | - $last->to_base64_string() |
|
| 184 | - ); |
|
| 185 | - } |
|
| 177 | + return new Page( |
|
| 178 | + $items, |
|
| 179 | + $self->to_base64_string(), |
|
| 180 | + $first->to_base64_string(), |
|
| 181 | + $this->get_prev_cursor_as_base64_string( $cursor, $results, $limit, $position ), |
|
| 182 | + $this->get_next_cursor_as_base64_string( $cursor, $results, $limit, $next_position ), |
|
| 183 | + $last->to_base64_string() |
|
| 184 | + ); |
|
| 185 | + } |
|
| 186 | 186 | |
| 187 | - /** |
|
| 188 | - * @param Cursor $cursor |
|
| 189 | - * |
|
| 190 | - * @return string|null |
|
| 191 | - * @throws Exception when one of the called functions throws an exception. |
|
| 192 | - */ |
|
| 193 | - private function get_prev_cursor_as_base64_string( $cursor, $results, $limit, $position ) { |
|
| 194 | - if ( ( $cursor->get_direction() === 'ASCENDING' && $cursor->get_position() !== null ) || ( $cursor->get_direction() === 'DESCENDING' && count( $results ) > $limit ) ) { |
|
| 195 | - $cursor = new Cursor( $position, 'EXCLUDED', 'DESCENDING' ); |
|
| 187 | + /** |
|
| 188 | + * @param Cursor $cursor |
|
| 189 | + * |
|
| 190 | + * @return string|null |
|
| 191 | + * @throws Exception when one of the called functions throws an exception. |
|
| 192 | + */ |
|
| 193 | + private function get_prev_cursor_as_base64_string( $cursor, $results, $limit, $position ) { |
|
| 194 | + if ( ( $cursor->get_direction() === 'ASCENDING' && $cursor->get_position() !== null ) || ( $cursor->get_direction() === 'DESCENDING' && count( $results ) > $limit ) ) { |
|
| 195 | + $cursor = new Cursor( $position, 'EXCLUDED', 'DESCENDING' ); |
|
| 196 | 196 | |
| 197 | - return $cursor->to_base64_string(); |
|
| 198 | - } |
|
| 197 | + return $cursor->to_base64_string(); |
|
| 198 | + } |
|
| 199 | 199 | |
| 200 | - return null; |
|
| 201 | - } |
|
| 200 | + return null; |
|
| 201 | + } |
|
| 202 | 202 | |
| 203 | - /** |
|
| 204 | - * @param Cursor $cursor |
|
| 205 | - * |
|
| 206 | - * @return string|null |
|
| 207 | - * @throws Exception when one of the called functions throws an exception. |
|
| 208 | - */ |
|
| 209 | - private function get_next_cursor_as_base64_string( $cursor, $results, $limit, $position ) { |
|
| 210 | - if ( ( $cursor->get_direction() === 'DESCENDING' && $cursor->get_position() !== null ) || ( $cursor->get_direction() === 'ASCENDING' && count( $results ) > $limit ) ) { |
|
| 211 | - $cursor = new Cursor( $position, 'EXCLUDED', 'ASCENDING' ); |
|
| 203 | + /** |
|
| 204 | + * @param Cursor $cursor |
|
| 205 | + * |
|
| 206 | + * @return string|null |
|
| 207 | + * @throws Exception when one of the called functions throws an exception. |
|
| 208 | + */ |
|
| 209 | + private function get_next_cursor_as_base64_string( $cursor, $results, $limit, $position ) { |
|
| 210 | + if ( ( $cursor->get_direction() === 'DESCENDING' && $cursor->get_position() !== null ) || ( $cursor->get_direction() === 'ASCENDING' && count( $results ) > $limit ) ) { |
|
| 211 | + $cursor = new Cursor( $position, 'EXCLUDED', 'ASCENDING' ); |
|
| 212 | 212 | |
| 213 | - return $cursor->to_base64_string(); |
|
| 214 | - } |
|
| 213 | + return $cursor->to_base64_string(); |
|
| 214 | + } |
|
| 215 | 215 | |
| 216 | - return null; |
|
| 217 | - } |
|
| 216 | + return null; |
|
| 217 | + } |
|
| 218 | 218 | |
| 219 | - /** |
|
| 220 | - * Create a new match for a post. |
|
| 221 | - * |
|
| 222 | - * @param $request \WP_REST_Request |
|
| 223 | - * |
|
| 224 | - * @throws Exception If there was a problem creating the match. |
|
| 225 | - */ |
|
| 226 | - public function create_post_match( $request ) { |
|
| 227 | - $post_id = $request->get_param( 'post_id' ); |
|
| 219 | + /** |
|
| 220 | + * Create a new match for a post. |
|
| 221 | + * |
|
| 222 | + * @param $request \WP_REST_Request |
|
| 223 | + * |
|
| 224 | + * @throws Exception If there was a problem creating the match. |
|
| 225 | + */ |
|
| 226 | + public function create_post_match( $request ) { |
|
| 227 | + $post_id = $request->get_param( 'post_id' ); |
|
| 228 | 228 | |
| 229 | - // If we dont have a entry on the match table, then add one. |
|
| 230 | - $content_id = Wordpress_Content_Id::create_post( $post_id ); |
|
| 231 | - if ( ! Wordpress_Content_Service::get_instance()->get_entity_id( $content_id ) ) { |
|
| 232 | - $uri = Entity_Uri_Generator::create_uri( $content_id->get_type(), $content_id->get_id() ); |
|
| 233 | - Wordpress_Content_Service::get_instance()->set_entity_id( $content_id, $uri ); |
|
| 234 | - } |
|
| 229 | + // If we dont have a entry on the match table, then add one. |
|
| 230 | + $content_id = Wordpress_Content_Id::create_post( $post_id ); |
|
| 231 | + if ( ! Wordpress_Content_Service::get_instance()->get_entity_id( $content_id ) ) { |
|
| 232 | + $uri = Entity_Uri_Generator::create_uri( $content_id->get_type(), $content_id->get_id() ); |
|
| 233 | + Wordpress_Content_Service::get_instance()->set_entity_id( $content_id, $uri ); |
|
| 234 | + } |
|
| 235 | 235 | |
| 236 | - $match_id = Wordpress_Dataset_Content_Service_Hooks::get_id_or_create( |
|
| 237 | - $post_id, |
|
| 238 | - Object_Type_Enum::POST |
|
| 239 | - ); |
|
| 236 | + $match_id = Wordpress_Dataset_Content_Service_Hooks::get_id_or_create( |
|
| 237 | + $post_id, |
|
| 238 | + Object_Type_Enum::POST |
|
| 239 | + ); |
|
| 240 | 240 | |
| 241 | - return $this->match_service->set_jsonld( |
|
| 242 | - $post_id, |
|
| 243 | - Object_Type_Enum::POST, |
|
| 244 | - $match_id, |
|
| 245 | - $request->get_json_params() |
|
| 246 | - ); |
|
| 247 | - } |
|
| 241 | + return $this->match_service->set_jsonld( |
|
| 242 | + $post_id, |
|
| 243 | + Object_Type_Enum::POST, |
|
| 244 | + $match_id, |
|
| 245 | + $request->get_json_params() |
|
| 246 | + ); |
|
| 247 | + } |
|
| 248 | 248 | |
| 249 | - /** |
|
| 250 | - * Update post match. |
|
| 251 | - * |
|
| 252 | - * @param $request \WP_REST_Request |
|
| 253 | - * |
|
| 254 | - * @return Match_Entry |
|
| 255 | - * |
|
| 256 | - * @throws Exception If there was a problem updating the match. |
|
| 257 | - */ |
|
| 258 | - public function update_post_match( $request ) { |
|
| 249 | + /** |
|
| 250 | + * Update post match. |
|
| 251 | + * |
|
| 252 | + * @param $request \WP_REST_Request |
|
| 253 | + * |
|
| 254 | + * @return Match_Entry |
|
| 255 | + * |
|
| 256 | + * @throws Exception If there was a problem updating the match. |
|
| 257 | + */ |
|
| 258 | + public function update_post_match( $request ) { |
|
| 259 | 259 | |
| 260 | - return $this->match_service->set_jsonld( |
|
| 261 | - $request->get_param( 'post_id' ), |
|
| 262 | - Object_Type_Enum::POST, |
|
| 263 | - $request->get_param( 'match_id' ), |
|
| 264 | - $request->get_json_params() |
|
| 265 | - ); |
|
| 266 | - } |
|
| 260 | + return $this->match_service->set_jsonld( |
|
| 261 | + $request->get_param( 'post_id' ), |
|
| 262 | + Object_Type_Enum::POST, |
|
| 263 | + $request->get_param( 'match_id' ), |
|
| 264 | + $request->get_json_params() |
|
| 265 | + ); |
|
| 266 | + } |
|
| 267 | 267 | } |
@@ -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,7 +49,7 @@ 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', |
@@ -77,19 +77,19 @@ discard block |
||
| 77 | 77 | 'post_status' => array( |
| 78 | 78 | 'type' => 'string', |
| 79 | 79 | 'required' => false, |
| 80 | - 'enum' => array( 'publish', 'draft', 'all' ), |
|
| 80 | + 'enum' => array('publish', 'draft', 'all'), |
|
| 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,7 +100,7 @@ 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, |
@@ -108,8 +108,8 @@ discard block |
||
| 108 | 108 | 'type' => 'integer', |
| 109 | 109 | ), |
| 110 | 110 | ), |
| 111 | - 'permission_callback' => function () { |
|
| 112 | - return current_user_can( 'manage_options' ); |
|
| 111 | + 'permission_callback' => function() { |
|
| 112 | + return current_user_can('manage_options'); |
|
| 113 | 113 | }, |
| 114 | 114 | ) |
| 115 | 115 | ); |
@@ -120,7 +120,7 @@ discard block |
||
| 120 | 120 | '/post-matches/(?P<post_id>\d+)/matches/(?P<match_id>\d+)', |
| 121 | 121 | array( |
| 122 | 122 | 'methods' => 'PUT', |
| 123 | - 'callback' => array( $this, 'update_post_match' ), |
|
| 123 | + 'callback' => array($this, 'update_post_match'), |
|
| 124 | 124 | 'args' => array( |
| 125 | 125 | 'post_id' => array( |
| 126 | 126 | 'required' => true, |
@@ -133,9 +133,9 @@ discard block |
||
| 133 | 133 | 'type' => 'integer', |
| 134 | 134 | ), |
| 135 | 135 | ), |
| 136 | - 'permission_callback' => function () { |
|
| 136 | + 'permission_callback' => function() { |
|
| 137 | 137 | |
| 138 | - return current_user_can( 'manage_options' ); |
|
| 138 | + return current_user_can('manage_options'); |
|
| 139 | 139 | }, |
| 140 | 140 | ) |
| 141 | 141 | ); |
@@ -147,39 +147,39 @@ discard block |
||
| 147 | 147 | * @return array|object|\stdClass[]|null |
| 148 | 148 | * @throws Exception when some of the parameters are not in the accepted format. |
| 149 | 149 | */ |
| 150 | - public function get_post_matches( $request ) { |
|
| 151 | - $limit = $request->has_param( 'limit' ) ? $request->get_param( 'limit' ) : 10; |
|
| 152 | - $cursor = $request->has_param( 'cursor' ) |
|
| 153 | - ? Cursor::from_base64_string( $request->get_param( 'cursor' ) ) |
|
| 150 | + public function get_post_matches($request) { |
|
| 151 | + $limit = $request->has_param('limit') ? $request->get_param('limit') : 10; |
|
| 152 | + $cursor = $request->has_param('cursor') |
|
| 153 | + ? Cursor::from_base64_string($request->get_param('cursor')) |
|
| 154 | 154 | : Cursor::empty_cursor(); |
| 155 | 155 | $cursor_sort = new Cursor_Sort( |
| 156 | - $request->has_param( 'sort' ) ? $request->get_param( 'sort' ) : '-date_modified_gmt' |
|
| 156 | + $request->has_param('sort') ? $request->get_param('sort') : '-date_modified_gmt' |
|
| 157 | 157 | ); |
| 158 | 158 | |
| 159 | 159 | // If we're looking for recipes, we need the Recipe_Query. |
| 160 | - if ( in_array( 'wprm_recipe', (array) $request->get_param( 'post_types' ), true ) ) { |
|
| 161 | - $query = new Recipe_Query( $request, $cursor, $cursor_sort, $limit + 1 ); |
|
| 160 | + if (in_array('wprm_recipe', (array) $request->get_param('post_types'), true)) { |
|
| 161 | + $query = new Recipe_Query($request, $cursor, $cursor_sort, $limit + 1); |
|
| 162 | 162 | } else { |
| 163 | - $query = new Post_Query( $request, $cursor, $cursor_sort, $limit + 1 ); |
|
| 163 | + $query = new Post_Query($request, $cursor, $cursor_sort, $limit + 1); |
|
| 164 | 164 | } |
| 165 | 165 | |
| 166 | 166 | $results = $query->get_results(); |
| 167 | 167 | $items = $cursor->get_direction() === 'ASCENDING' |
| 168 | - ? array_slice( $results, 0, min( count( $results ), $limit ) ) |
|
| 169 | - : array_slice( $results, count( $results ) > $limit ? 1 : 0 ); |
|
| 170 | - $position = current( $items )->{$cursor_sort->get_sort_property()}; |
|
| 171 | - $next_position = end( $items )->{$cursor_sort->get_sort_property()}; |
|
| 168 | + ? array_slice($results, 0, min(count($results), $limit)) |
|
| 169 | + : array_slice($results, count($results) > $limit ? 1 : 0); |
|
| 170 | + $position = current($items)->{$cursor_sort->get_sort_property()}; |
|
| 171 | + $next_position = end($items)->{$cursor_sort->get_sort_property()}; |
|
| 172 | 172 | |
| 173 | 173 | $self = $cursor; |
| 174 | - $first = new Cursor( null, 'INCLUDED', 'ASCENDING' ); |
|
| 175 | - $last = new Cursor( null, 'INCLUDED', 'DESCENDING' ); |
|
| 174 | + $first = new Cursor(null, 'INCLUDED', 'ASCENDING'); |
|
| 175 | + $last = new Cursor(null, 'INCLUDED', 'DESCENDING'); |
|
| 176 | 176 | |
| 177 | 177 | return new Page( |
| 178 | 178 | $items, |
| 179 | 179 | $self->to_base64_string(), |
| 180 | 180 | $first->to_base64_string(), |
| 181 | - $this->get_prev_cursor_as_base64_string( $cursor, $results, $limit, $position ), |
|
| 182 | - $this->get_next_cursor_as_base64_string( $cursor, $results, $limit, $next_position ), |
|
| 181 | + $this->get_prev_cursor_as_base64_string($cursor, $results, $limit, $position), |
|
| 182 | + $this->get_next_cursor_as_base64_string($cursor, $results, $limit, $next_position), |
|
| 183 | 183 | $last->to_base64_string() |
| 184 | 184 | ); |
| 185 | 185 | } |
@@ -190,9 +190,9 @@ discard block |
||
| 190 | 190 | * @return string|null |
| 191 | 191 | * @throws Exception when one of the called functions throws an exception. |
| 192 | 192 | */ |
| 193 | - private function get_prev_cursor_as_base64_string( $cursor, $results, $limit, $position ) { |
|
| 194 | - if ( ( $cursor->get_direction() === 'ASCENDING' && $cursor->get_position() !== null ) || ( $cursor->get_direction() === 'DESCENDING' && count( $results ) > $limit ) ) { |
|
| 195 | - $cursor = new Cursor( $position, 'EXCLUDED', 'DESCENDING' ); |
|
| 193 | + private function get_prev_cursor_as_base64_string($cursor, $results, $limit, $position) { |
|
| 194 | + if (($cursor->get_direction() === 'ASCENDING' && $cursor->get_position() !== null) || ($cursor->get_direction() === 'DESCENDING' && count($results) > $limit)) { |
|
| 195 | + $cursor = new Cursor($position, 'EXCLUDED', 'DESCENDING'); |
|
| 196 | 196 | |
| 197 | 197 | return $cursor->to_base64_string(); |
| 198 | 198 | } |
@@ -206,9 +206,9 @@ discard block |
||
| 206 | 206 | * @return string|null |
| 207 | 207 | * @throws Exception when one of the called functions throws an exception. |
| 208 | 208 | */ |
| 209 | - private function get_next_cursor_as_base64_string( $cursor, $results, $limit, $position ) { |
|
| 210 | - if ( ( $cursor->get_direction() === 'DESCENDING' && $cursor->get_position() !== null ) || ( $cursor->get_direction() === 'ASCENDING' && count( $results ) > $limit ) ) { |
|
| 211 | - $cursor = new Cursor( $position, 'EXCLUDED', 'ASCENDING' ); |
|
| 209 | + private function get_next_cursor_as_base64_string($cursor, $results, $limit, $position) { |
|
| 210 | + if (($cursor->get_direction() === 'DESCENDING' && $cursor->get_position() !== null) || ($cursor->get_direction() === 'ASCENDING' && count($results) > $limit)) { |
|
| 211 | + $cursor = new Cursor($position, 'EXCLUDED', 'ASCENDING'); |
|
| 212 | 212 | |
| 213 | 213 | return $cursor->to_base64_string(); |
| 214 | 214 | } |
@@ -223,14 +223,14 @@ discard block |
||
| 223 | 223 | * |
| 224 | 224 | * @throws Exception If there was a problem creating the match. |
| 225 | 225 | */ |
| 226 | - public function create_post_match( $request ) { |
|
| 227 | - $post_id = $request->get_param( 'post_id' ); |
|
| 226 | + public function create_post_match($request) { |
|
| 227 | + $post_id = $request->get_param('post_id'); |
|
| 228 | 228 | |
| 229 | 229 | // If we dont have a entry on the match table, then add one. |
| 230 | - $content_id = Wordpress_Content_Id::create_post( $post_id ); |
|
| 231 | - if ( ! Wordpress_Content_Service::get_instance()->get_entity_id( $content_id ) ) { |
|
| 232 | - $uri = Entity_Uri_Generator::create_uri( $content_id->get_type(), $content_id->get_id() ); |
|
| 233 | - Wordpress_Content_Service::get_instance()->set_entity_id( $content_id, $uri ); |
|
| 230 | + $content_id = Wordpress_Content_Id::create_post($post_id); |
|
| 231 | + if ( ! Wordpress_Content_Service::get_instance()->get_entity_id($content_id)) { |
|
| 232 | + $uri = Entity_Uri_Generator::create_uri($content_id->get_type(), $content_id->get_id()); |
|
| 233 | + Wordpress_Content_Service::get_instance()->set_entity_id($content_id, $uri); |
|
| 234 | 234 | } |
| 235 | 235 | |
| 236 | 236 | $match_id = Wordpress_Dataset_Content_Service_Hooks::get_id_or_create( |
@@ -255,12 +255,12 @@ discard block |
||
| 255 | 255 | * |
| 256 | 256 | * @throws Exception If there was a problem updating the match. |
| 257 | 257 | */ |
| 258 | - public function update_post_match( $request ) { |
|
| 258 | + public function update_post_match($request) { |
|
| 259 | 259 | |
| 260 | 260 | return $this->match_service->set_jsonld( |
| 261 | - $request->get_param( 'post_id' ), |
|
| 261 | + $request->get_param('post_id'), |
|
| 262 | 262 | Object_Type_Enum::POST, |
| 263 | - $request->get_param( 'match_id' ), |
|
| 263 | + $request->get_param('match_id'), |
|
| 264 | 264 | $request->get_json_params() |
| 265 | 265 | ); |
| 266 | 266 | } |
@@ -8,33 +8,33 @@ discard block |
||
| 8 | 8 | |
| 9 | 9 | class Food_Kg_Recipe_Stats { |
| 10 | 10 | |
| 11 | - public function register_hooks() { |
|
| 12 | - add_filter( 'wl_dashboard__stats__settings', array( $this, 'dashboard__stats__settings' ) ); |
|
| 13 | - } |
|
| 11 | + public function register_hooks() { |
|
| 12 | + add_filter( 'wl_dashboard__stats__settings', array( $this, 'dashboard__stats__settings' ) ); |
|
| 13 | + } |
|
| 14 | 14 | |
| 15 | - public function dashboard__stats__settings( $arr ) { |
|
| 15 | + public function dashboard__stats__settings( $arr ) { |
|
| 16 | 16 | |
| 17 | - $data = Recipe_Query::get_data(); // $this->get_data(); |
|
| 17 | + $data = Recipe_Query::get_data(); // $this->get_data(); |
|
| 18 | 18 | |
| 19 | - $arr[] = new Stats_Settings( |
|
| 20 | - __( 'Boosted Recipes are the ones WordLift matched with KG. Some Explanation how it helps them.', 'wordlift' ), |
|
| 21 | - __( 'Lifted Recipes', 'wordlift' ), |
|
| 22 | - __( 'Recipes', 'wordlift' ), |
|
| 23 | - '#00c48c', |
|
| 24 | - '../recipes', |
|
| 25 | - (int) $data->total, |
|
| 26 | - (int) $data->lifted |
|
| 27 | - ); |
|
| 19 | + $arr[] = new Stats_Settings( |
|
| 20 | + __( 'Boosted Recipes are the ones WordLift matched with KG. Some Explanation how it helps them.', 'wordlift' ), |
|
| 21 | + __( 'Lifted Recipes', 'wordlift' ), |
|
| 22 | + __( 'Recipes', 'wordlift' ), |
|
| 23 | + '#00c48c', |
|
| 24 | + '../recipes', |
|
| 25 | + (int) $data->total, |
|
| 26 | + (int) $data->lifted |
|
| 27 | + ); |
|
| 28 | 28 | |
| 29 | - return $arr; |
|
| 30 | - } |
|
| 29 | + return $arr; |
|
| 30 | + } |
|
| 31 | 31 | |
| 32 | - public function get_data() { |
|
| 33 | - global $wpdb; |
|
| 32 | + public function get_data() { |
|
| 33 | + global $wpdb; |
|
| 34 | 34 | |
| 35 | - return $wpdb->get_row( |
|
| 36 | - $wpdb->prepare( |
|
| 37 | - " |
|
| 35 | + return $wpdb->get_row( |
|
| 36 | + $wpdb->prepare( |
|
| 37 | + " |
|
| 38 | 38 | SELECT COUNT(1) AS total, COUNT(e.about_jsonld) as lifted |
| 39 | 39 | FROM {$wpdb->prefix}posts p |
| 40 | 40 | LEFT JOIN {$wpdb->prefix}wl_entities e |
@@ -42,10 +42,10 @@ discard block |
||
| 42 | 42 | AND e.content_type = %d |
| 43 | 43 | WHERE p.post_type = %s |
| 44 | 44 | ", |
| 45 | - Object_Type_Enum::POST, |
|
| 46 | - 'wprm_recipe' |
|
| 47 | - ) |
|
| 48 | - ); |
|
| 49 | - } |
|
| 45 | + Object_Type_Enum::POST, |
|
| 46 | + 'wprm_recipe' |
|
| 47 | + ) |
|
| 48 | + ); |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | 51 | } |
@@ -9,17 +9,17 @@ |
||
| 9 | 9 | class Food_Kg_Recipe_Stats { |
| 10 | 10 | |
| 11 | 11 | public function register_hooks() { |
| 12 | - add_filter( 'wl_dashboard__stats__settings', array( $this, 'dashboard__stats__settings' ) ); |
|
| 12 | + add_filter('wl_dashboard__stats__settings', array($this, 'dashboard__stats__settings')); |
|
| 13 | 13 | } |
| 14 | 14 | |
| 15 | - public function dashboard__stats__settings( $arr ) { |
|
| 15 | + public function dashboard__stats__settings($arr) { |
|
| 16 | 16 | |
| 17 | 17 | $data = Recipe_Query::get_data(); // $this->get_data(); |
| 18 | 18 | |
| 19 | 19 | $arr[] = new Stats_Settings( |
| 20 | - __( 'Boosted Recipes are the ones WordLift matched with KG. Some Explanation how it helps them.', 'wordlift' ), |
|
| 21 | - __( 'Lifted Recipes', 'wordlift' ), |
|
| 22 | - __( 'Recipes', 'wordlift' ), |
|
| 20 | + __('Boosted Recipes are the ones WordLift matched with KG. Some Explanation how it helps them.', 'wordlift'), |
|
| 21 | + __('Lifted Recipes', 'wordlift'), |
|
| 22 | + __('Recipes', 'wordlift'), |
|
| 23 | 23 | '#00c48c', |
| 24 | 24 | '../recipes', |
| 25 | 25 | (int) $data->total, |
@@ -9,59 +9,59 @@ |
||
| 9 | 9 | |
| 10 | 10 | class Jsonld { |
| 11 | 11 | |
| 12 | - /** |
|
| 13 | - * @var Content_Service $content_service |
|
| 14 | - */ |
|
| 15 | - private $content_service; |
|
| 12 | + /** |
|
| 13 | + * @var Content_Service $content_service |
|
| 14 | + */ |
|
| 15 | + private $content_service; |
|
| 16 | 16 | |
| 17 | - public function __construct( Content_Service $content_service ) { |
|
| 18 | - $this->content_service = $content_service; |
|
| 19 | - } |
|
| 17 | + public function __construct( Content_Service $content_service ) { |
|
| 18 | + $this->content_service = $content_service; |
|
| 19 | + } |
|
| 20 | 20 | |
| 21 | - public function register_hooks() { |
|
| 22 | - add_filter( 'wprm_recipe_metadata', array( $this, '__recipe_metadata' ), 10, 2 ); |
|
| 23 | - } |
|
| 21 | + public function register_hooks() { |
|
| 22 | + add_filter( 'wprm_recipe_metadata', array( $this, '__recipe_metadata' ), 10, 2 ); |
|
| 23 | + } |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * @param array $metadata |
|
| 27 | - * @param WPRM_Recipe $recipe |
|
| 28 | - * |
|
| 29 | - * @return array |
|
| 30 | - */ |
|
| 31 | - public function __recipe_metadata( $metadata, $recipe ) { |
|
| 25 | + /** |
|
| 26 | + * @param array $metadata |
|
| 27 | + * @param WPRM_Recipe $recipe |
|
| 28 | + * |
|
| 29 | + * @return array |
|
| 30 | + */ |
|
| 31 | + public function __recipe_metadata( $metadata, $recipe ) { |
|
| 32 | 32 | |
| 33 | - $ingredients = get_the_terms( $recipe->id(), 'wprm_ingredient' ); |
|
| 34 | - if ( ! $ingredients ) { |
|
| 35 | - return $metadata; |
|
| 36 | - } |
|
| 37 | - $jsonlds = array_filter( array_map( array( $this, '__term_id_to_jsonld' ), $ingredients ) ); |
|
| 33 | + $ingredients = get_the_terms( $recipe->id(), 'wprm_ingredient' ); |
|
| 34 | + if ( ! $ingredients ) { |
|
| 35 | + return $metadata; |
|
| 36 | + } |
|
| 37 | + $jsonlds = array_filter( array_map( array( $this, '__term_id_to_jsonld' ), $ingredients ) ); |
|
| 38 | 38 | |
| 39 | - if ( empty( $jsonlds ) ) { |
|
| 40 | - return $metadata; |
|
| 41 | - } |
|
| 39 | + if ( empty( $jsonlds ) ) { |
|
| 40 | + return $metadata; |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | - // We're embedding the full json-ld here because WL doesn't output its own markup, so it makes no sense |
|
| 44 | - // to hook to wl_after_json_ld. |
|
| 45 | - $metadata['mentions'] = isset( $metadata['mentions'] ) ? $metadata['mentions'] : array(); |
|
| 46 | - $metadata['mentions'] = array_merge( $metadata['mentions'], $jsonlds ); |
|
| 43 | + // We're embedding the full json-ld here because WL doesn't output its own markup, so it makes no sense |
|
| 44 | + // to hook to wl_after_json_ld. |
|
| 45 | + $metadata['mentions'] = isset( $metadata['mentions'] ) ? $metadata['mentions'] : array(); |
|
| 46 | + $metadata['mentions'] = array_merge( $metadata['mentions'], $jsonlds ); |
|
| 47 | 47 | |
| 48 | - return $metadata; |
|
| 49 | - } |
|
| 48 | + return $metadata; |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | - /** |
|
| 52 | - * @param WP_Term $ingredient |
|
| 53 | - * |
|
| 54 | - * @return array|null |
|
| 55 | - */ |
|
| 56 | - private function __term_id_to_jsonld( $ingredient ) { |
|
| 57 | - $content_id = Wordpress_Content_Id::create_term( $ingredient->term_id ); |
|
| 58 | - $jsonld = $this->content_service->get_about_jsonld( $content_id ); |
|
| 51 | + /** |
|
| 52 | + * @param WP_Term $ingredient |
|
| 53 | + * |
|
| 54 | + * @return array|null |
|
| 55 | + */ |
|
| 56 | + private function __term_id_to_jsonld( $ingredient ) { |
|
| 57 | + $content_id = Wordpress_Content_Id::create_term( $ingredient->term_id ); |
|
| 58 | + $jsonld = $this->content_service->get_about_jsonld( $content_id ); |
|
| 59 | 59 | |
| 60 | - if ( ! is_string( $jsonld ) ) { |
|
| 61 | - return null; |
|
| 62 | - } |
|
| 60 | + if ( ! is_string( $jsonld ) ) { |
|
| 61 | + return null; |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | - return json_decode( $jsonld, true ); |
|
| 65 | - } |
|
| 64 | + return json_decode( $jsonld, true ); |
|
| 65 | + } |
|
| 66 | 66 | |
| 67 | 67 | } |
@@ -14,12 +14,12 @@ discard block |
||
| 14 | 14 | */ |
| 15 | 15 | private $content_service; |
| 16 | 16 | |
| 17 | - public function __construct( Content_Service $content_service ) { |
|
| 17 | + public function __construct(Content_Service $content_service) { |
|
| 18 | 18 | $this->content_service = $content_service; |
| 19 | 19 | } |
| 20 | 20 | |
| 21 | 21 | public function register_hooks() { |
| 22 | - add_filter( 'wprm_recipe_metadata', array( $this, '__recipe_metadata' ), 10, 2 ); |
|
| 22 | + add_filter('wprm_recipe_metadata', array($this, '__recipe_metadata'), 10, 2); |
|
| 23 | 23 | } |
| 24 | 24 | |
| 25 | 25 | /** |
@@ -28,22 +28,22 @@ discard block |
||
| 28 | 28 | * |
| 29 | 29 | * @return array |
| 30 | 30 | */ |
| 31 | - public function __recipe_metadata( $metadata, $recipe ) { |
|
| 31 | + public function __recipe_metadata($metadata, $recipe) { |
|
| 32 | 32 | |
| 33 | - $ingredients = get_the_terms( $recipe->id(), 'wprm_ingredient' ); |
|
| 34 | - if ( ! $ingredients ) { |
|
| 33 | + $ingredients = get_the_terms($recipe->id(), 'wprm_ingredient'); |
|
| 34 | + if ( ! $ingredients) { |
|
| 35 | 35 | return $metadata; |
| 36 | 36 | } |
| 37 | - $jsonlds = array_filter( array_map( array( $this, '__term_id_to_jsonld' ), $ingredients ) ); |
|
| 37 | + $jsonlds = array_filter(array_map(array($this, '__term_id_to_jsonld'), $ingredients)); |
|
| 38 | 38 | |
| 39 | - if ( empty( $jsonlds ) ) { |
|
| 39 | + if (empty($jsonlds)) { |
|
| 40 | 40 | return $metadata; |
| 41 | 41 | } |
| 42 | 42 | |
| 43 | 43 | // We're embedding the full json-ld here because WL doesn't output its own markup, so it makes no sense |
| 44 | 44 | // to hook to wl_after_json_ld. |
| 45 | - $metadata['mentions'] = isset( $metadata['mentions'] ) ? $metadata['mentions'] : array(); |
|
| 46 | - $metadata['mentions'] = array_merge( $metadata['mentions'], $jsonlds ); |
|
| 45 | + $metadata['mentions'] = isset($metadata['mentions']) ? $metadata['mentions'] : array(); |
|
| 46 | + $metadata['mentions'] = array_merge($metadata['mentions'], $jsonlds); |
|
| 47 | 47 | |
| 48 | 48 | return $metadata; |
| 49 | 49 | } |
@@ -53,15 +53,15 @@ discard block |
||
| 53 | 53 | * |
| 54 | 54 | * @return array|null |
| 55 | 55 | */ |
| 56 | - private function __term_id_to_jsonld( $ingredient ) { |
|
| 57 | - $content_id = Wordpress_Content_Id::create_term( $ingredient->term_id ); |
|
| 58 | - $jsonld = $this->content_service->get_about_jsonld( $content_id ); |
|
| 56 | + private function __term_id_to_jsonld($ingredient) { |
|
| 57 | + $content_id = Wordpress_Content_Id::create_term($ingredient->term_id); |
|
| 58 | + $jsonld = $this->content_service->get_about_jsonld($content_id); |
|
| 59 | 59 | |
| 60 | - if ( ! is_string( $jsonld ) ) { |
|
| 60 | + if ( ! is_string($jsonld)) { |
|
| 61 | 61 | return null; |
| 62 | 62 | } |
| 63 | 63 | |
| 64 | - return json_decode( $jsonld, true ); |
|
| 64 | + return json_decode($jsonld, true); |
|
| 65 | 65 | } |
| 66 | 66 | |
| 67 | 67 | } |
@@ -8,40 +8,40 @@ |
||
| 8 | 8 | |
| 9 | 9 | class Main_Ingredient_Jsonld { |
| 10 | 10 | |
| 11 | - /** |
|
| 12 | - * @var Content_Service $content_service |
|
| 13 | - */ |
|
| 14 | - private $content_service; |
|
| 15 | - |
|
| 16 | - public function __construct( Content_Service $content_service ) { |
|
| 17 | - $this->content_service = $content_service; |
|
| 18 | - } |
|
| 19 | - |
|
| 20 | - public function register_hooks() { |
|
| 21 | - add_filter( 'wprm_recipe_metadata', array( $this, '__recipe_metadata' ), 10, 2 ); |
|
| 22 | - } |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * @param array $metadata |
|
| 26 | - * @param WPRM_Recipe $recipe |
|
| 27 | - * |
|
| 28 | - * @return array |
|
| 29 | - */ |
|
| 30 | - public function __recipe_metadata( $metadata, $recipe ) { |
|
| 31 | - $jsonld = $this->content_service->get_about_jsonld( |
|
| 32 | - Wordpress_Content_Id::create_post( $recipe->id() ) |
|
| 33 | - ); |
|
| 34 | - |
|
| 35 | - if ( empty( $jsonld ) ) { |
|
| 36 | - return $metadata; |
|
| 37 | - } |
|
| 38 | - |
|
| 39 | - // We're embedding the full json-ld here because WL doesn't output its own markup, so it makes no sense |
|
| 40 | - // to hook to wl_after_json_ld. |
|
| 41 | - $metadata['about'] = isset( $metadata['about'] ) ? $metadata['about'] : array(); |
|
| 42 | - $metadata['about'] = array_merge( $metadata['about'], array( json_decode( $jsonld, true ) ) ); |
|
| 43 | - |
|
| 44 | - return $metadata; |
|
| 45 | - } |
|
| 11 | + /** |
|
| 12 | + * @var Content_Service $content_service |
|
| 13 | + */ |
|
| 14 | + private $content_service; |
|
| 15 | + |
|
| 16 | + public function __construct( Content_Service $content_service ) { |
|
| 17 | + $this->content_service = $content_service; |
|
| 18 | + } |
|
| 19 | + |
|
| 20 | + public function register_hooks() { |
|
| 21 | + add_filter( 'wprm_recipe_metadata', array( $this, '__recipe_metadata' ), 10, 2 ); |
|
| 22 | + } |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * @param array $metadata |
|
| 26 | + * @param WPRM_Recipe $recipe |
|
| 27 | + * |
|
| 28 | + * @return array |
|
| 29 | + */ |
|
| 30 | + public function __recipe_metadata( $metadata, $recipe ) { |
|
| 31 | + $jsonld = $this->content_service->get_about_jsonld( |
|
| 32 | + Wordpress_Content_Id::create_post( $recipe->id() ) |
|
| 33 | + ); |
|
| 34 | + |
|
| 35 | + if ( empty( $jsonld ) ) { |
|
| 36 | + return $metadata; |
|
| 37 | + } |
|
| 38 | + |
|
| 39 | + // We're embedding the full json-ld here because WL doesn't output its own markup, so it makes no sense |
|
| 40 | + // to hook to wl_after_json_ld. |
|
| 41 | + $metadata['about'] = isset( $metadata['about'] ) ? $metadata['about'] : array(); |
|
| 42 | + $metadata['about'] = array_merge( $metadata['about'], array( json_decode( $jsonld, true ) ) ); |
|
| 43 | + |
|
| 44 | + return $metadata; |
|
| 45 | + } |
|
| 46 | 46 | |
| 47 | 47 | } |
@@ -13,12 +13,12 @@ discard block |
||
| 13 | 13 | */ |
| 14 | 14 | private $content_service; |
| 15 | 15 | |
| 16 | - public function __construct( Content_Service $content_service ) { |
|
| 16 | + public function __construct(Content_Service $content_service) { |
|
| 17 | 17 | $this->content_service = $content_service; |
| 18 | 18 | } |
| 19 | 19 | |
| 20 | 20 | public function register_hooks() { |
| 21 | - add_filter( 'wprm_recipe_metadata', array( $this, '__recipe_metadata' ), 10, 2 ); |
|
| 21 | + add_filter('wprm_recipe_metadata', array($this, '__recipe_metadata'), 10, 2); |
|
| 22 | 22 | } |
| 23 | 23 | |
| 24 | 24 | /** |
@@ -27,19 +27,19 @@ discard block |
||
| 27 | 27 | * |
| 28 | 28 | * @return array |
| 29 | 29 | */ |
| 30 | - public function __recipe_metadata( $metadata, $recipe ) { |
|
| 30 | + public function __recipe_metadata($metadata, $recipe) { |
|
| 31 | 31 | $jsonld = $this->content_service->get_about_jsonld( |
| 32 | - Wordpress_Content_Id::create_post( $recipe->id() ) |
|
| 32 | + Wordpress_Content_Id::create_post($recipe->id()) |
|
| 33 | 33 | ); |
| 34 | 34 | |
| 35 | - if ( empty( $jsonld ) ) { |
|
| 35 | + if (empty($jsonld)) { |
|
| 36 | 36 | return $metadata; |
| 37 | 37 | } |
| 38 | 38 | |
| 39 | 39 | // We're embedding the full json-ld here because WL doesn't output its own markup, so it makes no sense |
| 40 | 40 | // to hook to wl_after_json_ld. |
| 41 | - $metadata['about'] = isset( $metadata['about'] ) ? $metadata['about'] : array(); |
|
| 42 | - $metadata['about'] = array_merge( $metadata['about'], array( json_decode( $jsonld, true ) ) ); |
|
| 41 | + $metadata['about'] = isset($metadata['about']) ? $metadata['about'] : array(); |
|
| 42 | + $metadata['about'] = array_merge($metadata['about'], array(json_decode($jsonld, true))); |
|
| 43 | 43 | |
| 44 | 44 | return $metadata; |
| 45 | 45 | } |