1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Atomic Admin Menu file. |
4
|
|
|
* |
5
|
|
|
* @package Jetpack |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Automattic\Jetpack\Dashboard_Customizations; |
9
|
|
|
|
10
|
|
|
use Automattic\Jetpack\Connection\Manager as Connection_Manager; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class Atomic_Admin_Menu. |
14
|
|
|
*/ |
15
|
|
|
class Atomic_Admin_Menu extends Admin_Menu { |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Atomic_Admin_Menu constructor. |
19
|
|
|
*/ |
20
|
|
|
protected function __construct() { |
21
|
|
|
parent::__construct(); |
22
|
|
|
|
23
|
|
|
add_action( |
24
|
|
|
'admin_menu', |
25
|
|
|
function () { |
26
|
|
|
remove_action( 'admin_menu', 'gutenberg_menu', 9 ); |
27
|
|
|
}, |
28
|
|
|
0 |
29
|
|
|
); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Create the desired menu output. |
34
|
|
|
*/ |
35
|
|
View Code Duplication |
public function reregister_menu_items() { |
36
|
|
|
parent::reregister_menu_items(); |
37
|
|
|
|
38
|
|
|
// Not needed outside of wp-admin. |
39
|
|
|
if ( ! $this->is_api_request ) { |
40
|
|
|
$this->add_browse_sites_link(); |
41
|
|
|
$this->add_site_card_menu(); |
42
|
|
|
$this->add_new_site_link(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$this->add_jetpack_menu(); |
46
|
|
|
|
47
|
|
|
ksort( $GLOBALS['menu'] ); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Adds the site switcher link if user has more than one site. |
52
|
|
|
*/ |
53
|
|
|
public function add_browse_sites_link() { |
54
|
|
|
$wpcom_user_data = ( new Connection_Manager() )->get_connected_user_data(); |
55
|
|
|
|
56
|
|
|
if ( empty( $wpcom_user_data['site_count'] ) || $wpcom_user_data['site_count'] < 2 ) { |
57
|
|
|
return; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
// Add the menu item. |
61
|
|
|
add_menu_page( __( 'site-switcher', 'jetpack' ), __( 'Browse sites', 'jetpack' ), 'read', 'https://wordpress.com/home', null, 'dashicons-arrow-left-alt2', 0 ); |
62
|
|
|
add_filter( 'add_menu_classes', array( $this, 'set_browse_sites_link_class' ) ); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Adds a custom element class for Site Switcher menu item. |
67
|
|
|
* |
68
|
|
|
* @param array $menu Associative array of administration menu items. |
69
|
|
|
* @return array |
70
|
|
|
*/ |
71
|
|
View Code Duplication |
public function set_browse_sites_link_class( array $menu ) { |
72
|
|
|
foreach ( $menu as $key => $menu_item ) { |
73
|
|
|
if ( 'site-switcher' !== $menu_item[3] ) { |
74
|
|
|
continue; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$menu[ $key ][4] = add_cssclass( 'site-switcher', $menu_item[4] ); |
78
|
|
|
break; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $menu; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Adds a link to the menu to create a new site. |
86
|
|
|
*/ |
87
|
|
|
public function add_new_site_link() { |
88
|
|
|
global $menu; |
89
|
|
|
|
90
|
|
|
$wpcom_user_data = ( new Connection_Manager() )->get_connected_user_data(); |
91
|
|
|
if ( empty( $wpcom_user_data['site_count'] ) || $wpcom_user_data['site_count'] > 1 ) { |
92
|
|
|
return; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
// Attempt to get last position. |
96
|
|
|
$position = 1000; |
97
|
|
|
while ( isset( $menu[ $position ] ) ) { |
98
|
|
|
$position++; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$this->add_admin_menu_separator( ++$position ); |
102
|
|
|
add_menu_page( __( 'Add new site', 'jetpack' ), __( 'Add new site', 'jetpack' ), 'read', 'https://wordpress.com/start?ref=calypso-sidebar', null, 'dashicons-plus-alt', ++$position ); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Adds site card component. |
107
|
|
|
*/ |
108
|
|
|
public function add_site_card_menu() { |
109
|
|
|
$default = 'data:image/svg+xml,' . rawurlencode( '<svg class="gridicon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><title>Globe</title><rect fill-opacity="0" x="0" width="24" height="24"/><g><path fill="#fff" d="M12 2C6.477 2 2 6.477 2 12s4.477 10 10 10 10-4.477 10-10S17.523 2 12 2zm0 18l2-2 1-1v-2h-2v-1l-1-1H9v3l2 2v1.93c-3.94-.494-7-3.858-7-7.93l1 1h2v-2h2l3-3V6h-2L9 5v-.41C9.927 4.21 10.94 4 12 4s2.073.212 3 .59V6l-1 1v2l1 1 3.13-3.13c.752.897 1.304 1.964 1.606 3.13H18l-2 2v2l1 1h2l.286.286C18.03 18.06 15.24 20 12 20z"/></g></svg>' ); |
110
|
|
|
$icon = get_site_icon_url( 32, $default ); |
111
|
|
|
|
112
|
|
|
$badge = ''; |
113
|
|
|
if ( function_exists( 'site_is_private' ) && site_is_private() ) { |
114
|
|
|
$badge .= sprintf( |
115
|
|
|
'<span class="site__badge site__badge-private">%s</span>', |
116
|
|
|
site_is_coming_soon() ? esc_html__( 'Coming Soon', 'jetpack' ) : esc_html__( 'Private', 'jetpack' ) |
117
|
|
|
); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$site_card = ' |
121
|
|
|
<div class="site__info"> |
122
|
|
|
<div class="site__title">%1$s</div> |
123
|
|
|
<div class="site__domain">%2$s</div> |
124
|
|
|
%3$s |
125
|
|
|
</div>'; |
126
|
|
|
|
127
|
|
|
$site_card = sprintf( |
128
|
|
|
$site_card, |
129
|
|
|
get_option( 'blogname' ), |
130
|
|
|
$this->domain, |
131
|
|
|
$badge |
132
|
|
|
); |
133
|
|
|
|
134
|
|
|
add_menu_page( 'site-card', $site_card, 'read', get_home_url(), null, $icon, 1 ); |
135
|
|
|
add_filter( 'add_menu_classes', array( $this, 'set_site_card_menu_class' ) ); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Adds a custom element class and id for Site Card's menu item. |
140
|
|
|
* |
141
|
|
|
* @param array $menu Associative array of administration menu items. |
142
|
|
|
* @return array |
143
|
|
|
*/ |
144
|
|
|
public function set_site_card_menu_class( array $menu ) { |
145
|
|
|
foreach ( $menu as $key => $menu_item ) { |
146
|
|
|
if ( 'site-card' !== $menu_item[3] ) { |
147
|
|
|
continue; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
$classes = ' toplevel_page_site-card'; |
151
|
|
|
|
152
|
|
|
// webclip.png is the default on WoA sites. Anything other than that means we have a custom site icon. |
153
|
|
|
if ( has_site_icon() && 'https://s0.wp.com/i/webclip.png' !== get_site_icon_url( 512 ) ) { |
154
|
|
|
$classes .= ' has-site-icon'; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
$menu[ $key ][4] = $menu_item[4] . $classes; |
158
|
|
|
$menu[ $key ][5] = 'toplevel_page_site_card'; |
159
|
|
|
break; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
return $menu; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Adds Upgrades menu. |
167
|
|
|
*/ |
168
|
|
|
public function add_upgrades_menu() { |
169
|
|
|
parent::add_upgrades_menu(); |
170
|
|
|
|
171
|
|
|
add_submenu_page( 'https://wordpress.com/plans/' . $this->domain, __( 'Domains', 'jetpack' ), __( 'Domains', 'jetpack' ), 'manage_options', 'https://wordpress.com/domains/manage/' . $this->domain, null, 10 ); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Adds Jetpack menu. |
176
|
|
|
*/ |
177
|
|
View Code Duplication |
public function add_jetpack_menu() { |
178
|
|
|
global $menu; |
179
|
|
|
|
180
|
|
|
$position = 50; |
181
|
|
|
while ( isset( $menu[ $position ] ) ) { |
182
|
|
|
$position++; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
// TODO: Replace with proper SVG data url. |
186
|
|
|
$jetpack_icon = "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='20' height='20' viewBox='0 0 32 32' %3E%3Cpath fill='%23a0a5aa' d='M16,0C7.2,0,0,7.2,0,16s7.2,16,16,16s16-7.2,16-16S24.8,0,16,0z'%3E%3C/path%3E%3Cpolygon fill='%23fff' points='15,19 7,19 15,3 '%3E%3C/polygon%3E%3Cpolygon fill='%23fff' points='17,29 17,13 25,13 '%3E%3C/polygon%3E%3C/svg%3E"; |
187
|
|
|
$jetpack_slug = 'https://wordpress.com/activity-log/' . $this->domain; |
188
|
|
|
|
189
|
|
|
$this->add_admin_menu_separator( $position++, 'manage_options' ); |
190
|
|
|
add_menu_page( esc_attr__( 'Jetpack', 'jetpack' ), __( 'Jetpack', 'jetpack' ), 'manage_options', $jetpack_slug, null, $jetpack_icon, $position ); |
191
|
|
|
|
192
|
|
|
// Maintain id for jQuery selector. |
193
|
|
|
$menu[ $position ][5] = 'toplevel_page_jetpack'; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
194
|
|
|
|
195
|
|
|
remove_menu_page( 'jetpack' ); |
196
|
|
|
remove_submenu_page( 'jetpack', 'stats' ); |
197
|
|
|
|
198
|
|
|
$this->migrate_submenus( 'jetpack', $jetpack_slug ); |
199
|
|
|
|
200
|
|
|
add_submenu_page( $jetpack_slug, esc_attr__( 'Activity Log', 'jetpack' ), __( 'Activity Log', 'jetpack' ), 'manage_options', $jetpack_slug, null, 5 ); |
201
|
|
|
add_submenu_page( $jetpack_slug, esc_attr__( 'Backup', 'jetpack' ), __( 'Backup', 'jetpack' ), 'manage_options', 'https://wordpress.com/backup/' . $this->domain, null, 10 ); |
202
|
|
|
|
203
|
|
|
add_filter( 'parent_file', array( $this, 'jetpack_parent_file' ) ); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Filters the parent file of an admin menu sub-menu item. |
208
|
|
|
* |
209
|
|
|
* @param string $parent_file The parent file. |
210
|
|
|
* @return string Updated parent file. |
211
|
|
|
*/ |
212
|
|
|
public function jetpack_parent_file( $parent_file ) { |
213
|
|
|
if ( 'jetpack' === $parent_file ) { |
214
|
|
|
$parent_file = 'https://wordpress.com/activity-log/' . $this->domain; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
return $parent_file; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Adds Tools menu. |
222
|
|
|
* |
223
|
|
|
* @param bool $calypso Optional. Whether links should point to Calypso or wp-admin. Default true (Calypso). |
224
|
|
|
*/ |
225
|
|
View Code Duplication |
public function add_tools_menu( $calypso = true ) { |
226
|
|
|
$menu_slug = $calypso ? 'https://wordpress.com/marketing/tools/' . $this->domain : 'tools.php'; |
227
|
|
|
|
228
|
|
|
add_submenu_page( $menu_slug, esc_attr__( 'Marketing', 'jetpack' ), __( 'Marketing', 'jetpack' ), 'manage_options', 'https://wordpress.com/marketing/tools/' . $this->domain, null, 5 ); |
229
|
|
|
add_submenu_page( $menu_slug, esc_attr__( 'Earn', 'jetpack' ), __( 'Earn', 'jetpack' ), 'manage_options', 'https://wordpress.com/earn/' . $this->domain, null, 10 ); |
230
|
|
|
|
231
|
|
|
parent::add_tools_menu( $calypso ); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Adds Settings menu. |
236
|
|
|
* |
237
|
|
|
* @param bool $calypso Optional. Whether links should point to Calypso or wp-admin. Default true (Calypso). |
238
|
|
|
*/ |
239
|
|
|
public function add_options_menu( $calypso = true ) { |
240
|
|
|
add_options_page( esc_attr__( 'Hosting Configuration', 'jetpack' ), __( 'Hosting Configuration', 'jetpack' ), 'manage_options', 'https://wordpress.com/hosting-config/' . $this->domain, null, 6 ); |
241
|
|
|
|
242
|
|
|
parent::add_options_menu( $calypso ); |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
|