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

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