]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/class-wp-admin-bar.php
Wordpress 3.5.2-scripts
[autoinstalls/wordpress.git] / wp-includes / class-wp-admin-bar.php
1 <?php
2 /**
3  * The WordPress Toolbar
4  *
5  * @since 3.1.0
6  *
7  * @package WordPress
8  * @subpackage Toolbar
9  */
10 class WP_Admin_Bar {
11         private $nodes = array();
12         private $bound = false;
13         public $user;
14
15         public function __get( $name ) {
16                 switch ( $name ) {
17                         case 'proto' :
18                                 return is_ssl() ? 'https://' : 'http://';
19                                 break;
20                         case 'menu' :
21                                 _deprecated_argument( 'WP_Admin_Bar', '3.3', 'Modify admin bar nodes with WP_Admin_Bar::get_node(), WP_Admin_Bar::add_node(), and WP_Admin_Bar::remove_node(), not the <code>menu</code> property.' );
22                                 return array(); // Sorry, folks.
23                                 break;
24                 }
25         }
26
27         public function initialize() {
28                 $this->user = new stdClass;
29
30                 if ( is_user_logged_in() ) {
31                         /* Populate settings we need for the menu based on the current user. */
32                         $this->user->blogs = get_blogs_of_user( get_current_user_id() );
33                         if ( is_multisite() ) {
34                                 $this->user->active_blog = get_active_blog_for_user( get_current_user_id() );
35                                 $this->user->domain = empty( $this->user->active_blog ) ? user_admin_url() : trailingslashit( get_home_url( $this->user->active_blog->blog_id ) );
36                                 $this->user->account_domain = $this->user->domain;
37                         } else {
38                                 $this->user->active_blog = $this->user->blogs[get_current_blog_id()];
39                                 $this->user->domain = trailingslashit( home_url() );
40                                 $this->user->account_domain = $this->user->domain;
41                         }
42                 }
43
44                 add_action( 'wp_head', 'wp_admin_bar_header' );
45
46                 add_action( 'admin_head', 'wp_admin_bar_header' );
47
48                 if ( current_theme_supports( 'admin-bar' ) ) {
49                         $admin_bar_args = get_theme_support( 'admin-bar' ); // add_theme_support( 'admin-bar', array( 'callback' => '__return_false') );
50                         $header_callback = $admin_bar_args[0]['callback'];
51                 }
52
53                 if ( empty($header_callback) )
54                         $header_callback = '_admin_bar_bump_cb';
55
56                 add_action('wp_head', $header_callback);
57
58                 wp_enqueue_script( 'admin-bar' );
59                 wp_enqueue_style( 'admin-bar' );
60
61                 do_action( 'admin_bar_init' );
62         }
63
64         public function add_menu( $node ) {
65                 $this->add_node( $node );
66         }
67
68         public function remove_menu( $id ) {
69                 $this->remove_node( $id );
70         }
71
72         /**
73          * Add a node to the menu.
74          *
75          * @param array $args - The arguments for each node.
76          * - id         - string    - The ID of the item.
77          * - title      - string    - The title of the node.
78          * - parent     - string    - The ID of the parent node. Optional.
79          * - href       - string    - The link for the item. Optional.
80          * - group      - boolean   - If the node is a group. Optional. Default false.
81          * - meta       - array     - Meta data including the following keys: html, class, onclick, target, title, tabindex.
82          */
83         public function add_node( $args ) {
84                 // Shim for old method signature: add_node( $parent_id, $menu_obj, $args )
85                 if ( func_num_args() >= 3 && is_string( func_get_arg(0) ) )
86                         $args = array_merge( array( 'parent' => func_get_arg(0) ), func_get_arg(2) );
87
88                 if ( is_object( $args ) )
89                         $args = get_object_vars( $args );
90
91                 // Ensure we have a valid title.
92                 if ( empty( $args['id'] ) ) {
93                         if ( empty( $args['title'] ) )
94                                 return;
95
96                         _doing_it_wrong( __METHOD__, __( 'The menu ID should not be empty.' ), '3.3' );
97                         // Deprecated: Generate an ID from the title.
98                         $args['id'] = esc_attr( sanitize_title( trim( $args['title'] ) ) );
99                 }
100
101                 $defaults = array(
102                         'id'     => false,
103                         'title'  => false,
104                         'parent' => false,
105                         'href'   => false,
106                         'group'  => false,
107                         'meta'   => array(),
108                 );
109
110                 // If the node already exists, keep any data that isn't provided.
111                 if ( $maybe_defaults = $this->get_node( $args['id'] ) )
112                         $defaults = get_object_vars( $maybe_defaults );
113
114                 // Do the same for 'meta' items.
115                 if ( ! empty( $defaults['meta'] ) && ! empty( $args['meta'] ) )
116                         $args['meta'] = wp_parse_args( $args['meta'], $defaults['meta'] );
117
118                 $args = wp_parse_args( $args, $defaults );
119
120                 $back_compat_parents = array(
121                         'my-account-with-avatar' => array( 'my-account', '3.3' ),
122                         'my-blogs'               => array( 'my-sites',   '3.3' ),
123                 );
124
125                 if ( isset( $back_compat_parents[ $args['parent'] ] ) ) {
126                         list( $new_parent, $version ) = $back_compat_parents[ $args['parent'] ];
127                         _deprecated_argument( __METHOD__, $version, sprintf( 'Use <code>%s</code> as the parent for the <code>%s</code> admin bar node instead of <code>%s</code>.', $new_parent, $args['id'], $args['parent'] ) );
128                         $args['parent'] = $new_parent;
129                 }
130
131                 $this->_set_node( $args );
132         }
133
134         final protected function _set_node( $args ) {
135                 $this->nodes[ $args['id'] ] = (object) $args;
136         }
137
138         /**
139          * Gets a node.
140          *
141          * @return object Node.
142          */
143         final public function get_node( $id ) {
144                 if ( $node = $this->_get_node( $id ) )
145                         return clone $node;
146         }
147
148         final protected function _get_node( $id ) {
149                 if ( $this->bound )
150                         return;
151
152                 if ( empty( $id ) )
153                         $id = 'root';
154
155                 if ( isset( $this->nodes[ $id ] ) )
156                         return $this->nodes[ $id ];
157         }
158
159         final public function get_nodes() {
160                 if ( ! $nodes = $this->_get_nodes() )
161                         return;
162
163                 foreach ( $nodes as &$node ) {
164                         $node = clone $node;
165                 }
166                 return $nodes;
167         }
168
169         final protected function _get_nodes() {
170                 if ( $this->bound )
171                         return;
172
173                 return $this->nodes;
174         }
175
176         /**
177          * Add a group to a menu node.
178          *
179          * @since 3.3.0
180          *
181          * @param array $args - The arguments for each node.
182          * - id         - string    - The ID of the item.
183          * - parent     - string    - The ID of the parent node. Optional. Default root.
184          * - meta       - array     - Meta data including the following keys: class, onclick, target, title.
185          */
186         final public function add_group( $args ) {
187                 $args['group'] = true;
188
189                 $this->add_node( $args );
190         }
191
192         /**
193          * Remove a node.
194          *
195          * @param string The ID of the item.
196          */
197         public function remove_node( $id ) {
198                 $this->_unset_node( $id );
199         }
200
201         final protected function _unset_node( $id ) {
202                 unset( $this->nodes[ $id ] );
203         }
204
205         public function render() {
206                 $root = $this->_bind();
207                 if ( $root )
208                         $this->_render( $root );
209         }
210
211         final protected function _bind() {
212                 if ( $this->bound )
213                         return;
214
215                 // Add the root node.
216                 // Clear it first, just in case. Don't mess with The Root.
217                 $this->remove_node( 'root' );
218                 $this->add_node( array(
219                         'id'    => 'root',
220                         'group' => false,
221                 ) );
222
223                 // Normalize nodes: define internal 'children' and 'type' properties.
224                 foreach ( $this->_get_nodes() as $node ) {
225                         $node->children = array();
226                         $node->type = ( $node->group ) ? 'group' : 'item';
227                         unset( $node->group );
228
229                         // The Root wants your orphans. No lonely items allowed.
230                         if ( ! $node->parent )
231                                 $node->parent = 'root';
232                 }
233
234                 foreach ( $this->_get_nodes() as $node ) {
235                         if ( 'root' == $node->id )
236                                 continue;
237
238                         // Fetch the parent node. If it isn't registered, ignore the node.
239                         if ( ! $parent = $this->_get_node( $node->parent ) ) {
240                                 continue;
241                         }
242
243                         // Generate the group class (we distinguish between top level and other level groups).
244                         $group_class = ( $node->parent == 'root' ) ? 'ab-top-menu' : 'ab-submenu';
245
246                         if ( $node->type == 'group' ) {
247                                 if ( empty( $node->meta['class'] ) )
248                                         $node->meta['class'] = $group_class;
249                                 else
250                                         $node->meta['class'] .= ' ' . $group_class;
251                         }
252
253                         // Items in items aren't allowed. Wrap nested items in 'default' groups.
254                         if ( $parent->type == 'item' && $node->type == 'item' ) {
255                                 $default_id = $parent->id . '-default';
256                                 $default    = $this->_get_node( $default_id );
257
258                                 // The default group is added here to allow groups that are
259                                 // added before standard menu items to render first.
260                                 if ( ! $default ) {
261                                         // Use _set_node because add_node can be overloaded.
262                                         // Make sure to specify default settings for all properties.
263                                         $this->_set_node( array(
264                                                 'id'        => $default_id,
265                                                 'parent'    => $parent->id,
266                                                 'type'      => 'group',
267                                                 'children'  => array(),
268                                                 'meta'      => array(
269                                                         'class'     => $group_class,
270                                                 ),
271                                                 'title'     => false,
272                                                 'href'      => false,
273                                         ) );
274                                         $default = $this->_get_node( $default_id );
275                                         $parent->children[] = $default;
276                                 }
277                                 $parent = $default;
278
279                         // Groups in groups aren't allowed. Add a special 'container' node.
280                         // The container will invisibly wrap both groups.
281                         } elseif ( $parent->type == 'group' && $node->type == 'group' ) {
282                                 $container_id = $parent->id . '-container';
283                                 $container    = $this->_get_node( $container_id );
284
285                                 // We need to create a container for this group, life is sad.
286                                 if ( ! $container ) {
287                                         // Use _set_node because add_node can be overloaded.
288                                         // Make sure to specify default settings for all properties.
289                                         $this->_set_node( array(
290                                                 'id'       => $container_id,
291                                                 'type'     => 'container',
292                                                 'children' => array( $parent ),
293                                                 'parent'   => false,
294                                                 'title'    => false,
295                                                 'href'     => false,
296                                                 'meta'     => array(),
297                                         ) );
298
299                                         $container = $this->_get_node( $container_id );
300
301                                         // Link the container node if a grandparent node exists.
302                                         $grandparent = $this->_get_node( $parent->parent );
303
304                                         if ( $grandparent ) {
305                                                 $container->parent = $grandparent->id;
306
307                                                 $index = array_search( $parent, $grandparent->children, true );
308                                                 if ( $index === false )
309                                                         $grandparent->children[] = $container;
310                                                 else
311                                                         array_splice( $grandparent->children, $index, 1, array( $container ) );
312                                         }
313
314                                         $parent->parent = $container->id;
315                                 }
316
317                                 $parent = $container;
318                         }
319
320                         // Update the parent ID (it might have changed).
321                         $node->parent = $parent->id;
322
323                         // Add the node to the tree.
324                         $parent->children[] = $node;
325                 }
326
327                 $root = $this->_get_node( 'root' );
328                 $this->bound = true;
329                 return $root;
330         }
331
332         final protected function _render( $root ) {
333                 global $is_IE;
334
335                 // Add browser classes.
336                 // We have to do this here since admin bar shows on the front end.
337                 $class = 'nojq nojs';
338                 if ( $is_IE ) {
339                         if ( strpos( $_SERVER['HTTP_USER_AGENT'], 'MSIE 7' ) )
340                                 $class .= ' ie7';
341                         elseif ( strpos( $_SERVER['HTTP_USER_AGENT'], 'MSIE 8' ) )
342                                 $class .= ' ie8';
343                         elseif ( strpos( $_SERVER['HTTP_USER_AGENT'], 'MSIE 9' ) )
344                                 $class .= ' ie9';
345                 } elseif ( wp_is_mobile() ) {
346                         $class .= ' mobile';
347                 }
348
349                 ?>
350                 <div id="wpadminbar" class="<?php echo $class; ?>" role="navigation">
351                         <a class="screen-reader-shortcut" href="#wp-toolbar" tabindex="1"><?php _e('Skip to toolbar'); ?></a>
352                         <div class="quicklinks" id="wp-toolbar" role="navigation" aria-label="<?php esc_attr_e('Top navigation toolbar.'); ?>" tabindex="0">
353                                 <?php foreach ( $root->children as $group ) {
354                                         $this->_render_group( $group );
355                                 } ?>
356                         </div>
357                         <?php if ( is_user_logged_in() ) : ?>
358                         <a class="screen-reader-shortcut" href="<?php echo esc_url( wp_logout_url() ); ?>"><?php _e('Log Out'); ?></a>
359                         <?php endif; ?>
360                 </div>
361
362                 <?php
363         }
364
365         final protected function _render_container( $node ) {
366                 if ( $node->type != 'container' || empty( $node->children ) )
367                         return;
368
369                 ?><div id="<?php echo esc_attr( 'wp-admin-bar-' . $node->id ); ?>" class="ab-group-container"><?php
370                         foreach ( $node->children as $group ) {
371                                 $this->_render_group( $group );
372                         }
373                 ?></div><?php
374         }
375
376         final protected function _render_group( $node ) {
377                 if ( $node->type == 'container' )
378                         return $this->_render_container( $node );
379
380                 if ( $node->type != 'group' || empty( $node->children ) )
381                         return;
382
383                 if ( ! empty( $node->meta['class'] ) )
384                         $class = ' class="' . esc_attr( trim( $node->meta['class'] ) ) . '"';
385                 else
386                         $class = '';
387
388                 ?><ul id="<?php echo esc_attr( 'wp-admin-bar-' . $node->id ); ?>"<?php echo $class; ?>><?php
389                         foreach ( $node->children as $item ) {
390                                 $this->_render_item( $item );
391                         }
392                 ?></ul><?php
393         }
394
395         final protected function _render_item( $node ) {
396                 if ( $node->type != 'item' )
397                         return;
398
399                 $is_parent = ! empty( $node->children );
400                 $has_link  = ! empty( $node->href );
401
402                 $tabindex = isset( $node->meta['tabindex'] ) ? (int) $node->meta['tabindex'] : '';
403                 $aria_attributes = $tabindex ? 'tabindex="' . $tabindex . '"' : '';
404
405                 $menuclass = '';
406
407                 if ( $is_parent ) {
408                         $menuclass = 'menupop ';
409                         $aria_attributes .= ' aria-haspopup="true"';
410                 }
411
412                 if ( ! empty( $node->meta['class'] ) )
413                         $menuclass .= $node->meta['class'];
414
415                 if ( $menuclass )
416                         $menuclass = ' class="' . esc_attr( trim( $menuclass ) ) . '"';
417
418                 ?>
419
420                 <li id="<?php echo esc_attr( 'wp-admin-bar-' . $node->id ); ?>"<?php echo $menuclass; ?>><?php
421                         if ( $has_link ):
422                                 ?><a class="ab-item" <?php echo $aria_attributes; ?> href="<?php echo esc_url( $node->href ) ?>"<?php
423                                         if ( ! empty( $node->meta['onclick'] ) ) :
424                                                 ?> onclick="<?php echo esc_js( $node->meta['onclick'] ); ?>"<?php
425                                         endif;
426                                 if ( ! empty( $node->meta['target'] ) ) :
427                                         ?> target="<?php echo esc_attr( $node->meta['target'] ); ?>"<?php
428                                 endif;
429                                 if ( ! empty( $node->meta['title'] ) ) :
430                                         ?> title="<?php echo esc_attr( $node->meta['title'] ); ?>"<?php
431                                 endif;
432                                 ?>><?php
433                         else:
434                                 ?><div class="ab-item ab-empty-item" <?php echo $aria_attributes;
435                                 if ( ! empty( $node->meta['title'] ) ) :
436                                         ?> title="<?php echo esc_attr( $node->meta['title'] ); ?>"<?php
437                                 endif;
438                                 ?>><?php
439                         endif;
440
441                         echo $node->title;
442
443                         if ( $has_link ) :
444                                 ?></a><?php
445                         else:
446                                 ?></div><?php
447                         endif;
448
449                         if ( $is_parent ) :
450                                 ?><div class="ab-sub-wrapper"><?php
451                                         foreach ( $node->children as $group ) {
452                                                 $this->_render_group( $group );
453                                         }
454                                 ?></div><?php
455                         endif;
456
457                         if ( ! empty( $node->meta['html'] ) )
458                                 echo $node->meta['html'];
459
460                         ?>
461                 </li><?php
462         }
463
464         public function recursive_render( $id, $node ) {
465                 _deprecated_function( __METHOD__, '3.3', 'WP_Admin_bar::render(), WP_Admin_Bar::_render_item()' );
466                 $this->_render_item( $node );
467         }
468
469         public function add_menus() {
470                 // User related, aligned right.
471                 add_action( 'admin_bar_menu', 'wp_admin_bar_my_account_menu', 0 );
472                 add_action( 'admin_bar_menu', 'wp_admin_bar_search_menu', 4 );
473                 add_action( 'admin_bar_menu', 'wp_admin_bar_my_account_item', 7 );
474
475                 // Site related.
476                 add_action( 'admin_bar_menu', 'wp_admin_bar_wp_menu', 10 );
477                 add_action( 'admin_bar_menu', 'wp_admin_bar_my_sites_menu', 20 );
478                 add_action( 'admin_bar_menu', 'wp_admin_bar_site_menu', 30 );
479                 add_action( 'admin_bar_menu', 'wp_admin_bar_updates_menu', 40 );
480
481                 // Content related.
482                 if ( ! is_network_admin() && ! is_user_admin() ) {
483                         add_action( 'admin_bar_menu', 'wp_admin_bar_comments_menu', 60 );
484                         add_action( 'admin_bar_menu', 'wp_admin_bar_new_content_menu', 70 );
485                 }
486                 add_action( 'admin_bar_menu', 'wp_admin_bar_edit_menu', 80 );
487
488                 add_action( 'admin_bar_menu', 'wp_admin_bar_add_secondary_groups', 200 );
489
490                 do_action( 'add_admin_bar_menus' );
491         }
492 }