Skip to content

Commit

Permalink
Merge pull request #622 from GatherPress/GP-614
Browse files Browse the repository at this point in the history
Fix SQL calls with %i placeholder
  • Loading branch information
mauteri authored Mar 27, 2024
2 parents 92a9ad5 + a75e4a6 commit d3946c7
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 8 deletions.
4 changes: 2 additions & 2 deletions includes/core/classes/class-event-query.php
Original file line number Diff line number Diff line change
Expand Up @@ -337,9 +337,9 @@ public function adjust_event_sql( array $pieces, string $type = 'all', string $o
$current = gmdate( Event::DATETIME_FORMAT, time() );

if ( 'upcoming' === $type ) {
$pieces['where'] .= $wpdb->prepare( ' AND ' . esc_sql( $table ) . '.datetime_end_gmt >= %s', esc_sql( $current ) );
$pieces['where'] .= $wpdb->prepare( ' AND %i.datetime_end_gmt >= %s', $table, $current ); // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.UnsupportedIdentifierPlaceholder
} elseif ( 'past' === $type ) {
$pieces['where'] .= $wpdb->prepare( ' AND ' . esc_sql( $table ) . '.datetime_end_gmt < %s', esc_sql( $current ) );
$pieces['where'] .= $wpdb->prepare( ' AND %i.datetime_end_gmt < %s', $table, $current ); // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.UnsupportedIdentifierPlaceholder
}

return $pieces;
Expand Down
5 changes: 3 additions & 2 deletions includes/core/classes/class-event.php
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ public function get_datetime(): array {

if ( empty( $data ) || ! is_array( $data ) ) {
$table = sprintf( static::TABLE_FORMAT, $wpdb->prefix );
$data = (array) $wpdb->get_results( $wpdb->prepare( 'SELECT datetime_start, datetime_start_gmt, datetime_end, datetime_end_gmt, timezone FROM ' . esc_sql( $table ) . ' WHERE post_id = %d LIMIT 1', $this->event->ID ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$data = (array) $wpdb->get_results( $wpdb->prepare( 'SELECT datetime_start, datetime_start_gmt, datetime_end, datetime_end_gmt, timezone FROM %i WHERE post_id = %d LIMIT 1', $table, $this->event->ID ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQLPlaceholders.UnsupportedIdentifierPlaceholder
$data = ( ! empty( $data ) ) ? (array) current( $data ) : array();

set_transient( $cache_key, $data, 15 * MINUTE_IN_SECONDS );
Expand Down Expand Up @@ -666,7 +666,8 @@ function ( $key ) {
// @todo Add caching to this and create new method to check existence.
$exists = $wpdb->get_var( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$wpdb->prepare(
'SELECT post_id FROM ' . esc_sql( $table ) . ' WHERE post_id = %d',
'SELECT post_id FROM %i WHERE post_id = %d', // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.UnsupportedIdentifierPlaceholder
$table,
$fields['post_id']
)
);
Expand Down
4 changes: 2 additions & 2 deletions includes/core/classes/class-rsvp.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public function get( int $user_id ): array {
$table = sprintf( static::TABLE_FORMAT, $wpdb->prefix );

// @todo Consider implementing caching for improved performance in the future.
$data = $wpdb->get_row( $wpdb->prepare( 'SELECT id, timestamp, status, guests, anonymous FROM ' . esc_sql( $table ) . ' WHERE post_id = %d AND user_id = %d', $post_id, $user_id ), ARRAY_A ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$data = $wpdb->get_row( $wpdb->prepare( 'SELECT id, timestamp, status, guests, anonymous FROM %i WHERE post_id = %d AND user_id = %d', $table, $post_id, $user_id ), ARRAY_A ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQLPlaceholders.UnsupportedIdentifierPlaceholder

return array_merge( $default, (array) $data );
}
Expand Down Expand Up @@ -347,7 +347,7 @@ public function responses(): array {
$site_users = count_users();
$total_users = $site_users['total_users'];
$table = sprintf( static::TABLE_FORMAT, $wpdb->prefix );
$data = (array) $wpdb->get_results( $wpdb->prepare( 'SELECT user_id, timestamp, status, guests, anonymous FROM ' . esc_sql( $table ) . ' WHERE post_id = %d LIMIT %d', $post_id, $total_users ), ARRAY_A ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
$data = (array) $wpdb->get_results( $wpdb->prepare( 'SELECT user_id, timestamp, status, guests, anonymous FROM %i WHERE post_id = %d LIMIT %d', $table, $post_id, $total_users ), ARRAY_A ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.PreparedSQLPlaceholders.UnsupportedIdentifierPlaceholder
$data = ( ! empty( $data ) ) ? (array) $data : array();
$responses = array();
$all_guests = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,11 +239,11 @@ public function test_adjust_event_sql(): void {
$retval = $instance->adjust_event_sql( array(), 'past', 'desc' );

$this->assertStringContainsString( 'DESC', $retval['orderby'] );
$this->assertStringContainsString( "AND {$table}.datetime_end_gmt <", $retval['where'] );
$this->assertStringContainsString( "AND `{$table}`.datetime_end_gmt <", $retval['where'] );

$retval = $instance->adjust_event_sql( array(), 'upcoming', 'ASC' );

$this->assertStringContainsString( 'ASC', $retval['orderby'] );
$this->assertStringContainsString( "AND {$table}.datetime_end_gmt >=", $retval['where'] );
$this->assertStringContainsString( "AND `{$table}`.datetime_end_gmt >=", $retval['where'] );
}
}

0 comments on commit d3946c7

Please sign in to comment.