|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews; |
|
4
|
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Database\Query; |
|
6
|
|
|
use GeminiLabs\SiteReviews\Database\QuerySql; |
|
7
|
|
|
use GeminiLabs\SiteReviews\Database\SqlSchema; |
|
8
|
|
|
use GeminiLabs\SiteReviews\Defaults\RatingDefaults; |
|
9
|
|
|
use GeminiLabs\SiteReviews\Helper; |
|
10
|
|
|
use GeminiLabs\SiteReviews\Helpers\Str; |
|
11
|
|
|
use WP_Query; |
|
12
|
|
|
|
|
13
|
|
|
class Database |
|
14
|
|
|
{ |
|
15
|
|
|
protected $db; |
|
16
|
|
|
|
|
17
|
|
|
public function __construct() |
|
18
|
|
|
{ |
|
19
|
|
|
global $wpdb; |
|
20
|
|
|
$this->db = $wpdb; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @return void |
|
25
|
|
|
*/ |
|
26
|
|
|
public function createTables() |
|
27
|
|
|
{ |
|
28
|
|
|
glsr(SqlSchema::class)->createTables(); |
|
29
|
|
|
glsr(SqlSchema::class)->addTableConstraints(); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param int $reviewId |
|
34
|
|
|
* @return int|false |
|
35
|
|
|
*/ |
|
36
|
|
|
public function delete($reviewId) |
|
37
|
|
|
{ |
|
38
|
|
|
return $this->db->delete(glsr(Query::class)->table('ratings'), [ |
|
39
|
|
|
'review_id' => $reviewId, |
|
40
|
|
|
]); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Search SQL filter for matching against post title only. |
|
45
|
|
|
* @see http://wordpress.stackexchange.com/a/11826/1685 |
|
46
|
|
|
* @param string $search |
|
47
|
|
|
* @return string |
|
48
|
|
|
* @filter posts_search |
|
49
|
|
|
*/ |
|
50
|
|
|
public function filterSearchByTitle($search, WP_Query $query) |
|
51
|
|
|
{ |
|
52
|
|
|
if (empty($search) || empty($query->get('search_terms'))) { |
|
53
|
|
|
return $search; |
|
54
|
|
|
} |
|
55
|
|
|
global $wpdb; |
|
56
|
|
|
$n = empty($query->get('exact')) |
|
57
|
|
|
? '%' |
|
58
|
|
|
: ''; |
|
59
|
|
|
$search = []; |
|
60
|
|
|
foreach ((array) $query->get('search_terms') as $term) { |
|
61
|
|
|
$search[] = $wpdb->prepare("{$wpdb->posts}.post_title LIKE %s", $n.$wpdb->esc_like($term).$n); |
|
62
|
|
|
} |
|
63
|
|
|
if (!is_user_logged_in()) { |
|
64
|
|
|
$search[] = "{$wpdb->posts}.post_password = ''"; |
|
65
|
|
|
} |
|
66
|
|
|
return ' AND '.implode(' AND ', $search); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param int $reviewId |
|
71
|
|
|
* @return \GeminiLabs\SiteReviews\Review|false |
|
72
|
|
|
*/ |
|
73
|
|
|
public function insert($reviewId, array $data = []) |
|
74
|
|
|
{ |
|
75
|
|
|
$defaults = glsr(RatingDefaults::class)->restrict($data); |
|
76
|
|
|
$data = Arr::set($defaults, 'review_id', $reviewId); |
|
|
|
|
|
|
77
|
|
|
$result = $this->insertRaw(glsr(Query::class)->table('ratings'), $data); |
|
78
|
|
|
return (false !== $result) |
|
79
|
|
|
? glsr(ReviewManager::class)->get($reviewId) |
|
|
|
|
|
|
80
|
|
|
: false; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param string $table |
|
85
|
|
|
* @return int|false |
|
86
|
|
|
*/ |
|
87
|
|
|
public function insertBulk($table, array $values, array $fields) |
|
88
|
|
|
{ |
|
89
|
|
|
$this->db->insert_id = 0; |
|
90
|
|
|
$data = []; |
|
91
|
|
|
foreach ($values as $value) { |
|
92
|
|
|
$value = array_intersect_key($value, array_flip($fields)); // only keep field values |
|
93
|
|
|
if (count($value) === count($fields)) { |
|
94
|
|
|
$value = array_merge(array_flip($fields), $value); // make sure the order is correct |
|
95
|
|
|
$data[] = glsr(QuerySql::class)->escValuesForInsert($value); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
$table = glsr(Query::class)->table($table); |
|
99
|
|
|
$fields = glsr(QuerySql::class)->escFieldsForInsert(array_keys($data)); |
|
100
|
|
|
$values = implode(',', array_values($data)); |
|
101
|
|
|
return $this->db->query("INSERT IGNORE INTO {$table} {$fields} VALUES {$values}"); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @param string $table |
|
106
|
|
|
* @return int|false |
|
107
|
|
|
*/ |
|
108
|
|
|
public function insertRaw($table, array $data) |
|
109
|
|
|
{ |
|
110
|
|
|
$this->db->insert_id = 0; |
|
111
|
|
|
$fields = glsr(QuerySql::class)->escFieldsForInsert(array_keys($data)); |
|
112
|
|
|
$values = glsr(QuerySql::class)->escValuesForInsert($data); |
|
113
|
|
|
return $this->db->query("INSERT IGNORE INTO {$table} {$fields} VALUES {$values}"); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Do not remove this as it has been given in code snippets. |
|
118
|
|
|
* @return array |
|
119
|
|
|
*/ |
|
120
|
|
|
public function getTerms(array $args = []) |
|
121
|
|
|
{ |
|
122
|
|
|
$args = wp_parse_args($args, [ |
|
123
|
|
|
'count' => false, |
|
124
|
|
|
'fields' => 'id=>name', |
|
125
|
|
|
'hide_empty' => false, |
|
126
|
|
|
'taxonomy' => glsr()->taxonomy, |
|
127
|
|
|
]); |
|
128
|
|
|
$terms = get_terms($args); |
|
129
|
|
|
if (is_wp_error($terms)) { |
|
130
|
|
|
glsr_log()->error($terms->get_error_message()); |
|
131
|
|
|
return []; |
|
132
|
|
|
} |
|
133
|
|
|
return $terms; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @return bool |
|
138
|
|
|
*/ |
|
139
|
|
|
public function isMigrationNeeded() |
|
140
|
|
|
{ |
|
141
|
|
|
global $wpdb; |
|
142
|
|
|
$table = glsr(Query::class)->table('ratings'); |
|
143
|
|
|
$postCount = wp_count_posts(glsr()->post_type)->publish; |
|
144
|
|
|
if (empty($postCount)) { |
|
145
|
|
|
return false; |
|
146
|
|
|
} |
|
147
|
|
|
if (!empty($wpdb->get_var("SELECT COUNT(*) FROM {$table} WHERE is_approved = 1"))) { |
|
148
|
|
|
return false; |
|
149
|
|
|
} |
|
150
|
|
|
return true; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* @param int $postId |
|
155
|
|
|
* @param string $key |
|
156
|
|
|
* @param bool $single |
|
157
|
|
|
* @return mixed |
|
158
|
|
|
*/ |
|
159
|
|
|
public function meta($postId, $key, $single = true) |
|
160
|
|
|
{ |
|
161
|
|
|
$key = Str::prefix('_', $key); |
|
162
|
|
|
return get_post_meta(Helper::castToInt($postId), $key, $single); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* @param string $searchTerm |
|
167
|
|
|
* @return void|string |
|
168
|
|
|
*/ |
|
169
|
|
|
public function searchPosts($searchTerm) |
|
170
|
|
|
{ |
|
171
|
|
|
$args = [ |
|
172
|
|
|
'post_status' => 'publish', |
|
173
|
|
|
'post_type' => 'any', |
|
174
|
|
|
]; |
|
175
|
|
|
if (is_numeric($searchTerm)) { |
|
176
|
|
|
$args['post__in'] = [$searchTerm]; |
|
177
|
|
|
} else { |
|
178
|
|
|
$args['orderby'] = 'relevance'; |
|
179
|
|
|
$args['posts_per_page'] = 10; |
|
180
|
|
|
$args['s'] = $searchTerm; |
|
181
|
|
|
} |
|
182
|
|
|
add_filter('posts_search', [$this, 'filterSearchByTitle'], 500, 2); |
|
183
|
|
|
$search = new WP_Query($args); |
|
184
|
|
|
remove_filter('posts_search', [$this, 'filterSearchByTitle'], 500); |
|
185
|
|
|
if ($search->have_posts()) { |
|
186
|
|
|
$results = ''; |
|
187
|
|
|
while ($search->have_posts()) { |
|
188
|
|
|
$search->the_post(); |
|
189
|
|
|
ob_start(); |
|
190
|
|
|
glsr()->render('partials/editor/search-result', [ |
|
191
|
|
|
'ID' => get_the_ID(), |
|
192
|
|
|
'permalink' => esc_url((string) get_permalink()), |
|
193
|
|
|
'title' => esc_attr(get_the_title()), |
|
194
|
|
|
]); |
|
195
|
|
|
$results .= ob_get_clean(); |
|
196
|
|
|
} |
|
197
|
|
|
wp_reset_postdata(); |
|
198
|
|
|
return $results; |
|
199
|
|
|
} |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* @param int $reviewId |
|
204
|
|
|
* @return int|bool |
|
205
|
|
|
*/ |
|
206
|
|
|
public function update($reviewId, array $data = []) |
|
207
|
|
|
{ |
|
208
|
|
|
$defaults = glsr(RatingDefaults::class)->restrict($data); |
|
209
|
|
|
$data = array_intersect_key($data, $defaults); |
|
210
|
|
|
return $this->db->update(glsr(Query::class)->table('ratings'), $data, [ |
|
211
|
|
|
'review_id' => $reviewId, |
|
212
|
|
|
]); |
|
213
|
|
|
} |
|
214
|
|
|
} |
|
215
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths