Edit File by line

Deprecated: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in /home/sportsfever/public_html/filemanger/function.php on line 93
/home/sportsfe.../httpdocs/wp-conte.../plugins/network-.../componen.../db
File: NetsPostsQueryConditionBuilder.php
<?php
[0] Fix | Delete
[1] Fix | Delete
[2] Fix | Delete
namespace NetworkPosts\Components\db;
[3] Fix | Delete
[4] Fix | Delete
[5] Fix | Delete
use NetworkPosts\db\NetsPostsDbHelper;
[6] Fix | Delete
[7] Fix | Delete
class NetsPostsQueryConditionBuilder {
[8] Fix | Delete
[9] Fix | Delete
protected $blogs = array();
[10] Fix | Delete
protected $prefix = '';
[11] Fix | Delete
protected $current_blog = 1;
[12] Fix | Delete
[13] Fix | Delete
protected $included_posts = array();
[14] Fix | Delete
protected $excluded_posts = array();
[15] Fix | Delete
protected $offset = 0;
[16] Fix | Delete
protected $post_type = array( 'post', 'product' );
[17] Fix | Delete
protected $days = false;
[18] Fix | Delete
protected $after_days = false;
[19] Fix | Delete
protected $author = false;
[20] Fix | Delete
protected $excluded_author = false;
[21] Fix | Delete
protected $title_keywords = array();
[22] Fix | Delete
protected $hide_protected = false;
[23] Fix | Delete
[24] Fix | Delete
public function __construct( string $db_prefix ) {
[25] Fix | Delete
$this->prefix = $db_prefix;
[26] Fix | Delete
}
[27] Fix | Delete
[28] Fix | Delete
public function set_current_blog( int $blog ): void {
[29] Fix | Delete
$this->current_blog = $blog;
[30] Fix | Delete
}
[31] Fix | Delete
[32] Fix | Delete
public function include_blogs( array $blogs ): void {
[33] Fix | Delete
$this->blogs = $blogs;
[34] Fix | Delete
}
[35] Fix | Delete
[36] Fix | Delete
public function get_blogs(): array {
[37] Fix | Delete
return $this->blogs;
[38] Fix | Delete
}
[39] Fix | Delete
[40] Fix | Delete
public function exclude_blog( $blog_id ): void {
[41] Fix | Delete
if ( ! empty( $this->blogs ) ) {
[42] Fix | Delete
$index = array_search( $blog_id, $this->blogs );
[43] Fix | Delete
if ( $index !== false ) {
[44] Fix | Delete
unset( $this->blogs[ $index ] );
[45] Fix | Delete
$this->blogs = array_values( $this->blogs );
[46] Fix | Delete
}
[47] Fix | Delete
}
[48] Fix | Delete
}
[49] Fix | Delete
[50] Fix | Delete
public function include_blog_posts( int $blog_id, array $included_posts ): void {
[51] Fix | Delete
if ( isset( $this->included_posts[ $blog_id ] ) ) {
[52] Fix | Delete
$this->included_posts[ $blog_id ] = array_merge( $this->included_posts[ $blog_id ], $included_posts );
[53] Fix | Delete
} else {
[54] Fix | Delete
$this->included_posts[ $blog_id ] = $included_posts;
[55] Fix | Delete
}
[56] Fix | Delete
}
[57] Fix | Delete
[58] Fix | Delete
public function get_included_posts( int $blog_id ): array {
[59] Fix | Delete
return $this->included_posts[ $blog_id ];
[60] Fix | Delete
}
[61] Fix | Delete
[62] Fix | Delete
public function exclude_posts( int $blog_id, array $excluded_posts ): void {
[63] Fix | Delete
if ( isset( $this->excluded_posts[ $blog_id ] ) ) {
[64] Fix | Delete
$this->excluded_posts[ $blog_id ] = array_merge( $this->excluded_posts[ $blog_id ], $excluded_posts );
[65] Fix | Delete
} else {
[66] Fix | Delete
$this->excluded_posts[ $blog_id ] = $excluded_posts;
[67] Fix | Delete
}
[68] Fix | Delete
}
[69] Fix | Delete
[70] Fix | Delete
public function get_excluded_posts(): array {
[71] Fix | Delete
return $this->excluded_posts;
[72] Fix | Delete
}
[73] Fix | Delete
[74] Fix | Delete
/**
[75] Fix | Delete
* @param int $offset
[76] Fix | Delete
*/
[77] Fix | Delete
public function set_offset( int $offset ): void {
[78] Fix | Delete
$this->offset = $offset;
[79] Fix | Delete
}
[80] Fix | Delete
[81] Fix | Delete
public function get_post_type(): array {
[82] Fix | Delete
if( $this->post_type ) {
[83] Fix | Delete
return $this->post_type;
[84] Fix | Delete
}
[85] Fix | Delete
return false;
[86] Fix | Delete
}
[87] Fix | Delete
[88] Fix | Delete
/**
[89] Fix | Delete
* @param mixed $post_type
[90] Fix | Delete
*/
[91] Fix | Delete
public function set_post_type( $post_type ): void {
[92] Fix | Delete
if ( $post_type == 'any' ) {
[93] Fix | Delete
$this->post_type = false;
[94] Fix | Delete
} else {
[95] Fix | Delete
$this->post_type = $post_type;
[96] Fix | Delete
}
[97] Fix | Delete
}
[98] Fix | Delete
[99] Fix | Delete
public function set_author_id( int $author ): void {
[100] Fix | Delete
$this->author = $author;
[101] Fix | Delete
}
[102] Fix | Delete
[103] Fix | Delete
public function exclude_author_id( int $author ): void {
[104] Fix | Delete
$this->excluded_author = $author;
[105] Fix | Delete
}
[106] Fix | Delete
[107] Fix | Delete
/**
[108] Fix | Delete
* @param string $days
[109] Fix | Delete
*/
[110] Fix | Delete
public function set_days( string $days ): void {
[111] Fix | Delete
$this->days = $days;
[112] Fix | Delete
}
[113] Fix | Delete
[114] Fix | Delete
public function filter_after_days( int $after_days ) {
[115] Fix | Delete
$this->after_days = $after_days;
[116] Fix | Delete
}
[117] Fix | Delete
[118] Fix | Delete
/**
[119] Fix | Delete
* @param array $title_keywords
[120] Fix | Delete
*/
[121] Fix | Delete
public function set_title_keywords( array $title_keywords ): void {
[122] Fix | Delete
$this->title_keywords = $title_keywords;
[123] Fix | Delete
}
[124] Fix | Delete
[125] Fix | Delete
public function exclude_protected(): void {
[126] Fix | Delete
$this->hide_protected = true;
[127] Fix | Delete
}
[128] Fix | Delete
[129] Fix | Delete
public function build(): string {
[130] Fix | Delete
$condition = array();
[131] Fix | Delete
if ( $this->post_type && empty( $this->included_posts ) ) {
[132] Fix | Delete
$condition[] = $this->build_post_type();
[133] Fix | Delete
}
[134] Fix | Delete
if ( $this->days ) {
[135] Fix | Delete
$condition[] = $this->build_days_filter_query();
[136] Fix | Delete
}
[137] Fix | Delete
if ( $this->after_days ) {
[138] Fix | Delete
$condition[] = $this->build_after_days_filter_query();
[139] Fix | Delete
}
[140] Fix | Delete
if( $this->author ) {
[141] Fix | Delete
$condition[] = $this->build_author_inclusion();
[142] Fix | Delete
}
[143] Fix | Delete
if( $this->excluded_author ){
[144] Fix | Delete
$condition[] = $this->build_author_exclusion();
[145] Fix | Delete
}
[146] Fix | Delete
if ( ! empty( $this->included_posts ) ) {
[147] Fix | Delete
$condition[] = $this->build_posts_inclusion();
[148] Fix | Delete
}
[149] Fix | Delete
if ( ! empty( $this->excluded_posts ) ) {
[150] Fix | Delete
$condition[] = $this->build_posts_exclusion();
[151] Fix | Delete
}
[152] Fix | Delete
if ( $this->title_keywords ) {
[153] Fix | Delete
$condition[] = $this->build_title_filter();
[154] Fix | Delete
}
[155] Fix | Delete
if( $this->hide_protected ){
[156] Fix | Delete
$condition[] = $this->filter_protected_posts();
[157] Fix | Delete
}
[158] Fix | Delete
$condition[] = $this->build_post_status();
[159] Fix | Delete
[160] Fix | Delete
return join( ' AND ', $condition );
[161] Fix | Delete
}
[162] Fix | Delete
[163] Fix | Delete
protected function get_posts_table(): string {
[164] Fix | Delete
return NetsPostsDbHelper::make_table_name( $this->prefix, 'posts', $this->current_blog );
[165] Fix | Delete
}
[166] Fix | Delete
[167] Fix | Delete
protected function build_post_type(): string {
[168] Fix | Delete
$post_types = array_map( function ( $type ) {
[169] Fix | Delete
return "post_type='" . esc_sql( $type ) . "'";
[170] Fix | Delete
}, $this->post_type );
[171] Fix | Delete
[172] Fix | Delete
return '(' . join( ' OR ', $post_types ) . ')';
[173] Fix | Delete
[174] Fix | Delete
}
[175] Fix | Delete
[176] Fix | Delete
protected function build_days_filter_query(): string {
[177] Fix | Delete
$days = intval( $this->days );
[178] Fix | Delete
return "(post_date >= DATE_SUB(CURRENT_DATE(), INTERVAL $days DAY))";
[179] Fix | Delete
}
[180] Fix | Delete
[181] Fix | Delete
protected function build_after_days_filter_query(): string {
[182] Fix | Delete
$days = $this->after_days;
[183] Fix | Delete
return "(post_date < DATE_SUB(CURRENT_DATE(), INTERVAL $days DAY))";
[184] Fix | Delete
}
[185] Fix | Delete
[186] Fix | Delete
protected function build_author_inclusion(): string {
[187] Fix | Delete
return "(post_author = {$this->author})";
[188] Fix | Delete
}
[189] Fix | Delete
[190] Fix | Delete
protected function build_author_exclusion(): string {
[191] Fix | Delete
return "(post_author != {$this->excluded_author})";
[192] Fix | Delete
}
[193] Fix | Delete
[194] Fix | Delete
protected function build_posts_inclusion(): string {
[195] Fix | Delete
$conditions = array();
[196] Fix | Delete
$posts_view = $this->get_posts_table();
[197] Fix | Delete
foreach ( $this->included_posts as $blog_id => $posts ) {
[198] Fix | Delete
$conditions[] = $posts_view . '.ID IN (' . join( ',', $posts ) . ')';
[199] Fix | Delete
}
[200] Fix | Delete
[201] Fix | Delete
return '(' . join( ' OR ', $conditions ) . ')';
[202] Fix | Delete
}
[203] Fix | Delete
[204] Fix | Delete
[205] Fix | Delete
protected function build_posts_exclusion(): string {
[206] Fix | Delete
$conditions = array();
[207] Fix | Delete
$posts_view = $this->get_posts_table();
[208] Fix | Delete
foreach ( $this->excluded_posts as $blog_id => $posts ) {
[209] Fix | Delete
$conditions[] = $posts_view . '.ID NOT IN (' . join( ',', $posts ) . ')';
[210] Fix | Delete
}
[211] Fix | Delete
[212] Fix | Delete
return '(' . join( ' OR ', $conditions ) . ')';
[213] Fix | Delete
}
[214] Fix | Delete
[215] Fix | Delete
protected function build_title_filter(): string {
[216] Fix | Delete
$query_parts = array_map( function ( $keyword ) {
[217] Fix | Delete
$keyword = esc_sql( $keyword );
[218] Fix | Delete
[219] Fix | Delete
return "LOWER(post_title) LIKE '%$keyword%'";
[220] Fix | Delete
}, $this->title_keywords );
[221] Fix | Delete
[222] Fix | Delete
return '(' . join( ' OR ', $query_parts ) . ')';
[223] Fix | Delete
}
[224] Fix | Delete
[225] Fix | Delete
protected function build_post_status(): string {
[226] Fix | Delete
return '(post_status="publish")';
[227] Fix | Delete
}
[228] Fix | Delete
[229] Fix | Delete
protected function filter_protected_posts(): string {
[230] Fix | Delete
$posts_table = $this->get_posts_table();
[231] Fix | Delete
return "($posts_table.post_password='')";
[232] Fix | Delete
}
[233] Fix | Delete
}
[234] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function