Changeset 3134790
- Timestamp:
- 08/13/2024 09:24:08 AM (20 months ago)
- Location:
- mlsimport
- Files:
-
- 18 edited
- 1 copied
-
tags/5.8.1 (copied) (copied from mlsimport/trunk)
-
tags/5.8.1/admin/class-mlsimport-admin.php (modified) (21 diffs)
-
tags/5.8.1/admin/partials/mlsimport-administrative-options.php (modified) (1 diff)
-
tags/5.8.1/enviroment/EstateClass.php (modified) (2 diffs)
-
tags/5.8.1/enviroment/HouzezClass.php (modified) (2 diffs)
-
tags/5.8.1/enviroment/RealHomesClass.php (modified) (2 diffs)
-
tags/5.8.1/enviroment/ResidenceClass.php (modified) (2 diffs)
-
tags/5.8.1/includes/ThemeImport.php (modified) (3 diffs)
-
tags/5.8.1/includes/help_functions.php (modified) (1 diff)
-
tags/5.8.1/mlsimport.php (modified) (2 diffs)
-
trunk/admin/class-mlsimport-admin.php (modified) (21 diffs)
-
trunk/admin/partials/mlsimport-administrative-options.php (modified) (1 diff)
-
trunk/enviroment/EstateClass.php (modified) (2 diffs)
-
trunk/enviroment/HouzezClass.php (modified) (2 diffs)
-
trunk/enviroment/RealHomesClass.php (modified) (2 diffs)
-
trunk/enviroment/ResidenceClass.php (modified) (2 diffs)
-
trunk/includes/ThemeImport.php (modified) (3 diffs)
-
trunk/includes/help_functions.php (modified) (1 diff)
-
trunk/mlsimport.php (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
mlsimport/tags/5.8.1/admin/class-mlsimport-admin.php
r3093628 r3134790 419 419 $valid = array(); 420 420 421 $field_import = array( 'force_rand', 'min_price', 'max_price', 'title_format', 'property_agent', 'property_user', 'City', 'City_check', 'CountyOrParish', 'CountyOrParish_check', 'MlsStatus', 'MlsStatus_check', 'PropertySubType', 'PropertySubType_check', 'PropertyType', 'PropertyType_check', 'StandardStatus', 'StandardStatus_check', 'InternetEntireListingDisplayYN', 'InternetAddressDisplayYN' ); 421 $field_import = array( 'force_rand', 'min_price', 'max_price', 'title_format', 'property_agent', 'property_user', 'City', 'City_check', 'CountyOrParish', 'CountyOrParish_check', 'MlsStatus', 'MlsStatus_check', 'PropertySubType', 'PropertySubType_check', 'PropertyType', 'PropertyType_check', 422 'StandardStatus_delete', 'StandardStatus_delete_check', 'InternetEntireListingDisplayYN', 'InternetAddressDisplayYN' ); 422 423 foreach ( $field_import as $key ) { 423 424 $valid[ $key ] = $input[ $key ]; … … 543 544 global $post; 544 545 $options = get_option( $this->plugin_name . '_admin_fields_select' ); 545 546 echo 'ListingKey: '.get_post_meta( $post->ID, 'ListingKey', true ).'<br>'; 546 547 foreach ( $options['mls-fields-admin'] as $key => $value ) { 547 548 if ( 1 === intval($options['mls-fields-admin'][ $key ] ) ) { … … 810 811 $values['mlsimport_paragon_client_secret'] = $mlsimport_paragon_client_secret; 811 812 812 $answer = $this->theme_importer->global _api_request_saas( 'clients', $values, 'PATCH' );813 $answer = $this->theme_importer->globalApiRequestSaas( 'clients', $values, 'PATCH' ); 813 814 814 815 … … 897 898 898 899 $theme_Start = new ThemeImport(); 899 $answer = $theme_Start::global _api_request_saas( 'token', $values, 'POST' );900 $answer = $theme_Start::globalApiRequestSaas( 'token', $values, 'POST' ); 900 901 901 902 … … 959 960 'mlsimport_item_standardstatus_check', 960 961 'mlsimport_item_standardstatus', 962 'mlsimport_item_standardstatusdelete_check', 963 'mlsimport_item_standardstatusdelete', 964 961 965 'mlsimport_item_internetentirelistingdisplayyn', 962 966 'mlsimport_item_internetaddressdisplayyn', … … 1008 1012 1009 1013 1010 1011 1012 /** 1013 * 1014 * 1015 * Display Meta options 1016 */ 1017 public function mlsimport_saas_display_meta_options( $post ) { 1018 wp_nonce_field( plugin_basename( __FILE__ ), 'estate_agent_noncename' ); 1019 global $post; 1014 /** 1015 * Display Meta Options 1016 * 1017 * @param WP_Post $post The post object. 1018 */ 1019 public function mlsimport_saas_display_meta_options($post) { 1020 wp_nonce_field(plugin_basename(__FILE__), 'estate_agent_noncename'); 1020 1021 global $mlsimport; 1022 1023 $postId = $post->ID; 1021 1024 $token = $mlsimport->admin->mlsimport_saas_get_mls_api_token_from_transient(); 1022 1023 $post_id = $post->ID;1024 1025 $found_items = 'none';1026 $mlsimport_item_how_many = esc_html( get_post_meta( $post_id, 'mlsimport_item_how_many', true ) );1027 $mlsimport_item_stat_cron = esc_html( get_post_meta( $post_id, 'mlsimport_item_stat_cron', true ) );1028 $last_date = get_post_meta( $post_id, 'mlsimport_last_date', true );1029 $status = get_option( 'mlsimport_force_stop_' . $post_id );1030 $field_import = $this->mlsimport_saas_return_mls_fields();1031 1032 // admin options1033 $options = get_option( 'mlsimport_admin_options' );1034 $mlsimport_mls_id = 0;1035 if ( isset( $options['mlsimport_mls_name'] ) && '' !== $options['mlsimport_mls_name'] ) {1036 $mlsimport_mls_id = intval( $options['mlsimport_mls_name'] );1037 }1038 1039 $mlsrequest = $this->mlsimport_make_listing_requests( $post_id );1040 1041 if ( isset( $mlsrequest['success'] ) && ! $mlsrequest['success'] ) { ?>1042 1043 <div class="mlsimport_warning">1044 <?php print esc_html( $mlsrequest['message'] ) ;?>1045 </div>1046 <?php }1047 1048 //print_r($mlsrequest);1049 1025 1050 if (isset($mlsrequest['results'])): ?>1051 <?php $found_items = intval($mlsrequest['results']); ?>1052 <?php else: ?>1053 <?php1054 // delete_transient('mlsimport_saas_token');1055 $mlsimport->admin->mlsimport_saas_check_mls_connection();1056 esc_html_e('Your Token was expired. Please refresh the page to renew it wait while we renew it.','mlsimport');1057 ?>1058 <?php endif; ?>1026 $mlsimportItemHowMany = esc_html(get_post_meta($postId, 'mlsimport_item_how_many', true)); 1027 $mlsimportItemStatCron = esc_html(get_post_meta($postId, 'mlsimport_item_stat_cron', true)); 1028 $lastDate = get_post_meta($postId, 'mlsimport_last_date', true); 1029 $status = get_option('mlsimport_force_stop_' . $postId); 1030 $fieldImport = $this->mlsimport_saas_return_mls_fields(); 1031 $options = get_option('mlsimport_admin_options'); 1032 $mlsimportMlsId = isset($options['mlsimport_mls_name']) && $options['mlsimport_mls_name'] !== '' 1033 ? intval($options['mlsimport_mls_name']) 1034 : 0; 1059 1035 1060 <div class="mlsimport_item_search_url" style="display:xnone;"> <?php echo esc_html__('Last date/time we check :','mlsimport').' '. esc_html($last_date); ?> </div> 1036 $mlsRequest = $this->mlsimport_make_listing_requests($postId); 1037 if (isset($mlsRequest['success']) && !$mlsRequest['success']) { 1038 echo '<div class="mlsimport_warning">' . esc_html($mlsRequest['message']) . '</div>'; 1039 } 1061 1040 1041 $foundItems = isset($mlsRequest['results']) ? intval($mlsRequest['results']) : 'none'; 1042 if ($foundItems === 'none') { 1043 $mlsimport->admin->mlsimport_saas_check_mls_connection(); 1044 esc_html_e('Your Token was expired. Please refresh the page to renew it wait while we renew it.', 'mlsimport'); 1045 } 1046 1047 echo $this->generateMetaOptionsHtml($postId, $foundItems, $lastDate, $mlsimportItemHowMany, $mlsimportItemStatCron, $mlsimportMlsId, $fieldImport); 1048 } 1049 1050 1051 1052 1053 /** 1054 * Generate Meta Options HTML 1055 * 1056 * @param int $postId The post ID. 1057 * @param int $foundItems The number of found items. 1058 * @param string $lastDate The last date checked. 1059 * @param string $mlsimportItemHowMany How many items to import. 1060 * @param string $mlsimportItemStatCron The status of the cron job. 1061 * @param int $mlsimportMlsId The MLS import ID. 1062 * @param array $fieldImport The fields to import. 1063 * @return string The generated HTML. 1064 */ 1065 private function generateMetaOptionsHtml($postId, $foundItems, $lastDate, $mlsimportItemHowMany, $mlsimportItemStatCron, $mlsimportMlsId, $fieldImport) { 1066 ob_start(); 1067 1068 ?> 1069 <div class="mlsimport_item_search_url" style="display:none;"><?php echo esc_html__('Last date/time we check :', 'mlsimport') . ' ' . esc_html($lastDate); ?></div> 1062 1070 <ul> 1063 1071 <li>1. Set the import parameters.</li> … … 1067 1075 </ul> 1068 1076 1069 1070 <?php if (is_numeric($found_items) && $found_items >= 500): ?> 1077 <?php if (is_numeric($foundItems) && $foundItems >= 500): ?> 1071 1078 <div class="mlsimport_notification"> 1072 1079 <?php esc_html_e('You found a large number of listings. While MlsImport import can handle such a large number, you need to make sure that your server can do this operation. This import will take some time. Make sure your server has the capacity, there are no time limits for a long-running process and consider splitting the import between multiple MLS Import items.', 'mlsimport'); ?> … … 1075 1082 1076 1083 <div class="mlsimport_import_no"> 1077 <?php esc_html_e('We found','mlsimport');?> 1078 <strong><?php echo esc_html($found_items); ?></strong> listings. If you decide to import all of them make sure your server database can handle the load. Please do a database backup before initial import.</div> 1084 <?php esc_html_e('We found', 'mlsimport'); ?> 1085 <strong><?php echo esc_html($foundItems); ?></strong> listings. If you decide to import all of them make sure your server database can handle the load. Please do a database backup before initial import. 1086 </div> 1079 1087 1080 1088 <fieldset class="mlsimport-fieldset"> … … 1082 1090 <?php esc_html_e('How Many to import. Use 0 if you want to import all listings found.', 'mlsimport'); ?> 1083 1091 </label> 1084 <input type="text" id="mlsimport_item_how_many" name="mlsimport_item_how_many" value="<?php echo esc_attr($mlsimport _item_how_many); ?>"/>1092 <input type="text" id="mlsimport_item_how_many" name="mlsimport_item_how_many" value="<?php echo esc_attr($mlsimportItemHowMany); ?>"/> 1085 1093 </fieldset> 1086 1094 1087 1095 <fieldset class="mlsimport-fieldset mlsimport_auto_switch"> 1088 <?php esc_html_e('Enable Auto Update every hour?', 'mlsimport'); ?>1089 <label class="mlsimport_switch"> 1096 <?php esc_html_e('Enable Auto Update every hour?', 'mlsimport'); ?> 1097 <label class="mlsimport_switch"> 1090 1098 <input type="hidden" value="0" name="mlsimport_item_stat_cron"> 1091 <input type="checkbox" value="1" name="mlsimport_item_stat_cron"<?php if (intval($mlsimport _item_stat_cron) !== 0) echo esc_html(' checked'); ?>>1099 <input type="checkbox" value="1" name="mlsimport_item_stat_cron"<?php if (intval($mlsimportItemStatCron) !== 0) echo esc_html(' checked'); ?>> 1092 1100 <span class="slider round"></span> 1093 1101 </label> 1094 1102 </fieldset> 1095 1103 1096 1097 1098 1099 1100 1101 1102 <?php 1103 1104 if ('' !== $mlsimport_item_stat_cron): ?> 1104 <?php if ($mlsimportItemStatCron !== ''): ?> 1105 1105 <div id="mlsimport_item_status"></div> 1106 <input class="button mlsimport_button" type="button" id="mlsimport-start_item" 1107 data-post-number="<?php echo intval($found _items); ?>"1108 data-post_id="<?php echo intval($post _id); ?>" value="Start Import">1109 <input class="button mlsimport_button" type="button" id="mlsimport_stop_item" 1110 data-post-number="<?php echo intval($found _items); ?>"1111 data-post_id="<?php echo intval($post _id); ?>" value="Stop Import">1106 <input class="button mlsimport_button" type="button" id="mlsimport-start_item" 1107 data-post-number="<?php echo intval($foundItems); ?>" 1108 data-post_id="<?php echo intval($postId); ?>" value="Start Import"> 1109 <input class="button mlsimport_button" type="button" id="mlsimport_stop_item" 1110 data-post-number="<?php echo intval($foundItems); ?>" 1111 data-post_id="<?php echo intval($postId); ?>" value="Stop Import"> 1112 1112 <?php endif; ?> 1113 1113 1114 <input type="hidden" id="mlsimport_item_actions" value="<?php echo esc_attr($ajax_nonce = wp_create_nonce("mlsimport_item_actions")); ?>" /> 1115 <div class="mlsimport_param_wrapper"><h2>Import Parameters</h2> 1116 1117 <?php $mlsimport_item_title_format = esc_html(get_post_meta($post_id, 'mlsimport_item_title_format', true)); ?> 1118 1119 <fieldset class="mlsimport-fieldset"> 1120 <label class="mlsimport-label" for="mlsimport_item_title_format"> 1121 <?php esc_html_e('Title Format', 'mlsimport'); ?> 1122 </label> 1123 1124 <p class="mlsimport-exp">You can use {Address}, {City}, {CountyOrParish}, {StateOrProvince}, {PostalCode}, {PropertyType}, {Bedrooms}, {Bathrooms}, {ListingKey}, {ListingId},{StreetNumberNumeric} or {StreetName} </p> 1125 <input type="text" id="mlsimport_item_title_format" name="mlsimport_item_title_format" value="<?php echo '' !== $mlsimport_item_title_format ? trim(esc_html($mlsimport_item_title_format)) : esc_html('{Address},{City},{CountyOrParish},{PropertyType}'); ?>"/> 1126 </fieldset> 1127 1128 <?php $mlsimport_item_agent = esc_html(get_post_meta($post_id, 'mlsimport_item_agent', true)); ?> 1129 1130 <fieldset class="mlsimport-fieldset"> 1131 <label class="mlsimport-label" for="mlsimport_item_agent"> 1132 <?php esc_html_e('Select Agent', 'mlsimport'); ?> 1133 </label> 1134 <select class="mlsimport-select" name="mlsimport_item_agent" id="mlsimport_item_agent"> 1135 <?php 1136 $permited_tags= mlsimport_allowed_html_tags_content(); 1137 $select_agent= $this->theme_importer->mlsimport_saas_theme_import_select_agent($mlsimport_item_agent); 1138 print wp_kses($select_agent,$permited_tags); 1139 ?> 1140 </select> 1141 </fieldset> 1142 1143 1144 <?php 1145 $mlsimport_item_property_status = esc_html(get_post_meta($post_id, 'mlsimport_item_property_status', true)); 1146 if ('' === $mlsimport_item_property_status) { 1147 $mlsimport_item_property_status = 'publish'; 1148 } 1149 $status_array = array('publish', 'draft'); 1150 ?> 1151 <fieldset class="mlsimport-fieldset"> 1152 <label class="mlsimport-label" for="mlsimport_item_property_status"> 1153 <?php esc_html_e('Select Property Status on import', 'mlsimport'); ?> 1154 </label> 1155 <select class="mlsimport-select" name="mlsimport_item_property_status" id="mlsimport_item_property_status"> 1156 <?php foreach ($status_array as $value): ?> 1157 <option value="<?php echo esc_attr($value); ?>" <?php if ($value === $mlsimport_item_property_status) echo esc_html('selected'); ?>> 1158 <?php echo esc_html($value); ?> 1159 </option> 1160 <?php endforeach; ?> 1161 </select> 1162 </fieldset> 1163 1164 <?php 1165 $mlsimport_item_property_user = esc_html(get_post_meta($post_id, 'mlsimport_item_property_user', true)); 1166 ?> 1167 <fieldset class="mlsimport-fieldset"> 1168 <label class="mlsimport-label" for="mlsimport_item_property_user"> 1169 <?php esc_html_e('User', 'mlsimport'); ?> 1170 </label> 1171 <select class="mlsimport-select" id="mlsimport_item_property_user" name="mlsimport_item_property_user"> 1172 <?php 1173 $select_user = $this->theme_importer->mlsimport_saas_theme_import_select_user($mlsimport_item_property_user); 1174 print wp_kses($select_user,$permited_tags); 1175 ?> 1176 </select> 1177 </fieldset> 1178 1179 1180 1181 <?php 1182 $mlsimport_item_min_price = floatval(get_post_meta($post_id, 'mlsimport_item_min_price', true)); 1183 $mlsimport_item_max_price = floatval(get_post_meta($post_id, 'mlsimport_item_max_price', true)); 1184 if (0 === intval($mlsimport_item_max_price)) { 1185 $mlsimport_item_max_price = 10000000; 1186 } 1187 ?> 1188 <fieldset class="mlsimport-fieldset"> 1189 <label class="mlsimport-label"> 1190 <?php esc_html_e('Price Between', 'mlsimport'); ?> 1191 </label> 1192 <input type="text" class="mlsimport-select" id="mlsimport_item_min_price" name="mlsimport_item_min_price" value="<?php echo esc_attr($mlsimport_item_min_price); ?>"> and 1193 <input type="text" class="mlsimport-select" id="mlsimport_item_max_price" name="mlsimport_item_max_price" value="<?php echo esc_attr($mlsimport_item_max_price); ?>"> 1194 </fieldset> 1195 1196 1197 <?php 1198 $options = get_option( $this->plugin_name . '_admin_options' ); 1199 1200 $mls_id = ''; 1201 if ( isset( $options['mlsimport_mls_name'] ) ) { 1202 $mls_id = sanitize_text_field( trim( $options['mlsimport_mls_name'] ) ); 1203 } 1204 1205 if ( $mls_id > 5000 ) { 1206 $field_import['PropertyType']['multiple'] = 'no'; 1207 } 1208 1209 foreach ($field_import as $key => $field): 1210 $name_check = strtolower('mlsimport_item_' . $key . '_check'); 1211 $name = strtolower('mlsimport_item_' . $key); 1212 1213 $value = get_post_meta($post_id, $name, true); 1214 $value_check = get_post_meta($post_id, $name_check, true); 1215 $extra_class = ''; 1216 if ('extraCity' === $key || 'extraCounty' === $key) { 1217 $extra_class = ' mlsimport_hidden_field_button'; 1114 <input type="hidden" id="mlsimport_item_actions" value="<?php echo esc_attr(wp_create_nonce("mlsimport_item_actions")); ?>"/> 1115 <div class="mlsimport_param_wrapper"><h2><?php esc_html_e('Import Parameters', 'mlsimport'); ?></h2> 1116 1117 <?php 1118 $mlsimportItemTitleFormat = esc_html(get_post_meta($postId, 'mlsimport_item_title_format', true)); 1119 ?> 1120 1121 <fieldset class="mlsimport-fieldset"> 1122 <label class="mlsimport-label" for="mlsimport_item_title_format"> 1123 <?php esc_html_e('Title Format', 'mlsimport'); ?> 1124 </label> 1125 1126 <p class="mlsimport-exp"><?php esc_html_e('You can use {Address}, {City}, {CountyOrParish}, {StateOrProvince}, {PostalCode}, {PropertyType}, {Bedrooms}, {Bathrooms}, {ListingKey}, {ListingId},{StreetNumberNumeric} or {StreetName}', 'mlsimport'); ?></p> 1127 <input type="text" id="mlsimport_item_title_format" name="mlsimport_item_title_format" value="<?php echo '' !== $mlsimportItemTitleFormat ? trim(esc_html($mlsimportItemTitleFormat)) : esc_html('{Address},{City},{CountyOrParish},{PropertyType}'); ?>"/> 1128 </fieldset> 1129 1130 <?php 1131 $mlsimportItemAgent = esc_html(get_post_meta($postId, 'mlsimport_item_agent', true)); 1132 ?> 1133 1134 <fieldset class="mlsimport-fieldset"> 1135 <label class="mlsimport-label" for="mlsimport_item_agent"> 1136 <?php esc_html_e('Select Agent', 'mlsimport'); ?> 1137 </label> 1138 <select class="mlsimport-select" name="mlsimport_item_agent" id="mlsimport_item_agent"> 1139 <?php 1140 $permitedTags = mlsimport_allowed_html_tags_content(); 1141 $selectAgent =$this->theme_importer->mlsimportSaasThemeImportSelectAgent($mlsimportItemAgent); 1142 print wp_kses($selectAgent, $permitedTags); 1143 ?> 1144 </select> 1145 </fieldset> 1146 1147 <?php 1148 $mlsimportItemPropertyStatus = esc_html(get_post_meta($postId, 'mlsimport_item_property_status', true)); 1149 if ('' === $mlsimportItemPropertyStatus) { 1150 $mlsimportItemPropertyStatus = 'publish'; 1151 } 1152 $statusArray = array('publish', 'draft'); 1153 ?> 1154 <fieldset class="mlsimport-fieldset"> 1155 <label class="mlsimport-label" for="mlsimport_item_property_status"> 1156 <?php esc_html_e('Select Property Status on import', 'mlsimport'); ?> 1157 </label> 1158 <select class="mlsimport-select" name="mlsimport_item_property_status" id="mlsimport_item_property_status"> 1159 <?php foreach ($statusArray as $value): ?> 1160 <option value="<?php echo esc_attr($value); ?>" <?php if ($value === $mlsimportItemPropertyStatus) echo esc_html('selected'); ?>> 1161 <?php echo esc_html($value); ?> 1162 </option> 1163 <?php endforeach; ?> 1164 </select> 1165 </fieldset> 1166 1167 <?php 1168 $mlsimportItemPropertyUser = esc_html(get_post_meta($postId, 'mlsimport_item_property_user', true)); 1169 ?> 1170 <fieldset class="mlsimport-fieldset"> 1171 <label class="mlsimport-label" for="mlsimport_item_property_user"> 1172 <?php esc_html_e('User', 'mlsimport'); ?> 1173 </label> 1174 <select class="mlsimport-select" id="mlsimport_item_property_user" name="mlsimport_item_property_user"> 1175 <?php 1176 $selectUser = $this->theme_importer->mlsimportSaasThemeImportSelectUser($mlsimportItemPropertyUser); 1177 print wp_kses($selectUser, $permitedTags); 1178 ?> 1179 </select> 1180 </fieldset> 1181 1182 <?php 1183 $mlsimportItemMinPrice = floatval(get_post_meta($postId, 'mlsimport_item_min_price', true)); 1184 $mlsimportItemMaxPrice = floatval(get_post_meta($postId, 'mlsimport_item_max_price', true)); 1185 if (0 === intval($mlsimportItemMaxPrice)) { 1186 $mlsimportItemMaxPrice = 10000000; 1218 1187 } 1219 1188 ?> 1189 <fieldset class="mlsimport-fieldset"> 1190 <label class="mlsimport-label"> 1191 <?php esc_html_e('Price Between', 'mlsimport'); ?> 1192 </label> 1193 <input type="text" class="mlsimport-select" id="mlsimport_item_min_price" name="mlsimport_item_min_price" value="<?php echo esc_attr($mlsimportItemMinPrice); ?>"> and 1194 <input type="text" class="mlsimport-select" id="mlsimport_item_max_price" name="mlsimport_item_max_price" value="<?php echo esc_attr($mlsimportItemMaxPrice); ?>"> 1195 </fieldset> 1196 1197 <?php 1198 $options = get_option($this->plugin_name . '_admin_options'); 1199 1200 $mlsId = ''; 1201 if (isset($options['mlsimport_mls_name'])) { 1202 $mlsId = sanitize_text_field(trim($options['mlsimport_mls_name'])); 1203 } 1204 1205 if ($mlsId > 5000) { 1206 $fieldImport['PropertyType']['multiple'] = 'no'; 1207 } 1208 1209 foreach ($fieldImport as $key => $field): 1210 $nameCheck = strtolower('mlsimport_item_' . $key . '_check'); 1211 $name = strtolower('mlsimport_item_' . $key); 1212 1213 $value = get_post_meta($postId, $name, true); 1214 $valueCheck = get_post_meta($postId, $nameCheck, true); 1215 $extraClass = ''; 1216 if ('extraCity' === $key || 'extraCounty' === $key) { 1217 $extraClass = ' mlsimport_hidden_field_button'; 1218 } 1219 ?> 1220 1220 <fieldset class="mlsimport-fieldset"> 1221 <label class="mlsimport-label <?php echo esc_attr($extra _class); ?>" for="<?php echo esc_attr($name); ?>">1221 <label class="mlsimport-label <?php echo esc_attr($extraClass); ?>" for="<?php echo esc_attr($name); ?>"> 1222 1222 <?php echo esc_html($field['label']); ?> 1223 1223 </label> 1224 1224 <?php if ('extraCity' === $key || 'extraCounty' === $key): ?> 1225 <div class="mlsimport-input-wrapper" style="display:none"> 1226 <?php endif; ?> 1227 <p class="mlsimport-exp"><?php echo wp_kses_post($this->mlsimport_notes_for_mls($mlsimport_mls_id, $name, $field['description'])); ?> 1228 <?php 1229 $is_checkbox_admin = 0; 1230 if (1 === intval($value_check)) { 1231 $is_checkbox_admin = 1; 1232 } 1233 1234 $select_all_none = [ 1235 'InternetAddressDisplayYN', 1236 'InternetEntireListingDisplayYN', 1237 'PostalCode', 1238 'ListAgentKey', 1239 'ListAgentMlsId', 1240 'ListOfficeKey', 1241 'ListOfficeMlsId', 1242 'StandardStatus', 1243 'ListingId', 1244 'extraCity', 1245 'extraCounty', 1246 'Exclude_ListOfficeKey', 1247 'Exclude_ListOfficeMlsId', 1248 'Exclude_ListAgentKey', 1249 'Exclude_ListAgentMlsId', 1250 ]; 1251 1252 if ($mls_id > 5000) { 1253 $select_all_none[] = 'PropertyType'; 1254 } 1255 1256 if (!in_array($key, $select_all_none)): ?> 1257 <?php 1258 esc_html_e('- Or Select All ', 'mlsimport'); 1259 print esc_html( $is_checkbox_admin); 1260 ?> 1261 <input type="hidden" name="<?php echo esc_attr($name_check); ?>" value="0" /> 1262 <input type="checkbox" name="<?php echo esc_attr($name_check); ?>" value="1" <?php print esc_attr( checked($is_checkbox_admin, 1, 0)); ?> /> 1263 1264 1265 <?php endif; ?> 1266 </p> 1267 1225 <div class="mlsimport-input-wrapper" style="display:none"> 1226 <?php endif; ?> 1227 <p class="mlsimport-exp"><?php echo wp_kses_post($this->mlsimport_notes_for_mls($mlsimportMlsId, $name, $field['description'])); ?> 1228 <?php 1229 $isCheckboxAdmin = 0; 1230 if (1 === intval($valueCheck)) { 1231 $isCheckboxAdmin = 1; 1232 } 1233 1234 $selectAllNone = [ 1235 'InternetAddressDisplayYN', 1236 'InternetEntireListingDisplayYN', 1237 'PostalCode', 1238 'ListAgentKey', 1239 'ListAgentMlsId', 1240 'ListOfficeKey', 1241 'ListOfficeMlsId', 1242 'StandardStatus', 1243 'StandardStatusDelete', 1244 'ListingId', 1245 'extraCity', 1246 'extraCounty', 1247 'Exclude_ListOfficeKey', 1248 'Exclude_ListOfficeMlsId', 1249 'Exclude_ListAgentKey', 1250 'Exclude_ListAgentMlsId', 1251 ]; 1252 1253 if ($mlsId > 5000) { 1254 $selectAllNone[] = 'PropertyType'; 1255 } 1256 1257 if (!in_array($key, $selectAllNone)): ?> 1258 <?php 1259 esc_html_e('- Or Select All ', 'mlsimport'); 1260 1261 ?> 1262 <input type="hidden" name="<?php echo esc_attr($nameCheck); ?>" value="0"/> 1263 <input type="checkbox" name="<?php echo esc_attr($nameCheck); ?>" value="1" <?php print esc_attr(checked($isCheckboxAdmin, 1, 0)); ?>/> 1264 <?php endif; ?> 1265 </p> 1266 1267 <?php 1268 $permittedStatus = ['active', 'active under contract', 'coming soon', 'activeundercontract', 'comingsoon', 'pending']; 1269 1270 if ($field['type'] === 'select'): ?> 1271 <?php 1272 $multiple = ''; 1273 if ('yes' === $field['multiple']) { 1274 $multiple = 'multiple'; 1275 $name .= '[]'; 1276 } 1277 1278 if ('StandardStatus' === $key && '' === $value) { 1279 $value = ['Active']; 1280 } 1268 1281 1269 1282 1270 <?php 1271 $permited_status=array('active','active under contract','coming soon','activeundercontract','comingsoon','pending'); 1272 1273 1274 1275 if ($field['type'] === 'select'): ?> 1276 <?php 1277 $multiple = ''; 1278 if ('yes' === $field['multiple']) { 1279 $multiple = 'multiple'; 1280 $name .= '[]'; 1281 } 1282 1283 if ('StandardStatus' === $key && '' === $value) { 1284 $value = ['Active']; 1285 } 1286 //edmonton ray 1287 1288 if( $key=="StandardStatus" && $mls_id==111){ 1289 $value==''; 1290 $field['values'] =array(); 1291 1292 } 1293 1294 1295 // Additional conditions can be placed here. 1296 ?> 1297 <select class="mlsimport-select" id="<?php echo esc_attr($name); ?>" name="<?php echo esc_attr($name); ?>" <?php echo esc_attr($multiple); ?>> 1298 <?php foreach ($field['values'] as $select_key): ?> 1299 1300 <?php if ('' !== $select_key): ?> 1301 <option value="<?php echo esc_attr($select_key); ?>" 1302 <?php 1303 if( $key=="StandardStatus" && !in_array(strtolower($select_key), $permited_status) ){ 1304 print 'disabled'; 1305 } 1306 1307 ?> 1308 <?php if (is_array($value) ? in_array($select_key, $value) : $select_key === $value) echo 'selected'; ?>> 1309 1310 <?php echo esc_html($select_key); ?> 1311 </option> 1312 <?php endif; ?> 1313 1314 <?php endforeach; ?> 1315 </select> 1316 <?php elseif ($field['type'] === 'input'): ?> 1317 <input type="text" class="mlsimport-select" id="<?php echo esc_attr($name); ?>" name="<?php echo esc_attr($name); ?>" value="<?php echo esc_attr($value); ?>"> 1318 <?php endif; ?> 1319 <?php if ('extraCity' === $key || 'extraCounty' === $key): ?> 1320 </div> 1321 <?php endif; ?> 1283 1284 // Additional conditions can be placed here. 1285 ?> 1286 <select class="mlsimport-select" id="<?php echo esc_attr($name); ?>" name="<?php echo esc_attr($name); ?>" <?php echo esc_attr($multiple); ?>> 1287 <?php foreach ($field['values'] as $selectKey): ?> 1288 1289 <?php if ('' !== $selectKey): ?> 1290 <option value="<?php echo esc_attr($selectKey); ?>" 1291 <?php 1292 if ($key === "StandardStatusDelete" && $value==null ) { 1293 1294 print 'selected'; 1295 } 1296 ?> 1297 <?php if (is_array($value) ? in_array($selectKey, $value) : $selectKey === $value) echo 'selected'; ?>> 1298 <?php echo esc_html($selectKey); ?> 1299 </option> 1300 <?php endif; ?> 1301 1302 <?php endforeach; ?> 1303 </select> 1304 1305 <?php elseif ($field['type'] === 'input'): ?> 1306 <input type="text" class="mlsimport-select" id="<?php echo esc_attr($name); ?>" name="<?php echo esc_attr($name); ?>" value="<?php echo esc_attr($value); ?>"> 1307 <?php endif; ?> 1308 <?php if ('extraCity' === $key || 'extraCounty' === $key): ?> 1309 </div> 1310 <?php endif; ?> 1322 1311 </fieldset> 1323 <?php endforeach; ?> 1324 1325 1326 </div> 1327 1328 <?php 1329 } 1330 1312 <?php endforeach; ?> 1313 1314 </div> 1315 <?php 1316 return ob_get_clean(); 1317 } 1318 1319 1320 1331 1321 1332 1322 … … 1447 1437 public function mlsimport_saas_start_doing_reconciliation() { 1448 1438 global $mlsimport; 1439 print 'start'; 1449 1440 $listingKey_in_Local = $this->mlsimport_saas_get_all_meta_values( 'ListingKey' ); 1450 1441 … … 1469 1460 1470 1461 if ( in_array( $listingkey, $listingKey_in_MLS ) ) { 1471 print wp_kses_post( $listingkey . ' IS FOUND </br>');1462 print wp_kses_post('</br>'.$listingkey . ' IS FOUND'); 1472 1463 } else { 1473 ++$to_delete; 1474 print wp_kses_post('</br>' .$listingkey. ' ------------------------- NOT FOUND delete: '.$property_id.' <-'); 1475 $mlsimport->admin->theme_importer->mlsimport_saas_delete_property_via_mysql( $property_id, $listingkey ); 1464 $delete_status= get_post_meta( $property_id, 'mlsImportItemStatusDelete', true ); 1465 1466 if($delete_status=='' || $delete_status=='delete'){ 1467 ++$to_delete; 1468 print wp_kses_post('</br>' .$listingkey. ' ------------------------- NOT FOUND delete: '.$property_id.' <-'); 1469 // $mlsimport->admin->theme_importer->mlsimport_saas_delete_property_via_mysql( $property_id, $listingkey ); 1470 }else{ 1471 print wp_kses_post('</br>' .$listingkey. ' ------------------------- NOT FOUND BUT MARKED AS KEEP: '.$property_id.' / '.$delete_status.'<-'); 1472 } 1473 1476 1474 } 1477 1475 } … … 1490 1488 1491 1489 $arguments = array(); 1492 $answer = $this->theme_importer->global _api_request_CURL_saas( 'reconciliation', $arguments, 'GET' );1490 $answer = $this->theme_importer->globalApiRequestCurlSaas( 'reconciliation', $arguments, 'GET' ); 1493 1491 return $answer; 1494 1492 } … … 1560 1558 } 1561 1559 1562 $answer = $this->theme_importer->global _api_request_CURL_saas( 'listings', $arguments, 'POST' );1560 $answer = $this->theme_importer->globalApiRequestCurlSaas( 'listings', $arguments, 'POST' ); 1563 1561 $answer['potential_leght'] = $potential_leght; 1564 1562 return ( $answer ); … … 1702 1700 * 1703 1701 * add list items to parameters array 1704 * $values= $this->mls_import_return_multiple_param_value('StandardStatus',$item_id,'status',$values); 1705 1702 * 1706 1703 */ 1707 1704 … … 1822 1819 1823 1820 $standardstatus_array = array(); 1821 $standardstatus_delete_array=array(); 1824 1822 if ( isset( $metadata_api_call['PropertyEnums']['StandardStatus'] ) && is_array( $metadata_api_call['PropertyEnums']['StandardStatus'] ) ) { 1825 $standardstatus_array = array_keys( $metadata_api_call['PropertyEnums']['StandardStatus'] ); 1823 $standardstatus_array = array_keys( $metadata_api_call['PropertyEnums']['StandardStatus'] ); 1824 $standardstatus_delete_array= array_keys( $metadata_api_call['PropertyEnums']['StandardStatus'] ); 1826 1825 } 1827 1826 1828 1827 // if we do not have standart status 1829 1828 if ( empty( $standardstatus_array ) ) { 1830 $standardstatus_array = $mlsstatus_array; 1831 } 1829 $standardstatus_array = $mlsstatus_array; 1830 $standardstatus_delete_array = $mlsstatus_array; 1831 1832 } 1833 1834 $permited_status=array('active','active under contract','coming soon','activeundercontract','comingsoon','pending'); 1835 $permited_status_lower = array_map('strtolower', $permited_status); 1836 1837 // Filter out permitted statuses from array1 values 1838 $standardstatus_delete_array = array_filter($standardstatus_delete_array, function ($value) use ($permited_status_lower) { 1839 return !in_array(strtolower($value), $permited_status_lower); 1840 }); 1841 1842 1843 1832 1844 1833 1845 $extracounty_values = ''; … … 1891 1903 'StandardStatus' => array( 1892 1904 'label' => esc_html__( 'Select Status', 'mlsimport' ), 1893 'description' => __( 'The list is auto-populated with MLS available statuses BUT you can import only these statuses: <strong>Active, Active Under Contract or Coming Soon</strong>.', 'mlsimport' ),1905 'description' => __( 'The list is auto-populated with MLS available statuses. To select multiple statuses, use Ctrl (Windows) or Command (Mac).', 'mlsimport' ), 1894 1906 'type' => 'select', 1895 1907 'multiple' => 'yes', 1896 1908 'values' => $standardstatus_array, 1897 1909 ), 1910 'StandardStatusDelete' => array( 1911 'label' => esc_html__( 'Delete Statuses', 'mlsimport' ), 1912 'description' => __( 'If you edit the Delete Statuses after importing listings, the changes will NOT apply to listings that have already been imported.', 'mlsimport' ), 1913 'type' => 'select', 1914 'multiple' => 'yes', 1915 'values' => $standardstatus_delete_array, 1916 ), 1917 1898 1918 'InternetEntireListingDisplayYN' => array( 1899 1919 'label' => esc_html__( 'Internet Entire Listing Display ', 'mlsimport'), … … 2060 2080 mlsimport_saas_single_write_import_custom_logs( $log, 'cron' ); 2061 2081 2062 $api_call_array = $this->theme_importer->global _api_request_CURL_saas( 'listings', $import_arguments, 'POST' );2063 2064 $mlsimport->admin->theme_importer->mlsimport _saas_cron_parse_search_array_per_item( $api_call_array, $input_arg['item_id_array'], $key );2082 $api_call_array = $this->theme_importer->globalApiRequestCurlSaas( 'listings', $import_arguments, 'POST' ); 2083 2084 $mlsimport->admin->theme_importer->mlsimportSaasCronParseSearchArrayPerItem( $api_call_array, $input_arg['item_id_array'], $key ); 2065 2085 } 2066 2086 … … 2121 2141 mlsimport_saas_single_write_import_custom_logs( $log_prefix . wp_json_encode( $input_arg['item_id_array'] ) . PHP_EOL ); 2122 2142 2123 ini_set('memory_limit', '256M');2124 ini_set('max_execution_time', 0); // Unlimited execution time2143 //ini_set('memory_limit', '256M'); 2144 //ini_set('max_execution_time', 0); // Unlimited execution time 2125 2145 2126 2146 // Get from MLS Import the big argument array only once … … 2129 2149 // Retrieve all meta data in one go to reduce database queries 2130 2150 $mlsimport_item_option_data = array( 2131 'mlsimport_item_standardstatus' => get_post_meta( $mlsimportItemId, 'mlsimport_item_standardstatus', true ), 2132 'mlsimport_item_property_user' => get_post_meta( $mlsimportItemId, 'mlsimport_item_property_user', true ), 2133 'mlsimport_item_agent' => get_post_meta( $mlsimportItemId, 'mlsimport_item_agent', true ), 2134 'mlsimport_item_property_status' => get_post_meta( $mlsimportItemId, 'mlsimport_item_property_status', true ), 2151 'mlsimport_item_standardstatus' => get_post_meta( $mlsimportItemId, 'mlsimport_item_standardstatus', true ), 2152 'mlsimport_item_standardstatusdelete' => get_post_meta( $mlsimportItemId, 'mlsimport_item_standardstatusdelete', true ), 2153 'mlsimport_item_property_user' => get_post_meta( $mlsimportItemId, 'mlsimport_item_property_user', true ), 2154 'mlsimport_item_agent' => get_post_meta( $mlsimportItemId, 'mlsimport_item_agent', true ), 2155 'mlsimport_item_property_status' => get_post_meta( $mlsimportItemId, 'mlsimport_item_property_status', true ), 2135 2156 ); 2136 2157 … … 2165 2186 print esc_html($log); 2166 2187 2167 $api_call_array = $this->theme_importer->global_api_request_CURL_saas( 'listings', $import_arguments, 'POST' ); 2168 2169 $mlsimport->admin->theme_importer->mlsimport_saas_parse_search_array_per_item( $api_call_array, $input_arg['item_id_array'], $key, $mlsimport_item_option_data ); 2170 // $this->admin->theme_importer->mlsimport_saas_parse_search_array_per_item($api_call_array, $input_arg['item_id_array'], $key, $mlsimport_item_option_data); 2188 $api_call_array = $this->theme_importer->globalApiRequestCurlSaas( 'listings', $import_arguments, 'POST' ); 2189 2190 $mlsimport->admin->theme_importer->mlsimportSaasParseSearchArrayPerItem( $api_call_array, $input_arg['item_id_array'], $key, $mlsimport_item_option_data ); 2171 2191 } else { 2172 2192 update_post_meta( $mlsimportItemId, 'mlsimport_spawn_status', 'completed' ); … … 2291 2311 $url = 'clients?theme_id=' . intval( $options['mlsimport_theme_used'] ); 2292 2312 2293 $answer = $theme_Start::global _api_request_saas( $url, $values, 'GET' );2313 $answer = $theme_Start::globalApiRequestSaas( $url, $values, 'GET' ); 2294 2314 2295 2315 update_option( 'mlsimport_mls_metadata_populated', 'yes' ); -
mlsimport/tags/5.8.1/admin/partials/mlsimport-administrative-options.php
r3075646 r3134790 27 27 $mlsimport->admin->mlsimport_saas_setting_up(); 28 28 // mlsimport_saas_event_mls_import_auto_function(); 29 //mlsimport_saas_reconciliation_event_function();29 //mlsimport_saas_reconciliation_event_function(); 30 30 ?> 31 31 -
mlsimport/tags/5.8.1/enviroment/EstateClass.php
r3075646 r3134790 53 53 * Deal with extra meta 54 54 */ 55 public function mlsimport _saas_set_extra_meta( $property_id, $property ) {55 public function mlsimportSaasSetExtraMeta( $property_id, $property ) { 56 56 $property_history = ''; 57 57 $extra_meta_log = ''; … … 169 169 * @var string $plugin_name 170 170 */ 171 public function correlation _update_after( $is_insert, $property_id, $global_extra_fields ) {171 public function correlationUpdateAfter( $is_insert, $property_id, $global_extra_fields ) { 172 172 if ( 'yes' === $is_insert ) { 173 173 update_post_meta( $property_id, 'local_pgpr_slider_type', 'global' ); -
mlsimport/tags/5.8.1/enviroment/HouzezClass.php
r3075646 r3134790 59 59 * Deal with extra meta 60 60 */ 61 public function mlsimport _saas_set_extra_meta( $property_id, $property ) {61 public function mlsimportSaasSetExtraMeta( $property_id, $property ) { 62 62 $property_history = ''; 63 63 $extra_meta_log = ''; … … 176 176 * @var string $plugin_name 177 177 */ 178 public function correlation _update_after( $is_insert, $property_id, $global_extra_fields, $new_agent ) {178 public function correlationUpdateAfter( $is_insert, $property_id, $global_extra_fields, $new_agent ) { 179 179 if ( 'yes' === $is_insert ) { 180 180 $options_mls = get_option( 'mlsimport_admin_mls_sync' ); -
mlsimport/tags/5.8.1/enviroment/RealHomesClass.php
r3075646 r3134790 55 55 * Deal with extra meta 56 56 */ 57 public function mlsimport _saas_set_extra_meta( $property_id, $property ) {57 public function mlsimportSaasSetExtraMeta( $property_id, $property ) { 58 58 $property_history = ''; 59 59 $extra_meta_log = ''; … … 164 164 * @var string $plugin_name 165 165 */ 166 public function correlation _update_after( $is_insert, $property_id, $global_extra_fields, $new_agent ) {166 public function correlationUpdateAfter( $is_insert, $property_id, $global_extra_fields, $new_agent ) { 167 167 if ( 'yes' === $is_insert ) { 168 168 update_post_meta( $property_id, 'REAL_HOMES_featured', 0 ); -
mlsimport/tags/5.8.1/enviroment/ResidenceClass.php
r3093628 r3134790 56 56 * Deal with extra meta 57 57 */ 58 public function mlsimport _saas_set_extra_meta( $property_id, $property ) {58 public function mlsimportSaasSetExtraMeta( $property_id, $property ) { 59 59 $property_history = array(); 60 60 $extra_meta_log = array(); … … 174 174 * @var string $plugin_name 175 175 */ 176 public function correlation _update_after( $is_insert, $property_id, $global_extra_fields, $new_agent ) {176 public function correlationUpdateAfter( $is_insert, $property_id, $global_extra_fields, $new_agent ) { 177 177 if ( 'yes' === $is_insert ) { 178 178 update_post_meta( $property_id, 'prop_featured', 0 ); -
mlsimport/tags/5.8.1/includes/ThemeImport.php
r3093628 r3134790 7 7 * Description of ThemeImport 8 8 * 9 * @ author cretu9 * @class ThemeImport 10 10 */ 11 11 class ThemeImport { 12 12 13 // put your code here14 13 15 14 public $theme; … … 17 16 public $enviroment; 18 17 public $encoded_values; 19 /** 20 * class construct 21 * 22 * @since 1.0.0 23 * @access protected 24 * @var string $plugin_name 25 */ 26 27 28 /* 29 * 30 * 31 * Api Request to MLSimport APi 32 * 33 * 34 * 35 * */ 36 37 public function global_api_request_CURL_saas( $method, $values_array, $type = 'GET' ) { 18 19 20 /** 21 * Api Request to MLSimport API using CURL 22 * 23 * @param string $method The API method to call. 24 * @param array $values_array The values to pass to the API. 25 * @param string $type The request type (default is 'GET'). 26 * @return mixed The API response or error message. 27 */ 28 29 public function globalApiRequestCurlSaas($method, $valuesArray, $type = 'GET') { 38 30 global $mlsimport; 39 $url = MLSIMPORT_API_URL . $method; 40 $headers = array( 'Content-Type' => 'text/plain' ); 31 $url = MLSIMPORT_API_URL . $method; 32 $headers = ['Content-Type' => 'text/plain']; 33 34 if ($method !== 'token') { 35 $token = self::getApiToken(); 36 $headers = [ 37 'Content-Type' => 'application/json', 38 'authorizationToken' => $token, 39 ]; 40 } 41 41 42 if ( 'token' !== $method ) { 43 $token = $mlsimport->admin->mlsimport_saas_get_mls_api_token_from_transient(); 44 $headers = array(); 45 $headers['Content-Type'] = 'application/json'; 46 $headers['authorizationToken'] = $token; 47 48 } 49 50 51 $args = array( 52 'method' => 'GET', 53 'headers' => $headers, 54 'body' => wp_json_encode($values_array), 55 'timeout' => 120, 42 $args = [ 43 'method' => $type, 44 'headers' => $headers, 45 'body' => !empty($valuesArray) ? wp_json_encode($valuesArray) : null, 46 'timeout' => 120, 56 47 'redirection' => 10, 57 48 'httpversion' => '1.1', 58 'blocking' => true, 59 'user-agent' => $_SERVER['HTTP_USER_AGENT'] 60 ); 49 'blocking' => true, 50 'user-agent' => $_SERVER['HTTP_USER_AGENT'], 51 ]; 52 53 $response = $type === 'GET' ? wp_remote_get($url, $args) : wp_remote_post($url, $args); 54 61 55 62 56 63 if(empty($values_array)){ 64 unset($args['body']); 65 } 66 67 68 69 // Choose the appropriate WordPress HTTP API function based on the request type 70 if ( 'GET' === $type ) { 71 $response = wp_remote_get( $url, $args ); 57 58 if (is_wp_error($response)) { 59 return $response->get_error_message(); 72 60 } else { 73 $args['method'] = $type; 74 $response = wp_remote_post( $url, $args ); 75 } 61 $body = wp_remote_retrieve_body($response); 62 $toReturn = json_decode($body, true); 63 if (json_last_error() !== JSON_ERROR_NONE) { 64 return 'JSON decode error: ' . json_last_error_msg(); 65 } 66 return $toReturn; 67 } 68 } 69 76 70 77 78 79 // Handle response 80 if ( is_wp_error( $response ) ) { 81 return $response->get_error_message(); 82 } else { 83 $body = wp_remote_retrieve_body( $response ); 84 $to_return = json_decode( $body, true ); 85 86 return $to_return; 87 } 88 } 89 90 91 92 93 94 /* 95 * 96 * 97 * Api Request to MLSimport APi 98 * 99 * 100 * 101 * */ 102 103 104 public static function global_api_request_saas( $method, $values_array, $type = 'GET' ) { 71 /** 72 * Retrieve the API token 73 * 74 * @return string The API token. 75 */ 76 private static function getApiToken() { 77 global $mlsimport; 78 return $mlsimport->admin->mlsimport_saas_get_mls_api_token_from_transient(); 79 } 80 81 82 /** 83 * Api Request to MLSimport API 84 * 85 * @param string $method The API method to call. 86 * @param array $valuesArray The values to pass to the API. 87 * @param string $type The request type (default is 'GET'). 88 * @return array The API response data. 89 */ 90 91 public static function globalApiRequestSaas($method, $valuesArray, $type = 'GET') { 105 92 global $mlsimport; 106 93 $url = MLSIMPORT_API_URL . $method; 107 94 108 $headers = array();109 if ( 'token' !== $method && 'mls' !== $method) {110 $token = $mlsimport->admin->mlsimport_saas_get_mls_api_token_from_transient();111 $headers = array(95 $headers = []; 96 if ($method !== 'token' && $method !== 'mls') { 97 $token = self::getApiToken(); 98 $headers = [ 112 99 'authorizationToken' => $token, 113 'Content-Type' => 'application/json',114 );115 } 116 117 118 $arg uments = array(119 'method' => $type,120 'timeout' => 45,100 'Content-Type' => 'application/json', 101 ]; 102 } 103 104 105 $args = [ 106 'method' => $type, 107 'timeout' => 45, 121 108 'redirection' => 5, 122 109 'httpversion' => '1.0', 123 'blocking' => true, 124 'headers' => $headers, 125 'cookies' => array(), 126 ); 127 128 if ( is_array( $values_array ) && ! empty( $values_array ) ) { 129 $arguments['body'] = wp_json_encode( $values_array ); 130 } 131 132 $response = wp_remote_post( $url, $arguments ); 133 134 135 if ( is_wp_error( $response ) ) { 136 // It is a WordPress error. 137 $error_code = $response->get_error_code(); 138 $error_message = esc_html( $response->get_error_message( $error_code ) ); 139 $warning_message = sprintf( 140 esc_html__( 'You have a WordPress Error. Error code: %s. Error Description: %s', 'mlsimport' ), 141 esc_html( $error_code ), 142 $error_message 143 ); 144 print esc_html( '<div class="mlsimport_warning">' . $warning_message . '</div>' ); 145 146 $received_data['succes'] = false; 147 return $received_data; 148 } 149 150 if ( isset( $response['response']['code'] ) && 200 === $response['response']['code'] ) { 151 $received_data = json_decode( wp_remote_retrieve_body( $response ), true ); 110 'blocking' => true, 111 'headers' => $headers, 112 'cookies' => [], 113 'body' => !empty($valuesArray) ? wp_json_encode($valuesArray) : null, 114 ]; 115 $response = wp_remote_post($url, $args); 116 117 118 if (is_wp_error($response)) { 119 return [ 120 'success' => false, 121 'error_code' => $response->get_error_code(), 122 'error_message' => esc_html($response->get_error_message()) 123 ]; 124 } 125 126 if (isset($response['response']['code']) && $response['response']['code'] === 200) { 127 $receivedData = json_decode(wp_remote_retrieve_body($response), true); 128 return $receivedData; 129 } else { 130 return ['success' => false]; 131 } 132 133 exit(); 134 } 135 136 137 138 139 140 141 142 143 /** 144 * Parse Result Array 145 * 146 * @param array $readyToParseArray The array ready to be parsed. 147 * @param array $itemIdArray The item ID array. 148 * @param string $batchKey The batch key. 149 * @param array $mlsimportItemOptionData The item option data. 150 */ 152 151 153 return $received_data; 154 } else { 155 $received_data['succes'] = false; 156 return $received_data; 157 } 158 exit(); 159 } 160 161 162 163 164 165 166 167 168 /** 169 * 170 * 171 * 172 * Parse Result Array 173 */ 174 public function mlsimport_saas_parse_search_array_per_item( $ready_to_parse_array, $item_id_array, $batch_key, $mlsimport_item_option_data ) { 152 public function mlsimportSaasParseSearchArrayPerItem($readyToParseArray, $itemIdArray, $batchKey, $mlsimportItemOptionData) { 175 153 $logs = ''; 176 154 177 155 wp_cache_flush(); 178 156 gc_collect_cycles(); 179 $counter_prop = 0; 180 if(isset( $ready_to_parse_array['data'])){ 181 foreach ( $ready_to_parse_array['data'] as $key => $property ) { 182 ++$counter_prop; 183 184 $logs = $this->mlsimport_mem_usage() . '=== In parse search array, listing no ' . $key . ' from batch ' . $batch_key . ' with Listingkey: ' . $property['ListingKey'] . PHP_EOL; 185 mlsimport_saas_single_write_import_custom_logs( $logs, 'import' ); 157 $counterProp = 0; 158 159 if (isset($readyToParseArray['data'])) { 160 foreach ($readyToParseArray['data'] as $key => $property) { 161 ++$counterProp; 162 163 $logs = $this->mlsimportMemUsage() . '=== In parse search array, listing no ' . $key . ' from batch ' . $batchKey . ' with ListingKey: ' . $property['ListingKey'] . PHP_EOL; 164 $this->writeImportLogs($logs, 'import'); 165 166 wp_cache_delete('mlsimport_force_stop_' . $itemIdArray['item_id'], 'options'); 167 168 $status = get_option('mlsimport_force_stop_' . $itemIdArray['item_id']); 169 $logs = $this->mlsimportMemUsage() . ' / on Batch ' . $itemIdArray['batch_counter'] . ', Item ID: ' . $itemIdArray['item_id'] . '/' . $counterProp . ' check ListingKey ' . $property['ListingKey'] . ' - stop command issued ? ' . $status . PHP_EOL; 170 $this->writeImportLogs($logs, 'import'); 171 172 if ($status === 'no') { 173 $logs = 'Will proceed to import - Memory Used ' . $this->mlsimportMemUsage() . PHP_EOL; 174 $this->writeImportLogs($logs, 'import'); 175 $this->mlsimportSaasPrepareToImportPerItem($property, $itemIdArray, 'normal', $mlsimportItemOptionData); 176 } else { 177 update_post_meta($itemIdArray['item_id'], 'mlsimport_spawn_status', 'completed'); 178 } 179 unset($logs); 180 } 181 } 182 183 unset($readyToParseArray); 184 unset($logs); 185 } 186 187 188 /** 189 * Write logs for import process 190 * 191 * @param string $logs The log message to write. 192 * @param string $type The type of log. 193 */ 194 private function writeImportLogs($logs, $type) { 195 mlsimport_saas_single_write_import_custom_logs($logs, $type); 196 } 197 198 /** 199 * Get memory usage 200 * 201 * @return string The memory usage in MB. 202 */ 203 public function mlsimportMemUsage() { 204 $memUsage = memory_get_usage(true); 205 $memUsageShow = round($memUsage / 1048576, 2); 206 return $memUsageShow . 'mb '; 207 } 208 209 186 210 187 211 188 wp_cache_delete( 'mlsimport_force_stop_' . $item_id_array['item_id'], 'options' ); 189 190 $status = get_option( 'mlsimport_force_stop_' . $item_id_array['item_id'] ); 191 $logs = $this->mlsimport_mem_usage() . ' / on Batch ' . $item_id_array['batch_counter'] . ', Item ID: ' . $item_id_array['item_id'] . '/' . $counter_prop . ' check ListingKey ' . $property['ListingKey'] . ' - stop command issued ? ' . $status . PHP_EOL; 192 mlsimport_saas_single_write_import_custom_logs( $logs, 'import' ); 193 194 if ( 'no' === $status ) { 195 $logs = 'Will proceed to import - Memory Used ' . $this->mlsimport_mem_usage() . PHP_EOL; 196 mlsimport_saas_single_write_import_custom_logs( $logs, 'import' ); 197 $this->mlsimport_saas_prepare_to_import_per_item( $property, $item_id_array, 'normal', $mlsimport_item_option_data ); 198 } else { 199 update_post_meta( $item_id_array['item_id'], 'mlsimport_spawn_status', 'completed' ); 200 } 201 unset( $logs ); 202 } 203 } 204 205 unset( $ready_to_parse_array ); 206 unset( $logs ); 207 } 208 209 210 /** 211 * 212 * 213 * Parse Result Array in CROn 214 */ 215 public function mlsimport_saas_cron_parse_search_array_per_item( $ready_to_parse_array, $item_id_array, $batch_key ) { 216 217 $mlsimport_item_option_data = array(); 218 $mlsimport_item_option_data['mlsimport_item_standardstatus'] = get_post_meta( $item_id_array['item_id'], 'mlsimport_item_standardstatus', true ); 219 $mlsimport_item_option_data['mlsimport_item_property_user'] = get_post_meta( $item_id_array['item_id'], 'mlsimport_item_property_user', true ); 220 $mlsimport_item_option_data['mlsimport_item_agent'] = get_post_meta( $item_id_array['item_id'], 'mlsimport_item_agent', true ); 221 $mlsimport_item_option_data['mlsimport_item_property_status'] = get_post_meta( $item_id_array['item_id'], 'mlsimport_item_property_status', true ); 222 223 foreach ( $ready_to_parse_array['data'] as $key => $property ) { 224 $logs = 'In CRON parse search array, listing no ' . $key . ' from batch ' . $batch_key . ' with Listingkey: ' . $property['ListingKey'] . PHP_EOL; 225 mlsimport_saas_single_write_import_custom_logs( $logs, 'cron' ); 226 $this->mlsimport_saas_prepare_to_import_per_item( $property, $item_id_array, 'cron', $mlsimport_item_option_data ); 227 } 228 } 229 230 231 232 233 234 235 236 /** 237 * 238 * 239 * check if property already imported 240 */ 241 public function mlsimport_saas_retrive_property_by_id( $key, $post_type = 'estate_property' ) { 242 243 $args = array( 244 'post_type' => $post_type, 212 213 /** 214 * Parse Result Array in CRON 215 * 216 * @param array $readyToParseArray The array ready to be parsed. 217 * @param array $itemIdArray The item ID array. 218 * @param string $batchKey The batch key. 219 */ 220 public function mlsimportSaasCronParseSearchArrayPerItem($readyToParseArray, $itemIdArray, $batchKey) { 221 $mlsimportItemOptionData = [ 222 'mlsimport_item_standardstatus' => get_post_meta($itemIdArray['item_id'], 'mlsimport_item_standardstatus', true), 223 'mlsimport_item_standardstatusdelete' => get_post_meta($itemIdArray['item_id'], 'mlsimport_item_standardstatusdelete', true), 224 'mlsimport_item_property_user' => get_post_meta($itemIdArray['item_id'], 'mlsimport_item_property_user', true), 225 'mlsimport_item_agent' => get_post_meta($itemIdArray['item_id'], 'mlsimport_item_agent', true), 226 'mlsimport_item_property_status' => get_post_meta($itemIdArray['item_id'], 'mlsimport_item_property_status', true), 227 ]; 228 229 foreach ($readyToParseArray['data'] as $key => $property) { 230 $logs = 'In CRON parse search array, listing no ' . $key . ' from batch ' . $batchKey . ' with ListingKey: ' . $property['ListingKey'] . PHP_EOL; 231 $this->writeImportLogs($logs, 'cron'); 232 $this->mlsimportSaasPrepareToImportPerItem($property, $itemIdArray, 'cron', $mlsimportItemOptionData); 233 } 234 } 235 236 237 238 239 240 241 242 243 /** 244 * Check if property already imported 245 * 246 * @param string $key The key to search for. 247 * @param string $postType The post type to search within (default is 'estate_property'). 248 * @return int The post ID if found, or 0 if not found. 249 */ 250 public function mlsimportSaasRetrievePropertyById($key, $postType = 'estate_property') { 251 $args = [ 252 'post_type' => $postType, 245 253 'post_status' => 'any', 246 247 'meta_query' => array( 248 array( 249 'key' => 'ListingKey', 250 'value' => $key, 254 'meta_query' => [ 255 [ 256 'key' => 'ListingKey', 257 'value' => $key, 251 258 'compare' => '=', 252 ), 253 ), 254 'fields' => 'ids', 255 ); 256 257 $prop_selection = new WP_Query( $args ); 258 if ( $prop_selection->have_posts() ) { 259 while ( $prop_selection->have_posts() ) { 260 $prop_selection->the_post(); 261 $the_id = get_the_ID(); 262 wp_reset_postdata(); 263 return $the_id; 264 } 259 ], 260 ], 261 'fields' => 'ids', 262 ]; 263 264 $query = new WP_Query($args); 265 if ($query->have_posts()) { 266 $query->the_post(); 267 $propertyId = get_the_ID(); 268 wp_reset_postdata(); 269 return $propertyId; 265 270 } else { 266 271 wp_reset_postdata(); … … 272 277 273 278 274 275 /** 276 * clear taxonomy 277 * 278 * @since 1.0.0 279 * @access protected 280 * @var string $plugin_name 281 */ 282 public function mlsimport_saas_clear_property_for_taxonomy( $property_id, $taxonomies ) { 283 284 if ( is_array( $taxonomies ) ) : 285 foreach ( $taxonomies as $taxonomy => $term ) : 279 /** 280 * Clear taxonomy 281 * 282 * @param int $propertyId The property ID. 283 * @param array $taxonomies The taxonomies to clear. 284 */ 285 public function mlsimportSaasClearPropertyForTaxonomy($propertyId, $taxonomies) { 286 if (is_array($taxonomies)) { 287 foreach ($taxonomies as $taxonomy => $term) { 286 288 if (is_wp_error($taxonomy)) { 287 error_log('Error with taxonomy: ' . $taxonomy->get_error_message()); 288 continue; // Skip this iteration 289 } 290 291 if ( taxonomy_exists($taxonomy) ) { 292 wp_delete_object_term_relationships($property_id, $taxonomy); 293 } else { 294 error_log("Taxonomy does not exist: {$taxonomy}"); 295 } 296 endforeach; 297 endif; 298 } 299 300 301 302 303 304 305 306 307 /** 308 * return non encoded encoded values 309 * 310 * @since 1.0.0 311 * @access protected 312 * @var string $plugin_name 313 */ 314 public function mlsimport_saas_return_non_encoded_value( $item, $encoded_values ) { 315 316 if ( is_array( $item ) ) { 317 if ( ! empty( $encoded_values ) ) { 318 foreach ( $item as $key => $value ) { 319 if ( isset( $encoded_values [ $value ] ) ) { 320 $item[ $key ] = $encoded_values [ $value ]; 289 error_log('Error with taxonomy: ' . $taxonomy->get_error_message()); 290 continue; // Skip this iteration 291 } 292 293 if (taxonomy_exists($taxonomy)) { 294 wp_delete_object_term_relationships($propertyId, $taxonomy); 295 } else { 296 error_log("Taxonomy does not exist: {$taxonomy}"); 297 } 298 } 299 } 300 } 301 302 303 304 305 306 /** 307 * Set taxonomy for property 308 * 309 * @param string $taxonomy The taxonomy to set. 310 * @param int $propertyId The property ID. 311 * @param mixed $fieldValues The values to set. 312 */ 313 public function mlsimportSaasUpdateTaxonomyForProperty($taxonomy, $propertyId, $fieldValues) { 314 global $wpdb; 315 316 // Convert comma-separated values to array if necessary 317 if (!is_array($fieldValues)) { 318 $fieldValues = strpos($fieldValues, ',') !== false ? explode(',', $fieldValues) : [$fieldValues]; 319 } 320 321 // Trim values and remove empty ones 322 $fieldValues = array_filter(array_map('trim', $fieldValues)); 323 324 // Start a database transaction 325 $wpdb->query('START TRANSACTION'); 326 $taxLog = []; 327 328 foreach (array_chunk($fieldValues, 5) as $chunk) { 329 foreach ($chunk as $value) { 330 if (!empty($value)) { 331 // Check if the term already exists 332 $term = $wpdb->get_row($wpdb->prepare( 333 "SELECT t.*, tt.* FROM $wpdb->terms t 334 INNER JOIN $wpdb->term_taxonomy tt ON t.term_id = tt.term_id 335 WHERE t.name = %s AND tt.taxonomy = %s", 336 $value, $taxonomy 337 )); 338 339 $taxLog[] = json_encode($term); 340 if (is_null($term)) { 341 // Insert the term if it doesn't exist 342 $wpdb->insert($wpdb->terms, [ 343 'name' => $value, 344 'slug' => sanitize_title($value), 345 'term_group' => 0 346 ]); 347 348 $termId = $wpdb->insert_id; 349 350 if ($termId) { 351 // Insert term taxonomy 352 $wpdb->insert($wpdb->term_taxonomy, [ 353 'term_id' => $termId, 354 'taxonomy' => $taxonomy, 355 'description' => '', 356 'parent' => 0, 357 'count' => 0 358 ]); 359 360 $termTaxonomyId = $wpdb->insert_id; 361 } else { 362 $taxLog[] = 'Error inserting term'; 363 continue; 364 } 365 } else { 366 // Term exists, get term_id and term_taxonomy_id 367 $termId = $term->term_id; 368 $termTaxonomyId = $wpdb->get_var($wpdb->prepare( 369 "SELECT term_taxonomy_id FROM $wpdb->term_taxonomy WHERE term_id = %d AND taxonomy = %s", 370 $termId, $taxonomy 371 )); 372 } 373 374 if (!empty($termTaxonomyId)) { 375 // Insert term relationship 376 $wpdb->replace($wpdb->term_relationships, [ 377 'object_id' => $propertyId, 378 'term_taxonomy_id' => $termTaxonomyId 379 ]); 380 // Increment the term count 381 $wpdb->query($wpdb->prepare( 382 "UPDATE $wpdb->term_taxonomy SET count = count + 1 WHERE term_taxonomy_id = %d", 383 $termTaxonomyId 384 )); 385 } else { 386 $taxLog[] = 'Error: term_taxonomy_id is null'; 321 387 } 322 388 } 323 389 } 324 return $item; 325 } elseif ( ! empty( $encoded_values ) && isset( $encoded_values [ $item ] ) ) { 326 return $encoded_values [ $item ]; 327 } else { 328 return $item; 329 } 330 } 331 332 /** 333 * set taxonomy 334 * 335 * @since 1.0.0 336 * @access protected 337 * @var string $plugin_name 338 */ 339 340 function mlsimport_saas_update_taxonomy_for_property_new($taxonomy, $property_id, $field_values) { 341 global $wpdb; 342 343 // Remove filters temporarily to avoid caching issues 344 remove_filter('get_term_metadata', array($wpdb->terms, 'cache_term_counts')); 345 wp_cache_delete('terms', 'cache'); 346 347 if (!is_array($field_values)) { 348 if (strpos($field_values, ',') !== false) { 349 $field_values = explode(',', $field_values); 350 } else { 351 $field_values = array($field_values); 352 } 353 } 354 355 // Use transactions for better performance 356 $wpdb->query('START TRANSACTION'); 357 358 // Process in smaller chunks 359 foreach (array_chunk($field_values, 5) as $chunk) { 360 foreach ($chunk as $value) { 361 if (!empty($value)) { 362 $term = get_term_by('name', $value, $taxonomy); 363 364 if (is_wp_error($term) || empty($term)) { 365 // Term doesn't exist, insert it 366 $term = wp_insert_term($value, $taxonomy); 367 368 if (!is_wp_error($term)) { 369 $term_id = $term['term_id']; 370 $term_taxonomy_id = $term['term_taxonomy_id']; 371 } 372 } else { 373 // Term exists 374 $term_id = $term->term_id; 375 $term_taxonomy_id = $term->term_taxonomy_id; 376 } 377 378 if (!empty($term_id) && !empty($term_taxonomy_id)) { 379 // Insert term relationship 380 $wpdb->insert( 381 $wpdb->term_relationships, 382 array( 383 'object_id' => $property_id, 384 'term_taxonomy_id' => $term_taxonomy_id, 385 'term_order' => 0 386 ), 387 array( 388 '%d', 389 '%d', 390 '%d' 391 ) 392 ); 393 } 394 } 395 } 396 } 397 398 // Commit transaction 399 $wpdb->query('COMMIT'); 400 401 // Clear term cache selectively 402 wp_cache_delete("{$taxonomy}_terms", 'terms'); 403 wp_cache_delete("{$taxonomy}_children", 'terms'); 404 405 // Restore the term metadata filter 406 add_filter('get_term_metadata', array($wpdb->terms, 'cache_term_counts'), 10, 2); 407 } 408 409 function mlsimport_saas_update_taxonomy_for_property($taxonomy, $property_id, $field_values) { 410 global $wpdb; 411 412 // Convert comma-separated values to array if necessary 413 if (!is_array($field_values)) { 414 $field_values = strpos($field_values, ',') !== false ? explode(',', $field_values) : array($field_values); 415 } 416 417 // Trim values and remove empty ones 418 $field_values = array_filter(array_map('trim', $field_values)); 419 420 // Start a database transaction 421 $wpdb->query('START TRANSACTION'); 422 $tax_log = array(); 423 424 foreach (array_chunk($field_values, 5) as $chunk) { 425 foreach ($chunk as $value) { 426 if (!empty($value)) { 427 // Check if the term already exists 428 429 $term = $wpdb->get_row($wpdb->prepare( 430 "SELECT t.*, tt.* FROM $wpdb->terms t 431 INNER JOIN $wpdb->term_taxonomy tt ON t.term_id = tt.term_id 432 WHERE t.name = %s AND tt.taxonomy = %s", 433 $value, $taxonomy 434 )); 435 436 437 $tax_log[] = json_encode($term); 438 if (is_null($term)) { 439 // Insert the term if it doesn't exist 440 441 $wpdb->insert($wpdb->terms, array( 442 'name' => $value, 443 'slug' => sanitize_title($value), 444 'term_group' => 0 445 )); 446 447 $term_id = $wpdb->insert_id; 448 449 if ($term_id) { 450 // Insert term taxonomy 451 $wpdb->insert($wpdb->term_taxonomy, array( 452 'term_id' => $term_id, 453 'taxonomy' => $taxonomy, 454 'description' => '', 455 'parent' => 0, 456 'count' => 0 457 )); 458 459 $term_taxonomy_id = $wpdb->insert_id; 460 } else { 461 $tax_log[] = 'Error inserting term'; 462 continue; 463 } 464 } else { 465 // Term exists, get term_id and term_taxonomy_id 466 $term_id = $term->term_id; 467 $term_taxonomy_id = $wpdb->get_var($wpdb->prepare( 468 "SELECT term_taxonomy_id FROM $wpdb->term_taxonomy WHERE term_id = %d AND taxonomy = %s", 469 $term_id, 470 $taxonomy 471 )); 472 } 473 474 if (!empty($term_taxonomy_id)) { 475 // Insert term relationship 476 $wpdb->replace($wpdb->term_relationships, array( 477 'object_id' => $property_id, 478 'term_taxonomy_id' => $term_taxonomy_id 479 )); 480 // Increment the term count 481 $wpdb->query($wpdb->prepare( 482 "UPDATE $wpdb->term_taxonomy SET count = count + 1 WHERE term_taxonomy_id = %d", 483 $term_taxonomy_id 484 )); 485 } else { 486 $tax_log[] = 'Error: term_taxonomy_id is null'; 487 } 488 } 489 } 490 // Flush the cache to free up memory 491 wp_cache_flush(); 492 // Run garbage collection 493 gc_collect_cycles(); 494 } 495 // Commit the transaction 496 $wpdb->query('COMMIT'); 497 498 // Clear term cache selectively 499 wp_cache_delete("{$taxonomy}_terms", 'terms'); 500 wp_cache_delete("{$taxonomy}_children", 'terms'); 501 502 // Restore the term metadata filter 503 add_filter('get_term_metadata', array($wpdb->terms, 'cache_term_counts'), 10, 2); 504 505 // Log memory usage 506 /*$tip_import='normal'; 507 if (!empty($tax_log)) { 508 $tax_log_str = implode(PHP_EOL, $tax_log); 509 mlsimport_saas_single_write_import_custom_logs($tax_log_str, $tip_import); 510 unset($tax_log_str); 511 }*/ 512 } 513 514 515 516 public function mlsimport_saas_update_taxonomy_for_property2( $taxonomy, $property_id, $field_values ) { 390 // Flush the cache to free up memory 391 wp_cache_flush(); 392 // Run garbage collection 393 gc_collect_cycles(); 394 } 395 // Commit the transaction 396 $wpdb->query('COMMIT'); 397 398 // Clear term cache selectively 399 wp_cache_delete("{$taxonomy}_terms", 'terms'); 400 wp_cache_delete("{$taxonomy}_children", 'terms'); 401 402 // Restore the term metadata filter 403 add_filter('get_term_metadata', [$wpdb->terms, 'cache_term_counts'], 10, 2); 404 405 // Log memory usage 406 // if (!empty($taxLog)) { 407 // $taxLogStr = implode(PHP_EOL, $taxLog); 408 // mlsimport_saas_single_write_import_custom_logs($taxLogStr, 'normal'); 409 // unset($taxLogStr); 410 // } 411 } 412 413 414 415 416 /** 417 * Set Property Title 418 * 419 * @param int $propertyId The property ID. 420 * @param int $mlsImportPostId The MLS import post ID. 421 * @param array $property The property data. 422 * @return string The updated title format. 423 */ 424 public function mlsimportSaasUpdatePropertyTitle($propertyId, $mlsImportPostId, $property) { 425 global $mlsimport; 426 427 $titleFormat = esc_html(get_post_meta($mlsImportPostId, 'mlsimport_item_title_format', true)); 428 429 if ('' === $titleFormat) { 430 $options = get_option('mlsimport_admin_mls_sync'); 431 $titleFormat = $options['title_format']; 432 } 433 434 $titleArray = $this->strBetweenAll($titleFormat, '{', '}'); 435 436 $propertyExtraMetaArrayLowerCase = array_change_key_case($property['extra_meta'], CASE_LOWER); 437 438 foreach ($titleArray as $key => $value) { 439 $replace = ''; 440 switch ($value) { 441 case 'Address': 442 $replace = $property['adr_title'] ?? ''; 443 break; 444 case 'City': 445 $replace = $property['adr_city'] ?? ''; 446 break; 447 case 'CountyOrParish': 448 $replace = $property['adr_county'] ?? ''; 449 break; 450 case 'PropertyType': 451 $replace = $property['adr_type'] ?? ''; 452 break; 453 case 'Bedrooms': 454 $replace = $property['adr_bedrooms'] ?? ''; 455 break; 456 case 'Bathrooms': 457 $replace = $property['adr_bathrooms'] ?? ''; 458 break; 459 case 'ListingKey': 460 $replace = $property['ListingKey']; 461 break; 462 case 'ListingId': 463 $replace = $property['adr_listingid'] ?? ''; 464 break; 465 case 'StateOrProvince': 466 $replace = $property['extra_meta']['StateOrProvince'] ?? ''; 467 break; 468 case 'PostalCode': 469 $replace = $property['meta']['property_zip'] ?? $property['meta']['fave_property_zip'] ?? ''; 470 $replace = is_array($replace) ? strval($replace[0]) : strval($replace); 471 break; 472 case 'StreetNumberNumeric': 473 $replace = $propertyExtraMetaArrayLowerCase['streetnumbernumeric'] ?? ''; 474 break; 475 case 'StreetName': 476 $replace = $propertyExtraMetaArrayLowerCase['streetname'] ?? ''; 477 break; 478 } 479 $titleFormat = str_replace('{' . $value . '}', $replace, $titleFormat); 480 } 481 482 $post = [ 483 'ID' => $propertyId, 484 'post_title' => $titleFormat, 485 'post_name' => $titleFormat, 486 ]; 487 488 wp_update_post($post); 489 490 return $titleFormat; 491 } 492 517 493 518 if ( ! is_array( $field_values ) ) { 519 if ( strpos( $field_values, ',' ) !== false ) { 520 $field_values = explode( ',', $field_values ); 521 } else { 522 $field_values = array( $field_values ); 523 } 524 } 525 526 // Remove empty values and decode values in one pass 527 $processed_field_values = array(); 528 foreach ( $field_values as $value ) { 529 if ( ! empty( $value ) ) { 530 $processed_field_values[] = $value; 531 } 532 } 533 534 // Bulk update terms if array is not empty 535 if ( ! empty( $processed_field_values ) ) { 536 wp_set_object_terms( $property_id, $processed_field_values, $taxonomy, true ); 537 clean_term_cache( $property_id, $taxonomy ); 538 } 539 } 540 541 542 543 544 /** 545 * Set Property Title 546 * 547 * @since 1.0.0 548 * @access protected 549 * @var string $plugin_name 550 */ 551 public function mlsimport_saas_update_property_title( $property_id, $mls_import_post_id, $property ) { 494 495 496 497 498 /** 499 * Prepare meta data for property 500 * 501 * @param array $property The property data. 502 * @return array The property data with prepared meta. 503 */ 504 public function mlsimportSaasPrepareMetaForProperty($property) { 505 if (isset($property['extra_meta']['BathroomsTotalDecimal']) && floatval($property['extra_meta']['BathroomsTotalDecimal']) > 0) { 506 $bathrooms = floatval($property['extra_meta']['BathroomsTotalDecimal']); 507 $property['meta']['property_bathrooms'] = $bathrooms; 508 $property['meta']['fave_property_bathrooms'] = $bathrooms; 509 $property['meta']['REAL_HOMES_property_bathrooms'] = $bathrooms; 510 } 511 return $property; 512 } 513 514 515 516 517 518 /** 519 * Attach media to post 520 * 521 * @param int $propertyId The property ID. 522 * @param array $media The media data. 523 * @param string $isInsert Whether the property is being inserted. 524 * @return string The media history log. 525 */ 526 public function mlsimportSassAttachMediaToPost($propertyId, $media, $isInsert) { 527 $mediaHistory = []; 528 if ($isInsert === 'no') { 529 $mediaHistory[] = 'Media - We have edit - images are not replaced'; 530 return implode('</br>', $mediaHistory); 531 } 552 532 553 533 global $mlsimport; 554 555 $title_format = esc_html( get_post_meta( $mls_import_post_id, 'mlsimport_item_title_format', true ) ); 556 557 if ( '' === $title_format ) { 558 $options = get_option( 'mlsimport_admin_mls_sync' ); 559 $title_format = $options['title_format']; 560 } 561 562 $start = '{'; 563 $end = '}'; 564 $title_array = $this->str_between_all( $title_format, $start, $end ); 565 566 $property_extra_meta_array_lowecase = array_change_key_case( $property['extra_meta'], CASE_LOWER ); 567 568 foreach ( $title_array as $key => $value ) { 569 $replace = ''; 570 if ( 'Address' === $value ) { 571 if ( isset( $property['adr_title'] ) ) { 572 $replace = $property['adr_title']; 534 include_once ABSPATH . 'wp-admin/includes/image.php'; 535 $hasFeatured = false; 536 537 delete_post_meta($propertyId, 'fave_property_images'); 538 delete_post_meta($propertyId, 'REAL_HOMES_property_images'); 539 540 add_filter('intermediate_image_sizes_advanced', [$this, 'wpcUnsetImageSizes']); 541 542 // Sorting media 543 if (isset($media[0]['Order'])) { 544 $order = array_column($media, 'Order'); 545 array_multisort($order, SORT_ASC, $media); 546 } 547 548 if (is_array($media)) { 549 foreach ($media as $image) { 550 if (isset($image['MediaCategory']) && $image['MediaCategory'] !== 'Photo') { 551 continue; 573 552 } 574 $title_format = str_replace( '{Address}', $replace, $title_format ); 575 } elseif ( 'City' === $value ) { 576 if ( isset( $property['adr_city'] ) ) { 577 $replace = $property['adr_city']; 578 } 579 $title_format = str_replace( '{City}', $replace, $title_format ); 580 } elseif ( 'CountyOrParish' === $value ) { 581 if ( isset( $property['adr_county'] ) ) { 582 $replace = $property['adr_county']; 583 } 584 $title_format = str_replace( '{CountyOrParish}', $replace, $title_format ); 585 } elseif ( 'PropertyType' === $value ) { 586 if ( isset( $property['adr_type'] ) ) { 587 $replace = $property['adr_type']; 588 } 589 $title_format = str_replace( '{PropertyType}', $replace, $title_format ); 590 } elseif ( 'Bedrooms' === $value ) { 591 if ( isset( $property['adr_bedrooms'] ) ) { 592 $replace = $property['adr_bedrooms']; 593 } 594 $title_format = str_replace( '{Bedrooms}', $replace, $title_format ); 595 } elseif ( 'Bathrooms' === $value ) { 596 if ( isset( $property['adr_bathrooms'] ) ) { 597 $replace = $property['adr_bathrooms']; 598 } 599 $title_format = str_replace( '{Bathrooms}', $replace, $title_format ); 600 } elseif ( 'ListingKey' === $value ) { 601 $replace = $property['ListingKey']; 602 if ( '' !== $replace ) { 603 $title_format = str_replace( '{ListingKey}', $replace, $title_format ); 604 } 605 } elseif ( 'ListingId' === $value ) { 606 if ( isset( $property['adr_listingid'] ) ) { 607 $replace = $property['adr_listingid']; 608 } 609 if ( '' !== $replace ) { 610 $title_format = str_replace( '{ListingId}', $replace, $title_format ); 611 } 612 } elseif ( 'StateOrProvince' === $value ) { 613 if ( isset( $property['extra_meta']['StateOrProvince'] ) ) { 614 $replace = $property['extra_meta']['StateOrProvince']; 615 } 616 $title_format = str_replace( '{StateOrProvince}', $replace, $title_format ); 617 } elseif ( 'PostalCode' === $value ) { 618 if ( isset( $property['meta']['property_zip'] ) ) { 619 if ( is_array( $property['meta']['property_zip'] ) ) { 620 $replace = strval( $property['meta']['property_zip'][0] ); 621 } else { 622 $replace = strval( $property['meta']['property_zip'] ); 623 } 624 } elseif ( isset( $property['meta']['fave_property_zip'] ) ) { 625 if ( is_array( $property['meta']['fave_property_zip'] ) ) { 626 $replace = strval( $property['meta']['fave_property_zip'][0] ); 627 } else { 628 $replace = strval( $property['meta']['fave_property_zip'] ); 553 554 $file = $image['MediaURL']; 555 556 if (isset($image['MediaURL'])) { 557 $attachment = [ 558 'guid' => $image['MediaURL'], 559 'post_status' => 'inherit', 560 'post_content' => '', 561 'post_parent' => $propertyId, 562 'post_mime_type' => $image['MimeType'] ?? 'image/jpg', 563 'post_title' => $image['MediaKey'] ?? '', 564 ]; 565 566 $attachId = wp_insert_attachment($attachment, $file); 567 568 $mediaHistory[] = 'Media - Added ' . $image['MediaURL'] . ' as attachment ' . $attachId; 569 $mlsimport->admin->env_data->enviroment_image_save($propertyId, $attachId); 570 571 update_post_meta($attachId, 'is_mlsimport', 1); 572 if (!$hasFeatured) { 573 set_post_thumbnail($propertyId, $attachId); 574 $hasFeatured = true; 629 575 } 630 576 } 631 632 $title_format = str_replace( '{PostalCode}', $replace, $title_format ); 633 } elseif ( 'StreetNumberNumeric' === $value ) { 634 if ( isset( $property_extra_meta_array_lowecase['streetnumbernumeric'] ) ) { 635 $replace = $property_extra_meta_array_lowecase['streetnumbernumeric']; 636 } 637 $title_format = str_replace( '{StreetNumberNumeric}', $replace, $title_format ); 638 } elseif ( 'StreetName' === $value ) { 639 if ( isset( $property_extra_meta_array_lowecase['streetname'] ) ) { 640 $replace = $property_extra_meta_array_lowecase['streetname']; 641 } 642 $title_format = str_replace( '{StreetName}', $replace, $title_format ); 643 } 644 } 645 646 $post = array( 647 'ID' => $property_id, 648 'post_title' => $title_format, 649 'post_name' => $title_format, 650 ); 651 652 wp_update_post( $post ); 653 654 unset( $property_extra_meta_array_lowecase ); 655 return $title_format; 656 } 657 658 659 660 661 662 663 664 665 /** 666 * 667 * 668 * 669 * 670 * import property -prepare 671 * 672 * @since 1.0.0 673 * @access protected 674 * @var string $plugin_name 675 */ 676 public function mlsimport_saas_prepare_to_import_per_item( $property, $item_id_array, $tip_import, $mlsimport_item_option_data ) { 677 // check if MLS id is set 577 } 578 } else { 579 $mediaHistory[] = 'Media data is blank - there are no images'; 580 } 581 582 remove_filter('intermediate_image_sizes_advanced', [$this, 'wpcUnsetImageSizes']); 583 584 return implode('</br>', $mediaHistory); 585 } 586 587 /** 588 * Unset image sizes 589 * 590 * @param array $sizes The sizes to unset. 591 * @return array The modified sizes array. 592 */ 593 public function wpcUnsetImageSizes($sizes) { 594 return []; 595 } 596 597 598 599 600 601 602 603 /** 604 * Return user option 605 * 606 * @param int $selected The selected user ID. 607 * @return string The HTML option elements for users. 608 */ 609 public function mlsimportSaasThemeImportSelectUser($selected) { 610 $userOptions = ''; 611 $blogusers = get_users(['blog_id' => 1, 'orderby' => 'nicename']); 612 foreach ($blogusers as $user) { 613 $userOptions .= '<option value="' . esc_attr($user->ID) . '"'; 614 if ($user->ID == $selected) { 615 $userOptions .= ' selected="selected"'; 616 } 617 $userOptions .= '>' . esc_html($user->user_login) . '</option>'; 618 } 619 return $userOptions; 620 } 621 622 623 624 625 626 627 628 /** 629 * Return agent option 630 * 631 * @param int $selected The selected agent ID. 632 * @return string The HTML option elements for agents. 633 */ 634 public function mlsimportSaasThemeImportSelectAgent($selected) { 635 global $mlsimport; 636 $args = [ 637 'post_type' => $mlsimport->admin->env_data->get_agent_post_type(), 638 'post_status' => 'publish', 639 'posts_per_page' => 150, 640 ]; 641 642 $agentSelection = new WP_Query($args); 643 $agentOptions = '<option value=""></option>'; 644 645 while ($agentSelection->have_posts()) { 646 $agentSelection->the_post(); 647 $agentId = get_the_ID(); 648 649 $agentOptions .= '<option value="' . esc_attr($agentId) . '"'; 650 if ($agentId == $selected) { 651 $agentOptions .= ' selected="selected"'; 652 } 653 $agentOptions .= '>' . esc_html(get_the_title()) . '</option>'; 654 } 655 wp_reset_postdata(); 656 657 return $agentOptions; 658 } 659 660 661 662 663 664 665 666 667 /** 668 * Delete property 669 * 670 * @param int $deleteId The ID of the property to delete. 671 * @param string $ListingKey The listing key of the property. 672 */ 673 public function deleteProperty($deleteId, $ListingKey) { 674 if ($deleteId > 0) { 675 $args = [ 676 'numberposts' => -1, 677 'post_type' => 'attachment', 678 'post_parent' => $deleteId, 679 'post_status' => null, 680 'orderby' => 'menu_order', 681 'order' => 'ASC', 682 ]; 683 $postAttachments = get_posts($args); 684 685 foreach ($postAttachments as $attachment) { 686 wp_delete_post($attachment->ID); 687 } 688 689 wp_delete_post($deleteId); 690 $logEntry = 'Property with id ' . $deleteId . ' and ' . $ListingKey . ' was deleted on ' . current_time('Y-m-d\TH:i') . PHP_EOL; 691 $this->writeImportLogs($logEntry, 'delete'); 692 } 693 } 694 695 696 697 698 /** 699 * Return array with title items 700 * 701 * @param string $string The input string. 702 * @param string $start The start delimiter. 703 * @param string $end The end delimiter. 704 * @param bool $includeDelimiters Whether to include the delimiters in the result. 705 * @param int $offset The offset to start searching from. 706 * @return array The array of strings found between the delimiters. 707 */ 708 public function strBetweenAll(string $string, string $start, string $end, bool $includeDelimiters = false, int &$offset = 0): array { 709 $strings = []; 710 $length = strlen($string); 711 712 while ($offset < $length) { 713 $found = $this->strBetween($string, $start, $end, $includeDelimiters, $offset); 714 if ($found === null) { 715 break; 716 } 717 718 $strings[] = $found; 719 $offset += strlen($includeDelimiters ? $found : $start . $found . $end); // move offset to the end of the newfound string 720 } 721 722 return $strings; 723 } 724 725 /** 726 * Find string between delimiters 727 * 728 * @param string $string The input string. 729 * @param string $start The start delimiter. 730 * @param string $end The end delimiter. 731 * @param bool $includeDelimiters Whether to include the delimiters in the result. 732 * @param int $offset The offset to start searching from. 733 * @return string|null The string found between the delimiters, or null if not found. 734 */ 735 public function strBetween(string $string, string $start, string $end, bool $includeDelimiters = false, int &$offset = 0): ?string { 736 if ($string === '' || $start === '' || $end === '') { 737 return null; 738 } 739 740 $startLength = strlen($start); 741 $endLength = strlen($end); 742 743 $startPos = strpos($string, $start, $offset); 744 if ($startPos === false) { 745 return null; 746 } 747 748 $endPos = strpos($string, $end, $startPos + $startLength); 749 if ($endPos === false) { 750 return null; 751 } 752 753 $length = $endPos - $startPos + ($includeDelimiters ? $endLength : -$startLength); 754 if (!$length) { 755 return ''; 756 } 757 758 $offset = $startPos + ($includeDelimiters ? 0 : $startLength); 759 760 return substr($string, $offset, $length); 761 } 762 763 764 765 766 767 /** 768 * Delete property via SQL 769 * 770 * @param int $deleteId The ID of the property to delete. 771 * @param string $ListingKey The listing key of the property. 772 */ 773 public function mlsimportSaasDeletePropertyViaMysql($deleteId, $ListingKey) { 774 $postType = get_post_type($deleteId); 775 776 if (in_array($postType, ['estate_property', 'property'])) { 777 $termObjList = get_the_terms($deleteId, 'property_status'); 778 $deleteIdStatus = join(', ', wp_list_pluck($termObjList, 'name')); 779 780 $ListingKey = get_post_meta($deleteId, 'ListingKey', true); 781 if ('' === $ListingKey) { // manually added listing 782 $logEntry = 'User added listing with id ' . $deleteId . ' (' . $postType . ') (status ' . $deleteIdStatus . ') and ' . $ListingKey . ' NOT DELETED' . PHP_EOL; 783 $this->writeImportLogs($logEntry, 'delete'); 784 return; 785 } 786 787 global $wpdb; 788 $wpdb->query($wpdb->prepare("DELETE FROM $wpdb->postmeta WHERE `post_id` = %d", $deleteId)); 789 $wpdb->query($wpdb->prepare("DELETE FROM $wpdb->posts WHERE `post_parent` = %d OR `ID` = %d", $deleteId, $deleteId)); 790 791 $logEntry = 'MYSQL DELETE -> Property with id ' . $deleteId . ' (' . $postType . ') (status ' . $deleteIdStatus . ') and ' . $ListingKey . ' was deleted on ' . current_time('Y-m-d\TH:i') . PHP_EOL; 792 $this->writeImportLogs($logEntry, 'delete'); 793 } 794 } 795 796 797 798 799 800 801 802 803 /** 804 * Prepare to import per item 805 * 806 * @param array $property The property data. 807 * @param array $itemIdArray The item ID array. 808 * @param string $tipImport The import type. 809 * @param array $mlsimportItemOptionData The item option data. 810 */ 811 public function mlsimportSaasPrepareToImportPerItem($property, $itemIdArray, $tipImport, $mlsimportItemOptionData) { 678 812 set_time_limit(0); 679 813 global $mlsimport; 680 814 681 682 $mls_import_item_status = $mlsimport_item_option_data['mlsimport_item_standardstatus']; 683 $new_author = $mlsimport_item_option_data['mlsimport_item_property_user']; 684 $new_agent = $mlsimport_item_option_data['mlsimport_item_agent']; 685 $property_status = $mlsimport_item_option_data['mlsimport_item_property_status']; 686 687 if ( is_array( $mls_import_item_status ) ) { 688 $mls_import_item_status = array_map( 'strtolower', $mls_import_item_status ); 689 } 690 815 $mlsImportItemStatus = $mlsimportItemOptionData['mlsimport_item_standardstatus']; 816 $mlsImportItemStatusDelete = $mlsimportItemOptionData['mlsimport_item_standardstatusdelete']; 817 $newAuthor = $mlsimportItemOptionData['mlsimport_item_property_user']; 818 $newAgent = $mlsimportItemOptionData['mlsimport_item_agent']; 819 $propertyStatus = $mlsimportItemOptionData['mlsimport_item_property_status']; 820 821 if (is_array($mlsImportItemStatus)) { 822 $mlsImportItemStatus = array_map('strtolower', $mlsImportItemStatus); 823 } 824 825 if (!isset($property['ListingKey'])) { 826 $this->writeImportLogs('ERROR: No Listing Key ' . PHP_EOL, $tipImport); 827 return; 828 } 829 830 ob_start(); 831 832 $ListingKey = $property['ListingKey']; 833 $listingPostType = $mlsimport->admin->env_data->get_property_post_type(); 834 $propertyId = intval($this->mlsimportSaasRetrievePropertyById($ListingKey, $listingPostType)); 835 $status = isset($property['StandardStatus']) ? strtolower($property['StandardStatus']) : strtolower($property['extra_meta']['MlsStatus']); 836 $isInsert = $this->shouldInsertProperty($propertyId, $status, $mlsImportItemStatus, $tipImport); 837 838 $log = $this->mlsimportMemUsage() . '==========' . wp_json_encode($mlsImportItemStatus) . '/' . $newAuthor . '/' . $newAgent . '/' . $propertyStatus . '/ We have property with $ListingKey=' . $ListingKey . ' id=' . $propertyId . ' with status ' . $status . ' is insert? ' . $isInsert . PHP_EOL; 839 $this->writeImportLogs($log, $tipImport); 840 841 $propertyHistory = []; 842 $content = $property['content'] ?? ''; 843 $submitTitle = $ListingKey; 844 845 if ($isInsert === 'yes') { 846 $post = [ 847 'post_title' => $submitTitle, 848 'post_content' => $content, 849 'post_status' => $propertyStatus, 850 'post_type' => $listingPostType, 851 'post_author' => $newAuthor, 852 ]; 853 854 $propertyId = wp_insert_post($post); 855 if (is_wp_error($propertyId)) { 856 $this->writeImportLogs('ERROR: on inserting ' . PHP_EOL, $tipImport); 857 } else { 858 update_post_meta($propertyId, 'ListingKey', $ListingKey); 859 $keep_on_delete='delete'; 860 if(!in_array ($status,$mlsImportItemStatusDelete)){ 861 $keep_on_delete='keep'; 862 update_post_meta($propertyId, 'mlsImportItemStatusDelete', $keep_on_delete); 863 } 864 865 866 867 868 869 $propertyHistory[] = date('F j, Y, g:i a') . ': We Inserted the property with Default title : ' . $submitTitle . ' and received id:' . $propertyId.'. The delete statuses are '.$keep_on_delete; 870 } 871 872 clean_post_cache( $propertyId ); 873 874 } elseif ($propertyId !== 0) { 875 876 $keep_on_delete='delete'; 877 if(!in_array ($status,$mlsImportItemStatusDelete)){ 878 $keep_on_delete='keep'; 879 update_post_meta($propertyId, 'mlsImportItemStatusDelete', $keep_on_delete); 880 } 881 882 $propertyHistory = $this->updateExistingProperty($propertyId,$mlsImportItemStatusDelete, $content, $listingPostType, $newAuthor, $status, $mlsImportItemStatus, $propertyHistory, $tipImport, $ListingKey); 883 } 884 885 if ($propertyId === 0) { 886 $this->writeImportLogs('ERROR property id is 0' . PHP_EOL, $tipImport); 887 return; 888 } 889 890 $newTitle = $this->processPropertyDetails($property, $propertyId, $tipImport, $propertyHistory, $newAgent, $itemIdArray,$isInsert); 891 892 $log = PHP_EOL . 'Ending on Property ' . $propertyId . ', ListingKey: ' . $ListingKey . ' , is insert? ' . $isInsert . ' with new title: ' . $newTitle . ' ' . PHP_EOL; 893 $this->writeImportLogs($log, $tipImport); 894 895 clean_post_cache( $propertyId ); 896 897 ob_end_clean(); 898 } 899 900 901 902 903 /** 904 * Check if the property should be inserted 905 * 906 * @param int $propertyId The property ID. 907 * @param string $status The property status. 908 * @param array $mlsImportItemStatus The MLS import item status. 909 * @param string $tipImport The import type. 910 * @return string 'yes' or 'no' indicating if the property should be inserted. 911 */ 912 private function shouldInsertProperty($propertyId, $status, $mlsImportItemStatus, $tipImport) { 913 if ($propertyId === 0) { 914 return 'yes'; 915 } 916 /*if ($propertyId === 0) { 917 if (in_array($status, ['active', 'active under contract', 'active with contract', 'activewithcontract', 'status', 'activeundercontract', 'comingsoon', 'coming soon', 'pending'])) { 918 if ($tipImport === 'cron' && !in_array($status, $mlsImportItemStatus)) { 919 return 'no'; 920 } 921 return 'yes'; 922 } 923 return 'no'; 924 } 925 return 'no'; 926 */ 927 } 928 929 /** 930 * Update existing property 931 * 932 * @param int $propertyId The property ID. 933 * @param string $content The post content. 934 * @param string $listingPostType The listing post type. 935 * @param int $newAuthor The new author ID. 936 * @param string $status The property status. 937 * @param array $mlsImportItemStatus The MLS import item status. 938 * @param array $propertyHistory The property history. 939 * @param string $tipImport The import type. 940 * @param string $ListingKey The listing key. 941 * @return array Updated property history. 942 */ 943 private function updateExistingProperty($propertyId,$mlsImportItemStatusDelete, $content, $listingPostType, $newAuthor, $status, $mlsImportItemStatus, &$propertyHistory, $tipImport, $ListingKey) { 691 944 692 if ( ! isset( $property['ListingKey'] ) ) { 693 $log = 'ERROR : No Listing Key ' . PHP_EOL; 694 mlsimport_saas_single_write_import_custom_logs( $log, $tip_import ); 695 return; 696 } 697 698 ob_start(); 699 700 $ListingKey = $property['ListingKey']; 701 $listing_post_type = $mlsimport->admin->env_data->get_property_post_type(); 702 $property_id = intval( $this->mlsimport_saas_retrive_property_by_id( $ListingKey, $listing_post_type ) ); 703 $status = 'Not Found'; 704 $property_history = array(); 705 if ( isset( $property['StandardStatus'] ) ) { 706 $status = strtolower( $property['StandardStatus'] ); 945 $mlsImportItemStatusDelete = array_map('strtolower', $mlsImportItemStatusDelete); 946 947 if (in_array($status, $mlsImportItemStatusDelete)) { 948 $log = 'Property with ID ' . $propertyId . ' and with name ' . get_the_title($propertyId) . ' has a status of <strong>' . $status . '</strong> and will be deleted' . PHP_EOL; 949 $this->deleteProperty($propertyId, $ListingKey); 950 $this->writeImportLogs($log, $tipImport); 707 951 } else { 708 $status = strtolower( $property['extra_meta']['MlsStatus'] ); 709 } 710 711 if ( 0 === intval( $property_id) ) { 712 $is_insert = 'no'; 713 if ( 714 'active' === $status || 'active under contract' === $status || 715 'active with contract' === $status || 'activewithcontract' === $status || 716 'status' === $status || 'activeundercontract' === $status || 717 'comingsoon' === $status || 'coming soon' === $status || 718 'pending' === $status 719 ) { 720 $is_insert = 'yes'; 721 if ( 'cron' === $tip_import ) { 722 if ( ! in_array( $status, $mls_import_item_status ) ) { 723 // REJECTED because not selected'; 724 $is_insert = 'no'; 725 } 952 $post = [ 953 'ID' => $propertyId, 954 'post_content' => $content, 955 'post_type' => $listingPostType, 956 'post_author' => $newAuthor, 957 ]; 958 959 $log = 'Property with ID ' . $propertyId . ' and with name ' . get_the_title($propertyId) . ' has a status of <strong>' . $status . '</strong> and will be Edited</br>'; 960 $this->writeImportLogs($log, $tipImport); 961 962 $propertyId = wp_update_post($post); 963 if (is_wp_error($propertyId)) { 964 $this->writeImportLogs('ERROR: on edit ' . PHP_EOL, $tipImport); 965 } else { 966 $submitTitle = get_the_title($propertyId); 967 $propertyHistory[] = gmdate('F j, Y, g:i a') . ': Property with title: ' . $submitTitle . ', id:' . $propertyId . ', ListingKey:' . $ListingKey . ', Status:' . $status . ' will be edited'; 968 } 969 clean_post_cache( $propertyId ); 970 } 971 972 return $propertyHistory; 973 } 974 975 /** 976 * Process property details 977 * 978 * @param array $property The property data. 979 * @param int $propertyId The property ID. 980 * @param string $tipImport The import type. 981 * @param array $propertyHistory The property history. 982 * @param int $newAgent The new agent ID. 983 * @param array $itemIdArray The item ID array. 984 * @param string $isInsert If is a property insert 985 */ 986 private function processPropertyDetails($property, $propertyId, $tipImport, &$propertyHistory, $newAgent, $itemIdArray, $isInsert) { 987 global $mlsimport; 988 $log = PHP_EOL . $this->mlsimportMemUsage() . '====before tax======' . PHP_EOL; 989 $this->writeImportLogs($log, $tipImport); 990 991 if (isset($property['taxonomies']) && is_array($property['taxonomies'])) { 992 remove_filter('get_term_metadata', 'lazyload_term_meta', 10); 993 wp_cache_delete('get_ancestors', 'taxonomy'); 994 995 $this->mlsimportSaasClearPropertyForTaxonomy($propertyId, $property['taxonomies']); 996 997 foreach ($property['taxonomies'] as $taxonomy => $term) { 998 wp_cache_delete("{$taxonomy}_term_counts", 'counts'); 999 $this->mlsimportSaasUpdateTaxonomyForProperty($taxonomy, $propertyId, $term); 1000 $propertyHistory[] = 'Updated Taxonomy ' . $taxonomy . ' with terms ' . wp_json_encode($term); 1001 } 1002 1003 add_filter('get_term_metadata', 'lazyload_term_meta', 10, 2); 1004 delete_option('category_children'); 1005 } 1006 1007 wp_cache_flush(); 1008 1009 $property = $this->mlsimportSaasPrepareMetaForProperty($property); 1010 1011 if (isset($property['meta']) && is_array($property['meta'])) { 1012 foreach ($property['meta'] as $metaName => $metaValue) { 1013 if (is_array($metaValue)) { 1014 $metaValue = implode(',', $metaValue); 726 1015 } 727 } 728 } else { 729 $is_insert = 'no'; 730 } 731 732 $log = $this->mlsimport_mem_usage() . '==========' . wp_json_encode( $mls_import_item_status ) . '/' . $new_author . '/' . $new_agent . '/' . $property_status . '/ We have property with $ListingKey=' . $ListingKey . ' id=' . $property_id . ' with status ' . $status . ' is insert? ' . $is_insert . PHP_EOL; 733 734 mlsimport_saas_single_write_import_custom_logs( $log, $tip_import ); 735 736 // content set and check 737 $content = ''; 738 $submit_title = $ListingKey; 739 if ( isset( $property['content'] ) ) { 740 $content = $property['content']; 741 } 742 743 // $new_author = get_post_meta($item_id_array['item_id'],'mlsimport_item_property_user',true ); 744 // $new_agent = esc_html(get_post_meta($item_id_array['item_id'], 'mlsimport_item_agent', true)); 745 // $property_status = esc_html(get_post_meta($item_id_array['item_id'], 'mlsimport_item_property_status', true)); 746 747 $mlsimport_item_option_data = null; 748 749 if ( 'yes' === $is_insert ) { 750 $post = array( 751 'post_title' => $submit_title, 752 'post_content' => $content, 753 'post_status' => $property_status, 754 'post_type' => $listing_post_type, 755 'post_author' => $new_author, 756 ); 757 758 $property_id = wp_insert_post( $post ); 759 760 if ( is_wp_error( $property_id ) ) { 761 $log = 'ERROR : on inserting ' . PHP_EOL; 762 mlsimport_saas_single_write_import_custom_logs( $log, $tip_import ); 763 } else { 764 update_post_meta( $property_id, 'ListingKey', $ListingKey ); 765 $property_history[] = date( 'F j, Y, g:i a' ) . ': We Inserted the property with Default title : ' . $submit_title . ' and received id:' . $property_id; 766 } 767 768 clean_post_cache( $property_id ); 769 } elseif ( 0 !== $property_id ) { 770 $property_history = array(); 771 $property_history[] = get_post_meta( $property_id, 'mlsimport_property_history', true ); 772 773 $delete_statuses = array( 774 'incomplete' => 'incomplete', 775 'hold' => 'hold', 776 'canceled' => 'canceled', 777 'closed' => 'closed', 778 'delete' => 'delete', 779 'expired' => 'expired', 780 'withdrawn' => 'withdrawn', 781 ); 782 783 if ( ! in_array( 'pending', $mls_import_item_status ) ) { 784 $delete_statuses['pending'] = 'pending'; 785 } 786 787 if ( in_array( $status, $delete_statuses ) ) { 788 $log = 'Property with ID ' . $property_id . ' and with name ' . get_the_title( $property_id ) . ' has a status of <strong>' . $status . '</strong> and will be deleted' . PHP_EOL; 789 $log = '--1---> ' . $log . wp_json_encode( $mls_import_item_status ) . PHP_EOL; 790 if ( in_array( $status, $mls_import_item_status ) ) { 791 $log .= '-2--> canceling the delete ' . PHP_EOL; 792 } else { 793 $log .= '--3--> proceed with the delete ' . PHP_EOL; 794 $this->delete_property( $property_id, $ListingKey ); 795 } 796 797 mlsimport_saas_single_write_import_custom_logs( $log, $tip_import ); 798 unset( $log ); 799 800 return; 801 } else { 802 803 $post = array( 804 'ID' => $property_id, 805 'post_content' => $content, 806 'post_type' => $listing_post_type, 807 'post_author' => $new_author, 808 ); 809 810 $log = ' Property with ID ' . $property_id . ' and with name ' . get_the_title( $property_id ) . ' has a status of <strong>' . $status . '</strong> and will be Edited</br>'; 811 mlsimport_saas_single_write_import_custom_logs( $log, $tip_import ); 812 813 $property_id = wp_update_post( $post ); 814 815 if ( is_wp_error( $property_id ) ) { 816 $log = 'ERROR : on edit ' . PHP_EOL; 817 mlsimport_saas_single_write_import_custom_logs( $log, $tip_import ); 818 } else { 819 $submit_title = get_the_title( $property_id ); 820 $property_history[] = gmdate( 'F j, Y, g:i a' ) . ': Property with title: ' . $submit_title . ', id:' . $property_id . ', ListingKey:' . $ListingKey . ', Status:' . $status . ' will be edited'; 821 } 822 823 clean_post_cache( $property_id ); 824 } 825 } 826 827 // 828 // Insert or edit POST ends her - START ADDING DETAILS 829 // 830 831 $encoded_values = array();// may be obsolote 832 833 if ( intval( $property_id ) === 0 ) { 834 mlsimport_saas_single_write_import_custom_logs( 'ERROR property id is 0' . PHP_EOL, $tip_import ); 835 return; // no point in going forward if no id 836 } 837 838 // Start working on Taxonomies 839 // 840 $log = PHP_EOL.$this->mlsimport_mem_usage() . '====before tax======'. PHP_EOL; 841 mlsimport_saas_single_write_import_custom_logs( $log, $tip_import ); 842 843 $tax_log = array_fill(0, 1, 'Property with ID ' . $property_id . ' NO taxonomies found!'); 844 845 if ( isset( $property['taxonomies'] ) && is_array( $property['taxonomies'] ) ) { 846 847 remove_filter('get_term_metadata', 'lazyload_term_meta', 10); 848 wp_cache_delete('get_ancestors', 'taxonomy'); 849 850 $this->mlsimport_saas_clear_property_for_taxonomy( $property_id, $property['taxonomies'] ); 851 852 853 foreach ( $property['taxonomies'] as $taxonomy => $term ) : 854 wp_cache_delete("{$taxonomy}_term_counts", 'counts'); 855 $this->mlsimport_saas_update_taxonomy_for_property( $taxonomy, $property_id, $term ); 856 $property_history[] = 'Updatedx Taxonomy ' . $taxonomy . ' with terms ' . wp_json_encode( $term ); 857 $tax_log [] = 'Memory:' . $this->mlsimport_mem_usage() . ' Property with ID ' . $property_id . ' Updated Taxonomy ' . $taxonomy . ' with terms ' . wp_json_encode( $term ); 858 859 endforeach; 860 861 862 unset($property['taxonomies'] ); 863 gc_collect_cycles(); 864 add_filter('get_term_metadata', 'lazyload_term_meta', 10, 2); 865 delete_option('category_children'); 866 } 867 868 869 870 // Only log if there are updates 871 if (!empty($tax_log)) { 872 $tax_log_str = implode(PHP_EOL, $tax_log); 873 mlsimport_saas_single_write_import_custom_logs($tax_log_str, $tip_import); 874 unset($tax_log_str); 875 } 876 wp_cache_flush(); 877 878 // Pre Meta jobs 879 // 880 $property = $this->mlsimport_saas_prepare_meta_for_property( $property ); 881 882 // Start working on Meta 883 // 884 885 $meta_log = array(); 886 $meta_log [] = 'Property with ID ' . $property_id . ' NO meta found ! ' . PHP_EOL; 887 888 if ( isset( $property['meta'] ) && is_array( $property['meta'] ) ) { 889 $meta_properties = $property['meta']; 890 foreach ( $meta_properties as $meta_name => $meta_value ) : 891 if ( is_array( $meta_value ) ) { 892 $meta_value = implode( ',', $meta_value ); 893 } 894 update_post_meta( $property_id, $meta_name, $meta_value ); 895 896 $property_history[] = 'Updated Meta ' . $meta_name . ' with meta_value ' . $meta_value; 897 $meta_log [] = 'Memory:' . $this->mlsimport_mem_usage() . 'Property with ID ' . $property_id . ' Updated Meta ' . $meta_name . ' with value ' . $meta_value; 898 endforeach; 899 } 900 $meta_properties = null; 901 $meta_log = implode( PHP_EOL, $meta_log ); 902 mlsimport_saas_single_write_import_custom_logs( $meta_log, $tip_import ); 903 $meta_log = null; 904 905 // Start working on EXTRA Meta 906 // 907 908 $extra_meta_log = 'Property with ID ' . $property_id . ' Start Extra meta ! ' . PHP_EOL; 909 $extra_meta_result = $mlsimport->admin->env_data->mlsimport_saas_set_extra_meta( $property_id, $property ); 910 911 if ( isset( $extra_meta_result['property_history'] ) ) { 912 $property_history = array_merge( $property_history, (array) $extra_meta_result['property_history'] ); 913 } 914 if ( isset( $extra_meta_result['extra_meta_log'] ) ) { 915 $extra_meta_log .= $extra_meta_result['extra_meta_log']; 916 } 917 918 mlsimport_saas_single_write_import_custom_logs( $extra_meta_log, $tip_import ); 919 920 $extra_meta_log = null; 921 $extra_meta_result = null; 922 923 // Start working on Property Media 924 // 925 926 $media = $property['Media']; 927 $media_history = $this->mlsimport_sass_attach_media_to_post( $property_id, $media, $is_insert ); 928 $property_history = array_merge( $property_history, (array) $media_history ); 929 $media = null; 930 $media_history = null; 931 932 // Updateing property title and ending 933 // 934 935 $new_title = $this->mlsimport_saas_update_property_title( $property_id, $item_id_array['item_id'], $property ); 936 $property_history[] = 'Updated title to ' . $new_title . '</br>'; 937 938 // extra fields to be checked 939 $global_extra_fields = array(); 940 $mlsimport->admin->env_data->correlation_update_after( $is_insert, $property_id, $global_extra_fields, $new_agent ); 941 942 // saving history 943 if ( ! empty( $property_history ) ) { 944 $disable_history = intval( get_option( 'mlsimport-disable-history', 1 ) ); 945 if ( 1 === intval( $disable_history ) ) { 946 $property_history[] = '---------------------------------------------------------------</br>'; 947 $property_history = implode( '</br>', $property_history ); 948 update_post_meta( $property_id, 'mlsimport_property_history', $property_history ); 949 } 950 } 951 952 $logs = PHP_EOL . 'Ending on Property ' . $property_id . ', ListingKey: ' . $ListingKey . ' , is insert? ' . $is_insert . ' with new title: ' . $new_title . ' ' . PHP_EOL; 953 mlsimport_saas_single_write_import_custom_logs( $logs, $tip_import ); 954 955 $capture = ob_get_contents(); 956 ob_end_clean(); 957 mlsimport_saas_single_write_import_custom_logs( $capture, $tip_import ); 958 959 $post = null; 960 $capture = null; 961 $property_status = null; 962 $new_agent = null; 963 $new_author = null; 964 $property_history = null; 965 $tax_log = null; 966 $meta_log = null; 967 $extra_meta_log = null; 968 $media_history = null; 969 $logs = null; 970 $capture = null; 971 clean_post_cache( $property_id ); 972 wp_cache_flush(); 973 gc_collect_cycles(); 974 } 975 976 977 978 979 980 981 982 public function mlsimport_mem_usage() { 983 $mem_usage = memory_get_usage( true ); 984 $mem_usage_show = round( $mem_usage / 1048576, 2 ); 985 return $mem_usage_show . 'mb '; 986 } 987 988 989 /** 990 * prepare meta data 991 * 992 * @since 1.0.0 993 * @access protected 994 * @var string $plugin_name 995 */ 996 public function mlsimport_saas_prepare_meta_for_property( $property ) { 997 998 if ( isset( $property['extra_meta']['BathroomsTotalDecimal'] ) && floatval( $property['extra_meta']['BathroomsTotalDecimal'] ) > 0 ) { 999 $property['meta']['property_bathrooms'] = floatval( $property['extra_meta']['BathroomsTotalDecimal'] ); 1000 $property['meta']['fave_property_bathrooms'] = floatval( $property['extra_meta']['BathroomsTotalDecimal'] ); 1001 $property['meta']['REAL_HOMES_property_bathrooms'] = floatval( $property['extra_meta']['BathroomsTotalDecimal'] ); 1002 } 1003 return $property; 1004 } 1005 1006 1007 1008 1009 1010 /** 1011 * attach media to post 1012 * 1013 * @since 1.0.0 1014 * @access protected 1015 * @var string $plugin_name 1016 */ 1017 public function mlsimport_sass_attach_media_to_post( $property_id, $media, $is_insert ) { 1018 1019 1020 $media_history = array(); 1021 if ( 'no' === $is_insert ) { 1022 $media_history[] = ' Media - We have edit - images are not replaced'; 1023 return $media_history; 1024 } 1025 1026 global $mlsimport; 1027 include_once ABSPATH . 'wp-admin/includes/image.php'; 1028 $has_featured = false; 1029 $all_images = array(); 1030 1031 delete_post_meta( $property_id, 'fave_property_images' ); 1032 delete_post_meta( $property_id, 'REAL_HOMES_property_images' ); 1033 1034 add_filter( 'intermediate_image_sizes_advanced', array( $this, 'wpc_unset_imagesizes' ) ); 1035 1036 // sorting media 1037 if ( isset( $media[0]['Order'] ) ) { 1038 $order = array_column( $media, 'Order' ); 1039 array_multisort( $order, SORT_ASC, $media ); 1040 } 1041 1042 if ( is_array( $media ) ) { 1043 foreach ( $media as $key => $image ) : 1044 if ( isset( $image['MediaCategory'] ) && 'Photo' !== $image['MediaCategory'] ) { 1045 continue; 1046 } 1047 1048 $file = $image['MediaURL']; 1049 1050 $media_url = ''; 1051 if ( isset( $image['MediaURL'] ) ) { 1052 $attachment = array( 1053 'guid' => $image['MediaURL'], 1054 'post_status' => 'inherit', 1055 'post_content' => '', 1056 'post_parent' => $property_id, 1057 ); 1058 1059 if ( isset( $image['MimeType'] ) ) { 1060 $attachment['post_mime_type'] = $image['MimeType']; 1061 } else { 1062 $attachment['post_mime_type'] = 'image/jpg'; 1063 } 1064 1065 if ( isset( $image['MediaKey'] ) ) { 1066 $attachment['post_title'] = $image['MediaKey']; 1067 } else { 1068 $attachment['post_title'] = ''; 1069 } 1070 1071 $attach_id = wp_insert_attachment( $attachment, $file ); 1072 1073 $media_history[] = ' Media - Added ' . $image['MediaURL'] . ' as attachement ' . $attach_id; 1074 // wp_generate_attachment_metadata($attach_id,$image['MediaURL']); 1075 $mlsimport->admin->env_data->enviroment_image_save( $property_id, $attach_id ); 1076 1077 update_post_meta( $attach_id, 'is_mlsimport', 1 ); 1078 if ( ! $has_featured ) { 1079 set_post_thumbnail( $property_id, $attach_id ); 1080 $has_featured = true; 1081 } 1082 } 1083 endforeach; 1084 } else { 1085 $media_history[] = ' Media data is blank - there are no images'; 1086 } 1087 remove_filter( 'intermediate_image_sizes_advanced', array( $this, 'wpc_unset_imagesizes' ) ); 1088 1089 $media_history = implode( '</br>', $media_history ); 1090 return $media_history; 1091 } 1092 1093 1094 1095 function wpc_unset_imagesizes( $sizes ) { 1096 $sizes = array(); 1097 } 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 /** 1109 * return user option 1110 * 1111 * @since 1.0.0 1112 * @access protected 1113 * @var string $plugin_name 1114 */ 1115 public function mlsimport_saas_theme_import_select_user( $selected ) { 1116 $blog_list = ''; 1117 $blogusers = get_users( 'blog_id=1&orderby=nicename' ); 1118 foreach ( $blogusers as $user ) { 1119 $the_id = $user->ID; 1120 $blog_list .= '<option value="' . $the_id . '" '; 1121 if ( $the_id === intval($selected) ) { 1122 $blog_list .= ' selected="selected" '; 1123 } 1124 $blog_list .= '>' . $user->user_login . '</option>'; 1125 } 1126 return $blog_list; 1127 } 1128 1129 1130 1131 1132 1133 1134 1135 1136 /** 1137 * return agent option 1138 * 1139 * @since 1.0.0 1140 * @access protected 1141 * @var string $plugin_name 1142 */ 1143 public function mlsimport_saas_theme_import_select_agent( $selected ) { 1144 global $mlsimport; 1145 $args2 = array( 1146 'post_type' => $mlsimport->admin->env_data->get_agent_post_type(), 1147 'post_status' => 'publish', 1148 'posts_per_page' => 150, 1149 ); 1150 1151 if ( method_exists( $mlsimport, 'get_agent_post_type' ) ) { 1152 $args2['post_type'] = $mlsimport->admin->env_data->get_agent_post_type(); 1153 } 1154 1155 $agent_selection2 = new WP_Query( $args2 ); 1156 $agent_list_sec = '<option value=""><option>'; 1157 1158 while ( $agent_selection2->have_posts() ) { 1159 $agent_selection2->the_post(); 1160 $the_id = get_the_ID(); 1161 1162 $agent_list_sec .= '<option value="' . $the_id . '" '; 1163 if ( intval($selected) === $the_id ) { 1164 $agent_list_sec .= ' selected="selected" '; 1165 } 1166 $agent_list_sec .= '>' . get_the_title() . '</option>'; 1167 } 1168 wp_reset_postdata(); 1169 1170 return $agent_list_sec; 1171 } 1172 1173 1174 /** 1175 * delete property 1176 * 1177 * @since 1.0.0 1178 * @access protected 1179 * @var string $plugin_name 1180 */ 1181 public function delete_property( $delete_id, $ListingKey ) { 1182 if ( intval( $delete_id ) > 0 ) { 1183 $arguments = array( 1184 'numberposts' => -1, 1185 'post_type' => 'attachment', 1186 'post_parent' => $delete_id, 1187 'post_status' => null, 1188 'orderby' => 'menu_order', 1189 'order' => 'ASC', 1190 ); 1191 $post_attachments = get_posts( $arguments ); 1192 1193 foreach ( $post_attachments as $attachment ) { 1194 wp_delete_post( $attachment->ID ); 1195 } 1196 1197 wp_delete_post( $delete_id ); 1198 $log_entry = ' Property with id ' . $delete_id . ' and ' . $ListingKey . ' was deleted on ' . current_time( 'Y-m-d\TH:i' ) . PHP_EOL; 1199 mlsimport_saas_single_write_import_custom_logs( $log_entry, 'delete' ); 1200 } 1201 } 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 /** 1214 * return_array with title items 1215 * 1216 * @since 1.0.0 1217 * @access protected 1218 * @var string $plugin_name 1219 */ 1220 public function str_between_all( string $string, string $start, string $end, bool $includeDelimiters = false, int &$offset = 0 ) { 1221 $strings = array(); 1222 $length = strlen( $string ); 1223 1224 while ( $offset < $length ) { 1225 $found = $this->str_between( $string, $start, $end, $includeDelimiters, $offset ); 1226 if ( null === $found ) { 1227 break; 1228 } 1229 1230 $strings[] = $found; 1231 $offset += strlen( $includeDelimiters ? $found : $start . $found . $end ); // move offset to the end of the newfound string 1232 } 1233 1234 return $strings; 1235 } 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 /** 1246 * str_between 1247 * 1248 * @since 1.0.0 1249 * @access protected 1250 * @var string $plugin_name 1251 */ 1252 public function str_between( string $string, string $start, string $end, bool $includeDelimiters = false, int &$offset = 0 ) { 1253 if ('' === $string || '' === $start || '' === $end ) { 1254 return null; 1255 } 1256 1257 $startLength = strlen( $start ); 1258 $endLength = strlen( $end ); 1259 1260 $startPos = strpos( $string, $start, $offset ); 1261 if ( false === $startPos ) { 1262 return null; 1263 } 1264 1265 $endPos = strpos( $string, $end, $startPos + $startLength ); 1266 if ( false === $endPos) { 1267 return null; 1268 } 1269 1270 $length = $endPos - $startPos + ( $includeDelimiters ? $endLength : -$startLength ); 1271 if ( ! $length ) { 1272 return ''; 1273 } 1274 1275 $offset = $startPos + ( $includeDelimiters ? 0 : $startLength ); 1276 1277 $result = substr( $string, $offset, $length ); 1278 1279 return ( false !== $result ? $result : null ); 1280 } 1281 1282 1283 1284 1285 1286 1287 1288 1289 /** 1290 * delete property via sql 1291 * 1292 * @since 1.0.0 1293 * @access protected 1294 * @var string $plugin_name 1295 */ 1296 public function mlsimport_saas_delete_property_via_mysql( $delete_id, $ListingKey ) { 1297 1298 $post_type = get_post_type( $delete_id ); 1299 1300 if ( 'estate_property' === $post_type || 'property' === $post_type ) { 1301 $term_obj_list = get_the_terms( $delete_id, 'property_status' ); 1302 $delete_id_status = join( ', ', wp_list_pluck( $term_obj_list, 'name' ) ); 1303 1304 $ListingKey = get_post_meta( $delete_id, 'ListingKey', true ); 1305 if ( '' === $ListingKey ) { // manual added listing 1306 $log_entry = 'User added listing with id ' . $delete_id . ' (' . $post_type . ') (status ' . $delete_id_status . ') and ' . $ListingKey . ' NOT DELETED' . PHP_EOL; 1307 mlsimport_saas_single_write_import_custom_logs( $log_entry, 'delete' ); 1308 return; 1309 } 1310 1311 global $wpdb; 1312 $wpdb->query( 1313 $wpdb->prepare( 1314 " 1315 DELETE FROM $wpdb->postmeta 1316 WHERE `post_id` = %d", 1317 $delete_id 1318 ) 1319 ); 1320 1321 $wpdb->query( 1322 $wpdb->prepare( 1323 " 1324 DELETE FROM $wpdb->posts 1325 WHERE `post_parent` = %d", 1326 $delete_id 1327 ) 1328 ); 1329 1330 $wpdb->query( 1331 $wpdb->prepare( 1332 " 1333 DELETE FROM $wpdb->posts 1334 WHERE ID = %d", 1335 $delete_id 1336 ) 1337 ); 1338 1339 $log_entry = 'MYSQL DELETE -> Property with id ' . $delete_id . ' (' . $post_type . ') (status ' . $delete_id_status . ') and ' . $ListingKey . ' was deleted on ' . current_time( 'Y-m-d\TH:i' ) . PHP_EOL; 1340 mlsimport_saas_single_write_import_custom_logs( $log_entry, 'delete' ); 1341 } 1342 } 1016 update_post_meta($propertyId, $metaName, $metaValue); 1017 $propertyHistory[] = 'Updated Meta ' . $metaName . ' with meta_value ' . $metaValue; 1018 } 1019 } 1020 1021 $extraMetaResult = $mlsimport->admin->env_data->mlsimportSaasSetExtraMeta($propertyId, $property); 1022 if (isset($extraMetaResult['property_history'])) { 1023 $propertyHistory = array_merge($propertyHistory, (array)$extraMetaResult['property_history']); 1024 } 1025 1026 $mediaHistory = $this->mlsimportSassAttachMediaToPost($propertyId, $property['Media'], $isInsert); 1027 $propertyHistory = array_merge($propertyHistory, (array)$mediaHistory); 1028 1029 $newTitle = $this->mlsimportSaasUpdatePropertyTitle($propertyId, $itemIdArray['item_id'], $property); 1030 $propertyHistory[] = 'Updated title to ' . $newTitle . '</br>'; 1031 1032 $mlsimport->admin->env_data->correlationUpdateAfter($isInsert, $propertyId, [], $newAgent); 1033 1034 if (!empty($propertyHistory)) { 1035 if (intval(get_option('mlsimport-disable-history', 1)) === 1) { 1036 $propertyHistory[] = '---------------------------------------------------------------</br>'; 1037 $propertyHistory = implode('</br>', $propertyHistory); 1038 update_post_meta($propertyId, 'mlsimport_property_history', $propertyHistory); 1039 } 1040 } 1041 1042 return $newTitle; 1043 } 1044 1045 1046 1047 1343 1048 } -
mlsimport/tags/5.8.1/includes/help_functions.php
r3075646 r3134790 110 110 $values = array(); 111 111 112 $answer = $theme_Start::global _api_request_saas( 'mls', $values, 'GET' );112 $answer = $theme_Start::globalApiRequestSaas( 'mls', $values, 'GET' ); 113 113 114 114 if ( isset( $answer['succes'] ) && true === $answer['succes'] ) { -
mlsimport/tags/5.8.1/mlsimport.php
r3093628 r3134790 4 4 * Plugin URI: https://mlsimport.com/ 5 5 * Description: "MLS Import - The MLSImport plugin facilitates the connection to your real estate MLS database, allowing you to download and synchronize real estate property data from the MLS. 6 * Version: 5. 7.46 * Version: 5.8.1 7 7 * Requires at least: 5.2 8 8 * Requires PHP: 7.2 … … 21 21 22 22 23 define( 'MLSIMPORT_VERSION', '5. 7.4' );23 define( 'MLSIMPORT_VERSION', '5.8.1' ); 24 24 define( 'MLSIMPORT_CLUBLINK', 'mlsimport.com' ); 25 25 define( 'MLSIMPORT_CLUBLINKSSL', 'https' ); -
mlsimport/trunk/admin/class-mlsimport-admin.php
r3093628 r3134790 419 419 $valid = array(); 420 420 421 $field_import = array( 'force_rand', 'min_price', 'max_price', 'title_format', 'property_agent', 'property_user', 'City', 'City_check', 'CountyOrParish', 'CountyOrParish_check', 'MlsStatus', 'MlsStatus_check', 'PropertySubType', 'PropertySubType_check', 'PropertyType', 'PropertyType_check', 'StandardStatus', 'StandardStatus_check', 'InternetEntireListingDisplayYN', 'InternetAddressDisplayYN' ); 421 $field_import = array( 'force_rand', 'min_price', 'max_price', 'title_format', 'property_agent', 'property_user', 'City', 'City_check', 'CountyOrParish', 'CountyOrParish_check', 'MlsStatus', 'MlsStatus_check', 'PropertySubType', 'PropertySubType_check', 'PropertyType', 'PropertyType_check', 422 'StandardStatus_delete', 'StandardStatus_delete_check', 'InternetEntireListingDisplayYN', 'InternetAddressDisplayYN' ); 422 423 foreach ( $field_import as $key ) { 423 424 $valid[ $key ] = $input[ $key ]; … … 543 544 global $post; 544 545 $options = get_option( $this->plugin_name . '_admin_fields_select' ); 545 546 echo 'ListingKey: '.get_post_meta( $post->ID, 'ListingKey', true ).'<br>'; 546 547 foreach ( $options['mls-fields-admin'] as $key => $value ) { 547 548 if ( 1 === intval($options['mls-fields-admin'][ $key ] ) ) { … … 810 811 $values['mlsimport_paragon_client_secret'] = $mlsimport_paragon_client_secret; 811 812 812 $answer = $this->theme_importer->global _api_request_saas( 'clients', $values, 'PATCH' );813 $answer = $this->theme_importer->globalApiRequestSaas( 'clients', $values, 'PATCH' ); 813 814 814 815 … … 897 898 898 899 $theme_Start = new ThemeImport(); 899 $answer = $theme_Start::global _api_request_saas( 'token', $values, 'POST' );900 $answer = $theme_Start::globalApiRequestSaas( 'token', $values, 'POST' ); 900 901 901 902 … … 959 960 'mlsimport_item_standardstatus_check', 960 961 'mlsimport_item_standardstatus', 962 'mlsimport_item_standardstatusdelete_check', 963 'mlsimport_item_standardstatusdelete', 964 961 965 'mlsimport_item_internetentirelistingdisplayyn', 962 966 'mlsimport_item_internetaddressdisplayyn', … … 1008 1012 1009 1013 1010 1011 1012 /** 1013 * 1014 * 1015 * Display Meta options 1016 */ 1017 public function mlsimport_saas_display_meta_options( $post ) { 1018 wp_nonce_field( plugin_basename( __FILE__ ), 'estate_agent_noncename' ); 1019 global $post; 1014 /** 1015 * Display Meta Options 1016 * 1017 * @param WP_Post $post The post object. 1018 */ 1019 public function mlsimport_saas_display_meta_options($post) { 1020 wp_nonce_field(plugin_basename(__FILE__), 'estate_agent_noncename'); 1020 1021 global $mlsimport; 1022 1023 $postId = $post->ID; 1021 1024 $token = $mlsimport->admin->mlsimport_saas_get_mls_api_token_from_transient(); 1022 1023 $post_id = $post->ID;1024 1025 $found_items = 'none';1026 $mlsimport_item_how_many = esc_html( get_post_meta( $post_id, 'mlsimport_item_how_many', true ) );1027 $mlsimport_item_stat_cron = esc_html( get_post_meta( $post_id, 'mlsimport_item_stat_cron', true ) );1028 $last_date = get_post_meta( $post_id, 'mlsimport_last_date', true );1029 $status = get_option( 'mlsimport_force_stop_' . $post_id );1030 $field_import = $this->mlsimport_saas_return_mls_fields();1031 1032 // admin options1033 $options = get_option( 'mlsimport_admin_options' );1034 $mlsimport_mls_id = 0;1035 if ( isset( $options['mlsimport_mls_name'] ) && '' !== $options['mlsimport_mls_name'] ) {1036 $mlsimport_mls_id = intval( $options['mlsimport_mls_name'] );1037 }1038 1039 $mlsrequest = $this->mlsimport_make_listing_requests( $post_id );1040 1041 if ( isset( $mlsrequest['success'] ) && ! $mlsrequest['success'] ) { ?>1042 1043 <div class="mlsimport_warning">1044 <?php print esc_html( $mlsrequest['message'] ) ;?>1045 </div>1046 <?php }1047 1048 //print_r($mlsrequest);1049 1025 1050 if (isset($mlsrequest['results'])): ?>1051 <?php $found_items = intval($mlsrequest['results']); ?>1052 <?php else: ?>1053 <?php1054 // delete_transient('mlsimport_saas_token');1055 $mlsimport->admin->mlsimport_saas_check_mls_connection();1056 esc_html_e('Your Token was expired. Please refresh the page to renew it wait while we renew it.','mlsimport');1057 ?>1058 <?php endif; ?>1026 $mlsimportItemHowMany = esc_html(get_post_meta($postId, 'mlsimport_item_how_many', true)); 1027 $mlsimportItemStatCron = esc_html(get_post_meta($postId, 'mlsimport_item_stat_cron', true)); 1028 $lastDate = get_post_meta($postId, 'mlsimport_last_date', true); 1029 $status = get_option('mlsimport_force_stop_' . $postId); 1030 $fieldImport = $this->mlsimport_saas_return_mls_fields(); 1031 $options = get_option('mlsimport_admin_options'); 1032 $mlsimportMlsId = isset($options['mlsimport_mls_name']) && $options['mlsimport_mls_name'] !== '' 1033 ? intval($options['mlsimport_mls_name']) 1034 : 0; 1059 1035 1060 <div class="mlsimport_item_search_url" style="display:xnone;"> <?php echo esc_html__('Last date/time we check :','mlsimport').' '. esc_html($last_date); ?> </div> 1036 $mlsRequest = $this->mlsimport_make_listing_requests($postId); 1037 if (isset($mlsRequest['success']) && !$mlsRequest['success']) { 1038 echo '<div class="mlsimport_warning">' . esc_html($mlsRequest['message']) . '</div>'; 1039 } 1061 1040 1041 $foundItems = isset($mlsRequest['results']) ? intval($mlsRequest['results']) : 'none'; 1042 if ($foundItems === 'none') { 1043 $mlsimport->admin->mlsimport_saas_check_mls_connection(); 1044 esc_html_e('Your Token was expired. Please refresh the page to renew it wait while we renew it.', 'mlsimport'); 1045 } 1046 1047 echo $this->generateMetaOptionsHtml($postId, $foundItems, $lastDate, $mlsimportItemHowMany, $mlsimportItemStatCron, $mlsimportMlsId, $fieldImport); 1048 } 1049 1050 1051 1052 1053 /** 1054 * Generate Meta Options HTML 1055 * 1056 * @param int $postId The post ID. 1057 * @param int $foundItems The number of found items. 1058 * @param string $lastDate The last date checked. 1059 * @param string $mlsimportItemHowMany How many items to import. 1060 * @param string $mlsimportItemStatCron The status of the cron job. 1061 * @param int $mlsimportMlsId The MLS import ID. 1062 * @param array $fieldImport The fields to import. 1063 * @return string The generated HTML. 1064 */ 1065 private function generateMetaOptionsHtml($postId, $foundItems, $lastDate, $mlsimportItemHowMany, $mlsimportItemStatCron, $mlsimportMlsId, $fieldImport) { 1066 ob_start(); 1067 1068 ?> 1069 <div class="mlsimport_item_search_url" style="display:none;"><?php echo esc_html__('Last date/time we check :', 'mlsimport') . ' ' . esc_html($lastDate); ?></div> 1062 1070 <ul> 1063 1071 <li>1. Set the import parameters.</li> … … 1067 1075 </ul> 1068 1076 1069 1070 <?php if (is_numeric($found_items) && $found_items >= 500): ?> 1077 <?php if (is_numeric($foundItems) && $foundItems >= 500): ?> 1071 1078 <div class="mlsimport_notification"> 1072 1079 <?php esc_html_e('You found a large number of listings. While MlsImport import can handle such a large number, you need to make sure that your server can do this operation. This import will take some time. Make sure your server has the capacity, there are no time limits for a long-running process and consider splitting the import between multiple MLS Import items.', 'mlsimport'); ?> … … 1075 1082 1076 1083 <div class="mlsimport_import_no"> 1077 <?php esc_html_e('We found','mlsimport');?> 1078 <strong><?php echo esc_html($found_items); ?></strong> listings. If you decide to import all of them make sure your server database can handle the load. Please do a database backup before initial import.</div> 1084 <?php esc_html_e('We found', 'mlsimport'); ?> 1085 <strong><?php echo esc_html($foundItems); ?></strong> listings. If you decide to import all of them make sure your server database can handle the load. Please do a database backup before initial import. 1086 </div> 1079 1087 1080 1088 <fieldset class="mlsimport-fieldset"> … … 1082 1090 <?php esc_html_e('How Many to import. Use 0 if you want to import all listings found.', 'mlsimport'); ?> 1083 1091 </label> 1084 <input type="text" id="mlsimport_item_how_many" name="mlsimport_item_how_many" value="<?php echo esc_attr($mlsimport _item_how_many); ?>"/>1092 <input type="text" id="mlsimport_item_how_many" name="mlsimport_item_how_many" value="<?php echo esc_attr($mlsimportItemHowMany); ?>"/> 1085 1093 </fieldset> 1086 1094 1087 1095 <fieldset class="mlsimport-fieldset mlsimport_auto_switch"> 1088 <?php esc_html_e('Enable Auto Update every hour?', 'mlsimport'); ?>1089 <label class="mlsimport_switch"> 1096 <?php esc_html_e('Enable Auto Update every hour?', 'mlsimport'); ?> 1097 <label class="mlsimport_switch"> 1090 1098 <input type="hidden" value="0" name="mlsimport_item_stat_cron"> 1091 <input type="checkbox" value="1" name="mlsimport_item_stat_cron"<?php if (intval($mlsimport _item_stat_cron) !== 0) echo esc_html(' checked'); ?>>1099 <input type="checkbox" value="1" name="mlsimport_item_stat_cron"<?php if (intval($mlsimportItemStatCron) !== 0) echo esc_html(' checked'); ?>> 1092 1100 <span class="slider round"></span> 1093 1101 </label> 1094 1102 </fieldset> 1095 1103 1096 1097 1098 1099 1100 1101 1102 <?php 1103 1104 if ('' !== $mlsimport_item_stat_cron): ?> 1104 <?php if ($mlsimportItemStatCron !== ''): ?> 1105 1105 <div id="mlsimport_item_status"></div> 1106 <input class="button mlsimport_button" type="button" id="mlsimport-start_item" 1107 data-post-number="<?php echo intval($found _items); ?>"1108 data-post_id="<?php echo intval($post _id); ?>" value="Start Import">1109 <input class="button mlsimport_button" type="button" id="mlsimport_stop_item" 1110 data-post-number="<?php echo intval($found _items); ?>"1111 data-post_id="<?php echo intval($post _id); ?>" value="Stop Import">1106 <input class="button mlsimport_button" type="button" id="mlsimport-start_item" 1107 data-post-number="<?php echo intval($foundItems); ?>" 1108 data-post_id="<?php echo intval($postId); ?>" value="Start Import"> 1109 <input class="button mlsimport_button" type="button" id="mlsimport_stop_item" 1110 data-post-number="<?php echo intval($foundItems); ?>" 1111 data-post_id="<?php echo intval($postId); ?>" value="Stop Import"> 1112 1112 <?php endif; ?> 1113 1113 1114 <input type="hidden" id="mlsimport_item_actions" value="<?php echo esc_attr($ajax_nonce = wp_create_nonce("mlsimport_item_actions")); ?>" /> 1115 <div class="mlsimport_param_wrapper"><h2>Import Parameters</h2> 1116 1117 <?php $mlsimport_item_title_format = esc_html(get_post_meta($post_id, 'mlsimport_item_title_format', true)); ?> 1118 1119 <fieldset class="mlsimport-fieldset"> 1120 <label class="mlsimport-label" for="mlsimport_item_title_format"> 1121 <?php esc_html_e('Title Format', 'mlsimport'); ?> 1122 </label> 1123 1124 <p class="mlsimport-exp">You can use {Address}, {City}, {CountyOrParish}, {StateOrProvince}, {PostalCode}, {PropertyType}, {Bedrooms}, {Bathrooms}, {ListingKey}, {ListingId},{StreetNumberNumeric} or {StreetName} </p> 1125 <input type="text" id="mlsimport_item_title_format" name="mlsimport_item_title_format" value="<?php echo '' !== $mlsimport_item_title_format ? trim(esc_html($mlsimport_item_title_format)) : esc_html('{Address},{City},{CountyOrParish},{PropertyType}'); ?>"/> 1126 </fieldset> 1127 1128 <?php $mlsimport_item_agent = esc_html(get_post_meta($post_id, 'mlsimport_item_agent', true)); ?> 1129 1130 <fieldset class="mlsimport-fieldset"> 1131 <label class="mlsimport-label" for="mlsimport_item_agent"> 1132 <?php esc_html_e('Select Agent', 'mlsimport'); ?> 1133 </label> 1134 <select class="mlsimport-select" name="mlsimport_item_agent" id="mlsimport_item_agent"> 1135 <?php 1136 $permited_tags= mlsimport_allowed_html_tags_content(); 1137 $select_agent= $this->theme_importer->mlsimport_saas_theme_import_select_agent($mlsimport_item_agent); 1138 print wp_kses($select_agent,$permited_tags); 1139 ?> 1140 </select> 1141 </fieldset> 1142 1143 1144 <?php 1145 $mlsimport_item_property_status = esc_html(get_post_meta($post_id, 'mlsimport_item_property_status', true)); 1146 if ('' === $mlsimport_item_property_status) { 1147 $mlsimport_item_property_status = 'publish'; 1148 } 1149 $status_array = array('publish', 'draft'); 1150 ?> 1151 <fieldset class="mlsimport-fieldset"> 1152 <label class="mlsimport-label" for="mlsimport_item_property_status"> 1153 <?php esc_html_e('Select Property Status on import', 'mlsimport'); ?> 1154 </label> 1155 <select class="mlsimport-select" name="mlsimport_item_property_status" id="mlsimport_item_property_status"> 1156 <?php foreach ($status_array as $value): ?> 1157 <option value="<?php echo esc_attr($value); ?>" <?php if ($value === $mlsimport_item_property_status) echo esc_html('selected'); ?>> 1158 <?php echo esc_html($value); ?> 1159 </option> 1160 <?php endforeach; ?> 1161 </select> 1162 </fieldset> 1163 1164 <?php 1165 $mlsimport_item_property_user = esc_html(get_post_meta($post_id, 'mlsimport_item_property_user', true)); 1166 ?> 1167 <fieldset class="mlsimport-fieldset"> 1168 <label class="mlsimport-label" for="mlsimport_item_property_user"> 1169 <?php esc_html_e('User', 'mlsimport'); ?> 1170 </label> 1171 <select class="mlsimport-select" id="mlsimport_item_property_user" name="mlsimport_item_property_user"> 1172 <?php 1173 $select_user = $this->theme_importer->mlsimport_saas_theme_import_select_user($mlsimport_item_property_user); 1174 print wp_kses($select_user,$permited_tags); 1175 ?> 1176 </select> 1177 </fieldset> 1178 1179 1180 1181 <?php 1182 $mlsimport_item_min_price = floatval(get_post_meta($post_id, 'mlsimport_item_min_price', true)); 1183 $mlsimport_item_max_price = floatval(get_post_meta($post_id, 'mlsimport_item_max_price', true)); 1184 if (0 === intval($mlsimport_item_max_price)) { 1185 $mlsimport_item_max_price = 10000000; 1186 } 1187 ?> 1188 <fieldset class="mlsimport-fieldset"> 1189 <label class="mlsimport-label"> 1190 <?php esc_html_e('Price Between', 'mlsimport'); ?> 1191 </label> 1192 <input type="text" class="mlsimport-select" id="mlsimport_item_min_price" name="mlsimport_item_min_price" value="<?php echo esc_attr($mlsimport_item_min_price); ?>"> and 1193 <input type="text" class="mlsimport-select" id="mlsimport_item_max_price" name="mlsimport_item_max_price" value="<?php echo esc_attr($mlsimport_item_max_price); ?>"> 1194 </fieldset> 1195 1196 1197 <?php 1198 $options = get_option( $this->plugin_name . '_admin_options' ); 1199 1200 $mls_id = ''; 1201 if ( isset( $options['mlsimport_mls_name'] ) ) { 1202 $mls_id = sanitize_text_field( trim( $options['mlsimport_mls_name'] ) ); 1203 } 1204 1205 if ( $mls_id > 5000 ) { 1206 $field_import['PropertyType']['multiple'] = 'no'; 1207 } 1208 1209 foreach ($field_import as $key => $field): 1210 $name_check = strtolower('mlsimport_item_' . $key . '_check'); 1211 $name = strtolower('mlsimport_item_' . $key); 1212 1213 $value = get_post_meta($post_id, $name, true); 1214 $value_check = get_post_meta($post_id, $name_check, true); 1215 $extra_class = ''; 1216 if ('extraCity' === $key || 'extraCounty' === $key) { 1217 $extra_class = ' mlsimport_hidden_field_button'; 1114 <input type="hidden" id="mlsimport_item_actions" value="<?php echo esc_attr(wp_create_nonce("mlsimport_item_actions")); ?>"/> 1115 <div class="mlsimport_param_wrapper"><h2><?php esc_html_e('Import Parameters', 'mlsimport'); ?></h2> 1116 1117 <?php 1118 $mlsimportItemTitleFormat = esc_html(get_post_meta($postId, 'mlsimport_item_title_format', true)); 1119 ?> 1120 1121 <fieldset class="mlsimport-fieldset"> 1122 <label class="mlsimport-label" for="mlsimport_item_title_format"> 1123 <?php esc_html_e('Title Format', 'mlsimport'); ?> 1124 </label> 1125 1126 <p class="mlsimport-exp"><?php esc_html_e('You can use {Address}, {City}, {CountyOrParish}, {StateOrProvince}, {PostalCode}, {PropertyType}, {Bedrooms}, {Bathrooms}, {ListingKey}, {ListingId},{StreetNumberNumeric} or {StreetName}', 'mlsimport'); ?></p> 1127 <input type="text" id="mlsimport_item_title_format" name="mlsimport_item_title_format" value="<?php echo '' !== $mlsimportItemTitleFormat ? trim(esc_html($mlsimportItemTitleFormat)) : esc_html('{Address},{City},{CountyOrParish},{PropertyType}'); ?>"/> 1128 </fieldset> 1129 1130 <?php 1131 $mlsimportItemAgent = esc_html(get_post_meta($postId, 'mlsimport_item_agent', true)); 1132 ?> 1133 1134 <fieldset class="mlsimport-fieldset"> 1135 <label class="mlsimport-label" for="mlsimport_item_agent"> 1136 <?php esc_html_e('Select Agent', 'mlsimport'); ?> 1137 </label> 1138 <select class="mlsimport-select" name="mlsimport_item_agent" id="mlsimport_item_agent"> 1139 <?php 1140 $permitedTags = mlsimport_allowed_html_tags_content(); 1141 $selectAgent =$this->theme_importer->mlsimportSaasThemeImportSelectAgent($mlsimportItemAgent); 1142 print wp_kses($selectAgent, $permitedTags); 1143 ?> 1144 </select> 1145 </fieldset> 1146 1147 <?php 1148 $mlsimportItemPropertyStatus = esc_html(get_post_meta($postId, 'mlsimport_item_property_status', true)); 1149 if ('' === $mlsimportItemPropertyStatus) { 1150 $mlsimportItemPropertyStatus = 'publish'; 1151 } 1152 $statusArray = array('publish', 'draft'); 1153 ?> 1154 <fieldset class="mlsimport-fieldset"> 1155 <label class="mlsimport-label" for="mlsimport_item_property_status"> 1156 <?php esc_html_e('Select Property Status on import', 'mlsimport'); ?> 1157 </label> 1158 <select class="mlsimport-select" name="mlsimport_item_property_status" id="mlsimport_item_property_status"> 1159 <?php foreach ($statusArray as $value): ?> 1160 <option value="<?php echo esc_attr($value); ?>" <?php if ($value === $mlsimportItemPropertyStatus) echo esc_html('selected'); ?>> 1161 <?php echo esc_html($value); ?> 1162 </option> 1163 <?php endforeach; ?> 1164 </select> 1165 </fieldset> 1166 1167 <?php 1168 $mlsimportItemPropertyUser = esc_html(get_post_meta($postId, 'mlsimport_item_property_user', true)); 1169 ?> 1170 <fieldset class="mlsimport-fieldset"> 1171 <label class="mlsimport-label" for="mlsimport_item_property_user"> 1172 <?php esc_html_e('User', 'mlsimport'); ?> 1173 </label> 1174 <select class="mlsimport-select" id="mlsimport_item_property_user" name="mlsimport_item_property_user"> 1175 <?php 1176 $selectUser = $this->theme_importer->mlsimportSaasThemeImportSelectUser($mlsimportItemPropertyUser); 1177 print wp_kses($selectUser, $permitedTags); 1178 ?> 1179 </select> 1180 </fieldset> 1181 1182 <?php 1183 $mlsimportItemMinPrice = floatval(get_post_meta($postId, 'mlsimport_item_min_price', true)); 1184 $mlsimportItemMaxPrice = floatval(get_post_meta($postId, 'mlsimport_item_max_price', true)); 1185 if (0 === intval($mlsimportItemMaxPrice)) { 1186 $mlsimportItemMaxPrice = 10000000; 1218 1187 } 1219 1188 ?> 1189 <fieldset class="mlsimport-fieldset"> 1190 <label class="mlsimport-label"> 1191 <?php esc_html_e('Price Between', 'mlsimport'); ?> 1192 </label> 1193 <input type="text" class="mlsimport-select" id="mlsimport_item_min_price" name="mlsimport_item_min_price" value="<?php echo esc_attr($mlsimportItemMinPrice); ?>"> and 1194 <input type="text" class="mlsimport-select" id="mlsimport_item_max_price" name="mlsimport_item_max_price" value="<?php echo esc_attr($mlsimportItemMaxPrice); ?>"> 1195 </fieldset> 1196 1197 <?php 1198 $options = get_option($this->plugin_name . '_admin_options'); 1199 1200 $mlsId = ''; 1201 if (isset($options['mlsimport_mls_name'])) { 1202 $mlsId = sanitize_text_field(trim($options['mlsimport_mls_name'])); 1203 } 1204 1205 if ($mlsId > 5000) { 1206 $fieldImport['PropertyType']['multiple'] = 'no'; 1207 } 1208 1209 foreach ($fieldImport as $key => $field): 1210 $nameCheck = strtolower('mlsimport_item_' . $key . '_check'); 1211 $name = strtolower('mlsimport_item_' . $key); 1212 1213 $value = get_post_meta($postId, $name, true); 1214 $valueCheck = get_post_meta($postId, $nameCheck, true); 1215 $extraClass = ''; 1216 if ('extraCity' === $key || 'extraCounty' === $key) { 1217 $extraClass = ' mlsimport_hidden_field_button'; 1218 } 1219 ?> 1220 1220 <fieldset class="mlsimport-fieldset"> 1221 <label class="mlsimport-label <?php echo esc_attr($extra _class); ?>" for="<?php echo esc_attr($name); ?>">1221 <label class="mlsimport-label <?php echo esc_attr($extraClass); ?>" for="<?php echo esc_attr($name); ?>"> 1222 1222 <?php echo esc_html($field['label']); ?> 1223 1223 </label> 1224 1224 <?php if ('extraCity' === $key || 'extraCounty' === $key): ?> 1225 <div class="mlsimport-input-wrapper" style="display:none"> 1226 <?php endif; ?> 1227 <p class="mlsimport-exp"><?php echo wp_kses_post($this->mlsimport_notes_for_mls($mlsimport_mls_id, $name, $field['description'])); ?> 1228 <?php 1229 $is_checkbox_admin = 0; 1230 if (1 === intval($value_check)) { 1231 $is_checkbox_admin = 1; 1232 } 1233 1234 $select_all_none = [ 1235 'InternetAddressDisplayYN', 1236 'InternetEntireListingDisplayYN', 1237 'PostalCode', 1238 'ListAgentKey', 1239 'ListAgentMlsId', 1240 'ListOfficeKey', 1241 'ListOfficeMlsId', 1242 'StandardStatus', 1243 'ListingId', 1244 'extraCity', 1245 'extraCounty', 1246 'Exclude_ListOfficeKey', 1247 'Exclude_ListOfficeMlsId', 1248 'Exclude_ListAgentKey', 1249 'Exclude_ListAgentMlsId', 1250 ]; 1251 1252 if ($mls_id > 5000) { 1253 $select_all_none[] = 'PropertyType'; 1254 } 1255 1256 if (!in_array($key, $select_all_none)): ?> 1257 <?php 1258 esc_html_e('- Or Select All ', 'mlsimport'); 1259 print esc_html( $is_checkbox_admin); 1260 ?> 1261 <input type="hidden" name="<?php echo esc_attr($name_check); ?>" value="0" /> 1262 <input type="checkbox" name="<?php echo esc_attr($name_check); ?>" value="1" <?php print esc_attr( checked($is_checkbox_admin, 1, 0)); ?> /> 1263 1264 1265 <?php endif; ?> 1266 </p> 1267 1225 <div class="mlsimport-input-wrapper" style="display:none"> 1226 <?php endif; ?> 1227 <p class="mlsimport-exp"><?php echo wp_kses_post($this->mlsimport_notes_for_mls($mlsimportMlsId, $name, $field['description'])); ?> 1228 <?php 1229 $isCheckboxAdmin = 0; 1230 if (1 === intval($valueCheck)) { 1231 $isCheckboxAdmin = 1; 1232 } 1233 1234 $selectAllNone = [ 1235 'InternetAddressDisplayYN', 1236 'InternetEntireListingDisplayYN', 1237 'PostalCode', 1238 'ListAgentKey', 1239 'ListAgentMlsId', 1240 'ListOfficeKey', 1241 'ListOfficeMlsId', 1242 'StandardStatus', 1243 'StandardStatusDelete', 1244 'ListingId', 1245 'extraCity', 1246 'extraCounty', 1247 'Exclude_ListOfficeKey', 1248 'Exclude_ListOfficeMlsId', 1249 'Exclude_ListAgentKey', 1250 'Exclude_ListAgentMlsId', 1251 ]; 1252 1253 if ($mlsId > 5000) { 1254 $selectAllNone[] = 'PropertyType'; 1255 } 1256 1257 if (!in_array($key, $selectAllNone)): ?> 1258 <?php 1259 esc_html_e('- Or Select All ', 'mlsimport'); 1260 1261 ?> 1262 <input type="hidden" name="<?php echo esc_attr($nameCheck); ?>" value="0"/> 1263 <input type="checkbox" name="<?php echo esc_attr($nameCheck); ?>" value="1" <?php print esc_attr(checked($isCheckboxAdmin, 1, 0)); ?>/> 1264 <?php endif; ?> 1265 </p> 1266 1267 <?php 1268 $permittedStatus = ['active', 'active under contract', 'coming soon', 'activeundercontract', 'comingsoon', 'pending']; 1269 1270 if ($field['type'] === 'select'): ?> 1271 <?php 1272 $multiple = ''; 1273 if ('yes' === $field['multiple']) { 1274 $multiple = 'multiple'; 1275 $name .= '[]'; 1276 } 1277 1278 if ('StandardStatus' === $key && '' === $value) { 1279 $value = ['Active']; 1280 } 1268 1281 1269 1282 1270 <?php 1271 $permited_status=array('active','active under contract','coming soon','activeundercontract','comingsoon','pending'); 1272 1273 1274 1275 if ($field['type'] === 'select'): ?> 1276 <?php 1277 $multiple = ''; 1278 if ('yes' === $field['multiple']) { 1279 $multiple = 'multiple'; 1280 $name .= '[]'; 1281 } 1282 1283 if ('StandardStatus' === $key && '' === $value) { 1284 $value = ['Active']; 1285 } 1286 //edmonton ray 1287 1288 if( $key=="StandardStatus" && $mls_id==111){ 1289 $value==''; 1290 $field['values'] =array(); 1291 1292 } 1293 1294 1295 // Additional conditions can be placed here. 1296 ?> 1297 <select class="mlsimport-select" id="<?php echo esc_attr($name); ?>" name="<?php echo esc_attr($name); ?>" <?php echo esc_attr($multiple); ?>> 1298 <?php foreach ($field['values'] as $select_key): ?> 1299 1300 <?php if ('' !== $select_key): ?> 1301 <option value="<?php echo esc_attr($select_key); ?>" 1302 <?php 1303 if( $key=="StandardStatus" && !in_array(strtolower($select_key), $permited_status) ){ 1304 print 'disabled'; 1305 } 1306 1307 ?> 1308 <?php if (is_array($value) ? in_array($select_key, $value) : $select_key === $value) echo 'selected'; ?>> 1309 1310 <?php echo esc_html($select_key); ?> 1311 </option> 1312 <?php endif; ?> 1313 1314 <?php endforeach; ?> 1315 </select> 1316 <?php elseif ($field['type'] === 'input'): ?> 1317 <input type="text" class="mlsimport-select" id="<?php echo esc_attr($name); ?>" name="<?php echo esc_attr($name); ?>" value="<?php echo esc_attr($value); ?>"> 1318 <?php endif; ?> 1319 <?php if ('extraCity' === $key || 'extraCounty' === $key): ?> 1320 </div> 1321 <?php endif; ?> 1283 1284 // Additional conditions can be placed here. 1285 ?> 1286 <select class="mlsimport-select" id="<?php echo esc_attr($name); ?>" name="<?php echo esc_attr($name); ?>" <?php echo esc_attr($multiple); ?>> 1287 <?php foreach ($field['values'] as $selectKey): ?> 1288 1289 <?php if ('' !== $selectKey): ?> 1290 <option value="<?php echo esc_attr($selectKey); ?>" 1291 <?php 1292 if ($key === "StandardStatusDelete" && $value==null ) { 1293 1294 print 'selected'; 1295 } 1296 ?> 1297 <?php if (is_array($value) ? in_array($selectKey, $value) : $selectKey === $value) echo 'selected'; ?>> 1298 <?php echo esc_html($selectKey); ?> 1299 </option> 1300 <?php endif; ?> 1301 1302 <?php endforeach; ?> 1303 </select> 1304 1305 <?php elseif ($field['type'] === 'input'): ?> 1306 <input type="text" class="mlsimport-select" id="<?php echo esc_attr($name); ?>" name="<?php echo esc_attr($name); ?>" value="<?php echo esc_attr($value); ?>"> 1307 <?php endif; ?> 1308 <?php if ('extraCity' === $key || 'extraCounty' === $key): ?> 1309 </div> 1310 <?php endif; ?> 1322 1311 </fieldset> 1323 <?php endforeach; ?> 1324 1325 1326 </div> 1327 1328 <?php 1329 } 1330 1312 <?php endforeach; ?> 1313 1314 </div> 1315 <?php 1316 return ob_get_clean(); 1317 } 1318 1319 1320 1331 1321 1332 1322 … … 1447 1437 public function mlsimport_saas_start_doing_reconciliation() { 1448 1438 global $mlsimport; 1439 print 'start'; 1449 1440 $listingKey_in_Local = $this->mlsimport_saas_get_all_meta_values( 'ListingKey' ); 1450 1441 … … 1469 1460 1470 1461 if ( in_array( $listingkey, $listingKey_in_MLS ) ) { 1471 print wp_kses_post( $listingkey . ' IS FOUND </br>');1462 print wp_kses_post('</br>'.$listingkey . ' IS FOUND'); 1472 1463 } else { 1473 ++$to_delete; 1474 print wp_kses_post('</br>' .$listingkey. ' ------------------------- NOT FOUND delete: '.$property_id.' <-'); 1475 $mlsimport->admin->theme_importer->mlsimport_saas_delete_property_via_mysql( $property_id, $listingkey ); 1464 $delete_status= get_post_meta( $property_id, 'mlsImportItemStatusDelete', true ); 1465 1466 if($delete_status=='' || $delete_status=='delete'){ 1467 ++$to_delete; 1468 print wp_kses_post('</br>' .$listingkey. ' ------------------------- NOT FOUND delete: '.$property_id.' <-'); 1469 // $mlsimport->admin->theme_importer->mlsimport_saas_delete_property_via_mysql( $property_id, $listingkey ); 1470 }else{ 1471 print wp_kses_post('</br>' .$listingkey. ' ------------------------- NOT FOUND BUT MARKED AS KEEP: '.$property_id.' / '.$delete_status.'<-'); 1472 } 1473 1476 1474 } 1477 1475 } … … 1490 1488 1491 1489 $arguments = array(); 1492 $answer = $this->theme_importer->global _api_request_CURL_saas( 'reconciliation', $arguments, 'GET' );1490 $answer = $this->theme_importer->globalApiRequestCurlSaas( 'reconciliation', $arguments, 'GET' ); 1493 1491 return $answer; 1494 1492 } … … 1560 1558 } 1561 1559 1562 $answer = $this->theme_importer->global _api_request_CURL_saas( 'listings', $arguments, 'POST' );1560 $answer = $this->theme_importer->globalApiRequestCurlSaas( 'listings', $arguments, 'POST' ); 1563 1561 $answer['potential_leght'] = $potential_leght; 1564 1562 return ( $answer ); … … 1702 1700 * 1703 1701 * add list items to parameters array 1704 * $values= $this->mls_import_return_multiple_param_value('StandardStatus',$item_id,'status',$values); 1705 1702 * 1706 1703 */ 1707 1704 … … 1822 1819 1823 1820 $standardstatus_array = array(); 1821 $standardstatus_delete_array=array(); 1824 1822 if ( isset( $metadata_api_call['PropertyEnums']['StandardStatus'] ) && is_array( $metadata_api_call['PropertyEnums']['StandardStatus'] ) ) { 1825 $standardstatus_array = array_keys( $metadata_api_call['PropertyEnums']['StandardStatus'] ); 1823 $standardstatus_array = array_keys( $metadata_api_call['PropertyEnums']['StandardStatus'] ); 1824 $standardstatus_delete_array= array_keys( $metadata_api_call['PropertyEnums']['StandardStatus'] ); 1826 1825 } 1827 1826 1828 1827 // if we do not have standart status 1829 1828 if ( empty( $standardstatus_array ) ) { 1830 $standardstatus_array = $mlsstatus_array; 1831 } 1829 $standardstatus_array = $mlsstatus_array; 1830 $standardstatus_delete_array = $mlsstatus_array; 1831 1832 } 1833 1834 $permited_status=array('active','active under contract','coming soon','activeundercontract','comingsoon','pending'); 1835 $permited_status_lower = array_map('strtolower', $permited_status); 1836 1837 // Filter out permitted statuses from array1 values 1838 $standardstatus_delete_array = array_filter($standardstatus_delete_array, function ($value) use ($permited_status_lower) { 1839 return !in_array(strtolower($value), $permited_status_lower); 1840 }); 1841 1842 1843 1832 1844 1833 1845 $extracounty_values = ''; … … 1891 1903 'StandardStatus' => array( 1892 1904 'label' => esc_html__( 'Select Status', 'mlsimport' ), 1893 'description' => __( 'The list is auto-populated with MLS available statuses BUT you can import only these statuses: <strong>Active, Active Under Contract or Coming Soon</strong>.', 'mlsimport' ),1905 'description' => __( 'The list is auto-populated with MLS available statuses. To select multiple statuses, use Ctrl (Windows) or Command (Mac).', 'mlsimport' ), 1894 1906 'type' => 'select', 1895 1907 'multiple' => 'yes', 1896 1908 'values' => $standardstatus_array, 1897 1909 ), 1910 'StandardStatusDelete' => array( 1911 'label' => esc_html__( 'Delete Statuses', 'mlsimport' ), 1912 'description' => __( 'If you edit the Delete Statuses after importing listings, the changes will NOT apply to listings that have already been imported.', 'mlsimport' ), 1913 'type' => 'select', 1914 'multiple' => 'yes', 1915 'values' => $standardstatus_delete_array, 1916 ), 1917 1898 1918 'InternetEntireListingDisplayYN' => array( 1899 1919 'label' => esc_html__( 'Internet Entire Listing Display ', 'mlsimport'), … … 2060 2080 mlsimport_saas_single_write_import_custom_logs( $log, 'cron' ); 2061 2081 2062 $api_call_array = $this->theme_importer->global _api_request_CURL_saas( 'listings', $import_arguments, 'POST' );2063 2064 $mlsimport->admin->theme_importer->mlsimport _saas_cron_parse_search_array_per_item( $api_call_array, $input_arg['item_id_array'], $key );2082 $api_call_array = $this->theme_importer->globalApiRequestCurlSaas( 'listings', $import_arguments, 'POST' ); 2083 2084 $mlsimport->admin->theme_importer->mlsimportSaasCronParseSearchArrayPerItem( $api_call_array, $input_arg['item_id_array'], $key ); 2065 2085 } 2066 2086 … … 2121 2141 mlsimport_saas_single_write_import_custom_logs( $log_prefix . wp_json_encode( $input_arg['item_id_array'] ) . PHP_EOL ); 2122 2142 2123 ini_set('memory_limit', '256M');2124 ini_set('max_execution_time', 0); // Unlimited execution time2143 //ini_set('memory_limit', '256M'); 2144 //ini_set('max_execution_time', 0); // Unlimited execution time 2125 2145 2126 2146 // Get from MLS Import the big argument array only once … … 2129 2149 // Retrieve all meta data in one go to reduce database queries 2130 2150 $mlsimport_item_option_data = array( 2131 'mlsimport_item_standardstatus' => get_post_meta( $mlsimportItemId, 'mlsimport_item_standardstatus', true ), 2132 'mlsimport_item_property_user' => get_post_meta( $mlsimportItemId, 'mlsimport_item_property_user', true ), 2133 'mlsimport_item_agent' => get_post_meta( $mlsimportItemId, 'mlsimport_item_agent', true ), 2134 'mlsimport_item_property_status' => get_post_meta( $mlsimportItemId, 'mlsimport_item_property_status', true ), 2151 'mlsimport_item_standardstatus' => get_post_meta( $mlsimportItemId, 'mlsimport_item_standardstatus', true ), 2152 'mlsimport_item_standardstatusdelete' => get_post_meta( $mlsimportItemId, 'mlsimport_item_standardstatusdelete', true ), 2153 'mlsimport_item_property_user' => get_post_meta( $mlsimportItemId, 'mlsimport_item_property_user', true ), 2154 'mlsimport_item_agent' => get_post_meta( $mlsimportItemId, 'mlsimport_item_agent', true ), 2155 'mlsimport_item_property_status' => get_post_meta( $mlsimportItemId, 'mlsimport_item_property_status', true ), 2135 2156 ); 2136 2157 … … 2165 2186 print esc_html($log); 2166 2187 2167 $api_call_array = $this->theme_importer->global_api_request_CURL_saas( 'listings', $import_arguments, 'POST' ); 2168 2169 $mlsimport->admin->theme_importer->mlsimport_saas_parse_search_array_per_item( $api_call_array, $input_arg['item_id_array'], $key, $mlsimport_item_option_data ); 2170 // $this->admin->theme_importer->mlsimport_saas_parse_search_array_per_item($api_call_array, $input_arg['item_id_array'], $key, $mlsimport_item_option_data); 2188 $api_call_array = $this->theme_importer->globalApiRequestCurlSaas( 'listings', $import_arguments, 'POST' ); 2189 2190 $mlsimport->admin->theme_importer->mlsimportSaasParseSearchArrayPerItem( $api_call_array, $input_arg['item_id_array'], $key, $mlsimport_item_option_data ); 2171 2191 } else { 2172 2192 update_post_meta( $mlsimportItemId, 'mlsimport_spawn_status', 'completed' ); … … 2291 2311 $url = 'clients?theme_id=' . intval( $options['mlsimport_theme_used'] ); 2292 2312 2293 $answer = $theme_Start::global _api_request_saas( $url, $values, 'GET' );2313 $answer = $theme_Start::globalApiRequestSaas( $url, $values, 'GET' ); 2294 2314 2295 2315 update_option( 'mlsimport_mls_metadata_populated', 'yes' ); -
mlsimport/trunk/admin/partials/mlsimport-administrative-options.php
r3075646 r3134790 27 27 $mlsimport->admin->mlsimport_saas_setting_up(); 28 28 // mlsimport_saas_event_mls_import_auto_function(); 29 //mlsimport_saas_reconciliation_event_function();29 //mlsimport_saas_reconciliation_event_function(); 30 30 ?> 31 31 -
mlsimport/trunk/enviroment/EstateClass.php
r3075646 r3134790 53 53 * Deal with extra meta 54 54 */ 55 public function mlsimport _saas_set_extra_meta( $property_id, $property ) {55 public function mlsimportSaasSetExtraMeta( $property_id, $property ) { 56 56 $property_history = ''; 57 57 $extra_meta_log = ''; … … 169 169 * @var string $plugin_name 170 170 */ 171 public function correlation _update_after( $is_insert, $property_id, $global_extra_fields ) {171 public function correlationUpdateAfter( $is_insert, $property_id, $global_extra_fields ) { 172 172 if ( 'yes' === $is_insert ) { 173 173 update_post_meta( $property_id, 'local_pgpr_slider_type', 'global' ); -
mlsimport/trunk/enviroment/HouzezClass.php
r3075646 r3134790 59 59 * Deal with extra meta 60 60 */ 61 public function mlsimport _saas_set_extra_meta( $property_id, $property ) {61 public function mlsimportSaasSetExtraMeta( $property_id, $property ) { 62 62 $property_history = ''; 63 63 $extra_meta_log = ''; … … 176 176 * @var string $plugin_name 177 177 */ 178 public function correlation _update_after( $is_insert, $property_id, $global_extra_fields, $new_agent ) {178 public function correlationUpdateAfter( $is_insert, $property_id, $global_extra_fields, $new_agent ) { 179 179 if ( 'yes' === $is_insert ) { 180 180 $options_mls = get_option( 'mlsimport_admin_mls_sync' ); -
mlsimport/trunk/enviroment/RealHomesClass.php
r3075646 r3134790 55 55 * Deal with extra meta 56 56 */ 57 public function mlsimport _saas_set_extra_meta( $property_id, $property ) {57 public function mlsimportSaasSetExtraMeta( $property_id, $property ) { 58 58 $property_history = ''; 59 59 $extra_meta_log = ''; … … 164 164 * @var string $plugin_name 165 165 */ 166 public function correlation _update_after( $is_insert, $property_id, $global_extra_fields, $new_agent ) {166 public function correlationUpdateAfter( $is_insert, $property_id, $global_extra_fields, $new_agent ) { 167 167 if ( 'yes' === $is_insert ) { 168 168 update_post_meta( $property_id, 'REAL_HOMES_featured', 0 ); -
mlsimport/trunk/enviroment/ResidenceClass.php
r3093628 r3134790 56 56 * Deal with extra meta 57 57 */ 58 public function mlsimport _saas_set_extra_meta( $property_id, $property ) {58 public function mlsimportSaasSetExtraMeta( $property_id, $property ) { 59 59 $property_history = array(); 60 60 $extra_meta_log = array(); … … 174 174 * @var string $plugin_name 175 175 */ 176 public function correlation _update_after( $is_insert, $property_id, $global_extra_fields, $new_agent ) {176 public function correlationUpdateAfter( $is_insert, $property_id, $global_extra_fields, $new_agent ) { 177 177 if ( 'yes' === $is_insert ) { 178 178 update_post_meta( $property_id, 'prop_featured', 0 ); -
mlsimport/trunk/includes/ThemeImport.php
r3093628 r3134790 7 7 * Description of ThemeImport 8 8 * 9 * @ author cretu9 * @class ThemeImport 10 10 */ 11 11 class ThemeImport { 12 12 13 // put your code here14 13 15 14 public $theme; … … 17 16 public $enviroment; 18 17 public $encoded_values; 19 /** 20 * class construct 21 * 22 * @since 1.0.0 23 * @access protected 24 * @var string $plugin_name 25 */ 26 27 28 /* 29 * 30 * 31 * Api Request to MLSimport APi 32 * 33 * 34 * 35 * */ 36 37 public function global_api_request_CURL_saas( $method, $values_array, $type = 'GET' ) { 18 19 20 /** 21 * Api Request to MLSimport API using CURL 22 * 23 * @param string $method The API method to call. 24 * @param array $values_array The values to pass to the API. 25 * @param string $type The request type (default is 'GET'). 26 * @return mixed The API response or error message. 27 */ 28 29 public function globalApiRequestCurlSaas($method, $valuesArray, $type = 'GET') { 38 30 global $mlsimport; 39 $url = MLSIMPORT_API_URL . $method; 40 $headers = array( 'Content-Type' => 'text/plain' ); 31 $url = MLSIMPORT_API_URL . $method; 32 $headers = ['Content-Type' => 'text/plain']; 33 34 if ($method !== 'token') { 35 $token = self::getApiToken(); 36 $headers = [ 37 'Content-Type' => 'application/json', 38 'authorizationToken' => $token, 39 ]; 40 } 41 41 42 if ( 'token' !== $method ) { 43 $token = $mlsimport->admin->mlsimport_saas_get_mls_api_token_from_transient(); 44 $headers = array(); 45 $headers['Content-Type'] = 'application/json'; 46 $headers['authorizationToken'] = $token; 47 48 } 49 50 51 $args = array( 52 'method' => 'GET', 53 'headers' => $headers, 54 'body' => wp_json_encode($values_array), 55 'timeout' => 120, 42 $args = [ 43 'method' => $type, 44 'headers' => $headers, 45 'body' => !empty($valuesArray) ? wp_json_encode($valuesArray) : null, 46 'timeout' => 120, 56 47 'redirection' => 10, 57 48 'httpversion' => '1.1', 58 'blocking' => true, 59 'user-agent' => $_SERVER['HTTP_USER_AGENT'] 60 ); 49 'blocking' => true, 50 'user-agent' => $_SERVER['HTTP_USER_AGENT'], 51 ]; 52 53 $response = $type === 'GET' ? wp_remote_get($url, $args) : wp_remote_post($url, $args); 54 61 55 62 56 63 if(empty($values_array)){ 64 unset($args['body']); 65 } 66 67 68 69 // Choose the appropriate WordPress HTTP API function based on the request type 70 if ( 'GET' === $type ) { 71 $response = wp_remote_get( $url, $args ); 57 58 if (is_wp_error($response)) { 59 return $response->get_error_message(); 72 60 } else { 73 $args['method'] = $type; 74 $response = wp_remote_post( $url, $args ); 75 } 61 $body = wp_remote_retrieve_body($response); 62 $toReturn = json_decode($body, true); 63 if (json_last_error() !== JSON_ERROR_NONE) { 64 return 'JSON decode error: ' . json_last_error_msg(); 65 } 66 return $toReturn; 67 } 68 } 69 76 70 77 78 79 // Handle response 80 if ( is_wp_error( $response ) ) { 81 return $response->get_error_message(); 82 } else { 83 $body = wp_remote_retrieve_body( $response ); 84 $to_return = json_decode( $body, true ); 85 86 return $to_return; 87 } 88 } 89 90 91 92 93 94 /* 95 * 96 * 97 * Api Request to MLSimport APi 98 * 99 * 100 * 101 * */ 102 103 104 public static function global_api_request_saas( $method, $values_array, $type = 'GET' ) { 71 /** 72 * Retrieve the API token 73 * 74 * @return string The API token. 75 */ 76 private static function getApiToken() { 77 global $mlsimport; 78 return $mlsimport->admin->mlsimport_saas_get_mls_api_token_from_transient(); 79 } 80 81 82 /** 83 * Api Request to MLSimport API 84 * 85 * @param string $method The API method to call. 86 * @param array $valuesArray The values to pass to the API. 87 * @param string $type The request type (default is 'GET'). 88 * @return array The API response data. 89 */ 90 91 public static function globalApiRequestSaas($method, $valuesArray, $type = 'GET') { 105 92 global $mlsimport; 106 93 $url = MLSIMPORT_API_URL . $method; 107 94 108 $headers = array();109 if ( 'token' !== $method && 'mls' !== $method) {110 $token = $mlsimport->admin->mlsimport_saas_get_mls_api_token_from_transient();111 $headers = array(95 $headers = []; 96 if ($method !== 'token' && $method !== 'mls') { 97 $token = self::getApiToken(); 98 $headers = [ 112 99 'authorizationToken' => $token, 113 'Content-Type' => 'application/json',114 );115 } 116 117 118 $arg uments = array(119 'method' => $type,120 'timeout' => 45,100 'Content-Type' => 'application/json', 101 ]; 102 } 103 104 105 $args = [ 106 'method' => $type, 107 'timeout' => 45, 121 108 'redirection' => 5, 122 109 'httpversion' => '1.0', 123 'blocking' => true, 124 'headers' => $headers, 125 'cookies' => array(), 126 ); 127 128 if ( is_array( $values_array ) && ! empty( $values_array ) ) { 129 $arguments['body'] = wp_json_encode( $values_array ); 130 } 131 132 $response = wp_remote_post( $url, $arguments ); 133 134 135 if ( is_wp_error( $response ) ) { 136 // It is a WordPress error. 137 $error_code = $response->get_error_code(); 138 $error_message = esc_html( $response->get_error_message( $error_code ) ); 139 $warning_message = sprintf( 140 esc_html__( 'You have a WordPress Error. Error code: %s. Error Description: %s', 'mlsimport' ), 141 esc_html( $error_code ), 142 $error_message 143 ); 144 print esc_html( '<div class="mlsimport_warning">' . $warning_message . '</div>' ); 145 146 $received_data['succes'] = false; 147 return $received_data; 148 } 149 150 if ( isset( $response['response']['code'] ) && 200 === $response['response']['code'] ) { 151 $received_data = json_decode( wp_remote_retrieve_body( $response ), true ); 110 'blocking' => true, 111 'headers' => $headers, 112 'cookies' => [], 113 'body' => !empty($valuesArray) ? wp_json_encode($valuesArray) : null, 114 ]; 115 $response = wp_remote_post($url, $args); 116 117 118 if (is_wp_error($response)) { 119 return [ 120 'success' => false, 121 'error_code' => $response->get_error_code(), 122 'error_message' => esc_html($response->get_error_message()) 123 ]; 124 } 125 126 if (isset($response['response']['code']) && $response['response']['code'] === 200) { 127 $receivedData = json_decode(wp_remote_retrieve_body($response), true); 128 return $receivedData; 129 } else { 130 return ['success' => false]; 131 } 132 133 exit(); 134 } 135 136 137 138 139 140 141 142 143 /** 144 * Parse Result Array 145 * 146 * @param array $readyToParseArray The array ready to be parsed. 147 * @param array $itemIdArray The item ID array. 148 * @param string $batchKey The batch key. 149 * @param array $mlsimportItemOptionData The item option data. 150 */ 152 151 153 return $received_data; 154 } else { 155 $received_data['succes'] = false; 156 return $received_data; 157 } 158 exit(); 159 } 160 161 162 163 164 165 166 167 168 /** 169 * 170 * 171 * 172 * Parse Result Array 173 */ 174 public function mlsimport_saas_parse_search_array_per_item( $ready_to_parse_array, $item_id_array, $batch_key, $mlsimport_item_option_data ) { 152 public function mlsimportSaasParseSearchArrayPerItem($readyToParseArray, $itemIdArray, $batchKey, $mlsimportItemOptionData) { 175 153 $logs = ''; 176 154 177 155 wp_cache_flush(); 178 156 gc_collect_cycles(); 179 $counter_prop = 0; 180 if(isset( $ready_to_parse_array['data'])){ 181 foreach ( $ready_to_parse_array['data'] as $key => $property ) { 182 ++$counter_prop; 183 184 $logs = $this->mlsimport_mem_usage() . '=== In parse search array, listing no ' . $key . ' from batch ' . $batch_key . ' with Listingkey: ' . $property['ListingKey'] . PHP_EOL; 185 mlsimport_saas_single_write_import_custom_logs( $logs, 'import' ); 157 $counterProp = 0; 158 159 if (isset($readyToParseArray['data'])) { 160 foreach ($readyToParseArray['data'] as $key => $property) { 161 ++$counterProp; 162 163 $logs = $this->mlsimportMemUsage() . '=== In parse search array, listing no ' . $key . ' from batch ' . $batchKey . ' with ListingKey: ' . $property['ListingKey'] . PHP_EOL; 164 $this->writeImportLogs($logs, 'import'); 165 166 wp_cache_delete('mlsimport_force_stop_' . $itemIdArray['item_id'], 'options'); 167 168 $status = get_option('mlsimport_force_stop_' . $itemIdArray['item_id']); 169 $logs = $this->mlsimportMemUsage() . ' / on Batch ' . $itemIdArray['batch_counter'] . ', Item ID: ' . $itemIdArray['item_id'] . '/' . $counterProp . ' check ListingKey ' . $property['ListingKey'] . ' - stop command issued ? ' . $status . PHP_EOL; 170 $this->writeImportLogs($logs, 'import'); 171 172 if ($status === 'no') { 173 $logs = 'Will proceed to import - Memory Used ' . $this->mlsimportMemUsage() . PHP_EOL; 174 $this->writeImportLogs($logs, 'import'); 175 $this->mlsimportSaasPrepareToImportPerItem($property, $itemIdArray, 'normal', $mlsimportItemOptionData); 176 } else { 177 update_post_meta($itemIdArray['item_id'], 'mlsimport_spawn_status', 'completed'); 178 } 179 unset($logs); 180 } 181 } 182 183 unset($readyToParseArray); 184 unset($logs); 185 } 186 187 188 /** 189 * Write logs for import process 190 * 191 * @param string $logs The log message to write. 192 * @param string $type The type of log. 193 */ 194 private function writeImportLogs($logs, $type) { 195 mlsimport_saas_single_write_import_custom_logs($logs, $type); 196 } 197 198 /** 199 * Get memory usage 200 * 201 * @return string The memory usage in MB. 202 */ 203 public function mlsimportMemUsage() { 204 $memUsage = memory_get_usage(true); 205 $memUsageShow = round($memUsage / 1048576, 2); 206 return $memUsageShow . 'mb '; 207 } 208 209 186 210 187 211 188 wp_cache_delete( 'mlsimport_force_stop_' . $item_id_array['item_id'], 'options' ); 189 190 $status = get_option( 'mlsimport_force_stop_' . $item_id_array['item_id'] ); 191 $logs = $this->mlsimport_mem_usage() . ' / on Batch ' . $item_id_array['batch_counter'] . ', Item ID: ' . $item_id_array['item_id'] . '/' . $counter_prop . ' check ListingKey ' . $property['ListingKey'] . ' - stop command issued ? ' . $status . PHP_EOL; 192 mlsimport_saas_single_write_import_custom_logs( $logs, 'import' ); 193 194 if ( 'no' === $status ) { 195 $logs = 'Will proceed to import - Memory Used ' . $this->mlsimport_mem_usage() . PHP_EOL; 196 mlsimport_saas_single_write_import_custom_logs( $logs, 'import' ); 197 $this->mlsimport_saas_prepare_to_import_per_item( $property, $item_id_array, 'normal', $mlsimport_item_option_data ); 198 } else { 199 update_post_meta( $item_id_array['item_id'], 'mlsimport_spawn_status', 'completed' ); 200 } 201 unset( $logs ); 202 } 203 } 204 205 unset( $ready_to_parse_array ); 206 unset( $logs ); 207 } 208 209 210 /** 211 * 212 * 213 * Parse Result Array in CROn 214 */ 215 public function mlsimport_saas_cron_parse_search_array_per_item( $ready_to_parse_array, $item_id_array, $batch_key ) { 216 217 $mlsimport_item_option_data = array(); 218 $mlsimport_item_option_data['mlsimport_item_standardstatus'] = get_post_meta( $item_id_array['item_id'], 'mlsimport_item_standardstatus', true ); 219 $mlsimport_item_option_data['mlsimport_item_property_user'] = get_post_meta( $item_id_array['item_id'], 'mlsimport_item_property_user', true ); 220 $mlsimport_item_option_data['mlsimport_item_agent'] = get_post_meta( $item_id_array['item_id'], 'mlsimport_item_agent', true ); 221 $mlsimport_item_option_data['mlsimport_item_property_status'] = get_post_meta( $item_id_array['item_id'], 'mlsimport_item_property_status', true ); 222 223 foreach ( $ready_to_parse_array['data'] as $key => $property ) { 224 $logs = 'In CRON parse search array, listing no ' . $key . ' from batch ' . $batch_key . ' with Listingkey: ' . $property['ListingKey'] . PHP_EOL; 225 mlsimport_saas_single_write_import_custom_logs( $logs, 'cron' ); 226 $this->mlsimport_saas_prepare_to_import_per_item( $property, $item_id_array, 'cron', $mlsimport_item_option_data ); 227 } 228 } 229 230 231 232 233 234 235 236 /** 237 * 238 * 239 * check if property already imported 240 */ 241 public function mlsimport_saas_retrive_property_by_id( $key, $post_type = 'estate_property' ) { 242 243 $args = array( 244 'post_type' => $post_type, 212 213 /** 214 * Parse Result Array in CRON 215 * 216 * @param array $readyToParseArray The array ready to be parsed. 217 * @param array $itemIdArray The item ID array. 218 * @param string $batchKey The batch key. 219 */ 220 public function mlsimportSaasCronParseSearchArrayPerItem($readyToParseArray, $itemIdArray, $batchKey) { 221 $mlsimportItemOptionData = [ 222 'mlsimport_item_standardstatus' => get_post_meta($itemIdArray['item_id'], 'mlsimport_item_standardstatus', true), 223 'mlsimport_item_standardstatusdelete' => get_post_meta($itemIdArray['item_id'], 'mlsimport_item_standardstatusdelete', true), 224 'mlsimport_item_property_user' => get_post_meta($itemIdArray['item_id'], 'mlsimport_item_property_user', true), 225 'mlsimport_item_agent' => get_post_meta($itemIdArray['item_id'], 'mlsimport_item_agent', true), 226 'mlsimport_item_property_status' => get_post_meta($itemIdArray['item_id'], 'mlsimport_item_property_status', true), 227 ]; 228 229 foreach ($readyToParseArray['data'] as $key => $property) { 230 $logs = 'In CRON parse search array, listing no ' . $key . ' from batch ' . $batchKey . ' with ListingKey: ' . $property['ListingKey'] . PHP_EOL; 231 $this->writeImportLogs($logs, 'cron'); 232 $this->mlsimportSaasPrepareToImportPerItem($property, $itemIdArray, 'cron', $mlsimportItemOptionData); 233 } 234 } 235 236 237 238 239 240 241 242 243 /** 244 * Check if property already imported 245 * 246 * @param string $key The key to search for. 247 * @param string $postType The post type to search within (default is 'estate_property'). 248 * @return int The post ID if found, or 0 if not found. 249 */ 250 public function mlsimportSaasRetrievePropertyById($key, $postType = 'estate_property') { 251 $args = [ 252 'post_type' => $postType, 245 253 'post_status' => 'any', 246 247 'meta_query' => array( 248 array( 249 'key' => 'ListingKey', 250 'value' => $key, 254 'meta_query' => [ 255 [ 256 'key' => 'ListingKey', 257 'value' => $key, 251 258 'compare' => '=', 252 ), 253 ), 254 'fields' => 'ids', 255 ); 256 257 $prop_selection = new WP_Query( $args ); 258 if ( $prop_selection->have_posts() ) { 259 while ( $prop_selection->have_posts() ) { 260 $prop_selection->the_post(); 261 $the_id = get_the_ID(); 262 wp_reset_postdata(); 263 return $the_id; 264 } 259 ], 260 ], 261 'fields' => 'ids', 262 ]; 263 264 $query = new WP_Query($args); 265 if ($query->have_posts()) { 266 $query->the_post(); 267 $propertyId = get_the_ID(); 268 wp_reset_postdata(); 269 return $propertyId; 265 270 } else { 266 271 wp_reset_postdata(); … … 272 277 273 278 274 275 /** 276 * clear taxonomy 277 * 278 * @since 1.0.0 279 * @access protected 280 * @var string $plugin_name 281 */ 282 public function mlsimport_saas_clear_property_for_taxonomy( $property_id, $taxonomies ) { 283 284 if ( is_array( $taxonomies ) ) : 285 foreach ( $taxonomies as $taxonomy => $term ) : 279 /** 280 * Clear taxonomy 281 * 282 * @param int $propertyId The property ID. 283 * @param array $taxonomies The taxonomies to clear. 284 */ 285 public function mlsimportSaasClearPropertyForTaxonomy($propertyId, $taxonomies) { 286 if (is_array($taxonomies)) { 287 foreach ($taxonomies as $taxonomy => $term) { 286 288 if (is_wp_error($taxonomy)) { 287 error_log('Error with taxonomy: ' . $taxonomy->get_error_message()); 288 continue; // Skip this iteration 289 } 290 291 if ( taxonomy_exists($taxonomy) ) { 292 wp_delete_object_term_relationships($property_id, $taxonomy); 293 } else { 294 error_log("Taxonomy does not exist: {$taxonomy}"); 295 } 296 endforeach; 297 endif; 298 } 299 300 301 302 303 304 305 306 307 /** 308 * return non encoded encoded values 309 * 310 * @since 1.0.0 311 * @access protected 312 * @var string $plugin_name 313 */ 314 public function mlsimport_saas_return_non_encoded_value( $item, $encoded_values ) { 315 316 if ( is_array( $item ) ) { 317 if ( ! empty( $encoded_values ) ) { 318 foreach ( $item as $key => $value ) { 319 if ( isset( $encoded_values [ $value ] ) ) { 320 $item[ $key ] = $encoded_values [ $value ]; 289 error_log('Error with taxonomy: ' . $taxonomy->get_error_message()); 290 continue; // Skip this iteration 291 } 292 293 if (taxonomy_exists($taxonomy)) { 294 wp_delete_object_term_relationships($propertyId, $taxonomy); 295 } else { 296 error_log("Taxonomy does not exist: {$taxonomy}"); 297 } 298 } 299 } 300 } 301 302 303 304 305 306 /** 307 * Set taxonomy for property 308 * 309 * @param string $taxonomy The taxonomy to set. 310 * @param int $propertyId The property ID. 311 * @param mixed $fieldValues The values to set. 312 */ 313 public function mlsimportSaasUpdateTaxonomyForProperty($taxonomy, $propertyId, $fieldValues) { 314 global $wpdb; 315 316 // Convert comma-separated values to array if necessary 317 if (!is_array($fieldValues)) { 318 $fieldValues = strpos($fieldValues, ',') !== false ? explode(',', $fieldValues) : [$fieldValues]; 319 } 320 321 // Trim values and remove empty ones 322 $fieldValues = array_filter(array_map('trim', $fieldValues)); 323 324 // Start a database transaction 325 $wpdb->query('START TRANSACTION'); 326 $taxLog = []; 327 328 foreach (array_chunk($fieldValues, 5) as $chunk) { 329 foreach ($chunk as $value) { 330 if (!empty($value)) { 331 // Check if the term already exists 332 $term = $wpdb->get_row($wpdb->prepare( 333 "SELECT t.*, tt.* FROM $wpdb->terms t 334 INNER JOIN $wpdb->term_taxonomy tt ON t.term_id = tt.term_id 335 WHERE t.name = %s AND tt.taxonomy = %s", 336 $value, $taxonomy 337 )); 338 339 $taxLog[] = json_encode($term); 340 if (is_null($term)) { 341 // Insert the term if it doesn't exist 342 $wpdb->insert($wpdb->terms, [ 343 'name' => $value, 344 'slug' => sanitize_title($value), 345 'term_group' => 0 346 ]); 347 348 $termId = $wpdb->insert_id; 349 350 if ($termId) { 351 // Insert term taxonomy 352 $wpdb->insert($wpdb->term_taxonomy, [ 353 'term_id' => $termId, 354 'taxonomy' => $taxonomy, 355 'description' => '', 356 'parent' => 0, 357 'count' => 0 358 ]); 359 360 $termTaxonomyId = $wpdb->insert_id; 361 } else { 362 $taxLog[] = 'Error inserting term'; 363 continue; 364 } 365 } else { 366 // Term exists, get term_id and term_taxonomy_id 367 $termId = $term->term_id; 368 $termTaxonomyId = $wpdb->get_var($wpdb->prepare( 369 "SELECT term_taxonomy_id FROM $wpdb->term_taxonomy WHERE term_id = %d AND taxonomy = %s", 370 $termId, $taxonomy 371 )); 372 } 373 374 if (!empty($termTaxonomyId)) { 375 // Insert term relationship 376 $wpdb->replace($wpdb->term_relationships, [ 377 'object_id' => $propertyId, 378 'term_taxonomy_id' => $termTaxonomyId 379 ]); 380 // Increment the term count 381 $wpdb->query($wpdb->prepare( 382 "UPDATE $wpdb->term_taxonomy SET count = count + 1 WHERE term_taxonomy_id = %d", 383 $termTaxonomyId 384 )); 385 } else { 386 $taxLog[] = 'Error: term_taxonomy_id is null'; 321 387 } 322 388 } 323 389 } 324 return $item; 325 } elseif ( ! empty( $encoded_values ) && isset( $encoded_values [ $item ] ) ) { 326 return $encoded_values [ $item ]; 327 } else { 328 return $item; 329 } 330 } 331 332 /** 333 * set taxonomy 334 * 335 * @since 1.0.0 336 * @access protected 337 * @var string $plugin_name 338 */ 339 340 function mlsimport_saas_update_taxonomy_for_property_new($taxonomy, $property_id, $field_values) { 341 global $wpdb; 342 343 // Remove filters temporarily to avoid caching issues 344 remove_filter('get_term_metadata', array($wpdb->terms, 'cache_term_counts')); 345 wp_cache_delete('terms', 'cache'); 346 347 if (!is_array($field_values)) { 348 if (strpos($field_values, ',') !== false) { 349 $field_values = explode(',', $field_values); 350 } else { 351 $field_values = array($field_values); 352 } 353 } 354 355 // Use transactions for better performance 356 $wpdb->query('START TRANSACTION'); 357 358 // Process in smaller chunks 359 foreach (array_chunk($field_values, 5) as $chunk) { 360 foreach ($chunk as $value) { 361 if (!empty($value)) { 362 $term = get_term_by('name', $value, $taxonomy); 363 364 if (is_wp_error($term) || empty($term)) { 365 // Term doesn't exist, insert it 366 $term = wp_insert_term($value, $taxonomy); 367 368 if (!is_wp_error($term)) { 369 $term_id = $term['term_id']; 370 $term_taxonomy_id = $term['term_taxonomy_id']; 371 } 372 } else { 373 // Term exists 374 $term_id = $term->term_id; 375 $term_taxonomy_id = $term->term_taxonomy_id; 376 } 377 378 if (!empty($term_id) && !empty($term_taxonomy_id)) { 379 // Insert term relationship 380 $wpdb->insert( 381 $wpdb->term_relationships, 382 array( 383 'object_id' => $property_id, 384 'term_taxonomy_id' => $term_taxonomy_id, 385 'term_order' => 0 386 ), 387 array( 388 '%d', 389 '%d', 390 '%d' 391 ) 392 ); 393 } 394 } 395 } 396 } 397 398 // Commit transaction 399 $wpdb->query('COMMIT'); 400 401 // Clear term cache selectively 402 wp_cache_delete("{$taxonomy}_terms", 'terms'); 403 wp_cache_delete("{$taxonomy}_children", 'terms'); 404 405 // Restore the term metadata filter 406 add_filter('get_term_metadata', array($wpdb->terms, 'cache_term_counts'), 10, 2); 407 } 408 409 function mlsimport_saas_update_taxonomy_for_property($taxonomy, $property_id, $field_values) { 410 global $wpdb; 411 412 // Convert comma-separated values to array if necessary 413 if (!is_array($field_values)) { 414 $field_values = strpos($field_values, ',') !== false ? explode(',', $field_values) : array($field_values); 415 } 416 417 // Trim values and remove empty ones 418 $field_values = array_filter(array_map('trim', $field_values)); 419 420 // Start a database transaction 421 $wpdb->query('START TRANSACTION'); 422 $tax_log = array(); 423 424 foreach (array_chunk($field_values, 5) as $chunk) { 425 foreach ($chunk as $value) { 426 if (!empty($value)) { 427 // Check if the term already exists 428 429 $term = $wpdb->get_row($wpdb->prepare( 430 "SELECT t.*, tt.* FROM $wpdb->terms t 431 INNER JOIN $wpdb->term_taxonomy tt ON t.term_id = tt.term_id 432 WHERE t.name = %s AND tt.taxonomy = %s", 433 $value, $taxonomy 434 )); 435 436 437 $tax_log[] = json_encode($term); 438 if (is_null($term)) { 439 // Insert the term if it doesn't exist 440 441 $wpdb->insert($wpdb->terms, array( 442 'name' => $value, 443 'slug' => sanitize_title($value), 444 'term_group' => 0 445 )); 446 447 $term_id = $wpdb->insert_id; 448 449 if ($term_id) { 450 // Insert term taxonomy 451 $wpdb->insert($wpdb->term_taxonomy, array( 452 'term_id' => $term_id, 453 'taxonomy' => $taxonomy, 454 'description' => '', 455 'parent' => 0, 456 'count' => 0 457 )); 458 459 $term_taxonomy_id = $wpdb->insert_id; 460 } else { 461 $tax_log[] = 'Error inserting term'; 462 continue; 463 } 464 } else { 465 // Term exists, get term_id and term_taxonomy_id 466 $term_id = $term->term_id; 467 $term_taxonomy_id = $wpdb->get_var($wpdb->prepare( 468 "SELECT term_taxonomy_id FROM $wpdb->term_taxonomy WHERE term_id = %d AND taxonomy = %s", 469 $term_id, 470 $taxonomy 471 )); 472 } 473 474 if (!empty($term_taxonomy_id)) { 475 // Insert term relationship 476 $wpdb->replace($wpdb->term_relationships, array( 477 'object_id' => $property_id, 478 'term_taxonomy_id' => $term_taxonomy_id 479 )); 480 // Increment the term count 481 $wpdb->query($wpdb->prepare( 482 "UPDATE $wpdb->term_taxonomy SET count = count + 1 WHERE term_taxonomy_id = %d", 483 $term_taxonomy_id 484 )); 485 } else { 486 $tax_log[] = 'Error: term_taxonomy_id is null'; 487 } 488 } 489 } 490 // Flush the cache to free up memory 491 wp_cache_flush(); 492 // Run garbage collection 493 gc_collect_cycles(); 494 } 495 // Commit the transaction 496 $wpdb->query('COMMIT'); 497 498 // Clear term cache selectively 499 wp_cache_delete("{$taxonomy}_terms", 'terms'); 500 wp_cache_delete("{$taxonomy}_children", 'terms'); 501 502 // Restore the term metadata filter 503 add_filter('get_term_metadata', array($wpdb->terms, 'cache_term_counts'), 10, 2); 504 505 // Log memory usage 506 /*$tip_import='normal'; 507 if (!empty($tax_log)) { 508 $tax_log_str = implode(PHP_EOL, $tax_log); 509 mlsimport_saas_single_write_import_custom_logs($tax_log_str, $tip_import); 510 unset($tax_log_str); 511 }*/ 512 } 513 514 515 516 public function mlsimport_saas_update_taxonomy_for_property2( $taxonomy, $property_id, $field_values ) { 390 // Flush the cache to free up memory 391 wp_cache_flush(); 392 // Run garbage collection 393 gc_collect_cycles(); 394 } 395 // Commit the transaction 396 $wpdb->query('COMMIT'); 397 398 // Clear term cache selectively 399 wp_cache_delete("{$taxonomy}_terms", 'terms'); 400 wp_cache_delete("{$taxonomy}_children", 'terms'); 401 402 // Restore the term metadata filter 403 add_filter('get_term_metadata', [$wpdb->terms, 'cache_term_counts'], 10, 2); 404 405 // Log memory usage 406 // if (!empty($taxLog)) { 407 // $taxLogStr = implode(PHP_EOL, $taxLog); 408 // mlsimport_saas_single_write_import_custom_logs($taxLogStr, 'normal'); 409 // unset($taxLogStr); 410 // } 411 } 412 413 414 415 416 /** 417 * Set Property Title 418 * 419 * @param int $propertyId The property ID. 420 * @param int $mlsImportPostId The MLS import post ID. 421 * @param array $property The property data. 422 * @return string The updated title format. 423 */ 424 public function mlsimportSaasUpdatePropertyTitle($propertyId, $mlsImportPostId, $property) { 425 global $mlsimport; 426 427 $titleFormat = esc_html(get_post_meta($mlsImportPostId, 'mlsimport_item_title_format', true)); 428 429 if ('' === $titleFormat) { 430 $options = get_option('mlsimport_admin_mls_sync'); 431 $titleFormat = $options['title_format']; 432 } 433 434 $titleArray = $this->strBetweenAll($titleFormat, '{', '}'); 435 436 $propertyExtraMetaArrayLowerCase = array_change_key_case($property['extra_meta'], CASE_LOWER); 437 438 foreach ($titleArray as $key => $value) { 439 $replace = ''; 440 switch ($value) { 441 case 'Address': 442 $replace = $property['adr_title'] ?? ''; 443 break; 444 case 'City': 445 $replace = $property['adr_city'] ?? ''; 446 break; 447 case 'CountyOrParish': 448 $replace = $property['adr_county'] ?? ''; 449 break; 450 case 'PropertyType': 451 $replace = $property['adr_type'] ?? ''; 452 break; 453 case 'Bedrooms': 454 $replace = $property['adr_bedrooms'] ?? ''; 455 break; 456 case 'Bathrooms': 457 $replace = $property['adr_bathrooms'] ?? ''; 458 break; 459 case 'ListingKey': 460 $replace = $property['ListingKey']; 461 break; 462 case 'ListingId': 463 $replace = $property['adr_listingid'] ?? ''; 464 break; 465 case 'StateOrProvince': 466 $replace = $property['extra_meta']['StateOrProvince'] ?? ''; 467 break; 468 case 'PostalCode': 469 $replace = $property['meta']['property_zip'] ?? $property['meta']['fave_property_zip'] ?? ''; 470 $replace = is_array($replace) ? strval($replace[0]) : strval($replace); 471 break; 472 case 'StreetNumberNumeric': 473 $replace = $propertyExtraMetaArrayLowerCase['streetnumbernumeric'] ?? ''; 474 break; 475 case 'StreetName': 476 $replace = $propertyExtraMetaArrayLowerCase['streetname'] ?? ''; 477 break; 478 } 479 $titleFormat = str_replace('{' . $value . '}', $replace, $titleFormat); 480 } 481 482 $post = [ 483 'ID' => $propertyId, 484 'post_title' => $titleFormat, 485 'post_name' => $titleFormat, 486 ]; 487 488 wp_update_post($post); 489 490 return $titleFormat; 491 } 492 517 493 518 if ( ! is_array( $field_values ) ) { 519 if ( strpos( $field_values, ',' ) !== false ) { 520 $field_values = explode( ',', $field_values ); 521 } else { 522 $field_values = array( $field_values ); 523 } 524 } 525 526 // Remove empty values and decode values in one pass 527 $processed_field_values = array(); 528 foreach ( $field_values as $value ) { 529 if ( ! empty( $value ) ) { 530 $processed_field_values[] = $value; 531 } 532 } 533 534 // Bulk update terms if array is not empty 535 if ( ! empty( $processed_field_values ) ) { 536 wp_set_object_terms( $property_id, $processed_field_values, $taxonomy, true ); 537 clean_term_cache( $property_id, $taxonomy ); 538 } 539 } 540 541 542 543 544 /** 545 * Set Property Title 546 * 547 * @since 1.0.0 548 * @access protected 549 * @var string $plugin_name 550 */ 551 public function mlsimport_saas_update_property_title( $property_id, $mls_import_post_id, $property ) { 494 495 496 497 498 /** 499 * Prepare meta data for property 500 * 501 * @param array $property The property data. 502 * @return array The property data with prepared meta. 503 */ 504 public function mlsimportSaasPrepareMetaForProperty($property) { 505 if (isset($property['extra_meta']['BathroomsTotalDecimal']) && floatval($property['extra_meta']['BathroomsTotalDecimal']) > 0) { 506 $bathrooms = floatval($property['extra_meta']['BathroomsTotalDecimal']); 507 $property['meta']['property_bathrooms'] = $bathrooms; 508 $property['meta']['fave_property_bathrooms'] = $bathrooms; 509 $property['meta']['REAL_HOMES_property_bathrooms'] = $bathrooms; 510 } 511 return $property; 512 } 513 514 515 516 517 518 /** 519 * Attach media to post 520 * 521 * @param int $propertyId The property ID. 522 * @param array $media The media data. 523 * @param string $isInsert Whether the property is being inserted. 524 * @return string The media history log. 525 */ 526 public function mlsimportSassAttachMediaToPost($propertyId, $media, $isInsert) { 527 $mediaHistory = []; 528 if ($isInsert === 'no') { 529 $mediaHistory[] = 'Media - We have edit - images are not replaced'; 530 return implode('</br>', $mediaHistory); 531 } 552 532 553 533 global $mlsimport; 554 555 $title_format = esc_html( get_post_meta( $mls_import_post_id, 'mlsimport_item_title_format', true ) ); 556 557 if ( '' === $title_format ) { 558 $options = get_option( 'mlsimport_admin_mls_sync' ); 559 $title_format = $options['title_format']; 560 } 561 562 $start = '{'; 563 $end = '}'; 564 $title_array = $this->str_between_all( $title_format, $start, $end ); 565 566 $property_extra_meta_array_lowecase = array_change_key_case( $property['extra_meta'], CASE_LOWER ); 567 568 foreach ( $title_array as $key => $value ) { 569 $replace = ''; 570 if ( 'Address' === $value ) { 571 if ( isset( $property['adr_title'] ) ) { 572 $replace = $property['adr_title']; 534 include_once ABSPATH . 'wp-admin/includes/image.php'; 535 $hasFeatured = false; 536 537 delete_post_meta($propertyId, 'fave_property_images'); 538 delete_post_meta($propertyId, 'REAL_HOMES_property_images'); 539 540 add_filter('intermediate_image_sizes_advanced', [$this, 'wpcUnsetImageSizes']); 541 542 // Sorting media 543 if (isset($media[0]['Order'])) { 544 $order = array_column($media, 'Order'); 545 array_multisort($order, SORT_ASC, $media); 546 } 547 548 if (is_array($media)) { 549 foreach ($media as $image) { 550 if (isset($image['MediaCategory']) && $image['MediaCategory'] !== 'Photo') { 551 continue; 573 552 } 574 $title_format = str_replace( '{Address}', $replace, $title_format ); 575 } elseif ( 'City' === $value ) { 576 if ( isset( $property['adr_city'] ) ) { 577 $replace = $property['adr_city']; 578 } 579 $title_format = str_replace( '{City}', $replace, $title_format ); 580 } elseif ( 'CountyOrParish' === $value ) { 581 if ( isset( $property['adr_county'] ) ) { 582 $replace = $property['adr_county']; 583 } 584 $title_format = str_replace( '{CountyOrParish}', $replace, $title_format ); 585 } elseif ( 'PropertyType' === $value ) { 586 if ( isset( $property['adr_type'] ) ) { 587 $replace = $property['adr_type']; 588 } 589 $title_format = str_replace( '{PropertyType}', $replace, $title_format ); 590 } elseif ( 'Bedrooms' === $value ) { 591 if ( isset( $property['adr_bedrooms'] ) ) { 592 $replace = $property['adr_bedrooms']; 593 } 594 $title_format = str_replace( '{Bedrooms}', $replace, $title_format ); 595 } elseif ( 'Bathrooms' === $value ) { 596 if ( isset( $property['adr_bathrooms'] ) ) { 597 $replace = $property['adr_bathrooms']; 598 } 599 $title_format = str_replace( '{Bathrooms}', $replace, $title_format ); 600 } elseif ( 'ListingKey' === $value ) { 601 $replace = $property['ListingKey']; 602 if ( '' !== $replace ) { 603 $title_format = str_replace( '{ListingKey}', $replace, $title_format ); 604 } 605 } elseif ( 'ListingId' === $value ) { 606 if ( isset( $property['adr_listingid'] ) ) { 607 $replace = $property['adr_listingid']; 608 } 609 if ( '' !== $replace ) { 610 $title_format = str_replace( '{ListingId}', $replace, $title_format ); 611 } 612 } elseif ( 'StateOrProvince' === $value ) { 613 if ( isset( $property['extra_meta']['StateOrProvince'] ) ) { 614 $replace = $property['extra_meta']['StateOrProvince']; 615 } 616 $title_format = str_replace( '{StateOrProvince}', $replace, $title_format ); 617 } elseif ( 'PostalCode' === $value ) { 618 if ( isset( $property['meta']['property_zip'] ) ) { 619 if ( is_array( $property['meta']['property_zip'] ) ) { 620 $replace = strval( $property['meta']['property_zip'][0] ); 621 } else { 622 $replace = strval( $property['meta']['property_zip'] ); 623 } 624 } elseif ( isset( $property['meta']['fave_property_zip'] ) ) { 625 if ( is_array( $property['meta']['fave_property_zip'] ) ) { 626 $replace = strval( $property['meta']['fave_property_zip'][0] ); 627 } else { 628 $replace = strval( $property['meta']['fave_property_zip'] ); 553 554 $file = $image['MediaURL']; 555 556 if (isset($image['MediaURL'])) { 557 $attachment = [ 558 'guid' => $image['MediaURL'], 559 'post_status' => 'inherit', 560 'post_content' => '', 561 'post_parent' => $propertyId, 562 'post_mime_type' => $image['MimeType'] ?? 'image/jpg', 563 'post_title' => $image['MediaKey'] ?? '', 564 ]; 565 566 $attachId = wp_insert_attachment($attachment, $file); 567 568 $mediaHistory[] = 'Media - Added ' . $image['MediaURL'] . ' as attachment ' . $attachId; 569 $mlsimport->admin->env_data->enviroment_image_save($propertyId, $attachId); 570 571 update_post_meta($attachId, 'is_mlsimport', 1); 572 if (!$hasFeatured) { 573 set_post_thumbnail($propertyId, $attachId); 574 $hasFeatured = true; 629 575 } 630 576 } 631 632 $title_format = str_replace( '{PostalCode}', $replace, $title_format ); 633 } elseif ( 'StreetNumberNumeric' === $value ) { 634 if ( isset( $property_extra_meta_array_lowecase['streetnumbernumeric'] ) ) { 635 $replace = $property_extra_meta_array_lowecase['streetnumbernumeric']; 636 } 637 $title_format = str_replace( '{StreetNumberNumeric}', $replace, $title_format ); 638 } elseif ( 'StreetName' === $value ) { 639 if ( isset( $property_extra_meta_array_lowecase['streetname'] ) ) { 640 $replace = $property_extra_meta_array_lowecase['streetname']; 641 } 642 $title_format = str_replace( '{StreetName}', $replace, $title_format ); 643 } 644 } 645 646 $post = array( 647 'ID' => $property_id, 648 'post_title' => $title_format, 649 'post_name' => $title_format, 650 ); 651 652 wp_update_post( $post ); 653 654 unset( $property_extra_meta_array_lowecase ); 655 return $title_format; 656 } 657 658 659 660 661 662 663 664 665 /** 666 * 667 * 668 * 669 * 670 * import property -prepare 671 * 672 * @since 1.0.0 673 * @access protected 674 * @var string $plugin_name 675 */ 676 public function mlsimport_saas_prepare_to_import_per_item( $property, $item_id_array, $tip_import, $mlsimport_item_option_data ) { 677 // check if MLS id is set 577 } 578 } else { 579 $mediaHistory[] = 'Media data is blank - there are no images'; 580 } 581 582 remove_filter('intermediate_image_sizes_advanced', [$this, 'wpcUnsetImageSizes']); 583 584 return implode('</br>', $mediaHistory); 585 } 586 587 /** 588 * Unset image sizes 589 * 590 * @param array $sizes The sizes to unset. 591 * @return array The modified sizes array. 592 */ 593 public function wpcUnsetImageSizes($sizes) { 594 return []; 595 } 596 597 598 599 600 601 602 603 /** 604 * Return user option 605 * 606 * @param int $selected The selected user ID. 607 * @return string The HTML option elements for users. 608 */ 609 public function mlsimportSaasThemeImportSelectUser($selected) { 610 $userOptions = ''; 611 $blogusers = get_users(['blog_id' => 1, 'orderby' => 'nicename']); 612 foreach ($blogusers as $user) { 613 $userOptions .= '<option value="' . esc_attr($user->ID) . '"'; 614 if ($user->ID == $selected) { 615 $userOptions .= ' selected="selected"'; 616 } 617 $userOptions .= '>' . esc_html($user->user_login) . '</option>'; 618 } 619 return $userOptions; 620 } 621 622 623 624 625 626 627 628 /** 629 * Return agent option 630 * 631 * @param int $selected The selected agent ID. 632 * @return string The HTML option elements for agents. 633 */ 634 public function mlsimportSaasThemeImportSelectAgent($selected) { 635 global $mlsimport; 636 $args = [ 637 'post_type' => $mlsimport->admin->env_data->get_agent_post_type(), 638 'post_status' => 'publish', 639 'posts_per_page' => 150, 640 ]; 641 642 $agentSelection = new WP_Query($args); 643 $agentOptions = '<option value=""></option>'; 644 645 while ($agentSelection->have_posts()) { 646 $agentSelection->the_post(); 647 $agentId = get_the_ID(); 648 649 $agentOptions .= '<option value="' . esc_attr($agentId) . '"'; 650 if ($agentId == $selected) { 651 $agentOptions .= ' selected="selected"'; 652 } 653 $agentOptions .= '>' . esc_html(get_the_title()) . '</option>'; 654 } 655 wp_reset_postdata(); 656 657 return $agentOptions; 658 } 659 660 661 662 663 664 665 666 667 /** 668 * Delete property 669 * 670 * @param int $deleteId The ID of the property to delete. 671 * @param string $ListingKey The listing key of the property. 672 */ 673 public function deleteProperty($deleteId, $ListingKey) { 674 if ($deleteId > 0) { 675 $args = [ 676 'numberposts' => -1, 677 'post_type' => 'attachment', 678 'post_parent' => $deleteId, 679 'post_status' => null, 680 'orderby' => 'menu_order', 681 'order' => 'ASC', 682 ]; 683 $postAttachments = get_posts($args); 684 685 foreach ($postAttachments as $attachment) { 686 wp_delete_post($attachment->ID); 687 } 688 689 wp_delete_post($deleteId); 690 $logEntry = 'Property with id ' . $deleteId . ' and ' . $ListingKey . ' was deleted on ' . current_time('Y-m-d\TH:i') . PHP_EOL; 691 $this->writeImportLogs($logEntry, 'delete'); 692 } 693 } 694 695 696 697 698 /** 699 * Return array with title items 700 * 701 * @param string $string The input string. 702 * @param string $start The start delimiter. 703 * @param string $end The end delimiter. 704 * @param bool $includeDelimiters Whether to include the delimiters in the result. 705 * @param int $offset The offset to start searching from. 706 * @return array The array of strings found between the delimiters. 707 */ 708 public function strBetweenAll(string $string, string $start, string $end, bool $includeDelimiters = false, int &$offset = 0): array { 709 $strings = []; 710 $length = strlen($string); 711 712 while ($offset < $length) { 713 $found = $this->strBetween($string, $start, $end, $includeDelimiters, $offset); 714 if ($found === null) { 715 break; 716 } 717 718 $strings[] = $found; 719 $offset += strlen($includeDelimiters ? $found : $start . $found . $end); // move offset to the end of the newfound string 720 } 721 722 return $strings; 723 } 724 725 /** 726 * Find string between delimiters 727 * 728 * @param string $string The input string. 729 * @param string $start The start delimiter. 730 * @param string $end The end delimiter. 731 * @param bool $includeDelimiters Whether to include the delimiters in the result. 732 * @param int $offset The offset to start searching from. 733 * @return string|null The string found between the delimiters, or null if not found. 734 */ 735 public function strBetween(string $string, string $start, string $end, bool $includeDelimiters = false, int &$offset = 0): ?string { 736 if ($string === '' || $start === '' || $end === '') { 737 return null; 738 } 739 740 $startLength = strlen($start); 741 $endLength = strlen($end); 742 743 $startPos = strpos($string, $start, $offset); 744 if ($startPos === false) { 745 return null; 746 } 747 748 $endPos = strpos($string, $end, $startPos + $startLength); 749 if ($endPos === false) { 750 return null; 751 } 752 753 $length = $endPos - $startPos + ($includeDelimiters ? $endLength : -$startLength); 754 if (!$length) { 755 return ''; 756 } 757 758 $offset = $startPos + ($includeDelimiters ? 0 : $startLength); 759 760 return substr($string, $offset, $length); 761 } 762 763 764 765 766 767 /** 768 * Delete property via SQL 769 * 770 * @param int $deleteId The ID of the property to delete. 771 * @param string $ListingKey The listing key of the property. 772 */ 773 public function mlsimportSaasDeletePropertyViaMysql($deleteId, $ListingKey) { 774 $postType = get_post_type($deleteId); 775 776 if (in_array($postType, ['estate_property', 'property'])) { 777 $termObjList = get_the_terms($deleteId, 'property_status'); 778 $deleteIdStatus = join(', ', wp_list_pluck($termObjList, 'name')); 779 780 $ListingKey = get_post_meta($deleteId, 'ListingKey', true); 781 if ('' === $ListingKey) { // manually added listing 782 $logEntry = 'User added listing with id ' . $deleteId . ' (' . $postType . ') (status ' . $deleteIdStatus . ') and ' . $ListingKey . ' NOT DELETED' . PHP_EOL; 783 $this->writeImportLogs($logEntry, 'delete'); 784 return; 785 } 786 787 global $wpdb; 788 $wpdb->query($wpdb->prepare("DELETE FROM $wpdb->postmeta WHERE `post_id` = %d", $deleteId)); 789 $wpdb->query($wpdb->prepare("DELETE FROM $wpdb->posts WHERE `post_parent` = %d OR `ID` = %d", $deleteId, $deleteId)); 790 791 $logEntry = 'MYSQL DELETE -> Property with id ' . $deleteId . ' (' . $postType . ') (status ' . $deleteIdStatus . ') and ' . $ListingKey . ' was deleted on ' . current_time('Y-m-d\TH:i') . PHP_EOL; 792 $this->writeImportLogs($logEntry, 'delete'); 793 } 794 } 795 796 797 798 799 800 801 802 803 /** 804 * Prepare to import per item 805 * 806 * @param array $property The property data. 807 * @param array $itemIdArray The item ID array. 808 * @param string $tipImport The import type. 809 * @param array $mlsimportItemOptionData The item option data. 810 */ 811 public function mlsimportSaasPrepareToImportPerItem($property, $itemIdArray, $tipImport, $mlsimportItemOptionData) { 678 812 set_time_limit(0); 679 813 global $mlsimport; 680 814 681 682 $mls_import_item_status = $mlsimport_item_option_data['mlsimport_item_standardstatus']; 683 $new_author = $mlsimport_item_option_data['mlsimport_item_property_user']; 684 $new_agent = $mlsimport_item_option_data['mlsimport_item_agent']; 685 $property_status = $mlsimport_item_option_data['mlsimport_item_property_status']; 686 687 if ( is_array( $mls_import_item_status ) ) { 688 $mls_import_item_status = array_map( 'strtolower', $mls_import_item_status ); 689 } 690 815 $mlsImportItemStatus = $mlsimportItemOptionData['mlsimport_item_standardstatus']; 816 $mlsImportItemStatusDelete = $mlsimportItemOptionData['mlsimport_item_standardstatusdelete']; 817 $newAuthor = $mlsimportItemOptionData['mlsimport_item_property_user']; 818 $newAgent = $mlsimportItemOptionData['mlsimport_item_agent']; 819 $propertyStatus = $mlsimportItemOptionData['mlsimport_item_property_status']; 820 821 if (is_array($mlsImportItemStatus)) { 822 $mlsImportItemStatus = array_map('strtolower', $mlsImportItemStatus); 823 } 824 825 if (!isset($property['ListingKey'])) { 826 $this->writeImportLogs('ERROR: No Listing Key ' . PHP_EOL, $tipImport); 827 return; 828 } 829 830 ob_start(); 831 832 $ListingKey = $property['ListingKey']; 833 $listingPostType = $mlsimport->admin->env_data->get_property_post_type(); 834 $propertyId = intval($this->mlsimportSaasRetrievePropertyById($ListingKey, $listingPostType)); 835 $status = isset($property['StandardStatus']) ? strtolower($property['StandardStatus']) : strtolower($property['extra_meta']['MlsStatus']); 836 $isInsert = $this->shouldInsertProperty($propertyId, $status, $mlsImportItemStatus, $tipImport); 837 838 $log = $this->mlsimportMemUsage() . '==========' . wp_json_encode($mlsImportItemStatus) . '/' . $newAuthor . '/' . $newAgent . '/' . $propertyStatus . '/ We have property with $ListingKey=' . $ListingKey . ' id=' . $propertyId . ' with status ' . $status . ' is insert? ' . $isInsert . PHP_EOL; 839 $this->writeImportLogs($log, $tipImport); 840 841 $propertyHistory = []; 842 $content = $property['content'] ?? ''; 843 $submitTitle = $ListingKey; 844 845 if ($isInsert === 'yes') { 846 $post = [ 847 'post_title' => $submitTitle, 848 'post_content' => $content, 849 'post_status' => $propertyStatus, 850 'post_type' => $listingPostType, 851 'post_author' => $newAuthor, 852 ]; 853 854 $propertyId = wp_insert_post($post); 855 if (is_wp_error($propertyId)) { 856 $this->writeImportLogs('ERROR: on inserting ' . PHP_EOL, $tipImport); 857 } else { 858 update_post_meta($propertyId, 'ListingKey', $ListingKey); 859 $keep_on_delete='delete'; 860 if(!in_array ($status,$mlsImportItemStatusDelete)){ 861 $keep_on_delete='keep'; 862 update_post_meta($propertyId, 'mlsImportItemStatusDelete', $keep_on_delete); 863 } 864 865 866 867 868 869 $propertyHistory[] = date('F j, Y, g:i a') . ': We Inserted the property with Default title : ' . $submitTitle . ' and received id:' . $propertyId.'. The delete statuses are '.$keep_on_delete; 870 } 871 872 clean_post_cache( $propertyId ); 873 874 } elseif ($propertyId !== 0) { 875 876 $keep_on_delete='delete'; 877 if(!in_array ($status,$mlsImportItemStatusDelete)){ 878 $keep_on_delete='keep'; 879 update_post_meta($propertyId, 'mlsImportItemStatusDelete', $keep_on_delete); 880 } 881 882 $propertyHistory = $this->updateExistingProperty($propertyId,$mlsImportItemStatusDelete, $content, $listingPostType, $newAuthor, $status, $mlsImportItemStatus, $propertyHistory, $tipImport, $ListingKey); 883 } 884 885 if ($propertyId === 0) { 886 $this->writeImportLogs('ERROR property id is 0' . PHP_EOL, $tipImport); 887 return; 888 } 889 890 $newTitle = $this->processPropertyDetails($property, $propertyId, $tipImport, $propertyHistory, $newAgent, $itemIdArray,$isInsert); 891 892 $log = PHP_EOL . 'Ending on Property ' . $propertyId . ', ListingKey: ' . $ListingKey . ' , is insert? ' . $isInsert . ' with new title: ' . $newTitle . ' ' . PHP_EOL; 893 $this->writeImportLogs($log, $tipImport); 894 895 clean_post_cache( $propertyId ); 896 897 ob_end_clean(); 898 } 899 900 901 902 903 /** 904 * Check if the property should be inserted 905 * 906 * @param int $propertyId The property ID. 907 * @param string $status The property status. 908 * @param array $mlsImportItemStatus The MLS import item status. 909 * @param string $tipImport The import type. 910 * @return string 'yes' or 'no' indicating if the property should be inserted. 911 */ 912 private function shouldInsertProperty($propertyId, $status, $mlsImportItemStatus, $tipImport) { 913 if ($propertyId === 0) { 914 return 'yes'; 915 } 916 /*if ($propertyId === 0) { 917 if (in_array($status, ['active', 'active under contract', 'active with contract', 'activewithcontract', 'status', 'activeundercontract', 'comingsoon', 'coming soon', 'pending'])) { 918 if ($tipImport === 'cron' && !in_array($status, $mlsImportItemStatus)) { 919 return 'no'; 920 } 921 return 'yes'; 922 } 923 return 'no'; 924 } 925 return 'no'; 926 */ 927 } 928 929 /** 930 * Update existing property 931 * 932 * @param int $propertyId The property ID. 933 * @param string $content The post content. 934 * @param string $listingPostType The listing post type. 935 * @param int $newAuthor The new author ID. 936 * @param string $status The property status. 937 * @param array $mlsImportItemStatus The MLS import item status. 938 * @param array $propertyHistory The property history. 939 * @param string $tipImport The import type. 940 * @param string $ListingKey The listing key. 941 * @return array Updated property history. 942 */ 943 private function updateExistingProperty($propertyId,$mlsImportItemStatusDelete, $content, $listingPostType, $newAuthor, $status, $mlsImportItemStatus, &$propertyHistory, $tipImport, $ListingKey) { 691 944 692 if ( ! isset( $property['ListingKey'] ) ) { 693 $log = 'ERROR : No Listing Key ' . PHP_EOL; 694 mlsimport_saas_single_write_import_custom_logs( $log, $tip_import ); 695 return; 696 } 697 698 ob_start(); 699 700 $ListingKey = $property['ListingKey']; 701 $listing_post_type = $mlsimport->admin->env_data->get_property_post_type(); 702 $property_id = intval( $this->mlsimport_saas_retrive_property_by_id( $ListingKey, $listing_post_type ) ); 703 $status = 'Not Found'; 704 $property_history = array(); 705 if ( isset( $property['StandardStatus'] ) ) { 706 $status = strtolower( $property['StandardStatus'] ); 945 $mlsImportItemStatusDelete = array_map('strtolower', $mlsImportItemStatusDelete); 946 947 if (in_array($status, $mlsImportItemStatusDelete)) { 948 $log = 'Property with ID ' . $propertyId . ' and with name ' . get_the_title($propertyId) . ' has a status of <strong>' . $status . '</strong> and will be deleted' . PHP_EOL; 949 $this->deleteProperty($propertyId, $ListingKey); 950 $this->writeImportLogs($log, $tipImport); 707 951 } else { 708 $status = strtolower( $property['extra_meta']['MlsStatus'] ); 709 } 710 711 if ( 0 === intval( $property_id) ) { 712 $is_insert = 'no'; 713 if ( 714 'active' === $status || 'active under contract' === $status || 715 'active with contract' === $status || 'activewithcontract' === $status || 716 'status' === $status || 'activeundercontract' === $status || 717 'comingsoon' === $status || 'coming soon' === $status || 718 'pending' === $status 719 ) { 720 $is_insert = 'yes'; 721 if ( 'cron' === $tip_import ) { 722 if ( ! in_array( $status, $mls_import_item_status ) ) { 723 // REJECTED because not selected'; 724 $is_insert = 'no'; 725 } 952 $post = [ 953 'ID' => $propertyId, 954 'post_content' => $content, 955 'post_type' => $listingPostType, 956 'post_author' => $newAuthor, 957 ]; 958 959 $log = 'Property with ID ' . $propertyId . ' and with name ' . get_the_title($propertyId) . ' has a status of <strong>' . $status . '</strong> and will be Edited</br>'; 960 $this->writeImportLogs($log, $tipImport); 961 962 $propertyId = wp_update_post($post); 963 if (is_wp_error($propertyId)) { 964 $this->writeImportLogs('ERROR: on edit ' . PHP_EOL, $tipImport); 965 } else { 966 $submitTitle = get_the_title($propertyId); 967 $propertyHistory[] = gmdate('F j, Y, g:i a') . ': Property with title: ' . $submitTitle . ', id:' . $propertyId . ', ListingKey:' . $ListingKey . ', Status:' . $status . ' will be edited'; 968 } 969 clean_post_cache( $propertyId ); 970 } 971 972 return $propertyHistory; 973 } 974 975 /** 976 * Process property details 977 * 978 * @param array $property The property data. 979 * @param int $propertyId The property ID. 980 * @param string $tipImport The import type. 981 * @param array $propertyHistory The property history. 982 * @param int $newAgent The new agent ID. 983 * @param array $itemIdArray The item ID array. 984 * @param string $isInsert If is a property insert 985 */ 986 private function processPropertyDetails($property, $propertyId, $tipImport, &$propertyHistory, $newAgent, $itemIdArray, $isInsert) { 987 global $mlsimport; 988 $log = PHP_EOL . $this->mlsimportMemUsage() . '====before tax======' . PHP_EOL; 989 $this->writeImportLogs($log, $tipImport); 990 991 if (isset($property['taxonomies']) && is_array($property['taxonomies'])) { 992 remove_filter('get_term_metadata', 'lazyload_term_meta', 10); 993 wp_cache_delete('get_ancestors', 'taxonomy'); 994 995 $this->mlsimportSaasClearPropertyForTaxonomy($propertyId, $property['taxonomies']); 996 997 foreach ($property['taxonomies'] as $taxonomy => $term) { 998 wp_cache_delete("{$taxonomy}_term_counts", 'counts'); 999 $this->mlsimportSaasUpdateTaxonomyForProperty($taxonomy, $propertyId, $term); 1000 $propertyHistory[] = 'Updated Taxonomy ' . $taxonomy . ' with terms ' . wp_json_encode($term); 1001 } 1002 1003 add_filter('get_term_metadata', 'lazyload_term_meta', 10, 2); 1004 delete_option('category_children'); 1005 } 1006 1007 wp_cache_flush(); 1008 1009 $property = $this->mlsimportSaasPrepareMetaForProperty($property); 1010 1011 if (isset($property['meta']) && is_array($property['meta'])) { 1012 foreach ($property['meta'] as $metaName => $metaValue) { 1013 if (is_array($metaValue)) { 1014 $metaValue = implode(',', $metaValue); 726 1015 } 727 } 728 } else { 729 $is_insert = 'no'; 730 } 731 732 $log = $this->mlsimport_mem_usage() . '==========' . wp_json_encode( $mls_import_item_status ) . '/' . $new_author . '/' . $new_agent . '/' . $property_status . '/ We have property with $ListingKey=' . $ListingKey . ' id=' . $property_id . ' with status ' . $status . ' is insert? ' . $is_insert . PHP_EOL; 733 734 mlsimport_saas_single_write_import_custom_logs( $log, $tip_import ); 735 736 // content set and check 737 $content = ''; 738 $submit_title = $ListingKey; 739 if ( isset( $property['content'] ) ) { 740 $content = $property['content']; 741 } 742 743 // $new_author = get_post_meta($item_id_array['item_id'],'mlsimport_item_property_user',true ); 744 // $new_agent = esc_html(get_post_meta($item_id_array['item_id'], 'mlsimport_item_agent', true)); 745 // $property_status = esc_html(get_post_meta($item_id_array['item_id'], 'mlsimport_item_property_status', true)); 746 747 $mlsimport_item_option_data = null; 748 749 if ( 'yes' === $is_insert ) { 750 $post = array( 751 'post_title' => $submit_title, 752 'post_content' => $content, 753 'post_status' => $property_status, 754 'post_type' => $listing_post_type, 755 'post_author' => $new_author, 756 ); 757 758 $property_id = wp_insert_post( $post ); 759 760 if ( is_wp_error( $property_id ) ) { 761 $log = 'ERROR : on inserting ' . PHP_EOL; 762 mlsimport_saas_single_write_import_custom_logs( $log, $tip_import ); 763 } else { 764 update_post_meta( $property_id, 'ListingKey', $ListingKey ); 765 $property_history[] = date( 'F j, Y, g:i a' ) . ': We Inserted the property with Default title : ' . $submit_title . ' and received id:' . $property_id; 766 } 767 768 clean_post_cache( $property_id ); 769 } elseif ( 0 !== $property_id ) { 770 $property_history = array(); 771 $property_history[] = get_post_meta( $property_id, 'mlsimport_property_history', true ); 772 773 $delete_statuses = array( 774 'incomplete' => 'incomplete', 775 'hold' => 'hold', 776 'canceled' => 'canceled', 777 'closed' => 'closed', 778 'delete' => 'delete', 779 'expired' => 'expired', 780 'withdrawn' => 'withdrawn', 781 ); 782 783 if ( ! in_array( 'pending', $mls_import_item_status ) ) { 784 $delete_statuses['pending'] = 'pending'; 785 } 786 787 if ( in_array( $status, $delete_statuses ) ) { 788 $log = 'Property with ID ' . $property_id . ' and with name ' . get_the_title( $property_id ) . ' has a status of <strong>' . $status . '</strong> and will be deleted' . PHP_EOL; 789 $log = '--1---> ' . $log . wp_json_encode( $mls_import_item_status ) . PHP_EOL; 790 if ( in_array( $status, $mls_import_item_status ) ) { 791 $log .= '-2--> canceling the delete ' . PHP_EOL; 792 } else { 793 $log .= '--3--> proceed with the delete ' . PHP_EOL; 794 $this->delete_property( $property_id, $ListingKey ); 795 } 796 797 mlsimport_saas_single_write_import_custom_logs( $log, $tip_import ); 798 unset( $log ); 799 800 return; 801 } else { 802 803 $post = array( 804 'ID' => $property_id, 805 'post_content' => $content, 806 'post_type' => $listing_post_type, 807 'post_author' => $new_author, 808 ); 809 810 $log = ' Property with ID ' . $property_id . ' and with name ' . get_the_title( $property_id ) . ' has a status of <strong>' . $status . '</strong> and will be Edited</br>'; 811 mlsimport_saas_single_write_import_custom_logs( $log, $tip_import ); 812 813 $property_id = wp_update_post( $post ); 814 815 if ( is_wp_error( $property_id ) ) { 816 $log = 'ERROR : on edit ' . PHP_EOL; 817 mlsimport_saas_single_write_import_custom_logs( $log, $tip_import ); 818 } else { 819 $submit_title = get_the_title( $property_id ); 820 $property_history[] = gmdate( 'F j, Y, g:i a' ) . ': Property with title: ' . $submit_title . ', id:' . $property_id . ', ListingKey:' . $ListingKey . ', Status:' . $status . ' will be edited'; 821 } 822 823 clean_post_cache( $property_id ); 824 } 825 } 826 827 // 828 // Insert or edit POST ends her - START ADDING DETAILS 829 // 830 831 $encoded_values = array();// may be obsolote 832 833 if ( intval( $property_id ) === 0 ) { 834 mlsimport_saas_single_write_import_custom_logs( 'ERROR property id is 0' . PHP_EOL, $tip_import ); 835 return; // no point in going forward if no id 836 } 837 838 // Start working on Taxonomies 839 // 840 $log = PHP_EOL.$this->mlsimport_mem_usage() . '====before tax======'. PHP_EOL; 841 mlsimport_saas_single_write_import_custom_logs( $log, $tip_import ); 842 843 $tax_log = array_fill(0, 1, 'Property with ID ' . $property_id . ' NO taxonomies found!'); 844 845 if ( isset( $property['taxonomies'] ) && is_array( $property['taxonomies'] ) ) { 846 847 remove_filter('get_term_metadata', 'lazyload_term_meta', 10); 848 wp_cache_delete('get_ancestors', 'taxonomy'); 849 850 $this->mlsimport_saas_clear_property_for_taxonomy( $property_id, $property['taxonomies'] ); 851 852 853 foreach ( $property['taxonomies'] as $taxonomy => $term ) : 854 wp_cache_delete("{$taxonomy}_term_counts", 'counts'); 855 $this->mlsimport_saas_update_taxonomy_for_property( $taxonomy, $property_id, $term ); 856 $property_history[] = 'Updatedx Taxonomy ' . $taxonomy . ' with terms ' . wp_json_encode( $term ); 857 $tax_log [] = 'Memory:' . $this->mlsimport_mem_usage() . ' Property with ID ' . $property_id . ' Updated Taxonomy ' . $taxonomy . ' with terms ' . wp_json_encode( $term ); 858 859 endforeach; 860 861 862 unset($property['taxonomies'] ); 863 gc_collect_cycles(); 864 add_filter('get_term_metadata', 'lazyload_term_meta', 10, 2); 865 delete_option('category_children'); 866 } 867 868 869 870 // Only log if there are updates 871 if (!empty($tax_log)) { 872 $tax_log_str = implode(PHP_EOL, $tax_log); 873 mlsimport_saas_single_write_import_custom_logs($tax_log_str, $tip_import); 874 unset($tax_log_str); 875 } 876 wp_cache_flush(); 877 878 // Pre Meta jobs 879 // 880 $property = $this->mlsimport_saas_prepare_meta_for_property( $property ); 881 882 // Start working on Meta 883 // 884 885 $meta_log = array(); 886 $meta_log [] = 'Property with ID ' . $property_id . ' NO meta found ! ' . PHP_EOL; 887 888 if ( isset( $property['meta'] ) && is_array( $property['meta'] ) ) { 889 $meta_properties = $property['meta']; 890 foreach ( $meta_properties as $meta_name => $meta_value ) : 891 if ( is_array( $meta_value ) ) { 892 $meta_value = implode( ',', $meta_value ); 893 } 894 update_post_meta( $property_id, $meta_name, $meta_value ); 895 896 $property_history[] = 'Updated Meta ' . $meta_name . ' with meta_value ' . $meta_value; 897 $meta_log [] = 'Memory:' . $this->mlsimport_mem_usage() . 'Property with ID ' . $property_id . ' Updated Meta ' . $meta_name . ' with value ' . $meta_value; 898 endforeach; 899 } 900 $meta_properties = null; 901 $meta_log = implode( PHP_EOL, $meta_log ); 902 mlsimport_saas_single_write_import_custom_logs( $meta_log, $tip_import ); 903 $meta_log = null; 904 905 // Start working on EXTRA Meta 906 // 907 908 $extra_meta_log = 'Property with ID ' . $property_id . ' Start Extra meta ! ' . PHP_EOL; 909 $extra_meta_result = $mlsimport->admin->env_data->mlsimport_saas_set_extra_meta( $property_id, $property ); 910 911 if ( isset( $extra_meta_result['property_history'] ) ) { 912 $property_history = array_merge( $property_history, (array) $extra_meta_result['property_history'] ); 913 } 914 if ( isset( $extra_meta_result['extra_meta_log'] ) ) { 915 $extra_meta_log .= $extra_meta_result['extra_meta_log']; 916 } 917 918 mlsimport_saas_single_write_import_custom_logs( $extra_meta_log, $tip_import ); 919 920 $extra_meta_log = null; 921 $extra_meta_result = null; 922 923 // Start working on Property Media 924 // 925 926 $media = $property['Media']; 927 $media_history = $this->mlsimport_sass_attach_media_to_post( $property_id, $media, $is_insert ); 928 $property_history = array_merge( $property_history, (array) $media_history ); 929 $media = null; 930 $media_history = null; 931 932 // Updateing property title and ending 933 // 934 935 $new_title = $this->mlsimport_saas_update_property_title( $property_id, $item_id_array['item_id'], $property ); 936 $property_history[] = 'Updated title to ' . $new_title . '</br>'; 937 938 // extra fields to be checked 939 $global_extra_fields = array(); 940 $mlsimport->admin->env_data->correlation_update_after( $is_insert, $property_id, $global_extra_fields, $new_agent ); 941 942 // saving history 943 if ( ! empty( $property_history ) ) { 944 $disable_history = intval( get_option( 'mlsimport-disable-history', 1 ) ); 945 if ( 1 === intval( $disable_history ) ) { 946 $property_history[] = '---------------------------------------------------------------</br>'; 947 $property_history = implode( '</br>', $property_history ); 948 update_post_meta( $property_id, 'mlsimport_property_history', $property_history ); 949 } 950 } 951 952 $logs = PHP_EOL . 'Ending on Property ' . $property_id . ', ListingKey: ' . $ListingKey . ' , is insert? ' . $is_insert . ' with new title: ' . $new_title . ' ' . PHP_EOL; 953 mlsimport_saas_single_write_import_custom_logs( $logs, $tip_import ); 954 955 $capture = ob_get_contents(); 956 ob_end_clean(); 957 mlsimport_saas_single_write_import_custom_logs( $capture, $tip_import ); 958 959 $post = null; 960 $capture = null; 961 $property_status = null; 962 $new_agent = null; 963 $new_author = null; 964 $property_history = null; 965 $tax_log = null; 966 $meta_log = null; 967 $extra_meta_log = null; 968 $media_history = null; 969 $logs = null; 970 $capture = null; 971 clean_post_cache( $property_id ); 972 wp_cache_flush(); 973 gc_collect_cycles(); 974 } 975 976 977 978 979 980 981 982 public function mlsimport_mem_usage() { 983 $mem_usage = memory_get_usage( true ); 984 $mem_usage_show = round( $mem_usage / 1048576, 2 ); 985 return $mem_usage_show . 'mb '; 986 } 987 988 989 /** 990 * prepare meta data 991 * 992 * @since 1.0.0 993 * @access protected 994 * @var string $plugin_name 995 */ 996 public function mlsimport_saas_prepare_meta_for_property( $property ) { 997 998 if ( isset( $property['extra_meta']['BathroomsTotalDecimal'] ) && floatval( $property['extra_meta']['BathroomsTotalDecimal'] ) > 0 ) { 999 $property['meta']['property_bathrooms'] = floatval( $property['extra_meta']['BathroomsTotalDecimal'] ); 1000 $property['meta']['fave_property_bathrooms'] = floatval( $property['extra_meta']['BathroomsTotalDecimal'] ); 1001 $property['meta']['REAL_HOMES_property_bathrooms'] = floatval( $property['extra_meta']['BathroomsTotalDecimal'] ); 1002 } 1003 return $property; 1004 } 1005 1006 1007 1008 1009 1010 /** 1011 * attach media to post 1012 * 1013 * @since 1.0.0 1014 * @access protected 1015 * @var string $plugin_name 1016 */ 1017 public function mlsimport_sass_attach_media_to_post( $property_id, $media, $is_insert ) { 1018 1019 1020 $media_history = array(); 1021 if ( 'no' === $is_insert ) { 1022 $media_history[] = ' Media - We have edit - images are not replaced'; 1023 return $media_history; 1024 } 1025 1026 global $mlsimport; 1027 include_once ABSPATH . 'wp-admin/includes/image.php'; 1028 $has_featured = false; 1029 $all_images = array(); 1030 1031 delete_post_meta( $property_id, 'fave_property_images' ); 1032 delete_post_meta( $property_id, 'REAL_HOMES_property_images' ); 1033 1034 add_filter( 'intermediate_image_sizes_advanced', array( $this, 'wpc_unset_imagesizes' ) ); 1035 1036 // sorting media 1037 if ( isset( $media[0]['Order'] ) ) { 1038 $order = array_column( $media, 'Order' ); 1039 array_multisort( $order, SORT_ASC, $media ); 1040 } 1041 1042 if ( is_array( $media ) ) { 1043 foreach ( $media as $key => $image ) : 1044 if ( isset( $image['MediaCategory'] ) && 'Photo' !== $image['MediaCategory'] ) { 1045 continue; 1046 } 1047 1048 $file = $image['MediaURL']; 1049 1050 $media_url = ''; 1051 if ( isset( $image['MediaURL'] ) ) { 1052 $attachment = array( 1053 'guid' => $image['MediaURL'], 1054 'post_status' => 'inherit', 1055 'post_content' => '', 1056 'post_parent' => $property_id, 1057 ); 1058 1059 if ( isset( $image['MimeType'] ) ) { 1060 $attachment['post_mime_type'] = $image['MimeType']; 1061 } else { 1062 $attachment['post_mime_type'] = 'image/jpg'; 1063 } 1064 1065 if ( isset( $image['MediaKey'] ) ) { 1066 $attachment['post_title'] = $image['MediaKey']; 1067 } else { 1068 $attachment['post_title'] = ''; 1069 } 1070 1071 $attach_id = wp_insert_attachment( $attachment, $file ); 1072 1073 $media_history[] = ' Media - Added ' . $image['MediaURL'] . ' as attachement ' . $attach_id; 1074 // wp_generate_attachment_metadata($attach_id,$image['MediaURL']); 1075 $mlsimport->admin->env_data->enviroment_image_save( $property_id, $attach_id ); 1076 1077 update_post_meta( $attach_id, 'is_mlsimport', 1 ); 1078 if ( ! $has_featured ) { 1079 set_post_thumbnail( $property_id, $attach_id ); 1080 $has_featured = true; 1081 } 1082 } 1083 endforeach; 1084 } else { 1085 $media_history[] = ' Media data is blank - there are no images'; 1086 } 1087 remove_filter( 'intermediate_image_sizes_advanced', array( $this, 'wpc_unset_imagesizes' ) ); 1088 1089 $media_history = implode( '</br>', $media_history ); 1090 return $media_history; 1091 } 1092 1093 1094 1095 function wpc_unset_imagesizes( $sizes ) { 1096 $sizes = array(); 1097 } 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 /** 1109 * return user option 1110 * 1111 * @since 1.0.0 1112 * @access protected 1113 * @var string $plugin_name 1114 */ 1115 public function mlsimport_saas_theme_import_select_user( $selected ) { 1116 $blog_list = ''; 1117 $blogusers = get_users( 'blog_id=1&orderby=nicename' ); 1118 foreach ( $blogusers as $user ) { 1119 $the_id = $user->ID; 1120 $blog_list .= '<option value="' . $the_id . '" '; 1121 if ( $the_id === intval($selected) ) { 1122 $blog_list .= ' selected="selected" '; 1123 } 1124 $blog_list .= '>' . $user->user_login . '</option>'; 1125 } 1126 return $blog_list; 1127 } 1128 1129 1130 1131 1132 1133 1134 1135 1136 /** 1137 * return agent option 1138 * 1139 * @since 1.0.0 1140 * @access protected 1141 * @var string $plugin_name 1142 */ 1143 public function mlsimport_saas_theme_import_select_agent( $selected ) { 1144 global $mlsimport; 1145 $args2 = array( 1146 'post_type' => $mlsimport->admin->env_data->get_agent_post_type(), 1147 'post_status' => 'publish', 1148 'posts_per_page' => 150, 1149 ); 1150 1151 if ( method_exists( $mlsimport, 'get_agent_post_type' ) ) { 1152 $args2['post_type'] = $mlsimport->admin->env_data->get_agent_post_type(); 1153 } 1154 1155 $agent_selection2 = new WP_Query( $args2 ); 1156 $agent_list_sec = '<option value=""><option>'; 1157 1158 while ( $agent_selection2->have_posts() ) { 1159 $agent_selection2->the_post(); 1160 $the_id = get_the_ID(); 1161 1162 $agent_list_sec .= '<option value="' . $the_id . '" '; 1163 if ( intval($selected) === $the_id ) { 1164 $agent_list_sec .= ' selected="selected" '; 1165 } 1166 $agent_list_sec .= '>' . get_the_title() . '</option>'; 1167 } 1168 wp_reset_postdata(); 1169 1170 return $agent_list_sec; 1171 } 1172 1173 1174 /** 1175 * delete property 1176 * 1177 * @since 1.0.0 1178 * @access protected 1179 * @var string $plugin_name 1180 */ 1181 public function delete_property( $delete_id, $ListingKey ) { 1182 if ( intval( $delete_id ) > 0 ) { 1183 $arguments = array( 1184 'numberposts' => -1, 1185 'post_type' => 'attachment', 1186 'post_parent' => $delete_id, 1187 'post_status' => null, 1188 'orderby' => 'menu_order', 1189 'order' => 'ASC', 1190 ); 1191 $post_attachments = get_posts( $arguments ); 1192 1193 foreach ( $post_attachments as $attachment ) { 1194 wp_delete_post( $attachment->ID ); 1195 } 1196 1197 wp_delete_post( $delete_id ); 1198 $log_entry = ' Property with id ' . $delete_id . ' and ' . $ListingKey . ' was deleted on ' . current_time( 'Y-m-d\TH:i' ) . PHP_EOL; 1199 mlsimport_saas_single_write_import_custom_logs( $log_entry, 'delete' ); 1200 } 1201 } 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 /** 1214 * return_array with title items 1215 * 1216 * @since 1.0.0 1217 * @access protected 1218 * @var string $plugin_name 1219 */ 1220 public function str_between_all( string $string, string $start, string $end, bool $includeDelimiters = false, int &$offset = 0 ) { 1221 $strings = array(); 1222 $length = strlen( $string ); 1223 1224 while ( $offset < $length ) { 1225 $found = $this->str_between( $string, $start, $end, $includeDelimiters, $offset ); 1226 if ( null === $found ) { 1227 break; 1228 } 1229 1230 $strings[] = $found; 1231 $offset += strlen( $includeDelimiters ? $found : $start . $found . $end ); // move offset to the end of the newfound string 1232 } 1233 1234 return $strings; 1235 } 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 /** 1246 * str_between 1247 * 1248 * @since 1.0.0 1249 * @access protected 1250 * @var string $plugin_name 1251 */ 1252 public function str_between( string $string, string $start, string $end, bool $includeDelimiters = false, int &$offset = 0 ) { 1253 if ('' === $string || '' === $start || '' === $end ) { 1254 return null; 1255 } 1256 1257 $startLength = strlen( $start ); 1258 $endLength = strlen( $end ); 1259 1260 $startPos = strpos( $string, $start, $offset ); 1261 if ( false === $startPos ) { 1262 return null; 1263 } 1264 1265 $endPos = strpos( $string, $end, $startPos + $startLength ); 1266 if ( false === $endPos) { 1267 return null; 1268 } 1269 1270 $length = $endPos - $startPos + ( $includeDelimiters ? $endLength : -$startLength ); 1271 if ( ! $length ) { 1272 return ''; 1273 } 1274 1275 $offset = $startPos + ( $includeDelimiters ? 0 : $startLength ); 1276 1277 $result = substr( $string, $offset, $length ); 1278 1279 return ( false !== $result ? $result : null ); 1280 } 1281 1282 1283 1284 1285 1286 1287 1288 1289 /** 1290 * delete property via sql 1291 * 1292 * @since 1.0.0 1293 * @access protected 1294 * @var string $plugin_name 1295 */ 1296 public function mlsimport_saas_delete_property_via_mysql( $delete_id, $ListingKey ) { 1297 1298 $post_type = get_post_type( $delete_id ); 1299 1300 if ( 'estate_property' === $post_type || 'property' === $post_type ) { 1301 $term_obj_list = get_the_terms( $delete_id, 'property_status' ); 1302 $delete_id_status = join( ', ', wp_list_pluck( $term_obj_list, 'name' ) ); 1303 1304 $ListingKey = get_post_meta( $delete_id, 'ListingKey', true ); 1305 if ( '' === $ListingKey ) { // manual added listing 1306 $log_entry = 'User added listing with id ' . $delete_id . ' (' . $post_type . ') (status ' . $delete_id_status . ') and ' . $ListingKey . ' NOT DELETED' . PHP_EOL; 1307 mlsimport_saas_single_write_import_custom_logs( $log_entry, 'delete' ); 1308 return; 1309 } 1310 1311 global $wpdb; 1312 $wpdb->query( 1313 $wpdb->prepare( 1314 " 1315 DELETE FROM $wpdb->postmeta 1316 WHERE `post_id` = %d", 1317 $delete_id 1318 ) 1319 ); 1320 1321 $wpdb->query( 1322 $wpdb->prepare( 1323 " 1324 DELETE FROM $wpdb->posts 1325 WHERE `post_parent` = %d", 1326 $delete_id 1327 ) 1328 ); 1329 1330 $wpdb->query( 1331 $wpdb->prepare( 1332 " 1333 DELETE FROM $wpdb->posts 1334 WHERE ID = %d", 1335 $delete_id 1336 ) 1337 ); 1338 1339 $log_entry = 'MYSQL DELETE -> Property with id ' . $delete_id . ' (' . $post_type . ') (status ' . $delete_id_status . ') and ' . $ListingKey . ' was deleted on ' . current_time( 'Y-m-d\TH:i' ) . PHP_EOL; 1340 mlsimport_saas_single_write_import_custom_logs( $log_entry, 'delete' ); 1341 } 1342 } 1016 update_post_meta($propertyId, $metaName, $metaValue); 1017 $propertyHistory[] = 'Updated Meta ' . $metaName . ' with meta_value ' . $metaValue; 1018 } 1019 } 1020 1021 $extraMetaResult = $mlsimport->admin->env_data->mlsimportSaasSetExtraMeta($propertyId, $property); 1022 if (isset($extraMetaResult['property_history'])) { 1023 $propertyHistory = array_merge($propertyHistory, (array)$extraMetaResult['property_history']); 1024 } 1025 1026 $mediaHistory = $this->mlsimportSassAttachMediaToPost($propertyId, $property['Media'], $isInsert); 1027 $propertyHistory = array_merge($propertyHistory, (array)$mediaHistory); 1028 1029 $newTitle = $this->mlsimportSaasUpdatePropertyTitle($propertyId, $itemIdArray['item_id'], $property); 1030 $propertyHistory[] = 'Updated title to ' . $newTitle . '</br>'; 1031 1032 $mlsimport->admin->env_data->correlationUpdateAfter($isInsert, $propertyId, [], $newAgent); 1033 1034 if (!empty($propertyHistory)) { 1035 if (intval(get_option('mlsimport-disable-history', 1)) === 1) { 1036 $propertyHistory[] = '---------------------------------------------------------------</br>'; 1037 $propertyHistory = implode('</br>', $propertyHistory); 1038 update_post_meta($propertyId, 'mlsimport_property_history', $propertyHistory); 1039 } 1040 } 1041 1042 return $newTitle; 1043 } 1044 1045 1046 1047 1343 1048 } -
mlsimport/trunk/includes/help_functions.php
r3075646 r3134790 110 110 $values = array(); 111 111 112 $answer = $theme_Start::global _api_request_saas( 'mls', $values, 'GET' );112 $answer = $theme_Start::globalApiRequestSaas( 'mls', $values, 'GET' ); 113 113 114 114 if ( isset( $answer['succes'] ) && true === $answer['succes'] ) { -
mlsimport/trunk/mlsimport.php
r3093628 r3134790 4 4 * Plugin URI: https://mlsimport.com/ 5 5 * Description: "MLS Import - The MLSImport plugin facilitates the connection to your real estate MLS database, allowing you to download and synchronize real estate property data from the MLS. 6 * Version: 5. 7.46 * Version: 5.8.1 7 7 * Requires at least: 5.2 8 8 * Requires PHP: 7.2 … … 21 21 22 22 23 define( 'MLSIMPORT_VERSION', '5. 7.4' );23 define( 'MLSIMPORT_VERSION', '5.8.1' ); 24 24 define( 'MLSIMPORT_CLUBLINK', 'mlsimport.com' ); 25 25 define( 'MLSIMPORT_CLUBLINKSSL', 'https' );
Note: See TracChangeset
for help on using the changeset viewer.