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[] = '%' . $this->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 = $this->where_values; $limit_query = ''; if ( -1 !== $per_page ) { $limit_query = ' LIMIT %d OFFSET %d '; $values[] = (int) $this->per_page; $values[] = (int) $this->offset; } $query = " SELECT l.* FROM {$this->table} l " . ( empty( $this->by_meta ) ? '' : $this->join() ) . ' ' . $this->where_sql() . " GROUP BY l.ID ORDER BY l.ID DESC {$limit_query} "; // $query only contains table names and %-placeholders; every value is bound here. // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $this->lessons = $this->wpdb->get_results( $values ? $this->wpdb->prepare( $query, $values ) : $query, // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared ARRAY_A ) ?? []; $this->load_meta(); $this->total = count( $this ); $this->total_pages = (int) ceil( $this->total / $this->per_page ); } public function join(): string { return " JOIN {$this->meta_table} lm ON l.ID = lm.lesson_id "; } public function getIterator(): ArrayIterator { return new ArrayIterator( $this->lessons ); } public function load_meta(): void { $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 ? [] : $this->wpdb->get_results( $this->wpdb->prepare(// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared "SELECT * FROM {$this->meta_table} WHERE lesson_id IN ({$placeholders})", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared ...$ids// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared ), 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 { $query = " SELECT COUNT( DISTINCT l.ID ) FROM {$this->table} l " . ( empty( $this->by_meta ) ? '' : $this->join() ) . ' ' . $this->where_sql(); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching return (int) ( $this->wpdb->get_var( $this->where_values ? $this->wpdb->prepare( $query, $this->where_values ) : $query // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared ) ?? 0 ); } /** * WHERE clause made only of fixed column names and placeholders. */ protected function where_sql(): string { return empty( $this->where ) ? '' : 'WHERE ' . implode( ' AND ', $this->where ); } }