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

Packages

  • EPL
    • Admin
      • Actions
      • Classes
        • ContactsTable
        • 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
    • 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_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 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 
<?php
/**
 * SHORTCODE :: Listing Advanced [listing_advanced]
 *
 * @package     EPL
 * @subpackage  Classes/ListingAdvanced
 * @copyright   Copyright (c) 2019, Merv Barrett
 * @license     http://opensource.org/licenses/gpl-2.0.php GNU Public License
 * @since       3.3.0
 */

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

// phpcs:disable WordPress.DB.SlowDBQuery

/**
 * EPL_Advanced_Shortcode_Listing Class
 *
 * @since 3.3.0
 */
class EPL_Advanced_Shortcode_Listing {

    /**
     * Default arguments.
     *
     * @since 3.3.0
     * @var array $default_args Default arguments.
     */
    public $default_args;

    /**
     * Attributes passed to shortcode.
     *
     * @since 3.3.0
     * @var array $atts Shortcode attributes.
     */
    public $atts;

    /**
     * Attributes passed to shortcode and defaults.
     *
     * @since 3.3.0
     * @var array $attributes Shortcode attributes.
     */
    public $attributes;

    /**
     * Array of arguments passed to WP_Query.
     *
     * @since 3.3.0
     * @var array $args Arguments.
     */
    public $args;

    /**
     * Meta price key.
     *
     * @since 3.3.0
     * @var string $meta_key_price Default price meta value.
     */
    public $meta_key_price = 'property_price';

        /**
     * Query object.
     *
     * @since 3.3.0
     * @var object $query_open instance of WP_Query object.
     */
    public $query_open;

    /**
     * Construct the shortcode.
     *
     * @since 3.3.0
     * @param array $atts Shortcode attributes.
     * @param array $overrides Array of variables to override defaults.
     */
    public function __construct( $atts, $overrides = array() ) {
        $this->atts = $atts;
        $this->shortcode_atts();
        $this->override_atts( $overrides );
        $this->build_query();
        $this->set_query();
    }

    /**
     * Override Attributes.
     *
     * @since 3.3.0
     * @param array $overrides Array of variables to override defaults.
     */
    public function override_atts( $overrides ) {

        if ( ! empty( $overrides ) ) {

            foreach ( $overrides as $key  => $value ) {
                $this->set_attribute( $key, $value );
            }
        }
    }

    /**
     * Get default post types.
     *
     * @since 3.3
     */
    public function get_default_post_types() {

        $property_types = epl_get_active_post_types();
        if ( ! empty( $property_types ) ) {
            $property_types = array_keys( $property_types );
        }

        return $property_types;
    }

    /**
     * Get meta key price.
     *
     * @since 3.3
     */
    public function get_meta_key_price() {

        if ( is_string( $this->attributes['post_type'] ) && 'rental' === $this->attributes['post_type'] ) {
            $this->meta_key_price = 'property_rent';
        } else {
            $this->meta_key_price = 'property_price';
        }

        return $this->meta_key_price;
    }

    /**
     * Get default options.
     *
     * @since 3.3
     */
    public function get_default_args() {

        /**
         *
         * Template     : used to render each listing in loop.
         *
         * Default to loop-listing-blog-default.php
         *
         * To override copy loop-listing-blog-default.php from EPL/templates/content to
         * active_theme/easypropertylistings and rename the file to loop-listing-blog-{$template}.php
         *
         *
         * wrap_template    : Used to render wrapper of listings render using shortcode.
         *
         * Default to shortcode-listing.php
         *
         * to override copy shortcode-listing.php from EPL/templates/content to
         * active_theme/easypropertylistings and rename the file to
         * shortcode-listing-{$wrap_template}.php
         */

        $this->default_args = array(
            'post_type'               => $this->get_default_post_types(), // Post Type.
            'status'                  => array( 'current', 'sold', 'leased' ),
            'commercial_listing_type' => '', // Listing Type, sale lease and both.
            'feature'                 => '', // Feature slug.
            'feature_id'              => '', // Feature ID.
            'limit'                   => '10', // Number of maximum posts to show.
            'offset'                  => '', // Offset posts. When used, pagination is disabled.
            'author'                  => '', // Author of listings.
            'agent'                   => '', // listings by agent.
            'featured'                => 0, // Featured listings.
            'open_house'              => false, // only show open house.
            'auction'                 => false, // only show properties for auction.
            'class'                   => 'epl-shortcode-listing', // wrapper class.
            'wrap_template'           => '', // explained in detail below.
            'template'                => false, // Template can be set to "slim" for home open style template.
            'location'                => '', // Location slug. Should be a name like sorrento.
            'location_id'             => '', // Location ID.
            'tools_top'               => 'off', // Tools before the loop like Sorter and Grid on or off.
            'tools_bottom'            => 'off', // Tools after the loop like pagination on or off.
            'sortby'                  => '', // Options: price, date : Default date.
            'orderby_clause'          => '', // order by two different pieces of postmeta (for example, suburb first and state second).
            'sort_order'              => 'DESC', // Sort by ASC or DESC.
            'query_object'            => '', // only for internal use . if provided use it instead of custom query.
            'pagination'              => 'on', // Enable or disable pagination.
            'post__in'                => '', // show only these posts.
            'post__not_in'            => '', // dont show these posts.
            'instance_id'             => '1', // when using multiple shortcodes on once page.
        );

        return $this->default_args;
    }

