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