| 1 |
<?php |
| 2 |
|
| 3 |
class LSD_Element extends LSD_Base |
| 4 |
{ |
| 5 |
public $key; |
| 6 |
public $label; |
| 7 |
protected $settings; |
| 8 |
|
| 9 |
/** |
| 10 |
* @var LSD_Entity_Listing object |
| 11 |
*/ |
| 12 |
public $listing; |
| 13 |
|
| 14 |
protected $pro_needed = false; |
| 15 |
protected $has_title_settings = true; |
| 16 |
protected $inline_title = false; |
| 17 |
|
| 18 |
public function __construct() |
| 19 |
{ |
| 20 |
// Listdom Settings |
| 21 |
$this->settings = LSD_Options::settings(); |
| 22 |
} |
| 23 |
|
| 24 |
public function form($data = []) |
| 25 |
{ |
| 26 |
// Disabled in Lite |
| 27 |
if ($this->isLite() && $this->pro()) return '<div class="lsd-form-row"> |
| 28 |
<div class="lsd-col-12 lsd-handler"> |
| 29 |
<input type="hidden" name="lsd[elements][' . esc_attr($this->key) . ']"> |
| 30 |
<input type="hidden" name="lsd[elements][' . esc_attr($this->key) . '][enabled]" value="0"> |
| 31 |
' . $this->missFeatureMessage(esc_html($this->label)) . ' |
| 32 |
</div> |
| 33 |
</div>'; |
| 34 |
|
| 35 |
// Third Party Fields |
| 36 |
ob_start(); |
| 37 |
do_action('lsd_element_form_options', $this->key, $data); |
| 38 |
$additional = LSD_Kses::form(ob_get_clean()); |
| 39 |
|
| 40 |
// Title Settings |
| 41 |
$title = $this->title_settings($data); |
| 42 |
|
| 43 |
// General Settings |
| 44 |
$general = $this->general_settings($data); |
| 45 |
|
| 46 |
// Has General Options |
| 47 |
$has_general_options = trim($general) || trim($additional); |
| 48 |
|
| 49 |
// Has Options |
| 50 |
$has_options = $has_general_options || trim($title); |
| 51 |
|
| 52 |
return '<div class="lsd-form-row"> |
| 53 |
<div class="lsd-col-10 lsd-handler"> |
| 54 |
<input type="hidden" name="lsd[elements][' . esc_attr($this->key) . ']"> |
| 55 |
<input type="hidden" name="lsd[elements][' . esc_attr($this->key) . '][enabled]" value="' . esc_attr($data['enabled']) . '"> |
| 56 |
' . $this->label . ' |
| 57 |
</div> |
| 58 |
<div class="lsd-col-2 lsd-actions lsd-details-page-element-toggle-status" id="lsd_actions_' . esc_attr($this->key) . '" data-key="' . esc_attr($this->key) . '"> |
| 59 |
' . ($has_options ? '<span class="lsd-toggle lsd-mr-2" data-for="#lsd_options_' . esc_attr($this->key) . '" data-all=".lsd-element-options"> |
| 60 |
<i class="lsd-icon fa fa-cog fa-lg"></i> |
| 61 |
</span>' : '') . ' |
| 62 |
<strong class="lsd-enabled ' . ($data['enabled'] ? '' : 'lsd-util-hide') . '"><i class="lsd-icon fa fa-check"></i></strong> |
| 63 |
<strong class="lsd-disabled ' . ($data['enabled'] ? 'lsd-util-hide' : '') . '"><i class="lsd-icon fa fa-minus-circle"></i></strong> |
| 64 |
</div> |
| 65 |
</div> |
| 66 |
<div class="lsd-element-options lsd-util-hide" id="lsd_options_' . esc_attr($this->key) . '"> |
| 67 |
' . ($has_general_options ? '<ul class="lsd-tab-switcher lsd-level-5-menu lsd-sub-tabs lsd-flex lsd-gap-3" id="lsd_element_option_switcher_' . esc_attr($this->key) . '" data-for=".lsd-tab-switcher-element-' . esc_attr($this->key) . '-content"> |
| 68 |
' . (trim($title) ? '<li data-tab="title-' . esc_attr($this->key) . '" class="lsd-sub-tabs-active"><a href="#">' . esc_html__('Title', 'listdom') . '</a></li>' : '') . ' |
| 69 |
<li data-tab="options-' . esc_attr($this->key) . '" ' . (trim($title) ? '' : 'class="lsd-sub-tabs-active"') . '><a href="#">' . esc_html__('Options', 'listdom') . '</a></li> |
| 70 |
</ul>' : '') . ' |
| 71 |
' . (trim($title) ? '<div class="lsd-element-option-wrapper ' . ($has_general_options ? 'lsd-tab-switcher-content lsd-tab-switcher-element-' . esc_attr($this->key) . '-content lsd-tab-switcher-content-active' : '') . '" id="lsd-tab-switcher-title-' . esc_attr($this->key) . '-content"> |
| 72 |
<div>' . $title . '</div> |
| 73 |
</div>' : '') . ' |
| 74 |
' . ($has_general_options ? '<div class="lsd-element-option-wrapper lsd-tab-switcher-content lsd-tab-switcher-element-' . esc_attr($this->key) . '-content ' . (trim($title) ? '' : 'lsd-tab-switcher-content-active') . '" id="lsd-tab-switcher-options-' . esc_attr($this->key) . '-content"> |
| 75 |
<div> |
| 76 |
' . $general . ' |
| 77 |
' . $additional . ' |
| 78 |
</div> |
| 79 |
</div>' : '') . ' |
| 80 |
</div>'; |
| 81 |
} |
| 82 |
|
| 83 |
private function title_settings(array $data): string |
| 84 |
{ |
| 85 |
// No Title Settings |
| 86 |
if (!$this->has_title_settings) return ''; |
| 87 |
|
| 88 |
// Show Title |
| 89 |
$show_title = isset($data['show_title']) && $data['show_title'] == 1; |
| 90 |
|
| 91 |
// Title Alignment |
| 92 |
$title_alignment = ''; |
| 93 |
|
| 94 |
if (!$this->inline_title) |
| 95 |
{ |
| 96 |
$title_alignment .= '<div class="lsd-title-dependent-' . esc_attr($this->key) . '-option ' . ($show_title ? '' : 'lsd-util-hide') . '"> |
| 97 |
<label class="lsd-fields-label-tiny" for="lsd_elements_' . esc_attr($this->key) . '_title_align">' . esc_html__('Title Alignment', 'listdom') . '</label> |
| 98 |
<select class="lsd-admin-input" name="lsd[elements][' . esc_attr($this->key) . '][title_align]" id="lsd_elements_' . esc_attr($this->key) . '_title_align"> |
| 99 |
<option value="">' . esc_html__('Default', 'listdom') . '</option> |
| 100 |
<option value="lsd-text-left" ' . (isset($data['title_align']) && $data['title_align'] === 'lsd-text-left' ? 'selected="selected"' : '') . '>' . esc_html__('Left', 'listdom') . '</option> |
| 101 |
<option value="lsd-text-center" ' . (isset($data['title_align']) && $data['title_align'] === 'lsd-text-center' ? 'selected="selected"' : '') . '>' . esc_html__('Center', 'listdom') . '</option> |
| 102 |
<option value="lsd-text-right" ' . (isset($data['title_align']) && $data['title_align'] === 'lsd-text-right' ? 'selected="selected"' : '') . '>' . esc_html__('Right', 'listdom') . '</option> |
| 103 |
</select> |
| 104 |
</div>'; |
| 105 |
} |
| 106 |
|
| 107 |
return '<div> |
| 108 |
<label class="lsd-fields-label-tiny" for="lsd_elements_' . esc_attr($this->key) . '_show_title">' . esc_html__('Show Title', 'listdom') . '</label> |
| 109 |
<select class="lsd-trigger-select-options lsd-admin-input" name="lsd[elements][' . esc_attr($this->key) . '][show_title]" id="lsd_elements_' . esc_attr($this->key) . '_show_title"> |
| 110 |
<option value="1" ' . ($show_title ? 'selected="selected"' : '') . ' data-lsd-show=".lsd-title-dependent-' . esc_attr($this->key) . '-option">' . esc_html__('Yes', 'listdom') . '</option> |
| 111 |
<option value="0" ' . (!$show_title ? 'selected="selected"' : '') . ' data-lsd-hide=".lsd-title-dependent-' . esc_attr($this->key) . '-option">' . esc_html__('No', 'listdom') . '</option> |
| 112 |
</select> |
| 113 |
</div> |
| 114 |
' . $title_alignment . ' |
| 115 |
<div class="lsd-title-dependent-' . esc_attr($this->key) . '-option ' . ($show_title ? '' : 'lsd-util-hide') . '"> |
| 116 |
<label class="lsd-fields-label-tiny" for="lsd_elements_' . esc_attr($this->key) . '_custom_title">' . esc_html__('Custom Title', 'listdom') . '</label> |
| 117 |
<input class="lsd-admin-input" type="text" name="lsd[elements][' . esc_attr($this->key) . '][custom_title]" id="lsd_elements_' . esc_attr($this->key) . '_custom_title" value="' . (isset($data['custom_title']) && trim($data['custom_title']) ? esc_attr($data['custom_title']) : '') . '" placeholder="' . esc_attr__('Custom Title', 'listdom') . '"> |
| 118 |
<p class="lsd-admin-description-tiny lsd-mb-0 lsd-mt-2">' . esc_html__('If a custom title is provided, it will replace the default title.', 'listdom') . '</p> |
| 119 |
</div>'; |
| 120 |
} |
| 121 |
|
| 122 |
protected function general_settings(array $data): string |
| 123 |
{ |
| 124 |
return ''; |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* @param LSD_Entity_Listing $listing |
| 129 |
*/ |
| 130 |
public function set_listing(LSD_Entity_Listing $listing) |
| 131 |
{ |
| 132 |
$this->listing = $listing; |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* @param $key |
| 137 |
* @return mixed |
| 138 |
*/ |
| 139 |
public static function instance($key) |
| 140 |
{ |
| 141 |
$element = 'LSD_Element_' . ucfirst($key); |
| 142 |
|
| 143 |
// Element Not Found! |
| 144 |
if (!class_exists($element)) return apply_filters('lsd_addon_elements', false, $key); |
| 145 |
|
| 146 |
return new $element(); |
| 147 |
} |
| 148 |
|
| 149 |
public function pro(): bool |
| 150 |
{ |
| 151 |
return $this->pro_needed; |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* @param string $content |
| 156 |
* @param LSD_Element $element |
| 157 |
* @param array $args |
| 158 |
* @return mixed|void |
| 159 |
*/ |
| 160 |
final protected function content($content, $element, $args = []) |
| 161 |
{ |
| 162 |
// Hook Name |
| 163 |
$hook = strtolower(get_called_class()) . '_content'; // e.g. lsd_element_address_content |
| 164 |
|
| 165 |
// Filter the Results |
| 166 |
return apply_filters($hook, $content, $element, $args); |
| 167 |
} |
| 168 |
} |
| 169 |
|