| 1 |
<?php |
| 2 |
/** |
| 3 |
* GamiPress Shortcode Class |
| 4 |
* |
| 5 |
* @package GamiPress\Shortcodes\Shortcode |
| 6 |
* @author GamiPress <contact@gamipress.com>, Ruben Garcia <rubengcdev@gmail.com> |
| 7 |
* @since 1.0.0 |
| 8 |
*/ |
| 9 |
// Exit if accessed directly |
| 10 |
if( !defined( 'ABSPATH' ) ) exit; |
| 11 |
|
| 12 |
class GamiPress_Shortcode { |
| 13 |
|
| 14 |
public $name = ''; |
| 15 |
public $description = ''; |
| 16 |
public $slug = ''; |
| 17 |
public $icon = ''; |
| 18 |
public $group = ''; |
| 19 |
public $output_callback = ''; |
| 20 |
public $tabs = array(); |
| 21 |
public $fields = array(); |
| 22 |
|
| 23 |
public function __construct( $slug, $args = array() ) { |
| 24 |
$this->slug = $slug; |
| 25 |
|
| 26 |
// Setup this shortcode's properties |
| 27 |
$this->_set_properties( $args ); |
| 28 |
|
| 29 |
// Register this shortcode with WP and GamiPress |
| 30 |
add_shortcode( $this->slug, $this->output_callback ); |
| 31 |
|
| 32 |
add_filter( 'gamipress_shortcodes', array( $this, 'register_shortcode' ) ); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Shortcode form with defaults. |
| 37 |
*/ |
| 38 |
public function show_form() { |
| 39 |
$cmb2 = $this->cmb2(); |
| 40 |
|
| 41 |
$cmb2->object_id( $this->slug ); |
| 42 |
|
| 43 |
CMB2_Hookup::enqueue_cmb_css(); |
| 44 |
CMB2_Hookup::enqueue_cmb_js(); |
| 45 |
|
| 46 |
$cmb2->show_form(); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Creates a new instance of CMB2 and adds the fields |
| 51 |
* |
| 52 |
* @since 1.0.0 |
| 53 |
* |
| 54 |
* @return CMB2 $cmb2 |
| 55 |
*/ |
| 56 |
public function cmb2( $saving = false ) { |
| 57 |
|
| 58 |
foreach( $this->tabs as $tab_id => $tab ) { |
| 59 |
// Generate the id of the tab based on shortcode slug |
| 60 |
$tab['id'] = $this->slug . '_' . $tab_id; |
| 61 |
|
| 62 |
foreach( $tab['fields'] as $tab_field_index => $tab_field ) { |
| 63 |
// Update the id of the tab field based on shortcode slug |
| 64 |
$tab['fields'][$tab_field_index] = $this->slug . '_' . $tab_field; |
| 65 |
} |
| 66 |
|
| 67 |
$this->tabs[$tab_id] = $tab; |
| 68 |
} |
| 69 |
|
| 70 |
// Create a new box to render the form |
| 71 |
$cmb2 = new CMB2( array( |
| 72 |
'id' => $this->slug .'_box', // Option name is taken from the WP_Widget class. |
| 73 |
'classes' => 'gamipress-form gamipress-shortcode-form', |
| 74 |
'tabs' => $this->tabs, |
| 75 |
'hookup' => false, |
| 76 |
'show_on' => array( |
| 77 |
'key' => 'options-page', // Tells CMB2 to handle this as an option |
| 78 |
'value' => array( $this->slug ) |
| 79 |
), |
| 80 |
), $this->slug ); |
| 81 |
|
| 82 |
foreach( $this->fields as $field_id => $field ) { |
| 83 |
|
| 84 |
// Set the id of the field based on shortcode slug |
| 85 |
$field['id'] = $this->slug . '_' . $field_id; |
| 86 |
|
| 87 |
// Loop through group fields |
| 88 |
if( $field['type'] === 'group' && ! empty( $field['fields'] ) ) { |
| 89 |
|
| 90 |
foreach( $field['fields'] as $group_field_id => $group_field ) { |
| 91 |
// Set the id of the group field based on shortcode slug |
| 92 |
$field['fields'][$group_field_id]['id'] = $this->slug . '_' . $group_field_id; |
| 93 |
} |
| 94 |
|
| 95 |
} |
| 96 |
|
| 97 |
$cmb2->add_field( $field ); |
| 98 |
} |
| 99 |
|
| 100 |
return $cmb2; |
| 101 |
} |
| 102 |
|
| 103 |
private function _set_properties( $args = array() ) { |
| 104 |
|
| 105 |
$args = wp_parse_args( $args, array( |
| 106 |
'name' => '', |
| 107 |
'description' => '', |
| 108 |
'icon' => 'gamipress', |
| 109 |
'group' => 'others', |
| 110 |
'output_callback' => '', |
| 111 |
'tabs' => array(), |
| 112 |
'fields' => array(), |
| 113 |
) ); |
| 114 |
|
| 115 |
$this->name = $args['name']; |
| 116 |
$this->description = $args['description']; |
| 117 |
$this->icon = $args['icon']; |
| 118 |
$this->group = $args['group']; |
| 119 |
$this->output_callback = $args['output_callback']; |
| 120 |
|
| 121 |
// Filter to register custom shortcode tabs |
| 122 |
$this->tabs = apply_filters( "gamipress_{$this->slug}_shortcode_tabs", $args['tabs'] ); |
| 123 |
|
| 124 |
// Filter to register custom shortcode fields |
| 125 |
$this->fields = apply_filters( "gamipress_{$this->slug}_shortcode_fields", $args['fields'] ); |
| 126 |
|
| 127 |
// Make some adjustments to fields attributes |
| 128 |
foreach( $this->fields as $field_id => $field ) { |
| 129 |
|
| 130 |
// Update tooltip position to left |
| 131 |
if( isset( $field['tooltip'] ) ) { |
| 132 |
|
| 133 |
if( ! is_array( $field['tooltip'] ) ) { |
| 134 |
// tooltip => description |
| 135 |
$field['tooltip'] = array( |
| 136 |
'desc' => $field['tooltip'], |
| 137 |
'position' => 'left', |
| 138 |
); |
| 139 |
} |
| 140 |
|
| 141 |
} |
| 142 |
|
| 143 |
$this->fields[$field_id] = $field; |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
public function register_shortcode( $shortcodes = array() ) { |
| 148 |
|
| 149 |
$shortcodes[ $this->slug ] = $this; |
| 150 |
|
| 151 |
return $shortcodes; |
| 152 |
|
| 153 |
} |
| 154 |
|
| 155 |
} |
| 156 |
|