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

@@ 449-510 (lines=62) @@
446
		return $this->_build_date_range_query( 'post_modified_gmt', $this->modified_range, $where );
447
	}
448
449
	function handle_where_for_page_handle( $where ) {
450
		global $wpdb;
451
452
		$column = $this->performed_query['orderby'];
453
		if ( ! $column ) {
454
			$column = 'date';
455
		}
456
		$order = $this->performed_query['order'];
457
		if ( ! $order ) {
458
			$order = 'DESC';
459
		}
460
461
		if ( ! in_array( $column, array( 'ID', 'title', 'date', 'modified', 'comment_count' ) ) ) {
462
			return $where;
463
		}
464
465
		if ( ! in_array( $order, array( 'DESC', 'ASC' ) ) ) {
466
			return $where;
467
		}
468
469
		$db_column = '';
470
		$db_value = '';
471
		switch( $column ) {
472
			case 'ID':
473
				$db_column = 'ID';
474
				$db_value = '%d';
475
				break;
476
			case 'title':
477
				$db_column = 'post_title';
478
				$db_value = '%s';
479
				break;
480
			case 'date':
481
				$db_column = 'post_date';
482
				$db_value = 'CAST( %s as DATETIME )';
483
				break;
484
			case 'modified':
485
				$db_column = 'post_modified';
486
				$db_value = 'CAST( %s as DATETIME )';
487
				break;
488
			case 'comment_count':
489
				$db_column = 'comment_count';
490
				$db_value = '%d';
491
				break;
492
		}
493
494
		if ( 'DESC'=== $order ) {
495
			$db_order = '<';
496
		} else {
497
			$db_order = '>';
498
		}
499
500
		// Add a clause that limits the results to items beyond the passed item, or equivalent to the passed item
501
		// but with an ID beyond the passed item. When we're ordering by the ID already, we only ask for items
502
		// beyond the passed item.
503
		$where .= $wpdb->prepare( " AND ( ( `$wpdb->posts`.`$db_column` $db_order $db_value ) ", $this->page_handle['value'] );
504
		if ( $db_column !== 'ID' ) {
505
			$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'] );
506
		}
507
		$where .= ' )';
508
509
		return $where;
510
	}
511
512
	function handle_orderby_for_page_handle( $orderby ) {
513
		global $wpdb;