woocommerce/plugins/woocommerce-admin/includes/connect-existing-pages.php

295 lines
7.9 KiB
PHP
Raw Normal View History

<?php
/**
* Connect existing WooCommerce pages to WooCommerce Admin.
*
* @package WooCommerce\Admin
*/
Add Coupon feature (https://github.com/woocommerce/woocommerce-admin/pull/4526) * Handle new object for posts and recommendations * Set breadcrumb parent for woocommerce-coupons to be woocommerce-marketing * Add main coupon wrapping component * Render coupon wrapper element below coupon table using wp-admin-scripts * Finish off implementing category param for data store resolvers * Create a helper trait for legacy coupons * Add coupon related titles and descriptions * Add note for the coupons being moved * Allow for querying by note name in the notes Data Store * Revamp coupon moved trait * Add the new note only if we don't have an unactioned note and perform a redirect to ensure menu updates * set_icon is deprecated * Move coupon menu, adding a note for customers * Translate title and descriptions * Whitespace * Account for coupon functionality being disabled * Hide legacy menu before redirect * Don’t keep adding the note if customer dismisses it from inbox * Move behind feature flag * Add note if feature enabled * Add filter to override coupon feature * Tweak option name to refer to wc_admin To help with finding etc. * use css variables * Add the new note only if we don't have an unactioned note * Switch the filter logic so `false` turns off the feature This is a bit more intuitive when utilizing the filter * Remove extraneous string and add trailing new lines * Use correct posts object in tests * Revert accidental removal of where_types * Add coupons category to RecommendedExtensions * Use 1.1 api to get categorized recommendations * Add missing text domains * Fix menu handling to point to woocommerce-marketing * Only load coupon scripts on the coupon page * Rework marketing menu logic to register pages more properly * Use correct wc-admin path for marketing page * Remove separate feature flag WC Admin has existing feature flags to load enable/disable the feature * Only set the coupon parent to marketing when the feature is enabled * Only load coupon feature if marketing feature is enabled Co-authored-by: Dan Bitzer <danielbitzer@gmail.com> Co-authored-by: Jeremy Pry <jeremy.pry@gmail.com>
2020-06-16 02:30:41 +00:00
use Automattic\WooCommerce\Admin\Loader;
use Automattic\WooCommerce\Admin\PageController;
/**
* Returns core WC pages to connect to WC-Admin.
*
* @return array
*/
function wc_admin_get_core_pages_to_connect() {
$all_reports = WC_Admin_Reports::get_reports();
$report_tabs = array();
foreach ( $all_reports as $report_id => $report_data ) {
$report_tabs[ $report_id ] = $report_data['title'];
}
return array(
'wc-addons' => array(
'title' => __( 'Extensions', 'woocommerce-admin' ),
'tabs' => array(),
),
'wc-reports' => array(
'title' => __( 'Reports', 'woocommerce-admin' ),
'tabs' => $report_tabs,
),
'wc-settings' => array(
'title' => __( 'Settings', 'woocommerce-admin' ),
'tabs' => apply_filters( 'woocommerce_settings_tabs_array', array() ),
),
'wc-status' => array(
'title' => __( 'Status', 'woocommerce-admin' ),
'tabs' => apply_filters(
'woocommerce_admin_status_tabs',
array(
'status' => __( 'System status', 'woocommerce-admin' ),
'tools' => __( 'Tools', 'woocommerce-admin' ),
'logs' => __( 'Logs', 'woocommerce-admin' ),
)
),
),
);
}
/**
* Filter breadcrumbs for core pages that aren't explicitly connected.
*
* @param array $breadcrumbs Breadcrumb pieces.
* @return array Filtered breadcrumb pieces.
*/
function wc_admin_filter_core_page_breadcrumbs( $breadcrumbs ) {
$screen_id = PageController::get_instance()->get_current_screen_id();
$pages_to_connect = wc_admin_get_core_pages_to_connect();
$woocommerce_breadcrumb = array(
'admin.php?page=wc-admin',
__( 'WooCommerce', 'woocommerce-admin' ),
);
foreach ( $pages_to_connect as $page_id => $page_data ) {
if ( preg_match( "/^woocommerce_page_{$page_id}\-/", $screen_id ) ) {
if ( empty( $page_data['tabs'] ) ) {
$new_breadcrumbs = array(
$woocommerce_breadcrumb,
$page_data['title'],
);
} else {
$new_breadcrumbs = array(
$woocommerce_breadcrumb,
array(
add_query_arg( 'page', $page_id, 'admin.php' ),
$page_data['title'],
),
);
if ( isset( $_GET['tab'] ) ) {
$current_tab = wc_clean( wp_unslash( $_GET['tab'] ) );
} else {
$current_tab = key( $page_data['tabs'] );
}
$new_breadcrumbs[] = $page_data['tabs'][ $current_tab ];
}
return $new_breadcrumbs;
}
}
return $breadcrumbs;
}
/**
* Render the WC-Admin header bar on all WooCommerce core pages.
*
* @param bool $is_connected Whether the current page is connected.
* @param bool $current_page The current page, if connected.
* @return bool Whether to connect the page.
*/
function wc_admin_connect_core_pages( $is_connected, $current_page ) {
if ( false === $is_connected && false === $current_page ) {
$screen_id = PageController::get_instance()->get_current_screen_id();
$pages_to_connect = wc_admin_get_core_pages_to_connect();
foreach ( $pages_to_connect as $page_id => $page_data ) {
if ( preg_match( "/^woocommerce_page_{$page_id}\-/", $screen_id ) ) {
add_filter( 'woocommerce_navigation_get_breadcrumbs', 'wc_admin_filter_core_page_breadcrumbs' );
return true;
}
}
}
return $is_connected;
}
add_filter( 'woocommerce_navigation_is_connected_page', 'wc_admin_connect_core_pages', 10, 2 );
$posttype_list_base = 'edit.php';
// WooCommerce > Orders.
wc_admin_connect_page(
array(
'id' => 'woocommerce-orders',
'screen_id' => 'edit-shop_order',
'title' => __( 'Orders', 'woocommerce-admin' ),
'path' => add_query_arg( 'post_type', 'shop_order', $posttype_list_base ),
)
);
// WooCommerce > Orders > Add New.
wc_admin_connect_page(
array(
'id' => 'woocommerce-add-order',
'parent' => 'woocommerce-orders',
'screen_id' => 'shop_order-add',
'title' => __( 'Add New', 'woocommerce-admin' ),
)
);
// WooCommerce > Orders > Edit Order.
wc_admin_connect_page(
array(
'id' => 'woocommerce-edit-order',
'parent' => 'woocommerce-orders',
'screen_id' => 'shop_order',
'title' => __( 'Edit Order', 'woocommerce-admin' ),
)
);
// WooCommerce > Coupons.
wc_admin_connect_page(
array(
'id' => 'woocommerce-coupons',
Add Coupon feature (https://github.com/woocommerce/woocommerce-admin/pull/4526) * Handle new object for posts and recommendations * Set breadcrumb parent for woocommerce-coupons to be woocommerce-marketing * Add main coupon wrapping component * Render coupon wrapper element below coupon table using wp-admin-scripts * Finish off implementing category param for data store resolvers * Create a helper trait for legacy coupons * Add coupon related titles and descriptions * Add note for the coupons being moved * Allow for querying by note name in the notes Data Store * Revamp coupon moved trait * Add the new note only if we don't have an unactioned note and perform a redirect to ensure menu updates * set_icon is deprecated * Move coupon menu, adding a note for customers * Translate title and descriptions * Whitespace * Account for coupon functionality being disabled * Hide legacy menu before redirect * Don’t keep adding the note if customer dismisses it from inbox * Move behind feature flag * Add note if feature enabled * Add filter to override coupon feature * Tweak option name to refer to wc_admin To help with finding etc. * use css variables * Add the new note only if we don't have an unactioned note * Switch the filter logic so `false` turns off the feature This is a bit more intuitive when utilizing the filter * Remove extraneous string and add trailing new lines * Use correct posts object in tests * Revert accidental removal of where_types * Add coupons category to RecommendedExtensions * Use 1.1 api to get categorized recommendations * Add missing text domains * Fix menu handling to point to woocommerce-marketing * Only load coupon scripts on the coupon page * Rework marketing menu logic to register pages more properly * Use correct wc-admin path for marketing page * Remove separate feature flag WC Admin has existing feature flags to load enable/disable the feature * Only set the coupon parent to marketing when the feature is enabled * Only load coupon feature if marketing feature is enabled Co-authored-by: Dan Bitzer <danielbitzer@gmail.com> Co-authored-by: Jeremy Pry <jeremy.pry@gmail.com>
2020-06-16 02:30:41 +00:00
'parent' => Loader::is_feature_enabled( 'coupons' ) ? 'woocommerce-marketing' : null,
'screen_id' => 'edit-shop_coupon',
'title' => __( 'Coupons', 'woocommerce-admin' ),
'path' => add_query_arg( 'post_type', 'shop_coupon', $posttype_list_base ),
)
);
// WooCommerce > Coupons > Add New.
wc_admin_connect_page(
array(
'id' => 'woocommerce-add-coupon',
'parent' => 'woocommerce-coupons',
'screen_id' => 'shop_coupon-add',
'title' => __( 'Add New', 'woocommerce-admin' ),
)
);
// WooCommerce > Coupons > Edit Coupon.
wc_admin_connect_page(
array(
'id' => 'woocommerce-edit-coupon',
'parent' => 'woocommerce-coupons',
'screen_id' => 'shop_coupon',
'title' => __( 'Edit Coupon', 'woocommerce-admin' ),
)
);
// WooCommerce > Products.
wc_admin_connect_page(
array(
'id' => 'woocommerce-products',
'screen_id' => 'edit-product',
'title' => __( 'Products', 'woocommerce-admin' ),
'path' => add_query_arg( 'post_type', 'product', $posttype_list_base ),
)
);
// WooCommerce > Products > Add New.
wc_admin_connect_page(
array(
'id' => 'woocommerce-add-product',
'parent' => 'woocommerce-products',
'screen_id' => 'product-add',
'title' => __( 'Add New', 'woocommerce-admin' ),
)
);
// WooCommerce > Products > Edit Order.
wc_admin_connect_page(
array(
'id' => 'woocommerce-edit-product',
'parent' => 'woocommerce-products',
'screen_id' => 'product',
'title' => __( 'Edit Product', 'woocommerce-admin' ),
)
);
// WooCommerce > Products > Import Products.
wc_admin_connect_page(
array(
'id' => 'woocommerce-import-products',
'parent' => 'woocommerce-products',
'screen_id' => 'product_page_product_importer',
'title' => __( 'Import Products', 'woocommerce-admin' ),
)
);
// WooCommerce > Products > Export Products.
wc_admin_connect_page(
array(
'id' => 'woocommerce-export-products',
'parent' => 'woocommerce-products',
'screen_id' => 'product_page_product_exporter',
'title' => __( 'Export Products', 'woocommerce-admin' ),
)
);
// WooCommerce > Products > Product categories.
wc_admin_connect_page(
array(
'id' => 'woocommerce-product-categories',
'parent' => 'woocommerce-products',
'screen_id' => 'edit-product_cat',
'title' => __( 'Product categories', 'woocommerce-admin' ),
)
);
// WooCommerce > Products > Edit category.
wc_admin_connect_page(
array(
'id' => 'woocommerce-product-edit-category',
'parent' => 'woocommerce-products',
'screen_id' => 'product_cat',
'title' => __( 'Edit category', 'woocommerce-admin' ),
)
);
// WooCommerce > Products > Product tags.
wc_admin_connect_page(
array(
'id' => 'woocommerce-product-tags',
'parent' => 'woocommerce-products',
'screen_id' => 'edit-product_tag',
'title' => __( 'Product tags', 'woocommerce-admin' ),
)
);
// WooCommerce > Products > Edit tag.
wc_admin_connect_page(
array(
'id' => 'woocommerce-product-edit-tag',
'parent' => 'woocommerce-products',
'screen_id' => 'product_tag',
'title' => __( 'Edit tag', 'woocommerce-admin' ),
)
);
// WooCommerce > Products > Attributes.
wc_admin_connect_page(
array(
'id' => 'woocommerce-product-attributes',
'parent' => 'woocommerce-products',
'screen_id' => 'product_page_product_attributes',
'title' => __( 'Attributes', 'woocommerce-admin' ),
)
);
// WooCommerce > Products > Edit attribute.
wc_admin_connect_page(
array(
'id' => 'woocommerce-product-edit-attribute',
'parent' => 'woocommerce-products',
'screen_id' => 'product_page_product_attribute-edit',
'title' => __( 'Edit attribute', 'woocommerce-admin' ),
)
);