|
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
|
|
|
* @return array |
|
76
|
|
|
*/ |
|
77
|
1 |
|
public function getTerms( array $args = [] ) |
|
78
|
|
|
{ |
|
79
|
1 |
|
$args = wp_parse_args( $args, [ |
|
80
|
1 |
|
'fields' => 'id=>name', |
|
81
|
|
|
'hide_empty' => false, |
|
82
|
|
|
'taxonomy' => Application::TAXONOMY, |
|
83
|
|
|
]); |
|
84
|
1 |
|
unset( $args['count'] ); // we don't want a term count |
|
85
|
1 |
|
$terms = get_terms( $args ); |
|
86
|
1 |
|
if( is_wp_error( $terms )) { |
|
87
|
|
|
glsr_log()->error( $terms->get_error_message() ); |
|
88
|
|
|
return []; |
|
89
|
|
|
} |
|
90
|
1 |
|
return $terms; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param string $searchTerm |
|
95
|
|
|
* @return void|string |
|
96
|
|
|
*/ |
|
97
|
|
|
public function searchPosts( $searchTerm ) |
|
98
|
|
|
{ |
|
99
|
|
|
$args = [ |
|
100
|
|
|
'post_status' => 'publish', |
|
101
|
|
|
'post_type' => 'any', |
|
102
|
|
|
]; |
|
103
|
|
|
if( is_numeric( $searchTerm )) { |
|
104
|
|
|
$args['post__in'] = [$searchTerm]; |
|
105
|
|
|
} |
|
106
|
|
|
else { |
|
107
|
|
|
$args['orderby'] = 'relevance'; |
|
108
|
|
|
$args['posts_per_page'] = 10; |
|
109
|
|
|
$args['s'] = $searchTerm; |
|
110
|
|
|
} |
|
111
|
|
|
$queryBuilder = glsr( QueryBuilder::class ); |
|
112
|
|
|
add_filter( 'posts_search', [$queryBuilder, 'filterSearchByTitle'], 500, 2 ); |
|
113
|
|
|
$search = new WP_Query( $args ); |
|
114
|
|
|
remove_filter( 'posts_search', [$queryBuilder, 'filterSearchByTitle'], 500 ); |
|
115
|
|
|
if( !$search->have_posts() )return; |
|
116
|
|
|
$results = ''; |
|
117
|
|
|
while( $search->have_posts() ) { |
|
118
|
|
|
$search->the_post(); |
|
119
|
|
|
ob_start(); |
|
120
|
|
|
glsr()->render( 'partials/editor/search-result', [ |
|
121
|
|
|
'ID' => get_the_ID(), |
|
122
|
|
|
'permalink' => esc_url( (string) get_permalink() ), |
|
123
|
|
|
'title' => esc_attr( get_the_title() ), |
|
124
|
|
|
]); |
|
125
|
|
|
$results .= ob_get_clean(); |
|
126
|
|
|
} |
|
127
|
|
|
wp_reset_postdata(); |
|
128
|
|
|
return $results; |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|