Completed
Push — master ( ea28d3...1760c1 )
by Dennis
04:45
created

MslsOptionsTax::create()   C

Complexity

Conditions 7
Paths 30

Size

Total Lines 31
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 23
nc 30
nop 1
dl 0
loc 31
rs 6.7272
c 0
b 0
f 0
1
<?php
2
/**
3
 * MslsOptionsTax
4
 * @author Dennis Ploetner <[email protected]>
5
 * @since 0.9.8
6
 */
7
8
namespace lloc\Msls;
9
10
/**
11
 * Taxonomy options
12
 * @package Msls
13
 */
14
class MslsOptionsTax extends MslsOptions {
15
16
	/**
17
	 * Separator
18
	 * @var string
19
	 */
20
	protected $sep = '_term_';
21
22
	/**
23
	 * Autoload
24
	 * @var string
25
	 */
26
	protected $autoload = 'no';
27
28
	/**
29
	 * Factory method
30
	 *
31
	 * @param int $id
32
	 *
33
	 * @return MslsOptionsTax
34
	 */
35
	public static function create( $id = 0 ) {
36
		if ( is_admin() ) {
37
			$obj = MslsContentTypes::create();
38
39
			$id  = (int) $id;
40
			$req = $obj->acl_request();
41
		} else {
42
			$id  = get_queried_object_id();
43
			$req = is_category() ? 'category' : is_tag() ? 'post_tag' : '';
44
		}
45
46
		switch ( $req ) {
47
			case 'category':
48
				$options = new MslsOptionsTaxTermCategory( $id );
49
				break;
50
			case 'post_tag':
51
				$options = new MslsOptionsTaxTerm( $id );
52
				break;
53
			default:
54
				$options = new MslsOptionsTax( $id );
55
		}
56
57
		if ( $req ) {
58
			add_filter( 'check_url', [ $options, 'check_base' ], 9, 2 );
59
		} else {
60
			global $wp_rewrite;
61
			$options->with_front = ! empty( $wp_rewrite->extra_permastructs[ $options->get_tax_query() ]['with_front'] );
62
		}
63
64
		return $options;
65
	}
66
67
	/**
68
	 * Get the queried taxonomy
69
	 * @return string
70
	 */
71
	public function get_tax_query() {
72
		global $wp_query;
73
74
		return isset( $wp_query->tax_query->queries[0]['taxonomy'] ) ?
75
			$wp_query->tax_query->queries[0]['taxonomy'] :
76
			'';
77
	}
78
79
	/**
80
	 * Get postlink
81
	 *
82
	 * @param string $language
83
	 *
84
	 * @return string
85
	 */
86
	public function get_postlink( $language ) {
87
		$url = '';
88
89
		if ( $this->has_value( $language ) ) {
90
			$url = $this->get_term_link( (int) $this->__get( $language ) );
91
		}
92
93
		return apply_filters( 'check_url', $url, $this );
94
	}
95
96
	/**
97
	 * Get current link
98
	 * @return string
99
	 */
100
	public function get_current_link() {
101
		return $this->get_term_link( $this->get_arg( 0, 0 ) );
102
	}
103
104
	/**
105
	 * Wraps the call to get_term_link
106
	 *
107
	 * @param int $term_id
108
	 *
109
	 * @return string
110
	 */
111
	public function get_term_link( $term_id ) {
112
		if ( ! empty( $term_id ) ) {
113
			$taxonomy = $this->get_tax_query();
114
			if ( ! empty( $taxonomy ) ) {
115
				$link = get_term_link( $term_id, $taxonomy );
116
				if ( ! is_wp_error( $link ) ) {
117
					return $link;
118
				}
119
			}
120
		}
121
122
		return '';
123
	}
124
125
}
126