1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use GeminiLabs\SiteReviews\Application; |
4
|
|
|
use GeminiLabs\SiteReviews\Arguments; |
5
|
|
|
use GeminiLabs\SiteReviews\BlackHole; |
6
|
|
|
use GeminiLabs\SiteReviews\Commands\CreateReview; |
7
|
|
|
use GeminiLabs\SiteReviews\Database\OptionManager; |
8
|
|
|
use GeminiLabs\SiteReviews\Database\RatingManager; |
9
|
|
|
use GeminiLabs\SiteReviews\Database\ReviewManager; |
10
|
|
|
use GeminiLabs\SiteReviews\Exceptions\BindingResolutionException; |
11
|
|
|
use GeminiLabs\SiteReviews\Helpers\Arr; |
12
|
|
|
use GeminiLabs\SiteReviews\Helpers\Cast; |
13
|
|
|
use GeminiLabs\SiteReviews\Helpers\Str; |
14
|
|
|
use GeminiLabs\SiteReviews\Modules\Backtrace; |
15
|
|
|
use GeminiLabs\SiteReviews\Modules\Console; |
16
|
|
|
use GeminiLabs\SiteReviews\Modules\Dump; |
17
|
|
|
use GeminiLabs\SiteReviews\Modules\Html\Builder; |
18
|
|
|
use GeminiLabs\SiteReviews\Modules\Html\Partial; |
19
|
|
|
use GeminiLabs\SiteReviews\Modules\Rating; |
20
|
|
|
use GeminiLabs\SiteReviews\Request; |
21
|
|
|
use GeminiLabs\SiteReviews\Review; |
22
|
|
|
|
23
|
|
|
defined('ABSPATH') || exit; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Alternate method of using the functions without having to use `function_exists()` |
27
|
|
|
* Example: apply_filters('glsr_get_reviews', [], ['assigned_posts' => 'post_id']); |
28
|
|
|
*/ |
29
|
|
|
add_action('plugins_loaded', function () { |
30
|
|
|
$hooks = [ |
31
|
|
|
'glsr_create_review' => 2, |
32
|
|
|
'glsr_debug' => 10, |
33
|
|
|
'glsr_get' => 4, |
34
|
|
|
'glsr_get_option' => 4, |
35
|
|
|
'glsr_get_options' => 1, |
36
|
|
|
'glsr_get_ratings' => 2, |
37
|
|
|
'glsr_get_review' => 2, |
38
|
|
|
'glsr_get_reviews' => 2, |
39
|
|
|
'glsr_log' => 3, |
40
|
|
|
'glsr_star_rating' => 4, |
41
|
|
|
'glsr_trace' => 2, |
42
|
|
|
'glsr_update_review' => 3, |
43
|
|
|
]; |
44
|
|
|
foreach ($hooks as $function => $acceptedArgs) { |
45
|
|
|
add_filter($function, function () use ($function) { |
46
|
|
|
$args = func_get_args(); |
47
|
|
|
array_shift($args); // remove the fallback value |
48
|
|
|
return call_user_func_array($function, $args); |
49
|
|
|
}, 10, $acceptedArgs); |
50
|
|
|
} |
51
|
|
|
}); |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return mixed |
55
|
|
|
*/ |
56
|
|
|
function glsr($alias = null, array $parameters = []) |
57
|
|
|
{ |
58
|
|
|
if (is_null($alias)) { |
59
|
|
|
return Application::load(); |
60
|
|
|
} |
61
|
|
|
try { |
62
|
|
|
return Application::load()->make($alias, $parameters); |
63
|
|
|
} catch (BindingResolutionException $e) { |
64
|
|
|
glsr_log()->error($e->getMessage()); |
65
|
|
|
return Application::load()->make(BlackHole::class, compact('alias')); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param string $page |
71
|
|
|
* @param string $tab |
72
|
|
|
* @param string $sub |
73
|
|
|
* |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
|
|
function glsr_admin_url($page = '', $tab = '', $sub = '') |
77
|
|
|
{ |
78
|
|
|
if ('welcome' === $page) { |
79
|
|
|
$page = glsr()->id.'-welcome'; |
80
|
|
|
$args = array_filter(compact('page', 'tab')); |
81
|
|
|
return add_query_arg($args, admin_url('index.php')); |
82
|
|
|
} |
83
|
|
|
if (!empty($page)) { |
84
|
|
|
$page = Str::dashCase(glsr()->prefix.$page); |
85
|
|
|
} |
86
|
|
|
$post_type = glsr()->post_type; |
87
|
|
|
$args = array_filter(compact('post_type', 'page', 'tab', 'sub')); |
88
|
|
|
return add_query_arg($args, admin_url('edit.php')); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
function glsr_admin_link(string $url, string $text = '', string $expand = ''): string |
92
|
|
|
{ |
93
|
|
|
$expand = ltrim($expand, '#'); |
94
|
|
|
if (!empty($expand)) { |
95
|
|
|
$expand = '#'.$expand; |
96
|
|
|
} |
97
|
|
|
return glsr(Builder::class)->a([ |
98
|
|
|
'data-expand' => $expand, |
99
|
|
|
'href' => $url, |
100
|
|
|
'text' => esc_html($text ?: $url), |
101
|
|
|
]); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return Review|false |
106
|
|
|
*/ |
107
|
|
|
function glsr_create_review($values = []) |
108
|
|
|
{ |
109
|
|
|
$values = Arr::removeEmptyValues(Arr::consolidate($values)); |
110
|
|
|
$request = new Request($values); |
111
|
|
|
$review = false; |
112
|
|
|
glsr()->store('glsr_create_review', true); |
113
|
|
|
$command = new CreateReview($request); |
114
|
|
|
if ($command->isRequestValid()) { |
115
|
|
|
$review = glsr(ReviewManager::class)->create($command); |
116
|
|
|
} |
117
|
|
|
glsr()->discard('glsr_create_review'); |
118
|
|
|
return $review; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @return WP_Screen|object |
123
|
|
|
*/ |
124
|
|
|
function glsr_current_screen() |
125
|
|
|
{ |
126
|
|
|
if (function_exists('get_current_screen')) { |
127
|
|
|
$screen = get_current_screen(); |
128
|
|
|
} |
129
|
|
|
return empty($screen) |
130
|
|
|
? (object) array_fill_keys(['action', 'base', 'id', 'post_type'], '') |
131
|
|
|
: $screen; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param mixed ...$vars |
136
|
|
|
*/ |
137
|
|
|
function glsr_debug(...$vars): void |
138
|
|
|
{ |
139
|
|
|
if (1 === count($vars)) { |
140
|
|
|
$dump = glsr(Dump::class)->dump($vars[0]); |
141
|
|
|
$value = htmlspecialchars($dump, ENT_QUOTES, 'UTF-8'); |
142
|
|
|
printf('<div class="glsr-debug"><pre>%s</pre></div>', $value); |
143
|
|
|
} else { |
144
|
|
|
echo '<div class="glsr-debug-group">'; |
145
|
|
|
foreach ($vars as $var) { |
146
|
|
|
glsr_debug($var); |
147
|
|
|
} |
148
|
|
|
echo '</div>'; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param string|int $path |
154
|
|
|
* @param mixed $fallback |
155
|
|
|
* |
156
|
|
|
* @return mixed |
157
|
|
|
*/ |
158
|
|
|
function glsr_get($array, $path = '', $fallback = '') |
159
|
|
|
{ |
160
|
|
|
return Arr::get($array, $path, $fallback); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @param string $path |
165
|
|
|
* @param mixed $fallback |
166
|
|
|
* @param string $cast |
167
|
|
|
* |
168
|
|
|
* @return mixed |
169
|
|
|
*/ |
170
|
|
|
function glsr_get_option($path = '', $fallback = '', $cast = '') |
171
|
|
|
{ |
172
|
|
|
return is_string($path) |
173
|
|
|
? glsr(OptionManager::class)->get(Str::prefix($path, 'settings.'), $fallback, $cast) |
174
|
|
|
: $fallback; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @return array |
179
|
|
|
*/ |
180
|
|
|
function glsr_get_options() |
181
|
|
|
{ |
182
|
|
|
return glsr(OptionManager::class)->get('settings'); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @return Arguments |
187
|
|
|
*/ |
188
|
|
|
function glsr_get_ratings($args = []) |
189
|
|
|
{ |
190
|
|
|
$counts = glsr(RatingManager::class)->ratings(Arr::consolidate($args)); |
191
|
|
|
return new Arguments([ |
192
|
|
|
'average' => glsr(Rating::class)->average($counts), |
193
|
|
|
'maximum' => Rating::max(), |
194
|
|
|
'minimum' => Rating::min(), |
195
|
|
|
'ranking' => glsr(Rating::class)->ranking($counts), |
196
|
|
|
'ratings' => $counts, |
197
|
|
|
'reviews' => array_sum($counts), |
198
|
|
|
]); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
function glsr_get_review($postId): Review |
202
|
|
|
{ |
203
|
|
|
return glsr(ReviewManager::class)->get(Cast::toInt($postId)); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @return GeminiLabs\SiteReviews\Reviews |
208
|
|
|
*/ |
209
|
|
|
function glsr_get_reviews($args = []) |
210
|
|
|
{ |
211
|
|
|
return glsr(ReviewManager::class)->reviews(Arr::consolidate($args)); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @param mixed ...$args |
216
|
|
|
* |
217
|
|
|
* @return Console |
218
|
|
|
*/ |
219
|
|
|
function glsr_log(...$args) |
220
|
|
|
{ |
221
|
|
|
$console = glsr(Console::class); |
222
|
|
|
return !empty($args) |
223
|
|
|
? call_user_func_array([$console, 'debug'], $args) |
224
|
|
|
: $console; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
function glsr_premium_link(string $path, string $text = ''): string |
228
|
|
|
{ |
229
|
|
|
$url = glsr_premium_url($path); |
230
|
|
|
$texts = [ |
231
|
|
|
'license-keys' => _x('License Keys', 'admin-text', 'site-reviews'), |
232
|
|
|
'site-reviews-actions' => _x('Review Actions', 'admin-text', 'site-reviews'), |
233
|
|
|
'site-reviews-authors' => _x('Review Authors', 'admin-text', 'site-reviews'), |
234
|
|
|
'site-reviews-filters' => _x('Review Filters', 'admin-text', 'site-reviews'), |
235
|
|
|
'site-reviews-forms' => _x('Review Forms', 'admin-text', 'site-reviews'), |
236
|
|
|
'site-reviews-images' => _x('Review Images', 'admin-text', 'site-reviews'), |
237
|
|
|
'site-reviews-notifications' => _x('Review Notifications', 'admin-text', 'site-reviews'), |
238
|
|
|
'site-reviews-premium' => _x('Site Reviews Premium', 'admin-text', 'site-reviews'), |
239
|
|
|
'site-reviews-themes' => _x('Review Themes', 'admin-text', 'site-reviews'), |
240
|
|
|
]; |
241
|
|
|
if (empty($text)) { |
242
|
|
|
$text = $texts[$path] ?? $url; |
243
|
|
|
} |
244
|
|
|
return sprintf('<a href="%s" target="_blank">%s</a>', $url, esc_html($text)); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
function glsr_premium_url(string $path = '/'): string |
248
|
|
|
{ |
249
|
|
|
$baseUrl = 'https://niftyplugins.com/'; |
250
|
|
|
$paths = [ |
251
|
|
|
'account' => '/account/', |
252
|
|
|
'addons' => '/plugins/', |
253
|
|
|
'license-keys' => '/account/license-keys/', |
254
|
|
|
'site-reviews-actions' => '/plugins/site-reviews-actions/', |
255
|
|
|
'site-reviews-authors' => '/plugins/site-reviews-authors/', |
256
|
|
|
'site-reviews-filters' => '/plugins/site-reviews-filters/', |
257
|
|
|
'site-reviews-forms' => '/plugins/site-reviews-forms/', |
258
|
|
|
'site-reviews-images' => '/plugins/site-reviews-images/', |
259
|
|
|
'site-reviews-notifications' => '/plugins/site-reviews-notifications/', |
260
|
|
|
'site-reviews-premium' => '/plugins/site-reviews-premium/', |
261
|
|
|
'site-reviews-themes' => '/plugins/site-reviews-themes/', |
262
|
|
|
'support' => '/account/support/', |
263
|
|
|
]; |
264
|
|
|
$urlPath = trim($paths[$path] ?? $path); |
265
|
|
|
$urlPath = trailingslashit(ltrim($urlPath, '/')); |
266
|
|
|
return esc_url(trailingslashit($baseUrl).$urlPath); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* @param string $path |
271
|
|
|
* @param mixed $value |
272
|
|
|
* |
273
|
|
|
* @return array |
274
|
|
|
*/ |
275
|
|
|
function glsr_set(array $data, $path, $value) |
276
|
|
|
{ |
277
|
|
|
return Arr::set($data, $path, $value); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* @param mixed $rating |
282
|
|
|
* @param int|null $reviews |
283
|
|
|
* |
284
|
|
|
* @return string |
285
|
|
|
*/ |
286
|
|
|
function glsr_star_rating($rating, $reviews = 0, array $args = []) |
287
|
|
|
{ |
288
|
|
|
return glsr(Partial::class)->build('star-rating', [ |
289
|
|
|
'args' => $args, |
290
|
|
|
'rating' => $rating, |
291
|
|
|
'reviews' => $reviews, |
292
|
|
|
]); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* @param int $limit |
297
|
|
|
* |
298
|
|
|
* @return void |
299
|
|
|
*/ |
300
|
|
|
function glsr_trace($limit = 5) |
301
|
|
|
{ |
302
|
|
|
glsr_log(glsr(Backtrace::class)->trace($limit)); |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* @param int $postId |
307
|
|
|
* |
308
|
|
|
* @return Review|false |
309
|
|
|
*/ |
310
|
|
|
function glsr_update_review($postId, $values = []) |
311
|
|
|
{ |
312
|
|
|
$postId = Cast::toInt($postId); |
313
|
|
|
$values = Arr::consolidate($values); |
314
|
|
|
glsr()->store('glsr_update_review', true); |
315
|
|
|
$result = glsr(ReviewManager::class)->update($postId, $values); |
316
|
|
|
glsr()->discard('glsr_update_review'); |
317
|
|
|
return $result; |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* @return int |
322
|
|
|
*/ |
323
|
|
|
function glsr_user_count() |
324
|
|
|
{ |
325
|
|
|
if (function_exists('get_user_count')) { |
326
|
|
|
return get_user_count(); |
327
|
|
|
} |
328
|
|
|
return Arr::getAs('int', count_users(), 'total_users'); |
329
|
|
|
} |
330
|
|
|
|