Current File : /home/n742ef5/royalanteam.com/wp-content/plugins/myhome-core/src/Widgets/General/TermListWidget.php |
<?php
namespace Tangibledesign\MyHome\Widgets\General;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Repeater;
use Tangibledesign\Framework\Core\Collection;
use Tangibledesign\Framework\Models\Field\TaxonomyField;
use Tangibledesign\Framework\Models\Term\CustomTerm;
use Tangibledesign\Framework\Widgets\Helpers\BaseGeneralWidget;
use Tangibledesign\Framework\Widgets\Helpers\SelectRemoteControl;
class TermListWidget extends BaseGeneralWidget
{
public function getKey(): string
{
return 'term_list';
}
public function getName(): string
{
return esc_html__('Term List', 'myhome-core');
}
protected function register_controls(): void
{
$this->addGeneralContentSection();
$this->addGeneralStyleSection();
}
private function addGeneralContentSection(): void
{
$this->startContentControlsSection();
$this->addTermsControl();
$this->endControlsSection();
}
private function addGeneralStyleSection(): void
{
$this->startStyleControlsSection();
$this->add_responsive_control(
'terms_columns',
[
'label' => esc_html__('Columns', 'myhome-core'),
'type' => Controls_Manager::SELECT,
'options' => [
'1' => '1',
'2' => '2',
'3' => '3',
'4' => '4',
'5' => '5',
'6' => '6',
],
'selectors' => [
'{{WRAPPER}} .myhome-term-list' => 'grid-template-columns: repeat({{VALUE}}, minmax(0, 1fr));',
],
]
);
$this->add_responsive_control(
'terms_gap',
[
'label' => esc_html__('Gap', 'myhome-core'),
'type' => Controls_Manager::SLIDER,
'size_units' => ['px'],
'range' => [
'px' => [
'min' => 0,
'max' => 100,
'step' => 1,
],
],
'selectors' => [
'{{WRAPPER}} .myhome-term-list' => 'gap: {{SIZE}}{{UNIT}};',
],
]
);
$this->add_control(
'term_name_heading',
[
'label' => esc_html__('Name', 'myhome-core'),
'type' => Controls_Manager::HEADING,
]
);
$this->add_control(
'term_name_color',
[
'label' => esc_html__('Color', 'myhome-core'),
'type' => Controls_Manager::COLOR,
'selectors' => [
'{{WRAPPER}} .myhome-term-card__name' => 'color: {{VALUE}};',
],
]
);
$this->add_group_control(
Group_Control_Typography::get_type(),
[
'name' => 'term_name_typography',
'label' => esc_html__('Typography', 'myhome-core'),
'selector' => '{{WRAPPER}} .myhome-term-card__name',
]
);
$this->add_control(
'term_count_heading',
[
'label' => esc_html__('Count', 'myhome-core'),
'type' => Controls_Manager::HEADING,
]
);
$this->add_control(
'term_count_color',
[
'label' => esc_html__('Color', 'myhome-core'),
'type' => Controls_Manager::COLOR,
'selectors' => [
'{{WRAPPER}} .myhome-term-card__count' => 'color: {{VALUE}};',
],
]
);
$this->add_group_control(
Group_Control_Typography::get_type(),
[
'name' => 'term_count_typography',
'label' => esc_html__('Typography', 'myhome-core'),
'selector' => '{{WRAPPER}} .myhome-term-card__count',
]
);
$this->endControlsSection();
}
private function addTermsControl(): void
{
$this->add_control(
'taxonomy',
[
'label' => tdf_admin_string('taxonomy'),
'type' => Controls_Manager::SELECT,
'options' => tdf_app('taxonomy_list'),
]
);
foreach (tdf_taxonomy_fields() as $taxonomy) {
$this->addTermsControlByTaxonomy($taxonomy);
}
}
private function addTermsControlByTaxonomy(TaxonomyField $taxonomyField): void
{
$terms = new Repeater();
$terms->add_control(
'termId',
[
'label' => tdf_admin_string('term'),
'type' => SelectRemoteControl::TYPE,
'source' => $taxonomyField->getApiEndpoint(),
'multiple' => false,
]
);
$terms->add_control(
'image',
[
'label' => tdf_admin_string('image'),
'type' => Controls_Manager::MEDIA,
]
);
$terms->add_control(
'text',
[
'label' => esc_html__('Text', 'myhome-core'),
'type' => Controls_Manager::TEXTAREA,
]
);
$this->add_control(
'terms_' . $taxonomyField->getKey(),
[
'label' => tdf_admin_string('terms'),
'type' => Controls_Manager::REPEATER,
'fields' => $terms->get_controls(),
'prevent_empty' => false,
'condition' => [
'taxonomy' => $taxonomyField->getKey(),
]
]
);
}
public function getTerms(): Collection
{
$taxonomyField = $this->getTaxonomyField();
if (!$taxonomyField) {
return tdf_collect();
}
$termsData = $this->get_settings_for_display('terms_' . $taxonomyField->getKey());
if (empty($termsData) || !is_array($termsData)) {
return tdf_collect();
}
return tdf_collect($termsData)
->map(static function ($termData) {
$term = tdf_term_factory()->create((int)$termData['termId']);
if (!$term instanceof CustomTerm) {
return false;
}
$termData['term'] = $term;
return $termData;
})
->filter(static function ($termData) {
return $termData !== false;
});
}
private function getTaxonomyField(): ?TaxonomyField
{
$taxonomyKey = $this->get_settings_for_display('taxonomy');
if (empty($taxonomyKey)) {
return null;
}
$taxonomyField = tdf_taxonomy_fields()->find(static function ($taxonomy) use ($taxonomyKey) {
/* @var TaxonomyField $taxonomy */
return $taxonomy->getKey() === $taxonomyKey;
});
if (!$taxonomyField instanceof TaxonomyField) {
return null;
}
return $taxonomyField;
}
}