    /**
     * Shortcode attributes
     *
     * @since 3.3
     */
    public function shortcode_atts() {
        $this->attributes = shortcode_atts( $this->get_default_args(), $this->atts );
    }

    /**
     * Set attributes
     *
     * @since 3.3
     * @param string $key Meta key.
     * @param string $value Meta value.
     */
    public function set_attribute( $key, $value ) {

        if ( isset( $this->attributes[ $key ] ) ) {
            $this->attributes[ $key ] = $value;
        }
    }

    /**
     * Get attributes
     *
     * @param string $key Meta key.
     *
     * @return mixed|null
     * @since 3.3
     */
    public function get_attribute( $key ) {

        return isset( $this->attributes[ $key ] ) ? $this->attributes[ $key ] : null;
    }

    /**
     * Set the post type
     *
     * @since 3.3
     */
    public function set_post_type() {
        if ( ! is_array( $this->attributes['post_type'] ) ) {
            $this->attributes['post_type'] =
            array_map( 'trim', explode( ',', $this->attributes['post_type'] ) );
        }
    }

    /**
     * Set initial options
     *
     * @since 3.3
     */
    public function set_initial_args() {

        $paged      = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
        $this->args = array(
            'post_type'      => $this->attributes['post_type'],
            'posts_per_page' => $this->attributes['limit'],
            'paged'          => absint( $paged ),
        );
    }

    /**
     * Set the offset
     *
     * @since 3.3
     */
    public function set_offset() {

        // Offset query does not work with pagination.
        if ( ! empty( $this->attributes['offset'] ) ) {
            $this->args['offset']           = $this->attributes['offset'];
            $this->attributes['pagination'] = 'off'; // Disable pagination when offset is used.
        }
    }

    /**
     * Set posts in
     *
     * @since 3.3
     */
    public function set_post__in() {
        if ( ! empty( $this->attributes['post__in'] ) ) {
            $post__in = array_map( 'trim', explode( ',', $this->attributes['post__in'] ) );
            if ( ! empty( $post__in ) ) {
                $this->args['post__in'] = $post__in;
            }
        }
    }

    /**
     * Set posts not in
     *
     * @since 3.3
     */
    public function set_post__not_in() {
        if ( ! empty( $this->attributes['post__not_in'] ) ) {
            $post__not_in = array_map( 'trim', explode( ',', $this->attributes['post__not_in'] ) );
            if ( ! empty( $post__not_in ) ) {
                $this->args['post__not_in'] = $post__not_in;
            }
        }
    }

    /**
     * Process EPL attributes
     *
     * @since 3.3
     */
    public function process_epl_atts() {

        $this->args['meta_query'] = epl_parse_atts( $this->atts );
    }

    /**
     * Set the author
     *
     * @since 3.3
     */
    public function set_author() {

        // Listings of specified author.
        if ( ! empty( $this->attributes['author'] ) ) {
            $this->attributes['author'] = (array) $this->attributes['author'];
            if ( is_array( $this->attributes['author'] ) ) {
                $author_ids                 = array_map( 'epl_get_author_id_from_name', $this->attributes['author'] );
                $this->attributes['author'] = implode( ',', $author_ids );
            }
            $this->args['author'] = trim( $this->attributes['author'] );
        }
    }

