| 1 |
<?php |
| 2 |
/** |
| 3 |
* Theme Builder Archive Title widget (Free). |
| 4 |
* |
| 5 |
* @package King_Addons |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace King_Addons; |
| 9 |
|
| 10 |
use Elementor\Controls_Manager; |
| 11 |
use Elementor\Group_Control_Typography; |
| 12 |
use Elementor\Widget_Base; |
| 13 |
|
| 14 |
if (!defined('ABSPATH')) { |
| 15 |
exit; |
| 16 |
} |
| 17 |
|
| 18 |
/** |
| 19 |
* Displays the current archive title. |
| 20 |
*/ |
| 21 |
class TB_Archive_Title extends Widget_Base |
| 22 |
{ |
| 23 |
/** |
| 24 |
* Widget slug. |
| 25 |
* |
| 26 |
* @return string |
| 27 |
*/ |
| 28 |
public function get_name(): string |
| 29 |
{ |
| 30 |
return 'king-addons-tb-archive-title'; |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Widget title. |
| 35 |
* |
| 36 |
* @return string |
| 37 |
*/ |
| 38 |
public function get_title(): string |
| 39 |
{ |
| 40 |
return esc_html__('TB - Archive Title', '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-tb-archive-title'; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Style dependencies. |
| 55 |
* |
| 56 |
* @return array<int, string> |
| 57 |
*/ |
| 58 |
public function get_style_depends(): array |
| 59 |
{ |
| 60 |
return [KING_ADDONS_ASSETS_UNIQUE_KEY . '-tb-archive-title-style']; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Script dependencies. |
| 65 |
* |
| 66 |
* @return array<int, string> |
| 67 |
*/ |
| 68 |
public function get_script_depends(): array |
| 69 |
{ |
| 70 |
return [KING_ADDONS_ASSETS_UNIQUE_KEY . '-tb-archive-title-script']; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Categories. |
| 75 |
* |
| 76 |
* @return array<int, string> |
| 77 |
*/ |
| 78 |
public function get_categories(): array |
| 79 |
{ |
| 80 |
return ['king-addons-theme-builder']; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Keywords. |
| 85 |
* |
| 86 |
* @return array<int, string> |
| 87 |
*/ |
| 88 |
public function get_keywords(): array |
| 89 |
{ |
| 90 |
return ['archive', 'title', 'taxonomy', 'search', 'king-addons']; |
| 91 |
} |
| 92 |
|
| 93 |
public function get_custom_help_url() |
| 94 |
{ |
| 95 |
return 'mailto:[email protected]?subject=Bug Report - King Addons&body=Please describe the issue'; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Register controls. |
| 100 |
* |
| 101 |
* @return void |
| 102 |
*/ |
| 103 |
public function register_controls(): void |
| 104 |
{ |
| 105 |
$this->register_content_controls(false); |
| 106 |
$this->register_style_controls(false); |
| 107 |
$this->register_pro_notice_controls(); |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Render output. |
| 112 |
* |
| 113 |
* @return void |
| 114 |
*/ |
| 115 |
public function render(): void |
| 116 |
{ |
| 117 |
$settings = $this->get_settings_for_display(); |
| 118 |
$this->render_output($settings, false); |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Content controls. |
| 123 |
* |
| 124 |
* @param bool $is_pro Whether Pro controls are enabled. |
| 125 |
* |
| 126 |
* @return void |
| 127 |
*/ |
| 128 |
protected function register_content_controls(bool $is_pro): void |
| 129 |
{ |
| 130 |
$this->start_controls_section( |
| 131 |
'kng_content_section', |
| 132 |
[ |
| 133 |
'label' => KING_ADDONS_ELEMENTOR_ICON . esc_html__('Content', 'king-addons'), |
| 134 |
'tab' => Controls_Manager::TAB_CONTENT, |
| 135 |
] |
| 136 |
); |
| 137 |
|
| 138 |
$this->add_control( |
| 139 |
'kng_html_tag', |
| 140 |
[ |
| 141 |
'label' => esc_html__('HTML Tag', 'king-addons'), |
| 142 |
'type' => Controls_Manager::SELECT, |
| 143 |
'default' => 'h1', |
| 144 |
'options' => [ |
| 145 |
'h1' => 'H1', |
| 146 |
'h2' => 'H2', |
| 147 |
'h3' => 'H3', |
| 148 |
'div' => 'div', |
| 149 |
'span' => 'span', |
| 150 |
], |
| 151 |
] |
| 152 |
); |
| 153 |
|
| 154 |
$this->add_control( |
| 155 |
'kng_prefix_mode', |
| 156 |
[ |
| 157 |
'label' => esc_html__('Prefix', 'king-addons'), |
| 158 |
'type' => Controls_Manager::SELECT, |
| 159 |
'default' => 'default', |
| 160 |
'options' => [ |
| 161 |
'default' => esc_html__('Use Default', 'king-addons'), |
| 162 |
'remove' => esc_html__('Remove Default Prefix', 'king-addons'), |
| 163 |
'custom' => $is_pro ? |
| 164 |
esc_html__('Custom Prefix', 'king-addons') : |
| 165 |
sprintf(__('Custom Prefix %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'), |
| 166 |
], |
| 167 |
] |
| 168 |
); |
| 169 |
|
| 170 |
$this->add_control( |
| 171 |
'kng_custom_prefix', |
| 172 |
[ |
| 173 |
'label' => esc_html__('Custom Prefix Text', 'king-addons'), |
| 174 |
'type' => Controls_Manager::TEXT, |
| 175 |
'dynamic' => ['active' => true], |
| 176 |
'placeholder' => esc_html__('Posts in', 'king-addons'), |
| 177 |
'condition' => [ |
| 178 |
'kng_prefix_mode' => 'custom', |
| 179 |
], |
| 180 |
'classes' => $is_pro ? '' : 'king-addons-pro-control', |
| 181 |
] |
| 182 |
); |
| 183 |
|
| 184 |
$this->end_controls_section(); |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Style controls. |
| 189 |
* |
| 190 |
* @param bool $is_pro Whether Pro controls are enabled. |
| 191 |
* |
| 192 |
* @return void |
| 193 |
*/ |
| 194 |
protected function register_style_controls(bool $is_pro): void |
| 195 |
{ |
| 196 |
$this->start_controls_section( |
| 197 |
'kng_style_section', |
| 198 |
[ |
| 199 |
'label' => esc_html__('Style', 'king-addons'), |
| 200 |
'tab' => Controls_Manager::TAB_STYLE, |
| 201 |
] |
| 202 |
); |
| 203 |
|
| 204 |
$this->add_group_control( |
| 205 |
Group_Control_Typography::get_type(), |
| 206 |
[ |
| 207 |
'name' => 'kng_typography', |
| 208 |
'selector' => '{{WRAPPER}} .king-addons-tb-archive-title', |
| 209 |
] |
| 210 |
); |
| 211 |
|
| 212 |
$this->add_control( |
| 213 |
'kng_color', |
| 214 |
[ |
| 215 |
'label' => esc_html__('Color', 'king-addons'), |
| 216 |
'type' => Controls_Manager::COLOR, |
| 217 |
'selectors' => [ |
| 218 |
'{{WRAPPER}} .king-addons-tb-archive-title' => 'color: {{VALUE}};', |
| 219 |
], |
| 220 |
] |
| 221 |
); |
| 222 |
|
| 223 |
$this->add_responsive_control( |
| 224 |
'kng_alignment', |
| 225 |
[ |
| 226 |
'label' => esc_html__('Alignment', 'king-addons'), |
| 227 |
'type' => Controls_Manager::CHOOSE, |
| 228 |
'options' => [ |
| 229 |
'left' => [ |
| 230 |
'title' => esc_html__('Left', 'king-addons'), |
| 231 |
'icon' => 'eicon-text-align-left', |
| 232 |
], |
| 233 |
'center' => [ |
| 234 |
'title' => esc_html__('Center', 'king-addons'), |
| 235 |
'icon' => 'eicon-text-align-center', |
| 236 |
], |
| 237 |
'right' => [ |
| 238 |
'title' => esc_html__('Right', 'king-addons'), |
| 239 |
'icon' => 'eicon-text-align-right', |
| 240 |
], |
| 241 |
], |
| 242 |
'selectors' => [ |
| 243 |
'{{WRAPPER}} .king-addons-tb-archive-title' => 'text-align: {{VALUE}};', |
| 244 |
], |
| 245 |
] |
| 246 |
); |
| 247 |
|
| 248 |
$this->add_responsive_control( |
| 249 |
'kng_margin', |
| 250 |
[ |
| 251 |
'label' => esc_html__('Margin', 'king-addons'), |
| 252 |
'type' => Controls_Manager::DIMENSIONS, |
| 253 |
'size_units' => ['px', 'em', '%'], |
| 254 |
'selectors' => [ |
| 255 |
'{{WRAPPER}} .king-addons-tb-archive-title' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', |
| 256 |
], |
| 257 |
] |
| 258 |
); |
| 259 |
|
| 260 |
$this->end_controls_section(); |
| 261 |
} |
| 262 |
|
| 263 |
/** |
| 264 |
* Pro upsell. |
| 265 |
* |
| 266 |
* @return void |
| 267 |
*/ |
| 268 |
protected function register_pro_notice_controls(): void |
| 269 |
{ |
| 270 |
if (king_addons_freemius()->can_use_premium_code__premium_only()) { |
| 271 |
return; |
| 272 |
} |
| 273 |
|
| 274 |
Core::renderProFeaturesSection( |
| 275 |
$this, |
| 276 |
'', |
| 277 |
Controls_Manager::RAW_HTML, |
| 278 |
'tb-archive-title', |
| 279 |
[ |
| 280 |
'Custom prefixes per archive type', |
| 281 |
'Advanced conditional formatting', |
| 282 |
] |
| 283 |
); |
| 284 |
} |
| 285 |
|
| 286 |
/** |
| 287 |
* Render output helper. |
| 288 |
* |
| 289 |
* @param array<string, mixed> $settings Settings. |
| 290 |
* @param bool $is_pro Pro flag. |
| 291 |
* |
| 292 |
* @return void |
| 293 |
*/ |
| 294 |
protected function render_output(array $settings, bool $is_pro): void |
| 295 |
{ |
| 296 |
$tag = isset($settings['kng_html_tag']) ? strtolower((string) $settings['kng_html_tag']) : 'h1'; |
| 297 |
$allowed_tags = ['h1', 'h2', 'h3', 'div', 'span']; |
| 298 |
if (!in_array($tag, $allowed_tags, true)) { |
| 299 |
$tag = 'h1'; |
| 300 |
} |
| 301 |
|
| 302 |
// get_the_archive_title() has nothing to say on a search page and falls |
| 303 |
// back to a bare "Archives", so the Search template needs the query. |
| 304 |
if (is_search()) { |
| 305 |
$title = sprintf( |
| 306 |
/* translators: %s: the search phrase the visitor typed */ |
| 307 |
esc_html__('Search results for: %s', 'king-addons'), |
| 308 |
get_search_query() |
| 309 |
); |
| 310 |
} else { |
| 311 |
$title = get_the_archive_title(); |
| 312 |
} |
| 313 |
|
| 314 |
if ('remove' === ($settings['kng_prefix_mode'] ?? 'default')) { |
| 315 |
$title = preg_replace('/^[^:]+:\\s*/', '', $title); |
| 316 |
} |
| 317 |
|
| 318 |
// Custom prefix replaces WordPress's "Category:" / "Tag:" prefix. |
| 319 |
// Prepending to the already-prefixed string produced |
| 320 |
// "ARCH-OK Category: Journal" instead of "ARCH-OK Journal". |
| 321 |
if ('custom' === ($settings['kng_prefix_mode'] ?? 'default') && $is_pro && !empty($settings['kng_custom_prefix'])) { |
| 322 |
$bare = preg_replace('/^[^:]+:\\s*/', '', (string) $title); |
| 323 |
$title = sprintf('%s %s', $settings['kng_custom_prefix'], $bare); |
| 324 |
} |
| 325 |
|
| 326 |
$title = wp_strip_all_tags((string) $title); |
| 327 |
if ('' === $title) { |
| 328 |
return; |
| 329 |
} |
| 330 |
|
| 331 |
printf( |
| 332 |
'<%1$s class="king-addons-tb-archive-title">%2$s</%1$s>', |
| 333 |
esc_attr($tag), |
| 334 |
esc_html($title) |
| 335 |
); |
| 336 |
} |
| 337 |
} |
| 338 |
|