Passed
Push — master ( 3e60cb...36c2a1 )
by Paul
07:09 queued 03:38
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()->info( __( 'Recalculated rating counts.', 'site-reviews' ));
61
}
62
63
/**
64
 * @return \GeminiLabs\SiteReviews\Review|false
65
 */
66
function glsr_create_review( $reviewValues = array() ) {
67
	if( !is_array( $reviewValues )) {
68
		$reviewValues = array();
69
	}
70
	$review = new \GeminiLabs\SiteReviews\Commands\CreateReview( $reviewValues );
71
	return glsr( 'Database\ReviewManager' )->create( $review );
72
}
73
74
/**
75
 * @return \WP_Screen|object
76
 */
77
function glsr_current_screen() {
78
	if( function_exists( 'get_current_screen' )) {
79
		$screen = get_current_screen();
80
	}
81
	return empty( $screen )
82
		? (object)array_fill_keys( ['base', 'id', 'post_type'], null )
83
		: $screen;
84
}
85
86
/**
87
 * @param mixed ...$vars
88
 * @return void
89
 */
90
function glsr_debug( ...$vars ) {
91
	if( count( $vars ) == 1 ) {
92
		$value = htmlspecialchars( print_r( $vars[0], true ), ENT_QUOTES, 'UTF-8' );
93
		printf( '<div class="glsr-debug"><pre>%s</pre></div>', $value );
94
	}
95
	else {
96
		echo '<div class="glsr-debug-group">';
97
		foreach( $vars as $var ) {
98
			glsr_debug( $var );
99
		}
100
		echo '</div>';
101
	}
102
}
103
104
/**
105
 * @param string $path
106
 * @param mixed $fallback
107
 * @return string|array
108
 */
109
function glsr_get_option( $path = '', $fallback = '' ) {
110
	return is_string( $path )
111
		? glsr( 'Database\OptionManager' )->get( 'settings.'.$path, $fallback )
112
		: $fallback;
113
}
114
115
/**
116
 * @return array
117
 */
118
function glsr_get_options() {
119
	return glsr( 'Database\OptionManager' )->get( 'settings' );
120
}
121
122
/**
123
 * @param int $post_id
124
 * @return \GeminiLabs\SiteReviews\Review
125
 */
126
function glsr_get_review( $post_id ) {
127
	$post = null;
128
	if( is_numeric( $post_id )) {
129
		$post = get_post( $post_id );
130
	}
131
	if( !( $post instanceof WP_Post )) {
132
		$post = new WP_Post( (object)[] );
133
	}
134
	return glsr( 'Database\ReviewManager' )->single( $post );
135
}
136
137
/**
138
 * @return array
139
 */
140
function glsr_get_reviews( $args = array() ) {
141
	if( !is_array( $args )) {
142
		$args = [];
143
	}
144
	return glsr( 'Database\ReviewManager' )->get( $args );
145
}
146
147
/**
148
 * @return \GeminiLabs\SiteReviews\Modules\Console
149
 */
150
function glsr_log() {
151
	$args = func_get_args();
152
	$context = isset( $args[1] )
153
		? $args[1]
154
		: [];
155
	$console = glsr( 'Modules\Console' );
156
	return !empty( $args )
157
		? $console->log( 'debug', $args[0], $context )
158
		: $console;
159
}
160
161
/**
162
 * @return string
163
 */
164
function glsr_star_rating( $rating ) {
165
	return glsr( 'Modules\Html\Partial' )->build( 'star-rating', ['rating' => $rating] );
166
}
167