PluginProbe
Edit Flow / 0.9.9
Edit Flow v0.9.9
0.11.1 0.11.0 0.7.2 0.7.3 0.7.4 0.7.5 0.7.6 0.8 0.8.1 0.8.2 0.9 0.9.1 0.9.2 0.9.3 0.9.4 0.9.5 0.9.6 0.9.7 0.9.8 0.9.9 trunk 0.1.5 0.10.0 0.10.1 0.10.2 All 44 releases
edit-flow / common / php / screen-options.php

screen-options.php in Edit Flow 0.9.9, at common/php/screen-options.php

284 lines 8.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 // Retrieved from this blog post: http://w-shadow.com/blog/2010/06/29/adding-stuff-to-wordpress-screen-options/
4
5 if ( !class_exists('wsScreenOptions10') ):
6
7 /**
8 * Class for adding new panels to the "Screen Options" box.
9 *
10 * Do not access this class directly. Instead, use the add_screen_options_panel() function.
11 *
12 * @author Janis Elsts
13 * @copyright 2010
14 * @version 1.0
15 * @access public
16 */
17 class wsScreenOptions10 {
18 var $registered_panels; //List of custom "Screen Options" panels
19 var $page_panels; //Index of panels registered for each page ($page => array of panel ids).
20
21 /**
22 * Class constructor
23 *
24 * @return void
25 */
26 function __construct(){
27 $this->registered_panels = array();
28 $this->page_panels = array();
29
30 add_filter('screen_settings', array( $this, 'append_screen_settings' ), 10, 2);
31 add_action('admin_print_scripts', array( $this, 'add_autosave_script' ) );
32 }
33
34 /**
35 * Add a new settings panel to the "Screen Options" box.
36 *
37 * @param string $id String to use in the 'id' attribute of the settings panel. Should be unique.
38 * @param string $title Title of the settings panel. Set to an empty string to omit title.
39 * @param callback $callback Function that fills the panel with the desired content. Should return its output.
40 * @param string|array $page The page(s) on which to show the panel (similar to add_meta_box()).
41 * @param callback $save_callback Optional. Function that saves the settings.
42 * @param bool $autosave Optional. If se, settings will be automatically saved (via AJAX) when the value of any input element in the panel changes. Defaults to false.
43 * @return void
44 */
45 function add_screen_options_panel($id, $title, $callback, $page, $save_callback = null, $autosave = false){
46 if ( !is_array($page) ){
47 $page = array($page);
48 }
49 //Convert page hooks/slugs to screen IDs
50 $page = array_map( array( $this, 'page_to_screen_id' ), $page);
51 $page = array_unique($page);
52
53 $new_panel = array(
54 'title' => $title,
55 'callback' => $callback,
56 'page' => $page,
57 'save_callback' => $save_callback,
58 'autosave' => $autosave,
59 );
60
61 if ( $save_callback ){
62 add_action('wp_ajax_save_settings-' . $id, array( $this, 'ajax_save_callback' ) );
63 }
64
65 //Store the panel ID in each relevant page's list
66 foreach($page as $page_id){
67 if ( !isset($this->page_panels[$page_id]) ){
68 $this->page_panels[$page_id] = array();
69 }
70 $this->page_panels[$page_id][] = $id;
71 }
72
73 $this->registered_panels[$id] = $new_panel;
74 }
75
76 /**
77 * Convert a page hook name to a screen ID.
78 *
79 * @uses convert_to_screen()
80 * @access private
81 *
82 * @param string $page
83 * @return string
84 */
85 function page_to_screen_id($page){
86 if ( function_exists('convert_to_screen') ){
87 $screen = convert_to_screen($page);
88 if ( isset($screen->id) ){
89 return $screen->id;
90 } else {
91 return '';
92 }
93 } else {
94 return str_replace( array('.php', '-new', '-add' ), '', $page);
95 }
96 }
97
98 /**
99 * Append custom panel HTML to the "Screen Options" box of the current page.
100 * Callback for the 'screen_settings' filter (available in WP 3.0 and up).
101 *
102 * @access private
103 *
104 * @param string $current
105 * @param string $screen Screen object (undocumented).
106 * @return string The HTML code to append to "Screen Options"
107 */
108 function append_screen_settings($current, $screen){
109 global $hook_suffix;
110
111 //Sanity check
112 if ( !isset($screen->id) ) {
113 return $current;
114 }
115
116 //Are there any panels that want to appear on this page?
117 $panels = $this->get_panels_for_screen($screen->id, $hook_suffix);
118 if ( empty($panels) ){
119 return $current;
120 }
121
122 //Append all panels registered for this screen
123 foreach($panels as $panel_id){
124 $panel = $this->registered_panels[$panel_id];
125
126 //Add panel title
127 if ( !empty($panel['title']) ){
128 $current .= "\n<h5>".$panel['title']."</h5>\n";
129 }
130 //Generate panel contents
131 if ( is_callable($panel['callback']) ){
132 $contents = call_user_func($panel['callback']);
133 $classes = array(
134 'metabox-prefs',
135 'custom-options-panel',
136 );
137 if ( $panel['autosave'] ){
138 $classes[] = 'requires-autosave';
139 }
140
141 $contents = sprintf(
142 '<div id="%s" class="%s"><input type="hidden" name="_wpnonce-%s" value="%s" />%s</div>',
143 esc_attr($panel_id),
144 implode(' ',$classes),
145 esc_attr($panel_id),
146 wp_create_nonce('save_settings-'.$panel_id),
147 $contents
148 );
149
150 $current .= $contents;
151 }
152 }
153
154 return $current;
155 }
156
157 /**
158 * AJAX callback for the "Screen Options" autosave.
159 *
160 * @access private
161 * @return void
162 */
163 function ajax_save_callback(){
164 if ( empty($_POST['action']) ){
165 die('0');
166 }
167
168 //The 'action' argument is in the form "save_settings-panel_id"
169 $ids = explode( '-', $_POST['action'], 2 );
170 $id = end( $ids );
171
172 //Basic security check.
173 check_ajax_referer('save_settings-' . $id, '_wpnonce-' . $id);
174
175 //Hand the request to the registered callback, if any
176 if ( !isset($this->registered_panels[$id]) ){
177 exit('0');
178 }
179 $panel = $this->registered_panels[$id];
180 if ( is_callable($panel['save_callback']) ){
181 call_user_func($panel['save_callback'], $_POST);
182 die('1');
183 } else {
184 die('0');
185 }
186 }
187
188 /**
189 * Add/enqueue supporting JavaScript for the autosave function of custom "Screen Options" panels.
190 *
191 * Checks if the current page is supposed to contain any autosave-enabled
192 * panels and adds the script only if that's the case.
193 *
194 * @return void
195 */
196 function add_autosave_script(){
197 //Get the page id/hook/slug/whatever.
198 global $hook_suffix;
199
200 //Check if we have some panels with autosave registered for this page.
201 $panels = $this->get_panels_for_screen('', $hook_suffix);
202 if ( empty($panels) ){
203 return;
204 }
205
206 $got_autosave = false;
207 foreach($panels as $panel_id){
208 if ( $this->registered_panels[$panel_id]['autosave'] ){
209 $got_autosave = true;
210 break;
211 }
212 }
213
214 if ( $got_autosave ){
215 //Enqueue the script itself
216 $url = EDIT_FLOW_URL . '/common/js/screen-options.js';
217 wp_enqueue_script('screen-options-custom-autosave', $url, array('jquery'), EDIT_FLOW_VERSION );
218 }
219 }
220
221 /**
222 * Get custom panels registered for a particular screen and/or page.
223 *
224 * @param string $screen_id Screen ID.
225 * @param string $page Optional. Page filename or hook name.
226 * @return array Array of custom panels.
227 */
228 function get_panels_for_screen($screen_id, $page = ''){
229 if ( isset($this->page_panels[$screen_id]) && !empty($this->page_panels[$screen_id]) ){
230 $panels = $this->page_panels[$screen_id];
231 } else {
232 $panels = array();
233 }
234 if ( !empty($page) ){
235 $page_as_screen = $this->page_to_screen_id($page);
236 if ( isset($this->page_panels[$page_as_screen]) && !empty($this->page_panels[$page_as_screen]) ){
237 $panels = array_merge($panels, $this->page_panels[$page_as_screen]);
238 }
239 }
240 return array_unique($panels);
241 }
242 }
243
244 //All versions of the class are stored in a global array
245 //and only the latest version is actually used.
246 global $ws_screen_options_versions;
247 if ( !isset($ws_screen_options_versions) ){
248 $ws_screen_options_versions = array();
249 }
250 $ws_screen_options_versions['1.0'] = 'wsScreenOptions10';
251
252 endif;
253
254 if ( !function_exists('add_screen_options_panel') ){
255
256 /**
257 * Add a new settings panel to the "Screen Options" box.
258 *
259 * @see wsScreenOptions10::add_screen_options_panel()
260 *
261 * @param string $id String to use in the 'id' attribute of the settings panel. Should be unique.
262 * @param string $title Title of the settings panel. Set to an empty string to omit title.
263 * @param callback $callback Function that fills the panel with the desired content. Should return its output.
264 * @param string|array $page The page(s) on which to show the panel (similar to add_meta_box()).
265 * @param callback $save_callback Optional. Function that saves the settings contained in the panel.
266 * @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.
267 * @return void
268 */
269 function add_screen_options_panel($id, $title, $callback, $page, $save_callback = null, $autosave = false){
270 global $ws_screen_options_versions;
271
272 static $instance = null;
273 if ( is_null($instance) ){
274 //Instantiate the latest version of the wsScreenOptions class
275 uksort($ws_screen_options_versions, 'version_compare');
276 $className = end($ws_screen_options_versions);
277 $instance = new $className;
278 }
279
280 return $instance->add_screen_options_panel($id, $title, $callback, $page, $save_callback, $autosave);
281 }
282
283 }
284