Nessuna descrizione

connect-existing-pages.php 8.2KB

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