get_id(), 0 ) || ! SystemCheck::is_ready() ) { return ''; } $type = (string) TypeResolver::for_product( $product->get_id() ); self::$rendered = true; self::enqueue(); $fitzaro = array( 'id' => 'fitzaro-' . $product->get_id(), 'type' => $type, 'tip' => self::tip( $type ), 'consent' => self::consent(), 'context' => wp_interactivity_data_wp_context( self::context( $product->get_id(), $product->is_type( 'variable' ) ) ), ); ob_start(); include self::template(); return wp_interactivity_process_directives( (string) ob_get_clean() ); } /** * Path of the dialog template: the theme's copy (yourtheme/fitzaro/try-on.php) when there is one. */ private static function template(): string { $template = locate_template( 'fitzaro/try-on.php' ); $template = '' !== $template ? $template : FITZARO_PATH . 'templates/try-on.php'; /** * Filters the path of the try-on dialog template. * * @since 1.0.0 * * @param string $template Absolute path. */ return self::readable( apply_filters( 'fitzaro/template', $template ) ) ?? FITZARO_PATH . 'templates/try-on.php'; } /** * The path when it names a readable file. A filter can return anything. * * @param mixed $path Filter result. */ private static function readable( $path ): ?string { return is_string( $path ) && is_readable( $path ) ? $path : null; } /** * Initial context of one widget. * * @param int $product_id Product. * @param bool $is_variable Whether the product has variations. * @return array */ private static function context( int $product_id, bool $is_variable ): array { return array( 'productId' => $product_id, 'isVariable' => $is_variable, 'needsVariant' => false, 'showForm' => true, 'busy' => false, 'done' => false, 'hasFile' => false, 'previewUrl' => '', 'dragging' => false, 'position' => 50, 'imageUrl' => '', 'photo' => '', 'tryonId' => '', 'status' => '', 'error' => '', ); } /** * Enqueues the assets and the shared state (API URL, nonce, texts). */ private static function enqueue(): void { // Block themes render the template before wp_enqueue_scripts runs, so register here too. if ( ! wp_style_is( self::HANDLE, 'registered' ) ) { self::register_assets(); } if ( wp_style_is( self::HANDLE, 'registered' ) ) { wp_enqueue_style( self::HANDLE ); } if ( function_exists( 'wp_enqueue_script_module' ) ) { wp_enqueue_script_module( self::HANDLE ); } wp_interactivity_state( 'fitzaro', array( 'api' => untrailingslashit( rest_url( RestController::NAMESPACE ) ), 'nonce' => is_user_logged_in() ? wp_create_nonce( 'wp_rest' ) : '', 'i18n' => array( 'uploading' => __( 'Uploading your photo…', 'fitzaro' ), 'working' => __( 'Creating your preview. This can take up to a minute…', 'fitzaro' ), 'ready' => __( 'Your preview is ready.', 'fitzaro' ), 'deleted' => __( 'Your photo and preview were deleted.', 'fitzaro' ), 'failed' => __( 'The preview could not be created. Please try again later.', 'fitzaro' ), 'tooLong' => __( 'This is taking longer than expected. Please try again later.', 'fitzaro' ), 'offline' => __( 'Could not connect. Check your internet connection and try again.', 'fitzaro' ), 'wrongType' => __( 'Please choose a JPEG, PNG or WebP photo.', 'fitzaro' ), ), ) ); } /** * What makes a good photo for this kind of product. * * @param string $type ProductType constant. */ private static function tip( string $type ): string { switch ( $type ) { case ProductType::EYEWEAR: return __( 'Use a well-lit photo of your face from the front, with nothing covering your eyes.', 'fitzaro' ); case ProductType::DRESS: return __( 'Use a well-lit, full-length photo of yourself from the front, standing straight.', 'fitzaro' ); default: return __( 'Use a well-lit photo of yourself from the front, with your upper body fully visible.', 'fitzaro' ); } } /** * Consent text, with a link to the privacy policy when the site has one. */ private static function consent(): string { $text = esc_html__( 'Your photo is sent to OpenAI to create this preview. This store deletes it within 24 hours, and you can delete it sooner.', 'fitzaro' ); $policy = get_privacy_policy_url(); if ( '' !== $policy ) { $text .= ' ' . esc_html__( 'Privacy policy', 'fitzaro' ) . ''; } return $text; } }