null,
'showDescription' => true,
'showPrice' => true,
'customImage' => '',
'backgroundColor' => '#fffeeb',
'contentColor' => '',
'pricingColor' => '',
'template' => 'left'
];
$atts = wp_parse_args($data['attrs'], $defaultAtts);
$productId = (int) $atts['productId'];
if (!$productId) {
return '';
}
if(isset(self::$productsCache[$productId])) {
$product = self::$productsCache[$productId];
} else {
self::$productsCache[$productId] = wc_get_product($productId);
$product = self::$productsCache[$productId];
}
if (!$product) {
return '';
}
$tableStyle = 'border-radius: 5px;';
if ($bgColor = Arr::get($atts, 'backgroundColor')) {
$tableStyle .= 'background-color: ' . $bgColor . ';';
}
$contentColorStyle = '';
if ($color = Arr::get($atts, 'contentColor')) {
$tableStyle .= 'color: ' . $color . ';';
$contentColorStyle = 'color: ' . $color . ';';
}
$pricingStyle = 'display:block;margin:8px 0 10px;font-size:18px;line-height:1.25;';
if ($color = Arr::get($atts, 'pricingColor')) {
$pricingStyle .= 'color: ' . $color . ';';
}
$contentHtml = sprintf(
'
%2$s
',
$contentColorStyle.'margin: 0;',
wp_kses_post($product->get_title())
);
if (Arr::get($atts, 'showDescription')) {
$contentHtml .= sprintf(
'%2$s
',
$contentColorStyle.'margin-bottom: 5px;',
wc_format_content(wp_kses_post($product->get_short_description() ? $product->get_short_description() : wc_trim_string($product->get_description(), 400)))
);
}
if (Arr::get($atts, 'showPrice')) {
$contentHtml .= sprintf(
'%2$s
',
$pricingStyle,
ProductListRenderer::preparePriceHtml($product->get_price_html(), __('Free', 'fluent-crm'))
);
}
$image = '';
$template = Arr::get($atts, 'template', 'left');
if($template != 'none') {
$image = self::getImage($product);
}
if (!$image) {
$template = 'none';
}
$atts['template'] = $template;
$contentStyle = '';
if ($template == 'none' || $template == 'top') {
$contentStyle = 'text-align: center;';
}
$imageTd = '';
if($image) {
if($template == 'top') {
$contentHtml = '
'.$contentHtml;
} else if($template == 'left') {
$imageContent = '
';
$imageTd = self::getContentTd($imageContent, $template);
}
}
$buttonHtml = ''.$buttonHtml.'
';
$contentHtml .= $buttonHtml;
$tableClass = 'fce_row';
if ($template === 'left' || $template === 'right') {
$tableClass .= ' fc_woo_product_stack_mobile';
}
ob_start();
?>
'; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
/**
* Returns the main product image URL.
*
* @param \WC_Product $product Product object.
* @param string $size Image size, defaults to 'full'.
* @return string
*/
public static function getImage($product, $size = 'full')
{
if(!defined('WC_PLUGIN_FILE')) {
return '';
}
static $imageCache = [];
if(isset($imageCache[$product->get_id()])) {
return $imageCache[$product->get_id()];
}
$image = '';
if ($product->get_image_id()) {
$image = wp_get_attachment_image_url($product->get_image_id(), $size);
} elseif ($product->get_parent_id()) {
$parent_product = wc_get_product($product->get_parent_id());
if ($parent_product) {
$image = wp_get_attachment_image_url($parent_product->get_image_id(), $size);
}
}
$imageCache[$product->get_id()] = $image;
return $imageCache[$product->get_id()] ;
}
}