ID : 0; $current = $product_id > 0 ? TypeResolver::for_product( $product_id ) : null; echo '
'; woocommerce_wp_select( array( 'id' => TypeResolver::PRODUCT_META, 'label' => __( 'Virtual try-on', 'fitzaro' ), 'options' => TypeResolver::choices(), 'desc_tip' => true, 'description' => null === $current ? __( 'Try-on is not offered for this product. Choose a type to turn it on.', 'fitzaro' ) /* translators: %s: product type, e.g. Dress. */ : sprintf( __( 'Shoppers can try this product on as: %s.', 'fitzaro' ), TypeResolver::choices()[ $current ] ?? $current ), ) ); echo '
'; } /** * Saves the product setting. WooCommerce checked the nonce and capability before this hook. * * @param mixed $product Product being saved. */ public static function save_product( $product ): void { // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Verified by WooCommerce before woocommerce_admin_process_product_object. $value = isset( $_POST[ TypeResolver::PRODUCT_META ] ) ? sanitize_key( wp_unslash( $_POST[ TypeResolver::PRODUCT_META ] ) ) : null; if ( ! $product instanceof \WC_Product || null === $value ) { return; } if ( '' === $value || ! isset( TypeResolver::choices()[ $value ] ) ) { $product->delete_meta_data( TypeResolver::PRODUCT_META ); return; } $product->update_meta_data( TypeResolver::PRODUCT_META, $value ); } /** * Add-category form field. */ public static function render_new_category(): void { echo '
'; self::category_select( '' ); echo '

' . esc_html( self::category_help() ) . '

'; } /** * Edit-category form row. * * @param mixed $term Category being edited. */ public static function render_category( $term ): void { $current = $term instanceof \WP_Term ? (string) get_term_meta( $term->term_id, TypeResolver::TERM_META, true ) : ''; echo ''; self::category_select( $current ); echo '

' . esc_html( self::category_help() ) . '

'; } /** * Saves the category setting. WordPress checked the nonce before this hook; the capability is checked here. * * @param int $term_id Category ID. */ public static function save_category( $term_id ): void { // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Verified by WordPress (add-tag / update-tag) before these hooks. $value = isset( $_POST['fitzaro_type'] ) ? sanitize_key( wp_unslash( $_POST['fitzaro_type'] ) ) : null; if ( null === $value || ! current_user_can( 'manage_product_terms' ) ) { return; } if ( '' === $value || ! isset( TypeResolver::choices()[ $value ] ) ) { delete_term_meta( (int) $term_id, TypeResolver::TERM_META ); return; } update_term_meta( (int) $term_id, TypeResolver::TERM_META, $value ); } /** * The category select element. * * @param string $current Saved value. */ private static function category_select( string $current ): void { echo ''; } /** * Help text under the category select. */ private static function category_help(): string { return __( 'Applies to all products in this category and its subcategories. Automatic guesses from the category name; set a type when the name is not in English.', 'fitzaro' ); } }