Passed
Push — master ( e9a082...4a454f )
by Paul
03:30
created

Database::getTermIds()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 2
dl 0
loc 9
ccs 0
cts 7
cp 0
crap 12
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace GeminiLabs\SiteReviews;
4
5
use GeminiLabs\SiteReviews\Application;
6
use GeminiLabs\SiteReviews\Database\Cache;
7
use GeminiLabs\SiteReviews\Database\OptionManager;
8
use GeminiLabs\SiteReviews\Database\QueryBuilder;
9
use GeminiLabs\SiteReviews\Database\SqlQueries;
10
use GeminiLabs\SiteReviews\Modules\Rating;
11
use WP_Post;
12
use WP_Query;
13
14
class Database
15
{
16
	/**
17
	 * @param int $postId
18
	 * @param string $assignedTo
19
	 * @return void|WP_Post
20
	 */
21
	public function getAssignedToPost( $postId, $assignedTo = '' )
22
	{
23
		if( empty( $assignedTo )) {
24
			$assignedTo = get_post_meta( $postId, 'assigned_to', true );
25
		}
26
		if( empty( $assignedTo ))return;
27
		$assignedPost = get_post( $assignedTo );
28
		if( $assignedPost instanceof WP_Post && $assignedPost->ID != $postId ) {
29
			return $assignedPost;
30
		}
31
	}
32
33
	/**
34
	 * @param string $metaKey
35
	 * @param string $metaValue
36
	 * @return array|int
37
	 */
38
	public function getReviewCount( $metaKey = '', $metaValue = '' )
39
	{
40
		if( !$metaKey ) {
41
			return (array)wp_count_posts( Application::POST_TYPE );
42
		}
43
		$counts = glsr( Cache::class )->getReviewCountsFor( $metaKey );
44
		if( !$metaValue ) {
45
			return $counts;
46
		}
47
		return isset( $counts[$metaValue] )
48
			? $counts[$metaValue]
49
			: 0;
50
	}
51
52
	/**
53
	 * @param string $metaReviewType
54
	 * @return array
55
	 */
56
	public function getReviewIdsByType( $metaReviewType )
57
	{
58
		return glsr( SqlQueries::class )->getReviewIdsByType( $metaReviewType );
59
	}
60
61
	/**
62
	 * @param string $key
63
	 * @param string $status
64
	 * @return array
65
	 */
66
	public function getReviewsMeta( $key, $status = 'publish' )
67
	{
68
		if( $status == 'all' || empty( $status )) {
69
			$status = get_post_stati( ['exclude_from_search' => false] );
70
		}
71
		return glsr( SqlQueries::class )->getReviewsMeta( $key, $status );
72
	}
73
74
	/**
75
	 * @param string $field
76
	 * @return array
77
	 */
78
	public function getTermIds( array $values, $field )
79
	{
80
		$termIds = [];
81
		foreach( $values as $value ) {
82
			$term = get_term_by( $field, $value, Application::TAXONOMY );
83
			if( !isset( $term->term_id ))continue;
84
			$termIds[] = $term->term_id;
85
		}
86
		return $termIds;
87
	}
88
89
	/**
90
	 * @return array
91
	 */
92 1
	public function getTerms( array $args = [] )
93
	{
94 1
		$args = wp_parse_args( $args, [
95 1
			'count' => false,
96
			'fields' => 'id=>name',
97
			'hide_empty' => false,
98
			'taxonomy' => Application::TAXONOMY,
99
		]);
100 1
		$terms = get_terms( $args );
101 1
		if( is_wp_error( $terms )) {
102
			glsr_log()->error( $terms->get_error_message() );
103
			return [];
104
		}
105 1
		return $terms;
106
	}
107
108
	/**
109
	 * @param string $searchTerm
110
	 * @return void|string
111
	 */
112
	public function searchPosts( $searchTerm )
113
	{
114
		$args = [
115
			'post_status' => 'publish',
116
			'post_type' => 'any',
117
		];
118
		if( is_numeric( $searchTerm )) {
119
			$args['post__in'] = [$searchTerm];
120
		}
121
		else {
122
			$args['orderby'] = 'relevance';
123
			$args['posts_per_page'] = 10;
124
			$args['s'] = $searchTerm;
125
		}
126
		$queryBuilder = glsr( QueryBuilder::class );
127
		add_filter( 'posts_search', [$queryBuilder, 'filterSearchByTitle'], 500, 2 );
128
		$search = new WP_Query( $args );
129
		remove_filter( 'posts_search', [$queryBuilder, 'filterSearchByTitle'], 500 );
130
		if( !$search->have_posts() )return;
131
		$results = '';
132
		while( $search->have_posts() ) {
133
			$search->the_post();
134
			ob_start();
135
			glsr()->render( 'partials/editor/search-result', [
136
				'ID' => get_the_ID(),
137
				'permalink' => esc_url( (string) get_permalink() ),
138
				'title' => esc_attr( get_the_title() ),
139
			]);
140
			$results .= ob_get_clean();
141
		}
142
		wp_reset_postdata();
143
		return $results;
144
	}
145
}
146