Skip to content

Commit

Permalink
Merge pull request #1203 from xwp/bugfix/deprecated-mysql-fix
Browse files Browse the repository at this point in the history
SQL_CALC_FOUND_ROWS statements refactored
  • Loading branch information
kidunot89 committed Jan 5, 2021
2 parents d8be280 + be2ff03 commit b8a08b5
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions classes/class-query.php
Expand Up @@ -229,7 +229,7 @@ public function query( $args ) {
/**
* BUILD THE FINAL QUERY
*/
$query = "SELECT SQL_CALC_FOUND_ROWS {$select}
$query = "SELECT {$select}
FROM $wpdb->stream
{$join}
WHERE 1=1 {$where}
Expand All @@ -246,12 +246,29 @@ public function query( $args ) {
*/
$query = apply_filters( 'wp_stream_db_query', $query, $args );

$result = array();
// Build result count query.
$count_query = "SELECT COUNT(*) as found
FROM $wpdb->stream
{$join}
WHERE 1=1 {$where}";

/**
* Filter allows the result count query to be modified before execution.
*
* @param string $query
* @param array $args
*
* @return string
*/
$count_query = apply_filters( 'wp_stream_db_count_query', $count_query, $args );

/**
* QUERY THE DATABASE FOR RESULTS
*/
$result['items'] = $wpdb->get_results( $query ); // @codingStandardsIgnoreLine $query already prepared
$result['count'] = $result['items'] ? absint( $wpdb->get_var( 'SELECT FOUND_ROWS()' ) ) : 0;
$result = array(
'items' => $wpdb->get_results( $query ), // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
'count' => absint( $wpdb->get_var( $count_query ) ), // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
);

return $result;
}
Expand Down

0 comments on commit b8a08b5

Please sign in to comment.