Test Failed
Push — hotfix/fix-counts ( e9420b...4803a6 )
by Paul
05:49
created

TaxonomyController::restrictTermSelection()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 6
c 1
b 0
f 0
nc 5
nop 6
dl 0
loc 9
ccs 0
cts 7
cp 0
crap 30
rs 9.6111
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 ))return;
39
		echo glsr( Builder::class )->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
	 * @param int $postId
60
	 * @param array $terms
61
	 * @param array $newTTIds
62
	 * @param string $taxonomy
63
	 * @param bool $append
64
	 * @param array $oldTTIds
65
	 * @return void
66
	 * @action set_object_terms
67
	 */
68
	public function restrictTermSelection( $postId, $terms, $newTTIds, $taxonomy, $append, $oldTTIds )
69
	{
70
		if( $taxonomy != Application::TAXONOMY || count( $newTTIds ) <= 1 )return;
71
		$diff = array_diff( $newTTIds, $oldTTIds );
72
		if( empty( $newTerm = array_shift( $diff ))) {
73
			$newTerm = array_shift( $newTTIds );
74
		}
75
		if( $newTerm ) {
76
			wp_set_object_terms( $postId, intval( $newTerm ), $taxonomy );
77
		}
78
	}
79
80
	/**
81
	 * @return string
82
	 */
83
	protected function getSelected()
84
	{
85
		global $wp_query;
86
		return glsr_get( $wp_query->query, Application::TAXONOMY );
87
	}
88
89
	/**
90
	 * @return string
91
	 */
92
	protected function getShowOptionAll()
93
	{
94
		$taxonomy = get_taxonomy( Application::TAXONOMY );
95
		return $taxonomy
96
			? ucfirst( strtolower( $taxonomy->labels->all_items ))
97
			: '';
98
	}
99
}
100