1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
<?php /** * WooCommerce Product Data Views */
declare( strict_types = 1 );
namespace Automattic\WooCommerce\Admin\Features\ProductDataViews;
use Automattic\Jetpack\Constants; use Automattic\WooCommerce\Blocks\Utils\Utils; use Automattic\WooCommerce\Internal\Admin\WCAdminAssets;
/** * Loads assets related to the product block editor. */ class Init { /** * Constructor */ public function __construct() { if ( $this->has_data_views_support() ) { add_action( 'admin_menu', array( $this, 'woocommerce_add_new_products_dashboard' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_styles' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
if ( $this->is_product_data_view_page() ) { add_filter( 'admin_body_class', static function ( $classes ) { return "$classes"; } ); } } }
/** * Returns true if we are on a JS powered admin page. */ private static function is_product_data_view_page() { // phpcs:disable WordPress.Security.NonceVerification return isset( $_GET['page'] ) && 'woocommerce-products-dashboard' === $_GET['page']; // phpcs:enable WordPress.Security.NonceVerification }
/** * Checks for data views support. */ private function has_data_views_support() { if ( Utils::wp_version_compare( '6.6', '>=' ) ) { return true; }
if ( is_plugin_active( 'gutenberg/gutenberg.php' ) ) { $gutenberg_version = '';
if ( defined( 'GUTENBERG_VERSION' ) ) { $gutenberg_version = GUTENBERG_VERSION; }
if ( ! $gutenberg_version ) { $gutenberg_data = get_file_data( WP_PLUGIN_DIR . '/gutenberg/gutenberg.php', array( 'Version' => 'Version' ) ); $gutenberg_version = $gutenberg_data['Version']; } return version_compare( $gutenberg_version, '19.0', '>=' ); }
return false; }
/** * Enqueue styles needed for the rich text editor. */ public function enqueue_styles() { if ( ! $this->is_product_data_view_page() ) { return; } wp_enqueue_style( 'wc-product-editor' ); }
/** * Enqueue scripts needed for the product form block editor. */ public function enqueue_scripts() { if ( ! $this->is_product_data_view_page() ) { return; }
$script_handle = 'wc-admin-edit-product'; wp_register_script( $script_handle, '', array( 'wp-blocks' ), '0.1.0', true ); wp_enqueue_script( $script_handle ); wp_enqueue_media(); wp_register_style( 'wc-global-presets', false ); // phpcs:ignore wp_add_inline_style( 'wc-global-presets', wp_get_global_stylesheet( array( 'presets' ) ) ); wp_enqueue_style( 'wc-global-presets' ); }
/** * Replaces the default posts menu item with the new posts dashboard. */ public function woocommerce_add_new_products_dashboard() { $gutenberg_experiments = get_option( 'gutenberg-experiments' ); if ( ! $gutenberg_experiments ) { return; } $ptype_obj = get_post_type_object( 'product' ); add_submenu_page( 'edit.php?post_type=product', $ptype_obj->labels->name, esc_html__( 'All Products ( new )', 'woocommerce' ), 'manage_woocommerce', 'woocommerce-products-dashboard', array( $this, 'woocommerce_products_dashboard' ), 1 ); }
/** * Renders the new posts dashboard page. */ public function woocommerce_products_dashboard() { $suffix = Constants::is_true( 'SCRIPT_DEBUG' ) ? '' : '.min'; $version = Constants::get_constant( 'WC_VERSION' ); if ( function_exists( 'gutenberg_url' ) ) { // phpcs:disable WordPress.WP.EnqueuedResourceParameters.MissingVersion wp_register_style( 'wp-gutenberg-posts-dashboard', gutenberg_url( 'build/edit-site/posts.css', __FILE__ ), array( 'wp-components' ), ); // phpcs:enable WordPress.WP.EnqueuedResourceParameters.MissingVersion wp_enqueue_style( 'wp-gutenberg-posts-dashboard' ); } WCAdminAssets::get_instance(); wp_enqueue_script( 'wc-admin-product-editor', WC()->plugin_url() . '/assets/js/admin/product-editor' . $suffix . '.js', array( 'wc-product-editor' ), $version, false ); wp_add_inline_script( 'wp-edit-site', 'window.wc.productEditor.initializeProductsDashboard( "woocommerce-products-dashboard" );', 'after' ); wp_enqueue_script( 'wp-edit-site' );
echo '<div id="woocommerce-products-dashboard"></div>'; } }
|