Skip to content

Instantly share code, notes, and snippets.

@pablo-sg-pacheco
Last active April 28, 2016 18:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pablo-sg-pacheco/1b840ebcb2b99b4a42cad9bfc3aed6d1 to your computer and use it in GitHub Desktop.
Save pablo-sg-pacheco/1b840ebcb2b99b4a42cad9bfc3aed6d1 to your computer and use it in GitHub Desktop.
CMB2 - Generic Box - Group field (image, title, description, link, target, label)
<?php
namespace OOPFunctions\MetaBoxes\GenericBox;
if ( !class_exists('\OOPFunctions\MetaBoxes\GenericBox\GenericBox') ) {
/**
* Description of GenericBox
*
* @author Pablo Pacheco <pablo.pacheco@origgami.com.br>
*/
class GenericBox {
//put your code here
private $id = 'genericbox';
/**
*
* @var Metas
*/
private $metas;
public function __construct() {
$this->setMetas(new Metas());
add_action('cmb2_admin_init', array($this, 'addCmb'));
}
public function addCmb() {
/**
* Repeatable Field Groups
*/
$cmb_group = new_cmb2_box(array(
'id' => $this->getId(),
'title' => __('Boxes', 'cmb2'),
'object_types' => array('page',),
//'show_on' => array( 'key' => 'page-template', 'value' => 'page-templates/home.php' ),
));
// $group_field_id is the field id string, so in this case: $prefix . 'demo'
$group_field_id = $cmb_group->add_field(array(
'id' => $this->getMetas()->getBoxesMetas()->id,
'type' => 'group',
//'description' => __('Generates reusable form entries', 'cmb2'),
'options' => array(
'group_title' => __('Box {#}', 'cmb2'), // {#} gets replaced by row number
'add_button' => __('Adicionar box', 'cmb2'),
'remove_button' => __('Remover box', 'cmb2'),
'sortable' => true, // beta
// 'closed' => true, // true to have the groups closed by default
),
));
$cmb_group->add_group_field($group_field_id, array(
'name' => __('Imagem', 'cmb2'),
'id' => $this->getMetas()->getBoxesMetas()->image,
'type' => 'file',
'options' => array(
'url' => false, // Hide the text input for the url
),
));
$cmb_group->add_group_field($group_field_id, array(
'name' => __('Título', 'cmb2'),
'id' => $this->getMetas()->getBoxesMetas()->title,
'type' => 'text',
'attributes'=>array('style'=>'width:99%')
));
$cmb_group->add_group_field($group_field_id, array(
'name' => __('Descrição', 'cmb2'),
'id' => $this->getMetas()->getBoxesMetas()->description,
'type'=>'textarea'
/*'type' => 'wysiwyg',
'options' => array(
'textarea_rows' => 7, // rows="..."
'wpautop' => true, // use wpautop?
'media_buttons' => false, // show insert/upload button(s)
'teeny' => true, // output the minimal editor config used in Press This
'dfw' => false, // replace the default fullscreen with DFW (needs specific css)
'tinymce' => true, // load TinyMCE, can be used to pass settings directly to TinyMCE using an array()
'quicktags' => true // load Quicktags, can be used to pass settings directly to Quicktags using an array()
),*/
));
$cmb_group->add_group_field($group_field_id, array(
'name' => __('Link', 'cmb2'),
'id' => $this->getMetas()->getBoxesMetas()->link,
'type' => 'text_url' ,
'attributes'=>array('style'=>'width:99%')
));
$cmb_group->add_group_field($group_field_id, array(
'name' => __('Abrir link', 'cmb2'),
'id' => $this->getMetas()->getBoxesMetas()->linkTarget,
'type' => 'select',
'options' => array(
'_self' => 'Na mesma aba', // Hide the text input for the url
'_blank' => 'Em nova aba', // Hide the text input for the url
),
'attributes'=>array('style'=>'width:99%')
));
$cmb_group->add_group_field($group_field_id, array(
'name' => __('Texto do link', 'cmb2'),
'id' => $this->getMetas()->getBoxesMetas()->linkLabel,
'type' => 'text',
'attributes'=>array('style'=>'width:99%')
));
}
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 {
/**
*
* @var BoxesMetas
*/
public $boxesMetas;
public function __construct() {
$this->setBoxesMetas(new BoxesMetas());
}
function getBoxesMetas() {
return $this->boxesMetas;
}
function setBoxesMetas( BoxesMetas $boxesMetas ) {
$this->boxesMetas = $boxesMetas;
}
}
class BoxesMetas {
public $id = '_boxesmetas';
public $image = '_image';
public $title = '_title';
public $description = '_description';
public $link = '_link';
public $linkTarget = '_linktarget';
public $linkLabel = '_linklabel';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment