| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package RGC\CMB2\Field_Tooltip |
| 4 |
* @author Ruben Garcia (RubenGC) <[email protected]>, GamiPress <[email protected]> |
| 5 |
* @copyright Copyright (c) Tsunoa |
| 6 |
* |
| 7 |
* Plugin Name: CMB2 Field Tooltip |
| 8 |
* Plugin URI: https://github.com/rubengc/cmb2-field-tooltip |
| 9 |
* GitHub Plugin URI: https://github.com/rubengc/cmb2-field-tooltip |
| 10 |
* Description: Adds tooltips to CMB2 fields. |
| 11 |
* Version: 1.0.4 |
| 12 |
* Author: Tsunoa |
| 13 |
* Author URI: https://tsunoa.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_Field_Tooltip' ) ) { |
| 21 |
|
| 22 |
/** |
| 23 |
* Class RGC_CMB2_Field_Tooltip |
| 24 |
*/ |
| 25 |
class RGC_CMB2_Field_Tooltip { |
| 26 |
|
| 27 |
/** |
| 28 |
* Current version number |
| 29 |
*/ |
| 30 |
const VERSION = '1.0.4'; |
| 31 |
|
| 32 |
/** |
| 33 |
* Initialize the plugin by hooking into CMB2 |
| 34 |
*/ |
| 35 |
public function __construct() { |
| 36 |
add_action( 'admin_enqueue_scripts', array( $this, 'setup_admin_scripts' ) ); |
| 37 |
|
| 38 |
// TODO: Find a way to add this content if field has parameter 'tooltip' => desc |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* @param array $field_args Current field args |
| 43 |
* @param CMB2_Field $field Current field object |
| 44 |
*/ |
| 45 |
public function label_cb( $field_args, $field ) { |
| 46 |
$field_args = $this->parse_field_args( $field_args, 'top' ); |
| 47 |
|
| 48 |
if( empty( $field_args['tooltip']['desc'] ) ) { |
| 49 |
if( isset( $field_args['name'] ) ) { |
| 50 |
?> |
| 51 |
<label for="<?php echo esc_attr( $field_args['id'] ); ?>"><?php echo $field_args['name']; ?></label> |
| 52 |
<?php |
| 53 |
} |
| 54 |
|
| 55 |
return; |
| 56 |
} |
| 57 |
|
| 58 |
?> |
| 59 |
<label for="<?php echo esc_attr( $field_args['id'] ); ?>"><?php echo $field_args['name']; ?> |
| 60 |
<?php $this->tooltip_html( $field_args['tooltip']['desc'], $field_args['tooltip']['icon'], $field_args['tooltip']['position'] ); ?> |
| 61 |
</label> |
| 62 |
<?php |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* @param array $field_args Current field args |
| 67 |
* @param CMB2_Field $field Current field object |
| 68 |
*/ |
| 69 |
public function after_field( $field_args, $field ) { |
| 70 |
$field_args = $this->parse_field_args( $field_args, 'top' ); |
| 71 |
|
| 72 |
if( empty( $field_args['tooltip']['desc'] ) ) { |
| 73 |
return; |
| 74 |
} |
| 75 |
|
| 76 |
$this->tooltip_html( $field_args['tooltip']['desc'], $field_args['tooltip']['icon'], $field_args['tooltip']['position'] ); |
| 77 |
} |
| 78 |
|
| 79 |
public function tooltip_html( $desc, $icon = 'cmb-tooltip', $position = 'top' ) { |
| 80 |
?> |
| 81 |
<span class="cmb-tooltip"> |
| 82 |
<span class="dashicons dashicons-<?php echo esc_attr( $icon ); ?>"></span> |
| 83 |
<span class="cmb-tooltip-desc cmb-tooltip-<?php echo esc_attr( $position ); ?>"><?php echo $desc; ?></span> |
| 84 |
</span> |
| 85 |
<?php |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* @param array $field_args Current field args |
| 90 |
* @return array |
| 91 |
*/ |
| 92 |
private function parse_field_args( $field_args, $position = 'top' ) { |
| 93 |
|
| 94 |
$defaults = array( |
| 95 |
'icon' => 'cmb-tooltip', |
| 96 |
'position' => $position, |
| 97 |
'desc' => '', |
| 98 |
); |
| 99 |
|
| 100 |
// Support for 'tooltip' => 'Content' |
| 101 |
if( isset( $field_args['tooltip'] ) && ! is_array( $field_args['tooltip'] ) && ! empty( $field_args['tooltip'] ) ) { |
| 102 |
$defaults['desc'] = $field_args['tooltip']; |
| 103 |
|
| 104 |
// Turn into array( 'icon', 'desc' ) |
| 105 |
$field_args['tooltip'] = $defaults; |
| 106 |
} |
| 107 |
|
| 108 |
$field_args['tooltip'] = array_merge( |
| 109 |
$defaults, |
| 110 |
( ( isset( $field_args['tooltip'] ) && is_array( $field_args['tooltip'] ) ) ? $field_args['tooltip'] : array() ) |
| 111 |
); |
| 112 |
|
| 113 |
return $field_args; |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Enqueue scripts and styles |
| 118 |
*/ |
| 119 |
public function setup_admin_scripts() { |
| 120 |
wp_enqueue_style( 'cmb-tooltip', plugins_url( 'tooltip.css', __FILE__ ), array(), self::VERSION ); |
| 121 |
} |
| 122 |
|
| 123 |
} |
| 124 |
|
| 125 |
// TODO: Temporal solution to output html content |
| 126 |
function cmb_tooltip_label_cb( $field_args, $field ) { |
| 127 |
$cmb2_field_tooltip = new RGC_CMB2_Field_Tooltip(); |
| 128 |
|
| 129 |
$cmb2_field_tooltip->label_cb( $field_args, $field ); |
| 130 |
} |
| 131 |
|
| 132 |
function cmb_tooltip_after_field( $field_args, $field ) { |
| 133 |
$cmb2_field_tooltip = new RGC_CMB2_Field_Tooltip(); |
| 134 |
|
| 135 |
$cmb2_field_tooltip->after_field( $field_args, $field ); |
| 136 |
} |
| 137 |
|
| 138 |
function cmb_tooltip_html( $desc, $icon = 'cmb-tooltip', $position = 'top' ) { |
| 139 |
|
| 140 |
$cmb2_field_tooltip = new RGC_CMB2_Field_Tooltip(); |
| 141 |
|
| 142 |
$cmb2_field_tooltip->tooltip_html( $desc, $icon, $position ); |
| 143 |
|
| 144 |
} |
| 145 |
|
| 146 |
function cmb_tooltip_get_html( $desc, $icon = 'cmb-tooltip', $position = 'top' ) { |
| 147 |
|
| 148 |
$cmb2_field_tooltip = new RGC_CMB2_Field_Tooltip(); |
| 149 |
|
| 150 |
ob_start(); |
| 151 |
$cmb2_field_tooltip->tooltip_html( $desc, $icon, $position ); |
| 152 |
$output = ob_get_clean(); |
| 153 |
|
| 154 |
return $output; |
| 155 |
|
| 156 |
} |
| 157 |
|
| 158 |
$cmb2_field_tooltip = new RGC_CMB2_Field_Tooltip(); |
| 159 |
} |