PluginProbe
MLSImport: IDX Plugin & MLS Plugin for Real Estate Listings / 5.7.3
MLSImport: IDX Plugin & MLS Plugin for Real Estate Listings v5.7.3
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 6.0.7 All 35 releases
mlsimport / admin / class-mlsimport-admin.php

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

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