PluginProbe
WP Directory Kit / 1.2.3
WP Directory Kit v1.2.3
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.3, at application/controllers/Wdk_settings.php

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