PluginProbe
AliNext – WooCommerce Dropshipping Plugin for AliExpress / trunk
AliNext – WooCommerce Dropshipping Plugin for AliExpress vtrunk
ali2woo-lite / includes / classes / controller / ReviewBackendController.php

ReviewBackendController.php in AliNext – WooCommerce Dropshipping Plugin for AliExpress trunk, at includes/classes/controller/ReviewBackendController.php

184 lines 6.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Description of ReviewBackendController
5 *
6 * @author Ali2Woo Team
7 *
8 * @autoload: a2wl_admin_init
9 *
10 * @ajax: true
11 */
12
13 namespace AliNext_Lite;;
14
15 use Exception;
16 use Throwable;
17
18 class ReviewBackendController extends AbstractController
19 {
20
21 private $upd_rvws_task_id = "a2wl_product_update_reviews_manual";
22
23 function __construct() {
24 parent::__construct();
25
26 add_action('admin_enqueue_scripts', [$this, 'assets']);
27
28 add_action('wp_ajax_a2wl_arvi_remove_product_reviews', [$this, 'ajax_remove_product_reviews']);
29 add_action('wp_ajax_a2wl_arvi_get_comment_photos', [$this, 'ajax_get_comment_photos']);
30 add_action('wp_ajax_a2wl_arvi_save_comment_photos', [$this, 'ajax_save_comment_photos']);
31
32 add_filter('a2wl_ajax_product_info', [$this, 'product_info'], 4, 10);
33
34 //todo: this doesn't work for new Woocommerce, because they moved reviews to a separate section
35 add_filter('comment_row_actions', [$this, 'row_actions'], 10, 2);
36
37 if (is_admin()) {
38 // add bulk action to product list
39 add_filter('a2wl_wcpl_bulk_actions_init', [$this, 'bulk_actions_init']);
40 }
41 }
42
43 public function assets(): void
44 {
45 $current_screen = get_current_screen();
46
47 if ($current_screen->id === "product" || $current_screen->id === "edit-comments") {
48 wp_enqueue_style(
49 'a2wl-review-comment-widget-style',
50 A2WL()->plugin_url() . '/assets/css/review/comment_widget.css',
51 [],
52 A2WL()->version
53 );
54 wp_enqueue_script(
55 'a2wl-review-comment-widget-script',
56 A2WL()->plugin_url() . '/assets/js/review/comment_widget.js',
57 [],
58 A2WL()->version,
59 true
60 );
61
62 $data = [
63 'current_page' => $current_screen->id,
64 'i18n_please_wait' => 'Please wait...',
65 'i18n_done' => 'Done!',
66 'i18n_error_occur' => 'Server error occurred!',
67 'ajaxurl' => admin_url('admin-ajax.php'),
68 'nonce_action' => wp_create_nonce(self::AJAX_NONCE_ACTION),
69 ];
70 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
71 if ($current_screen->id === "product" && isset($_GET['post'])) {
72 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
73 $data['product_id'] = intval($_GET['post']);
74 }
75
76 wp_localize_script('a2wl-review-comment-widget-script', 'WPDATA', $data);
77 }
78 }
79
80 public function row_actions($actions, $comment) {
81 if (Review::get_comment_photos($comment->comment_ID)) {
82 $actions = array_merge($actions, array('a2wl_comment_edit_photo_link' => sprintf('<a id="a2wl-%1$d" href="#">%2$s</a>', $comment->comment_ID, 'Edit Photos')));
83 }
84 return $actions;
85 }
86
87 function bulk_actions_init($bulk_actions_array) {
88 if (get_setting('load_review')) {
89 $bulk_actions_array[0][] = $this->upd_rvws_task_id;
90 $bulk_actions_array[1][$this->upd_rvws_task_id] = 'Update reviews';
91 }
92
93 return $bulk_actions_array;
94 }
95
96 public function product_info($content, $post_id, $external_id) {
97 $time_value = get_post_meta($post_id, '_a2w_reviews_last_update', true);
98 $time_value = $time_value ? gmdate("Y-m-d H:i:s", $time_value) : 'not loaded';
99
100 $content[] = "Reviews update: <span class='a2wl_value'>" . $time_value . "</span>";
101
102 return $content;
103 }
104
105 public function ajax_remove_product_reviews(): void
106 {
107 check_admin_referer(self::AJAX_NONCE_ACTION, self::NONCE);
108
109 $result = ResultBuilder::buildOk();
110
111 $post_id = isset($_POST['id']) ? $_POST['id'] : false;
112
113 if (!$post_id) {
114 echo wp_json_encode(ResultBuilder::buildError("Product related with this ID not found"));
115 wp_die();
116 }
117
118 a2wl_init_error_handler();
119 try {
120 $comments = Review::get_product_review_ids($post_id);
121 Review::remove_reviews_by_ids($comments);
122 restore_error_handler();
123 } catch (Throwable $e) {
124 a2wl_print_throwable($e);
125 $result = ResultBuilder::buildError($e->getMessage());
126 }
127
128 echo wp_json_encode($result);
129 wp_die();
130 }
131
132 public function ajax_get_comment_photos(): void
133 {
134 check_admin_referer(self::AJAX_NONCE_ACTION, self::NONCE);
135
136 $result = ["state" => "ok", "message" => ""];
137
138 $comment_id = isset($_POST['id']) ? $_POST['id'] : 0;
139 $photos = Review::get_comment_photos($comment_id);
140 if ($photos) {
141 $result['photos'] = $photos;
142 } else {
143 $result['state'] = 'error';
144 $result['message'] = 'No photos available';
145 }
146
147 echo wp_json_encode($result);
148 wp_die();
149 }
150
151 public function ajax_save_comment_photos(): void {
152 check_admin_referer(self::AJAX_NONCE_ACTION, self::NONCE);
153
154 $result = ["state" => "ok", "message" => ""];
155
156 $comment_id = isset($_POST['id']) ? intval($_POST['id']) : false;
157 $photos = isset($_POST['photos']) ? $_POST['photos'] : [];
158
159 if (is_numeric($comment_id)) {
160 $photos = $this->normalizePhotoArray($photos);
161 Review::save_comment_photos($comment_id, $photos);
162 } else {
163 $result['state'] = 'error';
164 $result['message'] = _x(
165 'Comment id is not provided', 'error text', 'ali2woo'
166 );
167 }
168
169 echo wp_json_encode($result);
170 wp_die();
171 }
172
173 private function normalizePhotoArray($photo_array): array
174 {
175 $result = [];
176 foreach ($photo_array as $photo){
177 $result[] = $photo['photo_id'];
178 }
179
180 return $result;
181 }
182
183 }
184