Skip to content

Commit

Permalink
Admin Bar: Replace the wp-admin url with the calypso ones for the def…
Browse files Browse the repository at this point in the history
…ault interface
  • Loading branch information
arthur791004 committed Jul 17, 2024
1 parent 6bd32ec commit d0f37ea
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
/**
* The WordPress.com WP Admin Bar for the default interface.
*
* @package automattic/jetpack-mu-wpcom
*/

namespace Automattic\Jetpack\Jetpack_Mu_Wpcom;

/**
* The WPCOM_Admin_Bar.
*/
class WPCOM_Admin_Bar extends \WP_Admin_Bar {
/**
* The map of url from the wp admin to calypso.
*
* @var array
*/
private $map_wp_admin_url_to_calypso_url = array(
/**
* Site menu
*/
'wp-admin/' => 'https://wordpress.com/home/%home_url%',
'wp-admin/plugins.php' => 'https://wordpress.com/plugins/%home_url%',
'wp-admin/themes.php' => 'https://wordpress.com/themes/%home_url%',

/**
* +New menu
*/
'wp-admin/post-new.php' => '/wp-admin/post-new.php?post_type=post&calypsoify=1',
'wp-admin/media-new.php' => 'https://wordpress.com/media/%home_url%',
'wp-admin/post-new.php?post_type=page' => 'https://wordpress.com/page/%home_url%',
'wp-admin/user-new.php' => 'https://wordpress.com/people/new/%home_url%',

/**
* Profile menu
*/
'wp-admin/profile.php' => 'https://wordpress.com/me',
);

/**
* Adds a node to the menu.
*
* @param array $args {
* Arguments for adding a node.
*
* @type string $id ID of the item.
* @type string $title Title of the node.
* @type string $parent Optional. ID of the parent node.
* @type string $href Optional. Link for the item.
* @type bool $group Optional. Whether or not the node is a group. Default false.
* @type array $meta Meta data including the following keys: 'html', 'class', 'rel', 'lang', 'dir',
* 'onclick', 'target', 'title', 'tabindex', 'menu_title'. Default empty.
* }
*/
public function add_node( $args ) {
if ( empty( $args['href'] ) ) {
parent::add_node( $args );
return;
}

$home_url = home_url( '/' );
$site_slug = wp_parse_url( $home_url, PHP_URL_HOST );
$href = str_replace( $home_url, '', $args['href'] );
if ( array_key_exists( $href, $this->map_wp_admin_url_to_calypso_url ) ) {
$calypso_url = $this->map_wp_admin_url_to_calypso_url[ $href ];
$args['href'] = str_replace( '%home_url%', $site_slug, $calypso_url );
}

parent::add_node( $args );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,19 @@ function wpcom_hide_help_center_menu() {
<?php
}
add_action( 'admin_head', 'wpcom_hide_help_center_menu' );

/**
* Replaces the default admin bar class with our own.
*
* @param string $wp_admin_bar_class Admin bar class to use. Default 'WP_Admin_Bar'.
* @return string Name of the admin bar class.
*/
function wpcom_custom_admin_bar_class( $wp_admin_bar_class ) {
if ( get_option( 'wpcom_admin_interface' ) === 'wp-admin' ) {
return $wp_admin_bar_class;
}

require_once __DIR__ . '/class-wpcom-admin-bar.php';
return '\Automattic\Jetpack\Jetpack_Mu_Wpcom\WPCOM_Admin_Bar';
}
add_filter( 'wp_admin_bar_class', 'wpcom_custom_admin_bar_class' );

0 comments on commit d0f37ea

Please sign in to comment.