|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Commands\ChangeStatus; |
|
6
|
|
|
use GeminiLabs\SiteReviews\Commands\TogglePinned; |
|
7
|
|
|
use GeminiLabs\SiteReviews\Database; |
|
8
|
|
|
use GeminiLabs\SiteReviews\Helpers\Arr; |
|
9
|
|
|
use GeminiLabs\SiteReviews\Modules\Console; |
|
10
|
|
|
use GeminiLabs\SiteReviews\Modules\Html\Partials\SiteReviews as SiteReviewsPartial; |
|
11
|
|
|
use GeminiLabs\SiteReviews\Modules\Notice; |
|
12
|
|
|
use GeminiLabs\SiteReviews\Modules\Translation; |
|
13
|
|
|
use GeminiLabs\SiteReviews\Role; |
|
14
|
|
|
use GeminiLabs\SiteReviews\Shortcodes\SiteReviewsShortcode; |
|
15
|
|
|
|
|
16
|
|
|
class AjaxController extends Controller |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @return void |
|
20
|
|
|
*/ |
|
21
|
|
|
public function routerChangeStatus(array $request) |
|
22
|
|
|
{ |
|
23
|
|
|
wp_send_json_success($this->execute(new ChangeStatus($request))); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @return void |
|
28
|
|
|
*/ |
|
29
|
|
|
public function routerClearConsole() |
|
30
|
|
|
{ |
|
31
|
|
|
glsr(AdminController::class)->routerClearConsole(); |
|
32
|
|
|
wp_send_json_success([ |
|
33
|
|
|
'console' => glsr(Console::class)->get(), |
|
34
|
|
|
'notices' => glsr(Notice::class)->get(), |
|
35
|
|
|
]); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @return void |
|
40
|
|
|
*/ |
|
41
|
|
|
public function routerCountReviews() |
|
42
|
|
|
{ |
|
43
|
|
|
glsr(AdminController::class)->routerCountReviews(); |
|
44
|
|
|
wp_send_json_success([ |
|
45
|
|
|
'notices' => glsr(Notice::class)->get(), |
|
46
|
|
|
]); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @return void |
|
51
|
|
|
*/ |
|
52
|
|
|
public function routerMigrateReviews() |
|
53
|
|
|
{ |
|
54
|
|
|
glsr(AdminController::class)->routerMigrateReviews(); |
|
55
|
|
|
wp_send_json_success([ |
|
56
|
|
|
'notices' => glsr(Notice::class)->get(), |
|
57
|
|
|
]); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @return void |
|
62
|
|
|
*/ |
|
63
|
|
|
public function routerDismissNotice(array $request) |
|
64
|
|
|
{ |
|
65
|
|
|
glsr(NoticeController::class)->routerDismissNotice($request); |
|
66
|
|
|
wp_send_json_success(); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @return void |
|
71
|
|
|
*/ |
|
72
|
|
|
public function routerMceShortcode(array $request) |
|
73
|
|
|
{ |
|
74
|
|
|
$shortcode = $request['shortcode']; |
|
75
|
|
|
$response = false; |
|
76
|
|
|
if (array_key_exists($shortcode, glsr()->mceShortcodes)) { |
|
77
|
|
|
$data = glsr()->mceShortcodes[$shortcode]; |
|
78
|
|
|
if (!empty($data['errors'])) { |
|
79
|
|
|
$data['btn_okay'] = [esc_html__('Okay', 'site-reviews')]; |
|
80
|
|
|
} |
|
81
|
|
|
$response = [ |
|
82
|
|
|
'body' => $data['fields'], |
|
83
|
|
|
'close' => $data['btn_close'], |
|
84
|
|
|
'ok' => $data['btn_okay'], |
|
85
|
|
|
'shortcode' => $shortcode, |
|
86
|
|
|
'title' => $data['title'], |
|
87
|
|
|
]; |
|
88
|
|
|
} |
|
89
|
|
|
wp_send_json_success($response); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @return void |
|
94
|
|
|
*/ |
|
95
|
|
|
public function routerFetchConsole() |
|
96
|
|
|
{ |
|
97
|
|
|
glsr(AdminController::class)->routerFetchConsole(); |
|
98
|
|
|
wp_send_json_success([ |
|
99
|
|
|
'console' => glsr(Console::class)->get(), |
|
100
|
|
|
'notices' => glsr(Notice::class)->get(), |
|
101
|
|
|
]); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @return void |
|
106
|
|
|
*/ |
|
107
|
|
|
public function routerResetPermissions() |
|
108
|
|
|
{ |
|
109
|
|
|
glsr(Role::class)->resetAll(); |
|
110
|
|
|
glsr(Notice::class)->clear()->addSuccess(__('The permissions have been reset, please reload the page for them to take effect.', 'site-reviews')); |
|
111
|
|
|
wp_send_json_success([ |
|
112
|
|
|
'notices' => glsr(Notice::class)->get(), |
|
113
|
|
|
]); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @return void |
|
118
|
|
|
*/ |
|
119
|
|
|
public function routerSearchPosts(array $request) |
|
120
|
|
|
{ |
|
121
|
|
|
$results = glsr(Database::class)->searchPosts($request['search']); |
|
122
|
|
|
wp_send_json_success([ |
|
123
|
|
|
'empty' => '<div>'.__('Nothing found.', 'site-reviews').'</div>', |
|
124
|
|
|
'items' => $results, |
|
125
|
|
|
]); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @return void |
|
130
|
|
|
*/ |
|
131
|
|
|
public function routerSearchTranslations(array $request) |
|
132
|
|
|
{ |
|
133
|
|
|
if (empty($request['exclude'])) { |
|
134
|
|
|
$request['exclude'] = []; |
|
135
|
|
|
} |
|
136
|
|
|
$results = glsr(Translation::class) |
|
137
|
|
|
->search($request['search']) |
|
138
|
|
|
->exclude() |
|
139
|
|
|
->exclude($request['exclude']) |
|
140
|
|
|
->renderResults(); |
|
141
|
|
|
wp_send_json_success([ |
|
142
|
|
|
'empty' => '<div>'.__('Nothing found.', 'site-reviews').'</div>', |
|
143
|
|
|
'items' => $results, |
|
144
|
|
|
]); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @return void |
|
149
|
|
|
*/ |
|
150
|
1 |
|
public function routerSubmitReview(array $request) |
|
151
|
|
|
{ |
|
152
|
1 |
|
$command = glsr(PublicController::class)->routerSubmitReview($request); |
|
153
|
1 |
|
$redirect = trim(strval(get_post_meta($command->post_id, 'redirect_to', true))); |
|
154
|
1 |
|
$redirect = apply_filters('site-reviews/review/redirect', $redirect, $command); |
|
155
|
|
|
$data = [ |
|
156
|
1 |
|
'errors' => glsr()->sessionGet($command->form_id.'errors', false), |
|
157
|
1 |
|
'message' => glsr()->sessionGet($command->form_id.'message', ''), |
|
158
|
1 |
|
'recaptcha' => glsr()->sessionGet($command->form_id.'recaptcha', false), |
|
159
|
1 |
|
'redirect' => $redirect, |
|
160
|
|
|
]; |
|
161
|
1 |
|
if (false === $data['errors']) { |
|
162
|
1 |
|
glsr()->sessionClear(); |
|
163
|
1 |
|
wp_send_json_success($data); |
|
164
|
|
|
} |
|
165
|
|
|
wp_send_json_error($data); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @return void |
|
170
|
|
|
*/ |
|
171
|
|
|
public function routerFetchPagedReviews(array $request) |
|
172
|
|
|
{ |
|
173
|
|
|
$args = [ |
|
174
|
|
|
'paged' => Arr::get($request, 'page', false), |
|
175
|
|
|
'pagedUrl' => '', |
|
176
|
|
|
'pagination' => 'ajax', |
|
177
|
|
|
'schema' => false, |
|
178
|
|
|
]; |
|
179
|
|
|
if (!$args['paged']) { |
|
180
|
|
|
$homePath = untrailingslashit(parse_url(home_url(), PHP_URL_PATH)); |
|
181
|
|
|
$urlPath = untrailingslashit(parse_url(Arr::get($request, 'url'), PHP_URL_PATH)); |
|
182
|
|
|
$urlQuery = []; |
|
183
|
|
|
parse_str(parse_url(Arr::get($request, 'url'), PHP_URL_QUERY), $urlQuery); |
|
184
|
|
|
$args['paged'] = (int) Arr::get($urlQuery, glsr()->constant('PAGED_QUERY_VAR'), 1); |
|
185
|
|
|
$args['pagedUrl'] = $homePath === $urlPath |
|
186
|
|
|
? trailingslashit(home_url()) |
|
187
|
|
|
: trailingslashit(home_url($urlPath)); |
|
188
|
|
|
} |
|
189
|
|
|
$atts = (array) json_decode(Arr::get($request, 'atts')); |
|
190
|
|
|
$atts = glsr(SiteReviewsShortcode::class)->normalizeAtts($atts); |
|
191
|
|
|
$html = glsr(SiteReviewsPartial::class)->build(wp_parse_args($args, $atts)); |
|
192
|
|
|
return wp_send_json_success([ |
|
193
|
|
|
'pagination' => $html->getPagination(), |
|
194
|
|
|
'reviews' => $html->getReviews(), |
|
195
|
|
|
]); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* @return void |
|
200
|
|
|
*/ |
|
201
|
|
|
public function routerTogglePinned(array $request) |
|
202
|
|
|
{ |
|
203
|
|
|
$isPinned = $this->execute(new TogglePinned($request)); |
|
204
|
|
|
wp_send_json_success([ |
|
205
|
|
|
'notices' => glsr(Notice::class)->get(), |
|
206
|
|
|
'pinned' => $isPinned, |
|
207
|
|
|
]); |
|
208
|
|
|
} |
|
209
|
|
|
} |
|
210
|
|
|
|