| 1 |
<?php |
| 2 |
/** |
| 3 |
* Base class for Single Product builder widgets. |
| 4 |
* |
| 5 |
* @package King_Addons |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace King_Addons; |
| 9 |
|
| 10 |
use Elementor\Widget_Base; |
| 11 |
use King_Addons\Woo_Builder\Context as Woo_Context; |
| 12 |
use WC_Product; |
| 13 |
|
| 14 |
if (!defined('ABSPATH')) { |
| 15 |
exit; |
| 16 |
} |
| 17 |
|
| 18 |
/** |
| 19 |
* Provides product resolution and editor notices. |
| 20 |
*/ |
| 21 |
abstract class Abstract_Single_Widget extends Widget_Base |
| 22 |
{ |
| 23 |
/** |
| 24 |
* Get current product object. |
| 25 |
* In editor mode, will return a preview product if no product is set. |
| 26 |
* |
| 27 |
* @return WC_Product|null |
| 28 |
*/ |
| 29 |
protected function get_product(): ?WC_Product |
| 30 |
{ |
| 31 |
global $product, $post; |
| 32 |
|
| 33 |
if (!function_exists('wc_get_product') || !class_exists('WC_Product')) { |
| 34 |
return null; |
| 35 |
} |
| 36 |
|
| 37 |
// If global product is already set, use it |
| 38 |
if ($product instanceof WC_Product) { |
| 39 |
return $product; |
| 40 |
} |
| 41 |
|
| 42 |
// Try to get from post |
| 43 |
if (!empty($post->ID) && 'product' === get_post_type($post->ID)) { |
| 44 |
$prod = wc_get_product($post->ID); |
| 45 |
if ($prod instanceof WC_Product) { |
| 46 |
return $prod; |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
// In editor mode (including AJAX render), try to set up a preview product |
| 51 |
if (class_exists('King_Addons\\Woo_Builder\\Context')) { |
| 52 |
$preview = Woo_Context::setup_preview_product(); |
| 53 |
if ($preview instanceof WC_Product) { |
| 54 |
return $preview; |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
return null; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Render placeholder when product context is missing. |
| 63 |
* |
| 64 |
* @return void |
| 65 |
*/ |
| 66 |
protected function render_missing_product_notice(): void |
| 67 |
{ |
| 68 |
$is_editor = false; |
| 69 |
if (class_exists('King_Addons\\Woo_Builder\\Context')) { |
| 70 |
$is_editor = Woo_Context::is_editor(); |
| 71 |
} |
| 72 |
|
| 73 |
$is_elementor_edit = false; |
| 74 |
if (class_exists('\Elementor\Plugin') && \Elementor\Plugin::$instance && isset(\Elementor\Plugin::$instance->editor)) { |
| 75 |
$is_elementor_edit = \Elementor\Plugin::$instance->editor->is_edit_mode(); |
| 76 |
} |
| 77 |
|
| 78 |
if (!$is_editor && !$is_elementor_edit) { |
| 79 |
return; |
| 80 |
} |
| 81 |
|
| 82 |
// Check if we're editing a Single Product template but just don't have any products |
| 83 |
if (class_exists('King_Addons\\Woo_Builder\\Context') && Woo_Context::is_editing_template_type('single_product')) { |
| 84 |
echo '<div class="king-addons-woo-builder-notice">' . esc_html__('No products found. Please create at least one WooCommerce product to preview this widget.', 'king-addons') . '</div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 85 |
return; |
| 86 |
} |
| 87 |
|
| 88 |
echo '<div class="king-addons-woo-builder-notice">' . esc_html__('This widget works only in a Single Product context.', 'king-addons') . '</div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|