PluginProbe
WP Directory Kit / 1.2.4
WP Directory Kit v1.2.4
1.5.6 1.5.5 1.5.4 1.5.3 trunk 1.1.0 1.1.6 1.1.8 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.8 1.4.0 1.4.1 All 35 releases
wpdirectorykit / application / controllers / Wdk_settings.php

Wdk_settings.php in WP Directory Kit 1.2.4, at application/controllers/Wdk_settings.php

1,523 lines 65.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly;
3
4 class Wdk_settings extends Winter_MVC_Controller {
5 public $import_log = '';
6
7 public function __construct(){
8 parent::__construct();
9 }
10
11 // Edit listing method
12 public function index()
13 {
14
15 $this->load->model('settings_m');
16 $this->data['db_data'] = NULL;
17 $this->data['form'] = &$this->form;
18 $this->data['fields'] = $this->settings_m->fields_list;
19 $this->data['fields_list_tabs'] = $this->settings_m->fields_list_tabs;
20
21 $this->form->add_error_message('wdk_slug_format', __('Custom Listing Preview Page Slug allowed only a-z 0-9 - _ ', 'wpdirectorykit'));
22
23 if($this->form->run($this->data['fields']))
24 {
25
26 // Check _wpnonce
27 check_admin_referer( 'wdk-settings-edit', '_wpnonce' );
28
29 // Save procedure for basic data
30
31 $data = $this->settings_m->prepare_data(wdk_get_post(), $this->data['fields']);
32 $wdk_slug_listing_preview_page = get_option('wdk_slug_listing_preview_page');
33
34 // Save standard wp post
35 foreach($data as $key => $val)
36 {
37 update_option( $key, $val, TRUE);
38 }
39
40 if(isset($data['wdk_slug_listing_preview_page']) && $wdk_slug_listing_preview_page != $data['wdk_slug_listing_preview_page']) {
41 update_option('wdk_slug_listing_preview_page_changed', 1);
42 }
43
44 // redirect
45 if(empty($listing_post_id) && !empty($id))
46 {
47 //wp_redirect(admin_url("admin.php?page=wdk_settings&is_updated=true"));
48 exit;
49 }
50
51 }
52
53 // fetch data, after update/insert to get updated last data
54 $fields_data = $this->settings_m->get();
55
56 /* slug udpate rules*/
57 //flush_rewrite_rules();
58
59 foreach($fields_data as $field)
60 {
61 $this->data['db_data'][$field->option_name] = $field->option_value;
62 }
63
64 $this->load->view('wdk_settings/index', $this->data);
65 }
66
67 function reset_data () {
68
69 wdk_access_check('settings_m', 1);
70
71 // Check _wpnonce
72 check_admin_referer( 'reset-data', '_wpnonce' );
73
74 $this->load->model('location_m');
75 $this->load->model('category_m');
76 $this->load->model('field_m');
77 $this->load->model('listingfield_m');
78 $this->load->model('listing_m');
79 $this->load->model('resultitem_m');
80 $this->load->model('searchform_m');
81
82 /* remove fields */
83 $this->db->delete($this->location_m->_table_name);
84 /* end remove fields */
85
86 /* remove fields */
87 $this->db->delete($this->category_m->_table_name);
88 /* end remove fields */
89
90 /* remove fields */
91 $this->db->delete($this->location_m->_table_name);
92 /* end remove fields */
93
94 /* remove fields */
95 $this->db->delete($this->field_m->_table_name);
96 $field_names = $this->listingfield_m->get_available_fields();
97 foreach($field_names as $field_name => $value){
98 if(strpos($field_name, 'field_') ===FALSE) continue;
99 $this->db->query('ALTER TABLE '.$this->listingfield_m->_table_name.' DROP COLUMN '.$field_name.'');
100 }
101 /* end remove fields */
102
103 /* remove listings */
104 $this->db->delete($this->listing_m->_table_name);
105 $this->db->delete($this->listingfield_m->_table_name);
106 /* end remove listings */
107
108 /* reset autoincrement */
109 $this->db->query('TRUNCATE TABLE `'.$this->location_m->_table_name.'`');
110 $this->db->query('TRUNCATE TABLE `'.$this->category_m->_table_name.'`');
111 $this->db->query('TRUNCATE TABLE `'.$this->field_m->_table_name.'`');
112 $this->db->query('TRUNCATE TABLE `'.$this->listingfield_m->_table_name.'`');
113 $this->db->query('TRUNCATE TABLE `'.$this->listing_m->_table_name.'`');
114 /* end reset autoincrement */
115
116 /* remove listings */
117 $this->db->delete($this->resultitem_m->_table_name);
118 $this->db->delete($this->searchform_m->_table_name);
119 /* end remove listings */
120
121 /* reset autoincrement */
122 $this->db->query('TRUNCATE TABLE `'.$this->resultitem_m->_table_name.'`');
123 $this->db->query('TRUNCATE TABLE `'.$this->searchform_m->_table_name.'`');
124
125 $wdk_posts = get_posts( array( 'post_type' => 'wdk-listing', 'numberposts' => -1));
126 foreach( $wdk_posts as $post ) {
127 // Delete's each post.
128 wp_delete_post( $post->ID, true);
129 // Set to False if you want to send them to Trash.
130 }
131
132 $redirect_url = admin_url("admin.php?page=wdk_settings&is_updated");
133 if(isset($_GET['redirect_url']) && strpos($_GET['redirect_url'], 'http') === FALSE && strpos($_GET['redirect_url'], '//') === FALSE)
134 $redirect_url = admin_url(sanitize_text_field($_GET['redirect_url']));
135
136 wp_redirect($redirect_url);
137 exit;
138 }
139
140 // Import demo data listing method
141 public function import_demo()
142 {
143 wdk_access_check('settings_m', 1);
144
145 $this->load->model('field_m');
146 $this->load->model('listingfield_m');
147 $this->load->model('listing_m');
148 $this->load->model('category_m');
149 $this->load->model('location_m');
150 $this->load->model('searchform_m');
151 $this->load->model('resultitem_m');
152 $this->load->model('listingusers_m');
153
154 $this->data['installed'] = false;
155 if($this->field_m->get() || $this->listingfield_m->get() || $this->listing_m->get() || $this->category_m->get() || $this->location_m->get())
156 $this->data['installed'] = true;
157
158 $this->data['required_plugins'] = false;
159 if(!function_exists('eli_installer') || !function_exists('run_elementinvader') || !is_plugin_active('elementor/elementor.php'))
160 $this->data['required_plugins'] = true;
161
162 $this->data['db_data'] = NULL;
163 $this->data['fields'] = array(
164 array('field' => 'import_locations', 'field_label' => __('Locations', 'wpdirectorykit'), 'hint' => __('All Locations will be removed and import demo locations', 'wpdirectorykit'), 'field_type' => 'CHECKBOX', 'rules' => ''),
165 array('field' => 'import_categories', 'field_label' => __('Categories', 'wpdirectorykit'), 'hint' => __('All categories will be removed and import demo categories', 'wpdirectorykit'), 'field_type' => 'CHECKBOX', 'rules' => ''),
166 array('field' => 'import_fields', 'field_label' => __('Fields', 'wpdirectorykit'), 'hint' => __('All fiedls will be removed and import demo fields', 'wpdirectorykit'), 'field_type' => 'CHECKBOX', 'rules' => ''),
167 array('field' => 'import_listings', 'field_label' => __('Listings', 'wpdirectorykit'), 'hint' => __('All listings will be removed and import demo listings', 'wpdirectorykit'), 'field_type' => 'CHECKBOX', 'rules' => ''),
168 array('field' => 'import_visual_data', 'field_label' => __('Search Form & Result Card', 'wpdirectorykit'), 'hint' => __('Import demo Search Form & Result Card', 'wpdirectorykit'), 'field_type' => 'CHECKBOX', 'rules' => ''),
169 array('field' => 'import_page_listing_preview', 'field_label' => __('Listing Preview Page', 'wpdirectorykit'), 'hint' => __('Import demo Listing Preivew Page', 'wpdirectorykit'), 'field_type' => 'CHECKBOX', 'rules' => ''),
170 array('field' => 'import_page_results', 'field_label' => __('Results Page', 'wpdirectorykit'), 'hint' => __('Import demo Results Page', 'wpdirectorykit'), 'field_type' => 'CHECKBOX', 'rules' => ''),
171 );
172
173 $multipurpose_values = array('' => __('Not Selected', 'wpdirectorykit'));
174 if(file_exists(WPDIRECTORYKIT_PATH.'demo-data/'))
175 {
176 $files = array();
177 $dir = WPDIRECTORYKIT_PATH.'demo-data/';
178 if (is_dir($dir)) {
179 if ($dh = opendir($dir)) {
180 while (($file = readdir($dh)) !== false) {
181 if($file == 'locations.xml') continue;
182 if(strpos($file, '.xml') !== false && strpos($file, '~') === false)
183 $multipurpose_values[$file] = ucwords(str_replace(array('_', '-', '.xml'), ' ', $file));
184 }
185 closedir($dh);
186 }
187 }
188 }
189 ksort($multipurpose_values);
190 if(count($multipurpose_values) > 0)
191 $this->data['fields']['multipurpose'] = array('field'=>'multipurpose', 'field_label'=>__('Portal version', 'wpdirectorykit'), 'field_type'=>'DROPDOWN', 'rules'=>'required', 'values'=>$multipurpose_values);
192
193
194 $this->data['form'] = &$this->form;
195 ini_set('max_execution_time', 900);
196
197 // [/Check requirements]
198
199 foreach($this->data['fields'] as $field)
200 {
201 $this->data['db_data'][$field['field']] = 1;
202 }
203
204 if($this->field_m->get()) {
205 $this->data['db_data']['import_fields'] = 0;
206 }
207 if($this->listing_m->get()) {
208 $this->data['db_data']['import_listings'] = 0;
209 }
210 if($this->category_m->get()) {
211 $this->data['db_data']['import_categories'] = 0;
212 }
213 if($this->location_m->get()) {
214 $this->data['db_data']['import_locations'] = 0;
215 }
216
217 if($this->searchform_m->get() || $this->resultitem_m->get()){
218 $this->data['db_data']['import_visual_data'] = 0;
219 }
220
221 if((get_option('wdk_listing_page')) && get_post_status(get_option('wdk_listing_page')) =='publish'){
222 $this->data['db_data']['import_page_listing_preview'] = 0;
223 }
224
225 if((get_option('wdk_results_page')) && get_post_status(get_option('wdk_results_page')) == 'publish'){
226 $this->data['db_data']['import_page_results'] = 0;
227 }
228
229 $this->data['db_data']['multipurpose'] = 'real-estate.xml';
230
231 if(isset($_GET['multipurpose']))
232 $this->data['db_data']['multipurpose'] = sanitize_text_field($_GET['multipurpose']);
233
234 $this->data['import_log'] = '';
235 $rules = array(
236 array(
237 'field' => 'import_locations',
238 'label' => __('Locations', 'wpdirectorykit'),
239 'rules' => ''
240 ),
241 array(
242 'field' => 'import_categories',
243 'label' => __('Categories', 'wpdirectorykit'),
244 'rules' => ''
245 ),
246 array(
247 'field' => 'import_fields',
248 'label' => __('Fields', 'wpdirectorykit'),
249 'rules' => ''
250 ),
251 array(
252 'field' => 'import_listings',
253 'label' => __('Listings', 'wpdirectorykit'),
254 'rules' => ''
255 ),
256 array(
257 'field' => 'import_visual_data',
258 'label' => __('Search Form & Result Card', 'wpdirectorykit'),
259 'rules' => ''
260 ),
261 array(
262 'field' => 'import_page_listing_preview',
263 'label' => __('Listing Preview Page', 'wpdirectorykit'),
264 'rules' => ''
265 ),
266 array(
267 'field' => 'import_page_results',
268 'label' => __('Results Page', 'wpdirectorykit'),
269 'rules' => ''
270 ),
271 array(
272 'field' => 'multipurpose',
273 'label' => __('Purpose', 'wpdirectorykit'),
274 'rules' => 'required'
275 ),
276 );
277
278 $plugin = 'elementor/elementor.php';
279 if (in_array( $plugin, apply_filters( 'active_plugins', get_option( 'active_plugins' ))) && !class_exists('Elementor\Plugin') ) {
280 $this->data['import_log'] .= '<div class="alert alert-danger" role="alert">'.esc_html__('Your Elementor Plugin is not fully active, usually this happen because of old PHP version on server, in such case eventually you can try older Elementor Version or Update PHP on your server').'</div>';
281 $this->data['required_plugins'] = true;
282 }
283
284 if(!$this->data['required_plugins'])
285 if($this->form->run($rules))
286 {
287 // Check _wpnonce
288 check_admin_referer( 'wdk-settings_import', '_wpnonce' );
289
290 // Save procedure for basic data
291 $data = $this->input->post();
292
293 $this->import_settings();
294
295 if( !empty($data['import_locations'])) {
296 $this->import_locations();
297 }
298
299 if( !empty($data['import_categories'])) {
300 $this->import_categories($data['multipurpose']);
301 }
302
303 if( !empty($data['import_fields'])) {
304 $this->import_fields($data['multipurpose']);
305 }
306
307 if( !empty($data['import_listings'])) {
308 $this->import_listings($data['multipurpose']);
309 }
310
311 if( !empty($data['import_visual_data'])) {
312 $this->import_visual_data($data['multipurpose']);
313 }
314
315 if( !empty($data['import_page_listing_preview'])) {
316 $this->demo_page_listing($data['multipurpose']);
317 }
318
319 if( !empty($data['import_page_results'])) {
320 $this->demo_page_results($data['multipurpose']);
321 }
322
323 $this->replace_content_data($data['multipurpose']);
324
325 }
326 $this->load->view('wdk_install/index', $this->data);
327 }
328
329 // Import demo data listing method
330 public function remove()
331 {
332
333 wdk_access_check('settings_m', 1);
334
335 // Check _wpnonce
336 check_admin_referer( 'remove-data', '_wpnonce' );
337
338 $this->load->model('field_m');
339 $this->load->model('listingfield_m');
340 $this->load->model('listing_m');
341 $this->load->model('category_m');
342 $this->load->model('location_m');
343
344 $this->data['data_log'] = '';
345
346 $this->load->model('location_m');
347 $this->load->model('category_m');
348 $this->load->model('field_m');
349 $this->load->model('listingfield_m');
350 $this->load->model('listing_m');
351 $this->load->model('resultitem_m');
352 $this->load->model('searchform_m');
353
354 /* remove fields */
355 $this->db->delete($this->location_m->_table_name);
356
357 $this->data['data_log'] .= '<div class="alert alert-success" role="alert">'.esc_html__('Locations removed').'</div>';
358 /* end remove fields */
359
360 /* remove fields */
361 $this->db->delete($this->category_m->_table_name);
362
363 $this->data['data_log'] .= '<div class="alert alert-success" role="alert">'.esc_html__('Categories removed').'</div>';
364 /* end remove fields */
365
366 /* remove fields */
367 $this->db->delete($this->field_m->_table_name);
368 $field_names = $this->listingfield_m->get_available_fields();
369 foreach($field_names as $field_name => $value){
370 if(strpos($field_name, 'field_') ===FALSE) continue;
371 $this->db->query('ALTER TABLE '.$this->listingfield_m->_table_name.' DROP COLUMN '.$field_name.'');
372 }
373
374 $this->data['data_log'] .= '<div class="alert alert-success" role="alert">'.esc_html__('Fields removed').'</div>';
375 /* end remove fields */
376
377 /* remove listings */
378 $this->db->delete($this->listing_m->_table_name);
379 $this->db->delete($this->listingfield_m->_table_name);
380
381 $this->data['data_log'] .= '<div class="alert alert-success" role="alert">'.esc_html__('Listings removed').'</div>';
382 /* end remove listings */
383
384
385 /* remove listings */
386 $this->db->delete($this->resultitem_m->_table_name);
387 $this->db->delete($this->searchform_m->_table_name);
388 $this->data['data_log'] .= '<div class="alert alert-success" role="alert">'.esc_html__('Result Card And Search form removed').'</div>';
389 /* end remove listings */
390
391
392 /* reset autoincrement */
393 $this->db->query('TRUNCATE TABLE `'.$this->location_m->_table_name.'`');
394 $this->db->query('TRUNCATE TABLE `'.$this->category_m->_table_name.'`');
395 $this->db->query('TRUNCATE TABLE `'.$this->field_m->_table_name.'`');
396 $this->db->query('TRUNCATE TABLE `'.$this->listingfield_m->_table_name.'`');
397 $this->db->query('TRUNCATE TABLE `'.$this->listing_m->_table_name.'`');
398 $this->db->query('TRUNCATE TABLE `'.$this->resultitem_m->_table_name.'`');
399 $this->db->query('TRUNCATE TABLE `'.$this->searchform_m->_table_name.'`');
400
401 $this->data['data_log'] .= '<div class="alert alert-success" role="alert">'.esc_html__('Tables reset').'</div>';
402 /* end reset autoincrement */
403
404 $wdk_posts = get_posts( array( 'post_type' => 'wdk-listing', 'numberposts' => -1));
405 foreach( $wdk_posts as $post ) {
406 // Delete's each post.
407 wp_delete_post( $post->ID, true);
408 // Set to False if you want to send them to Trash.
409 }
410
411 if(isset($_GET['redirect_url']) && strpos($_GET['redirect_url'], 'http') === FALSE && strpos($_GET['redirect_url'], '//') === FALSE){
412 if(wmvc_xss_clean($_GET['redirect_url']) =='on_install') {
413 $redirect_url = admin_url("admin.php?page=wdk_settings&function=import_demo&is_updated");
414 } else {
415 $redirect_url = admin_url(sanitize_text_field($_GET['redirect_url']));
416 }
417 wp_redirect($redirect_url);
418 }
419
420 $this->load->view('wdk_install/remove_log', $this->data);
421 }
422
423 /* add demo pages */
424 private function demo_pages($purpose = '') {
425
426 // if(get_option('wdk_listing_page') || get_option('wdk_results_page')) return true;
427
428 add_action('wpdirectorykit/elementor-elements/register_widget', function($self){
429 $self->add_widget('Wdk\Elementor\Extensions\WdkContactFormExt');
430 });
431
432 add_action('wpdirectorykit/elementor-elements/register_widget', function(){
433 add_action('eli/includes', function(){
434 require_once WPDIRECTORYKIT_PATH . '/elementor-extensions/class-contact-form.php';
435 });
436
437 add_action('eli/register_widget', function(){
438 $object = new Wdk\Elementor\Extensions\WdkContactFormExt();
439 \Elementor\Plugin::instance()->widgets_manager->register( $object );
440 });
441 });
442
443 add_action('wpdirectorykit/elementor-elements/register_widget', function($self){
444 $self->add_widget('Wdk\Elementor\Widgets\WdkFieldLabel');
445 $self->add_widget('Wdk\Elementor\Widgets\WdkFieldValue');
446 $self->add_widget('Wdk\Elementor\Widgets\WdkFieldImages');
447 $self->add_widget('Wdk\Elementor\Widgets\WdkFieldIcon');
448 $self->add_widget('Wdk\Elementor\Widgets\WdkListingSimilarListings');
449 $self->add_widget('Wdk\Elementor\Widgets\WdkListingSlider');
450 $self->add_widget('Wdk\Elementor\Widgets\WdkListingFieldsSection');
451 $self->add_widget('Wdk\Elementor\Widgets\WdkListingMap');
452 $self->add_widget('Wdk\Elementor\Widgets\WdkListinAgent');
453 $self->add_widget('Wdk\Elementor\Widgets\WdkListingAgentField');
454 $self->add_widget('Wdk\Elementor\Widgets\WdkListingAgentAvatar');
455 });
456
457
458 // Import elementor templates
459 $page_listing_preview = $this->create_page(esc_html__('Listing Preview', 'wpdirectorykit'), '', 'elementor_canvas');
460 $this->elementor_assign($page_listing_preview->ID, 'page-listing-preview.json');
461
462 $page_results = $this->create_page(esc_html__('Results Listings', 'wpdirectorykit'), '', 'elementor_canvas');
463 $this->elementor_assign($page_results->ID, 'page-results-listings.json');
464
465
466 $menus = get_registered_nav_menus();
467 // first menu defined by template
468 $first_menu = key($menus);
469
470 if ( has_nav_menu($first_menu) ) {
471 $menus = wp_get_nav_menus();
472 $menu_id = (int)$menus[0]->term_id;
473 } else {
474 wp_delete_nav_menu('Primary menu');
475 $menu_name = 'Primary menu';
476 $menu_exists = wp_get_nav_menu_object( $menu_name );
477 $menu_term = $menu_exists;
478 $menu_id = wp_create_nav_menu($menu_name);
479 }
480
481 // first menu defined by template
482 if (!empty($menu_id)) {
483 $itemData = array(
484 'menu-item-object-id' => $page_results->ID,
485 'menu-item-title' => esc_html__('Grid Map', 'wpdirectorykit'),
486 'menu-item-parent-id' => 0,
487 'menu-item-position' => 3,
488 'menu-item-object' => 'page',
489 'menu-item-type' => 'post_type',
490 'menu-item-status' => 'publish'
491 );
492
493 wp_update_nav_menu_item((int)$menu_id, 0, $itemData);
494
495 // assign menu to top menu
496 $locations = get_theme_mod( 'nav_menu_locations' );
497 $locations[$first_menu] = $menu_id;
498 set_theme_mod('nav_menu_locations', $locations);
499 }
500
501 // Assign page.
502 update_option( 'show_on_front', 'page' );
503 update_option( 'page_on_front', $page_results->ID );
504
505 if($page_listing_preview)
506 update_option( 'wdk_listing_page', $page_listing_preview->ID, TRUE);
507
508 if($page_results)
509 update_option( 'wdk_results_page', $page_results->ID, TRUE);
510
511 $this->replace_content_data($purpose);
512
513 return true;
514 }
515
516
517 private function add_demo_user($username, $name, $surname, $email_address='', $type='wdk_agent', $password=NULL, $meta_data = array())
518 {
519
520 if ( username_exists( $username )) {
521 return username_exists( $username );
522 }
523
524 if ( email_exists($email_address)) {
525 return email_exists($email_address);
526 }
527
528 if ( empty($email_address)) {
529 return false;
530 }
531
532 // Generate the password and create the user
533 if(empty($password))
534 $password = wp_generate_password( 12, false );
535
536 $user_id = wp_create_user( $username, $password, $email_address );
537
538 // Set the nickname
539 wp_update_user(
540 array(
541 'ID' => $user_id,
542 'nickname' => $name.' '.$surname,
543 'first_name' => $name,
544 'last_name' => $surname,
545 'description' => 'Aenean sollicitudin, lorem quis bibendum auctor, nisi elit consequat ipsum, nec sagittis sem nibh id elit. Duis sed odio sit amet nibh vulputate cursus a sit amet maurs. Morbi accumsan ipsum velit. Nam nec tellus a odio tincidunt auctor a ornare odi non mauris vitae erat consequat Aenean sollicitudin, lorem quis bibendum auctor, nisi elit consequat ipsum, nec sagittis sem nibh id elit.',
546 'display_name'=> $name.' '.$surname,
547 'user_url' => '' ,
548 'admin_bar_front' => 0 ,
549 'wdk_phone' => '123456789'
550 )
551 );
552
553 $user = new WP_User( $user_id );
554 $user->set_role($type);
555
556 if(!empty($meta_data))
557 foreach ($meta_data as $meta_key => $meta_value){
558 update_user_meta( $user_id, $meta_key, wmvc_xss_clean($meta_value) );
559 }
560
561 update_user_meta( $user_id, 'show_admin_bar_front', 'false' );
562 update_user_meta( $user_id, 'show_admin_bar_admin', 'false' );
563
564 return $user_id;
565 }
566
567 private function replace_content_data($purpose = '') {
568 /* Replace Links */
569 /* login */
570 $from = 'https://www.wpdirectorykit.com/nexproperty/wp-admin/wp-login.php';
571 $to = get_admin_url();
572 $this->replace_meta($from, $to);
573
574
575 $from = 'https://www.wpdirectorykit.com/nexproperty';
576 $to = get_home_url();
577 $this->replace_meta($from, $to);
578
579 /* homepage */
580 $from = '2020';
581 $to = date('Y');
582 $this->replace_meta($from, $to);
583
584
585 /* replace in db */
586 $xml_list = WPDIRECTORYKIT_PATH.'demo-data/'.$purpose;
587 if(file_exists($xml_list)) {
588 $dom_array = $this->xmlstr_to_array(file_get_contents($xml_list));
589 if(isset($dom_array['replace']) && !empty($dom_array['replace'])) {
590 if(!empty($dom_array['replace']['text'])) {
591 foreach($dom_array['replace']['text'] as $string){
592 if(!isset($string['@attributes']) || !isset($string['@attributes']['from']) || !isset($string['@attributes']['to'])) continue;
593 if(empty($string['@attributes']['from']) || empty( $string['@attributes']['to'])) continue;
594 $this->replace_meta($string['@attributes']['from'], $string['@attributes']['to']);
595 }
596 }
597 } else {
598
599 }
600 }
601 return true;
602
603 }
604
605 /* add demo listing preview page */
606 private function demo_page_listing($purpose = '') {
607
608 if((get_option('wdk_listing_page')) && get_post_status(get_option('wdk_listing_page')) =='publish'){
609 $this->data['import_log'] .= '<div class="alert alert-danger" role="alert">'.esc_html__('Listing Preview Page already exists').'</div>';
610 return false;
611 }
612
613 add_action('wpdirectorykit/elementor-elements/register_widget', function($self){
614 $self->add_widget('Wdk\Elementor\Extensions\WdkContactFormExt');
615 });
616
617 add_action('wpdirectorykit/elementor-elements/register_widget', function(){
618 add_action('eli/includes', function(){
619 require_once WPDIRECTORYKIT_PATH . '/elementor-extensions/class-contact-form.php';
620 });
621
622 add_action('eli/register_widget', function(){
623 $object = new Wdk\Elementor\Extensions\WdkContactFormExt();
624 \Elementor\Plugin::instance()->widgets_manager->register( $object );
625 });
626 });
627
628 add_action('wpdirectorykit/elementor-elements/register_widget', function($self){
629 $self->add_widget('Wdk\Elementor\Widgets\WdkFieldLabel');
630 $self->add_widget('Wdk\Elementor\Widgets\WdkFieldValue');
631 $self->add_widget('Wdk\Elementor\Widgets\WdkFieldImages');
632 $self->add_widget('Wdk\Elementor\Widgets\WdkFieldIcon');
633 $self->add_widget('Wdk\Elementor\Widgets\WdkListingSimilarListings');
634 $self->add_widget('Wdk\Elementor\Widgets\WdkListingSlider');
635 $self->add_widget('Wdk\Elementor\Widgets\WdkListingFieldsSection');
636 $self->add_widget('Wdk\Elementor\Widgets\WdkListingMap');
637 $self->add_widget('Wdk\Elementor\Widgets\WdkListinAgent');
638 $self->add_widget('Wdk\Elementor\Widgets\WdkListingAgentField');
639 $self->add_widget('Wdk\Elementor\Widgets\WdkListingAgentAvatar');
640 });
641
642 // Import elementor templates
643 $page_listing_preview = $this->create_page(esc_html__('Listing Preview', 'wpdirectorykit'), '', 'elementor_canvas');
644 $this->elementor_assign($page_listing_preview->ID, 'page-listing-preview.json');
645
646 // Assign page.
647 if($page_listing_preview)
648 update_option( 'wdk_listing_page', $page_listing_preview->ID, TRUE);
649
650 $this->data['import_log'] .= '<div class="alert alert-success" role="alert">'.esc_html__('Listing Preview Page imported').'</div>';
651
652 return true;
653 }
654
655 /* add demo results page */
656 private function demo_page_results($purpose = '') {
657
658 if((get_option('wdk_results_page')) && get_post_status(get_option('wdk_results_page')) == 'publish'){
659 $this->data['import_log'] .= '<div class="alert alert-danger" role="alert">'.esc_html__('Results Listings Page already exists').'</div>';
660 return false;
661 }
662
663 add_action('wpdirectorykit/elementor-elements/register_widget', function($self){
664 $self->add_widget('Wdk\Elementor\Extensions\WdkContactFormExt');
665 });
666
667 add_action('wpdirectorykit/elementor-elements/register_widget', function(){
668 add_action('eli/includes', function(){
669 require_once WPDIRECTORYKIT_PATH . '/elementor-extensions/class-contact-form.php';
670 });
671
672 add_action('eli/register_widget', function(){
673 $object = new Wdk\Elementor\Extensions\WdkContactFormExt();
674 \Elementor\Plugin::instance()->widgets_manager->register( $object );
675 });
676 });
677
678 add_action('wpdirectorykit/elementor-elements/register_widget', function($self){
679 $self->add_widget('Wdk\Elementor\Widgets\WdkFieldLabel');
680 $self->add_widget('Wdk\Elementor\Widgets\WdkFieldValue');
681 $self->add_widget('Wdk\Elementor\Widgets\WdkFieldImages');
682 $self->add_widget('Wdk\Elementor\Widgets\WdkFieldIcon');
683 $self->add_widget('Wdk\Elementor\Widgets\WdkListingSimilarListings');
684 $self->add_widget('Wdk\Elementor\Widgets\WdkListingSlider');
685 $self->add_widget('Wdk\Elementor\Widgets\WdkListingFieldsSection');
686 $self->add_widget('Wdk\Elementor\Widgets\WdkListingMap');
687 $self->add_widget('Wdk\Elementor\Widgets\WdkListinAgent');
688 $self->add_widget('Wdk\Elementor\Widgets\WdkListingAgentField');
689 $self->add_widget('Wdk\Elementor\Widgets\WdkListingAgentAvatar');
690 });
691
692 $page_results = $this->create_page(esc_html__('Results Listings', 'wpdirectorykit'), '', 'elementor_canvas');
693 $this->elementor_assign($page_results->ID, 'page-results-listings.json');
694
695 // Assign page.
696 update_option( 'show_on_front', 'page' );
697 update_option( 'page_on_front', $page_results->ID );
698
699 update_option( 'wdk_results_page', $page_results->ID, TRUE);
700
701 $this->data['import_log'] .= '<div class="alert alert-success" role="alert">'.esc_html__('Results Listings Page imported').'</div>';
702
703
704 $menus = get_registered_nav_menus();
705 // first menu defined by template
706 $first_menu = key($menus);
707
708 if ( has_nav_menu($first_menu) ) {
709 $menus = wp_get_nav_menus();
710 $menu_id = (int)$menus[0]->term_id;
711 } else {
712 wp_delete_nav_menu('Primary menu');
713 $menu_name = 'Primary menu';
714 $menu_exists = wp_get_nav_menu_object( $menu_name );
715 $menu_term = $menu_exists;
716 $menu_id = wp_create_nav_menu($menu_name);
717 }
718
719 // first menu defined by template
720 if (!empty($menu_id)) {
721 $itemData = array(
722 'menu-item-object-id' => $page_results->ID,
723 'menu-item-title' => esc_html__('Grid Map', 'wpdirectorykit'),
724 'menu-item-parent-id' => 0,
725 'menu-item-position' => 3,
726 'menu-item-object' => 'page',
727 'menu-item-type' => 'post_type',
728 'menu-item-status' => 'publish'
729 );
730
731 wp_update_nav_menu_item((int)$menu_id, 0, $itemData);
732
733 // assign menu to top menu
734 $locations = get_theme_mod( 'nav_menu_locations' );
735 $locations[$first_menu] = $menu_id;
736 set_theme_mod('nav_menu_locations', $locations);
737 }
738
739 return true;
740 }
741
742 /* Create Page */
743 private function create_page($post_title, $post_content = '', $post_template = NULL, $post_parent=0)
744 {
745 $post = wdk_page_by_title($post_title, 'OBJECT', 'page' );
746
747 $post_id = NULL;
748
749 // Delete posts and rebuild
750 if(!empty($post))
751 {
752 wp_delete_post($post->ID, true);
753 $post=NULL;
754 }
755
756 if(!empty($post))
757 $post_id = $post->ID;
758
759 if(empty($post_id))
760 {
761 $error_obj = NULL;
762 $post_insert = array(
763 'post_title' => wp_strip_all_tags( $post_title ),
764 'post_content' => $post_content,
765 'post_status' => 'publish',
766 'post_type' => 'page',
767 'post_author' => get_current_user_id(),
768 'post_category' => array(1,2),
769 'page_template' => $post_template,
770 'post_parent' => $post_parent
771 );
772 $post_id = wp_insert_post( $post_insert, $error_obj );
773 }
774
775 $post_insert = get_post( $post_id );
776
777 return $post_insert;
778 }
779
780 /* Elementor Import Template */
781 private function elementor_assign($page_id, $json_template_name = '')
782 {
783
784 $file = false;
785
786 if(is_child_theme() && file_exists(get_stylesheet_directory().'/elementor-data/wpdirectorykit/'.$json_template_name))
787 {
788 $file = get_stylesheet_directory().'/elementor-data/wpdirectorykit/'.$json_template_name;
789 }
790 elseif(file_exists(get_template_directory().'/demo-data/wpdirectorykit/'.$json_template_name))
791 {
792 $file = get_template_directory().'/demo-data/wpdirectorykit/'.$json_template_name;
793 }
794 elseif(file_exists( WPDIRECTORYKIT_PATH. '/demo-data/'.$json_template_name))
795 {
796 $file = WPDIRECTORYKIT_PATH.'demo-data/'.$json_template_name;
797 }
798
799 if(!$file || !class_exists('Elementor\Plugin'))
800 {
801 return false;
802 }
803
804 $page_template = get_page_template_slug( $page_id );
805
806 add_post_meta( $page_id, '_elementor_edit_mode', 'builder' );
807
808 global $wp_filesystem;
809 // Initialize the WP filesystem, no more using 'file-put-contents' function
810 if (empty($wp_filesystem)) {
811 WP_Filesystem();
812 }
813
814 $string = $wp_filesystem->get_contents($file);
815
816 $json_template = json_decode($string, true);
817 $elements = $json_template['content'];
818
819 $data = array(
820 'elements' => $elements,
821 'settings' => array('post_status'=>'autosave', 'template'=>$page_template),
822 );
823 // @codingStandardsIgnoreStart
824 $document = Elementor\Plugin::$instance->documents->get( $page_id, false );
825 // @codingStandardsIgnoreEnd
826 return $document->save( $data );
827 }
828
829 private function import_visual_data($purpose = '') {
830 $this->load->model('searchform_m');
831 $this->load->model('resultitem_m');
832 $this->load->model('field_m');
833
834 if($this->searchform_m->get() || $this->resultitem_m->get()){
835 $this->data['import_log'] .= '<div class="alert alert-danger" role="alert">'.esc_html__('Search and result card already imported', 'wpdirectorykit').'</div>';
836 return false;
837 }
838
839 /* remove listings */
840 $this->db->delete($this->resultitem_m->_table_name);
841 $this->db->delete($this->searchform_m->_table_name);
842 /* end remove listings */
843
844 /* reset autoincrement */
845 $this->db->query('TRUNCATE TABLE `'.$this->resultitem_m->_table_name.'`');
846 $this->db->query('TRUNCATE TABLE `'.$this->searchform_m->_table_name.'`');
847
848 $xml_list = WPDIRECTORYKIT_PATH.'demo-data/'.$purpose;
849 if(file_exists($xml_list)) {
850 $dom_array = $this->xmlstr_to_array(file_get_contents($xml_list));
851
852 if(!isset($dom_array['search_form']) && empty($dom_array['search_form'])) {
853 } else {
854 // Save searchform_m
855 $data = array();
856 $data['searchform_name'] = 'Primary';
857 $data['searchform_json'] = trim($dom_array['search_form']);
858
859 if (function_exists('run_wdk_bookings') && isset($dom_array['search_form_premium']) && !empty($dom_array['search_form_premium'])) {
860 $data['searchform_json'] = trim($dom_array['search_form_premium']);
861 }
862
863 $data['idsearchform'] = 1;
864 $insert_id = $this->searchform_m->insert($data, NULL);
865 }
866
867 if(!isset($dom_array['resultitem']) && empty($dom_array['resultitem'])) {
868 } else {
869 // Save searchform_m
870 $data = array();
871 $data['resultitem_name'] = 'Primary';
872 $data['resultitem_json'] = trim($dom_array['resultitem']);
873 $data['idresultitem'] = 1;
874 $insert_id = $this->resultitem_m->insert($data, NULL);
875 }
876
877 $this->data['import_log'] .= '<div class="alert alert-success" role="alert">'.esc_html__('Search Form & Result Card imported').'</div>';
878 return true;
879 }
880
881 $this->data['import_log'] .= '<div class="alert alert-danger" role="alert">'.esc_html__('Missing xml file '.$xml_list, 'wpdirectorykit').'</div>';
882 return false;
883
884 }
885
886 private function import_locations() {
887 $xml_list = WPDIRECTORYKIT_PATH.'demo-data/locations.xml';
888 $this->load->model('location_m');
889
890 if($this->location_m->get()){
891 $this->data['import_log'] .= '<div class="alert alert-danger" role="alert">'.esc_html__('Locations already imported', 'wpdirectorykit').'</div>';
892 return false;
893 }
894
895 if(file_exists($xml_list)) {
896 $dom_array = $this->xmlstr_to_array(file_get_contents($xml_list));
897 if(!isset($dom_array['locations']['location']) && empty($dom_array['locations']['location'])) {
898 $this->data['import_log'] .= '<div class="alert alert-danger" role="alert">'.esc_html__('No fields in xml file '.$xml_list, 'wpdirectorykit').'</div>';
899 return false;
900 }
901 foreach ($dom_array['locations']['location'] as $key => $location_data) {
902 $this->_recursive_add_locations($location_data);
903 }
904
905 $this->data['import_log'] .= '<div class="alert alert-success" role="alert">'.esc_html__('Locations imported').'</div>';
906 return true;
907 }
908
909 $this->data['import_log'] .= '<div class="alert alert-danger" role="alert">'.esc_html__('Missing xml file '.$xml_list, 'wpdirectorykit').'</div>';
910 return false;
911 }
912
913 private function _recursive_add_locations ($data = array(), $parent_id = 0, $level = 0 ) {
914 $base = array(
915 'location_title' => '',
916 'parent_id' => $parent_id,
917 'order_index' => '',
918 'level' => $level,
919 'font_icon_code' => '',
920 'image_id' => '',
921 'icon_id' => '',
922 'code' => '',
923 );
924 $location_data = array_intersect_key(array_merge($base, $data), $base);
925 array_walk($location_data, function(&$item){$item = (is_array($item)) ? '' : trim(wmvc_xss_clean($item));});
926
927 if(empty(wmvc_show_data('location_title', $location_data))) {
928 return false;
929 }
930
931 if(!empty($location_data['image_id']) && file_exists(WPDIRECTORYKIT_PATH.'demo-data/images/'.$location_data['image_id'])){
932 /* add files */
933 $location_data['image_id'] = wmvc_add_wp_image(WPDIRECTORYKIT_PATH.'demo-data/images/'.$location_data['image_id']);
934 }
935
936 if(!empty($location_data['icon_id']) && file_exists(WPDIRECTORYKIT_PATH.'demo-data/images/'.$location_data['icon_id'])){
937 /* add files */
938 $location_data['icon_id'] = wmvc_add_wp_image(WPDIRECTORYKIT_PATH.'demo-data/images/'.$location_data['icon_id']);
939 }
940
941 $location_data['icon_path'] = wdk_generate_path_image($location_data['icon_id']);
942 $location_data['image_path'] = wdk_generate_path_image($location_data['image_id']);
943
944 /* add locations */
945 $insert_id = $this->location_m->insert($location_data, NULL);
946 if(isset($data['childs']["location"]) && !empty($data['childs']["location"])) {
947 foreach ($data['childs']["location"] as $sub_location_data) {
948 $this->_recursive_add_locations($sub_location_data, $insert_id, ($level+1));
949 }
950 }
951 }
952
953 private function import_categories($purpose = '') {
954 $xml_list = WPDIRECTORYKIT_PATH.'demo-data/'.$purpose;
955 $this->load->model('category_m');
956
957 if($this->category_m->get()){
958 $this->data['import_log'] .= '<div class="alert alert-danger" role="alert">'.esc_html__('Categories already imported', 'wpdirectorykit').'</div>';
959 return false;
960 }
961
962 if(file_exists($xml_list)) {
963 $dom_array = $this->xmlstr_to_array(file_get_contents($xml_list));
964 if(!isset($dom_array['categories']['category']) && empty($dom_array['categories']['category'])) {
965 $this->data['import_log'] .= '<div class="alert alert-danger" role="alert">'.esc_html__('No fields in xml file '.$xml_list, 'wpdirectorykit').'</div>';
966 return false;
967 }
968
969 foreach ($dom_array['categories']['category'] as $key => $category_data) {
970 $this->_recursive_add_categories($category_data);
971 }
972
973 $this->data['import_log'] .= '<div class="alert alert-success" role="alert">'.esc_html__('Categories imported').'</div>';
974 return true;
975 }
976
977 $this->data['import_log'] .= '<div class="alert alert-danger" role="alert">'.esc_html__('Missing xml file '.$xml_list, 'wpdirectorykit').'</div>';
978 return false;
979 }
980
981 private function _recursive_add_categories ($data = array(), $parent_id = 0, $level = 0 ) {
982 $base = array(
983 'category_title' => '',
984 'parent_id' => $parent_id,
985 'order_index' => '',
986 'level' => $level,
987 'font_icon_code' => '',
988 'image_id' => '',
989 'icon_id' => '',
990 'code' => '',
991 );
992
993 $category_data = array_intersect_key(array_merge($base, $data), $base);
994 array_walk($category_data, function(&$item){$item = (is_array($item)) ? '' : trim(wmvc_xss_clean($item));});
995
996 if(empty(wmvc_show_data('category_title', $category_data))) {
997 return false;
998 }
999
1000 if(!empty($category_data['image_id'])){
1001
1002 $file = false;
1003 if(is_child_theme() && file_exists(get_stylesheet_directory().'/elementor-data/wpdirectorykit/images/'.$category_data['image_id']))
1004 {
1005 $file = get_stylesheet_directory().'/elementor-data/wpdirectorykit/images/'.$category_data['image_id'];
1006 }
1007 elseif(file_exists(get_template_directory().'/demo-data/wpdirectorykit/images/'.$category_data['image_id']))
1008 {
1009 $file = get_template_directory().'/demo-data/wpdirectorykit/images/'.$category_data['image_id'];
1010 }
1011 elseif(file_exists( WPDIRECTORYKIT_PATH.'demo-data/images/'.$category_data['image_id']))
1012 {
1013 $file = WPDIRECTORYKIT_PATH.'demo-data/images/'.$category_data['image_id'];
1014 }
1015 /* add files */
1016
1017 if($file)
1018 $category_data['image_id'] = wmvc_add_wp_image($file);
1019 }
1020
1021 if(!empty($category_data['icon_id'])){
1022 /* add files */
1023 $file = false;
1024 if(is_child_theme() && file_exists(get_stylesheet_directory().'/elementor-data/wpdirectorykit/images/'.$category_data['icon_id']))
1025 {
1026 $file = get_stylesheet_directory().'/elementor-data/wpdirectorykit/images/'.$category_data['icon_id'];
1027 }
1028 elseif(file_exists(get_template_directory().'/demo-data/wpdirectorykit/images/'.$category_data['icon_id']))
1029 {
1030 $file = get_template_directory().'/demo-data/wpdirectorykit/images/'.$category_data['icon_id'];
1031 }
1032 elseif(file_exists( WPDIRECTORYKIT_PATH.'demo-data/images/'.$category_data['icon_id']))
1033 {
1034 $file = WPDIRECTORYKIT_PATH.'demo-data/images/'.$category_data['icon_id'];
1035 }
1036 /* add files */
1037
1038 if($file)
1039 $category_data['icon_id'] = wmvc_add_wp_image($file);
1040 }
1041
1042 $category_data['icon_path'] = wdk_generate_path_image($category_data['icon_id']);
1043 $category_data['image_path'] = wdk_generate_path_image($category_data['image_id']);
1044
1045 /* add categories */
1046 $insert_id = $this->category_m->insert($category_data, NULL);
1047 if(isset($data['childs']["category"]) && !empty($data['childs']["category"])) {
1048 foreach ($data['childs']["category"] as $sub_category_data) {
1049 $this->_recursive_add_categories($sub_category_data, $insert_id, ($level+1));
1050 }
1051 }
1052 }
1053
1054 private function import_fields($purpose = '') {
1055 $this->load->model('field_m');
1056 $this->load->model('listingfield_m');
1057
1058 if($this->field_m->get()){
1059 $this->data['import_log'] .= '<div class="alert alert-danger" role="alert">'.esc_html__('Fields already imported', 'wpdirectorykit').'</div>';
1060 return false;
1061 }
1062
1063 $xml_list = WPDIRECTORYKIT_PATH.'demo-data/'.$purpose;
1064
1065 $sections_added_array = array();
1066 if(file_exists($xml_list)) {
1067 $dom_array = $this->xmlstr_to_array(file_get_contents($xml_list));
1068
1069 $base = array(
1070 'field_type' => '',
1071 'field_label' => '',
1072 'hint' => '',
1073 'columns_number' => '',
1074 'is_visible_frontend' => '1',
1075 'is_visible_dashboard' => '1',
1076 'prefix' => '',
1077 'suffix' => '',
1078 'values_list' => '',
1079 'icon_id' => '',
1080 'is_required' => '',
1081 'order_index' => '',
1082 );
1083
1084 if(!isset($dom_array['fields']['field']) && empty($dom_array['fields']['field'])) {
1085 $this->data['import_log'] .= '<div class="alert alert-danger" role="alert">'.esc_html__('No fields in xml file '.$xml_list, 'wpdirectorykit').'</div>';
1086 return false;
1087 }
1088
1089 foreach ($dom_array['fields']['field'] as $key => $field) {
1090 $field_data = array_intersect_key(array_merge($base, $field), $base);
1091 array_walk($field_data, function(&$item){$item = (is_array($item)) ? '' : trim(wmvc_xss_clean($item));});
1092
1093 if(empty(wmvc_show_data('field_type', $field_data)) || empty(wmvc_show_data('field_label', $field_data))) {
1094 continue;
1095 }
1096
1097 $field_data['is_visible_frontend'] = 1;
1098 $field_data['is_visible_dashboard'] = 1;
1099
1100
1101 /* special set number format for price fields */
1102 if($field_data['field_label'] == 'Rent Price' || $field_data['field_label'] == 'Sale Price') {
1103 $field_data['is_price_format'] = 1;
1104 }
1105
1106 if(!in_array($field['section'], $sections_added_array)) {
1107 /* add section */
1108 $section_data = array(
1109 'field_type' => 'SECTION',
1110 'field_label' => $field['section'],
1111 'columns_number' => '12',
1112 'is_visible_frontend' => '1',
1113 'is_visible_dashboard' => '1',
1114 'order_index' => $field['order_index'],
1115 );
1116 /* add field */
1117
1118 if($section_data['field_label'] == 'SEO') {
1119 $section_data['is_visible_frontend'] = 0;
1120 }
1121
1122 $this->field_m->insert($section_data, NULL);
1123
1124 $sections_added_array[] = $field['section'];
1125 }
1126
1127 /* add field */
1128
1129 if($field_data['field_label'] == 'Keywords' || $field_data['field_label'] == 'Short Description') {
1130 $field_data['is_visible_frontend'] = 0;
1131 }
1132
1133 $insert_id = $this->field_m->insert($field_data, NULL);
1134 // check if column exists, add if nto exists
1135 if(!empty($insert_id))
1136 $this->listingfield_m->create_table_column($field_data, $insert_id);
1137 }
1138
1139 $this->data['import_log'] .= '<div class="alert alert-success" role="alert">'.esc_html__('Fields imported').'</div>';
1140 return true;
1141 }
1142
1143 $this->data['import_log'] .= '<div class="alert alert-danger" role="alert">'.esc_html__('Missing xml file '.$xml_list, 'wpdirectorykit').'</div>';
1144 return false;
1145 }
1146
1147 private function import_listings($purpose = '') {
1148 $this->load->model('field_m');
1149 $this->load->model('listingfield_m');
1150 $this->load->model('listing_m');
1151 $this->load->model('category_m');
1152 $this->load->model('location_m');
1153 $this->load->model('listingusers_m');
1154
1155 if($this->listing_m->get()){
1156 $this->data['import_log'] .= '<div class="alert alert-danger" role="alert">'.esc_html__('Listings already imported', 'wpdirectorykit').'</div>';
1157 return false;
1158 }
1159
1160 /* generate listings */
1161 $categories_list = $this->category_m->get();
1162 $locations_list = $this->location_m->get();
1163 $categories = array();
1164 $locations = array();
1165 if($categories_list)
1166 foreach($categories_list as $category){
1167 $categories[] = $category->idcategory;
1168 }
1169 if($locations_list)
1170 foreach($locations_list as $location){
1171 $locations[] = $location->idlocation;
1172 }
1173
1174 $this->db->where(array('field_type !='=> 'SECTION'));
1175 $fields = $this->field_m->get();
1176
1177 $listingfield_list = $this->listingfield_m->get_available_fields();
1178 foreach($fields as $key => $field_data) {
1179 if(!isset($listingfield_list['field_'.$field_data->idfield.'_'.$field_data->field_type])) {
1180 unset($fields[$key]);
1181 }
1182 }
1183
1184 // Open a known directory, and proceed to read its contents
1185 $dir = WPDIRECTORYKIT_PATH.'demo-data/images';
1186 $purpose_name = str_replace('.xml','',$purpose);
1187 $files = array();
1188 if (is_dir($dir)) {
1189 if ($dh = opendir($dir)) {
1190 while (($file = readdir($dh)) !== false) {
1191 if(strpos($file, '.jpg') !== false && strpos($file, $purpose_name) !== false && strpos($file, 'category') === false)
1192 $files[] = $file;
1193 }
1194 closedir($dh);
1195 }
1196 }
1197 sort($files);
1198
1199 $xml_list = WPDIRECTORYKIT_PATH.'demo-data/'.$purpose;
1200 if(file_exists($xml_list)) {
1201 $dom_array = $this->xmlstr_to_array(file_get_contents($xml_list));
1202 if(!isset($dom_array['listings']['listing']) && empty($dom_array['listings']['listing'])) {
1203 $this->data['import_log'] .= '<div class="alert alert-danger" role="alert">'.esc_html__('No listings in xml file '.$xml_list, 'wpdirectorykit').'</div>';
1204 return false;
1205 }
1206
1207 /* demo agent add/get if exists */
1208 $user_ids = array();
1209 $user_ids[] = $this->add_demo_user('Debra_Moran', 'Debra', 'Moran', 'agent1@wpdirectorykit.com','wdk_agent', NULL, array('wdk_phone' =>'(917) 367-2058','wdk_facebook'=>'#','wdk_linkedin'=>'#','wdk_instagram'=>'#','wdk_telegram'=>'#','wdk_twitter'=>'#'));
1210 $user_ids[] = $this->add_demo_user('Garry_Novan', 'Garry', 'Novan', 'agent2@wpdirectorykit.com','wdk_agent', NULL, array('wdk_phone' =>'(918) 345-2054','wdk_facebook'=>'#','wdk_linkedin'=>'#','wdk_instagram'=>'#','wdk_telegram'=>'#','wdk_twitter'=>'#'));
1211 $user_ids[] = $this->add_demo_user('Kety_Spear', 'Kety', 'Spear', 'agent3@wpdirectorykit.com','wdk_agent', NULL, array('wdk_phone' =>'(919) 854-2056','wdk_facebook'=>'#','wdk_linkedin'=>'#','wdk_instagram'=>'#','wdk_telegram'=>'#','wdk_twitter'=>'#'));
1212
1213 foreach ($dom_array['listings']['listing'] as $listing_num => $listing) {
1214 $location_id = '';
1215 $category_id = '';
1216
1217 if(!empty($categories))
1218 $category_id = $categories[rand(0, count($categories)-1)];
1219
1220 if(!empty($location))
1221 $location_id = $locations[rand(0, count($locations)-1)];
1222
1223 $data_listing = array(
1224 'post_title' => '',
1225 'post_content' => '',
1226 'category_id' => $category_id,
1227 'location_id' => $location_id,
1228 'address' => '',
1229 'lat' => '',
1230 'listing_images' => '',
1231 'listing_images_path' => '',
1232 'listing_images_path_medium' => '',
1233 'date_modified' => date('Y-m-d H:i:s'),
1234 'lng' => '',
1235 'is_featured' => '',
1236 'is_activated' => '1',
1237 'is_approved' => '1',
1238 'user_id_editor' => NULL,
1239 );
1240
1241 $data_listing = array_intersect_key(array_merge($data_listing, $listing), $data_listing);
1242 array_walk($data_listing, function(&$item){$item = (is_array($item)) ? '' : trim(wmvc_xss_clean($item));});
1243
1244 if(empty(wmvc_show_data('post_title', $data_listing))) {
1245 continue;
1246 }
1247
1248 /*fields */
1249 foreach($fields as $field_data) {
1250 if(isset($listing['field_'.$field_data->idfield])) {
1251 $data_listing['field_'.$field_data->idfield] = $listing['field_'.$field_data->idfield];
1252 } else {
1253 if($field_data->field_type == 'DROPDOWN' || $field_data->field_type == 'DROPDOWN_MULTIPLE')
1254 {
1255 if(empty($field_data->values_list)) continue;
1256 $values = explode(',', $field_data->values_list);
1257 if(count($values) > 0)
1258 {
1259 $start=0;
1260 if(empty($values[0]))$start=1;
1261 $data_listing['field_'.$field_data->idfield] = $values[rand($start, count($values)-1)];
1262 }
1263 }
1264 elseif($field_data->field_type == 'NUMBER')
1265 {
1266 $data_listing['field_'.$field_data->idfield] = rand(2,499);
1267
1268 }
1269 elseif($field_data->field_type == 'CHECKBOX')
1270 {
1271 $data_listing['field_'.$field_data->idfield] = rand(0,1);
1272 }
1273 elseif($field_data->field_type == 'TEXTAREA')
1274 {
1275 //$data_listing['field_'.$field_data->idfield] = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.';
1276 }
1277 }
1278 }
1279
1280 /* add files */
1281 $count_images = 2;
1282 if($purpose == 'car-dealer.xml') {
1283 $count_images = 1;
1284 }
1285
1286 for ($i=0,$image_num=$listing_num; $i<$count_images; $i++,$image_num++) {
1287 if($image_num >= count($files))
1288 $image_num = 0;
1289
1290 if(!empty($data_listing['listing_images']))
1291 $data_listing['listing_images'].= ',';
1292
1293 $image_id = wmvc_add_wp_image(WPDIRECTORYKIT_PATH.'demo-data/images/'.esc_html($files[$image_num]));
1294 $data_listing['listing_images'] .= $image_id;
1295
1296 $image_path = wp_get_original_image_path( $image_id);
1297
1298 if($image_path) {
1299 /* path of image */
1300 $next_path = str_replace(WP_CONTENT_DIR . '/uploads/','', $image_path);
1301
1302 /* check length listing_images_path + next image + comma, should be less 200*/
1303 if(strlen($data_listing['listing_images_path'].$next_path)<195) {
1304 if(!empty($data_listing['listing_images_path']))
1305 $data_listing['listing_images_path'] .= ',';
1306
1307 $data_listing['listing_images_path'] .= $next_path;
1308 }
1309 }
1310
1311 $image_url = wp_get_attachment_image_url($image_id, 'large');
1312 if($image_url) {
1313 $parsed = parse_url($image_url);
1314 $next_path = substr($parsed['path'], strpos($parsed['path'], 'uploads/')+8);
1315
1316 if(!empty($data_listing['listing_images_path_medium']))
1317 $data_listing['listing_images_path_medium'] .= ',';
1318
1319 $data_listing['listing_images_path_medium'] .= $next_path;
1320 }
1321 }
1322
1323 // Create post object
1324 $listing_post = array(
1325 'ID' => NULL,
1326 'post_type' => 'wdk-listing',
1327 'post_title' => wp_strip_all_tags( $data_listing['post_title'] ),
1328 'post_content' => $data_listing['post_content'],
1329 'post_status' => 'publish',
1330 'post_author' => get_current_user_id()
1331 );
1332 // Insert the post into the database
1333 $id = wp_insert_post( $listing_post );
1334
1335 $listing_data = array('post_id' => $id);
1336 $listing_data_fields = array('category_id', 'location_id', 'address', 'lat', 'lng', 'listing_images', 'is_featured', 'is_activated','is_approved','listing_images_path', 'listing_images_path_medium');
1337 foreach($listing_data_fields as $field_name)
1338 {
1339 if(isset( $data_listing[$field_name]))
1340 $listing_data[$field_name] = $data_listing[$field_name];
1341 }
1342 $listing_data['user_id_editor'] = $user_ids[array_rand($user_ids)];
1343
1344 $this->listing_m->insert($listing_data, NULL);
1345
1346 $data_listing['post_id'] = $id;
1347 $this->listingfield_m->insert_custom_fields($fields, $data_listing, NULL);
1348 }
1349 $this->data['import_log'] .= '<div class="alert alert-success" role="alert">'.esc_html__('Listings imported').'</div>';
1350 return true;
1351 }
1352
1353 $this->data['import_log'] .= '<div class="alert alert-danger" role="alert">'.esc_html__('Missing xml file '.$xml_list, 'wpdirectorykit').'</div>';
1354 return false;
1355 }
1356
1357 protected function xmlstr_to_array($xmlstr) {
1358 $doc = new DOMDocument();
1359
1360 if ( !@$doc->loadXML($xmlstr) ) {
1361 return false;
1362 }
1363
1364 $root = $doc->documentElement;
1365 $output = $this->domnode_to_array($root);
1366 $output['@root'] = $root->tagName;
1367 return $output;
1368 }
1369
1370 protected function domnode_to_array($node) {
1371 $output = array();
1372 switch ($node->nodeType) {
1373 case XML_CDATA_SECTION_NODE:
1374 case XML_TEXT_NODE:
1375 $output = trim($node->textContent);
1376 break;
1377 case XML_ELEMENT_NODE:
1378 for ($i=0, $m=$node->childNodes->length; $i<$m; $i++) {
1379 $child = $node->childNodes->item($i);
1380 $v = $this->domnode_to_array($child);
1381 if(isset($child->tagName)) {
1382 $t = $child->tagName;
1383 if(!isset($output[$t])) {
1384 $output[$t] = array();
1385 }
1386 $output[$t][] = $v;
1387 }
1388 elseif($v || $v === '0') {
1389 $output = (string) $v;
1390 }
1391 }
1392 if($node->attributes->length && !is_array($output)) { //Has attributes but isn't an array
1393 $output = array('@content'=>$output); //Change output into an array.
1394 }
1395 if(is_array($output)) {
1396 if($node->attributes->length) {
1397 $a = array();
1398 foreach($node->attributes as $attrName => $attrNode) {
1399 $a[$attrName] = (string) $attrNode->value;
1400 }
1401 $output['@attributes'] = $a;
1402 }
1403 foreach ($output as $t => $v) {
1404 if(is_array($v) && count($v)==1 && $t!='@attributes') {
1405 $output[$t] = $v[0];
1406 }
1407 }
1408 }
1409 break;
1410 }
1411 return $output;
1412 }
1413
1414 public function run()
1415 {
1416 wdk_access_check('settings_m', 1);
1417
1418 // Check _wpnonce
1419 check_admin_referer( 'wdk-import-data-run', '_wpnonce' );
1420
1421
1422 ini_set('max_execution_time', 900);
1423 $this->data['import_log'] = '';
1424 $multipurpose = 'real-estate.xml';
1425 if(isset($_GET['multipurpose']))
1426 $multipurpose = sanitize_text_field($_GET['multipurpose']);
1427
1428 $redirect_url = admin_url("admin.php?page=wdk_fields&is_updated");
1429 if(isset($_GET['redirect_url']) && strpos($_GET['redirect_url'], 'http') === FALSE && strpos($_GET['redirect_url'], '//') === FALSE)
1430 $redirect_url = admin_url(sanitize_text_field($_GET['redirect_url']));
1431
1432 $this->import_locations();
1433 $this->import_categories($multipurpose);
1434 $this->import_fields($multipurpose);
1435 $this->import_listings($multipurpose);
1436 $this->import_visual_data($multipurpose);
1437 $this->demo_pages($multipurpose);
1438
1439 $message = str_replace(" ", '+', __('Import completed successfuly', 'wpdirectorykit'));
1440
1441 wp_redirect($redirect_url.'&message='.$message);
1442 exit;
1443 }
1444
1445 public function api_import()
1446 {
1447 wdk_access_check('settings_m', 1);
1448
1449 /* block url access */
1450 if(
1451 (isset($_GET['function']) && $_GET['function'] == 'api_import' ) ||
1452 (isset($_POST['function']) && $_POST['function'] == 'api_import' )
1453 ) {
1454 echo esc_html__('Security blocked', 'wpdirectorykit');
1455 exit();
1456 }
1457
1458 ini_set('max_execution_time', 900);
1459 $this->data['import_log'] = '';
1460 $multipurpose = 'real-estate.xml';
1461 if(isset($_GET['multipurpose']))
1462 $multipurpose = sanitize_text_field($_GET['multipurpose']);
1463
1464 $this->import_locations();
1465 $this->import_categories($multipurpose);
1466 $this->import_fields($multipurpose);
1467 $this->import_listings($multipurpose);
1468 $this->import_visual_data($multipurpose);
1469 $this->import_settings();
1470
1471 /* action */
1472 do_action('wpdirectorykit/install/api');
1473
1474 return true;
1475 }
1476
1477 public function _api_import()
1478 {
1479 wdk_access_check('settings_m', 1);
1480 ini_set('max_execution_time', 900);
1481 $this->data['import_log'] = '';
1482 $multipurpose = 'real-estate.xml';
1483 if(isset($_GET['multipurpose']))
1484 $multipurpose = sanitize_text_field($_GET['multipurpose']);
1485
1486 $this->import_locations();
1487 $this->import_categories($multipurpose);
1488 $this->import_fields($multipurpose);
1489 $this->import_listings($multipurpose);
1490 $this->import_visual_data($multipurpose);
1491 $this->import_settings();
1492
1493 /* action */
1494 do_action('wpdirectorykit/install/api');
1495
1496 return true;
1497 }
1498
1499 private function replace_meta($from = '', $to = '') {
1500 global $wpdb;
1501 // @codingStandardsIgnoreStart cannot use `$wpdb->prepare` because it remove's the backslashes
1502 $rows_affected = $wpdb->query(
1503 "UPDATE ".esc_sql($wpdb->postmeta)." " .
1504 "SET `meta_value` = REPLACE(`meta_value`, '" . str_replace( '/', '\\\/', esc_sql($from) ) . "', '" . str_replace( '/', '\\\/', esc_sql($to) ) . "') " .
1505 "WHERE `meta_key` = '_elementor_data' AND `meta_value` LIKE '[%' ;" );
1506 /* end login */
1507 }
1508
1509 private function import_settings() {
1510 wdk_access_check('settings_m', 1);
1511
1512 update_option( 'wdk_is_category_enabled', '1' );
1513 update_option( 'wdk_is_location_enabled', '1' );
1514 update_option( 'wdk_is_address_enabled', '1' );
1515 update_option( 'wdk_is_results_page_require', '1' );
1516 update_option( 'wdk_seo_description', '2' );
1517 update_option( 'wdk_seo_keywords', '3' );
1518 update_option( 'wdk_card_slider_enable', '1' );
1519 return true;
1520 }
1521
1522 }
1523