PluginProbe
WP Directory Kit / 1.5.9
WP Directory Kit v1.5.9
1.5.9 1.5.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 All 37 releases
wpdirectorykit / application / controllers / Wdk_backendajax.php

Wdk_backendajax.php in WP Directory Kit 1.5.9, at application/controllers/Wdk_backendajax.php

1,057 lines 38.4 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_backendajax extends Winter_MVC_Controller
5 {
6
7 public function __construct()
8 {
9 if (defined('WP_DEBUG') && WP_DEBUG) {
10 ini_set('display_errors', 1);
11 ini_set('display_startup_errors', 1);
12 error_reporting(-1);
13 }
14 parent::__construct();
15
16 $this->data['is_ajax'] = true;
17 }
18
19 public function index(&$output = NULL, $atts = array())
20 {
21 }
22
23
24 public function plugin_news()
25 {
26 $data = array();
27 $data['message'] = __('No message returned!', 'wpdirectorykit');
28 $data['success'] = false;
29 $data['response'] = NULL;
30 $data['rss'] = array();
31
32 /* protect */
33 if(!current_user_can( 'read' ) && !wmvc_user_in_role('administrator')) {
34 $this->output($data);
35 }
36
37 //https://wpdirectorykit.com/wp/last_news.php?f=news.json
38
39 $request = wp_remote_get('https://wpdirectorykit.com/wp/last_news.php?f=news.json');
40
41 // request failed
42 if (is_wp_error($request)) {
43 $data['response'] = $request;
44 }
45 $code = (int) wp_remote_retrieve_response_code($request);
46
47 // make sure the fetch was successful
48 if (empty($data['response']) && $code == 200) {
49 $response = wp_remote_retrieve_body($request);
50
51 // Decode the json
52 $output = json_decode($response);
53 $count = 0;
54 foreach ($output as $key => $value) {
55 $data['rss'][] = array(
56 'date' => wdk_get_date(wmvc_show_data('date', $value, gmdate('Y-m-d H:i:s'), TRUE, TRUE), false),
57 'title' => wmvc_show_data('title', $value, '', TRUE, TRUE),
58 'link' => wmvc_show_data('link', $value, '', TRUE, TRUE),
59 );
60 $count++;
61
62 if ($count > 10) break;
63 }
64 } else {
65 $data['response'] = get_status_header_desc($code);
66 }
67
68 $this->output($data);
69 }
70
71 public function plugin_upgrader($output = "", $atts = array(), $instance = NULL)
72 {
73 $data = array();
74 $data['message'] = __('No message returned!', 'wpdirectorykit');
75 $data['success'] = false;
76
77 /* protect */
78 if(!current_user_can( 'install_plugins' ) && !wmvc_user_in_role('administrator')) {
79 $data['message'] = __('Disabled for current user', 'wpdirectorykit');
80 $this->output($data);
81 }
82
83 // Check _wpnonce
84 check_admin_referer( 'wdk-plugin_upgrader', '_wpnonce' );
85
86 ob_start();
87
88 $parameters = array();
89
90 foreach ($_POST as $key => $value) {
91 $parameters[$key] = sanitize_text_field($value);
92 }
93
94 $source = 'https://downloads.wordpress.org/plugin/' . $parameters['slug'] . '.zip';
95
96 if (!empty($parameters['source']))
97 $source = $parameters['source'];
98
99 if (!class_exists('Plugin_Upgrader', false)) {
100 require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
101 }
102
103 $upgrader = new Plugin_Upgrader(new PluginInstallerSkinSilentWdk($skin_args));
104
105 if (!file_exists(WP_PLUGIN_DIR . '/' . $parameters['slug'] . '/' . $parameters['slug'] . '.php')) {
106 //exit(WP_PLUGIN_DIR .'/'.$parameters['slug'].'/'.$parameters['slug'].'.php');
107
108 $upgrader->install($source);
109 }
110
111 ob_clean();
112
113 $activate = activate_plugin($parameters['slug'] . '/' . $parameters['slug'] . '.php');
114
115 if (is_wp_error($activate)) {
116 $data['message'] = wp_kses_post($activate->get_error_message());
117 $data['success'] = false;
118 } else {
119 $data['success'] = true;
120 }
121
122 $data['slug'] = $parameters['slug'];
123
124 $data['parameters'] = $parameters;
125
126 //$data['sql'] = $this->db->last_query();
127 $this->output($data);
128 }
129
130 public function install_content($output = "", $atts = array(), $instance = NULL)
131 {
132 $data = array();
133 $data['message'] = __('No message returned!', 'wpdirectorykit');
134 $data['success'] = false;
135
136
137 /* protect */
138 if(!current_user_can( 'install_plugins' ) && !wmvc_user_in_role('administrator')) {
139 $data['message'] = __('Disabled for current user', 'wpdirectorykit');
140 $this->output($data);
141 }
142
143 // Check _wpnonce
144 check_admin_referer( 'wdk-install_content', '_wpnonce' );
145
146 ob_start();
147
148 $parameters = array();
149
150 foreach ($_POST as $key => $value) {
151 $parameters[$key] = sanitize_text_field($value);
152 }
153
154 if (!class_exists('\WP_Importer')) {
155 require ABSPATH . '/wp-admin/includes/class-wp-importer.php';
156 }
157
158 require_once WPDIRECTORYKIT_PATH . 'vendor/WordPress-Importer/class-logger.php';
159 require_once WPDIRECTORYKIT_PATH . 'vendor/WordPress-Importer/class-logger-html.php';
160 require_once WPDIRECTORYKIT_PATH . 'vendor/WordPress-Importer/class-logger-serversentevents.php';
161 require_once WPDIRECTORYKIT_PATH . 'vendor/WordPress-Importer/class-wxr-importer.php';
162 require_once WPDIRECTORYKIT_PATH . 'vendor/WordPress-Importer/class-wxr-import-info.php';
163
164 $importer_options = array(
165 'fetch_attachments' => true
166 );
167
168 $logger = new WP_Importer_Logger();
169
170 $importer = new WXR_Importer($importer_options);
171
172 $importer->set_logger($logger);
173
174 $current_theme = wp_get_theme();
175
176 $tmp_file = download_url($current_theme->get('AuthorURI') . '/demo_themes/' . $current_theme->get('TextDomain') . '.xml');
177
178 $results_importer = $importer->import($tmp_file);
179
180 ob_clean();
181
182 if (is_wp_error($results_importer)) {
183 $data['message'] = wp_kses_post($results_importer->get_error_message());
184 $data['success'] = false;
185 } else {
186 $data['success'] = true;
187 }
188
189 $data['parameters'] = $parameters;
190
191 //$data['sql'] = $this->db->last_query();
192 $this->output($data);
193 }
194
195 public function install_listings($output = "", $atts = array(), $instance = NULL)
196 {
197 $this->load->load_helper('listing');
198 $this->load->model('listing_m');
199 $this->load->model('listingfield_m');
200
201 $data = array();
202 $data['message'] = __('No message returned!', 'wpdirectorykit');
203 $data['success'] = false;
204
205 /* protect */
206 if(!current_user_can( 'install_plugins' ) && !wmvc_user_in_role('administrator')) {
207 $data['message'] = __('Disabled for current user', 'wpdirectorykit');
208 $this->output($data);
209 }
210
211 // Check _wpnonce
212 check_admin_referer( 'wdk-install_listings', '_wpnonce' );
213
214 ob_start();
215
216 $parameters = array();
217
218 foreach ($_POST as $key => $value) {
219 $parameters[$key] = sanitize_text_field($value);
220 }
221
222 $data['parameters'] = $parameters;
223
224 //$data['sql'] = $this->db->last_query();
225 $this->output($data);
226 }
227
228
229 public function generated_listings_images_path()
230 {
231 $data = array();
232 $data['message'] = '';
233 $data['popup_text_success'] = '';
234 $data['popup_text_error'] = '';
235 $data['parameters'] = $_POST;
236 $data['success'] = false;
237
238 /* protect */
239 if(!wmvc_user_in_role('administrator') && !current_user_can('wdk_listings_manage')) {
240 $data['message'] = __('Disabled for current user', 'wpdirectorykit');
241 $this->output($data);
242 }
243
244 // Check _wpnonce
245 check_admin_referer( 'wdk-generated_listings_images_path', '_wpnonce' );
246
247 $this->load->load_helper('listing');
248 $this->load->model('listing_m');
249 $listings = $this->listing_m->get();
250
251 foreach ($listings as $listing) {
252 $image_ids = explode(',', $listing->listing_images);
253 $listing_data = array('listing_images_path' => '', 'listing_images_path_medium' => '');
254 if (is_array($image_ids)) {
255 foreach ($image_ids as $image_id) {
256 if (is_numeric($image_id)) {
257 $image_path = wp_get_original_image_path($image_id);
258 if (!$image_path) continue;
259
260 /* path of image */
261 $next_path = str_replace(WP_CONTENT_DIR . '/uploads/', '', $image_path);
262
263 if (!empty($listing_data['listing_images_path']))
264 $listing_data['listing_images_path'] .= ',';
265
266 $listing_data['listing_images_path'] .= $next_path;
267 }
268
269 $image_url = wp_get_attachment_image_url($image_id, 'large');
270 if ($image_url) {
271 $parsed = wp_parse_url($image_url);
272 $next_path = substr($parsed['path'], strpos($parsed['path'], 'uploads/') + 8);
273
274 if (!empty($listing_data['listing_images_path_medium']))
275 $listing_data['listing_images_path_medium'] .= ',';
276
277 $listing_data['listing_images_path_medium'] .= $next_path;
278 }
279 }
280 }
281
282 $this->listing_m->insert($listing_data, $listing->post_id);
283 }
284
285 $this->load->model('location_m');
286 $locations = $this->location_m->get();
287 if ($locations) foreach ($locations as $location) {
288 $save_data = array(
289 "parent_id" => $location->parent_id,
290 );
291 $save_data['icon_path'] = wdk_generate_path_image($location->icon_id);
292 $save_data['image_path'] = wdk_generate_path_image($location->image_id);
293
294 $this->location_m->insert($save_data, $location->idlocation);
295 }
296
297 $this->load->model('category_m');
298 $categories = $this->category_m->get();
299 if ($categories) foreach ($categories as $category) {
300 $save_data = array(
301 "parent_id" => $category->parent_id,
302 );
303 $save_data['icon_path'] = wdk_generate_path_image($category->icon_id);
304 $save_data['image_path'] = wdk_generate_path_image($category->image_id);
305 $save_data['marker_image_path'] = wdk_generate_path_image($category->marker_image_id);
306
307 $this->category_m->insert($save_data, $category->idcategory);
308 }
309
310 $data['success'] = true;
311 $data['popup_text_success'] = __('images path of listings,locations,categories generated', 'wpdirectorykit');
312 $this->output($data);
313 }
314
315 public function optimization_listingfields_table()
316 {
317 $data = array();
318 $data['message'] = '';
319 $data['popup_text_success'] = '';
320 $data['popup_text_error'] = '';
321 $data['parameters'] = $_POST;
322 $data['success'] = false;
323
324 /* protect */
325 if(!wmvc_user_in_role('administrator') && !current_user_can('wdk_listings_manage')) {
326 $data['message'] = __('Disabled for current user', 'wpdirectorykit');
327 $this->output($data);
328 }
329
330 // Check _wpnonce
331 check_admin_referer( 'wdk-optimization_listingfields_table', '_wpnonce' );
332
333 $this->load->load_helper('listing');
334 $this->load->model('listingfield_m');
335 $this->load->model('field_m');
336 $listingfields = $this->listingfield_m->get_available_fields();
337
338 global $wpdb;
339
340 /* remove if missing */
341 foreach ($listingfields as $listingfield => $field_data) {
342 if (strpos($listingfield, 'field_') !== FALSE) {
343 list($prefix, $field_id, $field_type) = explode('_', $listingfield);
344
345 if (!wdk_field_option($field_id, 'field_type', false)) {
346 $wpdb->query("ALTER TABLE {$this->listingfield_m->_table_name} DROP COLUMN $listingfield;");
347 }
348 }
349 }
350
351 /* add if missing listingfield and change from varchar to TEXT */
352 $this->db->where(array('field_type !=' => 'SECTION'));
353 $fields = $this->field_m->get();
354
355 $listingfields = $this->listingfield_m->get_available_fields();
356 foreach ($fields as $field) {
357 $column_name = 'field_' . $field->idfield . '_' . $field->field_type;
358 if (!isset($listingfields[$column_name])) {
359 $this->listingfield_m->create_table_column(array('field_type' => $field->field_type), $field->idfield);
360 } else {
361
362 /* change from varchar to TEXT */
363 if (
364 stripos($listingfields[$column_name]->Type, 'VARCHAR') !== FALSE
365 && ($field->field_type == 'INPUTBOX' || $field->field_type == 'DROPDOWN' || $field->field_type == 'DROPDOWNMULTIPLE')
366 ) {
367 $wpdb->query("ALTER TABLE `{$this->listingfield_m->_table_name}` CHANGE `$column_name` `$column_name` TEXT NULL DEFAULT NULL;");
368 }
369 }
370 }
371
372 $data['success'] = true;
373 $data['popup_text_success'] = __('Table listingfields optimized', 'wpdirectorykit');
374 $this->output($data);
375 }
376
377 public function update_depend()
378 {
379 $data = array();
380 $data['message'] = __('No message returned!', 'wpdirectorykit');
381 $data['success'] = false;
382 $data['parameters'] = $_POST;
383
384 /* protect */
385 if(!wmvc_user_in_role('administrator') && !current_user_can('wdk_listings_manage')) {
386 $data['message'] = __('Disabled for current user', 'wpdirectorykit');
387 $this->output($data);
388 }
389
390 // Check _wpnonce
391 check_admin_referer( 'wdk-update_depend', '_wpnonce' );
392
393 $this->load->load_helper('listing');
394 $this->load->model('dependfields_m');
395
396 if (wmvc_show_data('field_id', $data['parameters'], false) && wmvc_show_data('main_field', $data['parameters'], false)) {
397
398 $hidden_fields = array();
399 foreach ($data['parameters'] as $key => $value) {
400 if (strpos($key, 'field_hide_') !== FALSE) {
401 $field_id = str_replace('field_hide_', '', $key);
402 if (is_intval($field_id)) {
403 $hidden_fields[] = $field_id;
404 }
405 }
406 }
407
408 $data_insert = array(
409 'main_field' => wmvc_show_data('main_field', $data['parameters'], false),
410 'field_id' => wmvc_show_data('field_id', $data['parameters'], false),
411 'hidden_fields_list' => join(',', $hidden_fields),
412 );
413
414 $this->dependfields_m->delete_where(array('field_id' => $data_insert['field_id'], 'main_field' => $data_insert['main_field']));
415 $this->dependfields_m->insert($data_insert);
416 }
417
418 $this->output($data);
419 }
420
421 public function depend_copy_on_subcategories()
422 {
423 $data = array();
424 $data['message'] = __('No message returned!', 'wpdirectorykit');
425 $data['success'] = false;
426 $data['parameters'] = $_POST;
427
428 /* protect */
429 if(!wmvc_user_in_role('administrator') && !current_user_can('wdk_listings_manage')) {
430 $data['message'] = __('Disabled for current user', 'wpdirectorykit');
431 $this->output($data);
432 }
433
434 // Check _wpnonce
435 check_admin_referer( 'wdk_depend_copy_on_subcategories', '_wpnonce' );
436
437 $this->load->load_helper('listing');
438 $this->load->model('dependfields_m');
439
440 if (wmvc_show_data('category_id', $data['parameters'], false)) {
441
442 $sub_categories_ids = wdk_category_get_all_childs(intval($data['parameters']['category_id']));
443
444 $depends_list = wdk_depend_get_hidden_fields(intval($data['parameters']['category_id']));
445
446
447 $data['sub_categories_ids'] = $sub_categories_ids;
448 $data['depends_list'] = $depends_list;
449
450 if($sub_categories_ids){
451 $this->dependfields_m->delete_where(array('field_id IN ('.join(',',$sub_categories_ids).')'=>NULL));
452 foreach ($sub_categories_ids as $key => $id) {
453 # code...
454 $data_insert = array(
455 'main_field' => 'categories',
456 'field_id' => $id,
457 'hidden_fields_list' => $depends_list,
458 );
459 $this->dependfields_m->insert($data_insert);
460 }
461 }
462 }
463
464 $this->output($data);
465 }
466
467 public function generated_strings()
468 {
469 $data = array();
470 $data['message'] = '';
471 $data['popup_text_success'] = '';
472 $data['popup_text_error'] = '';
473 $data['parameters'] = $_POST;
474 $data['success'] = false;
475
476 /* protect */
477 if(!wmvc_user_in_role('administrator') && !current_user_can('wdk_listings_manage')) {
478 $data['message'] = __('Disabled for current user', 'wpdirectorykit');
479 $this->output($data);
480 }
481
482 // Check _wpnonce
483 check_admin_referer( 'wdk-backendajax', '_wpnonce' );
484
485 global $wp_filesystem;
486 // Initialize the WP filesystem, no more using 'file-put-contents' function
487 if (empty($wp_filesystem)) {
488 require_once(ABSPATH . '/wp-admin/includes/file.php');
489 WP_Filesystem();
490 }
491 // @codingStandardsIgnoreEnd
492
493 $this->load->load_helper('listing');
494 $this->load->model('location_m');
495 $this->load->model('category_m');
496
497
498 /* locations strings */
499 $this->db->select('location_title');
500 $this->db->from($this->location_m->_table_name);
501 $query = $this->db->get();
502 $count_file = 1;
503 $limit_size = '80000';
504 $file_strings_content = '<?php ';
505 if ($this->db->num_rows() > 0) {
506 foreach ($this->db->results() as $location) {
507 $file_strings_content .= '__(\'' . addslashes($location->location_title) . '\',\'wpdirectorykit\');' . "\n";
508
509 if (strlen($file_strings_content) > $limit_size) {
510 $file_strings_content .= "\n" . '?>';
511 $wp_filesystem->put_contents(WPDIRECTORYKIT_PATH . 'translation_strings/locations_' . $count_file . '.php', $file_strings_content);
512
513 /* create new file */
514 $file_strings_content = '<?php ';
515 $count_file++;
516 }
517 }
518 $file_strings_content .= "\n" . '?>';
519
520 if (!empty($file_strings_content))
521 $wp_filesystem->put_contents(WPDIRECTORYKIT_PATH . 'translation_strings/locations_' . $count_file . '.php', $file_strings_content);
522 }
523
524
525 /* categories strings */
526 $this->db->select('category_title');
527 $this->db->from($this->category_m->_table_name);
528 $query = $this->db->get();
529 $count_file = 1;
530 $limit_size = '80000';
531 $file_strings_content = '<?php ';
532 if ($this->db->num_rows() > 0) {
533 foreach ($this->db->results() as $category) {
534 $file_strings_content .= '__(\'' . addslashes($category->category_title) . '\',\'wpdirectorykit\');' . "\n";
535
536 if (strlen($file_strings_content) > $limit_size) {
537 $file_strings_content .= "\n" . '?>';
538 $wp_filesystem->put_contents(WPDIRECTORYKIT_PATH . 'translation_strings/categories_' . $count_file . '.php', $file_strings_content);
539
540 /* create new file */
541 $file_strings_content = '<?php ';
542 $count_file++;
543 }
544 }
545 $file_strings_content .= "\n" . '?>';
546
547 if (!empty($file_strings_content))
548 $wp_filesystem->put_contents(WPDIRECTORYKIT_PATH . 'translation_strings/categories_' . $count_file . '.php', $file_strings_content);
549 }
550
551 /* fields */
552
553 $this->load->model('field_m');
554 $this->db->select('field_label,prefix,suffix,values_list,hint,placeholder');
555 $this->db->from($this->field_m->_table_name);
556 $query = $this->db->get();
557 $count_file = 1;
558 $limit_size = '70000';
559 $file_strings_content = '<?php ';
560 if ($this->db->num_rows() > 0) {
561 foreach ($this->db->results() as $field) {
562
563 if (!empty($field->field_label))
564 $file_strings_content .= '__(\'' . addslashes($field->field_label) . '\',\'wpdirectorykit\');' . "\n";
565
566 if (!empty($field->prefix))
567 $file_strings_content .= '__(\'' . addslashes($field->prefix) . '\',\'wpdirectorykit\');' . "\n";
568
569 if (!empty($field->suffix))
570 $file_strings_content .= '__(\'' . addslashes($field->suffix) . '\',\'wpdirectorykit\');' . "\n";
571
572 if (!empty($field->values_list))
573 $file_strings_content .= '__(\'' . addslashes($field->values_list) . '\',\'wpdirectorykit\');' . "\n";
574
575 if (!empty($field->placeholder))
576 $file_strings_content .= '__(\'' . addslashes($field->placeholder) . '\',\'wpdirectorykit\');' . "\n";
577
578 if (strlen($file_strings_content) > $limit_size) {
579 $file_strings_content .= "\n" . '?>';
580 $wp_filesystem->put_contents(WPDIRECTORYKIT_PATH . 'translation_strings/fields_' . $count_file . '.php', $file_strings_content);
581
582 /* create new file */
583 $file_strings_content = '<?php ';
584 $count_file++;
585 }
586 }
587 $file_strings_content .= "\n" . '?>';
588
589 if (!empty($file_strings_content))
590 $wp_filesystem->put_contents(WPDIRECTORYKIT_PATH . 'translation_strings/fields_' . $count_file . '.php', $file_strings_content);
591 }
592
593
594 $data['success'] = true;
595 $data['popup_text_success'] = __('Strings generated', 'wpdirectorykit');
596 $this->output($data);
597 }
598
599 public function optimization_db_fields()
600 {
601 $data = array();
602 $data['message'] = '';
603 $data['popup_text_success'] = '';
604 $data['popup_text_error'] = '';
605 $data['parameters'] = $_POST;
606 $data['success'] = false;
607
608 /* protect */
609 if(!wmvc_user_in_role('administrator') && !current_user_can('wdk_listings_manage')) {
610 $data['message'] = __('Disabled for current user', 'wpdirectorykit');
611 $this->output($data);
612 }
613
614 // Check _wpnonce
615 check_admin_referer( 'wdk-backendajax', '_wpnonce' );
616
617 $this->load->load_helper('listing');
618 $this->load->model('field_m');
619 $this->load->model('listingfield_m');
620
621 global $wpdb;
622
623 $existing_listing_fields = $this->listingfield_m->get_available_fields();
624 unset($existing_listing_fields['idlistings_fields'],$existing_listing_fields['post_id'],$existing_listing_fields['lang_code']);
625
626 $fields = $this->field_m->get();
627 foreach ($fields as $key => $field) {
628 // add exits fields
629 $field_col = 'field_'.$field->idfield.'_'.$field->field_type;
630 if(isset($existing_listing_fields[$field_col])) {
631 unset($existing_listing_fields[$field_col]);
632 }
633 }
634 $table_listing_fields = $wpdb->prefix . 'wdk_listings_fields';
635 if(!empty($existing_listing_fields)) foreach ($existing_listing_fields as $field_listing_col => $field_listing) {
636 if(substr($field_listing_col, 0, 6) == 'field_') {
637 $wpdb->query( $wpdb->prepare( "ALTER TABLE `{$table_listing_fields}` DROP `$field_listing_col`;" ) );
638 }
639 }
640
641
642 /* fields */
643 $data['success'] = true;
644 $data['popup_text_success'] = __('Fields optimization', 'wpdirectorykit');
645 $this->output($data);
646 }
647
648 public function loading_sublistings()
649 {
650 $data = array();
651 $data['message'] = '';
652 $data['popup_text_success'] = '';
653 $data['popup_text_error'] = '';
654 $data['parameters'] = $_POST;
655 $data['success'] = false;
656 $data['results'] = array();
657
658 /* protect */
659 if(!current_user_can('edit_own_listings') && !wmvc_user_in_role('administrator')) {
660 $data['message'] = __('Disabled for current user', 'wpdirectorykit');
661 $this->output($data);
662 }
663
664 // Check _wpnonce
665 check_admin_referer( 'wdk-backendajax', '_wpnonce' );
666
667 $listing_id = intval(wmvc_show_data('listing_id', $data['parameters']));
668
669 if (empty($listing_id)) {
670 $data['popup_text_error'] = __('Missing Listing', 'wpdirectorykit');
671 }
672
673 if (empty($data['popup_text_error'])) {
674
675 $this->load->load_helper('listing');
676 $this->load->model('listing_m');
677
678
679 $this->db->where($this->db->prefix . 'wdk_listings.post_id IN(' . wdk_field_value('listing_related_ids', $listing_id) . ')', null, false);
680 $this->db->order_by('FIELD(' . $this->db->prefix . 'wdk_listings.post_id, ' . wdk_field_value('listing_related_ids', $listing_id) . ')');
681
682 $results = $this->listing_m->get();
683
684 foreach ($results as $sublisting) {
685 $_listing = array();
686 $_listing['post_id'] = wdk_field_value('post_id', $sublisting);
687 $_listing['post_title'] = wdk_field_value('post_title', $sublisting);
688 $_listing['post_content_short'] = wmvc_character_limiter(wdk_field_value('post_content', $sublisting), 30);
689 $_listing['date'] = wdk_get_date(wdk_field_value('date', $sublisting), false);
690 $_listing['location'] = wdk_field_value('location_title', $sublisting);
691 $_listing['category'] = wdk_field_value('category_title', $sublisting);
692 $_listing['counter_views'] = wdk_field_value('counter_views', $sublisting);
693 $_listing['counter_results_views'] = wdk_field_value('counter_results_views', $sublisting);
694 $_listing['image_src'] = wdk_image_src($sublisting);
695 $_listing['listing_remove_url'] = esc_url(get_admin_url() . "admin.php?page=wdk&function=delete&id=" . wdk_field_value('post_id', $sublisting));
696 $_listing['listing_view_url'] = get_permalink($sublisting);
697 $_listing['listing_edit_url'] = esc_url(get_admin_url() . "admin.php?page=wdk_listing&id=" . wdk_field_value('post_id', $sublisting));
698 $_listing['listing_dash_remove_url'] = '';
699 $_listing['listing_dash_edit_url'] = '';
700 if(function_exists(' wdk_dash_url')) {
701 $_listing['listing_dash_remove_url'] = wdk_dash_url("dash_page=listings&table_action=table&action=delete&ids=" . wdk_field_value('post_id', $sublisting));
702 $_listing['listing_dash_edit_url'] = wdk_dash_url("dash_page=listings&function=edit&id=" . wdk_field_value('post_id', $sublisting));
703 }
704
705 $data['results'][] = $_listing;
706
707 }
708 }
709
710 $data['success'] = true;
711 $this->output($data);
712 }
713
714 public function remove_listing()
715 {
716 $data = array();
717 $data['message'] = '';
718 $data['popup_text_success'] = '';
719 $data['popup_text_error'] = '';
720 $data['parameters'] = $_POST;
721 $data['success'] = false;
722 $data['results'] = array();
723
724 /* protect */
725 if(!current_user_can('edit_own_listings') && !wmvc_user_in_role('administrator')) {
726 $data['message'] = __('Disabled for current user', 'wpdirectorykit');
727 $this->output($data);
728 }
729
730 // Check _wpnonce
731 check_admin_referer( 'wdk-backendajax', '_wpnonce' );
732
733 $listing_id = intval(wmvc_show_data('listing_id', $data['parameters']));
734
735 if (empty($listing_id)) {
736 $data['popup_text_error'] = __('Missing Listing', 'wpdirectorykit');
737 }
738
739 if (empty($data['popup_text_error'])) {
740
741 $this->load->load_helper('listing');
742 $this->load->model('listing_m');
743
744 $listing = $this->listing_m->get($listing_id, TRUE);
745 $parent_listing = $this->listing_m->get($listing->listing_parent_post_id, TRUE);
746
747 $this->listing_m->delete($listing_id);
748
749 if(isset($parent_listing->listing_related_ids) && !empty($parent_listing->listing_related_ids)) {
750 $related_ids = trim(str_replace(','.$listing_id.'','', ','.$parent_listing->listing_related_ids.','),',');
751 $this->listing_m->insert(array('listing_related_ids' => $related_ids), $listing->listing_parent_post_id);
752 }
753
754 $data['success'] = true;
755 }
756
757
758 $this->output($data);
759 }
760
761 public function reinstall_page()
762 {
763 $data = array();
764 $data['message'] = '';
765 $data['popup_text_success'] = '';
766 $data['popup_text_error'] = '';
767 $data['parameters'] = $_POST;
768 $data['success'] = false;
769 $data['page_id'] = false;
770 $data['page_title'] = false;
771 $data['results'] = array();
772 /* protect */
773 if(!wmvc_user_in_role('administrator')) {
774 $data['message'] = __('Disabled for current user', 'wpdirectorykit');
775 $this->output($data);
776 }
777
778 // Check _wpnonce
779 check_admin_referer( 'wdk-backendajax', '_wpnonce' );
780
781 $page = sanitize_text_field(wmvc_show_data('arg', $data['parameters']));
782
783
784
785 // Import elementor templates
786 switch($page) {
787 case 'wdk_listing_page':
788 $new_page = $this->create_page(esc_html__('Listing Preview', 'wpdirectorykit'), '', 'elementor_canvas');
789 $this->elementor_assign($new_page->ID, 'page-listing-preview.json');
790 update_option('wdk_listing_page', $new_page->ID);
791 $data['page_title'] = esc_html__('Listing Preview', 'wpdirectorykit');
792 break;
793 case 'wdk_results_page':
794 $new_page = $this->create_page(esc_html__('Results Listings', 'wpdirectorykit'), '', 'elementor_canvas');
795 $this->elementor_assign($new_page->ID, 'page-results-listings.json');
796 update_option('wdk_results_page', $new_page->ID);
797 $data['page_title'] = esc_html__('Results Listings', 'wpdirectorykit');
798 break;
799 }
800
801 if (empty($new_page)) {
802 $data['popup_text_error'] = __('Page not Created', 'wpdirectorykit');
803 } else {
804 $data['page_id'] = $new_page->ID;
805 }
806
807 if (empty($data['popup_text_error'])) {
808 $data['success'] = true;
809 }
810
811 $this->output($data);
812 }
813
814 public function wdk_save_ai_search_fields()
815 {
816 $data = array();
817 $data['message'] = '';
818 $data['popup_text_success'] = '';
819 $data['popup_text_error'] = '';
820 $data['parameters'] = $_POST;
821 $data['success'] = false;
822 $data['page_id'] = false;
823 $data['page_title'] = false;
824 $data['results'] = array();
825
826 /* protect */
827 if(!wmvc_user_in_role('administrator')) {
828 $data['message'] = __('Disabled for current user', 'wpdirectorykit');
829 $this->output($data);
830 }
831
832 // Check _wpnonce
833 check_admin_referer( 'wdk-backendajax', '_wpnonce' );
834
835 $fields = isset($_POST['fields'])
836 ? array_filter(
837 array_map('intval', (array) wp_unslash($_POST['fields'])),
838 function($val) { return is_int($val) && $val > 0; }
839 )
840 : [];
841
842
843 update_option(
844 'wdk_ai_search_fields',
845 $fields
846 );
847
848 $data['popup_text_success'] = __('AI Search fields updated successfully.', 'wpdirectorykit');
849
850 if (empty($data['popup_text_error'])) {
851 $data['success'] = true;
852 }
853
854 $this->output($data);
855 }
856
857 public function upload_image()
858 {
859 $data = array();
860 $data['message'] = '';
861 $data['popup_text_success'] = '';
862 $data['popup_text_error'] = '';
863 $data['parameters'] = $_POST;
864 $data['success'] = false;
865 $data['page_id'] = false;
866 $data['page_title'] = false;
867 $data['results'] = array();
868 /* protect */
869 if(!wmvc_user_in_role('administrator')) {
870 $data['message'] = __('Disabled for current user', 'wpdirectorykit');
871 $this->output($data);
872 }
873
874 // Check _wpnonce
875 check_admin_referer( 'wdk-backendajax', '_wpnonce' );
876
877 if(!empty($data['parameters']['image']))
878 foreach (explode(',', sanitize_text_field($_POST['image'])) as $key => $image_or_src) {
879 if(empty($image_or_src)) continue;
880 $image_id = wmvc_add_wp_image($image_or_src);
881 $image_url = wp_get_attachment_image_url($image_id, 'large');
882
883 $data['results'][] = [
884 'image_id'=> $image_id,
885 'image_url'=> $image_url,
886 ];
887 }
888
889
890 if (empty($data['popup_text_error'])) {
891 $data['success'] = true;
892 }
893
894 $this->output($data);
895 }
896
897
898 /* Create Page */
899 private function create_page($post_title, $post_content = '', $post_template = NULL, $post_parent=0, $rebuild = FALSE)
900 {
901
902 $post_id = NULL;
903
904 // Delete posts and rebuild
905 if($rebuild) {
906
907 $post = wdk_page_by_title($post_title, 'OBJECT', 'page' );
908 if(!empty($post))
909 {
910 wp_delete_post($post->ID, true);
911 $post=NULL;
912 }
913 if(!empty($post))
914 $post_id = $post->ID;
915 }
916
917
918 if(empty($post_id))
919 {
920 $error_obj = NULL;
921 $post_insert = array(
922 'post_title' => wp_strip_all_tags( $post_title ),
923 'post_content' => $post_content,
924 'post_status' => 'publish',
925 'post_type' => 'page',
926 'post_author' => get_current_user_id(),
927 'post_category' => array(1,2),
928 'page_template' => $post_template,
929 'post_parent' => $post_parent
930 );
931 $post_id = wp_insert_post( $post_insert, $error_obj );
932 }
933
934 $post_insert = get_post( $post_id );
935
936 return $post_insert;
937 }
938
939 /* Elementor Import Template */
940 private function elementor_assign($page_id, $json_template_name = '')
941 {
942
943 $file = false;
944
945 if(is_child_theme() && file_exists(get_stylesheet_directory().'/elementor-data/wpdirectorykit/'.$json_template_name))
946 {
947 $file = get_stylesheet_directory().'/elementor-data/wpdirectorykit/'.$json_template_name;
948 }
949 elseif(file_exists(get_template_directory().'/demo-data/wpdirectorykit/'.$json_template_name))
950 {
951 $file = get_template_directory().'/demo-data/wpdirectorykit/'.$json_template_name;
952 }
953 elseif(file_exists( WPDIRECTORYKIT_PATH. '/demo-data/'.$json_template_name))
954 {
955 $file = WPDIRECTORYKIT_PATH.'demo-data/'.$json_template_name;
956 }
957
958 if(!$file || !class_exists('Elementor\Plugin'))
959 {
960 return false;
961 }
962
963 $page_template = get_page_template_slug( $page_id );
964
965 add_post_meta( $page_id, '_elementor_edit_mode', 'builder' );
966
967 global $wp_filesystem;
968 // Initialize the WP filesystem, no more using 'file-put-contents' function
969 if (empty($wp_filesystem)) {
970 WP_Filesystem();
971 }
972
973 $string = $wp_filesystem->get_contents($file);
974
975 $json_template = json_decode($string, true);
976 $elements = $json_template['content'];
977
978 $data = array(
979 'elements' => $elements,
980 'settings' => array('post_status'=>'autosave', 'template'=>$page_template),
981 );
982 // @codingStandardsIgnoreStart
983 $document = Elementor\Plugin::$instance->documents->get( $page_id, false );
984 // @codingStandardsIgnoreEnd
985 return $document->save( $data );
986 }
987
988
989
990 private function output($data, $print = TRUE)
991 {
992 if ($print) {
993 header('Pragma: no-cache');
994 header('Cache-Control: no-store, no-cache');
995 header('Content-Type: application/json; charset=utf8');
996 //header('Content-Length: '.$length); // special characters causing troubles
997 echo (wp_json_encode($data));
998 exit();
999 } else {
1000 return $data;
1001 }
1002 }
1003 }
1004
1005 if (!class_exists('\Plugin_Upgrader', false)) {
1006 require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
1007 }
1008
1009 /**
1010 * WordPress class extended for on-the-fly plugin installations.
1011 */
1012 class PluginInstallerSkinSilentWdk extends \WP_Upgrader_Skin
1013 {
1014
1015 /**
1016 * Empty out the header of its HTML content.
1017 */
1018 public function header()
1019 {
1020 }
1021
1022 /**
1023 * Empty out the footer of its HTML content.
1024 */
1025 public function footer()
1026 {
1027 }
1028
1029 /**
1030 * Empty out the footer of its HTML content.
1031 *
1032 * @param string $string
1033 * @param mixed ...$args Optional text replacements.
1034 */
1035 public function feedback($string, ...$args)
1036 {
1037 }
1038
1039 /**
1040 * Empty out JavaScript output that calls function to decrement the update counts.
1041 *
1042 * @param string $type Type of update count to decrement.
1043 */
1044 public function decrement_update_count($type)
1045 {
1046 }
1047
1048 /**
1049 * Empty out the error HTML content.
1050 *
1051 * @param string|WP_Error $errors A string or WP_Error object of the install error/s.
1052 */
1053 public function error($errors)
1054 {
1055 }
1056 }
1057