1: <?php
2: /**
3: * Listing Elements Shortcode class
4: *
5: * @package EPL
6: * @subpackage Classes/ListingElements
7: * @copyright Copyright (c) 2020, Merv Barrett
8: * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
9: * @since 3.3
10: */
11:
12: // Exit if accessed directly.
13: if ( ! defined( 'ABSPATH' ) ) {
14: exit;
15: }
16:
17: /**
18: * EPL_Listing_Elements Class
19: *
20: * @since 3.3.0
21: */
22: class EPL_Listing_Elements {
23:
24: /**
25: * Construct the shortcode.
26: *
27: * @since 3.3.0
28: */
29: public function __construct() {
30: add_shortcode( 'listing_element', array( $this, 'listing_element' ) );
31: add_shortcode( 'epl_listing_action', array( $this, 'epl_action' ) );
32: add_shortcode( 'epl_listing_meta', array( $this, 'epl_meta' ) );
33: add_shortcode( 'epl_listing_post', array( $this, 'post' ) );
34: add_action( 'epl_suburb_profile', array( $this, 'epl_suburb_profile' ) );
35: add_action( 'epl_listing_address', array( $this, 'epl_formatted_address' ) );
36: add_shortcode( 'epl_listing_excerpt', array( $this, 'epl_the_excerpt' ) );
37: }
38:
39: /**
40: * Output example code or value if in th loop
41: *
42: * @param array $atts Array of attributes.
43: *
44: * @return false|string|void
45: * @since 3.3
46: */
47: public function listing_element( $atts ) {
48:
49: global $property;
50:
51: if ( ! isset( $atts['type'] ) ) {
52: return;
53: }
54:
55: if ( is_null( $property ) ) {
56:
57: $key_name = '';
58:
59: switch ( $atts['type'] ) {
60:
61: case 'action':
62: $key_name = 'action_key';
63: break;
64:
65: case 'meta':
66: $key_name = 'meta_key';
67: break;
68:
69: case 'post':
70: $key_name = 'post_key';
71: break;
72:
73: }
74:
75: ob_start();
76: echo '[ ' . esc_html__( 'Listing', 'easy-property-listings' ) . ' ' . esc_attr( ucwords( $atts['type'] ) );
77:
78: if ( ! empty( $key_name ) ) {
79: echo ': ' . esc_attr( ucwords( str_replace( '_', ' ', $atts[ $key_name ] ) ) );
80: }
81: echo ' ]';
82:
83: return ob_get_clean();
84: }
85:
86: $return = '';
87:
88: switch ( $atts['type'] ) {
89:
90: case 'action':
91: $return = $this->epl_action( $atts );
92: break;
93:
94: case 'meta':
95: $return = $this->epl_meta( $atts );
96: break;
97:
98: case 'post':
99: $return = $this->post( $atts );
100: break;
101:
102: case 'suburb_profile':
103: $return = $this->epl_suburb_profile();
104: break;
105:
106: case 'formatted_address':
107: $return = $this->epl_formatted_address();
108: break;
109:
110: case 'excerpt':
111: $return = $this->epl_the_excerpt();
112: break;
113:
114: }
115:
116: return $return;
117: }
118:
119: /**
120: * Output action
121: *
122: * @param array $atts Array of attributes for the action.
123: *
124: * @return false|string|void
125: * @since 3.3
126: */
127: public function epl_action( $atts ) {
128: if ( ! isset( $atts['action_key'] ) ) {
129: return;
130: }
131: ob_start();
132: do_action( $atts['action_key'] );
133: return ob_get_clean();
134: }
135:
136: /**
137: * Get meta
138: *
139: * @param array $atts Array of attributes for the meta field.
140: *
141: * @return string|void
142: * @since 3.3
143: */
144: public function epl_meta( $atts ) {
145:
146: if ( ! isset( $atts['meta_key'] ) ) {
147: return;
148: }
149:
150: return get_property_meta( $atts['meta_key'] );
151: }
152:
153: /**
154: * Get post
155: *
156: * @param array $atts Array of attributes for the post object.
157: *
158: * @return false|string
159: * @since 3.3
160: */
161: public function post( $atts ) {
162:
163: global $property;
164:
165: $return = '';
166: switch ( $atts['post_key'] ) {
167:
168: case 'permalink':
169: $return = get_permalink( $property->post->ID );
170: break;
171:
172: default:
173: $return = isset( $property->post->{$atts['post_key']} ) ? $property->post->{$atts['post_key']} : '';
174: break;
175:
176: }
177:
178: return $return;
179: }
180:
181: /**
182: * Get suburb profile
183: *
184: * @since 3.3
185: */
186: public function epl_suburb_profile() {
187:
188: global $property;
189: echo esc_html( $property->get_suburb_profile() );
190: }
191:
192: /**
193: * Get formatted listing address
194: *
195: * @since 3.3
196: */
197: public function epl_formatted_address() {
198:
199: global $property;
200: echo esc_html( $property->get_formatted_property_address() );
201: }
202:
203: /**
204: * Get the excerpt
205: *
206: * @since 3.3
207: */
208: public function epl_the_excerpt() {
209:
210: global $property;
211: return esc_html( epl_get_the_excerpt() );
212: }
213: }
214:
215: new EPL_Listing_Elements();
216: