| 1 |
<?php |
| 2 |
namespace MasterAddons\Inc\Admin\WidgetBuilder\Controls; |
| 3 |
|
| 4 |
defined('ABSPATH') || exit; |
| 5 |
|
| 6 |
/** |
| 7 |
* URL Control |
| 8 |
* Handles Elementor URL control type |
| 9 |
* URL input control with link options (target, nofollow, custom attributes) |
| 10 |
* |
| 11 |
* Supported properties: |
| 12 |
* - label: Control label |
| 13 |
* - name: Field name (used for control key) |
| 14 |
* - description: Description below field |
| 15 |
* - placeholder: Placeholder text (default: "Paste URL or type") |
| 16 |
* - default: Default URL array ['url' => '', 'is_external' => '', 'nofollow' => ''] |
| 17 |
* - options: Array of enabled options ['url', 'is_external', 'nofollow', 'custom_attributes'] |
| 18 |
* - autocomplete: Enable search functionality (boolean, default: true) |
| 19 |
* - show_external: Deprecated - use 'options' instead |
| 20 |
* - show_label: Show/hide label |
| 21 |
* - label_block: Display label on separate line |
| 22 |
* |
| 23 |
* Common settings (handled by base class): |
| 24 |
* - dynamic: Enable dynamic content support |
| 25 |
* - separator: Control separator position |
| 26 |
* - condition: Conditional display logic |
| 27 |
*/ |
| 28 |
class Url extends Control_Base { |
| 29 |
|
| 30 |
public function get_type() { |
| 31 |
return 'URL'; |
| 32 |
} |
| 33 |
|
| 34 |
public function build($control_key, $field) { |
| 35 |
// Ensure proper default structure for URL control |
| 36 |
if (!isset($field['default']) || (is_string($field['default']) && $field['default'] === '')) { |
| 37 |
$field['default'] = [ |
| 38 |
'url' => '', |
| 39 |
'is_external' => '', |
| 40 |
'nofollow' => '', |
| 41 |
]; |
| 42 |
} |
| 43 |
|
| 44 |
$label = !empty($field['label']) ? $field['label'] : 'Link'; |
| 45 |
|
| 46 |
// Check if responsive control |
| 47 |
$is_responsive = isset($field['responsive']) && $field['responsive']; |
| 48 |
|
| 49 |
// Build control header |
| 50 |
$content = $this->build_control_header($control_key, $label, 'URL', $is_responsive); |
| 51 |
|
| 52 |
// Add URL-specific properties FIRST |
| 53 |
$content .= $this->build_url_properties($field); |
| 54 |
|
| 55 |
// Add common properties |
| 56 |
$content .= $this->build_common_properties($field); |
| 57 |
|
| 58 |
// Close control |
| 59 |
$content .= $this->build_control_footer(); |
| 60 |
|
| 61 |
return $content; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Build URL-specific properties |
| 66 |
* |
| 67 |
* @param array $field Field configuration |
| 68 |
* @return string |
| 69 |
*/ |
| 70 |
protected function build_url_properties($field) { |
| 71 |
$content = ''; |
| 72 |
|
| 73 |
// Options - Define which fields to show (url, is_external, nofollow, custom_attributes) |
| 74 |
if (!empty($field['options']) && is_array($field['options'])) { |
| 75 |
$content .= "\t\t\t\t'options' => ["; |
| 76 |
$content .= "'" . implode("', '", array_map('esc_js', $field['options'])) . "'"; |
| 77 |
$content .= "],\n"; |
| 78 |
} else { |
| 79 |
// Default options if not specified |
| 80 |
$content .= "\t\t\t\t'options' => ['url', 'is_external', 'nofollow'],\n"; |
| 81 |
} |
| 82 |
|
| 83 |
// Autocomplete - Enable search functionality |
| 84 |
if (isset($field['autocomplete'])) { |
| 85 |
$content .= "\t\t\t\t'autocomplete' => " . ($field['autocomplete'] ? 'true' : 'false') . ",\n"; |
| 86 |
} |
| 87 |
|
| 88 |
// Show external checkbox (deprecated, kept for backwards compatibility) |
| 89 |
if (isset($field['show_external']) && !$field['show_external']) { |
| 90 |
$content .= "\t\t\t\t'show_external' => false,\n"; |
| 91 |
} |
| 92 |
|
| 93 |
return $content; |
| 94 |
} |
| 95 |
|
| 96 |
protected function default_label() { |
| 97 |
return 'Link'; |
| 98 |
} |
| 99 |
|
| 100 |
public function get_config($control_key, $field) { |
| 101 |
// Ensure proper default structure for the URL control (mirrors build()). |
| 102 |
if (!isset($field['default']) || (is_string($field['default']) && $field['default'] === '')) { |
| 103 |
$field['default'] = [ |
| 104 |
'url' => '', |
| 105 |
'is_external' => '', |
| 106 |
'nofollow' => '', |
| 107 |
]; |
| 108 |
} |
| 109 |
return parent::get_config($control_key, $field); |
| 110 |
} |
| 111 |
|
| 112 |
protected function get_type_specific_config($field) { |
| 113 |
$config = []; |
| 114 |
|
| 115 |
if (!empty($field['options']) && is_array($field['options'])) { |
| 116 |
$config['options'] = array_values($field['options']); |
| 117 |
} else { |
| 118 |
$config['options'] = ['url', 'is_external', 'nofollow']; |
| 119 |
} |
| 120 |
|
| 121 |
if (isset($field['autocomplete'])) { |
| 122 |
$config['autocomplete'] = (bool) $field['autocomplete']; |
| 123 |
} |
| 124 |
|
| 125 |
if (isset($field['show_external']) && !$field['show_external']) { |
| 126 |
$config['show_external'] = false; |
| 127 |
} |
| 128 |
|
| 129 |
return $config; |
| 130 |
} |
| 131 |
} |
| 132 |
|