Skip to content
This repository was archived by the owner on Feb 23, 2024. It is now read-only.

Commit e188d62

Browse files
authored
Set up plugin to be published on npm (#456)
* Move block rendering class into assets * Update package.json for npm * Move script registration into publish-able file * Fix phpcs issue * Add note about source of file * Add wcSettings back in * Rename files to match class name * Add languages & includes back, so we can use this for publishing zip files * Update documentation, versions, and add hook for wc components settings * Update wcSettings output to use fitler * Update version to `alpha` * Make block library class into a singleton
1 parent 76627c7 commit e188d62

File tree

5 files changed

+340
-364
lines changed

5 files changed

+340
-364
lines changed

includes/blocks/class-wc-block-featured-product.php renamed to assets/php/class-wgpb-block-featured-product.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
<?php
22
/**
33
* Display the featured product block in the content.
4+
* NOTE: DO NOT edit this file in WooCommerce core, this is generated from woocommerce-gutenberg-products-block.
45
*
56
* @package WooCommerce\Blocks
7+
* @version 1.3.0
68
*/
79

810
if ( ! defined( 'ABSPATH' ) ) {
@@ -12,7 +14,7 @@
1214
/**
1315
* Wrapper class for Featured Product callback.
1416
*/
15-
class WC_Block_Featured_Product {
17+
class WGPB_Block_Featured_Product {
1618
/**
1719
* Block name.
1820
*
Lines changed: 290 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,290 @@
1+
<?php
2+
/**
3+
* Register the scripts, styles, and blocks needed for the block editor.
4+
* NOTE: DO NOT edit this file in WooCommerce core, this is generated from woocommerce-gutenberg-products-block.
5+
*
6+
* @package WooCommerce\Blocks
7+
* @version 2.0.0
8+
*/
9+
10+
if ( ! defined( 'ABSPATH' ) ) {
11+
exit;
12+
}
13+
14+
/**
15+
* WGPB_Block_Library Class.
16+
*/
17+
class WGPB_Block_Library {
18+
19+
/**
20+
* Class instance.
21+
*
22+
* @var WGPB_Block_Library instance
23+
*/
24+
protected static $instance = null;
25+
26+
/**
27+
* Get class instance
28+
*/
29+
public static function get_instance() {
30+
if ( ! self::$instance ) {
31+
self::$instance = new self();
32+
}
33+
return self::$instance;
34+
}
35+
36+
/**
37+
* Constructor.
38+
*/
39+
public function __construct() {
40+
if ( function_exists( 'register_block_type' ) ) {
41+
add_action( 'init', array( 'WGPB_Block_Library', 'register_blocks' ) );
42+
add_action( 'init', array( 'WGPB_Block_Library', 'register_assets' ) );
43+
add_filter( 'block_categories', array( 'WGPB_Block_Library', 'add_block_category' ) );
44+
add_action( 'admin_print_footer_scripts', array( 'WGPB_Block_Library', 'print_script_settings' ), 1 );
45+
}
46+
}
47+
48+
/**
49+
* Get the file modified time as a cache buster if we're in dev mode.
50+
*
51+
* @param string $file Local path to the file.
52+
* @return string The cache buster value to use for the given file.
53+
*/
54+
protected static function get_file_version( $file ) {
55+
if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) {
56+
$file = trim( $file, '/' );
57+
return filemtime( WGPB_ABSPATH . $file );
58+
}
59+
return WGPB_VERSION;
60+
}
61+
62+
/**
63+
* Registers a script according to `wp_register_script`, additionally loading the translations for the file.
64+
*
65+
* @since 2.0.0
66+
*
67+
* @param string $handle Name of the script. Should be unique.
68+
* @param string $src Full URL of the script, or path of the script relative to the WordPress root directory.
69+
* @param array $deps Optional. An array of registered script handles this script depends on. Default empty array.
70+
* @param bool $has_i18n Optional. Whether to add a script translation call to this file. Default 'true'.
71+
*/
72+
protected static function register_script( $handle, $src, $deps = array(), $has_i18n = true ) {
73+
$filename = str_replace( plugins_url( '/', WGPB_PLUGIN_FILE ), '', $src );
74+
$ver = self::get_file_version( $filename );
75+
wp_register_script( $handle, $src, $deps, $ver, true );
76+
if ( $has_i18n && function_exists( 'wp_set_script_translations' ) ) {
77+
wp_set_script_translations( $handle, 'woo-gutenberg-products-block', WGPB_ABSPATH . 'languages' );
78+
}
79+
}
80+
81+
/**
82+
* Registers a style according to `wp_register_style`.
83+
*
84+
* @since 2.0.0
85+
*
86+
* @param string $handle Name of the stylesheet. Should be unique.
87+
* @param string $src Full URL of the stylesheet, or path of the stylesheet relative to the WordPress root directory.
88+
* @param array $deps Optional. An array of registered stylesheet handles this stylesheet depends on. Default empty array.
89+
* @param string $media Optional. The media for which this stylesheet has been defined. Default 'all'. Accepts media types like
90+
* 'all', 'print' and 'screen', or media queries like '(orientation: portrait)' and '(max-width: 640px)'.
91+
*/
92+
protected static function register_style( $handle, $src, $deps = array(), $media = 'all' ) {
93+
$filename = str_replace( plugins_url( '/', WGPB_PLUGIN_FILE ), '', $src );
94+
$ver = self::get_file_version( $filename );
95+
wp_register_style( $handle, $src, $deps, $ver, $media );
96+
}
97+
98+
/**
99+
* Register block scripts & styles.
100+
*
101+
* @since 2.0.0
102+
*/
103+
public static function register_assets() {
104+
self::register_style( 'wc-block-editor', plugins_url( 'build/editor.css', WGPB_PLUGIN_FILE ), array( 'wp-edit-blocks' ) );
105+
self::register_style( 'wc-block-style', plugins_url( 'build/style.css', WGPB_PLUGIN_FILE ), array() );
106+
107+
// Shared libraries and components across all blocks.
108+
self::register_script( 'wc-vendors', plugins_url( 'build/vendors.js', WGPB_PLUGIN_FILE ), array(), false );
109+
110+
$block_dependencies = array(
111+
'wp-api-fetch',
112+
'wp-blocks',
113+
'wp-components',
114+
'wp-compose',
115+
'wp-data',
116+
'wp-element',
117+
'wp-editor',
118+
'wp-i18n',
119+
'wp-url',
120+
'lodash',
121+
'wc-vendors',
122+
);
123+
124+
self::register_script( 'wc-handpicked-products', plugins_url( 'build/handpicked-products.js', WGPB_PLUGIN_FILE ), $block_dependencies );
125+
self::register_script( 'wc-product-best-sellers', plugins_url( 'build/product-best-sellers.js', WGPB_PLUGIN_FILE ), $block_dependencies );
126+
self::register_script( 'wc-product-category', plugins_url( 'build/product-category.js', WGPB_PLUGIN_FILE ), $block_dependencies );
127+
self::register_script( 'wc-product-new', plugins_url( 'build/product-new.js', WGPB_PLUGIN_FILE ), $block_dependencies );
128+
self::register_script( 'wc-product-on-sale', plugins_url( 'build/product-on-sale.js', WGPB_PLUGIN_FILE ), $block_dependencies );
129+
self::register_script( 'wc-product-top-rated', plugins_url( 'build/product-top-rated.js', WGPB_PLUGIN_FILE ), $block_dependencies );
130+
self::register_script( 'wc-products-attribute', plugins_url( 'build/products-attribute.js', WGPB_PLUGIN_FILE ), $block_dependencies );
131+
self::register_script( 'wc-featured-product', plugins_url( 'build/featured-product.js', WGPB_PLUGIN_FILE ), $block_dependencies );
132+
}
133+
134+
/**
135+
* Register blocks, hooking up assets and render functions as needed.
136+
*
137+
* @since 2.0.0
138+
*/
139+
public static function register_blocks() {
140+
require_once dirname( __FILE__ ) . '/class-wgpb-block-featured-product.php';
141+
142+
register_block_type(
143+
'woocommerce/handpicked-products',
144+
array(
145+
'editor_script' => 'wc-handpicked-products',
146+
'editor_style' => 'wc-block-editor',
147+
'style' => 'wc-block-style',
148+
)
149+
);
150+
register_block_type(
151+
'woocommerce/product-best-sellers',
152+
array(
153+
'editor_script' => 'wc-product-best-sellers',
154+
'editor_style' => 'wc-block-editor',
155+
'style' => 'wc-block-style',
156+
)
157+
);
158+
register_block_type(
159+
'woocommerce/product-category',
160+
array(
161+
'editor_script' => 'wc-product-category',
162+
'editor_style' => 'wc-block-editor',
163+
'style' => 'wc-block-style',
164+
)
165+
);
166+
register_block_type(
167+
'woocommerce/product-new',
168+
array(
169+
'editor_script' => 'wc-product-new',
170+
'editor_style' => 'wc-block-editor',
171+
'style' => 'wc-block-style',
172+
)
173+
);
174+
register_block_type(
175+
'woocommerce/product-on-sale',
176+
array(
177+
'editor_script' => 'wc-product-on-sale',
178+
'editor_style' => 'wc-block-editor',
179+
'style' => 'wc-block-style',
180+
)
181+
);
182+
register_block_type(
183+
'woocommerce/product-top-rated',
184+
array(
185+
'editor_script' => 'wc-product-top-rated',
186+
'editor_style' => 'wc-block-editor',
187+
'style' => 'wc-block-style',
188+
)
189+
);
190+
register_block_type(
191+
'woocommerce/products-by-attribute',
192+
array(
193+
'editor_script' => 'wc-products-attribute',
194+
'editor_style' => 'wc-block-editor',
195+
'style' => 'wc-block-style',
196+
)
197+
);
198+
register_block_type(
199+
'woocommerce/featured-product',
200+
array(
201+
'render_callback' => array( 'WGPB_Block_Featured_Product', 'render' ),
202+
'editor_script' => 'wc-featured-product',
203+
'editor_style' => 'wc-block-editor',
204+
'style' => 'wc-block-style',
205+
)
206+
);
207+
}
208+
209+
/**
210+
* Adds a WooCommerce category to the block inserter.
211+
*
212+
* @since 2.0.0
213+
*
214+
* @param array $categories Array of categories.
215+
* @return array Array of block categories.
216+
*/
217+
public static function add_block_category( $categories ) {
218+
return array_merge(
219+
$categories,
220+
array(
221+
array(
222+
'slug' => 'woocommerce',
223+
'title' => __( 'WooCommerce', 'woo-gutenberg-products-block' ),
224+
'icon' => 'woocommerce',
225+
),
226+
)
227+
);
228+
}
229+
230+
/**
231+
* Output useful globals before printing any script tags.
232+
*
233+
* These are used by @woocommerce/components & the block library to set up defaults
234+
* based on user-controlled settings from WordPress.
235+
*
236+
* @since 2.0.0
237+
*/
238+
public static function print_script_settings() {
239+
global $wp_locale;
240+
$code = get_woocommerce_currency();
241+
// NOTE: wcSettings is not used directly, it's only for @woocommerce/components
242+
//
243+
// Settings and variables can be passed here for access in the app.
244+
// Will need `wcAdminAssetUrl` if the ImageAsset component is used.
245+
// Will need `dataEndpoints.countries` if Search component is used with 'country' type.
246+
// Will need `orderStatuses` if the OrderStatus component is used.
247+
// Deliberately excluding: `embedBreadcrumbs`, `trackingEnabled`.
248+
$settings = array(
249+
'adminUrl' => admin_url(),
250+
'wcAssetUrl' => plugins_url( 'assets/', WC_PLUGIN_FILE ),
251+
'siteLocale' => esc_attr( get_bloginfo( 'language' ) ),
252+
'currency' => array(
253+
'code' => $code,
254+
'precision' => wc_get_price_decimals(),
255+
'symbol' => get_woocommerce_currency_symbol( $code ),
256+
'position' => get_option( 'woocommerce_currency_pos' ),
257+
),
258+
'stockStatuses' => wc_get_product_stock_status_options(),
259+
'siteTitle' => get_bloginfo( 'name' ),
260+
'dataEndpoints' => array(),
261+
'l10n' => array(
262+
'userLocale' => get_user_locale(),
263+
'weekdaysShort' => array_values( $wp_locale->weekday_abbrev ),
264+
),
265+
);
266+
// NOTE: wcSettings is not used directly, it's only for @woocommerce/components.
267+
$settings = apply_filters( 'woocommerce_components_settings', $settings );
268+
269+
// Global settings used in each block.
270+
$block_settings = array(
271+
'min_columns' => wc_get_theme_support( 'product_grid::min_columns', 1 ),
272+
'max_columns' => wc_get_theme_support( 'product_grid::max_columns', 6 ),
273+
'default_columns' => wc_get_default_products_per_row(),
274+
'min_rows' => wc_get_theme_support( 'product_grid::min_rows', 1 ),
275+
'max_rows' => wc_get_theme_support( 'product_grid::max_rows', 6 ),
276+
'default_rows' => wc_get_default_product_rows_per_page(),
277+
'placeholderImgSrc' => wc_placeholder_img_src(),
278+
'min_height' => wc_get_theme_support( 'featured_block::min_height', 500 ),
279+
'default_height' => wc_get_theme_support( 'featured_block::default_height', 500 ),
280+
);
281+
?>
282+
<script type="text/javascript">
283+
var wcSettings = wcSettings || JSON.parse( decodeURIComponent( '<?php echo rawurlencode( wp_json_encode( $settings ) ); ?>' ) );
284+
var wc_product_block_data = JSON.parse( decodeURIComponent( '<?php echo rawurlencode( wp_json_encode( $block_settings ) ); ?>' ) );
285+
</script>
286+
<?php
287+
}
288+
}
289+
290+
WGPB_Block_Library::get_instance();

0 commit comments

Comments
 (0)