Completed
Push — develop ( ea7315...985324 )
by David
01:53 queued 15s
created
src/modules/dashboard/includes/Post_Entity_Match/Recipe_Query.php 1 patch
Indentation   +204 added lines, -204 removed lines patch added patch discarded remove patch
@@ -6,51 +6,51 @@  discard block
 block discarded – undo
6 6
 use WP_REST_Request;
7 7
 
8 8
 class Recipe_Query {
9
-	/**
10
-	 * @var WP_REST_Request
11
-	 */
12
-	private $request;
13
-	/**
14
-	 * @var mixed
15
-	 */
16
-	private $position;
17
-	/**
18
-	 * @var mixed
19
-	 */
20
-	private $element;
21
-	/**
22
-	 * @var mixed
23
-	 */
24
-	private $direction;
25
-
26
-	private $sort;
27
-
28
-	private $sortby;
29
-	/**
30
-	 * @var mixed
31
-	 */
32
-	private $limit;
33
-
34
-	/** @var Cursor_Sort $cursor_sort */
35
-	private $cursor_sort;
36
-
37
-	/**
38
-	 * @param WP_REST_Request $request
39
-	 * @param Cursor          $cursor
40
-	 */
41
-	public function __construct( $request, $cursor, $cursor_sort, $limit ) {
42
-		global $wpdb;
43
-
44
-		$this->request     = $request;
45
-		$this->position    = $cursor->get_position();
46
-		$this->element     = $cursor->get_element();
47
-		$this->direction   = $cursor->get_direction();
48
-		$this->limit       = $limit;
49
-		$this->cursor_sort = $cursor_sort;
50
-
51
-		$this->set_sort();
52
-
53
-		$this->sql = "
9
+    /**
10
+     * @var WP_REST_Request
11
+     */
12
+    private $request;
13
+    /**
14
+     * @var mixed
15
+     */
16
+    private $position;
17
+    /**
18
+     * @var mixed
19
+     */
20
+    private $element;
21
+    /**
22
+     * @var mixed
23
+     */
24
+    private $direction;
25
+
26
+    private $sort;
27
+
28
+    private $sortby;
29
+    /**
30
+     * @var mixed
31
+     */
32
+    private $limit;
33
+
34
+    /** @var Cursor_Sort $cursor_sort */
35
+    private $cursor_sort;
36
+
37
+    /**
38
+     * @param WP_REST_Request $request
39
+     * @param Cursor          $cursor
40
+     */
41
+    public function __construct( $request, $cursor, $cursor_sort, $limit ) {
42
+        global $wpdb;
43
+
44
+        $this->request     = $request;
45
+        $this->position    = $cursor->get_position();
46
+        $this->element     = $cursor->get_element();
47
+        $this->direction   = $cursor->get_direction();
48
+        $this->limit       = $limit;
49
+        $this->cursor_sort = $cursor_sort;
50
+
51
+        $this->set_sort();
52
+
53
+        $this->sql = "
54 54
 			SELECT p.ID as id,
55 55
 				p.post_title,
56 56
 				p.post_status,
@@ -69,156 +69,156 @@  discard block
 block discarded – undo
69 69
 			WHERE 1=1 
70 70
 		";
71 71
 
72
-		$this->cursor();
73
-		$this->has_match();
74
-		$this->post_types();
75
-		$this->post_status();
76
-		$this->sort();
77
-		$this->limit();
78
-
79
-		// p.post_modified_gmt $condition %s
80
-		// AND p.post_types IN ( '" . implode( "','", $post_types ) . "' )
81
-		// AND p.post_status = %s
82
-		// ORDER BY p.post_modified_gmt $sort
83
-		// LIMIT %d
84
-	}
85
-
86
-	public function get_results() {
87
-		global $wpdb;
88
-
89
-		// The `sql` is prepared in each delegated function in this class.
90
-		// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
91
-		$items = $wpdb->get_results( $this->sql );
92
-
93
-		$sort = ( $this->sort === 'ASC' ? SORT_ASC : SORT_DESC );
94
-		array_multisort( array_column( $items, $this->cursor_sort->get_sort_property() ), $sort, $items );
95
-		$items = array_map( array( $this, 'map_item' ), $items );
96
-
97
-		return $items;
98
-	}
99
-
100
-	public function map_item( $item ) {
101
-		if ( $item->id ) {
102
-			$item->post_link    = get_edit_post_link( $item->id, 'ui' );
103
-			$item->view_link    = get_permalink( $item->id );
104
-			$item->preview_link = get_preview_post_link( $item->id );
105
-		}
106
-
107
-		if ( $item->parent_post_id ) {
108
-			$item->parent_post_link = get_edit_post_link( $item->parent_post_id, 'ui' );
109
-		}
110
-
111
-		$item->match_name = $this->get_match_name( $item->match_jsonld );
112
-
113
-		return $item;
114
-	}
115
-
116
-	private function get_match_name( $jsonld ) {
117
-		$data = json_decode( $jsonld, true );
118
-		if ( ! $data || ! array_key_exists( 'name', $data ) ) {
119
-			return null;
120
-		}
121
-
122
-		return $data['name'];
123
-	}
124
-
125
-	private function post_types() {
126
-		$post_types = $this->request->has_param( 'post_types' )
127
-			? (array) $this->request->get_param( 'post_types' )
128
-			: array( 'post', 'page' );
129
-		$value      = array_map( 'esc_sql', $post_types );
130
-		$this->sql .= " AND p.post_type IN ( '" . implode( "', '", $value ) . "' )";
131
-	}
132
-
133
-	private function limit() {
134
-		$value      = is_numeric( $this->limit ) ? $this->limit : 10;
135
-		$this->sql .= ' LIMIT ' . esc_sql( $value );
136
-	}
137
-
138
-	private function has_match() {
139
-		if ( ! $this->request->has_param( 'has_match' ) ) {
140
-			return;
141
-		}
142
-
143
-		$value = (bool) $this->request->get_param( 'has_match' );
144
-
145
-		if ( $value ) {
146
-			$this->sql .= ' AND e.about_jsonld IS NOT NULL';
147
-		} else {
148
-			$this->sql .= ' AND e.about_jsonld IS NULL';
149
-		}
150
-	}
151
-
152
-	private function sort() {
153
-		switch ( $this->direction . '$' . $this->sort ) {
154
-			case 'ASCENDING$ASC':
155
-			case 'DESCENDING$DESC':
156
-				$sort = 'ASC';
157
-				break;
158
-			case 'ASCENDING$DESC':
159
-			case 'DESCENDING$ASC':
160
-				$sort = 'DESC';
161
-				break;
162
-		}
163
-
164
-		$this->sql .= ' ORDER BY p.' . $this->sortby . ' ' . $sort;
165
-	}
166
-
167
-	private function post_status() {
168
-		if ( ! $this->request->has_param( 'post_status' ) ) {
169
-			$this->sql .= " AND p.post_status IN ( 'draft', 'publish' ) ";
170
-
171
-			return;
172
-		}
173
-
174
-		global $wpdb;
175
-		$value      = $this->request->get_param( 'post_status' );
176
-		$this->sql .= $wpdb->prepare( ' AND p.post_status = %s', $value );
177
-	}
178
-
179
-	private function cursor() {
180
-		if ( ! isset( $this->position ) ) {
181
-			return;
182
-		}
183
-
184
-		switch ( $this->direction . '$' . $this->sort ) {
185
-			case 'ASCENDING$ASC':
186
-			case 'DESCENDING$DESC':
187
-				$condition = '>';
188
-				break;
189
-			case 'ASCENDING$DESC':
190
-			case 'DESCENDING$ASC':
191
-				$condition = '<';
192
-				break;
193
-		}
194
-
195
-		$condition .= ( $this->element === 'INCLUDED' ? '=' : '' );
196
-		global $wpdb;
197
-		// We control the vars in this method.
198
-		// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
199
-		$this->sql .= $wpdb->prepare( ' AND p.' . esc_sql( $this->sortby ) . ' ' . $condition . ' %s', $this->position );
200
-	}
201
-
202
-	private function set_sort() {
203
-		$sortby_to_col = array(
204
-			'date_modified_gmt' => 'post_modified_gmt',
205
-		);
206
-
207
-		$value = $this->request->has_param( 'sort' )
208
-			? $this->request->get_param( 'sort' )
209
-			: '-date_modified_gmt';
210
-
211
-		$sortby       = substr( $value, 1 );
212
-		$this->sortby = isset( $sortby_to_col[ $sortby ] ) ? $sortby_to_col[ $sortby ] : $sortby;
213
-		$this->sort   = substr( $value, 0, 1 ) === '+' ? 'ASC' : 'DESC';
214
-	}
215
-
216
-	public static function get_data() {
217
-		global $wpdb;
218
-
219
-		return $wpdb->get_row(
220
-			$wpdb->prepare(
221
-				"
72
+        $this->cursor();
73
+        $this->has_match();
74
+        $this->post_types();
75
+        $this->post_status();
76
+        $this->sort();
77
+        $this->limit();
78
+
79
+        // p.post_modified_gmt $condition %s
80
+        // AND p.post_types IN ( '" . implode( "','", $post_types ) . "' )
81
+        // AND p.post_status = %s
82
+        // ORDER BY p.post_modified_gmt $sort
83
+        // LIMIT %d
84
+    }
85
+
86
+    public function get_results() {
87
+        global $wpdb;
88
+
89
+        // The `sql` is prepared in each delegated function in this class.
90
+        // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
91
+        $items = $wpdb->get_results( $this->sql );
92
+
93
+        $sort = ( $this->sort === 'ASC' ? SORT_ASC : SORT_DESC );
94
+        array_multisort( array_column( $items, $this->cursor_sort->get_sort_property() ), $sort, $items );
95
+        $items = array_map( array( $this, 'map_item' ), $items );
96
+
97
+        return $items;
98
+    }
99
+
100
+    public function map_item( $item ) {
101
+        if ( $item->id ) {
102
+            $item->post_link    = get_edit_post_link( $item->id, 'ui' );
103
+            $item->view_link    = get_permalink( $item->id );
104
+            $item->preview_link = get_preview_post_link( $item->id );
105
+        }
106
+
107
+        if ( $item->parent_post_id ) {
108
+            $item->parent_post_link = get_edit_post_link( $item->parent_post_id, 'ui' );
109
+        }
110
+
111
+        $item->match_name = $this->get_match_name( $item->match_jsonld );
112
+
113
+        return $item;
114
+    }
115
+
116
+    private function get_match_name( $jsonld ) {
117
+        $data = json_decode( $jsonld, true );
118
+        if ( ! $data || ! array_key_exists( 'name', $data ) ) {
119
+            return null;
120
+        }
121
+
122
+        return $data['name'];
123
+    }
124
+
125
+    private function post_types() {
126
+        $post_types = $this->request->has_param( 'post_types' )
127
+            ? (array) $this->request->get_param( 'post_types' )
128
+            : array( 'post', 'page' );
129
+        $value      = array_map( 'esc_sql', $post_types );
130
+        $this->sql .= " AND p.post_type IN ( '" . implode( "', '", $value ) . "' )";
131
+    }
132
+
133
+    private function limit() {
134
+        $value      = is_numeric( $this->limit ) ? $this->limit : 10;
135
+        $this->sql .= ' LIMIT ' . esc_sql( $value );
136
+    }
137
+
138
+    private function has_match() {
139
+        if ( ! $this->request->has_param( 'has_match' ) ) {
140
+            return;
141
+        }
142
+
143
+        $value = (bool) $this->request->get_param( 'has_match' );
144
+
145
+        if ( $value ) {
146
+            $this->sql .= ' AND e.about_jsonld IS NOT NULL';
147
+        } else {
148
+            $this->sql .= ' AND e.about_jsonld IS NULL';
149
+        }
150
+    }
151
+
152
+    private function sort() {
153
+        switch ( $this->direction . '$' . $this->sort ) {
154
+            case 'ASCENDING$ASC':
155
+            case 'DESCENDING$DESC':
156
+                $sort = 'ASC';
157
+                break;
158
+            case 'ASCENDING$DESC':
159
+            case 'DESCENDING$ASC':
160
+                $sort = 'DESC';
161
+                break;
162
+        }
163
+
164
+        $this->sql .= ' ORDER BY p.' . $this->sortby . ' ' . $sort;
165
+    }
166
+
167
+    private function post_status() {
168
+        if ( ! $this->request->has_param( 'post_status' ) ) {
169
+            $this->sql .= " AND p.post_status IN ( 'draft', 'publish' ) ";
170
+
171
+            return;
172
+        }
173
+
174
+        global $wpdb;
175
+        $value      = $this->request->get_param( 'post_status' );
176
+        $this->sql .= $wpdb->prepare( ' AND p.post_status = %s', $value );
177
+    }
178
+
179
+    private function cursor() {
180
+        if ( ! isset( $this->position ) ) {
181
+            return;
182
+        }
183
+
184
+        switch ( $this->direction . '$' . $this->sort ) {
185
+            case 'ASCENDING$ASC':
186
+            case 'DESCENDING$DESC':
187
+                $condition = '>';
188
+                break;
189
+            case 'ASCENDING$DESC':
190
+            case 'DESCENDING$ASC':
191
+                $condition = '<';
192
+                break;
193
+        }
194
+
195
+        $condition .= ( $this->element === 'INCLUDED' ? '=' : '' );
196
+        global $wpdb;
197
+        // We control the vars in this method.
198
+        // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
199
+        $this->sql .= $wpdb->prepare( ' AND p.' . esc_sql( $this->sortby ) . ' ' . $condition . ' %s', $this->position );
200
+    }
201
+
202
+    private function set_sort() {
203
+        $sortby_to_col = array(
204
+            'date_modified_gmt' => 'post_modified_gmt',
205
+        );
206
+
207
+        $value = $this->request->has_param( 'sort' )
208
+            ? $this->request->get_param( 'sort' )
209
+            : '-date_modified_gmt';
210
+
211
+        $sortby       = substr( $value, 1 );
212
+        $this->sortby = isset( $sortby_to_col[ $sortby ] ) ? $sortby_to_col[ $sortby ] : $sortby;
213
+        $this->sort   = substr( $value, 0, 1 ) === '+' ? 'ASC' : 'DESC';
214
+    }
215
+
216
+    public static function get_data() {
217
+        global $wpdb;
218
+
219
+        return $wpdb->get_row(
220
+            $wpdb->prepare(
221
+                "
222 222
 				SELECT COUNT(1) AS total, COUNT(e.about_jsonld) as lifted
223 223
 				FROM {$wpdb->prefix}posts p
224 224
 				LEFT JOIN {$wpdb->prefix}wl_entities e
@@ -230,14 +230,14 @@  discard block
 block discarded – undo
230 230
 					ON pm.meta_value = parent.ID 
231 231
 				WHERE p.post_type = %s
232 232
 				",
233
-				Object_Type_Enum::POST,
234
-				'wprm_recipe'
235
-			)
236
-		);
237
-	}
238
-
239
-	/**
240
-	 * SELECT p.ID as id,
233
+                Object_Type_Enum::POST,
234
+                'wprm_recipe'
235
+            )
236
+        );
237
+    }
238
+
239
+    /**
240
+     * SELECT p.ID as id,
241 241
 	p.post_title,
242 242
 	p.post_status,
243 243
 	p.post_modified_gmt as date_modified_gmt,
@@ -253,5 +253,5 @@  discard block
 block discarded – undo
253 253
 	INNER JOIN {$wpdb->prefix}posts parent
254 254
 	ON pm.meta_value = parent.ID
255 255
 	WHERE 1=1
256
-	 */
256
+     */
257 257
 }
Please login to merge, or discard this patch.
src/modules/dashboard/includes/Post_Entity_Match/Post_Query.php 1 patch
Indentation   +181 added lines, -181 removed lines patch added patch discarded remove patch
@@ -5,51 +5,51 @@  discard block
 block discarded – undo
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,141 +62,141 @@  discard block
 block discarded – undo
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
-		$item->match_name   = $this->get_match_name( $item->match_jsonld );
98
-
99
-		return $item;
100
-	}
101
-
102
-	private function get_match_name( $jsonld ) {
103
-		$data = json_decode( $jsonld, true );
104
-		if ( ! $data || ! array_key_exists( 'name', $data ) ) {
105
-			return null;
106
-		}
107
-
108
-		return $data['name'];
109
-	}
110
-
111
-	private function post_types() {
112
-		$post_types = $this->request->has_param( 'post_types' )
113
-			? (array) $this->request->get_param( 'post_types' )
114
-			: array( 'post', 'page' );
115
-		$value      = array_map( 'esc_sql', $post_types );
116
-		$this->sql .= " AND p.post_type IN ( '" . implode( "', '", $value ) . "' )";
117
-	}
118
-
119
-	private function limit() {
120
-		$value      = is_numeric( $this->limit ) ? $this->limit : 10;
121
-		$this->sql .= ' LIMIT ' . esc_sql( $value );
122
-	}
123
-
124
-	private function has_match() {
125
-		if ( ! $this->request->has_param( 'has_match' ) ) {
126
-			return;
127
-		}
128
-
129
-		$value = (bool) $this->request->get_param( 'has_match' );
130
-
131
-		if ( $value ) {
132
-			$this->sql .= ' AND e.about_jsonld IS NOT NULL';
133
-		} else {
134
-			$this->sql .= ' AND e.about_jsonld IS NULL';
135
-		}
136
-	}
137
-
138
-	private function sort() {
139
-		switch ( $this->direction . '$' . $this->sort ) {
140
-			case 'ASCENDING$ASC':
141
-			case 'DESCENDING$DESC':
142
-				$sort = 'ASC';
143
-				break;
144
-			case 'ASCENDING$DESC':
145
-			case 'DESCENDING$ASC':
146
-				$sort = 'DESC';
147
-				break;
148
-		}
149
-
150
-		$this->sql .= ' ORDER BY p.' . $this->sortby . ' ' . $sort;
151
-	}
152
-
153
-	private function post_status() {
154
-		if ( ! $this->request->has_param( 'post_status' ) ) {
155
-			$this->sql .= " AND p.post_status IN ( 'draft', 'publish' ) ";
156
-
157
-			return;
158
-		}
159
-
160
-		global $wpdb;
161
-		$value      = $this->request->get_param( 'post_status' );
162
-		$this->sql .= $wpdb->prepare( ' AND p.post_status = %s', $value );
163
-	}
164
-
165
-	private function cursor() {
166
-		if ( ! isset( $this->position ) ) {
167
-			return;
168
-		}
169
-
170
-		switch ( $this->direction . '$' . $this->sort ) {
171
-			case 'ASCENDING$ASC':
172
-			case 'DESCENDING$DESC':
173
-				$condition = '>';
174
-				break;
175
-			case 'ASCENDING$DESC':
176
-			case 'DESCENDING$ASC':
177
-				$condition = '<';
178
-				break;
179
-		}
180
-
181
-		$condition .= ( $this->element === 'INCLUDED' ? '=' : '' );
182
-		global $wpdb;
183
-		// We control the vars in this method.
184
-		// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
185
-		$this->sql .= $wpdb->prepare( ' AND p.' . esc_sql( $this->sortby ) . ' ' . $condition . ' %s', $this->position );
186
-	}
187
-
188
-	private function set_sort() {
189
-		$sortby_to_col = array(
190
-			'date_modified_gmt' => 'post_modified_gmt',
191
-		);
192
-
193
-		$value = $this->request->has_param( 'sort' )
194
-			? $this->request->get_param( 'sort' )
195
-			: '-date_modified_gmt';
196
-
197
-		$sortby       = substr( $value, 1 );
198
-		$this->sortby = isset( $sortby_to_col[ $sortby ] ) ? $sortby_to_col[ $sortby ] : $sortby;
199
-		$this->sort   = substr( $value, 0, 1 ) === '+' ? 'ASC' : 'DESC';
200
-	}
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
+        $item->match_name   = $this->get_match_name( $item->match_jsonld );
98
+
99
+        return $item;
100
+    }
101
+
102
+    private function get_match_name( $jsonld ) {
103
+        $data = json_decode( $jsonld, true );
104
+        if ( ! $data || ! array_key_exists( 'name', $data ) ) {
105
+            return null;
106
+        }
107
+
108
+        return $data['name'];
109
+    }
110
+
111
+    private function post_types() {
112
+        $post_types = $this->request->has_param( 'post_types' )
113
+            ? (array) $this->request->get_param( 'post_types' )
114
+            : array( 'post', 'page' );
115
+        $value      = array_map( 'esc_sql', $post_types );
116
+        $this->sql .= " AND p.post_type IN ( '" . implode( "', '", $value ) . "' )";
117
+    }
118
+
119
+    private function limit() {
120
+        $value      = is_numeric( $this->limit ) ? $this->limit : 10;
121
+        $this->sql .= ' LIMIT ' . esc_sql( $value );
122
+    }
123
+
124
+    private function has_match() {
125
+        if ( ! $this->request->has_param( 'has_match' ) ) {
126
+            return;
127
+        }
128
+
129
+        $value = (bool) $this->request->get_param( 'has_match' );
130
+
131
+        if ( $value ) {
132
+            $this->sql .= ' AND e.about_jsonld IS NOT NULL';
133
+        } else {
134
+            $this->sql .= ' AND e.about_jsonld IS NULL';
135
+        }
136
+    }
137
+
138
+    private function sort() {
139
+        switch ( $this->direction . '$' . $this->sort ) {
140
+            case 'ASCENDING$ASC':
141
+            case 'DESCENDING$DESC':
142
+                $sort = 'ASC';
143
+                break;
144
+            case 'ASCENDING$DESC':
145
+            case 'DESCENDING$ASC':
146
+                $sort = 'DESC';
147
+                break;
148
+        }
149
+
150
+        $this->sql .= ' ORDER BY p.' . $this->sortby . ' ' . $sort;
151
+    }
152
+
153
+    private function post_status() {
154
+        if ( ! $this->request->has_param( 'post_status' ) ) {
155
+            $this->sql .= " AND p.post_status IN ( 'draft', 'publish' ) ";
156
+
157
+            return;
158
+        }
159
+
160
+        global $wpdb;
161
+        $value      = $this->request->get_param( 'post_status' );
162
+        $this->sql .= $wpdb->prepare( ' AND p.post_status = %s', $value );
163
+    }
164
+
165
+    private function cursor() {
166
+        if ( ! isset( $this->position ) ) {
167
+            return;
168
+        }
169
+
170
+        switch ( $this->direction . '$' . $this->sort ) {
171
+            case 'ASCENDING$ASC':
172
+            case 'DESCENDING$DESC':
173
+                $condition = '>';
174
+                break;
175
+            case 'ASCENDING$DESC':
176
+            case 'DESCENDING$ASC':
177
+                $condition = '<';
178
+                break;
179
+        }
180
+
181
+        $condition .= ( $this->element === 'INCLUDED' ? '=' : '' );
182
+        global $wpdb;
183
+        // We control the vars in this method.
184
+        // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
185
+        $this->sql .= $wpdb->prepare( ' AND p.' . esc_sql( $this->sortby ) . ' ' . $condition . ' %s', $this->position );
186
+    }
187
+
188
+    private function set_sort() {
189
+        $sortby_to_col = array(
190
+            'date_modified_gmt' => 'post_modified_gmt',
191
+        );
192
+
193
+        $value = $this->request->has_param( 'sort' )
194
+            ? $this->request->get_param( 'sort' )
195
+            : '-date_modified_gmt';
196
+
197
+        $sortby       = substr( $value, 1 );
198
+        $this->sortby = isset( $sortby_to_col[ $sortby ] ) ? $sortby_to_col[ $sortby ] : $sortby;
199
+        $this->sort   = substr( $value, 0, 1 ) === '+' ? 'ASC' : 'DESC';
200
+    }
201 201
 
202 202
 }
Please login to merge, or discard this patch.