| @@ -1,0 +1,132 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * @package RGC\CMB2\Tabs | |
| 4 | + * @author Ruben Garcia (RubenGC) <[email protected]>, GamiPress <[email protected]>, AutomatorWP <[email protected]> | |
| 5 | + * @copyright Copyright (c) Ruben Garcia | |
| 6 | + * | |
| 7 | + * Plugin Name: CMB2 Tabs | |
| 8 | + * Plugin URI: https://github.com/rubengc/cmb2-tabs | |
| 9 | + * GitHub Plugin URI: https://github.com/rubengc/cmb2-tabs | |
| 10 | + * Description: Tabs for CMB2 boxes. | |
| 11 | + * Version: 1.0.5 | |
| 12 | + * Author: Ruben Garcia | |
| 13 | + * Author URI: http://rubengc.com/ | |
| 14 | + * License: GPLv2+ | |
| 15 | + */ | |
| 16 | +// Exit if accessed directly | |
| 17 | +if( !defined( 'ABSPATH' ) ) exit; | |
| 18 | + | |
| 19 | +// Prevent CMB2 autoload adding "RGC_" at start | |
| 20 | +if( !class_exists( 'RGC_CMB2_Tabs' ) ) { | |
| 21 | + /** | |
| 22 | + * Class RGC_CMB2_Tabs | |
| 23 | + */ | |
| 24 | + class RGC_CMB2_Tabs { | |
| 25 | + | |
| 26 | + /** | |
| 27 | + * Current version number | |
| 28 | + */ | |
| 29 | + const VERSION = '1.0.5'; | |
| 30 | + | |
| 31 | + /** | |
| 32 | + * Initialize the plugin by hooking into CMB2 | |
| 33 | + */ | |
| 34 | + public function __construct() { | |
| 35 | + add_action( 'admin_enqueue_scripts', array( $this, 'setup_admin_scripts' ) ); | |
| 36 | + add_action( 'doing_dark_mode', array( $this, 'setup_dark_mode' ) ); | |
| 37 | + add_action( 'cmb2_before_form', array( $this, 'before_form' ), 10, 4 ); | |
| 38 | + add_action( 'cmb2_after_form', array( $this, 'after_form' ), 10, 4 ); | |
| 39 | + } | |
| 40 | + | |
| 41 | + /** | |
| 42 | + * Render tabs | |
| 43 | + * | |
| 44 | + * @param array $cmb_id The current box ID | |
| 45 | + * @param int $object_id The ID of the current object | |
| 46 | + * @param string $object_type The type of object you are working with. | |
| 47 | + * @param array $cmb This CMB2 object | |
| 48 | + */ | |
| 49 | + public function before_form( $cmb_id, $object_id, $object_type, $cmb ) { | |
| 50 | + if( $cmb->prop( 'tabs' ) && is_array( $cmb->prop( 'tabs' ) ) ) : ?> | |
| 51 | + <div class="cmb-tabs-wrap cmb-tabs-<?php echo ( ( $cmb->prop( 'vertical_tabs' ) ) ? 'vertical' : 'horizontal' ) ?>"> | |
| 52 | + <div class="cmb-tabs" data-speed="<?php echo ( ( $cmb->prop( 'tabs_speed' ) !== null ) ? $cmb->prop( 'tabs_speed' ) : 'fast' ) ?>"> | |
| 53 | + | |
| 54 | + <?php foreach( $cmb->prop( 'tabs' ) as $tab ) : | |
| 55 | + $fields_selector = array(); | |
| 56 | + | |
| 57 | + if( ! isset( $tab['id'] ) ) { | |
| 58 | + continue; | |
| 59 | + } | |
| 60 | + | |
| 61 | + if( ! isset( $tab['fields'] ) ) { | |
| 62 | + $tab['fields'] = array(); | |
| 63 | + } | |
| 64 | + | |
| 65 | + if( ! is_array( $tab['fields'] ) ) { | |
| 66 | + $tab['fields'] = array(); | |
| 67 | + } | |
| 68 | + | |
| 69 | + foreach( $tab['fields'] as $tab_field ) : | |
| 70 | + $fields_selector[] = '.' . 'cmb2-id-' . str_replace( '_', '-', sanitize_html_class( $tab_field ) ) . ':not(.cmb2-tab-ignore)'; | |
| 71 | + endforeach; | |
| 72 | + | |
| 73 | + $fields_selector = apply_filters( 'cmb2_tabs_tab_fields_selector', $fields_selector, $tab, $cmb_id, $object_id, $object_type, $cmb ); | |
| 74 | + $fields_selector = apply_filters( 'cmb2_tabs_tab_' . $tab['id'] . '_fields_selector', $fields_selector, $tab, $cmb_id, $object_id, $object_type, $cmb ); | |
| 75 | + ?> | |
| 76 | + | |
| 77 | + <div id="<?php echo esc_attr( $cmb_id ) . '-tab-' . esc_attr( $tab['id'] ); ?>" class="cmb-tab" data-fields="<?php echo esc_attr( implode( ', ', $fields_selector ) ); ?>"> | |
| 78 | + | |
| 79 | + <?php if( isset( $tab['icon'] ) && ! empty( $tab['icon'] ) ) : | |
| 80 | + $tab['icon'] = strpos($tab['icon'], 'dashicons') !== false ? 'dashicons ' . $tab['icon'] : $tab['icon']?> | |
| 81 | + <span class="cmb-tab-icon"><i class="<?php echo esc_attr( $tab['icon'] ); ?>"></i></span> | |
| 82 | + <?php endif; ?> | |
| 83 | + | |
| 84 | + <?php if( isset( $tab['title'] ) && ! empty( $tab['title'] ) ) : ?> | |
| 85 | + <span class="cmb-tab-title"><?php echo $tab['title']; ?></span> | |
| 86 | + <?php endif; ?> | |
| 87 | + </div> | |
| 88 | + <?php endforeach; ?> | |
| 89 | + | |
| 90 | + </div> <!-- .cmb-tabs --> | |
| 91 | + <?php endif; | |
| 92 | + } | |
| 93 | + | |
| 94 | + /** | |
| 95 | + * Close tabs | |
| 96 | + * | |
| 97 | + * @param array $cmb_id The current box ID | |
| 98 | + * @param int $object_id The ID of the current object | |
| 99 | + * @param string $object_type The type of object you are working with. | |
| 100 | + * @param array $cmb This CMB2 object | |
| 101 | + */ | |
| 102 | + public function after_form( $cmb_id, $object_id, $object_type, $cmb ) { | |
| 103 | + if( $cmb->prop( 'tabs' ) && is_array( $cmb->prop( 'tabs' ) ) ) : ?> | |
| 104 | + </div> <!-- .cmb-tabs-wrap --> | |
| 105 | + <?php endif; | |
| 106 | + } | |
| 107 | + | |
| 108 | + /** | |
| 109 | + * Enqueue scripts and styles | |
| 110 | + */ | |
| 111 | + public function setup_admin_scripts() { | |
| 112 | + wp_register_script( 'cmb-tabs', plugins_url( 'js/tabs.js', __FILE__ ), array( 'jquery' ), self::VERSION, true ); | |
| 113 | + wp_enqueue_script( 'cmb-tabs' ); | |
| 114 | + | |
| 115 | + wp_enqueue_style( 'cmb-tabs', plugins_url( 'css/tabs.css', __FILE__ ), array(), self::VERSION ); | |
| 116 | + wp_enqueue_style( 'cmb-tabs' ); | |
| 117 | + | |
| 118 | + } | |
| 119 | + | |
| 120 | + /** | |
| 121 | + * Enqueue dark mode styles | |
| 122 | + */ | |
| 123 | + public function setup_dark_mode() { | |
| 124 | + wp_enqueue_style( 'cmb-tabs-dark-mode', plugins_url( 'css/dark-mode.css', __FILE__ ), array(), self::VERSION ); | |
| 125 | + wp_enqueue_style( 'cmb-tabs-dark-mode' ); | |
| 126 | + | |
| 127 | + } | |
| 128 | + | |
| 129 | + } | |
| 130 | + | |
| 131 | + $cmb2_tabs = new RGC_CMB2_Tabs(); | |
| 132 | +} | |