author | ymh <ymh.work@gmail.com> |
Tue, 15 Dec 2020 13:49:49 +0100 | |
changeset 16 | a86126ab1dd4 |
parent 9 | 177826044cd9 |
child 18 | be944660c56a |
permissions | -rw-r--r-- |
0 | 1 |
<?php |
2 |
/** |
|
3 |
* WordPress Administration Screen API. |
|
4 |
* |
|
5 |
* @package WordPress |
|
6 |
* @subpackage Administration |
|
7 |
*/ |
|
8 |
||
9 |
/** |
|
10 |
* Get the column headers for a screen |
|
11 |
* |
|
12 |
* @since 2.7.0 |
|
13 |
* |
|
14 |
* @param string|WP_Screen $screen The screen you want the headers for |
|
16 | 15 |
* @return string[] The column header labels keyed by column ID. |
0 | 16 |
*/ |
17 |
function get_column_headers( $screen ) { |
|
9 | 18 |
if ( is_string( $screen ) ) { |
0 | 19 |
$screen = convert_to_screen( $screen ); |
9 | 20 |
} |
0 | 21 |
|
22 |
static $column_headers = array(); |
|
23 |
||
5 | 24 |
if ( ! isset( $column_headers[ $screen->id ] ) ) { |
25 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
26 |
* Filters the column headers for a list table on a specific screen. |
5 | 27 |
* |
28 |
* The dynamic portion of the hook name, `$screen->id`, refers to the |
|
29 |
* ID of a specific screen. For example, the screen ID for the Posts |
|
30 |
* list table is edit-post, so the filter for that screen would be |
|
31 |
* manage_edit-post_columns. |
|
32 |
* |
|
33 |
* @since 3.0.0 |
|
34 |
* |
|
16 | 35 |
* @param string[] $columns The column header labels keyed by column ID. |
5 | 36 |
*/ |
37 |
$column_headers[ $screen->id ] = apply_filters( "manage_{$screen->id}_columns", array() ); |
|
38 |
} |
|
0 | 39 |
|
40 |
return $column_headers[ $screen->id ]; |
|
41 |
} |
|
42 |
||
43 |
/** |
|
44 |
* Get a list of hidden columns. |
|
45 |
* |
|
46 |
* @since 2.7.0 |
|
47 |
* |
|
48 |
* @param string|WP_Screen $screen The screen you want the hidden columns for |
|
16 | 49 |
* @return string[] Array of IDs of hidden columns. |
0 | 50 |
*/ |
51 |
function get_hidden_columns( $screen ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
52 |
if ( is_string( $screen ) ) { |
0 | 53 |
$screen = convert_to_screen( $screen ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
54 |
} |
0 | 55 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
56 |
$hidden = get_user_option( 'manage' . $screen->id . 'columnshidden' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
57 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
58 |
$use_defaults = ! is_array( $hidden ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
59 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
60 |
if ( $use_defaults ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
61 |
$hidden = array(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
62 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
63 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
64 |
* Filters the default list of hidden columns. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
65 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
66 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
67 |
* |
16 | 68 |
* @param string[] $hidden Array of IDs of columns hidden by default. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
69 |
* @param WP_Screen $screen WP_Screen object of the current screen. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
70 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
71 |
$hidden = apply_filters( 'default_hidden_columns', $hidden, $screen ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
72 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
73 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
74 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
75 |
* Filters the list of hidden columns. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
76 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
77 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
78 |
* @since 4.4.1 Added the `use_defaults` parameter. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
79 |
* |
16 | 80 |
* @param string[] $hidden Array of IDs of hidden columns. |
81 |
* @param WP_Screen $screen WP_Screen object of the current screen. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
82 |
* @param bool $use_defaults Whether to show the default columns. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
83 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
84 |
return apply_filters( 'hidden_columns', $hidden, $screen, $use_defaults ); |
0 | 85 |
} |
86 |
||
87 |
/** |
|
88 |
* Prints the meta box preferences for screen meta. |
|
89 |
* |
|
90 |
* @since 2.7.0 |
|
91 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
92 |
* @global array $wp_meta_boxes |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
93 |
* |
5 | 94 |
* @param WP_Screen $screen |
0 | 95 |
*/ |
96 |
function meta_box_prefs( $screen ) { |
|
97 |
global $wp_meta_boxes; |
|
98 |
||
9 | 99 |
if ( is_string( $screen ) ) { |
0 | 100 |
$screen = convert_to_screen( $screen ); |
9 | 101 |
} |
0 | 102 |
|
9 | 103 |
if ( empty( $wp_meta_boxes[ $screen->id ] ) ) { |
0 | 104 |
return; |
9 | 105 |
} |
0 | 106 |
|
9 | 107 |
$hidden = get_hidden_meta_boxes( $screen ); |
0 | 108 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
109 |
foreach ( array_keys( $wp_meta_boxes[ $screen->id ] ) as $context ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
110 |
foreach ( array( 'high', 'core', 'default', 'low' ) as $priority ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
111 |
if ( ! isset( $wp_meta_boxes[ $screen->id ][ $context ][ $priority ] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
112 |
continue; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
113 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
114 |
foreach ( $wp_meta_boxes[ $screen->id ][ $context ][ $priority ] as $box ) { |
9 | 115 |
if ( false == $box || ! $box['title'] ) { |
0 | 116 |
continue; |
9 | 117 |
} |
16 | 118 |
// Submit box cannot be hidden. |
119 |
if ( 'submitdiv' === $box['id'] || 'linksubmitdiv' === $box['id'] ) { |
|
0 | 120 |
continue; |
9 | 121 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
122 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
123 |
$widget_title = $box['title']; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
124 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
125 |
if ( is_array( $box['args'] ) && isset( $box['args']['__widget_basename'] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
126 |
$widget_title = $box['args']['__widget_basename']; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
127 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
128 |
|
16 | 129 |
$is_hidden = in_array( $box['id'], $hidden, true ); |
130 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
131 |
printf( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
132 |
'<label for="%1$s-hide"><input class="hide-postbox-tog" name="%1$s-hide" type="checkbox" id="%1$s-hide" value="%1$s" %2$s />%3$s</label>', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
133 |
esc_attr( $box['id'] ), |
16 | 134 |
checked( $is_hidden, false, false ), |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
135 |
$widget_title |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
136 |
); |
0 | 137 |
} |
138 |
} |
|
139 |
} |
|
140 |
} |
|
141 |
||
142 |
/** |
|
16 | 143 |
* Gets an array of IDs of hidden meta boxes. |
0 | 144 |
* |
145 |
* @since 2.7.0 |
|
146 |
* |
|
147 |
* @param string|WP_Screen $screen Screen identifier |
|
16 | 148 |
* @return string[] IDs of hidden meta boxes. |
0 | 149 |
*/ |
150 |
function get_hidden_meta_boxes( $screen ) { |
|
9 | 151 |
if ( is_string( $screen ) ) { |
0 | 152 |
$screen = convert_to_screen( $screen ); |
9 | 153 |
} |
0 | 154 |
|
155 |
$hidden = get_user_option( "metaboxhidden_{$screen->id}" ); |
|
156 |
||
157 |
$use_defaults = ! is_array( $hidden ); |
|
158 |
||
16 | 159 |
// Hide slug boxes by default. |
0 | 160 |
if ( $use_defaults ) { |
161 |
$hidden = array(); |
|
16 | 162 |
if ( 'post' === $screen->base ) { |
163 |
if ( in_array( $screen->post_type, array( 'post', 'page', 'attachment' ), true ) ) { |
|
9 | 164 |
$hidden = array( 'slugdiv', 'trackbacksdiv', 'postcustom', 'postexcerpt', 'commentstatusdiv', 'commentsdiv', 'authordiv', 'revisionsdiv' ); |
165 |
} else { |
|
0 | 166 |
$hidden = array( 'slugdiv' ); |
9 | 167 |
} |
0 | 168 |
} |
5 | 169 |
|
170 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
171 |
* Filters the default list of hidden meta boxes. |
5 | 172 |
* |
173 |
* @since 3.1.0 |
|
174 |
* |
|
16 | 175 |
* @param string[] $hidden An array of IDs of meta boxes hidden by default. |
5 | 176 |
* @param WP_Screen $screen WP_Screen object of the current screen. |
177 |
*/ |
|
0 | 178 |
$hidden = apply_filters( 'default_hidden_meta_boxes', $hidden, $screen ); |
179 |
} |
|
180 |
||
5 | 181 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
182 |
* Filters the list of hidden meta boxes. |
5 | 183 |
* |
184 |
* @since 3.3.0 |
|
185 |
* |
|
16 | 186 |
* @param string[] $hidden An array of IDs of hidden meta boxes. |
5 | 187 |
* @param WP_Screen $screen WP_Screen object of the current screen. |
188 |
* @param bool $use_defaults Whether to show the default meta boxes. |
|
189 |
* Default true. |
|
190 |
*/ |
|
0 | 191 |
return apply_filters( 'hidden_meta_boxes', $hidden, $screen, $use_defaults ); |
192 |
} |
|
193 |
||
194 |
/** |
|
195 |
* Register and configure an admin screen option |
|
196 |
* |
|
197 |
* @since 3.1.0 |
|
198 |
* |
|
199 |
* @param string $option An option name. |
|
16 | 200 |
* @param mixed $args Option-dependent arguments. |
0 | 201 |
*/ |
202 |
function add_screen_option( $option, $args = array() ) { |
|
203 |
$current_screen = get_current_screen(); |
|
204 |
||
9 | 205 |
if ( ! $current_screen ) { |
0 | 206 |
return; |
9 | 207 |
} |
0 | 208 |
|
209 |
$current_screen->add_option( $option, $args ); |
|
210 |
} |
|
211 |
||
212 |
/** |
|
213 |
* Get the current screen object |
|
214 |
* |
|
215 |
* @since 3.1.0 |
|
216 |
* |
|
16 | 217 |
* @global WP_Screen $current_screen WordPress current screen object. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
218 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
219 |
* @return WP_Screen|null Current screen object or null when screen not defined. |
0 | 220 |
*/ |
221 |
function get_current_screen() { |
|
222 |
global $current_screen; |
|
223 |
||
9 | 224 |
if ( ! isset( $current_screen ) ) { |
0 | 225 |
return null; |
9 | 226 |
} |
0 | 227 |
|
228 |
return $current_screen; |
|
229 |
} |
|
230 |
||
231 |
/** |
|
232 |
* Set the current screen object |
|
233 |
* |
|
234 |
* @since 3.0.0 |
|
235 |
* |
|
16 | 236 |
* @param string|WP_Screen $hook_name Optional. The hook name (also known as the hook suffix) used to determine the screen, |
237 |
* or an existing screen object. |
|
0 | 238 |
*/ |
239 |
function set_current_screen( $hook_name = '' ) { |
|
240 |
WP_Screen::get( $hook_name )->set_current_screen(); |
|
241 |
} |