Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
WPCOM_Admin_Bar
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 2
132
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
30
 add_node
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
42
1<?php
2/**
3 * The WordPress.com WP Admin Bar for the default interface.
4 *
5 * @package automattic/jetpack-mu-wpcom
6 */
7
8namespace Automattic\Jetpack\Jetpack_Mu_Wpcom;
9
10/**
11 * The WPCOM_Admin_Bar.
12 */
13class WPCOM_Admin_Bar extends \WP_Admin_Bar {
14    /**
15     * The map of url from the wp admin to calypso.
16     *
17     * @var array
18     */
19    private $map_wp_admin_url_to_calypso_url = array(
20        /**
21         * Site menu
22         */
23        'wp-admin/'             => 'https://wordpress.com/home/%home_url%',
24        'wp-admin/plugins.php'  => 'https://wordpress.com/plugins/%home_url%',
25        'wp-admin/themes.php'   => 'https://wordpress.com/themes/%home_url%',
26
27        /**
28         * +New menu
29         */
30        'wp-admin/post-new.php' => '/wp-admin/post-new.php?post_type=post',
31        'wp-admin/user-new.php' => 'https://wordpress.com/people/new/%home_url%',
32    );
33
34    /**
35     * Constructor.
36     */
37    public function __construct() {
38        if ( ! wpcom_should_disable_calypso_links( 'upload.php' ) ) {
39            $this->map_wp_admin_url_to_calypso_url['wp-admin/media-new.php'] = 'https://wordpress.com/media/%home_url%';
40        }
41
42        if ( ! wpcom_should_disable_calypso_links( 'edit.php?post_type=page' ) ) {
43            $this->map_wp_admin_url_to_calypso_url['wp-admin/post-new.php?post_type=page'] = 'https://wordpress.com/page/%home_url%';
44        }
45
46        /**
47         * Jetpack
48         */
49        if ( ! wpcom_should_disable_calypso_links( 'edit.php?post_type=jetpack-testimonial' ) ) {
50            $this->map_wp_admin_url_to_calypso_url['wp-admin/post-new.php?post_type=jetpack-testimonial'] = 'https://wordpress.com/types/jetpack-testimonial/%home_url%';
51        }
52
53        if ( ! wpcom_should_disable_calypso_links( 'edit.php?post_type=jetpack-portfolio' ) ) {
54            $this->map_wp_admin_url_to_calypso_url['wp-admin/post-new.php?post_type=jetpack-portfolio'] = 'https://wordpress.com/types/jetpack-portfolio/%home_url%';
55        }
56    }
57
58    /**
59     * Adds a node to the menu.
60     *
61     * @param array $args {
62     *     Arguments for adding a node.
63     *
64     *     @type string $id     ID of the item.
65     *     @type string $title  Title of the node.
66     *     @type string $parent Optional. ID of the parent node.
67     *     @type string $href   Optional. Link for the item.
68     *     @type bool   $group  Optional. Whether or not the node is a group. Default false.
69     *     @type array  $meta   Meta data including the following keys: 'html', 'class', 'rel', 'lang', 'dir',
70     *                          'onclick', 'target', 'title', 'tabindex', 'menu_title'. Default empty.
71     * }
72     */
73    public function add_node( $args ) {
74        if ( ! is_array( $args ) || empty( $args['href'] ) ) {
75            parent::add_node( $args );
76            return;
77        }
78        if ( isset( $args['id'] ) && $args['id'] === 'wpcom-blog-dashboard' ) {
79            parent::add_node( $args );
80            return;
81        }
82        $home_url  = home_url( '/' );
83        $site_slug = wp_parse_url( $home_url, PHP_URL_HOST );
84        $href      = str_replace( $home_url, '', $args['href'] );
85        if ( array_key_exists( $href, $this->map_wp_admin_url_to_calypso_url ) ) {
86            $calypso_url  = $this->map_wp_admin_url_to_calypso_url[ $href ];
87            $args['href'] = str_replace( '%home_url%', $site_slug, $calypso_url );
88        }
89
90        parent::add_node( $args );
91    }
92}