page = absint( $page ); $this->per_page = $per_page; $this->offset = absint( ( $this->page - 1 ) * $this->per_page ); $this->skip_meta = $skip_meta; $this->by_meta = $by_meta; if ( ! empty( $author_id ) ) { $this->where[] = 'lesson_author = %d'; $this->where_values[] = (int) $author_id; } if ( ! empty( $search ) ) { $this->where[] = 'lesson_title LIKE %s'; $this->where_values[] = '%' . $wpdb->esc_like( $search ) . '%'; } if ( ! empty( $status ) ) { $this->where[] = 'lesson_status = %s'; $this->where_values[] = $status; } $this->by_meta = array_merge( $by_meta, apply_filters( 'academy/lesson/meta_query', [] ) ); if ( ! empty( $this->by_meta ) ) { $ins = new HpWhere( $this->by_meta, 'AND', 'lm' ); if ( '' !== $ins->query() ) { $this->where[] = '( ' . $ins->query() . ' )'; $this->where_values = array_merge( $this->where_values, $ins->values() ); } } $values = array_merge( $this->table_values(), $this->where_values ); $limit_query = ''; if ( -1 !== $per_page ) { $limit_query = ' LIMIT %d OFFSET %d '; $values[] = (int) $this->per_page; $values[] = (int) $this->offset; } // Only %i / %d / %s placeholders are interpolated (join(), where_sql()); every value is bound here. // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQL.NotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter -- join()/where_sql() return only %i/%d/%s placeholders; values are bound by prepare(). $this->lessons = $wpdb->get_results( $wpdb->prepare( 'SELECT l.* FROM %i l ' . $this->join() . ' ' . $this->where_sql() . ' GROUP BY l.ID ORDER BY l.ID DESC' . $limit_query, $values ), ARRAY_A ) ?? []; // phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQL.NotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter $this->load_meta(); $this->total = count( $this ); $this->total_pages = (int) ceil( $this->total / $this->per_page ); } /** * JOIN on the meta table, as a %i placeholder (see table_values()). */ public function join(): string { return empty( $this->by_meta ) ? '' : ' JOIN %i lm ON l.ID = lm.lesson_id '; } /** * Values for the %i table placeholders, in query order. */ protected function table_values(): array { return empty( $this->by_meta ) ? array( $this->table ) : array( $this->table, $this->meta_table ); } public function getIterator(): ArrayIterator { return new ArrayIterator( $this->lessons ); } public function load_meta(): void { global $wpdb; $ids = array_map( 'absint', array_column( $this->lessons, 'ID' ) ); if ( empty( $ids ) ) { $this->lessons = []; return; } $placeholders = implode( ',', array_fill( 0, count( $ids ), '%d' ) ); // phpcs:disable WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare, WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber -- placeholders are generated to match the values $meta_data = $this->skip_meta ? [] // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- custom lesson table / one-off lesson migration query; no WP API equivalent. : $wpdb->get_results( $wpdb->prepare(// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared "SELECT * FROM %i WHERE lesson_id IN ({$placeholders})", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- $placeholders is a generated list of %d. $this->meta_table, ...$ids ), ARRAY_A ) ?? []; // phpcs:enable WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare, WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber foreach ( $meta_data as $meta ) { $this->meta_data[ $meta['lesson_id'] ][ $meta['meta_key'] ] = $meta['meta_value']; } $lessons = []; foreach ( $this->lessons as $lesson ) { $lesson['meta'] = $this->meta_data[ $lesson['ID'] ] ?? []; $lessons[] = new HpLesson( $lesson, $lesson['meta'] ); } $this->lessons = $lessons; } public function count(): int { global $wpdb; // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQL.NotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter -- join()/where_sql() return only %i/%d/%s placeholders; values are bound by prepare(). return (int) ( $wpdb->get_var( $wpdb->prepare( 'SELECT COUNT( DISTINCT l.ID ) FROM %i l ' . $this->join() . ' ' . $this->where_sql(), array_merge( $this->table_values(), $this->where_values ) ) ) ?? 0 ); // phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQL.NotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter } /** * WHERE clause made only of fixed column names and placeholders. */ protected function where_sql(): string { return empty( $this->where ) ? '' : 'WHERE ' . implode( ' AND ', $this->where ); } }