: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
namespace NetworkPosts\DB\Category;
use NetworkPosts\db\NetsPostsDbHelper;
class NetsPostsCategoryQueryBuilder {
private $relationships_table;
private $taxonomy = array();
private $taxonomy_types = array();
public function __construct( string $base_prefix, int $blog_id = 1 ) {
$this->base_prefix = $base_prefix;
$this->set_blog_id( $blog_id );
public function set_blog_id( int $id ): void {
$this->terms_table = NetsPostsDbHelper::make_table_name( $this->base_prefix, 'terms', $this->blog_id );
$this->taxonomy_table = NetsPostsDbHelper::make_table_name( $this->base_prefix, 'term_taxonomy', $this->blog_id );
$this->relationships_table = NetsPostsDbHelper::make_table_name( $this->base_prefix, 'term_relationships', $this->blog_id );
* @param string[] $taxonomy
public function set_taxonomy( array $taxonomy ): void {
$this->taxonomy = $taxonomy;
public function set_taxonomy_types( array $types ): void {
$this->taxonomy_types = $types;
public function build( int $mode = CategoryInclusionMode::INCLUDE_ANY ): string {
$base_query = $this->build_base_query( $columns );
$where = $this->build_where_condition();
return $base_query . ' ' . $where;
protected function build_base_query( array $columns ): string {
$tables = $this->join_tables();
$base_select_query = 'SELECT %1$s, %2$d as blog_id FROM %3$s %4$s';
$column_str = join( ',', $columns );
protected function join_tables(): string {
$query = 'INNER JOIN %2$s ON %1$s.term_id = %2$s.term_id
INNER JOIN %3$s ON %2$s.term_taxonomy_id = %3$s.term_taxonomy_id';
$this->relationships_table
protected function build_where_condition(): string {
if( $this->taxonomy_types ){
$conditions[] = $this->build_taxonomy_types_filter();
$condition = $this->build_inclusion();
if( $this->mode !== CategoryInclusionMode::INCLUDE_ANY ) {
$condition .= $this->build_strict_inclusion();
$conditions[] = $condition;
return 'WHERE ' . implode( ' AND ', $conditions );
protected function build_taxonomy_types_filter() {
$taxonomy_types = array_map( function( $type ){ return "'$type'"; }, $this->taxonomy_types );
$types = join( ',', $taxonomy_types );
return '(taxonomy IN (' . $types . '))';
protected function build_inclusion(): string {
foreach ( $this->taxonomy as $taxonomy ){
$parts[] = "(LOWER(slug) LIKE '%$taxonomy%')";
$query .= join( ' OR ', $parts ) . ')';
protected function build_strict_inclusion(): string {
$category_count = count( $this->taxonomy );
$relationship_table = $this->relationships_table;
return "GROUP BY $relationship_table.object_id HAVING COUNT(*) = $category_count;";