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

MslsOptionsTaxTerm::check_base()   C

Complexity

Conditions 7
Paths 13

Size

Total Lines 33
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 21
nc 13
nop 2
dl 0
loc 33
rs 6.7272
c 0
b 0
f 0
1
<?php
2
/**
3
 * MslsOptionsTaxTerm
4
 * @author Dennis Ploetner <[email protected]>
5
 * @since 0.9.8
6
 */
7
8
namespace lloc\Msls;
9
10
/**
11
 * Tag options
12
 * @package Msls
13
 */
14
class MslsOptionsTaxTerm extends MslsOptionsTax {
15
16
	/**
17
	 * Base option
18
	 * @var string
19
	 */
20
	protected $base_option = 'tag_base';
21
22
	/**
23
	 * Base definition
24
	 * @var string
25
	 */
26
	protected $base_defined = 'tag';
27
28
	/**
29
	 * Rewrite with front
30
	 * @var bool
31
	 */
32
	public $with_front = true;
33
34
	/**
35
	 * Check and correct URL
36
	 * @param string $url
37
	 * @return string
38
	 */
39
	public function check_base( $url, $options ) {
40
		if ( ! is_string( $url ) || empty( $url ) ) {
41
			return $url;
42
		}
43
44
		global $wp_rewrite;
45
46
		$base_defined = $options->base_defined;
47
48
		$permastruct = $wp_rewrite->get_extra_permastruct( $options->get_tax_query() );
49
		if ( $permastruct ) {
50
			$permastruct = explode( '/', $permastruct );
51
			end( $permastruct );
52
			$permastruct = prev( $permastruct );
53
			if ( false !== $permastruct ) {
54
				$base_defined = $permastruct;
55
			}
56
		}
57
58
		$base_option = get_option( $options->base_option );
59
		if ( empty( $base_option ) ) {
60
			$base_option = $options->base_defined;
61
		}
62
63
		if ( $base_defined != $base_option ) {
64
			$search  = '/' . $base_defined . '/';
65
			$replace = '/' . $base_option . '/';
66
			$count   = 1;
67
			$url     = str_replace( $search, $replace, $url, $count );
68
		}
69
70
		return $url;
71
	}
72
73
}
74