| 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 WC_Product; |
| 12 |
|
| 13 |
if (!defined('ABSPATH')) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Provides product resolution and editor notices. |
| 19 |
*/ |
| 20 |
abstract class Abstract_Single_Widget extends Widget_Base |
| 21 |
{ |
| 22 |
/** |
| 23 |
* Get current product object. |
| 24 |
* |
| 25 |
* @return WC_Product|null |
| 26 |
*/ |
| 27 |
protected function get_product(): ?WC_Product |
| 28 |
{ |
| 29 |
global $product, $post; |
| 30 |
|
| 31 |
if ($product instanceof WC_Product) { |
| 32 |
return $product; |
| 33 |
} |
| 34 |
|
| 35 |
if (!empty($post->ID) && 'product' === get_post_type($post->ID)) { |
| 36 |
$prod = wc_get_product($post->ID); |
| 37 |
return $prod instanceof WC_Product ? $prod : null; |
| 38 |
} |
| 39 |
|
| 40 |
return null; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Render placeholder when product context is missing. |
| 45 |
* |
| 46 |
* @return void |
| 47 |
*/ |
| 48 |
protected function render_missing_product_notice(): void |
| 49 |
{ |
| 50 |
if (!\Elementor\Plugin::$instance->editor->is_edit_mode()) { |
| 51 |
return; |
| 52 |
} |
| 53 |
|
| 54 |
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 |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|