screen-options.php
9.34 KB
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
<?php
if ( ! class_exists( 'wsScreenOptions12' ) ) :
/**
* Class for adding new panels to the "Screen Options" box.
*
* Do not access this class directly. Instead, use the add_screen_options_panel() function.
*
* @author Janis Elsts
* @copyright 2014
* @version 1.3
* @access public
*/
class wsScreenOptions13 {
var $registered_panels; //List of custom "Screen Options" panels
var $page_panels; //Index of panels registered for each page ($page => array of panel ids).
/**
* Class constructor
*
* @return void
*/
function init() {
$this->registered_panels = array();
$this->page_panels = array();
add_action( 'current_screen', array( $this, 'populate_page_panels' ) );
add_filter( 'screen_settings', array( &$this, 'append_screen_settings' ), 10, 2 );
add_action( 'admin_print_scripts', array( &$this, 'add_autosave_script' ) );
}
/**
* Add a new settings panel to the "Screen Options" box.
*
* @param string $id String to use in the 'id' attribute of the settings panel. Should be unique.
* @param string $title Title of the settings panel. Set to an empty string to omit title.
* @param callback $callback Function that fills the panel with the desired content. Should return its output.
* @param string|array $page The page(s) on which to show the panel (similar to add_meta_box()).
* @param callback $save_callback Optional. Function that saves the settings.
* @param bool $autosave Optional. If set, settings will be automatically saved (via AJAX) when the value of any input element in the panel changes. Defaults to false.
* @return void
*/
function add_screen_options_panel( $id, $title, $callback, $page, $save_callback = null, $autosave = false ) {
if ( ! is_array( $page ) ) {
$page = array( $page );
}
$new_panel = array(
'title' => $title,
'callback' => $callback,
'page' => $page,
'save_callback' => $save_callback,
'autosave' => $autosave,
);
$this->registered_panels[ $id ] = $new_panel;
if ( $save_callback ) {
add_action( 'wp_ajax_save_settings-' . $id, array( $this, 'ajax_save_callback' ) );
}
}
/**
* Populate a lookup array for screen -> panels queries.
*
* This is a callback for the "current_screen" action. We have to do it in this hook or WordPress will
* complain about "doing it wrong" and incorrectly suggest using the "add_meta_boxes" action.
*
* "add_meta_boxes" doesn't work here because it only gets called on CPT pages and we want the ability
* to add screen options to any page.
*/
function populate_page_panels() {
foreach ( $this->registered_panels as $id => $panel ) {
$page = $panel['page'];
//Convert page hooks/slugs to screen IDs
$page = array_map( array( $this, 'page_to_screen_id' ), $page );
$page = array_unique( $page );
//Store the panel ID in each relevant page's list
foreach ( $page as $page_id ) {
if ( ! isset( $this->page_panels[ $page_id ] ) ) {
$this->page_panels[ $page_id ] = array();
}
$this->page_panels[ $page_id ][] = $id;
}
}
}
/**
* Convert a page hook name to a screen ID.
*
* @uses convert_to_screen()
* @access private
*
* @param string $page
* @return string
*/
function page_to_screen_id( $page ) {
if ( function_exists( 'convert_to_screen' ) ) {
$screen = convert_to_screen( $page );
if ( isset( $screen->id ) ) {
return $screen->id;
} else {
return '';
}
} else {
return str_replace( array( '.php', '-new', '-add' ), '', $page );
}
}
/**
* Append custom panel HTML to the "Screen Options" box of the current page.
* Callback for the 'screen_settings' filter (available in WP 3.0 and up).
*
* @access private
*
* @param string $current
* @param string $screen Screen object (undocumented).
* @return string The HTML code to append to "Screen Options"
*/
function append_screen_settings( $current, $screen ) {
global $hook_suffix;
//Sanity check
if ( ! isset( $screen->id ) ) {
return $current;
}
//Are there any panels that want to appear on this page?
$panels = $this->get_panels_for_screen( $screen->id, $hook_suffix );
if ( empty( $panels ) ) {
return $current;
}
//Append all panels registered for this screen
foreach ( $panels as $panel_id ) {
$panel = $this->registered_panels[ $panel_id ];
//Add panel title
if ( ! empty( $panel['title'] ) ) {
$current .= "\n<h5>" . $panel['title'] . "</h5>\n";
}
//Generate panel contents
if ( is_callable( $panel['callback'] ) ) {
$contents = call_user_func( $panel['callback'] );
$classes = array(
'custom-options-panel',
);
if ( $panel['autosave'] ) {
$classes[] = 'requires-autosave';
}
$contents = sprintf(
'<div id="%s" class="%s"><input type="hidden" name="_wpnonce-%s" value="%s" />%s</div>',
esc_attr( $panel_id ),
implode( ' ', $classes ),
esc_attr( $panel_id ),
wp_create_nonce( 'save_settings-' . $panel_id ),
$contents
);
$current .= $contents;
}
}
return $current;
}
/**
* AJAX callback for the "Screen Options" autosave.
*
* @access private
* @return void
*/
function ajax_save_callback() {
if ( empty( $_POST['action'] ) ) {
die( '0' );
}
//The 'action' argument is in the form "save_settings-panel_id"
$id = end( explode( '-', $_POST['action'], 2 ) );
//Basic security check.
check_ajax_referer( 'save_settings-' . $id, '_wpnonce-' . $id );
//Hand the request to the registered callback, if any
if ( ! isset( $this->registered_panels[ $id ] ) ) {
exit( '0' );
}
$panel = $this->registered_panels[ $id ];
if ( is_callable( $panel['save_callback'] ) ) {
call_user_func( $panel['save_callback'], $_POST );
die( '1' );
} else {
die( '0' );
}
}
/**
* Add/enqueue supporting JavaScript for the autosave function of custom "Screen Options" panels.
*
* Checks if the current page is supposed to contain any autosave-enabled
* panels and adds the script only if that's the case.
*
* @return void
*/
function add_autosave_script() {
//Get the page id/hook/slug/whatever.
global $hook_suffix;
//Check if we have some panels with autosave registered for this page.
$panels = $this->get_panels_for_screen( '', $hook_suffix );
if ( empty( $panels ) ) {
return;
}
$got_autosave = false;
foreach ( $panels as $panel_id ) {
if ( $this->registered_panels[ $panel_id ]['autosave'] ) {
$got_autosave = true;
break;
}
}
if ( $got_autosave ) {
//Enqueue the script itself
$url = plugins_url( 'screen-options.js', __FILE__ );
wp_enqueue_script( 'screen-options-custom-autosave', $url, array( 'jquery' ) );
}
}
/**
* Get custom panels registered for a particular screen and/or page.
*
* @param string $screen_id Screen ID.
* @param string $page Optional. Page filename or hook name.
* @return array Array of custom panels.
*/
function get_panels_for_screen( $screen_id, $page = '' ) {
if ( isset( $this->page_panels[ $screen_id ] ) && ! empty( $this->page_panels[ $screen_id ] ) ) {
$panels = $this->page_panels[ $screen_id ];
} else {
$panels = array();
}
if ( ! empty( $page ) ) {
$page_as_screen = $this->page_to_screen_id( $page );
if ( isset( $this->page_panels[ $page_as_screen ] ) && ! empty( $this->page_panels[ $page_as_screen ] ) ) {
$panels = array_merge( $panels, $this->page_panels[ $page_as_screen ] );
}
}
return array_unique( $panels );
}
}
//All versions of the class are stored in a global array
//and only the latest version is actually used.
global $ws_screen_options_versions;
if ( ! isset( $ws_screen_options_versions ) ) {
$ws_screen_options_versions = array();
}
$ws_screen_options_versions['1.3'] = 'wsScreenOptions13';
endif;
if ( ! function_exists( 'add_screen_options_panel' ) ) {
/**
* Add a new settings panel to the "Screen Options" box.
*
* @see wsScreenOptions10::add_screen_options_panel()
*
* @param string $id String to use in the 'id' attribute of the settings panel. Should be unique.
* @param string $title Title of the settings panel. Set to an empty string to omit title.
* @param callback $callback Function that fills the panel with the desired content. Should return its output.
* @param string|array $page The page(s) on which to show the panel (similar to add_meta_box()).
* @param callback $save_callback Optional. Function that saves the settings contained in the panel.
* @param bool $autosave Optional. If set, settings will be automatically saved (via AJAX) when the value of any input element in the panel changes. Defaults to false.
* @return void
*/
function add_screen_options_panel( $id, $title, $callback, $page, $save_callback = null, $autosave = false ) {
global $ws_screen_options_versions;
static $instance = null; /** @var wsScreenOptions13 $instance */
if ( is_null( $instance ) ) {
//Instantiate the latest version of the wsScreenOptions class
uksort( $ws_screen_options_versions, 'version_compare' );
$className = end( $ws_screen_options_versions );
$instance = new $className;
$instance->init();
}
$instance->add_screen_options_panel( $id, $title, $callback, $page, $save_callback, $autosave );
}
}