audio.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. <?php
  2. namespace Elementor;
  3. use Elementor\Modules\DynamicTags\Module as TagsModule;
  4. if ( ! defined( 'ABSPATH' ) ) {
  5. exit; // Exit if accessed directly.
  6. }
  7. /**
  8. * Elementor audio widget.
  9. *
  10. * Elementor widget that displays an audio player.
  11. *
  12. * @since 1.0.0
  13. */
  14. class Widget_Audio extends Widget_Base {
  15. /**
  16. * Current instance.
  17. *
  18. * @access protected
  19. *
  20. * @var array
  21. */
  22. protected $_current_instance = [];
  23. /**
  24. * Get widget name.
  25. *
  26. * Retrieve audio widget name.
  27. *
  28. * @since 1.0.0
  29. * @access public
  30. *
  31. * @return string Widget name.
  32. */
  33. public function get_name() {
  34. return 'audio';
  35. }
  36. /**
  37. * Get widget title.
  38. *
  39. * Retrieve audio widget title.
  40. *
  41. * @since 1.0.0
  42. * @access public
  43. *
  44. * @return string Widget title.
  45. */
  46. public function get_title() {
  47. return __( 'SoundCloud', 'elementor' );
  48. }
  49. /**
  50. * Get widget icon.
  51. *
  52. * Retrieve audio widget icon.
  53. *
  54. * @since 1.0.0
  55. * @access public
  56. *
  57. * @return string Widget icon.
  58. */
  59. public function get_icon() {
  60. return 'eicon-headphones';
  61. }
  62. /**
  63. * Get widget keywords.
  64. *
  65. * Retrieve the list of keywords the widget belongs to.
  66. *
  67. * @since 2.1.0
  68. * @access public
  69. *
  70. * @return array Widget keywords.
  71. */
  72. public function get_keywords() {
  73. return [ 'audio', 'player', 'soundcloud', 'embed' ];
  74. }
  75. /**
  76. * Register audio widget controls.
  77. *
  78. * Adds different input fields to allow the user to change and customize the widget settings.
  79. *
  80. * @since 1.0.0
  81. * @access protected
  82. */
  83. protected function _register_controls() {
  84. $this->start_controls_section(
  85. 'section_audio',
  86. [
  87. 'label' => __( 'SoundCloud', 'elementor' ),
  88. ]
  89. );
  90. $this->add_control(
  91. 'link',
  92. [
  93. 'label' => __( 'Link', 'elementor' ),
  94. 'type' => Controls_Manager::URL,
  95. 'dynamic' => [
  96. 'active' => true,
  97. 'categories' => [
  98. TagsModule::POST_META_CATEGORY,
  99. TagsModule::URL_CATEGORY,
  100. ],
  101. ],
  102. 'default' => [
  103. 'url' => 'https://soundcloud.com/shchxango/john-coltrane-1963-my-favorite',
  104. ],
  105. 'show_external' => false,
  106. ]
  107. );
  108. $this->add_control(
  109. 'visual',
  110. [
  111. 'label' => __( 'Visual Player', 'elementor' ),
  112. 'type' => Controls_Manager::SELECT,
  113. 'default' => 'no',
  114. 'options' => [
  115. 'yes' => __( 'Yes', 'elementor' ),
  116. 'no' => __( 'No', 'elementor' ),
  117. ],
  118. ]
  119. );
  120. $this->add_control(
  121. 'sc_options',
  122. [
  123. 'label' => __( 'Additional Options', 'elementor' ),
  124. 'type' => Controls_Manager::HEADING,
  125. 'separator' => 'before',
  126. ]
  127. );
  128. $this->add_control(
  129. 'sc_auto_play',
  130. [
  131. 'label' => __( 'Autoplay', 'elementor' ),
  132. 'type' => Controls_Manager::SWITCHER,
  133. ]
  134. );
  135. $this->add_control(
  136. 'sc_buying',
  137. [
  138. 'label' => __( 'Buy Button', 'elementor' ),
  139. 'type' => Controls_Manager::SWITCHER,
  140. 'label_off' => __( 'Hide', 'elementor' ),
  141. 'label_on' => __( 'Show', 'elementor' ),
  142. 'default' => 'yes',
  143. ]
  144. );
  145. $this->add_control(
  146. 'sc_liking',
  147. [
  148. 'label' => __( 'Like Button', 'elementor' ),
  149. 'type' => Controls_Manager::SWITCHER,
  150. 'label_off' => __( 'Hide', 'elementor' ),
  151. 'label_on' => __( 'Show', 'elementor' ),
  152. 'default' => 'yes',
  153. ]
  154. );
  155. $this->add_control(
  156. 'sc_download',
  157. [
  158. 'label' => __( 'Download Button', 'elementor' ),
  159. 'type' => Controls_Manager::SWITCHER,
  160. 'label_off' => __( 'Hide', 'elementor' ),
  161. 'label_on' => __( 'Show', 'elementor' ),
  162. 'default' => 'yes',
  163. ]
  164. );
  165. $this->add_control(
  166. 'sc_show_artwork',
  167. [
  168. 'label' => __( 'Artwork', 'elementor' ),
  169. 'type' => Controls_Manager::SWITCHER,
  170. 'label_off' => __( 'Hide', 'elementor' ),
  171. 'label_on' => __( 'Show', 'elementor' ),
  172. 'default' => 'yes',
  173. 'condition' => [
  174. 'visual' => 'no',
  175. ],
  176. ]
  177. );
  178. $this->add_control(
  179. 'sc_sharing',
  180. [
  181. 'label' => __( 'Share Button', 'elementor' ),
  182. 'type' => Controls_Manager::SWITCHER,
  183. 'label_off' => __( 'Hide', 'elementor' ),
  184. 'label_on' => __( 'Show', 'elementor' ),
  185. 'default' => 'yes',
  186. ]
  187. );
  188. $this->add_control(
  189. 'sc_show_comments',
  190. [
  191. 'label' => __( 'Comments', 'elementor' ),
  192. 'type' => Controls_Manager::SWITCHER,
  193. 'label_off' => __( 'Hide', 'elementor' ),
  194. 'label_on' => __( 'Show', 'elementor' ),
  195. 'default' => 'yes',
  196. ]
  197. );
  198. $this->add_control(
  199. 'sc_show_playcount',
  200. [
  201. 'label' => __( 'Play Counts', 'elementor' ),
  202. 'type' => Controls_Manager::SWITCHER,
  203. 'label_off' => __( 'Hide', 'elementor' ),
  204. 'label_on' => __( 'Show', 'elementor' ),
  205. 'default' => 'yes',
  206. ]
  207. );
  208. $this->add_control(
  209. 'sc_show_user',
  210. [
  211. 'label' => __( 'Username', 'elementor' ),
  212. 'type' => Controls_Manager::SWITCHER,
  213. 'label_off' => __( 'Hide', 'elementor' ),
  214. 'label_on' => __( 'Show', 'elementor' ),
  215. 'default' => 'yes',
  216. ]
  217. );
  218. $this->add_control(
  219. 'sc_color',
  220. [
  221. 'label' => __( 'Controls Color', 'elementor' ),
  222. 'type' => Controls_Manager::COLOR,
  223. ]
  224. );
  225. $this->add_control(
  226. 'view',
  227. [
  228. 'label' => __( 'View', 'elementor' ),
  229. 'type' => Controls_Manager::HIDDEN,
  230. 'default' => 'soundcloud',
  231. ]
  232. );
  233. $this->end_controls_section();
  234. }
  235. /**
  236. * Render audio widget output on the frontend.
  237. *
  238. * Written in PHP and used to generate the final HTML.
  239. *
  240. * @since 1.0.0
  241. * @access protected
  242. */
  243. protected function render() {
  244. $settings = $this->get_settings_for_display();
  245. if ( empty( $settings['link'] ) ) {
  246. return;
  247. }
  248. $this->_current_instance = $settings;
  249. add_filter( 'oembed_result', [ $this, 'filter_oembed_result' ], 50, 3 );
  250. $video_html = wp_oembed_get( $settings['link']['url'], wp_embed_defaults() );
  251. remove_filter( 'oembed_result', [ $this, 'filter_oembed_result' ], 50 );
  252. if ( $video_html ) : ?>
  253. <div class="elementor-soundcloud-wrapper">
  254. <?php echo $video_html; ?>
  255. </div>
  256. <?php
  257. endif;
  258. }
  259. /**
  260. * Filter audio widget oEmbed results.
  261. *
  262. * Written in PHP and used to generate the final HTML.
  263. *
  264. * @since 1.0.0
  265. * @access public
  266. *
  267. * @param string $html The HTML returned by the oEmbed provider.
  268. *
  269. * @return string Filtered audio widget oEmbed HTML.
  270. */
  271. public function filter_oembed_result( $html ) {
  272. $param_keys = [
  273. 'auto_play',
  274. 'buying',
  275. 'liking',
  276. 'download',
  277. 'sharing',
  278. 'show_comments',
  279. 'show_playcount',
  280. 'show_user',
  281. 'show_artwork',
  282. ];
  283. $params = [];
  284. foreach ( $param_keys as $param_key ) {
  285. $params[ $param_key ] = 'yes' === $this->_current_instance[ 'sc_' . $param_key ] ? 'true' : 'false';
  286. }
  287. $params['color'] = str_replace( '#', '', $this->_current_instance['sc_color'] );
  288. preg_match( '/<iframe.*src=\"(.*)\".*><\/iframe>/isU', $html, $matches );
  289. $url = esc_url( add_query_arg( $params, $matches[1] ) );
  290. $visual = 'yes' === $this->_current_instance['visual'] ? 'true' : 'false';
  291. $html = str_replace( [ $matches[1], 'visual=true' ], [ $url, 'visual=' . $visual ], $html );
  292. if ( 'false' === $visual ) {
  293. $html = str_replace( 'height="400"', 'height="200"', $html );
  294. }
  295. return $html;
  296. }
  297. /**
  298. * Render audio widget output in the editor.
  299. *
  300. * Written as a Backbone JavaScript template and used to generate the live preview.
  301. *
  302. * @since 1.0.0
  303. * @access protected
  304. */
  305. protected function _content_template() {}
  306. }