Passed
Push — master ( 41584c...c36188 )
by Paul
12:21
created

glsr_trace()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 1
c 1
b 0
f 1
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
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\Backtrace;
16
use GeminiLabs\SiteReviews\Modules\Console;
17
use GeminiLabs\SiteReviews\Modules\Html\Partial;
18
use GeminiLabs\SiteReviews\Modules\Rating;
19
use GeminiLabs\SiteReviews\Request;
20
21
defined('ABSPATH') || die;
22
23
/*
24
 * Alternate method of using the functions without having to use `function_exists()`
25
 * Example: apply_filters('glsr_get_reviews', [], ['assigned_posts' => 'post_id']);
26
 * @param mixed ...
27
 * @return mixed
28
 */
29
add_filter('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_rating' => 2,
37
        'glsr_get_ratings' => 2,
38
        'glsr_get_review' => 2,
39
        'glsr_get_reviews' => 2,
40
        'glsr_log' => 3,
41
        'glsr_star_rating' => 2,
42
        'glsr_trace' => 2,
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
    $app = Application::load();
59
    if (is_null($alias)) {
60
        return $app;
61
    }
62
    try {
63
        return $app->make($alias, $parameters);
64
    } catch (BindingResolutionException $e) {
65
        glsr_log()->error($e->getMessage());
66
        return $app->make(BlackHole::class, compact('alias'));
67
    }
68
}
69
70
/**
71
 * @return \GeminiLabs\SiteReviews\Review|false
72
 */
73
function glsr_create_review($reviewValues = [])
74
{
75
    $values = Arr::removeEmptyValues(Arr::consolidate($reviewValues));
76
    $request = new Request($values);
77
    $command = new CreateReview($request);
78
    return $command->isValid()
79
        ? glsr(ReviewManager::class)->create($command)
80
        : false;
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 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(OptionManager::class)->get(Str::prefix($path, 'settings.'), $fallback, $cast)
135
        : $fallback;
136
}
137
138
/**
139
 * @return array
140
 */
141
function glsr_get_options()
142
{
143
    return glsr(OptionManager::class)->get('settings');
144
}
145
146
/**
147
 * @return \GeminiLabs\SiteReviews\Arguments
148
 */
149
function glsr_get_ratings($args = [])
150
{
151
    $counts = glsr(RatingManager::class)->ratings(Arr::consolidate($args));
152
    return new Arguments([
153
        'average' => glsr(Rating::class)->average($counts),
154
        'maximum' => Cast::toInt(glsr()->constant('MAX_RATING', Rating::class)),
155
        'minimum' => Cast::toInt(glsr()->constant('MIN_RATING', Rating::class)),
156
        'ranking' => glsr(Rating::class)->ranking($counts),
157
        'ratings' => $counts,
158
        'reviews' => array_sum($counts),
159
    ]);
160
}
161
162
/**
163
 * @param int|\WP_Post $postId
164
 * @return \GeminiLabs\SiteReviews\Review
165
 */
166
function glsr_get_review($postId)
167
{
168
    return glsr(ReviewManager::class)->get(Helper::getPostId($postId));
169
}
170
171
/**
172
 * @return \GeminiLabs\SiteReviews\Reviews
173
 */
174
function glsr_get_reviews($args = [])
175
{
176
    glsr()->sessionSet('glsr_get_reviews', true); // Tell Site Reviews that the helper function was used
177
    return glsr(ReviewManager::class)->reviews(Arr::consolidate($args));
178
}
179
180
/**
181
 * @return \GeminiLabs\SiteReviews\Modules\Console
182
 */
183
function glsr_log(...$args)
184
{
185
    $console = glsr(Console::class);
186
    return !empty($args)
187
        ? call_user_func_array([$console, 'debug'], $args)
188
        : $console;
189
}
190
191
/**
192
 * @param array $array
193
 * @param string $path
194
 * @param mixed $value
195
 * @return array
196
 */
197
function glsr_set(array $data, $path, $value)
198
{
199
    return Arr::set($data, $path, $value);
200
}
201
202
/**
203
 * @param int $rating
204
 * @param int $count
205
 * @return string
206
 */
207
function glsr_star_rating($rating, $count = 0)
208
{
209
    return glsr(Partial::class)->build('star-rating', [
210
        'count' => $count,
211
        'rating' => $rating,
212
    ]);
213
}
214
215
/**
216
 * @param int $limit
217
 * @return void
218
 */
219
function glsr_trace($limit = 5)
220
{
221
    glsr_log(glsr(Backtrace::class)->trace($limit));
222
}
223