Code Duplication    Length = 62-62 lines in 2 locations

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

@@ 178-239 (lines=62) @@
175
		return build_query( array( 'value' => urlencode( $post->$column ), 'id' => $post->ID ) );
176
	}
177
178
	function handle_where_for_page_handle( $where ) {
179
		global $wpdb;
180
181
		$column = $this->performed_query['orderby'];
182
		if ( ! $column ) {
183
			$column = 'date';
184
		}
185
		$order = $this->performed_query['order'];
186
		if ( ! $order ) {
187
			$order = 'DESC';
188
		}
189
190
		if ( ! in_array( $column, array( 'ID', 'title', 'date', 'modified', 'comment_count' ) ) ) {
191
			return $where;
192
		}
193
194
		if ( ! in_array( $order, array( 'DESC', 'ASC' ) ) ) {
195
			return $where;
196
		}
197
198
		$db_column = '';
199
		$db_value = '';
200
		switch( $column ) {
201
			case 'ID':
202
				$db_column = 'ID';
203
				$db_value = '%d';
204
				break;
205
			case 'title':
206
				$db_column = 'post_title';
207
				$db_value = '%s';
208
				break;
209
			case 'date':
210
				$db_column = 'post_date';
211
				$db_value = 'CAST( %s as DATETIME )';
212
				break;
213
			case 'modified':
214
				$db_column = 'post_modified';
215
				$db_value = 'CAST( %s as DATETIME )';
216
				break;
217
			case 'comment_count':
218
				$db_column = 'comment_count';
219
				$db_value = '%d';
220
				break;
221
		}
222
223
		if ( 'DESC'=== $order ) {
224
			$db_order = '<';
225
		} else {
226
			$db_order = '>';
227
		}
228
229
		// Add a clause that limits the results to items beyond the passed item, or equivalent to the passed item
230
		// but with an ID beyond the passed item. When we're ordering by the ID already, we only ask for items
231
		// beyond the passed item.
232
		$where .= $wpdb->prepare( " AND ( ( `$wpdb->posts`.`$db_column` $db_order $db_value ) ", $this->page_handle['value'] );
233
		if ( $db_column !== 'ID' ) {
234
			$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'] );
235
		}
236
		$where .= ' )';
237
238
		return $where;
239
	}
240
241
	function handle_date_range( $where ) {
242
		global $wpdb;

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

@@ 473-534 (lines=62) @@
470
		return $this->_build_date_range_query( 'post_modified_gmt', $this->modified_range, $where );
471
	}
472
473
	function handle_where_for_page_handle( $where ) {
474
		global $wpdb;
475
476
		$column = $this->performed_query['orderby'];
477
		if ( ! $column ) {
478
			$column = 'date';
479
		}
480
		$order = $this->performed_query['order'];
481
		if ( ! $order ) {
482
			$order = 'DESC';
483
		}
484
485
		if ( ! in_array( $column, array( 'ID', 'title', 'date', 'modified', 'comment_count' ) ) ) {
486
			return $where;
487
		}
488
489
		if ( ! in_array( $order, array( 'DESC', 'ASC' ) ) ) {
490
			return $where;
491
		}
492
493
		$db_column = '';
494
		$db_value  = '';
495
		switch ( $column ) {
496
			case 'ID':
497
				$db_column = 'ID';
498
				$db_value  = '%d';
499
				break;
500
			case 'title':
501
				$db_column = 'post_title';
502
				$db_value  = '%s';
503
				break;
504
			case 'date':
505
				$db_column = 'post_date';
506
				$db_value  = 'CAST( %s as DATETIME )';
507
				break;
508
			case 'modified':
509
				$db_column = 'post_modified';
510
				$db_value  = 'CAST( %s as DATETIME )';
511
				break;
512
			case 'comment_count':
513
				$db_column = 'comment_count';
514
				$db_value  = '%d';
515
				break;
516
		}
517
518
		if ( 'DESC' === $order ) {
519
			$db_order = '<';
520
		} else {
521
			$db_order = '>';
522
		}
523
524
		// Add a clause that limits the results to items beyond the passed item, or equivalent to the passed item
525
		// but with an ID beyond the passed item. When we're ordering by the ID already, we only ask for items
526
		// beyond the passed item.
527
		$where .= $wpdb->prepare( " AND ( ( `$wpdb->posts`.`$db_column` $db_order $db_value ) ", $this->page_handle['value'] );
528
		if ( $db_column !== 'ID' ) {
529
			$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'] );
530
		}
531
		$where .= ' )';
532
533
		return $where;
534
	}
535
536
	function handle_orderby_for_page_handle( $orderby ) {
537
		global $wpdb;