Skip to content

Commit b9e34c3

Browse files
author
András Guseo
committed
Adding Settings_Helper
1 parent f63d8fb commit b9e34c3

File tree

1 file changed

+174
-0
lines changed

1 file changed

+174
-0
lines changed

src/Tribe/Settings_Helper.php

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

0 commit comments

Comments
 (0)