1: <?php
2: /**
3: * Global Functions
4: *
5: * @package EPL
6: * @subpackage Functions/Global
7: * @copyright Copyright (c) 2020, Merv Barrett
8: * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
9: * @since 1.0
10: */
11:
12: // Exit if accessed directly.
13: if ( ! defined( 'ABSPATH' ) ) {
14: exit;
15: }
16:
17: /**
18: * Get list of all EPL image sizes
19: *
20: * @since 3.2.3
21: * @since 3.6 The medium crop (epl-image-medium-crop) doubled from 300x200 to 600x400, for sharp images on high resolution screens and wider cards. Images uploaded before need their thumbnails regenerated to get the new size; until then the existing 300x200 files are used.
22: */
23: function get_epl_image_sizes() {
24: return apply_filters(
25: 'epl_image_sizes',
26: array(
27: array(
28: 'id' => 'admin-list-thumb',
29: 'height' => 100,
30: 'width' => 100,
31: 'crop' => true,
32: ),
33: array(
34: 'id' => 'epl-image-medium-crop',
35: 'height' => 400,
36: 'width' => 600,
37: 'crop' => true,
38: ),
39: )
40: );
41: }
42:
43: /**
44: * Register image sizes for the admin list of property
45: * and a hard cropped 600x400 px image for use in archives and widgets
46: *
47: * @since 1.0
48: */
49: function epl_image_sizes() {
50:
51: $epl_sizes = get_epl_image_sizes();
52:
53: foreach ( $epl_sizes as $epl_size ) {
54: add_image_size( $epl_size['id'], $epl_size['width'], $epl_size['height'], $epl_size['crop'] );
55: }
56: }
57: add_action( 'after_setup_theme', 'epl_image_sizes' );
58:
59: /**
60: * Gives the hard cropped image a friendly name
61: *
62: * @param array $epl_sizes Array of image sizes.
63: *
64: * @return array
65: * @since 1.0
66: * @since 3.6 Label says 600x400, the new medium crop size.
67: */
68: function epl_image_size_names_choose( $epl_sizes ) {
69: return array_merge(
70: $epl_sizes,
71: array(
72: 'epl-image-medium-crop' => __( 'Medium 600x400 hard Crop', 'easy-property-listings' ),
73: )
74: );
75: }
76: add_filter( 'image_size_names_choose', 'epl_image_size_names_choose' );
77:
78: /**
79: * Add Custom Post Types to Author Archives
80: *
81: * @param array $query WP Query object.
82: * @since 1.0
83: */
84: function epl_property_author_archives( $query ) {
85: // Add 'Rentals' post type to author archives.
86: if ( $query->is_author ) {
87: $query->set( 'post_type', array( 'directory', 'rental', 'testimonial', 'property', 'post' ) );
88: }
89:
90: // Remove the action after it's run.
91: remove_action( 'pre_get_posts', 'epl_property_author_archives' );
92: }
93: add_action( 'pre_get_posts', 'epl_property_author_archives' );
94:
95: /**
96: * Add Custom Post Types to Author Archive Page
97: *
98: * @param array $query WP Query object.
99: * @since 1.0
100: */
101: function epl_custom_post_author_archive( $query ) {
102: if ( $query->is_author ) {
103: $query->set( 'post_type', array( 'property', 'rental', 'testimonial', 'post', 'commercial', 'land' ) );
104: }
105:
106: remove_action( 'pre_get_posts', 'epl_custom_post_author_archive' );
107: }
108: add_action( 'pre_get_posts', 'epl_custom_post_author_archive' );
109:
110: /**
111: * Gravity Forms Filter for populating contact form with author email address
112: *
113: * Create a gravity from and add a hidden field and enable "Allow field to be populated dynamically"
114: * option on the advanced tab. Use author_email as the Parameter Name and when a form is submitted
115: * the email address of the author of that property/page/post will be added to the hidden field.
116: * You can then setup the forwarding and notifications directly to the author as their email
117: * will be part of the form.
118: *
119: * @param string $value Form pre populating field.
120: *
121: * @return string
122: * @since 1.0
123: * @since 3.5.14 Improvements to author email return values in gravity forms email hook.
124: */
125: function epl_populate_post_author_email( $value ) {
126:
127: if ( is_author() ) {
128: $author = get_queried_object();
129: if ( $author instanceof WP_User ) {
130: return $author->user_email;
131: }
132: }
133:
134: global $post;
135: if ( $post instanceof WP_Post ) {
136: $author_email = get_the_author_meta( 'user_email', $post->post_author );
137: if ( ! empty( $author_email ) ) {
138: return $author_email;
139: }
140: }
141:
142: return '';
143: }
144: add_filter( 'gform_field_value_author_email', 'epl_populate_post_author_email' );
145:
146: /**
147: * Gravity Forms: populate a field with the listing's second agent email
148: *
149: * Add a hidden field, tick "Allow field to be populated dynamically" on its Advanced tab and
150: * use the Parameter Name author_email_secondary. Empty when the listing has no second agent.
151: *
152: * @param string $value Field value.
153: *
154: * @return string
155: * @since 3.6
156: */
157: function epl_populate_secondary_author_email( $value ) {
158: global $property;
159:
160: if ( ! $property instanceof EPL_Property_Meta ) {
161: return '';
162: }
163:
164: $agent_id = epl_listing_has_secondary_agent();
165:
166: return $agent_id ? (string) get_the_author_meta( 'user_email', $agent_id ) : '';
167: }
168: add_filter( 'gform_field_value_author_email_secondary', 'epl_populate_secondary_author_email' );
169:
170: /**
171: * Gravity Forms: populate a field with the primary agent email
172: *
173: * Parameter Name primary_agent_email. Uses the current EPL author, so it follows the Staff
174: * Directory agent on staff pages.
175: *
176: * @param string $value Field value.
177: *
178: * @return string
179: * @since 3.6
180: */
181: function epl_populate_primary_agent_email( $value ) {
182: global $epl_author;
183:
184: return ( is_object( $epl_author ) && ! empty( $epl_author->email ) ) ? (string) $epl_author->email : '';
185: }
186: add_filter( 'gform_field_value_primary_agent_email', 'epl_populate_primary_agent_email' );
187:
188: /**
189: * Gravity Forms: populate a field with the primary agent name
190: *
191: * Parameter Name primary_agent_name.
192: *
193: * @param string $value Field value.
194: *
195: * @return string
196: * @since 3.6
197: */
198: function epl_populate_primary_agent_name( $value ) {
199: global $epl_author;
200:
201: return ( is_object( $epl_author ) && ! empty( $epl_author->name ) ) ? (string) $epl_author->name : '';
202: }
203: add_filter( 'gform_field_value_primary_agent_name', 'epl_populate_primary_agent_name' );
204:
205: /**
206: * Gravity Forms: populate a field with the listing unique ID
207: *
208: * Parameter Name property_unique_id.
209: *
210: * @param string $value Field value.
211: *
212: * @return string
213: * @since 3.6
214: */
215: function epl_populate_property_unique_id( $value ) {
216: global $property;
217:
218: return $property instanceof EPL_Property_Meta ? (string) $property->get_property_meta( 'property_unique_id' ) : '';
219: }
220: add_filter( 'gform_field_value_property_unique_id', 'epl_populate_property_unique_id' );
221:
222: /**
223: * Gravity Forms: populate a field with the listing office ID
224: *
225: * Parameter Name property_office_id.
226: *
227: * @param string $value Field value.
228: *
229: * @return string
230: * @since 3.6
231: */
232: function epl_populate_property_office_id( $value ) {
233: global $property;
234:
235: return $property instanceof EPL_Property_Meta ? (string) $property->get_property_meta( 'property_office_id' ) : '';
236: }
237: add_filter( 'gform_field_value_property_office_id', 'epl_populate_property_office_id' );
238: