1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Controllers; |
4
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Commands\CreateReview; |
6
|
|
|
use GeminiLabs\SiteReviews\Commands\EnqueuePublicAssets; |
7
|
|
|
use GeminiLabs\SiteReviews\Defaults\SiteReviewsDefaults; |
8
|
|
|
use GeminiLabs\SiteReviews\Helpers\Arr; |
9
|
|
|
use GeminiLabs\SiteReviews\Modules\Html\Builder; |
10
|
|
|
use GeminiLabs\SiteReviews\Modules\Schema; |
11
|
|
|
use GeminiLabs\SiteReviews\Modules\Style; |
12
|
|
|
use GeminiLabs\SiteReviews\Request; |
13
|
|
|
use GeminiLabs\SiteReviews\Shortcodes\SiteReviewsShortcode; |
14
|
|
|
|
15
|
|
|
class PublicController extends Controller |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @return void |
19
|
|
|
* @action wp_enqueue_scripts |
20
|
|
|
*/ |
21
|
|
|
public function enqueueAssets() |
22
|
|
|
{ |
23
|
|
|
$this->execute(new EnqueuePublicAssets()); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @return void |
28
|
|
|
* @action site-reviews/route/ajax/fetch-paged-reviews |
29
|
|
|
*/ |
30
|
|
|
public function fetchPagedReviewsAjax(Request $request) |
31
|
|
|
{ |
32
|
|
|
glsr()->store(glsr()->paged_handle, $request); |
33
|
|
|
$args = Arr::consolidate($request->atts); |
34
|
|
|
$args = glsr(SiteReviewsDefaults::class)->restrict($args); |
35
|
|
|
$html = glsr(SiteReviewsShortcode::class)->buildReviewsHtml($args); |
36
|
|
|
$response = [ |
37
|
|
|
'pagination' => $html->getPagination($wrap = false), |
38
|
|
|
'reviews' => $html->getReviews(), |
39
|
|
|
]; |
40
|
|
|
glsr()->discard(glsr()->paged_handle); |
41
|
|
|
wp_send_json_success($response); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param string $tag |
46
|
|
|
* @param string $handle |
47
|
|
|
* @return string |
48
|
|
|
* @filter script_loader_tag |
49
|
|
|
*/ |
50
|
|
|
public function filterEnqueuedScriptTags($tag, $handle) |
51
|
|
|
{ |
52
|
|
|
$scripts = [glsr()->id.'/google-recaptcha']; |
53
|
|
|
if (in_array($handle, glsr()->filterArray('async-scripts', $scripts))) { |
54
|
|
|
$tag = str_replace(' src=', ' async src=', $tag); |
55
|
|
|
} |
56
|
|
|
if (in_array($handle, glsr()->filterArray('defer-scripts', $scripts))) { |
57
|
|
|
$tag = str_replace(' src=', ' defer src=', $tag); |
58
|
|
|
} |
59
|
|
|
return $tag; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return array |
64
|
|
|
* @filter site-reviews/config/forms/review-form |
65
|
|
|
*/ |
66
|
|
|
public function filterFieldOrder(array $config) |
67
|
|
|
{ |
68
|
|
|
$order = array_keys($config); |
69
|
|
|
$order = glsr()->filterArray('review-form/order', $order); |
70
|
|
|
return array_intersect_key(array_merge(array_flip($order), $config), $config); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param string $view |
75
|
|
|
* @return string |
76
|
|
|
* @filter site-reviews/render/view |
77
|
|
|
*/ |
78
|
8 |
|
public function filterRenderView($view) |
79
|
|
|
{ |
80
|
8 |
|
return glsr(Style::class)->filterView($view); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return void |
85
|
|
|
* @action site-reviews/builder |
86
|
|
|
*/ |
87
|
6 |
|
public function modifyBuilder(Builder $builder) |
88
|
|
|
{ |
89
|
6 |
|
$reflection = new \ReflectionClass($builder); |
90
|
6 |
|
if ('Builder' === $reflection->getShortName()) { // only modify public fields |
91
|
6 |
|
call_user_func_array([glsr(Style::class), 'modifyField'], [$builder]); |
92
|
|
|
} |
93
|
6 |
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return void |
97
|
|
|
* @action wp_footer |
98
|
|
|
*/ |
99
|
|
|
public function renderModal() |
100
|
|
|
{ |
101
|
|
|
if (glsr()->retrieve('use_modal', false)) { |
102
|
|
|
glsr()->render('views/partials/modal'); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return void |
108
|
|
|
* @action wp_footer |
109
|
|
|
*/ |
110
|
|
|
public function renderSchema() |
111
|
|
|
{ |
112
|
|
|
glsr(Schema::class)->render(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return void |
117
|
|
|
* @action site-reviews/route/public/submit-review |
118
|
|
|
*/ |
119
|
|
|
public function submitReview(Request $request) |
120
|
|
|
{ |
121
|
|
|
$command = $this->execute(new CreateReview($request)); |
122
|
|
|
if ($command->success()) { |
123
|
|
|
wp_safe_redirect($command->referer()); // @todo add review ID to referer? |
124
|
|
|
exit; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return void |
130
|
|
|
* @action site-reviews/route/ajax/submit-review |
131
|
|
|
*/ |
132
|
7 |
|
public function submitReviewAjax(Request $request) |
133
|
|
|
{ |
134
|
7 |
|
$command = $this->execute(new CreateReview($request)); |
135
|
7 |
|
if ($command->success()) { |
136
|
6 |
|
wp_send_json_success($command->response()); |
137
|
|
|
} |
138
|
7 |
|
wp_send_json_error($command->response()); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|