Init.php
119 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WooCommerce Product Data Views |
| 4 | */ |
| 5 | |
| 6 | declare( strict_types = 1 ); |
| 7 | |
| 8 | namespace Automattic\WooCommerce\Admin\Features\ProductDataViews; |
| 9 | |
| 10 | /** |
| 11 | * Loads assets related to the product block editor. |
| 12 | */ |
| 13 | class Init { |
| 14 | /** |
| 15 | * Constructor |
| 16 | */ |
| 17 | public function __construct() { |
| 18 | add_action( 'admin_menu', array( $this, 'woocommerce_add_new_products_dashboard' ) ); |
| 19 | add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_styles' ), 20 ); |
| 20 | add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ), 20 ); |
| 21 | |
| 22 | if ( $this->is_product_data_view_page() ) { |
| 23 | add_filter( |
| 24 | 'admin_body_class', |
| 25 | static function ( $classes ) { |
| 26 | return "$classes"; |
| 27 | } |
| 28 | ); |
| 29 | |
| 30 | add_filter( 'admin_footer_text', '__return_empty_string', 20 ); |
| 31 | add_filter( 'update_footer', '__return_empty_string', 20 ); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Returns true if we are on a JS powered admin page. |
| 37 | */ |
| 38 | public static function is_product_data_view_page() { |
| 39 | // phpcs:disable WordPress.Security.NonceVerification |
| 40 | return isset( $_GET['page'] ) && 'woocommerce-products-dashboard' === $_GET['page']; |
| 41 | // phpcs:enable WordPress.Security.NonceVerification |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Enqueue styles needed for the rich text editor. |
| 46 | */ |
| 47 | public function enqueue_styles() { |
| 48 | if ( ! $this->is_product_data_view_page() ) { |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | wp_enqueue_style( 'wc-experimental-products-app' ); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Enqueue scripts needed for the product form block editor. |
| 57 | */ |
| 58 | public function enqueue_scripts() { |
| 59 | if ( ! $this->is_product_data_view_page() ) { |
| 60 | return; |
| 61 | } |
| 62 | wp_enqueue_script( 'wc-experimental-products-app' ); |
| 63 | wp_add_inline_script( 'wc-experimental-products-app', 'window.wc.experimentalProductsApp.initializeProductsDashboard( "woocommerce-products-dashboard" );', 'after' ); |
| 64 | |
| 65 | $script_handle = 'wc-admin-edit-product'; |
| 66 | wp_register_script( $script_handle, '', array( 'wp-blocks' ), '0.1.0', true ); |
| 67 | wp_enqueue_script( $script_handle ); |
| 68 | wp_enqueue_media(); |
| 69 | wp_register_style( 'wc-global-presets', false ); // phpcs:ignore |
| 70 | wp_add_inline_style( 'wc-global-presets', wp_get_global_stylesheet( array( 'presets' ) ) ); |
| 71 | wp_enqueue_style( 'wc-global-presets' ); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Replaces the default posts menu item with the new posts dashboard. |
| 76 | */ |
| 77 | public function woocommerce_add_new_products_dashboard() { |
| 78 | $gutenberg_experiments = get_option( 'gutenberg-experiments' ); |
| 79 | if ( ! $gutenberg_experiments ) { |
| 80 | return; |
| 81 | } |
| 82 | $ptype_obj = get_post_type_object( 'product' ); |
| 83 | add_submenu_page( |
| 84 | 'edit.php?post_type=product', |
| 85 | $ptype_obj->labels->name, |
| 86 | esc_html__( 'All Products ( new )', 'woocommerce' ), |
| 87 | 'manage_woocommerce', |
| 88 | 'woocommerce-products-dashboard', |
| 89 | array( $this, 'woocommerce_products_dashboard' ), |
| 90 | 1 |
| 91 | ); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Renders the new posts dashboard page. |
| 96 | */ |
| 97 | public function woocommerce_products_dashboard() { |
| 98 | if ( function_exists( 'gutenberg_url' ) ) { |
| 99 | // phpcs:disable WordPress.WP.EnqueuedResourceParameters.MissingVersion |
| 100 | wp_register_style( |
| 101 | 'wp-gutenberg-posts-dashboard', |
| 102 | gutenberg_url( 'build/edit-site/posts.css', __FILE__ ), |
| 103 | array( 'wp-components' ), |
| 104 | ); |
| 105 | // phpcs:enable WordPress.WP.EnqueuedResourceParameters.MissingVersion |
| 106 | wp_enqueue_style( 'wp-gutenberg-posts-dashboard' ); |
| 107 | } |
| 108 | |
| 109 | if ( ! wp_script_is( 'wc-experimental-products-app', 'enqueued' ) ) { |
| 110 | printf( |
| 111 | '<div class="notice notice-error"><p>%s</p></div>', |
| 112 | esc_html__( 'The experimental products app assets are not available yet. Rebuild the admin assets and reload this page.', 'woocommerce' ) |
| 113 | ); |
| 114 | } |
| 115 | |
| 116 | echo '<div id="woocommerce-products-dashboard"></div>'; |
| 117 | } |
| 118 | } |
| 119 |