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

class-mlsimport-admin.php in MLSImport: IDX Plugin & MLS Plugin for Real Estate Listings 5.8.4, at admin/class-mlsimport-admin.php

2,398 lines 75.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) {
3 exit; // Exit if accessed directly
4 }
5
6
7 /**
8 * The admin-specific functionality of the plugin.
9 *
10 * @link http://mlsimport.com/
11 * @since 1.0.0
12 *
13 * @package Mlsimport
14 * @subpackage Mlsimport/admin
15 */
16
17
18 /**
19 * The admin-specific functionality of the plugin.
20 *
21 * Defines the plugin name, version, and two examples hooks for how to
22 * enqueue the admin-specific stylesheet and JavaScript.
23 *
24 * @package Mlsimport
25 * @subpackage Mlsimport/admin
26 * @author MlsImport <office@mlsimport.com>
27 */
28 class Mlsimport_Admin {
29
30 /**
31 * The ID of this plugin.
32 *
33 * @since 1.0.0
34 * @access private
35 * @var string $plugin_name The ID of this plugin.
36 */
37 private $plugin_name;
38
39 /**
40 * The version of this plugin.
41 *
42 * @since 1.0.0
43 * @access private
44 * @var string $version The current version of this plugin.
45 */
46 private $version;
47 public $main;
48 public $theme_importer;
49 public $env_data;
50 public $mls_env_data;
51 protected $process_all;
52 public $field_import;
53 public $themes;
54 /**
55 * Initialize the class and set its properties.
56 *
57 * @since 1.0.0
58 * @param string $plugin_name The name of this plugin.
59 * @param string $version The version of this plugin.
60 */
61 public function __construct( $plugin_name, $version ) {
62
63 $this->plugin_name = $plugin_name;
64 $this->version = $version;
65
66 $this->field_import = array(
67 'City',
68 'CountyOrParish',
69 'MlsStatus',
70 'PropertySubType',
71 'PropertyType',
72 'StandardStatus',
73 'InternetEntireListingDisplayYN',
74 'InternetAddressDisplayYN',
75 );
76
77 $this->themes = array(
78 991 => 'WpResidence',
79 992 => 'Houzez',
80 993 => 'RealHomes',
81 994 => 'Wpestate',
82 );
83 }
84 /**
85 *
86 *
87 *
88 * Admin Setup
89 *
90 * @since 1.0.0
91 */
92 public function admin_setup( $plugin_name, $mls_enviroment, $theme_enviroment ) {
93
94 $options = get_option( $this->plugin_name . '_admin_options' );
95 $theme_id = 0;
96 if ( isset( $options['mlsimport_theme_used'] ) ) {
97 $theme_id = intval( $options['mlsimport_theme_used'] );
98 }
99 $themes = $this->themes;
100
101 $theme_enviroment = '';
102 if ( isset( $themes[ $theme_id ] ) ) {
103 $theme_enviroment = $themes[ $theme_id ];
104 }
105
106 $this->theme_importer = new ThemeImport( $plugin_name );
107
108 $options_api = get_option( $this->plugin_name . '_admin_options' );
109
110 if ( '' !== $theme_enviroment ) {
111 $classname = str_replace('Wp','',$theme_enviroment ). 'Class';
112 $this->env_data = new $classname();
113 } else {
114 $this->env_data = new stdClass();
115 }
116
117 if ( '' !== $mls_enviroment ) {
118 $mls_classname = $mls_enviroment . 'Class';
119
120 $this->mls_env_data = new $mls_classname( $this->theme_importer );
121 } else {
122 $this->mls_env_data = new stdClass();
123 }
124 }
125
126 /**
127 *
128 *
129 *
130 * Register the stylesheets for the admin area.
131 *
132 * @since 1.0.0
133 */
134 public function enqueue_styles() {
135 wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/mlsimport-admin.css', array(), $this->version, 'all' );
136 }
137
138
139
140
141 /**
142 *
143 *
144 *
145 * Register the JavaScript for the admin area.
146 *
147 * @since 1.0.0
148 */
149 public function enqueue_scripts($hook_suffix) {
150 wp_enqueue_script( 'jquery-ui-autocomplete' );
151 $mls_import_list = mlsimport_saas_request_list();
152 wp_enqueue_script( 'mlsimport-admin', plugin_dir_url( __FILE__ ) . 'js/mlsimport-admin.js', array( 'jquery' ), $this->version, true );
153 wp_localize_script(
154 'mlsimport-admin',
155 'mlsimport_vars',
156 array(
157 'ajax_url' => admin_url( 'admin-ajax.php' )
158 )
159 );
160
161 if ('toplevel_page_mlsimport_plugin_options' === $hook_suffix &&
162 isset($_GET['page']) && $_GET['page'] === 'mlsimport_plugin_options' &&
163 isset($_GET['tab']) && $_GET['tab'] === 'field_options') {
164 $mlsimport_mls_metadata_populated = get_option( 'mlsimport_mls_metadata_populated', '' );
165 if ( 'yes' !== $mlsimport_mls_metadata_populated ) {
166 $inline_script = 'jQuery(document).ready(function($){ mlsimport_saas_get_metadata(); });';
167 wp_add_inline_script('mlsimport-admin', $inline_script);
168 }
169 }
170
171 if ('toplevel_page_mlsimport_plugin_options' === $hook_suffix &&
172 ( isset($_GET['page']) && $_GET['page'] === 'mlsimport_plugin_options' && isset($_GET['tab']) && $_GET['tab'] === 'display_options') ||
173 (isset($_GET['page']) && $_GET['page'] === 'mlsimport_plugin_options' && !isset($_GET['tab']) ) ) {
174
175 $mls_import_list = mlsimport_saas_request_list();
176 $inline_script = 'jQuery(document).ready(function($){ var autofill='.wp_kses_post($mls_import_list).';mlsimport_autocomplte_mls_selection(autofill); });';
177 wp_add_inline_script('mlsimport-admin', $inline_script);
178 }
179
180 }
181
182
183
184
185
186 /**
187 *
188 *
189 *
190 * Register the administration menu for this plugin into the WordPress Dashboard menu.
191 *
192 * @since 1.0.0
193 */
194 public function add_plugin_admin_menu() {
195 add_menu_page(
196 esc_html__( 'MLS Import Settings', 'mlsimport'),
197 esc_html__( 'MLS Import Settings', 'mlsimport' ),
198 'administrator',
199 'mlsimport_plugin_options',
200 array( $this, 'display_plugin_setup_page' ),
201 MLSIMPORT_PLUGIN_URL . '/img/mlsimport_menu.png',
202 22
203 );
204 }
205
206
207
208
209
210
211
212
213 /**
214 *
215 *
216 *
217 * Add settings action link to the plugins page.
218 *
219 * @since 1.0.0
220 */
221 public function add_action_links( $links ) {
222 $settings_link = array(
223 '<a href="' . admin_url( 'admin.php?page=mlsimport_plugin_options' ) . '">' . esc_html__( 'Settings', 'mlsimport') . '</a>',
224 );
225 return array_merge( $settings_link, $links );
226 }
227
228
229
230
231
232
233
234
235 /**
236 *
237 *
238 * Render the settings page for this plugin.
239 *
240 * @since 1.0.0
241 */
242 public function display_plugin_setup_page() {
243 include_once 'partials/' . $this->plugin_name . '-admin-display.php';
244 }
245
246
247
248
249
250
251 /**
252 *
253 *
254 * Validate plugin options fields
255 *
256 * @since 1.0.0
257 */
258 public function validate_admin_options( $input ) {
259
260 $valid = array();
261 $settings_list = array(
262 'auth_username' => array(
263 'name' => esc_html__( 'Api auth_username ', 'mlsimport' ),
264 'details' => 'to be added',
265 ),
266 'auth_password' => array(
267 'name' => esc_html__( 'Api auth_password', 'mlsimport' ),
268 'details' => 'to be added',
269 ),
270 'client_id' => array(
271 'name' => esc_html__( 'Api client_id', 'mlsimport' ),
272 'details' => 'to be added',
273 ),
274 'client_secret' => array(
275 'name' => esc_html__( 'client_secret', 'mlsimport' ),
276 'details' => 'to be added',
277 ),
278 'redirect_uri' => array(
279 'name' => esc_html__( 'redirect_uri', 'mlsimport' ),
280 'details' => 'to be added',
281 ),
282 'title_format' => array(
283 'name' => esc_html__( 'title_format', 'mlsimport' ),
284 'details' => 'to be added',
285 ),
286 'force_rand' => array(
287 'name' => esc_html__( 'title_format', 'mlsimport' ),
288 'details' => 'to be added',
289 ),
290 'mlsimport_username' => array(
291 'name' => esc_html__( 'MLSImport Username', 'mlsimport' ),
292 'details' => 'to be added',
293 ),
294 'mlsimport_password' => array(
295 'name' => esc_html__( 'MLSImport Password', 'mlsimport' ),
296 'details' => 'to be added',
297 ),
298 'mlsimport_mls_name' => array(
299 'name' => esc_html__( 'MLSImport Name', 'mlsimport' ),
300 'details' => 'to be added',
301 ),
302 'mlsimport_mls_token' => array(
303 'name' => esc_html__( 'MLSImport Token', 'mlsimport' ),
304 'details' => 'to be added',
305 ),
306
307 'mlsimport_tresle_client_id' => array(
308 'name' => esc_html__( 'MLSImport Tresle Client id', 'mlsimport' ),
309 'details' => 'to be added',
310 ),
311
312 'mlsimport_tresle_client_secret' => array(
313 'name' => esc_html__( 'MLSImport Client Secret', 'mlsimport' ),
314 'details' => 'to be added',
315 ),
316
317 'mlsimport_rapattoni_client_id' => array(
318 'name' => esc_html__( 'MLSImport Rapattoni Client id','mlsimport'),
319 'details' => 'to be added',
320 ),
321
322 'mlsimport_rapattoni_client_secret' => array(
323 'name' => esc_html__( 'MLSImport Rapattoni Secret', 'mlsimport' ),
324 'details' => 'to be added',
325 ),
326
327 'mlsimport_rapattoni_username' => array(
328 'name' => esc_html__( 'MLSImport Rapattoni Username', 'mlsimport' ),
329 'details' => 'to be added',
330 ),
331
332 'mlsimport_rapattoni_password' => array(
333 'name' => esc_html__( 'MLSImport Rapattoni Password', 'mlsimport' ),
334 'details' => 'to be added',
335 ),
336
337 'mlsimport_paragon_client_id' => array(
338 'name' => esc_html__( 'MLSImport Paragon Client id','mlsimport' ),
339 'details' => 'to be added',
340 ),
341
342 'mlsimport_paragon_client_secret' => array(
343 'name' => esc_html__( 'MLSImport Paragon Secret', 'mlsimport' ),
344 'details' => 'to be added',
345 ),
346
347 'mlsimport_theme_used' => array(
348 'name' => esc_html__( 'Your Wordpress Theme', 'mlsimport' ),
349 'details' => 'to be added',
350 ),
351 'mlsimport_mls_name_front' => array(
352 'name' => '',
353 'details' => 'to be added',
354 ),
355 'mlsimport-disable-logs' => array(
356 'name' => '',
357 'details' => 'to be added',
358 ),
359 );
360
361 foreach ( $settings_list as $key => $setting ) {
362 $valid[ $key ] = ( isset( $input[ $key ] ) && ! empty( $input[ $key ] ) ) ? esc_attr( $input[ $key ] ) : '';
363 }
364
365 delete_option( 'mlsimport_connection_test' );
366 delete_option( 'mlsimport_mls_metadata_populated' );
367
368 update_option( 'mlsimport_encoding_array', '' );
369 delete_transient( 'mlsimport_token_request' );
370 delete_transient( 'mlsimport_schema' );
371 delete_transient( 'mlsimport_plugin_data_schema' );
372
373 delete_transient( 'mlsimport_saas_token' );
374 return $valid;
375 }
376
377
378
379
380
381 /**
382 *
383 *
384 * Validate admin fields
385 *
386 * @since 1.0.0
387 */
388 public function validate_admin_fields_select( $input ) {
389 $valid = array();
390
391 $mlsimport_mls_metadata_mls_data = get_option( 'mlsimport_mls_metadata_mls_data', '' );
392 $metadata_api_call = json_decode( $mlsimport_mls_metadata_mls_data, true );
393
394 foreach ( $metadata_api_call as $key => $value ) {
395 if ( isset( $input['mls-fields'][ $key ] ) ) {
396 $valid['mls-fields'][ $key ] = esc_attr( $input['mls-fields'][ $key ] );
397 }
398
399 if ( isset( $input['mls-fields-admin'][ $key ] ) ) {
400 $valid['mls-fields-admin'][ $key ] = esc_attr( $input['mls-fields-admin'][ $key ] );
401 $valid['mls-fields-label'][ $key ] = esc_attr( $input['mls-fields-label'][ $key ] );
402 $valid['mls-fields-map-postmeta'][ $key ] = esc_attr( $input['mls-fields-map-postmeta'][ $key ] );
403 $valid['mls-fields-map-taxonomy'][ $key ] = esc_attr( $input['mls-fields-map-taxonomy'][ $key ] );
404
405 }
406 }
407 $valid['mls-fields-admin']['force_rand'] = esc_attr( $input['mls-fields-admin']['force_rand'] );
408 return $valid;
409 }
410
411 /**
412 *
413 *
414 * Validate Mls Sync fields
415 *
416 * @since 1.0.0
417 */
418 public function validate_admin_mls_sync( $input ) {
419 $valid = array();
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',
422 'StandardStatus_delete', 'StandardStatus_delete_check', 'InternetEntireListingDisplayYN', 'InternetAddressDisplayYN' );
423 foreach ( $field_import as $key ) {
424 $valid[ $key ] = $input[ $key ];
425 }
426
427 return $valid;
428 }
429
430
431 /**
432 *
433 *
434 * Validate Administrative options
435 *
436 * @since 1.0.0
437 */
438 public function validate_administrative_options( $input ) {
439
440 $valid = array();
441
442 $field_import = array( 'import' );
443 foreach ( $field_import as $key ) {
444 $valid[ $key ] = $input[ $key ];
445 }
446
447 return $valid;
448 }
449
450 /**
451 *
452 *
453 *
454 * Validate Import Options fields
455 *
456 * @since 1.0.0
457 */
458 public function validate_admin_import_options( $input ) {
459 $valid = array();
460
461 $field_import = array( 'import_number' );
462 foreach ( $field_import as $key ) {
463 $valid[ $key ] = intval( $input[ $key ] );
464 }
465
466 if ( isset( $input['import'] ) && '' !== $input['import'] ) {
467 $decode = json_decode( $input['import'] );
468 update_option( 'mlsimport_admin_fields_select', $decode['mlsimport_admin_fields_select'] );
469 update_option( 'mlsimport_admin_mls_sync', $decode['mlsimport_admin_mls_sync'] );
470 update_option( 'mlsimport_admin_import_options', $decode['mlsimport_admin_import_options'] );
471 update_option( 'mlsimport_admin_use_transients', $decode['mlsimport_admin_use_transients'] );
472 }
473
474 return $valid;
475 }
476
477
478
479
480
481
482 /**
483 *
484 *
485 * plugin options update
486 */
487 public function options_update() {
488 register_setting( $this->plugin_name . '_admin_options', $this->plugin_name . '_admin_options', array( $this, 'validate_admin_options' ) );
489 register_setting( $this->plugin_name . '_admin_fields_select', $this->plugin_name . '_admin_fields_select', array( $this, 'validate_admin_fields_select' ) );
490 register_setting( $this->plugin_name . '_admin_mls_sync', $this->plugin_name . '_admin_mls_sync', array( $this, 'validate_admin_mls_sync' ) );
491 register_setting( $this->plugin_name . '_admin_import_options', $this->plugin_name . '_admin_import_options', array( $this, 'validate_admin_import_options' ) );
492 register_setting( $this->plugin_name . '_administrative_options', $this->plugin_name . '_administrative_options', array( $this, 'validate_administrative_options' ) );
493 }
494
495
496
497 /**
498 *
499 *
500 *
501 *
502 *
503 *
504 *
505 */
506 public function update_option_mlsimport_administrative_options() {
507 $import = get_option( 'mlsimport_administrative_options' );
508 if ( '' !== $import ) {
509 $decode = json_decode( $import['import'], true );
510 update_option( 'mlsimport_admin_fields_select', $decode['mlsimport_admin_fields_select'] );
511 update_option( 'mlsimport_admin_mls_sync', $decode['mlsimport_admin_mls_sync'] );
512 update_option( 'mlsimport_admin_import_options', $decode['mlsimport_admin_import_options'] );
513 }
514 }
515
516 /**
517 *
518 *
519 * plugin options update
520 */
521 public function update_option_mlsimport_admin_fields_select() {
522
523 $this->env_data->enviroment_custom_fields( $this->plugin_name );
524 }
525
526
527 /**
528 *
529 *
530 * plugin options update
531 */
532 public function mlsimport_meta_options() {
533 if ( method_exists( $this->env_data, 'get_property_post_type' ) ) {
534 add_meta_box( 'mlsimport_hidden_fields', esc_html__( 'Mls Import Hidden Fields', 'mlsimport' ), array( $this, 'mlsimport_hidden_fields' ), $this->env_data->get_property_post_type(), 'normal', 'low' );
535 }
536 }
537
538 /**
539 *
540 *
541 * plugin options update
542 */
543 public function mlsimport_hidden_fields() {
544 global $post;
545
546 $options = get_option( $this->plugin_name . '_admin_fields_select' );
547
548 $MLSimport_item_inserted = get_post_meta( $post->ID, 'MLSimport_item_inserted', true );
549 $MLSimport_item_updated = get_post_meta( $post->ID, 'MLSimport_item_updated', true );
550 $listing_key = get_post_meta( $post->ID, 'ListingKey', true );
551 $mlsImportItemStatusDelete = get_post_meta($post->ID, 'mlsImportItemStatusDelete', true);
552
553
554
555 // Check if the ListingKey exists
556 if ( !empty( $listing_key ) ) {
557 echo 'ListingKey: ' . $listing_key . '<br>';
558 }
559
560 // Check if MLSimport_item_inserted exists
561 if ( !empty( $MLSimport_item_inserted ) ) {
562 echo 'Added via MLS item id: ' . $MLSimport_item_inserted . ' - ' . get_the_title( $MLSimport_item_inserted ) . '<br>';
563 }
564
565 // Check if MLSimport_item_updated exists
566 if ( !empty( $MLSimport_item_updated ) ) {
567 echo 'Updated via MLS item id: ' . $MLSimport_item_updated . ' - ' . get_the_title( $MLSimport_item_updated ) . '<br>';
568 }
569
570
571 if(!empty($mlsImportItemStatusDelete)) {
572 if(is_array($mlsImportItemStatusDelete)) {
573
574 echo 'Do not delete if status: ' . implode(',' ,$mlsImportItemStatusDelete) . '<br>';
575 } else {
576
577 echo 'Do not delete if status: ' . esc_html($mlsImportItemStatusDelete) . '<br>';
578
579 }
580
581 }
582
583 foreach ( $options['mls-fields-admin'] as $key => $value ) {
584 if ( 1 === intval($options['mls-fields-admin'][ $key ] ) ) {
585 if ( isset( $options['mls-fields-label'][ $key ] ) && '' !== $options['mls-fields-label'][ $key ] ) {
586 $key = $options['mls-fields-label'][ $key ];
587 }
588
589 if ( 'ListingKey' !== $key ) {
590 $meta_key = strtolower( $key );
591 } else {
592 $meta_key = $key;
593 }
594 ?>
595
596 <strong><?php echo esc_html($key);?>:</strong>
597 <?php echo esc_html( get_post_meta( $post->ID, $meta_key, true ) ); ?> </br>
598 <?php
599 }
600 }
601 ?>
602
603 <h2 style="font-weight:bold;padding-left:0px;">Mls Import History</h2>
604 <?php
605 $meta = get_post_meta( $post->ID, 'mlsimport_property_history', true );
606 if ( '' === trim( $meta ) ) { ?>
607 <strong>Property history is blank - you can enable it in Settings/ Tools page </strong>
608 <?php
609 } else {
610 print wp_kses_post($meta);
611 }
612 }
613
614
615
616
617 /**
618 * delete cache
619 */
620 function mlsimport_delete_cache() {
621
622 check_ajax_referer( 'mlsimport_tool_actions', 'security' );
623
624 delete_transient( 'mlsimport_token_request' );
625 delete_transient( 'mlsimport_metadata_api_call_data_service_property' );
626 delete_transient( 'mls_import_meta_enums' );
627 delete_transient( 'mls_import_meta' );
628 delete_transient( 'mlsimport_plugin_data_schema' );
629 delete_transient( 'mlsimport_ready_to_go_mlsimport_data' );
630 delete_transient( 'mlsimport_saas_token' );
631
632 delete_option( 'mlsimport_mls_metadata_populated' );
633 die( 'deleted' );
634 }
635
636 /**
637 *
638 *
639 *
640 *
641 * delete properties
642 */
643 function mlsimport_delete_properties() {
644 $error = false;
645 global $mlsimport;
646
647 check_ajax_referer( 'mlsimport_tool_actions', 'security' );
648
649
650 if ( current_user_can( 'administrator' ) ) :
651 $mlsimport_delete_category = sanitize_text_field( wp_unslash( $_POST['mlsimport_delete_category'] ) ) ;
652 $mlsimport_delete_category_term = sanitize_title( sanitize_text_field( wp_unslash( $_POST['mlsimport_delete_category_term']) ) );
653 $mlsimport_delete_timeout = intval( $_POST['mlsimport_delete_timeout'] );
654
655 if ( '' === $mlsimport_delete_category ) {
656 $error_message = esc_html__('Category cannot be blank','mlsimport');
657 $error = true;
658 }
659
660 if ( '' === $mlsimport_delete_category_term ) {
661 $error_message = esc_html__('Category Term cannot be blank','mlsimport');
662 $error = true;
663 }
664
665 $category = get_term_by( 'slug', $mlsimport_delete_category_term, $mlsimport_delete_category );
666
667 if ( $error ) {
668 print wp_json_encode(
669 array(
670 'message' => esc_html($error_message),
671 )
672 );
673 } else {
674 $mlsimport_delete_category_term_array = array();
675
676 $mlsimport_delete_category_term_array[] = $mlsimport_delete_category_term;
677 $tax_array = array(
678 'taxonomy' => $mlsimport_delete_category,
679 'field' => 'slug',
680 'terms' => $mlsimport_delete_category_term_array,
681 );
682
683 $args = array(
684 'post_type' => array( 'estate_property', 'property' ),
685 'post_status' => 'any',
686 'paged' => 1,
687 'posts_per_page' => -1,
688 'tax_query' => array(
689 $tax_array,
690 ),
691 'fields' => 'ids',
692 );
693
694 $prop_selection = new WP_Query( $args );
695
696 foreach ( $prop_selection->posts as $key => $delete_get_id ) {
697 if ( 0 !== $mlsimport_delete_timeout ) {
698 set_timeout( $mlsimport_delete_timeout );
699 }
700
701 $mlsimport->admin->theme_importer->mlsimportSaasDeletePropertyViaMysql( $delete_get_id, ' delete from tools ' );
702 }
703
704 wp_update_term_count_now( array( $category->term_id ), $mlsimport_delete_category );
705
706 print wp_json_encode(
707 array(
708 '$category' => $category->term_id,
709 'arguments' => $args,
710 'posts' => $prop_selection->posts,
711 'found' => $prop_selection->found_posts,
712 'message' => 'Done...',
713 )
714 );
715 }
716 endif;
717 die();
718 }
719
720
721
722
723 /**
724 *
725 *
726 * Testing enviroment variables
727 */
728 public function mlsimport_saas_setting_up() {
729 if (intval(WP_MEMORY_LIMIT) < 256): ?>
730 <div class="mlsimport_warning long_warning">
731 <strong>WordPress Memory Limit</strong> is set to <strong><?php echo esc_html(WP_MEMORY_LIMIT); ?></strong>. Allocated Memory should be at least <strong>256MB</strong>. Please refer to: <a href="https://wordpress.org/support/article/editing-wp-config-php/#increasing-memory-allocated-to-php" target="_blank">Increasing memory allocated to PHP</a>
732 </div>
733 <?php endif; ?>
734
735 <?php
736 $max_input_vars = ini_get('max_input_vars');
737 if ($max_input_vars < 2000):
738 ?>
739 <div class="mlsimport_warning long_warning">Your <strong>max_input_vars</strong> setting in php is set to <strong><?php echo esc_html($max_input_vars); ?></strong>. When importing real estate listings from an MLS, we work with a lot of data that needs to be saved. Please increase this value to at least <strong>2000</strong> or higher in order to save all details. Please refer to <a href="https://themezly.com/docs/how-to-increase-the-max-input-vars-limit/" target="_blank">How to Increase the Max Input Vars Limit</a></div>
740 <?php endif; ?>
741
742 <?php
743 $max_time = ini_get('max_execution_time');
744 if ($max_time < 600 && 0 !== $max_time):
745 ?>
746 <div class="mlsimport_warning long_warning">Your <strong>max_execution_time</strong> setting in php is set to <strong><?php echo esc_html($max_time); ?></strong>. Importing hundreds of listings requires extra time. Please set max_execution_time to <strong>0 (unlimited)</strong>. If that is not possible, set it to a minimum of <strong>600 (10 minutes)</strong>.</div>
747 <?php endif;
748
749 }
750
751 /**
752 * Check if token validates with MLS
753 *
754 * @since 4.0.1
755 * returns token fron mlsimport
756 */
757 public function mlsimport_saas_check_mls_connection() {
758
759 $values = array();
760 $options = get_option( $this->plugin_name . '_admin_options' );
761
762 $mls_id = '';
763 if ( isset( $options['mlsimport_mls_name'] ) ) {
764 $mls_id = sanitize_text_field( trim( $options['mlsimport_mls_name'] ) );
765 }
766
767 $mls_token = '';
768 if ( isset( $options['mlsimport_mls_name'] ) ) {
769 $mls_token = sanitize_text_field( trim( $options['mlsimport_mls_token'] ) );
770 }
771
772 $mlsimport_tresle_client_id = '';
773 if ( isset( $options['mlsimport_tresle_client_id'] ) ) {
774 $mlsimport_tresle_client_id = sanitize_text_field( trim( $options['mlsimport_tresle_client_id'] ) );
775 }
776
777 $mlsimport_tresle_client_secret = '';
778 if ( isset( $options['mlsimport_tresle_client_secret'] ) ) {
779 $mlsimport_tresle_client_secret = sanitize_text_field( trim( $options['mlsimport_tresle_client_secret'] ) );
780 }
781
782 // rapattoni data
783 $mlsimport_rapattoni_client_id = '';
784 if ( isset( $options['mlsimport_rapattoni_client_id'] ) ) {
785 $mlsimport_rapattoni_client_id = sanitize_text_field( trim( $options['mlsimport_rapattoni_client_id'] ) );
786 }
787 $mlsimport_rapattoni_client_secret = '';
788 if ( isset( $options['mlsimport_rapattoni_client_secret'] ) ) {
789 $mlsimport_rapattoni_client_secret = sanitize_text_field( trim( $options['mlsimport_rapattoni_client_secret'] ) );
790 }
791
792 $mlsimport_rapattoni_username = '';
793 if ( isset( $options['mlsimport_rapattoni_username'] ) ) {
794 $mlsimport_rapattoni_username = sanitize_text_field( trim( $options['mlsimport_rapattoni_username'] ) );
795 }
796
797 $mlsimport_rapattoni_password = '';
798 if ( isset( $options['mlsimport_rapattoni_password'] ) ) {
799 $mlsimport_rapattoni_password = sanitize_text_field( trim( $options['mlsimport_rapattoni_password'] ) );
800 }
801
802 // paragon data
803 $mlsimport_paragon_client_id = '';
804 if ( isset( $options['mlsimport_paragon_client_id'] ) ) {
805 $mlsimport_paragon_client_id = sanitize_text_field( trim( $options['mlsimport_paragon_client_id'] ) );
806 }
807 $mlsimport_paragon_client_secret = '';
808 if ( isset( $options['mlsimport_paragon_client_secret'] ) ) {
809 $mlsimport_paragon_client_secret = sanitize_text_field( trim( $options['mlsimport_paragon_client_secret'] ) );
810 }
811
812 if ( trim( $mls_token ) === '' ) {
813 if ( intval( $mls_id ) > 900 && intval( $mls_id ) < 3000 ) {
814 if ( trim( $mlsimport_tresle_client_id ) === '' || trim( $mlsimport_tresle_client_secret ) === '' ) {
815 return;
816 }
817 } elseif ( intval( $mls_id ) >= 5000 && intval( $mls_id ) < 6000 ) {
818 if (
819 trim( $mlsimport_rapattoni_client_id ) === '' ||
820 trim( $mlsimport_rapattoni_client_secret ) === '' ||
821 trim( $mlsimport_rapattoni_username ) === '' ||
822 trim( $mlsimport_rapattoni_password ) === ''
823 ) {
824 return;
825 }
826 } elseif ( intval( $mls_id ) >= 6000 ) {
827 if (
828 trim( $mlsimport_paragon_client_id ) === '' ||
829 trim( $mlsimport_paragon_client_secret ) === ''
830 ) {
831 return;
832 }
833 }
834 }
835
836 $values['mls_token'] = $mls_token;
837 $values['mls_id'] = $mls_id;
838 $values['mlsimport_tresle_client_id'] = $mlsimport_tresle_client_id;
839 $values['mlsimport_tresle_client_secret'] = $mlsimport_tresle_client_secret;
840
841 $values['mlsimport_rapattoni_client_id'] = $mlsimport_rapattoni_client_id;
842 $values['mlsimport_rapattoni_client_secret'] = $mlsimport_rapattoni_client_secret;
843 $values['mlsimport_rapattoni_username'] = $mlsimport_rapattoni_username;
844 $values['mlsimport_rapattoni_password'] = $mlsimport_rapattoni_password;
845
846 $values['mlsimport_paragon_client_id'] = $mlsimport_paragon_client_id;
847 $values['mlsimport_paragon_client_secret'] = $mlsimport_paragon_client_secret;
848
849 $answer = $this->theme_importer->globalApiRequestSaas( 'clients', $values, 'PATCH' );
850
851
852
853
854 if ( isset( $answer['succes'] ) && true === $answer['succes'] ) {
855 if ( isset( $answer['tested'] ) && true === $answer['tested'] ) {
856 update_option( 'mlsimport_connection_test', 'yes' );
857 } else {
858 delete_option( 'mlsimport_connection_test' );
859 delete_option( 'mlsimport_mls_metadata_populated' );
860 }
861 } else {
862 delete_option( 'mlsimport_connection_test' );
863 delete_option( 'mlsimport_mls_metadata_populated' );
864 }
865
866 return $answer;
867 }
868
869
870
871
872
873
874 /**
875 * Request auth token from mlsimport.net
876 *
877 * @since 4.0.1
878 * returns token fron mlsimport
879 */
880 public function mlsimport_saas_get_mls_api_token_from_transient() {
881
882 $token = get_transient( 'mlsimport_saas_token' );
883
884 if ( false === $token || '' === $token ) {
885 $token_json_answer = $this->mlsimport_saas_get_mls_api_token();
886
887
888 if ( isset( $token_json_answer['succes'] ) && true === $token_json_answer['succes'] ) {
889 $token = $token_json_answer['token'];
890
891 set_transient( 'mlsimport_saas_token', $token, 6 * 60 * 60 );
892 }
893 }
894
895 return $token;
896 }
897
898
899 /**
900 * call for token
901 *
902 * @since 4.0.1
903 * returns token fron mlsimport
904 */
905 protected function mlsimport_saas_get_mls_api_token() {
906 $values = array();
907 $options = get_option( $this->plugin_name . '_admin_options' );
908
909 $username = '';
910 if ( isset( $options['mlsimport_username'] ) ) {
911 $username = sanitize_text_field( trim( $options['mlsimport_username'] ) );
912 }
913
914 $password = '';
915 if ( isset( $options['mlsimport_password'] ) ) {
916 $password = sanitize_text_field( trim( $options['mlsimport_password'] ) );
917 }
918 $mls_name = '';
919 if ( isset( $options['mlsimport_mls_name'] ) ) {
920 $mls_name = sanitize_text_field( trim( $options['mlsimport_mls_name'] ) );
921 }
922
923 $mls_token = '';
924 if ( isset( $options['mlsimport_mls_token'] ) ) {
925 $mls_token = sanitize_text_field( trim( $options['mlsimport_mls_token'] ) );
926 }
927
928 $values['username'] = $username;
929 $values['password'] = $password;
930
931 if ( '' === $username || '' === $password ) {
932 return '';
933 }
934
935 $theme_Start = new ThemeImport();
936 $answer = $theme_Start::globalApiRequestSaas( 'token', $values, 'POST' );
937
938
939
940 return $answer;
941 }
942
943
944
945
946
947
948 /**
949 * save meta options
950 *
951 * @since 3.0.1
952 */
953 public function mlsimport_item_product_metaboxes() {
954 add_meta_box( 'mlsimport_item_metaboxes-sectionid', __( 'Set Import data', 'mlsimport' ), array( $this, 'mlsimport_saas_display_meta_options' ), 'mlsimport_item', 'normal', 'default' );
955 }
956
957
958
959 /**
960 *
961 *
962 *
963 * save meta options
964 *
965 * @since 3.0.1
966 */
967 public function mlsimport_item_product_save_metaboxes( $post_id, $post ) {
968
969 if ( ! is_object( $post ) || ! isset( $post->post_type ) ) {
970 return;
971 }
972
973 if ( 'mlsimport_item' !== $post->post_type ) {
974 return;
975 }
976
977 $allowed_keys = array(
978 'mlsimport_item_how_many',
979 'mlsimport_item_title_format',
980 'mlsimport_item_agent',
981 'mlsimport_item_property_status',
982 'mlsimport_item_property_user',
983 'mlsimport_item_min_price',
984 'mlsimport_item_max_price',
985 'mlsimport_item_city_check',
986 'mlsimport_item_city',
987 'mlsimport_item_city[]',
988 'mlsimport_item_countyorparish_check',
989 'mlsimport_item_countyorparish',
990 'mlsimport_item_mlsstatus_check',
991 'mlsimport_item_mlsstatus',
992 'mlsimport_item_propertysubtype_check',
993 'mlsimport_item_propertysubtype',
994 'mlsimport_item_propertytype_check',
995 'mlsimport_item_propertytype',
996 'mlsimport_item_standardstatus_check',
997 'mlsimport_item_standardstatus',
998 'mlsimport_item_standardstatusdelete_check',
999 'mlsimport_item_standardstatusdelete',
1000
1001 'mlsimport_item_internetentirelistingdisplayyn',
1002 'mlsimport_item_internetaddressdisplayyn',
1003 'mlsimport_item_stat_cron',
1004 'mlsimport_item_listagentkey',
1005 'mlsimport_item_listagentmlsid',
1006 'mlsimport_item_listofficekey',
1007 'mlsimport_item_postalcode',
1008 'mlsimport_item_listofficemlsid',
1009 'mlsimport_item_listingid',
1010 'mlsimport_item_extracity',
1011 'mlsimport_item_extracounty',
1012 'mlsimport_item_exclude_listofficemlsid',
1013 'mlsimport_item_exclude_listofficekey',
1014 'mlsimport_item_exclude_listagentmlsid',
1015 'mlsimport_item_exclude_listagentkey',
1016 );
1017
1018
1019
1020
1021 foreach ( $allowed_keys as $key => $key_value ) {
1022 if( isset($_POST[$key_value]) ){
1023 $postmeta = mlsimport_sanitize_multi_dimensional_array ( $_POST[$key_value] ) ;
1024 update_post_meta( $post_id, sanitize_key( $key_value ), $postmeta );
1025 }
1026
1027 }
1028
1029 $blank_keys = array(
1030 'mlsimport_item_standardstatus',
1031 'mlsimport_item_city',
1032 'mlsimport_item_countyorparish',
1033 'mlsimport_item_propertysubtype',
1034 'mlsimport_item_propertytype',
1035 'mlsimport_item_standardstatus',
1036 'mlsimport_item_listingid',
1037
1038 );
1039
1040 foreach ( $blank_keys as $key ) {
1041 if ( ! isset( $_POST[ $key ] ) ) {
1042 update_post_meta( $post_id, $key, '' );
1043 }
1044 }
1045
1046
1047 }
1048
1049
1050 /**
1051 * Display Meta Options
1052 *
1053 * @param WP_Post $post The post object.
1054 */
1055 public function mlsimport_saas_display_meta_options($post) {
1056 wp_nonce_field(plugin_basename(__FILE__), 'estate_agent_noncename');
1057 global $mlsimport;
1058
1059 $postId = $post->ID;
1060 $token = $mlsimport->admin->mlsimport_saas_get_mls_api_token_from_transient();
1061
1062 $mlsimportItemHowMany = esc_html(get_post_meta($postId, 'mlsimport_item_how_many', true));
1063 $mlsimportItemStatCron = esc_html(get_post_meta($postId, 'mlsimport_item_stat_cron', true));
1064 $lastDate = get_post_meta($postId, 'mlsimport_last_date', true);
1065 $status = get_option('mlsimport_force_stop_' . $postId);
1066 $fieldImport = $this->mlsimport_saas_return_mls_fields();
1067 $options = get_option('mlsimport_admin_options');
1068 $mlsimportMlsId = isset($options['mlsimport_mls_name']) && $options['mlsimport_mls_name'] !== ''
1069 ? intval($options['mlsimport_mls_name'])
1070 : 0;
1071
1072 $mlsRequest = $this->mlsimport_make_listing_requests($postId);
1073 if (isset($mlsRequest['success']) && !$mlsRequest['success']) {
1074 echo '<div class="mlsimport_warning">' . esc_html($mlsRequest['message']) . '</div>';
1075 }
1076
1077 $foundItems = isset($mlsRequest['results']) ? intval($mlsRequest['results']) : 'none';
1078 if ($foundItems === 'none') {
1079 $mlsimport->admin->mlsimport_saas_check_mls_connection();
1080 esc_html_e('Your Token was expired. Please refresh the page to renew it wait while we renew it.', 'mlsimport');
1081 }
1082
1083 echo $this->generateMetaOptionsHtml($postId, $foundItems, $lastDate, $mlsimportItemHowMany, $mlsimportItemStatCron, $mlsimportMlsId, $fieldImport);
1084 }
1085
1086
1087
1088
1089 /**
1090 * Generate Meta Options HTML
1091 *
1092 * @param int $postId The post ID.
1093 * @param int $foundItems The number of found items.
1094 * @param string $lastDate The last date checked.
1095 * @param string $mlsimportItemHowMany How many items to import.
1096 * @param string $mlsimportItemStatCron The status of the cron job.
1097 * @param int $mlsimportMlsId The MLS import ID.
1098 * @param array $fieldImport The fields to import.
1099 * @return string The generated HTML.
1100 */
1101 private function generateMetaOptionsHtml($postId, $foundItems, $lastDate, $mlsimportItemHowMany, $mlsimportItemStatCron, $mlsimportMlsId, $fieldImport) {
1102 ob_start();
1103
1104 ?>
1105 <div class="mlsimport_item_search_url" style="display:none;"><?php echo esc_html__('Last date/time we check :', 'mlsimport') . ' ' . esc_html($lastDate); ?></div>
1106 <ul>
1107 <li>1. Set the import parameters.</li>
1108 <li>2. Hit Publish or Update, otherwise import will not work correctly.</li>
1109 <li>3. Click the Start Import button. Most MLS limit the import number to 1000. If you need to import more create additional import items.</li>
1110 <li>4. Press the Update button after you make any change in the import settings.</li>
1111 </ul>
1112
1113 <?php if (is_numeric($foundItems) && $foundItems >= 500): ?>
1114 <div class="mlsimport_notification">
1115 <?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'); ?>
1116 </div>
1117 <?php endif; ?>
1118
1119 <div class="mlsimport_import_no">
1120 <?php esc_html_e('We found', 'mlsimport'); ?>
1121 <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.
1122 </div>
1123
1124 <fieldset class="mlsimport-fieldset">
1125 <label class="mlsimport-label" for="mlsimport_item_how_many">
1126 <?php esc_html_e('How Many to import. Use 0 if you want to import all listings found.', 'mlsimport'); ?>
1127 </label>
1128 <input type="text" id="mlsimport_item_how_many" name="mlsimport_item_how_many" value="<?php echo esc_attr($mlsimportItemHowMany); ?>"/>
1129 </fieldset>
1130
1131 <fieldset class="mlsimport-fieldset mlsimport_auto_switch">
1132 <?php esc_html_e('Enable Auto Update every hour?', 'mlsimport'); ?>
1133 <label class="mlsimport_switch">
1134 <input type="hidden" value="0" name="mlsimport_item_stat_cron">
1135 <input type="checkbox" value="1" name="mlsimport_item_stat_cron"<?php if (intval($mlsimportItemStatCron) !== 0) echo esc_html(' checked'); ?>>
1136 <span class="slider round"></span>
1137 </label>
1138 </fieldset>
1139
1140 <?php if ($mlsimportItemStatCron !== ''): ?>
1141 <div id="mlsimport_item_status"></div>
1142 <input class="button mlsimport_button" type="button" id="mlsimport-start_item"
1143 data-post-number="<?php echo intval($foundItems); ?>"
1144 data-post_id="<?php echo intval($postId); ?>" value="Start Import">
1145 <input class="button mlsimport_button" type="button" id="mlsimport_stop_item"
1146 data-post-number="<?php echo intval($foundItems); ?>"
1147 data-post_id="<?php echo intval($postId); ?>" value="Stop Import">
1148 <?php endif; ?>
1149
1150 <input type="hidden" id="mlsimport_item_actions" value="<?php echo esc_attr(wp_create_nonce("mlsimport_item_actions")); ?>"/>
1151 <div class="mlsimport_param_wrapper"><h2><?php esc_html_e('Import Parameters', 'mlsimport'); ?></h2>
1152
1153 <?php
1154 $mlsimportItemTitleFormat = esc_html(get_post_meta($postId, 'mlsimport_item_title_format', true));
1155 ?>
1156
1157 <fieldset class="mlsimport-fieldset">
1158 <label class="mlsimport-label" for="mlsimport_item_title_format">
1159 <?php esc_html_e('Title Format', 'mlsimport'); ?>
1160 </label>
1161
1162 <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>
1163 <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}'); ?>"/>
1164 </fieldset>
1165
1166 <?php
1167 $mlsimportItemAgent = esc_html(get_post_meta($postId, 'mlsimport_item_agent', true));
1168 ?>
1169
1170 <fieldset class="mlsimport-fieldset">
1171 <label class="mlsimport-label" for="mlsimport_item_agent">
1172 <?php esc_html_e('Select Agent', 'mlsimport'); ?>
1173 </label>
1174 <select class="mlsimport-select" name="mlsimport_item_agent" id="mlsimport_item_agent">
1175 <?php
1176 $permitedTags = mlsimport_allowed_html_tags_content();
1177 $selectAgent =$this->theme_importer->mlsimportSaasThemeImportSelectAgent($mlsimportItemAgent);
1178 print wp_kses($selectAgent, $permitedTags);
1179 ?>
1180 </select>
1181 </fieldset>
1182
1183 <?php
1184 $mlsimportItemPropertyStatus = esc_html(get_post_meta($postId, 'mlsimport_item_property_status', true));
1185 if ('' === $mlsimportItemPropertyStatus) {
1186 $mlsimportItemPropertyStatus = 'publish';
1187 }
1188 $statusArray = array('publish', 'draft');
1189 ?>
1190 <fieldset class="mlsimport-fieldset">
1191 <label class="mlsimport-label" for="mlsimport_item_property_status">
1192 <?php esc_html_e('Select Property Status on import', 'mlsimport'); ?>
1193 </label>
1194 <select class="mlsimport-select" name="mlsimport_item_property_status" id="mlsimport_item_property_status">
1195 <?php foreach ($statusArray as $value): ?>
1196 <option value="<?php echo esc_attr($value); ?>" <?php if ($value === $mlsimportItemPropertyStatus) echo esc_html('selected'); ?>>
1197 <?php echo esc_html($value); ?>
1198 </option>
1199 <?php endforeach; ?>
1200 </select>
1201 </fieldset>
1202
1203 <?php
1204 $mlsimportItemPropertyUser = esc_html(get_post_meta($postId, 'mlsimport_item_property_user', true));
1205 ?>
1206 <fieldset class="mlsimport-fieldset">
1207 <label class="mlsimport-label" for="mlsimport_item_property_user">
1208 <?php esc_html_e('User', 'mlsimport'); ?>
1209 </label>
1210 <select class="mlsimport-select" id="mlsimport_item_property_user" name="mlsimport_item_property_user">
1211 <?php
1212 $selectUser = $this->theme_importer->mlsimportSaasThemeImportSelectUser($mlsimportItemPropertyUser);
1213 print wp_kses($selectUser, $permitedTags);
1214 ?>
1215 </select>
1216 </fieldset>
1217
1218 <?php
1219 $mlsimportItemMinPrice = floatval(get_post_meta($postId, 'mlsimport_item_min_price', true));
1220 $mlsimportItemMaxPrice = floatval(get_post_meta($postId, 'mlsimport_item_max_price', true));
1221 if (0 === intval($mlsimportItemMaxPrice)) {
1222 $mlsimportItemMaxPrice = 10000000;
1223 }
1224 ?>
1225 <fieldset class="mlsimport-fieldset">
1226 <label class="mlsimport-label">
1227 <?php esc_html_e('Price Between', 'mlsimport'); ?>
1228 </label>
1229 <input type="text" class="mlsimport-select" id="mlsimport_item_min_price" name="mlsimport_item_min_price" value="<?php echo esc_attr($mlsimportItemMinPrice); ?>"> and
1230 <input type="text" class="mlsimport-select" id="mlsimport_item_max_price" name="mlsimport_item_max_price" value="<?php echo esc_attr($mlsimportItemMaxPrice); ?>">
1231 </fieldset>
1232
1233 <?php
1234 $options = get_option($this->plugin_name . '_admin_options');
1235
1236 $mlsId = '';
1237 if (isset($options['mlsimport_mls_name'])) {
1238 $mlsId = sanitize_text_field(trim($options['mlsimport_mls_name']));
1239 }
1240
1241 if ($mlsId > 5000) {
1242 $fieldImport['PropertyType']['multiple'] = 'no';
1243 }
1244
1245 foreach ($fieldImport as $key => $field):
1246 $nameCheck = strtolower('mlsimport_item_' . $key . '_check');
1247 $name = strtolower('mlsimport_item_' . $key);
1248
1249 $value = get_post_meta($postId, $name, true);
1250 $valueCheck = get_post_meta($postId, $nameCheck, true);
1251 $extraClass = '';
1252 if ('extraCity' === $key || 'extraCounty' === $key) {
1253 $extraClass = ' mlsimport_hidden_field_button';
1254 }
1255 ?>
1256 <fieldset class="mlsimport-fieldset">
1257 <label class="mlsimport-label <?php echo esc_attr($extraClass); ?>" for="<?php echo esc_attr($name); ?>">
1258 <?php echo esc_html($field['label']); ?>
1259 </label>
1260 <?php if ('extraCity' === $key || 'extraCounty' === $key): ?>
1261 <div class="mlsimport-input-wrapper" style="display:none">
1262 <?php endif; ?>
1263 <p class="mlsimport-exp"><?php echo wp_kses_post($this->mlsimport_notes_for_mls($mlsimportMlsId, $name, $field['description'])); ?>
1264 <?php
1265 $isCheckboxAdmin = 0;
1266 if (1 === intval($valueCheck)) {
1267 $isCheckboxAdmin = 1;
1268 }
1269
1270 $selectAllNone = [
1271 'InternetAddressDisplayYN',
1272 'InternetEntireListingDisplayYN',
1273 'PostalCode',
1274 'ListAgentKey',
1275 'ListAgentMlsId',
1276 'ListOfficeKey',
1277 'ListOfficeMlsId',
1278 'StandardStatus',
1279 'StandardStatusDelete',
1280 'ListingId',
1281 'extraCity',
1282 'extraCounty',
1283 'Exclude_ListOfficeKey',
1284 'Exclude_ListOfficeMlsId',
1285 'Exclude_ListAgentKey',
1286 'Exclude_ListAgentMlsId',
1287 ];
1288
1289 if ($mlsId > 5000) {
1290 $selectAllNone[] = 'PropertyType';
1291 }
1292
1293 if (!in_array($key, $selectAllNone)): ?>
1294 <?php
1295 esc_html_e('- Or Select All ', 'mlsimport');
1296
1297 ?>
1298 <input type="hidden" name="<?php echo esc_attr($nameCheck); ?>" value="0"/>
1299 <input type="checkbox" name="<?php echo esc_attr($nameCheck); ?>" value="1" <?php print esc_attr(checked($isCheckboxAdmin, 1, 0)); ?>/>
1300 <?php endif; ?>
1301 </p>
1302
1303 <?php
1304 $permittedStatus = ['active', 'active under contract', 'coming soon', 'activeundercontract', 'comingsoon', 'pending'];
1305
1306 if ($field['type'] === 'select'): ?>
1307 <?php
1308 $multiple = '';
1309 if ('yes' === $field['multiple']) {
1310 $multiple = 'multiple';
1311 $name .= '[]';
1312 }
1313
1314 if ('StandardStatus' === $key && '' === $value) {
1315 $value = ['Active'];
1316 }
1317
1318
1319
1320 // Additional conditions can be placed here.
1321 ?>
1322 <select class="mlsimport-select" id="<?php echo esc_attr($name); ?>" name="<?php echo esc_attr($name); ?>" <?php echo esc_attr($multiple); ?>>
1323 <?php foreach ($field['values'] as $selectKey): ?>
1324
1325 <?php if ('' !== $selectKey): ?>
1326 <option value="<?php echo esc_attr($selectKey); ?>"
1327 <?php
1328 if ($key === "StandardStatusDelete" && $value==null ) {
1329
1330 print 'selected';
1331 }
1332 ?>
1333 <?php if (is_array($value) ? in_array($selectKey, $value) : $selectKey === $value) echo 'selected'; ?>>
1334 <?php echo esc_html($selectKey); ?>
1335 </option>
1336 <?php endif; ?>
1337
1338 <?php endforeach; ?>
1339 </select>
1340
1341 <?php elseif ($field['type'] === 'input'): ?>
1342 <input type="text" class="mlsimport-select" id="<?php echo esc_attr($name); ?>" name="<?php echo esc_attr($name); ?>" value="<?php echo esc_attr($value); ?>">
1343 <?php endif; ?>
1344 <?php if ('extraCity' === $key || 'extraCounty' === $key): ?>
1345 </div>
1346 <?php endif; ?>
1347 </fieldset>
1348 <?php endforeach; ?>
1349
1350 </div>
1351 <?php
1352 return ob_get_clean();
1353 }
1354
1355
1356
1357
1358
1359
1360 public function mlsimport_add_extra_fields() {
1361 }
1362
1363 /**
1364 *
1365 *
1366 *
1367 *
1368 *
1369 *
1370 */
1371 function mlsimport_notes_for_mls( $mlsimport_mls_id, $name, $description ) {
1372 // 111 - Rae Edmonton
1373
1374 if ( 111 === intval($mlsimport_mls_id) && 'mlsimport_item_standardstatus' === $name ) {
1375 return esc_html__( 'Your MLS does not use this field - all listings are considered Active.', 'mlsimport' );
1376 } else {
1377 return $description;
1378 }
1379 }
1380
1381
1382 /**
1383 *
1384 *
1385 * Get Last date
1386 */
1387 public function mlsimport_saas_get_last_date( $item_id ) {
1388 $last_date = get_post_meta( $item_id, 'mlsimport_last_date', true );
1389
1390 if ( '' === $last_date ) {
1391 $last_date = $this->mlsimport_saas_update_last_date( $item_id );
1392 }
1393 return $last_date;
1394 }
1395
1396
1397 /**
1398 *
1399 *
1400 * Save Last date
1401 */
1402 public function mlsimport_saas_update_last_date( $item_id ) {
1403
1404 $unix_time = current_time( 'timestamp', 0 ) - ( 2 * 60 * 60 );
1405 print $last_date_to_save = date( 'Y-m-d\TH:i', $unix_time );
1406 update_post_meta( $item_id, 'mlsimport_last_date', $last_date_to_save );
1407
1408 return $last_date_to_save;
1409 }
1410
1411
1412
1413 /**
1414 *
1415 *
1416 * Check if there are modified items in the last 2h
1417 */
1418 public function mlsimport_saas_start_cron_links_per_item( $item_id ) {
1419
1420 $last_date = $this->mlsimport_saas_get_last_date( $item_id );
1421 print 'MLSitem id: '.$item_id.' - ';
1422 esc_html_e('date to consider: ','mlsimport');
1423 print esc_html($last_date ). '. ';
1424
1425 $mlsrequest = $this->mlsimport_make_listing_requests( $item_id, $last_date );
1426
1427 $found_items = 0;
1428 if ( isset( $mlsrequest['results'] ) ) {
1429 $found_items = intval( $mlsrequest['results'] );
1430 } else {
1431 delete_transient( 'mlsimport_saas_token' );
1432 }
1433
1434 print esc_html__('We found ','mlsimport') .esc_html( $found_items ). ' listings.</br>' . PHP_EOL;
1435
1436 $attachments_to_move = array();
1437
1438 if ( $found_items > 0 ) {
1439 $item_id_array = array(
1440 'item_id' => $item_id,
1441 'how_many' => 0,
1442 'max_number' => $found_items,
1443 'batch_counter' => 1,
1444 );
1445
1446 $attachments_to_move = (array) $this->mlsimport_saas_generate_import_requests_per_item( $item_id_array, $last_date );
1447
1448 update_post_meta( $item_id, 'mlsimport_spawn_status_cron_job', 'started' );
1449 update_post_meta( $item_id, 'mlsimport_cron_attach_to_move_' . $item_id, $attachments_to_move );
1450
1451 // save last date for next run
1452 $this->mlsimport_saas_update_last_date( $item_id );
1453
1454 $attachments_to_send = array(
1455 'args' => array(
1456 'attachments_to_move' => $item_id,
1457 'item_id_array' => $item_id_array,
1458 ),
1459 );
1460
1461 $this->mlsimport_background_process_per_item_cron_function( $attachments_to_send['args'] );
1462
1463 }
1464 }
1465
1466
1467
1468
1469 /**
1470 *
1471 *
1472 * Reconciliation log
1473 */
1474 public function mlsimport_saas_start_doing_reconciliation() {
1475 global $mlsimport;
1476 print 'start';
1477 $listingKey_in_Local = $this->mlsimport_saas_get_all_meta_values( 'ListingKey' );
1478
1479 if ( empty( $listingKey_in_Local ) ) {
1480 return;
1481 }
1482
1483
1484 $mls_data = $this->mlsimport_saas_get_mls_reconciliation_data();
1485 $listingKey_in_MLS = $mls_data['all_data'];
1486
1487 if ( empty( $listingKey_in_MLS ) ) {
1488 return;
1489 }
1490
1491 $to_delete = 0;
1492 $counter = 0;
1493 foreach ( $listingKey_in_Local as $key => $item ) {
1494 $listingkey = $item->meta_value;
1495 $property_id = $item->iD;
1496 ++$counter;
1497
1498 //print '</br>'.$counter. ' **************************</br>';
1499
1500 if ( in_array( $listingkey, $listingKey_in_MLS ) ) {
1501 print wp_kses_post('</br>'.$listingkey . ' IS FOUND');
1502 } else {
1503
1504 $keep = $mlsimport->admin->theme_importer->check_if_delete_when_status($property_id);
1505
1506 if(!$keep){
1507 ++$to_delete;
1508 print wp_kses_post('</br>' .$listingkey. ' ------------------------- NOT FOUND delete: '.$property_id.' /'.$post_status.'<-');
1509 $mlsimport->admin->theme_importer->mlsimportSaasDeletePropertyViaMysql( $property_id, $listingkey );
1510 }else{
1511 print wp_kses_post('</br>' .$listingkey. ' ------------------------- NOT FOUND BUT MARKED AS KEEP: '.$property_id.' /'.$post_status.'<-');
1512 }
1513
1514 }
1515
1516 //print '</br> ************************** ';
1517 }
1518
1519 print esc_html(' to delete:' .$to_delete);
1520 return;
1521 }
1522
1523
1524 /**
1525 *
1526 *
1527 * Requestq Reconciliation log
1528 */
1529 public function mlsimport_saas_get_mls_reconciliation_data() {
1530
1531 $arguments = array();
1532 $answer = $this->theme_importer->globalApiRequestCurlSaas( 'reconciliation', $arguments, 'GET' );
1533 return $answer;
1534 }
1535
1536 /**
1537 *
1538 *
1539 * Reconciliation get local data
1540 */
1541 public function mlsimport_saas_get_all_meta_values( $key ) {
1542 global $wpdb;
1543
1544 $result = $wpdb->get_results(
1545 $wpdb->prepare(
1546 "
1547 SELECT DISTINCT pm.meta_value,p.iD FROM {$wpdb->postmeta} pm
1548 LEFT JOIN {$wpdb->posts} p ON p.ID = pm.post_id
1549 WHERE pm.meta_key = %s
1550 AND p.post_status = 'publish'",
1551 $key
1552 )
1553 );
1554
1555 return $result;
1556 }
1557
1558
1559
1560
1561
1562 /*
1563 * Do api Listing Requests
1564 *
1565 *
1566 *
1567 *
1568 * */
1569 public function mlsimport_make_listing_requests( $item_id, $last_date = '', $skip = '', $top = '' ) {
1570 $options = get_option( $this->plugin_name . '_admin_options' );
1571 $mls_id = '';
1572 if ( isset( $options['mlsimport_mls_name'] ) ) {
1573 $mls_id = sanitize_text_field( trim( $options['mlsimport_mls_name'] ) );
1574 }
1575
1576 $arguments = $this->mlsimport_saas_make_listing_requests_arguments( $item_id, $last_date, $skip, $top );
1577
1578
1579 if (
1580 $mls_id > 5000 && $mls_id < 6000 &&
1581 ( ! isset( $arguments['property_type'] ) or
1582 ( isset( $arguments['property_type'] ) && '' === $arguments['property_type'] ) or
1583 ( isset( $arguments['property_type'][0] ) && '' === $arguments['property_type'][0] )
1584 )
1585 ) {
1586 return array(
1587 'success' => false,
1588 'type' => 'rapattoni',
1589 'message' => esc_html__( 'This MLS requires to have one item selected from "Property Action Category" dropdown', 'mlsimport' ),
1590 );
1591 }
1592
1593 $potential_leght = strlen( wp_json_encode( $arguments ) );
1594 if ( $potential_leght > 1750 ) {
1595 return array(
1596 'success' => false,
1597 'potential_leght' => $potential_leght,
1598 'message' => esc_html__( 'You have too many parameters selected. Split the import beween multiple MLS Import items: For ex : Import per County instead of selecting 10 cities or import listing between certain price range.', 'mlsimport' ),
1599 );
1600 }
1601
1602 $answer = $this->theme_importer->globalApiRequestCurlSaas( 'listings', $arguments, 'POST' );
1603 $answer['potential_leght'] = $potential_leght;
1604 return ( $answer );
1605 }
1606
1607
1608
1609
1610
1611
1612 /*
1613 * Create Api query arguments
1614 *
1615 *
1616 *
1617 *
1618 *
1619 * */
1620
1621 public function mlsimport_saas_make_listing_requests_arguments( $item_id, $last_date = '', $skip = '', $top = '' ) {
1622
1623 $options = get_option( $this->plugin_name . '_admin_options' );
1624 if ( isset( $options['mlsimport_mls_name'] ) ) {
1625 $mls_id = intval( $options['mlsimport_mls_name'] );
1626 } else {
1627 return '';
1628 }
1629
1630 if ( isset( $options['mlsimport_theme_used'] ) ) {
1631 $theme_id = intval( $options['mlsimport_theme_used'] );
1632 } else {
1633 return '';
1634 }
1635
1636 $values = array();
1637 $values['mls_id'] = $mls_id;
1638 $values['theme_id'] = $theme_id;
1639
1640 if ( '' !== $top ) {
1641 $values['top'] = $top;
1642 $values['skip'] = intval( $skip );
1643 }
1644
1645 // // add price
1646 $mlsimport_item_min_price = get_post_meta( $item_id, 'mlsimport_item_min_price', true );
1647 $mlsimport_item_max_price = get_post_meta( $item_id, 'mlsimport_item_max_price', true );
1648 if ( '' !== $mlsimport_item_min_price && '' !== $mlsimport_item_max_price ) {
1649 $values['list_price_min'] = floatval( $mlsimport_item_min_price );
1650 $values['list_price_max'] = floatval( $mlsimport_item_max_price );
1651 }
1652
1653 // add city
1654 $values = $this->mls_import_return_multiple_param_value( 'city', $item_id, 'city', $values );
1655
1656 // add county
1657 $values = $this->mls_import_return_multiple_param_value( 'countyorparish', $item_id, 'county_or_parish', $values );
1658
1659 // add postal code
1660 $values = $this->mls_import_saas_add_to_parms_input( 'PostalCode', $item_id, 'postal_code', $values );
1661
1662 // add status
1663
1664 if ( 111 !== $mls_id ) { // edmonton check
1665 $values = $this->mls_import_return_multiple_param_value( 'StandardStatus', $item_id, 'status', $values );
1666 }
1667
1668 // add property_subtype
1669 $values = $this->mls_import_return_multiple_param_value( 'PropertySubType', $item_id, 'property_subtype', $values );
1670
1671 // add property_type
1672 $values = $this->mls_import_return_multiple_param_value( 'PropertyType', $item_id, 'property_type', $values );
1673
1674 // rapattoni exception
1675 if ( $mls_id > 5000 ) {
1676 $values = $this->mls_import_saas_add_to_parms_input( 'PropertyType', $item_id, 'property_type', $values );
1677 $temp = $values['property_type'];
1678 $temp = str_replace( ' ', '', $temp );
1679 $values['property_type'] = array();
1680 $values['property_type'][] = $temp;
1681 }
1682
1683 // add internet_entirelisting_displayyn
1684 $values = $this->mls_import_saas_add_to_parms_input( 'InternetEntireListingDisplayYN', $item_id, 'internet_entirelisting_displayyn', $values );
1685
1686 // add internet_address_displayyn
1687 $values = $this->mls_import_saas_add_to_parms_input( 'InternetAddressDisplayYN', $item_id, 'internet_address_displayyn', $values );
1688
1689 // add ListAgentKey
1690 $values = $this->mls_import_saas_add_to_parms_input( 'ListAgentKey', $item_id, 'list_agentkey', $values );
1691 // add ListAgentKey
1692 $values = $this->mls_import_saas_add_to_parms_input( 'ListAgentMlsId', $item_id, 'list_agentmlsid', $values );
1693 // add ListOfficeKey
1694 $values = $this->mls_import_saas_add_to_parms_input( 'ListOfficeKey', $item_id, 'list_officekey', $values );
1695 // add ListOfficeMlsId
1696 $values = $this->mls_import_saas_add_to_parms_input( 'ListOfficeMlsId', $item_id, 'list_officemlsid', $values );
1697
1698 // add ListingId
1699 $values = $this->mls_import_saas_add_to_parms_input( 'ListingId', $item_id, 'listingid', $values );
1700
1701 //add Exclude_ListOfficeKey
1702 $values = $this->mls_import_saas_add_to_parms_input( 'Exclude_ListOfficeKey', $item_id, 'exclude_list_officekey', $values );
1703 // add Exclude_ListOfficeMlsId
1704 $values = $this->mls_import_saas_add_to_parms_input( 'Exclude_ListOfficeMlsId', $item_id, 'exclude_list_officemlsid', $values );
1705
1706
1707
1708 //add Exclude_ListAgentKey
1709 $values = $this->mls_import_saas_add_to_parms_input( 'Exclude_ListAgentKey', $item_id, 'exclude_list_agentkey', $values );
1710 // add Exclude_ListAgentMlsId
1711 $values = $this->mls_import_saas_add_to_parms_input( 'Exclude_ListAgentMlsId', $item_id, 'exclude_list_agentmlsid', $values );
1712
1713
1714
1715 if ( '' !== $last_date ) {
1716 $values['modification_time'] = $last_date;
1717 }
1718
1719 return( $values );
1720 }
1721
1722
1723
1724 /*
1725 *
1726 * add input items to parameters array
1727 *
1728 */
1729
1730 public function mls_import_saas_add_to_parms_input( $key, $post_id, $new_name, $all_values ) {
1731 $name = strtolower( 'mlsimport_item_' . $key );
1732 $value = get_post_meta( $post_id, $name, true );
1733 if ( '' !== $value ) {
1734 $all_values[ $new_name ] = $value;
1735 }
1736
1737 return $all_values;
1738 }
1739
1740
1741 /*
1742 *
1743 * add list items to parameters array
1744 *
1745 */
1746
1747 public function mls_import_return_multiple_param_value( $key, $post_id, $new_name, $all_values ) {
1748 $name_check = strtolower( 'mlsimport_item_' . $key . '_check' );
1749 $name = strtolower( 'mlsimport_item_' . $key );
1750
1751 $value = get_post_meta( $post_id, $name, true );
1752
1753 // add extra county - should be moved into function if pass tests
1754 if ( 'countyorparish' === $key ) {
1755 $extracounty_values = get_post_meta( $post_id, 'mlsimport_item_extracounty', true );
1756
1757 if ( '' !== $extracounty_values ) {
1758 $extracounty_array = explode( ',', $extracounty_values );
1759
1760 if ( ! is_array( $value ) ) {
1761 if ( '' === $value ) {
1762 $value = array();
1763 } else {
1764 $value = array( $value );
1765 }
1766 }
1767
1768 foreach ( $extracounty_array as $extra ) {
1769 $value[] = $extra;
1770 }
1771 }
1772 }
1773
1774 // add extra city - should be moved into function if pass tests
1775 if ( 'city' === $key ) {
1776 $extracity_values = get_post_meta( $post_id, 'mlsimport_item_extracity', true );
1777 if ( '' !== $extracity_values ) {
1778 $extracity_array = explode( ',', $extracity_values );
1779
1780 if ( ! is_array( $value ) ) {
1781 if ('' === $value ) {
1782 $value = array();
1783 } else {
1784 $value = array( $value );
1785 }
1786 }
1787
1788 foreach ( $extracity_array as $extra ) {
1789 $value[] = $extra;
1790 }
1791 }
1792 }
1793
1794 $value_check = get_post_meta( $post_id, $name_check, true );
1795
1796 if ( 0 === intval($value_check) && '' !== $value ) {
1797 $all_values[ $new_name ] = $value;
1798 }
1799
1800 // status exception
1801 if ( 'status' === $new_name ) {
1802 $all_values[ $new_name ] = $value;
1803 }
1804
1805 return $all_values;
1806 }
1807
1808
1809
1810 /*
1811 *
1812 * All Enums fiels to be used on MLS import Item
1813 *
1814 *
1815 *
1816 *
1817 *
1818 *
1819 * */
1820
1821 public function mlsimport_saas_return_mls_fields() {
1822
1823 $mlsimport_mls_metadata_mls_enums = get_option( 'mlsimport_mls_metadata_mls_enums', '' );
1824
1825 if ( '' === $mlsimport_mls_metadata_mls_enums ) {
1826 ?>
1827 <div class="mlsimport_warning long_warning">Please select the import fields(from MLS Import Settings) before starting a MLS import process.</div>
1828 <?php
1829 }
1830
1831 $metadata_api_call_full = json_decode( $mlsimport_mls_metadata_mls_enums, true );
1832
1833 if ( isset( $metadata_api_call_full['global_array'] ) ) {
1834 $metadata_api_call = $metadata_api_call_full['global_array'];
1835 }
1836
1837 $city_array = array();
1838 if ( isset( $metadata_api_call['PropertyEnums']['City'] ) && is_array( $metadata_api_call['PropertyEnums']['City'] ) ) {
1839 $city_array = array_keys( $metadata_api_call['PropertyEnums']['City'] );
1840 }
1841
1842 $county_array = array();
1843 if ( isset( $metadata_api_call['PropertyEnums']['CountyOrParish'] ) && is_array( $metadata_api_call['PropertyEnums']['CountyOrParish'] ) ) {
1844 $county_array = array_keys( $metadata_api_call['PropertyEnums']['CountyOrParish'] );
1845 }
1846
1847 $mlsstatus_array = array();
1848 if ( isset( $metadata_api_call['PropertyEnums']['MlsStatus'] ) && is_array( $metadata_api_call['PropertyEnums']['MlsStatus'] ) ) {
1849 $mlsstatus_array = array_keys( $metadata_api_call['PropertyEnums']['MlsStatus'] );
1850 }
1851
1852 $propertysubtype_array = array();
1853 if ( isset( $metadata_api_call['PropertyEnums']['PropertySubType'] ) && is_array( $metadata_api_call['PropertyEnums']['PropertySubType'] ) ) {
1854 $propertysubtype_array = array_keys( $metadata_api_call['PropertyEnums']['PropertySubType'] );
1855 }
1856
1857 $propertytype_array = array();
1858 if ( isset( $metadata_api_call['PropertyEnums']['PropertyType'] ) && is_array( $metadata_api_call['PropertyEnums']['PropertyType'] ) ) {
1859 $propertytype_array = array_keys( $metadata_api_call['PropertyEnums']['PropertyType'] );
1860 }
1861
1862 $standardstatus_array = array();
1863 $standardstatus_delete_array=array();
1864 if ( isset( $metadata_api_call['PropertyEnums']['StandardStatus'] ) && is_array( $metadata_api_call['PropertyEnums']['StandardStatus'] ) ) {
1865 $standardstatus_array = array_keys( $metadata_api_call['PropertyEnums']['StandardStatus'] );
1866 $standardstatus_delete_array= array_keys( $metadata_api_call['PropertyEnums']['StandardStatus'] );
1867 }
1868
1869 // if we do not have standart status
1870 if ( empty( $standardstatus_array ) ) {
1871 $standardstatus_array = $mlsstatus_array;
1872 $standardstatus_delete_array = $mlsstatus_array;
1873
1874 }
1875
1876
1877 $permited_status=array('active','active under contract','coming soon','activeundercontract','comingsoon','pending');
1878 $permited_status_lower = array_map('strtolower', $permited_status);
1879
1880 // Filter out permitted statuses from array1 values
1881 // $standardstatus_delete_array = array_filter($standardstatus_delete_array, function ($value) use ($permited_status_lower) {
1882 // return !in_array(strtolower($value), $permited_status_lower);
1883 // });
1884
1885
1886
1887
1888 $extracounty_values = '';
1889 $extracity_values = '';
1890
1891 $field_import = array(
1892 'City' => array(
1893 'label' => esc_html__( 'Select cities', 'mlsimport' ),
1894 'description' => esc_html__( 'Select the cities from where we will import data.', 'mlsimport' ),
1895 'type' => 'select',
1896 'multiple' => 'yes',
1897 'values' => $city_array,
1898 ),
1899
1900 'extraCity' => array(
1901 'label' => esc_html__( 'Add extra Cities', 'mlsimport' ),
1902 'description' => esc_html__( 'Add extra cities, separated by comma. They need to be written exactly like they are stored in MLS (for example all caps)', 'mlsimport' ),
1903 'type' => 'input',
1904 'multiple' => 'no',
1905 'values' => $extracity_values,
1906 ),
1907
1908 'CountyOrParish' => array(
1909 'label' => esc_html__( 'Select Counties', 'mlsimport' ),
1910 'description' => esc_html__( 'Select the counties from where we will import data.', 'mlsimport' ),
1911 'type' => 'select',
1912 'multiple' => 'yes',
1913 'values' => $county_array,
1914 'show_extra_field' => true,
1915 ),
1916
1917 'extraCounty' => array(
1918 'label' => esc_html__( 'Add extra Counties', 'mlsimport' ),
1919 'description' => esc_html__( 'Add extra counties, separated by comma. They need to be written exactly like they are stored in MLS (for example all caps)', 'mlsimport' ),
1920 'type' => 'input',
1921 'multiple' => 'no',
1922 'values' => $extracounty_values,
1923 ),
1924
1925 'PostalCode' => array(
1926 'label' => esc_html__( 'Select Postal Code', 'mlsimport' ),
1927 'description' => esc_html__( 'Select the PostalCode from where to import listings. Works with only one PostalCode.', 'mlsimport' ),
1928 'type' => 'input',
1929 'multiple' => 'no',
1930 ),
1931
1932 'PropertySubType' => array(
1933 'label' => esc_html__( 'Select Property Category', 'mlsimport' ),
1934 'description' => esc_html__( 'Property Category', 'mlsimport' ),
1935 'type' => 'select',
1936 'multiple' => 'yes',
1937 'values' => $propertysubtype_array,
1938 ),
1939 'PropertyType' => array(
1940 'label' => esc_html__( 'Select Property Action Category', 'mlsimport' ),
1941 'description' => esc_html__( 'Property Action Category', 'mlsimport' ),
1942 'type' => 'select',
1943 'multiple' => 'yes',
1944 'values' => $propertytype_array,
1945 ),
1946 'StandardStatus' => array(
1947 'label' => esc_html__( 'Select Status', 'mlsimport' ),
1948 'description' => __( 'The list is auto-populated with MLS available statuses. To select multiple statuses, use Ctrl (Windows) or Command (Mac).', 'mlsimport' ),
1949 'type' => 'select',
1950 'multiple' => 'yes',
1951 'values' => $standardstatus_array,
1952 ),
1953 'StandardStatusDelete' => array(
1954 'label' => esc_html__( 'Delete Statuses', 'mlsimport' ),
1955 'description' => __( 'Properties with these statuses will be deleted from your website after they are removed from MLS database. If you edit the field after importing, the changes will NOT apply to listings that have already been imported.', 'mlsimport' ),
1956 'type' => 'select',
1957 'multiple' => 'yes',
1958 'values' => $standardstatus_delete_array,
1959 ),
1960
1961 'InternetEntireListingDisplayYN' => array(
1962 'label' => esc_html__( 'Internet Entire Listing Display ', 'mlsimport'),
1963 'description' => esc_html__( 'A yes/no field that states the seller has allowed the listing to be displayed on Internet sites.', 'mlsimport' ),
1964 'type' => 'select',
1965 'multiple' => 'no',
1966 'values' => array(
1967 'yes',
1968 'no',
1969 ),
1970 ),
1971 'InternetAddressDisplayYN' => array(
1972 'label' => esc_html__( 'Internet Address display', 'mlsimport' ),
1973 'description' => esc_html__( 'A yes/no field that states the seller has allowed the listing address to be displayed on Internet sites.', 'mlsimport' ),
1974 'type' => 'select',
1975 'multiple' => 'no',
1976 'values' => array(
1977 'yes',
1978 'no',
1979 ),
1980 ),
1981 'ListAgentKey' => array(
1982 'label' => esc_html__( 'ListAgentKey', 'mlsimport' ),
1983 'description' => esc_html__( 'Import listings from a specific Agent (contact your MLS for this information)', 'mlsimport' ),
1984 'type' => 'input',
1985 'multiple' => 'no',
1986 ),
1987 'ListAgentMlsId' => array(
1988 'label' => esc_html__( 'ListAgentMlsId', 'mlsimport' ),
1989 'description' => esc_html__( 'Import listings from a specific Agent (contact your MLS for this information)', 'mlsimport' ),
1990 'type' => 'input',
1991 'multiple' => 'no',
1992 ),
1993 'ListOfficeKey' => array(
1994 'label' => esc_html__( 'ListOfficeKey', 'mlsimport' ),
1995 'description' => esc_html__( 'Import listings from a specific Office (contact your MLS for this information)', 'mlsimport'),
1996 'type' => 'input',
1997 'multiple' => 'no',
1998 ),
1999 'ListOfficeMlsId' => array(
2000 'label' => esc_html__( 'ListOfficeMlsId', 'mlsimport' ),
2001 'description' => esc_html__( 'Import listings from a specific Office (contact your MLS for this information)', 'mlsimport' ),
2002 'type' => 'input',
2003 'multiple' => 'no',
2004 ),
2005 'ListingId' => array(
2006 'label' => esc_html__( 'ListingId', 'mlsimport' ),
2007 'description' => esc_html__( 'Import One Property Only via parameter ListingID. If this does not work for you, please contact us to check if the field exists in your MLS.', 'mlsimport'),
2008 'type' => 'input',
2009 'multiple' => 'no',
2010 ),
2011 'Exclude_ListOfficeMlsId' => array(
2012 'label' => esc_html__( 'Exclude listings with ListOfficeMlsId', 'mlsimport' ),
2013 'description' => esc_html__( 'Exclude listings that belong to one or more ListOfficeMlsId.', 'mlsimport' ),
2014 'type' => 'input',
2015 'multiple' => 'no',
2016 ),
2017 'Exclude_ListOfficeKey' => array(
2018 'label' => esc_html__( 'Exclude listings with ListOfficeKey', 'mlsimport' ),
2019 'description' => esc_html__( 'Exclude listings that belong to one or more ListOfficeKey', 'mlsimport'),
2020 'type' => 'input',
2021 'multiple' => 'no',
2022 ),
2023
2024
2025 'Exclude_ListAgentMlsId' => array(
2026 'label' => esc_html__( 'Exclude listings with ListAgentMlsId', 'mlsimport' ),
2027 'description' => esc_html__( 'Exclude listings that belong to one or more ListAgentMlsId.', 'mlsimport' ),
2028 'type' => 'input',
2029 'multiple' => 'no',
2030 ),
2031 'Exclude_ListAgentKey' => array(
2032 'label' => esc_html__( 'Exclude listings with ListAgentKey ', 'mlsimport' ),
2033 'description' => esc_html__( 'Exclude listings that belong to one or more ListAgentKey ', 'mlsimport'),
2034 'type' => 'input',
2035 'multiple' => 'no',
2036 ),
2037
2038
2039 );
2040 return $field_import;
2041 }
2042
2043
2044
2045
2046
2047
2048
2049 /**
2050 *
2051 *
2052 * AYsnc Test
2053 */
2054 public function mlsimport_move_files_per_item() {
2055 check_ajax_referer( 'mlsimport_item_actions', 'security' );
2056 $post_id = 0;
2057 $how_many = 0;
2058 $max_number = 0;
2059 if(isset( $_POST['post_id'] )){
2060 $post_id = intval( $_POST['post_id'] );
2061 }
2062 if(isset( $_POST['how_many'] )){
2063 $how_many = intval( $_POST['how_many'] );
2064 }
2065 if(isset( $_POST['post_number'] )){
2066 $max_number = intval( $_POST['post_number'] );
2067 }
2068
2069 update_option( 'mlsimport_force_stop_' . $post_id, 'no', false );
2070
2071 $item_id_array = array(
2072 'item_id' => $post_id,
2073 'how_many' => $how_many,
2074 'max_number' => $max_number,
2075 'batch_counter' => 1,
2076 );
2077
2078 update_post_meta( $post_id, 'mlsimport_attach_to_move_' . $post_id, '' );
2079 $attachments_to_move = (array) $this->mlsimport_saas_generate_import_requests_per_item( $item_id_array );
2080
2081 update_post_meta( $post_id, 'mlsimport_attach_to_move_' . $post_id, $attachments_to_move );
2082
2083 $attachments_to_send = array(
2084 'args' => array(
2085 'attachments_to_move' => $post_id,
2086 'item_id_array' => $item_id_array,
2087 ),
2088 );
2089
2090 mlsimport_saas_single_write_import_custom_logs( 'Preparing the import. Please hold on.' . PHP_EOL );
2091 mlsimport_debuglogs_per_plugin( 'Preparing the import. Please hold on.' . PHP_EOL );
2092
2093 update_post_meta( $post_id, 'mlsimport_spawn_status', 'started' );
2094 as_enqueue_async_action( 'mlsimport_background_process_per_item', $attachments_to_send );
2095 spawn_cron();
2096
2097 unset( $attachments_to_send );
2098 die();
2099 }
2100
2101 /**
2102 *
2103 *
2104 * Processing Cron
2105 */
2106 public function mlsimport_background_process_per_item_cron_function( $input_arg ) {
2107 $log = 'In cron processing function ->' . wp_json_encode( $input_arg['item_id_array'] ) . PHP_EOL;
2108 mlsimport_saas_single_write_import_custom_logs( $log, 'cron' );
2109 global $mlsimport;
2110
2111 // Get from MLS Import it the big argument arrat
2112 $attachments_to_move = get_post_meta( $input_arg['item_id_array']['item_id'], 'mlsimport_cron_attach_to_move_' . $input_arg['item_id_array']['item_id'], true );
2113
2114 $log = 'In processing function mlsimport_cron_attach_to_move_ ->' . wp_json_encode( $attachments_to_move ) . PHP_EOL;
2115 mlsimport_saas_single_write_import_custom_logs( $log );
2116
2117
2118 // foreach($input_arg['attachments_to_move'] as $key=>$import_link){
2119 foreach ( $attachments_to_move as $key => $import_arguments ) {
2120 $GLOBALS['wp_object_cache']->delete( 'mlsimport_force_stop_' . $input_arg['item_id_array']['item_id'], 'options' );
2121
2122 $log = PHP_EOL . ' Cron Parsing importing batch : ' . $key . ' = ' . wp_json_encode( $import_arguments ) . PHP_EOL;
2123 mlsimport_saas_single_write_import_custom_logs( $log, 'cron' );
2124
2125 $api_call_array = $this->theme_importer->globalApiRequestCurlSaas( 'listings', $import_arguments, 'POST' );
2126
2127 $mlsimport->admin->theme_importer->mlsimportSaasCronParseSearchArrayPerItem( $api_call_array, $input_arg['item_id_array'], $key );
2128 }
2129
2130 mlsimport_saas_single_write_import_custom_logs( 'CRON JOB Import Completed ' . PHP_EOL );
2131 mlsimport_debuglogs_per_plugin( 'CRON JOB Import Completed ' . PHP_EOL );
2132 update_post_meta( $input_arg['item_id_array']['item_id'], 'mlsimport_spawn_status', 'completed' );
2133 }
2134
2135 /**
2136 *
2137 *
2138 * Generate import Requests per item
2139 */
2140 public function mlsimport_saas_generate_import_requests_per_item( $item_id_array, $last_date = '' ) {
2141 $import_step = 25;
2142
2143 $prop_id = $item_id_array['item_id'];
2144 $max_found = $item_id_array['max_number'];
2145 $how_many = $item_id_array['how_many'];
2146 if ( 0=== intval($how_many) ) {
2147 $how_many = $max_found;
2148 }
2149 if ( $how_many > $max_found ) {
2150 $how_many = $max_found;
2151 }
2152
2153 $search_url_step = '';
2154 $urls_array = array();
2155
2156 $skip = 0;
2157 if ( $how_many > 10000 ) {
2158 $how_many = 10000;
2159 }
2160
2161 if ( $how_many < $import_step ) {
2162 $import_step = $how_many;
2163 }
2164
2165 while ( $skip < $how_many ) {
2166 $search_url_step = $this->mlsimport_saas_make_listing_requests_arguments( $prop_id, $last_date, $skip, $import_step );
2167 $skip = $skip + $import_step;
2168 $urls_array[] = $search_url_step;
2169 }
2170 return $urls_array;
2171 }
2172
2173
2174
2175
2176
2177 /**
2178 * Process Async function
2179 */
2180 public function mlsimport_background_process_per_item_function( $input_arg ) {
2181
2182 $mlsimportItemId = $input_arg['item_id_array']['item_id'];
2183 $log_prefix = 'In processing function - Item ID: ' . $mlsimportItemId . ' -> ';
2184 mlsimport_saas_single_write_import_custom_logs( $log_prefix . wp_json_encode( $input_arg['item_id_array'] ) . PHP_EOL );
2185
2186 //ini_set('memory_limit', '256M');
2187 //ini_set('max_execution_time', 0); // Unlimited execution time
2188
2189 // Get from MLS Import the big argument array only once
2190 $attachments_to_move = get_post_meta( $mlsimportItemId, 'mlsimport_attach_to_move_' . $mlsimportItemId, true );
2191
2192 // Retrieve all meta data in one go to reduce database queries
2193 $mlsimport_item_option_data = array(
2194 'mlsimport_item_standardstatus' => get_post_meta( $mlsimportItemId, 'mlsimport_item_standardstatus', true ),
2195 'mlsimport_item_standardstatusdelete' => get_post_meta( $mlsimportItemId, 'mlsimport_item_standardstatusdelete', true ),
2196 'mlsimport_item_property_user' => get_post_meta( $mlsimportItemId, 'mlsimport_item_property_user', true ),
2197 'mlsimport_item_agent' => get_post_meta( $mlsimportItemId, 'mlsimport_item_agent', true ),
2198 'mlsimport_item_property_status' => get_post_meta( $mlsimportItemId, 'mlsimport_item_property_status', true ),
2199 );
2200
2201 $total_batches = count( $attachments_to_move );
2202
2203 // removed because $this
2204 global $mlsimport;
2205
2206 $log = 'In processing function $attachments_to_move ->' . wp_json_encode( $attachments_to_move ) . PHP_EOL;
2207 mlsimport_saas_single_write_import_custom_logs( $log );
2208 // mlsimport_debuglogs_per_plugin($log);
2209 print esc_html($log);
2210
2211 foreach ( $attachments_to_move as $key => $import_arguments ) {
2212 // reconsider use
2213 // $GLOBALS['wp_object_cache']->delete('mlsimport_force_stop_' . $input_arg['item_id_array']['item_id'], 'options');
2214 $status = get_option( 'mlsimport_force_stop_' . $mlsimportItemId );
2215 if ( 'no' === $status ) {
2216 // reconsider use
2217 // wp_cache_flush();
2218 $mem_usage = memory_get_usage( true );
2219 $mem_usage_show = round( $mem_usage / 1048576, 2 );
2220
2221
2222
2223 mlsimport_saas_single_write_import_custom_logs( $log );
2224 $log = 'Parsing import batch: ' . ( $key + 1 ) . ' of ' . $total_batches . '. Memory used: ' . $mem_usage_show . ' MB.' . PHP_EOL;
2225
2226 // Combine logs and reduce function calls
2227 mlsimport_saas_single_write_import_custom_logs( $log );
2228 mlsimport_debuglogs_per_plugin( $log );
2229 print esc_html($log);
2230
2231 $api_call_array = $this->theme_importer->globalApiRequestCurlSaas( 'listings', $import_arguments, 'POST' );
2232
2233 $mlsimport->admin->theme_importer->mlsimportSaasParseSearchArrayPerItem( $api_call_array, $input_arg['item_id_array'], $key, $mlsimport_item_option_data );
2234 } else {
2235 update_post_meta( $mlsimportItemId, 'mlsimport_spawn_status', 'completed' );
2236 delete_post_meta( $mlsimportItemId, 'mlsimport_attach_to_move_' . $mlsimportItemId );
2237 mlsimport_saas_single_write_import_custom_logs( PHP_EOL . 'Parsing importing link FORCE STOP : ' );
2238 mlsimport_debuglogs_per_plugin( 'Parsing importing link FORCE STOP : ' );
2239 break; // Exit the loop if forced to stop
2240 }
2241 }
2242
2243 mlsimport_saas_single_write_import_custom_logs( 'Import Completed ' . PHP_EOL );
2244 mlsimport_debuglogs_per_plugin( 'Import Completed ' . PHP_EOL );
2245
2246 update_post_meta( $mlsimportItemId, 'mlsimport_spawn_status', 'completed' );
2247 delete_post_meta( $mlsimportItemId, 'mlsimport_attach_to_move_' . $mlsimportItemId );
2248
2249 unset( $attachments_to_move );
2250 unset( $api_call_array );
2251 unset( $input_arg );
2252 unset( $log );
2253 unset( $log2 );
2254 }
2255
2256
2257
2258
2259
2260 /**
2261 *
2262 *
2263 *
2264 * update log function
2265 */
2266 public function mlsimport_logger_per_item() {
2267 check_ajax_referer( 'mlsimport_item_actions', 'security' );
2268 $post_id=0;
2269 if(isset($_POST['post_id'] )){
2270 $post_id = intval( $_POST['post_id'] );
2271 }
2272
2273 $status = get_post_meta( $post_id, 'mlsimport_spawn_status', true );
2274 $path = WP_PLUGIN_DIR . '/mlsimport/logs/status_logs.log';
2275 $logs = file_get_contents( $path );
2276
2277 $force_status = intval( get_post_meta( $post_id, 'mlsimport_force_stop', true ) );
2278 $force_status = get_option( 'mlsimport_force_stop_' . $post_id );
2279
2280 if ( 'no' !== $force_status ) {
2281 echo wp_json_encode(
2282 array(
2283 'is_done' => 'done',
2284 'status' => $status,
2285 'logs' => $logs,
2286 )
2287 );
2288 die();
2289 }
2290
2291 if ( '' === $status || 'completed' === $status ) {
2292 echo wp_json_encode(
2293 array(
2294 'is_done' => 'done',
2295 'status' => $status,
2296 'logs' => $logs,
2297 )
2298 );
2299 } else {
2300 // return from log
2301 echo wp_json_encode(
2302 array(
2303 'is_done' => 'wip',
2304 'status' => $status,
2305 'logs' => $logs,
2306 )
2307 );
2308 }
2309 die();
2310 }
2311
2312
2313
2314
2315
2316
2317 /**
2318 *
2319 *
2320 *
2321 *
2322 * Force Stop Import
2323 */
2324 public function mlsimport_stop_import_per_item() {
2325 check_ajax_referer( 'mlsimport_item_actions', 'security' );
2326 $post_id=0;
2327 if(isset($_POST['post_id'] )){
2328 $post_id = intval( $_POST['post_id'] );
2329 }
2330 update_option( 'mlsimport_force_stop_' . $post_id, 'yes', false );
2331 mlsimport_saas_single_write_import_custom_logs( 'Stopeed for ' . $post_id . PHP_EOL );
2332 mlsimport_debuglogs_per_plugin( 'Stopeed for ' . $post_id . PHP_EOL );
2333
2334 die();
2335 }
2336
2337
2338
2339 /*
2340 *
2341 * Get MLS Metadata
2342 *
2343 *
2344 *
2345 *
2346 **/
2347
2348 public function mlsimport_saas_get_metadata_function() {
2349 check_ajax_referer( 'mlsimport_saas_get_metadata', 'security' );
2350 $theme_Start = new ThemeImport();
2351
2352 $values = array();
2353 $options = get_option( $this->plugin_name . '_admin_options' );
2354 $url = 'clients?theme_id=' . intval( $options['mlsimport_theme_used'] );
2355
2356 $answer = $theme_Start::globalApiRequestSaas( $url, $values, 'GET' );
2357
2358 update_option( 'mlsimport_mls_metadata_populated', 'yes' );
2359
2360 update_option( 'mlsimport_mls_metadata_theme_schema', $answer['theme_schema'] );
2361 update_option( 'mlsimport_mls_metadata_mls_data', $answer['mls_data']['mls_meta_data'] );
2362 update_option( 'mlsimport_mls_metadata_mls_enums', $answer['mls_data']['mls_meta_enums'] );
2363 }
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376 /**
2377 *
2378 *
2379 * write debug logs
2380 */
2381 public function mlsimport_debuglog_cron( $message ) {
2382 if ( is_array( $message ) ) {
2383 $message = wp_json_encode( $message );
2384 }
2385 $message = date( 'F j, Y, g:i a' ) . ' -> ' . $message;
2386 global $wp_filesystem;
2387 if ( empty( $wp_filesystem ) ) {
2388 require_once ABSPATH . '/wp-admin/includes/file.php';
2389 WP_Filesystem();
2390 }
2391
2392 $path = WP_PLUGIN_DIR . '/mlsimport/logs/cron_logs.log';
2393
2394 file_put_contents( $path, $message, FILE_APPEND | LOCK_EX );
2395 }
2396
2397 }
2398