-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSettings_Helper.php
174 lines (144 loc) · 5.36 KB
/
Settings_Helper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
// Do not load directly.
if ( ! defined( 'ABSPATH' ) ) {
die( '-1' );
}
if ( class_exists( 'Tribe__Settings_Helper' ) ) {
return;
}
/**
* Helper for inserting/removing fields on the WP Admin Tribe Settings pages
*/
class Tribe__Settings_Helper {
/**
* Fields inserted into misc section
*
* @var array
*/
private $insert_fields_misc = array();
/**
* Fields that will be inserted above a specified field
*
* @var array
*/
private $insert_fields_above = array();
/**
* Fields that will be inserted below a specified field
*
* @var array
*/
private $insert_fields_below = array();
/**
* Array of settings being added to a Tribe Settings tab
*
* @var array
*/
private $remove_fields = array();
/**
* Setup the helper
*
* @param int $priority Priority at which this hooks into 'tribe_settings_tab_fields'.
*/
public function __construct( $priority = 100 ) {
add_filter( 'tribe_settings_tab_fields', array( $this, 'filter_options' ), $priority, 2 );
}
/**
* Add a field to a Tribe Settings tab
*
* @param string $field_key Option key for your setting. Example: 'fancyOptionName'.
* @param array $field_args See Tribe__Field() for available args.
* Example: array( 'type' => 'checkbox_bool, 'label' => ... )
* @param string $setting_tab Settings tab where this will be added. Example: 'display'.
* @param string $neighboring_field (optional) The field key/HTML name="" attribute to insert this under.
* @param bool $above (optional) Insert above or below its neighbor.
*/
public function add_field( $field_key, $field_args, $setting_tab, $neighboring_field = null, $above = true ) {
// Our settings walker needs 'key' => arg pairs.
$field = array( $field_key => $field_args );
$this->add_fields( $field, $setting_tab, $neighboring_field, $above );
}
/**
* Add multiple fields to a Tribe Settings tab
*
* @param array $fields Fields that will be added, expects 'fieldname' => (array) args.
* @param string $setting_tab Settings tab where this will be added. Example: 'display'.
* @param string $neighboring_field (optional) The field key/HTML name="" attribute to insert this under.
* @param bool $above (optional) Insert above or below its neighbor.
*/
public function add_fields( $fields, $setting_tab, $neighboring_field = null, $above = false ) {
if ( ! is_string( $neighboring_field ) ) {
// If neighbor is not specified, add this to misc section.
$this->insert_fields_misc = array_replace_recursive(
$this->insert_fields_misc,
array( $setting_tab => $fields )
);
} elseif ( true === $above ) {
// Add to above fields list with neighbor specified.
$this->insert_fields_above = array_replace_recursive(
$this->insert_fields_above,
array( $setting_tab => array( $neighboring_field => $fields ) )
);
} else {
// Add to below fields list with neighbor specified.
$this->insert_fields_below = array_replace_recursive(
$this->insert_fields_below,
array( $setting_tab => array( $neighboring_field => $fields ) )
);
}
}
/**
* Remove a field from one of the tabs in WP Admin > Events > Settings
*
* @param string $field_key Option key for your setting. Example: 'fancyOptionName'.
* @param string $setting_tab Settings tab where this will be added. Example: 'display'.
*/
public function remove_field( $field_key, $setting_tab ) {
$this->remove_fields[ $setting_tab ][] = $field_key;
}
/**
* Attached to 'tribe_settings_tab_fields' to add/remove this class' fields on Tribe Settings pages.
*
* @param array $fields The fields within tribe settings page.
* @param string $tab The settings tab key.
*
* @return array $fields The fields within tribe settings page
*/
public function filter_options( $fields, $tab ) {
// Fields appended to misc section.
if ( array_key_exists( $tab, $this->insert_fields_misc ) ) {
// Add a misc heading if none exists.
if ( ! array_key_exists( 'tribeMiscSettings', $fields ) ) {
$misc_heading = array(
'tribeMiscSettings' => array(
'type' => 'html',
'html' => '<h3>' . esc_html__( 'Miscellaneous Settings', 'tribe-common' ) . '</h3>',
),
);
$fields = Tribe__Main::array_insert_before_key( 'tribe-form-content-end', $fields, $misc_heading );
}
// Insert these settings under misc heading.
$fields = Tribe__Main::array_insert_after_key( 'tribeMiscSettings', $fields, $this->insert_fields_misc[ $tab ] );
}
// Fields inserted above a neighboring field.
if ( array_key_exists( $tab, $this->insert_fields_above ) ) {
foreach ( $this->insert_fields_above[ $tab ] as $insert_after => $new_field ) {
$fields = Tribe__Main::array_insert_before_key( $insert_after, $fields, $new_field );
}
}
// Fields inserted below a neighboring field.
if ( array_key_exists( $tab, $this->insert_fields_below ) ) {
foreach ( $this->insert_fields_below[ $tab ] as $insert_after => $new_field ) {
$fields = Tribe__Main::array_insert_after_key( $insert_after, $fields, $new_field );
}
}
// Fields that will be removed.
if ( array_key_exists( $tab, $this->remove_fields ) ) {
foreach ( $this->remove_fields[ $tab ] as $remove_field ) {
if ( array_key_exists( $remove_field, $fields ) ) {
unset( $fields[ $remove_field ] );
}
}
}
return $fields;
}
}