| 1 |
<?php |
| 2 |
/** |
| 3 |
* Woo My Account Logout widget. |
| 4 |
* |
| 5 |
* @package King_Addons |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace King_Addons; |
| 9 |
|
| 10 |
use Elementor\Controls_Manager; |
| 11 |
use Elementor\Widget_Base; |
| 12 |
use King_Addons\Woo_Builder\Context as Woo_Context; |
| 13 |
|
| 14 |
if (!defined('ABSPATH')) { |
| 15 |
exit; |
| 16 |
} |
| 17 |
|
| 18 |
/** |
| 19 |
* Renders logout button for My Account. |
| 20 |
*/ |
| 21 |
class Woo_My_Account_Logout extends Widget_Base |
| 22 |
{ |
| 23 |
/** |
| 24 |
* Widget slug. |
| 25 |
* |
| 26 |
* @return string |
| 27 |
*/ |
| 28 |
public function get_name(): string |
| 29 |
{ |
| 30 |
return 'woo_my_account_logout'; |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Widget title. |
| 35 |
* |
| 36 |
* @return string |
| 37 |
*/ |
| 38 |
public function get_title(): string |
| 39 |
{ |
| 40 |
return esc_html__('My Account Logout', 'king-addons'); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Widget icon. |
| 45 |
* |
| 46 |
* @return string |
| 47 |
*/ |
| 48 |
public function get_icon(): string |
| 49 |
{ |
| 50 |
return 'king-addons-icon king-addons-woo-my-account-logout'; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Widget categories. |
| 55 |
* |
| 56 |
* @return array<int,string> |
| 57 |
*/ |
| 58 |
public function get_categories(): array |
| 59 |
{ |
| 60 |
return ['king-addons-woo-builder']; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Style dependencies. |
| 65 |
* |
| 66 |
* @return array<int, string> |
| 67 |
*/ |
| 68 |
public function get_style_depends(): array |
| 69 |
{ |
| 70 |
return [KING_ADDONS_ASSETS_UNIQUE_KEY . '-woo-my-account-logout-style']; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Script dependencies. |
| 75 |
* |
| 76 |
* @return array<int, string> |
| 77 |
*/ |
| 78 |
public function get_script_depends(): array |
| 79 |
{ |
| 80 |
return [KING_ADDONS_ASSETS_UNIQUE_KEY . '-woo-my-account-logout-script']; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Register controls. |
| 85 |
* |
| 86 |
* @return void |
| 87 |
*/ |
| 88 |
public function get_custom_help_url() |
| 89 |
{ |
| 90 |
return 'mailto:[email protected]?subject=Bug Report - King Addons&body=Please describe the issue'; |
| 91 |
} |
| 92 |
|
| 93 |
protected function register_controls(): void |
| 94 |
{ |
| 95 |
$this->start_controls_section( |
| 96 |
'section_content', |
| 97 |
[ |
| 98 |
'label' => esc_html__('Content', 'king-addons'), |
| 99 |
'tab' => Controls_Manager::TAB_CONTENT, |
| 100 |
] |
| 101 |
); |
| 102 |
|
| 103 |
$this->add_control( |
| 104 |
'label', |
| 105 |
[ |
| 106 |
'label' => esc_html__('Button Text', 'king-addons'), |
| 107 |
'type' => Controls_Manager::TEXT, |
| 108 |
'dynamic' => ['active' => true], |
| 109 |
'default' => __('Log out', 'king-addons'), |
| 110 |
] |
| 111 |
); |
| 112 |
|
| 113 |
$this->add_control( |
| 114 |
'confirm', |
| 115 |
[ |
| 116 |
'label' => esc_html__('Confirm before logout', 'king-addons'), |
| 117 |
'type' => Controls_Manager::SWITCHER, |
| 118 |
'return_value' => 'yes', |
| 119 |
'default' => '', |
| 120 |
'description' => esc_html__('If enabled, user will see a confirmation step.', 'king-addons'), |
| 121 |
] |
| 122 |
); |
| 123 |
|
| 124 |
$this->end_controls_section(); |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Render widget output. |
| 129 |
* |
| 130 |
* @return void |
| 131 |
*/ |
| 132 |
protected function render(): void |
| 133 |
{ |
| 134 |
if (!Woo_Context::maybe_render_context_notice('my_account')) { |
| 135 |
return; |
| 136 |
} |
| 137 |
$in_builder = class_exists('King_Addons\\Woo_Builder\\Context') && Woo_Context::is_editing_template_type('my_account'); |
| 138 |
if (!function_exists('is_account_page') || (!is_account_page() && !$in_builder)) { |
| 139 |
return; |
| 140 |
} |
| 141 |
|
| 142 |
$settings = $this->get_settings_for_display(); |
| 143 |
// ?: not ??: a cleared field is '' rather than null, which produced a |
| 144 |
// link with no text at all. |
| 145 |
$label = ($settings['label'] ?? null) ?: __('Log out', 'king-addons'); |
| 146 |
$logout_url = wc_logout_url(); |
| 147 |
if (($settings['confirm'] ?? '') === 'yes') { |
| 148 |
// The confirmation screen is rendered on the account page, but |
| 149 |
// wc_logout_url() is just wp_logout_url() in current WooCommerce - |
| 150 |
// it points at wp-login.php, which never reaches that screen and |
| 151 |
// signs the customer out immediately. Use the account endpoint so |
| 152 |
// the interstitial can intercept it. |
| 153 |
$confirm_url = ''; |
| 154 |
if (function_exists('wc_get_endpoint_url') && get_option('woocommerce_logout_endpoint')) { |
| 155 |
$endpoint_url = wc_get_endpoint_url('customer-logout', '', wc_get_page_permalink('myaccount')); |
| 156 |
if ($endpoint_url) { |
| 157 |
$confirm_url = wp_nonce_url(add_query_arg('ka_logout_confirm', '1', $endpoint_url), 'customer-logout'); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
$logout_url = $confirm_url ?: add_query_arg('ka_logout_confirm', '1', $logout_url); |
| 162 |
} |
| 163 |
|
| 164 |
$confirm_attr = ($settings['confirm'] ?? '') === 'yes' ? ' data-ka-logout-confirm="1"' : ''; |
| 165 |
|
| 166 |
echo '<a class="ka-woo-account-logout button" href="' . esc_url($logout_url) . '"' . $confirm_attr . '>'; |
| 167 |
echo esc_html($label); |
| 168 |
echo '</a>'; |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|