    /**
     * Set the agent
     *
     * @since 3.3
     * @since 3.5 support for third & fourth agent
     */
    public function set_agent() {

        // Listings by specified agent.
        if ( ! empty( $this->attributes['agent'] ) ) {
            $this->attributes['agent'] = array_map( 'trim', explode( ',', $this->attributes['agent'] ) );
            $this->attributes['agent'] = array_filter( $this->attributes['agent'] );

            $agent_meta_query = array(
                'relation' => 'OR',
            );

            foreach ( $this->attributes['agent'] as $single_agent ) {
                $agent_meta_query[] = array(
                    'key'     => 'property_agent',
                    'value'   => array( $single_agent, sanitize_user( $single_agent ) ),
                    'compare' => 'IN',
                );
                $agent_meta_query[] = array(
                    'key'     => 'property_second_agent',
                    'value'   => array( $single_agent, sanitize_user( $single_agent ) ),
                    'compare' => 'IN',
                );

                if( 
                    in_array( 'commercial', $this->attributes['post_type'], true ) ||
                    in_array( 'commercial_land', $this->attributes['post_type'], true ) ||
                    in_array( 'business', $this->attributes['post_type'], true ) 
                ) {

                    $agent_meta_query[] = array(
                        'key'     => 'property_third_agent',
                        'value'   => array( $single_agent, sanitize_user( $single_agent ) ),
                        'compare' => 'IN',
                    );
                    $agent_meta_query[] = array(
                        'key'     => 'property_fourth_agent',
                        'value'   => array( $single_agent, sanitize_user( $single_agent ) ),
                        'compare' => 'IN',
                    );

                }
            }

            $this->args['meta_query']['agent_clause'] = $agent_meta_query;
        }
    }

    /**
     * Set featured
     *
     * @since 3.3
     */
    public function set_featured() {

        // Featured listings.
        if ( $this->attributes['featured'] ) {
            $this->args['meta_query']['featured_clause'] = array(
                'key'   => 'property_featured',
                'value' => 'yes',
            );
        }
    }

    /**
     * Set auction
     *
     * @since 3.3
     */
    public function set_auction() {

        // Auction only listings.
        if ( $this->attributes['auction'] ) {
            $this->args['meta_query']['property_authority_clause'] = array(
                'key'   => 'property_authority',
                'value' => 'auction',
            );
        }
    }

    /**
     * Set open for inspection
     *
     * @since 3.3
     */
    public function set_open_house() {

        // Open house only.
        if ( $this->attributes['open_house'] ) {
            $this->args['meta_query']['open_house_clause'] = array(
                'key'     => 'property_inspection_times',
                'value'   => '',
                'compare' => '!=',
            );
        }
    }

    /**
     * Set location taxonomy query
     *
     * @since 3.3
     */
    public function set_location_tax_query() {

        // Location taxonomy.
        // Query by slug.
        if ( ! empty( $this->attributes['location'] ) ) {
            if ( ! is_array( $this->attributes['location'] ) ) {
                $this->attributes['location'] = array_map( 'trim', explode( ',', $this->attributes['location'] ) );

                $this->args['tax_query']['tax_location_clause'] = array(
                    'taxonomy' => 'location',
                    'field'    => 'slug',
                    'terms'    => $this->attributes['location'],
                );
            }
        }

        // Query by ID.
        if ( ! empty( $this->attributes['location_id'] ) ) {
            if ( ! is_array( $this->attributes['location_id'] ) ) {
                $this->attributes['location_id'] = array_map( 'trim', explode( ',', $this->attributes['location_id'] ) );

                $this->args['tax_query']['tax_location_id_clause'] = array(
                    'taxonomy' => 'location',
                    'field'    => 'id',
                    'terms'    => $this->attributes['location_id'],
                );
            }
        }
    }

    /**
     * Set feeatures taxonomy query
     *
     * @since 3.3
     */
    public function set_features_tax_query() {

        // Features taxonomy.
        // Query by slug.
        if ( ! empty( $this->attributes['feature'] ) ) {
            if ( ! is_array( $this->attributes['feature'] ) ) {
                $this->attributes['feature'] = array_map( 'trim', explode( ',', $this->attributes['feature'] ) );

                $this->args['tax_query']['tax_feature_clause'] = array(
                    'taxonomy' => 'tax_feature',
                    'field'    => 'slug',
                    'terms'    => $this->attributes['feature'],
                );
            }
        }
        // Query by ID.
        if ( ! empty( $this->attributes['feature_id'] ) ) {
            if ( ! is_array( $this->attributes['feature_id'] ) ) {
                $this->attributes['feature_id'] = array_map( 'trim', explode( ',', $this->attributes['feature_id'] ) );

                $this->args['tax_query']['tax_feature_id_clause'] = array(
                    'taxonomy' => 'tax_feature',
                    'field'    => 'id',
                    'terms'    => $this->attributes['feature_id'],
                );
            }
        }
    }

    /**
     * Set status
     *
     * @since 3.3
     */
    public function set_status() {

        if ( ! empty( $this->attributes['status'] ) ) {
            if ( ! is_array( $this->attributes['status'] ) ) {
                $this->attributes['status'] = array_map( 'trim', explode( ',', $this->attributes['status'] ) );

                $this->args['meta_query']['property_status_clause'] = array(
                    'key'     => 'property_status',
                    'value'   => $this->attributes['status'],
                    'compare' => 'IN',
                );

                add_filter( 'epl_sorting_options', 'epl_sorting_options_callback' );
            }
        }
    }

