/
home
/
u303693141
/
public_html
/
wp-content
/
plugins
/
Upload File
HOME
<?php /** * Plugin Name: REFC Land Map * Description: Displays Real Estate For Cheap listing locations on a lightweight map. * Version: 1.0.2 * Author: Real Estate For Cheap * Text Domain: refc-land-map */ if (!defined('ABSPATH')) { exit; } final class REFC_Land_Map { const VERSION = '1.0.2'; public function __construct() { add_action('wp_enqueue_scripts', array($this, 'register_assets'), 30); add_shortcode('refc_land_map', array($this, 'render_shortcode')); } public function register_assets() { wp_register_style( 'refc-leaflet', 'https://unpkg.com/leaflet@1.9.4/dist/leaflet.css', array(), '1.9.4' ); wp_register_style( 'refc-land-map', plugin_dir_url(__FILE__) . 'assets/refc-land-map.css', array('refc-leaflet'), self::VERSION ); wp_register_script( 'refc-leaflet', 'https://unpkg.com/leaflet@1.9.4/dist/leaflet.js', array(), '1.9.4', true ); wp_register_script( 'refc-land-map', plugin_dir_url(__FILE__) . 'assets/refc-land-map.js', array('refc-leaflet'), self::VERSION, true ); if ($this->current_page_has_shortcode()) { $this->enqueue_assets(); } } public function render_shortcode($atts) { $atts = shortcode_atts( array( 'height' => '430', 'number' => '-1', 'purpose' => 'Sell', 'status' => '', 'show_list' => 'false', ), $atts, 'refc_land_map' ); $markers = $this->get_markers($atts); $map_id = 'refc-land-map-' . wp_rand(1000, 999999); $height = max(280, min(720, absint($atts['height']))); $this->enqueue_assets(); $payload = array( 'markers' => $markers, 'settings' => array( 'showList' => $atts['show_list'] === 'true', ), ); ob_start(); ?> <section class="refc-land-map" id="<?php echo esc_attr($map_id); ?>"> <div class="refc-land-map__header"> <div> <h2>Map of Available Properties</h2> <p><?php echo esc_html($this->summary_text($markers)); ?></p> </div> <label class="refc-land-map__search"> <span class="screen-reader-text">Search mapped listings</span> <input type="search" placeholder="Filter by city, title, or price"> </label> </div> <div class="refc-land-map__canvas" style="height: <?php echo esc_attr($height); ?>px"></div> <?php if ($atts['show_list'] === 'true') : ?> <div class="refc-land-map__list" aria-label="Mapped property list"></div> <?php endif; ?> <script type="application/json" class="refc-land-map__data"> <?php echo wp_json_encode($payload, JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT); ?> </script> </section> <?php return ob_get_clean(); } private function enqueue_assets() { wp_enqueue_style('refc-land-map'); wp_enqueue_script('refc-land-map'); } private function current_page_has_shortcode() { $post = get_post(); return $post instanceof WP_Post && has_shortcode($post->post_content, 'refc_land_map'); } private function get_markers(array $atts) { $meta_query = array( 'relation' => 'AND', array( 'key' => '_wre_listing_lat', 'value' => '', 'compare' => '!=', ), array( 'key' => '_wre_listing_lng', 'value' => '', 'compare' => '!=', ), ); if ($atts['purpose'] !== '') { $meta_query[] = array( 'key' => '_wre_listing_purpose', 'value' => sanitize_text_field($atts['purpose']), 'compare' => '=', ); } if ($atts['status'] !== '') { $meta_query[] = array( 'key' => '_wre_listing_status', 'value' => sanitize_text_field($atts['status']), 'compare' => '=', ); } $query = new WP_Query( array( 'post_type' => 'listing', 'post_status' => 'publish', 'posts_per_page' => (int) $atts['number'], 'orderby' => 'date', 'order' => 'DESC', 'no_found_rows' => true, 'meta_query' => $meta_query, ) ); $markers = array(); while ($query->have_posts()) { $query->the_post(); $post_id = get_the_ID(); $lat = (float) get_post_meta($post_id, '_wre_listing_lat', true); $lng = (float) get_post_meta($post_id, '_wre_listing_lng', true); if (!$this->valid_coordinate($lat, $lng)) { continue; } $price = get_post_meta($post_id, '_wre_listing_price', true); $land_size = get_post_meta($post_id, '_wre_listing_land_size', true); $thumbnail = $this->thumbnail_url($post_id); $markers[] = array( 'id' => $post_id, 'title' => html_entity_decode(get_the_title(), ENT_QUOTES, get_bloginfo('charset')), 'url' => get_permalink($post_id), 'lat' => $lat, 'lng' => $lng, 'price' => $this->format_price($price), 'landSize' => $this->format_land_size($land_size), 'address' => $this->address($post_id), 'status' => $this->status_label($post_id), 'thumbnail' => $thumbnail, ); } wp_reset_postdata(); return $markers; } private function valid_coordinate($lat, $lng) { return $lat >= -90 && $lat <= 90 && $lng >= -180 && $lng <= 180 && ($lat !== 0.0 || $lng !== 0.0); } private function format_price($price) { if ($price === '' || $price === null) { return ''; } if (function_exists('wre_raw_price')) { return html_entity_decode(wre_raw_price($price), ENT_QUOTES, get_bloginfo('charset')); } return '$' . number_format((float) $price); } private function format_land_size($land_size) { if ($land_size === '' || $land_size === null) { return ''; } $size = (float) $land_size; $label = number_format($size) . ' sqft'; if ($size >= 43560) { $label .= ' / ' . round($size / 43560, 2) . ' acres'; } return $label; } private function status_label($post_id) { $status = get_post_meta($post_id, '_wre_listing_status', true); if ($status === '') { return 'Available'; } return ucwords(str_replace('-', ' ', $status)); } private function address($post_id) { $displayed = get_post_meta($post_id, '_wre_listing_displayed_address', true); if ($displayed !== '') { return $displayed; } $parts = array_filter( array( get_post_meta($post_id, '_wre_listing_city', true), get_post_meta($post_id, '_wre_listing_state', true), get_post_meta($post_id, '_wre_listing_zip', true), ) ); return implode(', ', $parts); } private function thumbnail_url($post_id) { if (function_exists('wre_get_first_image')) { $image = wre_get_first_image($post_id); if (!empty($image['sml'])) { return $image['sml']; } } return get_the_post_thumbnail_url($post_id, 'medium') ?: ''; } private function summary_text(array $markers) { $count = count($markers); if ($count === 0) { return 'No mapped listings are available yet.'; } return sprintf( '%d %s with mapped locations across Illinois.', $count, $count === 1 ? 'property' : 'properties' ); } } new REFC_Land_Map();