helper
2 weeks ago
importers
2 weeks ago
list-tables
1 week ago
marketplace-suggestions
1 year ago
meta-boxes
1 week ago
notes
3 months ago
plugin-updates
2 years ago
reports
2 weeks ago
settings
2 weeks ago
views
2 weeks ago
class-wc-admin-addons.php
10 months ago
class-wc-admin-api-keys-table-list.php
3 years ago
class-wc-admin-api-keys.php
1 month ago
class-wc-admin-assets.php
2 weeks ago
class-wc-admin-attributes.php
2 weeks ago
class-wc-admin-brands.php
2 weeks ago
class-wc-admin-customize.php
6 years ago
class-wc-admin-dashboard-setup.php
2 weeks ago
class-wc-admin-dashboard.php
2 weeks ago
class-wc-admin-duplicate-product.php
2 weeks ago
class-wc-admin-exporters.php
1 year ago
class-wc-admin-help.php
2 years ago
class-wc-admin-importers.php
1 year ago
class-wc-admin-log-table-list.php
6 months ago
class-wc-admin-marketplace-promotions.php
1 month ago
class-wc-admin-menus.php
2 weeks ago
class-wc-admin-meta-boxes.php
2 months ago
class-wc-admin-notices.php
2 weeks ago
class-wc-admin-permalink-settings.php
5 years ago
class-wc-admin-pointers.php
3 years ago
class-wc-admin-post-types.php
2 weeks ago
class-wc-admin-profile.php
1 year ago
class-wc-admin-reports.php
6 months ago
class-wc-admin-settings.php
2 weeks ago
class-wc-admin-setup-wizard.php
4 days ago
class-wc-admin-status.php
1 year ago
class-wc-admin-taxonomies.php
2 months ago
class-wc-admin-upload-downloadable-product.php
2 years ago
class-wc-admin-webhooks-table-list.php
2 months ago
class-wc-admin-webhooks.php
1 month ago
class-wc-admin.php
2 weeks ago
wc-admin-functions.php
2 weeks ago
wc-meta-box-functions.php
1 year ago
woocommerce-legacy-reports.php
1 year ago
class-wc-admin-menus.php
678 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Setup menus in WP admin. |
| 4 | * |
| 5 | * @package WooCommerce\Admin |
| 6 | * @version 2.5.0 |
| 7 | */ |
| 8 | |
| 9 | use Automattic\WooCommerce\Internal\Admin\Marketplace; |
| 10 | use Automattic\WooCommerce\Internal\Admin\Orders\COTRedirectionController; |
| 11 | use Automattic\WooCommerce\Internal\Admin\Orders\PageController as Custom_Orders_PageController; |
| 12 | use Automattic\WooCommerce\Internal\Admin\Logging\PageController as LoggingPageController; |
| 13 | use Automattic\WooCommerce\Internal\Admin\Logging\FileV2\{ FileListTable, SearchListTable }; |
| 14 | use Automattic\WooCommerce\Internal\DataStores\Orders\CustomOrdersTableController; |
| 15 | |
| 16 | defined( 'ABSPATH' ) || exit; |
| 17 | |
| 18 | if ( class_exists( 'WC_Admin_Menus', false ) ) { |
| 19 | return new WC_Admin_Menus(); |
| 20 | } |
| 21 | |
| 22 | /** |
| 23 | * WC_Admin_Menus Class. |
| 24 | */ |
| 25 | class WC_Admin_Menus { |
| 26 | |
| 27 | /** |
| 28 | * The CSS classes used to hide the submenu items in navigation. |
| 29 | * |
| 30 | * @var string |
| 31 | */ |
| 32 | const HIDE_CSS_CLASS = 'hide-if-js'; |
| 33 | |
| 34 | /** |
| 35 | * Hook in tabs. |
| 36 | */ |
| 37 | public function __construct() { |
| 38 | // Add menus. |
| 39 | add_action( 'admin_menu', array( $this, 'menu_highlight' ) ); |
| 40 | add_action( 'admin_menu', array( $this, 'menu_order_count' ) ); |
| 41 | add_action( 'admin_menu', array( $this, 'admin_menu' ), 9 ); |
| 42 | add_action( 'admin_menu', array( $this, 'orders_menu' ), 9 ); |
| 43 | add_action( 'admin_menu', array( $this, 'reports_menu' ), 20 ); |
| 44 | add_action( 'admin_menu', array( $this, 'settings_menu' ), 50 ); |
| 45 | add_action( 'admin_menu', array( $this, 'status_menu' ), 60 ); |
| 46 | |
| 47 | /** |
| 48 | * Controls whether we add a submenu item for the WooCommerce Addons page. |
| 49 | * Woo Express uses this filter. |
| 50 | * |
| 51 | * @since 8.2.1 |
| 52 | * |
| 53 | * @param bool $show_addons_page If the addons page should be included. |
| 54 | */ |
| 55 | if ( apply_filters( 'woocommerce_show_addons_page', true ) ) { |
| 56 | $container = wc_get_container(); |
| 57 | $container->get( Marketplace::class ); |
| 58 | |
| 59 | add_action( 'admin_menu', array( $this, 'addons_my_subscriptions' ), 70 ); |
| 60 | } |
| 61 | |
| 62 | add_filter( 'menu_order', array( $this, 'menu_order' ) ); |
| 63 | add_filter( 'custom_menu_order', array( $this, 'custom_menu_order' ) ); |
| 64 | add_filter( 'set-screen-option', array( $this, 'set_screen_option' ), 10, 3 ); |
| 65 | |
| 66 | // Add endpoints custom URLs in Appearance > Menus > Pages. |
| 67 | add_action( 'admin_head-nav-menus.php', array( $this, 'add_nav_menu_meta_boxes' ) ); |
| 68 | |
| 69 | // Ensure WC taxonomy meta boxes are visible by default on the first visit to Appearance > Menus. |
| 70 | add_action( 'load-nav-menus.php', array( $this, 'register_default_nav_menu_meta_boxes_filter' ), 9 ); |
| 71 | |
| 72 | // Admin bar menus. |
| 73 | if ( apply_filters( 'woocommerce_show_admin_bar_visit_store', true ) ) { |
| 74 | add_action( 'admin_bar_menu', array( $this, 'admin_bar_menus' ), 31 ); |
| 75 | } |
| 76 | |
| 77 | // Handle saving settings earlier than load-{page} hook to avoid race conditions in conditional menus. |
| 78 | add_action( 'wp_loaded', array( $this, 'save_settings' ) ); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Add menu items. |
| 83 | * |
| 84 | * @return void |
| 85 | */ |
| 86 | public function admin_menu() { |
| 87 | global $menu, $admin_page_hooks; |
| 88 | |
| 89 | $woocommerce_icon = 'data:image/svg+xml;base64,PHN2ZyB2ZXJzaW9uPSIxLjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmlld0JveD0iMCAwIDg1LjkgNDcuNiI+CjxwYXRoIGZpbGw9IiNhMmFhYjIiIGQ9Ik03Ny40LDAuMWMtNC4zLDAtNy4xLDEuNC05LjYsNi4xTDU2LjQsMjcuN1Y4LjZjMC01LjctMi43LTguNS03LjctOC41cy03LjEsMS43LTkuNiw2LjVMMjguMywyNy43VjguOAoJYzAtNi4xLTIuNS04LjctOC42LTguN0g3LjNDMi42LDAuMSwwLDIuMywwLDYuM3MyLjUsNi40LDcuMSw2LjRoNS4xdjI0LjFjMCw2LjgsNC42LDEwLjgsMTEuMiwxMC44UzMzLDQ1LDM2LjMsMzguOWw3LjItMTMuNXYxMS40CgljMCw2LjcsNC40LDEwLjgsMTEuMSwxMC44czkuMi0yLjMsMTMtOC43bDE2LjYtMjhjMy42LTYuMSwxLjEtMTAuOC02LjktMTAuOEM3Ny4zLDAuMSw3Ny4zLDAuMSw3Ny40LDAuMXoiLz4KPC9zdmc+Cg=='; |
| 90 | |
| 91 | if ( self::can_view_woocommerce_menu_item() ) { |
| 92 | $menu[] = array( '', 'read', 'separator-woocommerce', '', 'wp-menu-separator woocommerce' ); // WPCS: override ok. |
| 93 | } |
| 94 | |
| 95 | add_menu_page( __( 'WooCommerce', 'woocommerce' ), __( 'WooCommerce', 'woocommerce' ), 'edit_others_shop_orders', 'woocommerce', null, $woocommerce_icon, '55.5' ); |
| 96 | |
| 97 | // Work around https://github.com/woocommerce/woocommerce/issues/35677 (and related https://core.trac.wordpress.org/ticket/18857). |
| 98 | // Translating the menu item breaks screen IDs and page hooks, so we force the hookname to be untranslated. |
| 99 | $admin_page_hooks['woocommerce'] = 'woocommerce'; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
| 100 | |
| 101 | add_submenu_page( 'edit.php?post_type=product', __( 'Attributes', 'woocommerce' ), __( 'Attributes', 'woocommerce' ), 'manage_product_terms', 'product_attributes', array( $this, 'attributes_page' ) ); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Add menu item. |
| 106 | * |
| 107 | * @return void |
| 108 | */ |
| 109 | public function reports_menu() { |
| 110 | if ( self::can_view_woocommerce_menu_item() ) { |
| 111 | add_submenu_page( 'woocommerce', __( 'Reports', 'woocommerce' ), __( 'Reports', 'woocommerce' ), 'view_woocommerce_reports', 'wc-reports', array( $this, 'reports_page' ) ); |
| 112 | } else { |
| 113 | add_menu_page( __( 'Sales reports', 'woocommerce' ), __( 'Sales reports', 'woocommerce' ), 'view_woocommerce_reports', 'wc-reports', array( $this, 'reports_page' ), 'dashicons-chart-bar', '55.6' ); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Add menu item. |
| 119 | * |
| 120 | * @return void |
| 121 | */ |
| 122 | public function settings_menu() { |
| 123 | $settings_page = add_submenu_page( |
| 124 | 'woocommerce', |
| 125 | __( 'WooCommerce settings', 'woocommerce' ), |
| 126 | __( 'Settings', 'woocommerce' ), |
| 127 | 'manage_woocommerce', |
| 128 | 'wc-settings', |
| 129 | array( $this, 'settings_page' ) |
| 130 | ); |
| 131 | |
| 132 | add_action( 'load-' . $settings_page, array( $this, 'settings_page_init' ) ); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Check if the user can access the top-level WooCommerce item. |
| 137 | * |
| 138 | * @return bool |
| 139 | */ |
| 140 | public static function can_view_woocommerce_menu_item() { |
| 141 | return current_user_can( 'edit_others_shop_orders' ); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Loads gateways and shipping methods into memory for use within settings. |
| 146 | * |
| 147 | * @return void |
| 148 | */ |
| 149 | public function settings_page_init() { |
| 150 | WC()->payment_gateways(); |
| 151 | WC()->shipping(); |
| 152 | |
| 153 | // Include settings pages. |
| 154 | WC_Admin_Settings::get_settings_pages(); |
| 155 | |
| 156 | // Add any posted messages. |
| 157 | if ( ! empty( $_GET['wc_error'] ) ) { // WPCS: input var okay, CSRF ok. |
| 158 | WC_Admin_Settings::add_error( wp_kses_post( wp_unslash( $_GET['wc_error'] ) ) ); // WPCS: input var okay, CSRF ok. |
| 159 | } |
| 160 | |
| 161 | if ( ! empty( $_GET['wc_message'] ) ) { // WPCS: input var okay, CSRF ok. |
| 162 | WC_Admin_Settings::add_message( wp_kses_post( wp_unslash( $_GET['wc_message'] ) ) ); // WPCS: input var okay, CSRF ok. |
| 163 | } |
| 164 | |
| 165 | do_action( 'woocommerce_settings_page_init' ); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Handle saving of settings. |
| 170 | * |
| 171 | * @return void |
| 172 | */ |
| 173 | public function save_settings() { |
| 174 | global $current_tab, $current_section; |
| 175 | |
| 176 | // We should only save on the settings page. |
| 177 | if ( ! is_wc_admin_settings_page() ) { |
| 178 | return; |
| 179 | } |
| 180 | |
| 181 | // Include settings pages. |
| 182 | WC_Admin_Settings::get_settings_pages(); |
| 183 | |
| 184 | // Get current tab/section. |
| 185 | $current_tab = empty( $_GET['tab'] ) ? 'general' : sanitize_title( wp_unslash( $_GET['tab'] ) ); // WPCS: input var okay, CSRF ok. |
| 186 | $current_section = empty( $_REQUEST['section'] ) ? '' : sanitize_title( wp_unslash( $_REQUEST['section'] ) ); // WPCS: input var okay, CSRF ok. |
| 187 | |
| 188 | // Save settings if data has been posted. |
| 189 | if ( '' !== $current_section && apply_filters( "woocommerce_save_settings_{$current_tab}_{$current_section}", ! empty( $_POST['save'] ) ) ) { // WPCS: input var okay, CSRF ok. |
| 190 | WC_Admin_Settings::save(); |
| 191 | } elseif ( '' === $current_section && apply_filters( "woocommerce_save_settings_{$current_tab}", ! empty( $_POST['save'] ) ) ) { // WPCS: input var okay, CSRF ok. |
| 192 | WC_Admin_Settings::save(); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Add menu item. |
| 198 | * |
| 199 | * @return void |
| 200 | */ |
| 201 | public function status_menu() { |
| 202 | $status_page = add_submenu_page( 'woocommerce', __( 'WooCommerce status', 'woocommerce' ), __( 'Status', 'woocommerce' ), 'manage_woocommerce', 'wc-status', array( $this, 'status_page' ) ); |
| 203 | |
| 204 | add_action( |
| 205 | 'load-' . $status_page, |
| 206 | function () { |
| 207 | if ( 'logs' === filter_input( INPUT_GET, 'tab' ) ) { |
| 208 | // Initialize the logging page controller early so that it can hook into things. |
| 209 | wc_get_container()->get( LoggingPageController::class ); |
| 210 | } |
| 211 | }, |
| 212 | 1 |
| 213 | ); |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Addons menu item. |
| 218 | * |
| 219 | * @deprecated 10.5.0 The marketplace feature is now always enabled. Use the Extensions menu instead. |
| 220 | * |
| 221 | * @return void |
| 222 | */ |
| 223 | public function addons_menu() { |
| 224 | wc_deprecated_function( __METHOD__, '10.5.0' ); |
| 225 | |
| 226 | $count_html = WC_Helper_Updater::get_updates_count_html(); |
| 227 | /* translators: %s: extensions count */ |
| 228 | $menu_title = sprintf( __( 'Extensions %s', 'woocommerce' ), $count_html ); |
| 229 | add_submenu_page( 'woocommerce', __( 'WooCommerce extensions', 'woocommerce' ), $menu_title, 'manage_woocommerce', 'wc-addons', array( $this, 'addons_page' ) ); |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Registers the wc-addons page within the WooCommerce menu. |
| 234 | * Temporary measure till we convert the whole page to React. |
| 235 | * |
| 236 | * @return void |
| 237 | */ |
| 238 | public function addons_my_subscriptions() { |
| 239 | add_submenu_page( 'woocommerce', __( 'WooCommerce extensions', 'woocommerce' ), null, 'manage_woocommerce', 'wc-addons', array( $this, 'addons_page' ) ); |
| 240 | // Temporarily hide the submenu item we've just added. |
| 241 | $this->hide_submenu_page( 'woocommerce', 'wc-addons' ); |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Highlights the correct top level admin menu item for post type add screens. |
| 246 | * |
| 247 | * @return void |
| 248 | */ |
| 249 | public function menu_highlight() { |
| 250 | global $parent_file, $submenu_file, $post_type; |
| 251 | |
| 252 | switch ( $post_type ) { |
| 253 | case 'shop_order': |
| 254 | case 'shop_coupon': |
| 255 | $parent_file = 'woocommerce'; // WPCS: override ok. |
| 256 | break; |
| 257 | case 'product': |
| 258 | $screen = get_current_screen(); |
| 259 | if ( $screen && taxonomy_is_product_attribute( $screen->taxonomy ) ) { |
| 260 | $submenu_file = 'product_attributes'; // WPCS: override ok. |
| 261 | $parent_file = 'edit.php?post_type=product'; // WPCS: override ok. |
| 262 | } |
| 263 | break; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * Adds the order processing count to the menu. |
| 269 | * |
| 270 | * @return void |
| 271 | */ |
| 272 | public function menu_order_count() { |
| 273 | global $submenu; |
| 274 | |
| 275 | if ( isset( $submenu['woocommerce'] ) ) { |
| 276 | // Remove 'WooCommerce' sub menu item. |
| 277 | unset( $submenu['woocommerce'][0] ); |
| 278 | |
| 279 | // Add count if user has access. |
| 280 | if ( apply_filters( 'woocommerce_include_processing_order_count_in_menu', true ) && current_user_can( 'edit_others_shop_orders' ) ) { |
| 281 | $order_count = apply_filters( 'woocommerce_menu_order_count', wc_processing_order_count() ); |
| 282 | |
| 283 | if ( $order_count ) { |
| 284 | foreach ( $submenu['woocommerce'] as $key => $menu_item ) { |
| 285 | if ( 0 === strpos( $menu_item[0], _x( 'Orders', 'Admin menu name', 'woocommerce' ) ) ) { |
| 286 | $submenu['woocommerce'][ $key ][0] .= ' <span class="menu-counter count-' . esc_attr( $order_count ) . '"><span class="processing-count">' . number_format_i18n( $order_count ) . '</span></span>'; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
| 287 | break; |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | /** |
| 296 | * Reorder the WC menu items in admin. |
| 297 | * |
| 298 | * @param int $menu_order Menu order. |
| 299 | * @return array |
| 300 | */ |
| 301 | public function menu_order( $menu_order ) { |
| 302 | // Initialize our custom order array. |
| 303 | $woocommerce_menu_order = array(); |
| 304 | |
| 305 | // Get the index of our custom separator. |
| 306 | $woocommerce_separator = array_search( 'separator-woocommerce', $menu_order, true ); |
| 307 | |
| 308 | // Get index of product menu. |
| 309 | $woocommerce_product = array_search( 'edit.php?post_type=product', $menu_order, true ); |
| 310 | |
| 311 | // Loop through menu order and do some rearranging. |
| 312 | foreach ( $menu_order as $index => $item ) { |
| 313 | |
| 314 | if ( 'woocommerce' === $item ) { |
| 315 | $woocommerce_menu_order[] = 'separator-woocommerce'; |
| 316 | $woocommerce_menu_order[] = $item; |
| 317 | $woocommerce_menu_order[] = 'edit.php?post_type=product'; |
| 318 | unset( $menu_order[ $woocommerce_separator ] ); |
| 319 | unset( $menu_order[ $woocommerce_product ] ); |
| 320 | } elseif ( ! in_array( $item, array( 'separator-woocommerce' ), true ) ) { |
| 321 | $woocommerce_menu_order[] = $item; |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | // Return order. |
| 326 | return $woocommerce_menu_order; |
| 327 | } |
| 328 | |
| 329 | /** |
| 330 | * Custom menu order. |
| 331 | * |
| 332 | * @param bool $enabled Whether custom menu ordering is already enabled. |
| 333 | * @return bool |
| 334 | */ |
| 335 | public function custom_menu_order( $enabled ) { |
| 336 | return $enabled || self::can_view_woocommerce_menu_item(); |
| 337 | } |
| 338 | |
| 339 | /** |
| 340 | * Validate screen options on update. |
| 341 | * |
| 342 | * @param bool|int $status Screen option value. Default false to skip. |
| 343 | * @param string $option The option name. |
| 344 | * @param int $value The number of rows to use. |
| 345 | * @return bool|int |
| 346 | */ |
| 347 | public function set_screen_option( $status, $option, $value ) { |
| 348 | $screen_options = array( |
| 349 | 'woocommerce_keys_per_page', |
| 350 | 'woocommerce_webhooks_per_page', |
| 351 | FileListTable::PER_PAGE_USER_OPTION_KEY, |
| 352 | SearchListTable::PER_PAGE_USER_OPTION_KEY, |
| 353 | WC_Admin_Log_Table_List::PER_PAGE_USER_OPTION_KEY, |
| 354 | ); |
| 355 | |
| 356 | if ( in_array( $option, $screen_options, true ) ) { |
| 357 | return $value; |
| 358 | } |
| 359 | |
| 360 | return $status; |
| 361 | } |
| 362 | |
| 363 | /** |
| 364 | * Init the reports page. |
| 365 | * |
| 366 | * @return void |
| 367 | */ |
| 368 | public function reports_page() { |
| 369 | WC_Admin_Reports::output(); |
| 370 | } |
| 371 | |
| 372 | /** |
| 373 | * Init the settings page. |
| 374 | * |
| 375 | * @return void |
| 376 | */ |
| 377 | public function settings_page() { |
| 378 | WC_Admin_Settings::output(); |
| 379 | } |
| 380 | |
| 381 | /** |
| 382 | * Init the attributes page. |
| 383 | * |
| 384 | * @return void |
| 385 | */ |
| 386 | public function attributes_page() { |
| 387 | WC_Admin_Attributes::output(); |
| 388 | } |
| 389 | |
| 390 | /** |
| 391 | * Init the status page. |
| 392 | * |
| 393 | * @return void |
| 394 | */ |
| 395 | public function status_page() { |
| 396 | WC_Admin_Status::output(); |
| 397 | } |
| 398 | |
| 399 | /** |
| 400 | * Init the addons page. |
| 401 | * |
| 402 | * @return void |
| 403 | */ |
| 404 | public function addons_page() { |
| 405 | WC_Admin_Addons::handle_legacy_marketplace_redirects(); |
| 406 | } |
| 407 | |
| 408 | /** |
| 409 | * Link to the order admin list table from the main WooCommerce menu. |
| 410 | * |
| 411 | * @return void |
| 412 | */ |
| 413 | public function orders_menu(): void { |
| 414 | if ( wc_get_container()->get( CustomOrdersTableController::class )->custom_orders_table_usage_is_enabled() ) { |
| 415 | wc_get_container()->get( Custom_Orders_PageController::class )->setup(); |
| 416 | } else { |
| 417 | wc_get_container()->get( COTRedirectionController::class )->setup(); |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | /** |
| 422 | * Add custom nav meta box. |
| 423 | * |
| 424 | * Adapted from http://www.johnmorrisonline.com/how-to-add-a-fully-functional-custom-meta-box-to-wordpress-navigation-menus/. |
| 425 | * |
| 426 | * @return void |
| 427 | */ |
| 428 | public function add_nav_menu_meta_boxes() { |
| 429 | add_meta_box( 'woocommerce_endpoints_nav_link', __( 'WooCommerce endpoints', 'woocommerce' ), array( $this, 'nav_menu_links' ), 'nav-menus', 'side', 'low' ); |
| 430 | } |
| 431 | |
| 432 | /** |
| 433 | * Ensure the Product Categories, Product Tags, and Brands meta boxes |
| 434 | * are visible by default on the Appearance > Menus screen. |
| 435 | * |
| 436 | * WordPress's wp_initial_nav_menu_meta_boxes() hides every box except page, |
| 437 | * post, custom-links, and category on a user's first visit. We intercept |
| 438 | * the empty user option and return a hidden list that excludes the WC |
| 439 | * taxonomy boxes, so WP's existing-user guard short-circuits and leaves |
| 440 | * them visible. |
| 441 | * |
| 442 | * The computed list is persisted to user meta on that first read, mirroring |
| 443 | * core's wp_initial_nav_menu_meta_boxes(). This keeps the snapshot stable |
| 444 | * across the multiple reads in a single page load and, crucially, across |
| 445 | * later requests: boxes registered by plugins installed after the first |
| 446 | * visit stay visible, matching vanilla WordPress instead of defaulting to |
| 447 | * hidden on every recompute. As in core, only the current user's snapshot is |
| 448 | * persisted, so reads on behalf of another user never overwrite that user's |
| 449 | * own first-visit snapshot. |
| 450 | * |
| 451 | * @since 11.1.0 |
| 452 | * |
| 453 | * @return void |
| 454 | */ |
| 455 | public function register_default_nav_menu_meta_boxes_filter() { |
| 456 | add_filter( 'get_user_option_metaboxhidden_nav-menus', array( $this, 'filter_default_nav_menu_hidden_meta_boxes' ), 10, 3 ); |
| 457 | } |
| 458 | |
| 459 | /** |
| 460 | * Filter the default hidden meta boxes for the Appearance > Menus screen. |
| 461 | * |
| 462 | * Only applies when the user has no saved preference for the nav-menus |
| 463 | * screen. Returns a list of every registered nav-menus meta box except the |
| 464 | * four core visible boxes, the "WooCommerce endpoints" box, and the WC |
| 465 | * taxonomy boxes. Product Brands is visible only when its taxonomy exists. |
| 466 | * |
| 467 | * @since 11.1.0 |
| 468 | * |
| 469 | * @param mixed $result Value for the user's option, or false if not set. |
| 470 | * @param string $option Name of the option being retrieved. |
| 471 | * @param WP_User $user WP_User object of the user whose option is being retrieved. |
| 472 | * @return mixed The filtered option value. |
| 473 | */ |
| 474 | public function filter_default_nav_menu_hidden_meta_boxes( $result, $option, $user ) { |
| 475 | global $wp_meta_boxes; |
| 476 | |
| 477 | // Once a snapshot exists the option reads non-false, so this early return |
| 478 | // also makes the update_user_meta() call below idempotent: a re-entrant read |
| 479 | // bails here instead of persisting again. |
| 480 | if ( false !== $result ) { |
| 481 | return $result; |
| 482 | } |
| 483 | |
| 484 | if ( ! $user || ! isset( $wp_meta_boxes['nav-menus'] ) || ! is_array( $wp_meta_boxes['nav-menus'] ) ) { |
| 485 | return $result; |
| 486 | } |
| 487 | |
| 488 | $visible = array( |
| 489 | 'add-post-type-page', |
| 490 | 'add-post-type-post', |
| 491 | 'add-custom-links', |
| 492 | 'add-category', |
| 493 | 'add-product_cat', |
| 494 | 'add-product_tag', |
| 495 | 'woocommerce_endpoints_nav_link', |
| 496 | ); |
| 497 | |
| 498 | if ( taxonomy_exists( 'product_brand' ) ) { |
| 499 | $visible[] = 'add-product_brand'; |
| 500 | } |
| 501 | |
| 502 | $hidden = array(); |
| 503 | foreach ( $wp_meta_boxes['nav-menus'] as $priorities ) { |
| 504 | foreach ( (array) $priorities as $priority => $boxes ) { |
| 505 | foreach ( (array) $boxes as $box ) { |
| 506 | if ( isset( $box['id'] ) && ! in_array( $box['id'], $visible, true ) ) { |
| 507 | $hidden[] = $box['id']; |
| 508 | } |
| 509 | } |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | // Persist the snapshot, mirroring core's wp_initial_nav_menu_meta_boxes(), |
| 514 | // so later reads in this request and in future requests return this same |
| 515 | // list instead of recomputing it against a changed meta box registry. |
| 516 | // |
| 517 | // Like core, only the current user's snapshot is persisted. This filter |
| 518 | // runs for whichever user's option is being read, so a read performed on |
| 519 | // behalf of another user (user-switching or admin tooling) still receives |
| 520 | // the computed default below for display, but must not consume that user's |
| 521 | // durable first-visit snapshot from this request's meta box registry. |
| 522 | $current_user_id = get_current_user_id(); |
| 523 | if ( $current_user_id && (int) $user->ID === $current_user_id ) { |
| 524 | update_user_meta( $current_user_id, 'metaboxhidden_nav-menus', $hidden ); |
| 525 | } |
| 526 | |
| 527 | return $hidden; |
| 528 | } |
| 529 | |
| 530 | /** |
| 531 | * Output menu links. |
| 532 | * |
| 533 | * @return void |
| 534 | */ |
| 535 | public function nav_menu_links() { |
| 536 | // Get items from account menu. |
| 537 | $endpoints = wc_get_account_menu_items(); |
| 538 | |
| 539 | // Remove dashboard item. |
| 540 | if ( isset( $endpoints['dashboard'] ) ) { |
| 541 | unset( $endpoints['dashboard'] ); |
| 542 | } |
| 543 | |
| 544 | // Include missing lost password endpoint, if set in WooCommerce > Settings > Advanced > Account endpoints. |
| 545 | if ( ! empty( get_option( 'woocommerce_myaccount_lost_password_endpoint' ) ) ) { |
| 546 | $endpoints['lost-password'] = __( 'Lost password', 'woocommerce' ); |
| 547 | } |
| 548 | |
| 549 | $endpoints = apply_filters( 'woocommerce_custom_nav_menu_items', $endpoints ); |
| 550 | |
| 551 | ?> |
| 552 | <div id="posttype-woocommerce-endpoints" class="posttypediv"> |
| 553 | <div id="tabs-panel-woocommerce-endpoints" class="tabs-panel tabs-panel-active"> |
| 554 | <ul id="woocommerce-endpoints-checklist" class="categorychecklist form-no-clear"> |
| 555 | <?php |
| 556 | $i = -1; |
| 557 | foreach ( $endpoints as $key => $value ) : |
| 558 | ?> |
| 559 | <li> |
| 560 | <label class="menu-item-title"> |
| 561 | <input type="checkbox" class="menu-item-checkbox" name="menu-item[<?php echo esc_attr( $i ); ?>][menu-item-object-id]" value="<?php echo esc_attr( $i ); ?>" /> <?php echo esc_html( $value ); ?> |
| 562 | </label> |
| 563 | <input type="hidden" class="menu-item-type" name="menu-item[<?php echo esc_attr( $i ); ?>][menu-item-type]" value="custom" /> |
| 564 | <input type="hidden" class="menu-item-title" name="menu-item[<?php echo esc_attr( $i ); ?>][menu-item-title]" value="<?php echo esc_attr( $value ); ?>" /> |
| 565 | <input type="hidden" class="menu-item-url" name="menu-item[<?php echo esc_attr( $i ); ?>][menu-item-url]" value="<?php echo esc_url( wc_get_account_endpoint_url( $key ) ); ?>" /> |
| 566 | <input type="hidden" class="menu-item-classes" name="menu-item[<?php echo esc_attr( $i ); ?>][menu-item-classes]" /> |
| 567 | </li> |
| 568 | <?php |
| 569 | --$i; |
| 570 | endforeach; |
| 571 | ?> |
| 572 | </ul> |
| 573 | </div> |
| 574 | <p class="button-controls" data-items-type="posttype-woocommerce-endpoints"> |
| 575 | <span class="list-controls"> |
| 576 | <label> |
| 577 | <input type="checkbox" class="select-all" /> |
| 578 | <?php esc_html_e( 'Select all', 'woocommerce' ); ?> |
| 579 | </label> |
| 580 | </span> |
| 581 | <span class="add-to-menu"> |
| 582 | <button type="submit" class="button-secondary submit-add-to-menu right" value="<?php esc_attr_e( 'Add to menu', 'woocommerce' ); ?>" name="add-post-type-menu-item" id="submit-posttype-woocommerce-endpoints"><?php esc_html_e( 'Add to menu', 'woocommerce' ); ?></button> |
| 583 | <span class="spinner"></span> |
| 584 | </span> |
| 585 | </p> |
| 586 | </div> |
| 587 | <?php |
| 588 | } |
| 589 | |
| 590 | /** |
| 591 | * Add the "Visit Store" link in admin bar main menu. |
| 592 | * |
| 593 | * @since 2.4.0 |
| 594 | * @param WP_Admin_Bar $wp_admin_bar Admin bar instance. |
| 595 | * @return void |
| 596 | */ |
| 597 | public function admin_bar_menus( $wp_admin_bar ) { |
| 598 | if ( ! is_admin() || ! is_admin_bar_showing() ) { |
| 599 | return; |
| 600 | } |
| 601 | |
| 602 | // Show only when the user is a member of this site, or they're a super admin. |
| 603 | if ( ! is_user_member_of_blog() && ! is_super_admin() ) { |
| 604 | return; |
| 605 | } |
| 606 | |
| 607 | // Don't display when shop page is the same of the page on front. |
| 608 | if ( intval( get_option( 'page_on_front' ) ) === wc_get_page_id( 'shop' ) ) { |
| 609 | return; |
| 610 | } |
| 611 | |
| 612 | // Add an option to visit the store. |
| 613 | $wp_admin_bar->add_node( |
| 614 | array( |
| 615 | 'parent' => 'site-name', |
| 616 | 'id' => 'view-store', |
| 617 | 'title' => __( 'Visit Store', 'woocommerce' ), |
| 618 | 'href' => wc_get_page_permalink( 'shop' ), |
| 619 | ) |
| 620 | ); |
| 621 | } |
| 622 | |
| 623 | /** |
| 624 | * Hide the submenu page based on slug and return the item that was hidden. |
| 625 | * |
| 626 | * Borrowed from Jetpack's Base_Admin_Menu class. |
| 627 | * |
| 628 | * Instead of actually removing the submenu item, a safer approach is to hide it and filter it in the API response. |
| 629 | * In this manner we'll avoid breaking third-party plugins depending on items that no longer exist. |
| 630 | * |
| 631 | * A false|array value is returned to be consistent with remove_submenu_page() function |
| 632 | * |
| 633 | * @param string $menu_slug The parent menu slug. |
| 634 | * @param string $submenu_slug The submenu slug that should be hidden. |
| 635 | * @return false|array |
| 636 | */ |
| 637 | public function hide_submenu_page( $menu_slug, $submenu_slug ) { |
| 638 | global $submenu; |
| 639 | |
| 640 | if ( ! isset( $submenu[ $menu_slug ] ) ) { |
| 641 | return false; |
| 642 | } |
| 643 | |
| 644 | foreach ( $submenu[ $menu_slug ] as $i => $item ) { |
| 645 | if ( $submenu_slug !== $item[2] ) { |
| 646 | continue; |
| 647 | } |
| 648 | |
| 649 | $this->hide_submenu_element( $i, $menu_slug, $item ); |
| 650 | |
| 651 | return $item; |
| 652 | } |
| 653 | |
| 654 | return false; |
| 655 | } |
| 656 | |
| 657 | /** |
| 658 | * Apply the hide-if-js CSS class to a submenu item. |
| 659 | * |
| 660 | * Borrowed from Jetpack's Base_Admin_Menu class. |
| 661 | * |
| 662 | * @param int $index The position of a submenu item in the submenu array. |
| 663 | * @param string $parent_slug The parent slug. |
| 664 | * @param array $item The submenu item. |
| 665 | * @return void |
| 666 | */ |
| 667 | public function hide_submenu_element( $index, $parent_slug, $item ) { |
| 668 | global $submenu; |
| 669 | |
| 670 | $css_classes = empty( $item[4] ) ? self::HIDE_CSS_CLASS : $item[4] . ' ' . self::HIDE_CSS_CLASS; |
| 671 | |
| 672 | // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
| 673 | $submenu [ $parent_slug ][ $index ][4] = $css_classes; |
| 674 | } |
| 675 | } |
| 676 | |
| 677 | return new WC_Admin_Menus(); |
| 678 |