Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Admin Bar: Replace the wp-admin url with the calypso ones for the default interface #38377

Merged
merged 11 commits into from
Jul 19, 2024
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: changed

Admin Bar: Replace the wp-admin url with the calypso ones for the default interface
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?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',

/**
* Jetpack
*/
'wp-admin/post-new.php?post_type=jetpack-testimonial' => 'https://wordpress.com/types/jetpack-testimonial/%home_url%',
'wp-admin/post-new.php?post_type=jetpack-portfolio' => 'https://wordpress.com/types/jetpack-portfolio/%home_url%',
);

/**
* 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 @@ -133,3 +133,19 @@ function wpcom_add_my_account_item_to_profile_menu( $wp_admin_bar ) {
}
}
add_action( 'admin_bar_menu', 'wpcom_add_my_account_item_to_profile_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_wpcom_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_wpcom_admin_bar_class' );
Loading