Completed
Push — develop ( a9eff4...17e8c5 )
by David
02:06 queued 14s
created
src/modules/dashboard/includes/Term_Entity_Match/Term_Query.php 1 patch
Indentation   +181 added lines, -181 removed lines patch added patch discarded remove patch
@@ -7,52 +7,52 @@  discard block
 block discarded – undo
7 7
 use WP_REST_Request;
8 8
 
9 9
 class Term_Query {
10
-	/**
11
-	 * @var WP_REST_Request
12
-	 */
13
-	private $request;
14
-	/**
15
-	 * @var mixed
16
-	 */
17
-	private $position;
18
-	/**
19
-	 * @var mixed
20
-	 */
21
-	private $element;
22
-	/**
23
-	 * @var mixed
24
-	 */
25
-	private $direction;
26
-
27
-	private $sort;
28
-
29
-	private $sortby;
30
-	/**
31
-	 * @var mixed
32
-	 */
33
-	private $limit;
34
-
35
-	/** @var Cursor_Sort $cursor_sort */
36
-	private $cursor_sort;
37
-
38
-	/**
39
-	 * @param WP_REST_Request $request
40
-	 * @param Cursor          $cursor
41
-	 */
42
-	public function __construct( $request, $cursor, $cursor_sort, $limit ) {
43
-		global $wpdb;
44
-
45
-		$this->request     = $request;
46
-		$this->position    = $cursor->get_position();
47
-		$this->element     = $cursor->get_element();
48
-		$this->direction   = $cursor->get_direction();
49
-		$this->limit       = $limit;
50
-		$this->cursor_sort = $cursor_sort;
51
-
52
-		$this->set_sort();
53
-
54
-		// the `term_name` is required for sort.
55
-		$this->sql = "
10
+    /**
11
+     * @var WP_REST_Request
12
+     */
13
+    private $request;
14
+    /**
15
+     * @var mixed
16
+     */
17
+    private $position;
18
+    /**
19
+     * @var mixed
20
+     */
21
+    private $element;
22
+    /**
23
+     * @var mixed
24
+     */
25
+    private $direction;
26
+
27
+    private $sort;
28
+
29
+    private $sortby;
30
+    /**
31
+     * @var mixed
32
+     */
33
+    private $limit;
34
+
35
+    /** @var Cursor_Sort $cursor_sort */
36
+    private $cursor_sort;
37
+
38
+    /**
39
+     * @param WP_REST_Request $request
40
+     * @param Cursor          $cursor
41
+     */
42
+    public function __construct( $request, $cursor, $cursor_sort, $limit ) {
43
+        global $wpdb;
44
+
45
+        $this->request     = $request;
46
+        $this->position    = $cursor->get_position();
47
+        $this->element     = $cursor->get_element();
48
+        $this->direction   = $cursor->get_direction();
49
+        $this->limit       = $limit;
50
+        $this->cursor_sort = $cursor_sort;
51
+
52
+        $this->set_sort();
53
+
54
+        // the `term_name` is required for sort.
55
+        $this->sql = "
56 56
 			SELECT t.term_id as id,
57 57
 				e.about_jsonld as match_jsonld,
58 58
 				t.name,
@@ -66,140 +66,140 @@  discard block
 block discarded – undo
66 66
 			WHERE 1=1
67 67
 		";
68 68
 
69
-		$this->cursor();
70
-		$this->has_match();
71
-		$this->term_contains();
72
-		$this->taxonomies();
73
-		$this->sort();
74
-		$this->limit();
75
-
76
-	}
77
-
78
-	public function get_results() {
79
-		global $wpdb;
80
-
81
-		// The `sql` is prepared in each delegated function in this class.
82
-		// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
83
-		$items = $wpdb->get_results( $this->sql );
84
-
85
-		$sort = ( $this->sort === 'ASC' ? SORT_ASC : SORT_DESC );
86
-		array_multisort( array_column( $items, $this->cursor_sort->get_sort_property() ), $sort, $items );
87
-		$items = array_map( array( $this, 'map_item' ), $items );
88
-
89
-		return $items;
90
-	}
91
-
92
-	public function map_item( $item ) {
93
-		$item->match_name = $this->get_match_name( $item->match_jsonld );
94
-
95
-		return $item;
96
-	}
97
-
98
-	private function get_match_name( $jsonld ) {
99
-		$data = json_decode( $jsonld, true );
100
-		if ( ! $data || ! array_key_exists( 'name', $data ) ) {
101
-			return null;
102
-		}
103
-
104
-		return $data['name'];
105
-	}
106
-
107
-	private function post_types() {
108
-		$post_types = $this->request->has_param( 'post_types' )
109
-			? (array) $this->request->get_param( 'post_types' )
110
-			: array( 'post', 'page' );
111
-		$value      = array_map( 'esc_sql', $post_types );
112
-		$this->sql .= " AND p.post_type IN ( '" . implode( "', '", $value ) . "' )";
113
-	}
114
-
115
-	private function limit() {
116
-		$value      = is_numeric( $this->limit ) ? $this->limit : 10;
117
-		$this->sql .= ' LIMIT ' . esc_sql( $value );
118
-	}
119
-
120
-	private function has_match() {
121
-		if ( ! $this->request->has_param( 'has_match' ) ) {
122
-			return;
123
-		}
124
-
125
-		$value = (bool) $this->request->get_param( 'has_match' );
126
-
127
-		if ( $value ) {
128
-			$this->sql .= ' AND e.about_jsonld IS NOT NULL';
129
-		} else {
130
-			$this->sql .= ' AND e.about_jsonld IS NULL';
131
-		}
132
-	}
133
-
134
-	private function sort() {
135
-		switch ( $this->direction . '$' . $this->sort ) {
136
-			case 'ASCENDING$ASC':
137
-			case 'DESCENDING$DESC':
138
-				$sort = 'ASC';
139
-				break;
140
-			case 'ASCENDING$DESC':
141
-			case 'DESCENDING$ASC':
142
-				$sort = 'DESC';
143
-				break;
144
-		}
145
-
146
-		$this->sql .= ' ORDER BY t.' . $this->sortby . ' ' . $sort;
147
-	}
148
-
149
-	private function cursor() {
150
-		if ( ! isset( $this->position ) ) {
151
-			return;
152
-		}
153
-
154
-		switch ( $this->direction . '$' . $this->sort ) {
155
-			case 'ASCENDING$ASC':
156
-			case 'DESCENDING$DESC':
157
-				$condition = '>';
158
-				break;
159
-			case 'ASCENDING$DESC':
160
-			case 'DESCENDING$ASC':
161
-				$condition = '<';
162
-				break;
163
-		}
164
-
165
-		$condition .= ( $this->element === 'INCLUDED' ? '=' : '' );
166
-		global $wpdb;
167
-		// We control the vars in this method.
168
-		// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
169
-		$this->sql .= $wpdb->prepare( ' AND t.' . esc_sql( $this->sortby ) . ' ' . $condition . ' %s', $this->position );
170
-	}
171
-
172
-	private function set_sort() {
173
-		$sortby_to_col = array(
174
-			// sort param  col
175
-			'term_name' => 'name',
176
-		);
177
-
178
-		$value = $this->request->has_param( 'sort' )
179
-			? $this->request->get_param( 'sort' )
180
-			: '+term_name';
181
-
182
-		$sortby       = substr( $value, 1 );
183
-		$this->sortby = isset( $sortby_to_col[ $sortby ] ) ? $sortby_to_col[ $sortby ] : $sortby;
184
-		$this->sort   = substr( $value, 0, 1 ) === '+' ? 'ASC' : 'DESC';
185
-	}
186
-
187
-	private function term_contains() {
188
-		if ( ! $this->request->has_param( 'term_contains' ) ) {
189
-			return;
190
-		}
191
-
192
-		global $wpdb;
193
-		$value      = $this->request->get_param( 'term_contains' );
194
-		$this->sql .= $wpdb->prepare( ' and t.name LIKE %s', '%' . esc_sql( $value ) . '%' );
195
-	}
196
-
197
-	private function taxonomies() {
198
-		$taxonomies = $this->request->has_param( 'taxonomies' )
199
-			? (array) $this->request->get_param( 'taxonomies' )
200
-			: array( 'post_tag', 'category' );
201
-		$value      = array_map( 'esc_sql', $taxonomies );
202
-		$this->sql .= " AND tt.taxonomy IN ( '" . implode( "', '", $value ) . "' )";
203
-	}
69
+        $this->cursor();
70
+        $this->has_match();
71
+        $this->term_contains();
72
+        $this->taxonomies();
73
+        $this->sort();
74
+        $this->limit();
75
+
76
+    }
77
+
78
+    public function get_results() {
79
+        global $wpdb;
80
+
81
+        // The `sql` is prepared in each delegated function in this class.
82
+        // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
83
+        $items = $wpdb->get_results( $this->sql );
84
+
85
+        $sort = ( $this->sort === 'ASC' ? SORT_ASC : SORT_DESC );
86
+        array_multisort( array_column( $items, $this->cursor_sort->get_sort_property() ), $sort, $items );
87
+        $items = array_map( array( $this, 'map_item' ), $items );
88
+
89
+        return $items;
90
+    }
91
+
92
+    public function map_item( $item ) {
93
+        $item->match_name = $this->get_match_name( $item->match_jsonld );
94
+
95
+        return $item;
96
+    }
97
+
98
+    private function get_match_name( $jsonld ) {
99
+        $data = json_decode( $jsonld, true );
100
+        if ( ! $data || ! array_key_exists( 'name', $data ) ) {
101
+            return null;
102
+        }
103
+
104
+        return $data['name'];
105
+    }
106
+
107
+    private function post_types() {
108
+        $post_types = $this->request->has_param( 'post_types' )
109
+            ? (array) $this->request->get_param( 'post_types' )
110
+            : array( 'post', 'page' );
111
+        $value      = array_map( 'esc_sql', $post_types );
112
+        $this->sql .= " AND p.post_type IN ( '" . implode( "', '", $value ) . "' )";
113
+    }
114
+
115
+    private function limit() {
116
+        $value      = is_numeric( $this->limit ) ? $this->limit : 10;
117
+        $this->sql .= ' LIMIT ' . esc_sql( $value );
118
+    }
119
+
120
+    private function has_match() {
121
+        if ( ! $this->request->has_param( 'has_match' ) ) {
122
+            return;
123
+        }
124
+
125
+        $value = (bool) $this->request->get_param( 'has_match' );
126
+
127
+        if ( $value ) {
128
+            $this->sql .= ' AND e.about_jsonld IS NOT NULL';
129
+        } else {
130
+            $this->sql .= ' AND e.about_jsonld IS NULL';
131
+        }
132
+    }
133
+
134
+    private function sort() {
135
+        switch ( $this->direction . '$' . $this->sort ) {
136
+            case 'ASCENDING$ASC':
137
+            case 'DESCENDING$DESC':
138
+                $sort = 'ASC';
139
+                break;
140
+            case 'ASCENDING$DESC':
141
+            case 'DESCENDING$ASC':
142
+                $sort = 'DESC';
143
+                break;
144
+        }
145
+
146
+        $this->sql .= ' ORDER BY t.' . $this->sortby . ' ' . $sort;
147
+    }
148
+
149
+    private function cursor() {
150
+        if ( ! isset( $this->position ) ) {
151
+            return;
152
+        }
153
+
154
+        switch ( $this->direction . '$' . $this->sort ) {
155
+            case 'ASCENDING$ASC':
156
+            case 'DESCENDING$DESC':
157
+                $condition = '>';
158
+                break;
159
+            case 'ASCENDING$DESC':
160
+            case 'DESCENDING$ASC':
161
+                $condition = '<';
162
+                break;
163
+        }
164
+
165
+        $condition .= ( $this->element === 'INCLUDED' ? '=' : '' );
166
+        global $wpdb;
167
+        // We control the vars in this method.
168
+        // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
169
+        $this->sql .= $wpdb->prepare( ' AND t.' . esc_sql( $this->sortby ) . ' ' . $condition . ' %s', $this->position );
170
+    }
171
+
172
+    private function set_sort() {
173
+        $sortby_to_col = array(
174
+            // sort param  col
175
+            'term_name' => 'name',
176
+        );
177
+
178
+        $value = $this->request->has_param( 'sort' )
179
+            ? $this->request->get_param( 'sort' )
180
+            : '+term_name';
181
+
182
+        $sortby       = substr( $value, 1 );
183
+        $this->sortby = isset( $sortby_to_col[ $sortby ] ) ? $sortby_to_col[ $sortby ] : $sortby;
184
+        $this->sort   = substr( $value, 0, 1 ) === '+' ? 'ASC' : 'DESC';
185
+    }
186
+
187
+    private function term_contains() {
188
+        if ( ! $this->request->has_param( 'term_contains' ) ) {
189
+            return;
190
+        }
191
+
192
+        global $wpdb;
193
+        $value      = $this->request->get_param( 'term_contains' );
194
+        $this->sql .= $wpdb->prepare( ' and t.name LIKE %s', '%' . esc_sql( $value ) . '%' );
195
+    }
196
+
197
+    private function taxonomies() {
198
+        $taxonomies = $this->request->has_param( 'taxonomies' )
199
+            ? (array) $this->request->get_param( 'taxonomies' )
200
+            : array( 'post_tag', 'category' );
201
+        $value      = array_map( 'esc_sql', $taxonomies );
202
+        $this->sql .= " AND tt.taxonomy IN ( '" . implode( "', '", $value ) . "' )";
203
+    }
204 204
 
205 205
 }
Please login to merge, or discard this patch.