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