1: <?php
2: /**
3: * Elementor Listing Agents Widget
4: *
5: * @package EPL
6: * @subpackage PageBuilders/Elementor/Widgets/Elements
7: * @since 3.6.0
8: */
9:
10: // Exit if accessed directly.
11: if ( ! defined( 'ABSPATH' ) ) {
12: exit;
13: }
14:
15: /**
16: * EPL_Elementor_Listing_Agents Class
17: *
18: * Renders agents assigned to a listing with a dedicated EPL Agent Template.
19: *
20: * @since 3.6.0
21: */
22: class EPL_Elementor_Listing_Agents extends \Elementor\Widget_Base {
23: use EPL_Elementor_Dynamic_Widget;
24:
25: /**
26: * Get widget name.
27: *
28: * @return string
29: * @since 3.6.0 New: Elementor widgets (beta).
30: */
31: public function get_name() {
32: return 'epl-listing-agents';
33: }
34:
35: /**
36: * Get widget title.
37: *
38: * @return string
39: * @since 3.6.0 New: Elementor widgets (beta).
40: */
41: public function get_title() {
42: return esc_html__( 'Agents', 'easy-property-listings' );
43: }
44:
45: /**
46: * Get widget icon.
47: *
48: * @return string
49: * @since 3.6.0 New: Elementor widgets (beta).
50: */
51: public function get_icon() {
52: return 'eicon-person';
53: }
54:
55: /**
56: * Get widget categories.
57: *
58: * @return array
59: * @since 3.6.0 New: Elementor widgets (beta).
60: */
61: public function get_categories() {
62: return array( 'epl-author' );
63: }
64:
65: /**
66: * Get widget keywords.
67: *
68: * @return array
69: * @since 3.6.0 New: Elementor widgets (beta).
70: */
71: public function get_keywords() {
72: return array( 'agent', 'agents', 'author', 'listing', 'template', 'epl' );
73: }
74:
75: /**
76: * Template IDs currently being rendered, used as a recursion guard.
77: *
78: * @var array
79: */
80: private static $rendering_templates = array();
81:
82: /**
83: * Get available Elementor templates.
84: *
85: * @return array
86: * @since 3.6.0 New: Elementor widgets (beta).
87: */
88: private function get_elementor_templates() {
89: $templates = array(
90: '' => esc_html__( '— Select Template —', 'easy-property-listings' ),
91: );
92:
93: $args = array(
94: 'post_type' => 'elementor_library',
95: 'posts_per_page' => -1,
96: 'post_status' => 'publish',
97: 'orderby' => 'title',
98: 'order' => 'ASC',
99: 'meta_query' => array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
100: array(
101: 'key' => '_elementor_template_type',
102: 'value' => EPL_Elementor_Agent_Document::TYPE,
103: 'compare' => '=',
104: ),
105: ),
106: );
107:
108: $template_query = new \WP_Query( $args );
109:
110: if ( $template_query->have_posts() ) {
111: foreach ( $template_query->posts as $post ) {
112: $templates[ $post->ID ] = $post->post_title;
113: }
114: }
115:
116: return $templates;
117: }
118:
119: /**
120: * Register widget controls.
121: * @since 3.6.0 New: Elementor widgets (beta).
122: */
123: protected function register_controls() {
124:
125: // Template Section.
126: $this->start_controls_section(
127: 'section_template',
128: array(
129: 'label' => esc_html__( 'Template', 'easy-property-listings' ),
130: 'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
131: )
132: );
133:
134: $this->add_control(
135: 'render_type',
136: array(
137: 'label' => esc_html__( 'Render Type', 'easy-property-listings' ),
138: 'type' => \Elementor\Controls_Manager::SELECT,
139: 'options' => array(
140: 'default' => esc_html__( 'Default Agent Box', 'easy-property-listings' ),
141: 'template' => esc_html__( 'Elementor Template', 'easy-property-listings' ),
142: ),
143: 'default' => 'default',
144: )
145: );
146:
147: $this->add_control(
148: 'template_id',
149: array(
150: 'label' => esc_html__( 'Agent Template', 'easy-property-listings' ),
151: 'type' => \Elementor\Controls_Manager::SELECT,
152: 'options' => $this->get_elementor_templates(),
153: 'default' => '',
154: 'description' => esc_html__( 'Only dedicated EPL Agent Templates can be used here.', 'easy-property-listings' ),
155: 'condition' => array(
156: 'render_type' => 'template',
157: ),
158: )
159: );
160:
161: $this->add_control(
162: 'template_help',
163: array(
164: 'type' => \Elementor\Controls_Manager::RAW_HTML,
165: 'raw' => sprintf(
166: '<p style="color:#666;font-size:12px;">%s <a href="%s" target="_blank">%s</a></p>',
167: esc_html__( 'No template?', 'easy-property-listings' ),
168: esc_url( \Elementor\Plugin::$instance->documents->get_create_new_post_url( 'elementor_library', EPL_Elementor_Agent_Document::TYPE ) ),
169: esc_html__( 'Create an EPL Agent Template', 'easy-property-listings' )
170: ),
171: 'content_classes' => 'elementor-panel-alert',
172: 'condition' => array(
173: 'render_type' => 'template',
174: ),
175: )
176: );
177:
178: $this->end_controls_section();
179:
180: // Layout Section.
181: $this->start_controls_section(
182: 'section_layout',
183: array(
184: 'label' => esc_html__( 'Layout', 'easy-property-listings' ),
185: 'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
186: )
187: );
188:
189: $this->add_control(
190: 'agents_count',
191: array(
192: 'label' => esc_html__( 'Max Agents', 'easy-property-listings' ),
193: 'type' => \Elementor\Controls_Manager::NUMBER,
194: 'min' => 1,
195: 'max' => 10,
196: 'step' => 1,
197: 'default' => 4,
198: )
199: );
200:
201: $this->add_control(
202: 'display_mode',
203: array(
204: 'label' => esc_html__( 'Display Mode', 'easy-property-listings' ),
205: 'type' => \Elementor\Controls_Manager::SELECT,
206: 'options' => array(
207: 'normal' => esc_html__( 'Normal (Grid/List)', 'easy-property-listings' ),
208: 'tabbed' => esc_html__( 'Tabbed', 'easy-property-listings' ),
209: ),
210: 'default' => 'normal',
211: )
212: );
213:
214: // Normal mode controls.
215: $this->add_control(
216: 'layout',
217: array(
218: 'label' => esc_html__( 'Layout', 'easy-property-listings' ),
219: 'type' => \Elementor\Controls_Manager::SELECT,
220: 'options' => array(
221: 'grid' => esc_html__( 'Grid', 'easy-property-listings' ),
222: 'list' => esc_html__( 'List', 'easy-property-listings' ),
223: ),
224: 'default' => 'grid',
225: 'condition' => array(
226: 'display_mode' => 'normal',
227: ),
228: )
229: );
230:
231: $this->add_responsive_control(
232: 'columns',
233: array(
234: 'label' => esc_html__( 'Columns', 'easy-property-listings' ),
235: 'type' => \Elementor\Controls_Manager::SELECT,
236: 'options' => array(
237: '1' => '1',
238: '2' => '2',
239: '3' => '3',
240: '4' => '4',
241: ),
242: 'default' => '2',
243: 'tablet_default' => '2',
244: 'mobile_default' => '1',
245: 'selectors' => array(
246: '{{WRAPPER}} .epl-listing-agents-grid' => '--epl-agent-columns: {{VALUE}};',
247: ),
248: 'condition' => array(
249: 'display_mode' => 'normal',
250: 'layout' => 'grid',
251: ),
252: )
253: );
254:
255: $this->add_responsive_control(
256: 'gap',
257: array(
258: 'label' => esc_html__( 'Gap', 'easy-property-listings' ),
259: 'type' => \Elementor\Controls_Manager::SLIDER,
260: 'size_units' => array( 'px', 'em' ),
261: 'range' => array(
262: 'px' => array(
263: 'min' => 0,
264: 'max' => 100,
265: ),
266: ),
267: 'default' => array(
268: 'size' => 20,
269: 'unit' => 'px',
270: ),
271: 'selectors' => array(
272: '{{WRAPPER}} .epl-listing-agents-grid' => 'gap: {{SIZE}}{{UNIT}};',
273: '{{WRAPPER}} .epl-listing-agents-list' => 'gap: {{SIZE}}{{UNIT}};',
274: ),
275: 'condition' => array(
276: 'display_mode' => 'normal',
277: ),
278: )
279: );
280:
281: // Tabbed mode controls.
282: $this->add_control(
283: 'tab_style',
284: array(
285: 'label' => esc_html__( 'Tab Style', 'easy-property-listings' ),
286: 'type' => \Elementor\Controls_Manager::SELECT,
287: 'options' => array(
288: 'horizontal' => esc_html__( 'Horizontal', 'easy-property-listings' ),
289: 'vertical' => esc_html__( 'Vertical', 'easy-property-listings' ),
290: ),
291: 'default' => 'horizontal',
292: 'condition' => array(
293: 'display_mode' => 'tabbed',
294: ),
295: )
296: );
297:
298: $this->add_control(
299: 'tab_label',
300: array(
301: 'label' => esc_html__( 'Tab Label', 'easy-property-listings' ),
302: 'type' => \Elementor\Controls_Manager::SELECT,
303: 'options' => array(
304: 'name' => esc_html__( 'Agent Name', 'easy-property-listings' ),
305: 'position' => esc_html__( 'Position/Title', 'easy-property-listings' ),
306: 'both' => esc_html__( 'Name + Position', 'easy-property-listings' ),
307: ),
308: 'default' => 'name',
309: 'condition' => array(
310: 'display_mode' => 'tabbed',
311: ),
312: )
313: );
314:
315: $this->add_control(
316: 'empty_message',
317: array(
318: 'label' => esc_html__( 'Empty Message', 'easy-property-listings' ),
319: 'type' => \Elementor\Controls_Manager::TEXT,
320: 'default' => esc_html__( 'No agents assigned to this listing.', 'easy-property-listings' ),
321: 'placeholder' => esc_html__( 'Message when no agents', 'easy-property-listings' ),
322: )
323: );
324:
325: $this->end_controls_section();
326:
327: // Style Section - Tabs.
328: $this->start_controls_section(
329: 'section_style_tabs',
330: array(
331: 'label' => esc_html__( 'Tabs', 'easy-property-listings' ),
332: 'tab' => \Elementor\Controls_Manager::TAB_STYLE,
333: 'condition' => array(
334: 'display_mode' => 'tabbed',
335: ),
336: )
337: );
338:
339: $this->add_control(
340: 'tab_bg_color',
341: array(
342: 'label' => esc_html__( 'Tab Background', 'easy-property-listings' ),
343: 'type' => \Elementor\Controls_Manager::COLOR,
344: 'selectors' => array(
345: '{{WRAPPER}} .epl-agents-tabs .epl-tab-button' => 'background-color: {{VALUE}};',
346: ),
347: )
348: );
349:
350: $this->add_control(
351: 'tab_active_bg_color',
352: array(
353: 'label' => esc_html__( 'Active Tab Background', 'easy-property-listings' ),
354: 'type' => \Elementor\Controls_Manager::COLOR,
355: 'selectors' => array(
356: '{{WRAPPER}} .epl-agents-tabs .epl-tab-button.active' => 'background-color: {{VALUE}};',
357: ),
358: )
359: );
360:
361: $this->add_control(
362: 'tab_text_color',
363: array(
364: 'label' => esc_html__( 'Tab Text Color', 'easy-property-listings' ),
365: 'type' => \Elementor\Controls_Manager::COLOR,
366: 'selectors' => array(
367: '{{WRAPPER}} .epl-agents-tabs .epl-tab-button' => 'color: {{VALUE}};',
368: ),
369: )
370: );
371:
372: $this->add_control(
373: 'tab_active_text_color',
374: array(
375: 'label' => esc_html__( 'Active Tab Text Color', 'easy-property-listings' ),
376: 'type' => \Elementor\Controls_Manager::COLOR,
377: 'selectors' => array(
378: '{{WRAPPER}} .epl-agents-tabs .epl-tab-button.active' => 'color: {{VALUE}};',
379: ),
380: )
381: );
382:
383: $this->add_group_control(
384: \Elementor\Group_Control_Typography::get_type(),
385: array(
386: 'name' => 'tab_typography',
387: 'selector' => '{{WRAPPER}} .epl-agents-tabs .epl-tab-button',
388: )
389: );
390:
391: $this->add_responsive_control(
392: 'tab_padding',
393: array(
394: 'label' => esc_html__( 'Tab Padding', 'easy-property-listings' ),
395: 'type' => \Elementor\Controls_Manager::DIMENSIONS,
396: 'size_units' => array( 'px', 'em' ),
397: 'selectors' => array(
398: '{{WRAPPER}} .epl-agents-tabs .epl-tab-button' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
399: ),
400: )
401: );
402:
403: $this->end_controls_section();
404: }
405:
406: /**
407: * Get agents assigned to the current listing.
408: *
409: * @param EPL_Property_Meta $property The property object.
410: * @return array Array of author loader objects.
411: * @since 3.6.0 New: Elementor widgets (beta).
412: */
413: private function get_listing_agents( $property ) {
414: return EPL_Elementor::get_listing_agents( $property );
415: }
416:
417: /**
418: * Get tab label for an agent.
419: *
420: * @param EPL_Author_Loader $author The author object.
421: * @param string $format Label format.
422: * @return string The tab label.
423: * @since 3.6.0 New: Elementor widgets (beta).
424: */
425: private function get_tab_label( $author, $format ) {
426: $name = $author->get_author_name();
427: $position = $author->get_author_position();
428:
429: switch ( $format ) {
430: case 'position':
431: return ! empty( $position ) ? $position : $name;
432: case 'both':
433: return ! empty( $position ) ? $name . ' - ' . $position : $name;
434: default:
435: return $name;
436: }
437: }
438:
439: /**
440: * Render the selected template or default content.
441: *
442: * @param int $template_id The template post ID.
443: * @param string $render_type The render type (default/template).
444: * @since 3.6.0 New: Elementor widgets (beta).
445: * @since 3.6.0 Security: only renders a published Agent Template.
446: */
447: private function render_agent_item( $template_id, $render_type ) {
448: global $epl_current_agent;
449:
450: if ( 'default' === $render_type ) {
451: // Render default EPL agent box for the current agent.
452: // Passing 'epl_author' arg overrides globals in the template.
453: epl_get_template_part( 'content-author-box.php', array( 'epl_author' => $epl_current_agent ) );
454: } else {
455: // Render Elementor template.
456: $template_id = absint( $template_id );
457:
458: if ( empty( $template_id ) ) {
459: return;
460: }
461:
462: if ( 'publish' !== get_post_status( $template_id ) || EPL_Elementor_Agent_Document::TYPE !== get_post_meta( $template_id, '_elementor_template_type', true ) ) {
463: if ( EPL_Elementor::is_editor() ) {
464: echo '<div class="epl-elementor-placeholder">' . esc_html__( 'The selected template is not an EPL Agent Template.', 'easy-property-listings' ) . '</div>';
465: }
466: return;
467: }
468:
469: // Guard against infinite recursion when the selected template
470: // contains an Agents Loop widget pointing back at itself.
471: if ( isset( self::$rendering_templates[ $template_id ] ) ) {
472: return;
473: }
474:
475: self::$rendering_templates[ $template_id ] = true;
476: echo \Elementor\Plugin::instance()->frontend->get_builder_content_for_display( $template_id, true ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Elementor builds and escapes its own template HTML.
477: unset( self::$rendering_templates[ $template_id ] );
478: }
479: }
480:
481: /**
482: * Render widget output.
483: * @since 3.6.0 New: Elementor widgets (beta).
484: */
485: protected function render() {
486: $property = EPL_Elementor::setup_listing_context();
487: $is_editor = \Elementor\Plugin::$instance->editor->is_edit_mode();
488:
489: if ( ! $property ) {
490: if ( $is_editor ) {
491: echo '<div class="epl-elementor-placeholder">' . esc_html__( 'Listing Agents - No listings found.', 'easy-property-listings' ) . '</div>';
492: }
493: EPL_Elementor::restore_listing_context();
494: return;
495: }
496:
497: $settings = $this->get_settings_for_display();
498: $template_id = isset( $settings['template_id'] ) ? $settings['template_id'] : '';
499: $render_type = isset( $settings['render_type'] ) ? $settings['render_type'] : 'default';
500: $agents_count = isset( $settings['agents_count'] ) ? intval( $settings['agents_count'] ) : 4;
501: $agents = $this->get_listing_agents( $property );
502: $display_mode = $settings['display_mode'];
503:
504: // Limit agents based on setting.
505: if ( ! empty( $agents ) && $agents_count > 0 ) {
506: $agents = array_slice( $agents, 0, $agents_count );
507: }
508:
509: // Check if template is selected (only if render_type is template).
510: if ( 'template' === $render_type && empty( $template_id ) ) {
511: if ( $is_editor ) {
512: echo '<div class="epl-elementor-placeholder">';
513: echo esc_html__( 'Please select an Agent Template from the widget settings.', 'easy-property-listings' );
514: echo '<br><br>';
515: echo '<small>' . esc_html__( 'Create a template using EPL Agent widgets (Photo, Name, Contact, Social, Bio) and select it above.', 'easy-property-listings' ) . '</small>';
516: echo '</div>';
517: }
518: EPL_Elementor::restore_listing_context();
519: return;
520: }
521:
522: if ( empty( $agents ) ) {
523: if ( ! empty( $settings['empty_message'] ) ) {
524: echo '<div class="epl-agents-empty">' . esc_html( $settings['empty_message'] ) . '</div>';
525: }
526: EPL_Elementor::restore_listing_context();
527: return;
528: }
529:
530: if ( 'tabbed' === $display_mode ) {
531: $this->render_tabbed( $agents, $settings, $template_id, $render_type );
532: } else {
533: $this->render_normal( $agents, $settings, $template_id, $render_type );
534: }
535:
536: EPL_Elementor::restore_listing_context();
537: }
538:
539: /**
540: * Render normal (grid/list) layout.
541: *
542: * @param array $agents Array of agent IDs.
543: * @param array $settings Widget settings.
544: * @param int $template_id Template ID.
545: * @param string $render_type Render type.
546: * @since 3.6.0 New: Elementor widgets (beta).
547: */
548: private function render_normal( $agents, $settings, $template_id, $render_type ) {
549: global $epl_current_agent;
550:
551: $layout = $settings['layout'];
552: $columns = isset( $settings['columns'] ) ? $settings['columns'] : '2';
553:
554: $wrapper_class = 'epl-listing-agents epl-listing-agents-' . $layout;
555: if ( 'grid' === $layout ) {
556: $wrapper_class .= ' epl-agents-cols-' . $columns;
557: }
558:
559: echo '<div class="' . esc_attr( $wrapper_class ) . '">';
560:
561: $previous_agent = $epl_current_agent;
562: foreach ( $agents as $agent ) {
563: $epl_current_agent = $agent;
564:
565: echo '<div class="epl-agent-item">';
566: $this->render_agent_item( $template_id, $render_type );
567: echo '</div>';
568: }
569:
570: echo '</div>';
571:
572: // Reset global.
573: $epl_current_agent = $previous_agent;
574: }
575:
576: /**
577: * Render tabbed layout.
578: *
579: * @param array $agents Array of agent IDs.
580: * @param array $settings Widget settings.
581: * @param int $template_id Template ID.
582: * @param string $render_type Render type.
583: * @since 3.6.0 New: Elementor widgets (beta).
584: */
585: private function render_tabbed( $agents, $settings, $template_id, $render_type ) {
586: global $epl_current_agent;
587:
588: $tab_style = $settings['tab_style'];
589: $tab_label = $settings['tab_label'];
590:
591: $wrapper_class = 'epl-agents-tabs epl-agents-tabs-' . $tab_style;
592:
593: echo '<div class="' . esc_attr( $wrapper_class ) . '">';
594:
595: // Prefix tab IDs with the widget ID so multiple widgets on the same
596: // page (or the same agent in two widgets) never share DOM IDs.
597: $tab_id_prefix = 'epl-agent-tab-' . $this->get_id() . '-';
598:
599: // Tab buttons.
600: echo '<div class="epl-tab-buttons">';
601: $index = 0;
602: foreach ( $agents as $author ) {
603: $label = $this->get_tab_label( $author, $tab_label );
604: $active_class = ( 0 === $index ) ? ' active' : '';
605:
606: echo '<button class="epl-tab-button' . esc_attr( $active_class ) . '" data-tab="' . esc_attr( $tab_id_prefix . $author->author_id ) . '">';
607: echo esc_html( $label );
608: echo '</button>';
609:
610: ++$index;
611: }
612: echo '</div>';
613:
614: // Tab content.
615: echo '<div class="epl-tab-contents">';
616: $index = 0;
617: $previous_agent = $epl_current_agent;
618: foreach ( $agents as $agent ) {
619: $epl_current_agent = $agent;
620: $active_class = ( 0 === $index ) ? ' active' : '';
621:
622: echo '<div id="' . esc_attr( $tab_id_prefix . $agent->author_id ) . '" class="epl-tab-content' . esc_attr( $active_class ) . '">';
623: $this->render_agent_item( $template_id, $render_type );
624: echo '</div>';
625:
626: ++$index;
627: }
628: echo '</div>';
629:
630: echo '</div>';
631:
632: // Reset global.
633: $epl_current_agent = $previous_agent;
634: }
635: }
636: