emails
5 years ago
connect-existing-pages.php
5 years ago
core-functions.php
5 years ago
feature-config.php
5 years ago
page-controller-functions.php
5 years ago
wc-admin-update-functions.php
5 years ago
connect-existing-pages.php
295 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Connect existing WooCommerce pages to WooCommerce Admin. |
| 4 | * |
| 5 | * @package WooCommerce\Admin |
| 6 | */ |
| 7 | |
| 8 | use Automattic\WooCommerce\Admin\Loader; |
| 9 | use Automattic\WooCommerce\Admin\PageController; |
| 10 | |
| 11 | /** |
| 12 | * Returns core WC pages to connect to WC-Admin. |
| 13 | * |
| 14 | * @return array |
| 15 | */ |
| 16 | function wc_admin_get_core_pages_to_connect() { |
| 17 | $all_reports = WC_Admin_Reports::get_reports(); |
| 18 | $report_tabs = array(); |
| 19 | |
| 20 | foreach ( $all_reports as $report_id => $report_data ) { |
| 21 | $report_tabs[ $report_id ] = $report_data['title']; |
| 22 | } |
| 23 | |
| 24 | return array( |
| 25 | 'wc-addons' => array( |
| 26 | 'title' => __( 'Extensions', 'woocommerce' ), |
| 27 | 'tabs' => array(), |
| 28 | ), |
| 29 | 'wc-reports' => array( |
| 30 | 'title' => __( 'Reports', 'woocommerce' ), |
| 31 | 'tabs' => $report_tabs, |
| 32 | ), |
| 33 | 'wc-settings' => array( |
| 34 | 'title' => __( 'Settings', 'woocommerce' ), |
| 35 | 'tabs' => apply_filters( 'woocommerce_settings_tabs_array', array() ), |
| 36 | ), |
| 37 | 'wc-status' => array( |
| 38 | 'title' => __( 'Status', 'woocommerce' ), |
| 39 | 'tabs' => apply_filters( |
| 40 | 'woocommerce_admin_status_tabs', |
| 41 | array( |
| 42 | 'status' => __( 'System status', 'woocommerce' ), |
| 43 | 'tools' => __( 'Tools', 'woocommerce' ), |
| 44 | 'logs' => __( 'Logs', 'woocommerce' ), |
| 45 | ) |
| 46 | ), |
| 47 | ), |
| 48 | ); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Filter breadcrumbs for core pages that aren't explicitly connected. |
| 53 | * |
| 54 | * @param array $breadcrumbs Breadcrumb pieces. |
| 55 | * @return array Filtered breadcrumb pieces. |
| 56 | */ |
| 57 | function wc_admin_filter_core_page_breadcrumbs( $breadcrumbs ) { |
| 58 | $screen_id = PageController::get_instance()->get_current_screen_id(); |
| 59 | $pages_to_connect = wc_admin_get_core_pages_to_connect(); |
| 60 | $woocommerce_breadcrumb = array( |
| 61 | 'admin.php?page=wc-admin', |
| 62 | __( 'WooCommerce', 'woocommerce' ), |
| 63 | ); |
| 64 | |
| 65 | foreach ( $pages_to_connect as $page_id => $page_data ) { |
| 66 | if ( preg_match( "/^woocommerce_page_{$page_id}\-/", $screen_id ) ) { |
| 67 | if ( empty( $page_data['tabs'] ) ) { |
| 68 | $new_breadcrumbs = array( |
| 69 | $woocommerce_breadcrumb, |
| 70 | $page_data['title'], |
| 71 | ); |
| 72 | } else { |
| 73 | $new_breadcrumbs = array( |
| 74 | $woocommerce_breadcrumb, |
| 75 | array( |
| 76 | add_query_arg( 'page', $page_id, 'admin.php' ), |
| 77 | $page_data['title'], |
| 78 | ), |
| 79 | ); |
| 80 | |
| 81 | if ( isset( $_GET['tab'] ) ) { |
| 82 | $current_tab = wc_clean( wp_unslash( $_GET['tab'] ) ); |
| 83 | } else { |
| 84 | $current_tab = key( $page_data['tabs'] ); |
| 85 | } |
| 86 | |
| 87 | $new_breadcrumbs[] = $page_data['tabs'][ $current_tab ]; |
| 88 | } |
| 89 | |
| 90 | return $new_breadcrumbs; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | return $breadcrumbs; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Render the WC-Admin header bar on all WooCommerce core pages. |
| 99 | * |
| 100 | * @param bool $is_connected Whether the current page is connected. |
| 101 | * @param bool $current_page The current page, if connected. |
| 102 | * @return bool Whether to connect the page. |
| 103 | */ |
| 104 | function wc_admin_connect_core_pages( $is_connected, $current_page ) { |
| 105 | if ( false === $is_connected && false === $current_page ) { |
| 106 | $screen_id = PageController::get_instance()->get_current_screen_id(); |
| 107 | $pages_to_connect = wc_admin_get_core_pages_to_connect(); |
| 108 | |
| 109 | foreach ( $pages_to_connect as $page_id => $page_data ) { |
| 110 | if ( preg_match( "/^woocommerce_page_{$page_id}\-/", $screen_id ) ) { |
| 111 | add_filter( 'woocommerce_navigation_get_breadcrumbs', 'wc_admin_filter_core_page_breadcrumbs' ); |
| 112 | |
| 113 | return true; |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | return $is_connected; |
| 119 | } |
| 120 | |
| 121 | add_filter( 'woocommerce_navigation_is_connected_page', 'wc_admin_connect_core_pages', 10, 2 ); |
| 122 | |
| 123 | $posttype_list_base = 'edit.php'; |
| 124 | |
| 125 | // WooCommerce > Orders. |
| 126 | wc_admin_connect_page( |
| 127 | array( |
| 128 | 'id' => 'woocommerce-orders', |
| 129 | 'screen_id' => 'edit-shop_order', |
| 130 | 'title' => __( 'Orders', 'woocommerce' ), |
| 131 | 'path' => add_query_arg( 'post_type', 'shop_order', $posttype_list_base ), |
| 132 | ) |
| 133 | ); |
| 134 | |
| 135 | // WooCommerce > Orders > Add New. |
| 136 | wc_admin_connect_page( |
| 137 | array( |
| 138 | 'id' => 'woocommerce-add-order', |
| 139 | 'parent' => 'woocommerce-orders', |
| 140 | 'screen_id' => 'shop_order-add', |
| 141 | 'title' => __( 'Add New', 'woocommerce' ), |
| 142 | ) |
| 143 | ); |
| 144 | |
| 145 | // WooCommerce > Orders > Edit Order. |
| 146 | wc_admin_connect_page( |
| 147 | array( |
| 148 | 'id' => 'woocommerce-edit-order', |
| 149 | 'parent' => 'woocommerce-orders', |
| 150 | 'screen_id' => 'shop_order', |
| 151 | 'title' => __( 'Edit Order', 'woocommerce' ), |
| 152 | ) |
| 153 | ); |
| 154 | |
| 155 | // WooCommerce > Coupons. |
| 156 | wc_admin_connect_page( |
| 157 | array( |
| 158 | 'id' => 'woocommerce-coupons', |
| 159 | 'parent' => Loader::is_feature_enabled( 'coupons' ) ? 'woocommerce-marketing' : null, |
| 160 | 'screen_id' => 'edit-shop_coupon', |
| 161 | 'title' => __( 'Coupons', 'woocommerce' ), |
| 162 | 'path' => add_query_arg( 'post_type', 'shop_coupon', $posttype_list_base ), |
| 163 | ) |
| 164 | ); |
| 165 | |
| 166 | // WooCommerce > Coupons > Add New. |
| 167 | wc_admin_connect_page( |
| 168 | array( |
| 169 | 'id' => 'woocommerce-add-coupon', |
| 170 | 'parent' => 'woocommerce-coupons', |
| 171 | 'screen_id' => 'shop_coupon-add', |
| 172 | 'title' => __( 'Add New', 'woocommerce' ), |
| 173 | ) |
| 174 | ); |
| 175 | |
| 176 | // WooCommerce > Coupons > Edit Coupon. |
| 177 | wc_admin_connect_page( |
| 178 | array( |
| 179 | 'id' => 'woocommerce-edit-coupon', |
| 180 | 'parent' => 'woocommerce-coupons', |
| 181 | 'screen_id' => 'shop_coupon', |
| 182 | 'title' => __( 'Edit Coupon', 'woocommerce' ), |
| 183 | ) |
| 184 | ); |
| 185 | |
| 186 | // WooCommerce > Products. |
| 187 | wc_admin_connect_page( |
| 188 | array( |
| 189 | 'id' => 'woocommerce-products', |
| 190 | 'screen_id' => 'edit-product', |
| 191 | 'title' => __( 'Products', 'woocommerce' ), |
| 192 | 'path' => add_query_arg( 'post_type', 'product', $posttype_list_base ), |
| 193 | ) |
| 194 | ); |
| 195 | |
| 196 | // WooCommerce > Products > Add New. |
| 197 | wc_admin_connect_page( |
| 198 | array( |
| 199 | 'id' => 'woocommerce-add-product', |
| 200 | 'parent' => 'woocommerce-products', |
| 201 | 'screen_id' => 'product-add', |
| 202 | 'title' => __( 'Add New', 'woocommerce' ), |
| 203 | ) |
| 204 | ); |
| 205 | |
| 206 | // WooCommerce > Products > Edit Order. |
| 207 | wc_admin_connect_page( |
| 208 | array( |
| 209 | 'id' => 'woocommerce-edit-product', |
| 210 | 'parent' => 'woocommerce-products', |
| 211 | 'screen_id' => 'product', |
| 212 | 'title' => __( 'Edit Product', 'woocommerce' ), |
| 213 | ) |
| 214 | ); |
| 215 | |
| 216 | // WooCommerce > Products > Import Products. |
| 217 | wc_admin_connect_page( |
| 218 | array( |
| 219 | 'id' => 'woocommerce-import-products', |
| 220 | 'parent' => 'woocommerce-products', |
| 221 | 'screen_id' => 'product_page_product_importer', |
| 222 | 'title' => __( 'Import Products', 'woocommerce' ), |
| 223 | ) |
| 224 | ); |
| 225 | |
| 226 | // WooCommerce > Products > Export Products. |
| 227 | wc_admin_connect_page( |
| 228 | array( |
| 229 | 'id' => 'woocommerce-export-products', |
| 230 | 'parent' => 'woocommerce-products', |
| 231 | 'screen_id' => 'product_page_product_exporter', |
| 232 | 'title' => __( 'Export Products', 'woocommerce' ), |
| 233 | ) |
| 234 | ); |
| 235 | |
| 236 | // WooCommerce > Products > Product categories. |
| 237 | wc_admin_connect_page( |
| 238 | array( |
| 239 | 'id' => 'woocommerce-product-categories', |
| 240 | 'parent' => 'woocommerce-products', |
| 241 | 'screen_id' => 'edit-product_cat', |
| 242 | 'title' => __( 'Product categories', 'woocommerce' ), |
| 243 | ) |
| 244 | ); |
| 245 | |
| 246 | // WooCommerce > Products > Edit category. |
| 247 | wc_admin_connect_page( |
| 248 | array( |
| 249 | 'id' => 'woocommerce-product-edit-category', |
| 250 | 'parent' => 'woocommerce-products', |
| 251 | 'screen_id' => 'product_cat', |
| 252 | 'title' => __( 'Edit category', 'woocommerce' ), |
| 253 | ) |
| 254 | ); |
| 255 | |
| 256 | // WooCommerce > Products > Product tags. |
| 257 | wc_admin_connect_page( |
| 258 | array( |
| 259 | 'id' => 'woocommerce-product-tags', |
| 260 | 'parent' => 'woocommerce-products', |
| 261 | 'screen_id' => 'edit-product_tag', |
| 262 | 'title' => __( 'Product tags', 'woocommerce' ), |
| 263 | ) |
| 264 | ); |
| 265 | |
| 266 | // WooCommerce > Products > Edit tag. |
| 267 | wc_admin_connect_page( |
| 268 | array( |
| 269 | 'id' => 'woocommerce-product-edit-tag', |
| 270 | 'parent' => 'woocommerce-products', |
| 271 | 'screen_id' => 'product_tag', |
| 272 | 'title' => __( 'Edit tag', 'woocommerce' ), |
| 273 | ) |
| 274 | ); |
| 275 | |
| 276 | // WooCommerce > Products > Attributes. |
| 277 | wc_admin_connect_page( |
| 278 | array( |
| 279 | 'id' => 'woocommerce-product-attributes', |
| 280 | 'parent' => 'woocommerce-products', |
| 281 | 'screen_id' => 'product_page_product_attributes', |
| 282 | 'title' => __( 'Attributes', 'woocommerce' ), |
| 283 | ) |
| 284 | ); |
| 285 | |
| 286 | // WooCommerce > Products > Edit attribute. |
| 287 | wc_admin_connect_page( |
| 288 | array( |
| 289 | 'id' => 'woocommerce-product-edit-attribute', |
| 290 | 'parent' => 'woocommerce-products', |
| 291 | 'screen_id' => 'product_page_product_attribute-edit', |
| 292 | 'title' => __( 'Edit attribute', 'woocommerce' ), |
| 293 | ) |
| 294 | ); |
| 295 |