|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
defined('WPINC') || die; |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* Alternate method of using the functions without having to use `function_exists()` |
|
7
|
|
|
* Example: apply_filters('glsr_get_reviews', [], ['assigned_to' => 'post_id']); |
|
8
|
|
|
* @param mixed ... |
|
9
|
|
|
* @return mixed |
|
10
|
|
|
*/ |
|
11
|
|
|
add_filter('plugins_loaded', function () { |
|
12
|
|
|
$hooks = array( |
|
13
|
|
|
'glsr_calculate_ratings' => 1, |
|
14
|
|
|
'glsr_create_review' => 2, |
|
15
|
|
|
'glsr_debug' => 10, |
|
16
|
|
|
'glsr_get' => 4, |
|
17
|
|
|
'glsr_get_option' => 4, |
|
18
|
|
|
'glsr_get_options' => 1, |
|
19
|
|
|
'glsr_get_rating' => 2, |
|
20
|
|
|
'glsr_get_review' => 2, |
|
21
|
|
|
'glsr_get_reviews' => 2, |
|
22
|
|
|
'glsr_log' => 3, |
|
23
|
|
|
'glsr_star_rating' => 2, |
|
24
|
|
|
); |
|
25
|
|
|
foreach ($hooks as $function => $acceptedArgs) { |
|
26
|
|
|
add_filter($function, function () use ($function) { |
|
27
|
|
|
$args = func_get_args(); |
|
28
|
|
|
array_shift($args); // remove the fallback value |
|
29
|
|
|
return call_user_func_array($function, $args); |
|
30
|
|
|
}, 10, $acceptedArgs); |
|
31
|
|
|
} |
|
32
|
|
|
}); |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @return mixed |
|
36
|
|
|
*/ |
|
37
|
|
|
function glsr($alias = null) |
|
38
|
|
|
{ |
|
39
|
|
|
$app = \GeminiLabs\SiteReviews\Application::load(); |
|
40
|
|
|
return !is_null($alias) |
|
41
|
|
|
? $app->make($alias) |
|
42
|
|
|
: $app; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* array_column() alternative specifically for PHP v7.0.x. |
|
47
|
|
|
* @param $column string |
|
48
|
|
|
* @return array |
|
49
|
|
|
*/ |
|
50
|
|
|
function glsr_array_column(array $array, $column) |
|
51
|
|
|
{ |
|
52
|
|
|
$result = array(); |
|
53
|
|
|
foreach ($array as $subarray) { |
|
54
|
|
|
$subarray = (array) $subarray; |
|
55
|
|
|
if (!isset($subarray[$column])) { |
|
56
|
|
|
continue; |
|
57
|
|
|
} |
|
58
|
|
|
$result[] = $subarray[$column]; |
|
59
|
|
|
} |
|
60
|
|
|
return $result; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @return void |
|
65
|
|
|
*/ |
|
66
|
|
|
function glsr_calculate_ratings() |
|
67
|
|
|
{ |
|
68
|
|
|
glsr('Database\CountsManager')->updateAll(); |
|
69
|
|
|
glsr_log()->notice(__('Recalculated rating counts.', 'site-reviews')); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @return \GeminiLabs\SiteReviews\Review|false |
|
74
|
|
|
*/ |
|
75
|
|
|
function glsr_create_review($reviewValues = array()) |
|
76
|
|
|
{ |
|
77
|
|
|
$review = new \GeminiLabs\SiteReviews\Commands\CreateReview( |
|
78
|
|
|
\GeminiLabs\SiteReviews\Helpers\Arr::consolidateArray($reviewValues) |
|
79
|
|
|
); |
|
80
|
|
|
return glsr('Database\ReviewManager')->create($review); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @return \WP_Screen|object |
|
85
|
|
|
*/ |
|
86
|
|
|
function glsr_current_screen() |
|
87
|
|
|
{ |
|
88
|
|
|
if (function_exists('get_current_screen')) { |
|
89
|
|
|
$screen = get_current_screen(); |
|
90
|
|
|
} |
|
91
|
|
|
return empty($screen) |
|
92
|
|
|
? (object) array_fill_keys(['base', 'id', 'post_type'], null) |
|
93
|
|
|
: $screen; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @param mixed ...$vars |
|
98
|
|
|
* @return void |
|
99
|
|
|
*/ |
|
100
|
|
|
function glsr_debug(...$vars) |
|
101
|
|
|
{ |
|
102
|
|
|
if (1 == count($vars)) { |
|
103
|
|
|
$value = htmlspecialchars(print_r($vars[0], true), ENT_QUOTES, 'UTF-8'); |
|
104
|
|
|
printf('<div class="glsr-debug"><pre>%s</pre></div>', $value); |
|
105
|
|
|
} else { |
|
106
|
|
|
echo '<div class="glsr-debug-group">'; |
|
107
|
|
|
foreach ($vars as $var) { |
|
108
|
|
|
glsr_debug($var); |
|
109
|
|
|
} |
|
110
|
|
|
echo '</div>'; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @param array $data |
|
116
|
|
|
* @param string $path |
|
117
|
|
|
* @param mixed $fallback |
|
118
|
|
|
* @return mixed |
|
119
|
|
|
*/ |
|
120
|
|
|
function glsr_get($array, $path = '', $fallback = '') |
|
121
|
|
|
{ |
|
122
|
|
|
return \GeminiLabs\SiteReviews\Helpers\Arr::get($array, $path, $fallback); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @param string $path |
|
127
|
|
|
* @param mixed $fallback |
|
128
|
|
|
* @param string $cast |
|
129
|
|
|
* @return string|array |
|
130
|
|
|
*/ |
|
131
|
|
|
function glsr_get_option($path = '', $fallback = '', $cast = '') |
|
132
|
|
|
{ |
|
133
|
|
|
return is_string($path) |
|
|
|
|
|
|
134
|
|
|
? glsr('Database\OptionManager')->get(\GeminiLabs\SiteReviews\Helpers\Str::prefix('settings.', $path), $fallback, $cast) |
|
135
|
|
|
: $fallback; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @return array |
|
140
|
|
|
*/ |
|
141
|
|
|
function glsr_get_options() |
|
142
|
|
|
{ |
|
143
|
|
|
return glsr('Database\OptionManager')->get('settings'); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* @return object |
|
148
|
|
|
*/ |
|
149
|
|
|
function glsr_get_rating($args = array()) |
|
150
|
|
|
{ |
|
151
|
|
|
$args = \GeminiLabs\SiteReviews\Helpers\Arr::consolidateArray($args); |
|
152
|
|
|
$counts = glsr('Database\ReviewManager')->getRatingCounts($args); |
|
153
|
|
|
return (object) array( |
|
154
|
|
|
'average' => glsr('Modules\Rating')->getAverage($counts), |
|
155
|
|
|
'maximum' => glsr()->constant('MAX_RATING', \GeminiLabs\SiteReviews\Modules\Rating::class), |
|
156
|
|
|
'minimum' => glsr()->constant('MIN_RATING', \GeminiLabs\SiteReviews\Modules\Rating::class), |
|
157
|
|
|
'ratings' => $counts, |
|
158
|
|
|
'reviews' => array_sum($counts), |
|
159
|
|
|
); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* @param \WP_Post|int $post |
|
164
|
|
|
* @return \GeminiLabs\SiteReviews\Review |
|
165
|
|
|
*/ |
|
166
|
|
|
function glsr_get_review($post) |
|
167
|
|
|
{ |
|
168
|
|
|
if (is_numeric($post)) { |
|
169
|
|
|
$post = get_post($post); |
|
170
|
|
|
} |
|
171
|
|
|
if (!($post instanceof WP_Post)) { |
|
172
|
|
|
$post = new WP_Post((object) []); |
|
173
|
|
|
} |
|
174
|
|
|
return glsr('Database\ReviewManager')->single($post); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* @return array |
|
179
|
|
|
*/ |
|
180
|
|
|
function glsr_get_reviews($args = array()) |
|
181
|
|
|
{ |
|
182
|
|
|
return glsr('Database\ReviewManager')->get(\GeminiLabs\SiteReviews\Helpers\Arr::consolidateArray($args)); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* @return \GeminiLabs\SiteReviews\Modules\Console |
|
187
|
|
|
*/ |
|
188
|
|
|
function glsr_log() |
|
189
|
|
|
{ |
|
190
|
|
|
$args = func_get_args(); |
|
191
|
|
|
$console = glsr('Modules\Console'); |
|
192
|
|
|
if ($value = \GeminiLabs\SiteReviews\Helpers\Arr::get($args, '0')) { |
|
193
|
|
|
return $console->debug($value, \GeminiLabs\SiteReviews\Helpers\Arr::get($args, '1', [])); |
|
194
|
|
|
} |
|
195
|
|
|
return $console; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* @return string |
|
200
|
|
|
*/ |
|
201
|
|
|
function glsr_star_rating($rating) |
|
202
|
|
|
{ |
|
203
|
|
|
return glsr('Modules\Html\Partial')->build('star-rating', ['rating' => $rating]); |
|
204
|
|
|
} |
|
205
|
|
|
|