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

Create AssetsController and BlockTypesController (replaces Assets.php and Library.php) #4094

Merged
merged 9 commits into from
Apr 26, 2021
95 changes: 95 additions & 0 deletions src/Assets.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php
namespace Automattic\WooCommerce\Blocks;

use Automattic\WooCommerce\Blocks\Package;
use Automattic\WooCommerce\Blocks\Assets\Api as AssetApi;

/**
* Assets class.
*
* @deprecated $VID:$ This class will be removed in a future release. This has been replaced by AssetsController.
* @internal
*/
class Assets {

/**
* Initialize class features on init.
*
* @since 2.5.0
* @deprecated $VID:$
*/
public static function init() {
_deprecated_function( 'Assets::init', '$VID:$' );
}

/**
* Register block scripts & styles.
*
* @since 2.5.0
* @deprecated $VID:$
*/
public static function register_assets() {
_deprecated_function( 'Assets::register_assets', '$VID:$' );
}

/**
* Register the vendors style file. We need to do it after the other files
* because we need to check if `wp-edit-post` has been enqueued.
*
* @deprecated $VID:$
*/
public static function enqueue_scripts() {
_deprecated_function( 'Assets::enqueue_scripts', '$VID:$' );
}

/**
* Add body classes.
*
* @deprecated $VID:$
* @param array $classes Array of CSS classnames.
* @return array Modified array of CSS classnames.
*/
public static function add_theme_body_class( $classes = [] ) {
_deprecated_function( 'Assets::add_theme_body_class', '$VID:$' );
return $classes;
}

/**
* Add theme class to admin body.
*
* @deprecated $VID:$
* @param array $classes String with the CSS classnames.
* @return array Modified string of CSS classnames.
*/
public static function add_theme_admin_body_class( $classes = '' ) {
_deprecated_function( 'Assets::add_theme_admin_body_class', '$VID:$' );
return $classes;
}

/**
* Adds a redirect field to the login form so blocks can redirect users after login.
*
* @deprecated $VID:$
*/
public static function redirect_to_field() {
_deprecated_function( 'Assets::redirect_to_field', '$VID:$' );
}

/**
* Queues a block script in the frontend.
*
* @since 2.3.0
* @since 2.6.0 Changed $name to $script_name and added $handle argument.
* @since 2.9.0 Made it so scripts are not loaded in admin pages.
* @deprecated 4.5.0 Block types register the scripts themselves.
*
* @param string $script_name Name of the script used to identify the file inside build folder.
* @param string $handle Optional. Provided if the handle should be different than the script name. `wc-` prefix automatically added.
* @param array $dependencies Optional. An array of registered script handles this script depends on. Default empty array.
*/
public static function register_block_script( $script_name, $handle = '', $dependencies = [] ) {
_deprecated_function( 'register_block_script', '4.5.0' );
$asset_api = Package::container()->get( AssetApi::class );
$asset_api->register_block_script( $script_name, $handle, $dependencies );
}
}
20 changes: 2 additions & 18 deletions src/AssetsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
/**
* AssetsController class.
*
* @since $VID:$
* @internal
*/
class AssetsController {
final class AssetsController {

/**
* Asset API interface for various asset registration.
Expand All @@ -36,16 +37,12 @@ protected function init() {
add_action( 'init', array( $this, 'register_assets' ) );
add_action( 'body_class', array( $this, 'add_theme_body_class' ), 1 );
add_action( 'admin_body_class', array( $this, 'add_theme_body_class' ), 1 );
add_action( 'woocommerce_login_form_end', array( $this, 'redirect_to_field' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
}

/**
* Register block scripts & styles.
*
* @since 2.5.0
* Moved data related enqueuing to new AssetDataRegistry class as part of ongoing refactoring.
*/
public function register_assets() {
$this->register_style( 'wc-block-editor', plugins_url( $this->api->get_block_asset_build_path( 'editor', 'css' ), __DIR__ ), array( 'wp-edit-blocks' ) );
Expand Down Expand Up @@ -108,17 +105,6 @@ public function add_theme_body_class( $classes ) {
return $classes;
}

/**
* Adds a redirect field to the login form so blocks can redirect users after login.
*/
public function redirect_to_field() {
// phpcs:ignore WordPress.Security.NonceVerification
if ( empty( $_GET['redirect_to'] ) ) {
return;
}
echo '<input type="hidden" name="redirect" value="' . esc_attr( esc_url_raw( wp_unslash( $_GET['redirect_to'] ) ) ) . '" />'; // phpcs:ignore WordPress.Security.NonceVerification
}

/**
* Get the file modified time as a cache buster if we're in dev mode.
*
Expand All @@ -135,8 +121,6 @@ protected function get_file_version( $file ) {
/**
* Registers a style according to `wp_register_style`.
*
* @since 2.0.0
*
* @param string $handle Name of the stylesheet. Should be unique.
* @param string $src Full URL of the stylesheet, or path of the stylesheet relative to the WordPress root directory.
* @param array $deps Optional. An array of registered stylesheet handles this stylesheet depends on. Default empty array.
Expand Down
15 changes: 14 additions & 1 deletion src/BlockTypesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
/**
* BlockTypesController class.
*
* @since $VID:$
* @internal
*/
class BlockTypesController {
final class BlockTypesController {

/**
* Instance of the asset API.
Expand Down Expand Up @@ -45,6 +46,7 @@ public function __construct( AssetApi $asset_api, AssetDataRegistry $asset_data_
*/
protected function init() {
add_action( 'init', array( $this, 'register_blocks' ) );
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note, we don't need to define tables any more now that the stock table is in core.

add_action( 'woocommerce_login_form_end', array( $this, 'redirect_to_field' ) );
}

/**
Expand All @@ -63,6 +65,17 @@ public function register_blocks() {
}
}

/**
* Adds a redirect field to the login form so blocks can redirect users after login.
*/
public function redirect_to_field() {
// phpcs:ignore WordPress.Security.NonceVerification
if ( empty( $_GET['redirect_to'] ) ) {
return;
}
echo '<input type="hidden" name="redirect" value="' . esc_attr( esc_url_raw( wp_unslash( $_GET['redirect_to'] ) ) ) . '" />'; // phpcs:ignore WordPress.Security.NonceVerification
}

/**
* Get list of block types.
*
Expand Down
44 changes: 44 additions & 0 deletions src/Library.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
namespace Automattic\WooCommerce\Blocks;

use Automattic\WooCommerce\Blocks\BlockTypes\AtomicBlock;
use Automattic\WooCommerce\Blocks\Package;
use Automattic\WooCommerce\Blocks\Assets\AssetDataRegistry;
use Automattic\WooCommerce\Blocks\Assets\Api as AssetApi;
use Automattic\WooCommerce\Blocks\Integrations\IntegrationRegistry;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably don't need all these imports now?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ignore this, it was an artifact of the commits I was viewing and how the changes were being shown, I see they don't actually exist in the file.


/**
* Library class.
*
* @deprecated $VID:$ This class will be removed in a future release. This has been replaced by BlockTypesController.
* @internal
*/
class Library {

/**
* Initialize block library features.
*
* @deprecated $VID:$
*/
public static function init() {
_deprecated_function( 'Library::init', '$VID:$' );
}

/**
* Register custom tables within $wpdb object.
*
* @deprecated $VID:$
*/
public static function define_tables() {
_deprecated_function( 'Library::define_tables', '$VID:$' );
}

/**
* Register blocks, hooking up assets and render functions as needed.
*
* @deprecated $VID:$
*/
public static function register_blocks() {
_deprecated_function( 'Library::register_blocks', '$VID:$' );
}
}