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