, $block_editor_context ); } /** * Get default product templates. * * @return array The default templates. */ private function get_default_product_templates() { $templates = array(); $templates[] = new ProductTemplate( array( 'id' => 'standard-product-template', 'title' => __( 'Standard product', 'woocommerce' ), 'description' => __( 'A single physical or virtual product, e.g. a t-shirt or an eBook.', 'woocommerce' ), 'order' => 10, 'icon' => 'shipping', 'layout_template_id' => 'simple-product', 'product_data' => array( 'type' => 'simple', ), ) ); $templates[] = new ProductTemplate( array( 'id' => 'grouped-product-template', 'title' => __( 'Grouped product', 'woocommerce' ), 'description' => __( 'A set of products that go well together, e.g. camera kit.', 'woocommerce' ), 'order' => 20, 'icon' => 'group', 'layout_template_id' => 'simple-product', 'product_data' => array( 'type' => 'grouped', ), ) ); $templates[] = new ProductTemplate( array( 'id' => 'affiliate-product-template', 'title' => __( 'Affiliate product', 'woocommerce' ), 'description' => __( 'A link to a product sold on a different website, e.g. brand collab.', 'woocommerce' ), 'order' => 30, 'icon' => 'link', 'layout_template_id' => 'simple-product', 'product_data' => array( 'type' => 'external', ), ) ); return $templates; } /** * Create default product template by custom product type if it does not have a * template associated yet. * * @param array $templates The registered product templates. * @return array The new templates. */ private function create_default_product_template_by_custom_product_type( array $templates ) { // Getting the product types registered via the classic editor. $registered_product_types = wc_get_product_types(); $custom_product_types = array_filter( $registered_product_types, function ( $product_type ) { return ! in_array( $product_type, $this->supported_product_types, true ); }, ARRAY_FILTER_USE_KEY ); $templates_with_product_type = array_filter( $templates, function ( $template ) { $product_data = $template->get_product_data(); return ! is_null( $product_data ) && array_key_exists( 'type', $product_data ); } ); $custom_product_types_on_templates = array_map( function ( $template ) { $product_data = $template->get_product_data(); return $product_data['type']; }, $templates_with_product_type ); foreach ( $custom_product_types as $product_type => $title ) { if ( in_array( $product_type, $custom_product_types_on_templates, true ) ) { continue; } $templates[] = new ProductTemplate( array( 'id' => $product_type . '-product-template', 'title' => $title, 'product_data' => array( 'type' => $product_type, ), ) ); } return $templates; } /** * Register layout templates. */ public function register_layout_templates() { $layout_template_registry = wc_get_container()->get( LayoutTemplateRegistry::class ); if ( ! $layout_template_registry->is_registered( 'simple-product' ) ) { $layout_template_registry->register( 'simple-product', 'product-form', SimpleProductTemplate::class ); } if ( ! $layout_template_registry->is_registered( 'product-variation' ) ) { $layout_template_registry->register( 'product-variation', 'product-form', ProductVariationTemplate::class ); } } /** * Register product templates. */ public function register_product_templates() { /** * Allows for new product template registration. * * @since 8.5.0 */ $this->product_templates = apply_filters( 'woocommerce_product_editor_product_templates', $this->get_default_product_templates() ); $this->product_templates = $this->create_default_product_template_by_custom_product_type( $this->product_templates ); usort( $this->product_templates, function ( $a, $b ) { return $a->get_order() - $b->get_order(); } ); $this->redirection_controller->set_product_templates( $this->product_templates ); // PFT: Initialize the product form controller. if ( Features::is_enabled( 'product-editor-template-system' ) ) { $product_form_controller = new ProductFormsController(); $product_form_controller->init(); } } /** * Register user metas. */ public function register_user_metas() { register_rest_field( 'user', 'metaboxhidden_product', array( 'get_callback' => function ( $object, $attr ) { $hidden = get_user_meta( $object['id'], $attr, true ); if ( is_array( $hidden ) ) { // Ensures to always return a string array. return array_values( $hidden ); } return array( 'postcustom' ); }, 'update_callback' => function ( $value, $object, $attr ) { // Update the field/meta value. update_user_meta( $object->ID, $attr, $value ); }, 'schema' => array( 'type' => 'array', 'description' => __( 'The metaboxhidden_product meta from the user metas.', 'woocommerce' ), 'items' => array( 'type' => 'string', ), 'arg_options' => array( 'sanitize_callback' => 'wp_parse_list', 'validate_callback' => 'rest_validate_request_arg', ), ), ) ); } /** * Registers the metadata block attribute for all block types. * This is a fallback/temporary solution until * the Gutenberg core version registers the metadata attribute. * * @see https://github.com/WordPress/gutenberg/blob/6aaa3686ae67adc1a6a6b08096d3312859733e1b/lib/compat/wordpress-6.5/blocks.php#L27-L47 * To do: Remove this method once the Gutenberg core version registers the metadata attribute. * * @param array $args Array of arguments for registering a block type. * @return array $args */ public function register_metadata_attribute( $args ) { // Setup attributes if needed. if ( ! isset( $args['attributes'] ) || ! is_array( $args['attributes'] ) ) { $args['attributes'] = array(); } // Add metadata attribute if it doesn't exist. if ( ! array_key_exists( 'metadata', $args['attributes'] ) ) { $args['attributes']['metadata'] = array( 'type' => 'object', ); } return $args; } /** * Filters woocommerce block types. * * @param string[] $block_types Array of woocommerce block types. * @return array */ public function get_block_types( $block_types ) { if ( PageController::is_admin_page() ) { // Ignore all woocommerce blocks. return array(); } return $block_types; } }