ReusableVideoWidget.php
218 lines
| 1 | <?php |
| 2 | |
| 3 | namespace PrestoPlayer\Integrations\Elementor; |
| 4 | |
| 5 | use Elementor\Widget_Base; |
| 6 | use Elementor\Controls_Manager; |
| 7 | use PrestoPlayer\WPackio\Enqueue; |
| 8 | use PrestoPlayer\Models\ReusableVideo; |
| 9 | use Elementor\Modules\DynamicTags\Module as TagsModule; |
| 10 | |
| 11 | if (!defined('ABSPATH')) { |
| 12 | exit; // Exit if accessed directly. |
| 13 | } |
| 14 | |
| 15 | /** |
| 16 | * presto-player video widget. |
| 17 | * |
| 18 | * presto-player widget that displays a video player. |
| 19 | * |
| 20 | * @since 1.0.0 |
| 21 | */ |
| 22 | class ReusableVideoWidget extends Widget_Base |
| 23 | { |
| 24 | /** |
| 25 | * Get widget name. |
| 26 | * |
| 27 | * Retrieve video widget name. |
| 28 | * |
| 29 | * @since 1.0.0 |
| 30 | * @access public |
| 31 | * |
| 32 | * @return string Widget name. |
| 33 | */ |
| 34 | public function get_name() |
| 35 | { |
| 36 | return 'presto_video'; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Get widget title. |
| 41 | * |
| 42 | * Retrieve video widget title. |
| 43 | * |
| 44 | * @since 1.0.0 |
| 45 | * @access public |
| 46 | * |
| 47 | * @return string Widget title. |
| 48 | */ |
| 49 | public function get_title() |
| 50 | { |
| 51 | return __('Presto Player Media', 'presto-player'); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Get widget icon. |
| 56 | * |
| 57 | * Retrieve video widget icon. |
| 58 | * |
| 59 | * @since 1.0.0 |
| 60 | * @access public |
| 61 | * |
| 62 | * @return string Widget icon. |
| 63 | */ |
| 64 | public function get_icon() |
| 65 | { |
| 66 | return 'eicon-youtube'; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Get widget categories. |
| 71 | * |
| 72 | * Retrieve the list of categories the video widget belongs to. |
| 73 | * |
| 74 | * Used to determine where to display the widget in the editor. |
| 75 | * |
| 76 | * @since 2.0.0 |
| 77 | * @access public |
| 78 | * |
| 79 | * @return array Widget categories. |
| 80 | */ |
| 81 | public function get_categories() |
| 82 | { |
| 83 | return ['basic']; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Get widget keywords. |
| 88 | * |
| 89 | * Retrieve the list of keywords the widget belongs to. |
| 90 | * |
| 91 | * @since 2.1.0 |
| 92 | * @access public |
| 93 | * |
| 94 | * @return array Widget keywords. |
| 95 | */ |
| 96 | public function get_keywords() |
| 97 | { |
| 98 | return ['video', 'audio', 'embed', 'youtube', 'vimeo']; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Register video widget controls. |
| 103 | * |
| 104 | * Adds different input fields to allow the user to change and customize the widget settings. |
| 105 | * |
| 106 | * @since 1.0.0 |
| 107 | * @access protected |
| 108 | */ |
| 109 | protected function _register_controls() |
| 110 | { |
| 111 | $this->start_controls_section( |
| 112 | 'section_video', |
| 113 | [ |
| 114 | 'label' => __('Media', 'presto-player'), |
| 115 | ] |
| 116 | ); |
| 117 | |
| 118 | |
| 119 | $options = $this->get_videos_options(); |
| 120 | $this->add_control( |
| 121 | 'video_block', |
| 122 | [ |
| 123 | 'label' => __('Media Hub Item', 'presto-player'), |
| 124 | 'type' => \Elementor\Controls_Manager::SELECT2, |
| 125 | 'options' => $options, |
| 126 | 'default' => '-1' |
| 127 | ] |
| 128 | ); |
| 129 | |
| 130 | $this->add_control( |
| 131 | 'edit_video', |
| 132 | [ |
| 133 | 'label' => __('Edit Media', 'plugin-name'), |
| 134 | 'type' => \Elementor\Controls_Manager::BUTTON, |
| 135 | 'text' => __('Edit', 'plugin-domain'), |
| 136 | 'event' => 'presto:video:edit', |
| 137 | 'condition' => array( |
| 138 | 'video_block!' => '-1', |
| 139 | ), |
| 140 | ] |
| 141 | ); |
| 142 | |
| 143 | $this->add_control( |
| 144 | 'create_video', |
| 145 | [ |
| 146 | 'label' => __('Create Media', 'plugin-name'), |
| 147 | 'separator' => 'before', |
| 148 | 'classes' => 'testclass', |
| 149 | 'type' => \Elementor\Controls_Manager::BUTTON, |
| 150 | 'text' => __('Create', 'plugin-domain'), |
| 151 | 'event' => 'presto:video:create', |
| 152 | ] |
| 153 | ); |
| 154 | |
| 155 | |
| 156 | $this->add_control( |
| 157 | 'url_override', |
| 158 | [ |
| 159 | 'label' => __('Dynamic URL Override', 'presto-player'), |
| 160 | 'separator' => 'before', |
| 161 | 'type' => Controls_Manager::TEXT, |
| 162 | 'dynamic' => [ |
| 163 | 'active' => true, |
| 164 | 'categories' => [ |
| 165 | TagsModule::POST_META_CATEGORY, |
| 166 | TagsModule::URL_CATEGORY, |
| 167 | ], |
| 168 | ], |
| 169 | 'placeholder' => __('Enter a url override', 'presto-player'), |
| 170 | 'default' => '', |
| 171 | 'label_block' => true, |
| 172 | ] |
| 173 | ); |
| 174 | |
| 175 | $this->end_controls_section(); |
| 176 | } |
| 177 | |
| 178 | public function get_videos_options() |
| 179 | { |
| 180 | $videos = (new ReusableVideo())->fetch(); |
| 181 | $options = []; |
| 182 | foreach ($videos as $video) { |
| 183 | $options[$video->ID] = sanitize_text_field($video->post_title); |
| 184 | } |
| 185 | return $options; |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Render video widget output on the frontend. |
| 190 | * |
| 191 | * Written in PHP and used to generate the final HTML. |
| 192 | * |
| 193 | * @since 1.0.0 |
| 194 | * @access protected |
| 195 | */ |
| 196 | protected function render() |
| 197 | { |
| 198 | global $load_presto_js; |
| 199 | $load_presto_js = true; |
| 200 | $settings = $this->get_settings_for_display(); |
| 201 | // For backward compatibility. |
| 202 | $video_url = ( '-1' !== $settings['video_block'] ) ? $settings['video_block'] : '0'; |
| 203 | $video = new ReusableVideo( $video_url ); |
| 204 | $overrides = []; |
| 205 | if ($settings['url_override']) { |
| 206 | $overrides['src'] = $settings['url_override']; |
| 207 | } |
| 208 | $render = $video->renderBlock($overrides); |
| 209 | if ($render) { |
| 210 | echo $render; |
| 211 | return; |
| 212 | } |
| 213 | |
| 214 | $video = (new ReusableVideo())->first(); |
| 215 | echo $video ? $video->renderBlock($overrides) : ''; |
| 216 | } |
| 217 | } |
| 218 |