| 1 |
<?php |
| 2 |
/** |
| 3 |
* ResidenceClass — theme adapter for the WpResidence (WpEstate family) WordPress theme. |
| 4 |
* |
| 5 |
* One of the plugin's theme adapters (enviroment/ directory). Maps RESO-standard MLS |
| 6 |
* property data onto WpResidence's post type ('estate_property') and post-meta |
| 7 |
* keys (property_*, wpestate_*). Stored Listing Write owns shared selection and |
| 8 |
* persistence; this adapter keeps only WpResidence gallery representation, |
| 9 |
* theme field projection, creation defaults, and custom-field registry support. |
| 10 |
* |
| 11 |
* @package MLSImport |
| 12 |
*/ |
| 13 |
// Abort if the file is accessed directly outside of WordPress. |
| 14 |
if ( ! defined( 'ABSPATH' ) ) { |
| 15 |
exit; // Exit if accessed directly |
| 16 |
} |
| 17 |
|
| 18 |
|
| 19 |
/** |
| 20 |
* Persist genuine WpResidence variation behind the Stored adapter seam. |
| 21 |
* |
| 22 |
* @author mlsimport |
| 23 |
*/ |
| 24 |
class ResidenceClass { |
| 25 |
|
| 26 |
/** |
| 27 |
* Constructor — no setup required for this adapter. |
| 28 |
*/ |
| 29 |
public function __construct() { |
| 30 |
|
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
/** |
| 35 |
* return custom post field |
| 36 |
* |
| 37 |
* The post type WpResidence uses to store property listings. |
| 38 |
* |
| 39 |
* @since 1.0.0 |
| 40 |
* @access protected |
| 41 |
* @var string $plugin_name |
| 42 |
* @return string The 'estate_property' post-type slug. |
| 43 |
*/ |
| 44 |
public function get_property_post_type() { |
| 45 |
// WpResidence stores listings in the 'estate_property' post type. |
| 46 |
return 'estate_property'; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Return the WpResidence property post type to Stored Listing Write. |
| 51 |
* |
| 52 |
* @return string Property post-type slug. |
| 53 |
*/ |
| 54 |
public function property_post_type(): string { |
| 55 |
return 'estate_property'; |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Persist WpResidence's one-array gallery representation. |
| 60 |
* |
| 61 |
* @param int $property_id Managed Listing post ID. |
| 62 |
* @param array<int, int> $attachment_ids Final ordered attachment IDs. |
| 63 |
* @return bool Whether the gallery representation was written. |
| 64 |
*/ |
| 65 |
public function write_gallery( int $property_id, array $attachment_ids ): bool { |
| 66 |
update_post_meta( $property_id, 'wpestate_property_gallery', $attachment_ids ); |
| 67 |
return true; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Persist only WpResidence-specific fields, defaults, and agent linkage. |
| 72 |
* |
| 73 |
* Shared selection, normalization, mappings, Rooms, and order have already |
| 74 |
* been resolved by Stored Listing Write. Existing listings skip the defaults |
| 75 |
* and Assigned Agent so later task changes cannot reassign them. |
| 76 |
* |
| 77 |
* @param int $property_id Managed Listing post ID. |
| 78 |
* @param array<string, mixed> $property Raw incoming property. |
| 79 |
* @param array<string, mixed> $context Prepared fields and creation choices. |
| 80 |
* @return bool Whether the theme projection completed. |
| 81 |
*/ |
| 82 |
public function write_theme_projection( int $property_id, array $property, array $context ): bool { |
| 83 |
foreach ( (array) ( $context['fields'] ?? array() ) as $field ) { |
| 84 |
// GitHub issue #286: 'listingkey' is the same case-insensitive meta row |
| 85 |
// as the listing identity, and a WPResidence custom field the edit |
| 86 |
// screen saves blank. The identity is never projected as theme meta. |
| 87 |
if ( 'listingkey' === strtolower( (string) $field['field'] ) ) { |
| 88 |
continue; |
| 89 |
} |
| 90 |
update_post_meta( $property_id, strtolower( (string) $field['field'] ), (string) $field['value'] ); |
| 91 |
} |
| 92 |
if ( empty( $context['is_new'] ) ) { |
| 93 |
return true; |
| 94 |
} |
| 95 |
|
| 96 |
update_post_meta( $property_id, 'prop_featured', 0 ); |
| 97 |
update_post_meta( $property_id, 'page_custom_zoom', 16 ); |
| 98 |
// Country comes from the feed itself (Centris sends "CA"); the helper |
| 99 |
// falls back to the historical "United States" default when absent. |
| 100 |
update_post_meta( $property_id, 'property_country', mlsimport_property_country( $property ) ); |
| 101 |
update_post_meta( $property_id, 'property_agent', (int) ( $context['assigned_agent_id'] ?? 0 ) ); |
| 102 |
update_post_meta( $property_id, 'property_page_desing_local', '' ); |
| 103 |
foreach ( array( 'header_transparent', 'page_show_adv_search', 'sidebar_agent_option', 'local_pgpr_slider_type', 'local_pgpr_content_type', 'sidebar_select', 'sidebar_option' ) as $key ) { |
| 104 |
update_post_meta( $property_id, $key, 'global' ); |
| 105 |
} |
| 106 |
update_post_meta( $property_id, 'header_type', 0 ); |
| 107 |
if ( function_exists( 'wpestate_update_hiddent_address_single' ) ) { |
| 108 |
wpestate_update_hiddent_address_single( $property_id ); |
| 109 |
} |
| 110 |
return true; |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
/** |
| 116 |
* return custom post field |
| 117 |
* |
| 118 |
* The post type(s) WpResidence uses for agents, agencies, and developers. |
| 119 |
* |
| 120 |
* @since 1.0.0 |
| 121 |
* @access protected |
| 122 |
* @var string $plugin_name |
| 123 |
* @return array The agent/agency/developer post-type slugs. |
| 124 |
*/ |
| 125 |
public function get_agent_post_type() { |
| 126 |
// WpResidence splits contacts across three post types. |
| 127 |
return array('estate_agent','estate_agency','estate_developer'); |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
/** |
| 133 |
* save custom fields per environment |
| 134 |
* |
| 135 |
* Syncs the plugin's selected MLS fields into WpResidence's custom-fields registry |
| 136 |
* (stored in 'wpresidence_admin' -> 'wpestate_custom_fields_list'). Enabled, |
| 137 |
* non-admin, non-taxonomy fields are added/updated; others removed; list re-sorted. |
| 138 |
* |
| 139 |
* @since 1.0.0 |
| 140 |
* @access protected |
| 141 |
* @var string $plugin_name |
| 142 |
* @param string $option_name Option prefix used to read '{prefix}_admin_fields_select'. |
| 143 |
*/ |
| 144 |
public function enviroment_custom_fields( $option_name ) { |
| 145 |
// Load the theme option holding the custom-fields registry. |
| 146 |
$theme_options = get_option( 'wpresidence_admin' ); |
| 147 |
$custom_fields = array(); |
| 148 |
if ( isset( $theme_options['wpestate_custom_fields_list'] ) ) { |
| 149 |
$custom_fields = $theme_options['wpestate_custom_fields_list']; |
| 150 |
} |
| 151 |
|
| 152 |
// Ensure we have an array to work with. |
| 153 |
if ( ! is_array( $custom_fields ) ) { |
| 154 |
$custom_fields = array(); |
| 155 |
} |
| 156 |
|
| 157 |
// Guarantee every parallel column exists as an array. |
| 158 |
foreach ( array( 'add_field_name', 'add_field_label', 'add_field_type', 'add_field_order', 'add_dropdown_order' ) as $field_key ) { |
| 159 |
if ( ! isset( $custom_fields[ $field_key ] ) || ! is_array( $custom_fields[ $field_key ] ) ) { |
| 160 |
$custom_fields[ $field_key ] = array(); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
// Load the plugin's field-selection options for this prefix. |
| 165 |
$options = mlsimport_normalized_field_configuration(); |
| 166 |
$active_fields = array_fill_keys( array_keys( mlsimport_field_configuration_metadata() ), true ); |
| 167 |
|
| 168 |
// Reconcile each MLS field against the theme registry. |
| 169 |
foreach ( $options['mls-fields'] as $key => $value ) { |
| 170 |
// Per-field flags: enabled, admin-managed, taxonomy target, order. |
| 171 |
$import = intval( $value ); |
| 172 |
$admin = isset( $options['mls-fields-admin'][ $key ] ) ? intval( $options['mls-fields-admin'][ $key ] ) : 0; |
| 173 |
$taxonomy = isset( $options['mls-fields-map-taxonomy'][ $key ] ) ? $options['mls-fields-map-taxonomy'][ $key ] : ''; |
| 174 |
$order_value = isset( $options['field_order'][ $key ] ) ? intval( $options['field_order'][ $key ] ) + 100 : 100; |
| 175 |
|
| 176 |
// Add/update only enabled, non-admin, non-taxonomy fields. |
| 177 |
if ( isset( $active_fields[ $key ] ) && 1 === $import && 0 === $admin && '' === $taxonomy ) { |
| 178 |
// Is this field already registered? |
| 179 |
$existing_index = array_search( $key, $custom_fields['add_field_name'], true ); |
| 180 |
|
| 181 |
if ( false === $existing_index ) { |
| 182 |
// New field: append across all parallel columns. |
| 183 |
$custom_fields['add_field_name'][] = $key; |
| 184 |
$label = isset( $options['mls-fields-label'][ $key ] ) && '' !== $options['mls-fields-label'][ $key ] ? $options['mls-fields-label'][ $key ] : $key; |
| 185 |
$custom_fields['add_field_label'][] = $label; |
| 186 |
$custom_fields['add_field_type'][] = 'short text'; |
| 187 |
$custom_fields['add_field_order'][] = $order_value; |
| 188 |
$custom_fields['add_dropdown_order'][] = ''; |
| 189 |
} else { |
| 190 |
// Existing field: refresh its label and order in place. |
| 191 |
$label = isset( $options['mls-fields-label'][ $key ] ) && '' !== $options['mls-fields-label'][ $key ] ? $options['mls-fields-label'][ $key ] : $key; |
| 192 |
$custom_fields['add_field_label'][ $existing_index ] = $label; |
| 193 |
$custom_fields['add_field_order'][ $existing_index ] = $order_value; |
| 194 |
} |
| 195 |
} else { |
| 196 |
// Not eligible: remove it from every parallel column if present. |
| 197 |
$remove_index = array_search( $key, $custom_fields['add_field_name'], true ); |
| 198 |
if ( false !== $remove_index ) { |
| 199 |
unset( $custom_fields['add_field_name'][ $remove_index ] ); |
| 200 |
unset( $custom_fields['add_field_label'][ $remove_index ] ); |
| 201 |
unset( $custom_fields['add_field_type'][ $remove_index ] ); |
| 202 |
unset( $custom_fields['add_field_order'][ $remove_index ] ); |
| 203 |
unset( $custom_fields['add_dropdown_order'][ $remove_index ] ); |
| 204 |
} |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
// Re-sort all columns by field order so the registry stays ordered. |
| 209 |
if ( ! empty( $custom_fields['add_field_order'] ) ) { |
| 210 |
// Sort the order column, preserving keys as the new sequence. |
| 211 |
asort( $custom_fields['add_field_order'] ); |
| 212 |
$ordered = array( |
| 213 |
'add_field_name' => array(), |
| 214 |
'add_field_label' => array(), |
| 215 |
'add_field_type' => array(), |
| 216 |
'add_field_order' => array(), |
| 217 |
'add_dropdown_order' => array(), |
| 218 |
); |
| 219 |
// Rebuild each column following the sorted order of indices. |
| 220 |
foreach ( array_keys( $custom_fields['add_field_order'] ) as $idx ) { |
| 221 |
$ordered['add_field_name'][] = $custom_fields['add_field_name'][ $idx ]; |
| 222 |
$ordered['add_field_label'][] = $custom_fields['add_field_label'][ $idx ]; |
| 223 |
$ordered['add_field_type'][] = $custom_fields['add_field_type'][ $idx ]; |
| 224 |
$ordered['add_field_order'][] = $custom_fields['add_field_order'][ $idx ]; |
| 225 |
$ordered['add_dropdown_order'][] = $custom_fields['add_dropdown_order'][ $idx ]; |
| 226 |
} |
| 227 |
$custom_fields = $ordered; |
| 228 |
} |
| 229 |
|
| 230 |
// Persist the updated registry back to the theme option. |
| 231 |
$theme_options['wpestate_custom_fields_list'] = $custom_fields; |
| 232 |
update_option( 'wpresidence_admin', $theme_options ); |
| 233 |
|
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
/** |
| 242 |
* return theme schema |
| 243 |
* |
| 244 |
* No-op placeholder: WpResidence provides no field schema through this adapter. |
| 245 |
* |
| 246 |
* @since 1.0.0 |
| 247 |
* @access protected |
| 248 |
* @var string $plugin_name |
| 249 |
*/ |
| 250 |
public function return_theme_schema() { |
| 251 |
// No schema to return for this theme. |
| 252 |
return; |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
} |
| 258 |
|