pryley /
site-reviews
| 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\Helper; |
||||||
| 12 | use GeminiLabs\SiteReviews\Helpers\Arr; |
||||||
| 13 | use GeminiLabs\SiteReviews\Helpers\Cast; |
||||||
| 14 | use GeminiLabs\SiteReviews\Helpers\Str; |
||||||
| 15 | use GeminiLabs\SiteReviews\Modules\Console; |
||||||
| 16 | use GeminiLabs\SiteReviews\Modules\Html\Partial; |
||||||
| 17 | use GeminiLabs\SiteReviews\Modules\Rating; |
||||||
| 18 | use GeminiLabs\SiteReviews\Request; |
||||||
| 19 | |||||||
| 20 | defined('ABSPATH') || die; |
||||||
| 21 | |||||||
| 22 | /* |
||||||
| 23 | * Alternate method of using the functions without having to use `function_exists()` |
||||||
| 24 | * Example: apply_filters('glsr_get_reviews', [], ['assigned_posts' => 'post_id']); |
||||||
| 25 | * @param mixed ... |
||||||
| 26 | * @return mixed |
||||||
| 27 | */ |
||||||
| 28 | add_filter('plugins_loaded', function () { |
||||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||||
| 29 | $hooks = [ |
||||||
| 30 | 'glsr_create_review' => 2, |
||||||
| 31 | 'glsr_debug' => 10, |
||||||
| 32 | 'glsr_get' => 4, |
||||||
| 33 | 'glsr_get_option' => 4, |
||||||
| 34 | 'glsr_get_options' => 1, |
||||||
| 35 | 'glsr_get_rating' => 2, |
||||||
| 36 | 'glsr_get_ratings' => 2, |
||||||
| 37 | 'glsr_get_review' => 2, |
||||||
| 38 | 'glsr_get_reviews' => 2, |
||||||
| 39 | 'glsr_log' => 3, |
||||||
| 40 | 'glsr_star_rating' => 2, |
||||||
| 41 | ]; |
||||||
| 42 | foreach ($hooks as $function => $acceptedArgs) { |
||||||
| 43 | add_filter($function, function () use ($function) { |
||||||
|
0 ignored issues
–
show
The function
add_filter was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 44 | $args = func_get_args(); |
||||||
| 45 | array_shift($args); // remove the fallback value |
||||||
| 46 | return call_user_func_array($function, $args); |
||||||
| 47 | }, 10, $acceptedArgs); |
||||||
| 48 | } |
||||||
| 49 | }); |
||||||
| 50 | |||||||
| 51 | /** |
||||||
| 52 | * @return mixed |
||||||
| 53 | */ |
||||||
| 54 | function glsr($alias = null, array $parameters = []) |
||||||
| 55 | { |
||||||
| 56 | $app = Application::load(); |
||||||
| 57 | if (is_null($alias)) { |
||||||
| 58 | return $app; |
||||||
| 59 | } |
||||||
| 60 | try { |
||||||
| 61 | return $app->make($alias, $parameters); |
||||||
| 62 | } catch (BindingResolutionException $e) { |
||||||
| 63 | glsr_log()->error($e->getMessage()); |
||||||
| 64 | return $app->make(BlackHole::class); |
||||||
| 65 | } |
||||||
| 66 | } |
||||||
| 67 | |||||||
| 68 | /** |
||||||
| 69 | * @return \GeminiLabs\SiteReviews\Review|false |
||||||
| 70 | */ |
||||||
| 71 | function glsr_create_review($reviewValues = []) |
||||||
| 72 | { |
||||||
| 73 | $values = Arr::removeEmptyValues(Arr::consolidate($reviewValues)); |
||||||
| 74 | $request = new Request($values); |
||||||
| 75 | $command = new CreateReview($request); |
||||||
| 76 | return $command->isValid() |
||||||
| 77 | ? glsr(ReviewManager::class)->create($command) |
||||||
| 78 | : false; |
||||||
| 79 | } |
||||||
| 80 | |||||||
| 81 | /** |
||||||
| 82 | * @return \WP_Screen|object |
||||||
|
0 ignored issues
–
show
The type
WP_Screen was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||||||
| 83 | */ |
||||||
| 84 | function glsr_current_screen() |
||||||
| 85 | { |
||||||
| 86 | if (function_exists('get_current_screen')) { |
||||||
| 87 | $screen = get_current_screen(); |
||||||
| 88 | } |
||||||
| 89 | return empty($screen) |
||||||
| 90 | ? (object) array_fill_keys(['base', 'id', 'post_type'], null) |
||||||
| 91 | : $screen; |
||||||
| 92 | } |
||||||
| 93 | |||||||
| 94 | /** |
||||||
| 95 | * @param mixed ...$vars |
||||||
| 96 | * @return void |
||||||
| 97 | */ |
||||||
| 98 | function glsr_debug(...$vars) |
||||||
| 99 | { |
||||||
| 100 | if (1 == count($vars)) { |
||||||
| 101 | $value = htmlspecialchars(print_r($vars[0], true), ENT_QUOTES, 'UTF-8'); |
||||||
| 102 | printf('<div class="glsr-debug"><pre>%s</pre></div>', $value); |
||||||
| 103 | } else { |
||||||
| 104 | echo '<div class="glsr-debug-group">'; |
||||||
| 105 | foreach ($vars as $var) { |
||||||
| 106 | glsr_debug($var); |
||||||
| 107 | } |
||||||
| 108 | echo '</div>'; |
||||||
| 109 | } |
||||||
| 110 | } |
||||||
| 111 | |||||||
| 112 | /** |
||||||
| 113 | * @param array $data |
||||||
| 114 | * @param string $path |
||||||
| 115 | * @param mixed $fallback |
||||||
| 116 | * @return mixed |
||||||
| 117 | */ |
||||||
| 118 | function glsr_get($array, $path = '', $fallback = '') |
||||||
| 119 | { |
||||||
| 120 | return Arr::get($array, $path, $fallback); |
||||||
| 121 | } |
||||||
| 122 | |||||||
| 123 | /** |
||||||
| 124 | * @param string $path |
||||||
| 125 | * @param mixed $fallback |
||||||
| 126 | * @param string $cast |
||||||
| 127 | * @return string|array |
||||||
| 128 | */ |
||||||
| 129 | function glsr_get_option($path = '', $fallback = '', $cast = '') |
||||||
| 130 | { |
||||||
| 131 | return is_string($path) |
||||||
| 132 | ? glsr(OptionManager::class)->get(Str::prefix($path, 'settings.'), $fallback, $cast) |
||||||
| 133 | : $fallback; |
||||||
| 134 | } |
||||||
| 135 | |||||||
| 136 | /** |
||||||
| 137 | * @return array |
||||||
| 138 | */ |
||||||
| 139 | function glsr_get_options() |
||||||
| 140 | { |
||||||
| 141 | return glsr(OptionManager::class)->get('settings'); |
||||||
| 142 | } |
||||||
| 143 | |||||||
| 144 | /** |
||||||
| 145 | * @return \GeminiLabs\SiteReviews\Arguments |
||||||
| 146 | */ |
||||||
| 147 | function glsr_get_ratings($args = []) |
||||||
| 148 | { |
||||||
| 149 | $counts = glsr(RatingManager::class)->ratings(Arr::consolidate($args)); |
||||||
| 150 | return new Arguments([ |
||||||
| 151 | 'average' => glsr(Rating::class)->average($counts), |
||||||
| 152 | 'maximum' => Cast::toInt(glsr()->constant('MAX_RATING', Rating::class)), |
||||||
| 153 | 'minimum' => Cast::toInt(glsr()->constant('MIN_RATING', Rating::class)), |
||||||
| 154 | 'ranking' => glsr(Rating::class)->ranking($counts), |
||||||
| 155 | 'ratings' => $counts, |
||||||
| 156 | 'reviews' => array_sum($counts), |
||||||
| 157 | ]); |
||||||
| 158 | } |
||||||
| 159 | |||||||
| 160 | /** |
||||||
| 161 | * @param int|\WP_Post $postId |
||||||
| 162 | * @return \GeminiLabs\SiteReviews\Review |
||||||
| 163 | */ |
||||||
| 164 | function glsr_get_review($postId) |
||||||
| 165 | { |
||||||
| 166 | return glsr(ReviewManager::class)->get(Helper::getPostId($postId)); |
||||||
| 167 | } |
||||||
| 168 | |||||||
| 169 | /** |
||||||
| 170 | * @return \GeminiLabs\SiteReviews\Reviews |
||||||
| 171 | */ |
||||||
| 172 | function glsr_get_reviews($args = []) |
||||||
| 173 | { |
||||||
| 174 | glsr()->sessionSet('glsr_get_reviews', true); // Tell Site Reviews that the helper function was used |
||||||
| 175 | return glsr(ReviewManager::class)->reviews(Arr::consolidate($args)); |
||||||
| 176 | } |
||||||
| 177 | |||||||
| 178 | /** |
||||||
| 179 | * @return \GeminiLabs\SiteReviews\Modules\Console |
||||||
| 180 | */ |
||||||
| 181 | function glsr_log(...$args) |
||||||
| 182 | { |
||||||
| 183 | $console = glsr(Console::class); |
||||||
| 184 | return !empty($args) |
||||||
| 185 | ? call_user_func_array([$console, 'debug'], $args) |
||||||
| 186 | : $console; |
||||||
| 187 | } |
||||||
| 188 | |||||||
| 189 | /** |
||||||
| 190 | * @param array $array |
||||||
| 191 | * @param string $path |
||||||
| 192 | * @param mixed $value |
||||||
| 193 | * @return array |
||||||
| 194 | */ |
||||||
| 195 | function glsr_set(array $data, $path, $value) |
||||||
| 196 | { |
||||||
| 197 | return Arr::set($data, $path, $value); |
||||||
| 198 | } |
||||||
| 199 | |||||||
| 200 | /** |
||||||
| 201 | * @return string |
||||||
| 202 | */ |
||||||
| 203 | function glsr_star_rating($rating) |
||||||
| 204 | { |
||||||
| 205 | return glsr(Partial::class)->build('star-rating', ['rating' => $rating]); |
||||||
| 206 | } |
||||||
| 207 |