Sindbad~EG File Manager

Current Path : /home/xiedrke/entrepot/wp-content/plugins/realpress/app/Controllers/
Upload File :
Current File : /home/xiedrke/entrepot/wp-content/plugins/realpress/app/Controllers/ModifyQueryController.php

<?php

namespace RealPress\Controllers;

class ModifyQueryController {

	public function __construct() {
		add_filter( 'posts_distinct', array( $this, 'add_agent_property_distinct_query' ), 10, 2 );
		add_filter( 'posts_join', array( $this, 'add_agent_property_join_query' ), 10, 2 );
		add_filter( 'posts_where', array( $this, 'add_agent_property_where_query' ), 10, 2 );
	}

	/**
	 * @param $distinct
	 * @param $that
	 *
	 * @return mixed|string
	 */
	public function add_agent_property_distinct_query( $distinct, $that ) {
		if ( ! isset( $that->query_vars['post_type'] ) || $that->query_vars['post_type'] !== REALPRESS_PROPERTY_CPT ) {
			return $distinct;
		}

		if ( empty( $that->query_vars['user_id'] ) ) {
			return $distinct;
		}

		return 'DISTINCT';
	}

	/**
	 * @param $join
	 * @param $that
	 *
	 * @return mixed|string
	 */
	public function add_agent_property_join_query( $join, $that ) {
		if ( ! isset( $that->query_vars['post_type'] ) || $that->query_vars['post_type'] !== REALPRESS_PROPERTY_CPT ) {
			return $join;
		}

		if ( empty( $that->query_vars['user_id'] ) ) {
			return $join;
		}

		global $wpdb;
		$post_tbl     = $wpdb->posts;
		$postmeta_tbl = $wpdb->postmeta;

		$join .= "INNER JOIN $postmeta_tbl AS pmt1 ON $post_tbl.ID = pmt1.post_id 
				INNER JOIN $postmeta_tbl AS pmt2 ON $post_tbl.ID = pmt2.post_id 
				INNER JOIN $postmeta_tbl AS pmt3 ON $post_tbl.ID = pmt3.post_id";

		return $join;
	}

	/**
	 * @param $where
	 * @param $that
	 *
	 * @return mixed|string
	 */
	public function add_agent_property_where_query( $where, $that ) {
		if ( ! isset( $that->query_vars['post_type'] ) || $that->query_vars['post_type'] !== REALPRESS_PROPERTY_CPT ) {
			return $where;
		}

		if ( empty( $that->query_vars['user_id'] ) ) {
			return $where;
		}

		global $wpdb;
		$post_tbl     = $wpdb->posts;
		$postmeta_tbl = $wpdb->postmeta;
		$user_id      = $that->query_vars['user_id'];
		$where       .= "
		AND (
				(
					pmt1.meta_key = 'realpress_group:agent:section:agent_information:fields:enable' AND pmt1.meta_value = 'on'
					AND pmt2.meta_key = 'realpress_group:agent:section:agent_information:fields:information' AND pmt2.meta_value = 'agent'
					AND pmt3.meta_key = 'realpress_group:agent:section:agent_user:fields:agent_user' AND pmt3.meta_value = $user_id
				) 
				OR (
					$post_tbl.post_author = $user_id
					AND (
							(
							pmt1.meta_key = 'realpress_group:agent:section:agent_information:fields:enable' AND pmt1.meta_value != 'on'
							)
							OR 
							(
							pmt1.meta_key = 'realpress_group:agent:section:agent_information:fields:enable' AND pmt1.meta_value = 'on'
							AND pmt2.meta_key = 'realpress_group:agent:section:agent_information:fields:information' AND pmt2.meta_value != 'agent'
							)
							OR 
							(
							pmt1.meta_key = 'realpress_group:agent:section:agent_information:fields:enable' AND pmt1.meta_value = 'on'
							AND pmt2.meta_key = 'realpress_group:agent:section:agent_information:fields:information' AND pmt2.meta_value = 'agent'
							AND pmt3.meta_key = 'realpress_group:agent:section:agent_user:fields:agent_user' AND (pmt3.meta_value = $user_id OR pmt3.meta_value ='')
							)
						)
				)
			)";

		return $where;
	}
}

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists