| 1 |
<?php |
| 2 |
/** |
| 3 |
* Base class for My Account 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 |
|
| 13 |
if (!defined('ABSPATH')) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Provides my-account context utilities. |
| 19 |
*/ |
| 20 |
abstract class Abstract_My_Account_Widget extends Widget_Base |
| 21 |
{ |
| 22 |
/** |
| 23 |
* Check if we should render the widget. |
| 24 |
* Returns true if in editor editing my_account template or on actual my account page. |
| 25 |
* |
| 26 |
* @return bool |
| 27 |
*/ |
| 28 |
protected function should_render(): bool |
| 29 |
{ |
| 30 |
if (!$this->can_use_builder_widgets()) { |
| 31 |
return false; |
| 32 |
} |
| 33 |
|
| 34 |
// In editor mode editing my_account template |
| 35 |
if (class_exists('King_Addons\\Woo_Builder\\Context') && Woo_Context::is_editing_template_type('my_account')) { |
| 36 |
return true; |
| 37 |
} |
| 38 |
|
| 39 |
// On actual my account page |
| 40 |
if (function_exists('is_account_page') && is_account_page()) { |
| 41 |
return true; |
| 42 |
} |
| 43 |
|
| 44 |
return false; |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Whether this request is the given My Account endpoint. |
| 49 |
* |
| 50 |
* An empty endpoint means the account dashboard (no WooCommerce endpoint |
| 51 |
* query var). The Elementor editor always returns true so every widget |
| 52 |
* on the template stays visible while designing. |
| 53 |
* |
| 54 |
* @param string $endpoint WooCommerce endpoint slug, or '' for dashboard. |
| 55 |
* |
| 56 |
* @return bool |
| 57 |
*/ |
| 58 |
protected function is_current_endpoint(string $endpoint = ''): bool |
| 59 |
{ |
| 60 |
if (class_exists('King_Addons\\Woo_Builder\\Context') && (Woo_Context::is_editing_template_type('my_account') || Woo_Context::is_editor())) { |
| 61 |
return true; |
| 62 |
} |
| 63 |
|
| 64 |
if (!function_exists('is_wc_endpoint_url')) { |
| 65 |
return '' === $endpoint; |
| 66 |
} |
| 67 |
|
| 68 |
if ('' === $endpoint) { |
| 69 |
return !is_wc_endpoint_url(); |
| 70 |
} |
| 71 |
|
| 72 |
return is_wc_endpoint_url($endpoint); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Cart / checkout / account builder widgets are a Pro feature. |
| 77 |
* |
| 78 |
* @return bool |
| 79 |
*/ |
| 80 |
protected function can_use_builder_widgets(): bool |
| 81 |
{ |
| 82 |
return function_exists('king_addons_can_use_pro') && king_addons_can_use_pro(); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Render placeholder when my account context is missing. |
| 87 |
* |
| 88 |
* @return void |
| 89 |
*/ |
| 90 |
protected function render_missing_account_notice(): void |
| 91 |
{ |
| 92 |
if (!class_exists('\Elementor\Plugin') || !\Elementor\Plugin::$instance->editor->is_edit_mode()) { |
| 93 |
return; |
| 94 |
} |
| 95 |
|
| 96 |
if (!$this->can_use_builder_widgets()) { |
| 97 |
$this->render_pro_required_notice(); |
| 98 |
return; |
| 99 |
} |
| 100 |
|
| 101 |
// Check if we're editing a My Account template |
| 102 |
if (class_exists('King_Addons\\Woo_Builder\\Context') && Woo_Context::is_editing_template_type('my_account')) { |
| 103 |
// Don't show notice - we're in the right template type |
| 104 |
return; |
| 105 |
} |
| 106 |
|
| 107 |
echo '<div class="king-addons-woo-builder-notice">' . esc_html__('This widget works only on the WooCommerce My Account page.', 'king-addons') . '</div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Editor-only upsell when the site cannot use Pro. |
| 112 |
* |
| 113 |
* @return void |
| 114 |
*/ |
| 115 |
protected function render_pro_required_notice(): void |
| 116 |
{ |
| 117 |
if (class_exists('King_Addons\\Core')) { |
| 118 |
Core::renderEditorHint(esc_html__('Available in King Addons Pro.', 'king-addons')); |
| 119 |
return; |
| 120 |
} |
| 121 |
|
| 122 |
echo '<div class="king-addons-woo-builder-notice">' . esc_html__('Available in King Addons Pro.', 'king-addons') . '</div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Render login form if user is not logged in. |
| 127 |
* In editor mode, shows a placeholder instead. |
| 128 |
* |
| 129 |
* @return bool True when login form rendered. |
| 130 |
*/ |
| 131 |
protected function maybe_render_login_form(): bool |
| 132 |
{ |
| 133 |
// In editor mode, don't show login form - show preview content |
| 134 |
if (class_exists('King_Addons\\Woo_Builder\\Context') && Woo_Context::is_editor()) { |
| 135 |
return false; |
| 136 |
} |
| 137 |
|
| 138 |
if (is_user_logged_in()) { |
| 139 |
return false; |
| 140 |
} |
| 141 |
|
| 142 |
echo '<div class="king-addons-my-account-login">'; |
| 143 |
woocommerce_login_form(['redirect' => wc_get_page_permalink('myaccount')]); |
| 144 |
echo '</div>'; |
| 145 |
return true; |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|