Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
15 | class Atomic_Admin_Menu extends Admin_Menu { |
||
16 | |||
17 | /** |
||
18 | * Atomic_Admin_Menu constructor. |
||
19 | */ |
||
20 | protected function __construct() { |
||
31 | |||
32 | /** |
||
33 | * Create the desired menu output. |
||
34 | */ |
||
35 | View Code Duplication | public function reregister_menu_items() { |
|
52 | |||
53 | /** |
||
54 | * Adds Plugins menu. |
||
55 | * |
||
56 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
57 | */ |
||
58 | public function add_plugins_menu( $wp_admin = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
62 | |||
63 | /** |
||
64 | * Adds the site switcher link if user has more than one site. |
||
65 | */ |
||
66 | public function add_browse_sites_link() { |
||
76 | |||
77 | /** |
||
78 | * Adds a custom element class for Site Switcher menu item. |
||
79 | * |
||
80 | * @param array $menu Associative array of administration menu items. |
||
81 | * @return array |
||
82 | */ |
||
83 | View Code Duplication | public function set_browse_sites_link_class( array $menu ) { |
|
95 | |||
96 | /** |
||
97 | * Adds a link to the menu to create a new site. |
||
98 | */ |
||
99 | public function add_new_site_link() { |
||
116 | |||
117 | /** |
||
118 | * Adds site card component. |
||
119 | */ |
||
120 | public function add_site_card_menu() { |
||
121 | $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>' ); |
||
122 | $icon = get_site_icon_url( 32, $default ); |
||
123 | $blog_name = get_option( 'blogname' ) !== '' ? get_option( 'blogname' ) : $this->domain; |
||
124 | |||
125 | $badge = ''; |
||
126 | if ( function_exists( 'site_is_private' ) && site_is_private() ) { |
||
127 | $badge .= sprintf( |
||
128 | '<span class="site__badge site__badge-private">%s</span>', |
||
129 | site_is_coming_soon() ? esc_html__( 'Coming Soon', 'jetpack' ) : esc_html__( 'Private', 'jetpack' ) |
||
130 | ); |
||
131 | } |
||
132 | |||
133 | $site_card = ' |
||
134 | <div class="site__info"> |
||
135 | <div class="site__title">%1$s</div> |
||
136 | <div class="site__domain">%2$s</div> |
||
137 | %3$s |
||
138 | </div>'; |
||
139 | |||
140 | $site_card = sprintf( |
||
141 | $site_card, |
||
142 | $blog_name, |
||
143 | $this->domain, |
||
144 | $badge |
||
145 | ); |
||
146 | |||
147 | add_menu_page( 'site-card', $site_card, 'read', get_home_url(), null, $icon, 1 ); |
||
148 | add_filter( 'add_menu_classes', array( $this, 'set_site_card_menu_class' ) ); |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * Adds a custom element class and id for Site Card's menu item. |
||
153 | * |
||
154 | * @param array $menu Associative array of administration menu items. |
||
155 | * @return array |
||
156 | */ |
||
157 | public function set_site_card_menu_class( array $menu ) { |
||
158 | foreach ( $menu as $key => $menu_item ) { |
||
159 | if ( 'site-card' !== $menu_item[3] ) { |
||
160 | continue; |
||
161 | } |
||
162 | |||
163 | $classes = ' toplevel_page_site-card'; |
||
164 | |||
165 | // webclip.png is the default on WoA sites. Anything other than that means we have a custom site icon. |
||
166 | if ( has_site_icon() && 'https://s0.wp.com/i/webclip.png' !== get_site_icon_url( 512 ) ) { |
||
167 | $classes .= ' has-site-icon'; |
||
168 | } |
||
169 | |||
170 | $menu[ $key ][4] = $menu_item[4] . $classes; |
||
171 | $menu[ $key ][5] = 'toplevel_page_site_card'; |
||
172 | break; |
||
173 | } |
||
174 | |||
175 | return $menu; |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * Adds Upgrades menu. |
||
180 | */ |
||
181 | public function add_upgrades_menu() { |
||
182 | parent::add_upgrades_menu(); |
||
183 | |||
184 | add_submenu_page( 'https://wordpress.com/plans/' . $this->domain, __( 'Domains', 'jetpack' ), __( 'Domains', 'jetpack' ), 'manage_options', 'https://wordpress.com/domains/manage/' . $this->domain, null, 10 ); |
||
185 | } |
||
186 | |||
187 | /** |
||
188 | * Adds Tools menu. |
||
189 | * |
||
190 | * @param bool $wp_admin_import Optional. Whether Import link should point to Calypso or wp-admin. Default false (Calypso). |
||
191 | * @param bool $wp_admin_export Optional. Whether Export link should point to Calypso or wp-admin. Default false (Calypso). |
||
192 | */ |
||
193 | public function add_tools_menu( $wp_admin_import = false, $wp_admin_export = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
194 | // Export on Atomic sites is always handled on WP Admin. |
||
195 | parent::add_tools_menu( $wp_admin_import, true ); |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * Adds Settings menu. |
||
200 | * |
||
201 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
202 | */ |
||
203 | public function add_options_menu( $wp_admin = false ) { |
||
204 | add_options_page( esc_attr__( 'Hosting Configuration', 'jetpack' ), __( 'Hosting Configuration', 'jetpack' ), 'manage_options', 'https://wordpress.com/hosting-config/' . $this->domain, null, 6 ); |
||
205 | |||
206 | parent::add_options_menu( $wp_admin ); |
||
207 | |||
208 | // No need to add a menu linking to WP Admin if there is already one. |
||
209 | if ( ! $wp_admin ) { |
||
210 | $parent_menu_slug = 'https://wordpress.com/settings/general/' . $this->domain; |
||
211 | add_submenu_page( $parent_menu_slug, esc_attr__( 'Advanced General', 'jetpack' ), __( 'Advanced General', 'jetpack' ), 'manage_options', 'options-general.php' ); |
||
212 | add_submenu_page( $parent_menu_slug, esc_attr__( 'Advanced Writing', 'jetpack' ), __( 'Advanced Writing', 'jetpack' ), 'manage_options', 'options-writing.php' ); |
||
213 | } |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * Adds a WordPress.org theme install menu item. |
||
218 | * |
||
219 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
220 | */ |
||
221 | public function add_theme_install_menu( $wp_admin = false ) { |
||
226 | |||
227 | /** |
||
228 | * Adds Appearance menu. |
||
229 | * |
||
230 | * @param bool $wp_admin_themes Optional. Whether Themes link should point to Calypso or wp-admin. Default false (Calypso). |
||
231 | * @param bool $wp_admin_customize Optional. Whether Customize link should point to Calypso or wp-admin. Default false (Calypso). |
||
232 | */ |
||
233 | View Code Duplication | public function add_appearance_menu( $wp_admin_themes = false, $wp_admin_customize = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
241 | |||
242 | /** |
||
243 | * Adds Users menu. |
||
244 | * |
||
245 | * @param bool $wp_admin Optional. Whether links should point to Calypso or wp-admin. Default false (Calypso). |
||
246 | */ |
||
247 | View Code Duplication | public function add_users_menu( $wp_admin = false ) { |
|
259 | } |
||
260 |