1: <?php
2: /**
3: * Elementor Integration for Easy Property Listings
4: *
5: * @package EPL
6: * @subpackage PageBuilders/Elementor
7: * @copyright Copyright (c) 2024
8: * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
9: * @since 3.6.0
10: */
11:
12: // Exit if accessed directly.
13: if ( ! defined( 'ABSPATH' ) ) {
14: exit;
15: }
16:
17: /** Ensure listing-context widgets are never stored in Elementor Element Cache. */
18: trait EPL_Elementor_Dynamic_Widget {
19: /**
20: * Keep this widget out of the Elementor element cache.
21: *
22: * @since 3.6.0 New: Elementor widgets (beta).
23: */
24: protected function is_dynamic_content(): bool {
25: return true;
26: }
27: }
28:
29: /**
30: * EPL_Elementor Class
31: *
32: * Main class for Elementor integration.
33: *
34: * @since 3.6.0
35: */
36: class EPL_Elementor {
37:
38: /**
39: * Instance
40: *
41: * @var EPL_Elementor The single instance of the class.
42: * @since 3.6.0
43: */
44: private static $instance = null;
45:
46: /**
47: * Minimum Elementor Version
48: *
49: * The elementor/widgets/register hook and Widgets_Manager::register()
50: * used below were introduced in Elementor 3.5.
51: *
52: * @var string Minimum Elementor version required.
53: * @since 3.6.0
54: */
55: const MINIMUM_ELEMENTOR_VERSION = '3.5.0';
56:
57: /**
58: * Widgets path
59: *
60: * @var string Path to widgets directory.
61: * @since 3.6.0
62: */
63: private $widgets_path;
64:
65: /** @var array<string,WP_Query> Queries produced by Elementor Loop Grid widgets. */
66: private $loop_grid_queries = array();
67:
68: /** @var array Settings from the Loop Grid currently being rendered. */
69: private $active_loop_grid_settings = array();
70:
71: /**
72: * Constructor
73: *
74: * @since 3.6.0
75: */
76: public function __construct() {
77: $this->widgets_path = EPL_PATH_LIB . 'page-builders/elementor/widgets/';
78: require_once EPL_PATH_LIB . 'page-builders/elementor/class-epl-elementor-agent-document.php';
79: require_once EPL_PATH_LIB . 'page-builders/elementor/class-epl-elementor-listing-document.php';
80: require_once EPL_PATH_LIB . 'page-builders/elementor/class-epl-elementor-single-document.php';
81: require_once EPL_PATH_LIB . 'page-builders/elementor/class-epl-elementor-loop-card-document.php';
82: require_once EPL_PATH_LIB . 'page-builders/elementor/class-epl-elementor-template-router.php';
83: require_once EPL_PATH_LIB . 'page-builders/elementor/class-epl-elementor-template-seeder.php';
84:
85: // Check Elementor version.
86: if ( ! $this->is_compatible() ) {
87: add_action( 'admin_notices', array( $this, 'admin_notice_minimum_elementor_version' ) );
88: return;
89: }
90:
91: // Register widget categories.
92: add_action( 'elementor/elements/categories_registered', array( $this, 'register_widget_categories' ) );
93:
94: // Register widgets.
95: // Register after Elementor Pro so Loop Grid availability can be detected
96: // from the widget registry rather than from plugin names or class files.
97: add_action( 'elementor/widgets/register', array( $this, 'register_widgets' ), 100 );
98: add_action( 'elementor/documents/register', array( $this, 'register_documents' ) );
99:
100: // Enqueue styles.
101: add_action( 'elementor/frontend/after_enqueue_styles', array( $this, 'frontend_styles' ) );
102: add_action( 'elementor/editor/after_enqueue_scripts', array( $this, 'editor_scripts' ) );
103:
104: // Give Elementor Pro Loop Grid users a documented EPL query ID.
105: add_action( 'elementor/query/epl_listings', array( $this, 'elementor_listings_query' ), 10, 2 );
106: add_action( 'elementor/query/epl_staff', array( $this, 'elementor_staff_query' ), 10, 2 );
107: add_filter( 'elementor/query/query_args', array( $this, 'elementor_loop_query_args' ), 20, 2 );
108: add_filter( 'elementor/query/get_query_args/current_query', array( $this, 'elementor_current_query_args' ), 20 );
109: add_action( 'elementor/frontend/widget/before_render', array( $this, 'begin_loop_grid_render' ) );
110: add_action( 'elementor/frontend/widget/after_render', array( $this, 'end_loop_grid_render' ) );
111: add_action( 'elementor/query/query_results', array( $this, 'capture_loop_grid_query' ), 20, 2 );
112: add_action( 'elementor/element/loop-grid/section_pagination/before_section_end', array( $this, 'register_loop_grid_pagination_provider' ) );
113: add_filter( 'elementor/widget/render_content', array( $this, 'render_loop_grid_epl_pagination' ), 20, 2 );
114: add_filter( 'pre_handle_404', array( $this, 'allow_epl_loop_grid_page' ), 20, 2 );
115: add_filter( 'elementor/theme/need_override_location', array( $this, 'override_staff_directory_location' ), 20, 3 );
116:
117: // Elementor can clear Theme Builder conditions while an Archive document
118: // is switched to/from a Loop Item in the editor. Keep a last-known-good
119: // copy for EPL archive documents and restore it after that save request.
120: add_action( 'added_post_meta', array( $this, 'track_archive_conditions' ), 10, 4 );
121: add_action( 'updated_post_meta', array( $this, 'track_archive_conditions' ), 10, 4 );
122: add_action( 'deleted_post_meta', array( $this, 'track_archive_conditions' ), 10, 4 );
123:
124: // Free-Elementor Theme Builder equivalent: single listing templates,
125: // default listing cards, and archive pages.
126: new EPL_Elementor_Template_Router();
127:
128: // Install the design templates that ship with the plugin.
129: new EPL_Elementor_Template_Seeder();
130: }
131:
132: /**
133: * Register dedicated Elementor documents.
134: *
135: * @param \Elementor\Core\Documents_Manager $documents_manager Documents manager.
136: * @since 3.6.0 New: Elementor widgets (beta).
137: */
138: public function register_documents( $documents_manager ) {
139: $documents_manager->register_document_type( EPL_Elementor_Agent_Document::TYPE, EPL_Elementor_Agent_Document::get_class_full_name() );
140: $documents_manager->register_document_type( EPL_Elementor_Single_Document::TYPE, EPL_Elementor_Single_Document::get_class_full_name() );
141: $documents_manager->register_document_type( EPL_Elementor_Loop_Card_Document::TYPE, EPL_Elementor_Loop_Card_Document::get_class_full_name() );
142: }
143:
144: /**
145: * Instance
146: *
147: * @return EPL_Elementor
148: * @since 3.6.0
149: */
150: public static function instance() {
151: if ( is_null( self::$instance ) ) {
152: self::$instance = new self();
153: }
154: return self::$instance;
155: }
156:
157: /**
158: * Check compatibility
159: *
160: * @return bool
161: * @since 3.6.0
162: */
163: private function is_compatible() {
164: if ( ! defined( 'ELEMENTOR_VERSION' ) ) {
165: return false;
166: }
167: return version_compare( ELEMENTOR_VERSION, self::MINIMUM_ELEMENTOR_VERSION, '>=' );
168: }
169:
170: /**
171: * Admin notice for minimum Elementor version
172: *
173: * @since 3.6.0
174: */
175: public function admin_notice_minimum_elementor_version() {
176: ?>
177: <div class="notice notice-warning is-dismissible">
178: <p>
179: <?php
180: echo wp_kses(
181: sprintf(
182: /* translators: 1: Plugin name 2: Elementor 3: Version */
183: esc_html__( '"%1$s" requires "%2$s" version %3$s or greater.', 'easy-property-listings' ),
184: '<strong>' . esc_html__( 'Easy Property Listings - Elementor', 'easy-property-listings' ) . '</strong>',
185: '<strong>Elementor</strong>',
186: esc_html( self::MINIMUM_ELEMENTOR_VERSION )
187: ),
188: array( 'strong' => array() )
189: );
190: ?>
191: </p>
192: </div>
193: <?php
194: }
195:
196: /**
197: * Register Widget Categories
198: *
199: * @param \Elementor\Elements_Manager $elements_manager Elementor elements manager.
200: * @since 3.6.0
201: */
202: public function register_widget_categories( $elements_manager ) {
203: $elements_manager->add_category(
204: 'epl-elements',
205: array(
206: 'title' => esc_html__( 'EPL Elements (Beta)', 'easy-property-listings' ),
207: 'icon' => 'eicon-apps',
208: )
209: );
210:
211: $elements_manager->add_category(
212: 'epl-archive',
213: array(
214: 'title' => esc_html__( 'EPL Archive (Beta)', 'easy-property-listings' ),
215: 'icon' => 'eicon-archive',
216: )
217: );
218:
219: $elements_manager->add_category(
220: 'epl-author',
221: array(
222: 'title' => esc_html__( 'EPL Author (Beta)', 'easy-property-listings' ),
223: 'icon' => 'eicon-person',
224: )
225: );
226:
227: $elements_manager->add_category(
228: 'epl-staff',
229: array(
230: 'title' => esc_html__( 'EPL Staff Directory (Beta)', 'easy-property-listings' ),
231: 'icon' => 'eicon-person',
232: )
233: );
234: }
235:
236: /**
237: * Register Widgets
238: *
239: * @param \Elementor\Widgets_Manager $widgets_manager Elementor widgets manager.
240: * @since 3.6.0
241: */
242: public function register_widgets( $widgets_manager ) {
243: $this->include_widget_files();
244:
245: // Element widgets.
246: $element_widgets = array(
247: 'EPL_Elementor_Property_Title',
248: 'EPL_Elementor_Property_Heading',
249: 'EPL_Elementor_Property_Price',
250: 'EPL_Elementor_Property_Address',
251: 'EPL_Elementor_Property_Icons',
252: 'EPL_Elementor_Property_Image',
253: 'EPL_Elementor_Property_Content',
254: 'EPL_Elementor_Property_Excerpt',
255: 'EPL_Elementor_Property_Features',
256: 'EPL_Elementor_Property_Inspection',
257: 'EPL_Elementor_Agent_Box',
258: 'EPL_Elementor_Listing_Agents',
259: 'EPL_Elementor_Agent_Photo',
260: 'EPL_Elementor_Agent_Name',
261: 'EPL_Elementor_Agent_Position',
262: 'EPL_Elementor_Agent_Contact',
263: 'EPL_Elementor_Agent_Social',
264: 'EPL_Elementor_Agent_Bio',
265: 'EPL_Elementor_Staff_Listings',
266: 'EPL_Elementor_Property_Map',
267: 'EPL_Elementor_Property_Gallery',
268: 'EPL_Elementor_Property_Video',
269: 'EPL_Elementor_Property_Buttons',
270: 'EPL_Elementor_Property_Status',
271: 'EPL_Elementor_Property_Meta',
272: 'EPL_Elementor_Property_Taxonomy',
273: 'EPL_Elementor_Single_Extensions',
274: );
275:
276: foreach ( $element_widgets as $widget_class ) {
277: if ( class_exists( $widget_class ) ) {
278: $widgets_manager->register( new $widget_class() );
279: }
280: }
281:
282: // Archive widgets.
283: $archive_widgets = array(
284: 'EPL_Elementor_Listing_Advanced',
285: 'EPL_Elementor_Listing_Search',
286: 'EPL_Elementor_Listing_Results',
287: 'EPL_Elementor_Pagination',
288: );
289:
290: // Elementor's Loop Grid is a Pro feature. Free users get an EPL-native
291: // listings widget; Pro users keep one clear loop-building workflow.
292: if ( ! $this->has_elementor_loop_grid( $widgets_manager ) ) {
293: $archive_widgets[] = 'EPL_Elementor_Listings';
294: }
295:
296: foreach ( $archive_widgets as $widget_class ) {
297: if ( class_exists( $widget_class ) ) {
298: $widgets_manager->register( new $widget_class() );
299: }
300: }
301: }
302:
303: /**
304: * Include Widget Files
305: *
306: * @since 3.6.0
307: */
308: private function include_widget_files() {
309: // Element widgets.
310: $elements = array(
311: 'class-epl-elementor-property-title.php',
312: 'class-epl-elementor-property-heading.php',
313: 'class-epl-elementor-property-price.php',
314: 'class-epl-elementor-property-address.php',
315: 'class-epl-elementor-property-icons.php',
316: 'class-epl-elementor-property-image.php',
317: 'class-epl-elementor-property-content.php',
318: 'class-epl-elementor-property-excerpt.php',
319: 'class-epl-elementor-property-features.php',
320: 'class-epl-elementor-property-inspection.php',
321: 'class-epl-elementor-agent-box.php',
322: 'class-epl-elementor-agents-loop.php',
323: 'class-epl-elementor-agent-photo.php',
324: 'class-epl-elementor-agent-name.php',
325: 'class-epl-elementor-agent-position.php',
326: 'class-epl-elementor-agent-contact.php',
327: 'class-epl-elementor-agent-social.php',
328: 'class-epl-elementor-agent-bio.php',
329: 'class-epl-elementor-staff-listings.php',
330: 'class-epl-elementor-property-map.php',
331: 'class-epl-elementor-property-gallery.php',
332: 'class-epl-elementor-property-video.php',
333: 'class-epl-elementor-property-buttons.php',
334: 'class-epl-elementor-property-status.php',
335: 'class-epl-elementor-property-meta.php',
336: 'class-epl-elementor-property-taxonomy.php',
337: 'class-epl-elementor-single-extensions.php',
338: );
339:
340: foreach ( $elements as $file ) {
341: $path = $this->widgets_path . 'elements/' . $file;
342: if ( file_exists( $path ) ) {
343: require_once $path;
344: }
345: }
346:
347: // Archive widgets.
348: $archives = array(
349: 'class-epl-elementor-listing-advanced.php',
350: 'class-epl-elementor-listings.php',
351: 'class-epl-elementor-listing-search.php',
352: 'class-epl-elementor-listing-results.php',
353: 'class-epl-elementor-pagination.php',
354: );
355:
356: foreach ( $archives as $file ) {
357: $path = $this->widgets_path . 'archive/' . $file;
358: if ( file_exists( $path ) ) {
359: require_once $path;
360: }
361: }
362: }
363:
364: /**
365: * Determine whether Elementor's native Loop Grid is available.
366: *
367: * Checking the registered widget supports Elementor Pro as well as
368: * compatible distributions without relying on a plugin basename.
369: *
370: * @param \Elementor\Widgets_Manager $widgets_manager Widgets manager.
371: * @return bool
372: * @since 3.6.0 New: Elementor widgets (beta).
373: */
374: private function has_elementor_loop_grid( $widgets_manager ) {
375: $available = (bool) $widgets_manager->get_widget_types( 'loop-grid' );
376:
377: return (bool) apply_filters( 'epl_elementor_has_loop_grid', $available, $widgets_manager );
378: }
379:
380: /**
381: * Frontend Styles
382: *
383: * @since 3.6.0
384: */
385: public function frontend_styles() {
386: wp_enqueue_style(
387: 'epl-elementor',
388: EPL_PLUGIN_URL . 'lib/assets/css/epl-elementor.css',
389: array(),
390: EPL_PROPERTY_VER
391: );
392:
393: // Tabs functionality for the agents loop widget. A delegated listener
394: // keeps working for content injected after load (Elementor editor
395: // preview) and scopes the tab lookup to the widget container so
396: // multiple widgets on one page do not interfere with each other.
397: wp_register_script( 'epl-elementor-tabs', '', array(), EPL_PROPERTY_VER, true );
398: wp_enqueue_script( 'epl-elementor-tabs' );
399: wp_add_inline_script(
400: 'epl-elementor-tabs',
401: '
402: document.addEventListener("click", function(e) {
403: var button = e.target.closest(".epl-agents-tabs .epl-tab-button");
404: if (!button) {
405: return;
406: }
407: var container = button.closest(".epl-agents-tabs");
408: var tabId = button.getAttribute("data-tab");
409:
410: container.querySelectorAll(".epl-tab-button").forEach(function(btn) {
411: btn.classList.remove("active");
412: });
413: container.querySelectorAll(".epl-tab-content").forEach(function(content) {
414: content.classList.remove("active");
415: });
416:
417: button.classList.add("active");
418: var content = container.querySelector("[id=\"" + tabId + "\"]") || document.getElementById(tabId);
419: if (content) {
420: content.classList.add("active");
421: }
422: });
423: '
424: );
425:
426: // Sync dual-view Listing Advanced grids with EPL's grid/list switch. The
427: // switch is client-side only (it flips a CSS class, no re-render), so we
428: // mirror the selected view onto our own grid container. The initial state
429: // is set server-side to avoid a flash of the wrong view on load.
430: wp_add_inline_script(
431: 'epl-elementor-tabs',
432: '
433: document.addEventListener("click", function(e) {
434: var toggle = e.target.closest(".epl-switch-view ul li[data-view]");
435: if (!toggle) {
436: return;
437: }
438: var isList = "list" === toggle.getAttribute("data-view");
439: document.querySelectorAll(".epl-elementor-advanced-grid.epl-ea-dual-view").forEach(function(grid) {
440: grid.classList.toggle("is-list-view", isList);
441: grid.classList.toggle("is-grid-view", !isList);
442: });
443: });
444: '
445: );
446: }
447:
448: /**
449: * Editor Scripts
450: *
451: * Two independent editor-only fixes, both injected as a single inline
452: * script on the real `elementor-editor` script:
453: *
454: * 1. Works around an Elementor editor bug where a responsive control that
455: * also has a `condition` (e.g. `grid_columns` on the Archive Results
456: * widget) can get stuck with a stale inline `display: none;` on its
457: * wrapper — set once during the control's initial render and never
458: * cleared afterwards, even once its condition is satisfied and
459: * Elementor's own Conditions module has correctly removed the
460: * `elementor-hidden-control` class. A responsive control with no
461: * `condition` (e.g. `grid_gap`) never has this problem: Elementor's
462: * device-tab switcher shows/hides those purely via CSS classes, never
463: * inline styles, so an inline `display: none;` with the hidden-control
464: * class absent is always this bug, never a legitimate state — safe to
465: * clear unconditionally, for any widget's controls, not just EPL's own.
466: *
467: * 2. Narrows the editing canvas to roughly a single card's width while
468: * editing an EPL Listing Card document, per its own "Editing Preview
469: * Width" document setting — see EPL_Elementor_Loop_Card_Document.
470: *
471: * @since 3.6.0
472: * @since 3.6.0 New: Elementor templates for free Elementor (beta).
473: */
474: public function editor_scripts() {
475: wp_register_script( 'epl-elementor-editor-fixes', '', array( 'elementor-editor' ), EPL_PROPERTY_VER, true );
476: wp_enqueue_script( 'epl-elementor-editor-fixes' );
477: wp_add_inline_script(
478: 'epl-elementor-editor-fixes',
479: '
480: ( function () {
481: function isStuckHidden( el ) {
482: return el.style.display === "none" && ! el.classList.contains( "elementor-hidden-control" );
483: }
484:
485: function sweep( root ) {
486: root.querySelectorAll( ".elementor-control" ).forEach( function ( el ) {
487: if ( isStuckHidden( el ) ) {
488: el.style.removeProperty( "display" );
489: }
490: } );
491: }
492:
493: function watch( panel ) {
494: sweep( panel );
495:
496: new MutationObserver( function ( mutations ) {
497: mutations.forEach( function ( mutation ) {
498: if ( "attributes" === mutation.type ) {
499: if ( mutation.target.classList && mutation.target.classList.contains( "elementor-control" ) && isStuckHidden( mutation.target ) ) {
500: mutation.target.style.removeProperty( "display" );
501: }
502: return;
503: }
504:
505: mutation.addedNodes.forEach( function ( node ) {
506: if ( 1 === node.nodeType ) {
507: sweep( node );
508: }
509: } );
510: } );
511: } ).observe( panel, {
512: childList: true,
513: subtree: true,
514: attributes: true,
515: attributeFilter: [ "style", "class" ],
516: } );
517: }
518:
519: elementor.on( "panel:init", function () {
520: var panel = document.getElementById( "elementor-panel" );
521: if ( panel ) {
522: watch( panel );
523: }
524: } );
525: } )();
526:
527: // An EPL Listing Card document is a single card, meant to sit
528: // narrow inside a grid — left alone it renders full page width in
529: // the editor, which makes it hard to judge how it will actually
530: // look. When editing this document type, narrow the preview
531: // canvas to the "Editing Preview Width" document setting
532: // (default 320px) and frame it like an isolated card on a grey
533: // backdrop. Purely an editor affordance — never touches front-end
534: // output, and every other document type renders full width as
535: // before.
536: ( function () {
537: function applyPreviewWidth() {
538: var config = elementor.config && elementor.config.document;
539: if ( ! config || "epl-loop-card" !== config.type ) {
540: return;
541: }
542:
543: var iframe = document.getElementById( "elementor-preview-iframe" );
544: var doc = iframe && iframe.contentDocument;
545: if ( ! doc || ! doc.head ) {
546: return;
547: }
548:
549: var width = 320;
550: if ( elementor.settings && elementor.settings.page && elementor.settings.page.model ) {
551: var configured = parseInt( elementor.settings.page.model.get( "epl_card_preview_width" ), 10 );
552: if ( configured ) {
553: width = configured;
554: }
555: }
556:
557: var style = doc.getElementById( "epl-card-preview-style" );
558: if ( ! style ) {
559: style = doc.createElement( "style" );
560: style.id = "epl-card-preview-style";
561: doc.head.appendChild( style );
562: }
563:
564: // The card document still renders through the active theme
565: // normal single-post wrapper (header, page title, footer) —
566: // there is no bare data-elementor-type directly under body
567: // to key off. Hide that surrounding chrome and narrow the
568: // actual Elementor wrapper wherever it sits in the tree
569: // instead.
570: style.textContent = (
571: "body { background: #e4e4e4 !important; }" +
572: "#site-header, #site-footer, .page-header, .site-header, .site-footer { display: none !important; }" +
573: "[data-elementor-type] { max-width: " + width + "px !important; margin: 40px auto !important; float: none !important; " +
574: "background: #fff !important; box-shadow: 0 0 0 1px rgba(0,0,0,.08), 0 12px 28px rgba(0,0,0,.15) !important; }"
575: );
576: }
577:
578: elementor.on( "preview:loaded", function () {
579: applyPreviewWidth();
580:
581: if ( elementor.settings && elementor.settings.page && elementor.settings.page.model ) {
582: elementor.settings.page.model.on( "change:epl_card_preview_width", applyPreviewWidth );
583: }
584: } );
585: } )();
586: '
587: );
588: }
589:
590: /**
591: * Get EPL Post Types
592: *
593: * @return array
594: * @since 3.6.0
595: */
596: public static function get_post_types() {
597: $post_types = array();
598: $epl_posts = epl_get_core_post_types();
599:
600: foreach ( $epl_posts as $post_type ) {
601: $obj = get_post_type_object( $post_type );
602: if ( $obj ) {
603: $post_types[ $post_type ] = $obj->labels->singular_name;
604: }
605: }
606:
607: return $post_types;
608: }
609:
610: /**
611: * Get Listing Status Options
612: *
613: * @return array
614: * @since 3.6.0
615: */
616: public static function get_status_options() {
617: return array(
618: '' => esc_html__( 'All', 'easy-property-listings' ),
619: 'current' => esc_html__( 'Current', 'easy-property-listings' ),
620: 'sold' => esc_html__( 'Sold', 'easy-property-listings' ),
621: 'leased' => esc_html__( 'Leased', 'easy-property-listings' ),
622: 'withdrawn' => esc_html__( 'Withdrawn', 'easy-property-listings' ),
623: 'offmarket' => esc_html__( 'Off Market', 'easy-property-listings' ),
624: );
625: }
626:
627: /**
628: * Listing context stack for widget renders.
629: *
630: * @var array
631: * @since 3.6.0
632: */
633: private static $context_stack = array();
634:
635: /**
636: * Set up the global listing context for a widget render.
637: *
638: * Resolves the property from the existing global, the current post, or —
639: * in the Elementor editor — a preview listing. When a preview listing is
640: * used, the global $post is switched as well so core template callbacks
641: * that read the global post (galleries, stickers, content) work too.
642: *
643: * Always pair with restore_listing_context() before render() returns.
644: *
645: * @return EPL_Property_Meta|false The property object or false if none found.
646: * @since 3.6.0
647: */
648: public static function setup_listing_context() {
649: global $property, $post, $epl_author, $epl_author_secondary, $epl_author_third, $epl_author_fourth;
650:
651: $frame = array(
652: 'post' => isset( $post ) ? $post : null,
653: 'property' => isset( $property ) ? $property : null,
654: 'epl_author' => isset( $epl_author ) ? $epl_author : null,
655: 'epl_author_secondary' => isset( $epl_author_secondary ) ? $epl_author_secondary : null,
656: 'epl_author_third' => isset( $epl_author_third ) ? $epl_author_third : null,
657: 'epl_author_fourth' => isset( $epl_author_fourth ) ? $epl_author_fourth : null,
658: );
659:
660: // The current loop post is authoritative. A global property can be stale
661: // while Elementor renders nested loop documents.
662: if ( $post && is_epl_post( $post->post_type ) && ( ! $property || (int) $property->post->ID !== (int) $post->ID ) ) {
663: $property = new EPL_Property_Meta( $post );
664: }
665:
666: // Nested Loop Item documents can temporarily expose the Archive document
667: // or a regular post as the global post while Elementor builds its editor
668: // canvas. In that case a stale global property must not suppress the EPL
669: // preview fallback.
670: if ( ( self::is_editor() || self::is_epl_elementor_document_context() ) && ( ! $post || ! is_epl_post( $post->post_type ) ) ) {
671: $preview = self::get_preview_property();
672:
673: if ( $preview ) {
674: $property = $preview;
675: $post = $preview->post; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Restored in restore_listing_context().
676: setup_postdata( $post );
677: }
678: }
679:
680: self::$context_stack[] = $frame;
681:
682: return $property ? $property : false;
683: }
684:
685: /**
686: * Restore the global listing context after a widget render.
687: *
688: * @since 3.6.0
689: */
690: public static function restore_listing_context() {
691: global $property, $post, $epl_author, $epl_author_secondary, $epl_author_third, $epl_author_fourth;
692:
693: $frame = array_pop( self::$context_stack );
694:
695: if ( null === $frame ) {
696: return;
697: }
698:
699: $post = $frame['post']; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Restoring original.
700: $property = $frame['property']; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Restoring original.
701: $epl_author = $frame['epl_author']; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Restoring original.
702: $epl_author_secondary = $frame['epl_author_secondary']; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Restoring original.
703: $epl_author_third = $frame['epl_author_third']; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Restoring original.
704: $epl_author_fourth = $frame['epl_author_fourth']; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Restoring original.
705:
706: if ( $post instanceof WP_Post ) {
707: setup_postdata( $post );
708: }
709: }
710:
711: /**
712: * Whether Elementor is rendering an editor preview.
713: *
714: * @return bool
715: * @since 3.6.0 New: Elementor widgets (beta).
716: * @since 3.6.0 Security: a preview URL or Elementor AJAX request needs a user who can edit posts.
717: */
718: public static function is_editor() {
719: $is_edit_mode = isset( \Elementor\Plugin::$instance->editor ) && \Elementor\Plugin::$instance->editor->is_edit_mode();
720: $is_preview = isset( \Elementor\Plugin::$instance->preview ) && \Elementor\Plugin::$instance->preview->is_preview_mode();
721: $is_preview_request = isset( $_GET['elementor-preview'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only preview detection.
722: $ajax_action = isset( $_REQUEST['action'] ) ? sanitize_key( wp_unslash( $_REQUEST['action'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only editor request detection.
723: $is_elementor_ajax = wp_doing_ajax() && 0 === strpos( $ajax_action, 'elementor' );
724:
725: // A preview URL or Elementor AJAX request only counts for someone who can edit (anyone can add ?elementor-preview to a URL).
726: if ( ( $is_preview_request || $is_elementor_ajax ) && ! current_user_can( 'edit_posts' ) ) {
727: $is_preview_request = false;
728: $is_elementor_ajax = false;
729: }
730:
731: return $is_edit_mode || $is_preview || $is_preview_request || $is_elementor_ajax;
732: }
733:
734: /**
735: * Detect an EPL Loop Item while Elementor renders a nested document.
736: *
737: * Elementor does not consistently expose edit/preview mode while a Loop Item
738: * is rendered inside an Archive document. Inspect the active Elementor
739: * documents so dynamic EPL widgets can still receive preview listing data.
740: *
741: * @return bool
742: * @since 3.6.0 New: Elementor widgets (beta).
743: */
744: private static function is_epl_elementor_document_context() {
745: $document_ids = array();
746: $current_post = get_post();
747:
748: if ( $current_post && 'elementor_library' === $current_post->post_type ) {
749: $document_ids[] = (int) $current_post->ID;
750: }
751:
752: if ( isset( \Elementor\Plugin::$instance->documents ) ) {
753: $current_document = \Elementor\Plugin::$instance->documents->get_current();
754: if ( $current_document ) {
755: $document_ids[] = (int) $current_document->get_main_id();
756: }
757: }
758:
759: foreach ( array_unique( array_filter( $document_ids ) ) as $document_id ) {
760: $data = (string) get_post_meta( $document_id, '_elementor_data', true );
761: if ( 'listing' === self::get_epl_template_context( $document_id ) ) {
762: return true;
763: }
764:
765: if ( preg_match_all( '/"template_id":(?:"?)(\d+)(?:"?)/', $data, $matches ) ) {
766: foreach ( $matches[1] as $template_id ) {
767: if ( 'listing' === self::get_epl_template_context( absint( $template_id ) ) ) {
768: return true;
769: }
770: }
771: }
772: }
773:
774: return false;
775: }
776:
777: /**
778: * Resolve one listing agent without assuming agents are WordPress users.
779: *
780: * @param EPL_Property_Meta $property Listing property.
781: * @param int $index Zero-based agent position.
782: * @return EPL_Author_Loader|null
783: * @since 3.6.0 New: Elementor widgets (beta).
784: */
785: public static function get_listing_agent( $property, $index = 0 ) {
786: if ( 0 === $index && isset( $property->post ) && 'directory' === $property->post->post_type ) {
787: return new EPL_Author_Loader( $property->post->ID );
788: }
789:
790: $keys = array( 'property_agent', 'property_second_agent', 'property_third_agent', 'property_fourth_agent' );
791: $key = isset( $keys[ $index ] ) ? $keys[ $index ] : $keys[0];
792: $raw = $property->get_property_meta( $key );
793: $id = 0;
794:
795: // Staff Directory stores the canonical identity in a directory post.
796: if ( 'directory' === epl_get_option( 'epl_sd_staff_details_location', 'wp_user' ) && post_type_exists( 'directory' ) && ! empty( $raw ) ) {
797: $directory = self::get_directory_agent( $raw );
798: $id = $directory instanceof WP_Post ? $directory->ID : 0;
799: } elseif ( ! empty( $raw ) ) {
800: $user = get_user_by( 'login', sanitize_user( (string) $raw ) );
801: $id = $user ? $user->ID : ( is_numeric( $raw ) ? absint( $raw ) : 0 );
802: }
803:
804: if ( 0 === $index && ! $id && isset( $property->post->post_author ) ) {
805: $id = absint( $property->post->post_author );
806: }
807:
808: $id = absint( apply_filters( 'epl_elementor_agent_id', $id, $raw, $index, $property ) );
809: return $id ? new EPL_Author_Loader( $id ) : null;
810: }
811:
812: /**
813: * Resolve a Staff Directory username deterministically.
814: *
815: * Imports can leave more than one directory post with the same staff
816: * username. Prefer the record with a featured image and then the oldest ID,
817: * so editor and frontend queries always select the same canonical record.
818: *
819: * @param string $username Staff username stored on the listing.
820: * @return WP_Post|null
821: * @since 3.6.0 New: Elementor widgets (beta).
822: */
823: private static function get_directory_agent( $username ) {
824: $posts = get_posts(
825: array(
826: 'post_type' => 'directory',
827: 'post_status' => 'publish',
828: 'posts_per_page' => -1,
829: 'orderby' => 'ID',
830: 'order' => 'ASC',
831: 'meta_key' => 'property_staff_username', // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
832: 'meta_value' => sanitize_user( (string) $username ), // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
833: )
834: );
835:
836: if ( empty( $posts ) ) {
837: return null;
838: }
839:
840: foreach ( $posts as $directory ) {
841: if ( has_post_thumbnail( $directory ) ) {
842: return apply_filters( 'epl_elementor_directory_agent', $directory, $posts, $username );
843: }
844: }
845:
846: return apply_filters( 'epl_elementor_directory_agent', reset( $posts ), $posts, $username );
847: }
848:
849: /**
850: * Resolve all unique agents assigned to a listing.
851: *
852: * @param EPL_Property_Meta $property Listing property.
853: * @return array
854: * @since 3.6.0 New: Elementor widgets (beta).
855: */
856: public static function get_listing_agents( $property ) {
857: $agents = array();
858: $seen = array();
859:
860: for ( $index = 0; $index < 4; $index++ ) {
861: $agent = self::get_listing_agent( $property, $index );
862: if ( $agent && ! isset( $seen[ (string) $agent->author_id ] ) ) {
863: $seen[ (string) $agent->author_id ] = true;
864: $agents[] = $agent;
865: }
866: }
867:
868: return apply_filters( 'epl_elementor_listing_agents', $agents, $property );
869: }
870:
871: /**
872: * Get an agent image, including Staff Directory post-backed images.
873: *
874: * Staff Directory's author-box image filter is conditionally attached and
875: * can be unavailable while Elementor renders nested documents on an archive.
876: * Resolve its canonical directory image first, then retain the public EPL
877: * filter so extensions can make a final adjustment.
878: *
879: * @param EPL_Author_Loader $agent Agent loader.
880: * @param int $size Requested image size in pixels.
881: * @return string
882: * @since 3.6.0 New: Elementor widgets (beta).
883: */
884: public static function get_agent_image( $agent, $size = 150 ) {
885: $size = max( 1, absint( $size ) );
886: $image_html = get_avatar( $agent->email, $size );
887: $directory = ! empty( $agent->directory ) && $agent->directory instanceof WP_Post ? $agent->directory : null;
888:
889: if ( $directory && 'directory' === $directory->post_type && 1 === (int) epl_get_option( 'epl_sd_staff_image_type', 0 ) ) {
890: $image_size = apply_filters( 'epl_sd_author_tab_image_size', epl_get_option( 'epl_sd_staff_image_size_box', 'thumbnail' ) );
891: if ( has_post_thumbnail( $directory ) ) {
892: $image_html = wp_get_attachment_image( get_post_thumbnail_id( $directory ), $image_size );
893: } elseif ( function_exists( 'epl_sd_has_fallback_featured_image' ) && epl_sd_has_fallback_featured_image() && function_exists( 'epl_sd_get_the_fallback_featured_image' ) ) {
894: $image_html = epl_sd_get_the_fallback_featured_image( $image_size, array( 'class' => 'epl-sd-fallback-image' ) );
895: }
896: }
897:
898: $image_html = apply_filters( 'epl_author_tab_image', $image_html, $agent );
899: return apply_filters( 'epl_elementor_agent_image', $image_html, $agent, $size );
900: }
901:
902: /**
903: * Apply EPL defaults to an Elementor Pro Loop Grid query whose Query ID is
904: * "epl_listings".
905: *
906: * @param WP_Query $query Elementor query.
907: * @param \Elementor\Widget_Base $widget Elementor widget.
908: * @since 3.6.0 New: Elementor widgets (beta).
909: */
910: public function elementor_listings_query( $query, $widget ) {
911: $post_type = $query->get( 'post_type' );
912: if ( empty( $post_type ) || 'post' === $post_type ) {
913: $query->set( 'post_type', epl_get_core_post_types() );
914: }
915: $query->set( 'epl_elementor_loop', true );
916: do_action( 'epl_elementor_loop_query', $query, $widget );
917: }
918:
919: /**
920: * Configure an Elementor Pro Loop Grid whose Query ID is "epl_staff".
921: *
922: * @param WP_Query $query Elementor query.
923: * @param \Elementor\Widget_Base $widget Elementor widget.
924: * @since 3.6.0 New: Elementor widgets (beta).
925: */
926: public function elementor_staff_query( $query, $widget ) {
927: $query->set( 'post_type', 'directory' );
928: $query->set( 'epl_elementor_context', 'staff' );
929: do_action( 'epl_elementor_staff_query', $query, $widget );
930: }
931:
932: /**
933: * Ensure Theme Builder owns Staff Directory single/archive requests.
934: *
935: * Staff Directory renders through EPL's PHP templates, which do not call
936: * Elementor's single/archive theme locations. Force Elementor's header/footer
937: * page template only for Directory requests where a matching Theme Builder
938: * document has already been selected.
939: *
940: * @param bool $override Whether Elementor should override.
941: * @param string $location Theme location.
942: * @param object $locations_manager Elementor locations manager.
943: * @return bool
944: * @since 3.6.0 New: Elementor widgets (beta).
945: */
946: public function override_staff_directory_location( $override, $location, $locations_manager ) {
947: if ( 'single' === $location && is_singular( 'directory' ) ) {
948: return true;
949: }
950: if ( 'archive' === $location && ( is_post_type_archive( 'directory' ) || is_tax( 'department' ) ) ) {
951: return true;
952: }
953: return $override;
954: }
955:
956: /**
957: * Make Loop Grids that use an EPL Loop Item query EPL listings in both the
958: * Theme Builder editor and on the archive front end. Elementor otherwise
959: * defaults an unset Loop Grid source to regular posts.
960: *
961: * @param array $query_args Elementor query arguments.
962: * @param \Elementor\Widget_Base $widget Elementor widget.
963: * @return array
964: * @since 3.6.0 New: Elementor widgets (beta).
965: */
966: public function elementor_loop_query_args( $query_args, $widget ) {
967: if ( ! $widget || 'loop-grid' !== $widget->get_name() ) {
968: return $query_args;
969: }
970:
971: $template_id = absint( $widget->get_settings( 'template_id' ) );
972: $context = $template_id ? self::get_epl_template_context( $template_id ) : '';
973: if ( ! $context ) {
974: return $query_args;
975: }
976:
977: $source = (string) $widget->get_settings( 'post_query_post_type' );
978: $is_editor = self::is_editor();
979:
980: // Elementor resolves "Current Query" from the global query. Inside the
981: // Theme Builder editor that global points at the elementor_library archive
982: // document, producing an empty Loop Grid. Use representative EPL posts for
983: // the editor only; the live archive must continue inheriting its real query.
984: if ( $is_editor && 'current_query' === $source ) {
985: $query_args = array(
986: 'post_type' => 'staff' === $context ? array( 'directory' ) : self::get_archive_preview_post_types(),
987: 'post_status' => 'publish',
988: 'posts_per_page' => max( 1, absint( $widget->get_settings( 'posts_per_page' ) ) ),
989: 'paged' => 1,
990: 'orderby' => 'date',
991: 'order' => 'DESC',
992: 'ignore_sticky_posts' => true,
993: );
994: }
995:
996: $post_type = isset( $query_args['post_type'] ) ? $query_args['post_type'] : '';
997: if ( empty( $post_type ) || 'post' === $post_type ) {
998: $query_args['post_type'] = 'staff' === $context ? array( 'directory' ) : self::get_archive_preview_post_types();
999: }
1000:
1001: $query_args['epl_elementor_loop'] = true;
1002: $query_args['epl_elementor_context'] = $context;
1003: return apply_filters( 'epl_elementor_loop_query_args', $query_args, $widget, $template_id );
1004: }
1005:
1006: /**
1007: * Supply representative listings for a Current Query Loop Grid in the editor.
1008: *
1009: * Elementor returns early for Current Query and therefore does not run its
1010: * general elementor/query/query_args filter. Its Theme Builder preview query
1011: * normally contains one post, irrespective of the Loop Grid item count.
1012: *
1013: * @param array $query_args Current global query variables.
1014: * @return array
1015: * @since 3.6.0 New: Elementor widgets (beta).
1016: */
1017: public function elementor_current_query_args( $query_args ) {
1018: // Prefer the live widget model. This includes unsaved editor changes and
1019: // avoids stale document/element caches. Parsing the saved archive remains
1020: // a fallback for render paths that do not fire Elementor's widget hooks.
1021: $loop_settings = $this->active_loop_grid_settings;
1022: if ( empty( $loop_settings ) ) {
1023: $loop_settings = self::get_current_epl_loop_grid_settings();
1024: }
1025: if ( empty( $loop_settings ) ) {
1026: return $query_args;
1027: }
1028:
1029: $posts_per_page = isset( $loop_settings['posts_per_page'] ) ? absint( $loop_settings['posts_per_page'] ) : 3;
1030: $posts_per_page = max( 1, $posts_per_page );
1031:
1032: // Elementor's Current Query source copies the archive's main query and
1033: // returns before its normal Loop Grid query filters run. That also copies
1034: // the site's Reading setting (commonly 8 or 10 posts), silently ignoring
1035: // the Loop Grid's Items Per Page control. Keep the real archive filters on
1036: // the frontend, but make the saved widget value authoritative.
1037: if ( ! self::is_editor() ) {
1038: $query_args['posts_per_page'] = $posts_per_page;
1039: $query_args['epl_elementor_loop'] = true;
1040: if ( ! empty( $loop_settings['_epl_widget_id'] ) ) {
1041: $query_args['instance_id'] = 'elementor-' . sanitize_key( $loop_settings['_epl_widget_id'] );
1042: $query_args['is_epl_shortcode'] = true;
1043: $query_args['epl_shortcode_name'] = 'listing_advanced';
1044: }
1045: return $query_args;
1046: }
1047:
1048: return array(
1049: 'post_type' => self::get_archive_preview_post_types(),
1050: 'post_status' => 'publish',
1051: 'posts_per_page' => $posts_per_page,
1052: 'paged' => 1,
1053: 'orderby' => 'date',
1054: 'order' => 'DESC',
1055: 'ignore_sticky_posts' => true,
1056: 'epl_elementor_loop' => true,
1057: );
1058: }
1059:
1060: /** Capture live Loop Grid settings immediately before its query is created. */
1061: /**
1062: * Begin loop grid render.
1063: *
1064: * @since 3.6.0 New: Elementor widgets (beta).
1065: */
1066: public function begin_loop_grid_render( $widget ) {
1067: if ( ! $widget || 'loop-grid' !== $widget->get_name() ) {
1068: return;
1069: }
1070: $template_id = absint( $widget->get_settings( 'template_id' ) );
1071: if ( $template_id && 'listing' === self::get_epl_template_context( $template_id ) ) {
1072: $this->active_loop_grid_settings = $widget->get_settings_for_display();
1073: $this->active_loop_grid_settings['_epl_widget_id'] = $widget->get_id();
1074: }
1075: }
1076:
1077: /** Clear live Loop Grid state after rendering to avoid affecting another query. */
1078: /**
1079: * End loop grid render.
1080: *
1081: * @since 3.6.0 New: Elementor widgets (beta).
1082: */
1083: public function end_loop_grid_render( $widget ) {
1084: if ( $widget && 'loop-grid' === $widget->get_name() ) {
1085: $this->active_loop_grid_settings = array();
1086: }
1087: }
1088:
1089: /**
1090: * Add an EPL renderer choice to Elementor Pro's native Loop Grid pagination.
1091: *
1092: * Elementor remains the default and retains all of its AJAX/load-more modes.
1093: * EPL pagination is available for the numbered pagination modes.
1094: *
1095: * @param \Elementor\Element_Base $element Loop Grid element.
1096: * @since 3.6.0 New: Elementor widgets (beta).
1097: */
1098: public function register_loop_grid_pagination_provider( $element ) {
1099: $element->add_control(
1100: 'epl_pagination_provider',
1101: array(
1102: 'label' => esc_html__( 'Pagination Renderer', 'easy-property-listings' ),
1103: 'type' => \Elementor\Controls_Manager::SELECT,
1104: 'default' => 'elementor',
1105: 'options' => array(
1106: 'elementor' => esc_html__( 'Elementor', 'easy-property-listings' ),
1107: 'epl' => esc_html__( 'EPL', 'easy-property-listings' ),
1108: ),
1109: 'condition' => array(
1110: 'pagination_type!' => array( '', 'load_more_on_click', 'load_more_infinite_scroll' ),
1111: ),
1112: )
1113: );
1114: $element->add_control(
1115: 'epl_pagination_style',
1116: array(
1117: 'label' => esc_html__( 'EPL Pagination Type', 'easy-property-listings' ),
1118: 'type' => \Elementor\Controls_Manager::SELECT,
1119: 'default' => 'default',
1120: 'options' => array(
1121: 'fancy' => esc_html__( 'Fancy', 'easy-property-listings' ),
1122: 'default' => esc_html__( 'WordPress Default', 'easy-property-listings' ),
1123: ),
1124: 'condition' => array(
1125: 'epl_pagination_provider' => 'epl',
1126: ),
1127: )
1128: );
1129: }
1130:
1131: /**
1132: * Retain the exact query Elementor used so EPL pagination cannot drift from it.
1133: *
1134: * @param WP_Query $query Elementor query.
1135: * @param \Elementor\Widget_Base $widget Elementor widget.
1136: * @since 3.6.0 New: Elementor widgets (beta).
1137: */
1138: public function capture_loop_grid_query( $query, $widget ) {
1139: if ( $query instanceof WP_Query && $widget && 'loop-grid' === $widget->get_name() ) {
1140: $this->loop_grid_queries[ $widget->get_id() ] = $query;
1141: }
1142: }
1143:
1144: /**
1145: * Replace Loop Grid's native numbered navigation with EPL navigation on demand.
1146: *
1147: * @param string $content Rendered widget HTML.
1148: * @param \Elementor\Widget_Base $widget Elementor widget.
1149: * @return string
1150: * @since 3.6.0 New: Elementor widgets (beta).
1151: */
1152: public function render_loop_grid_epl_pagination( $content, $widget ) {
1153: if ( ! $widget || 'loop-grid' !== $widget->get_name() ) {
1154: return $content;
1155: }
1156: $integrated_epl = 'epl' === $widget->get_settings( 'epl_pagination_provider' );
1157: $standalone_epl = self::current_document_has_epl_pagination_widget();
1158: if ( ! $integrated_epl && ! $standalone_epl ) {
1159: return $content;
1160: }
1161:
1162: $query = $this->loop_grid_queries[ $widget->get_id() ] ?? null;
1163: if ( ! $query instanceof WP_Query ) {
1164: return $content;
1165: }
1166:
1167: // Native numbered pagination is removed only after Elementor has used its
1168: // settings to calculate the correct current page and query offset.
1169: $content = preg_replace( '#\s*<div class="e-load-more-anchor"[^>]*></div>\s*<nav class="elementor-pagination".*?</nav>#s', '', $content );
1170: if ( $standalone_epl ) {
1171: return $content;
1172: }
1173: ob_start();
1174: $pagination_style = $widget->get_settings( 'epl_pagination_style' );
1175: self::render_loop_grid_epl_navigation( $query, $widget->get_id(), $pagination_style ? $pagination_style : 'default' );
1176: return $content . ob_get_clean();
1177: }
1178:
1179: /** Whether the active Elementor archive already contains EPL Pagination. */
1180: /**
1181: * Current document has epl pagination widget.
1182: *
1183: * @since 3.6.0 New: Elementor widgets (beta).
1184: */
1185: private static function current_document_has_epl_pagination_widget() {
1186: $document_id = get_the_ID();
1187: if ( isset( \Elementor\Plugin::$instance->documents ) ) {
1188: $document = \Elementor\Plugin::$instance->documents->get_current();
1189: if ( $document ) {
1190: $document_id = $document->get_main_id();
1191: }
1192: }
1193: $data = $document_id ? (string) get_post_meta( $document_id, '_elementor_data', true ) : '';
1194: return false !== strpos( $data, '"widgetType":"epl-pagination"' );
1195: }
1196:
1197: /**
1198: * Render canonical EPL navigation for an Elementor Loop Grid.
1199: *
1200: * @param WP_Query $query Loop Grid query.
1201: * @param string $widget_id Elementor widget ID.
1202: * @since 3.6.0 New: Elementor widgets (beta).
1203: */
1204: public static function render_loop_grid_epl_navigation( $query, $widget_id, $pagination_style = '' ) {
1205: if ( ! $query instanceof WP_Query || $query->max_num_pages < 2 ) {
1206: return;
1207: }
1208: $original = array(
1209: 'instance_id' => $query->get( 'instance_id' ),
1210: 'is_epl_shortcode' => $query->get( 'is_epl_shortcode' ),
1211: 'epl_shortcode_name' => $query->get( 'epl_shortcode_name' ),
1212: );
1213: $query->set( 'instance_id', 'elementor-' . sanitize_key( $widget_id ) );
1214: $query->set( 'is_epl_shortcode', true );
1215: $query->set( 'epl_shortcode_name', 'listing_advanced' );
1216: self::render_epl_pagination( $query, $pagination_style );
1217: foreach ( $original as $key => $value ) {
1218: $query->set( $key, $value );
1219: }
1220: }
1221:
1222: /**
1223: * Render EPL pagination while optionally overriding its global style setting.
1224: *
1225: * @param WP_Query $query Query being paginated.
1226: * @param string $pagination_style Either "fancy", "default", or empty to
1227: * follow the global EPL setting.
1228: * @since 3.6.0 New: Elementor widgets (beta).
1229: */
1230: public static function render_epl_pagination( $query, $pagination_style = '' ) {
1231: $style_filter = null;
1232: if ( in_array( $pagination_style, array( 'fancy', 'default' ), true ) ) {
1233: $style_filter = static function () use ( $pagination_style ) {
1234: return 'fancy' === $pagination_style ? 1 : 0;
1235: };
1236: add_filter( 'epl_get_option_use_fancy_navigation', $style_filter, PHP_INT_MAX );
1237: }
1238:
1239: do_action( 'epl_pagination', array( 'query' => $query ) );
1240:
1241: if ( $style_filter ) {
1242: remove_filter( 'epl_get_option_use_fancy_navigation', $style_filter, PHP_INT_MAX );
1243: }
1244: }
1245:
1246: /** Return the most recently rendered Loop Grid query for a following widget. */
1247: /**
1248: * Get last loop grid query.
1249: *
1250: * @since 3.6.0 New: Elementor widgets (beta).
1251: */
1252: public function get_last_loop_grid_query() {
1253: if ( empty( $this->loop_grid_queries ) ) {
1254: return array();
1255: }
1256: $widget_id = array_key_last( $this->loop_grid_queries );
1257: return array( 'widget_id' => $widget_id, 'query' => $this->loop_grid_queries[ $widget_id ] );
1258: }
1259:
1260: /** Prevent the archive main-query page size from 404ing an EPL Loop Grid page. */
1261: /**
1262: * Allow epl loop grid page.
1263: *
1264: * @since 3.6.0 New: Elementor widgets (beta).
1265: */
1266: public function allow_epl_loop_grid_page( $preempt, $query ) {
1267: $instance_id = isset( $_GET['pagination_id'] ) ? sanitize_text_field( wp_unslash( $_GET['pagination_id'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Public pagination state.
1268: if ( 0 !== strpos( $instance_id, 'elementor-' ) || ! is_post_type_archive( epl_get_core_post_types() ) ) {
1269: return $preempt;
1270: }
1271: return true;
1272: }
1273:
1274: /**
1275: * Find the EPL Loop Grid settings in the active Elementor document.
1276: *
1277: * @return array
1278: * @since 3.6.0 New: Elementor widgets (beta).
1279: */
1280: private static function get_current_epl_loop_grid_settings() {
1281: $document_ids = array();
1282: $current_post = get_post();
1283:
1284: if ( $current_post && 'elementor_library' === $current_post->post_type ) {
1285: $document_ids[] = (int) $current_post->ID;
1286: }
1287:
1288: if ( isset( \Elementor\Plugin::$instance->documents ) ) {
1289: $current_document = \Elementor\Plugin::$instance->documents->get_current();
1290: if ( $current_document ) {
1291: $document_ids[] = (int) $current_document->get_main_id();
1292: }
1293: }
1294:
1295: foreach ( array_unique( array_filter( $document_ids ) ) as $document_id ) {
1296: $elements = json_decode( (string) get_post_meta( $document_id, '_elementor_data', true ), true );
1297: $stack = is_array( $elements ) ? $elements : array();
1298:
1299: while ( $stack ) {
1300: $element = array_pop( $stack );
1301: if ( ! is_array( $element ) ) {
1302: continue;
1303: }
1304:
1305: if ( 'loop-grid' === ( $element['widgetType'] ?? '' ) ) {
1306: $settings = isset( $element['settings'] ) && is_array( $element['settings'] ) ? $element['settings'] : array();
1307: $template_id = isset( $settings['template_id'] ) ? absint( $settings['template_id'] ) : 0;
1308: if ( $template_id && 'listing' === self::get_epl_template_context( $template_id ) ) {
1309: $settings['_epl_widget_id'] = isset( $element['id'] ) ? sanitize_key( $element['id'] ) : '';
1310: return $settings;
1311: }
1312: }
1313:
1314: if ( ! empty( $element['elements'] ) && is_array( $element['elements'] ) ) {
1315: $stack = array_merge( $stack, $element['elements'] );
1316: }
1317: }
1318: }
1319:
1320: return array();
1321: }
1322:
1323: /**
1324: * Determine whether a Loop Item is for listings or Staff Directory posts.
1325: *
1326: * @param int $template_id Elementor template ID.
1327: * @return string "listing", "staff", or an empty string.
1328: * @since 3.6.0 New: Elementor widgets (beta).
1329: */
1330: private static function get_epl_template_context( $template_id ) {
1331: $stored_context = (string) get_post_meta( $template_id, '_epl_elementor_template_context', true );
1332: if ( in_array( $stored_context, array( 'listing', 'staff' ), true ) ) {
1333: return $stored_context;
1334: }
1335:
1336: // A brand-new EPL Single Listing / Listing Card document has no
1337: // content yet to sniff below, but its type alone already implies
1338: // listing context (its own save() sets the meta above afterwards).
1339: $template_type = get_post_meta( $template_id, '_elementor_template_type', true );
1340: if ( in_array( $template_type, array( EPL_Elementor_Single_Document::TYPE, EPL_Elementor_Loop_Card_Document::TYPE ), true ) ) {
1341: return 'listing';
1342: }
1343:
1344: $data = (string) get_post_meta( $template_id, '_elementor_data', true );
1345: if ( false !== strpos( $data, '"widgetType":"epl-property-' ) || false !== strpos( $data, '"widgetType":"epl-listing-agents"' ) ) {
1346: return 'listing';
1347: }
1348: if ( false !== strpos( $data, '"widgetType":"epl-agent-' ) ) {
1349: return 'staff';
1350: }
1351: return '';
1352: }
1353:
1354: /**
1355: * Infer the intended EPL archive post type from the current archive
1356: * document's display conditions. Falls back to every core listing type.
1357: *
1358: * @return array
1359: * @since 3.6.0 New: Elementor widgets (beta).
1360: */
1361: private static function get_archive_preview_post_types() {
1362: $post_types = epl_get_core_post_types();
1363: $document_id = get_the_ID();
1364: if ( isset( \Elementor\Plugin::$instance->documents ) ) {
1365: $current_document = \Elementor\Plugin::$instance->documents->get_current();
1366: if ( $current_document ) {
1367: $document_id = $current_document->get_main_id();
1368: }
1369: }
1370: $conditions = $document_id ? get_post_meta( $document_id, '_elementor_conditions', true ) : array();
1371:
1372: if ( is_array( $conditions ) ) {
1373: foreach ( $conditions as $condition ) {
1374: $parts = explode( '/', (string) $condition );
1375: $last = end( $parts );
1376: if ( substr( $last, -8 ) === '_archive' ) {
1377: $candidate = substr( $last, 0, -8 );
1378: if ( in_array( $candidate, $post_types, true ) ) {
1379: return array( $candidate );
1380: }
1381: }
1382: }
1383: }
1384:
1385: return $post_types;
1386: }
1387:
1388: /**
1389: * Track and, when necessary, restore EPL archive display conditions.
1390: *
1391: * @param int $meta_id Metadata ID.
1392: * @param int $post_id Template post ID.
1393: * @param string $meta_key Metadata key.
1394: * @param string|array $meta_value Metadata value.
1395: * @since 3.6.0 New: Elementor widgets (beta).
1396: */
1397: public function track_archive_conditions( $meta_id, $post_id, $meta_key, $meta_value ) {
1398: if ( '_elementor_conditions' !== $meta_key || 'archive' !== get_post_meta( $post_id, '_elementor_template_type', true ) ) {
1399: return;
1400: }
1401:
1402: if ( ! empty( $meta_value ) && is_array( $meta_value ) ) {
1403: update_post_meta( $post_id, '_epl_elementor_conditions_backup', $meta_value );
1404: }
1405:
1406: self::$archive_condition_checks[ $post_id ] = true;
1407: if ( ! self::$archive_condition_shutdown_registered ) {
1408: self::$archive_condition_shutdown_registered = true;
1409: add_action( 'shutdown', array( $this, 'restore_archive_conditions' ), 1 );
1410: }
1411: }
1412:
1413: /**
1414: * Archive template IDs whose conditions changed during this request.
1415: *
1416: * @var array
1417: */
1418: private static $archive_condition_checks = array();
1419:
1420: /**
1421: * Whether the shutdown restoration callback is registered.
1422: *
1423: * @var bool
1424: */
1425: private static $archive_condition_shutdown_registered = false;
1426:
1427: /**
1428: * Restore accidentally emptied conditions after Elementor finishes saving.
1429: * @since 3.6.0 New: Elementor widgets (beta).
1430: */
1431: public function restore_archive_conditions() {
1432: foreach ( array_keys( self::$archive_condition_checks ) as $post_id ) {
1433: $current = get_post_meta( $post_id, '_elementor_conditions', true );
1434: $backup = get_post_meta( $post_id, '_epl_elementor_conditions_backup', true );
1435:
1436: if ( empty( $current ) && ! empty( $backup ) && apply_filters( 'epl_elementor_restore_archive_conditions', true, $post_id, $backup ) ) {
1437: update_post_meta( $post_id, '_elementor_conditions', $backup );
1438: }
1439: }
1440: }
1441:
1442: /**
1443: * Get a sample property for editor preview.
1444: *
1445: * Fetches the newest published EPL listing to use as preview data in the Elementor editor.
1446: *
1447: * @return EPL_Property_Meta|false The property object or false if none found.
1448: * @since 3.6.0
1449: * @since 3.6.0 Security: ?epl_elementor_preview_id only shows published listings, or listings the user may edit.
1450: */
1451: public static function get_preview_property() {
1452: static $preview_property = null;
1453:
1454: // Return cached if already fetched.
1455: if ( null !== $preview_property ) {
1456: return $preview_property;
1457: }
1458:
1459: $requested_id = isset( $_GET['epl_elementor_preview_id'] ) ? absint( wp_unslash( $_GET['epl_elementor_preview_id'] ) ) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only editor preview.
1460: $requested_id = absint( apply_filters( 'epl_elementor_preview_listing_id', $requested_id ) );
1461: if ( $requested_id ) {
1462: $requested_post = get_post( $requested_id );
1463: // Only a published listing, or one the visitor may edit, so drafts and private listings can't be read from the URL.
1464: if ( $requested_post && is_epl_post( $requested_post->post_type ) && ( 'publish' === $requested_post->post_status || current_user_can( 'edit_post', $requested_id ) ) ) {
1465: $preview_property = new EPL_Property_Meta( $requested_post );
1466: return $preview_property;
1467: }
1468: }
1469:
1470: // Prefer the listing type selected by the active Archive document. Editing
1471: // a Loop Item directly has no archive condition, so this naturally falls
1472: // back to every core EPL listing type.
1473: $post_types = self::get_archive_preview_post_types();
1474:
1475: if ( empty( $post_types ) ) {
1476: $preview_property = false;
1477: return false;
1478: }
1479:
1480: // Prefer a listing with a featured image so image-led Loop Items produce a
1481: // useful preview while Elementor switches templates. Fall back to any EPL
1482: // listing below for sites whose listings do not use featured images.
1483: $args = array(
1484: 'post_type' => $post_types,
1485: 'post_status' => 'publish',
1486: 'posts_per_page' => 1,
1487: 'orderby' => 'date',
1488: 'order' => 'DESC',
1489: 'no_found_rows' => true,
1490: 'meta_query' => array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query -- Single editor-preview record only.
1491: array(
1492: 'key' => '_thumbnail_id',
1493: 'compare' => 'EXISTS',
1494: ),
1495: ),
1496: );
1497:
1498: $query = new WP_Query( $args );
1499: if ( ! $query->have_posts() ) {
1500: unset( $args['meta_query'] );
1501: $query = new WP_Query( $args );
1502: }
1503:
1504: if ( $query->have_posts() ) {
1505: $preview_property = new EPL_Property_Meta( $query->posts[0] );
1506: } else {
1507: $preview_property = false;
1508: }
1509: return $preview_property;
1510: }
1511: }
1512:
1513: // Initialize.
1514: add_action( 'elementor/loaded', array( 'EPL_Elementor', 'instance' ) );
1515: