Passed
Push — master ( 054ae2...bf35ce )
by Paul
04:22
created

helpers.php (1 issue)

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