PluginProbe
MLSImport: IDX Plugin & MLS Plugin for Real Estate Listings / 5.8.3
MLSImport: IDX Plugin & MLS Plugin for Real Estate Listings v5.8.3
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.3, at admin/class-mlsimport-admin.php

2,355 lines 73.8 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 $options = get_option( $this->plugin_name . '_admin_fields_select' );
546 echo 'ListingKey: '.get_post_meta( $post->ID, 'ListingKey', true ).'<br>';
547 foreach ( $options['mls-fields-admin'] as $key => $value ) {
548 if ( 1 === intval($options['mls-fields-admin'][ $key ] ) ) {
549 if ( isset( $options['mls-fields-label'][ $key ] ) && '' !== $options['mls-fields-label'][ $key ] ) {
550 $key = $options['mls-fields-label'][ $key ];
551 }
552
553 if ( 'ListingKey' !== $key ) {
554 $meta_key = strtolower( $key );
555 } else {
556 $meta_key = $key;
557 }
558 ?>
559
560 <strong><?php echo esc_html($key);?>:</strong>
561 <?php echo esc_html( get_post_meta( $post->ID, $meta_key, true ) ); ?> </br>
562 <?php
563 }
564 }
565 ?>
566
567 <h2 style="font-weight:bold;padding-left:0px;">Mls Import History</h2>
568 <?php
569 $meta = get_post_meta( $post->ID, 'mlsimport_property_history', true );
570 if ( '' === trim( $meta ) ) { ?>
571 <strong>Property history is blank - you can enable it in Settings/ Tools page </strong>
572 <?php
573 } else {
574 print wp_kses_post($meta);
575 }
576 }
577
578
579
580
581 /**
582 * delete cache
583 */
584 function mlsimport_delete_cache() {
585
586 check_ajax_referer( 'mlsimport_tool_actions', 'security' );
587
588 delete_transient( 'mlsimport_token_request' );
589 delete_transient( 'mlsimport_metadata_api_call_data_service_property' );
590 delete_transient( 'mls_import_meta_enums' );
591 delete_transient( 'mls_import_meta' );
592 delete_transient( 'mlsimport_plugin_data_schema' );
593 delete_transient( 'mlsimport_ready_to_go_mlsimport_data' );
594 delete_transient( 'mlsimport_saas_token' );
595
596 delete_option( 'mlsimport_mls_metadata_populated' );
597 die( 'deleted' );
598 }
599
600 /**
601 *
602 *
603 *
604 *
605 * delete properties
606 */
607 function mlsimport_delete_properties() {
608 $error = false;
609 global $mlsimport;
610
611 check_ajax_referer( 'mlsimport_tool_actions', 'security' );
612
613
614 if ( current_user_can( 'administrator' ) ) :
615 $mlsimport_delete_category = sanitize_text_field( wp_unslash( $_POST['mlsimport_delete_category'] ) ) ;
616 $mlsimport_delete_category_term = sanitize_title( sanitize_text_field( wp_unslash( $_POST['mlsimport_delete_category_term']) ) );
617 $mlsimport_delete_timeout = intval( $_POST['mlsimport_delete_timeout'] );
618
619 if ( '' === $mlsimport_delete_category ) {
620 $error_message = esc_html__('Category cannot be blank','mlsimport');
621 $error = true;
622 }
623
624 if ( '' === $mlsimport_delete_category_term ) {
625 $error_message = esc_html__('Category Term cannot be blank','mlsimport');
626 $error = true;
627 }
628
629 $category = get_term_by( 'slug', $mlsimport_delete_category_term, $mlsimport_delete_category );
630
631 if ( $error ) {
632 print wp_json_encode(
633 array(
634 'message' => esc_html($error_message),
635 )
636 );
637 } else {
638 $mlsimport_delete_category_term_array = array();
639
640 $mlsimport_delete_category_term_array[] = $mlsimport_delete_category_term;
641 $tax_array = array(
642 'taxonomy' => $mlsimport_delete_category,
643 'field' => 'slug',
644 'terms' => $mlsimport_delete_category_term_array,
645 );
646
647 $args = array(
648 'post_type' => array( 'estate_property', 'property' ),
649 'post_status' => 'any',
650 'paged' => 1,
651 'posts_per_page' => -1,
652 'tax_query' => array(
653 $tax_array,
654 ),
655 'fields' => 'ids',
656 );
657
658 $prop_selection = new WP_Query( $args );
659
660 foreach ( $prop_selection->posts as $key => $delete_get_id ) {
661 if ( 0 !== $mlsimport_delete_timeout ) {
662 set_timeout( $mlsimport_delete_timeout );
663 }
664
665 $mlsimport->admin->theme_importer->mlsimport_saas_delete_property_via_mysql( $delete_get_id, ' delete from tools ' );
666 }
667
668 wp_update_term_count_now( array( $category->term_id ), $mlsimport_delete_category );
669
670 print wp_json_encode(
671 array(
672 '$category' => $category->term_id,
673 'arguments' => $args,
674 'posts' => $prop_selection->posts,
675 'found' => $prop_selection->found_posts,
676 'message' => 'Done...',
677 )
678 );
679 }
680 endif;
681 die();
682 }
683
684
685
686
687 /**
688 *
689 *
690 * Testing enviroment variables
691 */
692 public function mlsimport_saas_setting_up() {
693 if (intval(WP_MEMORY_LIMIT) < 256): ?>
694 <div class="mlsimport_warning long_warning">
695 <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>
696 </div>
697 <?php endif; ?>
698
699 <?php
700 $max_input_vars = ini_get('max_input_vars');
701 if ($max_input_vars < 2000):
702 ?>
703 <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>
704 <?php endif; ?>
705
706 <?php
707 $max_time = ini_get('max_execution_time');
708 if ($max_time < 600 && 0 !== $max_time):
709 ?>
710 <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>
711 <?php endif;
712
713 }
714
715 /**
716 * Check if token validates with MLS
717 *
718 * @since 4.0.1
719 * returns token fron mlsimport
720 */
721 public function mlsimport_saas_check_mls_connection() {
722
723 $values = array();
724 $options = get_option( $this->plugin_name . '_admin_options' );
725
726 $mls_id = '';
727 if ( isset( $options['mlsimport_mls_name'] ) ) {
728 $mls_id = sanitize_text_field( trim( $options['mlsimport_mls_name'] ) );
729 }
730
731 $mls_token = '';
732 if ( isset( $options['mlsimport_mls_name'] ) ) {
733 $mls_token = sanitize_text_field( trim( $options['mlsimport_mls_token'] ) );
734 }
735
736 $mlsimport_tresle_client_id = '';
737 if ( isset( $options['mlsimport_tresle_client_id'] ) ) {
738 $mlsimport_tresle_client_id = sanitize_text_field( trim( $options['mlsimport_tresle_client_id'] ) );
739 }
740
741 $mlsimport_tresle_client_secret = '';
742 if ( isset( $options['mlsimport_tresle_client_secret'] ) ) {
743 $mlsimport_tresle_client_secret = sanitize_text_field( trim( $options['mlsimport_tresle_client_secret'] ) );
744 }
745
746 // rapattoni data
747 $mlsimport_rapattoni_client_id = '';
748 if ( isset( $options['mlsimport_rapattoni_client_id'] ) ) {
749 $mlsimport_rapattoni_client_id = sanitize_text_field( trim( $options['mlsimport_rapattoni_client_id'] ) );
750 }
751 $mlsimport_rapattoni_client_secret = '';
752 if ( isset( $options['mlsimport_rapattoni_client_secret'] ) ) {
753 $mlsimport_rapattoni_client_secret = sanitize_text_field( trim( $options['mlsimport_rapattoni_client_secret'] ) );
754 }
755
756 $mlsimport_rapattoni_username = '';
757 if ( isset( $options['mlsimport_rapattoni_username'] ) ) {
758 $mlsimport_rapattoni_username = sanitize_text_field( trim( $options['mlsimport_rapattoni_username'] ) );
759 }
760
761 $mlsimport_rapattoni_password = '';
762 if ( isset( $options['mlsimport_rapattoni_password'] ) ) {
763 $mlsimport_rapattoni_password = sanitize_text_field( trim( $options['mlsimport_rapattoni_password'] ) );
764 }
765
766 // paragon data
767 $mlsimport_paragon_client_id = '';
768 if ( isset( $options['mlsimport_paragon_client_id'] ) ) {
769 $mlsimport_paragon_client_id = sanitize_text_field( trim( $options['mlsimport_paragon_client_id'] ) );
770 }
771 $mlsimport_paragon_client_secret = '';
772 if ( isset( $options['mlsimport_paragon_client_secret'] ) ) {
773 $mlsimport_paragon_client_secret = sanitize_text_field( trim( $options['mlsimport_paragon_client_secret'] ) );
774 }
775
776 if ( trim( $mls_token ) === '' ) {
777 if ( intval( $mls_id ) > 900 && intval( $mls_id ) < 3000 ) {
778 if ( trim( $mlsimport_tresle_client_id ) === '' || trim( $mlsimport_tresle_client_secret ) === '' ) {
779 return;
780 }
781 } elseif ( intval( $mls_id ) >= 5000 && intval( $mls_id ) < 6000 ) {
782 if (
783 trim( $mlsimport_rapattoni_client_id ) === '' ||
784 trim( $mlsimport_rapattoni_client_secret ) === '' ||
785 trim( $mlsimport_rapattoni_username ) === '' ||
786 trim( $mlsimport_rapattoni_password ) === ''
787 ) {
788 return;
789 }
790 } elseif ( intval( $mls_id ) >= 6000 ) {
791 if (
792 trim( $mlsimport_paragon_client_id ) === '' ||
793 trim( $mlsimport_paragon_client_secret ) === ''
794 ) {
795 return;
796 }
797 }
798 }
799
800 $values['mls_token'] = $mls_token;
801 $values['mls_id'] = $mls_id;
802 $values['mlsimport_tresle_client_id'] = $mlsimport_tresle_client_id;
803 $values['mlsimport_tresle_client_secret'] = $mlsimport_tresle_client_secret;
804
805 $values['mlsimport_rapattoni_client_id'] = $mlsimport_rapattoni_client_id;
806 $values['mlsimport_rapattoni_client_secret'] = $mlsimport_rapattoni_client_secret;
807 $values['mlsimport_rapattoni_username'] = $mlsimport_rapattoni_username;
808 $values['mlsimport_rapattoni_password'] = $mlsimport_rapattoni_password;
809
810 $values['mlsimport_paragon_client_id'] = $mlsimport_paragon_client_id;
811 $values['mlsimport_paragon_client_secret'] = $mlsimport_paragon_client_secret;
812
813 $answer = $this->theme_importer->globalApiRequestSaas( 'clients', $values, 'PATCH' );
814
815
816
817
818 if ( isset( $answer['succes'] ) && true === $answer['succes'] ) {
819 if ( isset( $answer['tested'] ) && true === $answer['tested'] ) {
820 update_option( 'mlsimport_connection_test', 'yes' );
821 } else {
822 delete_option( 'mlsimport_connection_test' );
823 delete_option( 'mlsimport_mls_metadata_populated' );
824 }
825 } else {
826 delete_option( 'mlsimport_connection_test' );
827 delete_option( 'mlsimport_mls_metadata_populated' );
828 }
829
830 return $answer;
831 }
832
833
834
835
836
837
838 /**
839 * Request auth token from mlsimport.net
840 *
841 * @since 4.0.1
842 * returns token fron mlsimport
843 */
844 public function mlsimport_saas_get_mls_api_token_from_transient() {
845
846 $token = get_transient( 'mlsimport_saas_token' );
847
848 if ( false === $token || '' === $token ) {
849 $token_json_answer = $this->mlsimport_saas_get_mls_api_token();
850
851
852 if ( isset( $token_json_answer['succes'] ) && true === $token_json_answer['succes'] ) {
853 $token = $token_json_answer['token'];
854
855 set_transient( 'mlsimport_saas_token', $token, 6 * 60 * 60 );
856 }
857 }
858
859 return $token;
860 }
861
862
863 /**
864 * call for token
865 *
866 * @since 4.0.1
867 * returns token fron mlsimport
868 */
869 protected function mlsimport_saas_get_mls_api_token() {
870 $values = array();
871 $options = get_option( $this->plugin_name . '_admin_options' );
872
873 $username = '';
874 if ( isset( $options['mlsimport_username'] ) ) {
875 $username = sanitize_text_field( trim( $options['mlsimport_username'] ) );
876 }
877
878 $password = '';
879 if ( isset( $options['mlsimport_password'] ) ) {
880 $password = sanitize_text_field( trim( $options['mlsimport_password'] ) );
881 }
882 $mls_name = '';
883 if ( isset( $options['mlsimport_mls_name'] ) ) {
884 $mls_name = sanitize_text_field( trim( $options['mlsimport_mls_name'] ) );
885 }
886
887 $mls_token = '';
888 if ( isset( $options['mlsimport_mls_token'] ) ) {
889 $mls_token = sanitize_text_field( trim( $options['mlsimport_mls_token'] ) );
890 }
891
892 $values['username'] = $username;
893 $values['password'] = $password;
894
895 if ( '' === $username || '' === $password ) {
896 return '';
897 }
898
899 $theme_Start = new ThemeImport();
900 $answer = $theme_Start::globalApiRequestSaas( 'token', $values, 'POST' );
901
902
903
904 return $answer;
905 }
906
907
908
909
910
911
912 /**
913 * save meta options
914 *
915 * @since 3.0.1
916 */
917 public function mlsimport_item_product_metaboxes() {
918 add_meta_box( 'mlsimport_item_metaboxes-sectionid', __( 'Set Import data', 'mlsimport' ), array( $this, 'mlsimport_saas_display_meta_options' ), 'mlsimport_item', 'normal', 'default' );
919 }
920
921
922
923 /**
924 *
925 *
926 *
927 * save meta options
928 *
929 * @since 3.0.1
930 */
931 public function mlsimport_item_product_save_metaboxes( $post_id, $post ) {
932
933 if ( ! is_object( $post ) || ! isset( $post->post_type ) ) {
934 return;
935 }
936
937 if ( 'mlsimport_item' !== $post->post_type ) {
938 return;
939 }
940
941 $allowed_keys = array(
942 'mlsimport_item_how_many',
943 'mlsimport_item_title_format',
944 'mlsimport_item_agent',
945 'mlsimport_item_property_status',
946 'mlsimport_item_property_user',
947 'mlsimport_item_min_price',
948 'mlsimport_item_max_price',
949 'mlsimport_item_city_check',
950 'mlsimport_item_city',
951 'mlsimport_item_city[]',
952 'mlsimport_item_countyorparish_check',
953 'mlsimport_item_countyorparish',
954 'mlsimport_item_mlsstatus_check',
955 'mlsimport_item_mlsstatus',
956 'mlsimport_item_propertysubtype_check',
957 'mlsimport_item_propertysubtype',
958 'mlsimport_item_propertytype_check',
959 'mlsimport_item_propertytype',
960 'mlsimport_item_standardstatus_check',
961 'mlsimport_item_standardstatus',
962 'mlsimport_item_standardstatusdelete_check',
963 'mlsimport_item_standardstatusdelete',
964
965 'mlsimport_item_internetentirelistingdisplayyn',
966 'mlsimport_item_internetaddressdisplayyn',
967 'mlsimport_item_stat_cron',
968 'mlsimport_item_listagentkey',
969 'mlsimport_item_listagentmlsid',
970 'mlsimport_item_listofficekey',
971 'mlsimport_item_postalcode',
972 'mlsimport_item_listofficemlsid',
973 'mlsimport_item_listingid',
974 'mlsimport_item_extracity',
975 'mlsimport_item_extracounty',
976 'mlsimport_item_exclude_listofficemlsid',
977 'mlsimport_item_exclude_listofficekey',
978 'mlsimport_item_exclude_listagentmlsid',
979 'mlsimport_item_exclude_listagentkey',
980 );
981
982
983
984
985 foreach ( $allowed_keys as $key => $key_value ) {
986 if( isset($_POST[$key_value]) ){
987 $postmeta = mlsimport_sanitize_multi_dimensional_array ( $_POST[$key_value] ) ;
988 update_post_meta( $post_id, sanitize_key( $key_value ), $postmeta );
989 }
990
991 }
992
993 $blank_keys = array(
994 'mlsimport_item_standardstatus',
995 'mlsimport_item_city',
996 'mlsimport_item_countyorparish',
997 'mlsimport_item_propertysubtype',
998 'mlsimport_item_propertytype',
999 'mlsimport_item_standardstatus',
1000 'mlsimport_item_listingid',
1001
1002 );
1003
1004 foreach ( $blank_keys as $key ) {
1005 if ( ! isset( $_POST[ $key ] ) ) {
1006 update_post_meta( $post_id, $key, '' );
1007 }
1008 }
1009
1010
1011 }
1012
1013
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');
1021 global $mlsimport;
1022
1023 $postId = $post->ID;
1024 $token = $mlsimport->admin->mlsimport_saas_get_mls_api_token_from_transient();
1025
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;
1035
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 }
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>
1070 <ul>
1071 <li>1. Set the import parameters.</li>
1072 <li>2. Hit Publish or Update, otherwise import will not work correctly.</li>
1073 <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>
1074 <li>4. Press the Update button after you make any change in the import settings.</li>
1075 </ul>
1076
1077 <?php if (is_numeric($foundItems) && $foundItems >= 500): ?>
1078 <div class="mlsimport_notification">
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'); ?>
1080 </div>
1081 <?php endif; ?>
1082
1083 <div class="mlsimport_import_no">
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>
1087
1088 <fieldset class="mlsimport-fieldset">
1089 <label class="mlsimport-label" for="mlsimport_item_how_many">
1090 <?php esc_html_e('How Many to import. Use 0 if you want to import all listings found.', 'mlsimport'); ?>
1091 </label>
1092 <input type="text" id="mlsimport_item_how_many" name="mlsimport_item_how_many" value="<?php echo esc_attr($mlsimportItemHowMany); ?>"/>
1093 </fieldset>
1094
1095 <fieldset class="mlsimport-fieldset mlsimport_auto_switch">
1096 <?php esc_html_e('Enable Auto Update every hour?', 'mlsimport'); ?>
1097 <label class="mlsimport_switch">
1098 <input type="hidden" value="0" name="mlsimport_item_stat_cron">
1099 <input type="checkbox" value="1" name="mlsimport_item_stat_cron"<?php if (intval($mlsimportItemStatCron) !== 0) echo esc_html(' checked'); ?>>
1100 <span class="slider round"></span>
1101 </label>
1102 </fieldset>
1103
1104 <?php if ($mlsimportItemStatCron !== ''): ?>
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($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 <?php endif; ?>
1113
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;
1187 }
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 <fieldset class="mlsimport-fieldset">
1221 <label class="mlsimport-label <?php echo esc_attr($extraClass); ?>" for="<?php echo esc_attr($name); ?>">
1222 <?php echo esc_html($field['label']); ?>
1223 </label>
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($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 }
1281
1282
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; ?>
1311 </fieldset>
1312 <?php endforeach; ?>
1313
1314 </div>
1315 <?php
1316 return ob_get_clean();
1317 }
1318
1319
1320
1321
1322
1323
1324 public function mlsimport_add_extra_fields() {
1325 }
1326
1327 /**
1328 *
1329 *
1330 *
1331 *
1332 *
1333 *
1334 */
1335 function mlsimport_notes_for_mls( $mlsimport_mls_id, $name, $description ) {
1336 // 111 - Rae Edmonton
1337
1338 if ( 111 === intval($mlsimport_mls_id) && 'mlsimport_item_standardstatus' === $name ) {
1339 return esc_html__( 'Your MLS does not use this field - all listings are considered Active.', 'mlsimport' );
1340 } else {
1341 return $description;
1342 }
1343 }
1344
1345
1346 /**
1347 *
1348 *
1349 * Get Last date
1350 */
1351 public function mlsimport_saas_get_last_date( $item_id ) {
1352 $last_date = get_post_meta( $item_id, 'mlsimport_last_date', true );
1353
1354 if ( '' === $last_date ) {
1355 $last_date = $this->mlsimport_saas_update_last_date( $item_id );
1356 }
1357
1358 return $last_date;
1359 }
1360
1361
1362 /**
1363 *
1364 *
1365 * Save Last date
1366 */
1367 public function mlsimport_saas_update_last_date( $item_id ) {
1368
1369 $unix_time = current_time( 'timestamp', 0 ) - ( 2 * 60 * 60 );
1370 $last_date_to_save = date( 'Y-m-d\TH:i', $unix_time );
1371 update_post_meta( $item_id, 'mlsimport_last_date', $last_date_to_save );
1372
1373 return $last_date_to_save;
1374 }
1375
1376
1377
1378 /**
1379 *
1380 *
1381 * Check if there are modified items in the last 2h
1382 */
1383 public function mlsimport_saas_start_cron_links_per_item( $item_id ) {
1384
1385 $last_date = $this->mlsimport_saas_get_last_date( $item_id );
1386
1387 esc_html_e('we work with','mlsimport').' '. esc_html($last_date ). PHP_EOL;
1388
1389 $mlsrequest = $this->mlsimport_make_listing_requests( $item_id, $last_date );
1390 $found_items = 0;
1391 if ( isset( $mlsrequest['results'] ) ) {
1392 $found_items = intval( $mlsrequest['results'] );
1393 } else {
1394 delete_transient( 'mlsimport_saas_token' );
1395 }
1396
1397 print esc_html__('we found ','mlsimport') .esc_html( $found_items ). '</br>' . PHP_EOL;
1398
1399 $attachments_to_move = array();
1400
1401 if ( $found_items > 0 ) {
1402 $item_id_array = array(
1403 'item_id' => $item_id,
1404 'how_many' => 0,
1405 'max_number' => $found_items,
1406 'batch_counter' => 1,
1407 );
1408
1409 $attachments_to_move = (array) $this->mlsimport_saas_generate_import_requests_per_item( $item_id_array, $last_date );
1410
1411 update_post_meta( $item_id, 'mlsimport_spawn_status_cron_job', 'started' );
1412 update_post_meta( $item_id, 'mlsimport_cron_attach_to_move_' . $item_id, $attachments_to_move );
1413
1414 // save last date for next run
1415 $this->mlsimport_saas_update_last_date( $item_id );
1416
1417 $attachments_to_send = array(
1418 'args' => array(
1419 'attachments_to_move' => $item_id,
1420 'item_id_array' => $item_id_array,
1421 ),
1422 );
1423
1424 $this->mlsimport_background_process_per_item_cron_function( $attachments_to_send['args'] );
1425
1426 }
1427 }
1428
1429
1430
1431
1432 /**
1433 *
1434 *
1435 * Reconciliation log
1436 */
1437 public function mlsimport_saas_start_doing_reconciliation() {
1438 global $mlsimport;
1439 print 'start';
1440 $listingKey_in_Local = $this->mlsimport_saas_get_all_meta_values( 'ListingKey' );
1441
1442 if ( empty( $listingKey_in_Local ) ) {
1443 return;
1444 }
1445
1446
1447 $mls_data = $this->mlsimport_saas_get_mls_reconciliation_data();
1448 $listingKey_in_MLS = $mls_data['all_data'];
1449
1450 if ( empty( $listingKey_in_MLS ) ) {
1451 return;
1452 }
1453
1454 $to_delete = 0;
1455 $counter = 0;
1456 foreach ( $listingKey_in_Local as $key => $item ) {
1457 $listingkey = $item->meta_value;
1458 $property_id = $item->iD;
1459 ++$counter;
1460
1461 if ( in_array( $listingkey, $listingKey_in_MLS ) ) {
1462 print wp_kses_post('</br>'.$listingkey . ' IS FOUND');
1463 } else {
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
1474 }
1475 }
1476
1477 print esc_html(' to delete:' .$to_delete);
1478 return;
1479 }
1480
1481
1482 /**
1483 *
1484 *
1485 * Requestq Reconciliation log
1486 */
1487 public function mlsimport_saas_get_mls_reconciliation_data() {
1488
1489 $arguments = array();
1490 $answer = $this->theme_importer->globalApiRequestCurlSaas( 'reconciliation', $arguments, 'GET' );
1491 return $answer;
1492 }
1493
1494 /**
1495 *
1496 *
1497 * Reconciliation get local data
1498 */
1499 public function mlsimport_saas_get_all_meta_values( $key ) {
1500 global $wpdb;
1501
1502 $result = $wpdb->get_results(
1503 $wpdb->prepare(
1504 "
1505 SELECT DISTINCT pm.meta_value,p.iD FROM {$wpdb->postmeta} pm
1506 LEFT JOIN {$wpdb->posts} p ON p.ID = pm.post_id
1507 WHERE pm.meta_key = %s
1508 AND p.post_status = 'publish'",
1509 $key
1510 )
1511 );
1512
1513 return $result;
1514 }
1515
1516
1517
1518
1519
1520 /*
1521 * Do api Listing Requests
1522 *
1523 *
1524 *
1525 *
1526 * */
1527 public function mlsimport_make_listing_requests( $item_id, $last_date = '', $skip = '', $top = '' ) {
1528 $options = get_option( $this->plugin_name . '_admin_options' );
1529 $mls_id = '';
1530 if ( isset( $options['mlsimport_mls_name'] ) ) {
1531 $mls_id = sanitize_text_field( trim( $options['mlsimport_mls_name'] ) );
1532 }
1533
1534 $arguments = $this->mlsimport_saas_make_listing_requests_arguments( $item_id, $last_date, $skip, $top );
1535
1536
1537 if (
1538 $mls_id > 5000 && $mls_id < 6000 &&
1539 ( ! isset( $arguments['property_type'] ) or
1540 ( isset( $arguments['property_type'] ) && '' === $arguments['property_type'] ) or
1541 ( isset( $arguments['property_type'][0] ) && '' === $arguments['property_type'][0] )
1542 )
1543 ) {
1544 return array(
1545 'success' => false,
1546 'type' => 'rapattoni',
1547 'message' => esc_html__( 'This MLS requires to have one item selected from "Property Action Category" dropdown', 'mlsimport' ),
1548 );
1549 }
1550
1551 $potential_leght = strlen( wp_json_encode( $arguments ) );
1552 if ( $potential_leght > 1750 ) {
1553 return array(
1554 'success' => false,
1555 'potential_leght' => $potential_leght,
1556 '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' ),
1557 );
1558 }
1559
1560 $answer = $this->theme_importer->globalApiRequestCurlSaas( 'listings', $arguments, 'POST' );
1561 $answer['potential_leght'] = $potential_leght;
1562 return ( $answer );
1563 }
1564
1565
1566
1567
1568
1569
1570 /*
1571 * Create Api query arguments
1572 *
1573 *
1574 *
1575 *
1576 *
1577 * */
1578
1579 public function mlsimport_saas_make_listing_requests_arguments( $item_id, $last_date = '', $skip = '', $top = '' ) {
1580
1581 $options = get_option( $this->plugin_name . '_admin_options' );
1582 if ( isset( $options['mlsimport_mls_name'] ) ) {
1583 $mls_id = intval( $options['mlsimport_mls_name'] );
1584 } else {
1585 return '';
1586 }
1587
1588 if ( isset( $options['mlsimport_theme_used'] ) ) {
1589 $theme_id = intval( $options['mlsimport_theme_used'] );
1590 } else {
1591 return '';
1592 }
1593
1594 $values = array();
1595 $values['mls_id'] = $mls_id;
1596 $values['theme_id'] = $theme_id;
1597
1598 if ( '' !== $top ) {
1599 $values['top'] = $top;
1600 $values['skip'] = intval( $skip );
1601 }
1602
1603 // // add price
1604 $mlsimport_item_min_price = get_post_meta( $item_id, 'mlsimport_item_min_price', true );
1605 $mlsimport_item_max_price = get_post_meta( $item_id, 'mlsimport_item_max_price', true );
1606 if ( '' !== $mlsimport_item_min_price && '' !== $mlsimport_item_max_price ) {
1607 $values['list_price_min'] = floatval( $mlsimport_item_min_price );
1608 $values['list_price_max'] = floatval( $mlsimport_item_max_price );
1609 }
1610
1611 // add city
1612 $values = $this->mls_import_return_multiple_param_value( 'city', $item_id, 'city', $values );
1613
1614 // add county
1615 $values = $this->mls_import_return_multiple_param_value( 'countyorparish', $item_id, 'county_or_parish', $values );
1616
1617 // add postal code
1618 $values = $this->mls_import_saas_add_to_parms_input( 'PostalCode', $item_id, 'postal_code', $values );
1619
1620 // add status
1621
1622 if ( 111 !== $mls_id ) { // edmonton check
1623 $values = $this->mls_import_return_multiple_param_value( 'StandardStatus', $item_id, 'status', $values );
1624 }
1625
1626 // add property_subtype
1627 $values = $this->mls_import_return_multiple_param_value( 'PropertySubType', $item_id, 'property_subtype', $values );
1628
1629 // add property_type
1630 $values = $this->mls_import_return_multiple_param_value( 'PropertyType', $item_id, 'property_type', $values );
1631
1632 // rapattoni exception
1633 if ( $mls_id > 5000 ) {
1634 $values = $this->mls_import_saas_add_to_parms_input( 'PropertyType', $item_id, 'property_type', $values );
1635 $temp = $values['property_type'];
1636 $temp = str_replace( ' ', '', $temp );
1637 $values['property_type'] = array();
1638 $values['property_type'][] = $temp;
1639 }
1640
1641 // add internet_entirelisting_displayyn
1642 $values = $this->mls_import_saas_add_to_parms_input( 'InternetEntireListingDisplayYN', $item_id, 'internet_entirelisting_displayyn', $values );
1643
1644 // add internet_address_displayyn
1645 $values = $this->mls_import_saas_add_to_parms_input( 'InternetAddressDisplayYN', $item_id, 'internet_address_displayyn', $values );
1646
1647 // add ListAgentKey
1648 $values = $this->mls_import_saas_add_to_parms_input( 'ListAgentKey', $item_id, 'list_agentkey', $values );
1649 // add ListAgentKey
1650 $values = $this->mls_import_saas_add_to_parms_input( 'ListAgentMlsId', $item_id, 'list_agentmlsid', $values );
1651 // add ListOfficeKey
1652 $values = $this->mls_import_saas_add_to_parms_input( 'ListOfficeKey', $item_id, 'list_officekey', $values );
1653 // add ListOfficeMlsId
1654 $values = $this->mls_import_saas_add_to_parms_input( 'ListOfficeMlsId', $item_id, 'list_officemlsid', $values );
1655
1656 // add ListingId
1657 $values = $this->mls_import_saas_add_to_parms_input( 'ListingId', $item_id, 'listingid', $values );
1658
1659 //add Exclude_ListOfficeKey
1660 $values = $this->mls_import_saas_add_to_parms_input( 'Exclude_ListOfficeKey', $item_id, 'exclude_list_officekey', $values );
1661 // add Exclude_ListOfficeMlsId
1662 $values = $this->mls_import_saas_add_to_parms_input( 'Exclude_ListOfficeMlsId', $item_id, 'exclude_list_officemlsid', $values );
1663
1664
1665
1666 //add Exclude_ListAgentKey
1667 $values = $this->mls_import_saas_add_to_parms_input( 'Exclude_ListAgentKey', $item_id, 'exclude_list_agentkey', $values );
1668 // add Exclude_ListAgentMlsId
1669 $values = $this->mls_import_saas_add_to_parms_input( 'Exclude_ListAgentMlsId', $item_id, 'exclude_list_agentmlsid', $values );
1670
1671
1672
1673 if ( '' !== $last_date ) {
1674 $values['modification_time'] = $last_date;
1675 }
1676
1677 return( $values );
1678 }
1679
1680
1681
1682 /*
1683 *
1684 * add input items to parameters array
1685 *
1686 */
1687
1688 public function mls_import_saas_add_to_parms_input( $key, $post_id, $new_name, $all_values ) {
1689 $name = strtolower( 'mlsimport_item_' . $key );
1690 $value = get_post_meta( $post_id, $name, true );
1691 if ( '' !== $value ) {
1692 $all_values[ $new_name ] = $value;
1693 }
1694
1695 return $all_values;
1696 }
1697
1698
1699 /*
1700 *
1701 * add list items to parameters array
1702 *
1703 */
1704
1705 public function mls_import_return_multiple_param_value( $key, $post_id, $new_name, $all_values ) {
1706 $name_check = strtolower( 'mlsimport_item_' . $key . '_check' );
1707 $name = strtolower( 'mlsimport_item_' . $key );
1708
1709 $value = get_post_meta( $post_id, $name, true );
1710
1711 // add extra county - should be moved into function if pass tests
1712 if ( 'countyorparish' === $key ) {
1713 $extracounty_values = get_post_meta( $post_id, 'mlsimport_item_extracounty', true );
1714
1715 if ( '' !== $extracounty_values ) {
1716 $extracounty_array = explode( ',', $extracounty_values );
1717
1718 if ( ! is_array( $value ) ) {
1719 if ( '' === $value ) {
1720 $value = array();
1721 } else {
1722 $value = array( $value );
1723 }
1724 }
1725
1726 foreach ( $extracounty_array as $extra ) {
1727 $value[] = $extra;
1728 }
1729 }
1730 }
1731
1732 // add extra city - should be moved into function if pass tests
1733 if ( 'city' === $key ) {
1734 $extracity_values = get_post_meta( $post_id, 'mlsimport_item_extracity', true );
1735 if ( '' !== $extracity_values ) {
1736 $extracity_array = explode( ',', $extracity_values );
1737
1738 if ( ! is_array( $value ) ) {
1739 if ('' === $value ) {
1740 $value = array();
1741 } else {
1742 $value = array( $value );
1743 }
1744 }
1745
1746 foreach ( $extracity_array as $extra ) {
1747 $value[] = $extra;
1748 }
1749 }
1750 }
1751
1752 $value_check = get_post_meta( $post_id, $name_check, true );
1753
1754 if ( 0 === intval($value_check) && '' !== $value ) {
1755 $all_values[ $new_name ] = $value;
1756 }
1757
1758 // status exception
1759 if ( 'status' === $new_name ) {
1760 $all_values[ $new_name ] = $value;
1761 }
1762
1763 return $all_values;
1764 }
1765
1766
1767
1768 /*
1769 *
1770 * All Enums fiels to be used on MLS import Item
1771 *
1772 *
1773 *
1774 *
1775 *
1776 *
1777 * */
1778
1779 public function mlsimport_saas_return_mls_fields() {
1780
1781 $mlsimport_mls_metadata_mls_enums = get_option( 'mlsimport_mls_metadata_mls_enums', '' );
1782
1783 if ( '' === $mlsimport_mls_metadata_mls_enums ) {
1784 ?>
1785 <div class="mlsimport_warning long_warning">Please select the import fields(from MLS Import Settings) before starting a MLS import process.</div>
1786 <?php
1787 }
1788
1789 $metadata_api_call_full = json_decode( $mlsimport_mls_metadata_mls_enums, true );
1790
1791 if ( isset( $metadata_api_call_full['global_array'] ) ) {
1792 $metadata_api_call = $metadata_api_call_full['global_array'];
1793 }
1794
1795 $city_array = array();
1796 if ( isset( $metadata_api_call['PropertyEnums']['City'] ) && is_array( $metadata_api_call['PropertyEnums']['City'] ) ) {
1797 $city_array = array_keys( $metadata_api_call['PropertyEnums']['City'] );
1798 }
1799
1800 $county_array = array();
1801 if ( isset( $metadata_api_call['PropertyEnums']['CountyOrParish'] ) && is_array( $metadata_api_call['PropertyEnums']['CountyOrParish'] ) ) {
1802 $county_array = array_keys( $metadata_api_call['PropertyEnums']['CountyOrParish'] );
1803 }
1804
1805 $mlsstatus_array = array();
1806 if ( isset( $metadata_api_call['PropertyEnums']['MlsStatus'] ) && is_array( $metadata_api_call['PropertyEnums']['MlsStatus'] ) ) {
1807 $mlsstatus_array = array_keys( $metadata_api_call['PropertyEnums']['MlsStatus'] );
1808 }
1809
1810 $propertysubtype_array = array();
1811 if ( isset( $metadata_api_call['PropertyEnums']['PropertySubType'] ) && is_array( $metadata_api_call['PropertyEnums']['PropertySubType'] ) ) {
1812 $propertysubtype_array = array_keys( $metadata_api_call['PropertyEnums']['PropertySubType'] );
1813 }
1814
1815 $propertytype_array = array();
1816 if ( isset( $metadata_api_call['PropertyEnums']['PropertyType'] ) && is_array( $metadata_api_call['PropertyEnums']['PropertyType'] ) ) {
1817 $propertytype_array = array_keys( $metadata_api_call['PropertyEnums']['PropertyType'] );
1818 }
1819
1820 $standardstatus_array = array();
1821 $standardstatus_delete_array=array();
1822 if ( isset( $metadata_api_call['PropertyEnums']['StandardStatus'] ) && is_array( $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'] );
1825 }
1826
1827 // if we do not have standart status
1828 if ( empty( $standardstatus_array ) ) {
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
1844
1845 $extracounty_values = '';
1846 $extracity_values = '';
1847
1848 $field_import = array(
1849 'City' => array(
1850 'label' => esc_html__( 'Select cities', 'mlsimport' ),
1851 'description' => esc_html__( 'Select the cities from where we will import data.', 'mlsimport' ),
1852 'type' => 'select',
1853 'multiple' => 'yes',
1854 'values' => $city_array,
1855 ),
1856
1857 'extraCity' => array(
1858 'label' => esc_html__( 'Add extra Cities', 'mlsimport' ),
1859 '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' ),
1860 'type' => 'input',
1861 'multiple' => 'no',
1862 'values' => $extracity_values,
1863 ),
1864
1865 'CountyOrParish' => array(
1866 'label' => esc_html__( 'Select Counties', 'mlsimport' ),
1867 'description' => esc_html__( 'Select the counties from where we will import data.', 'mlsimport' ),
1868 'type' => 'select',
1869 'multiple' => 'yes',
1870 'values' => $county_array,
1871 'show_extra_field' => true,
1872 ),
1873
1874 'extraCounty' => array(
1875 'label' => esc_html__( 'Add extra Counties', 'mlsimport' ),
1876 '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' ),
1877 'type' => 'input',
1878 'multiple' => 'no',
1879 'values' => $extracounty_values,
1880 ),
1881
1882 'PostalCode' => array(
1883 'label' => esc_html__( 'Select Postal Code', 'mlsimport' ),
1884 'description' => esc_html__( 'Select the PostalCode from where to import listings. Works with only one PostalCode.', 'mlsimport' ),
1885 'type' => 'input',
1886 'multiple' => 'no',
1887 ),
1888
1889 'PropertySubType' => array(
1890 'label' => esc_html__( 'Select Property Category', 'mlsimport' ),
1891 'description' => esc_html__( 'Property Category', 'mlsimport' ),
1892 'type' => 'select',
1893 'multiple' => 'yes',
1894 'values' => $propertysubtype_array,
1895 ),
1896 'PropertyType' => array(
1897 'label' => esc_html__( 'Select Property Action Category', 'mlsimport' ),
1898 'description' => esc_html__( 'Property Action Category', 'mlsimport' ),
1899 'type' => 'select',
1900 'multiple' => 'yes',
1901 'values' => $propertytype_array,
1902 ),
1903 'StandardStatus' => array(
1904 'label' => esc_html__( 'Select Status', 'mlsimport' ),
1905 'description' => __( 'The list is auto-populated with MLS available statuses. To select multiple statuses, use Ctrl (Windows) or Command (Mac).', 'mlsimport' ),
1906 'type' => 'select',
1907 'multiple' => 'yes',
1908 'values' => $standardstatus_array,
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
1918 'InternetEntireListingDisplayYN' => array(
1919 'label' => esc_html__( 'Internet Entire Listing Display ', 'mlsimport'),
1920 'description' => esc_html__( 'A yes/no field that states the seller has allowed the listing to be displayed on Internet sites.', 'mlsimport' ),
1921 'type' => 'select',
1922 'multiple' => 'no',
1923 'values' => array(
1924 'yes',
1925 'no',
1926 ),
1927 ),
1928 'InternetAddressDisplayYN' => array(
1929 'label' => esc_html__( 'Internet Address display', 'mlsimport' ),
1930 'description' => esc_html__( 'A yes/no field that states the seller has allowed the listing address to be displayed on Internet sites.', 'mlsimport' ),
1931 'type' => 'select',
1932 'multiple' => 'no',
1933 'values' => array(
1934 'yes',
1935 'no',
1936 ),
1937 ),
1938 'ListAgentKey' => array(
1939 'label' => esc_html__( 'ListAgentKey', 'mlsimport' ),
1940 'description' => esc_html__( 'Import listings from a specific Agent (contact your MLS for this information)', 'mlsimport' ),
1941 'type' => 'input',
1942 'multiple' => 'no',
1943 ),
1944 'ListAgentMlsId' => array(
1945 'label' => esc_html__( 'ListAgentMlsId', 'mlsimport' ),
1946 'description' => esc_html__( 'Import listings from a specific Agent (contact your MLS for this information)', 'mlsimport' ),
1947 'type' => 'input',
1948 'multiple' => 'no',
1949 ),
1950 'ListOfficeKey' => array(
1951 'label' => esc_html__( 'ListOfficeKey', 'mlsimport' ),
1952 'description' => esc_html__( 'Import listings from a specific Office (contact your MLS for this information)', 'mlsimport'),
1953 'type' => 'input',
1954 'multiple' => 'no',
1955 ),
1956 'ListOfficeMlsId' => array(
1957 'label' => esc_html__( 'ListOfficeMlsId', 'mlsimport' ),
1958 'description' => esc_html__( 'Import listings from a specific Office (contact your MLS for this information)', 'mlsimport' ),
1959 'type' => 'input',
1960 'multiple' => 'no',
1961 ),
1962 'ListingId' => array(
1963 'label' => esc_html__( 'ListingId', 'mlsimport' ),
1964 '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'),
1965 'type' => 'input',
1966 'multiple' => 'no',
1967 ),
1968 'Exclude_ListOfficeMlsId' => array(
1969 'label' => esc_html__( 'Exclude listings with ListOfficeMlsId', 'mlsimport' ),
1970 'description' => esc_html__( 'Exclude listings that belong to one or more ListOfficeMlsId.', 'mlsimport' ),
1971 'type' => 'input',
1972 'multiple' => 'no',
1973 ),
1974 'Exclude_ListOfficeKey' => array(
1975 'label' => esc_html__( 'Exclude listings with ListOfficeKey', 'mlsimport' ),
1976 'description' => esc_html__( 'Exclude listings that belong to one or more ListOfficeKey', 'mlsimport'),
1977 'type' => 'input',
1978 'multiple' => 'no',
1979 ),
1980
1981
1982 'Exclude_ListAgentMlsId' => array(
1983 'label' => esc_html__( 'Exclude listings with ListAgentMlsId', 'mlsimport' ),
1984 'description' => esc_html__( 'Exclude listings that belong to one or more ListAgentMlsId.', 'mlsimport' ),
1985 'type' => 'input',
1986 'multiple' => 'no',
1987 ),
1988 'Exclude_ListAgentKey' => array(
1989 'label' => esc_html__( 'Exclude listings with ListAgentKey ', 'mlsimport' ),
1990 'description' => esc_html__( 'Exclude listings that belong to one or more ListAgentKey ', 'mlsimport'),
1991 'type' => 'input',
1992 'multiple' => 'no',
1993 ),
1994
1995
1996 );
1997 return $field_import;
1998 }
1999
2000
2001
2002
2003
2004
2005
2006 /**
2007 *
2008 *
2009 * AYsnc Test
2010 */
2011 public function mlsimport_move_files_per_item() {
2012 check_ajax_referer( 'mlsimport_item_actions', 'security' );
2013 $post_id = 0;
2014 $how_many = 0;
2015 $max_number = 0;
2016 if(isset( $_POST['post_id'] )){
2017 $post_id = intval( $_POST['post_id'] );
2018 }
2019 if(isset( $_POST['how_many'] )){
2020 $how_many = intval( $_POST['how_many'] );
2021 }
2022 if(isset( $_POST['post_number'] )){
2023 $max_number = intval( $_POST['post_number'] );
2024 }
2025
2026 update_option( 'mlsimport_force_stop_' . $post_id, 'no', false );
2027
2028 $item_id_array = array(
2029 'item_id' => $post_id,
2030 'how_many' => $how_many,
2031 'max_number' => $max_number,
2032 'batch_counter' => 1,
2033 );
2034
2035 update_post_meta( $post_id, 'mlsimport_attach_to_move_' . $post_id, '' );
2036 $attachments_to_move = (array) $this->mlsimport_saas_generate_import_requests_per_item( $item_id_array );
2037
2038 update_post_meta( $post_id, 'mlsimport_attach_to_move_' . $post_id, $attachments_to_move );
2039
2040 $attachments_to_send = array(
2041 'args' => array(
2042 'attachments_to_move' => $post_id,
2043 'item_id_array' => $item_id_array,
2044 ),
2045 );
2046
2047 mlsimport_saas_single_write_import_custom_logs( 'Preparing the import. Please hold on.' . PHP_EOL );
2048 mlsimport_debuglogs_per_plugin( 'Preparing the import. Please hold on.' . PHP_EOL );
2049
2050 update_post_meta( $post_id, 'mlsimport_spawn_status', 'started' );
2051 as_enqueue_async_action( 'mlsimport_background_process_per_item', $attachments_to_send );
2052 spawn_cron();
2053
2054 unset( $attachments_to_send );
2055 die();
2056 }
2057
2058 /**
2059 *
2060 *
2061 * Processing Cron
2062 */
2063 public function mlsimport_background_process_per_item_cron_function( $input_arg ) {
2064 $log = 'In cron processing function ->' . wp_json_encode( $input_arg['item_id_array'] ) . PHP_EOL;
2065 mlsimport_saas_single_write_import_custom_logs( $log, 'cron' );
2066 global $mlsimport;
2067
2068 // Get from MLS Import it the big argument arrat
2069 $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 );
2070
2071 $log = 'In processing function mlsimport_cron_attach_to_move_ ->' . wp_json_encode( $attachments_to_move ) . PHP_EOL;
2072 mlsimport_saas_single_write_import_custom_logs( $log );
2073
2074
2075 // foreach($input_arg['attachments_to_move'] as $key=>$import_link){
2076 foreach ( $attachments_to_move as $key => $import_arguments ) {
2077 $GLOBALS['wp_object_cache']->delete( 'mlsimport_force_stop_' . $input_arg['item_id_array']['item_id'], 'options' );
2078
2079 $log = PHP_EOL . ' Cron Parsing importing batch : ' . $key . ' = ' . wp_json_encode( $import_arguments ) . PHP_EOL;
2080 mlsimport_saas_single_write_import_custom_logs( $log, 'cron' );
2081
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 );
2085 }
2086
2087 mlsimport_saas_single_write_import_custom_logs( 'CRON JOB Import Completed ' . PHP_EOL );
2088 mlsimport_debuglogs_per_plugin( 'CRON JOB Import Completed ' . PHP_EOL );
2089 update_post_meta( $input_arg['item_id_array']['item_id'], 'mlsimport_spawn_status', 'completed' );
2090 }
2091
2092 /**
2093 *
2094 *
2095 * Generate import Requests per item
2096 */
2097 public function mlsimport_saas_generate_import_requests_per_item( $item_id_array, $last_date = '' ) {
2098 $import_step = 25;
2099
2100 $prop_id = $item_id_array['item_id'];
2101 $max_found = $item_id_array['max_number'];
2102 $how_many = $item_id_array['how_many'];
2103 if ( 0=== intval($how_many) ) {
2104 $how_many = $max_found;
2105 }
2106 if ( $how_many > $max_found ) {
2107 $how_many = $max_found;
2108 }
2109
2110 $search_url_step = '';
2111 $urls_array = array();
2112
2113 $skip = 0;
2114 if ( $how_many > 10000 ) {
2115 $how_many = 10000;
2116 }
2117
2118 if ( $how_many < $import_step ) {
2119 $import_step = $how_many;
2120 }
2121
2122 while ( $skip < $how_many ) {
2123 $search_url_step = $this->mlsimport_saas_make_listing_requests_arguments( $prop_id, $last_date, $skip, $import_step );
2124 $skip = $skip + $import_step;
2125 $urls_array[] = $search_url_step;
2126 }
2127 return $urls_array;
2128 }
2129
2130
2131
2132
2133
2134 /**
2135 * Process Async function
2136 */
2137 public function mlsimport_background_process_per_item_function( $input_arg ) {
2138
2139 $mlsimportItemId = $input_arg['item_id_array']['item_id'];
2140 $log_prefix = 'In processing function - Item ID: ' . $mlsimportItemId . ' -> ';
2141 mlsimport_saas_single_write_import_custom_logs( $log_prefix . wp_json_encode( $input_arg['item_id_array'] ) . PHP_EOL );
2142
2143 //ini_set('memory_limit', '256M');
2144 //ini_set('max_execution_time', 0); // Unlimited execution time
2145
2146 // Get from MLS Import the big argument array only once
2147 $attachments_to_move = get_post_meta( $mlsimportItemId, 'mlsimport_attach_to_move_' . $mlsimportItemId, true );
2148
2149 // Retrieve all meta data in one go to reduce database queries
2150 $mlsimport_item_option_data = array(
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 ),
2156 );
2157
2158 $total_batches = count( $attachments_to_move );
2159
2160 // removed because $this
2161 global $mlsimport;
2162
2163 $log = 'In processing function $attachments_to_move ->' . wp_json_encode( $attachments_to_move ) . PHP_EOL;
2164 mlsimport_saas_single_write_import_custom_logs( $log );
2165 // mlsimport_debuglogs_per_plugin($log);
2166 print esc_html($log);
2167
2168 foreach ( $attachments_to_move as $key => $import_arguments ) {
2169 // reconsider use
2170 // $GLOBALS['wp_object_cache']->delete('mlsimport_force_stop_' . $input_arg['item_id_array']['item_id'], 'options');
2171 $status = get_option( 'mlsimport_force_stop_' . $mlsimportItemId );
2172 if ( 'no' === $status ) {
2173 // reconsider use
2174 // wp_cache_flush();
2175 $mem_usage = memory_get_usage( true );
2176 $mem_usage_show = round( $mem_usage / 1048576, 2 );
2177
2178
2179
2180 mlsimport_saas_single_write_import_custom_logs( $log );
2181 $log = 'Parsing import batch: ' . ( $key + 1 ) . ' of ' . $total_batches . '. Memory used: ' . $mem_usage_show . ' MB.' . PHP_EOL;
2182
2183 // Combine logs and reduce function calls
2184 mlsimport_saas_single_write_import_custom_logs( $log );
2185 mlsimport_debuglogs_per_plugin( $log );
2186 print esc_html($log);
2187
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 );
2191 } else {
2192 update_post_meta( $mlsimportItemId, 'mlsimport_spawn_status', 'completed' );
2193 delete_post_meta( $mlsimportItemId, 'mlsimport_attach_to_move_' . $mlsimportItemId );
2194 mlsimport_saas_single_write_import_custom_logs( PHP_EOL . 'Parsing importing link FORCE STOP : ' );
2195 mlsimport_debuglogs_per_plugin( 'Parsing importing link FORCE STOP : ' );
2196 break; // Exit the loop if forced to stop
2197 }
2198 }
2199
2200 mlsimport_saas_single_write_import_custom_logs( 'Import Completed ' . PHP_EOL );
2201 mlsimport_debuglogs_per_plugin( 'Import Completed ' . PHP_EOL );
2202
2203 update_post_meta( $mlsimportItemId, 'mlsimport_spawn_status', 'completed' );
2204 delete_post_meta( $mlsimportItemId, 'mlsimport_attach_to_move_' . $mlsimportItemId );
2205
2206 unset( $attachments_to_move );
2207 unset( $api_call_array );
2208 unset( $input_arg );
2209 unset( $log );
2210 unset( $log2 );
2211 }
2212
2213
2214
2215
2216
2217 /**
2218 *
2219 *
2220 *
2221 * update log function
2222 */
2223 public function mlsimport_logger_per_item() {
2224 check_ajax_referer( 'mlsimport_item_actions', 'security' );
2225 $post_id=0;
2226 if(isset($_POST['post_id'] )){
2227 $post_id = intval( $_POST['post_id'] );
2228 }
2229
2230 $status = get_post_meta( $post_id, 'mlsimport_spawn_status', true );
2231 $path = WP_PLUGIN_DIR . '/mlsimport/logs/status_logs.log';
2232 $logs = file_get_contents( $path );
2233
2234 $force_status = intval( get_post_meta( $post_id, 'mlsimport_force_stop', true ) );
2235 $force_status = get_option( 'mlsimport_force_stop_' . $post_id );
2236
2237 if ( 'no' !== $force_status ) {
2238 echo wp_json_encode(
2239 array(
2240 'is_done' => 'done',
2241 'status' => $status,
2242 'logs' => $logs,
2243 )
2244 );
2245 die();
2246 }
2247
2248 if ( '' === $status || 'completed' === $status ) {
2249 echo wp_json_encode(
2250 array(
2251 'is_done' => 'done',
2252 'status' => $status,
2253 'logs' => $logs,
2254 )
2255 );
2256 } else {
2257 // return from log
2258 echo wp_json_encode(
2259 array(
2260 'is_done' => 'wip',
2261 'status' => $status,
2262 'logs' => $logs,
2263 )
2264 );
2265 }
2266 die();
2267 }
2268
2269
2270
2271
2272
2273
2274 /**
2275 *
2276 *
2277 *
2278 *
2279 * Force Stop Import
2280 */
2281 public function mlsimport_stop_import_per_item() {
2282 check_ajax_referer( 'mlsimport_item_actions', 'security' );
2283 $post_id=0;
2284 if(isset($_POST['post_id'] )){
2285 $post_id = intval( $_POST['post_id'] );
2286 }
2287 update_option( 'mlsimport_force_stop_' . $post_id, 'yes', false );
2288 mlsimport_saas_single_write_import_custom_logs( 'Stopeed for ' . $post_id . PHP_EOL );
2289 mlsimport_debuglogs_per_plugin( 'Stopeed for ' . $post_id . PHP_EOL );
2290
2291 die();
2292 }
2293
2294
2295
2296 /*
2297 *
2298 * Get MLS Metadata
2299 *
2300 *
2301 *
2302 *
2303 **/
2304
2305 public function mlsimport_saas_get_metadata_function() {
2306 check_ajax_referer( 'mlsimport_saas_get_metadata', 'security' );
2307 $theme_Start = new ThemeImport();
2308
2309 $values = array();
2310 $options = get_option( $this->plugin_name . '_admin_options' );
2311 $url = 'clients?theme_id=' . intval( $options['mlsimport_theme_used'] );
2312
2313 $answer = $theme_Start::globalApiRequestSaas( $url, $values, 'GET' );
2314
2315 update_option( 'mlsimport_mls_metadata_populated', 'yes' );
2316
2317 update_option( 'mlsimport_mls_metadata_theme_schema', $answer['theme_schema'] );
2318 update_option( 'mlsimport_mls_metadata_mls_data', $answer['mls_data']['mls_meta_data'] );
2319 update_option( 'mlsimport_mls_metadata_mls_enums', $answer['mls_data']['mls_meta_enums'] );
2320 }
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333 /**
2334 *
2335 *
2336 * write debug logs
2337 */
2338 public function mlsimport_debuglog_cron( $message ) {
2339 if ( is_array( $message ) ) {
2340 $message = wp_json_encode( $message );
2341 }
2342 $message = date( 'F j, Y, g:i a' ) . ' -> ' . $message;
2343 global $wp_filesystem;
2344 if ( empty( $wp_filesystem ) ) {
2345 require_once ABSPATH . '/wp-admin/includes/file.php';
2346 WP_Filesystem();
2347 }
2348
2349 $path = WP_PLUGIN_DIR . '/mlsimport/logs/cron_logs.log';
2350
2351 file_put_contents( $path, $message, FILE_APPEND | LOCK_EX );
2352 }
2353
2354 }
2355