<?php
namespace Flyerimage\App\Services;

use Flyerimage\App\Constants\ITables;
use Flyerimage\App\Exceptions\ServiceException;
use Flyerimage\Core\Database\QueryBuilder;
use Flyerimage\Core\Util\Helper;

/**
 * WpFilterCallback service
 *
 * @author Pritom Kumar Mondal <pritomkucse@gmail.com>
 * @version
 */
class WpFilterCallback extends AbstractService {
    public function __construct($params) {
        parent::__construct("");
        $this->params = $params;
    }

    public function wp_posts_filter_posts_where_query( $where ) {
        global $wpdb;
        $field = $wpdb->posts . '.post_title';
        $where .= ' AND ' . "$field IS NOT NULL AND CHAR_LENGTH($field)>0 ";

        $title = trim( $this->getParamValue( "filter", "" ) );
        if (Helper::is_valid_string($title)) {
            $where .= ' AND ' . $field . ' LIKE \'%' . esc_sql( $wpdb->esc_like( $title ) ) . '%\' ';
        }
        return $where;
    }

    public function wp_posts_filter_posts_fields_query( $fields ) {
        global $wpdb;
        return implode( ",", [
            $wpdb->posts . ".ID", $wpdb->posts . ".post_title", $wpdb->posts . ".post_type",
            $wpdb->posts . ".post_name"
        ] );
    }
}