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

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

2,407 lines 75.6 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 //print_r($mlsRequest );
1074
1075 if (isset($mlsRequest['success']) && !$mlsRequest['success']) {
1076 echo '<div class="mlsimport_warning">' . esc_html($mlsRequest['message']) . '</div>';
1077 }
1078
1079 $foundItems = isset($mlsRequest['results']) ? intval($mlsRequest['results']) : 'none';
1080 if ($foundItems === 'none') {
1081 $mlsimport->admin->mlsimport_saas_check_mls_connection();
1082 esc_html_e('Your Token was expired. Please refresh the page to renew it wait while we renew it.', 'mlsimport');
1083 }
1084
1085 echo $this->generateMetaOptionsHtml($postId, $foundItems, $lastDate, $mlsimportItemHowMany, $mlsimportItemStatCron, $mlsimportMlsId, $fieldImport);
1086 }
1087
1088
1089
1090
1091 /**
1092 * Generate Meta Options HTML
1093 *
1094 * @param int $postId The post ID.
1095 * @param int $foundItems The number of found items.
1096 * @param string $lastDate The last date checked.
1097 * @param string $mlsimportItemHowMany How many items to import.
1098 * @param string $mlsimportItemStatCron The status of the cron job.
1099 * @param int $mlsimportMlsId The MLS import ID.
1100 * @param array $fieldImport The fields to import.
1101 * @return string The generated HTML.
1102 */
1103 private function generateMetaOptionsHtml($postId, $foundItems, $lastDate, $mlsimportItemHowMany, $mlsimportItemStatCron, $mlsimportMlsId, $fieldImport) {
1104
1105 ob_start();
1106
1107 ?>
1108 <div class="mlsimport_item_search_url" style="display:none;"><?php echo esc_html__('Last date/time we check :', 'mlsimport') . ' ' . esc_html($lastDate); ?></div>
1109 <ul>
1110 <li>1. Set the import parameters.</li>
1111 <li>2. Hit Publish or Update, otherwise import will not work correctly.</li>
1112 <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>
1113 <li>4. Press the Update button after you make any change in the import settings.</li>
1114 </ul>
1115
1116 <?php if (is_numeric($foundItems) && $foundItems >= 500): ?>
1117 <div class="mlsimport_notification">
1118 <?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'); ?>
1119 </div>
1120 <?php endif; ?>
1121
1122 <div class="mlsimport_import_no">
1123 <?php esc_html_e('We found', 'mlsimport'); ?>
1124 <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.
1125 </div>
1126
1127 <fieldset class="mlsimport-fieldset">
1128 <label class="mlsimport-label" for="mlsimport_item_how_many">
1129 <?php esc_html_e('How Many to import. Use 0 if you want to import all listings found.', 'mlsimport'); ?>
1130 </label>
1131 <input type="text" id="mlsimport_item_how_many" name="mlsimport_item_how_many" value="<?php echo esc_attr($mlsimportItemHowMany); ?>"/>
1132 </fieldset>
1133
1134 <fieldset class="mlsimport-fieldset mlsimport_auto_switch">
1135 <?php esc_html_e('Enable Auto Update every hour?', 'mlsimport'); ?>
1136 <label class="mlsimport_switch">
1137 <input type="hidden" value="0" name="mlsimport_item_stat_cron">
1138 <input type="checkbox" value="1" name="mlsimport_item_stat_cron"<?php if (intval($mlsimportItemStatCron) !== 0) echo esc_html(' checked'); ?>>
1139 <span class="slider round"></span>
1140 </label>
1141 </fieldset>
1142
1143 <?php if ($mlsimportItemStatCron !== ''): ?>
1144 <div id="mlsimport_item_status"></div>
1145 <input class="button mlsimport_button" type="button" id="mlsimport-start_item"
1146 data-post-number="<?php echo intval($foundItems); ?>"
1147 data-post_id="<?php echo intval($postId); ?>" value="Start Import">
1148 <input class="button mlsimport_button" type="button" id="mlsimport_stop_item"
1149 data-post-number="<?php echo intval($foundItems); ?>"
1150 data-post_id="<?php echo intval($postId); ?>" value="Stop Import">
1151 <?php endif; ?>
1152
1153 <input type="hidden" id="mlsimport_item_actions" value="<?php echo esc_attr(wp_create_nonce("mlsimport_item_actions")); ?>"/>
1154 <div class="mlsimport_param_wrapper"><h2><?php esc_html_e('Import Parameters', 'mlsimport'); ?></h2>
1155
1156 <?php
1157 $mlsimportItemTitleFormat = esc_html(get_post_meta($postId, 'mlsimport_item_title_format', true));
1158 ?>
1159
1160 <fieldset class="mlsimport-fieldset">
1161 <label class="mlsimport-label" for="mlsimport_item_title_format">
1162 <?php esc_html_e('Title Format', 'mlsimport'); ?>
1163 </label>
1164
1165 <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>
1166 <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}'); ?>"/>
1167 </fieldset>
1168
1169 <?php
1170 $mlsimportItemAgent = esc_html(get_post_meta($postId, 'mlsimport_item_agent', true));
1171 ?>
1172
1173 <fieldset class="mlsimport-fieldset">
1174 <label class="mlsimport-label" for="mlsimport_item_agent">
1175 <?php esc_html_e('Select Agent', 'mlsimport'); ?>
1176 </label>
1177 <select class="mlsimport-select" name="mlsimport_item_agent" id="mlsimport_item_agent">
1178 <?php
1179 $permitedTags = mlsimport_allowed_html_tags_content();
1180 $selectAgent =$this->theme_importer->mlsimportSaasThemeImportSelectAgent($mlsimportItemAgent);
1181 print wp_kses($selectAgent, $permitedTags);
1182 ?>
1183 </select>
1184 </fieldset>
1185
1186 <?php
1187 $mlsimportItemPropertyStatus = esc_html(get_post_meta($postId, 'mlsimport_item_property_status', true));
1188 if ('' === $mlsimportItemPropertyStatus) {
1189 $mlsimportItemPropertyStatus = 'publish';
1190 }
1191 $statusArray = array('publish', 'draft');
1192 ?>
1193 <fieldset class="mlsimport-fieldset">
1194 <label class="mlsimport-label" for="mlsimport_item_property_status">
1195 <?php esc_html_e('Select Property Status on import', 'mlsimport'); ?>
1196 </label>
1197 <select class="mlsimport-select" name="mlsimport_item_property_status" id="mlsimport_item_property_status">
1198 <?php foreach ($statusArray as $value): ?>
1199 <option value="<?php echo esc_attr($value); ?>" <?php if ($value === $mlsimportItemPropertyStatus) echo esc_html('selected'); ?>>
1200 <?php echo esc_html($value); ?>
1201 </option>
1202 <?php endforeach; ?>
1203 </select>
1204 </fieldset>
1205
1206 <?php
1207 $mlsimportItemPropertyUser = esc_html(get_post_meta($postId, 'mlsimport_item_property_user', true));
1208 ?>
1209 <fieldset class="mlsimport-fieldset">
1210 <label class="mlsimport-label" for="mlsimport_item_property_user">
1211 <?php esc_html_e('User', 'mlsimport'); ?>
1212 </label>
1213 <select class="mlsimport-select" id="mlsimport_item_property_user" name="mlsimport_item_property_user">
1214 <?php
1215 $selectUser = $this->theme_importer->mlsimportSaasThemeImportSelectUser($mlsimportItemPropertyUser);
1216 print wp_kses($selectUser, $permitedTags);
1217 ?>
1218 </select>
1219 </fieldset>
1220
1221 <?php
1222 $mlsimportItemMinPrice = floatval(get_post_meta($postId, 'mlsimport_item_min_price', true));
1223 $mlsimportItemMaxPrice = floatval(get_post_meta($postId, 'mlsimport_item_max_price', true));
1224 if (0 === intval($mlsimportItemMaxPrice)) {
1225 $mlsimportItemMaxPrice = 10000000;
1226 }
1227 ?>
1228 <fieldset class="mlsimport-fieldset">
1229 <label class="mlsimport-label">
1230 <?php esc_html_e('Price Between', 'mlsimport'); ?>
1231 </label>
1232 <input type="text" class="mlsimport-select" id="mlsimport_item_min_price" name="mlsimport_item_min_price" value="<?php echo esc_attr($mlsimportItemMinPrice); ?>"> and
1233 <input type="text" class="mlsimport-select" id="mlsimport_item_max_price" name="mlsimport_item_max_price" value="<?php echo esc_attr($mlsimportItemMaxPrice); ?>">
1234 </fieldset>
1235
1236 <?php
1237 $options = get_option($this->plugin_name . '_admin_options');
1238
1239 $mlsId = '';
1240 if (isset($options['mlsimport_mls_name'])) {
1241 $mlsId = sanitize_text_field(trim($options['mlsimport_mls_name']));
1242 }
1243
1244 if ($mlsId > 5000) {
1245 $fieldImport['PropertyType']['multiple'] = 'no';
1246 }
1247
1248 foreach ($fieldImport as $key => $field):
1249 $nameCheck = strtolower('mlsimport_item_' . $key . '_check');
1250 $name = strtolower('mlsimport_item_' . $key);
1251
1252 $value = get_post_meta($postId, $name, true);
1253 $valueCheck = get_post_meta($postId, $nameCheck, true);
1254 $extraClass = '';
1255 if ('extraCity' === $key || 'extraCounty' === $key) {
1256 $extraClass = ' mlsimport_hidden_field_button';
1257 }
1258 ?>
1259 <fieldset class="mlsimport-fieldset">
1260 <label class="mlsimport-label <?php echo esc_attr($extraClass); ?>" for="<?php echo esc_attr($name); ?>">
1261 <?php echo esc_html($field['label']); ?>
1262 </label>
1263 <?php if ('extraCity' === $key || 'extraCounty' === $key): ?>
1264 <div class="mlsimport-input-wrapper" style="display:none">
1265 <?php endif; ?>
1266 <p class="mlsimport-exp"><?php echo wp_kses_post($this->mlsimport_notes_for_mls($mlsimportMlsId, $name, $field['description'])); ?>
1267 <?php
1268 $isCheckboxAdmin = 0;
1269 if (1 === intval($valueCheck)) {
1270 $isCheckboxAdmin = 1;
1271 }
1272
1273 $selectAllNone = [
1274 'InternetAddressDisplayYN',
1275 'InternetEntireListingDisplayYN',
1276 'PostalCode',
1277 'ListAgentKey',
1278 'ListAgentMlsId',
1279 'ListOfficeKey',
1280 'ListOfficeMlsId',
1281 'StandardStatus',
1282 'StandardStatusDelete',
1283 'ListingId',
1284 'extraCity',
1285 'extraCounty',
1286 'Exclude_ListOfficeKey',
1287 'Exclude_ListOfficeMlsId',
1288 'Exclude_ListAgentKey',
1289 'Exclude_ListAgentMlsId',
1290 ];
1291
1292 if ($mlsId > 5000) {
1293 $selectAllNone[] = 'PropertyType';
1294 }
1295
1296 if (!in_array($key, $selectAllNone)): ?>
1297 <?php
1298 esc_html_e('- Or Select All ', 'mlsimport');
1299
1300 ?>
1301 <input type="hidden" name="<?php echo esc_attr($nameCheck); ?>" value="0"/>
1302 <input type="checkbox" name="<?php echo esc_attr($nameCheck); ?>" value="1" <?php print esc_attr(checked($isCheckboxAdmin, 1, 0)); ?>/>
1303 <?php endif; ?>
1304 </p>
1305
1306 <?php
1307 $permittedStatus = ['active', 'active under contract', 'coming soon', 'activeundercontract', 'comingsoon', 'pending'];
1308
1309 if ($field['type'] === 'select'): ?>
1310 <?php
1311 $multiple = '';
1312 if ('yes' === $field['multiple']) {
1313 $multiple = 'multiple';
1314 $name .= '[]';
1315 }
1316
1317 if ('StandardStatus' === $key && '' === $value) {
1318 $value = ['Active'];
1319 }
1320
1321
1322
1323 // Additional conditions can be placed here.
1324 ?>
1325 <select class="mlsimport-select" id="<?php echo esc_attr($name); ?>" name="<?php echo esc_attr($name); ?>" <?php echo esc_attr($multiple); ?>>
1326 <?php foreach ($field['values'] as $selectKey): ?>
1327
1328 <?php if ('' !== $selectKey): ?>
1329 <option value="<?php echo esc_attr($selectKey); ?>"
1330 <?php
1331 if ($key === "StandardStatusDelete" && $value==null ) {
1332
1333 print 'selected';
1334 }
1335 ?>
1336 <?php if (is_array($value) ? in_array($selectKey, $value) : $selectKey === $value) echo 'selected'; ?>>
1337 <?php echo esc_html($selectKey); ?>
1338 </option>
1339 <?php endif; ?>
1340
1341 <?php endforeach; ?>
1342 </select>
1343
1344 <?php elseif ($field['type'] === 'input'): ?>
1345 <input type="text" class="mlsimport-select" id="<?php echo esc_attr($name); ?>" name="<?php echo esc_attr($name); ?>" value="<?php echo esc_attr($value); ?>">
1346 <?php endif; ?>
1347 <?php if ('extraCity' === $key || 'extraCounty' === $key): ?>
1348 </div>
1349 <?php endif; ?>
1350 </fieldset>
1351 <?php endforeach; ?>
1352
1353 </div>
1354 <?php
1355 return ob_get_clean();
1356 }
1357
1358
1359
1360
1361
1362
1363 public function mlsimport_add_extra_fields() {
1364 }
1365
1366 /**
1367 *
1368 *
1369 *
1370 *
1371 *
1372 *
1373 */
1374 function mlsimport_notes_for_mls( $mlsimport_mls_id, $name, $description ) {
1375 // 111 - Rae Edmonton
1376
1377 if ( 111 === intval($mlsimport_mls_id) && 'mlsimport_item_standardstatus' === $name ) {
1378 return esc_html__( 'Your MLS does not use this field - all listings are considered Active.', 'mlsimport' );
1379 } else {
1380 return $description;
1381 }
1382 }
1383
1384
1385 /**
1386 *
1387 *
1388 * Get Last date
1389 */
1390 public function mlsimport_saas_get_last_date( $item_id ) {
1391 $last_date = get_post_meta( $item_id, 'mlsimport_last_date', true );
1392
1393 if ( '' === $last_date ) {
1394 $last_date = $this->mlsimport_saas_update_last_date( $item_id );
1395 }
1396 return $last_date;
1397 }
1398
1399
1400 /**
1401 *
1402 *
1403 * Save Last date
1404 */
1405 public function mlsimport_saas_update_last_date( $item_id ) {
1406
1407 $unix_time = current_time( 'timestamp', 0 ) - ( 2 * 60 * 60 );
1408 print $last_date_to_save = date( 'Y-m-d\TH:i', $unix_time );
1409 update_post_meta( $item_id, 'mlsimport_last_date', $last_date_to_save );
1410
1411 return $last_date_to_save;
1412 }
1413
1414
1415
1416 /**
1417 *
1418 *
1419 * Check if there are modified items in the last 2h
1420 */
1421 public function mlsimport_saas_start_cron_links_per_item( $item_id ) {
1422
1423 $last_date = $this->mlsimport_saas_get_last_date( $item_id );
1424 print 'MLSitem id: '.$item_id.' - ';
1425 esc_html_e('date to consider: ','mlsimport');
1426 print esc_html($last_date ). '. ';
1427
1428 $mlsrequest = $this->mlsimport_make_listing_requests( $item_id, $last_date );
1429
1430 $found_items = 0;
1431 if ( isset( $mlsrequest['results'] ) ) {
1432 $found_items = intval( $mlsrequest['results'] );
1433 } else {
1434 delete_transient( 'mlsimport_saas_token' );
1435 }
1436
1437 print esc_html__('We found ','mlsimport') .esc_html( $found_items ). ' listings.</br>' . PHP_EOL;
1438
1439 $attachments_to_move = array();
1440
1441 if ( $found_items > 0 ) {
1442 $item_id_array = array(
1443 'item_id' => $item_id,
1444 'how_many' => 0,
1445 'max_number' => $found_items,
1446 'batch_counter' => 1,
1447 );
1448
1449 $attachments_to_move = (array) $this->mlsimport_saas_generate_import_requests_per_item( $item_id_array, $last_date );
1450
1451 update_post_meta( $item_id, 'mlsimport_spawn_status_cron_job', 'started' );
1452 update_post_meta( $item_id, 'mlsimport_cron_attach_to_move_' . $item_id, $attachments_to_move );
1453
1454 // save last date for next run
1455 $this->mlsimport_saas_update_last_date( $item_id );
1456
1457 $attachments_to_send = array(
1458 'args' => array(
1459 'attachments_to_move' => $item_id,
1460 'item_id_array' => $item_id_array,
1461 ),
1462 );
1463
1464 $this->mlsimport_background_process_per_item_cron_function( $attachments_to_send['args'] );
1465
1466 }
1467 }
1468
1469
1470
1471
1472 /**
1473 *
1474 *
1475 * Reconciliation log
1476 */
1477 public function mlsimport_saas_start_doing_reconciliation() {
1478 global $mlsimport;
1479 print 'start';
1480 $listingKey_in_Local = $this->mlsimport_saas_get_all_meta_values( 'ListingKey' );
1481
1482 if ( empty( $listingKey_in_Local ) ) {
1483 return;
1484 }
1485
1486 $mls_data = $this->mlsimport_saas_get_mls_reconciliation_data();
1487 $listingKey_in_MLS = $mls_data['all_data'];
1488 if ( empty( $listingKey_in_MLS ) ) {
1489 return;
1490 }
1491
1492 $to_delete = 0;
1493 $counter = 0;
1494 foreach ( $listingKey_in_Local as $key => $item ) {
1495 $listingkey = $item->meta_value;
1496 $property_id = $item->iD;
1497 ++$counter;
1498
1499 print '</br>'.$counter. ' **************************';
1500
1501 if ( in_array( $listingkey, $listingKey_in_MLS ) ) {
1502 $keep_when_in_mls = $mlsimport->admin->theme_importer->check_if_delete_when_status_when_in_mls($property_id);
1503 if(!$keep_when_in_mls){
1504 ++$to_delete;
1505 print wp_kses_post('</br>WE DELETE -------' .$listingkey. ' ------------------------- FOUND but item option says delete: '.$property_id.' <-');
1506 $mlsimport->admin->theme_importer->mlsimportSaasDeletePropertyViaMysql( $property_id, $listingkey );
1507 }else{
1508 print wp_kses_post('</br> WE KEEP ->>>>>> '.$listingkey . ' IS FOUND with '.$property_id);
1509 }
1510 } else {
1511
1512
1513 $keep = $mlsimport->admin->theme_importer->check_if_delete_when_status($property_id);
1514 if(!$keep){
1515 ++$to_delete;
1516 print wp_kses_post('</br>WE DELETE -------' .$listingkey. ' ------------------------- NOT FOUND delete: '.$property_id.' /'.$post_status.'<-');
1517 $mlsimport->admin->theme_importer->mlsimportSaasDeletePropertyViaMysql( $property_id, $listingkey );
1518 }else{
1519 print wp_kses_post('</br>WE KEEP ->>>>>>' .$listingkey. ' ------------------------- NOT FOUND BUT MARKED AS KEEP: '.$property_id.' /'.$post_status.'<-');
1520 }
1521
1522 }
1523
1524
1525 }
1526
1527 print esc_html(' to delete:' .$to_delete);
1528 return;
1529 }
1530
1531
1532
1533 /**
1534 *
1535 *
1536 * Requestq Reconciliation log
1537 */
1538 public function mlsimport_saas_get_mls_reconciliation_data() {
1539
1540 $arguments = array();
1541 $answer = $this->theme_importer->globalApiRequestCurlSaas( 'reconciliation', $arguments, 'GET' );
1542 return $answer;
1543 }
1544
1545 /**
1546 *
1547 *
1548 * Reconciliation get local data
1549 */
1550 public function mlsimport_saas_get_all_meta_values( $key ) {
1551 global $wpdb;
1552
1553 $result = $wpdb->get_results(
1554 $wpdb->prepare(
1555 "
1556 SELECT DISTINCT pm.meta_value,p.iD FROM {$wpdb->postmeta} pm
1557 LEFT JOIN {$wpdb->posts} p ON p.ID = pm.post_id
1558 WHERE pm.meta_key = %s
1559 AND p.post_status = 'publish'",
1560 $key
1561 )
1562 );
1563
1564 return $result;
1565 }
1566
1567
1568
1569
1570
1571 /*
1572 * Do api Listing Requests
1573 *
1574 *
1575 *
1576 *
1577 * */
1578 public function mlsimport_make_listing_requests( $item_id, $last_date = '', $skip = '', $top = '' ) {
1579 $options = get_option( $this->plugin_name . '_admin_options' );
1580 $mls_id = '';
1581 if ( isset( $options['mlsimport_mls_name'] ) ) {
1582 $mls_id = sanitize_text_field( trim( $options['mlsimport_mls_name'] ) );
1583 }
1584
1585 $arguments = $this->mlsimport_saas_make_listing_requests_arguments( $item_id, $last_date, $skip, $top );
1586
1587
1588 if (
1589 $mls_id > 5000 && $mls_id < 6000 &&
1590 ( ! isset( $arguments['property_type'] ) or
1591 ( isset( $arguments['property_type'] ) && '' === $arguments['property_type'] ) or
1592 ( isset( $arguments['property_type'][0] ) && '' === $arguments['property_type'][0] )
1593 )
1594 ) {
1595 return array(
1596 'success' => false,
1597 'type' => 'rapattoni',
1598 'message' => esc_html__( 'This MLS requires to have one item selected from "Property Action Category" dropdown', 'mlsimport' ),
1599 );
1600 }
1601
1602 $potential_leght = strlen( wp_json_encode( $arguments ) );
1603 if ( $potential_leght > 1750 ) {
1604 return array(
1605 'success' => false,
1606 'potential_leght' => $potential_leght,
1607 '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' ),
1608 );
1609 }
1610
1611 $answer = $this->theme_importer->globalApiRequestCurlSaas( 'listings', $arguments, 'POST' );
1612 $answer['potential_leght'] = $potential_leght;
1613 return ( $answer );
1614 }
1615
1616
1617
1618
1619
1620
1621 /*
1622 * Create Api query arguments
1623 *
1624 *
1625 *
1626 *
1627 *
1628 * */
1629
1630 public function mlsimport_saas_make_listing_requests_arguments( $item_id, $last_date = '', $skip = '', $top = '' ) {
1631
1632 $options = get_option( $this->plugin_name . '_admin_options' );
1633 if ( isset( $options['mlsimport_mls_name'] ) ) {
1634 $mls_id = intval( $options['mlsimport_mls_name'] );
1635 } else {
1636 return '';
1637 }
1638
1639 if ( isset( $options['mlsimport_theme_used'] ) ) {
1640 $theme_id = intval( $options['mlsimport_theme_used'] );
1641 } else {
1642 return '';
1643 }
1644
1645 $values = array();
1646 $values['mls_id'] = $mls_id;
1647 $values['theme_id'] = $theme_id;
1648
1649 if ( '' !== $top ) {
1650 $values['top'] = $top;
1651 $values['skip'] = intval( $skip );
1652 }
1653
1654 // // add price
1655 $mlsimport_item_min_price = get_post_meta( $item_id, 'mlsimport_item_min_price', true );
1656 $mlsimport_item_max_price = get_post_meta( $item_id, 'mlsimport_item_max_price', true );
1657 if ( '' !== $mlsimport_item_min_price && '' !== $mlsimport_item_max_price ) {
1658 $values['list_price_min'] = floatval( $mlsimport_item_min_price );
1659 $values['list_price_max'] = floatval( $mlsimport_item_max_price );
1660 }
1661
1662 // add city
1663 $values = $this->mls_import_return_multiple_param_value( 'city', $item_id, 'city', $values );
1664
1665 // add county
1666 $values = $this->mls_import_return_multiple_param_value( 'countyorparish', $item_id, 'county_or_parish', $values );
1667
1668 // add postal code
1669 $values = $this->mls_import_saas_add_to_parms_input( 'PostalCode', $item_id, 'postal_code', $values );
1670
1671 // add status
1672
1673 if ( 111 !== $mls_id ) { // edmonton check
1674 $values = $this->mls_import_return_multiple_param_value( 'StandardStatus', $item_id, 'status', $values );
1675 }
1676
1677 // add property_subtype
1678 $values = $this->mls_import_return_multiple_param_value( 'PropertySubType', $item_id, 'property_subtype', $values );
1679
1680 // add property_type
1681 $values = $this->mls_import_return_multiple_param_value( 'PropertyType', $item_id, 'property_type', $values );
1682
1683 // rapattoni exception
1684 if ( $mls_id > 5000 ) {
1685 $values = $this->mls_import_saas_add_to_parms_input( 'PropertyType', $item_id, 'property_type', $values );
1686 $temp = $values['property_type'];
1687 $temp = str_replace( ' ', '', $temp );
1688 $values['property_type'] = array();
1689 $values['property_type'][] = $temp;
1690 }
1691
1692 // add internet_entirelisting_displayyn
1693 $values = $this->mls_import_saas_add_to_parms_input( 'InternetEntireListingDisplayYN', $item_id, 'internet_entirelisting_displayyn', $values );
1694
1695 // add internet_address_displayyn
1696 $values = $this->mls_import_saas_add_to_parms_input( 'InternetAddressDisplayYN', $item_id, 'internet_address_displayyn', $values );
1697
1698 // add ListAgentKey
1699 $values = $this->mls_import_saas_add_to_parms_input( 'ListAgentKey', $item_id, 'list_agentkey', $values );
1700 // add ListAgentKey
1701 $values = $this->mls_import_saas_add_to_parms_input( 'ListAgentMlsId', $item_id, 'list_agentmlsid', $values );
1702 // add ListOfficeKey
1703 $values = $this->mls_import_saas_add_to_parms_input( 'ListOfficeKey', $item_id, 'list_officekey', $values );
1704 // add ListOfficeMlsId
1705 $values = $this->mls_import_saas_add_to_parms_input( 'ListOfficeMlsId', $item_id, 'list_officemlsid', $values );
1706
1707 // add ListingId
1708 $values = $this->mls_import_saas_add_to_parms_input( 'ListingId', $item_id, 'listingid', $values );
1709
1710 //add Exclude_ListOfficeKey
1711 $values = $this->mls_import_saas_add_to_parms_input( 'Exclude_ListOfficeKey', $item_id, 'exclude_list_officekey', $values );
1712 // add Exclude_ListOfficeMlsId
1713 $values = $this->mls_import_saas_add_to_parms_input( 'Exclude_ListOfficeMlsId', $item_id, 'exclude_list_officemlsid', $values );
1714
1715
1716
1717 //add Exclude_ListAgentKey
1718 $values = $this->mls_import_saas_add_to_parms_input( 'Exclude_ListAgentKey', $item_id, 'exclude_list_agentkey', $values );
1719 // add Exclude_ListAgentMlsId
1720 $values = $this->mls_import_saas_add_to_parms_input( 'Exclude_ListAgentMlsId', $item_id, 'exclude_list_agentmlsid', $values );
1721
1722
1723
1724 if ( '' !== $last_date ) {
1725 $values['modification_time'] = $last_date;
1726 }
1727
1728 return( $values );
1729 }
1730
1731
1732
1733 /*
1734 *
1735 * add input items to parameters array
1736 *
1737 */
1738
1739 public function mls_import_saas_add_to_parms_input( $key, $post_id, $new_name, $all_values ) {
1740 $name = strtolower( 'mlsimport_item_' . $key );
1741 $value = get_post_meta( $post_id, $name, true );
1742 if ( '' !== $value ) {
1743 $all_values[ $new_name ] = $value;
1744 }
1745
1746 return $all_values;
1747 }
1748
1749
1750 /*
1751 *
1752 * add list items to parameters array
1753 *
1754 */
1755
1756 public function mls_import_return_multiple_param_value( $key, $post_id, $new_name, $all_values ) {
1757 $name_check = strtolower( 'mlsimport_item_' . $key . '_check' );
1758 $name = strtolower( 'mlsimport_item_' . $key );
1759
1760 $value = get_post_meta( $post_id, $name, true );
1761
1762 // add extra county - should be moved into function if pass tests
1763 if ( 'countyorparish' === $key ) {
1764 $extracounty_values = get_post_meta( $post_id, 'mlsimport_item_extracounty', true );
1765
1766 if ( '' !== $extracounty_values ) {
1767 $extracounty_array = explode( ',', $extracounty_values );
1768
1769 if ( ! is_array( $value ) ) {
1770 if ( '' === $value ) {
1771 $value = array();
1772 } else {
1773 $value = array( $value );
1774 }
1775 }
1776
1777 foreach ( $extracounty_array as $extra ) {
1778 $value[] = $extra;
1779 }
1780 }
1781 }
1782
1783 // add extra city - should be moved into function if pass tests
1784 if ( 'city' === $key ) {
1785 $extracity_values = get_post_meta( $post_id, 'mlsimport_item_extracity', true );
1786 if ( '' !== $extracity_values ) {
1787 $extracity_array = explode( ',', $extracity_values );
1788
1789 if ( ! is_array( $value ) ) {
1790 if ('' === $value ) {
1791 $value = array();
1792 } else {
1793 $value = array( $value );
1794 }
1795 }
1796
1797 foreach ( $extracity_array as $extra ) {
1798 $value[] = $extra;
1799 }
1800 }
1801 }
1802
1803 $value_check = get_post_meta( $post_id, $name_check, true );
1804
1805 if ( 0 === intval($value_check) && '' !== $value ) {
1806 $all_values[ $new_name ] = $value;
1807 }
1808
1809 // status exception
1810 if ( 'status' === $new_name ) {
1811 $all_values[ $new_name ] = $value;
1812 }
1813
1814 return $all_values;
1815 }
1816
1817
1818
1819 /*
1820 *
1821 * All Enums fiels to be used on MLS import Item
1822 *
1823 *
1824 *
1825 *
1826 *
1827 *
1828 * */
1829
1830 public function mlsimport_saas_return_mls_fields() {
1831
1832 $mlsimport_mls_metadata_mls_enums = get_option( 'mlsimport_mls_metadata_mls_enums', '' );
1833
1834 if ( '' === $mlsimport_mls_metadata_mls_enums ) {
1835 ?>
1836 <div class="mlsimport_warning long_warning">Please select the import fields(from MLS Import Settings) before starting a MLS import process.</div>
1837 <?php
1838 }
1839
1840 $metadata_api_call_full = json_decode( $mlsimport_mls_metadata_mls_enums, true );
1841
1842 if ( isset( $metadata_api_call_full['global_array'] ) ) {
1843 $metadata_api_call = $metadata_api_call_full['global_array'];
1844 }
1845
1846 $city_array = array();
1847 if ( isset( $metadata_api_call['PropertyEnums']['City'] ) && is_array( $metadata_api_call['PropertyEnums']['City'] ) ) {
1848 $city_array = array_keys( $metadata_api_call['PropertyEnums']['City'] );
1849 }
1850
1851 $county_array = array();
1852 if ( isset( $metadata_api_call['PropertyEnums']['CountyOrParish'] ) && is_array( $metadata_api_call['PropertyEnums']['CountyOrParish'] ) ) {
1853 $county_array = array_keys( $metadata_api_call['PropertyEnums']['CountyOrParish'] );
1854 }
1855
1856 $mlsstatus_array = array();
1857 if ( isset( $metadata_api_call['PropertyEnums']['MlsStatus'] ) && is_array( $metadata_api_call['PropertyEnums']['MlsStatus'] ) ) {
1858 $mlsstatus_array = array_keys( $metadata_api_call['PropertyEnums']['MlsStatus'] );
1859 }
1860
1861 $propertysubtype_array = array();
1862 if ( isset( $metadata_api_call['PropertyEnums']['PropertySubType'] ) && is_array( $metadata_api_call['PropertyEnums']['PropertySubType'] ) ) {
1863 $propertysubtype_array = array_keys( $metadata_api_call['PropertyEnums']['PropertySubType'] );
1864 }
1865
1866 $propertytype_array = array();
1867 if ( isset( $metadata_api_call['PropertyEnums']['PropertyType'] ) && is_array( $metadata_api_call['PropertyEnums']['PropertyType'] ) ) {
1868 $propertytype_array = array_keys( $metadata_api_call['PropertyEnums']['PropertyType'] );
1869 }
1870
1871 $standardstatus_array = array();
1872 $standardstatus_delete_array=array();
1873 if ( isset( $metadata_api_call['PropertyEnums']['StandardStatus'] ) && is_array( $metadata_api_call['PropertyEnums']['StandardStatus'] ) ) {
1874 $standardstatus_array = array_keys( $metadata_api_call['PropertyEnums']['StandardStatus'] );
1875 $standardstatus_delete_array= array_keys( $metadata_api_call['PropertyEnums']['StandardStatus'] );
1876 }
1877
1878 // if we do not have standart status
1879 if ( empty( $standardstatus_array ) ) {
1880 $standardstatus_array = $mlsstatus_array;
1881 $standardstatus_delete_array = $mlsstatus_array;
1882
1883 }
1884
1885
1886 $permited_status=array('active','active under contract','coming soon','activeundercontract','comingsoon','pending');
1887 $permited_status_lower = array_map('strtolower', $permited_status);
1888
1889 // Filter out permitted statuses from array1 values
1890 // $standardstatus_delete_array = array_filter($standardstatus_delete_array, function ($value) use ($permited_status_lower) {
1891 // return !in_array(strtolower($value), $permited_status_lower);
1892 // });
1893
1894
1895
1896
1897 $extracounty_values = '';
1898 $extracity_values = '';
1899
1900 $field_import = array(
1901 'City' => array(
1902 'label' => esc_html__( 'Select cities', 'mlsimport' ),
1903 'description' => esc_html__( 'Select the cities from where we will import data.', 'mlsimport' ),
1904 'type' => 'select',
1905 'multiple' => 'yes',
1906 'values' => $city_array,
1907 ),
1908
1909 'extraCity' => array(
1910 'label' => esc_html__( 'Add extra Cities', 'mlsimport' ),
1911 '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' ),
1912 'type' => 'input',
1913 'multiple' => 'no',
1914 'values' => $extracity_values,
1915 ),
1916
1917 'CountyOrParish' => array(
1918 'label' => esc_html__( 'Select Counties', 'mlsimport' ),
1919 'description' => esc_html__( 'Select the counties from where we will import data.', 'mlsimport' ),
1920 'type' => 'select',
1921 'multiple' => 'yes',
1922 'values' => $county_array,
1923 'show_extra_field' => true,
1924 ),
1925
1926 'extraCounty' => array(
1927 'label' => esc_html__( 'Add extra Counties', 'mlsimport' ),
1928 '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' ),
1929 'type' => 'input',
1930 'multiple' => 'no',
1931 'values' => $extracounty_values,
1932 ),
1933
1934 'PostalCode' => array(
1935 'label' => esc_html__( 'Select Postal Code', 'mlsimport' ),
1936 'description' => esc_html__( 'Select the PostalCode from where to import listings. Works with only one PostalCode.', 'mlsimport' ),
1937 'type' => 'input',
1938 'multiple' => 'no',
1939 ),
1940
1941 'PropertySubType' => array(
1942 'label' => esc_html__( 'Select Property Category', 'mlsimport' ),
1943 'description' => esc_html__( 'Property Category', 'mlsimport' ),
1944 'type' => 'select',
1945 'multiple' => 'yes',
1946 'values' => $propertysubtype_array,
1947 ),
1948 'PropertyType' => array(
1949 'label' => esc_html__( 'Select Property Action Category', 'mlsimport' ),
1950 'description' => esc_html__( 'Property Action Category', 'mlsimport' ),
1951 'type' => 'select',
1952 'multiple' => 'yes',
1953 'values' => $propertytype_array,
1954 ),
1955 'StandardStatus' => array(
1956 'label' => esc_html__( 'Select Status', 'mlsimport' ),
1957 'description' => __( 'The list is auto-populated with MLS available statuses. To select multiple statuses, use Ctrl (Windows) or Command (Mac).', 'mlsimport' ),
1958 'type' => 'select',
1959 'multiple' => 'yes',
1960 'values' => $standardstatus_array,
1961 ),
1962 'StandardStatusDelete' => array(
1963 'label' => esc_html__( 'Delete Statuses', 'mlsimport' ),
1964 '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' ),
1965 'type' => 'select',
1966 'multiple' => 'yes',
1967 'values' => $standardstatus_delete_array,
1968 ),
1969
1970 'InternetEntireListingDisplayYN' => array(
1971 'label' => esc_html__( 'Internet Entire Listing Display ', 'mlsimport'),
1972 'description' => esc_html__( 'A yes/no field that states the seller has allowed the listing to be displayed on Internet sites.', 'mlsimport' ),
1973 'type' => 'select',
1974 'multiple' => 'no',
1975 'values' => array(
1976 'yes',
1977 'no',
1978 ),
1979 ),
1980 'InternetAddressDisplayYN' => array(
1981 'label' => esc_html__( 'Internet Address display', 'mlsimport' ),
1982 'description' => esc_html__( 'A yes/no field that states the seller has allowed the listing address to be displayed on Internet sites.', 'mlsimport' ),
1983 'type' => 'select',
1984 'multiple' => 'no',
1985 'values' => array(
1986 'yes',
1987 'no',
1988 ),
1989 ),
1990 'ListAgentKey' => array(
1991 'label' => esc_html__( 'ListAgentKey', 'mlsimport' ),
1992 'description' => esc_html__( 'Import listings from a specific Agent (contact your MLS for this information)', 'mlsimport' ),
1993 'type' => 'input',
1994 'multiple' => 'no',
1995 ),
1996 'ListAgentMlsId' => array(
1997 'label' => esc_html__( 'ListAgentMlsId', 'mlsimport' ),
1998 'description' => esc_html__( 'Import listings from a specific Agent (contact your MLS for this information)', 'mlsimport' ),
1999 'type' => 'input',
2000 'multiple' => 'no',
2001 ),
2002 'ListOfficeKey' => array(
2003 'label' => esc_html__( 'ListOfficeKey', 'mlsimport' ),
2004 'description' => esc_html__( 'Import listings from a specific Office (contact your MLS for this information)', 'mlsimport'),
2005 'type' => 'input',
2006 'multiple' => 'no',
2007 ),
2008 'ListOfficeMlsId' => array(
2009 'label' => esc_html__( 'ListOfficeMlsId', 'mlsimport' ),
2010 'description' => esc_html__( 'Import listings from a specific Office (contact your MLS for this information)', 'mlsimport' ),
2011 'type' => 'input',
2012 'multiple' => 'no',
2013 ),
2014 'ListingId' => array(
2015 'label' => esc_html__( 'ListingId', 'mlsimport' ),
2016 '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'),
2017 'type' => 'input',
2018 'multiple' => 'no',
2019 ),
2020 'Exclude_ListOfficeMlsId' => array(
2021 'label' => esc_html__( 'Exclude listings with ListOfficeMlsId', 'mlsimport' ),
2022 'description' => esc_html__( 'Exclude listings that belong to one or more ListOfficeMlsId.', 'mlsimport' ),
2023 'type' => 'input',
2024 'multiple' => 'no',
2025 ),
2026 'Exclude_ListOfficeKey' => array(
2027 'label' => esc_html__( 'Exclude listings with ListOfficeKey', 'mlsimport' ),
2028 'description' => esc_html__( 'Exclude listings that belong to one or more ListOfficeKey', 'mlsimport'),
2029 'type' => 'input',
2030 'multiple' => 'no',
2031 ),
2032
2033
2034 'Exclude_ListAgentMlsId' => array(
2035 'label' => esc_html__( 'Exclude listings with ListAgentMlsId', 'mlsimport' ),
2036 'description' => esc_html__( 'Exclude listings that belong to one or more ListAgentMlsId.', 'mlsimport' ),
2037 'type' => 'input',
2038 'multiple' => 'no',
2039 ),
2040 'Exclude_ListAgentKey' => array(
2041 'label' => esc_html__( 'Exclude listings with ListAgentKey ', 'mlsimport' ),
2042 'description' => esc_html__( 'Exclude listings that belong to one or more ListAgentKey ', 'mlsimport'),
2043 'type' => 'input',
2044 'multiple' => 'no',
2045 ),
2046
2047
2048 );
2049 return $field_import;
2050 }
2051
2052
2053
2054
2055
2056
2057
2058 /**
2059 *
2060 *
2061 * AYsnc Test
2062 */
2063 public function mlsimport_move_files_per_item() {
2064 check_ajax_referer( 'mlsimport_item_actions', 'security' );
2065 $post_id = 0;
2066 $how_many = 0;
2067 $max_number = 0;
2068 if(isset( $_POST['post_id'] )){
2069 $post_id = intval( $_POST['post_id'] );
2070 }
2071 if(isset( $_POST['how_many'] )){
2072 $how_many = intval( $_POST['how_many'] );
2073 }
2074 if(isset( $_POST['post_number'] )){
2075 $max_number = intval( $_POST['post_number'] );
2076 }
2077
2078 update_option( 'mlsimport_force_stop_' . $post_id, 'no', false );
2079
2080 $item_id_array = array(
2081 'item_id' => $post_id,
2082 'how_many' => $how_many,
2083 'max_number' => $max_number,
2084 'batch_counter' => 1,
2085 );
2086
2087 update_post_meta( $post_id, 'mlsimport_attach_to_move_' . $post_id, '' );
2088 $attachments_to_move = (array) $this->mlsimport_saas_generate_import_requests_per_item( $item_id_array );
2089
2090 update_post_meta( $post_id, 'mlsimport_attach_to_move_' . $post_id, $attachments_to_move );
2091
2092 $attachments_to_send = array(
2093 'args' => array(
2094 'attachments_to_move' => $post_id,
2095 'item_id_array' => $item_id_array,
2096 ),
2097 );
2098
2099 mlsimport_saas_single_write_import_custom_logs( 'Preparing the import. Please hold on.' . PHP_EOL );
2100 mlsimport_debuglogs_per_plugin( 'Preparing the import. Please hold on.' . PHP_EOL );
2101
2102 update_post_meta( $post_id, 'mlsimport_spawn_status', 'started' );
2103 as_enqueue_async_action( 'mlsimport_background_process_per_item', $attachments_to_send );
2104 spawn_cron();
2105
2106 unset( $attachments_to_send );
2107 die();
2108 }
2109
2110 /**
2111 *
2112 *
2113 * Processing Cron
2114 */
2115 public function mlsimport_background_process_per_item_cron_function( $input_arg ) {
2116 $log = 'In cron processing function ->' . wp_json_encode( $input_arg['item_id_array'] ) . PHP_EOL;
2117 mlsimport_saas_single_write_import_custom_logs( $log, 'cron' );
2118 global $mlsimport;
2119
2120 // Get from MLS Import it the big argument arrat
2121 $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 );
2122
2123 $log = 'In processing function mlsimport_cron_attach_to_move_ ->' . wp_json_encode( $attachments_to_move ) . PHP_EOL;
2124 mlsimport_saas_single_write_import_custom_logs( $log );
2125
2126
2127 // foreach($input_arg['attachments_to_move'] as $key=>$import_link){
2128 foreach ( $attachments_to_move as $key => $import_arguments ) {
2129 $GLOBALS['wp_object_cache']->delete( 'mlsimport_force_stop_' . $input_arg['item_id_array']['item_id'], 'options' );
2130
2131 $log = PHP_EOL . ' Cron Parsing importing batch : ' . $key . ' = ' . wp_json_encode( $import_arguments ) . PHP_EOL;
2132 mlsimport_saas_single_write_import_custom_logs( $log, 'cron' );
2133
2134 $api_call_array = $this->theme_importer->globalApiRequestCurlSaas( 'listings', $import_arguments, 'POST' );
2135
2136 $mlsimport->admin->theme_importer->mlsimportSaasCronParseSearchArrayPerItem( $api_call_array, $input_arg['item_id_array'], $key );
2137 }
2138
2139 mlsimport_saas_single_write_import_custom_logs( 'CRON JOB Import Completed ' . PHP_EOL );
2140 mlsimport_debuglogs_per_plugin( 'CRON JOB Import Completed ' . PHP_EOL );
2141 update_post_meta( $input_arg['item_id_array']['item_id'], 'mlsimport_spawn_status', 'completed' );
2142 }
2143
2144 /**
2145 *
2146 *
2147 * Generate import Requests per item
2148 */
2149 public function mlsimport_saas_generate_import_requests_per_item( $item_id_array, $last_date = '' ) {
2150 $import_step = 25;
2151
2152 $prop_id = $item_id_array['item_id'];
2153 $max_found = $item_id_array['max_number'];
2154 $how_many = $item_id_array['how_many'];
2155 if ( 0=== intval($how_many) ) {
2156 $how_many = $max_found;
2157 }
2158 if ( $how_many > $max_found ) {
2159 $how_many = $max_found;
2160 }
2161
2162 $search_url_step = '';
2163 $urls_array = array();
2164
2165 $skip = 0;
2166 if ( $how_many > 10000 ) {
2167 $how_many = 10000;
2168 }
2169
2170 if ( $how_many < $import_step ) {
2171 $import_step = $how_many;
2172 }
2173
2174 while ( $skip < $how_many ) {
2175 $search_url_step = $this->mlsimport_saas_make_listing_requests_arguments( $prop_id, $last_date, $skip, $import_step );
2176 $skip = $skip + $import_step;
2177 $urls_array[] = $search_url_step;
2178 }
2179 return $urls_array;
2180 }
2181
2182
2183
2184
2185
2186 /**
2187 * Process Async function
2188 */
2189 public function mlsimport_background_process_per_item_function( $input_arg ) {
2190
2191 $mlsimportItemId = $input_arg['item_id_array']['item_id'];
2192 $log_prefix = 'In processing function - Item ID: ' . $mlsimportItemId . ' -> ';
2193 mlsimport_saas_single_write_import_custom_logs( $log_prefix . wp_json_encode( $input_arg['item_id_array'] ) . PHP_EOL );
2194
2195 //ini_set('memory_limit', '256M');
2196 //ini_set('max_execution_time', 0); // Unlimited execution time
2197
2198 // Get from MLS Import the big argument array only once
2199 $attachments_to_move = get_post_meta( $mlsimportItemId, 'mlsimport_attach_to_move_' . $mlsimportItemId, true );
2200
2201 // Retrieve all meta data in one go to reduce database queries
2202 $mlsimport_item_option_data = array(
2203 'mlsimport_item_standardstatus' => get_post_meta( $mlsimportItemId, 'mlsimport_item_standardstatus', true ),
2204 'mlsimport_item_standardstatusdelete' => get_post_meta( $mlsimportItemId, 'mlsimport_item_standardstatusdelete', true ),
2205 'mlsimport_item_property_user' => get_post_meta( $mlsimportItemId, 'mlsimport_item_property_user', true ),
2206 'mlsimport_item_agent' => get_post_meta( $mlsimportItemId, 'mlsimport_item_agent', true ),
2207 'mlsimport_item_property_status' => get_post_meta( $mlsimportItemId, 'mlsimport_item_property_status', true ),
2208 );
2209
2210 $total_batches = count( $attachments_to_move );
2211
2212 // removed because $this
2213 global $mlsimport;
2214
2215 $log = 'In processing function $attachments_to_move ->' . wp_json_encode( $attachments_to_move ) . PHP_EOL;
2216 mlsimport_saas_single_write_import_custom_logs( $log );
2217 // mlsimport_debuglogs_per_plugin($log);
2218 print esc_html($log);
2219
2220 foreach ( $attachments_to_move as $key => $import_arguments ) {
2221 // reconsider use
2222 // $GLOBALS['wp_object_cache']->delete('mlsimport_force_stop_' . $input_arg['item_id_array']['item_id'], 'options');
2223 $status = get_option( 'mlsimport_force_stop_' . $mlsimportItemId );
2224 if ( 'no' === $status ) {
2225 // reconsider use
2226 // wp_cache_flush();
2227 $mem_usage = memory_get_usage( true );
2228 $mem_usage_show = round( $mem_usage / 1048576, 2 );
2229
2230
2231
2232 mlsimport_saas_single_write_import_custom_logs( $log );
2233 $log = 'Parsing import batch: ' . ( $key + 1 ) . ' of ' . $total_batches . '. Memory used: ' . $mem_usage_show . ' MB.' . PHP_EOL;
2234
2235 // Combine logs and reduce function calls
2236 mlsimport_saas_single_write_import_custom_logs( $log );
2237 mlsimport_debuglogs_per_plugin( $log );
2238 print esc_html($log);
2239
2240 $api_call_array = $this->theme_importer->globalApiRequestCurlSaas( 'listings', $import_arguments, 'POST' );
2241
2242 $mlsimport->admin->theme_importer->mlsimportSaasParseSearchArrayPerItem( $api_call_array, $input_arg['item_id_array'], $key, $mlsimport_item_option_data );
2243 } else {
2244 update_post_meta( $mlsimportItemId, 'mlsimport_spawn_status', 'completed' );
2245 delete_post_meta( $mlsimportItemId, 'mlsimport_attach_to_move_' . $mlsimportItemId );
2246 mlsimport_saas_single_write_import_custom_logs( PHP_EOL . 'Parsing importing link FORCE STOP : ' );
2247 mlsimport_debuglogs_per_plugin( 'Parsing importing link FORCE STOP : ' );
2248 break; // Exit the loop if forced to stop
2249 }
2250 }
2251
2252 mlsimport_saas_single_write_import_custom_logs( 'Import Completed ' . PHP_EOL );
2253 mlsimport_debuglogs_per_plugin( 'Import Completed ' . PHP_EOL );
2254
2255 update_post_meta( $mlsimportItemId, 'mlsimport_spawn_status', 'completed' );
2256 delete_post_meta( $mlsimportItemId, 'mlsimport_attach_to_move_' . $mlsimportItemId );
2257
2258 unset( $attachments_to_move );
2259 unset( $api_call_array );
2260 unset( $input_arg );
2261 unset( $log );
2262 unset( $log2 );
2263 }
2264
2265
2266
2267
2268
2269 /**
2270 *
2271 *
2272 *
2273 * update log function
2274 */
2275 public function mlsimport_logger_per_item() {
2276 check_ajax_referer( 'mlsimport_item_actions', 'security' );
2277 $post_id=0;
2278 if(isset($_POST['post_id'] )){
2279 $post_id = intval( $_POST['post_id'] );
2280 }
2281
2282 $status = get_post_meta( $post_id, 'mlsimport_spawn_status', true );
2283 $path = WP_PLUGIN_DIR . '/mlsimport/logs/status_logs.log';
2284 $logs = file_get_contents( $path );
2285
2286 $force_status = intval( get_post_meta( $post_id, 'mlsimport_force_stop', true ) );
2287 $force_status = get_option( 'mlsimport_force_stop_' . $post_id );
2288
2289 if ( 'no' !== $force_status ) {
2290 echo wp_json_encode(
2291 array(
2292 'is_done' => 'done',
2293 'status' => $status,
2294 'logs' => $logs,
2295 )
2296 );
2297 die();
2298 }
2299
2300 if ( '' === $status || 'completed' === $status ) {
2301 echo wp_json_encode(
2302 array(
2303 'is_done' => 'done',
2304 'status' => $status,
2305 'logs' => $logs,
2306 )
2307 );
2308 } else {
2309 // return from log
2310 echo wp_json_encode(
2311 array(
2312 'is_done' => 'wip',
2313 'status' => $status,
2314 'logs' => $logs,
2315 )
2316 );
2317 }
2318 die();
2319 }
2320
2321
2322
2323
2324
2325
2326 /**
2327 *
2328 *
2329 *
2330 *
2331 * Force Stop Import
2332 */
2333 public function mlsimport_stop_import_per_item() {
2334 check_ajax_referer( 'mlsimport_item_actions', 'security' );
2335 $post_id=0;
2336 if(isset($_POST['post_id'] )){
2337 $post_id = intval( $_POST['post_id'] );
2338 }
2339 update_option( 'mlsimport_force_stop_' . $post_id, 'yes', false );
2340 mlsimport_saas_single_write_import_custom_logs( 'Stopeed for ' . $post_id . PHP_EOL );
2341 mlsimport_debuglogs_per_plugin( 'Stopeed for ' . $post_id . PHP_EOL );
2342
2343 die();
2344 }
2345
2346
2347
2348 /*
2349 *
2350 * Get MLS Metadata
2351 *
2352 *
2353 *
2354 *
2355 **/
2356
2357 public function mlsimport_saas_get_metadata_function() {
2358 check_ajax_referer( 'mlsimport_saas_get_metadata', 'security' );
2359 $theme_Start = new ThemeImport();
2360
2361 $values = array();
2362 $options = get_option( $this->plugin_name . '_admin_options' );
2363 $url = 'clients?theme_id=' . intval( $options['mlsimport_theme_used'] );
2364
2365 $answer = $theme_Start::globalApiRequestSaas( $url, $values, 'GET' );
2366
2367 update_option( 'mlsimport_mls_metadata_populated', 'yes' );
2368
2369 update_option( 'mlsimport_mls_metadata_theme_schema', $answer['theme_schema'] );
2370 update_option( 'mlsimport_mls_metadata_mls_data', $answer['mls_data']['mls_meta_data'] );
2371 update_option( 'mlsimport_mls_metadata_mls_enums', $answer['mls_data']['mls_meta_enums'] );
2372 }
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385 /**
2386 *
2387 *
2388 * write debug logs
2389 */
2390 public function mlsimport_debuglog_cron( $message ) {
2391 if ( is_array( $message ) ) {
2392 $message = wp_json_encode( $message );
2393 }
2394 $message = date( 'F j, Y, g:i a' ) . ' -> ' . $message;
2395 global $wp_filesystem;
2396 if ( empty( $wp_filesystem ) ) {
2397 require_once ABSPATH . '/wp-admin/includes/file.php';
2398 WP_Filesystem();
2399 }
2400
2401 $path = WP_PLUGIN_DIR . '/mlsimport/logs/cron_logs.log';
2402
2403 file_put_contents( $path, $message, FILE_APPEND | LOCK_EX );
2404 }
2405
2406 }
2407