Code Duplication    Length = 62-62 lines in 2 locations

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

@@ 368-429 (lines=62) @@
365
		return $this->_build_date_range_query( 'post_modified_gmt', $this->modified_range, $where );
366
	}
367
368
	function handle_where_for_page_handle( $where ) {
369
		global $wpdb;
370
371
		$column = $this->performed_query['orderby'];
372
		if ( ! $column ) {
373
			$column = 'date';
374
		}
375
		$order = $this->performed_query['order'];
376
		if ( ! $order ) {
377
			$order = 'DESC';
378
		}
379
380
		if ( ! in_array( $column, array( 'ID', 'title', 'date', 'modified', 'comment_count' ) ) ) {
381
			return $where;
382
		}
383
384
		if ( ! in_array( $order, array( 'DESC', 'ASC' ) ) ) {
385
			return $where;
386
		}
387
388
		$db_column = '';
389
		$db_value = '';
390
		switch( $column ) {
391
			case 'ID':
392
				$db_column = 'ID';
393
				$db_value = '%d';
394
				break;
395
			case 'title':
396
				$db_column = 'post_title';
397
				$db_value = '%s';
398
				break;
399
			case 'date':
400
				$db_column = 'post_date';
401
				$db_value = 'CAST( %s as DATETIME )';
402
				break;
403
			case 'modified':
404
				$db_column = 'post_modified';
405
				$db_value = 'CAST( %s as DATETIME )';
406
				break;
407
			case 'comment_count':
408
				$db_column = 'comment_count';
409
				$db_value = '%d';
410
				break;
411
		}
412
413
		if ( 'DESC'=== $order ) {
414
			$db_order = '<';
415
		} else {
416
			$db_order = '>';
417
		}
418
419
		// Add a clause that limits the results to items beyond the passed item, or equivalent to the passed item
420
		// but with an ID beyond the passed item. When we're ordering by the ID already, we only ask for items
421
		// beyond the passed item.
422
		$where .= $wpdb->prepare( " AND ( ( `$wpdb->posts`.`$db_column` $db_order $db_value ) ", $this->page_handle['value'] );
423
		if ( $db_column !== 'ID' ) {
424
			$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'] );
425
		}
426
		$where .= ' )';
427
428
		return $where;
429
	}
430
431
	function handle_orderby_for_page_handle( $orderby ) {
432
		global $wpdb;

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

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