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