PluginProbe
MLSImport: IDX Plugin & MLS Plugin for Real Estate Listings / 6.3.6
MLSImport: IDX Plugin & MLS Plugin for Real Estate Listings v6.3.6
7.2.1 7.2 7.1.2 7.1.1 7.1 7.0.4 7.0.6 7.0.7 6.3.8 6.3.7 6.3.6 6.3.5 6.3.4 6.3.3 6.3.1 trunk 5.7.3 5.7.5 5.8.1 5.8.2 5.8.3 5.8.4 5.8.6 6.0.4 6.0.5 All 36 releases
mlsimport / includes / addons / agents_offices.php

agents_offices.php in MLSImport: IDX Plugin & MLS Plugin for Real Estate Listings 6.3.6, at includes/addons/agents_offices.php

275 lines 9.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3
4 /**
5 * Fetch agent data from MLS API and save it as post meta if not already saved or attempted.
6 *
7 * @param string $agent_id The MLS ID of the agent.
8 * @param int $post_id The post ID to save the fetched data.
9 * @param string $token The API authorization token.
10 */
11 function mlsimport_fetch_agent_data($mls_id,$agent_id, $post_id, $token) {
12 if (!$agent_id || !$token) {
13 //error_log("MLSImport: Missing agent ID or token for post ID {$post_id}");
14 return;
15 }
16
17 // Check if the agent data is already saved or fetch was attempted
18 $existing_member_name = get_post_meta($post_id, 'memberfullname', true);
19 $fetch_attempted = get_post_meta($post_id, 'mlsimport_agent_checked', true);
20
21 if (!empty($existing_member_name) || $fetch_attempted) {
22 //error_log("MLSImport: Agent data already fetched for post ID {$post_id}. Skipping API request.");
23 return;
24 }
25
26
27 $mls_array_data = [
28 '110' => 'mlspin',
29 '200' => 'shared_mlspin_41854c5'
30 ];
31
32 // Construct API URL
33 $api_url = "https://api.bridgedataoutput.com/api/v2/OData/{$mls_array_data[$mls_id]}/Member('{$agent_id}')";
34 //error_log("MLSImport: Fetching agent data from API for Agent ID: {$agent_id}, Post ID: {$post_id}");
35
36 // Set request headers with authorization token
37 $args = array(
38 'headers' => array(
39 'Authorization' => 'Bearer ' . $token
40 )
41 );
42
43 // Make GET request to the API
44 $response = wp_remote_get($api_url, $args);
45
46 // Handle request errors
47 if (is_wp_error($response)) {
48 //error_log("MLSImport: Error fetching agent data for Agent ID: {$agent_id}. Marking as checked.");
49 update_post_meta($post_id, 'mlsimport_agent_checked', 1);
50 return;
51 }
52
53 // Decode the JSON response
54 $data = json_decode(wp_remote_retrieve_body($response), true);
55 //error_log("MLSImport: API response for Agent ID: {$agent_id}: " . json_encode($data));
56
57 if (!empty($data['MemberFullName'])) {
58 update_post_meta($post_id, 'memberfullname', strtolower($data['MemberFullName']));
59 //error_log("MLSImport: Saved MemberFullName for Agent ID: {$agent_id}, Post ID: {$post_id}");
60 }
61
62 // Mark that we attempted to fetch the agent data
63 update_post_meta($post_id, 'mlsimport_agent_checked', 1);
64 }
65
66
67
68
69
70
71 /**
72 * Fetch office data from MLS API and save it as post meta if not already saved or attempted.
73 *
74 * @param string $office_id The MLS ID of the office.
75 * @param int $post_id The post ID to save the fetched data.
76 * @param string $token The API authorization token.
77 */
78 function mlsimport_fetch_office_data($mls_id,$office_id, $post_id, $token) {
79 if (!$office_id || !$token) {
80 //error_log("MLSImport: Missing office ID or token for post ID {$post_id}");
81 return;
82 }
83
84 // Check if the office data is already saved or fetch was attempted
85 $existing_office_name = get_post_meta($post_id, 'officename', true);
86 $fetch_attempted = get_post_meta($post_id, 'mlsimport_office_checked', true);
87
88 if (!empty($existing_office_name) || $fetch_attempted) {
89 //error_log("MLSImport: Office data already fetched for post ID {$post_id}. Skipping API request.");
90 return;
91 }
92
93
94 $mls_array_data = [
95 '110' => 'mlspin',
96 '200' => 'shared_mlspin_41854c5'
97 ];
98
99 // Construct API URL
100 $api_url = "https://api.bridgedataoutput.com/api/v2/OData/{$mls_array_data[$mls_id]}/Office('{$office_id}')";
101
102 //error_log("MLSImport: Fetching office data from API for Office ID: {$office_id}, Post ID: {$post_id}");
103
104 // Set request headers with authorization token
105 $args = array(
106 'headers' => array(
107 'Authorization' => 'Bearer ' . $token
108 )
109 );
110
111 // Make GET request to the API
112 $response = wp_remote_get($api_url, $args);
113
114 // Handle request errors
115 if (is_wp_error($response)) {
116 //error_log("MLSImport: Error fetching office data for Office ID: {$office_id}. Marking as checked.");
117 update_post_meta($post_id, 'mlsimport_office_checked', 1);
118 return;
119 }
120
121 // Decode the JSON response
122 $data = json_decode(wp_remote_retrieve_body($response), true);
123 //error_log("MLSImport: API response for Office ID: {$office_id}: " . json_encode($data));
124
125 if (!empty($data['OfficeName'])) {
126 update_post_meta($post_id, 'officename', strtolower($data['OfficeName']));
127 //error_log("MLSImport: Saved OfficeName for Office ID: {$office_id}, Post ID: {$post_id}");
128 }
129
130 // Mark that we attempted to fetch the office data
131 update_post_meta($post_id, 'mlsimport_office_checked', 1);
132 }
133
134
135
136
137
138 /**
139 * Setup function to fetch and save MLS agent and office data only if not already saved or checked.
140 */
141 function mlsimport_fetch_and_save_mls_data() {
142 if (!is_singular(array('estate_property', 'property'))) {
143 return; // Ensure it only runs on the correct post types
144 }
145
146 global $post;
147
148 // Retrieve plugin options
149 $options = get_option('mlsimport_admin_options');
150
151 // Extract MLS ID and token from options
152 $mls_id = isset($options['mlsimport_mls_name']) ? sanitize_text_field(trim($options['mlsimport_mls_name'])) : '';
153 $mls_token = isset($options['mlsimport_mls_token']) ? sanitize_text_field(trim($options['mlsimport_mls_token'])) : '';
154
155 // Ensure MLS ID is 110 or 200 before proceeding
156 if (!in_array($mls_id, ['110', '200']) || !$mls_token) {
157 // error_log("MLSImport: Invalid MLS ID ({$mls_id}) or missing token. Skipping.");
158 return;
159 }
160 // Get agent and office MLS IDs from post meta
161 $agent_id = get_post_meta($post->ID, 'listagentmlsid', true);
162 $office_id = get_post_meta($post->ID, 'listofficemlsid', true);
163
164 //error_log("MLSImport: Checking property Post ID: {$post->ID}, Agent ID: {$agent_id}, Office ID: {$office_id}");
165
166 // Fetch and save agent and office data only if they are not already stored or checked
167 if ($agent_id) {
168 mlsimport_fetch_agent_data($mls_id,$agent_id, $post->ID, $mls_token);
169 }
170
171 if ($office_id) {
172 mlsimport_fetch_office_data($mls_id,$office_id, $post->ID, $mls_token);
173 }
174 }
175
176 // Run the fetch and save function on template_redirect to ensure data is processed before rendering the page
177 add_action('template_redirect', 'mlsimport_fetch_and_save_mls_data');
178
179
180
181
182
183
184
185
186 /**
187 * Updates custom fields based on the theme ID.
188 *
189 * If the theme ID is 991, it modifies the `wpresidence_admin` options by adding new custom fields.
190 * If the theme ID is 992, it updates the `additional_features` meta field for properties.
191 * The function runs only once by checking `mlsimport_custom_fields_updated`.
192 */
193 function mlsimport_update_custom_fields() {
194 $options = get_option('mlsimport_admin_options');
195 $theme_id = isset($options['mlsimport_theme_used']) ? intval($options['mlsimport_theme_used']) : 0;
196 $mls_id = isset($options['mlsimport_mls_name']) ? sanitize_text_field(trim($options['mlsimport_mls_name'])) : '';
197
198
199
200 // Ensure MLS ID is 110 or 200 before proceeding
201 if (!in_array($mls_id, ['110', '200']) || !$mls_token) {
202 // error_log("MLSImport: Invalid MLS ID ({$mls_id}) or missing token. Skipping.");
203 return;
204 }
205
206 // Check if update has already run to prevent duplicate updates
207 if (get_option('mlsimport_custom_fields_updated')) {
208 return;
209 }
210
211 if ($theme_id === 991) {
212 // Retrieve theme options
213 $theme_options = get_option('wpresidence_admin');
214 $custom_fields = isset($theme_options['wpestate_custom_fields_list']) ? $theme_options['wpestate_custom_fields_list'] : array();
215
216 if (!is_array($custom_fields)) {
217 $custom_fields = array();
218 }
219
220 // Check if the fields already exist before adding
221 if (!in_array('officename', $custom_fields['add_field_name'] ?? [])) {
222 $custom_fields['add_field_name'][] = 'officename';
223 $custom_fields['add_field_label'][] = 'Office Name';
224 $custom_fields['add_field_type'][] = 'short text';
225 $custom_fields['add_field_order'][] = 998;
226 }
227
228 if (!in_array('memberfullname', $custom_fields['add_field_name'] ?? [])) {
229 $custom_fields['add_field_name'][] = 'memberfullname';
230 $custom_fields['add_field_label'][] = 'Member Name';
231 $custom_fields['add_field_type'][] = 'short text';
232 $custom_fields['add_field_order'][] = 999;
233 }
234
235 // Save updated custom fields
236 $theme_options['wpestate_custom_fields_list'] = $custom_fields;
237 if (update_option('wpresidence_admin', $theme_options)) {
238 // Mark as updated to prevent re-running
239 update_option('mlsimport_custom_fields_updated', true);
240 }
241 } else if ($theme_id === 992) {
242 // Update additional features meta for properties
243 global $post;
244 $property_id = $post->ID;
245 $extra_fields = get_post_meta($property_id, 'additional_features', true);
246
247 if (!is_array($extra_fields)) {
248 $extra_fields = array();
249 }
250
251 // Check if the fields already exist before adding
252 $existing_titles = array_column($extra_fields, 'fave_additional_feature_title');
253
254 if (!in_array('officename', $existing_titles)) {
255 $extra_fields[] = array(
256 'fave_additional_feature_title' => 'officename',
257 'fave_additional_feature_value' => '',
258 );
259 }
260
261 if (!in_array('memberfullname', $existing_titles)) {
262 $extra_fields[] = array(
263 'fave_additional_feature_title' => 'memberfullname',
264 'fave_additional_feature_value' => '',
265 );
266 }
267
268 // Save updated additional features
269 update_post_meta($property_id, 'additional_features', $extra_fields);
270 }
271 }
272
273 // Hook into admin init or another relevant action
274 add_action('admin_init', 'mlsimport_update_custom_fields');
275