MslsOptionsTax::create()   B
last analyzed

Complexity

Conditions 8
Paths 48

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
nc 48
nop 1
dl 0
loc 33
rs 8.1475
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
	 * @codeCoverageIgnore
32
	 *
33
	 * @param int $id
34
	 *
35
	 * @return MslsOptionsTax
36
	 */
37
	public static function create( $id = 0 ) {
38
		$id  = ! empty( $id ) ? (int) $id : get_queried_object_id();
39
		$req = '';
40
41
		if ( is_admin() ) {
42
			$req = MslsContentTypes::create()->acl_request();
43
		} elseif ( is_category() ) {
44
			$req = 'category';
45
		} elseif ( is_tag( $id ) ) {
46
			$req = 'post_tag';
47
		}
48
49
		switch ( $req ) {
50
			case 'category':
51
				$options = new MslsOptionsTaxTermCategory( $id );
52
				break;
53
			case 'post_tag':
54
				$options = new MslsOptionsTaxTerm( $id );
55
				break;
56
			default:
57
				$options = new MslsOptionsTax( $id );
58
		}
59
60
		if ( $req ) {
61
			add_filter( 'check_url', [ $options, 'check_base' ], 9, 2 );
62
		} else {
63
			global $wp_rewrite;
64
65
			$options->with_front = ! empty( $wp_rewrite->extra_permastructs[ $options->get_tax_query() ]['with_front'] );
66
		}
67
68
		return $options;
69
	}
70
71
	/**
72
	 * Get the queried taxonomy
73
	 * @return string
74
	 */
75
    public function get_tax_query() {
76
        global $wp_query;
77
78
        if ( class_exists('WooCommerce' ) ) {
79
            if ( is_woocommerce() && isset( $wp_query->tax_query->queries[1]['taxonomy'] ) ) {
80
                    return $wp_query->tax_query->queries[1]['taxonomy'];
81
            }
82
        } elseif ( isset( $wp_query->tax_query->queries[0]['taxonomy'] ) ) {
83
            return $wp_query->tax_query->queries[0]['taxonomy'];
84
        }
85
86
        return parent::get_tax_query();
87
    }
88
89
	/**
90
	 * Get postlink
91
	 *
92
	 * @param string $language
93
	 *
94
	 * @return string
95
	 */
96
	public function get_postlink( $language ) {
97
		$url = '';
98
99
		if ( $this->has_value( $language ) ) {
100
			$url = $this->get_term_link( (int) $this->__get( $language ) );
101
		}
102
103
		return apply_filters( 'check_url', $url, $this );
104
	}
105
106
	/**
107
	 * Get current link
108
	 * @return string
109
	 */
110
	public function get_current_link() {
111
		return $this->get_term_link( $this->get_arg( 0, 0 ) );
112
	}
113
114
	/**
115
	 * Wraps the call to get_term_link
116
	 *
117
	 * @param int $term_id
118
	 *
119
	 * @return string
120
	 */
121
	public function get_term_link( $term_id ) {
122
		if ( ! empty( $term_id ) ) {
123
			$taxonomy = $this->get_tax_query();
124
			if ( ! empty( $taxonomy ) ) {
125
				$link = get_term_link( $term_id, $taxonomy );
126
				if ( ! is_wp_error( $link ) ) {
127
					return $link;
128
				}
129
			}
130
		}
131
132
		return '';
133
	}
134
135
}
136