PluginProbe
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI / 7.9.8
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI v7.9.8
8.0.4 8.0.3 8.0.1 8.0.2 8.0.0 7.9.9.7 7.9.9.6 7.9.9.5 7.9.9.4 7.9.9.3 7.9.9.2 7.9.9.1 7.9.9 7.9.8 7.9.7 7.9.6 7.9.5 7.9.4 7.9.3 7.9.2 7.9.1 7.9.0 7.8.9 7.8.8 7.8.7 All 46 releases
gamipress / integrations / youtube / includes / shortcodes / gamipress_youtube.php

gamipress_youtube.php in GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI 7.9.8, at integrations/youtube/includes/shortcodes/gamipress_youtube.php

116 lines 4.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * GamiPress Youtube Shortcode
4 *
5 * @package GamiPress\Shortcodes\Shortcode\GamiPress_Youtube
6 * @since 1.0.0
7 */
8 // Exit if accessed directly
9 if( !defined( 'ABSPATH' ) ) exit;
10
11 /**
12 * Register the [gamipress_youtube] shortcode.
13 *
14 * @since 1.0.0
15 */
16 function gamipress_register_youtube_shortcode() {
17
18 gamipress_register_shortcode( 'gamipress_youtube', array(
19 'name' => __( 'Youtube', 'gamipress' ),
20 'description' => __( 'Render a youtube video.', 'gamipress' ),
21 'output_callback' => 'gamipress_youtube_shortcode',
22 'fields' => array(
23 'url' => array(
24 'name' => __( 'URL or ID', 'gamipress' ),
25 'tooltip' => __( 'The Youtube video URL or ID.', 'gamipress' ),
26 'label_cb' => 'cmb_tooltip_label_cb',
27 'type' => 'text',
28 ),
29 'width' => array(
30 'name' => __( 'Player width', 'gamipress' ),
31 'tooltip' => __( 'The player width (in pixels). By default, 640.', 'gamipress' ),
32 'label_cb' => 'cmb_tooltip_label_cb',
33 'type' => 'text',
34 'default' => '640',
35 ),
36 'height' => array(
37 'name' => __( 'Player height', 'gamipress' ),
38 'tooltip' => __( 'The player height (in pixels). By default, 360.', 'gamipress' ),
39 'label_cb' => 'cmb_tooltip_label_cb',
40 'type' => 'text',
41 'default' => '360',
42 ),
43 'autoplay' => array(
44 'name' => __( 'Autoplay', 'gamipress' ),
45 'tooltip' => __( 'Check this option to automatically play the video.', 'gamipress' ),
46 'label_cb' => 'cmb_tooltip_label_cb',
47 'type' => 'checkbox',
48 'classes' => 'gamipress-switch',
49 ),
50 'controls' => array(
51 'name' => __( 'Show player controls?', 'gamipress' ),
52 'tooltip' => __( 'Check this option to show player controls.', 'gamipress' ),
53 'label_cb' => 'cmb_tooltip_label_cb',
54 'type' => 'checkbox',
55 'classes' => 'gamipress-switch',
56 'default' => 'yes',
57 ),
58 ),
59 ) );
60
61 }
62 add_action( 'init', 'gamipress_register_youtube_shortcode' );
63
64 /**
65 * Youtube Shortcode.
66 *
67 * @since 1.0.0
68 *
69 * @param array $atts Shortcode attributes.
70 * @return string HTML markup.
71 */
72 function gamipress_youtube_shortcode( $atts = array() ) {
73
74 // Get the received shortcode attributes
75 $atts = shortcode_atts( array(
76 'url' => '',
77 'width' => '640',
78 'height' => '360',
79 'autoplay' => 'no',
80 'controls' => 'yes',
81 ), $atts, 'gamipress_youtube' );
82
83 $video_id = gamipress_youtube_get_video_id_from_url( $atts['url'] );
84 $thumbnail_url = GAMIPRESS_YOUTUBE_URL . 'assets/img/youtube-preview.svg';
85
86 // Show thumbnail only if is an admin preview
87 $show_thumbnail = ( defined( 'REST_REQUEST' ) && REST_REQUEST );
88
89 // Show thumbnail only for blocks
90 if( gamipress_get_renderer() !== 'block' ) {
91 $show_thumbnail = false;
92 }
93
94 if( ! empty( $video_id ) ) {
95 $thumbnail_url = "https://img.youtube.com/vi/{$video_id}/maxresdefault.jpg";
96 }
97
98 // Enqueue scripts
99 gamipress_youtube_enqueue_scripts();
100
101 ob_start(); ?>
102 <div id="<?php echo esc_attr( $video_id ); ?>" class="gamipress-youtube-video"
103 data-id="<?php echo esc_attr( $video_id ); ?>"
104 data-url="<?php echo esc_attr( $atts['url'] ); ?>"
105 data-width="<?php echo esc_attr( $atts['width'] ); ?>"
106 data-height="<?php echo esc_attr( $atts['height'] ); ?>"
107 data-autoplay="<?php echo esc_attr( $atts['autoplay'] ); ?>"
108 data-controls="<?php echo esc_attr( $atts['controls'] ); ?>"
109 ><?php if( $show_thumbnail ) : ?><img src="<?php echo esc_attr( $thumbnail_url ); ?>" width="<?php echo esc_attr( $atts['width'] ); ?>"/><?php endif; ?></div>
110 <?php $output = ob_get_clean();
111
112 // Return our rendered youtube video
113 return $output;
114
115 }
116