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