Code Duplication    Length = 62-62 lines in 2 locations

json-endpoints/class.wpcom-json-api-list-media-v1-1-endpoint.php 1 location

@@ 144-205 (lines=62) @@
141
		return build_query( array( 'value' => urlencode( $post->$column ), 'id' => $post->ID ) );
142
	}
143
144
	function handle_where_for_page_handle( $where ) {
145
		global $wpdb;
146
147
		$column = $this->performed_query['orderby'];
148
		if ( ! $column ) {
149
			$column = 'date';
150
		}
151
		$order = $this->performed_query['order'];
152
		if ( ! $order ) {
153
			$order = 'DESC';
154
		}
155
156
		if ( ! in_array( $column, array( 'ID', 'title', 'date', 'modified', 'comment_count' ) ) ) {
157
			return $where;
158
		}
159
160
		if ( ! in_array( $order, array( 'DESC', 'ASC' ) ) ) {
161
			return $where;
162
		}
163
164
		$db_column = '';
165
		$db_value = '';
166
		switch( $column ) {
167
			case 'ID':
168
				$db_column = 'ID';
169
				$db_value = '%d';
170
				break;
171
			case 'title':
172
				$db_column = 'post_title';
173
				$db_value = '%s';
174
				break;
175
			case 'date':
176
				$db_column = 'post_date';
177
				$db_value = 'CAST( %s as DATETIME )';
178
				break;
179
			case 'modified':
180
				$db_column = 'post_modified';
181
				$db_value = 'CAST( %s as DATETIME )';
182
				break;
183
			case 'comment_count':
184
				$db_column = 'comment_count';
185
				$db_value = '%d';
186
				break;
187
		}
188
189
		if ( 'DESC'=== $order ) {
190
			$db_order = '<';
191
		} else {
192
			$db_order = '>';
193
		}
194
195
		// Add a clause that limits the results to items beyond the passed item, or equivalent to the passed item
196
		// but with an ID beyond the passed item. When we're ordering by the ID already, we only ask for items
197
		// beyond the passed item.
198
		$where .= $wpdb->prepare( " AND ( ( `$wpdb->posts`.`$db_column` $db_order $db_value ) ", $this->page_handle['value'] );
199
		if ( $db_column !== 'ID' ) {
200
			$where .= $wpdb->prepare( "OR ( `$wpdb->posts`.`$db_column` = $db_value AND `$wpdb->posts`.ID $db_order %d )", $this->page_handle['value'], $this->page_handle['id'] );
201
		}
202
		$where .= ' )';
203
204
		return $where;
205
	}
206
207
	function handle_date_range( $where ) {
208
		global $wpdb;

json-endpoints/class.wpcom-json-api-list-posts-v1-1-endpoint.php 1 location

@@ 393-454 (lines=62) @@
390
		return $this->_build_date_range_query( 'post_modified_gmt', $this->modified_range, $where );
391
	}
392
393
	function handle_where_for_page_handle( $where ) {
394
		global $wpdb;
395
396
		$column = $this->performed_query['orderby'];
397
		if ( ! $column ) {
398
			$column = 'date';
399
		}
400
		$order = $this->performed_query['order'];
401
		if ( ! $order ) {
402
			$order = 'DESC';
403
		}
404
405
		if ( ! in_array( $column, array( 'ID', 'title', 'date', 'modified', 'comment_count' ) ) ) {
406
			return $where;
407
		}
408
409
		if ( ! in_array( $order, array( 'DESC', 'ASC' ) ) ) {
410
			return $where;
411
		}
412
413
		$db_column = '';
414
		$db_value = '';
415
		switch( $column ) {
416
			case 'ID':
417
				$db_column = 'ID';
418
				$db_value = '%d';
419
				break;
420
			case 'title':
421
				$db_column = 'post_title';
422
				$db_value = '%s';
423
				break;
424
			case 'date':
425
				$db_column = 'post_date';
426
				$db_value = 'CAST( %s as DATETIME )';
427
				break;
428
			case 'modified':
429
				$db_column = 'post_modified';
430
				$db_value = 'CAST( %s as DATETIME )';
431
				break;
432
			case 'comment_count':
433
				$db_column = 'comment_count';
434
				$db_value = '%d';
435
				break;
436
		}
437
438
		if ( 'DESC'=== $order ) {
439
			$db_order = '<';
440
		} else {
441
			$db_order = '>';
442
		}
443
444
		// Add a clause that limits the results to items beyond the passed item, or equivalent to the passed item
445
		// but with an ID beyond the passed item. When we're ordering by the ID already, we only ask for items
446
		// beyond the passed item.
447
		$where .= $wpdb->prepare( " AND ( ( `$wpdb->posts`.`$db_column` $db_order $db_value ) ", $this->page_handle['value'] );
448
		if ( $db_column !== 'ID' ) {
449
			$where .= $wpdb->prepare( "OR ( `$wpdb->posts`.`$db_column` = $db_value AND `$wpdb->posts`.ID $db_order %d )", $this->page_handle['value'], $this->page_handle['id'] );
450
		}
451
		$where .= ' )';
452
453
		return $where;
454
	}
455
456
	function handle_orderby_for_page_handle( $orderby ) {
457
		global $wpdb;