| 1 |
<?php |
| 2 |
/** |
| 3 |
* GamiPress Vimeo Shortcode |
| 4 |
* |
| 5 |
* @package GamiPress\Shortcodes\Shortcode\GamiPress_Vimeo |
| 6 |
* @since 1.0.0 |
| 7 |
*/ |
| 8 |
// Exit if accessed directly |
| 9 |
if( !defined( 'ABSPATH' ) ) exit; |
| 10 |
|
| 11 |
/** |
| 12 |
* Register the [gamipress_vimeo] shortcode. |
| 13 |
* |
| 14 |
* @since 1.0.0 |
| 15 |
*/ |
| 16 |
function gamipress_register_vimeo_shortcode() { |
| 17 |
|
| 18 |
gamipress_register_shortcode( 'gamipress_vimeo', array( |
| 19 |
'name' => __( 'Vimeo', 'gamipress' ), |
| 20 |
'description' => __( 'Render a vimeo video.', 'gamipress' ), |
| 21 |
'output_callback' => 'gamipress_vimeo_shortcode', |
| 22 |
'fields' => array( |
| 23 |
'url' => array( |
| 24 |
'name' => __( 'URL or ID', 'gamipress' ), |
| 25 |
'tooltip' => __( 'The Vimeo 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 |
'from_url' => array( |
| 44 |
'name' => __( 'Load video from URL', 'gamipress' ), |
| 45 |
'tooltip' => __( 'By default, video is loaded from ID. Check this option if video is private or if you can not get it loaded correctly.', 'gamipress' ), |
| 46 |
'label_cb' => 'cmb_tooltip_label_cb', |
| 47 |
'type' => 'checkbox', |
| 48 |
'classes' => 'gamipress-switch', |
| 49 |
), |
| 50 |
), |
| 51 |
) ); |
| 52 |
|
| 53 |
} |
| 54 |
add_action( 'init', 'gamipress_register_vimeo_shortcode' ); |
| 55 |
|
| 56 |
/** |
| 57 |
* Vimeo Shortcode. |
| 58 |
* |
| 59 |
* @since 1.0.0 |
| 60 |
* |
| 61 |
* @param array $atts Shortcode attributes. |
| 62 |
* @return string HTML markup. |
| 63 |
*/ |
| 64 |
function gamipress_vimeo_shortcode( $atts = array() ) { |
| 65 |
|
| 66 |
// Get the received shortcode attributes |
| 67 |
$atts = shortcode_atts( array( |
| 68 |
'url' => '', |
| 69 |
'width' => '640', |
| 70 |
'height' => '360', |
| 71 |
'from_url' => 'no', |
| 72 |
), $atts, 'gamipress_vimeo' ); |
| 73 |
|
| 74 |
$video_id = gamipress_vimeo_get_video_id_from_url( $atts['url'] ); |
| 75 |
$thumbnail_url = GAMIPRESS_VIMEO_URL . 'assets/img/vimeo-preview.svg'; |
| 76 |
|
| 77 |
// Show thumbnail only if is an admin preview |
| 78 |
$show_thumbnail = ( defined( 'REST_REQUEST' ) && REST_REQUEST ); |
| 79 |
|
| 80 |
// Show thumbnail only for blocks |
| 81 |
if( gamipress_get_renderer() !== 'block' ) { |
| 82 |
$show_thumbnail = false; |
| 83 |
} |
| 84 |
|
| 85 |
if( ! empty( $video_id ) && $show_thumbnail ) { |
| 86 |
$response = wp_remote_get( "http://vimeo.com/api/v2/video/{$video_id}.json" ); |
| 87 |
$response_body = wp_remote_retrieve_body( $response ); |
| 88 |
|
| 89 |
$data = json_decode( $response_body, true ); |
| 90 |
|
| 91 |
if( isset( $data[0] ) && isset( $data[0]['thumbnail_large'] ) ) { |
| 92 |
$thumbnail_url = $data[0]['thumbnail_large']; |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
// Enqueue scripts |
| 97 |
gamipress_vimeo_enqueue_scripts(); |
| 98 |
|
| 99 |
ob_start(); ?> |
| 100 |
<div id="<?php echo esc_attr( $video_id ); ?>" class="gamipress-vimeo-video" |
| 101 |
data-id="<?php echo esc_attr( $video_id ); ?>" |
| 102 |
data-url="<?php echo esc_attr( $atts['url'] ); ?>" |
| 103 |
data-width="<?php echo esc_attr( $atts['width'] ); ?>" |
| 104 |
data-height="<?php echo esc_attr( $atts['height'] ); ?>" |
| 105 |
data-from-url="<?php echo esc_attr( $atts['from_url'] ); ?>" |
| 106 |
><?php if( $show_thumbnail ) : ?><img src="<?php echo esc_attr( $thumbnail_url ); ?>" width="<?php echo esc_attr( $atts['width'] ); ?>"/><?php endif; ?></div> |
| 107 |
<?php $output = ob_get_clean(); |
| 108 |
|
| 109 |
// Return our rendered vimeo video |
| 110 |
return $output; |
| 111 |
|
| 112 |
} |
| 113 |
|