Passed
Push — master ( 9a7a6b...7de9ff )
by Paul
03:37
created

helpers.php (2 issues)

Severity
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_create_review',
16
		'glsr_debug',
17
		'glsr_get_option', 'glsr_get_options',
18
		'glsr_get_review', 'glsr_get_reviews',
19
		'glsr_log',
20
	);
21
	if( !in_array( $hook, $hooks ) || !function_exists( $hook ))return;
22
	add_filter( $hook, function() use( $hook, $args ) {
23
		array_shift( $args ); // remove the fallback value
24
		return call_user_func_array( $hook, $args );
25
	});
26
});
27
28
/**
29
 * @return mixed
30
 */
31
function glsr( $alias = null ) {
32
	$app = \GeminiLabs\SiteReviews\Application::load();
33
	return !empty( $alias )
34
		? $app->make( $alias )
35
		: $app;
36
}
37
38
/**
39
 * @return null|\GeminiLabs\SiteReviews\Review
40
 */
41
function glsr_create_review( $reviewValues = array() ) {
42
	if( !is_array( $reviewValues )) {
43
		$reviewValues = array();
44
	}
45
	$review = new \GeminiLabs\SiteReviews\Commands\CreateReview( $reviewValues );
46
	$result = glsr( 'Database\ReviewManager' )->create( $review );
47
	return !empty( $result )
48
		? $result
49
		: null;
50
}
51
52
/**
53
 * @return \WP_Screen|object
54
 */
55
function glsr_current_screen() {
56
	if( function_exists( 'get_current_screen' )) {
57
		$screen = get_current_screen();
58
	}
59
	return empty( $screen )
60
		? (object)array_fill_keys( ['base', 'id', 'post_type'], null )
61
		: $screen;
62
}
63
64
/**
65
 * @param mixed ...$vars
66
 * @return void
67
 */
68
function glsr_debug( ...$vars ) {
69
	if( count( $vars ) == 1 ) {
70
		$value = htmlspecialchars( print_r( $vars[0], true ), ENT_QUOTES, 'UTF-8' );
71
		printf( '<div class="glsr-debug"><pre>%s</pre></div>', $value );
72
	}
73
	else {
74
		echo '<div class="glsr-debug-group">';
75
		foreach( $vars as $var ) {
76
			glsr_debug( $var );
77
		}
78
		echo '</div>';
79
	}
80
}
81
82
/**
83
 * @param string $path
84
 * @param mixed $fallback
85
 * @return string|array
86
 */
87
function glsr_get_option( $path = '', $fallback = '' ) {
88
	return is_string( $path )
1 ignored issue
show
The condition is_string($path) is always true.
Loading history...
89
		? glsr( 'Database\OptionManager' )->get( 'settings.'.$path, $fallback )
90
		: $fallback;
91
}
92
93
/**
94
 * @return array
95
 */
96
function glsr_get_options() {
97
	return glsr( 'Database\OptionManager' )->get( 'settings' );
98
}
99
100
/**
101
 * @param int $post_id
102
 * @return \GeminiLabs\SiteReviews\Review|void
103
 */
104
function glsr_get_review( $post_id ) {
105
	if( !is_numeric( $post_id ))return;
1 ignored issue
show
The condition is_numeric($post_id) is always true.
Loading history...
106
	$post = get_post( $post_id );
107
	if( $post instanceof WP_Post ) {
108
		return glsr( 'Database\ReviewManager' )->single( $post );
109
	}
110
}
111
112
/**
113
 * @return array
114
 * @todo document change of $reviews->reviews to $reviews->results
115
 */
116
function glsr_get_reviews( $args = array() ) {
117
	if( !is_array( $args )) {
118
		$args = [];
119
	}
120
	return glsr( 'Database\ReviewManager' )->get( $args );
121
}
122
123
/**
124
 * @return \GeminiLabs\SiteReviews\Modules\Console
125
 */
126
function glsr_log() {
127
	$args = func_get_args();
128
	$context = isset( $args[1] )
129
		? $args[1]
130
		: [];
131
	$console = glsr( 'Modules\Console' );
132
	return !empty( $args )
133
		? $console->log( 'debug', $args[0], $context )
134
		: $console;
135
}
136