Passed
Push — master ( 78492a...4f268f )
by Paul
04:26
created

TaxonomyController::getSelected()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 6
ccs 0
cts 5
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Controllers;
4
5
use GeminiLabs\SiteReviews\Application;
6
use GeminiLabs\SiteReviews\Modules\Html;
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( get_current_screen()->post_type, Application::TAXONOMY ))return;
39
		glsr( Html::class )->render()->label( __( 'Filter by category', 'site-reviews' ), [
40
			'class' => 'screen-reader-text',
41
			'for' => Application::TAXONOMY,
42
		]);
43
		wp_dropdown_categories([
44
			'depth' => 3,
45
			'hide_empty' => true,
46
			'hide_if_empty' => true,
47
			'hierarchical' => true,
48
			'name' => Application::TAXONOMY,
49
			'orderby' => 'name',
50
			'selected' => $this->getSelected(),
51
			'show_count' => false,
52
			'show_option_all' => $this->getShowOptionAll(),
53
			'taxonomy' => Application::TAXONOMY,
54
			'value_field' => 'slug',
55
		]);
56
	}
57
58
	/**
59
	 * @return string
60
	 */
61
	protected function getSelected()
62
	{
63
		global $wp_query;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
64
		return isset( $wp_query->query[Application::TAXONOMY] )
65
			? $wp_query->query[Application::TAXONOMY]
66
			: '';
67
	}
68
69
	/**
70
	 * @return string
71
	 */
72
	protected function getShowOptionAll()
73
	{
74
		$taxonomy = get_taxonomy( Application::TAXONOMY );
75
		return $taxonomy
76
			? ucfirst( strtolower( $taxonomy->labels->all_items ))
77
			: '';
78
	}
79
}
80