Skip to content

Instantly share code, notes, and snippets.

@pablo-sg-pacheco
Created June 28, 2016 14:44
Show Gist options
  • Save pablo-sg-pacheco/60984cff35868ebc0b5b444463366d45 to your computer and use it in GitHub Desktop.
Save pablo-sg-pacheco/60984cff35868ebc0b5b444463366d45 to your computer and use it in GitHub Desktop.
CMB2 - Youtube video
<?php
namespace OOPFunctions\MetaBoxes\VideoCmb;
if ( !class_exists('\OOPFunctions\MetaBoxes\VideoCmb\VideoCmb') ) {
/**
* Description of VideoCmb
*
* @author Pablo Pacheco <pablo.pacheco@origgami.com.br>
*/
class VideoCmb {
private $id = 'videocmb';
/**
*
* @var Metas
*/
private $metas;
public function __construct() {
$this->setMetas(new Metas());
add_action('cmb2_admin_init', array($this, 'addCmb'));
}
public function addCmb() {
$functions = \OOPFunctions\OOPFunctions::getInstance();
/**
* Sample metabox to demonstrate each field type included
*/
$cmb_demo = new_cmb2_box(array(
'id' => $this->getId(),
'title' => __('Vídeo', 'cmb2'),
'object_types' => array($functions->getCpt()->getVideos()->getId()), // Post type
//'show_on' => array('key' => 'page-template', 'value' => 'page-templates/santo-gostinho.php'),
));
$cmb_demo->add_field(array(
'name' => __('Url do youtube', 'cmb2'),
//'desc' => __('field description (optional)', 'cmb2'),
'id' => $this->getMetas()->videourl,
'type' => 'oembed',
'sanitization_cb' => array($this, 'sanitizeVideo'),
'preview_size' => array(130, 130), // Default: array( 50, 50 )
));
}
public function sanitizeVideo( $value, $field_args, $field ) {
$postID = $field->object_id;
$videoUrl = get_post_meta($postID, $this->getMetas()->videourl, true);
if($videoUrl!=$value){
//die($videoUrl.'-'.$value);
if(!empty($value)){
$image = \OOPFunctions\Youtube\Youtube::getImageFromYoutubeUrl($value);
setThumbnailByUrl($image, $postID);
}
}
return $value;
}
function getId() {
return $this->id;
}
function getMetas() {
return $this->metas;
}
function setId( $id ) {
$this->id = $id;
}
function setMetas( Metas $metas ) {
$this->metas = $metas;
}
}
class Metas {
public $videourl = '_video_url';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment