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