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

2,335 lines 72.0 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', 'StandardStatus', 'StandardStatus_check', 'InternetEntireListingDisplayYN', 'InternetAddressDisplayYN' );
422 foreach ( $field_import as $key ) {
423 $valid[ $key ] = $input[ $key ];
424 }
425
426 return $valid;
427 }
428
429
430 /**
431 *
432 *
433 * Validate Administrative options
434 *
435 * @since 1.0.0
436 */
437 public function validate_administrative_options( $input ) {
438
439 $valid = array();
440
441 $field_import = array( 'import' );
442 foreach ( $field_import as $key ) {
443 $valid[ $key ] = $input[ $key ];
444 }
445
446 return $valid;
447 }
448
449 /**
450 *
451 *
452 *
453 * Validate Import Options fields
454 *
455 * @since 1.0.0
456 */
457 public function validate_admin_import_options( $input ) {
458 $valid = array();
459
460 $field_import = array( 'import_number' );
461 foreach ( $field_import as $key ) {
462 $valid[ $key ] = intval( $input[ $key ] );
463 }
464
465 if ( isset( $input['import'] ) && '' !== $input['import'] ) {
466 $decode = json_decode( $input['import'] );
467 update_option( 'mlsimport_admin_fields_select', $decode['mlsimport_admin_fields_select'] );
468 update_option( 'mlsimport_admin_mls_sync', $decode['mlsimport_admin_mls_sync'] );
469 update_option( 'mlsimport_admin_import_options', $decode['mlsimport_admin_import_options'] );
470 update_option( 'mlsimport_admin_use_transients', $decode['mlsimport_admin_use_transients'] );
471 }
472
473 return $valid;
474 }
475
476
477
478
479
480
481 /**
482 *
483 *
484 * plugin options update
485 */
486 public function options_update() {
487 register_setting( $this->plugin_name . '_admin_options', $this->plugin_name . '_admin_options', array( $this, 'validate_admin_options' ) );
488 register_setting( $this->plugin_name . '_admin_fields_select', $this->plugin_name . '_admin_fields_select', array( $this, 'validate_admin_fields_select' ) );
489 register_setting( $this->plugin_name . '_admin_mls_sync', $this->plugin_name . '_admin_mls_sync', array( $this, 'validate_admin_mls_sync' ) );
490 register_setting( $this->plugin_name . '_admin_import_options', $this->plugin_name . '_admin_import_options', array( $this, 'validate_admin_import_options' ) );
491 register_setting( $this->plugin_name . '_administrative_options', $this->plugin_name . '_administrative_options', array( $this, 'validate_administrative_options' ) );
492 }
493
494
495
496 /**
497 *
498 *
499 *
500 *
501 *
502 *
503 *
504 */
505 public function update_option_mlsimport_administrative_options() {
506 $import = get_option( 'mlsimport_administrative_options' );
507 if ( '' !== $import ) {
508 $decode = json_decode( $import['import'], true );
509 update_option( 'mlsimport_admin_fields_select', $decode['mlsimport_admin_fields_select'] );
510 update_option( 'mlsimport_admin_mls_sync', $decode['mlsimport_admin_mls_sync'] );
511 update_option( 'mlsimport_admin_import_options', $decode['mlsimport_admin_import_options'] );
512 }
513 }
514
515 /**
516 *
517 *
518 * plugin options update
519 */
520 public function update_option_mlsimport_admin_fields_select() {
521
522 $this->env_data->enviroment_custom_fields( $this->plugin_name );
523 }
524
525
526 /**
527 *
528 *
529 * plugin options update
530 */
531 public function mlsimport_meta_options() {
532 if ( method_exists( $this->env_data, 'get_property_post_type' ) ) {
533 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' );
534 }
535 }
536
537 /**
538 *
539 *
540 * plugin options update
541 */
542 public function mlsimport_hidden_fields() {
543 global $post;
544 $options = get_option( $this->plugin_name . '_admin_fields_select' );
545
546 foreach ( $options['mls-fields-admin'] as $key => $value ) {
547 if ( 1 === intval($options['mls-fields-admin'][ $key ] ) ) {
548 if ( isset( $options['mls-fields-label'][ $key ] ) && '' !== $options['mls-fields-label'][ $key ] ) {
549 $key = $options['mls-fields-label'][ $key ];
550 }
551
552 if ( 'ListingKey' !== $key ) {
553 $meta_key = strtolower( $key );
554 } else {
555 $meta_key = $key;
556 }
557 ?>
558
559 <strong><?php echo esc_html($key);?>:</strong>
560 <?php echo esc_html( get_post_meta( $post->ID, $meta_key, true ) ); ?> </br>
561 <?php
562 }
563 }
564 ?>
565
566 <h2 style="font-weight:bold;padding-left:0px;">Mls Import History</h2>
567 <?php
568 $meta = get_post_meta( $post->ID, 'mlsimport_property_history', true );
569 if ( '' === trim( $meta ) ) { ?>
570 <strong>Property history is blank - you can enable it in Settings/ Tools page </strong>
571 <?php
572 } else {
573 print wp_kses_post($meta);
574 }
575 }
576
577
578
579
580 /**
581 * delete cache
582 */
583 function mlsimport_delete_cache() {
584
585 check_ajax_referer( 'mlsimport_tool_actions', 'security' );
586
587 delete_transient( 'mlsimport_token_request' );
588 delete_transient( 'mlsimport_metadata_api_call_data_service_property' );
589 delete_transient( 'mls_import_meta_enums' );
590 delete_transient( 'mls_import_meta' );
591 delete_transient( 'mlsimport_plugin_data_schema' );
592 delete_transient( 'mlsimport_ready_to_go_mlsimport_data' );
593 delete_transient( 'mlsimport_saas_token' );
594
595 delete_option( 'mlsimport_mls_metadata_populated' );
596 die( 'deleted' );
597 }
598
599 /**
600 *
601 *
602 *
603 *
604 * delete properties
605 */
606 function mlsimport_delete_properties() {
607 $error = false;
608 global $mlsimport;
609
610 check_ajax_referer( 'mlsimport_tool_actions', 'security' );
611
612
613 if ( current_user_can( 'administrator' ) ) :
614 $mlsimport_delete_category = sanitize_text_field( wp_unslash( $_POST['mlsimport_delete_category'] ) ) ;
615 $mlsimport_delete_category_term = sanitize_title( sanitize_text_field( wp_unslash( $_POST['mlsimport_delete_category_term']) ) );
616 $mlsimport_delete_timeout = intval( $_POST['mlsimport_delete_timeout'] );
617
618 if ( '' === $mlsimport_delete_category ) {
619 $error_message = esc_html__('Category cannot be blank','mlsimport');
620 $error = true;
621 }
622
623 if ( '' === $mlsimport_delete_category_term ) {
624 $error_message = esc_html__('Category Term cannot be blank','mlsimport');
625 $error = true;
626 }
627
628 $category = get_term_by( 'slug', $mlsimport_delete_category_term, $mlsimport_delete_category );
629
630 if ( $error ) {
631 print wp_json_encode(
632 array(
633 'message' => esc_html($error_message),
634 )
635 );
636 } else {
637 $mlsimport_delete_category_term_array = array();
638
639 $mlsimport_delete_category_term_array[] = $mlsimport_delete_category_term;
640 $tax_array = array(
641 'taxonomy' => $mlsimport_delete_category,
642 'field' => 'slug',
643 'terms' => $mlsimport_delete_category_term_array,
644 );
645
646 $args = array(
647 'post_type' => array( 'estate_property', 'property' ),
648 'post_status' => 'any',
649 'paged' => 1,
650 'posts_per_page' => -1,
651 'tax_query' => array(
652 $tax_array,
653 ),
654 'fields' => 'ids',
655 );
656
657 $prop_selection = new WP_Query( $args );
658
659 foreach ( $prop_selection->posts as $key => $delete_get_id ) {
660 if ( 0 !== $mlsimport_delete_timeout ) {
661 set_timeout( $mlsimport_delete_timeout );
662 }
663
664 $mlsimport->admin->theme_importer->mlsimport_saas_delete_property_via_mysql( $delete_get_id, ' delete from tools ' );
665 }
666
667 wp_update_term_count_now( array( $category->term_id ), $mlsimport_delete_category );
668
669 print wp_json_encode(
670 array(
671 '$category' => $category->term_id,
672 'arguments' => $args,
673 'posts' => $prop_selection->posts,
674 'found' => $prop_selection->found_posts,
675 'message' => 'Done...',
676 )
677 );
678 }
679 endif;
680 die();
681 }
682
683
684
685
686 /**
687 *
688 *
689 * Testing enviroment variables
690 */
691 public function mlsimport_saas_setting_up() {
692 if (intval(WP_MEMORY_LIMIT) < 256): ?>
693 <div class="mlsimport_warning long_warning">
694 <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>
695 </div>
696 <?php endif; ?>
697
698 <?php
699 $max_input_vars = ini_get('max_input_vars');
700 if ($max_input_vars < 2000):
701 ?>
702 <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>
703 <?php endif; ?>
704
705 <?php
706 $max_time = ini_get('max_execution_time');
707 if ($max_time < 600 && 0 !== $max_time):
708 ?>
709 <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>
710 <?php endif;
711
712 }
713
714 /**
715 * Check if token validates with MLS
716 *
717 * @since 4.0.1
718 * returns token fron mlsimport
719 */
720 public function mlsimport_saas_check_mls_connection() {
721
722 $values = array();
723 $options = get_option( $this->plugin_name . '_admin_options' );
724
725 $mls_id = '';
726 if ( isset( $options['mlsimport_mls_name'] ) ) {
727 $mls_id = sanitize_text_field( trim( $options['mlsimport_mls_name'] ) );
728 }
729
730 $mls_token = '';
731 if ( isset( $options['mlsimport_mls_name'] ) ) {
732 $mls_token = sanitize_text_field( trim( $options['mlsimport_mls_token'] ) );
733 }
734
735 $mlsimport_tresle_client_id = '';
736 if ( isset( $options['mlsimport_tresle_client_id'] ) ) {
737 $mlsimport_tresle_client_id = sanitize_text_field( trim( $options['mlsimport_tresle_client_id'] ) );
738 }
739
740 $mlsimport_tresle_client_secret = '';
741 if ( isset( $options['mlsimport_tresle_client_secret'] ) ) {
742 $mlsimport_tresle_client_secret = sanitize_text_field( trim( $options['mlsimport_tresle_client_secret'] ) );
743 }
744
745 // rapattoni data
746 $mlsimport_rapattoni_client_id = '';
747 if ( isset( $options['mlsimport_rapattoni_client_id'] ) ) {
748 $mlsimport_rapattoni_client_id = sanitize_text_field( trim( $options['mlsimport_rapattoni_client_id'] ) );
749 }
750 $mlsimport_rapattoni_client_secret = '';
751 if ( isset( $options['mlsimport_rapattoni_client_secret'] ) ) {
752 $mlsimport_rapattoni_client_secret = sanitize_text_field( trim( $options['mlsimport_rapattoni_client_secret'] ) );
753 }
754
755 $mlsimport_rapattoni_username = '';
756 if ( isset( $options['mlsimport_rapattoni_username'] ) ) {
757 $mlsimport_rapattoni_username = sanitize_text_field( trim( $options['mlsimport_rapattoni_username'] ) );
758 }
759
760 $mlsimport_rapattoni_password = '';
761 if ( isset( $options['mlsimport_rapattoni_password'] ) ) {
762 $mlsimport_rapattoni_password = sanitize_text_field( trim( $options['mlsimport_rapattoni_password'] ) );
763 }
764
765 // paragon data
766 $mlsimport_paragon_client_id = '';
767 if ( isset( $options['mlsimport_paragon_client_id'] ) ) {
768 $mlsimport_paragon_client_id = sanitize_text_field( trim( $options['mlsimport_paragon_client_id'] ) );
769 }
770 $mlsimport_paragon_client_secret = '';
771 if ( isset( $options['mlsimport_paragon_client_secret'] ) ) {
772 $mlsimport_paragon_client_secret = sanitize_text_field( trim( $options['mlsimport_paragon_client_secret'] ) );
773 }
774
775 if ( trim( $mls_token ) === '' ) {
776 if ( intval( $mls_id ) > 900 && intval( $mls_id ) < 3000 ) {
777 if ( trim( $mlsimport_tresle_client_id ) === '' || trim( $mlsimport_tresle_client_secret ) === '' ) {
778 return;
779 }
780 } elseif ( intval( $mls_id ) >= 5000 && intval( $mls_id ) < 6000 ) {
781 if (
782 trim( $mlsimport_rapattoni_client_id ) === '' ||
783 trim( $mlsimport_rapattoni_client_secret ) === '' ||
784 trim( $mlsimport_rapattoni_username ) === '' ||
785 trim( $mlsimport_rapattoni_password ) === ''
786 ) {
787 return;
788 }
789 } elseif ( intval( $mls_id ) >= 6000 ) {
790 if (
791 trim( $mlsimport_paragon_client_id ) === '' ||
792 trim( $mlsimport_paragon_client_secret ) === ''
793 ) {
794 return;
795 }
796 }
797 }
798
799 $values['mls_token'] = $mls_token;
800 $values['mls_id'] = $mls_id;
801 $values['mlsimport_tresle_client_id'] = $mlsimport_tresle_client_id;
802 $values['mlsimport_tresle_client_secret'] = $mlsimport_tresle_client_secret;
803
804 $values['mlsimport_rapattoni_client_id'] = $mlsimport_rapattoni_client_id;
805 $values['mlsimport_rapattoni_client_secret'] = $mlsimport_rapattoni_client_secret;
806 $values['mlsimport_rapattoni_username'] = $mlsimport_rapattoni_username;
807 $values['mlsimport_rapattoni_password'] = $mlsimport_rapattoni_password;
808
809 $values['mlsimport_paragon_client_id'] = $mlsimport_paragon_client_id;
810 $values['mlsimport_paragon_client_secret'] = $mlsimport_paragon_client_secret;
811
812 $answer = $this->theme_importer->global_api_request_saas( 'clients', $values, 'PATCH' );
813
814
815
816
817 if ( isset( $answer['succes'] ) && true === $answer['succes'] ) {
818 if ( isset( $answer['tested'] ) && true === $answer['tested'] ) {
819 update_option( 'mlsimport_connection_test', 'yes' );
820 } else {
821 delete_option( 'mlsimport_connection_test' );
822 delete_option( 'mlsimport_mls_metadata_populated' );
823 }
824 } else {
825 delete_option( 'mlsimport_connection_test' );
826 delete_option( 'mlsimport_mls_metadata_populated' );
827 }
828
829 return $answer;
830 }
831
832
833
834
835
836
837 /**
838 * Request auth token from mlsimport.net
839 *
840 * @since 4.0.1
841 * returns token fron mlsimport
842 */
843 public function mlsimport_saas_get_mls_api_token_from_transient() {
844
845 $token = get_transient( 'mlsimport_saas_token' );
846
847 if ( false === $token || '' === $token ) {
848 $token_json_answer = $this->mlsimport_saas_get_mls_api_token();
849
850
851 if ( isset( $token_json_answer['succes'] ) && true === $token_json_answer['succes'] ) {
852 $token = $token_json_answer['token'];
853
854 set_transient( 'mlsimport_saas_token', $token, 6 * 60 * 60 );
855 }
856 }
857
858 return $token;
859 }
860
861
862 /**
863 * call for token
864 *
865 * @since 4.0.1
866 * returns token fron mlsimport
867 */
868 protected function mlsimport_saas_get_mls_api_token() {
869 $values = array();
870 $options = get_option( $this->plugin_name . '_admin_options' );
871
872 $username = '';
873 if ( isset( $options['mlsimport_username'] ) ) {
874 $username = sanitize_text_field( trim( $options['mlsimport_username'] ) );
875 }
876
877 $password = '';
878 if ( isset( $options['mlsimport_password'] ) ) {
879 $password = sanitize_text_field( trim( $options['mlsimport_password'] ) );
880 }
881 $mls_name = '';
882 if ( isset( $options['mlsimport_mls_name'] ) ) {
883 $mls_name = sanitize_text_field( trim( $options['mlsimport_mls_name'] ) );
884 }
885
886 $mls_token = '';
887 if ( isset( $options['mlsimport_mls_token'] ) ) {
888 $mls_token = sanitize_text_field( trim( $options['mlsimport_mls_token'] ) );
889 }
890
891 $values['username'] = $username;
892 $values['password'] = $password;
893
894 if ( '' === $username || '' === $password ) {
895 return '';
896 }
897
898 $theme_Start = new ThemeImport();
899 $answer = $theme_Start::global_api_request_saas( 'token', $values, 'POST' );
900
901
902
903 return $answer;
904 }
905
906
907
908
909
910
911 /**
912 * save meta options
913 *
914 * @since 3.0.1
915 */
916 public function mlsimport_item_product_metaboxes() {
917 add_meta_box( 'mlsimport_item_metaboxes-sectionid', __( 'Set Import data', 'mlsimport' ), array( $this, 'mlsimport_saas_display_meta_options' ), 'mlsimport_item', 'normal', 'default' );
918 }
919
920
921
922 /**
923 *
924 *
925 *
926 * save meta options
927 *
928 * @since 3.0.1
929 */
930 public function mlsimport_item_product_save_metaboxes( $post_id, $post ) {
931
932 if ( ! is_object( $post ) || ! isset( $post->post_type ) ) {
933 return;
934 }
935
936 if ( 'mlsimport_item' !== $post->post_type ) {
937 return;
938 }
939
940 $allowed_keys = array(
941 'mlsimport_item_how_many',
942 'mlsimport_item_title_format',
943 'mlsimport_item_agent',
944 'mlsimport_item_property_status',
945 'mlsimport_item_property_user',
946 'mlsimport_item_min_price',
947 'mlsimport_item_max_price',
948 'mlsimport_item_city_check',
949 'mlsimport_item_city',
950 'mlsimport_item_city[]',
951 'mlsimport_item_countyorparish_check',
952 'mlsimport_item_countyorparish',
953 'mlsimport_item_mlsstatus_check',
954 'mlsimport_item_mlsstatus',
955 'mlsimport_item_propertysubtype_check',
956 'mlsimport_item_propertysubtype',
957 'mlsimport_item_propertytype_check',
958 'mlsimport_item_propertytype',
959 'mlsimport_item_standardstatus_check',
960 'mlsimport_item_standardstatus',
961 'mlsimport_item_internetentirelistingdisplayyn',
962 'mlsimport_item_internetaddressdisplayyn',
963 'mlsimport_item_stat_cron',
964 'mlsimport_item_listagentkey',
965 'mlsimport_item_listagentmlsid',
966 'mlsimport_item_listofficekey',
967 'mlsimport_item_postalcode',
968 'mlsimport_item_listofficemlsid',
969 'mlsimport_item_listingid',
970 'mlsimport_item_extracity',
971 'mlsimport_item_extracounty',
972 'mlsimport_item_exclude_listofficemlsid',
973 'mlsimport_item_exclude_listofficekey',
974 'mlsimport_item_exclude_listagentmlsid',
975 'mlsimport_item_exclude_listagentkey',
976 );
977
978
979
980
981 foreach ( $allowed_keys as $key => $key_value ) {
982 if( isset($_POST[$key_value]) ){
983 $postmeta = mlsimport_sanitize_multi_dimensional_array ( $_POST[$key_value] ) ;
984 update_post_meta( $post_id, sanitize_key( $key_value ), $postmeta );
985 }
986
987 }
988
989 $blank_keys = array(
990 'mlsimport_item_standardstatus',
991 'mlsimport_item_city',
992 'mlsimport_item_countyorparish',
993 'mlsimport_item_propertysubtype',
994 'mlsimport_item_propertytype',
995 'mlsimport_item_standardstatus',
996 'mlsimport_item_listingid',
997
998 );
999
1000 foreach ( $blank_keys as $key ) {
1001 if ( ! isset( $_POST[ $key ] ) ) {
1002 update_post_meta( $post_id, $key, '' );
1003 }
1004 }
1005
1006
1007 }
1008
1009
1010
1011
1012 /**
1013 *
1014 *
1015 * Display Meta options
1016 */
1017 public function mlsimport_saas_display_meta_options( $post ) {
1018 wp_nonce_field( plugin_basename( __FILE__ ), 'estate_agent_noncename' );
1019 global $post;
1020 global $mlsimport;
1021 $token = $mlsimport->admin->mlsimport_saas_get_mls_api_token_from_transient();
1022
1023 $post_id = $post->ID;
1024
1025 $found_items = 'none';
1026 $mlsimport_item_how_many = esc_html( get_post_meta( $post_id, 'mlsimport_item_how_many', true ) );
1027 $mlsimport_item_stat_cron = esc_html( get_post_meta( $post_id, 'mlsimport_item_stat_cron', true ) );
1028 $last_date = get_post_meta( $post_id, 'mlsimport_last_date', true );
1029 $status = get_option( 'mlsimport_force_stop_' . $post_id );
1030 $field_import = $this->mlsimport_saas_return_mls_fields();
1031
1032 // admin options
1033 $options = get_option( 'mlsimport_admin_options' );
1034 $mlsimport_mls_id = 0;
1035 if ( isset( $options['mlsimport_mls_name'] ) && '' !== $options['mlsimport_mls_name'] ) {
1036 $mlsimport_mls_id = intval( $options['mlsimport_mls_name'] );
1037 }
1038
1039 $mlsrequest = $this->mlsimport_make_listing_requests( $post_id );
1040
1041 if ( isset( $mlsrequest['success'] ) && ! $mlsrequest['success'] ) { ?>
1042
1043 <div class="mlsimport_warning">
1044 <?php print esc_html( $mlsrequest['message'] ) ;?>
1045 </div>
1046 <?php }
1047
1048 //print_r($mlsrequest);
1049
1050 if (isset($mlsrequest['results'])): ?>
1051 <?php $found_items = intval($mlsrequest['results']); ?>
1052 <?php else: ?>
1053 <?php
1054 // delete_transient('mlsimport_saas_token');
1055 $mlsimport->admin->mlsimport_saas_check_mls_connection();
1056 esc_html_e('Your Token was expired. Please refresh the page to renew it wait while we renew it.','mlsimport');
1057 ?>
1058 <?php endif; ?>
1059
1060 <div class="mlsimport_item_search_url" style="display:xnone;"> <?php echo esc_html__('Last date/time we check :','mlsimport').' '. esc_html($last_date); ?> </div>
1061
1062 <ul>
1063 <li>1. Set the import parameters.</li>
1064 <li>2. Hit Publish or Update, otherwise import will not work correctly.</li>
1065 <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>
1066 <li>4. Press the Update button after you make any change in the import settings.</li>
1067 </ul>
1068
1069
1070 <?php if (is_numeric($found_items) && $found_items >= 500): ?>
1071 <div class="mlsimport_notification">
1072 <?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'); ?>
1073 </div>
1074 <?php endif; ?>
1075
1076 <div class="mlsimport_import_no">
1077 <?php esc_html_e('We found','mlsimport');?>
1078 <strong><?php echo esc_html($found_items); ?></strong> listings. If you decide to import all of them make sure your server database can handle the load. Please do a database backup before initial import.</div>
1079
1080 <fieldset class="mlsimport-fieldset">
1081 <label class="mlsimport-label" for="mlsimport_item_how_many">
1082 <?php esc_html_e('How Many to import. Use 0 if you want to import all listings found.', 'mlsimport'); ?>
1083 </label>
1084 <input type="text" id="mlsimport_item_how_many" name="mlsimport_item_how_many" value="<?php echo esc_attr($mlsimport_item_how_many); ?>"/>
1085 </fieldset>
1086
1087 <fieldset class="mlsimport-fieldset mlsimport_auto_switch">
1088 <?php esc_html_e('Enable Auto Update every hour?', 'mlsimport'); ?>
1089 <label class="mlsimport_switch">
1090 <input type="hidden" value="0" name="mlsimport_item_stat_cron">
1091 <input type="checkbox" value="1" name="mlsimport_item_stat_cron"<?php if (intval($mlsimport_item_stat_cron) !== 0) echo esc_html(' checked'); ?>>
1092 <span class="slider round"></span>
1093 </label>
1094 </fieldset>
1095
1096
1097
1098
1099
1100
1101
1102 <?php
1103
1104 if ('' !== $mlsimport_item_stat_cron): ?>
1105 <div id="mlsimport_item_status"></div>
1106 <input class="button mlsimport_button" type="button" id="mlsimport-start_item"
1107 data-post-number="<?php echo intval($found_items); ?>"
1108 data-post_id="<?php echo intval($post_id); ?>" value="Start Import">
1109 <input class="button mlsimport_button" type="button" id="mlsimport_stop_item"
1110 data-post-number="<?php echo intval($found_items); ?>"
1111 data-post_id="<?php echo intval($post_id); ?>" value="Stop Import">
1112 <?php endif; ?>
1113
1114 <input type="hidden" id="mlsimport_item_actions" value="<?php echo esc_attr($ajax_nonce = wp_create_nonce("mlsimport_item_actions")); ?>" />
1115 <div class="mlsimport_param_wrapper"><h2>Import Parameters</h2>
1116
1117 <?php $mlsimport_item_title_format = esc_html(get_post_meta($post_id, 'mlsimport_item_title_format', true)); ?>
1118
1119 <fieldset class="mlsimport-fieldset">
1120 <label class="mlsimport-label" for="mlsimport_item_title_format">
1121 <?php esc_html_e('Title Format', 'mlsimport'); ?>
1122 </label>
1123
1124 <p class="mlsimport-exp">You can use {Address}, {City}, {CountyOrParish}, {StateOrProvince}, {PostalCode}, {PropertyType}, {Bedrooms}, {Bathrooms}, {ListingKey}, {ListingId},{StreetNumberNumeric} or {StreetName} </p>
1125 <input type="text" id="mlsimport_item_title_format" name="mlsimport_item_title_format" value="<?php echo '' !== $mlsimport_item_title_format ? trim(esc_html($mlsimport_item_title_format)) : esc_html('{Address},{City},{CountyOrParish},{PropertyType}'); ?>"/>
1126 </fieldset>
1127
1128 <?php $mlsimport_item_agent = esc_html(get_post_meta($post_id, 'mlsimport_item_agent', true)); ?>
1129
1130 <fieldset class="mlsimport-fieldset">
1131 <label class="mlsimport-label" for="mlsimport_item_agent">
1132 <?php esc_html_e('Select Agent', 'mlsimport'); ?>
1133 </label>
1134 <select class="mlsimport-select" name="mlsimport_item_agent" id="mlsimport_item_agent">
1135 <?php
1136 $permited_tags= mlsimport_allowed_html_tags_content();
1137 $select_agent= $this->theme_importer->mlsimport_saas_theme_import_select_agent($mlsimport_item_agent);
1138 print wp_kses($select_agent,$permited_tags);
1139 ?>
1140 </select>
1141 </fieldset>
1142
1143
1144 <?php
1145 $mlsimport_item_property_status = esc_html(get_post_meta($post_id, 'mlsimport_item_property_status', true));
1146 if ('' === $mlsimport_item_property_status) {
1147 $mlsimport_item_property_status = 'publish';
1148 }
1149 $status_array = array('publish', 'draft');
1150 ?>
1151 <fieldset class="mlsimport-fieldset">
1152 <label class="mlsimport-label" for="mlsimport_item_property_status">
1153 <?php esc_html_e('Select Property Status on import', 'mlsimport'); ?>
1154 </label>
1155 <select class="mlsimport-select" name="mlsimport_item_property_status" id="mlsimport_item_property_status">
1156 <?php foreach ($status_array as $value): ?>
1157 <option value="<?php echo esc_attr($value); ?>" <?php if ($value === $mlsimport_item_property_status) echo esc_html('selected'); ?>>
1158 <?php echo esc_html($value); ?>
1159 </option>
1160 <?php endforeach; ?>
1161 </select>
1162 </fieldset>
1163
1164 <?php
1165 $mlsimport_item_property_user = esc_html(get_post_meta($post_id, 'mlsimport_item_property_user', true));
1166 ?>
1167 <fieldset class="mlsimport-fieldset">
1168 <label class="mlsimport-label" for="mlsimport_item_property_user">
1169 <?php esc_html_e('User', 'mlsimport'); ?>
1170 </label>
1171 <select class="mlsimport-select" id="mlsimport_item_property_user" name="mlsimport_item_property_user">
1172 <?php
1173 $select_user = $this->theme_importer->mlsimport_saas_theme_import_select_user($mlsimport_item_property_user);
1174 print wp_kses($select_user,$permited_tags);
1175 ?>
1176 </select>
1177 </fieldset>
1178
1179
1180
1181 <?php
1182 $mlsimport_item_min_price = floatval(get_post_meta($post_id, 'mlsimport_item_min_price', true));
1183 $mlsimport_item_max_price = floatval(get_post_meta($post_id, 'mlsimport_item_max_price', true));
1184 if (0 === intval($mlsimport_item_max_price)) {
1185 $mlsimport_item_max_price = 10000000;
1186 }
1187 ?>
1188 <fieldset class="mlsimport-fieldset">
1189 <label class="mlsimport-label">
1190 <?php esc_html_e('Price Between', 'mlsimport'); ?>
1191 </label>
1192 <input type="text" class="mlsimport-select" id="mlsimport_item_min_price" name="mlsimport_item_min_price" value="<?php echo esc_attr($mlsimport_item_min_price); ?>"> and
1193 <input type="text" class="mlsimport-select" id="mlsimport_item_max_price" name="mlsimport_item_max_price" value="<?php echo esc_attr($mlsimport_item_max_price); ?>">
1194 </fieldset>
1195
1196
1197 <?php
1198 $options = get_option( $this->plugin_name . '_admin_options' );
1199
1200 $mls_id = '';
1201 if ( isset( $options['mlsimport_mls_name'] ) ) {
1202 $mls_id = sanitize_text_field( trim( $options['mlsimport_mls_name'] ) );
1203 }
1204
1205 if ( $mls_id > 5000 ) {
1206 $field_import['PropertyType']['multiple'] = 'no';
1207 }
1208
1209 foreach ($field_import as $key => $field):
1210 $name_check = strtolower('mlsimport_item_' . $key . '_check');
1211 $name = strtolower('mlsimport_item_' . $key);
1212
1213 $value = get_post_meta($post_id, $name, true);
1214 $value_check = get_post_meta($post_id, $name_check, true);
1215 $extra_class = '';
1216 if ('extraCity' === $key || 'extraCounty' === $key) {
1217 $extra_class = ' mlsimport_hidden_field_button';
1218 }
1219 ?>
1220 <fieldset class="mlsimport-fieldset">
1221 <label class="mlsimport-label <?php echo esc_attr($extra_class); ?>" 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($mlsimport_mls_id, $name, $field['description'])); ?>
1228 <?php
1229 $is_checkbox_admin = 0;
1230 if (1 === intval($value_check)) {
1231 $is_checkbox_admin = 1;
1232 }
1233
1234 $select_all_none = [
1235 'InternetAddressDisplayYN',
1236 'InternetEntireListingDisplayYN',
1237 'PostalCode',
1238 'ListAgentKey',
1239 'ListAgentMlsId',
1240 'ListOfficeKey',
1241 'ListOfficeMlsId',
1242 'StandardStatus',
1243 'ListingId',
1244 'extraCity',
1245 'extraCounty',
1246 'Exclude_ListOfficeKey',
1247 'Exclude_ListOfficeMlsId',
1248 'Exclude_ListAgentKey',
1249 'Exclude_ListAgentMlsId',
1250 ];
1251
1252 if ($mls_id > 5000) {
1253 $select_all_none[] = 'PropertyType';
1254 }
1255
1256 if (!in_array($key, $select_all_none)): ?>
1257 <?php
1258 esc_html_e('- Or Select All ', 'mlsimport');
1259 print esc_html( $is_checkbox_admin);
1260 ?>
1261 <input type="hidden" name="<?php echo esc_attr($name_check); ?>" value="0" />
1262 <input type="checkbox" name="<?php echo esc_attr($name_check); ?>" value="1" <?php print esc_attr( checked($is_checkbox_admin, 1, 0)); ?> />
1263
1264
1265 <?php endif; ?>
1266 </p>
1267
1268
1269
1270 <?php
1271 $permited_status=array('active','active under contract','coming soon','activeundercontract','comingsoon','pending');
1272
1273
1274
1275 if ($field['type'] === 'select'): ?>
1276 <?php
1277 $multiple = '';
1278 if ('yes' === $field['multiple']) {
1279 $multiple = 'multiple';
1280 $name .= '[]';
1281 }
1282
1283 if ('StandardStatus' === $key && '' === $value) {
1284 $value = ['Active'];
1285 }
1286 //edmonton ray
1287
1288 if( $key=="StandardStatus" && $mls_id==111){
1289 $value=='';
1290 $field['values'] =array();
1291
1292 }
1293
1294
1295 // Additional conditions can be placed here.
1296 ?>
1297 <select class="mlsimport-select" id="<?php echo esc_attr($name); ?>" name="<?php echo esc_attr($name); ?>" <?php echo esc_attr($multiple); ?>>
1298 <?php foreach ($field['values'] as $select_key): ?>
1299
1300 <?php if ('' !== $select_key): ?>
1301 <option value="<?php echo esc_attr($select_key); ?>"
1302 <?php
1303 if( $key=="StandardStatus" && !in_array(strtolower($select_key), $permited_status) ){
1304 print 'disabled';
1305 }
1306
1307 ?>
1308 <?php if (is_array($value) ? in_array($select_key, $value) : $select_key === $value) echo 'selected'; ?>>
1309
1310 <?php echo esc_html($select_key); ?>
1311 </option>
1312 <?php endif; ?>
1313
1314 <?php endforeach; ?>
1315 </select>
1316 <?php elseif ($field['type'] === 'input'): ?>
1317 <input type="text" class="mlsimport-select" id="<?php echo esc_attr($name); ?>" name="<?php echo esc_attr($name); ?>" value="<?php echo esc_attr($value); ?>">
1318 <?php endif; ?>
1319 <?php if ('extraCity' === $key || 'extraCounty' === $key): ?>
1320 </div>
1321 <?php endif; ?>
1322 </fieldset>
1323 <?php endforeach; ?>
1324
1325
1326 </div>
1327
1328 <?php
1329 }
1330
1331
1332
1333
1334 public function mlsimport_add_extra_fields() {
1335 }
1336
1337 /**
1338 *
1339 *
1340 *
1341 *
1342 *
1343 *
1344 */
1345 function mlsimport_notes_for_mls( $mlsimport_mls_id, $name, $description ) {
1346 // 111 - Rae Edmonton
1347
1348 if ( 111 === intval($mlsimport_mls_id) && 'mlsimport_item_standardstatus' === $name ) {
1349 return esc_html__( 'Your MLS does not use this field - all listings are considered Active.', 'mlsimport' );
1350 } else {
1351 return $description;
1352 }
1353 }
1354
1355
1356 /**
1357 *
1358 *
1359 * Get Last date
1360 */
1361 public function mlsimport_saas_get_last_date( $item_id ) {
1362 $last_date = get_post_meta( $item_id, 'mlsimport_last_date', true );
1363
1364 if ( '' === $last_date ) {
1365 $last_date = $this->mlsimport_saas_update_last_date( $item_id );
1366 }
1367
1368 return $last_date;
1369 }
1370
1371
1372 /**
1373 *
1374 *
1375 * Save Last date
1376 */
1377 public function mlsimport_saas_update_last_date( $item_id ) {
1378
1379 $unix_time = current_time( 'timestamp', 0 ) - ( 2 * 60 * 60 );
1380 $last_date_to_save = date( 'Y-m-d\TH:i', $unix_time );
1381 update_post_meta( $item_id, 'mlsimport_last_date', $last_date_to_save );
1382
1383 return $last_date_to_save;
1384 }
1385
1386
1387
1388 /**
1389 *
1390 *
1391 * Check if there are modified items in the last 2h
1392 */
1393 public function mlsimport_saas_start_cron_links_per_item( $item_id ) {
1394
1395 $last_date = $this->mlsimport_saas_get_last_date( $item_id );
1396
1397 esc_html_e('we work with','mlsimport').' '. esc_html($last_date ). PHP_EOL;
1398
1399 $mlsrequest = $this->mlsimport_make_listing_requests( $item_id, $last_date );
1400 $found_items = 0;
1401 if ( isset( $mlsrequest['results'] ) ) {
1402 $found_items = intval( $mlsrequest['results'] );
1403 } else {
1404 delete_transient( 'mlsimport_saas_token' );
1405 }
1406
1407 print esc_html__('we found ','mlsimport') .esc_html( $found_items ). '</br>' . PHP_EOL;
1408
1409 $attachments_to_move = array();
1410
1411 if ( $found_items > 0 ) {
1412 $item_id_array = array(
1413 'item_id' => $item_id,
1414 'how_many' => 0,
1415 'max_number' => $found_items,
1416 'batch_counter' => 1,
1417 );
1418
1419 $attachments_to_move = (array) $this->mlsimport_saas_generate_import_requests_per_item( $item_id_array, $last_date );
1420
1421 update_post_meta( $item_id, 'mlsimport_spawn_status_cron_job', 'started' );
1422 update_post_meta( $item_id, 'mlsimport_cron_attach_to_move_' . $item_id, $attachments_to_move );
1423
1424 // save last date for next run
1425 $this->mlsimport_saas_update_last_date( $item_id );
1426
1427 $attachments_to_send = array(
1428 'args' => array(
1429 'attachments_to_move' => $item_id,
1430 'item_id_array' => $item_id_array,
1431 ),
1432 );
1433
1434 $this->mlsimport_background_process_per_item_cron_function( $attachments_to_send['args'] );
1435
1436 }
1437 }
1438
1439
1440
1441
1442 /**
1443 *
1444 *
1445 * Reconciliation log
1446 */
1447 public function mlsimport_saas_start_doing_reconciliation() {
1448 global $mlsimport;
1449 $listingKey_in_Local = $this->mlsimport_saas_get_all_meta_values( 'ListingKey' );
1450
1451 if ( empty( $listingKey_in_Local ) ) {
1452 return;
1453 }
1454
1455
1456 $mls_data = $this->mlsimport_saas_get_mls_reconciliation_data();
1457 $listingKey_in_MLS = $mls_data['all_data'];
1458
1459 if ( empty( $listingKey_in_MLS ) ) {
1460 return;
1461 }
1462
1463 $to_delete = 0;
1464 $counter = 0;
1465 foreach ( $listingKey_in_Local as $key => $item ) {
1466 $listingkey = $item->meta_value;
1467 $property_id = $item->iD;
1468 ++$counter;
1469
1470 if ( in_array( $listingkey, $listingKey_in_MLS ) ) {
1471 print wp_kses_post($listingkey . ' IS FOUND </br>');
1472 } else {
1473 ++$to_delete;
1474 print wp_kses_post('</br>' .$listingkey. ' ------------------------- NOT FOUND delete: '.$property_id.' <-');
1475 $mlsimport->admin->theme_importer->mlsimport_saas_delete_property_via_mysql( $property_id, $listingkey );
1476 }
1477 }
1478
1479 print esc_html(' to delete:' .$to_delete);
1480 return;
1481 }
1482
1483
1484 /**
1485 *
1486 *
1487 * Requestq Reconciliation log
1488 */
1489 public function mlsimport_saas_get_mls_reconciliation_data() {
1490
1491 $arguments = array();
1492 $answer = $this->theme_importer->global_api_request_CURL_saas( 'reconciliation', $arguments, 'GET' );
1493 return $answer;
1494 }
1495
1496 /**
1497 *
1498 *
1499 * Reconciliation get local data
1500 */
1501 public function mlsimport_saas_get_all_meta_values( $key ) {
1502 global $wpdb;
1503
1504 $result = $wpdb->get_results(
1505 $wpdb->prepare(
1506 "
1507 SELECT DISTINCT pm.meta_value,p.iD FROM {$wpdb->postmeta} pm
1508 LEFT JOIN {$wpdb->posts} p ON p.ID = pm.post_id
1509 WHERE pm.meta_key = %s
1510 AND p.post_status = 'publish'",
1511 $key
1512 )
1513 );
1514
1515 return $result;
1516 }
1517
1518
1519
1520
1521
1522 /*
1523 * Do api Listing Requests
1524 *
1525 *
1526 *
1527 *
1528 * */
1529 public function mlsimport_make_listing_requests( $item_id, $last_date = '', $skip = '', $top = '' ) {
1530 $options = get_option( $this->plugin_name . '_admin_options' );
1531 $mls_id = '';
1532 if ( isset( $options['mlsimport_mls_name'] ) ) {
1533 $mls_id = sanitize_text_field( trim( $options['mlsimport_mls_name'] ) );
1534 }
1535
1536 $arguments = $this->mlsimport_saas_make_listing_requests_arguments( $item_id, $last_date, $skip, $top );
1537
1538
1539 if (
1540 $mls_id > 5000 && $mls_id < 6000 &&
1541 ( ! isset( $arguments['property_type'] ) or
1542 ( isset( $arguments['property_type'] ) && '' === $arguments['property_type'] ) or
1543 ( isset( $arguments['property_type'][0] ) && '' === $arguments['property_type'][0] )
1544 )
1545 ) {
1546 return array(
1547 'success' => false,
1548 'type' => 'rapattoni',
1549 'message' => esc_html__( 'This MLS requires to have one item selected from "Property Action Category" dropdown', 'mlsimport' ),
1550 );
1551 }
1552
1553 $potential_leght = strlen( wp_json_encode( $arguments ) );
1554 if ( $potential_leght > 1750 ) {
1555 return array(
1556 'success' => false,
1557 'potential_leght' => $potential_leght,
1558 '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' ),
1559 );
1560 }
1561
1562 $answer = $this->theme_importer->global_api_request_CURL_saas( 'listings', $arguments, 'POST' );
1563 $answer['potential_leght'] = $potential_leght;
1564 return ( $answer );
1565 }
1566
1567
1568
1569
1570
1571
1572 /*
1573 * Create Api query arguments
1574 *
1575 *
1576 *
1577 *
1578 *
1579 * */
1580
1581 public function mlsimport_saas_make_listing_requests_arguments( $item_id, $last_date = '', $skip = '', $top = '' ) {
1582
1583 $options = get_option( $this->plugin_name . '_admin_options' );
1584 if ( isset( $options['mlsimport_mls_name'] ) ) {
1585 $mls_id = intval( $options['mlsimport_mls_name'] );
1586 } else {
1587 return '';
1588 }
1589
1590 if ( isset( $options['mlsimport_theme_used'] ) ) {
1591 $theme_id = intval( $options['mlsimport_theme_used'] );
1592 } else {
1593 return '';
1594 }
1595
1596 $values = array();
1597 $values['mls_id'] = $mls_id;
1598 $values['theme_id'] = $theme_id;
1599
1600 if ( '' !== $top ) {
1601 $values['top'] = $top;
1602 $values['skip'] = intval( $skip );
1603 }
1604
1605 // // add price
1606 $mlsimport_item_min_price = get_post_meta( $item_id, 'mlsimport_item_min_price', true );
1607 $mlsimport_item_max_price = get_post_meta( $item_id, 'mlsimport_item_max_price', true );
1608 if ( '' !== $mlsimport_item_min_price && '' !== $mlsimport_item_max_price ) {
1609 $values['list_price_min'] = floatval( $mlsimport_item_min_price );
1610 $values['list_price_max'] = floatval( $mlsimport_item_max_price );
1611 }
1612
1613 // add city
1614 $values = $this->mls_import_return_multiple_param_value( 'city', $item_id, 'city', $values );
1615
1616 // add county
1617 $values = $this->mls_import_return_multiple_param_value( 'countyorparish', $item_id, 'county_or_parish', $values );
1618
1619 // add postal code
1620 $values = $this->mls_import_saas_add_to_parms_input( 'PostalCode', $item_id, 'postal_code', $values );
1621
1622 // add status
1623
1624 if ( 111 !== $mls_id ) { // edmonton check
1625 $values = $this->mls_import_return_multiple_param_value( 'StandardStatus', $item_id, 'status', $values );
1626 }
1627
1628 // add property_subtype
1629 $values = $this->mls_import_return_multiple_param_value( 'PropertySubType', $item_id, 'property_subtype', $values );
1630
1631 // add property_type
1632 $values = $this->mls_import_return_multiple_param_value( 'PropertyType', $item_id, 'property_type', $values );
1633
1634 // rapattoni exception
1635 if ( $mls_id > 5000 ) {
1636 $values = $this->mls_import_saas_add_to_parms_input( 'PropertyType', $item_id, 'property_type', $values );
1637 $temp = $values['property_type'];
1638 $temp = str_replace( ' ', '', $temp );
1639 $values['property_type'] = array();
1640 $values['property_type'][] = $temp;
1641 }
1642
1643 // add internet_entirelisting_displayyn
1644 $values = $this->mls_import_saas_add_to_parms_input( 'InternetEntireListingDisplayYN', $item_id, 'internet_entirelisting_displayyn', $values );
1645
1646 // add internet_address_displayyn
1647 $values = $this->mls_import_saas_add_to_parms_input( 'InternetAddressDisplayYN', $item_id, 'internet_address_displayyn', $values );
1648
1649 // add ListAgentKey
1650 $values = $this->mls_import_saas_add_to_parms_input( 'ListAgentKey', $item_id, 'list_agentkey', $values );
1651 // add ListAgentKey
1652 $values = $this->mls_import_saas_add_to_parms_input( 'ListAgentMlsId', $item_id, 'list_agentmlsid', $values );
1653 // add ListOfficeKey
1654 $values = $this->mls_import_saas_add_to_parms_input( 'ListOfficeKey', $item_id, 'list_officekey', $values );
1655 // add ListOfficeMlsId
1656 $values = $this->mls_import_saas_add_to_parms_input( 'ListOfficeMlsId', $item_id, 'list_officemlsid', $values );
1657
1658 // add ListingId
1659 $values = $this->mls_import_saas_add_to_parms_input( 'ListingId', $item_id, 'listingid', $values );
1660
1661 //add Exclude_ListOfficeKey
1662 $values = $this->mls_import_saas_add_to_parms_input( 'Exclude_ListOfficeKey', $item_id, 'exclude_list_officekey', $values );
1663 // add Exclude_ListOfficeMlsId
1664 $values = $this->mls_import_saas_add_to_parms_input( 'Exclude_ListOfficeMlsId', $item_id, 'exclude_list_officemlsid', $values );
1665
1666
1667
1668 //add Exclude_ListAgentKey
1669 $values = $this->mls_import_saas_add_to_parms_input( 'Exclude_ListAgentKey', $item_id, 'exclude_list_agentkey', $values );
1670 // add Exclude_ListAgentMlsId
1671 $values = $this->mls_import_saas_add_to_parms_input( 'Exclude_ListAgentMlsId', $item_id, 'exclude_list_agentmlsid', $values );
1672
1673
1674
1675 if ( '' !== $last_date ) {
1676 $values['modification_time'] = $last_date;
1677 }
1678
1679 return( $values );
1680 }
1681
1682
1683
1684 /*
1685 *
1686 * add input items to parameters array
1687 *
1688 */
1689
1690 public function mls_import_saas_add_to_parms_input( $key, $post_id, $new_name, $all_values ) {
1691 $name = strtolower( 'mlsimport_item_' . $key );
1692 $value = get_post_meta( $post_id, $name, true );
1693 if ( '' !== $value ) {
1694 $all_values[ $new_name ] = $value;
1695 }
1696
1697 return $all_values;
1698 }
1699
1700
1701 /*
1702 *
1703 * add list items to parameters array
1704 * $values= $this->mls_import_return_multiple_param_value('StandardStatus',$item_id,'status',$values);
1705
1706 */
1707
1708 public function mls_import_return_multiple_param_value( $key, $post_id, $new_name, $all_values ) {
1709 $name_check = strtolower( 'mlsimport_item_' . $key . '_check' );
1710 $name = strtolower( 'mlsimport_item_' . $key );
1711
1712 $value = get_post_meta( $post_id, $name, true );
1713
1714 // add extra county - should be moved into function if pass tests
1715 if ( 'countyorparish' === $key ) {
1716 $extracounty_values = get_post_meta( $post_id, 'mlsimport_item_extracounty', true );
1717
1718 if ( '' !== $extracounty_values ) {
1719 $extracounty_array = explode( ',', $extracounty_values );
1720
1721 if ( ! is_array( $value ) ) {
1722 if ( '' === $value ) {
1723 $value = array();
1724 } else {
1725 $value = array( $value );
1726 }
1727 }
1728
1729 foreach ( $extracounty_array as $extra ) {
1730 $value[] = $extra;
1731 }
1732 }
1733 }
1734
1735 // add extra city - should be moved into function if pass tests
1736 if ( 'city' === $key ) {
1737 $extracity_values = get_post_meta( $post_id, 'mlsimport_item_extracity', true );
1738 if ( '' !== $extracity_values ) {
1739 $extracity_array = explode( ',', $extracity_values );
1740
1741 if ( ! is_array( $value ) ) {
1742 if ('' === $value ) {
1743 $value = array();
1744 } else {
1745 $value = array( $value );
1746 }
1747 }
1748
1749 foreach ( $extracity_array as $extra ) {
1750 $value[] = $extra;
1751 }
1752 }
1753 }
1754
1755 $value_check = get_post_meta( $post_id, $name_check, true );
1756
1757 if ( 0 === intval($value_check) && '' !== $value ) {
1758 $all_values[ $new_name ] = $value;
1759 }
1760
1761 // status exception
1762 if ( 'status' === $new_name ) {
1763 $all_values[ $new_name ] = $value;
1764 }
1765
1766 return $all_values;
1767 }
1768
1769
1770
1771 /*
1772 *
1773 * All Enums fiels to be used on MLS import Item
1774 *
1775 *
1776 *
1777 *
1778 *
1779 *
1780 * */
1781
1782 public function mlsimport_saas_return_mls_fields() {
1783
1784 $mlsimport_mls_metadata_mls_enums = get_option( 'mlsimport_mls_metadata_mls_enums', '' );
1785
1786 if ( '' === $mlsimport_mls_metadata_mls_enums ) {
1787 ?>
1788 <div class="mlsimport_warning long_warning">Please select the import fields(from MLS Import Settings) before starting a MLS import process.</div>
1789 <?php
1790 }
1791
1792 $metadata_api_call_full = json_decode( $mlsimport_mls_metadata_mls_enums, true );
1793
1794 if ( isset( $metadata_api_call_full['global_array'] ) ) {
1795 $metadata_api_call = $metadata_api_call_full['global_array'];
1796 }
1797
1798 $city_array = array();
1799 if ( isset( $metadata_api_call['PropertyEnums']['City'] ) && is_array( $metadata_api_call['PropertyEnums']['City'] ) ) {
1800 $city_array = array_keys( $metadata_api_call['PropertyEnums']['City'] );
1801 }
1802
1803 $county_array = array();
1804 if ( isset( $metadata_api_call['PropertyEnums']['CountyOrParish'] ) && is_array( $metadata_api_call['PropertyEnums']['CountyOrParish'] ) ) {
1805 $county_array = array_keys( $metadata_api_call['PropertyEnums']['CountyOrParish'] );
1806 }
1807
1808 $mlsstatus_array = array();
1809 if ( isset( $metadata_api_call['PropertyEnums']['MlsStatus'] ) && is_array( $metadata_api_call['PropertyEnums']['MlsStatus'] ) ) {
1810 $mlsstatus_array = array_keys( $metadata_api_call['PropertyEnums']['MlsStatus'] );
1811 }
1812
1813 $propertysubtype_array = array();
1814 if ( isset( $metadata_api_call['PropertyEnums']['PropertySubType'] ) && is_array( $metadata_api_call['PropertyEnums']['PropertySubType'] ) ) {
1815 $propertysubtype_array = array_keys( $metadata_api_call['PropertyEnums']['PropertySubType'] );
1816 }
1817
1818 $propertytype_array = array();
1819 if ( isset( $metadata_api_call['PropertyEnums']['PropertyType'] ) && is_array( $metadata_api_call['PropertyEnums']['PropertyType'] ) ) {
1820 $propertytype_array = array_keys( $metadata_api_call['PropertyEnums']['PropertyType'] );
1821 }
1822
1823 $standardstatus_array = array();
1824 if ( isset( $metadata_api_call['PropertyEnums']['StandardStatus'] ) && is_array( $metadata_api_call['PropertyEnums']['StandardStatus'] ) ) {
1825 $standardstatus_array = array_keys( $metadata_api_call['PropertyEnums']['StandardStatus'] );
1826 }
1827
1828 // if we do not have standart status
1829 if ( empty( $standardstatus_array ) ) {
1830 $standardstatus_array = $mlsstatus_array;
1831 }
1832
1833 $extracounty_values = '';
1834 $extracity_values = '';
1835
1836 $field_import = array(
1837 'City' => array(
1838 'label' => esc_html__( 'Select cities', 'mlsimport' ),
1839 'description' => esc_html__( 'Select the cities from where we will import data.', 'mlsimport' ),
1840 'type' => 'select',
1841 'multiple' => 'yes',
1842 'values' => $city_array,
1843 ),
1844
1845 'extraCity' => array(
1846 'label' => esc_html__( 'Add extra Cities', 'mlsimport' ),
1847 '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' ),
1848 'type' => 'input',
1849 'multiple' => 'no',
1850 'values' => $extracity_values,
1851 ),
1852
1853 'CountyOrParish' => array(
1854 'label' => esc_html__( 'Select Counties', 'mlsimport' ),
1855 'description' => esc_html__( 'Select the counties from where we will import data.', 'mlsimport' ),
1856 'type' => 'select',
1857 'multiple' => 'yes',
1858 'values' => $county_array,
1859 'show_extra_field' => true,
1860 ),
1861
1862 'extraCounty' => array(
1863 'label' => esc_html__( 'Add extra Counties', 'mlsimport' ),
1864 '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' ),
1865 'type' => 'input',
1866 'multiple' => 'no',
1867 'values' => $extracounty_values,
1868 ),
1869
1870 'PostalCode' => array(
1871 'label' => esc_html__( 'Select Postal Code', 'mlsimport' ),
1872 'description' => esc_html__( 'Select the PostalCode from where to import listings. Works with only one PostalCode.', 'mlsimport' ),
1873 'type' => 'input',
1874 'multiple' => 'no',
1875 ),
1876
1877 'PropertySubType' => array(
1878 'label' => esc_html__( 'Select Property Category', 'mlsimport' ),
1879 'description' => esc_html__( 'Property Category', 'mlsimport' ),
1880 'type' => 'select',
1881 'multiple' => 'yes',
1882 'values' => $propertysubtype_array,
1883 ),
1884 'PropertyType' => array(
1885 'label' => esc_html__( 'Select Property Action Category', 'mlsimport' ),
1886 'description' => esc_html__( 'Property Action Category', 'mlsimport' ),
1887 'type' => 'select',
1888 'multiple' => 'yes',
1889 'values' => $propertytype_array,
1890 ),
1891 'StandardStatus' => array(
1892 'label' => esc_html__( 'Select Status', 'mlsimport' ),
1893 'description' => __( 'The list is auto-populated with MLS available statuses BUT you can import only these statuses: <strong>Active, Active Under Contract or Coming Soon</strong>.', 'mlsimport' ),
1894 'type' => 'select',
1895 'multiple' => 'yes',
1896 'values' => $standardstatus_array,
1897 ),
1898 'InternetEntireListingDisplayYN' => array(
1899 'label' => esc_html__( 'Internet Entire Listing Display ', 'mlsimport'),
1900 'description' => esc_html__( 'A yes/no field that states the seller has allowed the listing to be displayed on Internet sites.', 'mlsimport' ),
1901 'type' => 'select',
1902 'multiple' => 'no',
1903 'values' => array(
1904 'yes',
1905 'no',
1906 ),
1907 ),
1908 'InternetAddressDisplayYN' => array(
1909 'label' => esc_html__( 'Internet Address display', 'mlsimport' ),
1910 'description' => esc_html__( 'A yes/no field that states the seller has allowed the listing address to be displayed on Internet sites.', 'mlsimport' ),
1911 'type' => 'select',
1912 'multiple' => 'no',
1913 'values' => array(
1914 'yes',
1915 'no',
1916 ),
1917 ),
1918 'ListAgentKey' => array(
1919 'label' => esc_html__( 'ListAgentKey', 'mlsimport' ),
1920 'description' => esc_html__( 'Import listings from a specific Agent (contact your MLS for this information)', 'mlsimport' ),
1921 'type' => 'input',
1922 'multiple' => 'no',
1923 ),
1924 'ListAgentMlsId' => array(
1925 'label' => esc_html__( 'ListAgentMlsId', 'mlsimport' ),
1926 'description' => esc_html__( 'Import listings from a specific Agent (contact your MLS for this information)', 'mlsimport' ),
1927 'type' => 'input',
1928 'multiple' => 'no',
1929 ),
1930 'ListOfficeKey' => array(
1931 'label' => esc_html__( 'ListOfficeKey', 'mlsimport' ),
1932 'description' => esc_html__( 'Import listings from a specific Office (contact your MLS for this information)', 'mlsimport'),
1933 'type' => 'input',
1934 'multiple' => 'no',
1935 ),
1936 'ListOfficeMlsId' => array(
1937 'label' => esc_html__( 'ListOfficeMlsId', 'mlsimport' ),
1938 'description' => esc_html__( 'Import listings from a specific Office (contact your MLS for this information)', 'mlsimport' ),
1939 'type' => 'input',
1940 'multiple' => 'no',
1941 ),
1942 'ListingId' => array(
1943 'label' => esc_html__( 'ListingId', 'mlsimport' ),
1944 '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'),
1945 'type' => 'input',
1946 'multiple' => 'no',
1947 ),
1948 'Exclude_ListOfficeMlsId' => array(
1949 'label' => esc_html__( 'Exclude listings with ListOfficeMlsId', 'mlsimport' ),
1950 'description' => esc_html__( 'Exclude listings that belong to one or more ListOfficeMlsId.', 'mlsimport' ),
1951 'type' => 'input',
1952 'multiple' => 'no',
1953 ),
1954 'Exclude_ListOfficeKey' => array(
1955 'label' => esc_html__( 'Exclude listings with ListOfficeKey', 'mlsimport' ),
1956 'description' => esc_html__( 'Exclude listings that belong to one or more ListOfficeKey', 'mlsimport'),
1957 'type' => 'input',
1958 'multiple' => 'no',
1959 ),
1960
1961
1962 'Exclude_ListAgentMlsId' => array(
1963 'label' => esc_html__( 'Exclude listings with ListAgentMlsId', 'mlsimport' ),
1964 'description' => esc_html__( 'Exclude listings that belong to one or more ListAgentMlsId.', 'mlsimport' ),
1965 'type' => 'input',
1966 'multiple' => 'no',
1967 ),
1968 'Exclude_ListAgentKey' => array(
1969 'label' => esc_html__( 'Exclude listings with ListAgentKey ', 'mlsimport' ),
1970 'description' => esc_html__( 'Exclude listings that belong to one or more ListAgentKey ', 'mlsimport'),
1971 'type' => 'input',
1972 'multiple' => 'no',
1973 ),
1974
1975
1976 );
1977 return $field_import;
1978 }
1979
1980
1981
1982
1983
1984
1985
1986 /**
1987 *
1988 *
1989 * AYsnc Test
1990 */
1991 public function mlsimport_move_files_per_item() {
1992 check_ajax_referer( 'mlsimport_item_actions', 'security' );
1993 $post_id = 0;
1994 $how_many = 0;
1995 $max_number = 0;
1996 if(isset( $_POST['post_id'] )){
1997 $post_id = intval( $_POST['post_id'] );
1998 }
1999 if(isset( $_POST['how_many'] )){
2000 $how_many = intval( $_POST['how_many'] );
2001 }
2002 if(isset( $_POST['post_number'] )){
2003 $max_number = intval( $_POST['post_number'] );
2004 }
2005
2006 update_option( 'mlsimport_force_stop_' . $post_id, 'no', false );
2007
2008 $item_id_array = array(
2009 'item_id' => $post_id,
2010 'how_many' => $how_many,
2011 'max_number' => $max_number,
2012 'batch_counter' => 1,
2013 );
2014
2015 update_post_meta( $post_id, 'mlsimport_attach_to_move_' . $post_id, '' );
2016 $attachments_to_move = (array) $this->mlsimport_saas_generate_import_requests_per_item( $item_id_array );
2017
2018 update_post_meta( $post_id, 'mlsimport_attach_to_move_' . $post_id, $attachments_to_move );
2019
2020 $attachments_to_send = array(
2021 'args' => array(
2022 'attachments_to_move' => $post_id,
2023 'item_id_array' => $item_id_array,
2024 ),
2025 );
2026
2027 mlsimport_saas_single_write_import_custom_logs( 'Preparing the import. Please hold on.' . PHP_EOL );
2028 mlsimport_debuglogs_per_plugin( 'Preparing the import. Please hold on.' . PHP_EOL );
2029
2030 update_post_meta( $post_id, 'mlsimport_spawn_status', 'started' );
2031 as_enqueue_async_action( 'mlsimport_background_process_per_item', $attachments_to_send );
2032 spawn_cron();
2033
2034 unset( $attachments_to_send );
2035 die();
2036 }
2037
2038 /**
2039 *
2040 *
2041 * Processing Cron
2042 */
2043 public function mlsimport_background_process_per_item_cron_function( $input_arg ) {
2044 $log = 'In cron processing function ->' . wp_json_encode( $input_arg['item_id_array'] ) . PHP_EOL;
2045 mlsimport_saas_single_write_import_custom_logs( $log, 'cron' );
2046 global $mlsimport;
2047
2048 // Get from MLS Import it the big argument arrat
2049 $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 );
2050
2051 $log = 'In processing function mlsimport_cron_attach_to_move_ ->' . wp_json_encode( $attachments_to_move ) . PHP_EOL;
2052 mlsimport_saas_single_write_import_custom_logs( $log );
2053
2054
2055 // foreach($input_arg['attachments_to_move'] as $key=>$import_link){
2056 foreach ( $attachments_to_move as $key => $import_arguments ) {
2057 $GLOBALS['wp_object_cache']->delete( 'mlsimport_force_stop_' . $input_arg['item_id_array']['item_id'], 'options' );
2058
2059 $log = PHP_EOL . ' Cron Parsing importing batch : ' . $key . ' = ' . wp_json_encode( $import_arguments ) . PHP_EOL;
2060 mlsimport_saas_single_write_import_custom_logs( $log, 'cron' );
2061
2062 $api_call_array = $this->theme_importer->global_api_request_CURL_saas( 'listings', $import_arguments, 'POST' );
2063
2064 $mlsimport->admin->theme_importer->mlsimport_saas_cron_parse_search_array_per_item( $api_call_array, $input_arg['item_id_array'], $key );
2065 }
2066
2067 mlsimport_saas_single_write_import_custom_logs( 'CRON JOB Import Completed ' . PHP_EOL );
2068 mlsimport_debuglogs_per_plugin( 'CRON JOB Import Completed ' . PHP_EOL );
2069 update_post_meta( $input_arg['item_id_array']['item_id'], 'mlsimport_spawn_status', 'completed' );
2070 }
2071
2072 /**
2073 *
2074 *
2075 * Generate import Requests per item
2076 */
2077 public function mlsimport_saas_generate_import_requests_per_item( $item_id_array, $last_date = '' ) {
2078 $import_step = 25;
2079
2080 $prop_id = $item_id_array['item_id'];
2081 $max_found = $item_id_array['max_number'];
2082 $how_many = $item_id_array['how_many'];
2083 if ( 0=== intval($how_many) ) {
2084 $how_many = $max_found;
2085 }
2086 if ( $how_many > $max_found ) {
2087 $how_many = $max_found;
2088 }
2089
2090 $search_url_step = '';
2091 $urls_array = array();
2092
2093 $skip = 0;
2094 if ( $how_many > 10000 ) {
2095 $how_many = 10000;
2096 }
2097
2098 if ( $how_many < $import_step ) {
2099 $import_step = $how_many;
2100 }
2101
2102 while ( $skip < $how_many ) {
2103 $search_url_step = $this->mlsimport_saas_make_listing_requests_arguments( $prop_id, $last_date, $skip, $import_step );
2104 $skip = $skip + $import_step;
2105 $urls_array[] = $search_url_step;
2106 }
2107 return $urls_array;
2108 }
2109
2110
2111
2112
2113
2114 /**
2115 * Process Async function
2116 */
2117 public function mlsimport_background_process_per_item_function( $input_arg ) {
2118
2119 $mlsimportItemId = $input_arg['item_id_array']['item_id'];
2120 $log_prefix = 'In processing function - Item ID: ' . $mlsimportItemId . ' -> ';
2121 mlsimport_saas_single_write_import_custom_logs( $log_prefix . wp_json_encode( $input_arg['item_id_array'] ) . PHP_EOL );
2122
2123 ini_set('memory_limit', '256M');
2124 ini_set('max_execution_time', 0); // Unlimited execution time
2125
2126 // Get from MLS Import the big argument array only once
2127 $attachments_to_move = get_post_meta( $mlsimportItemId, 'mlsimport_attach_to_move_' . $mlsimportItemId, true );
2128
2129 // Retrieve all meta data in one go to reduce database queries
2130 $mlsimport_item_option_data = array(
2131 'mlsimport_item_standardstatus' => get_post_meta( $mlsimportItemId, 'mlsimport_item_standardstatus', true ),
2132 'mlsimport_item_property_user' => get_post_meta( $mlsimportItemId, 'mlsimport_item_property_user', true ),
2133 'mlsimport_item_agent' => get_post_meta( $mlsimportItemId, 'mlsimport_item_agent', true ),
2134 'mlsimport_item_property_status' => get_post_meta( $mlsimportItemId, 'mlsimport_item_property_status', true ),
2135 );
2136
2137 $total_batches = count( $attachments_to_move );
2138
2139 // removed because $this
2140 global $mlsimport;
2141
2142 $log = 'In processing function $attachments_to_move ->' . wp_json_encode( $attachments_to_move ) . PHP_EOL;
2143 mlsimport_saas_single_write_import_custom_logs( $log );
2144 // mlsimport_debuglogs_per_plugin($log);
2145 print esc_html($log);
2146
2147 foreach ( $attachments_to_move as $key => $import_arguments ) {
2148 // reconsider use
2149 // $GLOBALS['wp_object_cache']->delete('mlsimport_force_stop_' . $input_arg['item_id_array']['item_id'], 'options');
2150 $status = get_option( 'mlsimport_force_stop_' . $mlsimportItemId );
2151 if ( 'no' === $status ) {
2152 // reconsider use
2153 // wp_cache_flush();
2154 $mem_usage = memory_get_usage( true );
2155 $mem_usage_show = round( $mem_usage / 1048576, 2 );
2156
2157
2158
2159 mlsimport_saas_single_write_import_custom_logs( $log );
2160 $log = 'Parsing import batch: ' . ( $key + 1 ) . ' of ' . $total_batches . '. Memory used: ' . $mem_usage_show . ' MB.' . PHP_EOL;
2161
2162 // Combine logs and reduce function calls
2163 mlsimport_saas_single_write_import_custom_logs( $log );
2164 mlsimport_debuglogs_per_plugin( $log );
2165 print esc_html($log);
2166
2167 $api_call_array = $this->theme_importer->global_api_request_CURL_saas( 'listings', $import_arguments, 'POST' );
2168
2169 $mlsimport->admin->theme_importer->mlsimport_saas_parse_search_array_per_item( $api_call_array, $input_arg['item_id_array'], $key, $mlsimport_item_option_data );
2170 // $this->admin->theme_importer->mlsimport_saas_parse_search_array_per_item($api_call_array, $input_arg['item_id_array'], $key, $mlsimport_item_option_data);
2171 } else {
2172 update_post_meta( $mlsimportItemId, 'mlsimport_spawn_status', 'completed' );
2173 delete_post_meta( $mlsimportItemId, 'mlsimport_attach_to_move_' . $mlsimportItemId );
2174 mlsimport_saas_single_write_import_custom_logs( PHP_EOL . 'Parsing importing link FORCE STOP : ' );
2175 mlsimport_debuglogs_per_plugin( 'Parsing importing link FORCE STOP : ' );
2176 break; // Exit the loop if forced to stop
2177 }
2178 }
2179
2180 mlsimport_saas_single_write_import_custom_logs( 'Import Completed ' . PHP_EOL );
2181 mlsimport_debuglogs_per_plugin( 'Import Completed ' . PHP_EOL );
2182
2183 update_post_meta( $mlsimportItemId, 'mlsimport_spawn_status', 'completed' );
2184 delete_post_meta( $mlsimportItemId, 'mlsimport_attach_to_move_' . $mlsimportItemId );
2185
2186 unset( $attachments_to_move );
2187 unset( $api_call_array );
2188 unset( $input_arg );
2189 unset( $log );
2190 unset( $log2 );
2191 }
2192
2193
2194
2195
2196
2197 /**
2198 *
2199 *
2200 *
2201 * update log function
2202 */
2203 public function mlsimport_logger_per_item() {
2204 check_ajax_referer( 'mlsimport_item_actions', 'security' );
2205 $post_id=0;
2206 if(isset($_POST['post_id'] )){
2207 $post_id = intval( $_POST['post_id'] );
2208 }
2209
2210 $status = get_post_meta( $post_id, 'mlsimport_spawn_status', true );
2211 $path = WP_PLUGIN_DIR . '/mlsimport/logs/status_logs.log';
2212 $logs = file_get_contents( $path );
2213
2214 $force_status = intval( get_post_meta( $post_id, 'mlsimport_force_stop', true ) );
2215 $force_status = get_option( 'mlsimport_force_stop_' . $post_id );
2216
2217 if ( 'no' !== $force_status ) {
2218 echo wp_json_encode(
2219 array(
2220 'is_done' => 'done',
2221 'status' => $status,
2222 'logs' => $logs,
2223 )
2224 );
2225 die();
2226 }
2227
2228 if ( '' === $status || 'completed' === $status ) {
2229 echo wp_json_encode(
2230 array(
2231 'is_done' => 'done',
2232 'status' => $status,
2233 'logs' => $logs,
2234 )
2235 );
2236 } else {
2237 // return from log
2238 echo wp_json_encode(
2239 array(
2240 'is_done' => 'wip',
2241 'status' => $status,
2242 'logs' => $logs,
2243 )
2244 );
2245 }
2246 die();
2247 }
2248
2249
2250
2251
2252
2253
2254 /**
2255 *
2256 *
2257 *
2258 *
2259 * Force Stop Import
2260 */
2261 public function mlsimport_stop_import_per_item() {
2262 check_ajax_referer( 'mlsimport_item_actions', 'security' );
2263 $post_id=0;
2264 if(isset($_POST['post_id'] )){
2265 $post_id = intval( $_POST['post_id'] );
2266 }
2267 update_option( 'mlsimport_force_stop_' . $post_id, 'yes', false );
2268 mlsimport_saas_single_write_import_custom_logs( 'Stopeed for ' . $post_id . PHP_EOL );
2269 mlsimport_debuglogs_per_plugin( 'Stopeed for ' . $post_id . PHP_EOL );
2270
2271 die();
2272 }
2273
2274
2275
2276 /*
2277 *
2278 * Get MLS Metadata
2279 *
2280 *
2281 *
2282 *
2283 **/
2284
2285 public function mlsimport_saas_get_metadata_function() {
2286 check_ajax_referer( 'mlsimport_saas_get_metadata', 'security' );
2287 $theme_Start = new ThemeImport();
2288
2289 $values = array();
2290 $options = get_option( $this->plugin_name . '_admin_options' );
2291 $url = 'clients?theme_id=' . intval( $options['mlsimport_theme_used'] );
2292
2293 $answer = $theme_Start::global_api_request_saas( $url, $values, 'GET' );
2294
2295 update_option( 'mlsimport_mls_metadata_populated', 'yes' );
2296
2297 update_option( 'mlsimport_mls_metadata_theme_schema', $answer['theme_schema'] );
2298 update_option( 'mlsimport_mls_metadata_mls_data', $answer['mls_data']['mls_meta_data'] );
2299 update_option( 'mlsimport_mls_metadata_mls_enums', $answer['mls_data']['mls_meta_enums'] );
2300 }
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313 /**
2314 *
2315 *
2316 * write debug logs
2317 */
2318 public function mlsimport_debuglog_cron( $message ) {
2319 if ( is_array( $message ) ) {
2320 $message = wp_json_encode( $message );
2321 }
2322 $message = date( 'F j, Y, g:i a' ) . ' -> ' . $message;
2323 global $wp_filesystem;
2324 if ( empty( $wp_filesystem ) ) {
2325 require_once ABSPATH . '/wp-admin/includes/file.php';
2326 WP_Filesystem();
2327 }
2328
2329 $path = WP_PLUGIN_DIR . '/mlsimport/logs/cron_logs.log';
2330
2331 file_put_contents( $path, $message, FILE_APPEND | LOCK_EX );
2332 }
2333
2334 }
2335