Sindbad~EG File Manager
<?php
namespace RealPress\Controllers;
use RealPress\Helpers\RestApi;
use RealPress\Helpers\Template;
use RealPress\Helpers\Config;
use RealPress\Helpers\Settings;
use RealPress\Models\PropertyMetaModel;
use RealPress\Models\PropertyModel;
use WP_REST_Server;
/**
* Class PropertyController
* @package RealPress\Controllers
*/
class PropertyController {
public function __construct() {
add_action( 'rest_api_init', array( $this, 'register_rest_routes' ) );
add_filter(
'manage_edit-' . REALPRESS_PROPERTY_CPT . '_columns',
array(
$this,
'add_header_thumbnail_column',
)
);
add_filter(
'manage_' . 'posts' . '_custom_column',
array(
$this,
'add_content_thumbnail_column',
),
10,
2
);
}
/**
* @param $column
* @param $post_id
*
* @return void
*/
public function add_content_thumbnail_column( $column, $post_id ) {
switch ( $column ) {
case 'realpress-property-thumbnail':
$image_url = PropertyModel::get_post_thumbnail_url( $post_id );
?>
<img src="<?php echo esc_url_raw( $image_url ); ?>" alt="<?php echo get_the_title( $post_id ); ?>">
<?php
break;
default:
break;
}
}
/**
* @param $columns
*
* @return mixed
*/
public function add_header_thumbnail_column( $columns ) {
$columns['realpress-property-thumbnail'] = esc_html__( 'Thumbnail', 'realpress' );
return $columns;
}
/**
* @return void
*/
public function register_rest_routes() {
do_action('realpress/rest-api/before-register');
register_rest_route(
RestApi::generate_namespace(),
'/properties',
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_properties' ),
'args' => array(
'posts_per_page' => array(
'required' => false,
'type' => 'integer',
'description' => 'The posts per page must be an integer',
),
'page' => array(
'required' => false,
'type' => 'integer',
'description' => 'The page must be an integer',
),
),
'permission_callback' => '__return_true',
),
);
}
/**
* @param \WP_REST_Request $request
*
* @return \WP_REST_Response
*/
public function get_properties( \WP_REST_Request $request ) {
$params = $request->get_params();
global $rp_lang;
if ( isset( $params['pll_current_lang'] ) ) {
$rp_lang = $params['pll_current_lang'];
}
if ( isset( $params['wpml_current_lang'] ) ) {
$rp_lang = $params['wpml_current_lang'];
}
$args = $this->parse_args( $params );
$data = array();
$query = new \WP_Query( $args );
$template = Template::instance();
$properties = array();
//Content
ob_start();
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
if ( isset( $params['loop-item-id'] ) && $params['loop-item-id'] ) {
\Thim_EL_Kit\Utilities\Elementor::instance()->render_loop_item_content( $params['loop-item-id'] );
} else {
$template->get_frontend_template_type_classic(
apply_filters( 'realpress/filter/property-list/property-item', 'shared/property-list/property-item.php' ),
array(
'data' => PropertyModel::get_property_list_item_data( get_the_ID() ),
)
);
}
$properties[] = PropertyModel::get_property_list_item_data( get_the_ID() );
}
wp_reset_postdata();
} else {
return RestApi::success( esc_html__( 'No property found.', 'realpress' ), array() );
}
$data['content'] = ob_get_clean();
$data['property'] = $properties;
//Paginate
$paginate_args = array(
'paged' => $args['paged'],
'pagination_type' => $params['pagination_type'] ?? '',
'max_pages' => $query->max_num_pages,
'total' => $query->found_posts,
'item_per_page' => $args['posts_per_page'],
'type' => 'property',
);
RestApi::add_pagination_data( $data, $paginate_args );
return RestApi::success( '', $data );
}
/**
* @param $params
*
* @return array
*/
public function parse_args( $params ) {
$args = array(
'posts_per_page' => $params['posts_per_page'] ?? Settings::get_property_per_page(),
'paged' => $params['page'] ?? 1,
'orderby' => $params['orderby'] ?? 'date',
'order' => $params['order'] ?? 'asc',
'post_type' => REALPRESS_PROPERTY_CPT,
);
if ( isset( $params['template'] ) ) {
if ( $params['template'] === 'similar_property' ) {
$args['post__not_in'] = array( $params['post_id'] );
$types = PropertyMetaModel::instance( $params['post_id'] )->get_property_terms( 'realpress-type' );
$term_ids = array();
if ( ! empty( $types ) ) {
$term_ids = array_map(
function ( $types ) {
return $types->term_id;
},
$types
);
}
$args['tax_query'] = array(
'relation' => 'AND',
array(
'taxonomy' => 'realpress-type',
'field' => 'id',
'terms' => $term_ids,
'operator' => 'IN',
),
);
} elseif ( $params['template'] === 'search_suggest' ) {
$args['posts_per_page'] = $params['posts_per_page'] ?? Settings::get_search_suggest_property_number();
}
}
if ( isset( $params['author_id'] ) ) {
$args['user_id'] = $params['author_id'];
}
//sort
if ( $args['orderby'] === 'rating' ) {
$args['meta_key'] = REALPRESS_PREFIX . '_property_average_review';
$args['orderby'] = 'meta_value_num';
}
if ( $args['orderby'] === 'price' ) {
$args['meta_key'] = REALPRESS_PREFIX . '_group:information:section:general:fields:price';
$args['orderby'] = 'meta_value_num';
}
//search
if ( ! empty( $params['keyword'] ) ) {
$args['s'] = $params['keyword'];
if ( apply_filters( 'realpress/filter/properties/search_columns/post_title' , false) ) {
$args['search_columns'] = 'post_title';
}
}
if ( isset( $params['room'] ) ) {
$compare = '=';
if ( strpos( $params['room'], '+' ) !== false ) {
$compare = '>=';
}
$args['meta_query'][] = array(
'key' => REALPRESS_PREFIX . '_group:information:section:general:fields:rooms',
'type' => 'DECIMAL',
'value' => $params['room'],
'compare' => $compare,
);
}
if ( isset( $params['bath'] ) ) {
$compare = '=';
if ( strpos( $params['bath'], '+' ) !== false ) {
$compare = '>=';
}
$args['meta_query'][] = array(
'key' => REALPRESS_PREFIX . '_group:information:section:general:fields:bathrooms',
'type' => 'DECIMAL',
'value' => $params['bath'],
'compare' => $compare,
);
}
if ( isset( $params['bed'] ) ) {
$compare = '=';
if ( strpos( $params['bed'], '+' ) !== false ) {
$compare = '>=';
}
$args['meta_query'][] = array(
'key' => REALPRESS_PREFIX . '_group:information:section:general:fields:bedrooms',
'type' => 'DECIMAL',
'value' => $params['bed'],
'compare' => $compare,
);
}
if ( isset( $params['verified'] ) && $params['verified'] === 'yes' ) {
$args['meta_query'][] = array(
'key' => REALPRESS_PREFIX . '_group:information:section:general:fields:verified',
'type' => 'CHAR',
'value' => 'on',
'compare' => '=',
);
}
//For taxonomy archive
if ( isset( $params['term_id'] ) && isset( $params['taxonomy'] ) ) {
$args['tax_query'][] = array(
'taxonomy' => $params['taxonomy'],
'terms' => array( $params['term_id'] ),
'operator' => 'IN',
'include_children' => false,
);
}
//Advanced Search
$taxonomies = array_keys( Config::instance()->get( 'property-type:taxonomies' ) );
foreach ( $taxonomies as $taxonomy ) {
$key = str_replace( array( 'realpress-', '-' ), array( '', '_' ), $taxonomy );
if ( isset( $params[ $key ] ) ) {
if ( is_string( $params[ $key ] ) ) {
$params[ $key ] = explode( ',', $params[ $key ] );
}
// if is realpress-feature taxonomy, use AND operator, other is IN
$tax_operator = ( $key === 'feature' ) ? 'AND' : 'IN';
$args['tax_query'][] = array(
'taxonomy' => $taxonomy,
'terms' => $params[ $key ],
'operator' => $tax_operator,
);
}
}
//Price
if ( isset( $params['min_price'] ) ) {
$args['meta_query'][] = array(
'key' => REALPRESS_PREFIX . '_group:information:section:general:fields:price',
'value' => $params['min_price'],
'type' => 'DECIMAL',
'compare' => '>=',
);
}
if ( isset( $params['max_price'] ) ) {
$args['meta_query'][] = array(
'key' => REALPRESS_PREFIX . '_group:information:section:general:fields:price',
'value' => $params['max_price'] ,
'type' => 'DECIMAL',
'compare' => '<=',
);
}
if ( isset( $params['min_area'] ) ) {
$args['meta_query'][] = array(
'key' => REALPRESS_PREFIX . '_group:information:section:general:fields:area_size',
'value' => $params['min_area'],
'type' => 'DECIMAL',
'compare' => '>=',
);
}
if ( isset( $params['max_area'] ) ) {
$args['meta_query'][] = array(
'key' => REALPRESS_PREFIX . '_group:information:section:general:fields:area_size',
'value' => $params['max_area'],
'type' => 'DECIMAL',
'compare' => '<=',
);
}
if ( isset( $params['min_year'] ) ) {
$args['meta_query'][] = array(
'key' => REALPRESS_PREFIX . '_group:information:section:general:fields:year_built',
'value' => $params['min_year'],
'type' => 'DECIMAL',
'compare' => '>=',
);
}
if ( isset( $params['max_year'] ) ) {
$args['meta_query'][] = array(
'key' => REALPRESS_PREFIX . '_group:information:section:general:fields:year_built',
'value' => $params['max_year'],
'type' => 'DECIMAL',
'compare' => '<=',
);
}
if ( isset( $params['property_id'] ) ) {
$args['meta_query'][] = array(
'key' => REALPRESS_PREFIX . '_group:information:section:general:fields:property_id',
'value' => $params['property_id'],
'compare' => 'LIKE',
);
}
if ( ! empty( $params['has_coordinates'] ) ) {
$args['meta_query'][] = array(
'key' => REALPRESS_PREFIX . '_group:map:section:enable_map:fields:enable',
'value' => 'on',
'type' => 'CHAR',
'compare' => '=',
);
$args['meta_query'][] = array(
'key' => REALPRESS_PREFIX . '_group:map:section:map:fields:lat',
'value' => '',
'type' => 'CHAR',
'compare' => '!=',
);
$args['meta_query'][] = array(
'key' => REALPRESS_PREFIX . '_group:map:section:map:fields:lng',
'value' => '',
'type' => 'CHAR',
'compare' => '!=',
);
}
if ( isset( $args['meta_query'] ) ) {
$args['meta_query']['relation'] = 'AND';
}
if ( isset( $args['tax_query'] ) ) {
$args['tax_query']['relation'] = 'AND';
}
return apply_filters( 'realpress/filter/list-property/args', $args, $params );
}
}
Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists