/
class.w4g-challenge-admin.php
221 lines (193 loc) · 9.12 KB
/
class.w4g-challenge-admin.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php
class W4G_Challenge_Admin {
public function __construct() {
// Hook into the admin menu
add_action( 'admin_menu', array( $this, 'create_plugin_settings_page' ) );
// Add Settings and Fields
add_action( 'admin_init', array( $this, 'setup_sections' ) );
add_action( 'admin_init', array( $this, 'setup_fields' ) );
}
public function create_plugin_settings_page() {
// Add the menu item and page
$page_title = 'Challenge Settings Page';
$menu_title = 'Challenge';
$capability = 'manage_options';
$slug = 'w4g_challenge_fields';
$callback = array( $this, 'plugin_settings_page_content' );
$icon = 'dashicons-admin-plugins';
$position = 100;
add_options_page( $page_title, $menu_title, $capability, $slug, $callback, $icon, $position );
}
public function plugin_settings_page_content() {?>
<div class="wrap">
<h2>Challenge Settings</h2><?php
if ( isset( $_GET['settings-updated'] ) && $_GET['settings-updated'] ){
// $this->admin_notice();
} ?>
<form method="POST" action="options.php">
<?php
settings_fields( 'w4g_challenge_fields' );
do_settings_sections( 'w4g_challenge_fields' );
submit_button();
?>
</form>
</div> <?php
}
public function admin_notice() { ?>
<div class="notice notice-success is-dismissible">
<p>Your settings have been updated!</p>
</div><?php
}
public function setup_sections() {
add_settings_section( 'our_second_section', 'Challenge Status', array( $this, 'section_callback' ), 'w4g_challenge_fields' );
add_settings_section( 'our_first_section', 'Application Settings', array( $this, 'section_callback' ), 'w4g_challenge_fields' );
add_settings_section( 'our_third_section', 'Application Page', array( $this, 'section_callback' ), 'w4g_challenge_fields' );
}
public function section_callback( $arguments ) {
switch( $arguments['id'] ){
case 'our_first_section':
echo 'These settings are required before running your challenge.';
break;
case 'our_second_section':
echo 'Control whether the challenge application is open or closed.';
break;
case 'our_third_section':
echo 'Messaging for the Application Page.';
break;
}
}
public function setup_fields() {
$fields = array(
array(
'uid' => 'challenge_status',
'label' => 'Challenge Status',
'section' => 'our_second_section',
'type' => 'radio',
'options' => array(
'inactive' => 'Pre-Challenge: <em>application is closed</em>',
'active' => 'Active: <em>application is open</em>',
'ended' => 'Post-Challenge: <em>application is closed</em>',
),
'default' => array('inactive')
),
array(
'uid' => 'custom_fields_id',
'label' => 'ACF Field Group ID',
'section' => 'our_first_section',
'type' => 'text',
'placeholder' => 'ID',
'helper' => '<a href="#">How to add fields to the Application</a>',
'supplimental' => 'Add additional fields to the application.',
),
array(
'uid' => 'challenge_edit',
'label' => 'Editing Capabilities',
'section' => 'our_first_section',
'type' => 'radio',
'options' => array(
'allow' => 'Allow applicants to edit their own applications after submitting and while challenge is active',
'deny' => "Don't allow applicants to edit their own applications after submitting",
),
'default' => array('allow')
),
array(
'uid' => 'challenge_information',
'label' => 'Pre-Challenge Messaging',
'section' => 'our_third_section',
'type' => 'textarea',
'placeholder' => '',
'helper' => '',
'supplimental' => 'Appears above the application while challenge status is Pre-Challenge',
),
array(
'uid' => 'challenge_instructions',
'label' => 'Active Challenge Messaging - Logged In',
'section' => 'our_third_section',
'placeholder' => '',
'type' => 'textarea',
'supplimental' => 'Appears above the application to users who are logged in while challenge status is Active',
),
array(
'uid' => 'challenge_logged_out_message',
'label' => 'Active Challenge Messaging - Logged Out',
'section' => 'our_third_section',
'placeholder' => '',
'type' => 'textarea',
'supplimental' => 'Appears above the application to users who are not logged in while challenge status is Active'
),
array(
'uid' => 'challenge_ended_message',
'label' => 'Post-Challenge Messaging',
'section' => 'our_third_section',
'placeholder' => '',
'type' => 'textarea',
'supplimental' => 'Appears above the application while challenge status is Pre-Challenge',
),
);
foreach( $fields as $field ){
add_settings_field( $field['uid'], $field['label'], array( $this, 'field_callback' ), 'w4g_challenge_fields', $field['section'], $field );
register_setting( 'w4g_challenge_fields', $field['uid'] );
}
}
public function field_callback( $arguments ) {
$default = isset( $arguments['default'] ) ? $arguments['default'] : '';
$value = get_option( $arguments['uid'], $default );
switch( $arguments['type'] ){
case 'text':
case 'password':
case 'number':
printf( '<input name="%1$s" id="%1$s" type="%2$s" placeholder="%3$s" value="%4$s" />', $arguments['uid'], $arguments['type'], $arguments['placeholder'], $value );
break;
case 'textarea':
printf( '<textarea name="%1$s" id="%1$s" placeholder="%2$s" rows="5" cols="50">%3$s</textarea>', $arguments['uid'], $arguments['placeholder'], $value );
break;
case 'select':
case 'multiselect':
if( ! empty ( $arguments['options'] ) && is_array( $arguments['options'] ) ) {
$attributes = '';
$options_markup = '';
if( !is_array( $value ) ) {
$value = array( $value );
}
foreach( $arguments['options'] as $key => $label ) {
$selected = '';
if( in_array( $key, $value ) ) {
$selected = "selected='selected'";
}
$options_markup .= sprintf( '<option value="%s" %s>%s</option>', $key, $selected, $label );
}
if( $arguments['type'] === 'multiselect' ){
$attributes = ' multiple="multiple" ';
}
printf( '<select name="%1$s[]" id="%1$s" %2$s>%3$s</select>', $arguments['uid'], $attributes, $options_markup );
}
break;
case 'radio':
case 'checkbox':
if( ! empty ( $arguments['options'] ) && is_array( $arguments['options'] ) ) {
$options_markup = '';
$iterator = 0;
if( !is_array( $value ) ) {
$value = array( $value );
}
foreach( $arguments['options'] as $key => $label ) {
$checked = '';
if( in_array( $key, $value ) ) {
$checked = "checked='checked'";
}
$iterator++;
$options_markup .= sprintf( '<label for="%1$s_%6$s"><input id="%1$s_%6$s" name="%1$s[]" type="%2$s" value="%3$s" %4$s /> %5$s</label><br/>', $arguments['uid'], $arguments['type'], $key, $checked, $label, $iterator );
}
printf( '<fieldset>%s</fieldset>', $options_markup );
}
break;
}
if( isset( $arguments['helper'] ) && !empty( $arguments['helper'] ) ) {
printf( '<span class="helper"> %s</span>', $arguments['helper'] );
}
if( isset( $arguments['supplimental'] ) && !empty( $arguments['supplimental'] ) ){
printf( '<p class="description">%s</p>', $arguments['supplimental'] );
}
}
}
new W4G_Challenge_Admin();