Carbon_Breadcrumb_Admin   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 1
dl 0
loc 133
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
A include_files() 0 8 1
A admin_menu() 0 4 1
A init() 0 4 1
A register_settings() 0 4 1
A apply_settings() 0 12 3
A is_enabled() 0 18 4
A current_dir() 0 3 1
1
<?php
2
/**
3
 * Main breadcrumb administration.
4
 *
5
 * @package carbon-breadcrumbs
6
 */
7
8
/**
9
 * The main breadcrumb administration class.
10
 *
11
 * Includes, wraps and manages the administration functionality.
12
 */
13
class Carbon_Breadcrumb_Admin {
14
15
	/**
16
	 * Settings container.
17
	 *
18
	 * @access public
19
	 *
20
	 * @var Carbon_Breadcrumb_Admin_Settings
21
	 */
22
	public $settings = null;
23
24
	/**
25
	 * Constructor.
26
	 *
27
	 * Creates the administration functionality wrapper.
28
	 *
29
	 * @access public
30
	 */
31
	public function __construct() {
32
		// Include the plugin files.
33
		$this->include_files();
34
35
		// If administration is enabled, initialize.
36
		if ( $this->is_enabled() ) {
37
			add_action( 'init', array( $this, 'init' ) );
38
			add_action( 'admin_menu', array( $this, 'admin_menu' ), 20 );
39
		}
40
	}
41
42
	/**
43
	 * Include the administration files.
44
	 *
45
	 * @access public
46
	 */
47
	public function include_files() {
48
		$dir = dirname( __FILE__ );
49
50
		include_once( $dir . '/class-carbon-breadcrumb-admin-settings.php' );
51
		include_once( $dir . '/class-carbon-breadcrumb-admin-settings-field.php' );
52
		include_once( $dir . '/class-carbon-breadcrumb-admin-settings-field-text.php' );
53
		include_once( $dir . '/class-carbon-breadcrumb-admin-settings-field-checkbox.php' );
54
	}
55
56
	/**
57
	 * Initialize breadcrumb administration.
58
	 *
59
	 * @access public
60
	 */
61
	public function admin_menu() {
62
		// Register settings.
63
		$this->register_settings();
64
	}
65
66
	/**
67
	 * Initialize breadcrumb frontend.
68
	 *
69
	 * @access public
70
	 */
71
	public function init() {
72
		// Apply the breadcrumb renderer settings.
73
		add_filter( 'carbon_breadcrumbs_renderer_default_options', array( $this, 'apply_settings' ), 20 );
74
	}
75
76
	/**
77
	 * Register and setup the settings page and fields.
78
	 *
79
	 * @access public
80
	 */
81
	public function register_settings() {
82
		// Register the settings page and fields.
83
		$this->settings = new Carbon_Breadcrumb_Admin_Settings();
84
	}
85
86
	/**
87
	 * Apply the settings to the breadcrumb trail renderer
88
	 *
89
	 * @access public
90
	 *
91
	 * @param array $settings The default settings.
92
	 * @return array $settings The modified settings.
93
	 */
94
	public function apply_settings( $settings = array() ) {
95
		$settings_fields = Carbon_Breadcrumb_Admin_Settings::get_field_data();
96
97
		foreach ( $settings_fields as $field_id => $field ) {
98
			$settings[ $field_id ] = get_option( 'carbon_breadcrumbs_' . $field_id );
99
			if ( 'checkbox' == $field['type'] ) {
100
				$settings[ $field_id ] = (bool) $settings[ $field_id ];
101
			}
102
		}
103
104
		return $settings;
105
	}
106
107
	/**
108
	 * Whether the administration interface should be enabled.
109
	 *
110
	 * @access public
111
	 *
112
	 * @return bool $is_enabled True if the admin interface is enabled.
113
	 */
114
	public function is_enabled() {
115
		$enabled = false;
116
117
		// Enabled if this plugin is installed as a regular WordPress plugin.
118
		$plugin_path = untrailingslashit( ABSPATH ) . DIRECTORY_SEPARATOR . 'wp-content' . DIRECTORY_SEPARATOR . 'plugins';
119
		$current_dir = $this->current_dir();
120
		if ( false !== strpos( $current_dir, $plugin_path ) ) {
121
			$enabled = true;
122
		}
123
124
		// Enabled if the CARBON_BREADCRUMB_ENABLE_ADMIN is defined as `true`.
125
		if ( defined( 'CARBON_BREADCRUMB_ENABLE_ADMIN' ) && CARBON_BREADCRUMB_ENABLE_ADMIN ) {
126
			$enabled = true;
127
		}
128
129
		// Allow manual enabling/disabling.
130
		return apply_filters( 'carbon_breadcrumb_enable_admin', $enabled );
131
	}
132
133
	/**
134
	 * Returns the current directory.
135
	 * Useful for covering multiple cases in unit tests.
136
	 *
137
	 * @access public
138
	 *
139
	 * @return string The absolute directory of the current file.
140
	 */
141
	public function current_dir() {
142
		return dirname( __FILE__ );
143
	}
144
145
}
146