    /**
     * Set commercial listing type
     *
     * @since 3.3
     */
    public function set_commercial_listing_type() {

        /** Commercial listing type */
        if ( ! empty( $this->attributes['commercial_listing_type'] ) ) {
            if ( ! is_array( $this->attributes['commercial_listing_type'] ) ) {
                $this->attributes['commercial_listing_type'] = array_map( 'trim', explode( ',', $this->attributes['commercial_listing_type'] ) );

                $this->args['meta_query']['property_com_listing_type_clause'] = array(
                    'key'     => 'property_com_listing_type',
                    'value'   => $this->attributes['commercial_listing_type'],
                    'compare' => 'IN',
                );
            }
        }
    }

    /**
     * Set orderby
     *
     * @since 3.3
     */
    public function set_orderby() {

        if ( ! empty( $this->attributes['sortby'] ) ) {
            if ( 'price' === $this->attributes['sortby'] ) {
                $this->args['orderby']  = 'meta_value_num';
                $this->args['meta_key'] = $this->get_meta_key_price();
            } elseif ( 'rand' === $this->attributes['sortby'] ) {
                $this->args['orderby'] = 'rand';
            } elseif ( 'status' === $this->attributes['sortby'] ) {
                $this->args['orderby']  = 'meta_value';
                $this->args['meta_key'] = 'property_status';
            } else {
                $this->args['orderby'] = 'post_date';
                $this->args['order']   = 'DESC';
            }
            $this->args['order'] = $this->attributes['sort_order'];
        }
    }

    /**
     * Set orderby clause
     *
     * @since 3.3
     * @since 3.4.35 Fixed the issue : [listing_advanced] not marked as EPL shortcode in WP_Query object
     */
    public function set_orderby_clause() {

        /**
         * Advance orderby using named meta query clauses, will override default orderby.
         */

        if ( ! empty( $this->attributes['orderby_clause'] ) ) {

            $this->attributes['orderby_clause'] =
            array_map( 'trim', explode( ',', $this->attributes['orderby_clause'] ) );
            foreach ( $this->attributes['orderby_clause'] as $oc ) {
                $oc_array = explode( '|', $oc );

                $this->args['orderby'][ $oc_array[0] ] = $oc_array[1];

            }

            unset( $this->args['order'] );
        }

        $this->args['instance_id'] = $this->attributes['instance_id'];

        // add sortby arguments to query, if listings sorted by $_GET['sortby'];.
        $this->args = epl_add_orderby_args( $this->args, 'shortcode', 'listing_advanced' );
        // Option to filter args.
        $this->args = apply_filters( 'epl_shortcode_listing_advanced_args', $this->args, $this->attributes );
    }

    /**
     * Set query
     *
     * @since 3.3
     */
    public function set_query() {

        $this->query_open = new WP_Query( $this->args );

        if ( is_object( $this->attributes['query_object'] ) ) {
            $this->query_open = $this->attributes['query_object'];
        }
    }

    /**
     * Get the template
     *
     * @since 3.3
     */
    public function get_wrap_template() {

        $attributes['wrap_template'] = str_replace( '_', '-', $this->attributes['wrap_template'] );

        $wrap_template = empty( $this->attributes['wrap_template'] ) ? 'shortcode-listing.php' : 'shortcode-listing-' . $this->attributes['wrap_template'] . '.php';

        return $wrap_template;
    }

    /**
     * Build the query
     *
     * @since 3.3
     */
    public function build_query() {

        $this->set_post_type();
        $this->set_initial_args();
        $this->set_offset();
        $this->set_post__in();
        $this->set_post__not_in();
        $this->process_epl_atts();
        $this->set_author();
        $this->set_agent();
        $this->set_featured();
        $this->set_auction();
        $this->set_open_house();
        $this->set_location_tax_query();
        $this->set_features_tax_query();
        $this->set_status();
        $this->set_commercial_listing_type();
        $this->set_orderby();
        $this->set_orderby_clause();
    }

    /**
     * Render the shortcode
     *
     * @since 3.3
     */
    public function render() {
        $wrap_template = $this->get_wrap_template();
        ob_start();
        epl_get_template_part(
            $wrap_template,
            array(
                'attributes' => $this->attributes,
                'query_open' => $this->query_open,
            )
        );

        return ob_get_clean();
    }
}
Easy Property Listings 3.5.15 Code Reference API documentation generated by ApiGen