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