posts} {$where['sql']} ORDER BY {$orderby} {$order}"; $params = $where['params']; $number = (int) $args['number']; if ( $number > 0 ) { $sql .= ' LIMIT %d OFFSET %d'; $params[] = $number; $params[] = max( 0, (int) $args['offset'] ); } // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared $sql = $wpdb->prepare( $sql, $params ); // phpcs:disable PluginCheck.Security.DirectDB.UnescapedDBParameter, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared -- Table name is a fixed value from ACSCW_Install, not user input; every value is bound through $wpdb->prepare(). Direct query is required and results are intentionally uncached. $rows = $wpdb->get_results( $sql, ARRAY_A ); // phpcs:enable PluginCheck.Security.DirectDB.UnescapedDBParameter, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared $items = array(); foreach ( (array) $rows as $row ) { $kind = self::detect_kind( (string) $row['post_content'] ); if ( ! self::kind_matches( $kind, $args['kind'] ) ) { continue; } $items[] = array( 'id' => (int) $row['ID'], 'title' => (string) $row['post_title'], 'post_type' => (string) $row['post_type'], 'status' => (string) $row['post_status'], 'date' => (string) $row['post_date'], 'kind' => $kind, ); } return $items; } /** * 条件に一致する件数を返す。 * * 種別(Paywall / 投げ銭)は本文を見ないと判定できないため、 * 件数も同じ経路で数える。 * * @param array $args query_items() と同じ条件。 * @return int */ public static function count_items( $args = array() ) { $args = wp_parse_args( $args, self::get_default_args() ); $args['number'] = 0; $args['offset'] = 0; return count( self::query_items( $args ) ); } /** * 既定の検索条件。 * * @return array */ private static function get_default_args() { return array( 'kind' => 'all', 'post_type' => '', 'status' => '', 'search' => '', 'number' => 0, 'offset' => 0, 'orderby' => 'post_date', 'order' => 'DESC', ); } /** * WHERE 句を組み立てる。 * * @param array $args 検索条件。 * @return array{sql:string,params:array} */ private static function build_where( $args ) { global $wpdb; $clauses = array(); $params = array(); $post_types = ACSCW_Products::get_supported_post_types(); if ( '' !== (string) $args['post_type'] && in_array( $args['post_type'], $post_types, true ) ) { $post_types = array( $args['post_type'] ); } $clauses[] = 'post_type IN ( ' . implode( ', ', array_fill( 0, count( $post_types ), '%s' ) ) . ' )'; $params = array_merge( $params, array_values( $post_types ) ); $statuses = self::get_statuses(); if ( isset( $statuses[ $args['status'] ] ) ) { $statuses = array( $args['status'] => '' ); } $keys = array_keys( $statuses ); $clauses[] = 'post_status IN ( ' . implode( ', ', array_fill( 0, count( $keys ), '%s' ) ) . ' )'; $params = array_merge( $params, $keys ); // 有料エリアか投げ銭のブロックを含む本文だけを対象にする。 $clauses[] = '( post_content LIKE %s OR post_content LIKE %s )'; $params[] = '%' . $wpdb->esc_like( '