Passed
Push — master ( ece31d...41b8a6 )
by Paul
10:20 queued 04:17
created

TaxonomyController   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Test Coverage

Coverage 8.11%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 35
c 1
b 0
f 0
dl 0
loc 94
ccs 3
cts 37
cp 0.0811
rs 10
wmc 12

6 Methods

Rating   Name   Duplication   Size   Complexity  
A enableParents() 0 4 1
A disableParents() 0 4 1
A restrictTermSelection() 0 11 5
A renderTaxonomyFilter() 0 21 2
A getShowOptionAll() 0 6 2
A getSelected() 0 4 1
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Controllers;
4
5
use GeminiLabs\SiteReviews\Application;
6
use GeminiLabs\SiteReviews\Modules\Html\Builder;
7
8
class TaxonomyController
9
{
10
    /**
11
     * @return void
12
     * @action Application::TAXONOMY._add_form_fields
13
     * @action Application::TAXONOMY._edit_form
14
     */
15
    public function disableParents()
16
    {
17
        global $wp_taxonomies;
18
        $wp_taxonomies[Application::TAXONOMY]->hierarchical = false;
19
    }
20
21
    /**
22
     * @return void
23
     * @action Application::TAXONOMY._term_edit_form_top
24
     * @action Application::TAXONOMY._term_new_form_tag
25
     */
26
    public function enableParents()
27
    {
28
        global $wp_taxonomies;
29
        $wp_taxonomies[Application::TAXONOMY]->hierarchical = true;
30
    }
31
32
    /**
33
     * @return void
34
     * @action restrict_manage_posts
35
     */
36
    public function renderTaxonomyFilter()
37
    {
38
        if (!is_object_in_taxonomy(glsr_current_screen()->post_type, Application::TAXONOMY)) {
39
            return;
40
        }
41
        echo glsr(Builder::class)->label(__('Filter by category', 'site-reviews'), [
42
            'class' => 'screen-reader-text',
43
            'for' => Application::TAXONOMY,
44
        ]);
45
        wp_dropdown_categories([
46
            'depth' => 3,
47
            'hide_empty' => true,
48
            'hide_if_empty' => true,
49
            'hierarchical' => true,
50
            'name' => Application::TAXONOMY,
51
            'orderby' => 'name',
52
            'selected' => $this->getSelected(),
53
            'show_count' => false,
54
            'show_option_all' => $this->getShowOptionAll(),
55
            'taxonomy' => Application::TAXONOMY,
56
            'value_field' => 'slug',
57
        ]);
58
    }
59
60
    /**
61
     * @param int $postId
62
     * @param array $terms
63
     * @param array $newTTIds
64
     * @param string $taxonomy
65
     * @param bool $append
66
     * @param array $oldTTIds
67
     * @return void
68
     * @action set_object_terms
69
     */
70 1
    public function restrictTermSelection($postId, $terms, $newTTIds, $taxonomy, $append, $oldTTIds)
71
    {
72 1
        if (Application::TAXONOMY != $taxonomy || count($newTTIds) <= 1) {
73 1
            return;
74
        }
75
        $diff = array_diff($newTTIds, $oldTTIds);
76
        if (empty($newTerm = array_shift($diff))) {
77
            $newTerm = array_shift($newTTIds);
78
        }
79
        if ($newTerm) {
80
            wp_set_object_terms($postId, intval($newTerm), $taxonomy);
81
        }
82
    }
83
84
    /**
85
     * @return string
86
     */
87
    protected function getSelected()
88
    {
89
        global $wp_query;
90
        return glsr_get($wp_query->query, Application::TAXONOMY);
91
    }
92
93
    /**
94
     * @return string
95
     */
96
    protected function getShowOptionAll()
97
    {
98
        $taxonomy = get_taxonomy(Application::TAXONOMY);
99
        return $taxonomy
100
            ? ucfirst(strtolower($taxonomy->labels->all_items))
101
            : '';
102
    }
103
}
104