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