Sindbad~EG File Manager
<?php
namespace RealPress\Controllers;
use RealPress\Helpers\RestApi;
use RealPress\Helpers\Settings;
use RealPress\Helpers\Template;
use RealPress\Models\PropertyModel;
use WP_REST_Server;
/**
* WishListController
*/
class WishListController {
public function __construct() {
add_action( 'rest_api_init', array( $this, 'register_rest_routes' ) );
add_action( 'wp_footer', array( $this, 'add_snackbar' ) );
}
/**
* @return void
*/
public function register_rest_routes() {
do_action('realpress/rest-api/before-register');
register_rest_route(
RestApi::generate_namespace(),
'/wishlist',
array(
array(
'methods' => WP_REST_Server::EDITABLE,
'callback' => array( $this, 'toggle_wishlist' ),
'args' => array(
'property_id' => array(
'required' => false,
'type' => 'integer',
'description' => 'The property id is required',
),
),
'permission_callback' => function () {
return is_user_logged_in();
},
),
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_wishlist' ),
'permission_callback' => '__return_true'
),
)
);
}
/**
* @param \WP_REST_Request $request
*
* @return \WP_REST_Response
*/
public function toggle_wishlist( \WP_REST_Request $request ) {
$user_id = get_current_user_id();
$property_id = $request->get_param( 'property_id' );
$wishlist = get_user_meta( $user_id, REALPRESS_PREFIX . '_my_wishlist', true );
$data = array();
if ( empty( $wishlist ) ) {
$meta_id = update_user_meta( $user_id, REALPRESS_PREFIX . '_my_wishlist', array( $property_id ) );
if ( ! empty( $meta_id ) ) {
$data['status'] = 'added';
}
} else {
if ( in_array( $property_id, $wishlist ) ) {
$key = array_search( $property_id, $wishlist );
unset( $wishlist[ $key ] );
if ( empty( $wishlist ) ) {
$meta_id = delete_user_meta( $user_id, REALPRESS_PREFIX . '_my_wishlist' );
if ( ! empty( $meta_id ) ) {
$data['status'] = 'removed';
}
} else {
$meta_id = update_user_meta( $user_id, REALPRESS_PREFIX . '_my_wishlist', array_values( $wishlist ) );
if ( ! empty( $meta_id ) ) {
$data['status'] = 'removed';
}
}
} else {
$wishlist[] = $property_id;
$meta_id = update_user_meta( $user_id, REALPRESS_PREFIX . '_my_wishlist', $wishlist );
if ( ! empty( $meta_id ) ) {
$data['status'] = 'added';
}
}
}
return RestApi::success( '', $data );
}
/**
* @param \WP_REST_Request $request
*
* @return \WP_REST_Response
*/
public function get_wishlist( \WP_REST_Request $request ) {
$params = $request->get_params();
if ( is_user_logged_in() ) {
$user_id = get_current_user_id();
$wishlist = get_user_meta( $user_id, REALPRESS_PREFIX . '_my_wishlist', true );
} else {
$wishlist = $params['post_in'] ?? array();
}
if ( empty( $wishlist ) ) {
return RestApi::success( esc_html__( 'No property found.', 'realpress' ), array() );
}
$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,
'post__in' => $wishlist,
);
$data = array();
$args = apply_filters( 'realpress/filter/wishlist/args', $args, $params );
$query = new \WP_Query( $args );
$template = Template::instance();
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() ),
)
);
}
}
wp_reset_postdata();
} else {
return RestApi::success( esc_html__( 'No property found.', 'realpress' ), array() );
}
$data['content'] = ob_get_clean();
//Paginate
$paginate_args = array(
'paged' => $args['paged'],
'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 );
}
public function add_snackbar() {
?>
<div id="realpress-snackbar"></div>
<?php
}
}
Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists