rue */ public function enable_preview_mode() { return true; } /** * Get the placeholder image for the preview email. * * @return string */ public function get_placeholder_image() { return ''; } /** * Force products in preview to be considered downloadable so core renders downloads section. * * @param bool $is_downloadable Current value. * @return bool */ public function force_product_downloadable( $is_downloadable ) { /** * Filters whether the current request is an email preview. * * When true, products should be considered downloadable so the downloads * section renders in applicable emails during preview. * * @since 9.6.0 * * @param bool $is_email_preview Whether preview mode is active. */ if ( apply_filters( 'woocommerce_is_email_preview', false ) ) { return true; } return $is_downloadable; } /** * Provide a dummy product file so product->has_file() returns true in preview. * * @param array|null $file Current file array or null. * @return array|null */ public function provide_dummy_product_file( $file ) { /** * Filters whether the current request is an email preview. * * When true, provide a dummy product file array so downloadable template parts * can render during preview. * * @since 9.6.0 * * @param bool $is_email_preview Whether preview mode is active. */ if ( apply_filters( 'woocommerce_is_email_preview', false ) ) { return array( 'name' => __( 'Sample Download File.pdf', 'woocommerce' ), 'file' => 'sample-download.pdf', ); } return $file; } /** * Get dummy downloadable items for email preview. * * @param array $downloads Existing downloads. * @return array */ public function get_dummy_downloadable_items( $downloads ) { $dummy_downloads = array( array( 'product_name' => $this->get_dummy_downloadable_product()->get_name(), 'product_id' => $this->get_dummy_downloadable_product()->get_id(), 'download_url' => 'https://example.com/download', 'download_name' => __( 'Sample Download File.pdf', 'woocommerce' ), 'access_expires' => time() + ( 30 * DAY_IN_SECONDS ), ), ); return array_merge( $downloads, $dummy_downloads ); } /** * Generate placeholder content for a specific email type, typically used in the email editor. * * Encapsulates the logic for setting the email type, generating raw content, applying styles, * ensuring links open in new tabs, and handling errors based on WP_DEBUG. * * @param string $email_type_class_name The class name of the WC_Email type (e.g., 'WC_Email_Customer_Processing_Order'). * @return string The generated and styled HTML content. * @throws \RuntimeException If content generation fails. If rendering fails. */ public function generate_placeholder_content( string $email_type_class_name ): string { // Note: set_email_type can throw InvalidArgumentException. $this->set_email_type( $email_type_class_name ); $woo_content_processor = wc_get_container()->get( WooContentProcessor::class ); $generate_content_closure = function () use ( $woo_content_processor ) { // Note: If 'woocommerce_email_styles' filter was intentional and `prepare_css` isn't // the intended callback, adjust accordingly. This assumes `prepare_css` applies styles // needed for the Woo content block. add_filter( 'woocommerce_email_styles', array( $woo_content_processor, 'prepare_css' ), 10, 2 ); $content = $woo_content_processor->get_woo_content( $this->get_email() ); $content = $this->get_email()->style_inline( $content ); $content = $this->ensure_links_open_in_new_tab( $content ); return $content; }; $this->set_up_filters(); $message = ''; try { if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { $message = $generate_content_closure(); } else { // Use output buffering to prevent partial renders with PHP notices or warnings when WP_DEBUG is off. ob_start(); try { $message = $generate_content_closure(); } catch ( Throwable $e ) { ob_end_clean(); // Let the caller handle the exception. throw new \RuntimeException( esc_html__( 'There was an error rendering the email editor placeholder content.', 'woocommerce' ), 0, $e ); } ob_end_clean(); } } finally { $this->clean_up_filters(); } return $message; } /** * Switch to the site locale. This is to ensure the email is displayed * in the store's language, as the customer would see it, not the admin's language. */ private function switch_to_site_locale() { if ( ! $this->locale_switched ) { wc_switch_to_site_locale(); $this->locale_switched = true; } } /** * Restore the original locale. */ private function restore_locale() { if ( $this->locale_switched ) { wc_restore_locale(); $this->locale_switched = false; } } }