Passed
Branch master (2fcc75)
by Paul
11:46
created

Database::getTerms()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0625

Importance

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