Easy Property Listings 3.5.28 Code Reference
  • Package
  • Class
  • Tree
  • Todo
  • Hook Reference

Packages

  • EPL
    • Admin
      • Actions
      • Classes
        • ContactsTable
        • EPL
          • Admin
            • Images
        • ReportsGraph
        • Welcome
      • Contacts
      • ContactsActions
      • ContactsFunctions
      • Elements
      • Functions
      • Help
      • HelpSingle
      • Menus
      • MenusAddons
      • MenusExtensions
      • MenusLicenses
      • Plugins
      • Reports
      • ReportsGraphing
      • User
    • Assets
      • ScriptsStyles
      • SVG
    • Classes
      • AuthorLoader
      • AuthorMeta
      • Contact
      • Cron
      • CustomPostType
      • Forms
      • License
      • ListingAdvanced
      • ListingElements
      • MetaboxesCustomFields
      • Pagination
      • PropertyMeta
      • RenderFields
      • RestAPI
      • Search
      • Session
      • Updater
    • Compatibility
      • Extensions
      • Functions
      • Shortcodes
    • Functions
      • Actions
      • ConditionalTags
      • ErrorTracking
      • Formatting
      • Front
      • Global
      • Install
      • Pagination
      • Settings
      • Templates
    • Hooks
      • EnergyCertificate
      • ExternalLinks
      • FloorPlan
      • Map
      • ReadMore
      • WebLink
    • Includes
      • UserMeta
    • Meta
      • InitCustomFields
      • Meta
    • PostTypes
      • Business
      • Commercial
      • CommercialLand
      • Contact
      • Functions
      • Land
      • Property
      • Rental
      • Rural
    • Shortcode
      • CommercialListingSearch
      • ContactForm
      • Listing
      • ListingAdvanced
      • ListingAuction
      • ListingCategory
      • ListingFeature
      • ListingLocation
      • ListingMetaDoc
      • ListingOpen
      • ListingResults
      • ListingSearch
      • Map
    • Taxonomy
      • BusinessCategories
      • ContactTag
      • Features
      • Location
    • Templates
      • Themes
        • iThemes
        • iThemesBuilder
    • Widget
      • Admin
        • Dashboard
      • Classes
        • Author
        • ContactCapture
        • Gallery
        • Listing
        • Search
      • Functions
  • None
  • WordPress
    • Session

Classes

  • EPL_Admin_Images
  • EPL_Advanced_Shortcode_Listing
  • EPL_Author
  • EPL_Author_Loader
  • EPL_Author_Meta
  • EPL_Contact
  • EPL_Contact_Reports_Table
  • EPL_CPT
  • EPL_Cron
  • EPL_FORM_BUILDER
  • EPL_Graph
  • EPL_License
  • EPL_Listing_Elements
  • EPL_METABOX
  • EPL_Pagination_Call
  • EPL_Property_Meta
  • EPL_Render_Fields
  • EPL_Rest_API
  • EPL_SEARCH
  • EPL_Search_Fields
  • EPL_Session
  • EPL_SL_Plugin_Updater
  • EPL_Welcome
  • EPL_Widget_Author
  • EPL_Widget_Contact_Capture
  • EPL_Widget_Property_Gallery
  • EPL_Widget_Property_Search
  • EPL_Widget_Recent_Property

Functions

  • EPL
  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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 
<?php
/**
 * EPL Rest API Class.
 *
 * @package     EPL
 * @subpackage  Classes/RestAPI
 * @copyright   Copyright (c) 2024, Merv Barrett
 * @license     http://opensource.org/licenses/gpl-2.0.php GNU Public License
 * @since       3.5.8
 */

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * EPL_Rest_API Class
 *
 * @since 3.5.8
 */
if ( ! class_exists( 'EPL_Rest_API' ) ) :

    /**
     * EPL_Rest_API Class
     *
     * @since 3.5.8
     */
    class EPL_Rest_API {

        /**
         * Instance
         *
         * @var EPL_Rest_API instance
         */
        private static $instance;

        /**
         * Insures that only one instance of EPL_Rest_API exists in memory.
         *
         * @return EPL_Rest_API instance
         * @static
         * @staticvar array $instance
         *
         * @since 3.5.8
         */
        public static function instance() {
            if ( ! isset( self::$instance ) && ! ( self::$instance instanceof EPL_Rest_API ) ) {
                self::$instance = new EPL_Rest_API();
                self::$instance->hooks();
            }
            return self::$instance;
        }

        /**
         * Add hooks to Rest API.
         *
         * @since 3.5.8
         */
        public function hooks() {
            foreach ( epl_get_core_post_types() as $post_type ) {
                add_filter( 'rest_prepare_' . $post_type, array( $this, 'meta_to_rest_api' ), 10, 3 );
            }
        }

        /**
         * Add EPL Meta data to Rest API.
         *
         * @param array $response The response.
         * @param array $post     The post.
         * @param array $request  The request.
         *
         * @return WP_REST_Response $response
         *
         * @since 3.5.8
         * @since 3.5.10 Fix error when group fields are empty.
         * @since 3.5.27 Hidden and internal fields are only returned to users who can edit the listing.
         * @since 3.5.28 Users who cannot edit the listing only receive public fields the listing displays.
         */
        public function meta_to_rest_api( $response, $post, $request ) {

            if ( ! is_epl_post( $post->post_type ) ) {
                return $response;
            }

            $meta_field_sections = epl_get_meta_boxes();

            if ( ! empty( $meta_field_sections ) ) {

                foreach ( $meta_field_sections as $epl_meta_box ) {

                    $section_post_types = (array) $epl_meta_box['post_type'];

                    if ( ! in_array( $post->post_type, $section_post_types, true ) ) {
                        continue;
                    }

                    if ( ! empty( $epl_meta_box['groups'] ) ) {

                        foreach ( $epl_meta_box['groups'] as $group ) {

                            if ( isset( $group['fields'] ) && is_array( $group['fields'] ) ) {
                                $fields = $group['fields'];
                                $fields = array_filter( $fields );

                            } else {
                                $fields = array();
                            }

                            if ( ! empty( $fields ) ) {

                                foreach ( $fields as $field ) {
                                    if ( isset( $field['exclude'] ) && ! empty( $field['exclude'] ) ) {
                                        if ( in_array( $post->post_type, $field['exclude'], true ) ) {
                                            continue;
                                        }
                                    }

                                    if ( isset( $field['include'] ) && ! empty( $field['include'] ) ) {
                                        if ( ! in_array( $post->post_type, $field['include'], true ) ) {
                                            continue;
                                        }
                                    }
                                    $response->data['meta'][ $field['name'] ] = get_post_meta( $post->ID, $field['name'], true );
                                }
                            }
                        }
                    }
                }
            }

            if ( ! empty( $response->data['meta'] ) && is_array( $response->data['meta'] ) && ! current_user_can( 'edit_post', $post->ID ) ) {
                $response->data['meta'] = array_intersect_key(
                    $response->data['meta'],
                    array_flip( $this->get_public_meta_keys( $post ) )
                );
            }

            return $response;
        }

        /**
         * Meta keys returned to users who cannot edit the listing.
         *
         * Only fields the listing displays on the front end are allowed. Any other field, including
         * fields added by extensions, is withheld unless added with the epl_rest_api_public_meta_keys
         * filter. Fields are then removed when the listing or the site settings hide them.
         *
         * @param WP_Post $post The post.
         *
         * @return array Meta keys.
         *
         * @since 3.5.28
         */
        public function get_public_meta_keys( $post ) {

            $keys = array(
                // Listing details.
                'property_heading',
                'property_status',
                'property_authority',
                'property_unique_id',
                'property_category',
                'property_rural_category',
                'property_land_category',
                'property_com_authority',
                'property_com_listing_type',
                'property_commercial_category',
                'property_inspection_times',
                'property_featured',
                'property_under_offer',
                'property_auction',
                'property_sold_date',

                // Agents.
                'property_agent',
                'property_second_agent',
                'property_third_agent',
                'property_fourth_agent',
                'property_agent_hide_author_box',

                // Features.
                'property_bedrooms',
                'property_bathrooms',
                'property_toilet',
                'property_ensuite',
                'property_garage',
                'property_carport',
                'property_open_spaces',
                'property_rooms',
                'property_year_built',
                'property_new_construction',
                'property_pool',
                'property_air_conditioning',
                'property_security_system',
                'property_pet_friendly',
                'property_broadband',
                'property_built_in_robes',
                'property_dishwasher',
                'property_floor_boards',
                'property_gym',
                'property_intercom',
                'property_pay_tv',
                'property_remote_garage',
                'property_rumpus_room',
                'property_secure_parking',
                'property_spa',
                'property_study',
                'property_vacuum_system',
                'property_workshop',
                'property_balcony',
                'property_courtyard',
                'property_deck',
                'property_outdoor_entertaining',
                'property_shed',
                'property_tennis_court',
                'property_ducted_cooling',
                'property_ducted_heating',
                'property_evaporative_cooling',
                'property_gas_heating',
                'property_hydronic_heating',
                'property_open_fire_place',
                'property_reverse_cycle_aircon',
                'property_split_system_aircon',
                'property_split_system_heating',

                // Land.
                'property_land_area',
                'property_land_area_unit',
                'property_building_area',
                'property_building_area_unit',
                'property_energy_rating',
                'property_land_fully_fenced',

                // Address.
                'property_address_display',
                'property_address_hide_map',
                'property_com_display_suburb',
                'property_address_lot_number',
                'property_address_sub_number',
                'property_address_street_number',
                'property_address_street',
                'property_address_suburb',
                'property_address_city',
                'property_address_state',
                'property_address_postal_code',
                'property_address_country',
                'property_address_coordinates',

                // Pricing.
                'property_price_display',
                'property_price',
                'property_price_view',
                'property_sold_price_display',
                'property_sold_price',
                'property_rent_display',
                'property_rent',
                'property_rent_period',
                'property_rent_view',
                'property_bond',
                'property_date_available',
                'property_furnished',
                'property_holiday_rental',

                // Rural.
                'property_rural_fencing',
                'property_rural_annual_rainfall',
                'property_rural_soil_types',
                'property_rural_improvements',
                'property_rural_council_rates',
                'property_rural_irrigation',
                'property_rural_carrying_capacity',

                // Commercial.
                'property_com_rent',
                'property_com_rent_period',
                'property_com_plus_outgoings',
                'property_com_further_options',
                'property_com_zone',
                'property_com_car_spaces',
                'property_com_highlight_1',
                'property_com_highlight_2',
                'property_com_highlight_3',
                'property_com_parking_comments',

                // Files and links.
                'property_video_url',
                'property_external_link',
                'property_external_link_label',
                'property_external_link_2',
                'property_external_link_2_label',
                'property_external_link_3',
                'property_external_link_3_label',
                'property_com_mini_web',
                'property_com_mini_web_2',
                'property_com_mini_web_3',
                'property_floorplan',
                'property_floorplan_label',
                'property_floorplan_2',
                'property_floorplan_2_label',
                'property_energy_certificate',
                'property_energy_certificate_label',
            );

            $keys   = (array) apply_filters( 'epl_rest_api_public_meta_keys', $keys, $post );
            $hidden = array();
            $status = get_post_meta( $post->ID, 'property_status', true );

            $show_price = 'yes' === get_post_meta( $post->ID, 'property_price_display', true ) && ! in_array( $status, array( 'sold', 'leased' ), true );
            $show_sold  = 'yes' === get_post_meta( $post->ID, 'property_sold_price_display', true ) && 'sold' === $status;
            $show_rent  = 'yes' === get_post_meta( $post->ID, 'property_rent_display', true ) && 'leased' !== $status;
            $show_bond  = $show_rent && in_array( epl_get_option( 'display_bond' ), array( 1, '1', 'yes' ), true );
            $show_addr  = 'yes' === get_post_meta( $post->ID, 'property_address_display', true );

            if ( ! $show_price ) {
                $hidden = array_merge( $hidden, array( 'property_price', 'property_price_view', 'property_com_rent', 'property_com_rent_period' ) );
            }

            if ( ! $show_sold ) {
                $hidden[] = 'property_sold_price';
            }

            if ( ! $show_rent ) {
                $hidden = array_merge( $hidden, array( 'property_rent', 'property_rent_view', 'property_rent_period' ) );
            }

            if ( ! $show_bond ) {
                $hidden[] = 'property_bond';
            }

            if ( ! $show_addr ) {
                $hidden = array_merge(
                    $hidden,
                    array(
                        'property_address_lot_number',
                        'property_address_sub_number',
                        'property_address_street_number',
                        'property_address_street',
                        'property_address_coordinates',
                    )
                );

                if ( in_array( $post->post_type, array( 'commercial', 'commercial_land', 'business' ), true ) && 'yes' !== get_post_meta( $post->ID, 'property_com_display_suburb', true ) ) {
                    $hidden[] = 'property_address_suburb';
                }
            }

            if ( 'yes' === get_post_meta( $post->ID, 'property_address_hide_map', true ) ) {
                $hidden[] = 'property_address_coordinates';
            }

            if ( 'yes' !== epl_get_option( 'epl_enable_city_field' ) ) {
                $hidden[] = 'property_address_city';
            }

            if ( 'yes' !== epl_get_option( 'epl_enable_country_field' ) ) {
                $hidden[] = 'property_address_country';
            }

            $hidden = (array) apply_filters( 'epl_rest_api_private_meta_keys', $hidden, $post );

            return array_values( array_diff( array_unique( $keys ), $hidden ) );
        }
    }
endif;

EPL_Rest_API::instance();
Easy Property Listings 3.5.28 Code Reference API documentation generated by ApiGen