PluginProbe
WP Directory Kit / 1.4.8
WP Directory Kit v1.4.8
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_backendajax.php

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

1,017 lines 37.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if (!defined('ABSPATH')) exit; // Exit if accessed directly;
3
4 class Wdk_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, date('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 = 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 $sql = "ALTER TABLE `{$table_listing_fields}`
638 DROP `$field_listing_col`;";
639
640 $wpdb->query( $sql );
641 }
642 }
643
644
645 /* fields */
646 $data['success'] = true;
647 $data['popup_text_success'] = __('Fields optimization', 'wpdirectorykit');
648 $this->output($data);
649 }
650
651 public function loading_sublistings()
652 {
653 $data = array();
654 $data['message'] = '';
655 $data['popup_text_success'] = '';
656 $data['popup_text_error'] = '';
657 $data['parameters'] = $_POST;
658 $data['success'] = false;
659 $data['results'] = array();
660
661 /* protect */
662 if(!current_user_can('edit_own_listings') && !wmvc_user_in_role('administrator')) {
663 $data['message'] = __('Disabled for current user', 'wpdirectorykit');
664 $this->output($data);
665 }
666
667 // Check _wpnonce
668 check_admin_referer( 'wdk-backendajax', '_wpnonce' );
669
670 $listing_id = intval(wmvc_show_data('listing_id', $data['parameters']));
671
672 if (empty($listing_id)) {
673 $data['popup_text_error'] = __('Missing Listing', 'wpdirectorykit');
674 }
675
676 if (empty($data['popup_text_error'])) {
677
678 $this->load->load_helper('listing');
679 $this->load->model('listing_m');
680
681
682 $this->db->where($this->db->prefix . 'wdk_listings.post_id IN(' . wdk_field_value('listing_related_ids', $listing_id) . ')', null, false);
683 $this->db->order_by('FIELD(' . $this->db->prefix . 'wdk_listings.post_id, ' . wdk_field_value('listing_related_ids', $listing_id) . ')');
684
685 $results = $this->listing_m->get();
686
687 foreach ($results as $sublisting) {
688 $_listing = array();
689 $_listing['post_id'] = wdk_field_value('post_id', $sublisting);
690 $_listing['post_title'] = wdk_field_value('post_title', $sublisting);
691 $_listing['post_content_short'] = wmvc_character_limiter(wdk_field_value('post_content', $sublisting), 30);
692 $_listing['date'] = wdk_get_date(wdk_field_value('date', $sublisting), false);
693 $_listing['location'] = wdk_field_value('location_title', $sublisting);
694 $_listing['category'] = wdk_field_value('category_title', $sublisting);
695 $_listing['counter_views'] = wdk_field_value('counter_views', $sublisting);
696 $_listing['counter_results_views'] = wdk_field_value('counter_results_views', $sublisting);
697 $_listing['image_src'] = wdk_image_src($sublisting);
698 $_listing['listing_remove_url'] = esc_url(get_admin_url() . "admin.php?page=wdk&function=delete&id=" . wdk_field_value('post_id', $sublisting));
699 $_listing['listing_view_url'] = get_permalink($sublisting);
700 $_listing['listing_edit_url'] = esc_url(get_admin_url() . "admin.php?page=wdk_listing&id=" . wdk_field_value('post_id', $sublisting));
701 $_listing['listing_dash_remove_url'] = '';
702 $_listing['listing_dash_edit_url'] = '';
703 if(function_exists(' wdk_dash_url')) {
704 $_listing['listing_dash_remove_url'] = wdk_dash_url("dash_page=listings&table_action=table&action=delete&ids=" . wdk_field_value('post_id', $sublisting));
705 $_listing['listing_dash_edit_url'] = wdk_dash_url("dash_page=listings&function=edit&id=" . wdk_field_value('post_id', $sublisting));
706 }
707
708 $data['results'][] = $_listing;
709
710 }
711 }
712
713 $data['success'] = true;
714 $this->output($data);
715 }
716
717 public function remove_listing()
718 {
719 $data = array();
720 $data['message'] = '';
721 $data['popup_text_success'] = '';
722 $data['popup_text_error'] = '';
723 $data['parameters'] = $_POST;
724 $data['success'] = false;
725 $data['results'] = array();
726
727 /* protect */
728 if(!current_user_can('edit_own_listings') && !wmvc_user_in_role('administrator')) {
729 $data['message'] = __('Disabled for current user', 'wpdirectorykit');
730 $this->output($data);
731 }
732
733 // Check _wpnonce
734 check_admin_referer( 'wdk-backendajax', '_wpnonce' );
735
736 $listing_id = intval(wmvc_show_data('listing_id', $data['parameters']));
737
738 if (empty($listing_id)) {
739 $data['popup_text_error'] = __('Missing Listing', 'wpdirectorykit');
740 }
741
742 if (empty($data['popup_text_error'])) {
743
744 $this->load->load_helper('listing');
745 $this->load->model('listing_m');
746
747 $listing = $this->listing_m->get($listing_id, TRUE);
748 $parent_listing = $this->listing_m->get($listing->listing_parent_post_id, TRUE);
749
750 $this->listing_m->delete($listing_id);
751
752 if(isset($parent_listing->listing_related_ids) && !empty($parent_listing->listing_related_ids)) {
753 $related_ids = trim(str_replace(','.$listing_id.'','', ','.$parent_listing->listing_related_ids.','),',');
754 $this->listing_m->insert(array('listing_related_ids' => $related_ids), $listing->listing_parent_post_id);
755 }
756
757 $data['success'] = true;
758 }
759
760
761 $this->output($data);
762 }
763
764 public function reinstall_page()
765 {
766 $data = array();
767 $data['message'] = '';
768 $data['popup_text_success'] = '';
769 $data['popup_text_error'] = '';
770 $data['parameters'] = $_POST;
771 $data['success'] = false;
772 $data['page_id'] = false;
773 $data['page_title'] = false;
774 $data['results'] = array();
775 /* protect */
776 if(!wmvc_user_in_role('administrator')) {
777 $data['message'] = __('Disabled for current user', 'wpdirectorykit');
778 $this->output($data);
779 }
780
781 // Check _wpnonce
782 check_admin_referer( 'wdk-backendajax', '_wpnonce' );
783
784 $page = sanitize_text_field(wmvc_show_data('arg', $data['parameters']));
785
786
787
788 // Import elementor templates
789 switch($page) {
790 case 'wdk_listing_page':
791 $new_page = $this->create_page(esc_html__('Listing Preview', 'wpdirectorykit'), '', 'elementor_canvas');
792 $this->elementor_assign($new_page->ID, 'page-listing-preview.json');
793 update_option('wdk_listing_page', $new_page->ID);
794 $data['page_title'] = esc_html__('Listing Preview', 'wpdirectorykit');
795 break;
796 case 'wdk_results_page':
797 $new_page = $this->create_page(esc_html__('Results Listings', 'wpdirectorykit'), '', 'elementor_canvas');
798 $this->elementor_assign($new_page->ID, 'page-results-listings.json');
799 update_option('wdk_results_page', $new_page->ID);
800 $data['page_title'] = esc_html__('Results Listings', 'wpdirectorykit');
801 break;
802 }
803
804 if (empty($new_page)) {
805 $data['popup_text_error'] = __('Page not Created', 'wpdirectorykit');
806 } else {
807 $data['page_id'] = $new_page->ID;
808 }
809
810 if (empty($data['popup_text_error'])) {
811 $data['success'] = true;
812 }
813
814 $this->output($data);
815 }
816
817 public function upload_image()
818 {
819 $data = array();
820 $data['message'] = '';
821 $data['popup_text_success'] = '';
822 $data['popup_text_error'] = '';
823 $data['parameters'] = $_POST;
824 $data['success'] = false;
825 $data['page_id'] = false;
826 $data['page_title'] = false;
827 $data['results'] = array();
828 /* protect */
829 if(!wmvc_user_in_role('administrator')) {
830 $data['message'] = __('Disabled for current user', 'wpdirectorykit');
831 $this->output($data);
832 }
833
834 // Check _wpnonce
835 check_admin_referer( 'wdk-backendajax', '_wpnonce' );
836
837 if(!empty($data['parameters']['image']))
838 foreach (explode(',', sanitize_text_field($_POST['image'])) as $key => $image_or_src) {
839 if(empty($image_or_src)) continue;
840 $image_id = wmvc_add_wp_image($image_or_src);
841 $image_url = wp_get_attachment_image_url($image_id, 'large');
842
843 $data['results'][] = [
844 'image_id'=> $image_id,
845 'image_url'=> $image_url,
846 ];
847 }
848
849
850 if (empty($data['popup_text_error'])) {
851 $data['success'] = true;
852 }
853
854 $this->output($data);
855 }
856
857
858 /* Create Page */
859 private function create_page($post_title, $post_content = '', $post_template = NULL, $post_parent=0, $rebuild = FALSE)
860 {
861
862 $post_id = NULL;
863
864 // Delete posts and rebuild
865 if($rebuild) {
866
867 $post = wdk_page_by_title($post_title, 'OBJECT', 'page' );
868 if(!empty($post))
869 {
870 wp_delete_post($post->ID, true);
871 $post=NULL;
872 }
873 if(!empty($post))
874 $post_id = $post->ID;
875 }
876
877
878 if(empty($post_id))
879 {
880 $error_obj = NULL;
881 $post_insert = array(
882 'post_title' => wp_strip_all_tags( $post_title ),
883 'post_content' => $post_content,
884 'post_status' => 'publish',
885 'post_type' => 'page',
886 'post_author' => get_current_user_id(),
887 'post_category' => array(1,2),
888 'page_template' => $post_template,
889 'post_parent' => $post_parent
890 );
891 $post_id = wp_insert_post( $post_insert, $error_obj );
892 }
893
894 $post_insert = get_post( $post_id );
895
896 return $post_insert;
897 }
898
899 /* Elementor Import Template */
900 private function elementor_assign($page_id, $json_template_name = '')
901 {
902
903 $file = false;
904
905 if(is_child_theme() && file_exists(get_stylesheet_directory().'/elementor-data/wpdirectorykit/'.$json_template_name))
906 {
907 $file = get_stylesheet_directory().'/elementor-data/wpdirectorykit/'.$json_template_name;
908 }
909 elseif(file_exists(get_template_directory().'/demo-data/wpdirectorykit/'.$json_template_name))
910 {
911 $file = get_template_directory().'/demo-data/wpdirectorykit/'.$json_template_name;
912 }
913 elseif(file_exists( WPDIRECTORYKIT_PATH. '/demo-data/'.$json_template_name))
914 {
915 $file = WPDIRECTORYKIT_PATH.'demo-data/'.$json_template_name;
916 }
917
918 if(!$file || !class_exists('Elementor\Plugin'))
919 {
920 return false;
921 }
922
923 $page_template = get_page_template_slug( $page_id );
924
925 add_post_meta( $page_id, '_elementor_edit_mode', 'builder' );
926
927 global $wp_filesystem;
928 // Initialize the WP filesystem, no more using 'file-put-contents' function
929 if (empty($wp_filesystem)) {
930 WP_Filesystem();
931 }
932
933 $string = $wp_filesystem->get_contents($file);
934
935 $json_template = json_decode($string, true);
936 $elements = $json_template['content'];
937
938 $data = array(
939 'elements' => $elements,
940 'settings' => array('post_status'=>'autosave', 'template'=>$page_template),
941 );
942 // @codingStandardsIgnoreStart
943 $document = Elementor\Plugin::$instance->documents->get( $page_id, false );
944 // @codingStandardsIgnoreEnd
945 return $document->save( $data );
946 }
947
948
949
950 private function output($data, $print = TRUE)
951 {
952 if ($print) {
953 header('Pragma: no-cache');
954 header('Cache-Control: no-store, no-cache');
955 header('Content-Type: application/json; charset=utf8');
956 //header('Content-Length: '.$length); // special characters causing troubles
957 echo (wp_json_encode($data));
958 exit();
959 } else {
960 return $data;
961 }
962 }
963 }
964
965 if (!class_exists('\Plugin_Upgrader', false)) {
966 require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
967 }
968
969 /**
970 * WordPress class extended for on-the-fly plugin installations.
971 */
972 class PluginInstallerSkinSilentWdk extends \WP_Upgrader_Skin
973 {
974
975 /**
976 * Empty out the header of its HTML content.
977 */
978 public function header()
979 {
980 }
981
982 /**
983 * Empty out the footer of its HTML content.
984 */
985 public function footer()
986 {
987 }
988
989 /**
990 * Empty out the footer of its HTML content.
991 *
992 * @param string $string
993 * @param mixed ...$args Optional text replacements.
994 */
995 public function feedback($string, ...$args)
996 {
997 }
998
999 /**
1000 * Empty out JavaScript output that calls function to decrement the update counts.
1001 *
1002 * @param string $type Type of update count to decrement.
1003 */
1004 public function decrement_update_count($type)
1005 {
1006 }
1007
1008 /**
1009 * Empty out the error HTML content.
1010 *
1011 * @param string|WP_Error $errors A string or WP_Error object of the install error/s.
1012 */
1013 public function error($errors)
1014 {
1015 }
1016 }
1017