      1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101:

    <?php

    namespace WPGMZA;

    class CustomFieldFilterController
    {
        public $params;

        public function __construct($params)
        {
            $this->params = $params;
        }

        protected function getFilterQueries()
        {
            $params = $this->params;

            $map_id = $params['map_id'];
            $queries = array();

            foreach($params['widgetData'] as $widgetData)
            {
                if(!is_array($widgetData))
                    $widgetData = (array)$widgetData;

                $field_id = $widgetData['field_id'];
                $filter = apply_filters('wpgmza_get_custom_field_filter', $field_id, $map_id);

                if(!empty($widgetData['value']))
                    $queries[] = $filter->getFilteringSQL($widgetData['value']);
            }

            return $queries;
        }

        /**
         * This function combines all the widget queries in a manner
         * that emulates the INTERSECT operator (Not available in MySQL)
         * @return string The query string
         */
        protected function getQuery()
        {
            global $wpdb;

            $queries = $this->getFilterQueries();

            if(empty($queries))
                return "SELECT id FROM {$wpdb->prefix}wpgmza WHERE map_id=" . (int)$this->params['map_id'];

            $numQueries = count($queries);

            foreach($queries as $key => $qstr)
                $queries[$key] = "($qstr)";

            $body = implode(' UNION ALL ', $queries);

            $query = "
                SELECT temp.id FROM (
                    $body
                ) AS temp
                GROUP BY id HAVING COUNT(id) >= $numQueries
            ";

            return $query;
        }

        public function getFilteredMarkerIDs()
        {
            global $wpdb;

            $sql = $this->getQuery();
            $ids = $wpdb->get_col($sql);

            return $ids;
        }
    }

    add_filter('wpgmza_get_custom_field_filter_controlller', 'WPGMZA\\get_custom_field_filter_controller');

    function get_custom_field_filter_controller($params)
    {
        return new CustomFieldFilterController($params);
    }

    add_action('wp_ajax_nopriv_wpgmza_custom_field_filter_get_filtered_marker_ids', 'WPGMZA\\custom_field_filter_get_filtered_marker_ids');
    add_action('wp_ajax_wpgmza_custom_field_filter_get_filtered_marker_ids', 'WPGMZA\\custom_field_filter_get_filtered_marker_ids');

    function custom_field_filter_get_filtered_marker_ids() {

        $controller = apply_filters('wpgmza_get_custom_field_filter_controlller', $_POST);
        $filtered_marker_ids = $controller->getFilteredMarkerIDs();

        $result = (object)array(
            'marker_ids' => $filtered_marker_ids
        );

        wp_send_json($result);

        exit;
    }