| 1 |
<?php |
| 2 |
|
| 3 |
namespace UiCoreElements\Controls; |
| 4 |
|
| 5 |
use Elementor\Controls_Manager; |
| 6 |
use Elementor\Frontend; |
| 7 |
use Elementor\Embed; |
| 8 |
use Elementor\Plugin; |
| 9 |
|
| 10 |
defined('ABSPATH') || exit(); |
| 11 |
|
| 12 |
/** |
| 13 |
* Update Elementor Button Control by injecting a new lightbox option. |
| 14 |
* Functions were extracted and updated from the 'lightbox' dinamic tag module |
| 15 |
* |
| 16 |
* @since 1.0.15 |
| 17 |
*/ |
| 18 |
|
| 19 |
class Lightbox_Button |
| 20 |
{ |
| 21 |
|
| 22 |
public function __construct() |
| 23 |
{ |
| 24 |
add_action('elementor/element/button/section_button/before_section_end', [$this, 'update_controls'], 10, 2); |
| 25 |
add_action('elementor/widget/render_content', [$this, 'render_widget'], 10, 2); // TODO: try targeting button specifically |
| 26 |
} |
| 27 |
|
| 28 |
public function update_controls($element) |
| 29 |
{ |
| 30 |
|
| 31 |
$element->update_control( |
| 32 |
'link', |
| 33 |
[ |
| 34 |
'condition' => [ |
| 35 |
'button_lightbox!' => 'yes', |
| 36 |
], |
| 37 |
] |
| 38 |
); |
| 39 |
|
| 40 |
$element->start_injection([ |
| 41 |
'of' => 'link', |
| 42 |
'at' => 'after', |
| 43 |
]); |
| 44 |
$element->add_control( |
| 45 |
'button_lightbox', |
| 46 |
[ |
| 47 |
'label' => UICORE_ELEMENTS_BADGE . esc_html__('Lightbox', 'uicore-elements'), |
| 48 |
'type' => Controls_Manager::SWITCHER, |
| 49 |
], |
| 50 |
); |
| 51 |
$element->add_control( |
| 52 |
'content_type', |
| 53 |
[ |
| 54 |
'label' => esc_html__('Type', 'uicore-elements'), |
| 55 |
'type' => Controls_Manager::CHOOSE, |
| 56 |
'options' => [ |
| 57 |
'video' => [ |
| 58 |
'title' => esc_html__('Video', 'uicore-elements'), |
| 59 |
'icon' => 'eicon-video-camera', |
| 60 |
], |
| 61 |
'image' => [ |
| 62 |
'title' => esc_html__('Image', 'uicore-elements'), |
| 63 |
'icon' => 'eicon-image-bold', |
| 64 |
], |
| 65 |
], |
| 66 |
'condition' => [ |
| 67 |
'button_lightbox' => 'yes', |
| 68 |
], |
| 69 |
] |
| 70 |
); |
| 71 |
$element->add_control( |
| 72 |
'image', |
| 73 |
[ |
| 74 |
'label' => esc_html__('Image', 'uicore-elements'), |
| 75 |
'type' => Controls_Manager::MEDIA, |
| 76 |
'condition' => [ |
| 77 |
'content_type' => 'image', |
| 78 |
'button_lightbox' => 'yes', |
| 79 |
], |
| 80 |
] |
| 81 |
); |
| 82 |
$element->add_control( |
| 83 |
'video_url', |
| 84 |
[ |
| 85 |
'label' => esc_html__('Video URL', 'uicore-elements'), |
| 86 |
'type' => Controls_Manager::TEXT, |
| 87 |
'condition' => [ |
| 88 |
'content_type' => 'video', |
| 89 |
'button_lightbox' => 'yes', |
| 90 |
], |
| 91 |
'ai' => [ |
| 92 |
'active' => false, |
| 93 |
], |
| 94 |
] |
| 95 |
); |
| 96 |
$element->end_injection(); |
| 97 |
} |
| 98 |
|
| 99 |
private function get_image_settings($settings) |
| 100 |
{ |
| 101 |
$image_settings = [ |
| 102 |
'url' => $settings['image']['url'], |
| 103 |
'type' => 'image', |
| 104 |
]; |
| 105 |
|
| 106 |
$image_id = $settings['image']['id']; |
| 107 |
|
| 108 |
if ($image_id) { |
| 109 |
$lightbox_image_attributes = Plugin::instance()->images_manager->get_lightbox_image_attributes($image_id); |
| 110 |
$image_settings = array_merge($image_settings, $lightbox_image_attributes); |
| 111 |
} |
| 112 |
|
| 113 |
return $image_settings; |
| 114 |
} |
| 115 |
|
| 116 |
private function get_video_settings($settings) |
| 117 |
{ |
| 118 |
$video_properties = Embed::get_video_properties($settings['video_url']); |
| 119 |
$video_url = null; |
| 120 |
if (! $video_properties) { |
| 121 |
$video_type = 'hosted'; |
| 122 |
$video_url = $settings['video_url']; |
| 123 |
} else { |
| 124 |
$video_type = $video_properties['provider']; |
| 125 |
$video_url = Embed::get_embed_url($settings['video_url']); |
| 126 |
} |
| 127 |
|
| 128 |
if (null === $video_url) { |
| 129 |
return ''; |
| 130 |
} |
| 131 |
|
| 132 |
return [ |
| 133 |
'type' => 'video', |
| 134 |
'videoType' => $video_type, |
| 135 |
'url' => $video_url, |
| 136 |
]; |
| 137 |
} |
| 138 |
|
| 139 |
public function render_widget($widget_content, $widget) |
| 140 |
{ |
| 141 |
|
| 142 |
// TODO: Discover how to target button widget specifically since this fallback at all widgets is not good |
| 143 |
if ('button' !== $widget->get_name()) { |
| 144 |
return $widget_content; |
| 145 |
} |
| 146 |
|
| 147 |
$settings = $widget->get_settings(); |
| 148 |
$value = []; |
| 149 |
|
| 150 |
if ($settings['button_lightbox'] !== 'yes') { |
| 151 |
return $widget_content; |
| 152 |
} |
| 153 |
|
| 154 |
// Get proper content settings |
| 155 |
if ('image' === $settings['content_type'] && $settings['image']) { |
| 156 |
$value = $this->get_image_settings($settings); |
| 157 |
} elseif ('video' === $settings['content_type'] && $settings['video_url']) { |
| 158 |
$value = $this->get_video_settings($settings); |
| 159 |
} |
| 160 |
|
| 161 |
if (! $value) { |
| 162 |
return $widget_content; |
| 163 |
} |
| 164 |
|
| 165 |
// Create the lightbox URL hash |
| 166 |
$url = Frontend::instance()->create_action_hash('lightbox', $value); |
| 167 |
|
| 168 |
// Inject URL to markup |
| 169 |
$widget_content = str_replace( |
| 170 |
'<a ', |
| 171 |
'<a href="' . esc_url($url) . '" ', |
| 172 |
$widget_content |
| 173 |
); |
| 174 |
|
| 175 |
return $widget_content; |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
// Initialize the class |
| 180 |
new Lightbox_Button(); |
| 181 |
|