Passed
Push — master ( f510e9...9822a2 )
by Paul
10:29
created

SearchPosts   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 25
dl 0
loc 75
ccs 0
cts 51
cp 0
rs 10
c 1
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A postTypes() 0 4 1
A searchById() 0 12 1
A posts() 0 7 2
A searchByTerm() 0 15 1
A render() 0 7 1
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Database\Search;
4
5
use GeminiLabs\SiteReviews\Database;
6
use GeminiLabs\SiteReviews\Database\Query;
7
use GeminiLabs\SiteReviews\Helpers\Str;
8
9
class SearchPosts extends AbstractSearch
10
{
11
    /**
12
     * @return array
13
     */
14
    public function posts()
15
    {
16
        $posts = [];
17
        foreach ($this->results as $result) {
18
            $posts[] = get_post($result->id);
19
        }
20
        return $posts;
21
    }
22
23
    /**
24
     * @return string
25
     */
26
    public function render()
27
    {
28
        return array_reduce($this->posts(), function ($carry, $post) {
29
            return $carry.glsr()->build('partials/editor/search-result', [
30
                'ID' => $post->ID,
31
                'permalink' => esc_url((string) get_permalink($post->ID)),
32
                'title' => esc_attr(get_the_title($post->ID)),
33
            ]);
34
        });
35
    }
36
37
    /**
38
     * @return string
39
     */
40
    protected function postTypes()
41
    {
42
        $types = array_keys(get_post_types(['exclude_from_search' => false]));
43
        return Str::join($types, true);
44
    }
45
46
    /**
47
     * @param int $searchId
48
     * @return array
49
     */
50
    protected function searchById($searchId)
51
    {
52
        $sql = $this->db->prepare("
53
            SELECT p.ID AS id, p.post_title AS name
54
            FROM {$this->db->posts} AS p
55
            WHERE 1=1
56
            AND p.ID = %d
57
            AND p.post_type IN ({$this->postTypes()})
58
            AND p.post_status = 'publish'
59
        ", $searchId);
60
        return glsr(Database::class)->dbGetResults(
61
            glsr(Query::class)->sql($sql)
62
        );
63
    }
64
65
    /**
66
     * @param string $searchTerm
67
     * @return array
68
     */
69
    protected function searchByTerm($searchTerm)
70
    {
71
        $like = '%'.$this->db->esc_like($searchTerm).'%';
72
        $sql = $this->db->prepare("
73
            SELECT p.ID AS id, p.post_title AS name
74
            FROM {$this->db->posts} AS p
75
            WHERE 1=1
76
            AND p.post_title LIKE %s
77
            AND p.post_type IN ({$this->postTypes()})
78
            AND p.post_status = 'publish'
79
            ORDER BY p.post_title LIKE %s DESC, p.post_date DESC
80
            LIMIT 0, 20
81
        ", $like, $like);
82
        return glsr(Database::class)->dbGetResults(
83
            glsr(Query::class)->sql($sql)
84
        );
85
    }
86
}
87