get( Marketplace::class ); add_action( 'admin_menu', array( $this, 'addons_my_subscriptions' ), 70 ); } add_filter( 'menu_order', array( $this, 'menu_order' ) ); add_filter( 'custom_menu_order', array( $this, 'custom_menu_order' ) ); add_filter( 'set-screen-option', array( $this, 'set_screen_option' ), 10, 3 ); // Add endpoints custom URLs in Appearance > Menus > Pages. add_action( 'admin_head-nav-menus.php', array( $this, 'add_nav_menu_meta_boxes' ) ); // Ensure WC taxonomy meta boxes are visible by default on the first visit to Appearance > Menus. add_action( 'load-nav-menus.php', array( $this, 'register_default_nav_menu_meta_boxes_filter' ), 9 ); // Admin bar menus. if ( apply_filters( 'woocommerce_show_admin_bar_visit_store', true ) ) { add_action( 'admin_bar_menu', array( $this, 'admin_bar_menus' ), 31 ); } // Handle saving settings earlier than load-{page} hook to avoid race conditions in conditional menus. add_action( 'wp_loaded', array( $this, 'save_settings' ) ); } /** * Add menu items. * * @return void */ public function admin_menu() { global $menu, $admin_page_hooks; $woocommerce_icon = 'data:image/svg+xml;base64,PHN2ZyB2ZXJzaW9uPSIxLjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmlld0JveD0iMCAwIDg1LjkgNDcuNiI+CjxwYXRoIGZpbGw9IiNhMmFhYjIiIGQ9Ik03Ny40LDAuMWMtNC4zLDAtNy4xLDEuNC05LjYsNi4xTDU2LjQsMjcuN1Y4LjZjMC01LjctMi43LTguNS03LjctOC41cy03LjEsMS43LTkuNiw2LjVMMjguMywyNy43VjguOAoJYzAtNi4xLTIuNS04LjctOC42LTguN0g3LjNDMi42LDAuMSwwLDIuMywwLDYuM3MyLjUsNi40LDcuMSw2LjRoNS4xdjI0LjFjMCw2LjgsNC42LDEwLjgsMTEuMiwxMC44UzMzLDQ1LDM2LjMsMzguOWw3LjItMTMuNXYxMS40CgljMCw2LjcsNC40LDEwLjgsMTEuMSwxMC44czkuMi0yLjMsMTMtOC43bDE2LjYtMjhjMy42LTYuMSwxLjEtMTAuOC02LjktMTAuOEM3Ny4zLDAuMSw3Ny4zLDAuMSw3Ny40LDAuMXoiLz4KPC9zdmc+Cg=='; if ( self::can_view_woocommerce_menu_item() ) { $menu[] = array( '', 'read', 'separator-woocommerce', '', 'wp-menu-separator woocommerce' ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- WordPress uses the $menu global for admin menu registration. } add_menu_page( __( 'WooCommerce', 'woocommerce' ), __( 'WooCommerce', 'woocommerce' ), 'edit_others_shop_orders', 'woocommerce', null, $woocommerce_icon, '55.5' ); // Work around https://github.com/woocommerce/woocommerce/issues/35677 (and related https://core.trac.wordpress.org/ticket/18857). // Translating the menu item breaks screen IDs and page hooks, so we force the hookname to be untranslated. $admin_page_hooks['woocommerce'] = 'woocommerce'; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited add_submenu_page( 'edit.php?post_type=product', __( 'Attributes', 'woocommerce' ), __( 'Attributes', 'woocommerce' ), 'manage_product_terms', 'product_attributes', array( $this, 'attributes_page' ) ); } /** * Add menu item. * * @return void */ public function reports_menu() { if ( self::can_view_woocommerce_menu_item() ) { add_submenu_page( 'woocommerce', __( 'Reports', 'woocommerce' ), __( 'Reports', 'woocommerce' ), 'view_woocommerce_reports', 'wc-reports', array( $this, 'reports_page' ) ); } else { add_menu_page( __( 'Sales reports', 'woocommerce' ), __( 'Sales reports', 'woocommerce' ), 'view_woocommerce_reports', 'wc-reports', array( $this, 'reports_page' ), 'dashicons-chart-bar', '55.6' ); } } /** * Add menu item. * * @return void */ public function settings_menu() { $settings_page = add_submenu_page( 'woocommerce', __( 'WooCommerce settings', 'woocommerce' ), __( 'Settings', 'woocommerce' ), 'manage_woocommerce', 'wc-settings', array( $this, 'settings_page' ) ); add_action( 'load-' . $settings_page, array( $this, 'settings_page_init' ) ); } /** * Check if the user can access the top-level WooCommerce item. * * @return bool */ public static function can_view_woocommerce_menu_item() { return current_user_can( 'edit_others_shop_orders' ); } /** * Loads gateways and shipping methods into memory for use within settings. * * @return void */ public function settings_page_init() { WC()->payment_gateways(); WC()->shipping(); // Include settings pages. WC_Admin_Settings::get_settings_pages(); // Add any posted messages. if ( ! empty( $_GET['wc_error'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only selector; settings mutations verify capability and nonce. WC_Admin_Settings::add_error( wp_kses_post( wp_unslash( $_GET['wc_error'] ) ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only selector; settings mutations verify capability and nonce. } if ( ! empty( $_GET['wc_message'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only selector; settings mutations verify capability and nonce. WC_Admin_Settings::add_message( wp_kses_post( wp_unslash( $_GET['wc_message'] ) ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only selector; settings mutations verify capability and nonce. } do_action( 'woocommerce_settings_page_init' ); } /** * Handle saving of settings. * * @return void */ public function save_settings() { global $current_tab, $current_section; // We should only save on the settings page. if ( ! is_wc_admin_settings_page() ) { return; } // Include settings pages. WC_Admin_Settings::get_settings_pages(); // Get current tab/section. $current_tab = empty( $_GET['tab'] ) ? 'general' : sanitize_title( wp_unslash( $_GET['tab'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only selector; settings mutations verify capability and nonce. $current_section = empty( $_REQUEST['section'] ) ? '' : sanitize_title( wp_unslash( $_REQUEST['section'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only selector; settings mutations verify capability and nonce. // Save settings if data has been posted. /** * Filters whether the posted data for a settings section should be saved. * * The dynamic portions of the hook name, `$current_tab` and `$current_section`, are the * sanitized `tab` and `section` request values of the settings screen being viewed. * * @since 3.3.0 * @param bool $save Whether to save. Defaults to true when the settings form was submitted. */ if ( '' !== $current_section && apply_filters( "woocommerce_save_settings_{$current_tab}_{$current_section}", ! empty( $_POST['save'] ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Presence check only; WC_Admin_Settings::save() verifies the manage_woocommerce capability and the woocommerce-settings nonce before anything is written. WC_Admin_Settings::save(); } elseif ( '' === $current_section /** * Filters whether the posted data for a settings tab should be saved. * * The dynamic portion of the hook name, `$current_tab`, is the sanitized `tab` request * value of the settings screen being viewed. This variant fires only when no section is * selected. * * @since 3.3.0 * @param bool $save Whether to save. Defaults to true when the settings form was submitted. */ && apply_filters( "woocommerce_save_settings_{$current_tab}", ! empty( $_POST['save'] ) ) // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Presence check only; WC_Admin_Settings::save() verifies the manage_woocommerce capability and the woocommerce-settings nonce before anything is written. ) { WC_Admin_Settings::save(); } } /** * Add menu item. * * @return void */ public function status_menu() { $status_page = add_submenu_page( 'woocommerce', __( 'WooCommerce status', 'woocommerce' ), __( 'Status', 'woocommerce' ), 'manage_woocommerce', 'wc-status', array( $this, 'status_page' ) ); add_action( 'load-' . $status_page, function () { if ( 'logs' === filter_input( INPUT_GET, 'tab' ) ) { // Initialize the logging page controller early so that it can hook into things. wc_get_container()->get( LoggingPageController::class ); } }, 1 ); } /** * Addons menu item. * * @deprecated 10.5.0 The marketplace feature is now always enabled. Use the Extensions menu instead. * * @return void */ public function addons_menu() { wc_deprecated_function( __METHOD__, '10.5.0' ); $count_html = WC_Helper_Updater::get_updates_count_html(); /* translators: %s: extensions count */ $menu_title = sprintf( __( 'Extensions %s', 'woocommerce' ), $count_html ); add_submenu_page( 'woocommerce', __( 'WooCommerce extensions', 'woocommerce' ), $menu_title, 'manage_woocommerce', 'wc-addons', array( $this, 'addons_page' ) ); } /** * Registers the wc-addons page within the WooCommerce menu. * Temporary measure till we convert the whole page to React. * * @return void */ public function addons_my_subscriptions() { add_submenu_page( 'woocommerce', __( 'WooCommerce extensions', 'woocommerce' ), null, 'manage_woocommerce', 'wc-addons', array( $this, 'addons_page' ) ); // Temporarily hide the submenu item we've just added. $this->hide_submenu_page( 'woocommerce', 'wc-addons' ); } /** * Highlights the correct top level admin menu item for post type add screens. * * @return void */ public function menu_highlight() { global $parent_file, $submenu_file, $post_type; switch ( $post_type ) { case 'shop_order': case 'shop_coupon': $parent_file = 'woocommerce'; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- WordPress uses $parent_file to control admin menu highlighting. break; case 'product': $screen = get_current_screen(); if ( $screen && taxonomy_is_product_attribute( $screen->taxonomy ) ) { $submenu_file = 'product_attributes'; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- $submenu_file is a WordPress admin menu routing global. $parent_file = 'edit.php?post_type=product'; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- $parent_file is a WordPress admin menu routing global. } break; } } /** * Adds the order processing count to the menu. * * @return void */ public function menu_order_count() { global $submenu; if ( isset( $submenu['woocommerce'] ) ) { // Remove 'WooCommerce' sub menu item. unset( $submenu['woocommerce'][0] ); // Add count if user has access. if ( apply_filters( 'woocommerce_include_processing_order_count_in_menu', true ) && current_user_can( 'edit_others_shop_orders' ) ) { $order_count = apply_filters( 'woocommerce_menu_order_count', wc_processing_order_count() ); if ( $order_count ) { foreach ( $submenu['woocommerce'] as $key => $menu_item ) { if ( 0 === strpos( $menu_item[0], _x( 'Orders', 'Admin menu name', 'woocommerce' ) ) ) { $submenu['woocommerce'][ $key ][0] .= ' '; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited break; } } } } } } /** * Reorder the WC menu items in admin. * * @param int $menu_order Menu order. * @return array */ public function menu_order( $menu_order ) { // Initialize our custom order array. $woocommerce_menu_order = array(); // Get the index of our custom separator. $woocommerce_separator = array_search( 'separator-woocommerce', $menu_order, true ); // Get index of product menu. $woocommerce_product = array_search( 'edit.php?post_type=product', $menu_order, true ); // Loop through menu order and do some rearranging. foreach ( $menu_order as $index => $item ) { if ( 'woocommerce' === $item ) { $woocommerce_menu_order[] = 'separator-woocommerce'; $woocommerce_menu_order[] = $item; $woocommerce_menu_order[] = 'edit.php?post_type=product'; unset( $menu_order[ $woocommerce_separator ] ); unset( $menu_order[ $woocommerce_product ] ); } elseif ( ! in_array( $item, array( 'separator-woocommerce' ), true ) ) { $woocommerce_menu_order[] = $item; } } // Return order. return $woocommerce_menu_order; } /** * Custom menu order. * * @param bool $enabled Whether custom menu ordering is already enabled. * @return bool */ public function custom_menu_order( $enabled ) { return $enabled || self::can_view_woocommerce_menu_item(); } /** * Validate screen options on update. * * @param bool|int $status Screen option value. Default false to skip. * @param string $option The option name. * @param int $value The number of rows to use. * @return bool|int */ public function set_screen_option( $status, $option, $value ) { $screen_options = array( 'woocommerce_keys_per_page', 'woocommerce_webhooks_per_page', FileListTable::PER_PAGE_USER_OPTION_KEY, SearchListTable::PER_PAGE_USER_OPTION_KEY, WC_Admin_Log_Table_List::PER_PAGE_USER_OPTION_KEY, ); if ( in_array( $option, $screen_options, true ) ) { return $value; } return $status; } /** * Init the reports page. * * @return void */ public function reports_page() { WC_Admin_Reports::output(); } /** * Init the settings page. * * @return void */ public function settings_page() { WC_Admin_Settings::output(); } /** * Init the attributes page. * * @return void */ public function attributes_page() { WC_Admin_Attributes::output(); } /** * Init the status page. * * @return void */ public function status_page() { WC_Admin_Status::output(); } /** * Init the addons page. * * @return void */ public function addons_page() { WC_Admin_Addons::handle_legacy_marketplace_redirects(); } /** * Link to the order admin list table from the main WooCommerce menu. * * @return void */ public function orders_menu(): void { if ( wc_get_container()->get( CustomOrdersTableController::class )->custom_orders_table_usage_is_enabled() ) { wc_get_container()->get( Custom_Orders_PageController::class )->setup(); } else { wc_get_container()->get( COTRedirectionController::class )->setup(); } } /** * Add custom nav meta box. * * Adapted from http://www.johnmorrisonline.com/how-to-add-a-fully-functional-custom-meta-box-to-wordpress-navigation-menus/. * * @return void */ public function add_nav_menu_meta_boxes() { add_meta_box( 'woocommerce_endpoints_nav_link', __( 'WooCommerce endpoints', 'woocommerce' ), array( $this, 'nav_menu_links' ), 'nav-menus', 'side', 'low' ); } /** * Ensure the Product Categories, Product Tags, and Brands meta boxes * are visible by default on the Appearance > Menus screen. * * WordPress's wp_initial_nav_menu_meta_boxes() hides every box except page, * post, custom-links, and category on a user's first visit. We intercept * the empty user option and return a hidden list that excludes the WC * taxonomy boxes, so WP's existing-user guard short-circuits and leaves * them visible. * * The computed list is persisted to user meta on that first read, mirroring * core's wp_initial_nav_menu_meta_boxes(). This keeps the snapshot stable * across the multiple reads in a single page load and, crucially, across * later requests: boxes registered by plugins installed after the first * visit stay visible, matching vanilla WordPress instead of defaulting to * hidden on every recompute. As in core, only the current user's snapshot is * persisted, so reads on behalf of another user never overwrite that user's * own first-visit snapshot. * * @since 11.1.0 * * @return void */ public function register_default_nav_menu_meta_boxes_filter() { add_filter( 'get_user_option_metaboxhidden_nav-menus', array( $this, 'filter_default_nav_menu_hidden_meta_boxes' ), 10, 3 ); } /** * Filter the default hidden meta boxes for the Appearance > Menus screen. * * Only applies when the user has no saved preference for the nav-menus * screen. Returns a list of every registered nav-menus meta box except the * four core visible boxes, the "WooCommerce endpoints" box, and the WC * taxonomy boxes. Product Brands is visible only when its taxonomy exists. * * @since 11.1.0 * * @param mixed $result Value for the user's option, or false if not set. * @param string $option Name of the option being retrieved. * @param WP_User $user WP_User object of the user whose option is being retrieved. * @return mixed The filtered option value. */ public function filter_default_nav_menu_hidden_meta_boxes( $result, $option, $user ) { global $wp_meta_boxes; // Once a snapshot exists the option reads non-false, so this early return // also makes the update_user_meta() call below idempotent: a re-entrant read // bails here instead of persisting again. if ( false !== $result ) { return $result; } if ( ! $user || ! isset( $wp_meta_boxes['nav-menus'] ) || ! is_array( $wp_meta_boxes['nav-menus'] ) ) { return $result; } $visible = array( 'add-post-type-page', 'add-post-type-post', 'add-custom-links', 'add-category', 'add-product_cat', 'add-product_tag', 'woocommerce_endpoints_nav_link', ); if ( taxonomy_exists( 'product_brand' ) ) { $visible[] = 'add-product_brand'; } $hidden = array(); foreach ( $wp_meta_boxes['nav-menus'] as $priorities ) { foreach ( (array) $priorities as $priority => $boxes ) { foreach ( (array) $boxes as $box ) { if ( isset( $box['id'] ) && ! in_array( $box['id'], $visible, true ) ) { $hidden[] = $box['id']; } } } } // Persist the snapshot, mirroring core's wp_initial_nav_menu_meta_boxes(), // so later reads in this request and in future requests return this same // list instead of recomputing it against a changed meta box registry. // // Like core, only the current user's snapshot is persisted. This filter // runs for whichever user's option is being read, so a read performed on // behalf of another user (user-switching or admin tooling) still receives // the computed default below for display, but must not consume that user's // durable first-visit snapshot from this request's meta box registry. $current_user_id = get_current_user_id(); if ( $current_user_id && (int) $user->ID === $current_user_id ) { update_user_meta( $current_user_id, 'metaboxhidden_nav-menus', $hidden ); } return $hidden; } /** * Output menu links. * * @return void */ public function nav_menu_links() { // Get items from account menu. $endpoints = wc_get_account_menu_items(); // Remove dashboard item. if ( isset( $endpoints['dashboard'] ) ) { unset( $endpoints['dashboard'] ); } // Include missing lost password endpoint, if set in WooCommerce > Settings > Advanced > Account endpoints. if ( ! empty( get_option( 'woocommerce_myaccount_lost_password_endpoint' ) ) ) { $endpoints['lost-password'] = __( 'Lost password', 'woocommerce' ); } $endpoints = apply_filters( 'woocommerce_custom_nav_menu_items', $endpoints ); ?>