Skip to content

Instantly share code, notes, and snippets.

@Gkiokan
Created January 30, 2017 09:23
Show Gist options
  • Save Gkiokan/fc33c6850d0eb40c20a19e72a1446409 to your computer and use it in GitHub Desktop.
Save Gkiokan/fc33c6850d0eb40c20a19e72a1446409 to your computer and use it in GitHub Desktop.
WP Shortcode Helper Class
<?php
/*
File: Button Controller
Date: today
Author: Gkiokan
Comment: A Shortcode Class Helper with VC integration
*/
if( ! defined( 'ABSPATH' ) ) exit;
new ExtendedButton(true);
class ExtendedButton {
/*
The Main Constructor for everything
*/
public function __construct($register=false) {
if($register)
$this->register();
}
/*
Register all Events here
*/
public function register(){
add_action('vc_before_init', [$this, 'register_vc']);
add_shortcode('ex_btn', [$this, 'shortcode']);
}
/*
Register Visual Composer Modules
*/
public function register_vc(){
if(!is_callable('vc_map')) return;
vc_map(array(
"name" => "Ex Button",
"base" => "ex_btn",
"category" => "idh",
"show_settings_on_create" => true,
'params' => [
// Title
[
"type" => "textfield",
"heading" => "Button Text",
"description" => "Button Text by Standby",
"param_name" => "title",
'admin_label' => true
],
// Link
[
"type" => "textfield",
"heading" => "Link",
"description" => "Target Link",
"param_name" => "url",
'group' => 'Target',
'admin_label' => true
],
// Target in new Link
[
"type" => "checkbox",
"heading" => "Tab",
"description" => "Link im neuen Tab öffnen?",
"param_name" => "new_window",
'group' => 'Target',
'admin_label' => true
],
// Front Color
[
'type' => 'colorpicker',
'heading' => 'Text Color ',
'description' => 'Textfarbe des Textes innerhalb des Buttons',
'param_name' => 'color',
'admin_label' => true,
],
// Back Color
[
'type' => 'colorpicker',
'heading' => 'Background StandBy',
'description' => 'Hintergrundfarbe im Standby',
'param_name' => 'bg_standby',
'admin_label' => true,
],
// Hover Back Color
[
'type' => 'colorpicker',
'heading' => 'Background Hover',
'description' => 'Hintergrundfarbe im Hover state',
'param_name' => 'bg_hover',
'admin_label' => true,
],
// Size
[
'type' => 'dropdown',
'heading' => 'Button Größe',
'description' => 'Wähle aus den zwei vordefinierten Button größen',
'param_name' => 'size',
'value' => [
'Standardgröße' => '1x',
'2x Größe' => '2x',
'3x Größe' => '3x',
]
]
]
));
}
/*
Controll the Output here
*/
public function shortcode($a=[], $c=''){
$title = $this->get('title', '', $a);
$bg_s = $this->get('bg_standby', '#dd7368', $a);
$bg_h = $this->get('bg_hover', '#00CBBE', $a);
$color = $this->get('color', '#fff', $a);
$size = 'size-' . $this->get('size', '1', $a);
$this->start();
echo "<div class='ex_btn $size'>";
echo "<div class='standby' style='background: $bg_s'></div>";
echo "<div class='hover' style='background: $bg_h'></div>";
echo "<div class='content' style='color: $color'> $title </div>";
echo "</div>";
return $this->stop();
}
/*
Easy getter
*/
public function get($item=null, $default='', $atts=null){
if(!$atts) $atts = $this->atts;
if(is_array($atts))
return !empty($atts[$item]) && isset($atts[$item]) ? esc_attr($atts[$item]) : $default;
}
/*
Helper function to start the ob cache
*/
public function start(){
ob_start();
}
/*
Helper function for getting the ob cache
*/
public function stop(){
$content = ob_get_contents();
ob_end_clean();
return $content;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment