Passed
Push — master ( 4562dd...74d0bd )
by Paul
04:12
created

glsr_get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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