Refactor and remove unused init code

This commit is contained in:
Matt Sherman 2023-11-13 13:32:47 -05:00
parent db22b89d12
commit 9e292485c3
1 changed files with 21 additions and 37 deletions

View File

@ -46,10 +46,7 @@ class Init {
$this->redirection_controller = new RedirectionController( $this->supported_post_types );
if ( \Automattic\WooCommerce\Utilities\FeaturesUtil::feature_is_enabled( 'product_block_editor' ) ) {
// Register the product block template.
$template_registry = wc_get_container()->get( BlockTemplateRegistry::class );
$template_registry->register( new SimpleProductTemplate() );
$template_registry->register( new ProductVariationTemplate() );
$this->register_product_editor_templates();
if ( ! Features::is_enabled( 'new-product-management-experience' ) ) {
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_styles' ) );
@ -58,7 +55,6 @@ class Init {
}
add_filter( 'woocommerce_admin_get_user_data_fields', array( $this, 'add_user_data_fields' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
add_filter( 'woocommerce_register_post_type_product', array( $this, 'add_product_template' ) );
add_filter( 'woocommerce_register_post_type_product_variation', array( $this, 'enable_rest_api_for_product_variation' ) );
add_action( 'current_screen', array( $this, 'set_current_screen_to_block_editor_if_wc_admin' ) );
@ -78,21 +74,19 @@ class Init {
if ( ! PageController::is_admin_or_embed_page() ) {
return;
}
$post_type_object = get_post_type_object( 'product' );
$block_editor_context = new WP_Block_Editor_Context( array( 'name' => self::EDITOR_CONTEXT_NAME ) );
$template_registry = wc_get_container()->get( BlockTemplateRegistry::class );
$template_registry = wc_get_container()->get( BlockTemplateRegistry::class );
$editor_settings = array();
if ( ! empty( $post_type_object->template ) ) {
$editor_settings['template'] = $post_type_object->template;
$editor_settings['templateLock'] = ! empty( $post_type_object->template_lock ) ? $post_type_object->template_lock : false;
$editor_settings['templates'] = array(
'product' => $post_type_object->template,
'product_variation' => $template_registry->get_registered( 'product-variation' )->get_formatted_template(),
);
}
$editor_settings = get_block_editor_settings( $editor_settings, $block_editor_context );
$editor_settings['template'] = $template_registry->get_registered( 'simple-product' )->get_formatted_template();
$editor_settings['templates'] = array(
'product' => $template_registry->get_registered( 'simple-product' )->get_formatted_template(),
'product_variation' => $template_registry->get_registered( 'product-variation' )->get_formatted_template(),
);
$block_editor_context = new WP_Block_Editor_Context( array( 'name' => self::EDITOR_CONTEXT_NAME ) );
$editor_settings = get_block_editor_settings( $editor_settings, $block_editor_context );
$script_handle = 'wc-admin-edit-product';
wp_register_script( $script_handle, '', array(), '0.1.0', true );
@ -161,26 +155,6 @@ class Init {
return $link;
}
/**
* Enqueue styles needed for the rich text editor.
*
* @param array $args Array of post type arguments.
* @return array Array of post type arguments.
*/
public function add_product_template( $args ) {
if ( ! isset( $args['template'] ) ) {
// Get the template from the registry.
$template_registry = wc_get_container()->get( BlockTemplateRegistry::class );
$template = $template_registry->get_registered( 'simple-product' );
if ( isset( $template ) ) {
$args['template_lock'] = 'all';
$args['template'] = $template->get_formatted_template();
}
}
return $args;
}
/**
* Enables variation post type in REST API.
*
@ -230,4 +204,14 @@ class Init {
);
}
}
/**
* Register product editor templates.
*/
private function register_product_editor_templates() {
$template_registry = wc_get_container()->get( BlockTemplateRegistry::class );
$template_registry->register( new SimpleProductTemplate() );
$template_registry->register( new ProductVariationTemplate() );
}
}